raze/source/games/exhumed/src/rat.cpp

376 lines
8.4 KiB
C++
Raw Normal View History

//-------------------------------------------------------------------------
/*
Copyright (C) 2010-2019 EDuke32 developers and contributors
Copyright (C) 2019 sirlemonhead, Nuke.YKT
This file is part of PCExhumed.
PCExhumed is free software; you can redistribute it and/or
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 "aistuff.h"
#include "engine.h"
#include "sequence.h"
#include "view.h"
#include "exhumed.h"
#include <assert.h>
BEGIN_PS_NS
int nMinChunk;
int nPlayerPic;
int nMaxChunk;
static actionSeq RatSeq[] = {
{0, 1},
{1, 0},
{1, 0},
{9, 1},
{0, 1}
};
2020-11-29 19:39:27 +00:00
void SerializeRat(FSerializer& arc)
{
if (arc.BeginObject("rat"))
{
arc("minchunk", nMinChunk)
("maxchunk", nMaxChunk)
("playerpic", nPlayerPic)
.EndObject();
}
}
void InitRats()
{
nMinChunk = 9999;
nMaxChunk = -1;
for (int i = 122; i <= 131; i++)
{
int nPic = seq_GetSeqPicnum(kSeqJoe, i, 0);
if (nPic < nMinChunk)
nMinChunk = nPic;
if (nPic > nMaxChunk)
nMaxChunk = nPic;
}
nPlayerPic = seq_GetSeqPicnum(kSeqJoe, 120, 0);
}
2021-10-17 21:06:15 +00:00
void SetRatVel(spritetype* pSprite)
{
pSprite->xvel = bcos(pSprite->ang, -2);
pSprite->yvel = bsin(pSprite->ang, -2);
}
2021-11-22 23:35:29 +00:00
void BuildRat(DExhumedActor* pActor, int x, int y, int z, sectortype* nSector, int nAngle)
{
2021-10-17 21:06:15 +00:00
spritetype* pSprite;
if (pActor == nullptr)
{
2021-10-17 21:06:15 +00:00
pActor = insertActor(nSector, 108);
pSprite = &pActor->s();
}
else
{
2021-10-17 21:06:15 +00:00
pSprite = &pActor->s();
x = pSprite->x;
y = pSprite->y;
z = pSprite->z;
nAngle = pSprite->ang;
2021-10-17 21:06:15 +00:00
ChangeActorStat(pActor, 108);
}
pSprite->cstat = 0x101;
pSprite->shade = -12;
pSprite->xoffset = 0;
pSprite->yoffset = 0;
pSprite->picnum = 1;
pSprite->pal = pSprite->sector()->ceilingpal;
pSprite->clipdist = 30;
pSprite->ang = nAngle;
pSprite->xrepeat = 50;
pSprite->yrepeat = 50;
pSprite->xvel = 0;
pSprite->yvel = 0;
pSprite->zvel = 0;
pSprite->lotag = runlist_HeadRun() + 1;
pSprite->hitag = 0;
pSprite->extra = -1;
if (nAngle >= 0) {
2021-10-23 19:50:57 +00:00
pActor->nAction = 2;
}
else {
2021-10-23 19:50:57 +00:00
pActor->nAction = 4;
}
2021-10-23 19:50:57 +00:00
pActor->nFrame = 0;
2021-10-17 21:06:15 +00:00
pActor->pTarget = nullptr;
2021-10-23 19:50:57 +00:00
pActor->nCount = RandomSize(5);
2021-10-17 21:06:15 +00:00
pActor->nPhase = RandomSize(3);
2021-10-17 21:06:15 +00:00
pSprite->owner = runlist_AddRunRec(pSprite->lotag - 1, pActor, 0x240000);
2021-10-17 21:06:15 +00:00
pActor->nRun = runlist_AddRunRec(NewRun, pActor, 0x240000);
}
2021-10-17 21:06:15 +00:00
DExhumedActor* FindFood(DExhumedActor* pActor)
{
2021-10-17 21:06:15 +00:00
auto pSprite = &pActor->s();
2021-11-22 21:38:27 +00:00
auto pSector = pSprite->sector();
int x = pSprite->x;
int y = pSprite->y;
int z = pSprite->z;
2021-11-22 21:38:27 +00:00
int z2 = (z + pSector->ceilingz) / 2;
if (nChunkTotal)
{
auto pActor2 = nChunkSprite[RandomSize(7) % nChunkTotal];
if (pActor2 != nullptr)
{
auto pSprite2 = &pActor2->s();
2021-11-22 21:38:27 +00:00
if (cansee(x, y, z2, pSector, pSprite2->x, pSprite2->y, pSprite2->z, pSprite2->sector())) {
2021-10-17 21:06:15 +00:00
return pActor2;
}
}
}
if (!nBodyTotal) {
2021-10-17 21:06:15 +00:00
return nullptr;
}
auto pActor2 = nBodySprite[RandomSize(7) % nBodyTotal];
if (pActor2 != nullptr)
{
auto pSprite2 = &pActor2->s();
if (nPlayerPic == pSprite2->picnum)
{
2021-11-22 21:38:27 +00:00
if (cansee(x, y, z, pSector, pSprite2->x, pSprite2->y, pSprite2->z, pSprite2->sector())) {
2021-10-17 21:06:15 +00:00
return pActor2;
}
}
}
2021-10-17 21:06:15 +00:00
return nullptr;
}
void AIRat::RadialDamage(RunListEvent* ev)
{
2021-10-17 21:06:15 +00:00
auto pActor = ev->pObjActor;
if (!pActor) return;
ev->nDamage = runlist_CheckRadialDamage(pActor);
Damage(ev);
}
void AIRat::Damage(RunListEvent* ev)
{
2021-10-17 21:06:15 +00:00
auto pActor = ev->pObjActor;
if (!pActor) return;
auto pSprite = &pActor->s();
if (ev->nDamage)
{
pSprite->cstat = 0;
pSprite->xvel = 0;
pSprite->yvel = 0;
2021-10-23 19:50:57 +00:00
pActor->nAction = 3;
pActor->nFrame = 0;
}
}
void AIRat::Draw(RunListEvent* ev)
{
2021-10-17 21:06:15 +00:00
auto pActor = ev->pObjActor;
if (!pActor) return;
int nAction = pActor->nAction;
2021-10-23 19:50:57 +00:00
seq_PlotSequence(ev->nParam, SeqOffsets[kSeqRat] + RatSeq[nAction].a, pActor->nFrame, RatSeq[nAction].b);
}
void AIRat::Tick(RunListEvent* ev)
{
2021-10-17 21:06:15 +00:00
auto pActor = ev->pObjActor;
if (!pActor) return;
int nAction = pActor->nAction;
2021-10-17 21:06:15 +00:00
auto pSprite = &pActor->s();
bool bVal = false;
int nSeq = SeqOffsets[kSeqRat] + RatSeq[nAction].a;
2021-10-23 19:50:57 +00:00
pSprite->picnum = seq_GetSeqPicnum2(nSeq, pActor->nFrame);
2021-10-17 21:06:15 +00:00
seq_MoveSequence(pActor, nSeq, pActor->nFrame);
2021-10-23 19:50:57 +00:00
pActor->nFrame++;
if (pActor->nFrame >= SeqSize[nSeq])
{
bVal = true;
2021-10-23 19:50:57 +00:00
pActor->nFrame = 0;
}
2021-10-17 21:06:15 +00:00
auto pTarget = pActor->pTarget;
2021-10-17 21:06:15 +00:00
Gravity(pActor);
switch (nAction)
{
default:
return;
case 0:
{
2021-10-23 19:50:57 +00:00
pActor->nCount--;
if (pActor->nCount > 0) {
return;
}
2021-10-17 21:06:15 +00:00
int xVal = abs(pSprite->x - pTarget->s().x);
int yVal = abs(pSprite->y - pTarget->s().y);
if (xVal > 50 || yVal > 50)
{
2021-10-23 19:50:57 +00:00
pActor->nAction = 2;
pActor->nFrame = 0;
2021-10-17 21:06:15 +00:00
pActor->pTarget = nullptr;
pSprite->xvel = 0;
pSprite->yvel = 0;
return;
}
2021-10-23 19:50:57 +00:00
pActor->nFrame ^= 1;
pActor->nCount = RandomSize(5) + 4;
2021-10-17 21:06:15 +00:00
pActor->nPhase--;
2021-10-17 21:06:15 +00:00
if (pActor->nPhase <= 0)
{
2021-10-17 21:06:15 +00:00
auto pFoodSprite = FindFood(pActor);
if (pFoodSprite == nullptr) {
return;
}
2021-10-17 21:06:15 +00:00
pActor->pTarget = pFoodSprite;
2021-10-17 21:06:15 +00:00
PlotCourseToSprite(pActor, pFoodSprite);
SetRatVel(pSprite);
2021-10-23 19:50:57 +00:00
pActor->nAction = 1;
2021-10-17 21:06:15 +00:00
pActor->nPhase = 900;
2021-10-23 19:50:57 +00:00
pActor->nFrame = 0;
}
return;
}
case 1:
{
2021-10-17 21:06:15 +00:00
pActor->nPhase--;
2021-10-17 21:06:15 +00:00
if (pActor->nPhase <= 0)
{
2021-10-23 19:50:57 +00:00
pActor->nAction = 2;
pActor->nFrame = 0;
2021-10-17 21:06:15 +00:00
pActor->pTarget = nullptr;
pSprite->xvel = 0;
pSprite->yvel = 0;
}
2021-10-17 21:06:15 +00:00
MoveCreature(pActor);
2021-10-17 21:06:15 +00:00
int xVal = abs(pSprite->x - pTarget->s().x);
int yVal = abs(pSprite->y - pTarget->s().y);
if (xVal >= 50 || yVal >= 50)
{
2021-10-23 19:50:57 +00:00
pActor->nCount--;
if (pActor->nCount < 0)
{
2021-10-17 21:06:15 +00:00
PlotCourseToSprite(pActor, pTarget);
SetRatVel(pSprite);
2021-10-23 19:50:57 +00:00
pActor->nCount = 32;
}
return;
}
2021-10-23 19:50:57 +00:00
pActor->nAction = 0;
pActor->nFrame = 0;
2021-10-17 21:06:15 +00:00
pActor->nPhase = RandomSize(3);
pSprite->xvel = 0;
pSprite->yvel = 0;
return;
}
case 2:
{
if (pSprite->xvel || pSprite->yvel || pSprite->zvel) {
2021-10-17 21:06:15 +00:00
MoveCreature(pActor);
}
2021-10-23 19:50:57 +00:00
pActor->nCount--;
if (pActor->nCount <= 0)
{
2021-10-17 21:06:15 +00:00
pActor->pTarget = FindFood(pActor);
2021-10-17 21:06:15 +00:00
if (pActor->pTarget == nullptr)
{
2021-10-23 19:50:57 +00:00
pActor->nCount = RandomSize(6);
if (pSprite->xvel || pSprite->yvel)
{
pSprite->xvel = 0;
pSprite->yvel = 0;
return;
}
pSprite->ang = RandomSize(11);
2021-10-17 21:06:15 +00:00
SetRatVel(pSprite);
return;
}
else
{
2021-10-17 21:06:15 +00:00
PlotCourseToSprite(pActor, pActor->pTarget);
SetRatVel(pSprite);
2021-10-23 19:50:57 +00:00
pActor->nAction = 1;
2021-10-17 21:06:15 +00:00
pActor->nPhase = 900;
2021-10-23 19:50:57 +00:00
pActor->nFrame = 0;
return;
}
}
return;
}
case 3:
{
if (bVal)
{
runlist_DoSubRunRec(pSprite->owner);
runlist_FreeRun(pSprite->lotag - 1);
2021-10-23 19:50:57 +00:00
runlist_SubRunRec(pActor->nRun);
pSprite->cstat = 0x8000;
2021-10-17 21:06:15 +00:00
DeleteActor(pActor);
}
return;
}
}
}
END_PS_NS