OctaCore/src/game/game.cc

504 lines
12 KiB
C++

#include "game.hh"
namespace game
{
int maptime = 0, maprealtime = 0, maplimit = -1;
int lasthit = 0, lastspawnattempt = 0;
gameent *player1 = NULL; // our client
vector<gameent *> players; // other clients
int following = -1;
VARFP(specmode, 0, 0, 2,
{
if(!specmode) stopfollowing();
else if(following < 0) nextfollow();
});
gameent *followingplayer(gameent *fallback)
{
if(player1->state!=CS_SPECTATOR || following<0) return fallback;
gameent *target = getclient(following);
if(target && target->state!=CS_SPECTATOR) return target;
return fallback;
}
ICOMMAND(getfollow, "", (),
{
gameent *f = followingplayer();
intret(f ? f->clientnum : -1);
});
void stopfollowing()
{
if(following<0) return;
following = -1;
}
void follow(char *arg)
{
}
COMMAND(follow, "s");
void nextfollow(int dir)
{
if(player1->state!=CS_SPECTATOR) return;
int cur = following >= 0 ? following : (dir < 0 ? clients.length() - 1 : 0);
loopv(clients)
{
cur = (cur + dir + clients.length()) % clients.length();
if(clients[cur] && clients[cur]->state!=CS_SPECTATOR)
{
following = cur;
return;
}
}
stopfollowing();
}
ICOMMAND(nextfollow, "i", (int *dir), nextfollow(*dir < 0 ? -1 : 1));
void checkfollow()
{
if(player1->state != CS_SPECTATOR)
{
if(following >= 0) stopfollowing();
}
else
{
if(following >= 0)
{
gameent *d = clients.inrange(following) ? clients[following] : NULL;
if(!d || d->state == CS_SPECTATOR) stopfollowing();
}
if(following < 0 && specmode) nextfollow();
}
}
const char *getclientmap() { return clientmap; }
void resetgamestate()
{
}
gameent *spawnstate(gameent *d) // reset player state not persistent accross spawns
{
d->respawn();
d->spawnstate(0);
return d;
}
void respawnself()
{
if(ispaused()) return;
spawnplayer(player1);
lasthit = 0;
if(cmode) cmode->respawned(player1);
}
gameent *pointatplayer()
{
return NULL;
}
gameent *hudplayer()
{
if((thirdperson && allowthirdperson()) || specmode > 1) return player1;
return followingplayer(player1);
}
void setupcamera()
{
gameent *target = followingplayer();
if(target)
{
player1->yaw = target->yaw;
player1->pitch = target->state==CS_DEAD ? 0 : target->pitch;
player1->o = target->o;
player1->resetinterp();
}
}
bool allowthirdperson()
{
return !multiplayer(false) || player1->state==CS_SPECTATOR || player1->state==CS_EDITING || m_edit;
}
bool detachcamera()
{
gameent *d = followingplayer();
if(d) return specmode > 1 || d->state == CS_DEAD;
return player1->state == CS_DEAD;
}
bool collidecamera()
{
switch(player1->state)
{
case CS_EDITING: return false;
case CS_SPECTATOR: return followingplayer()!=NULL;
}
return true;
}
VARP(smoothmove, 0, 75, 100);
VARP(smoothdist, 0, 32, 64);
void predictplayer(gameent *d, bool move)
{
d->o = d->newpos;
d->yaw = d->newyaw;
d->pitch = d->newpitch;
d->roll = d->newroll;
if(move)
{
moveplayer(d, 1, false);
d->newpos = d->o;
}
float k = 1.0f - float(lastmillis - d->smoothmillis)/smoothmove;
if(k>0)
{
d->o.add(vec(d->deltapos).mul(k));
d->yaw += d->deltayaw*k;
if(d->yaw<0) d->yaw += 360;
else if(d->yaw>=360) d->yaw -= 360;
d->pitch += d->deltapitch*k;
d->roll += d->deltaroll*k;
}
}
void otherplayers(int curtime)
{
}
void updateworld() // main game update loop
{
if(!maptime) { maptime = lastmillis; maprealtime = totalmillis; return; }
if(!curtime) { gets2c(); if(player1->clientnum>=0) c2sinfo(); return; }
physicsframe();
otherplayers(curtime);
moveragdolls();
gets2c();
if(connected)
{
crouchplayer(player1, 10, true);
moveplayer(player1, 10, true);
entities::checkitems(player1);
if(cmode) cmode->checkitems(player1);
}
if(player1->clientnum>=0) c2sinfo(); // do this last, to reduce the effective frame lag
}
void spawnplayer(gameent *d) // place at random spawn
{
if(cmode) cmode->pickspawn(d);
else findplayerspawn(d, -1, 0);
spawnstate(d);
if(d==player1)
{
if(editmode) d->state = CS_EDITING;
else if(d->state != CS_SPECTATOR) d->state = CS_ALIVE;
}
else d->state = CS_ALIVE;
checkfollow();
}
VARP(spawnwait, 0, 0, 1000);
void respawn()
{
if(player1->state==CS_DEAD)
{
respawnself();
}
}
COMMAND(respawn, "");
// inputs
VARP(jumpspawn, 0, 1, 1);
bool canjump()
{
if(!connected) return false;
if(jumpspawn) respawn();
return player1->state!=CS_DEAD;
}
bool cancrouch()
{
if(!connected) return false;
return player1->state!=CS_DEAD;
}
bool allowmove(physent *d)
{
return true;
}
void damaged(int damage, gameent *d, gameent *actor, bool local)
{
}
void deathstate(gameent *d, bool restore)
{
d->state = CS_DEAD;
if(d==player1)
{
disablezoom();
//d->pitch = 0;
d->roll = 0;
playsound(S_DIE2);
}
else
{
d->move = d->strafe = 0;
d->resetinterp();
d->smoothmillis = 0;
playsound(S_DIE1, &d->o);
}
}
VARP(teamcolorfrags, 0, 1, 1);
void killed(gameent *d, gameent *actor)
{
if(d->state==CS_EDITING)
{
d->editstate = CS_DEAD;
if(d!=player1) d->resetinterp();
return;
}
else if(d->state!=CS_ALIVE && d->state != CS_LAGGED && d->state != CS_SPAWNING) return;
deathstate(d);
}
void timeupdate(int secs)
{
if(secs > 0)
{
maplimit = lastmillis + secs*1000;
}
}
vector<gameent *> clients;
gameent *newclient(int cn) // ensure valid entity
{
if(cn < 0 || cn > max(0xFF, MAXCLIENTS))
{
neterr("clientnum", false);
return NULL;
}
if(cn == player1->clientnum) return player1;
while(cn >= clients.length()) clients.add(NULL);
if(!clients[cn])
{
gameent *d = new gameent;
d->clientnum = cn;
clients[cn] = d;
players.add(d);
}
return clients[cn];
}
gameent *getclient(int cn) // ensure valid entity
{
if(cn == player1->clientnum) return player1;
return clients.inrange(cn) ? clients[cn] : NULL;
}
void clientdisconnected(int cn, bool notify)
{
if(!clients.inrange(cn)) return;
gameent *d = clients[cn];
if(d)
{
if(notify && d->name[0]) conoutf("\f4leave:\f7 %s", colorname(d));
removetrackedparticles(d);
removetrackeddynlights(d);
if(cmode) cmode->removeplayer(d);
players.removeobj(d);
DELETEP(clients[cn]);
cleardynentcache();
}
if(following == cn)
{
if(specmode) nextfollow();
else stopfollowing();
}
}
void clearclients(bool notify)
{
loopv(clients) if(clients[i]) clientdisconnected(i, notify);
}
void initclient()
{
player1 = spawnstate(new gameent);
filtertext(player1->name, "unnamed", false, false, MAXNAMELEN);
players.add(player1);
}
VARP(showmodeinfo, 0, 1, 1);
void startgame()
{
// reset perma-state
loopv(players) players[i]->startgame();
setclientmode();
maptime = maprealtime = 0;
maplimit = -1;
if(cmode)
{
cmode->preload();
cmode->setup();
}
syncplayer();
disablezoom();
lasthit = 0;
execident("mapstart");
}
void startmap(const char *name) // called just after a map load
{
spawnplayer(player1);
entities::resetspawns();
copystring(clientmap, name ? name : "");
sendmapinfo();
}
const char *getmapinfo()
{
return NULL;
}
const char *getscreenshotinfo()
{
return NULL;
}
void physicstrigger(physent *d, bool local, int floorlevel, int waterlevel, int material)
{
if (waterlevel>0) { if(material!=MAT_LAVA) playsound(S_SPLASHOUT, d==player1 ? NULL : &d->o); }
else if(waterlevel<0) playsound(material==MAT_LAVA ? S_BURN : S_SPLASHIN, d==player1 ? NULL : &d->o);
if (floorlevel>0) { if(d==player1 || d->type!=ENT_PLAYER) msgsound(S_JUMP, d); }
else if(floorlevel<0) { if(d==player1 || d->type!=ENT_PLAYER) msgsound(S_LAND, d); }
}
void dynentcollide(physent *d, physent *o, const vec &dir)
{
}
void msgsound(int n, physent *d)
{
if(!d || d==player1)
{
addmsg(N_SOUND, "ci", d, n);
playsound(n);
}
else
{
playsound(n, &d->o);
}
}
int numdynents() { return players.length(); }
dynent *iterdynents(int i)
{
if(i<players.length()) return players[i];
return NULL;
}
bool duplicatename(gameent *d, const char *name = NULL, const char *alt = NULL)
{
if(!name) name = d->name;
if(alt && d != player1 && !strcmp(name, alt)) return true;
loopv(players) if(d!=players[i] && !strcmp(name, players[i]->name)) return true;
return false;
}
const char *colorname(gameent *d, const char *name, const char * alt, const char *color)
{
if(!name) name = alt && d == player1 ? alt : d->name;
return name;
}
bool needminimap() { return false; }
void drawicon(int icon, float x, float y, float sz)
{
}
float abovegameplayhud(int w, int h)
{
return 1;
}
void drawhudicons(gameent *d)
{
}
void gameplayhud(int w, int h)
{
pushhudscale(h/1800.0f);
gameent *d = hudplayer();
if(d->state!=CS_EDITING)
{
if(d->state!=CS_SPECTATOR) drawhudicons(d);
if(cmode) cmode->drawhud(d, w, h);
}
pophudmatrix();
}
float clipconsole(float w, float h)
{
if(cmode) return cmode->clipconsole(w, h);
return 0;
}
const char *defaultcrosshair(int index)
{
return "media/interface/crosshair/default.png";
}
int selectcrosshair(vec &)
{
return 0;
}
// any data written into this vector will get saved with the map data. Must take care to do own versioning, and endianess if applicable. Will not get called when loading maps from other games, so provide defaults.
void writegamedata(vector<char> &extras) {}
void readgamedata(vector<char> &extras) {}
const char *gameconfig() { return "config/game.cfg"; }
const char *savedconfig() { return "config/saved.cfg"; }
const char *restoreconfig() { return "config/restore.cfg"; }
const char *defaultconfig() { return "config/default.cfg"; }
const char *autoexec() { return "config/autoexec.cfg"; }
const char *savedservers() { return "config/servers.cfg"; }
void loadconfigs()
{
execfile("config/auth.cfg", false);
}
bool clientoption(const char *arg) { return false; }
}