Naval AI not repairing its ships!
Moderators: Slitherine Core, The Lordz
Naval AI not repairing its ships!
Hi guys, has anyone discovered how to make the AI repair its ships, especially its Dreadnoughts, because in all the games I play the AI does not repair its damaged ships ever?
Re: Naval AI not repairing its ships!
The check for repairs is included in ai_economics.lua
The AI takes a look at every damaged unit and puts these in a table, sorting the units accordingly to their damage status so the most damaged unit will be the first to be repaired.
There are two modifiers:
- units in "vulnerable cities" (threatend) cities get a modifier of 1.2 to their ranking in the repair list.
- units with less than 30 hitpoints (3 points of unitstrength) will be placed 20 places further up the list.
Furthermore if the unit in question has pending upgrades via technology it will be prefered over other units.
I believe the reason why ships rarely (if ever) get repaired by the AI is that ships mostly speed around and so are never considered to be in a "threatened city" (harbor) and threats from an enemy who can theoretically move out of the fog of war from a range of up to 14 hexes (as cruisers do) are almost impossible to foresee but the AI is cheating anyway (surprised?).
Bear in mind, that a harbor gives such a massive defense bonus to anchored ships that even Battleships have problems scrating a transporter so damaged ships in harbors were maybe even intentionally left out to let the AI spend it's resources on the more exposed units.
It's easy to include extra-checks into the repair list like Dreadnoughts getting a big priority bonus. I tried that out just today but couldn't notice any big differences over the course of the whole game.
This example should give all british ships a priority bonus of 10 when inserted in line 345 of ai_economics.lua.
The AI takes a look at every damaged unit and puts these in a table, sorting the units accordingly to their damage status so the most damaged unit will be the first to be repaired.
There are two modifiers:
- units in "vulnerable cities" (threatend) cities get a modifier of 1.2 to their ranking in the repair list.
- units with less than 30 hitpoints (3 points of unitstrength) will be placed 20 places further up the list.
Furthermore if the unit in question has pending upgrades via technology it will be prefered over other units.
I believe the reason why ships rarely (if ever) get repaired by the AI is that ships mostly speed around and so are never considered to be in a "threatened city" (harbor) and threats from an enemy who can theoretically move out of the fog of war from a range of up to 14 hexes (as cruisers do) are almost impossible to foresee but the AI is cheating anyway (surprised?).
Bear in mind, that a harbor gives such a massive defense bonus to anchored ships that even Battleships have problems scrating a transporter so damaged ships in harbors were maybe even intentionally left out to let the AI spend it's resources on the more exposed units.
It's easy to include extra-checks into the repair list like Dreadnoughts getting a big priority bonus. I tried that out just today but couldn't notice any big differences over the course of the whole game.
Code: Select all
if unit.hp < 30 then
VU[unit.id] = VU[unit.id] + 20
end
if unit.faction.id == 1 and unit.type == Unit.NAVAL then
VU[unit.id] = VU[unit.id] + 10
end
Re: Naval AI not repairing its ships!
Hi Robotron thanks for the info, I have tried the game using the above coding, and I still can't get the AI to repair any ships. Its more than possible that I have applied the code incorrectly,which would not surprise me one bit. Anyway I will continue trying to get the AI repair ships as a matter of top priority, how I'm going to get it do what I want is another matter entirely thou!!
function AIPlayer:repairUnits(front)
if self.sai.stackLogging then self:printLog("function AIPlayer:entered: self:repairUnits()") end
local units = {}
local VU = {}
for _,unit in ipairs(front.friendlyUnits) do
if unit.alive and unit.undeployed ~= true and unit.hex ~= nil and (self:aiCanRepair(unit) or self:aiCanUpgrade(unit)) then
VU[unit.id] = self:getVulnerability(unit,unit.hex) + (100-unit.hp)/100
if unit.hex.construction ~= nil and (self.sai.vulnerableCitiesThisTurn[unit.hex.id] == true or self:isAdjacentToAlliance(self.sai.enemyAlliance,unit.hex)) then
if VU[unit.id] == 0 and unit.hp < 100 then
VU[unit.id] = math.max((100-unit.hp)*1.2-10,0) --math.min((100-unit.hp)*1.2,100)
else
VU[unit.id] = VU[unit.id]*1.2
end
if unit.hp < 30 then
VU[unit.id] = VU[unit.id] + 20
end
if unit.faction.id == 0 and unit.type == Unit.NAVAL then
VU[unit.id] = VU[unit.id] + 10
end
if unit.faction.id == 1 and unit.type == Unit.NAVAL then
VU[unit.id] = VU[unit.id] + 10
end
if unit.faction.id == 2 and unit.type == Unit.NAVAL then
VU[unit.id] = VU[unit.id] + 10
end
if unit.faction.id == 3 and unit.type == Unit.NAVAL then
VU[unit.id] = VU[unit.id] + 10
end
if unit.faction.id == 4 and unit.type == Unit.NAVAL then
VU[unit.id] = VU[unit.id] + 10
end
if unit.faction.id == 8 and unit.type == Unit.NAVAL then
VU[unit.id] = VU[unit.id] + 10
end
if unit.faction.id == 10 and unit.type == Unit.NAVAL then
VU[unit.id] = VU[unit.id] + 10
end
self:printLog("Prioritising unit for repair: "..unit.id.." at "..unit.hex.x..","..unit.hex.y.." because it is in a vulnerable city: "..unit.hex.construction.name)
end
table.insert(units,unit)
end
end
table.sort(units,function(a,b) return VU[a.id] > VU[b.id] end)
for _,unit in ipairs(units) do
if self:aiCanRepair(unit) then
if self.sai.enemyAlliance:IsVisible(unit.hex) then
View:AddEffect(unit.hex, "marker_repair")
end
RepairUnit(unit)
self:printLog("AI repairing: "..unit.hex.x..","..unit.hex.y)
end
end
for _,unit in ipairs(units) do
if self:aiCanUpgrade(unit) then
if UpgradeUnit(unit) then
printLog("AI upgrading unit: "..unit.hex.x..","..unit.hex.y.." "..unit.faction.name)
if self.sai.enemyAlliance:IsVisible(unit.hex) then
View:AddEffect(unit.hex, "marker_upgrade")
end
end
end
end
if self.sai.stackLogging then self:printLog("function AIPlayer:completed: self:repairUnits()") end
end
function AIPlayer:repairUnits(front)
if self.sai.stackLogging then self:printLog("function AIPlayer:entered: self:repairUnits()") end
local units = {}
local VU = {}
for _,unit in ipairs(front.friendlyUnits) do
if unit.alive and unit.undeployed ~= true and unit.hex ~= nil and (self:aiCanRepair(unit) or self:aiCanUpgrade(unit)) then
VU[unit.id] = self:getVulnerability(unit,unit.hex) + (100-unit.hp)/100
if unit.hex.construction ~= nil and (self.sai.vulnerableCitiesThisTurn[unit.hex.id] == true or self:isAdjacentToAlliance(self.sai.enemyAlliance,unit.hex)) then
if VU[unit.id] == 0 and unit.hp < 100 then
VU[unit.id] = math.max((100-unit.hp)*1.2-10,0) --math.min((100-unit.hp)*1.2,100)
else
VU[unit.id] = VU[unit.id]*1.2
end
if unit.hp < 30 then
VU[unit.id] = VU[unit.id] + 20
end
if unit.faction.id == 0 and unit.type == Unit.NAVAL then
VU[unit.id] = VU[unit.id] + 10
end
if unit.faction.id == 1 and unit.type == Unit.NAVAL then
VU[unit.id] = VU[unit.id] + 10
end
if unit.faction.id == 2 and unit.type == Unit.NAVAL then
VU[unit.id] = VU[unit.id] + 10
end
if unit.faction.id == 3 and unit.type == Unit.NAVAL then
VU[unit.id] = VU[unit.id] + 10
end
if unit.faction.id == 4 and unit.type == Unit.NAVAL then
VU[unit.id] = VU[unit.id] + 10
end
if unit.faction.id == 8 and unit.type == Unit.NAVAL then
VU[unit.id] = VU[unit.id] + 10
end
if unit.faction.id == 10 and unit.type == Unit.NAVAL then
VU[unit.id] = VU[unit.id] + 10
end
self:printLog("Prioritising unit for repair: "..unit.id.." at "..unit.hex.x..","..unit.hex.y.." because it is in a vulnerable city: "..unit.hex.construction.name)
end
table.insert(units,unit)
end
end
table.sort(units,function(a,b) return VU[a.id] > VU[b.id] end)
for _,unit in ipairs(units) do
if self:aiCanRepair(unit) then
if self.sai.enemyAlliance:IsVisible(unit.hex) then
View:AddEffect(unit.hex, "marker_repair")
end
RepairUnit(unit)
self:printLog("AI repairing: "..unit.hex.x..","..unit.hex.y)
end
end
for _,unit in ipairs(units) do
if self:aiCanUpgrade(unit) then
if UpgradeUnit(unit) then
printLog("AI upgrading unit: "..unit.hex.x..","..unit.hex.y.." "..unit.faction.name)
if self.sai.enemyAlliance:IsVisible(unit.hex) then
View:AddEffect(unit.hex, "marker_upgrade")
end
end
end
end
if self.sai.stackLogging then self:printLog("function AIPlayer:completed: self:repairUnits()") end
end
Re: Naval AI not repairing its ships!
best what can ai do with ships is park them in port and never ever move them again
its ill bad

