From 205ecd4e87506d2dbf53dfb83c0f27c4f3ec8919 Mon Sep 17 00:00:00 2001
From: Lugent <35547583+Lugent@users.noreply.github.com>
Date: Sat, 26 Apr 2025 16:22:29 -0400
Subject: [PATCH] Implement lua conditions correctly?

---
 src/deh_soc.c |  4 ++--
 src/m_cond.c  | 43 ++++++++++++++++++++++++++++++-------------
 src/m_cond.h  |  5 +++--
 3 files changed, 35 insertions(+), 17 deletions(-)

diff --git a/src/deh_soc.c b/src/deh_soc.c
index 1e1a64295d..43f55e7d07 100644
--- a/src/deh_soc.c
+++ b/src/deh_soc.c
@@ -3746,9 +3746,9 @@ static void readcondition(UINT8 set, UINT32 id, char *word2)
 		ty = UC_LUA;
 		re = atoi(params[1]);
 
-		if (re <= 0 || re > MAXLUACONDITIONS)
+		if (re <= 0)
 		{
-			deh_warning("Lua condition %d out of range (1 - %d)", re, MAXLUACONDITIONS);
+			deh_warning("Lua condition %d must be positive", re);
 			return;
 		}
 	}
diff --git a/src/m_cond.c b/src/m_cond.c
index 152b1fde2e..a811329d3c 100644
--- a/src/m_cond.c
+++ b/src/m_cond.c
@@ -46,6 +46,10 @@ extraemblem_t *extra_emblems;
 INT32 num_unlockables;
 unlockable_t *unlockables;
 
+// Lua
+INT32 num_lua_unlockables;
+unlockable_t *lua_conditions;
+
 // Temporary holding place for nights data for the current map
 nightsdata_t ntemprecords[MAXPLAYERS];
 
@@ -80,10 +84,13 @@ void M_CopyGameData(gamedata_t *dest, gamedata_t *src)
 	memcpy(dest->extra_collected, src->extra_collected, sizeof(boolean) * src->num_extra_collected);
 	dest->unlocked = Z_Realloc(dest->unlocked, sizeof(boolean) * src->num_unlocked, PU_STATIC, NULL);
 	memcpy(dest->unlocked, src->unlocked, sizeof(boolean) * src->num_unlocked);
+	dest->lua = Z_Realloc(dest->lua, sizeof(boolean) * src->num_lua, PU_STATIC, NULL);
+	memcpy(dest->lua, src->lua, sizeof(boolean) * src->num_lua);
 	dest->num_achieved = src->num_achieved;
 	dest->num_collected = src->num_collected;
 	dest->num_extra_collected = src->num_extra_collected;
 	dest->num_unlocked = src->num_unlocked;
+	dest->num_lua = src->num_lua;
 
 	memcpy(dest->mapvisited, src->mapvisited, sizeof(dest->mapvisited));
 
@@ -159,20 +166,23 @@ void M_ClearConditionSet(UINT32 set)
 // Clear ALL secrets.
 void M_ClearSecrets(gamedata_t *data)
 {
-	INT32 i;
-
 	memset(data->mapvisited, 0, sizeof(data->mapvisited));
 
-	for (i = 0; i < MAXEMBLEMS; ++i)
-		data->collected[i] = false;
-	for (i = 0; i < MAXEXTRAEMBLEMS; ++i)
-		data->extra_collected[i] = false;
-	for (i = 0; i < MAXUNLOCKABLES; ++i)
-		data->unlocked[i] = false;
-	for (i = 0; i < MAXCONDITIONSETS; ++i)
-		data->achieved[i] = false;
-	for (i = 0; i < MAXLUACONDITIONS; ++i)
-		data->lua[i] = false;
+	data->num_collected = 0;
+	data->num_extra_collected = 0;
+	data->num_unlocked = 0;
+	data->num_achieved = 0;
+	data->num_lua = 0;
+	Z_Free(data->collected);
+	data->collected = NULL;
+	Z_Free(data->extra_collected);
+	data->extra_collected = NULL;
+	Z_Free(data->unlocked);
+	data->unlocked = NULL;
+	Z_Free(data->achieved);
+	data->achieved = NULL;
+	Z_Free(data->lua);
+	data->lua = NULL;
 
 	data->timesBeaten = data->timesBeatenWithEmeralds = data->timesBeatenUltimate = 0;
 
@@ -233,7 +243,7 @@ static UINT8 M_CheckCondition(condition_t *cn, gamedata_t *data)
 		case UC_EXTRAEMBLEM: // Requires extra emblem x to be obtained
 			return cn->requirement-1 < data->num_extra_collected && data->extra_collected[cn->requirement-1];
 		case UC_LUA:
-			return data->lua[cn->requirement-1];
+			return cn->requirement-1 < data->num_lua && data->lua[cn->requirement-1];
 		case UC_CONDITIONSET: // requires condition set x to already be achieved
 			return M_Achieved(cn->requirement-1, data);
 	}
@@ -269,6 +279,13 @@ void M_CheckGamedataBuffers(gamedata_t *data)
 		memset(&data->unlocked[data->num_unlocked], 0, (num_unlockables - data->num_unlocked) * sizeof(boolean));
 		data->num_unlocked = num_unlockables;
 	}
+
+	if (data->num_lua < num_lua_unlockables)
+	{
+		data->unlocked = Z_Realloc(data->unlocked, num_lua_unlockables * sizeof(boolean), PU_STATIC, NULL);
+		memset(&data->unlocked[data->num_unlocked], 0, (num_lua_unlockables - data->num_unlocked) * sizeof(boolean));
+		data->num_unlocked = num_lua_unlockables;
+	}
 }
 
 static UINT8 M_CheckConditionSet(conditionset_t *c, gamedata_t *data)
diff --git a/src/m_cond.h b/src/m_cond.h
index 72a083ca95..f558ff00e6 100644
--- a/src/m_cond.h
+++ b/src/m_cond.h
@@ -196,6 +196,7 @@ typedef struct
 	INT32 num_collected;
 	INT32 num_extra_collected;
 	INT32 num_unlocked;
+	INT32 num_lua;
 
 	// CONDITION SETS ACHIEVED
 	boolean *achieved;
@@ -207,10 +208,10 @@ typedef struct
 	boolean *extra_collected;
 
 	// UNLOCKABLES UNLOCKED
-	boolean unlocked[MAXUNLOCKABLES];
+	boolean *unlocked;
 
 	// LUA DATA (NOT SAVED INTO GAMEDATA)
-	boolean lua[MAXLUACONDITIONS];
+	boolean *lua;
 
 	// TIME ATTACK DATA
 	recorddata_t *mainrecords[NUMMAPS];
-- 
GitLab