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

433 lines
11 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 "engine.h"
#include "exhumed.h"
#include "aistuff.h"
#include "status.h"
#include "player.h"
#include "sequence.h"
#include "input.h"
#include "sound.h"
#include <string.h>
#include <assert.h>
BEGIN_PS_NS
FreeListArray<Snake, kMaxSnakes> SnakeList;
short nPlayerSnake[kMaxPlayers];
FSerializer& Serialize(FSerializer& arc, const char* keyname, Snake& w, Snake* def)
{
if (arc.BeginObject(keyname))
{
2021-10-19 09:19:50 +00:00
arc("enemy", w.pEnemy)
("countdown", w.nCountdown)
.Array("sprites", w.pSprites, kSnakeSprites)
("sc", w.sC)
("run", w.nRun)
.Array("c", w.c, countof(w.c))
("se", w.sE)
("player", w.nSnakePlayer)
.EndObject();
}
return arc;
}
void SerializeSnake(FSerializer& arc)
{
arc("snake", SnakeList);
arc.Array("playersnake", nPlayerSnake, PlayerCount);
}
void InitSnakes()
{
SnakeList.Clear();
memset(nPlayerSnake, 0, sizeof(nPlayerSnake));
}
short GrabSnake()
{
return SnakeList.Get();
}
void DestroySnake(int nSnake)
{
short nRun = SnakeList[nSnake].nRun;
runlist_SubRunRec(nRun);
for (int i = 0; i < kSnakeSprites; i++)
{
2021-10-19 09:19:50 +00:00
auto pSnake = SnakeList[nSnake].pSprites[i];
auto pSprite = &pSnake->s();
runlist_DoSubRunRec(pSprite->lotag - 1);
runlist_DoSubRunRec(pSprite->owner);
2021-10-19 09:19:50 +00:00
DeleteActor(pSnake);
}
SnakeList.Release(nSnake);
if (nSnake == nSnakeCam)
{
nSnakeCam = -1;
}
}
2021-10-19 09:19:50 +00:00
void ExplodeSnakeSprite(DExhumedActor* pActor, short nPlayer)
{
2021-10-19 09:19:50 +00:00
auto pSprite = &pActor->s();
short nDamage = BulletInfo[kWeaponStaff].nDamage;
if (PlayerList[nPlayer].nDouble > 0) {
nDamage *= 2;
}
// take a copy of this, to revert after call to runlist_RadialDamageEnemy()
short nOwner = pSprite->owner;
pSprite->owner = PlayerList[nPlayer].nSprite;
2021-10-19 09:19:50 +00:00
runlist_RadialDamageEnemy(pActor, nDamage, BulletInfo[kWeaponStaff].nRadius);
pSprite->owner = nOwner;
BuildAnim(nullptr, 23, 0, pSprite->x, pSprite->y, pSprite->z, pSprite->sectnum, 40, 4);
AddFlash(pSprite->sectnum, pSprite->x, pSprite->y, pSprite->z, 128);
2021-10-19 09:19:50 +00:00
StopActorSound(pActor);
}
void BuildSnake(short nPlayer, short zVal)
{
zVal -= 1280;
2021-10-19 09:19:50 +00:00
auto pPlayerActor = PlayerList[nPlayer].Actor();
auto pPlayerSprite = &pPlayerActor->s();
short nViewSect = PlayerList[nPlayer].nPlayerViewSect;
short nPic = seq_GetSeqPicnum(kSeqSnakBody, 0, 0);
int x = pPlayerSprite->x;
int y = pPlayerSprite->y;
int z = (pPlayerSprite->z + zVal) - 2560;
short nAngle = pPlayerSprite->ang;
2021-10-19 09:19:50 +00:00
short hitsect;
int hitx, hity, hitz;
2021-10-19 09:19:50 +00:00
DExhumedActor* hitactor;
vec3_t pos = { x, y, z };
hitdata_t hitData;
hitscan(&pos, pPlayerSprite->sectnum, bcos(nAngle), bsin(nAngle), 0, &hitData, CLIPMASK1);
hitx = hitData.pos.x;
hity = hitData.pos.y;
hitz = hitData.pos.z;
hitsect = hitData.sect;
2021-10-19 09:19:50 +00:00
hitactor = hitData.sprite == -1? nullptr : &exhumedActors[hitData.sprite];
uint32_t xDiff = abs(hitx - x);
uint32_t yDiff = abs(hity - y);
uint32_t sqrtNum = xDiff * xDiff + yDiff * yDiff;
if (sqrtNum > INT_MAX)
{
DPrintf(DMSG_WARNING, "%s %d: overflow\n", __func__, __LINE__);
sqrtNum = INT_MAX;
}
int nSqrt = ksqrt(sqrtNum);
if (nSqrt < bsin(512, -4))
{
BackUpBullet(&hitx, &hity, nAngle);
2021-10-19 09:19:50 +00:00
auto pActor = insertActor(hitsect, 202);
auto pSprite = &pActor->s();
pSprite->x = hitx;
pSprite->y = hity;
pSprite->z = hitz;
2021-10-19 09:19:50 +00:00
ExplodeSnakeSprite(pActor, nPlayer);
DeleteActor(pActor);
return;
}
else
{
2021-10-19 09:19:50 +00:00
DExhumedActor* pTarget = nullptr;
2021-10-19 09:19:50 +00:00
if (hitactor && hitactor->s().statnum >= 90 && hitactor->s().statnum <= 199) {
pTarget = hitactor;
}
2021-10-21 20:18:29 +00:00
else if (sPlayerInput[nPlayer].pTarget != nullptr)
2021-10-19 09:19:50 +00:00
{
2021-10-21 20:18:29 +00:00
pTarget = sPlayerInput[nPlayer].pTarget;
}
short nSnake = GrabSnake();
if (nSnake == -1) return;
2021-10-15 21:31:00 +00:00
// GrabTimeSlot(3);
2021-10-19 09:19:50 +00:00
DExhumedActor* sprt;
for (int i = 0; i < kSnakeSprites; i++)
{
2021-10-19 09:19:50 +00:00
auto pActor = insertActor(nViewSect, 202);
auto pSprite = &pActor->s();
2021-10-19 09:19:50 +00:00
pActor->pTarget = pPlayerActor;
//pSprite->owner = nPlayerSprite;
pSprite->picnum = nPic;
2019-11-18 19:38:19 +00:00
if (i == 0)
{
pSprite->x = pPlayerSprite->x;
pSprite->y = pPlayerSprite->y;
pSprite->z = pPlayerSprite->z + zVal;
pSprite->xrepeat = 32;
pSprite->yrepeat = 32;
nViewSect = pSprite->sectnum;
2021-10-19 09:19:50 +00:00
sprt = pActor;
}
2019-11-18 19:38:19 +00:00
else
{
2021-10-19 09:19:50 +00:00
pSprite->x = sprt->s().x;
pSprite->y = sprt->s().y;
pSprite->z = sprt->s().z;
2021-10-15 21:31:00 +00:00
pSprite->xrepeat = 40 - 3 * i;
pSprite->yrepeat = 40 - 3 * i;
2019-11-18 19:38:19 +00:00
}
pSprite->clipdist = 10;
pSprite->cstat = 0;
pSprite->shade = -64;
pSprite->pal = 0;
pSprite->xoffset = 0;
pSprite->yoffset = 0;
pSprite->ang = pPlayerSprite->ang;
pSprite->xvel = 0;
pSprite->yvel = 0;
pSprite->zvel = 0;
pSprite->hitag = 0;
pSprite->extra = -1;
pSprite->lotag = runlist_HeadRun() + 1;
pSprite->backuppos();
2021-10-19 09:19:50 +00:00
SnakeList[nSnake].pSprites[i] = pActor;
pSprite->owner = runlist_AddRunRec(pSprite->lotag - 1, ((nSnake << 8) | i), 0x110000);
}
SnakeList[nSnake].nRun = runlist_AddRunRec(NewRun, nSnake, 0x110000);
SnakeList[nSnake].c[1] = 2;
SnakeList[nSnake].c[5] = 5;
SnakeList[nSnake].c[2] = 4;
SnakeList[nSnake].c[3] = 6;
SnakeList[nSnake].c[4] = 7;
SnakeList[nSnake].c[6] = 6;
SnakeList[nSnake].c[7] = 7;
2021-10-19 09:19:50 +00:00
SnakeList[nSnake].pEnemy = pTarget;
SnakeList[nSnake].nCountdown = 0;
SnakeList[nSnake].sC = 1200;
SnakeList[nSnake].sE = 0;
SnakeList[nSnake].nSnakePlayer = nPlayer;
nPlayerSnake[nPlayer] = nSnake;
if (bSnakeCam)
{
if (nSnakeCam < 0) {
nSnakeCam = nSnake;
}
}
2021-10-19 09:19:50 +00:00
D3PlayFX(StaticSound[kSound6], sprt);
}
}
2021-10-19 09:19:50 +00:00
DExhumedActor* FindSnakeEnemy(short nSnake)
{
short nPlayer = SnakeList[nSnake].nSnakePlayer;
2021-10-19 09:19:50 +00:00
auto pPlayerActor = PlayerList[nPlayer].Actor();
auto pPlayerSprite = &pPlayerActor->s();
auto pActor = SnakeList[nSnake].pSprites[0]; // CHECKME
auto pSprite = &pActor->s();
short nAngle = pSprite->ang;
short nSector = pSprite->sectnum;
int esi = 2048;
2021-10-19 09:19:50 +00:00
DExhumedActor* pEnemy = nullptr;
2021-10-19 09:19:50 +00:00
ExhumedSectIterator it(nSector);
while (auto pAct2 = it.Next())
{
2021-10-19 09:19:50 +00:00
auto pSpr2 = &pAct2->s();
if (pSpr2->statnum >= 90 && pSpr2->statnum < 150 && (pSpr2->cstat & 0x101))
{
2021-10-19 09:19:50 +00:00
if (pAct2 != pPlayerActor && !(pSpr2->cstat & 0x8000))
{
int nAngle2 = (nAngle - GetAngleToSprite(pActor, pAct2)) & kAngleMask;
if (nAngle2 < esi)
{
2021-10-19 09:19:50 +00:00
pEnemy = pAct2;
esi = nAngle2;
}
}
}
}
2021-10-19 09:19:50 +00:00
if (pEnemy)
2019-11-18 19:38:19 +00:00
{
2021-10-19 09:19:50 +00:00
SnakeList[nSnake].pEnemy = pEnemy;
SnakeList[nSnake].nCountdown = 0;
2019-11-18 19:38:19 +00:00
}
else
{
2021-10-19 09:19:50 +00:00
SnakeList[nSnake].nCountdown--;
if (SnakeList[nSnake].nCountdown < -25)
{
2021-10-19 09:19:50 +00:00
pEnemy = pPlayerActor;
SnakeList[nSnake].pEnemy = pPlayerActor;
}
}
2021-10-19 09:19:50 +00:00
return pEnemy;
}
2021-10-15 21:31:00 +00:00
void AISnake::Tick(RunListEvent* ev)
{
short nSnake = RunData[ev->nRun].nObjIndex;
2021-10-15 21:31:00 +00:00
assert(nSnake >= 0 && nSnake < kMaxSnakes);
2021-10-19 09:19:50 +00:00
auto pActor = SnakeList[nSnake].pSprites[0];
auto pSprite = &pActor->s();
2021-10-19 09:19:50 +00:00
seq_MoveSequence(pActor, SeqOffsets[kSeqSnakehed], 0);
2021-10-19 09:19:50 +00:00
auto pEnemySprite = SnakeList[nSnake].pEnemy;
2021-10-19 09:19:50 +00:00
Collision nMov;
2021-10-15 21:31:00 +00:00
int zVal;
2021-10-19 09:19:50 +00:00
if (pEnemySprite == nullptr)
2021-10-15 21:31:00 +00:00
{
SEARCH_ENEMY:
2021-10-19 09:19:50 +00:00
nMov = movesprite(pActor,
2021-10-15 21:31:00 +00:00
600 * bcos(pSprite->ang),
600 * bsin(pSprite->ang),
bsin(SnakeList[nSnake].sE, -5),
0, 0, CLIPMASK1);
2021-10-15 21:31:00 +00:00
FindSnakeEnemy(nSnake);
2021-10-15 21:31:00 +00:00
zVal = 0;
}
else
{
2021-10-19 09:19:50 +00:00
if (!(pEnemySprite->s().cstat & 0x101))
2021-10-15 21:31:00 +00:00
{
2021-10-19 09:19:50 +00:00
SnakeList[nSnake].pEnemy = nullptr;
2021-10-15 21:31:00 +00:00
goto SEARCH_ENEMY;
}
2021-10-15 21:31:00 +00:00
zVal = pSprite->z;
2021-10-19 09:19:50 +00:00
nMov = AngleChase(pActor, pEnemySprite, 1200, SnakeList[nSnake].sE, 32);
2021-10-15 21:31:00 +00:00
zVal = pSprite->z - zVal;
}
2021-10-19 09:19:50 +00:00
if (nMov.type || nMov.exbits)
2021-10-15 21:31:00 +00:00
{
short nPlayer = SnakeList[nSnake].nSnakePlayer;
2021-10-19 09:19:50 +00:00
ExplodeSnakeSprite(SnakeList[nSnake].pSprites[0], nPlayer);
2021-10-15 21:31:00 +00:00
nPlayerSnake[nPlayer] = -1;
SnakeList[nSnake].nSnakePlayer = -1;
2021-10-15 21:31:00 +00:00
DestroySnake(nSnake);
}
else
{
short nAngle = pSprite->ang;
int var_30 = -bcos(nAngle, 6);
int var_34 = -bsin(nAngle, 6);
2021-10-15 21:31:00 +00:00
int var_20 = SnakeList[nSnake].sE;
2021-10-15 21:31:00 +00:00
SnakeList[nSnake].sE = (SnakeList[nSnake].sE + 64) & 0x7FF;
2021-10-15 21:31:00 +00:00
int var_28 = (nAngle + 512) & kAngleMask;
short nSector = pSprite->sectnum;
2021-10-15 21:31:00 +00:00
int x = pSprite->x;
int y = pSprite->y;
int z = pSprite->z;
2021-10-15 21:31:00 +00:00
for (int i = 7; i > 0; i--)
{
2021-10-19 09:19:50 +00:00
auto pActor2 = SnakeList[nSnake].pSprites[i];
auto pSprite2 = &pActor2->s();
2021-10-19 09:19:50 +00:00
pSprite2->ang = nAngle;
pSprite2->x = x;
pSprite2->y = y;
pSprite2->z = z;
2021-10-19 09:19:50 +00:00
ChangeActorSect(pActor2, nSector);
2021-10-15 21:31:00 +00:00
int eax = (bsin(var_20) * SnakeList[nSnake].c[i]) >> 9;
2021-10-19 09:19:50 +00:00
movesprite(pActor2, var_30 + var_30 * i + eax * bcos(var_28), var_30 + var_34 * i + eax * bsin(var_28),
2021-10-15 21:31:00 +00:00
-zVal * (i - 1), 0, 0, CLIPMASK1);
2021-10-15 21:31:00 +00:00
var_20 = (var_20 + 128) & kAngleMask;
2019-11-18 19:38:19 +00:00
}
2021-10-15 21:31:00 +00:00
}
}
2019-11-18 19:38:19 +00:00
2021-10-15 21:31:00 +00:00
void AISnake::Draw(RunListEvent* ev)
{
short nSnake = RunData[ev->nRun].nObjIndex;
short nSprite = ev->nParam;
2019-11-18 19:38:19 +00:00
2021-10-15 21:31:00 +00:00
if ((nSnake & 0xFF) == 0) {
seq_PlotSequence(nSprite, SeqOffsets[kSeqSnakehed], 0, 0);
}
else {
seq_PlotSequence(nSprite, SeqOffsets[kSeqSnakBody], 0, 0);
}
2019-11-18 19:38:19 +00:00
ev->pTSprite->owner = -1;
2021-10-15 21:31:00 +00:00
}
2019-11-18 19:38:19 +00:00
2021-10-15 21:31:00 +00:00
void FuncSnake(int nObject, int nMessage, int nDamage, int nRun)
{
AISnake ai;
runlist_DispatchEvent(&ai, nObject, nMessage, nDamage, nRun);
}
END_PS_NS