Skip to content

Commit

Permalink
Merge pull request #177 from itb-community/refactor/squadIndex
Browse files Browse the repository at this point in the history
Refactor code dealing with squad index
  • Loading branch information
Lemonymous committed Oct 27, 2022
2 parents 51ec7e9 + af05a66 commit bbb107f
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 19 deletions.
35 changes: 16 additions & 19 deletions scripts/mod_loader/altered/squad.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down
22 changes: 22 additions & 0 deletions scripts/mod_loader/bootstrap/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
70 changes: 70 additions & 0 deletions scripts/mod_loader/modapi/squad.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bbb107f

Please sign in to comment.