Clean up game loop function

This commit is contained in:
nukeykt 2019-08-31 23:07:47 +09:00 committed by Christoph Oelckers
parent 6b2ebdd444
commit b13e57efa4

View file

@ -705,9 +705,9 @@ void bail2dos(const char *fmt, ...)
void faketimerhandler() void faketimerhandler()
{ {
if ((totalclock < ototalclock + 4) || bInMove) if ((totalclock < ototalclock + 1) || bInMove)
return; return;
ototalclock += 4; ototalclock += 1;
if (moveframes < 4) if (moveframes < 4)
moveframes++; moveframes++;
@ -1523,14 +1523,17 @@ int app_main(int argc, char const* const* argv)
int i; int i;
int esi = 1; //int esi = 1;
int edi = esi; //int edi = esi;
int doTitle = kTrue;
int stopTitle = kFalse;
int tclocks, tclocks2;
levelnew = 1;
// REVERT - change back to kTrue // REVERT - change back to kTrue
// short bDoTitle = kFalse; // short bDoTitle = kFalse;
wConsoleNode = 0; wConsoleNode = 0;
int var_1C = 0;
int nMenu; // TEMP int nMenu; // TEMP
@ -1570,7 +1573,7 @@ int app_main(int argc, char const* const* argv)
vcrfp = fopen("data.vcr", "rb"); vcrfp = fopen("data.vcr", "rb");
if (vcrfp != NULL) { if (vcrfp != NULL) {
bPlayback = kTrue; bPlayback = kTrue;
esi = 0; doTitle = kFalse;
} }
else { else {
DebugOut("Can't open data file 'data.vcr' for reading\n"); DebugOut("Can't open data file 'data.vcr' for reading\n");
@ -1585,7 +1588,7 @@ int app_main(int argc, char const* const* argv)
nNetPlayerCount = 1; nNetPlayerCount = 1;
nTotalPlayers = 2; nTotalPlayers = 2;
esi = 0; doTitle = kFalse;
char ch = *pChar; char ch = *pChar;
@ -1614,7 +1617,7 @@ int app_main(int argc, char const* const* argv)
nNetPlayerCount = 1; nNetPlayerCount = 1;
nTotalPlayers = 2; nTotalPlayers = 2;
esi = 0; doTitle = kFalse;
if (forcelevel < 0) { if (forcelevel < 0) {
forcelevel = levelnew; forcelevel = levelnew;
@ -1627,7 +1630,7 @@ int app_main(int argc, char const* const* argv)
bModemPlay = kFalse; bModemPlay = kFalse;
bSerialPlay = kFalse; bSerialPlay = kFalse;
esi = 0; doTitle = kFalse;
} }
else if (Bstrcasecmp(pChar, "setup") == 0) { else if (Bstrcasecmp(pChar, "setup") == 0) {
g_commandSetup = 1; g_commandSetup = 1;
@ -1671,7 +1674,7 @@ int app_main(int argc, char const* const* argv)
levelnew = atoi(pChar); levelnew = atoi(pChar);
forcelevel = levelnew; forcelevel = levelnew;
esi = 0; doTitle = kFalse;
initprintf("Jumping to level %d...\n", levelnew); initprintf("Jumping to level %d...\n", levelnew);
} }
@ -1785,25 +1788,8 @@ int app_main(int argc, char const* const* argv)
nFirstPassInfo = FindGString("PASSINFO"); nFirstPassInfo = FindGString("PASSINFO");
// count the number of passwords available // count the number of passwords available
for (nPasswordCount = 0; ; nPasswordCount++) for (nPasswordCount = 0; strlen(gString[nFirstPassword+nPasswordCount]) != 0; nPasswordCount++)
{ {
#if 0
char *pStr = gString[nFirstPassword + nPasswordCount];
int nLen = strlen(pStr);
if (!nLen) {
break;
}
#else
/* note - can't make sense of what the code is doing but i've added the below breakout code which seems sensible.. */
// int v32 = (int)&v31[v33];
// if (c == 1)
// break;
if (nFirstPassword + nPasswordCount >= nFirstPassInfo - 1) {
break;
}
#endif
} }
ResetPassword(); ResetPassword();
@ -1864,6 +1850,9 @@ int app_main(int argc, char const* const* argv)
// temp - moving InstallEngine(); before FadeOut as we use nextpage() in FadeOut // temp - moving InstallEngine(); before FadeOut as we use nextpage() in FadeOut
InstallEngine(); InstallEngine();
if (enginePostInit())
ShutDown();
// loc_11745: // loc_11745:
FadeOut(0); FadeOut(0);
// InstallEngine(); // InstallEngine();
@ -1896,48 +1885,64 @@ int app_main(int argc, char const* const* argv)
bail2dos("Unable to connect"); bail2dos("Unable to connect");
} }
if (esi) if (doTitle)
{ {
while (!var_1C) while (!stopTitle)
{ {
DoTitle(); DoTitle();
var_1C = 1; stopTitle = kTrue;
} }
} }
// loc_11811: // loc_11811:
if (forcelevel > -1) if (forcelevel > -1)
{ {
levelnew = forcelevel; levelnew = forcelevel;
goto STARTGAME1;
} }
else
{
// no forcelevel specified... // no forcelevel specified...
if (!bPlayback) if (!bPlayback)
{ {
goto main_loc_I; goto STARTGAME1;
} }
MENU:
SavePosition = -1;
nMenu = menu_Menu(0);
switch (nMenu)
{
case -1:
goto MENU;
case 0:
goto EXITGAME;
case 3:
forcelevel = 0;
goto STARTGAME2;
case 9:
vcrfp = fopen("demo.vcr", "rb");
if (vcrfp == NULL) {
goto MENU;
} }
// multiple points return to here InitRandom();
main_loc_A: bInDemo = kTrue;
bPlayback = kTrue;
KB_FlushKeyboardQueue();
KB_ClearKeysDown();
break;
}
STARTGAME1:
levelnew = 1; levelnew = 1;
levelnum = 1; levelnum = 1;
if (!nNetPlayerCount) { if (!nNetPlayerCount) {
FadeOut(0); FadeOut(0);
} }
STARTGAME2:
main_loc_B:
esi = 0;
bCamera = kFalse; bCamera = kFalse;
ClearCinemaSeen(); ClearCinemaSeen();
PlayerCount = 0; PlayerCount = 0;
lastlevel = -1; lastlevel = -1;
// Start main_Loop_1
for (i = 0; i < nTotalPlayers; i++) for (i = 0; i < nTotalPlayers; i++)
{ {
int nPlayer = GrabPlayer(); int nPlayer = GrabPlayer();
@ -1954,9 +1959,6 @@ main_loc_B:
PlayerList[nPlayer].someNetVal = -4; PlayerList[nPlayer].someNetVal = -4;
} }
} }
// End main_Loop_1
esi = 0;
nNetMoves = 0; nNetMoves = 0;
@ -1968,18 +1970,25 @@ main_loc_B:
forcelevel = GameStats.nMap; forcelevel = GameStats.nMap;
} }
// Here, we either go into a loop of branch off if (forcelevel > -1)
if (forcelevel <= -1) // main_loc_F
{ {
// YELLOW SECTION
levelnew = forcelevel;
UpdateInputs();
forcelevel = -1;
if (bRecord && !bInDemo) {
menu_GameSave2(vcrfp);
}
goto LOOP3;
}
// PINK SECTION // PINK SECTION
UpdateInputs(); UpdateInputs();
nNetMoves = 1; nNetMoves = 1;
if (nMenu == 2) if (nMenu == 2)
{ {
esi = 1;
levelnew = 1; levelnew = 1;
levelnum = 1; levelnum = 1;
levelnew = menu_GameLoad(SavePosition); levelnew = menu_GameLoad(SavePosition);
@ -1991,25 +2000,26 @@ main_loc_B:
} }
nBestLevel = levelnew - 1; nBestLevel = levelnew - 1;
LOOP1:
// WE now need to go to main_loc_G: if (nPlayerLives[nLocalPlayer] <= 0) {
goto main_loc_G; goto MENU;
}
if (levelnew > 99) {
goto EXITGAME;
}
if (!bInDemo && levelnew > nBestLevel && levelnew != 0 && levelnew <= kMap20 && SavePosition > -1) {
menu_GameSave(SavePosition);
}
LOOP2:
if (!nNetPlayerCount && !bPlayback && levelnew > 0 && levelnew <= kMap20) {
levelnew = showmap(levelnum, levelnew, nBestLevel);
} }
// YELLOW SECTION if (levelnew > nBestLevel) {
levelnew = forcelevel; nBestLevel = levelnew;
UpdateInputs();
forcelevel = -1;
esi = bRecord;
if (bRecord && !bInDemo) {
menu_GameSave2(vcrfp);
} }
LOOP3:
main_loc_C:
// if (levelnew != -1)
while (levelnew != -1) while (levelnew != -1)
{ {
// CHECKME - this should be here? // CHECKME - this should be here?
@ -2038,8 +2048,6 @@ main_loc_C:
if (levelnew == kMap20) if (levelnew == kMap20)
{ {
lCountDown = 81000; lCountDown = 81000;
esi = 30;
edi = 0;
nAlarmTicks = 30; nAlarmTicks = 30;
nRedTicks = 0; nRedTicks = 0;
nClockVal = 0; nClockVal = 0;
@ -2048,16 +2056,10 @@ main_loc_C:
if (!LoadLevel(levelnew)) { if (!LoadLevel(levelnew)) {
// TODO "Can't load level %d...\n", nMap; // TODO "Can't load level %d...\n", nMap;
goto main_Exit; goto EXITGAME;
} }
else {
levelnew = -1; levelnew = -1;
// goto main_loc_C;
} }
// End BLUE
}
// else // nMap == -1
{
if (nNetPlayerCount == 0 && lastlevel == levelnum) if (nNetPlayerCount == 0 && lastlevel == levelnum)
{ {
RestoreSavePoint(nLocalPlayer, &initx, &inity, &initz, &initsect, &inita); RestoreSavePoint(nLocalPlayer, &initx, &inity, &initz, &initsect, &inita);
@ -2065,8 +2067,6 @@ main_loc_C:
lastlevel = levelnum; lastlevel = levelnum;
edi = 0;
for (i = 0; i < nTotalPlayers; i++) for (i = 0; i < nTotalPlayers; i++)
{ {
SetSavePoint(i, initx, inity, initz, initsect, inita); SetSavePoint(i, initx, inity, initz, initsect, inita);
@ -2085,12 +2085,7 @@ main_loc_C:
ResetMoveFifo(); ResetMoveFifo();
moveframes = 0; moveframes = 0;
bInMove = kFalse; bInMove = kFalse;
tclocks = totalclock;
edi = 0;
// int esi = totalclock;
esi = totalclock;
nPlayerDAng = 0; nPlayerDAng = 0;
lPlayerXVel = 0; lPlayerXVel = 0;
lPlayerYVel = 0; lPlayerYVel = 0;
@ -2104,16 +2099,12 @@ main_loc_C:
mysetbrightness((uint8_t)nGamma); mysetbrightness((uint8_t)nGamma);
//int edi = totalclock; //int edi = totalclock;
edi = totalclock; tclocks2 = totalclock;
// Game Loop
main_loc_D: while (1)
// ORANGE {
if (levelnew >= 0) if (levelnew >= 0)
{ goto LOOP1;
goto main_loc_G;
}
else
{
CONTROL_BindsEnabled = 1; CONTROL_BindsEnabled = 1;
// Section B // Section B
if (!nCDTrackLength && !nFreeze && !nNetPlayerCount) if (!nCDTrackLength && !nFreeze && !nNetPlayerCount)
@ -2168,13 +2159,13 @@ main_loc_D:
bInMove = kTrue; bInMove = kTrue;
moveframes = (totalclock - edi) / 4; moveframes = (totalclock - tclocks2) / 4;
if (moveframes > 4) if (moveframes > 4)
moveframes = 4; moveframes = 4;
if (moveframes != 0) if (moveframes != 0)
edi = totalclock; tclocks2 = totalclock;
if (bPlayback) if (bPlayback)
{ {
@ -2191,14 +2182,10 @@ main_loc_D:
fclose(vcrfp); fclose(vcrfp);
} }
// now we go to main_loc_I: goto MENU;
goto main_loc_I;
} }
} }
else else if (bRecord || moveframes)
{
// loc_11EE0
if (bRecord || moveframes)
{ {
GetLocalInput(); GetLocalInput();
@ -2214,7 +2201,6 @@ main_loc_D:
sPlayerInput[nLocalPlayer].horizon = nVertPan[nLocalPlayer]; sPlayerInput[nLocalPlayer].horizon = nVertPan[nLocalPlayer];
} }
}
// loc_11F72: // loc_11F72:
if (bRecord && !bInDemo) { if (bRecord && !bInDemo) {
@ -2243,13 +2229,8 @@ main_loc_D:
} }
// loc_11FEE: // loc_11FEE:
esi += moveframes * 4; tclocks += moveframes * 4;
while (moveframes&& levelnew < 0)
main_loc_J:
handleevents();
if (moveframes && levelnew < 0)
{ {
FixPalette(); FixPalette();
@ -2264,10 +2245,8 @@ main_loc_J:
DoFailedFinalScene(); DoFailedFinalScene();
levelnew = 100; levelnew = 100;
goto main_loc_O; break;
} }
else
{
// Pink section // Pink section
lCountDown--; lCountDown--;
DrawClock(); DrawClock();
@ -2288,7 +2267,6 @@ main_loc_J:
} }
} }
} }
}
// YELLOW SECTION // YELLOW SECTION
MoveThings(); MoveThings();
@ -2314,33 +2292,23 @@ main_loc_J:
if (!nNetTime) { if (!nNetTime) {
nFreeze = 3; nFreeze = 3;
} }
goto main_loc_J;
} }
else if (nNetTime != 0) else if (nNetTime == 0)
{
goto main_loc_J;
}
else // nNetTime == 0
{ {
if (BUTTON(gamefunc_Open)) if (BUTTON(gamefunc_Open))
{ {
CONTROL_ClearButton(gamefunc_Open); CONTROL_ClearButton(gamefunc_Open);
goto main_loc_K; goto MENU2;
}
else {
goto main_loc_J;
} }
} }
} }
// END YELLOW SECTION // END YELLOW SECTION
main_loc_O:
// loc_12149: // loc_12149:
if (bInDemo) if (bInDemo)
{ {
while (esi > totalclock) { handleevents(); } while (tclocks > totalclock) { handleevents(); }
esi = totalclock; tclocks = totalclock;
} }
bInMove = kFalse; bInMove = kFalse;
@ -2350,7 +2318,32 @@ main_loc_J:
if (BUTTON(gamefunc_Escape)) if (BUTTON(gamefunc_Escape))
{ {
CONTROL_ClearButton(gamefunc_Escape); CONTROL_ClearButton(gamefunc_Escape);
goto main_loc_K; MENU2:
nMenu = menu_Menu(1);
switch (nMenu)
{
case 0:
goto EXITGAME;
case 1:
goto STARTGAME1;
case 2:
levelnum = levelnew = menu_GameLoad(SavePosition);
lastlevel = -1;
nBestLevel = levelnew - 1;
goto LOOP2;
case 3: // When selecting 'Training' from ingame menu when already playing a level
if (levelnum == 0 || !Query(2, 4, "Quit current game?", "Y/N", 'Y', 13, 'N', 27))
{
levelnew = 0;
levelnum = 0;
}
break;
}
RefreshStatus();
} }
else if (KB_KeyDown[sc_PrintScreen]) else if (KB_KeyDown[sc_PrintScreen])
{ {
@ -2362,7 +2355,7 @@ main_loc_J:
CONTROL_ClearButton(gamefunc_Map); CONTROL_ClearButton(gamefunc_Map);
if (!nFreeze) { if (!nFreeze) {
bMapMode = bMapMode == 0; bMapMode = !bMapMode;
} }
} }
else if (BUTTON(gamefunc_Zoom_Out)) else if (BUTTON(gamefunc_Zoom_Out))
@ -2399,21 +2392,9 @@ main_loc_J:
} }
} }
goto main_loc_L;
}
else {
goto main_loc_N;
}
main_loc_L:
handleevents(); handleevents();
if (PlayerList[nLocalPlayer].nHealth <= 0) if (PlayerList[nLocalPlayer].nHealth > 0)
{
SetAirFrame();
}
else
{ {
if (BUTTON(gamefunc_Inventory_Left)) if (BUTTON(gamefunc_Inventory_Left))
{ {
@ -2431,164 +2412,15 @@ main_loc_J:
CONTROL_ClearButton(gamefunc_Inventory); CONTROL_ClearButton(gamefunc_Inventory);
} }
} }
else
goto main_loc_N; SetAirFrame();
} }
} if (record_mode == 3 && movefifopos == movefifoend) {
main_loc_M:
if (!nNetPlayerCount)
{
if (!bPlayback)
{
if (levelnew > 0 && levelnew <= kMap20)
{
levelnew = showmap(levelnum, levelnew, nBestLevel);
}
}
}
if (levelnew > nBestLevel) {
nBestLevel = levelnew;
}
goto main_loc_C;
main_loc_N:
if (record_mode == 3)
{
if (movefifopos == movefifoend)
levelnew = 0; levelnew = 0;
} }
fps++; fps++;
goto main_loc_D;
main_loc_G:
if (nPlayerLives[nLocalPlayer] <= 0) {
goto main_loc_I;
} }
else EXITGAME:
{
if (levelnew > 99)
{
goto main_Exit;
}
else
{
if (!bInDemo && levelnew > nBestLevel && levelnew != 0 && levelnew <= kMap20 && SavePosition > -1) {
menu_GameSave(SavePosition);
}
goto main_loc_M;
}
}
main_loc_K:
nMenu = menu_Menu(1);
switch (nMenu)
{
default:
{
RefreshStatus();
goto main_loc_L;
}
case 0:
{
goto main_Exit;
break;
}
case 1:
{
goto main_loc_A;
break;
}
case 2:
{
levelnew = menu_GameLoad(SavePosition);
levelnum = levelnew;
lastlevel = -1;
nBestLevel = levelnew - 1;
goto main_loc_M;
break;
}
case 3: // When selecting 'Training' from ingame menu when already playing a level
{
if (levelnum)
{
if (Query(2, 4, "Quit current game?", "Y/N", 'Y', 13, 'N', 27))
{
RefreshStatus();
goto main_loc_L;
}
}
levelnew = 0;
levelnum = 0;
goto main_loc_B;
}
}
main_loc_I:
SavePosition = -1;
nMenu = menu_Menu(0);
if (nMenu < 0) {
goto main_loc_I;
}
switch (nMenu)
{
case 0:
{
goto main_Exit;
break;
}
case 1:
case 2:
{
goto main_loc_A;
break;
}
case 3:
{
forcelevel = 0;
goto main_loc_B;
break;
}
case 9:
{
vcrfp = fopen("demo.vcr", "rb");
if (vcrfp == NULL) {
goto main_loc_I;
}
InitRandom();
bInDemo = kTrue;
bPlayback = kTrue;
KB_FlushKeyboardQueue();
KB_ClearKeysDown();
goto main_loc_A;
break;
}
}
main_Exit:
if (bRecord) { if (bRecord) {
fclose(vcrfp); fclose(vcrfp);
} }
@ -2616,8 +2448,7 @@ main_Exit:
} }
} }
bail2dos("\n"); ShutDown();
return 0; return 0;
} }