Javascript API documentation

For AI and campaign script related discussions and questions
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3780
Joined: 03 Aug 2006, 19:39

Re: Javascript API documentation

Post by Per »

A short heads up: I've shuffled the parameters for the addFeature() call, to make it similar to the order used in wzscript, and thus make porting simpler. I'm pretty sure nobody uses this function already, since it was broken and would crash until yesterday. But I'm saying just in case anyway.

I'm currently working on porting the cam1a campaign mission to javascript. I will say more about this when I am done.
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3780
Joined: 03 Aug 2006, 19:39

Re: Javascript API documentation

Post by Per »

I have added a new function:

hackMarkTiles([label | x, y[, x2, y2]])

It will mark tiles on the map with a pulsing red colour. You can pass it an area label, point coordinates, or area coordinates; or no parameters to clear all such marks.

It is mostly for debugging for now (hence the hack prefix), however I'm sure once you start playing with it, there will be a hundred follow up feature requests ... :) It is, if I may say so, a pretty nifty little trick... :geek:
User avatar
aubergine
Professional
Professional
Posts: 3462
Joined: 10 Oct 2010, 00:58

Re: Javascript API documentation

Post by aubergine »

Per wrote:hackMarkTiles([label | x, y[, x2, y2]])

... however I'm sure once you start playing with it, there will be a hundred follow up feature requests ...
1. I'd like to be able to toggle individual tiles on and off without affecting other tiles.

2. I'd like to be able to set the colour of the marking. For example, I want to create a heatmap of enemy threat level or most regularly travelled paths.
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO
User avatar
NoQ
Special
Special
Posts: 6226
Joined: 24 Dec 2009, 11:35
Location: /var/zone

Re: Javascript API documentation

Post by NoQ »

We still don't have a function for giving units or power to an ally? :o Just wanted to use something like that (:
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3780
Joined: 03 Aug 2006, 19:39

Re: Javascript API documentation

Post by Per »

NoQ wrote:We still don't have a function for giving units or power to an ally? :o Just wanted to use something like that (:
Added function donateObject(object, to) -- Donate a game object (currently restricted to droids) to another player. Returns true if donation was successful. May return false if this donation would push the receiving player over unit limits.
User avatar
Emdek
Regular
Regular
Posts: 1329
Joined: 24 Jan 2010, 13:14
Location: Poland

Re: Javascript API documentation

Post by Emdek »

Per wrote:May return false if this donation would push the receiving player over unit limits.
So maybe there should be also function to check if we can donate it (like canDonateObject(object, to) returning boolean)?
Nadszedł już czas, najwyższy czas, nienawiść zniszczyć w sobie.
The time has come, the high time, to destroy hatred in oneself.


Beware! Mad Qt Evangelist.
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3780
Joined: 03 Aug 2006, 19:39

Re: Javascript API documentation

Post by Per »

Added function donatePower(amount, to) -- pretty self-explanatory.

Semperfi-JS will now respond to 'donatetruck' and 'donatepower', if you are allied.

@Emdek - Just use getDroidLimit().
User avatar
aubergine
Professional
Professional
Posts: 3462
Joined: 10 Oct 2010, 00:58

Re: Javascript API documentation

Post by aubergine »

Would it be possible to rename donateObject() to transferObject() as the associated event is eventObjectTransfer()

And then rename donatePower() to transferPower() to keep with same convention + the computer voice says "power transfered" too: https://warzone.atlassian.net/wiki/down ... ferred.ogg

Also, would it be possible to get an eventPowerTransfer(amount, from, to) -- AIs that carefully track power usage will benefit from knowing when they have a sudden influx or loss of power.
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO
User avatar
NoQ
Special
Special
Posts: 6226
Joined: 24 Dec 2009, 11:35
Location: /var/zone

Re: Javascript API documentation

Post by NoQ »

Yay thanks!

I'm still looking for a function similar to playerPower(), but taking debts into account. That is, if a player has $99 and currently struggling to build a factory and a lab, it should return -$101 rather than $99.
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3780
Joined: 03 Aug 2006, 19:39