its ill bad
Re: Naval AI not repairing its ships!
The only way the game is going to improve, is if modders do it themselves, because if we don't try then the game is dead, and I for one don't want to see the game die, its potential for improvement is to great. All the game needs is time spent, fixing the AI's short comings, and then hopefully we can have a game that everyone will be able to enjoy playing for many years to come. My mod I'm creating ( NO PEACE IN OUR TIME ) will have many things even you will approve off, and so far over on the Matrix forum I have only posted a fraction of what the mod contains.
Re: Naval AI not repairing its ships!
ai is incredible weak, if u want to see it naked, choose some advanced scenario, 1917 has the most fronts i guess, dont do any attack on ai on any front (as entente vs cp its just huge)
ai will do just some random attacks with no point and many useless movements all around the map, it just loves trains, later it will most likely abandon major fronts for turkey
on sea there are tons of hexes where to move, so it will be even more hilarious
in 1914 scenario as cp im used to rape british navy in la manche, no big deal, ai cant properly fight even on own green dots (i recommend to capture antwerp before somebody will try this stunt
)
in mp its a great game, vs ai it needs to completely rebuild its behaviour or make one sided scenarios with huge boost for ai to be challenging
(maybe making turkey neutral will make positive outcome for ai, ai just cant solve situations, where doesnt have enough units)
i would like to see and try your mod, but that ai is just broken...
ai will do just some random attacks with no point and many useless movements all around the map, it just loves trains, later it will most likely abandon major fronts for turkey
on sea there are tons of hexes where to move, so it will be even more hilarious
in 1914 scenario as cp im used to rape british navy in la manche, no big deal, ai cant properly fight even on own green dots (i recommend to capture antwerp before somebody will try this stunt

in mp its a great game, vs ai it needs to completely rebuild its behaviour or make one sided scenarios with huge boost for ai to be challenging
(maybe making turkey neutral will make positive outcome for ai, ai just cant solve situations, where doesnt have enough units)
i would like to see and try your mod, but that ai is just broken...
Re: Naval AI not repairing its ships!
double post, sorry!
Last edited by Robotron on Thu May 05, 2016 1:24 pm, edited 1 time in total.
Re: Naval AI not repairing its ships!
@nehi: let's be more constructive and stop bickering about the ai being bad which we all already know. A complete AI re-coding will never happen anyway but we can try to adress the points where it might be in need of fixing once we can figure out how it's working.
@kirk: Totally agree with you about keeping the game alive. There are a lot of people who prefer singeplayer vs AI to multiplayer for different reasons and we should try to fix this beast.
By looking briefly at your script (please paste scripts in code-tags available in the editor next time for better readability) I can see no errors, but I believe that raising the index in the repair list as I proposed might not be enough.
Here's why: the whole decision-making of the AI seems to be commanded by the script cost_lua in the AI folder. It's the actual "brain" of the AI. "Cost" in this case is not PP as one might think but a value to favour a decision like attacking, moving or as in this case repairing a unit. The AI weights it's possible actions by comparing these with values called PROs and CONs and then executes the actions with the highest PRO values (I believe
). There are very intersting comments by the coders about what would have to be fixed and we should analyze these first. I recommend using NotepadPlus in case you don't do so already.
There is also a comment concerning the incentive to repair units in line 198 which notes that "being vulnerable" (as in: being prone to attack) as mentioned ai_economic.lua is not "accurate" enough but I have to study this part more thouroughly to get it's meaning.
I also found the comments (everything after -- is a comment) in this section worth of note.
This seems like a good place to look into to more deeply, maybe?
For now you could always experiment with the ai_economy.lua script by raising the modifiers (maybe +20 oder even multiply them) just in case it's sufficient to get satisfying results by modifying the "Repair-list" and report back.
Good luck!
*edit*
we should probably exclude transports and convoys from the repair checklist in ai_economic.lua, just in case it could lead to problems
@kirk: Totally agree with you about keeping the game alive. There are a lot of people who prefer singeplayer vs AI to multiplayer for different reasons and we should try to fix this beast.

