Jump to content

[Tutorial] Develop a new AI

Recommended Posts

should i use the name of the mod as module parameter?

I tried to use the bot name in caplocks but i keep having the error:

"failed to create AI Player   simulation/ai/imabot/data.json:   can't find the module that should contain the constructor "IMABOT" "

EDIT: i really don't know how, but i fixed it.

Edited by Grugnas
Link to post
Share on other sites
  • 8 months later...

So far I managed to do this, but I'm struggling to find why this.events is always undefined...


var PEDRA = (function(){
var m = {};

// Constructor of PedraBot (first function to be called by Pyrogenesis)
m.PedraBot = function PedraBot(settings)
  API3.warn("Pedrabot: initializing...");
  API3.BaseAI.call(this, settings);

m.Pedrabot.prototype = new API3.BaseAI();

m.Pedrabot.prototype.OnUpdate = function(sharedScript)
  API3.warn("Pedrabot: " + this.events.length + " events.");

  let time = this.gameState.getTimeElapsed()/1000;
  API3.warn("Pedrabot: elapsed time: " + time + "s");

  for(let i in this.events)
    // Why this.events is always undefined?
    API3.warn("Pedrabot: " + this.events[i].entity.templateName);   

return m;

The warnings show but it appears undefined instead of the length of this.events.

Edited by coworotel
Link to post
Share on other sites

The line

m.PedraBot.prototype = new API3.BaseAI();

makes PedraBot inherit from BaseAI (I guess?).

I've found in baseAI.js (part of the common-api):

m.BaseAI.prototype.HandleMessage = function(state, playerID, sharedAI)
	PlayerID = playerID;
	this.events = sharedAI.events;

So this.events in pedraBot is the same as this.events in BaseAI.

Now, when is HandleMessage called on BaseAI? I don't know. There's no call to it in Petra as well.

sharedAI.events is defined in shared.js (part of common-api):

 * General update of the shared script, before each AI's update
 * applies entity deltas, and each gamestate.
m.SharedScript.prototype.onUpdate = function(state)
	if (this.isDeserialized)
		this.init(state, true);
		this.isDeserialized = false;

	// deals with updating based on create and destroy messages.


	// those are dynamic and need to be reset as the "state" object moves in memory.
	this.events = state.events;

Now when is onUpdate called on sharedAI? No idea... Every turn maybe (by the engine)?

Is BaseAI.Init also called by the engine upon beginning of the game?

I'm also assuming that sharedAI is an object of "class" SharedScript, which is initialized through method SharedScript.Init, called by the engine. Not sure though...

I'm a bit lost in this. It doesn't help that I'm not skilled in javascript...

Would greatly appreciate any help.

Edited by coworotel
Link to post
Share on other sites
WARNING: ({Create:[{entity:1093}, {entity:1094}, {entity:1095}, {entity:1096}, {entity:1097}], Destroy:[], Attacked:[], ConstructionFinished:[], DiplomacyChanged:[], TrainingStarted:[], TrainingFinished:[], AIMetadata:[], PlayerDefeated:[], EntityRenamed:[], OwnershipChanged:[], Garrison:[], UnGarrison:[], TerritoriesChanged:[{}], TerritoryDecayChanged:[], TributeExchanged:[], AttackRequest:[], CeasefireEnded:[], DiplomacyRequest:[], TributeRequest:[]})

Thats what this.events looked like for me. So, I guess


would be undefined. Could you instead change that line to:


and see what happens.

Unfortunately, I do not know more than that. Not really familiar with AI code.

Link to post
Share on other sites
13 hours ago, (-_-) said:

Could you instead change that line to:


and see what happens.


That worked (0 A.D. dies if I put this in onUpdate btw).

So in fact it's not this.events that was undefined... Thank you very much!

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...