[Tool]Custom AI Script

Make AI scripting piece of a cake!:wink:

7 Likes

Giving this a crack now, but am not sure how ClassInitialize() in classdef.lua is supposed to be picking up ships, it’s not pulling anything through for me and starting with a CTD

parameter: table index is nil
stack traceback:
   1:  main of string "" at line 51
   2:  function `dofilepath' [C]
   3:  main of string "" at line 8 
 parameter: attempt to call global `ClassInitialize' (a nil value)
stack traceback:
   1:  function `oninit' at line 33 [string ""] 
 CPUPLAYER -- error in CPUPlayer: oninit() function was not found in CPU script -- FATAL EXIT -- hw2cpuplayer/196:! --stack trace--

oops, my fault
delete line 49,50,51 in classdef.lua and try again
sorry, will update the file shortly:sweat_smile:

1 Like

Line 42 in default.lua also has an extra “-” sign in there which causes a CTD

Once that’s done there’s also multiple calls on the following

 Cannot overwrite function Proc_DetermineDemandWithNoCounterInfo 
 Cannot overwrite function Proc_DetermineSpecialDemand 
 Cannot overwrite function CpuBuildSS_DefaultSubSystemDemandRules 
 Cannot overwrite function DoUpgradeDemand 
 Cannot overwrite function DoResearchTechDemand 

And then this

parameter: `for' table must be a table
stack traceback:
   1:  function `BuildChannelCount' at line 103 [string ""]
   2:  function `CalcOpenBuildChannels' at line 195 [string ""]
   3:  function `doai' at line 281 [string ""] 

Tables are my Achilles heel <_>

It looks like two build channels are hardcoded in there, I have up to three ship build channels and the default game has up to two (plus the subsystem one).

the ai.lua file is loaded by dofilepath(SelfRace_GetString(“path_ai”, “”))
so you need to define a path for it in scripts<RaceName>\props\default.lua
if you are using 2.X RM

edit: file updated, try start with new files

I just wrote over the default AI, so didn’t need to update any paths there. Let’s have a look at 1.1 for you!


Multiple calls are all resolved!

1.1 is still crashing with the extra “-” sign in default.lua mate :slight_smile:

parameter: attempt to perform arithmetic on global `sg_kDemandResetValue' (a nil value)
stack traceback:
   1:  function `oninit' at line 50 [string ""] 
 CPUPLAYER -- error in CPUPlayer: oninit() function was not found in CPU script -- FATAL EXIT -- hw2cpuplayer/196:! --stack trace--  

And the whole process is still getting hung up on that one build channel function too

 parameter: `for' table must be a table
stack traceback:
   1:  function `BuildChannelCount' at line 74 [string ""]
   2:  function `CalcOpenBuildChannels' at line 170 [string ""]
   3:  function `doai' at line 251 [string ""] 

I did a print statement in ai.lua and in default.lua to see if they were being called out of order too and the print statement for ai.lua just didn’t come up, I think you might need to move where the ai_data table is declared perhaps? :thinking:

i’m using this thing just fine, it can’t find ‘sg_kDemandResetValue’ because ai.lua is not loaded. don’t remove that ‘-’

1 Like

Looking thru the code, i’ve got 2 questions

  1. How does the AI handle or requirements on subsystems and research? (eg. CAPSHIPPRODUCTION & RESEARCHADVANCED | RESEARCH )
  2. How would the AI handle required subsystems that are pre-built on certain ships? (eg. The research subsystem on the research ships for both taiidan and kushan)

[Edit]
I’ll add this here instead of creating a new post, this is more to inform people who use your AI rather the reporting a bug.

With the entries in the ai.lua file, the types are case sensitive. Ship Subsystem Research . I’m posting this as in the example ai.lua file, the Hiigaran Battlecruiser has a type ship << all lower case which is wrong. At least based on lines 121 & 143 of cpulogic.lua.

I see what you mean here now…

Wait… does this mean we need to do an ai_data[<<SHIPNAME>>] entry for every single ship and subsystem for them to work in your framework?

1.just write down all requirements
2.write down only the ship carries that subsystem because you can only build the ship, the subsystem on it is not included in build list.

yes, every item that you want AI to build or research (except upgrade technologies)

I have this error (after add all ships / sub / research of my race test) :

 parameter: attempt to perform arithmetic on field `ShipBuildChannel' (a nil value)
stack traceback:
   1:  function `BuildChannelCount' at line 76 [string ""]
   2:  function `CalcOpenBuildChannels' at line 170 [string ""]
   3:  function `doai' at line 251 [string ""]  

This error is, at this time, on my Frigate. But it’s just a ship so the AI_DATA line is this :

ai_data[GOA_ZATFRIGATE]=
	{
		Type="Ship",
		Require={GOA_FRIGATE_MODULE,ZATTECH},
		Upgrades={},
		UpgradeDemand=1,
		BuildShipChannel=0, -- Seems to have an error here ... but it's the same of the other ... 
		BuildSubsystemChannel=0,
		BasicDemand=0,
		UnitCap="Frigate",
	}

You see any error on this part ? :slight_smile:

Thanks for help by the way.

is this unit included in build.lua?

Yes, all units are on my build file, and for research is the same.

make sure you didn’t give any reseach/subsystem a type “ship”

In CPULogic.lua

  1. At line 76, change item_info.ShipBuildChannel to item_info.BuildShipChannel
  2. At line 77, change item_info.SubsystemBuildChannel to item_info.BuildSubsystemChannel

wired, guess my example was wrong, anyway, thanks for pointing out, and that will fix the problem, too.

Thanks I will try this after work. I think it will work ^^

Ok, it’s work, but AI just want to build the salvage unit and other military unit, but don’t go on ressource point.

Other problem i have, i need to add technologie like this :

{
	Name =					"apophisgod",
	RequiredResearch =		"BattlecruiserIonWeapons & !anubisgod",
	RequiredSubSystems =	"Research | AdvancedResearch",
	Cost = 					1000,
	Time = 					120,
	DisplayedName =			"$8047", -- research name in description
	DisplayPriority =		100,
	Description =			"$8037", -- description
	TargetName = 		"lab2",
	Icon = Icon_Tech,
	ShortDisplayedName = "$8047", -- research name
},

That’s mean, the player can’t research this research if we have already research the other. It’s a choice for a player. :stuck_out_tongue:

So, we need to have this type of Require :

ai_data[APOPHISGOD]=
	{
		Type="Research",--upgrade technologies should not be included
		Require={BATTLECRUISERIONWEAPONS, !ANUBISGOD, GOA_RESEARCH_MODULE},
		BasicDemand=0,
	}

With ‘!’ to not allow this research if we have it … but i think we can add a new line in the table to have exclude research if the previous research is actually researched.

I will thinking next time on this… when i sleeping :smiley:

check your classdef.lua to see if every unit is in proper class.
As for NOT logic, I will add it later.