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

630 lines
14 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 "sequence.h"
#include "names.h"
#include "player.h"
#include "input.h"
#include "sound.h"
#include "view.h"
#include "status.h"
#include "version.h"
#include "aistuff.h"
#include "mapinfo.h"
#include <string.h>
#include <cstdio> // for printf
#include <cstdlib>
#include <stdarg.h>
#include <ctype.h>
#include <time.h>
#include <assert.h>
#include "gamecvars.h"
#include "savegamehelp.h"
#include "c_dispatch.h"
#include "raze_sound.h"
#include "gamestate.h"
#include "screenjob.h"
#include "c_console.h"
#include "cheathandler.h"
#include "inputstate.h"
#include "d_protocol.h"
#include "texturemanager.h"
#include "razemenu.h"
#include "v_draw.h"
#include "interpolate.h"
BEGIN_PS_NS
DExhumedActor exhumedActors[MAXSPRITES];
static MapRecord* NextMap;
void uploadCinemaPalettes();
int32_t registerosdcommands(void);
void InitCheats();
int EndLevel = 0;
InputPacket localInput;
////////
void ResetEngine()
{
EraseScreen(-1);
resettiming();
}
void GameInterface::loadPalette()
{
paletteLoadFromDisk();
uploadCinemaPalettes();
LoadPaletteLookups();
}
void CopyTileToBitmap(short nSrcTile, short nDestTile, int xPos, int yPos);
// void TestSaveLoad();
void EraseScreen(int nVal);
void LoadStatus();
void MySetView(int x1, int y1, int x2, int y2);
char sHollyStr[40];
2021-11-21 18:24:46 +00:00
int nFontFirstChar;
int nBackgroundPic;
int nShadowPic;
short nCreaturesKilled = 0, nCreaturesTotal = 0;
short nFreeze;
short nSnakeCam = -1;
int nNetPlayerCount = 0;
short nClockVal;
short nRedTicks;
short nAlarmTicks;
short nButtonColor;
short nEnergyChan;
int lCountDown = 0;
short nEnergyTowers = 0;
short nCfgNetPlayers = 0;
int lLocalCodes = 0;
short bCoordinates = false;
int nNetTime = -1;
int flash;
int totalmoves;
short nCurBodyNum = 0;
short nBodyTotal = 0;
short nTotalPlayers = 1;
// TODO: Rename this (or make it static) so it doesn't conflict with library function
short bSnakeCam = false;
short bRecord = false;
short bPlayback = false;
short bInDemo = false;
short bSlipMode = false;
short bDoFlashes = true;
2021-10-21 20:18:29 +00:00
DExhumedActor* bestTarget;
short scan_char = 0;
int nStartLevel;
int nTimeLimit;
2019-10-27 16:36:25 +00:00
int bVanilla = 0;
void DebugOut(const char *fmt, ...)
{
#ifdef _DEBUG
2019-08-27 06:08:18 +00:00
va_list args;
va_start(args, fmt);
2019-11-24 09:03:19 +00:00
VPrintf(PRINT_HIGH, fmt, args);
#endif
}
void DoClockBeep()
{
ExhumedStatIterator it(407);
while (auto i = it.Next())
2020-10-15 17:10:51 +00:00
{
2021-10-21 22:08:40 +00:00
PlayFX2(StaticSound[kSound74], i);
2019-08-27 06:08:18 +00:00
}
}
void DoRedAlert(int nVal)
{
2019-08-27 06:08:18 +00:00
if (nVal)
{
nAlarmTicks = 69;
nRedTicks = 30;
}
ExhumedStatIterator it(405);
while (auto ac = it.Next())
2019-08-27 06:08:18 +00:00
{
if (nVal)
{
auto spri = &ac->s();
PlayFXAtXYZ(StaticSound[kSoundAlarm], spri->x, spri->y, spri->z, spri->sectnum);
AddFlash(spri->sectnum, spri->x, spri->y, spri->z, 192);
2019-08-27 06:08:18 +00:00
}
}
}
void DrawClock()
{
2019-08-27 06:08:18 +00:00
int ebp = 49;
auto pixels = TileFiles.tileMakeWritable(kTile3603);
2020-04-11 22:04:02 +00:00
memset(pixels, TRANSPARENT_INDEX, 4096);
2019-08-27 06:08:18 +00:00
if (lCountDown / 30 != nClockVal)
{
nClockVal = lCountDown / 30;
DoClockBeep();
}
2019-08-27 06:08:18 +00:00
int nVal = nClockVal;
2019-08-27 06:08:18 +00:00
while (nVal)
{
int v2 = nVal & 0xF;
int yPos = 32 - tileHeight(v2 + kClockSymbol1) / 2;
CopyTileToBitmap(v2 + kClockSymbol1, kTile3603, ebp - tileWidth(v2 + kClockSymbol1) / 2, yPos);
2019-08-27 06:08:18 +00:00
ebp -= 15;
2019-08-27 06:08:18 +00:00
nVal /= 16;
}
2019-08-27 06:08:18 +00:00
DoEnergyTile();
}
double calc_smoothratio()
{
if (bRecord || bPlayback || nFreeze != 0 || paused || cl_capfps || !cl_interpolate)
return MaxSmoothRatio;
return I_GetTimeFrac() * MaxSmoothRatio;
}
void DoGameOverScene(bool finallevel)
{
// todo: make these customizable later.
StartCutscene(finallevel ? "ExhumedCutscenes.BuildCinemaLose" : "ExhumedCutscenes.BuildGameoverScene", 0, [](bool)
{
gameaction = ga_mainmenu;
});
}
void GameMove(void)
{
FixPalette();
ExhumedSpriteIterator it;
while (auto ac = it.Next())
{
ac->s().backuploc();
}
if (currentLevel->gameflags & LEVEL_EX_COUNTDOWN)
{
if (lCountDown <= 0)
{
2020-08-23 10:26:52 +00:00
DoGameOverScene(true);
return;
}
// Pink section
lCountDown--;
DrawClock();
if (nRedTicks)
{
nRedTicks--;
if (nRedTicks <= 0) {
DoRedAlert(0);
}
}
nAlarmTicks--;
nButtonColor--;
if (nAlarmTicks <= 0) {
DoRedAlert(1);
}
}
// YELLOW SECTION
MoveThings();
2019-11-08 16:55:26 +00:00
obobangle = bobangle;
if (PlayerList[nLocalPlayer].totalvel == 0)
{
bobangle = 0;
}
else
{
bobangle += 56;
bobangle &= kAngleMask;
}
UpdateCreepySounds();
// loc_120E9:
totalmoves++;
}
static int SelectAltWeapon(int weap2)
{
// todo
return 0;
}
void GameInterface::Ticker()
{
if (paused)
{
r_NoInterpolate = true;
}
else if (EndLevel == 0)
{
inita &= kAngleMask;
2020-08-28 07:07:36 +00:00
for (int i = 0; i < 4; i++)
{
lPlayerXVel += localInput.fvel * bcos(inita) + localInput.svel * bsin(inita);
lPlayerYVel += localInput.fvel * bsin(inita) - localInput.svel * bcos(inita);
lPlayerXVel -= (lPlayerXVel >> 5) + (lPlayerXVel >> 6);
lPlayerYVel -= (lPlayerYVel >> 5) + (lPlayerYVel >> 6);
}
UpdateInterpolations();
if (localInput.actions & SB_INVPREV)
{
int nItem = PlayerList[nLocalPlayer].nItem;
int i;
for (i = 6; i > 0; i--)
{
nItem--;
if (nItem < 0) nItem = 5;
if (PlayerList[nLocalPlayer].items[nItem] != 0)
break;
}
if (i > 0) PlayerList[nLocalPlayer].nItem = nItem;
}
if (localInput.actions & SB_INVNEXT)
{
int nItem = PlayerList[nLocalPlayer].nItem;
int i;
for (i = 6; i > 0; i--)
{
nItem++;
if (nItem == 6) nItem = 0;
if (PlayerList[nLocalPlayer].items[nItem] != 0)
break;
}
if (i > 0) PlayerList[nLocalPlayer].nItem = nItem;
}
if (localInput.actions & SB_INVUSE)
{
if (PlayerList[nLocalPlayer].nItem != -1)
{
localInput.setItemUsed(PlayerList[nLocalPlayer].nItem);
}
}
for (int i = 0; i < 6; i++)
{
if (localInput.isItemUsed(i))
{
localInput.clearItemUsed(i);
if (PlayerList[nLocalPlayer].items[i] > 0)
{
if (nItemMagic[i] <= PlayerList[nLocalPlayer].nMagic)
{
sPlayerInput[nLocalPlayer].nItem = i;
break;
}
}
}
}
auto currWeap = PlayerList[nLocalPlayer].nCurrentWeapon;
int weap2 = localInput.getNewWeapon();
if (weap2 == WeaponSel_Next)
{
auto newWeap = currWeap == 6 ? 0 : currWeap + 1;
while (newWeap != 0 && (!(PlayerList[nLocalPlayer].nPlayerWeapons & (1 << newWeap)) || (PlayerList[nLocalPlayer].nPlayerWeapons & (1 << newWeap) && PlayerList[nLocalPlayer].nAmmo[newWeap] == 0)))
{
newWeap++;
if (newWeap > 6) newWeap = 0;
}
localInput.setNewWeapon(newWeap + 1);
}
else if (weap2 == WeaponSel_Prev)
{
auto newWeap = currWeap == 0 ? 6 : currWeap - 1;
while (newWeap != 0 && ((!(PlayerList[nLocalPlayer].nPlayerWeapons & (1 << newWeap)) || (PlayerList[nLocalPlayer].nPlayerWeapons & (1 << newWeap) && PlayerList[nLocalPlayer].nAmmo[newWeap] == 0))))
{
newWeap--;
}
localInput.setNewWeapon(newWeap + 1);
}
else if (weap2 == WeaponSel_Alt)
{
weap2 = SelectAltWeapon(weap2);
}
// make weapon selection persist until it gets used up.
int weap = sPlayerInput[nLocalPlayer].getNewWeapon();
if (weap2 <= 0 || weap2 > 7) sPlayerInput[nLocalPlayer].SetNewWeapon(weap);
auto oldactions = sPlayerInput[nLocalPlayer].actions;
sPlayerInput[nLocalPlayer].actions = localInput.actions;
if (oldactions & SB_CENTERVIEW) sPlayerInput[nLocalPlayer].actions |= SB_CENTERVIEW;
sPlayerInput[nLocalPlayer].xVel = lPlayerXVel;
sPlayerInput[nLocalPlayer].yVel = lPlayerYVel;
sPlayerInput[nLocalPlayer].buttons = lLocalCodes;
2021-10-21 20:18:29 +00:00
sPlayerInput[nLocalPlayer].pTarget = bestTarget;
sPlayerInput[nLocalPlayer].nAngle = localInput.avel;
sPlayerInput[nLocalPlayer].pan = localInput.horz;
2021-10-21 20:18:29 +00:00
Ra[nLocalPlayer].pTarget = bestTarget;
lLocalCodes = 0;
PlayClock += 4;
if (PlayClock == 8) gameaction = ga_autosave; // let the game run for 1 frame before saving.
GameMove();
r_NoInterpolate = false;
}
else
{
// Wait for the end of level sound to play out, but stop updating the playsim.
if (EndLevel == 13)
PlayLocalSound(StaticSound[59], 0, true, CHANF_UI);
if (EndLevel > 1) EndLevel--;
r_NoInterpolate = true;
int flash = 7 - abs(EndLevel - 7);
videoTintBlood(flash * 30, flash * 30, flash * 30);
if (EndLevel == 1)
{
if (!soundEngine->GetSoundPlayingInfo(SOURCE_None, nullptr, StaticSound[59] + 1))
{
videoTintBlood(0, 0, 0);
CompleteLevel(NextMap);
NextMap = nullptr;
EndLevel = 0;
}
}
}
}
void LevelFinished()
{
NextMap = FindNextMap(currentLevel);
EndLevel = 13;
}
#define x(a, b) registerName(#a, b);
static void SetTileNames()
{
auto registerName = [](const char* name, int index)
{
TexMan.AddAlias(name, tileGetTexture(index));
};
#include "namelist.h"
}
#undef x
void GameInterface::app_init()
{
#if 0
help_disabled = true;
#endif
InitCheats();
registerosdcommands();
2019-08-27 06:08:18 +00:00
if (nNetPlayerCount == -1)
{
nNetPlayerCount = nCfgNetPlayers - 1;
nTotalPlayers += nNetPlayerCount;
}
SetTileNames();
2019-08-27 06:08:18 +00:00
2019-09-19 13:38:28 +00:00
InitFX();
2019-08-27 06:08:18 +00:00
seq_LoadSequences();
InitStatus();
resettiming();
2019-08-27 06:08:18 +00:00
GrabPalette();
enginecompatibility_mode = ENGINECOMPATIBILITY_19950829;
}
2021-10-21 19:20:54 +00:00
void DeleteActor(DExhumedActor* actor)
{
2021-10-21 19:20:54 +00:00
if (!actor)
{
return;
2019-08-27 06:08:18 +00:00
}
2021-10-21 19:20:54 +00:00
FVector3 pos = GetSoundPos(&actor->s().pos);
soundEngine->RelinkSound(SOURCE_Actor, &actor->s(), nullptr, &pos);
2021-10-21 19:20:54 +00:00
deletesprite(actor->GetSpriteIndex());
actor->s().ox = 0x80000000;
2021-10-21 20:18:29 +00:00
if (actor == bestTarget) {
bestTarget = nullptr;
2019-08-27 06:08:18 +00:00
}
}
void CopyTileToBitmap(short nSrcTile, short nDestTile, int xPos, int yPos)
{
int nOffs = tileHeight(nDestTile) * xPos;
2019-08-27 06:08:18 +00:00
auto pixels = TileFiles.tileMakeWritable(nDestTile);
uint8_t *pDest = pixels + nOffs + yPos;
uint8_t *pDestB = pDest;
2019-08-27 06:08:18 +00:00
tileLoad(nSrcTile);
int destYSize = tileHeight(nDestTile);
int srcYSize = tileHeight(nSrcTile);
const uint8_t *pSrc = tilePtr(nSrcTile);
for (int x = 0; x < tileWidth(nSrcTile); x++)
2019-08-27 06:08:18 +00:00
{
pDest += destYSize;
for (int y = 0; y < srcYSize; y++)
{
uint8_t val = *pSrc;
2020-04-11 22:04:02 +00:00
if (val != TRANSPARENT_INDEX) {
2019-08-27 06:08:18 +00:00
*pDestB = val;
}
pDestB++;
pSrc++;
}
// reset pDestB
pDestB = pDest;
}
2019-11-05 06:15:21 +00:00
TileFiles.InvalidateTile(nDestTile);
}
void EraseScreen(int nVal)
{
2020-04-12 05:44:55 +00:00
// There's no other values than 0 ever coming through here.
twod->ClearScreen();
}
2019-12-26 18:41:42 +00:00
bool GameInterface::CanSave()
{
return gamestate == GS_LEVEL && !bRecord && !bPlayback && !bInDemo && nTotalPlayers == 1 && nFreeze == 0;
2019-12-26 18:41:42 +00:00
}
::GameStats GameInterface::getStats()
{
return { nCreaturesKilled, nCreaturesTotal, 0, 0, PlayClock / 120, 0 };
}
::GameInterface* CreateInterface()
{
return new GameInterface;
}
FSerializer& Serialize(FSerializer& arc, const char* keyname, DExhumedActor& w, DExhumedActor* def)
{
if (arc.BeginObject(keyname))
{
arc("phase", w.nPhase)
("health", w.nHealth)
("frame", w.nFrame)
("action", w.nAction)
("target", w.pTarget)
("count", w.nCount)
2021-10-23 11:02:43 +00:00
("run", w.nRun)
2021-10-18 16:54:35 +00:00
("index", w.nIndex)
2021-10-24 11:20:50 +00:00
("index2", w.nIndex2)
("channel", w.nChannel)
("damage", w.nDamage)
("turn", w.nTurn)
("x", w.x)
("y", w.y)
.EndObject();
}
return arc;
}
void SerializeState(FSerializer& arc)
{
if (arc.BeginObject("state"))
{
if (arc.isReading() && (currentLevel->gameflags & LEVEL_EX_COUNTDOWN))
{
InitEnergyTile();
}
2021-10-21 20:18:29 +00:00
arc ("besttarget", bestTarget)
("creaturestotal", nCreaturesTotal)
("creatureskilled", nCreaturesKilled)
("freeze", nFreeze)
("snakecam", nSnakeCam)
("clockval", nClockVal) // kTile3603
("redticks", nRedTicks)
("alarmticks", nAlarmTicks)
("buttoncolor", nButtonColor)
("energychan", nEnergyChan)
("countdown", lCountDown)
("energytowers", nEnergyTowers)
("totalmoves", totalmoves)
("curbodynum", nCurBodyNum)
("bodytotal", nBodyTotal)
("bsnakecam", bSnakeCam)
("slipmode", bSlipMode)
("PlayClock", PlayClock)
2021-10-21 22:08:40 +00:00
("spiritsprite", pSpiritSprite)
.SparseArray("actors", exhumedActors, kMaxSprites, activeSprites)
.EndObject();
}
}
END_PS_NS