If you have played a game longer than 3 years you can probably see, that AI don’t buildup it convoys up. Especial Brits, Japan, China. Minors have this problem too. As result lowering of recourses and cutback of IC.
With this ai_production_minister.lua it should be fixed for long terms.
1.1c is included.
Features:
1. Building of convoys have now 1st priority. (before to check, what army need, AI check first, if convoys are needed)
2. The amount of convoys, which is needed, will be build parallel and not serial, like it was.
The same is for escorts too.
Waiting for your feedback.
You can download a complete file.
Or just replace this 2 funktions. ManageProductionand and ConstructConvoys. This will not effect the autoslider mods
Code:
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)
--=== EDITED from Kasper747 beginn
-- we need convoys at all?
ic = ConstructConvoys(ai, minister, ministerTag, ministerCountry, ic )
--=== EDITED from Kasper747 end
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
ConstructConvoys
Code:
function ConstructConvoys(ai, minister, ministerTag, ministerCountry, ic )
local ministerPortCount = ministerCountry:GetNumOfPorts()
if ministerPortCount > 0 then
local freeTransports = ministerCountry:GetTransports()
if freeTransports < ministerPortCount*2 then
local neededTransports = ministerCountry:GetTotalNeededTransports()
neededTransports = neededTransports + (ministerPortCount - freeTransports) -- want some spares
neededTransports = neededTransports - minister:CountTransportsUnderConstruction() * defines.economy.CONVOY_CONSTRUCTION_SIZE
if neededTransports > 0 then
local buildRequestCount = neededTransports / defines.economy.CONVOY_CONSTRUCTION_SIZE;
buildRequestCount = math.ceil( math.max( buildRequestCount, 1) )
local cost = ministerCountry:GetConvoyBuildCost():Get()
local res = ic / cost
---=== EDITED from Kasper747 begin
--if res > 0.7 then --was in vanila. now:
if res > 0.4 then
---=== EDITED from Kasper747 begin
buildRequestCount = math.min( buildRequestCount, math.floor(res + 0.5) )
ic = ic - buildRequestCount * cost
---=== EDITED from Kasper747 begin
while buildRequestCount > 0 do
--Utils.LUA_DEBUGOUT(tostring(ministerTag).."build convoys for " .. "ministerPortCount:" .. tostring(ministerPortCount))
local transportCommand = CConstructConvoyCommand( ministerTag, false, 1)
ai:Post( transportCommand )
buildRequestCount = buildRequestCount - 1
end
---=== EDITED from Kasper747 end
--Utils.LUA_DEBUGOUT("build convoys for " .. tostring(ministerTag) )
--Utils.LUA_DEBUGOUT("build convoys: " .. tostring(buildRequestCount) )
end
end
end
-- if at war, we could use protection
if minister:GetCountry():IsAtWar() then
local neededEscorts = minister:CountTotalDesiredEscorts()
if neededEscorts > 0 then
local buildRequestCount = neededEscorts / defines.economy.CONVOY_CONSTRUCTION_SIZE;
buildRequestCount = math.ceil( math.max( buildRequestCount, 1) )
local cost = ministerCountry:GetEscortBuildCost():Get()
local res = ic / cost
if res > 0.8 then
buildRequestCount = math.min( buildRequestCount, math.floor(res + 0.5) )
ic = ic - buildRequestCount * cost
--=== EDITED from Kasper747 begin
while buildRequestCount > 0 do
local escortCommand = CConstructConvoyCommand( ministerTag, true, 1)
ai:Post( escortCommand )
buildRequestCount = buildRequestCount - 1
end
--=== EDITED from Kasper747 end
--Utils.LUA_DEBUGOUT("build escorts for " .. tostring(ministerTag) )
--Utils.LUA_DEBUGOUT("build escorts: " .. tostring(buildRequestCount) )
end
end
end
end
return ic
end