By looking briefly at your script (please paste scripts in code-tags available in the editor next time for better readability) I can see no errors, but I believe that raising the index in the repair list as I proposed might not be enough.
Here's why: the whole decision-making of the AI seems to be commanded by the script cost_lua in the AI folder. It's the actual "brain" of the AI. "Cost" in this case is not PP as one might think but a value to favour a decision like attacking, moving or as in this case repairing a unit. The AI weights it's possible actions by comparing these with values called PROs and CONs and then executes the actions with the highest PRO values (I believe

There is also a comment concerning the incentive to repair units in line 198 which notes that "being vulnerable" (as in: being prone to attack) as mentioned ai_economic.lua is not "accurate" enough but I have to study this part more thouroughly to get it's meaning.
I also found the comments (everything after -- is a comment) in this section worth of note.
Code: Select all
local VUPostRepair = VU
local hpIncrease = 0
local repairIncentive = 80 -- Should be the max it can be
if (not likelyToAttack) and isRailMove ~= true and self:aiCanRepair(unit) then
hpIncrease = GetRepairPoints(unit,unit.mp == 0 or hex.id ~= unit.hex.id)
VUPostRepair = self:getVulnerability(unit,hex,-hpIncrease)
if VUPostRepair < unit.hp+hpIncrease then
-- repair incentive should be some kind of combination of:
-- How badly do we need to repair
-- How _much_ can we repair
-- Second is kinda taken care of by above if statement, but probably not enough?
-- First should use VUPostRepair... perhaps combined with VU
-- But it shouldn't be d ependent on this, because it's also important to repair units which aren't under direct threat.
--VU - VUPostRepair -- How much we reduce our vulnerability by. The higher the better. Technically max = 100, but we should cap it, then + 20 or something
local repairScore = math.min(VU - VUPostRepair,30) -- Max of 30
local repairScore = repairScore + (100-unit.hp)/2 -- Max of 50
-- Repair incentive is max of 50... seems very low
repairIncentive = 80 - repairScore --(100-unit.hp)/2 -- First attempt at balance - very rough guess
end
end
For now you could always experiment with the ai_economy.lua script by raising the modifiers (maybe +20 oder even multiply them) just in case it's sufficient to get satisfying results by modifying the "Repair-list" and report back.
Good luck!

