From cbf49832194abba7ab9d889d0ee118b864bc0fa4 Mon Sep 17 00:00:00 2001 From: Lee Salzman Date: Tue, 29 Jul 2008 22:42:07 +0000 Subject: [PATCH] movement smoothing fix git-svn-id: svn://svn.code.sf.net/p/actiongame/code/trunk/ac@2255 e016b1c5-ed68-44aa-a2f7-31cac88cffde --- source/src/clientgame.cpp | 40 ++++++++++++++++++++++----------------- source/src/clients2c.cpp | 9 +++++++-- source/src/entity.h | 4 ++-- source/src/protos.h | 1 + 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/source/src/clientgame.cpp b/source/src/clientgame.cpp index dbc9b587..f42bd0ef 100644 --- a/source/src/clientgame.cpp +++ b/source/src/clientgame.cpp @@ -228,6 +228,28 @@ void movelocalplayer() VARP(smoothmove, 0, 75, 100); VARP(smoothdist, 0, 8, 16); +void predictplayer(playerent *d, bool move) +{ + d->o = d->newpos; + d->yaw = d->newyaw; + d->pitch = d->newpitch; + 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->lastpredict = lastmillis; +} + void moveotherplayers() { loopv(players) if(players[i] && players[i]->type==ENT_PLAYER) @@ -242,23 +264,7 @@ void moveotherplayers() } if(d->state==CS_ALIVE || d->state==CS_EDITING) { - if(smoothmove && d->smoothmillis>0) - { - d->o = d->newpos; - d->yaw = d->newyaw; - d->pitch = d->newpitch; - 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; - } - } + if(smoothmove && d->smoothmillis>0) predictplayer(d, true); else moveplayer(d, 1, false); } else if(d->state==CS_DEAD && lastmillis-d->lastpain<2000) moveplayer(d, 1, true); diff --git a/source/src/clients2c.cpp b/source/src/clients2c.cpp index 3cb47cd0..f7249998 100644 --- a/source/src/clients2c.cpp +++ b/source/src/clients2c.cpp @@ -80,6 +80,8 @@ void parsepositions(ucharbuf &p) int f = getuint(p), seqcolor = (f>>6)&1; playerent *d = getclient(cn); if(!d || seqcolor!=(d->lifesequence&1)) continue; + extern int smoothmove, smoothdist; + if(smoothmove && d->smoothmillis>=0 && d->lastpredict < lastmillis) predictplayer(d, false); vec oldpos(d->o); float oldyaw = d->yaw, oldpitch = d->pitch; d->o = o; @@ -98,7 +100,6 @@ void parsepositions(ucharbuf &p) updatecrouch(d, f&1); updatepos(d); updatelagtime(d); - extern int smoothmove, smoothdist; if(d->state==CS_DEAD) { d->resetinterp(); @@ -717,7 +718,11 @@ void receivefile(uchar *data, int len) s_sprintfd(fname)("demos/%d.dmo", now_utc); //lastmillis); path(fname); FILE *demo = openfile(fname, "wb"); - if(!demo) return; + if(!demo) + { + conoutf("failed writing to \"%s\"", fname); + return; + } conoutf("received demo \"%s\"", fname); fwrite(data, 1, len, demo); fclose(demo); diff --git a/source/src/entity.h b/source/src/entity.h index f7ef9a42..8b3b3222 100644 --- a/source/src/entity.h +++ b/source/src/entity.h @@ -339,7 +339,7 @@ struct playerstate struct playerent : dynent, playerstate { - int clientnum, lastupdate, plag, ping; + int clientnum, lastupdate, lastpredict, plag, ping; int lifesequence; // sequence id for each respawn, used in damage test int frags, flagscore, deaths; int lastaction, lastmove, lastpain, lastvoicecom; @@ -362,7 +362,7 @@ struct playerent : dynent, playerstate float deltayaw, deltapitch, newyaw, newpitch; int smoothmillis; - playerent() : clientnum(-1), plag(0), ping(0), lifesequence(0), frags(0), flagscore(0), deaths(0), lastpain(0), lastvoicecom(0), clientrole(CR_DEFAULT), + playerent() : clientnum(-1), lastupdate(0), lastpredict(0), plag(0), ping(0), lifesequence(0), frags(0), flagscore(0), deaths(0), lastpain(0), lastvoicecom(0), clientrole(CR_DEFAULT), skin(0), nextskin(0), spectatemode(SM_NONE), followplayercn(-1), eardamagemillis(0), weaponsel(NULL), nextweaponsel(NULL), primweap(NULL), nextprimweap(NULL), lastattackweapon(NULL), smoothmillis(-1) diff --git a/source/src/protos.h b/source/src/protos.h index 8564d86e..024f1fb3 100644 --- a/source/src/protos.h +++ b/source/src/protos.h @@ -293,6 +293,7 @@ extern char *getclientmap(); extern void zapplayer(playerent *&d); extern playerent *getclient(int cn); extern playerent *newclient(int cn); +extern void predictplayer(playerent *d, bool move); extern void timeupdate(int timeremain); extern void respawnself(); extern void setskin(playerent *pl, uint skin);