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()
{
if ((totalclock < ototalclock + 4) || bInMove)
if ((totalclock < ototalclock + 1) || bInMove)
return;
ototalclock += 4;
ototalclock += 1;
if (moveframes < 4)
moveframes++;
@ -1523,14 +1523,17 @@ int app_main(int argc, char const* const* argv)
int i;
int esi = 1;
int edi = esi;
//int esi = 1;
//int edi = esi;
int doTitle = kTrue;
int stopTitle = kFalse;
int tclocks, tclocks2;
levelnew = 1;
// REVERT - change back to kTrue
// short bDoTitle = kFalse;
wConsoleNode = 0;
int var_1C = 0;
int nMenu; // TEMP
@ -1570,7 +1573,7 @@ int app_main(int argc, char const* const* argv)
vcrfp = fopen("data.vcr", "rb");
if (vcrfp != NULL) {
bPlayback = kTrue;
esi = 0;
doTitle = kFalse;
}
else {
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;
nTotalPlayers = 2;
esi = 0;
doTitle = kFalse;
char ch = *pChar;
@ -1614,7 +1617,7 @@ int app_main(int argc, char const* const* argv)
nNetPlayerCount = 1;
nTotalPlayers = 2;
esi = 0;
doTitle = kFalse;
if (forcelevel < 0) {
forcelevel = levelnew;
@ -1627,7 +1630,7 @@ int app_main(int argc, char const* const* argv)
bModemPlay = kFalse;
bSerialPlay = kFalse;
esi = 0;
doTitle = kFalse;
}
else if (Bstrcasecmp(pChar, "setup") == 0) {
g_commandSetup = 1;
@ -1671,7 +1674,7 @@ int app_main(int argc, char const* const* argv)
levelnew = atoi(pChar);
forcelevel = levelnew;
esi = 0;
doTitle = kFalse;
initprintf("Jumping to level %d...\n", levelnew);
}
@ -1785,25 +1788,8 @@ int app_main(int argc, char const* const* argv)
nFirstPassInfo = FindGString("PASSINFO");
// 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();
@ -1864,6 +1850,9 @@ int app_main(int argc, char const* const* argv)
// temp - moving InstallEngine(); before FadeOut as we use nextpage() in FadeOut
InstallEngine();
if (enginePostInit())
ShutDown();
// loc_11745:
FadeOut(0);
// InstallEngine();
@ -1896,48 +1885,64 @@ int app_main(int argc, char const* const* argv)
bail2dos("Unable to connect");
}
if (esi)
if (doTitle)
{
while (!var_1C)
while (!stopTitle)
{
DoTitle();
var_1C = 1;
stopTitle = kTrue;
}
}
// loc_11811:
if (forcelevel > -1)
{
levelnew = forcelevel;
goto STARTGAME1;
}
else
{
// no forcelevel specified...
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
main_loc_A:
InitRandom();
bInDemo = kTrue;
bPlayback = kTrue;
KB_FlushKeyboardQueue();
KB_ClearKeysDown();
break;
}
STARTGAME1:
levelnew = 1;
levelnum = 1;
if (!nNetPlayerCount) {
FadeOut(0);
}
main_loc_B:
esi = 0;
STARTGAME2:
bCamera = kFalse;
ClearCinemaSeen();
PlayerCount = 0;
lastlevel = -1;
// Start main_Loop_1
for (i = 0; i < nTotalPlayers; i++)
{
int nPlayer = GrabPlayer();
@ -1954,9 +1959,6 @@ main_loc_B:
PlayerList[nPlayer].someNetVal = -4;
}
}
// End main_Loop_1
esi = 0;
nNetMoves = 0;
@ -1968,18 +1970,25 @@ main_loc_B:
forcelevel = GameStats.nMap;
}
// Here, we either go into a loop of branch off
if (forcelevel <= -1) // main_loc_F
if (forcelevel > -1)
{
// YELLOW SECTION
levelnew = forcelevel;
UpdateInputs();
forcelevel = -1;
if (bRecord && !bInDemo) {
menu_GameSave2(vcrfp);
}
goto LOOP3;
}
// PINK SECTION
UpdateInputs();
nNetMoves = 1;
if (nMenu == 2)
{
esi = 1;
levelnew = 1;
levelnum = 1;
levelnew = menu_GameLoad(SavePosition);
@ -1991,25 +2000,26 @@ main_loc_B:
}
nBestLevel = levelnew - 1;
LOOP1:
// WE now need to go to main_loc_G:
goto main_loc_G;
if (nPlayerLives[nLocalPlayer] <= 0) {
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
levelnew = forcelevel;
UpdateInputs();
forcelevel = -1;
esi = bRecord;
if (bRecord && !bInDemo) {
menu_GameSave2(vcrfp);
if (levelnew > nBestLevel) {
nBestLevel = levelnew;
}
main_loc_C:
// if (levelnew != -1)
LOOP3:
while (levelnew != -1)
{
// CHECKME - this should be here?
@ -2038,8 +2048,6 @@ main_loc_C:
if (levelnew == kMap20)
{
lCountDown = 81000;
esi = 30;
edi = 0;
nAlarmTicks = 30;
nRedTicks = 0;
nClockVal = 0;
@ -2048,16 +2056,10 @@ main_loc_C:
if (!LoadLevel(levelnew)) {
// TODO "Can't load level %d...\n", nMap;
goto main_Exit;
goto EXITGAME;
}
else {
levelnew = -1;
// goto main_loc_C;
}
// End BLUE
}
// else // nMap == -1
{
if (nNetPlayerCount == 0 && lastlevel == levelnum)
{
RestoreSavePoint(nLocalPlayer, &initx, &inity, &initz, &initsect, &inita);
@ -2065,8 +2067,6 @@ main_loc_C:
lastlevel = levelnum;
edi = 0;
for (i = 0; i < nTotalPlayers; i++)
{
SetSavePoint(i, initx, inity, initz, initsect, inita);
@ -2085,12 +2085,7 @@ main_loc_C:
ResetMoveFifo();
moveframes = 0;
bInMove = kFalse;
edi = 0;
// int esi = totalclock;
esi = totalclock;
tclocks = totalclock;
nPlayerDAng = 0;
lPlayerXVel = 0;
lPlayerYVel = 0;
@ -2104,16 +2099,12 @@ main_loc_C:
mysetbrightness((uint8_t)nGamma);
//int edi = totalclock;
edi = totalclock;
main_loc_D:
// ORANGE
tclocks2 = totalclock;
// Game Loop
while (1)
{
if (levelnew >= 0)
{
goto main_loc_G;
}
else
{
goto LOOP1;
CONTROL_BindsEnabled = 1;
// Section B
if (!nCDTrackLength && !nFreeze && !nNetPlayerCount)
@ -2168,13 +2159,13 @@ main_loc_D:
bInMove = kTrue;
moveframes = (totalclock - edi) / 4;
moveframes = (totalclock - tclocks2) / 4;
if (moveframes > 4)
moveframes = 4;
if (moveframes != 0)
edi = totalclock;
tclocks2 = totalclock;
if (bPlayback)
{
@ -2191,14 +2182,10 @@ main_loc_D:
fclose(vcrfp);
}
// now we go to main_loc_I:
goto main_loc_I;
goto MENU;
}
}
else
{
// loc_11EE0
if (bRecord || moveframes)
else if (bRecord || moveframes)
{
GetLocalInput();
@ -2214,7 +2201,6 @@ main_loc_D:
sPlayerInput[nLocalPlayer].horizon = nVertPan[nLocalPlayer];
}
}
// loc_11F72:
if (bRecord && !bInDemo) {
@ -2243,13 +2229,8 @@ main_loc_D:
}
// loc_11FEE:
esi += moveframes * 4;
main_loc_J:
handleevents();
if (moveframes && levelnew < 0)
tclocks += moveframes * 4;
while (moveframes&& levelnew < 0)
{
FixPalette();
@ -2264,10 +2245,8 @@ main_loc_J:
DoFailedFinalScene();
levelnew = 100;
goto main_loc_O;
break;
}
else
{
// Pink section
lCountDown--;
DrawClock();
@ -2288,7 +2267,6 @@ main_loc_J:
}
}
}
}
// YELLOW SECTION
MoveThings();
@ -2314,33 +2292,23 @@ main_loc_J:
if (!nNetTime) {
nFreeze = 3;
}
goto main_loc_J;
}
else if (nNetTime != 0)
{
goto main_loc_J;
}
else // nNetTime == 0
else if (nNetTime == 0)
{
if (BUTTON(gamefunc_Open))
{
CONTROL_ClearButton(gamefunc_Open);
goto main_loc_K;
}
else {
goto main_loc_J;
goto MENU2;
}
}
}
// END YELLOW SECTION
main_loc_O:
// loc_12149:
if (bInDemo)
{
while (esi > totalclock) { handleevents(); }
esi = totalclock;
while (tclocks > totalclock) { handleevents(); }
tclocks = totalclock;
}
bInMove = kFalse;
@ -2350,7 +2318,32 @@ main_loc_J:
if (BUTTON(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])
{
@ -2362,7 +2355,7 @@ main_loc_J:
CONTROL_ClearButton(gamefunc_Map);
if (!nFreeze) {
bMapMode = bMapMode == 0;
bMapMode = !bMapMode;
}
}
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();
if (PlayerList[nLocalPlayer].nHealth <= 0)
{
SetAirFrame();
}
else
if (PlayerList[nLocalPlayer].nHealth > 0)
{
if (BUTTON(gamefunc_Inventory_Left))
{
@ -2431,164 +2412,15 @@ main_loc_J:
CONTROL_ClearButton(gamefunc_Inventory);
}
}
goto main_loc_N;
else
SetAirFrame();
}
}
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)
if (record_mode == 3 && movefifopos == movefifoend) {
levelnew = 0;
}
fps++;
goto main_loc_D;
main_loc_G:
if (nPlayerLives[nLocalPlayer] <= 0) {
goto main_loc_I;
}
else
{
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:
EXITGAME:
if (bRecord) {
fclose(vcrfp);
}
@ -2616,8 +2448,7 @@ main_Exit:
}
}
bail2dos("\n");
ShutDown();
return 0;
}