*edit*
we should probably exclude transports and convoys from the repair checklist in ai_economic.lua, just in case it could lead to problems

Code: Select all
if unit.faction.id == 1 and unit.type == Unit.NAVAL and unit.prototype.name ~= "transport" and unit.prototype.name ~= "convoy" then
VU[unit.id] = VU[unit.id] + 10
end
Last edited by Robotron on Thu May 05, 2016 2:02 pm, edited 3 times in total.
Re: Naval AI not repairing its ships!
i suggested two solutions, first to gave ai more units (one sided scenario/s) or to make turkey neutral (ai cant solve properly situations, where cant do coherent line of defense on every border, so lets shorten borders)Robotron wrote:let's be more constructive
Re: Naval AI not repairing its ships!
@nehi: I'd like to minimize workarounds like giving more units and altering the scenario as much as possible as the AI is given enough help in the scripts with stuff like bonus PP and the option to conjure units out of nothing.
But you are right about the game not being able to handle multiple fronts, Turkey and Italy come to mind when playing CP. Alas, shortening borders is not possible since we can't change the map-file where borders get defined. The constant train-transport of units is another point which must be fixed.
But you are right about the game not being able to handle multiple fronts, Turkey and Italy come to mind when playing CP. Alas, shortening borders is not possible since we can't change the map-file where borders get defined. The constant train-transport of units is another point which must be fixed.
Last edited by Robotron on Thu May 05, 2016 2:12 pm, edited 1 time in total.
Re: Naval AI not repairing its ships!
im convinced turkey plays most important role in ais bad decisions on both sides (too large country with just few units to defend it)
when i was playin cp, i could backstabb and take half russia with turks, while brits were cumulating units in egypt in like 10 rows
when i was playin entente, i could break turks without ressistance in the begining and later other cp nations abandoned major fronts in pointless try to stop me
when i was playin cp, i could backstabb and take half russia with turks, while brits were cumulating units in egypt in like 10 rows
when i was playin entente, i could break turks without ressistance in the begining and later other cp nations abandoned major fronts in pointless try to stop me
Re: Naval AI not repairing its ships!
@nehi: Regarding AI as Turkey vs. Russia: that's interesting, in my games Russia swarms Turkey with hordes of units, exposing the whole front vs. Germany.
Please open the file 1914_ai.lua in the folder data/scripts/scripted_ai in an editor. If you play a clean 1.66 version your script should read "return 50" and "return 10" in lines 129 and 134.
Change line 129 to "return -1000" and line 134 to "return -25" (without the quotation marks) and play a game as CP. How does Russia react now?
Please open the file 1914_ai.lua in the folder data/scripts/scripted_ai in an editor. If you play a clean 1.66 version your script should read "return 50" and "return 10" in lines 129 and 134.
Code: Select all
if playerAlliance.id == 2 then
local caucasus = GetHex(150,46)
local distanceToCaucasus = AIPlayer:getDistanceToFront(caucasus,front)
if distanceToCaucasus < 10 then
local turkey = game:GetFactionById(5)
if turkey.alliance.id ~= game:GetAllianceById(1).id then
printLog("React to turkey russia!")
return 50
else
local grozni = GetHex(152,40)
local nearestEnemyDist = AIPlayer:getDistanceToAlliance(game:GetAllianceById(1),grozni)
if nearestEnemyDist > 9 then
return 10
end
end
end
end
Re: Naval AI not repairing its ships!
i guess it depends on what germans and austrians do, when they just hold line, ai analyze it as save front and move any free unit down there
Re: Naval AI not repairing its ships!
Robotron wrote:@nehi: let's be more constructive and stop bickering about the ai being bad which we all already know. A complete AI re-coding will never happen anyway but we can try to adress the points where it might be in need of fixing once we can figure out how it's working.
@kirk: Totally agree with you about keeping the game alive. There are a lot of people who prefer singeplayer vs AI to multiplayer for different reasons and we should try to fix this beast.![]()
By looking briefly at your script (please paste scripts in code-tags available in the editor next time for better readability) I can see no errors, but I believe that raising the index in the repair list as I proposed might not be enough.
Here's why: the whole decision-making of the AI seems to be commanded by the script cost_lua in the AI folder. It's the actual "brain" of the AI. "Cost" in this case is not PP as one might think but a value to favour a decision like attacking, moving or as in this case repairing a unit. The AI weights it's possible actions by comparing these with values called PROs and CONs and then executes the actions with the highest PRO values (I believe). There are very intersting comments by the coders about what would have to be fixed and we should analyze these first. I recommend using NotepadPlus in case you don't do so already.
There is also a comment concerning the incentive to repair units in line 198 which notes that "being vulnerable" (as in: being prone to attack) as mentioned ai_economic.lua is not "accurate" enough but I have to study this part more thouroughly to get it's meaning.
I also found the comments (everything after -- is a comment) in this section worth of note.This seems like a good place to look into to more deeply, maybe?Code: Select all
local VUPostRepair = VU local hpIncrease = 0 local repairIncentive = 80 -- Should be the max it can be if (not likelyToAttack) and isRailMove ~= true and self:aiCanRepair(unit) then hpIncrease = GetRepairPoints(unit,unit.mp == 0 or hex.id ~= unit.hex.id) VUPostRepair = self:getVulnerability(unit,hex,-hpIncrease) if VUPostRepair < unit.hp+hpIncrease then -- repair incentive should be some kind of combination of: -- How badly do we need to repair -- How _much_ can we repair -- Second is kinda taken care of by above if statement, but probably not enough? -- First should use VUPostRepair... perhaps combined with VU -- But it shouldn't be d ependent on this, because it's also important to repair units which aren't under direct threat. --VU - VUPostRepair -- How much we reduce our vulnerability by. The higher the better. Technically max = 100, but we should cap it, then + 20 or something local repairScore = math.min(VU - VUPostRepair,30) -- Max of 30 local repairScore = repairScore + (100-unit.hp)/2 -- Max of 50 -- Repair incentive is max of 50... seems very low repairIncentive = 80 - repairScore --(100-unit.hp)/2 -- First attempt at balance - very rough guess end end
For now you could always experiment with the ai_economy.lua script by raising the modifiers (maybe +20 oder even multiply them) just in case it's sufficient to get satisfying results by modifying the "Repair-list" and report back.
Good luck!
*edit*
we should probably exclude transports and convoys from the repair checklist in ai_economic.lua, just in case it could lead to problems![]()
Code: Select all
if unit.faction.id == 1 and unit.type == Unit.NAVAL and unit.prototype.name ~= "transport" and unit.prototype.name ~= "convoy" then VU[unit.id] = VU[unit.id] + 10 end
Hi thanks for reply, I will take a much closer look at the (Cost Lua) file, and try to figure out a way to get the AI to repair its ships on a regular bases.
Re: Naval AI not repairing its ships!
privileged ai... turks met like 4 infantries there, but russia collapsed sooner (in 20 turns) than they could cross kavkazChange line 129 to "return -1000" and line 134 to "return -25" (without the quotation marks) and play a game as CP. How does Russia react now?
- Attachments
-
- dunno.jpg (217.58 KiB) Viewed 6202 times
Re: Naval AI not repairing its ships!
So, did you have the impression that by changing the values mentioned in the script the game on the Russia/Turkey front played substantially different from the average other game vs. the AI on the same level?
Did the Germans got more preassure on the Eastern Front by the Russians?
Did the Germans got more preassure on the Eastern Front by the Russians?
Re: Naval AI not repairing its ships!
in 1.6x is ai much weaker than in 1.5x, no pressure at all, in 1.6x it cant produce enough units, so its like no resistance
now i remember they were games in 1.5x i was talkin about turks backstabbing russia
in 1.6x i can beat all nations at once just with germans and austrians (thats impossible in 1.5x), after declaring war to all of them (except norway and sweden what can break convoys) in 6th turn - in 1.6x ive finished all scenarios in 15 turns or less - nivelle as entente in 10
now i remember they were games in 1.5x i was talkin about turks backstabbing russia
in 1.6x i can beat all nations at once just with germans and austrians (thats impossible in 1.5x), after declaring war to all of them (except norway and sweden what can break convoys) in 6th turn - in 1.6x ive finished all scenarios in 15 turns or less - nivelle as entente in 10
-
- Senior Corporal - Destroyer
- Posts: 117
- Joined: Sun Jan 11, 2015 7:51 pm
- Contact:
Re: Naval AI not repairing its ships!
In 1.6 the AI is easier because it does not disband its small garrisons which cost upkeep.
danielherr.github.io
Re: Naval AI not repairing its ships!
its not all the difference, in 1.5x there is something like free upkeep, not every built unit lowers production (cities there have lower production in general, so i guess in 1.6x cities production is boosted cause of small garrisons upkeep)
Re: Naval AI not repairing its ships!
Code: Select all
kirk23 wrote:Robotron wrote:@nehi: let's be more constructive and stop bickering about the ai being bad which we all already know. A complete AI re-coding will never happen anyway but we can try to adress the points where it might be in need of fixing once we can figure out how it's working.
@kirk: Totally agree with you about keeping the game alive. There are a lot of people who prefer singeplayer vs AI to multiplayer for different reasons and we should try to fix this beast.![]()
By looking briefly at your script (please paste scripts in code-tags available in the editor next time for better readability) I can see no errors, but I believe that raising the index in the repair list as I proposed might not be enough.
Here's why: the whole decision-making of the AI seems to be commanded by the script cost_lua in the AI folder. It's the actual "brain" of the AI. "Cost" in this case is not PP as one might think but a value to favour a decision like attacking, moving or as in this case repairing a unit. The AI weights it's possible actions by comparing these with values called PROs and CONs and then executes the actions with the highest PRO values (I believe). There are very intersting comments by the coders about what would have to be fixed and we should analyze these first. I recommend using NotepadPlus in case you don't do so already.
There is also a comment concerning the incentive to repair units in line 198 which notes that "being vulnerable" (as in: being prone to attack) as mentioned ai_economic.lua is not "accurate" enough but I have to study this part more thouroughly to get it's meaning.
I also found the comments (everything after -- is a comment) in this section worth of note.This seems like a good place to look into to more deeply, maybe?Code: Select all
local VUPostRepair = VU local hpIncrease = 0 local repairIncentive = 80 -- Should be the max it can be if (not likelyToAttack) and isRailMove ~= true and self:aiCanRepair(unit) then hpIncrease = GetRepairPoints(unit,unit.mp == 0 or hex.id ~= unit.hex.id) VUPostRepair = self:getVulnerability(unit,hex,-hpIncrease) if VUPostRepair < unit.hp+hpIncrease then -- repair incentive should be some kind of combination of: -- How badly do we need to repair -- How _much_ can we repair -- Second is kinda taken care of by above if statement, but probably not enough? -- First should use VUPostRepair... perhaps combined with VU -- But it shouldn't be d ependent on this, because it's also important to repair units which aren't under direct threat. --VU - VUPostRepair -- How much we reduce our vulnerability by. The higher the better. Technically max = 100, but we should cap it, then + 20 or something local repairScore = math.min(VU - VUPostRepair,30) -- Max of 30 local repairScore = repairScore + (100-unit.hp)/2 -- Max of 50 -- Repair incentive is max of 50... seems very low repairIncentive = 80 - repairScore --(100-unit.hp)/2 -- First attempt at balance - very rough guess end end
For now you could always experiment with the ai_economy.lua script by raising the modifiers (maybe +20 oder even multiply them) just in case it's sufficient to get satisfying results by modifying the "Repair-list" and report back.
Good luck!
*edit*
we should probably exclude transports and convoys from the repair checklist in ai_economic.lua, just in case it could lead to problems![]()
Code: Select all
if unit.faction.id == 1 and unit.type == Unit.NAVAL and unit.prototype.name ~= "transport" and unit.prototype.name ~= "convoy" then VU[unit.id] = VU[unit.id] + 10 end
Hi thanks for reply, I will take a much closer look at the (Cost Lua) file, and try to figure out a way to get the AI to repair its ships on a regular bases.
What I can see from looking at the Lua script, is that the AI repairs, any unit that it considers in the frontline. The problem is Naval units don't come under this heading, according to the script, they are regional units. I think I need to create a new FUNCTION that instructs the AI to repair its ships.
Below is the Economic data script that the game uses to allocate a repair value to units. The key part of the whole script is the first part the FUNCTION in brackets it only applies to units, that it considers in the front. function AIPlayer:repairUnits(front) and naval units don't come under this heading so they don't get repaired, which is a serious flaw.
Code: Select all
function AIPlayer:repairUnits(front)
if self.sai.stackLogging then self:printLog("function AIPlayer:entered: self:repairUnits()") end
local units = {}
local VU = {}
for _,unit in ipairs(front.friendlyUnits) do
if unit.alive and unit.undeployed ~= true and unit.hex ~= nil and (self:aiCanRepair(unit) or self:aiCanUpgrade(unit)) then
VU[unit.id] = self:getVulnerability(unit,unit.hex) + (100-unit.hp)/100
if unit.hex.construction ~= nil and (self.sai.vulnerableCitiesThisTurn[unit.hex.id] == true or self:isAdjacentToAlliance(self.sai.enemyAlliance,unit.hex)) then
if VU[unit.id] == 0 and unit.hp < 100 then
VU[unit.id] = math.max((100-unit.hp)*1.2-10,0) --math.min((100-unit.hp)*1.2,100)
else
VU[unit.id] = VU[unit.id]*1.2
end
if unit.hp < 30 then
VU[unit.id] = VU[unit.id] + 20
end
self:printLog("Prioritising unit for repair: "..unit.id.." at "..unit.hex.x..","..unit.hex.y.." because it is in a vulnerable city: "..unit.hex.construction.name)
end
table.insert(units,unit)
end
end
table.sort(units,function(a,b) return VU[a.id] > VU[b.id] end)
for _,unit in ipairs(units) do
if self:aiCanRepair(unit) then
if self.sai.enemyAlliance:IsVisible(unit.hex) then
View:AddEffect(unit.hex, "marker_repair")
end
RepairUnit(unit)
self:printLog("AI repairing: "..unit.hex.x..","..unit.hex.y)
end
end
for _,unit in ipairs(units) do
if self:aiCanUpgrade(unit) then