Skip to content

Commit

Permalink
Merge pull request #871 from DanielGibson/wlb-changes-rb
Browse files Browse the repository at this point in the history
Several improvements related to Blood: What Lies Beneath
  • Loading branch information
Hendricks266 authored Dec 21, 2024
2 parents 01afdb6 + a53bd06 commit 70978b1
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 21 deletions.
10 changes: 5 additions & 5 deletions source/blood/src/aicdud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,13 +629,13 @@ static void weaponShot(int, int nXIndex)

int nShots, nTime, nCode;
int dx1, dy1, dz1;
int dx2, dy2, dz2;
int dx3, dy3, dz3;
int dx2, dy2, dz2=0;
int dx3=0, dy3=0, dz3;
int i, j;

int txof; char hxof;
int sang; int hsht;
int tang; char styled;
int txof; char hxof=0;
int sang=0; int hsht;
int tang=0; char styled;


nnExtCoSin(pSpr->ang, &dx1, &dy1);
Expand Down
2 changes: 1 addition & 1 deletion source/blood/src/common_game.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ extern int g_useCwd;

#define BLOODWIDESCREENDEF "blood_widescreen.def"

#define BYTEVERSION 105
#define BYTEVERSION 106

void _SetErrorLoc(const char *pzFile, int nLine);
void _ThrowError(const char *pzFormat, ...);
Expand Down
30 changes: 19 additions & 11 deletions source/blood/src/eventq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -642,27 +642,35 @@ void EventQLoadSave::Load()
Read(bucketHead, sizeof(bucketHead));
}

struct EventWithTime {
EVENT event;
unsigned int evTime;
};

void EventQLoadSave::Save()
{
EVENT events[1024];
unsigned int eventstime[1024];
Write(&eventQ, sizeof(eventQ));
int nEvents = eventQ.PQueue->Size();
uint32_t nEvents = eventQ.PQueue->Size();
// DG: support arbitrary number of events instead of just 1024 (fix crashes when saving in WLB)
EventWithTime* events = new EventWithTime[nEvents];

Write(&eventQ, sizeof(eventQ)); // FIXME: what's the point of saving this? this only writes a pointer that is invalid on load anyway
Write(&nEvents, sizeof(nEvents));
for (int i = 0; i < nEvents; i++)
for (uint32_t i = 0; i < nEvents; i++)
{
eventstime[i] = eventQ.PQueue->LowestPriority();
events[i] = eventQ.ERemove();
Write(&eventstime[i], sizeof(eventstime[i]));
Write(&events[i], sizeof(events[i]));
EventWithTime& evt = events[i];
evt.evTime = eventQ.PQueue->LowestPriority();
evt.event = eventQ.ERemove();
Write(&evt.evTime, sizeof(evt.evTime));
Write(&evt.event, sizeof(evt.event));
}
dassert(eventQ.PQueue->Size() == 0);
for (int i = 0; i < nEvents; i++)
for (uint32_t i = 0; i < nEvents; i++)
{
eventQ.PQueue->Insert(eventstime[i], events[i]);
eventQ.PQueue->Insert(events[i].evTime, events[i].event);
}
Write(rxBucket, sizeof(rxBucket));
Write(bucketHead, sizeof(bucketHead));
delete[] events;
}

static EventQLoadSave *myLoadSave;
Expand Down
7 changes: 6 additions & 1 deletion source/blood/src/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ void _ThrowError(const char *pzFormat, ...)
vsprintf(buffer, pzFormat, args);
LOG_F(ERROR, "%s(%i): %s", _module, _line, buffer);

// NOTE: if NDEBUG is defined, this is a no-op (handled in build/include/compat.h)
debug_break();

#ifdef WM_MSGBOX_WINDOW
char titlebuf[256];
Bsprintf(titlebuf, APPNAME " %s", s_buildRev);
Expand All @@ -79,11 +82,13 @@ void _consoleSysMsg(const char* pzFormat, ...) {
OSD_Printf(OSDTEXT_RED "%s(%i): %s\n", _module, _line, buffer);
}


