From a8b0b26f99063312e2895b1e911d47096d21a371 Mon Sep 17 00:00:00 2001 From: driAn Date: Wed, 27 Aug 2008 22:57:06 +0000 Subject: [PATCH] rewrote footstep proc code, supports water footsteps (normal+crouch) git-svn-id: svn://svn.code.sf.net/p/actiongame/code/trunk/ac@2425 e016b1c5-ed68-44aa-a2f7-31cac88cffde --- source/src/sound.cpp | 120 +++++++++++++++++++++++++++++++++---------- source/src/sound.h | 9 ++-- 2 files changed, 97 insertions(+), 32 deletions(-) diff --git a/source/src/sound.cpp b/source/src/sound.cpp index 5a9c6d20..828fce51 100644 --- a/source/src/sound.cpp +++ b/source/src/sound.cpp @@ -21,7 +21,7 @@ static bool nosound = true; ALCdevice *device = NULL; ALCcontext *context = NULL; -void playsound(int n, const struct worldobjreference &r, int priority); +struct location *playsound(int n, const struct worldobjreference &r, int priority, float offset = 0.0f); void alclearerr() { @@ -213,6 +213,21 @@ struct source alGetSourcei(id, AL_SOURCE_STATE, &s); return s; } + + bool secoffset(float secs) + { + alclearerr(); + alSourcef(id, AL_SEC_OFFSET, secs); + return !alerr(); + } + + float secoffset() + { + alclearerr(); + ALfloat s; + alGetSourcef(id, AL_SEC_OFFSET, &s); + return s; + } bool playing() { @@ -1041,6 +1056,20 @@ struct location : sourceowner src->play(); } + void offset(float secs) + { + ASSERT(!stale); + if(stale) return; + src->secoffset(secs); + } + + float offset() + { + ASSERT(!stale); + if(stale) return 0.0f; + return src->secoffset(); + } + void drop() { src->stop(); @@ -1052,15 +1081,15 @@ struct locvector : vector { virtual ~locvector(){}; - int find(int sound, worldobjreference *ref = NULL, const vector &soundcollection = gamesounds) + location *find(int sound, worldobjreference *ref = NULL, const vector &soundcollection = gamesounds) { loopi(ulen) if(buf[i] && !buf[i]->stale) { if(buf[i]->cfg != &soundcollection[sound]) continue; // check if its the same sound if(ref && *(buf[i]->ref)!=*ref) continue; // optionally check if its the same reference - return i; // found + return buf[i]; // found } - return -1; + return NULL; } void delete_(int i) @@ -1387,39 +1416,75 @@ VAR(footsteps, 0, 1, 1); float lastpitch = 1.0f; -void updateplayerfootsteps(playerent *p, int sound) +void updateplayerfootsteps(playerent *p) { if(!p) return; const int footstepradius = 16; + static int lastfootsteps = 0; + static float lastoffset = 0; + + // find existing footstep sounds physentreference ref(p); - int locid = locations.find(sound, &ref); - location *loc = locid < 0 ? NULL : locations[locid]; + location *locs[] = { + locations.find(S_FOOTSTEPS, &ref), + locations.find(S_FOOTSTEPSCROUCH, &ref), + locations.find(S_WATERFOOTSTEPS, &ref), + locations.find(S_WATERFOOTSTEPSCROUCH, &ref) + }; + bool local = (p == camera1); - bool inrange = (local || (camera1->o.dist(p->o) < footstepradius && footsteps)); - bool nosteps = (!footsteps || p->state != CS_ALIVE || lastmillis-p->lastpain < 300 || (!p->onfloor && p->timeinair>50) || (!p->move && !p->strafe) || (sound==S_FOOTSTEPS && p->crouching) || (sound==S_FOOTSTEPSCROUCH && !p->crouching) || p->inwater); - if(inrange && !nosteps) + if(!footsteps || !inrange || p->state != CS_ALIVE || lastmillis-p->lastpain < 300 || (!p->onfloor && p->timeinair>50) || (!p->move && !p->strafe) || p->inwater) { - if(!loc) playsound(sound, p, local ? SP_HIGH : SP_LOW); + // no footsteps + if(lastfootsteps>0 && lastmillis-lastfootsteps>100) // stop sound after a short delay + { + loopi(sizeof(locs)/sizeof(locs[0])) + { + location *l = locs[i]; + if(!l) continue; + lastoffset = l->offset(); // save last offset + l->drop(); + } + } } - else if(loc) + else { - locations.delete_(locid); + // play footsteps + bool water = p->o.z-p->dyneyeheight()crouching) stepsound = water ? S_WATERFOOTSTEPSCROUCH : S_FOOTSTEPSCROUCH; // crouch + else stepsound = water ? S_WATERFOOTSTEPS : S_FOOTSTEPS; // normal + + + // proc existing sounds + bool isplaying = false; + loopi(sizeof(locs)/sizeof(locs[0])) + { + location *l = locs[i]; + if(!l) continue; + if(i+S_FOOTSTEPS==stepsound) isplaying = true; // already playing + else l->drop(); // different footstep sound, drop it + } + + if(!isplaying) + { + // play using existing offset, if available + playsound(stepsound, ref, local ? SP_HIGH : SP_LOW, lastoffset>0.01f ? lastoffset : 0.0f); + } + + lastfootsteps = lastmillis; } } void updateloopsound(int sound, bool active, float vol = 1.0f) { if(camera1->type != ENT_PLAYER) return; - int locid = locations.find(sound); - location *l = locid < 0 ? NULL : locations[locid]; + location *l = locations.find(sound); if(!l && active) playsound(sound, SP_HIGH); - else if(l && !active) - { - locations.delete_(locid); - } + else if(l && !active) l->drop(); if(l && vol != 1.0f) l->src->gain(vol); } @@ -1433,14 +1498,12 @@ void updateaudio() bool firstperson = camera1->type==ENT_PLAYER; // footsteps - updateplayerfootsteps(player1, S_FOOTSTEPS); - updateplayerfootsteps(player1, S_FOOTSTEPSCROUCH); + updateplayerfootsteps(player1); loopv(players) { playerent *p = players[i]; if(!p) continue; - updateplayerfootsteps(p, S_FOOTSTEPS); - updateplayerfootsteps(p, S_FOOTSTEPSCROUCH); + updateplayerfootsteps(p); } // water @@ -1474,8 +1537,7 @@ void updateaudio() entityreference entref(&e); // search existing sound loc - int locidx = locations.find(sound, &entref, mapsounds); - location *loc = locidx>=0 ? locations[locidx] : NULL; + location *loc = locations.find(sound, &entref, mapsounds); if(hearable && !loc) // play { @@ -1522,9 +1584,9 @@ void updateaudio() VARP(maxsoundsatonce, 0, 10, 100); -void playsound(int n, const worldobjreference &r, int priority) +location *playsound(int n, const worldobjreference &r, int priority, float offset) { - if(nosound || !soundvol) return; + if(nosound || !soundvol) return NULL; bool loop = false; @@ -1538,15 +1600,17 @@ void playsound(int n, const worldobjreference &r, int priority) if(maxsoundsatonce && soundsatonce>maxsoundsatonce) { if(audiodebug) conoutf("sound %d filtered by maxsoundsatonce (soundsatonce %d)", n, soundsatonce); - return; + return NULL; } } location *loc = new location(n, r, priority); locations.add(loc); + if(offset>0) loc->offset(offset); loc->play(loop); if(audiodebug) conoutf("played sound no %d with prio %d", n, priority); + return loc; } void playsound(int n, int priority) { playsound(n, camerareference(), priority); }; diff --git a/source/src/sound.h b/source/src/sound.h index 6bbb4e91..a44d4214 100644 --- a/source/src/sound.h +++ b/source/src/sound.h @@ -13,15 +13,16 @@ enum S_ITEMAMMO, S_ITEMHEALTH, S_ITEMARMOUR, S_ITEMAKIMBO, S_NOAMMO, S_AKIMBOOUT, - S_PAIN1, S_PAIN2, S_PAIN3, S_PAIN4, S_PAIN5, S_PAIN6, //24 + S_PAIN1, S_PAIN2, S_PAIN3, S_PAIN4, S_PAIN5, S_PAIN6, S_DIE1, S_DIE2, S_FEXPLODE, S_SPLASH1, S_SPLASH2, S_FLAGDROP, S_FLAGPICKUP, S_FLAGRETURN, S_FLAGSCORE, - S_GRENADEPULL, S_GRENADETHROW, S_GRENADEBOUNCE1, S_GRENADEBOUNCE2, - S_RAKIMBO, S_GUNCHANGE, S_GIB, S_HEADSHOT, + S_GRENADEPULL, S_GRENADETHROW, S_GRENADEBOUNCE1, S_GRENADEBOUNCE2, S_RAKIMBO, S_WATERIMPACT, + S_GUNCHANGE, + S_GIB, S_HEADSHOT, S_CALLVOTE, S_VOTEPASS, S_VOTEFAIL, - S_FOOTSTEPS, S_FOOTSTEPSCROUCH, + S_FOOTSTEPS, S_FOOTSTEPSCROUCH, S_WATERFOOTSTEPS, S_WATERFOOTSTEPSCROUCH, S_MENUSELECT, S_MENUENTER, S_UNDERWATER, S_TINNITUS,