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

398 lines
9.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
FTextureID nPlayerPic;
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("playerpic", nPlayerPic)
2020-11-29 19:39:27 +00:00
.EndObject();
}
}
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
void InitRats()
{
nPlayerPic = getSequence("joe", 120)->getFirstFrameTexture();
}
2021-12-23 16:06:48 +00:00
void SetRatVel(DExhumedActor* pActor)
{
pActor->VelFromAngle(-2);
}
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
void BuildRat(DExhumedActor* pActor, const DVector3& pos, sectortype* pSector, DAngle nAngle)
{
if (pActor == nullptr)
{
pActor = insertActor(pSector, 108);
pActor->spr.pos = pos;
}
else
{
ChangeActorStat(pActor, 108);
pActor->spr.pos.Z = pActor->sector()->floorz;
nAngle = pActor->spr.Angles.Yaw;
}
2021-12-23 16:06:48 +00:00
pActor->spr.cstat = CSTAT_SPRITE_BLOCK_ALL;
pActor->spr.shade = -12;
pActor->spr.xoffset = 0;
pActor->spr.yoffset = 0;
setvalidpic(pActor);
pActor->spr.pal = pActor->sector()->ceilingpal;
pActor->clipdist = 7.5;
pActor->spr.Angles.Yaw = nAngle;
pActor->spr.scale = DVector2(0.78125, 0.78125);
2022-09-03 08:02:25 +00:00
pActor->vel.X = 0;
pActor->vel.Y = 0;
pActor->vel.Z = 0;
2021-12-23 16:06:48 +00:00
pActor->spr.lotag = runlist_HeadRun() + 1;
pActor->spr.hitag = 0;
pActor->spr.extra = -1;
if (nAngle.Degrees() >= 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);
pActor->spr.intowner = runlist_AddRunRec(pActor->spr.lotag - 1, pActor, 0x240000);
2021-10-17 21:06:15 +00:00
pActor->nRun = runlist_AddRunRec(NewRun, pActor, 0x240000);
pActor->nSeqFile = "rat";
}
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
2021-10-17 21:06:15 +00:00
DExhumedActor* FindFood(DExhumedActor* pActor)
{
auto pSector = pActor->sector();
2022-08-22 16:41:05 +00:00
if (nChunkTotal)
{
2021-12-07 17:53:02 +00:00
DExhumedActor* pActor2 = nChunkSprite[RandomSize(7) % nChunkTotal];
if (pActor2 != nullptr)
{
2022-08-22 16:41:05 +00:00
if (cansee(pActor->spr.pos.plusZ(pSector->ceilingz * 0.5), pSector, pActor2->spr.pos, pActor2->sector())) {
2021-10-17 21:06:15 +00:00
return pActor2;
}
}
}
if (!nBodyTotal) {
2021-10-17 21:06:15 +00:00
return nullptr;
}
2021-12-07 17:53:02 +00:00
DExhumedActor* pActor2 = nBodySprite[RandomSize(7) % nBodyTotal];
if (pActor2 != nullptr)
{
if (legacyTileNum(nPlayerPic) == pActor2->spr.picnum)
{
2022-08-22 16:41:05 +00:00
if (cansee(pActor->spr.pos, pSector, pActor2->spr.pos, pActor2->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;
if (ev->nDamage)
{
2021-12-23 16:06:48 +00:00
pActor->spr.cstat = 0;
2022-09-03 08:02:25 +00:00
pActor->vel.X = 0;
pActor->vel.Y = 0;
2021-10-23 19:50:57 +00:00
pActor->nAction = 3;
pActor->nFrame = 0;
}
}
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
void AIRat::Draw(RunListEvent* ev)
{
if (const auto pActor = ev->pObjActor)
{
const auto ratSeq = &RatSeq[pActor->nAction];
seq_PlotSequence(ev->nParam, pActor->nSeqFile, ratSeq->nSeqId, pActor->nFrame, ratSeq->nFlags);
}
}
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
void AIRat::Tick(RunListEvent* ev)
{
2022-09-09 16:37:10 +00:00
constexpr double CHECK_DIST = 50/16.;
2021-10-17 21:06:15 +00:00
auto pActor = ev->pObjActor;
if (!pActor) return;
int nAction = pActor->nAction;
bool bVal = false;
const auto ratSeq = getSequence(pActor->nSeqFile, RatSeq[nAction].nSeqId);
const auto& seqFrame = ratSeq->frames[pActor->nFrame];
pActor->spr.setspritetexture(seqFrame.getFirstChunkTexture());
seqFrame.playSound(pActor);
2021-10-23 19:50:57 +00:00
pActor->nFrame++;
if (pActor->nFrame >= ratSeq->frames.Size())
{
bVal = true;
2021-10-23 19:50:57 +00:00
pActor->nFrame = 0;
}
2021-12-07 17:53:02 +00:00
DExhumedActor* 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;
}
2022-09-09 16:37:10 +00:00
auto delta = pActor->spr.pos.XY() - pTarget->spr.pos.XY();
if (abs(delta.X) > CHECK_DIST || abs(delta.Y) > CHECK_DIST)
{
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;
2022-09-03 08:02:25 +00:00
pActor->vel.X = 0;
pActor->vel.Y = 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--;
if (pActor->nFrame >= ratSeq->frames.Size())
{
bVal = true;
pActor->nFrame = 0;
}
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);
2021-12-23 16:06:48 +00:00
SetRatVel(pActor);
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;
2022-09-03 08:02:25 +00:00
pActor->vel.X = 0;
pActor->vel.Y = 0;
}
2021-10-17 21:06:15 +00:00
MoveCreature(pActor);
2022-09-09 16:37:10 +00:00
auto delta = pActor->spr.pos.XY() - pTarget->spr.pos.XY();
if (abs(delta.X) > CHECK_DIST || abs(delta.Y) > CHECK_DIST)
{
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);
2021-12-23 16:06:48 +00:00
SetRatVel(pActor);
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);
2022-09-03 08:02:25 +00:00
pActor->vel.X = 0;
pActor->vel.Y = 0;
return;
}
case 2:
{
if (pActor->vel.X != 0 || pActor->vel.Y != 0 || pActor->vel.Z != 0) {
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 (pActor->vel.X != 0 || pActor->vel.Y != 0)
{
2022-09-03 08:02:25 +00:00
pActor->vel.X = 0;
pActor->vel.Y = 0;
return;
}
pActor->spr.Angles.Yaw = RandomAngle();
2021-12-23 16:06:48 +00:00
SetRatVel(pActor);
return;
}
else
{
2021-10-17 21:06:15 +00:00
PlotCourseToSprite(pActor, pActor->pTarget);
2021-12-23 16:06:48 +00:00
SetRatVel(pActor);
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(pActor->spr.intowner);
2021-12-23 16:06:48 +00:00
runlist_FreeRun(pActor->spr.lotag - 1);
2021-10-23 19:50:57 +00:00
runlist_SubRunRec(pActor->nRun);
2021-12-23 16:06:48 +00:00
pActor->spr.cstat = CSTAT_SPRITE_INVISIBLE;
2021-10-17 21:06:15 +00:00
DeleteActor(pActor);
}
return;
}
}
}
END_PS_NS