How to force the AI to repair its ships?

Commander - The Great War: Tech Support

Moderators: Slitherine Core, The Lordz

Post Reply
kirk23
Administrative Corporal - SdKfz 232 8Rad
Administrative Corporal - SdKfz 232 8Rad
Posts: 155
Joined: Wed Jul 18, 2012 1:23 pm

How to force the AI to repair its ships?

Post by kirk23 »

Hello, I'm trying to solve a problem, that is top priority for me in my games. I need the AI to repair its ship's in the same way it routinely repairs its land forces.

I think that maybe altering the AI Economics Lua data file might fix the issue, but so far all my attempts have failed.

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
kirk23
Administrative Corporal - SdKfz 232 8Rad
Administrative Corporal - SdKfz 232 8Rad
Posts: 155
Joined: Wed Jul 18, 2012 1:23 pm

Re: How to force the AI to repair its ships?

Post by kirk23 »

what I can't do is tell the AI to repair its ships, and for me this is off vital importance, in fact I would go as far a say its a game breaker!

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.

Anyone from Slitherine,Matrix or Lords games studio,please answer me, how do you write a script that tell the AI to repair its ships, I need an answer, someone must know how to do it.

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
      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:findAllVulnerableCities(alliance)
  local units = {}
  for faction in self.sai.enemyAlliance.factions do
    for unit in faction.units do
      if unit.alive and unit.hex ~= nil and unit.type == Unit.LAND and unit.state ~= Unit.STATE_EMBARKED then
        table.insert(units,unit)
      end
    end
  end
VPaulus
Slitherine
Slitherine
Posts: 8311
Joined: Mon Dec 27, 2010 8:33 pm
Location: Portugal

Re: How to force the AI to repair its ships?

Post by VPaulus »

I'm sorry but only the developer can help you.
nehi
1st Lieutenant - Grenadier
1st Lieutenant - Grenadier
Posts: 794
Joined: Sat Oct 17, 2015 1:51 pm

Re: How to force the AI to repair its ships?

Post by nehi »

function AIPlayer:repairUnits(front)
front is a function? where is its code?
or if its units variable where is how its set?
which values it can have? true/false? so maybe u could just use trick to use always true, replace front with value which will place it in repairing queue
Robotron
Brigadier-General - Elite Grenadier
Brigadier-General - Elite Grenadier
Posts: 2151
Joined: Tue Nov 23, 2010 3:35 pm

Re: How to force the AI to repair its ships?

Post by Robotron »

I've posted a possible workaround in Mods&Scenarios.
Post Reply

Return to “Commander - The Great War: Tech Support”