Skip to content

Commit

Permalink
Add support for the new mobile factory setup (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
speed2CZ authored Mar 16, 2024
1 parent 4e04f94 commit a7da0de
Show file tree
Hide file tree
Showing 12 changed files with 272 additions and 245 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,40 @@ function CarrierAI(platoon)
if numCarriers <= numPositions then
for i = 1, numCarriers do
ForkThread(function(i)
local carrier = carriers[i]
IssueMove( {carriers[i]}, movePositions[i] )

while (carriers[i] and not carriers[i].Dead and carriers[i]:IsUnitState('Moving')) do
while (not carrier.Dead and carrier:IsUnitState('Moving')) do
WaitSeconds(.5)
end

if carrier.Dead then
return
end

local location
for num, loc in aiBrain.PBM.Locations do
if loc.LocationType == data.Location .. i then
location = loc
for _, location in aiBrain.PBM.Locations do
if location.LocationType == data.Location .. i then
location.PrimaryFactories.Air = factory
break
end
end

if not carriers[i].Dead then
location.PrimaryFactories.Air = carriers[i]
end

while (carriers[i] and not carriers[i].Dead) do
if table.getn(carriers[i]:GetCargo()) > 0 and carriers[i]:IsIdleState() then
IssueClearCommands(carriers[i])
IssueTransportUnload({carriers[i]}, carriers[i]:GetPosition())
carrier:ForkThread(function(self)
local factory = self.ExternalFactory

while true do
if table.getn(self:GetCargo()) > 0 and factory:IsIdleState() then
IssueClearCommands({self})
IssueTransportUnload({self}, carrier:GetPosition())

repeat
WaitSeconds(3)
until not self:IsUnitState("TransportUnloading")
end

WaitSeconds(1)
end
WaitSeconds(1)
end
end)
end, i)
end
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,40 @@ function CarrierAI(platoon)

for i = 1, numCarriers do
ForkThread(function(i)
IssueMove({carriers[i]}, movePositions[i])
local carrier = carriers[i]
IssueMove({carrier}, movePositions[i])

while not carriers[i].Dead and carriers[i]:IsUnitState('Moving') do
while not carrier.Dead and carrier:IsUnitState('Moving') do
WaitSeconds(.5)
end

if carriers[i].Dead then
if carrier.Dead then
return
end

for num, loc in aiBrain.PBM.Locations do
if loc.LocationType == data.Location .. i then
loc.PrimaryFactories.Air = carriers[i]
for _, location in aiBrain.PBM.Locations do
if location.LocationType == data.Location .. i then
location.PrimaryFactories.Air = carrier.ExternalFactory
break
end
end

while not carriers[i].Dead do
if table.getn(carriers[i]:GetCargo()) > 0 and carriers[i]:IsIdleState() then
IssueClearCommands({carriers[i]})
IssueTransportUnload({carriers[i]}, carriers[i]:GetPosition())
carrier:ForkThread(function(self)
local factory = self.ExternalFactory

while true do
if table.getn(self:GetCargo()) > 0 and factory:IsIdleState() then
IssueClearCommands({self})
IssueTransportUnload({self}, carrier:GetPosition())

repeat
WaitSeconds(3)
until not self:IsUnitState("TransportUnloading")
end

WaitSeconds(1)
end
WaitSeconds(1)
end
end)
end, i)
end
end
Expand Down Expand Up @@ -671,6 +681,7 @@ local minASFs = 25
function AtlantisThread(platoon)
local brain = platoon:GetBrain()
local atlantis = platoon:GetPlatoonUnits()[1]
local factory = atlantis.ExternalFactory

local rect = ScenarioUtils.AreaToRect('M3_Atlantis_Guard_Area')
local targetCats = categories.AIR * categories.MOBILE * categories.EXPERIMENTAL
Expand Down Expand Up @@ -711,9 +722,9 @@ function AtlantisThread(platoon)
end

local function deployASFs()
if atlantis:IsUnitState('Building') then
IssueStop({atlantis})
IssueClearCommands({atlantis})
if factory:IsUnitState('Building') then
IssueStop({factory})
IssueClearCommands({factory})
end

platoon:UnloadUnitsAtLocation(categories.uea0303, platoon:GetPlatoonPosition())
Expand All @@ -735,7 +746,7 @@ function AtlantisThread(platoon)
local toBuild = maxASFs[Difficulty] - numASFsInPlatoon - cargo
--LOG("Atlantis checking to build ASFs, needing: " .. toBuild)
if toBuild > 0 then
IssueBuildFactory({atlantis}, 'uea0303', toBuild)
IssueBuildFactory({factory}, 'uea0303', toBuild)
end
end

Expand All @@ -752,7 +763,7 @@ function AtlantisThread(platoon)
end
end

if atlantis:IsIdleState() then
if atlantis:IsIdleState() and factory:IsIdleState() then
buildASFs()
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,35 @@ function OrderCarrierFactory()
-- Adding build location for AI
ArmyBrains[Order]:PBMAddBuildLocation('M1_Order_Carrier_Start_Marker', 150, 'AircraftCarrier1')

local Carrier = ScenarioInfo.M1_Order_Carrier
if not Carrier then
return
end
local carrier = ScenarioInfo.M1_Order_Carrier

for num, loc in ArmyBrains[Order].PBM.Locations do
if loc.LocationType == 'AircraftCarrier1' then
loc.PrimaryFactories.Air = Carrier
for _, location in ArmyBrains[Order].PBM.Locations do
if location.LocationType == 'AircraftCarrier1' then
location.PrimaryFactories.Air = carrier.ExternalFactory
OrderCarrierAttacks()
break
end
end

IssueClearFactoryCommands({carrier.ExternalFactory})
IssueFactoryRallyPoint({carrier.ExternalFactory}, ScenarioUtils.MarkerToPosition("Naval Rally Point 08"))

while ScenarioInfo.MissionNumber == 1 and not Carrier.Dead do
if Carrier:IsIdleState() and Carrier:GetCargo()[1] then
IssueClearCommands({Carrier})
IssueTransportUnload({Carrier}, Carrier:GetPosition())
carrier.ReleaseUnitsThread = carrier:ForkThread(function(self)
local factory = self.ExternalFactory

while true do
if table.getn(self:GetCargo()) > 0 and factory:IsIdleState() then
IssueClearCommands({self})
IssueTransportUnload({self}, ScenarioUtils.MarkerToPosition("Naval Rally Point 08"))

repeat
WaitSeconds(3)
until not self:IsUnitState("TransportUnloading")
end

WaitSeconds(1)
end
WaitSeconds(1)
end
end)
end

-- Platoons built by carrier
Expand Down Expand Up @@ -75,13 +84,18 @@ end
function OrderTempestFactory()
ArmyBrains[Order]:PBMAddBuildLocation('M1_Order_Tempest_Start_Marker', 150, 'Tempest1')

for num, loc in ArmyBrains[Order].PBM.Locations do
if loc.LocationType == 'Tempest1' then
loc.PrimaryFactories.Sea = ScenarioInfo.M1_Order_Tempest
local tempest = ScenarioInfo.M1_Order_Tempest

for _, location in ArmyBrains[Order].PBM.Locations do
if location.LocationType == 'Tempest1' then
location.PrimaryFactories.Sea = tempest.ExternalFactory
OrderTempestAttacks()
return
break
end
end

IssueClearFactoryCommands({tempest.ExternalFactory})
IssueFactoryRallyPoint({tempest.ExternalFactory}, ScenarioUtils.MarkerToPosition("Naval Rally Point 08"))
end

function OrderTempestAttacks()
Expand All @@ -101,9 +115,8 @@ function OrderTempestAttacks()
PlatoonType = 'Sea',
RequiresConstruction = true,
LocationType = 'Tempest1',
PlatoonAIFunction = {ThisFile, 'MoveAndGivePlatoonToPlayer'},
PlatoonAIFunction = {ThisFile, 'GivePlatoonToPlayerAndPatrol'},
PlatoonData = {
MoveRoute = {'Rally Point 05'},
PatrolChain = 'M1_Oder_Naval_Def_Chain',
},
}
Expand Down Expand Up @@ -139,40 +152,15 @@ function GivePlatoonToPlayer(platoon)
end
end

