Skip to content

Commit

Permalink
Add deployment related events and functions
Browse files Browse the repository at this point in the history
### Functions
- modApi.deployment.isDeploymentPhase
- modApi.deployment.isLandingPhase
- modApi.deployment.getSelected
- modApi.deployment.getDeployed
- modApi.deployment.getRemaining

### Events
- modApi.events.onDeploymentPhaseStart
- modApi.events.onLandingPhaseStart
- modApi.events.onDeploymentPhaseEnd
- modApi.events.onPawnUnselectedForDeployment
- modApi.events.onPawnSelectedForDeployment
- modApi.events.onPawnDeployed
- modApi.events.onPawnUndeployed
- modApi.events.onPawnLanding
- modApi.events.onPawnLanded
  • Loading branch information
Lemonymous authored and KnightMiner committed Sep 18, 2022
1 parent 9b950b4 commit 57f9150
Show file tree
Hide file tree
Showing 4 changed files with 303 additions and 1 deletion.
10 changes: 10 additions & 0 deletions scripts/mod_loader/bootstrap/modApi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ t.onGameVictory = Event()
t.onSquadEnteredGame = Event()
t.onSquadExitedGame = Event()

t.onDeploymentPhaseStart = Event()
t.onLandingPhaseStart = Event()
t.onDeploymentPhaseEnd = Event()
t.onPawnUnselectedForDeployment = Event()
t.onPawnSelectedForDeployment = Event()
t.onPawnDeployed = Event()
t.onPawnUndeployed = Event()
t.onPawnLanding = Event()
t.onPawnLanded = Event()

t.onShiftToggled = Event()
t.onAltToggled = Event()
t.onCtrlToggled = Event()
Expand Down
35 changes: 35 additions & 0 deletions scripts/mod_loader/compat/nuke.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,42 @@

-- nuke libraries that have been migrated to the mod loader

mt_redirect = {
__index = function(self, key)
local redirect = self.redirects[key]

if redirect then
return redirect()
end
end
}

SquadEvents = { version = tostring(INT_MAX) }
DifficultyEvents = { version = tostring(INT_MAX) }

modApi.events.onRealDifficultyChanged = modApi.events.onDifficultyChanged

DetectDeployment = { version = tostring(INT_MAX),
events = {
redirects = {
onDeploymentPhaseStart = function() return modApi.events.onDeploymentPhaseStart end,
onLandingPhaseStart = function() return modApi.events.onLandingPhaseStart end,
onDeploymentPhaseEnd = function() return modApi.events.onDeploymentPhaseEnd end,
onPawnUnselected = function() return modApi.events.onPawnUnselectedForDeployment end,
onPawnSelected = function() return modApi.events.onPawnSelectedForDeployment end,
onPawnDeployed = function() return modApi.events.onPawnDeployed end,
onPawnUndeployed = function() return modApi.events.onPawnUndeployed end,
onPawnLanding = function() return modApi.events.onPawnLanding end,
onPawnLanded = function() return modApi.events.onPawnLanded end,
}
},
redirects = {
isDeploymentPhase = function() return modApi.deployment.isDeploymentPhase end,
isLandingPhase = function() return modApi.deployment.isLandingPhase end,
getSelected = function() return modApi.deployment.getSelected end,
getDeployed = function() return modApi.deployment.getDeployed end,
getRemaining = function() return modApi.deployment.getRemaining end,
}
}
setmetatable(DetectDeployment, mt_redirect)
setmetatable(DetectDeployment.events, mt_redirect)
3 changes: 2 additions & 1 deletion scripts/mod_loader/modapi/__scripts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ local scripts = {
"board",
"pawn",
"localization",
"compat"
"compat",
"deployment",
}

local rootpath = GetParentPath(...)
Expand Down
256 changes: 256 additions & 0 deletions scripts/mod_loader/modapi/deployment.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@

local STATE_SELECTED = 0
local STATE_REMAINING = 1
local STATE_DEPLOYED = 2
local STATE_LANDING = 3
local STATE_LANDED = 4
local PHASE_DEPLOYMENT = 0
local PHASE_LANDING = 1
local PHASE_LANDED = 2
local OUT_OF_BOUNDS = Point(-1,-1)

-- reusable tables
local prev = {
[0] = {},
[1] = {},
[2] = {},
}
local mechs = {
[0] = {},
[1] = {},
[2] = {},
}

local function getDeploymentData(mission)
return mission.deployment or {}
end


modApi.deployment = {}

function modApi.deployment.isDeploymentPhase(self)
local mission = GetCurrentMission()
if mission == nil then return false end

return getDeploymentData(mission).in_progress == true
end

function modApi.deployment.isLandingPhase(self)
local mission = GetCurrentMission()
if mission == nil then return false end

return getDeploymentData(mission).phase == PHASE_LANDING
end

function modApi.deployment.getSelected(self)
local mission = GetCurrentMission()
if mission == nil then return nil end

local deployment = getDeploymentData(mission)

if deployment.in_progress then
for pawnId = 0, 2 do
local mech = deployment[pawnId]
if mech.state == STATE_SELECTED then
return pawnId
end
end
end

return nil
end

function modApi.deployment.getDeployed(self)
local mission = GetCurrentMission()
if mission == nil then return {} end

local deployment = getDeploymentData(mission)
local deployed = {}

