| 1 | AI scripts receive data from the engine about the current simulation state, once per simulation turn. This is a fairly low-level mechanism - it is expected that AI scripts will use a wrapper to provide more convenient access to the data. Currently this wrapper is implemented in `common-ai/base.js`. |
| 2 | |
| 3 | The AI's `HandleMessage` method is called with one argument: |
| 4 | |
| 5 | {{{ |
| 6 | state = { |
| 7 | entities: ..., |
| 8 | events: ..., |
| 9 | players: [...], |
| 10 | timeElapsed: ..., // seconds since the start of the match |
| 11 | map: ..., |
| 12 | passabilityClasses: ... |
| 13 | }; |
| 14 | }}} |
| 15 | |
| 16 | == `entities` == |
| 17 | |
| 18 | TODO |
| 19 | |
| 20 | == `events` == |
| 21 | |
| 22 | TODO |
| 23 | |
| 24 | == `map` and `passabilityClasses` == |
| 25 | |
| 26 | {{{ |
| 27 | state.map = { |
| 28 | width: 256, |
| 29 | height: 256, |
| 30 | data: [ ... ] // Uint16Array with width*height entries |
| 31 | }; |
| 32 | }}} |
| 33 | |
| 34 | {{{ |
| 35 | state.passabilityClasses = { |
| 36 | "pathfinderObstruction": 1, |
| 37 | "foundationObstruction": 2, |
| 38 | "building-land": ..., // these are all the PassabilityClasses defined in simulation/data/pathfinder.xml |
| 39 | ... |
| 40 | }; |
| 41 | }}} |
| 42 | |
| 43 | `state.map.data` encodes all the passability data of each terrain tile. `state.passabilityClasses` gives bitmasks that define how to interpret `state.map.data`. For example: |
| 44 | |
| 45 | {{{ |
| 46 | // Get the bitmask for tiles that will obstruct foundations (i.e. you can't place any buildings there) |
| 47 | var obstructionMask = gameState.getPassabilityClassMask("foundationObstruction"); |
| 48 | |
| 49 | for (var i = 0; i < map.data.length; ++i) |
| 50 | if (map.data[i] & obstructionMask) |
| 51 | ; // tile i is an unbuildable location |
| 52 | |
| 53 | }}} |
| 54 | |
| 55 | Since these are bitmasks, you can 'or' them together: |
| 56 | |
| 57 | {{{ |
| 58 | var obstructionMask = gameState.getPassabilityClassMask("foundationObstruction"); |
| 59 | |
| 60 | // Add in the bitmask for tiles that are obstructed for the "building-land" passability class |
| 61 | // (i.e. tiles that are underwater or too steep, based on the definition in pathfinder.xml) |
| 62 | obstructionMask |= gameState.getPassabilityClassMask("building-land"); |
| 63 | |
| 64 | for (var i = 0; i < map.data.length; ++i) |
| 65 | if (map.data[i] & obstructionMask) |
| 66 | ; // tile i is an unbuildable location for land-based buildings |
| 67 | }}} |