shithub: cstory

Download patch

ref: e1d3898d59b4759efb4a04395d0f00c0786c910f
parent: c9d5b3d03afbdf613f8c84f390b3646bafdaaec0
author: Clownacy <Clownacy@users.noreply.github.com>
date: Mon May 13 17:31:14 EDT 2019

Made SelStage.cpp ASM-accurate

--- a/msvc2003/devilution/comparer-config.toml
+++ b/msvc2003/devilution/comparer-config.toml
@@ -963,6 +963,30 @@
 addr = 0x41D550
 
 [[func]]
+name = "ClearPermitStage"
+addr = 0x41D610
+
+[[func]]
+name = "AddPermitStage"
+addr = 0x41D630
+
+[[func]]
+name = "SubPermitStage"
+addr = 0x41D6A0
+
+[[func]]
+name = "MoveStageSelectCursor"
+addr = 0x41D740
+
+[[func]]
+name = "PutStageSelectObject"
+addr = 0x41D840
+
+[[func]]
+name = "StageSelectLoop"
+addr = 0x41DA00
+
+[[func]]
 name = "ShootBullet_Frontia1"
 addr = 0x41DBD0
 
--- a/src/SelStage.cpp
+++ b/src/SelStage.cpp
@@ -21,73 +21,82 @@
 	memset(gPermitStage, 0, 0x40);
 }
 
-bool AddPermitStage(int index, int event)
+BOOL AddPermitStage(int index, int event)
 {
-	for (int i = 0; i < 8; ++i)
+	int i = 0;
+	while (i < 8)
 	{
-		if (gPermitStage[i].index == 0 || gPermitStage[i].index == index)
-		{
-			gPermitStage[i].index = index;
-			gPermitStage[i].event = event;
-			return true;
-		}
+		if (gPermitStage[i].index == index)
+			break;
+
+		if (gPermitStage[i].index == 0)
+			break;
+
+		++i;
 	}
 
-	return false;
+	if (i == 8)
+		return FALSE;
+
+	gPermitStage[i].index = index;
+	gPermitStage[i].event = event;
+
+	return TRUE;
 }
 