if deployment.in_progress then
for pawnId = 0, 2 do
if deployment[pawnId].state == STATE_DEPLOYED then
table.insert(deployed, pawnId)
end
end
end

return deployed
end

function modApi.deployment.getRemaining(self)
local mission = GetCurrentMission()
if mission == nil then return {} end

local deployment = getDeploymentData(mission)
local remaining = {}

if deployment.in_progress then
for pawnId = 0, 2 do
if deployment[pawnId].state == STATE_REMAINING then
table.insert(remaining, pawnId)
end
end
end

return remaining
end


local function updateDeploymentListener(mission)
local deployment = getDeploymentData(mission)

if not deployment.in_progress then
return
end

if deployment.phase == PHASE_DEPLOYMENT then
local pwn0 = Board:GetPawn(0)
local pwn1 = Board:GetPawn(1)
local pwn2 = Board:GetPawn(2)

local prev = prev
prev[0].state = deployment[0].state
prev[1].state = deployment[1].state
prev[2].state = deployment[2].state

local mechs = mechs
mechs[0].loc = pwn0:GetSpace()
mechs[1].loc = pwn1:GetSpace()
mechs[2].loc = pwn2:GetSpace()
mechs[0].isSelected = pwn0:IsSelected()
mechs[1].isSelected = pwn1:IsSelected()
mechs[2].isSelected = pwn2:IsSelected()

for pawnId = 0, 2 do
local mech = mechs[pawnId]
if mech.isSelected then
mech.state = STATE_SELECTED
elseif mech.loc == OUT_OF_BOUNDS then
mech.state = STATE_REMAINING
else
mech.state = STATE_DEPLOYED
end
end

local isNoneSelected = true
and mechs[0].state ~= STATE_SELECTED
and mechs[1].state ~= STATE_SELECTED
and mechs[2].state ~= STATE_SELECTED

if isNoneSelected then
for pawnId = 0, 2 do
local mech = mechs[pawnId]
if mech.state == STATE_REMAINING then
mech.state = STATE_SELECTED
break
end
end
end

for pawnId = 0, 2 do
local mech = mechs[pawnId]
local saved = deployment[pawnId]
saved.state = mech.state
end

for pawnId = 0, 2 do
local mech = mechs[pawnId]
local prev = prev[pawnId]
if mech.state ~= prev.state then
if prev.state == STATE_DEPLOYED then
modApi.events.onPawnUndeployed:dispatch(pawnId)
elseif prev.state == STATE_SELECTED then
modApi.events.onPawnUnselectedForDeployment:dispatch(pawnId)
end

if mech.state == STATE_DEPLOYED then
modApi.events.onPawnDeployed:dispatch(pawnId)
elseif mech.state == STATE_SELECTED then
modApi.events.onPawnSelectedForDeployment:dispatch(pawnId)
end
end
end

local isAllDeployed = true
and mechs[0].state == STATE_DEPLOYED
and mechs[1].state == STATE_DEPLOYED
and mechs[2].state == STATE_DEPLOYED
and pwn0:IsBusy()

if isAllDeployed then
deployment.phase = PHASE_LANDING
modApi.events.onLandingPhaseStart:dispatch()
end
end

if deployment.phase == PHASE_LANDING then
for pawnId = 0, 2 do
local mech = deployment[pawnId]
local pawn = Board:GetPawn(pawnId)

if mech.state == STATE_DEPLOYED then
if pawn:IsBusy() then
mech.state = STATE_LANDING
modApi.events.onPawnLanding:dispatch(pawnId)
end

elseif mech.state == STATE_LANDING then
if not pawn:IsBusy() then
mech.state = STATE_LANDED
modApi.events.onPawnLanded:dispatch(pawnId)
end
end
end

local isAllLanded = true
and deployment[0].state == STATE_LANDED
and deployment[1].state == STATE_LANDED
and deployment[2].state == STATE_LANDED

if isAllLanded then
deployment.in_progress = false
deployment.phase = PHASE_LANDED
modApi.events.onDeploymentPhaseEnd:dispatch()
end
end
end

local function startDeploymentListener(mission)
mission.deployment = {
in_progress = true,
phase = PHASE_DEPLOYMENT,
[0] = { state = STATE_REMAINING },
[1] = { state = STATE_REMAINING },
[2] = { state = STATE_REMAINING },
}

modApi.events.onDeploymentPhaseStart:dispatch()
end

modApi.events.onPawnLanded:subscribe(function(pawnId)
local pawn = Board:GetPawn(pawnId)
local pawnType = pawn:GetType()
local deploySkill = _G[pawnType].DeploySkill

local isValidDeploySkill = true
and type(deploySkill) == 'string'
and type(_G[deploySkill]) == 'table'
and type(_G[deploySkill].GetSkillEffect) == 'function'

if isValidDeploySkill then
local Pawn_bak = Pawn; Pawn = pawn
local p2 = pawn:GetSpace()
local fx = _G[deploySkill]:GetSkillEffect(p2, p2)

for eventIndex = 1, fx.effect:size() do
local event = fx.effect:index(eventIndex)
Board:DamageSpace(event)
end

Pawn = Pawn_bak
end
end)

modApi.events.onMissionStart:subscribe(startDeploymentListener)
modApi.events.onMissionUpdate:subscribe(updateDeploymentListener)

0 comments on commit 57f9150

Please sign in to comment.