From ab006f509208cab554b547b899213b0435ae471d Mon Sep 17 00:00:00 2001 From: Lemonymous <10490534+Lemonymous@users.noreply.github.com> Date: Thu, 13 Oct 2022 22:08:49 +0200 Subject: [PATCH 1/5] Create separation for squad index types Create separation between how the game refers to squads by index, and how the mod loader refers to squads by index. For the game a squad `choice` is the index for the chosen squad. Squad Choice is: - 0-indexed - 0-7 for non-ae-squads - 8-9 for random/custom - 10 for secret squad - 11-15 for ae-squads For the mod loader a squad `index` is the index for the squad in mod loader tables. Squad Index is: - 1-indexed - 1-8 for non-ae-squads - 9 for secret squad - 10-14 for ae-squads ## New functions - number modApi:squadIndex2Choice(number) - Returns the squad choice corresponding to the specified squad index. Returns -1 for indexes that cannot be mapped to a squad choice. - number modApi:squadChoice2Index(choice) - Returns the squad index corresponding to the specified squad choice. Returns -1 for choices that cannot be mapped to a squad index. - table modApi:getSquadForChoice(number) - Returns the flat list of a squad currently mapped to the specified squad choice. Where content of the return table is as follows: ``` [1] = squad name [2-4] = mechs id = squad id ``` --- scripts/mod_loader/altered/squad.lua | 28 +++++++------- scripts/mod_loader/bootstrap/constants.lua | 22 +++++++++++ scripts/mod_loader/modapi/squad.lua | 43 ++++++++++++++++++++++ 3 files changed, 79 insertions(+), 14 deletions(-) diff --git a/scripts/mod_loader/altered/squad.lua b/scripts/mod_loader/altered/squad.lua index 84703892..dd5054d7 100644 --- a/scripts/mod_loader/altered/squad.lua +++ b/scripts/mod_loader/altered/squad.lua @@ -1,33 +1,33 @@ local oldGetStartingSquad = getStartingSquad function getStartingSquad(choice) - if choice == 0 then + if choice == modApi.constants.SQUAD_CHOICE_START then loadPilotsOrder() loadSquadSelection() end - -- squads 0-7 are the first 8 vanilla squads - -- 8 and 9 are random and custom - -- squad 10 is secret, 11-16 are Advanced - if (choice >= 0 and choice <= 7) or (choice >= 10 and choice <= (modApi.constants.MAX_SQUADS + 2)) then - if choice >= 10 then - choice = choice - 2 - end - local index = modApi.squadIndices[choice + 1] + if true + and choice ~= modApi.constants.SQUAD_CHOICE_RANDOM + and choice ~= modApi.constants.SQUAD_CHOICE_CUSTOM + and choice >= modApi.constants.SQUAD_CHOICE_START + and choice <= modApi.constants.SQUAD_CHOICE_END + then + local index = modApi:squadChoice2Index(choice) + local redirectedIndex = modApi.squadIndices[index] modApi:setText( - "TipTitle_"..modApi.squadKeys[choice + 1], - modApi.squad_text[2 * (index - 1) + 1] + "TipTitle_"..modApi.squadKeys[index], + modApi.squad_text[2 * (redirectedIndex - 1) + 1] ) modApi:setText( - "TipText_"..modApi.squadKeys[choice + 1], - modApi.squad_text[2 * (index - 1) + 2] + "TipText_"..modApi.squadKeys[index], + modApi.squad_text[2 * (redirectedIndex - 1) + 2] ) -- Create a copy of the squad table, and remove 'id' -- from it before returning, in order for the game -- not to mistake the entry for a mech. - local squad = copy_table(modApi.mod_squads[index]) + local squad = copy_table(modApi:getSquadForChoice(choice)) squad.id = nil return squad diff --git a/scripts/mod_loader/bootstrap/constants.lua b/scripts/mod_loader/bootstrap/constants.lua index dd2f3a1c..2df32653 100644 --- a/scripts/mod_loader/bootstrap/constants.lua +++ b/scripts/mod_loader/bootstrap/constants.lua @@ -26,4 +26,26 @@ modApi.constants = { PILOT_CONFIG_POD_NORMAL = 1, PILOT_CONFIG_POD_ADVANCED = 2, PILOT_CONFIG_RECRUIT = 4, + + -- squad choice constants + -- how the game arranges squads + -- 0-indexed + -- squads 0-7 are the first 8 vanilla squads + -- 8 and 9 are random and custom + -- squad 10 is secret, 11-15 are Advanced + SQUAD_CHOICE_START = 0, + SQUAD_CHOICE_END = 15, + SQUAD_CHOICE_RANDOM = 8, + SQUAD_CHOICE_CUSTOM = 9, + SQUAD_CHOICE_SECRET = 10, + + -- squad index constants + -- how the mod loader arranges squads + -- 1-indexed + -- 1-8 are the first 8 vanilla squads + -- random and custom are excluded + -- 9 is secret, 10-14 are Advanced + SQUAD_INDEX_START = 1, + SQUAD_INDEX_END = 14, + SQUAD_INDEX_SECRET = 9, } diff --git a/scripts/mod_loader/modapi/squad.lua b/scripts/mod_loader/modapi/squad.lua index 9dbbc837..4d6e09d3 100644 --- a/scripts/mod_loader/modapi/squad.lua +++ b/scripts/mod_loader/modapi/squad.lua @@ -69,6 +69,49 @@ function modApi:addSquad(squad, name, desc, icon) table.insert(self.squad_icon, icon or "resources/mods/squads/unknown.png") end +-- Convert from how the mod loader indexes squads to how the game indexes squads +function modApi:squadIndex2Choice(index) + Assert.Equals("table", type(self), "Check for . vs :") + + if index > self.constants.SQUAD_INDEX_END then + LOG("WARNING: Invalid squad index -> choice conversion") + return -1 + elseif index >= self.constants.SQUAD_INDEX_CUSTOM then + return index + 2 + else + return index + end +end + +-- Convert from how the game indexes squads to how the mod loader indexes squads +function modApi:squadChoice2Index(choice) + Assert.Equals("table", type(self), "Check for . vs :") + + if false + or choice == self.constants.SQUAD_CHOICE_RANDOM + or choice == self.constants.SQUAD_CHOICE_CUSTOM + or choice > self.constants.SQUAD_CHOICE_END + then + LOG("WARNING: Invalid squad choice -> index conversion") + return -1 + end + + if choice > self.constants.SQUAD_CHOICE_CUSTOM then + return choice - 1 + else + return choice + 1 + end +end + +function modApi:getSquadForChoice(choice) + Assert.Equals("table", type(self), "Check for . vs :") + Assert.NotEquals(nil, self.squadIndices, "Squad order not loaded") + + local index = self:squadChoice2Index(choice) + + return self.mod_squads[self.squadIndices[index]] +end + local function onGameEntered() local squadData = GAME.additionalSquadData local squadId = squadData.squad From 3fbd2be098a08c0f2a52ad7904eb0d83b6b820a6 Mon Sep 17 00:00:00 2001 From: Lemonymous <10490534+Lemonymous@users.noreply.github.com> Date: Sat, 15 Oct 2022 10:24:04 +0200 Subject: [PATCH 2/5] Fix squad index<->choice functions - Fix squadIndex2Choice incorrectly not 0-indexing the result - Add lower limit to invalid conversion warnings - Replace comparison from SQUAD_INDEX_CUSTOM to SQUAD_INDEX_SECRET - Replace comparison from SQUAD_CHOICE_CUSTOM to SQUAD_CHOICE_SECRET --- scripts/mod_loader/modapi/squad.lua | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/scripts/mod_loader/modapi/squad.lua b/scripts/mod_loader/modapi/squad.lua index 4d6e09d3..45a2bf07 100644 --- a/scripts/mod_loader/modapi/squad.lua +++ b/scripts/mod_loader/modapi/squad.lua @@ -73,13 +73,16 @@ end function modApi:squadIndex2Choice(index) Assert.Equals("table", type(self), "Check for . vs :") - if index > self.constants.SQUAD_INDEX_END then + if false + or index < self.constants.SQUAD_INDEX_START + or index > self.constants.SQUAD_INDEX_END + then LOG("WARNING: Invalid squad index -> choice conversion") return -1 - elseif index >= self.constants.SQUAD_INDEX_CUSTOM then - return index + 2 + elseif index >= self.constants.SQUAD_INDEX_SECRET then + return index + 1 else - return index + return index - 1 end end @@ -90,13 +93,14 @@ function modApi:squadChoice2Index(choice) if false or choice == self.constants.SQUAD_CHOICE_RANDOM or choice == self.constants.SQUAD_CHOICE_CUSTOM + or choice < self.constants.SQUAD_CHOICE_START or choice > self.constants.SQUAD_CHOICE_END then LOG("WARNING: Invalid squad choice -> index conversion") return -1 end - if choice > self.constants.SQUAD_CHOICE_CUSTOM then + if choice >= self.constants.SQUAD_CHOICE_SECRET then return choice - 1 else return choice + 1 From 11c0e403133e47fe962d26130882698727e6a798 Mon Sep 17 00:00:00 2001 From: Lemonymous <10490534+Lemonymous@users.noreply.github.com> Date: Sat, 15 Oct 2022 11:26:20 +0200 Subject: [PATCH 3/5] Edit argument validation ## getSquadForChoice - Assert that choice is within bounds and not Random or Custom. - loadSquadSelection if modApi.squadIndices is nil ## squadIndex2Choice - Create separation between validation block and the actual function content --- scripts/mod_loader/modapi/squad.lua | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/scripts/mod_loader/modapi/squad.lua b/scripts/mod_loader/modapi/squad.lua index 45a2bf07..b4073a27 100644 --- a/scripts/mod_loader/modapi/squad.lua +++ b/scripts/mod_loader/modapi/squad.lua @@ -79,7 +79,9 @@ function modApi:squadIndex2Choice(index) then LOG("WARNING: Invalid squad index -> choice conversion") return -1 - elseif index >= self.constants.SQUAD_INDEX_SECRET then + end + + if index >= self.constants.SQUAD_INDEX_SECRET then return index + 1 else return index - 1 @@ -109,10 +111,21 @@ end function modApi:getSquadForChoice(choice) Assert.Equals("table", type(self), "Check for . vs :") - Assert.NotEquals(nil, self.squadIndices, "Squad order not loaded") - local index = self:squadChoice2Index(choice) + if false + or choice == self.constants.SQUAD_CHOICE_RANDOM + or choice == self.constants.SQUAD_CHOICE_CUSTOM + or choice < self.constants.SQUAD_CHOICE_START + or choice > self.constants.SQUAD_CHOICE_END + then + Assert.Error("Invalid squad choice") + end + if self.squadIndices == nil then + loadSquadSelection() + end + + local index = self:squadChoice2Index(choice) return self.mod_squads[self.squadIndices[index]] end From 461049b55a4b4d2b25502c83c35c51ca68d76b94 Mon Sep 17 00:00:00 2001 From: Lemonymous <10490534+Lemonymous@users.noreply.github.com> Date: Sat, 15 Oct 2022 11:29:16 +0200 Subject: [PATCH 4/5] Change modApi.getSquadForChoice return format Change modApi.getSquadForChoice from returning a flat list of the squad. From ``` { [1] - squad name [2-4] - mech id's ["id"] - squad id } ``` to ``` { ["name"] - squad name ["id"] - squad id ["mechs"] - table of mech id's } ``` --- scripts/mod_loader/altered/squad.lua | 9 +++------ scripts/mod_loader/modapi/squad.lua | 12 +++++++++++- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/scripts/mod_loader/altered/squad.lua b/scripts/mod_loader/altered/squad.lua index dd5054d7..df6a1027 100644 --- a/scripts/mod_loader/altered/squad.lua +++ b/scripts/mod_loader/altered/squad.lua @@ -24,13 +24,10 @@ function getStartingSquad(choice) modApi.squad_text[2 * (redirectedIndex - 1) + 2] ) - -- Create a copy of the squad table, and remove 'id' - -- from it before returning, in order for the game - -- not to mistake the entry for a mech. - local squad = copy_table(modApi:getSquadForChoice(choice)) - squad.id = nil + local squad = modApi:getSquadForChoice(choice) - return squad + -- Return the squad in a flat list as the game expects + return { squad.name, squad.mechs[1], squad.mechs[2], squad.mechs[3] } else return oldGetStartingSquad(choice) end diff --git a/scripts/mod_loader/modapi/squad.lua b/scripts/mod_loader/modapi/squad.lua index b4073a27..97025701 100644 --- a/scripts/mod_loader/modapi/squad.lua +++ b/scripts/mod_loader/modapi/squad.lua @@ -126,7 +126,17 @@ function modApi:getSquadForChoice(choice) end local index = self:squadChoice2Index(choice) - return self.mod_squads[self.squadIndices[index]] + local squad_flat = self.mod_squads[self.squadIndices[index]] + + return { + name = squad_flat[1], + id = squad_flat.id, + mechs = { + squad_flat[2], + squad_flat[3], + squad_flat[4], + } + } end local function onGameEntered() From af05a662a4ea80def156f7c1d4565d010ad417fd Mon Sep 17 00:00:00 2001 From: Lemonymous <10490534+Lemonymous@users.noreply.github.com> Date: Thu, 27 Oct 2022 11:30:04 +0200 Subject: [PATCH 5/5] Update warning logs --- scripts/mod_loader/modapi/squad.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/mod_loader/modapi/squad.lua b/scripts/mod_loader/modapi/squad.lua index 97025701..0f4ae39d 100644 --- a/scripts/mod_loader/modapi/squad.lua +++ b/scripts/mod_loader/modapi/squad.lua @@ -77,7 +77,7 @@ function modApi:squadIndex2Choice(index) or index < self.constants.SQUAD_INDEX_START or index > self.constants.SQUAD_INDEX_END then - LOG("WARNING: Invalid squad index -> choice conversion") + LOGW("Invalid squad index -> choice conversion") return -1 end @@ -98,7 +98,7 @@ function modApi:squadChoice2Index(choice) or choice < self.constants.SQUAD_CHOICE_START or choice > self.constants.SQUAD_CHOICE_END then - LOG("WARNING: Invalid squad choice -> index conversion") + LOGW("Invalid squad choice -> index conversion") return -1 end