raze/source/games/blood/src/seq.cpp

772 lines
18 KiB
C++
Raw Normal View History

2019-09-19 22:42:45 +00:00
//-------------------------------------------------------------------------
/*
Copyright (C) 2010-2019 EDuke32 developers and contributors
Copyright (C) 2019 Nuke.YKT
Copyright (C) 2020 - Christoph Oelckers
2019-09-19 22:42:45 +00:00
This file is part of Raze
2019-09-19 22:42:45 +00:00
Raze is free software; you can redistribute it and/or
2019-09-19 22:42:45 +00:00
modify it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
//-------------------------------------------------------------------------
#include "ns.h"
#include "filesystem.h"
#include "tarray.h"
2019-09-19 22:42:45 +00:00
#include "build.h"
#include "blood.h"
#include "files.h"
2021-09-04 21:27:21 +00:00
#include "eventq.h"
#include "callback.h"
2019-09-19 22:42:45 +00:00
BEGIN_BLD_NS
static void (*seqClientCallback[])(int, DBloodActor*) = {
FireballSeqCallback,
Fx33Callback,
NapalmSeqCallback,
Fx32Callback,
TreeToGibCallback,
DudeToGibCallback1,
DudeToGibCallback2,
batBiteSeqCallback,
SlashSeqCallback,
StompSeqCallback,
eelBiteSeqCallback,
BurnSeqCallback,
SeqAttackCallback,
cerberusBiteSeqCallback,
cerberusBurnSeqCallback,
cerberusBurnSeqCallback2,
TommySeqCallback,
TeslaSeqCallback,
ShotSeqCallback,
cultThrowSeqCallback,
sub_68170,
sub_68230,
SlashFSeqCallback,
ThrowFSeqCallback,
ThrowSSeqCallback,
BlastSSeqCallback,
ghostSlashSeqCallback,
ghostThrowSeqCallback,
ghostBlastSeqCallback,
GillBiteSeqCallback,
HandJumpSeqCallback,
houndBiteSeqCallback,
houndBurnSeqCallback,
2020-11-21 19:10:45 +00:00
podAttack,
sub_70284,
sub_6FF08,
sub_6FF54,
ratBiteSeqCallback,
SpidBiteSeqCallback,
SpidJumpSeqCallback,
2021-08-21 09:52:24 +00:00
SpidBirthSeqCallback,
sub_71BD4,
sub_720AC,
sub_71A90,
genDudeAttack1,
punchCallback,
ThrowCallback1,
ThrowCallback2,
HackSeqCallback,
StandSeqCallback,
zombfHackSeqCallback,
PukeSeqCallback,
ThrowSeqCallback,
PlayerSurvive,
PlayerKneelsOver,
FireballTrapSeqCallback,
MGunFireSeqCallback,
MGunOpenSeqCallback,
};
2019-09-19 22:42:45 +00:00
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
2019-09-19 22:42:45 +00:00
void Seq::Precache(int palette)
2019-09-19 22:42:45 +00:00
{
if (memcmp(signature, "SEQ\x1a", 4) != 0)
I_Error("Invalid sequence");
if ((version & 0xff00) != 0x300)
I_Error("Obsolete sequence version");
for (int i = 0; i < nFrames; i++)
tilePrecacheTile(seqGetTile(&frames[i]), -1, palette);
2019-09-19 22:42:45 +00:00
}
void seqPrecacheId(int id, int palette)
2019-09-19 22:42:45 +00:00
{
auto pSeq = getSequence(id);
if (pSeq) pSeq->Precache(palette);
2019-09-19 22:42:45 +00:00
}
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
2019-09-19 22:42:45 +00:00
void UpdateCeiling(int nSector, SEQFRAME* pFrame)
2019-09-19 22:42:45 +00:00
{
assert(validSectorIndex(nSector));
sectortype* pSector = &sector[nSector];
pSector->ceilingpicnum = seqGetTile(pFrame);
pSector->ceilingshade = pFrame->shade;
if (pFrame->palette)
pSector->ceilingpal = pFrame->palette;
2019-09-19 22:42:45 +00:00
}
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
2019-09-19 22:42:45 +00:00
void UpdateFloor(int nSector, SEQFRAME* pFrame)
2019-09-19 22:42:45 +00:00
{
assert(validSectorIndex(nSector));
sectortype* pSector = &sector[nSector];
pSector->floorpicnum = seqGetTile(pFrame);
pSector->floorshade = pFrame->shade;
if (pFrame->palette)
pSector->floorpal = pFrame->palette;
2019-09-19 22:42:45 +00:00
}
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
void UpdateWall(int nWall, SEQFRAME* pFrame)
2019-09-19 22:42:45 +00:00
{
assert(validWallIndex(nWall));
walltype* pWall = &wall[nWall];
assert(pWall->hasX());
pWall->picnum = seqGetTile(pFrame);
if (pFrame->palette)
pWall->pal = pFrame->palette;
if (pFrame->transparent)
pWall->cstat |= 128;
else
pWall->cstat &= ~128;
if (pFrame->transparent2)
pWall->cstat |= 512;
else
pWall->cstat &= ~512;
if (pFrame->blockable)
pWall->cstat |= 1;
else
pWall->cstat &= ~1;
if (pFrame->hittable)
pWall->cstat |= 64;
else
pWall->cstat &= ~64;
2019-09-19 22:42:45 +00:00
}
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
void UpdateMasked(int nWall, SEQFRAME* pFrame)
2019-09-19 22:42:45 +00:00
{
assert(validWallIndex(nWall));
walltype* pWall = &wall[nWall];
assert(pWall->hasX());
walltype* pWallNext = pWall->nextWall();
pWall->overpicnum = pWallNext->overpicnum = seqGetTile(pFrame);
if (pFrame->palette)
pWall->pal = pWallNext->pal = pFrame->palette;
if (pFrame->transparent)
{
pWall->cstat |= 128;
pWallNext->cstat |= 128;
}
else
{
pWall->cstat &= ~128;
pWallNext->cstat &= ~128;
}
if (pFrame->transparent2)
{
pWall->cstat |= 512;
pWallNext->cstat |= 512;
}
else
{
pWall->cstat &= ~512;
pWallNext->cstat &= ~512;
}
if (pFrame->blockable)
{
pWall->cstat |= 1;
pWallNext->cstat |= 1;
}
else
{
pWall->cstat &= ~1;
pWallNext->cstat &= ~1;
}
if (pFrame->hittable)
{
pWall->cstat |= 64;
pWallNext->cstat |= 64;
}
else
{
pWall->cstat &= ~64;
pWallNext->cstat &= ~64;
}
2019-09-19 22:42:45 +00:00
}
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
2021-09-04 21:27:21 +00:00
void UpdateSprite(DBloodActor* actor, SEQFRAME* pFrame)
2019-09-19 22:42:45 +00:00
{
2021-09-04 21:27:21 +00:00
spritetype* pSprite = &actor->s();
assert(actor->hasX());
if (pSprite->flags & 2)
{
if (tileHeight(pSprite->picnum) != tileHeight(seqGetTile(pFrame)) || tileTopOffset(pSprite->picnum) != tileTopOffset(seqGetTile(pFrame))
|| (pFrame->yrepeat && pFrame->yrepeat != pSprite->yrepeat))
pSprite->flags |= 4;
}
pSprite->picnum = seqGetTile(pFrame);
if (pFrame->palette)
pSprite->pal = pFrame->palette;
pSprite->shade = pFrame->shade;
2021-09-04 21:27:21 +00:00
int scale = actor->x().scale; // SEQ size scaling
if (pFrame->xrepeat) {
if (scale) pSprite->xrepeat = ClipRange(MulScale(pFrame->xrepeat, scale, 8), 0, 255);
else pSprite->xrepeat = pFrame->xrepeat;
}
if (pFrame->yrepeat) {
if (scale) pSprite->yrepeat = ClipRange(MulScale(pFrame->yrepeat, scale, 8), 0, 255);
else pSprite->yrepeat = pFrame->yrepeat;
}
if (pFrame->transparent)
pSprite->cstat |= 2;
else
pSprite->cstat &= ~2;
if (pFrame->transparent2)
pSprite->cstat |= 512;
else
pSprite->cstat &= ~512;
if (pFrame->blockable)
pSprite->cstat |= 1;
else
pSprite->cstat &= ~1;
if (pFrame->hittable)
pSprite->cstat |= 256;
else
pSprite->cstat &= ~256;
if (pFrame->invisible)
pSprite->cstat |= 32768;
else
pSprite->cstat &= (unsigned short)~32768;
if (pFrame->pushable)
pSprite->cstat |= 4096;
else
pSprite->cstat &= ~4096;
if (pFrame->smoke)
pSprite->flags |= 256;
else
pSprite->flags &= ~256;
if (pFrame->aiming)
pSprite->flags |= 8;
else
pSprite->flags &= ~8;
if (pFrame->flipx)
pSprite->flags |= 1024;
else
pSprite->flags &= ~1024;
if (pFrame->flipy)
pSprite->flags |= 2048;
else
pSprite->flags &= ~2048;
2019-09-19 22:42:45 +00:00
}
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
void SEQINST::Update()
2019-09-19 22:42:45 +00:00
{
assert(frameIndex < pSequence->nFrames);
switch (type)
{
case 0:
2021-09-04 21:27:21 +00:00
UpdateWall(seqindex, &pSequence->frames[frameIndex]);
break;
case 1:
2021-09-04 21:27:21 +00:00
UpdateCeiling(seqindex, &pSequence->frames[frameIndex]);
break;
case 2:
2021-09-04 21:27:21 +00:00
UpdateFloor(seqindex, &pSequence->frames[frameIndex]);
break;
case 3:
{
2021-09-04 21:27:21 +00:00
if (!actor) break;
UpdateSprite(actor, &pSequence->frames[frameIndex]);
if (pSequence->frames[frameIndex].playsound) {
int sound = pSequence->soundId;
// by NoOne: add random sound range feature
if (!VanillaMode() && pSequence->frames[frameIndex].soundRange > 0)
sound += Random(((pSequence->frames[frameIndex].soundRange == 1) ? 2 : pSequence->frames[frameIndex].soundRange));
2021-09-04 21:27:21 +00:00
sfxPlay3DSound(actor, sound, -1, 0);
}
// by NoOne: add surfaceSound trigger feature
2021-09-04 21:27:21 +00:00
spritetype* pSprite = &actor->s();
if (!VanillaMode() && pSequence->frames[frameIndex].surfaceSound && actor->zvel == 0 && actor->xvel != 0) {
if (getUpperLink(pSprite->sectnum)) break; // don't play surface sound for stacked sectors
2021-08-27 19:49:18 +00:00
int surf = tileGetSurfType(sector[pSprite->sectnum].floorpicnum);
if (!surf) break;
static int surfSfxMove[15][4] = {
/* {snd1, snd2, gameVolume, myVolume} */
{800,801,80,25},
{802,803,80,25},
{804,805,80,25},
{806,807,80,25},
{808,809,80,25},
{810,811,80,25},
{812,813,80,25},
{814,815,80,25},
{816,817,80,25},
{818,819,80,25},
{820,821,80,25},
{822,823,80,25},
{824,825,80,25},
{826,827,80,25},
{828,829,80,25},
};
int sndId = surfSfxMove[surf][Random(2)];
auto snd = soundEngine->FindSoundByResID(sndId);
if (snd > 0)
{
auto udata = soundEngine->GetUserData(snd);
int relVol = udata ? udata[2] : 255;
sfxPlay3DSoundCP(pSprite, sndId, -1, 0, 0, (surfSfxMove[surf][2] != relVol) ? relVol : surfSfxMove[surf][3]);
}
}
break;
}
case 4:
2021-09-04 21:27:21 +00:00
UpdateMasked(seqindex, &pSequence->frames[frameIndex]);
break;
}
// all seq callbacks are for sprites, but there's no sanity checks here that what gets passed is meant to be for a sprite...
if (pSequence->frames[frameIndex].trigger && callback != -1)
{
assert(type == 3);
2021-09-04 21:27:21 +00:00
if (type == 3) seqClientCallback[callback](type, actor);
}
2019-09-19 22:42:45 +00:00
}
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
struct ActiveList
2019-09-19 22:42:45 +00:00
{
TArray<SEQINST> list;
void clear() { list.Clear(); }
int getSize() { return list.Size(); }
SEQINST* getInst(int num) { return &list[num]; }
2021-09-04 21:27:21 +00:00
int getIndex(int num) { return list[num].seqindex; }
DBloodActor* getActor(int num) { return list[num].actor; }
int getType(int num) { return list[num].type; }
void remove(int num)
{
list[num] = list.Last();
list.Pop();
}
SEQINST* getNew()
{
list.Reserve(1);
return &list.Last();
}
SEQINST* get(int type, int index)
{
for (auto& n : list)
{
2021-09-04 21:27:21 +00:00
if (n.type == type && n.seqindex == index) return &n;
}
return nullptr;
}
SEQINST* get(DBloodActor* actor)
{
for (auto& n : list)
{
if (n.type == 3 && n.actor == actor) return &n;
}
return nullptr;
}
void remove(int type, int index)
{
for (unsigned i = 0; i < list.Size(); i++)
{
auto& n = list[i];
2021-09-04 21:27:21 +00:00
if (n.type == type && n.seqindex == index)
{
remove(i);
return;
}
}
}
2021-09-04 21:27:21 +00:00
void remove(DBloodActor* actor)
{
for (unsigned i = 0; i < list.Size(); i++)
{
auto& n = list[i];
if (n.type == 3 && n.actor == actor)
{
remove(i);
return;
}
}
}
};
2019-09-19 22:42:45 +00:00
static ActiveList activeList;
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
SEQINST* GetInstance(int type, int nIndex)
2019-09-19 22:42:45 +00:00
{
return activeList.get(type, nIndex);
2019-09-19 22:42:45 +00:00
}
2020-12-03 19:30:30 +00:00
SEQINST* GetInstance(DBloodActor* actor)
{
2021-09-04 21:27:21 +00:00
return activeList.get(actor);
2020-12-03 19:30:30 +00:00
}
int seqGetStatus(DBloodActor* actor)
{
2021-09-04 21:27:21 +00:00
SEQINST* pInst = activeList.get(actor);
if (pInst) return pInst->frameIndex;
return -1;
}
2021-10-13 21:56:03 +00:00
int seqGetID(DBloodActor* actor)
{
2021-09-04 21:27:21 +00:00
SEQINST* pInst = activeList.get(actor);
if (pInst) return pInst->nSeqID;
return -1;
2021-10-13 21:56:03 +00:00
}
void seqKill(int type, int nIndex)
2019-09-19 22:42:45 +00:00
{
2021-09-04 21:27:21 +00:00
assert(type != SS_SPRITE);
activeList.remove(type, nIndex);
2019-09-19 22:42:45 +00:00
}
void seqKillAll()
2019-09-19 22:42:45 +00:00
{
activeList.clear();
2019-09-19 22:42:45 +00:00
}
void seqKill(DBloodActor* actor)
{
2021-09-04 21:27:21 +00:00
activeList.remove(actor);
}
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
static void ByteSwapSEQ(Seq* pSeq)
2019-09-19 22:42:45 +00:00
{
pSeq->version = LittleShort(pSeq->version);
pSeq->nFrames = LittleShort(pSeq->nFrames);
pSeq->ticksPerFrame = LittleShort(pSeq->ticksPerFrame);
pSeq->soundId = LittleShort(pSeq->soundId);
pSeq->flags = LittleLong(pSeq->flags);
for (int i = 0; i < pSeq->nFrames; i++)
{
SEQFRAME* pFrame = &pSeq->frames[i];
BitReader bitReader((char*)pFrame, sizeof(SEQFRAME));
SEQFRAME swapFrame;
swapFrame.tile = bitReader.readUnsigned(12);
swapFrame.transparent = bitReader.readBit();
swapFrame.transparent2 = bitReader.readBit();
swapFrame.blockable = bitReader.readBit();
swapFrame.hittable = bitReader.readBit();
swapFrame.xrepeat = bitReader.readUnsigned(8);
swapFrame.yrepeat = bitReader.readUnsigned(8);
swapFrame.shade = bitReader.readSigned(8);
swapFrame.palette = bitReader.readUnsigned(5);
swapFrame.trigger = bitReader.readBit();
swapFrame.smoke = bitReader.readBit();
swapFrame.aiming = bitReader.readBit();
swapFrame.pushable = bitReader.readBit();
swapFrame.playsound = bitReader.readBit();
swapFrame.invisible = bitReader.readBit();
swapFrame.flipx = bitReader.readBit();
swapFrame.flipy = bitReader.readBit();
swapFrame.tile2 = bitReader.readUnsigned(4);
swapFrame.soundRange = bitReader.readUnsigned(4);
swapFrame.surfaceSound = bitReader.readBit();
swapFrame.reserved = bitReader.readUnsigned(2);
*pFrame = swapFrame;
}
2019-09-19 22:42:45 +00:00
}
FMemArena seqcache;
static TMap<int, Seq*> sequences;
Seq* getSequence(int res_id)
2019-09-19 22:42:45 +00:00
{
auto p = sequences.CheckKey(res_id);
if (p != nullptr) return *p;
int index = fileSystem.FindResource(res_id, "SEQ");
if (index < 0)
{
return nullptr;
}
auto fr = fileSystem.OpenFileReader(index);
auto seqdata = (Seq*)seqcache.Alloc(fr.GetLength());
fr.Read(seqdata, fr.GetLength());
sequences.Insert(res_id, seqdata);
ByteSwapSEQ(seqdata);
return seqdata;
2019-09-19 22:42:45 +00:00
}
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
void seqSpawn(int nSeqID, int type, int nIndex, int callback)
2019-09-19 22:42:45 +00:00
{
2021-09-04 21:27:21 +00:00
assert(type != SS_SPRITE);
Seq* pSequence = getSequence(nSeqID);
if (pSequence == nullptr) return;
SEQINST* pInst = activeList.get(type, nIndex);
if (!pInst)
{
pInst = activeList.getNew();
}
else
{
// already playing this sequence?
if (pInst->nSeqID == nSeqID)
return;
}
pInst->pSequence = pSequence;
pInst->nSeqID = nSeqID;
pInst->callback = callback;
pInst->timeCounter = (short)pSequence->ticksPerFrame;
pInst->frameIndex = 0;
pInst->type = type;
pInst->seqindex = nIndex;
2021-09-04 21:27:21 +00:00
pInst->actor = nullptr;
pInst->Update();
2019-09-19 22:42:45 +00:00
}
2021-09-04 21:27:21 +00:00
void seqSpawn(int nSeqID, DBloodActor* actor, int callback)
{
2021-09-04 21:27:21 +00:00
Seq* pSequence = getSequence(nSeqID);
if (pSequence == nullptr) return;
SEQINST* pInst = activeList.get(actor);
if (!pInst)
{
pInst = activeList.getNew();
}
else
{
// already playing this sequence?
if (pInst->nSeqID == nSeqID)
return;
}
pInst->pSequence = pSequence;
pInst->nSeqID = nSeqID;
pInst->callback = callback;
pInst->timeCounter = (short)pSequence->ticksPerFrame;
pInst->frameIndex = 0;
pInst->type = SS_SPRITE;
pInst->seqindex = 0;
pInst->actor = actor;
pInst->Update();
}
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
2019-09-19 22:42:45 +00:00
int seqGetStatus(int type, int nIndex)
2019-09-19 22:42:45 +00:00
{
SEQINST* pInst = activeList.get(type, nIndex);
if (pInst) return pInst->frameIndex;
return -1;
2019-09-19 22:42:45 +00:00
}
int seqGetID(int type, int nIndex)
2019-09-19 22:42:45 +00:00
{
SEQINST* pInst = activeList.get(type, nIndex);
if (pInst) return pInst->nSeqID;
return -1;
2019-09-19 22:42:45 +00:00
}
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
void seqProcess(int nTicks)
2019-09-19 22:42:45 +00:00
{
for (int i = 0; i < activeList.getSize(); i++)
{
SEQINST* pInst = activeList.getInst(i);
Seq* pSeq = pInst->pSequence;
2021-09-04 21:27:21 +00:00
int index = pInst->seqindex;
auto actor = pInst->actor;
assert(pInst->frameIndex < pSeq->nFrames);
pInst->timeCounter -= nTicks;
while (pInst->timeCounter < 0)
{
pInst->timeCounter += pSeq->ticksPerFrame;
++pInst->frameIndex;
if (pInst->frameIndex == pSeq->nFrames)
{
if (!pSeq->isLooping())
{
if (pSeq->isRemovable())
{
if (pInst->type == SS_SPRITE)
{
2021-09-04 21:27:21 +00:00
if (actor)
{
2021-09-04 21:27:21 +00:00
evKillActor(actor);
if ((actor->s().hitag & kAttrRespawn) != 0 && (actor->s().inittype >= kDudeBase && actor->s().inittype < kDudeMax))
evPostActor(actor, gGameOptions.nMonsterRespawnTime, kCallbackRespawn);
else DeleteSprite(actor); // safe to not use actPostSprite here
}
}
2021-09-04 21:27:21 +00:00
else if (pInst->type == SS_MASKED)
{
assert(index >= 0 && index < kMaxWalls);
auto pWall = &wall[index];
pWall->cstat &= ~(8 + 16 + 32);
if (pWall->twoSided())
pWall->nextWall()->cstat &= ~(8 + 16 + 32);
}
}
// remove it from the active list
activeList.remove(i--);
break;
}
else pInst->frameIndex = 0;
}
pInst->Update();
}
}
2019-09-19 22:42:45 +00:00
}
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
FSerializer& Serialize(FSerializer& arc, const char* keyname, SEQINST& w, SEQINST* def)
{
if (arc.BeginObject(keyname))
{
2021-11-10 20:45:15 +00:00
arc ("type", w.type)
("callback", w.callback)
("seqid", w.nSeqID)
("timecounter", w.timeCounter)
("frameindex", w.frameIndex)
("index", w.seqindex)
2021-11-10 20:45:15 +00:00
("actor", w.actor);
arc.EndObject();
}
return arc;
}
void SerializeSequences(FSerializer& arc)
{
arc("sequences", activeList.list);
if (arc.isReading()) for (unsigned i = 0; i < activeList.list.Size(); i++)
{
activeList.list[i].pSequence = getSequence(activeList.list[i].nSeqID);
}
}
END_BLD_NS