Javascript API documentation

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

Re: Javascript API documentation

Post by Per » 20 Dec 2012, 00:12

Its .stattype will tell you that. Defined types for now are OIL_RESOURCE, OIL_DRUM, ARTIFACT and BUILDING.

Player numbers for features may change. (Not that this is planned.)

User avatar
aubergine
Professional
Professional
Posts: 3459
Joined: 10 Oct 2010, 00:58
Contact:

Re: Javascript API documentation

Post by aubergine » 20 Dec 2012, 00:30

True, but this...

Code: Select all

if (someObj.player == 12) {
  // do stuff
}
Is a lot cleaner than this...

Code: Select all

if (someObj.type == FEATURE && (someObj.stattype == OIL_RESOURCE || someObj.stattype == OIL_DRUM || someObj.stattype == ARTIFACT) ) {
  // do stuff
}
"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: 3777
Joined: 03 Aug 2006, 19:39

Re: Javascript API documentation

Post by Per » 30 Dec 2012, 23:02

Added new event eventSelectionChanged(objects) and new function enumSelected(), each giving you an array of selected game objects, which may be empty if all objects are deselected by the player.

Documentation from eventSelectionChanged:
//__ An event that is triggered whenever the host player selects one or more game objects.
//__ The \emph{objects} parameter contains an array of the currently selected game objects.
//__ Keep in mind that the player may drag and drop select many units at once, select one
//__ unit specifically, or even add more selections to a current selection one at a time.
//__ This event will trigger once for each user action, not once for each selected or
//__ deselected object. If all selected game objects are deselected, \emph{objects} will
//__ be empty.

Reading eventSelectionChanged()'s parameter and calling enumSelection() will yield the same results. So getting them passed to the event is just a convenience.

Interesting use cases for these will follow later.

User avatar
aubergine
Professional
Professional
Posts: 3459
Joined: 10 Oct 2010, 00:58
Contact:

Re: Javascript API documentation

Post by aubergine » 03 Jan 2013, 07:51

Ah, that is my cue to take some sound files from Stronghold Crusader by Firefly Studios and start giving units Welsh accents :)
"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: 3777
Joined: 03 Aug 2006, 19:39

Re: Javascript API documentation

Post by Per » 06 Jan 2013, 01:37

Added new function hackChangeMe(player) for changing the 'me' special variable. This is only intended for use in campaign scripts until we get a better way of adding scripts for individual players there. If used, it should be called at the earliest possible time in eventGameInit() to avoid making a mess out of timers and such that are tied to the player index.

User avatar
aubergine
Professional
Professional
Posts: 3459
Joined: 10 Oct 2010, 00:58
Contact:

Re: Javascript API documentation

Post by aubergine » 06 Jan 2013, 01:50

Just noticed this in the commits on github. What are the plans for AIs in campaigns? (possibly best discussed in a separate topic?) Specifically I was wondering if it was worth having generic AIs (eg. one per faction - scavs, paradigm, collective, nexus) and have some sort of chat-based protocol for a central 'extra' script to trigger certain actions within them. By default the AIs would lie dormant until they are told to start building / attacking / etc, by the 'extra' (mission-control) script. That way the specifics of the mission are all in one place, and the AIs can be used not just in campaign but also in challenges and, thanks to new <mapname>.ini, even skirmish and MP in WZ 3.2+...

EDIT: Just saw that the 'extra' script runs for all players. Would it be possible to get a singleton extra script (eg. a mission control script) which has only one instance per game, tied to the game host (player 0)?
"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: 3777
Joined: 03 Aug 2006, 19:39

Re: Javascript API documentation

Post by Per » 06 Jan 2013, 12:44

As far as I have read the campaign scripts (which is not very far, I must admit), there has been no need for any generic AI code, just very specific instructions (attack there at time N, retreat at condition M, attack again at time O, etc etc). Eg generic AI code in cam1a would destroy the player before he could even get the campaign off the ground!

I think 'extra' is what you are looking for. See my reply in the other thread.

User avatar
NoQ
Special
Special
Posts: 6226
Joined: 24 Dec 2009, 11:35
Location: /var/zone

Re: Javascript API documentation

Post by NoQ » 15 Jan 2013, 10:01

I'm trying to use labels instead of IDs for making AI remember what he attacks.

If i use addLabel function in an AI script, will other AI script instances see this label?
How to remove a label from an object?

User avatar
aubergine
Professional
Professional
Posts: 3459
Joined: 10 Oct 2010, 00:58
Contact:

Re: Javascript API documentation

Post by aubergine » 15 Jan 2013, 10:14

"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: 3777
Joined: 03 Aug 2006, 19:39

Re: Javascript API documentation

Post by Per » 15 Jan 2013, 13:12

Labels are currently shared between script instances.

User avatar
NoQ
Special
Special
Posts: 6226
Joined: 24 Dec 2009, 11:35
Location: /var/zone

Re: Javascript API documentation

Post by NoQ » 15 Jan 2013, 13:22

I see thanks (:
I think i'll have to use IDs until removeLabel() is implemented. Even if it is, i'm not sure how to make sure i don't run into label collisions with other scripts.

User avatar
aubergine
Professional
Professional
Posts: 3459
Joined: 10 Oct 2010, 00:58
Contact:

Re: Javascript API documentation

Post by aubergine » 15 Jan 2013, 18:57

What if we add a concept of 'tags, in addition to labels'?

Labels

* shared across all scripts
* 1:1 label to object ratio
* label names are unique (can only label one object "foo")

Thus, labels is a named list where label name references object.

Objects have a .label property which states the single label, if any, attached to that object.

Tags

* private to a player script environment
* many:1 tag to object ratio
* tag names not unique (can tag multiple objects with "foo")

Thus, tags is a global object in the scope of the players script (similar approach to groupSizes array).

Tag names are the properties of the tags object. They reference arrays, the arrays containing one or more object references.

Objects have a .tags property, an array listing the tags attributed to the object.

Notes

I realise that this is causing some data duplication, but assume it's relatively easy to keep things synchronised in the C++ backend.

Having tags will simplify many coding tasks for scripters, and also make code somewhat more readable.
"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: 3777
Joined: 03 Aug 2006, 19:39

Re: Javascript API documentation

Post by Per » 15 Jan 2013, 19:29

Labels aren't 1:1, they are N:1 (one object can have multiple labels, but each label can only occur once in the game). I've fixed the documentation for addLabel() that erroneously said otherwise.

I've added a removeLabel(label) function.

Note that labels are implemented as a map label -> game object, which means that looking up labels and getting game objects will be very fast, but looking up game objects to get labels will be very slow. That is why there is no .label property on game objects.

If we were to add tags, it would be most natural to make it a game object -> string map, I think.

Originway
Trained
Trained
Posts: 412
Joined: 08 Aug 2012, 06:22

Re: Javascript API documentation

Post by Originway » 15 Jan 2013, 20:06

can we have a list of all the old functions compared to the new functions for the scripting stuff?

Originway
Trained
Trained
Posts: 412
Joined: 08 Aug 2012, 06:22

Re: Javascript API documentation

Post by Originway » 15 Jan 2013, 20:20

this is the old one I found
Attachments
ScriptingManual.rar
(29.5 KiB) Downloaded 95 times

Post Reply