2019-11-20 16:21:32 +00:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
//-------------------------------------------------------------------------
|
2019-11-22 23:11:37 +00:00
|
|
|
#include "ns.h"
|
2020-08-18 07:52:08 +00:00
|
|
|
#include "aistuff.h"
|
2020-03-02 21:08:31 +00:00
|
|
|
#include "engine.h"
|
2019-08-26 03:59:14 +00:00
|
|
|
#include "sequence.h"
|
|
|
|
#include "view.h"
|
|
|
|
#include "exhumed.h"
|
|
|
|
#include <assert.h>
|
|
|
|
|
2019-11-22 23:11:37 +00:00
|
|
|
BEGIN_PS_NS
|
|
|
|
|
2019-08-26 03:59:14 +00:00
|
|
|
short nMinChunk;
|
2019-12-05 19:49:28 +00:00
|
|
|
short nPlayerPic;
|
2019-08-26 03:59:14 +00:00
|
|
|
short nMaxChunk;
|
|
|
|
|
|
|
|
struct Rat
|
|
|
|
{
|
2020-03-02 21:08:31 +00:00
|
|
|
short nFrame;
|
2019-08-31 07:47:15 +00:00
|
|
|
short nAction;
|
|
|
|
short nSprite;
|
2020-03-02 21:08:31 +00:00
|
|
|
short nRun;
|
2019-08-31 07:47:15 +00:00
|
|
|
short nTarget;
|
2020-11-29 19:39:27 +00:00
|
|
|
short nCount;
|
|
|
|
short nIndex;
|
2019-08-26 03:59:14 +00:00
|
|
|
};
|
|
|
|
|
2020-11-29 19:39:27 +00:00
|
|
|
|
|
|
|
TArray<Rat> RatList;
|
2019-08-26 03:59:14 +00:00
|
|
|
|
2020-10-11 09:33:28 +00:00
|
|
|
static actionSeq RatSeq[] = {
|
2020-03-02 21:08:31 +00:00
|
|
|
{0, 1},
|
|
|
|
{1, 0},
|
|
|
|
{1, 0},
|
|
|
|
{9, 1},
|
|
|
|
{0, 1}
|
|
|
|
};
|
2019-08-26 03:59:14 +00:00
|
|
|
|
2020-11-29 19:39:27 +00:00
|
|
|
FSerializer& Serialize(FSerializer& arc, const char* keyname, Rat& w, Rat* def)
|
|
|
|
{
|
|
|
|
if (arc.BeginObject(keyname))
|
|
|
|
{
|
|
|
|
arc("run", w.nRun)
|
|
|
|
("frame", w.nFrame)
|
|
|
|
("action", w.nAction)
|
|
|
|
("sprite", w.nSprite)
|
|
|
|
("target", w.nTarget)
|
|
|
|
("count", w.nCount)
|
|
|
|
("index", w.nIndex)
|
|
|
|
.EndObject();
|
|
|
|
}
|
|
|
|
return arc;
|
|
|
|
}
|
2019-08-26 03:59:14 +00:00
|
|
|
|
2020-11-29 19:39:27 +00:00
|
|
|
void SerializeRat(FSerializer& arc)
|
|
|
|
{
|
|
|
|
if (arc.BeginObject("rat"))
|
|
|
|
{
|
|
|
|
arc("minchunk", nMinChunk)
|
|
|
|
("maxchunk", nMaxChunk)
|
|
|
|
("playerpic", nPlayerPic)
|
|
|
|
("list", RatList)
|
|
|
|
.EndObject();
|
|
|
|
}
|
|
|
|
}
|
2019-12-26 21:00:04 +00:00
|
|
|
|
2019-08-26 03:59:14 +00:00
|
|
|
void InitRats()
|
|
|
|
{
|
2020-11-29 19:39:27 +00:00
|
|
|
RatList.Clear();
|
2019-08-31 07:47:15 +00:00
|
|
|
nMinChunk = 9999;
|
|
|
|
nMaxChunk = -1;
|
2019-08-26 03:59:14 +00:00
|
|
|
|
2019-12-05 19:49:28 +00:00
|
|
|
for (int i = 122; i <= 131; i++)
|
2019-08-31 07:47:15 +00:00
|
|
|
{
|
|
|
|
int nPic = seq_GetSeqPicnum(kSeqJoe, i, 0);
|
2019-08-26 03:59:14 +00:00
|
|
|
|
2019-08-31 07:47:15 +00:00
|
|
|
if (nPic < nMinChunk)
|
|
|
|
nMinChunk = nPic;
|
2019-08-26 03:59:14 +00:00
|
|
|
|
2019-08-31 07:47:15 +00:00
|
|
|
if (nPic > nMaxChunk)
|
|
|
|
nMaxChunk = nPic;
|
|
|
|
}
|
2019-08-26 03:59:14 +00:00
|
|
|
|
2019-08-31 07:47:15 +00:00
|
|
|
nPlayerPic = seq_GetSeqPicnum(kSeqJoe, 120, 0);
|
2019-08-26 03:59:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetRatVel(short nSprite)
|
|
|
|
{
|
2021-09-06 06:33:02 +00:00
|
|
|
auto pSprite = &sprite[nSprite];
|
|
|
|
|
|
|
|
pSprite->xvel = bcos(pSprite->ang, -2);
|
|
|
|
pSprite->yvel = bsin(pSprite->ang, -2);
|
2019-08-26 03:59:14 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 16:37:39 +00:00
|
|
|
void BuildRat(short nSprite, int x, int y, int z, short nSector, int nAngle)
|
2019-08-26 03:59:14 +00:00
|
|
|
{
|
2020-11-29 19:39:27 +00:00
|
|
|
auto nRat = RatList.Reserve(1);
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2021-09-06 06:33:02 +00:00
|
|
|
auto pSprite = &sprite[nSprite];
|
|
|
|
|
2019-08-31 07:47:15 +00:00
|
|
|
if (nSprite < 0)
|
|
|
|
{
|
|
|
|
nSprite = insertsprite(nSector, 108);
|
|
|
|
assert(nSprite >= 0 && nSprite < kMaxSprites);
|
2021-09-06 06:33:02 +00:00
|
|
|
pSprite = &sprite[nSprite];
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2021-09-06 06:33:02 +00:00
|
|
|
pSprite->x = x;
|
|
|
|
pSprite->y = y;
|
|
|
|
pSprite->z = z;
|
2019-08-31 07:47:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-09-06 06:33:02 +00:00
|
|
|
nAngle = pSprite->ang;
|
2019-08-31 07:47:15 +00:00
|
|
|
changespritestat(nSprite, 108);
|
|
|
|
}
|
|
|
|
|
2021-09-06 06:33:02 +00:00
|
|
|
pSprite->cstat = 0x101;
|
|
|
|
pSprite->shade = -12;
|
|
|
|
pSprite->xoffset = 0;
|
|
|
|
pSprite->yoffset = 0;
|
|
|
|
pSprite->picnum = 1;
|
|
|
|
pSprite->pal = sector[pSprite->sectnum].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;
|
2019-08-31 07:47:15 +00:00
|
|
|
|
|
|
|
if (nAngle >= 0) {
|
|
|
|
RatList[nRat].nAction = 2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
RatList[nRat].nAction = 4;
|
|
|
|
}
|
|
|
|
|
2020-03-02 21:08:31 +00:00
|
|
|
RatList[nRat].nFrame = 0;
|
2019-08-31 07:47:15 +00:00
|
|
|
RatList[nRat].nSprite = nSprite;
|
|
|
|
RatList[nRat].nTarget = -1;
|
2020-11-29 19:39:27 +00:00
|
|
|
RatList[nRat].nCount = RandomSize(5);
|
|
|
|
RatList[nRat].nIndex = RandomSize(3);
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2021-10-15 16:37:39 +00:00
|
|
|
pSprite->owner = runlist_AddRunRec(pSprite->lotag - 1, nRat, 0x240000);
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2021-10-15 16:37:39 +00:00
|
|
|
RatList[nRat].nRun = runlist_AddRunRec(NewRun, nRat, 0x240000);
|
2019-08-26 03:59:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int FindFood(short nSprite)
|
|
|
|
{
|
2021-09-06 06:33:02 +00:00
|
|
|
auto pSprite = &sprite[nSprite];
|
|
|
|
|
|
|
|
short nSector = pSprite->sectnum;
|
|
|
|
int x = pSprite->x;
|
|
|
|
int y = pSprite->y;
|
|
|
|
int z = pSprite->z;
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2019-12-05 19:49:28 +00:00
|
|
|
int z2 = (z + sector[nSector].ceilingz) / 2;
|
2019-08-31 07:47:15 +00:00
|
|
|
|
|
|
|
if (nChunkTotal)
|
|
|
|
{
|
2021-09-17 07:24:14 +00:00
|
|
|
auto pActor2 = nChunkSprite[RandomSize(7) % nChunkTotal];
|
|
|
|
if (pActor2 != nullptr)
|
|
|
|
{
|
|
|
|
auto pSprite2 = &pActor2->s();
|
|
|
|
if (cansee(x, y, z2, nSector, pSprite2->x, pSprite2->y, pSprite2->z, pSprite2->sectnum)) {
|
|
|
|
return pActor2->GetSpriteIndex();
|
2019-08-31 07:47:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!nBodyTotal) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-09-17 07:01:17 +00:00
|
|
|
auto pActor2 = nBodySprite[RandomSize(7) % nBodyTotal];
|
|
|
|
if (pActor2 != nullptr)
|
2019-08-31 07:47:15 +00:00
|
|
|
{
|
2021-09-17 07:01:17 +00:00
|
|
|
auto pSprite2 = &pActor2->s();
|
|
|
|
if (nPlayerPic == pSprite2->picnum)
|
2019-08-31 07:47:15 +00:00
|
|
|
{
|
2021-09-17 07:01:17 +00:00
|
|
|
if (cansee(x, y, z, nSector, pSprite2->x, pSprite2->y, pSprite2->z, pSprite2->sectnum)) {
|
|
|
|
return pActor2->GetSpriteIndex();
|
2019-08-31 07:47:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2019-08-26 03:59:14 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 21:00:02 +00:00
|
|
|
void AIRat::RadialDamage(RunListEvent* ev)
|
|
|
|
{
|
|
|
|
short nRat = RunData[ev->nRun].nVal;
|
|
|
|
short nSprite = RatList[nRat].nSprite;
|
|
|
|
ev->nDamage = runlist_CheckRadialDamage(nSprite);
|
|
|
|
Damage(ev);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AIRat::Damage(RunListEvent* ev)
|
|
|
|
{
|
|
|
|
short nRat = RunData[ev->nRun].nVal;
|
|
|
|
short nSprite = RatList[nRat].nSprite;
|
|
|
|
auto pSprite = &sprite[nSprite];
|
|
|
|
|
|
|
|
if (ev->nDamage)
|
|
|
|
{
|
|
|
|
pSprite->cstat = 0;
|
|
|
|
pSprite->xvel = 0;
|
|
|
|
pSprite->yvel = 0;
|
|
|
|
RatList[nRat].nAction = 3;
|
|
|
|
RatList[nRat].nFrame = 0;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AIRat::Draw(RunListEvent* ev)
|
2019-08-26 03:59:14 +00:00
|
|
|
{
|
2021-10-15 21:00:02 +00:00
|
|
|
short nRat = RunData[ev->nRun].nVal;
|
|
|
|
short nAction = RatList[nRat].nAction;
|
|
|
|
|
|
|
|
seq_PlotSequence(ev->nIndex, SeqOffsets[kSeqRat] + RatSeq[nAction].a, RatList[nRat].nFrame, RatSeq[nAction].b);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AIRat::Tick(RunListEvent* ev)
|
|
|
|
{
|
|
|
|
short nRat = RunData[ev->nRun].nVal;
|
2019-08-31 07:47:15 +00:00
|
|
|
short nSprite = RatList[nRat].nSprite;
|
|
|
|
short nAction = RatList[nRat].nAction;
|
2021-10-15 21:00:02 +00:00
|
|
|
auto pSprite = &sprite[nSprite];
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2020-03-02 21:08:31 +00:00
|
|
|
bool bVal = false;
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2021-10-15 21:00:02 +00:00
|
|
|
int nSeq = SeqOffsets[kSeqRat] + RatSeq[nAction].a;
|
|
|
|
pSprite->picnum = seq_GetSeqPicnum2(nSeq, RatList[nRat].nFrame);
|
|
|
|
|
|
|
|
seq_MoveSequence(nSprite, nSeq, RatList[nRat].nFrame);
|
|
|
|
|
|
|
|
RatList[nRat].nFrame++;
|
|
|
|
if (RatList[nRat].nFrame >= SeqSize[nSeq])
|
2019-08-31 07:47:15 +00:00
|
|
|
{
|
2021-10-15 21:00:02 +00:00
|
|
|
bVal = true;
|
|
|
|
RatList[nRat].nFrame = 0;
|
|
|
|
}
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2021-10-15 21:00:02 +00:00
|
|
|
short nTarget = RatList[nRat].nTarget;
|
|
|
|
|
|
|
|
Gravity(nSprite);
|
|
|
|
|
|
|
|
switch (nAction)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
RatList[nRat].nCount--;
|
|
|
|
if (RatList[nRat].nCount > 0) {
|
2019-12-05 19:49:28 +00:00
|
|
|
return;
|
2019-08-31 07:47:15 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 21:00:02 +00:00
|
|
|
int xVal = abs(pSprite->x - sprite[nTarget].x);
|
|
|
|
int yVal = abs(pSprite->y - sprite[nTarget].y);
|
|
|
|
|
|
|
|
if (xVal > 50 || yVal > 50)
|
2019-08-31 07:47:15 +00:00
|
|
|
{
|
2021-10-15 21:00:02 +00:00
|
|
|
RatList[nRat].nAction = 2;
|
|
|
|
RatList[nRat].nFrame = 0;
|
|
|
|
RatList[nRat].nTarget = -1;
|
|
|
|
|
|
|
|
pSprite->xvel = 0;
|
|
|
|
pSprite->yvel = 0;
|
2019-08-31 07:47:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-15 21:00:02 +00:00
|
|
|
RatList[nRat].nFrame ^= 1;
|
|
|
|
RatList[nRat].nCount = RandomSize(5) + 4;
|
|
|
|
RatList[nRat].nIndex--;
|
2019-11-20 16:21:32 +00:00
|
|
|
|
2021-10-15 21:00:02 +00:00
|
|
|
if (RatList[nRat].nIndex <= 0)
|
|
|
|
{
|
|
|
|
short nFoodSprite = FindFood(nSprite);
|
|
|
|
if (nFoodSprite == -1) {
|
|
|
|
return;
|
2019-08-31 07:47:15 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 21:00:02 +00:00
|
|
|
RatList[nRat].nTarget = nFoodSprite;
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2021-10-15 21:00:02 +00:00
|
|
|
PlotCourseToSprite(nSprite, nFoodSprite);
|
|
|
|
SetRatVel(nSprite);
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2021-10-15 21:00:02 +00:00
|
|
|
RatList[nRat].nAction = 1;
|
|
|
|
RatList[nRat].nIndex = 900;
|
|
|
|
RatList[nRat].nFrame = 0;
|
|
|
|
}
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2021-10-15 21:00:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
RatList[nRat].nIndex--;
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2021-10-15 21:00:02 +00:00
|
|
|
if (RatList[nRat].nIndex <= 0)
|
|
|
|
{
|
|
|
|
RatList[nRat].nAction = 2;
|
|
|
|
RatList[nRat].nFrame = 0;
|
|
|
|
RatList[nRat].nTarget = -1;
|
|
|
|
|
|
|
|
pSprite->xvel = 0;
|
|
|
|
pSprite->yvel = 0;
|
|
|
|
}
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2021-10-15 21:00:02 +00:00
|
|
|
MoveCreature(nSprite);
|
2019-12-05 19:49:28 +00:00
|
|
|
|
2021-10-15 21:00:02 +00:00
|
|
|
int xVal = abs(pSprite->x - sprite[nTarget].x);
|
|
|
|
int yVal = abs(pSprite->y - sprite[nTarget].y);
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2021-10-15 21:00:02 +00:00
|
|
|
if (xVal >= 50 || yVal >= 50)
|
|
|
|
{
|
|
|
|
RatList[nRat].nCount--;
|
|
|
|
if (RatList[nRat].nCount < 0)
|
|
|
|
{
|
|
|
|
PlotCourseToSprite(nSprite, nTarget);
|
|
|
|
SetRatVel(nSprite);
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2021-10-15 21:00:02 +00:00
|
|
|
RatList[nRat].nCount = 32;
|
|
|
|
}
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2021-10-15 21:00:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2021-10-15 21:00:02 +00:00
|
|
|
RatList[nRat].nAction = 0;
|
|
|
|
RatList[nRat].nFrame = 0;
|
|
|
|
RatList[nRat].nIndex = RandomSize(3);
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2021-10-15 21:00:02 +00:00
|
|
|
pSprite->xvel = 0;
|
|
|
|
pSprite->yvel = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
{
|
|
|
|
if (pSprite->xvel || pSprite->yvel || pSprite->zvel) {
|
|
|
|
MoveCreature(nSprite);
|
|
|
|
}
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2021-10-15 21:00:02 +00:00
|
|
|
RatList[nRat].nCount--;
|
|
|
|
if (RatList[nRat].nCount <= 0)
|
|
|
|
{
|
|
|
|
RatList[nRat].nTarget = FindFood(nSprite);
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2021-10-15 21:00:02 +00:00
|
|
|
if (RatList[nRat].nTarget <= -1)
|
|
|
|
{
|
|
|
|
RatList[nRat].nCount = RandomSize(6);
|
|
|
|
if (pSprite->xvel || pSprite->yvel)
|
|
|
|
{
|
2021-09-06 06:33:02 +00:00
|
|
|
pSprite->xvel = 0;
|
|
|
|
pSprite->yvel = 0;
|
2019-08-31 07:47:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-15 21:00:02 +00:00
|
|
|
pSprite->ang = RandomSize(11);
|
|
|
|
SetRatVel(nSprite);
|
|
|
|
return;
|
2019-08-31 07:47:15 +00:00
|
|
|
}
|
2021-10-15 21:00:02 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
PlotCourseToSprite(nSprite, RatList[nRat].nTarget);
|
|
|
|
SetRatVel(nSprite);
|
|
|
|
RatList[nRat].nAction = 1;
|
|
|
|
RatList[nRat].nIndex = 900;
|
|
|
|
RatList[nRat].nFrame = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2021-10-15 21:00:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
case 3:
|
|
|
|
{
|
|
|
|
if (bVal)
|
|
|
|
{
|
|
|
|
runlist_DoSubRunRec(pSprite->owner);
|
|
|
|
runlist_FreeRun(pSprite->lotag - 1);
|
|
|
|
runlist_SubRunRec(RatList[nRat].nRun);
|
|
|
|
|
|
|
|
pSprite->cstat = 0x8000;
|
|
|
|
mydeletesprite(nSprite);
|
2019-08-31 07:47:15 +00:00
|
|
|
}
|
2021-10-15 21:00:02 +00:00
|
|
|
return;
|
2019-08-31 07:47:15 +00:00
|
|
|
}
|
2021-10-15 21:00:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FuncRat(int nObject, int nMessage, int nDamage, int nRun)
|
|
|
|
{
|
|
|
|
AIRat ai;
|
|
|
|
runlist_DispatchEvent(&ai, nObject, nMessage, nDamage, nRun);
|
2019-08-26 03:59:14 +00:00
|
|
|
}
|
2021-10-15 21:00:02 +00:00
|
|
|
|
2019-11-22 23:11:37 +00:00
|
|
|
END_PS_NS
|