Re: Javascript API documentation

Post by Per »

NoQ wrote:I'm still looking for a function similar to playerPower(), but taking debts into account. That is, if a player has $99 and currently struggling to build a factory and a lab, it should return -$101 rather than $99.
Added function queuedPower(player) to return the amount of power queued up for production. So, myNetDebt = playerPower(me) - queuedPower(me);
User avatar
NoQ
Special
Special
Posts: 6226
Joined: 24 Dec 2009, 11:35
Location: /var/zone

Re: Javascript API documentation

Post by NoQ »

Thanks!

Whoops some bug: the AI's power doesn't reduce when he donates it.
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3780
Joined: 03 Aug 2006, 19:39

Re: Javascript API documentation

Post by Per »

NoQ wrote:Whoops some bug: the AI's power doesn't reduce when he donates it.
Already fixed :) 9e2837a23c24bca31c4f33e00ec3f0dcbf4c62bd.
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3780
Joined: 03 Aug 2006, 19:39

Re: Javascript API documentation

Post by Per »

The bind() function, which always said "This function is under construction and is subject to total change!", has been removed. It was true to its documentation in that respect, at least.

setTimer() and queue() can now take a string argument as their third argument, which will be passed on to the called function.

See 592854b6025d3b7ad66cdc9fe7af333f123fc9de for the gritty details.

I am considering the following changes to timers:
  • A repeat timer will stop if it returns false. This is just a convenience which does the same as calling removeTimer() but shorter.
  • If a timer is set (or "queued"), and a timer with the same name and arguments is already set, it will either replace it, if time to first invocation is shorter, or be dropped. The reason for this is that it is very easy to call timers too frequently without knowing it, causing lots of unnecessary CPU use, and checking each time is cumbersome. I discovered this when investigating CPU spikes in Semperfi-JS recently.
Feedback desired.
User avatar
aubergine
Professional
Professional
Posts: 3462
Joined: 10 Oct 2010, 00:58

Re: Javascript API documentation

Post by aubergine »

Per wrote:The bind() function, which always said "This function is under construction and is subject to total change!", has been removed. It was true to its documentation in that respect, at least.
Urg, how do we monitor enemy objects or features to detect when they are destroyed? I was looking to use this to have my attack groups switch to their next target when current target goes kaboom. I think ShadowWolf TJC was also planning to do similar.
Per wrote:setTimer() and queue() can now take a string argument as their third argument, which will be passed on to the called function.
Wooohooo!!! :D :yahoo: :bow2: Thank you!!! :D
Per wrote: I am considering the following changes to timers:
  • A repeat timer will stop if it returns false. This is just a convenience which does the same as calling removeTimer() but shorter.
  • If a timer is set (or "queued"), and a timer with the same name and arguments is already set, it will either replace it, if time to first invocation is shorter, or be dropped. The reason for this is that it is very easy to call timers too frequently without knowing it, causing lots of unnecessary CPU use, and checking each time is cumbersome. I discovered this when investigating CPU spikes in Semperfi-JS recently.
Feedback desired.
+1 to both of those ideas, they will make life much easier. I assume on the first item, about returning false, the timer will only be cancelled if the function returns an actual Boolean false, not a falsey value (eg. undefined and null are both falsey values).
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3780
Joined: 03 Aug 2006, 19:39

Re: Javascript API documentation

Post by Per »

aubergine wrote:Urg, how do we monitor enemy objects or features to detect when they are destroyed? I was looking to use this to have my attack groups switch to their next target when current target goes kaboom.
1) I'm looking at removing the throttling on object death events. I will look a bit at load balancing events first, to ensure I'm not creating a monster. eventDestroyed() is not throttled. It can be used to monitor object destruction.
2) You can drop the objects to be monitored into a group. When the group loses members, you will get a group loss notification. I think this is usually what you want to do.
3) You can add a label to the object and poll it.
aubergine wrote:+1 to both of those ideas, they will make life much easier. I assume on the first item, about returning false, the timer will only be cancelled if the function returns an actual Boolean false, not a falsey value (eg. undefined and null are both falsey values).
Yep.