From 8aca0774dcb181c33a80165d27adbaa30804cbdd Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 17 Oct 2020 09:26:52 +0200 Subject: [PATCH] - adding more helper wrappers. --- source/games/duke/src/dukeactor.h | 158 ++++++++++++++++++++++++++++++ source/games/duke/src/types.h | 46 +++++++++ 2 files changed, 204 insertions(+) diff --git a/source/games/duke/src/dukeactor.h b/source/games/duke/src/dukeactor.h index 6c936306a..a88b75467 100644 --- a/source/games/duke/src/dukeactor.h +++ b/source/games/duke/src/dukeactor.h @@ -46,5 +46,163 @@ public: } }; +// An interator to iterate over all sprites. +class DukeSpriteIterator +{ + DukeStatIterator it; + int stat = STAT_DEFAULT; + +public: + DukeSpriteIterator() : it(STAT_DEFAULT) {} + + DDukeActor* Next() + { + while (stat < MAXSTATUS) + { + auto ac = it.Next(); + if (ac) return ac; + stat++; + if (stat < MAXSTATUS) it.Reset(stat); + } + return nullptr; + } +}; + + +inline DDukeActor* player_struct::GetActor() +{ + return &hittype[i]; +} + +inline int player_struct::GetPlayerNum() +{ + return GetActor()->s.yvel; +} + +// Refactoring helpers/intermediates +inline void changespritestat(DDukeActor* a, int newstat) +{ + ::changespritestat(a->GetIndex(), newstat); +} + +// The int version also needs to be wrapped due to namespacing issues. +inline void changespritestat(int i, int newstat) +{ + ::changespritestat(i, newstat); +} + +inline void changespritesect(DDukeActor* a, int newsect) +{ + ::changespritesect(a->GetIndex(), newsect); +} + +inline void changespritesect(int i, int newsect) +{ + ::changespritesect(i, newsect); +} + +inline void deletesprite(DDukeActor* i) +{ + deletesprite(i->GetIndex()); +} + +inline int setsprite(DDukeActor* a, int x, int y, int z) +{ + return ::setsprite(a->GetIndex(), x, y, z); +} + +inline int setsprite(DDukeActor* a, const vec3_t& pos) +{ + return ::setsprite(a->GetIndex(), pos.x, pos.y, pos.z); +} + +// see comment for changespritestat. +inline int setsprite(int i, int x, int y, int z) +{ + return ::setsprite(i, x, y, z); +} + +inline int S_PlayActorSound(int soundNum, DDukeActor* spriteNum, int channel = CHAN_AUTO, EChanFlags flags = 0) +{ + return S_PlayActorSound(soundNum, spriteNum ? spriteNum->GetIndex() : -1, channel, flags); +} + +inline void S_StopSound(int sndNum, DDukeActor* actor, int flags = -1) +{ + S_StopSound(sndNum, actor ? actor->GetIndex() : -1, flags); +} + +inline void S_RelinkActorSound(DDukeActor* from, DDukeActor* to) +{ + S_RelinkActorSound(from ? from->GetIndex() : -1, to ? to->GetIndex() : -1); +} + +inline int S_CheckActorSoundPlaying(DDukeActor* a, int soundNum, int channel = 0) +{ + return S_CheckActorSoundPlaying(a->GetIndex(), soundNum, channel); +} + +inline DDukeActor* EGS(short whatsect, int s_x, int s_y, int s_z, short s_pn, signed char s_s, signed char s_xr, signed char s_yr, short s_a, short s_ve, int s_zv, DDukeActor* Owner, signed char s_ss) +{ + return &hittype[EGS(whatsect, s_x, s_y, s_z, s_pn, s_s, s_xr, s_yr, s_a, s_ve, s_zv, Owner ? Owner->GetIndex() : -1, s_ss)]; +} + +inline int ssp(DDukeActor* i, unsigned int cliptype) //The set sprite function +{ + return ssp(i->GetIndex(), cliptype); +} + +inline int ActorToScriptIndex(DDukeActor* a) +{ + if (!a) return -1; + return a->GetIndex(); +} + +inline DDukeActor* ScriptIndexToActor(int index) +{ + if (index == -1) return nullptr; + return &hittype[index]; +} + +inline bool wallswitchcheck(DDukeActor* s) +{ + return !!(tileinfo[s->s.picnum].flags & TFLAG_WALLSWITCH); +} + +inline DDukeActor* spawn(DDukeActor* spawner, int type) +{ + int i = fi.spawn(spawner ? spawner->GetIndex() : -1, type); + return i == -1 ? nullptr : &hittype[i]; +} + +inline int ldist(DDukeActor* s1, DDukeActor* s2) +{ + return ldist(&s1->s, &s2->s); +} + +inline int dist(DDukeActor* s1, DDukeActor* s2) +{ + return dist(&s1->s, &s2->s); +} + +inline int badguy(DDukeActor* pSprite) +{ + return badguypic(pSprite->s.picnum); +} + +inline int bossguy(DDukeActor* pSprite) +{ + return bossguypic(pSprite->s.picnum); +} + +inline int GetGameVarID(int id, DDukeActor* sActor, int sPlayer) +{ + return GetGameVarID(id, sActor->GetIndex(), sPlayer); +} + +inline void SetGameVarID(int id, int lValue, DDukeActor* sActor, int sPlayer) +{ + SetGameVarID(id, lValue, sActor->GetIndex(), sPlayer); +} END_DUKE_NS diff --git a/source/games/duke/src/types.h b/source/games/duke/src/types.h index 2c9c0e4a9..5f82cb849 100644 --- a/source/games/duke/src/types.h +++ b/source/games/duke/src/types.h @@ -45,6 +45,48 @@ struct weaponhit memset(temp_data, 0, sizeof(temp_data)); } int GetIndex() const { return this - array(); } + + // Wrapper around some ugliness. The 'owner' field gets abused by some actors, so better wrap its real use in access functions to keep things in order. + inline weaponhit* GetOwner() + { + return s.owner < 0 ? nullptr : &array()[s.owner]; + } + + inline void SetOwner(weaponhit* a) + { + s.owner = a->GetIndex(); + } + + // same for the 'hittype' owner - which is normally the shooter in an attack. + inline weaponhit* GetHitOwner() + { + return owner < 0 ? nullptr : &array()[owner]; + } + + inline void SetHitOwner(DDukeActor* a) + { + owner = a->GetIndex(); + } + + // The crane is a good example of an actor hijacking the 'owner' field for something other than an actual owner. Abstract this away. + inline bool IsActiveCrane() + { + return s.owner == -2; + } + + inline void SetActiveCrane(bool yes) + { + s.owner = yes ? -2 : -1; + } + + int PlayerIndex() const + { + // only valid for real players - just here to abstract yvel. + return s.yvel; + } + + + }; extern weaponhit hittype[MAXSPRITES + 1]; inline weaponhit* weaponhit::array() { return hittype; } @@ -229,6 +271,10 @@ struct player_struct // input stuff. InputPacket sync; + + DDukeActor* GetActor(); + int GetPlayerNum(); + };