[Tool]Custom AI Script

Yeah, i have copy paste you’re classdef, and i forget to replace this classdef without any ship, by mine (that same but with the ship ^^).

EDIT:
Oh, and i see you don’t use eAmiral in you’re classdef ? (For ship like SAJUUK (in my case anubisflagship for example)).

is eAmiral a stock class in HWRM? I’m not aware of that

Used in HW Classic, for Sajuuk and other Amiral ship, but doesn’t use in HWRM, so IA can’t build Sajuuk and other amiral ship.

EDIT :
The new error (after made lot of print to find the issue of other) :

 parameter: attempt to index a nil value
stack traceback:
   1:  function `CpuBuildSS_ProcessEachBuildShip' at line 61 [string ""]
   2:  function `CpuBuildSS_DoBuildSubSystem' at line 149 [string ""]
   3:  function `DoMilitaryBuild' at line 353 [string ""]
   4:  function `CpuBuild_Process' at line 325 [string ""]
   5:  function `SpendMoney' at line 240 [string ""]
   6:  function `doai' at line 255 [string ""] 

Seems to have link with my Battlecruiser units :

ai_data[GOA_HATAK]=
	{
		Type="Ship",
		Require={CAPSHIPPRODUCTION,BATTLECRUISERIONWEAPONS},
		Upgrades={},
		UpgradeDemand=1,
		BuildShipChannel=0,
		BuildSubsystemChannel=1,
		UpgradeSubsystem=
		{
			{Num=0},
		},
		BasicDemand=0,
		UnitCap="Battlecruiser",
	}

between line 60 and 61 of cpubildsubsystem.lua add print("Build ship is - "..buildShip). This is the “numbered” value of the ship that isn’t in ai_data or for which UpgradeDemand doesn’t appear in it’s ai_data.

You could try switching line 61 and 62 around.

[Edit]
I’m unsure of any proper way of getting the name of the ship, but you could try adding a new function

function getBuilderIndexPos(nShip)
  local nget = getn(squadclass[eBuilder])
  if (nget == 0) then
    print("No builders"); return
  end
  local npos = 0
  for i = 1,nget do
    npos = npos + 1
    if (squadclass[eBuilder][i]~= nil and squadclass[eBuilder][i]== nShip) then
      print("Builder at position "..npos)
      return
    end
  end
  print("Builder not found")
end

Then replace the earlier print function with print("Builder at pos - "..getBuilderIndexPos(buildShip))
Then in classdef.lua, find squadclass[eBuilder] and the ship should be at the position the print statement said. If all works, that unit is either not in ai_data, or it doesn’t include UpgradeDemand.

Delete the whole “UpgradeSubsystem” thing if you don’t want to use this function

It’s the mothership … But can’t build by the AI because it’s one ship don’t have build option in build.lua ^^

So, need to add to database the mothership ?
By the way, even if we have this error, AI build units and attack.

It’s more on Basicdemand value i need clarification. Higher value = more demand for AI (like : You need to build this when you can) or lower value ? :stuck_out_tongue: [Because i see, when all is 0, AI build the same number of Ressource collector / rafinery / corvette / fighter ]

must be in build.lua, otherwise it is nil.

higher BasicDemand, the AI is more likely to build/research

So mothership can’t build subsystem and that’s all … because i can’t add this ship in the build.lua, it’s the base ship added at the beginning …

why not? you can restrict it, right?

A restriction on unitcap, yeah. And the only ship can build mothership is the mothership … but can’t build with unitcap … Yeah i see… x)

After Test, all seems to work but … When you have 3 different races AI, the game crash.

that’s wired, any error report in the log file?
Does every single race’ ai run properly?

single yes.

No Error on log. Crash for … nothing xD But crash on loading when load AI.

I’ve loaded 7 races and it’s fine.
Files updated to 1.04, you may try again.
Please notice that the path for classdef.lua has changed.

ok.

I will try that tomorrow :wink:

I have update my file.

But, the AI don’t go harvest RU, only build and made “shopping” with a limited Credit Card :smile: It’s the first problem here … But i can launch more than 3 AI with 3 different race.

1 Like

path for classdef.lua has changed, make sure you’ve load it correctly.

Yeah i have already apply this change. Don’t worry about that ^^

Problem Solved … just edited the wrong file (file on my Dest’, and not the good file in ai xD )

:stuck_out_tongue_closed_eyes:

Thanks a lot for this code, @HW_Lover! I’ve already implemented it in my mod for the next release (the source of the code will be listed in the mod description, of course).

1 Like

@HW_Lover

There’s a bug reported by my players and that I reproduced: when the AI Script is turned on, trying to save the game lead to an immediate CTD (if I rename the ai folder as ai-old, it doesn’t crash) with the log saying:

parameter: invalid key for `next’
parameter: unable to recover; exiting

According to this (http://www.computercraft.info/forums2/index.php?/topic/11043-invalid-key-to-next/), it occurs when you add or remove a key from a table while iterating on it using next (e.g. in pairs()). Is there any way to fix this or is this an error from my implementation of the script?