diff --git a/scripts/mod_loader/altered/squad.lua b/scripts/mod_loader/altered/squad.lua index 84703892..df6a1027 100644 --- a/scripts/mod_loader/altered/squad.lua +++ b/scripts/mod_loader/altered/squad.lua @@ -1,36 +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]) - 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/bootstrap/constants.lua b/scripts/mod_loader/bootstrap/constants.lua index 59a5ca4d..35995072 100644 --- a/scripts/mod_loader/bootstrap/constants.lua +++ b/scripts/mod_loader/bootstrap/constants.lua @@ -27,6 +27,28 @@ modApi.constants = { 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, + -- ui constants ORIENTATION_HORIZONTAL = true, ORIENTATION_VERTICAL = false, diff --git a/scripts/mod_loader/modapi/squad.lua b/scripts/mod_loader/modapi/squad.lua index 9dbbc837..0f4ae39d 100644 --- a/scripts/mod_loader/modapi/squad.lua +++ b/scripts/mod_loader/modapi/squad.lua @@ -69,6 +69,76 @@ 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 false + or index < self.constants.SQUAD_INDEX_START + or index > self.constants.SQUAD_INDEX_END + then + LOGW("Invalid squad index -> choice conversion") + return -1 + end + + if index >= self.constants.SQUAD_INDEX_SECRET then + return index + 1 + else + return index - 1 + 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_START + or choice > self.constants.SQUAD_CHOICE_END + then + LOGW("Invalid squad choice -> index conversion") + return -1 + end + + if choice >= self.constants.SQUAD_CHOICE_SECRET then + return choice - 1 + else + return choice + 1 + end +end + +function modApi:getSquadForChoice(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_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) + 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() local squadData = GAME.additionalSquadData local squadId = squadData.squad