Page 1 of 2

Help with creating an AI using JS

Posted: 12 Jan 2012, 03:43
by aubergine
I'm interested in creating an AI using JavaScript so I started poking around the Semperfi JS AI.

Using the master version of semperfi.js (ie. what you get when you donwload latest wz master) in a single player skirmish, the AI doesn't seem to do anything. Is there a version of semperfi.js that will build something, or is the ability to use JS AIs unavailable in the current master?

Re: Help with creating an AI using JS

Posted: 12 Jan 2012, 07:14
by NoQ
Based on what i've seen Semperfi-JS doesn't do much yet, even though it builds a few oils and power gens. On the other hand, i'm already trying to play with it too (:

Re: Help with creating an AI using JS

Posted: 12 Jan 2012, 08:50
by Per
Right. It does not do much yet. In particular, it does not yet build up its base. If you let it start with buildings, or give it some, it will use them to build an army and attack you sometimes.

Re: Help with creating an AI using JS

Posted: 12 Jan 2012, 08:52
by NoQ
Anyhow, i suppose in this topic people help with creating an AI using JS? :D

I try to code:

Code: Select all

const tankBodies = [
	"Body7ABT", // retribution
	"Body8MBT", // scorpion
	"Body5REC", // cobra
	"Body1REC", // viper
];

const tankPropulsions = [
	"tracked01", // tracks
	"HalfTrack", // half-track
	"wheeled01", // wheels
];

const mgWeapons = [
	"MG5TWINROTARY",
	"MG4ROTARYMk1",
	"MG3Mk1",
	"MG2Mk1",
	"MG1Mk1",
];

/* LOTS OF STUFF IN BETWEEN */

    buildDroid(struct, "Some Tank", tankBodies, tankPropulsions, "", DROID_CONSTRUCT, mgWeapons);
I get the following error:

Code: Select all

error   |10:56:03: [get_first_available_component] No such component: MG5TWINROTARY
error   |10:56:03: [get_first_available_component] No such component: MG4ROTARYMk1
error   |10:56:03: [get_first_available_component] No such component: MG3Mk1
error   |10:56:03: [get_first_available_component] No such component: MG2Mk1
error   |10:56:03: [get_first_available_component] No such component: MG1Mk1
:hmm: :hmm: :hmm:

Re: Help with creating an AI using JS

Posted: 12 Jan 2012, 08:59
by NoQ
Hmm, since it works fine with Semperfi JS, i'd better post the complete code ... of an AI that builds a base but doesn't attack yet ^^

Re: Help with creating an AI using JS

Posted: 12 Jan 2012, 12:16
by aubergine
That nullbot is really useful - thanks! If I wanted to turn that in to a mod, what would I do? (mainly because I'm having problems editing base.wz so if I can experiment in a mod that would make life easier)

Re: Help with creating an AI using JS

Posted: 12 Jan 2012, 12:24
by NoQ
You need to add nullbot.ai file with obvious contents

Code: Select all

[AI]
name = "NullBot v0.000001 pre-alpha"
js = nullbot.js
put the two files into clean /multiplay/skirmish/ folder and zip multiplay folder (not its contents, but the folder itself), and the multiplayer mod is ready.

Re: Help with creating an AI using JS

Posted: 12 Jan 2012, 12:52
by NoQ
DROID_CONSTRUCT
Meet the idiot :oops:

Re: Help with creating an AI using JS

Posted: 12 Jan 2012, 13:00
by aubergine
w00t, it works - here's sample mod .wz file (attached) for anyone else wanting to play with nullbot.

It seems like pickStructLocation() is having problems (or it's return value is misinterpreted?) as after building a research lab the next thing on the list is a factory, however the location for the factory seems to be overlapping (or directly adjacent to) the existing research lab position = doesn't get built. I've not been fast enough to grab a screenshot, but mocked this up to illustrate what is happening:

Image

Are the x,y returned by pickStructLocation() the center, top-left or top-right of a building? And when ordering a droid to create a building, are the x,y using the same origin point? It would be useful to have that clarified in the JS API docu.

Re: Help with creating an AI using JS

Posted: 12 Jan 2012, 13:09
by NoQ

Re: Help with creating an AI using JS

Posted: 12 Jan 2012, 13:14
by aubergine
What was the issue with DROID_CONSTRUCT? (I'm totally new to WZ scripting so still lots to learn)

Re: Help with creating an AI using JS

Posted: 12 Jan 2012, 13:15
by aubergine
I don't have a build environment set-up on my mac - is there a JS workaround I can implement for now to overcome that issue?

Re: Help with creating an AI using JS

Posted: 12 Jan 2012, 13:18
by NoQ
I used to add +1 to both loc.x and loc.y and it seemed to do the job, but it doesn't work for cyborg factories.
aubergine wrote:What was the issue with DROID_CONSTRUCT? (I'm totally new to WZ scripting so still lots to learn)
Producing tanks failed because i put DROID_CONSTRUCT instead of DROID_WEAPON (By the way, can't this value be auto-detected, based on turrets?)

Re: Help with creating an AI using JS

Posted: 12 Jan 2012, 14:12
by aubergine
Just dabbling with some utility functions before diving in to more complex stuff - is this of any use:

Code: Select all

// find the closest item in featureList to a given x,y point
// optionally limited by a min/max range and droid's ability to get there
function findNearestFeature(featureList,x,y,minRange,maxRange,droid) {
	// param defaults (leave param empty or null to use default)
	minRange = minRange || 0;
	maxRange = maxRange || Number.MAX_VALUE;
	
	var nearestFeature = {
		dist:    maxRange,
		feature: null
	};
	var feature, range;
	var i = featureList.length;

	// find the nearest feature within range limits
	while (-1<--i) {
		feature = featureList[i];
		if (!!droid && !droidCanReach(droid,feature.x,feature.y)) continue;
		dist    = distBetweenTwoPoints(x,y,feature.x,feature.y);
		if (dist<nearestFeature.dist && dist>=minRange) {
			nearestFeature = {
				dist:    dist,
				feature: feature;
			};
		}
	}

	return nearestFeature;
}

Re: Help with creating an AI using JS

Posted: 12 Jan 2012, 15:11
by aubergine
How do I find out if a given droid object is a VTOL or not? I know it will be defined in the .droidType property, but can't find a list of possible values for that property :s