void __dassert(const char * pzExpr, const char * pzFile, int nLine)
{
LOG_F(ERROR, "Assertion failed: %s in file %s at line %i", pzExpr, pzFile, nLine);

// NOTE: if NDEBUG is defined, this is a no-op (handled in build/include/compat.h)
debug_break();

#ifdef WM_MSGBOX_WINDOW
char titlebuf[256];
Bsprintf(titlebuf, APPNAME " %s", s_buildRev);
Expand Down
8 changes: 7 additions & 1 deletion source/blood/src/levels.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

#define kMaxMessages 32
#define kMaxEpisodes 7
#define kMaxLevels 16
#ifdef NOONE_EXTENSIONS
// allow advanced mods to use more than 16 levels.
// for example, Blood: What Lies Beneath has 43 (as of version 1.1)
#define kMaxLevels 64
#else // original blood only allowed 16 levels per episode
#define kMaxLevels 16
#endif

#pragma pack(push, 1)

Expand Down
38 changes: 37 additions & 1 deletion source/blood/src/loadsave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,33 @@ void LoadSave::LoadGame(char *pzFile)
//sndPlaySong(gGameOptions.zLevelSong, 1);
}

// TODO: when starting to use buildvfs in SaveGame(), remove this function and use buildvfs_exists() instead
static int file_exists(char const* path)
{
#ifdef _WIN32
return GetFileAttributes(path) != INVALID_FILE_ATTRIBUTES;
#else
struct Bstat st;
return !Bstat(path, &st);
#endif
}

void LoadSave::SaveGame(char *pzFile)
{
hSFile = fopen(pzFile, "wb");
// TODO: use buildvfs_open_write() etc (probably in the whole file?)
char fileNameTmp[BMAX_PATH+4];
const char* saveFileName = pzFile;
bool saveFileExists = file_exists(saveFileName);
if (saveFileExists)
{
// write to a different file first, so in case the game crashes while saving
// (which would result in a corrupted savegame) at least the old savegame is preserved
strcpy(fileNameTmp, pzFile);
strcat(fileNameTmp, "_tmp");
saveFileName = fileNameTmp;
}

hSFile = fopen(saveFileName, "wb");
if (hSFile == NULL)
ThrowError("File error #%d creating save file.", errno);
dword_27AA38 = 0;
Expand All @@ -250,6 +274,18 @@ void LoadSave::SaveGame(char *pzFile)
}
fclose(hSFile);
hSFile = NULL;
if (saveFileExists)
{
// I'd like to have a backup of the old savegame, just in case I regret the last save :-p
char fileNameBk[BMAX_PATH+3];
strcpy(fileNameBk, pzFile);
strcat(fileNameBk, "_bk");
rename(pzFile, fileNameBk);

// the savegame was written successfully, so we can rename the saved file
// to the requested name (from gameXXX.sav_tmp to gameXXX.sav)
rename(saveFileName, pzFile);
}
}

class MyLoadSave : public LoadSave
Expand Down
8 changes: 7 additions & 1 deletion source/blood/src/nnexts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,13 @@ void nnExtInitSprite(int nSpr, bool bSaveLoad)
{
int i;
spritetype* pSpr = &sprite[nSpr];
if ((pSpr->flags & kHitagFree))
// FIXME: the extra < 0 check prevents crashes when loading some savegames, for example
// due to `XSPRITE* pXSpr = &xsprite[pSpr->extra]` accessing invalid memory,
// or due to that memory containing a value != 0 at pXspr->physAddr and then
// getSpriteMassBySize() throwing an error because pSpr->extra < 0..
// However, it maybe hides real issues that should be investigated, like
// when/why is extra -1 here?!
if ((pSpr->flags & kHitagFree) || pSpr->extra < 0)
return;

XSPRITE* pXSpr = &xsprite[pSpr->extra];
Expand Down

0 comments on commit 70978b1

Please sign in to comment.