function ManageProduction(minister)
local ministerTag = minister:GetCountryTag()
local ministerCountry = minister:GetCountry()
local ai = minister:GetOwnerAI()
local strategy = ministerCountry:GetStrategy()
local capitalProvId = ministerCountry:GetActingCapitalLocation():GetProvinceID()
--Utils.LUA_DEBUGOUT("ManageProduction - " .. tostring(ministerTag) )
--local ic = ministerCountry:GetAvailableIC()
local ic = ministerCountry:GetICPart( CDistributionSetting._PRODUCTION_PRODUCTION_ ):Get()
ic = ic - ministerCountry:GetUsedIC():Get()
--Utils.LUA_DEBUGOUT( "ic before " .. tostring(ic) )
-- ai list of requests, in prio order
local bBuildReserve = not ministerCountry:IsAtWar()
local requestQueue = ai:GetReqProdQueue()
--Utils.LUA_DEBUGOUT(tostring(ministerTag) .. " checkign queue and ic is " .. ic)
[COLOR="Pink"] --=== EDITED from Kasper747 beginn
-- we need convoys at all?
ic = ConstructConvoys(ai, minister, ministerTag, ministerCountry, ic )
--=== EDITED from Kasper747 end[/COLOR]
while (not requestQueue:IsEmpty()) and ic >= 0.0 do
local unit = requestQueue:GetTailData().pUnit
local tmp = ministerCountry:GetBuildCostIC( unit, 1, bBuildReserve ):Get()
--Utils.LUA_DEBUGOUT(tostring(ministerTag) .. " " .. tostring(unit:GetKey()) .. " cost " .. tmp .. ", we got ic " .. tostring(ic) )
ic = ic - tmp
--Utils.LUA_DEBUGOUT( tostring(ministerTag) .. " " .."leftover ic " .. tostring(ic) )
--if ic >= 0.0 then
requestQueue:RemoveTail()
local orderlist = SubUnitList()
SubUnitList.Append( orderlist, unit )
local construct = CConstructUnitCommand( ministerTag, orderlist, capitalProvId, 1, bBuildReserve, CNullTag(), CID() )
ai:Post( construct )
--end
end
--Utils.LUA_DEBUGOUT( "------" )
-- any requests by strategic ai
for subunit in CSubUnitDataBase.GetSubUnitList() do
local count = strategy:GetWantedSubUnits(subunit)
if count > 0.0 then
local orderlist = SubUnitList()
SubUnitList.Append( orderlist, subunit )
local construct = CConstructUnitCommand( ministerTag, orderlist, capitalProvId, count, false, CNullTag(), CID() )
ai:Post( construct )
end
end
--Utils.LUA_DEBUGOUT("---------------" )
-- Buildings
if ic >= 0 then
if not ( ministerCountry:IsAtWar() or ministerCountry:GetStrategy():IsPreparingWar() ) then
local factory = CBuildingDataBase.GetBuilding( "industry" )
local factoryCost = ministerCountry:GetBuildCost( factory ):Get()
for provinceId in ministerCountry:GetOwnedProvinces() do
local province = CCurrentGameState.GetProvince( provinceId )
--Utils.LUA_DEBUGOUT("b4")
ic = ic - factoryCost
if ic < 0.0 then
break
end
--Utils.LUA_DEBUGOUT("build with " .. tostring(ministerTag) )
if ministerCountry:IsBuildingAllowed(factory, province)
and (not province:IsFrontProvince(false) )
--and (not province:HasAdjacentEnemyOrCB() )
and province:GetInfrastructure():Get() > 0.3 then
local constructCommand = CConstructBuildingCommand( ministerTag, factory, provinceId, 1 )
if constructCommand:IsValid() then
ai:Post( constructCommand )
--Utils.LUA_DEBUGOUT("building factory in " .. provinceId ..
-- " for " .. tostring(ministerTag) ..
-- " costing: " .. factoryCost ..
-- " of IC left: " .. ic )
break -- one enough for now
end
end
end
end
-- look at building defences
local costalFort = CBuildingDataBase.GetBuilding( "coastal_fort" )
local costalFortCost = ministerCountry:GetBuildCost( costalFort ):Get()
--if ministerCountry:GetStrategicWarfare():GetBombingImpact():Get() > 0.3 then
for navalBaseProvince in ministerCountry:GetNavalBases() do
--Utils.LUA_DEBUGOUT("navalBaseProvince " )
if not navalBaseProvince:HasBuilding( costalFort ) then
--Utils.LUA_DEBUGOUT("WANT building coastal fort in " .. navalBaseProvince:GetProvinceID() ..
-- " for " .. tostring(ministerTag) ..
-- " costing: " .. costalFortCost ..
-- " of IC left: " .. ic )
if ministerCountry:IsBuildingAllowed(costalFort, navalBaseProvince) then
--Utils.LUA_DEBUGOUT("building is allowed")
ic = ic - costalFortCost
if ic < 0.0 then
break
end
--Utils.LUA_DEBUGOUT("building coastal fort in " .. navalBaseProvince:GetProvinceID() ..
-- " for " .. tostring(ministerTag) ..
-- " costing: " .. costalFortCost ..
-- " of IC left: " .. ic )
local constructCommand = CConstructBuildingCommand( ministerTag, costalFort, navalBaseProvince:GetProvinceID(), 1 )
if constructCommand:IsValid() then
ai:Post( constructCommand )
break -- one enough for now
end
end
end
end
local antiAir = CBuildingDataBase.GetBuilding( "anti_air" )
local antiAirCost = ministerCountry:GetBuildCost( antiAir):Get()
for airBaseProvince in ministerCountry:GetAirBases() do
--Utils.LUA_DEBUGOUT("airBaseProvince " )
if not airBaseProvince:HasBuilding( antiAir ) then
--Utils.LUA_DEBUGOUT("WANT building AA in " .. airBaseProvince:GetProvinceID() ..
-- " for " .. tostring(ministerTag) ..
-- " costing: " .. antiAirCost ..
-- " of IC left: " .. ic )
if ministerCountry:IsBuildingAllowed(antiAir, airBaseProvince) then
--Utils.LUA_DEBUGOUT("building is allowed")
ic = ic - antiAirCost
if ic < 0.0 then
break
end
--Utils.LUA_DEBUGOUT("building AA in " .. airBaseProvince:GetProvinceID() ..
--- " for " .. tostring(ministerTag) ..
-- " costing: " .. antiAirCost ..
-- " of IC left: " .. ic )
local constructCommand = CConstructBuildingCommand( ministerTag, antiAir, airBaseProvince:GetProvinceID(), 1 )
if constructCommand:IsValid() then
ai:Post( constructCommand )
break -- one enough for now
end
end
end
end
end
--Utils.LUA_DEBUGOUT( "ic at end > " .. tostring(ic) )
local manpower = ministerCountry:GetManpower():Get()
--Utils.LUA_DEBUGOUT( "mp at end > " .. tostring(manpower) )
-- always fill out with more infantry if possible.
if ic > 0 and manpower > 10 then
--Utils.LUA_DEBUGOUT( "has ic and manpower" )
local orderlist = SubUnitList()
local infantry = CSubUnitDataBase.GetSubUnit("infantry_brigade")
local militia = CSubUnitDataBase.GetSubUnit("militia_brigade")
if ministerCountry:GetTechnologyStatus():IsUnitAvailable(infantry) then
SubUnitList.Append( orderlist, infantry )
SubUnitList.Append( orderlist, infantry )
SubUnitList.Append( orderlist, infantry )
else
SubUnitList.Append( orderlist, militia )
SubUnitList.Append( orderlist, militia )
SubUnitList.Append( orderlist, militia )
end
--Utils.LUA_DEBUGOUT( "builds a division." )
local construct = CConstructUnitCommand( ministerTag, orderlist, capitalProvId, 1, bBuildReserve, CNullTag(), CID() )
ai:Post( construct )
end
end