function MoveAndGivePlatoonToPlayer(platoon)
function GivePlatoonToPlayerAndPatrol(platoon)
local givenUnits = {}
local data = platoon.PlatoonData

if not data then
error('*MoveAndGivePlatoonToPlayer: PlatoonData not defined', 2)
elseif not (data.MoveRoute or data.MoveChain) then
error('*MoveAndGivePlatoonToPlayer: MoveToRoute or MoveChain not defined', 2)
error('*GivePlatoonToPlayerAndPatrol: PlatoonData not defined', 2)
end

local movePositions = {}
if data.MoveChain then
movePositions = ScenarioUtils.ChainToPositions(data.MoveChain)
else
for _, v in data.MoveRoute do
if type(v) == 'string' then
table.insert(movePositions, ScenarioUtils.MarkerToPosition(v))
else
table.insert(movePositions, v)
end
end
end

for _, v in movePositions do
platoon:MoveToLocation(v, false)
end

WaitSeconds(1)

for _, unit in platoon:GetPlatoonUnits() do
while (not unit.Dead and unit:IsUnitState('Moving')) do
WaitSeconds(1)
end

local tempUnit = ScenarioFramework.GiveUnitToArmy(unit, 'Player1')
table.insert(givenUnits, tempUnit)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -626,23 +626,27 @@ function CybranM2IslandBaseCarrierAttacks()
local units = ArmyBrains[Cybran]:GetListOfUnits(categories.CARRIER, false)
for i = 1, 2 do
local carrier = units[i]
carrier:SetCustomName('Carrier' .. i)