-bool SubPermitStage(int index)
+BOOL SubPermitStage(int index)
 {
 	int i;
-	for (i = 0; i < 8 && gPermitStage[i].index != index; ++i);
+	for (i = 0; i < 8; ++i)
+		if (gPermitStage[i].index == index)
+			break;
 
 #ifdef FIX_BUGS
-	if (i != 8)
+	if (i == 8)
 #else
-	if (i != 32)
+	if (i == 32)
 #endif
-	{
-		int ia;
-		for (ia = i + 1; ia < 8; ++ia)
-		{
-			gPermitStage[ia - 1].index = gPermitStage[ia].index;
-			gPermitStage[ia - 1].event = gPermitStage[ia].event;
-		}
+		return FALSE;
 
-		gPermitStage[ia - 1].index = 0;
-		gPermitStage[ia - 1].event = 0;
-
-		return true;
+	for (i += 1; i < 8; ++i)
+	{
+		gPermitStage[i - 1] = gPermitStage[i];
 	}
 
-	return false;
+	gPermitStage[i - 1].index = 0;
+	gPermitStage[i - 1].event = 0;
+
+	return TRUE;
 }
 
 void MoveStageSelectCursor(void)
 {
-	int stage_num;
-	for (stage_num = 0; gPermitStage[stage_num].index != 0; ++stage_num);
+	int stage_num = 0;
+	while (gPermitStage[stage_num].index != 0)
+		++stage_num;
 
-	if (stage_num)
-	{
-		if (gKeyTrg & gKeyLeft)
-			--gSelectedStage;
+	int stage_x = (WINDOW_WIDTH - 40 * stage_num) / 2;	// Unused
 
-		if (gKeyTrg & gKeyRight)
-			++gSelectedStage;
+	if (stage_num == 0)
+		return;
 
-		if (gSelectedStage < 0)
-			gSelectedStage = stage_num - 1;
+	if (gKeyTrg & gKeyLeft)
+		--gSelectedStage;
 
-		if (stage_num - 1 < gSelectedStage)
-			gSelectedStage = 0;
+	if (gKeyTrg & gKeyRight)
+		++gSelectedStage;
 
-		if ((gKeyRight | gKeyLeft) & gKeyTrg)
-			StartTextScript(gPermitStage[gSelectedStage].index + 1000);
+	if (gSelectedStage < 0)
+		gSelectedStage = stage_num - 1;
 
-		if ((gKeyRight | gKeyLeft) & gKeyTrg)
-			PlaySoundObject(1, 1);
-	}
+	if (gSelectedStage > stage_num - 1)
+		gSelectedStage = 0;
+
+	if ((gKeyLeft | gKeyRight) & gKeyTrg)
+		StartTextScript(gPermitStage[gSelectedStage].index + 1000);
+
+	if ((gKeyLeft | gKeyRight) & gKeyTrg)
+		PlaySoundObject(1, 1);
 }
 
 void PutStageSelectObject(void)
@@ -103,25 +112,33 @@
 
 	RECT rcTitle1 = {80, 64, 144, 72};
 
+	int i;
+	int stage_num;
+	int stage_x;
+	RECT rcStage;
+
 	if (gStageSelectTitleY > (WINDOW_HEIGHT / 2) - 74)
 		--gStageSelectTitleY;
 
 	PutBitmap3(&rcView, (WINDOW_WIDTH / 2) - 32, gStageSelectTitleY, &rcTitle1, SURFACE_ID_TEXT_BOX);
 
-	int stage_num;
-	for (stage_num = 0; gPermitStage[stage_num].index; ++stage_num);
+	stage_num = 0;
+	while (gPermitStage[stage_num].index)
+		++stage_num;
 
 	++flash;
 
 	if (stage_num)
 	{
-		int stage_x = (WINDOW_WIDTH - 40 * stage_num) / 2;
+		stage_x = (WINDOW_WIDTH - 40 * stage_num) / 2;
 
 		PutBitmap3(&rcView, stage_x + 40 * gSelectedStage, (WINDOW_HEIGHT / 2) - 56, &rcCur[(flash >> 1) % 2], SURFACE_ID_TEXT_BOX);
 
-		for (int i = 0; i < 8 && gPermitStage[i].index; ++i)
+		for (i = 0; i < 8; ++i)
 		{
-			RECT rcStage;
+			if (gPermitStage[i].index == 0)
+				break;
+
 			rcStage.left = 32 * (gPermitStage[i].index % 8);
 			rcStage.right = rcStage.left + 32;
 			rcStage.top = 16 * (gPermitStage[i].index / 8);
@@ -145,26 +162,30 @@
 	gStageSelectTitleY = (WINDOW_HEIGHT / 2) - 66;
 	StartTextScript(gPermitStage[gSelectedStage].index + 1000);
 
-	do
+	for (;;)
 	{
 		GetTrg();
 
 		if (gKey & KEY_ESCAPE)
 		{
-			int escRet = Call_Escape(ghWnd);
-			if (escRet == 0)
-				return 0;
-			if (escRet == 2)
-				return 2;
+			switch (Call_Escape(ghWnd))
+			{
+				case 0:
+					return 0;
+				case 2:
+					return 2;
+			}
 		}
 
 		MoveStageSelectCursor();
 
-		int tscRet = TextScriptProc();
-		if (tscRet == 0)
-			return 0;
-		if (tscRet == 2)
-			return 2;
+		switch (TextScriptProc())
+		{
+			case 0:
+				return 0;
+			case 2:
+				return 2;
+		}
 
 #ifdef FIX_BUGS
 		PutBitmap4(&rcView, 0, 0, &rcView, SURFACE_ID_SCREEN_GRAB);
@@ -178,12 +199,9 @@
 		if (gKeyTrg & gKeyOk)
 		{
 			StopTextScript();
-			LoadTextScript_Stage(old_script_path);
-			*p_event = gPermitStage[gSelectedStage].event;
-			return 1;
+			break;
 		}
-
-		if (gKeyTrg & gKeyCancel)
+		else if (gKeyTrg & gKeyCancel)
 		{
 			StopTextScript();
 			LoadTextScript_Stage(old_script_path);
@@ -192,8 +210,12 @@
 		}
 
 		PutFramePerSecound();
+
+		if (!Flip_SystemTask(ghWnd))
+			return 0;
 	}
-	while (Flip_SystemTask(ghWnd));
 
-	return 0;
+	LoadTextScript_Stage(old_script_path);
+	*p_event = gPermitStage[gSelectedStage].event;
+	return 1;
 }
--- a/src/SelStage.h
+++ b/src/SelStage.h
@@ -1,5 +1,7 @@
 #pragma once
 
+#include "WindowsWrapper.h"
+
 struct PERMIT_STAGE
 {
 	int index;
@@ -9,8 +11,8 @@
 extern PERMIT_STAGE gPermitStage[8];
 
 void ClearPermitStage(void);
-bool AddPermitStage(int index, int event);
-bool SubPermitStage(int index);
+BOOL AddPermitStage(int index, int event);
+BOOL SubPermitStage(int index);
 void MoveStageSelectCursor(void);
 void PutStageSelectObject(void);
 int StageSelectLoop(int *p_event);