local location
for num, loc in ArmyBrains[Cybran].PBM.Locations do
if loc.LocationType == 'M2_Cybran_Carrier_' .. i then
location = loc
for _, location in ArmyBrains[Cybran].PBM.Locations do
if location.LocationType == 'M2_Cybran_Carrier_' .. i then
location.PrimaryFactories.Air = carrier.ExternalFactory
break
end
end
location.PrimaryFactories.Air = carrier

ForkThread(function()
while carrier and not carrier:IsDead() do
if table.getn(carrier:GetCargo()) > 0 and carrier:IsIdleState() then
IssueClearCommands({carrier})
IssueTransportUnload({carrier}, carrier:GetPosition())

carrier:ForkThread(function(self)
local factory = self.ExternalFactory

while true do
if table.getn(self:GetCargo()) > 0 and factory:IsIdleState() then
IssueClearCommands({self})
IssueTransportUnload({self}, carrier:GetPosition())

repeat
WaitSeconds(3)
until not self:IsUnitState("TransportUnloading")
end

WaitSeconds(1)
end
end)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local BaseManager = import('/lua/ai/opai/basemanager.lua')
local CustomFunctions = '/maps/FAF_Coop_Novax_Station_Assault/FAF_Coop_Novax_Station_Assault_CustomFunctions.lua'
local SPAIFileName = '/lua/ScenarioPlatoonAI.lua'
local ScenarioPlatoonAI = import(SPAIFileName)
local ScenarioUtils = import('/lua/sim/ScenarioUtilities.lua')
local ThisFile = '/maps/FAF_Coop_Novax_Station_Assault/FAF_Coop_Novax_Station_Assault_m2orderai.lua'

---------
Expand Down Expand Up @@ -401,26 +402,36 @@ end
-----------
-- Carriers
-----------
function OrderM2CarriersAI(unit)
function OrderM2CarriersAI(carrier)
ArmyBrains[Order]:PBMAddBuildLocation('M2_Order_Carrier_Marker_2', 40, 'M2_Order_Carrier_2')

if unit and not unit.Dead then
for num, loc in ArmyBrains[Order].PBM.Locations do
if loc.LocationType == 'M2_Order_Carrier_2' then
loc.PrimaryFactories.Air = unit
OrderM2CarrierAttacks()
break
end
for _, location in ArmyBrains[Order].PBM.Locations do
if location.LocationType == 'M2_Order_Carrier_2' then
location.PrimaryFactories.Air = carrier.ExternalFactory
OrderM2CarrierAttacks()
break
end
end

IssueClearFactoryCommands({carrier.ExternalFactory})
IssueFactoryRallyPoint({carrier.ExternalFactory}, ScenarioUtils.MarkerToPosition("Rally Point 00"))

carrier.ReleaseUnitsThread = carrier:ForkThread(function(self)
local factory = self.ExternalFactory

while true do
if table.getn(self:GetCargo()) > 0 and factory:IsIdleState() then
IssueClearCommands({self})
IssueTransportUnload({self}, carrier:GetPosition())

while not unit.Dead do
if unit:IsIdleState() and unit:GetCargo()[1] then
IssueClearCommands({unit})
IssueTransportUnload({unit}, unit:GetPosition())
repeat
WaitSeconds(3)
until not self:IsUnitState("TransportUnloading")
end

WaitSeconds(1)
end
end
end)
end

function OrderM2CarrierAttacks()
Expand Down Expand Up @@ -582,18 +593,19 @@ end
----------
-- Tempest
----------
function OrderM2TempestAI(unit)
function OrderM2TempestAI(tempest)
ArmyBrains[Order]:PBMAddBuildLocation('M2_Order_Starting_Tempest', 60, 'M2_Tempest1')

if unit and not unit.Dead then
for num, loc in ArmyBrains[Order].PBM.Locations do
if loc.LocationType == 'M2_Tempest1' then
loc.PrimaryFactories.Sea = unit
OrderM2TempestAttacks()
break
end
for _, location in ArmyBrains[Order].PBM.Locations do
if location.LocationType == 'M2_Tempest1' then
location.PrimaryFactories.Sea = tempest.ExternalFactory
OrderM2TempestAttacks()
break
end
end

IssueClearFactoryCommands({tempest.ExternalFactory})
IssueFactoryRallyPoint({tempest.ExternalFactory}, ScenarioUtils.MarkerToPosition("Naval Rally Point 00"))
end

function OrderM2TempestAttacks()
Expand Down
Loading

0 comments on commit a7da0de

Please sign in to comment.