📜 ⬆️ ⬇️

Web Worker Wars

Web Worker Wars is a JavaScript program developed by me for programmers written, of course, in JavaScript.
A variety of games Battle in memory. Something similar to the Google AI Challenge or HabraWars.

Web Worker Wars

Features and rules


1. The game is a turn-based strategy for 2 or more bots.
2. Each player writes his Web Worker, which accepts special commands from the game engine and can return an action.
3. On each move, the bot has 4 action points and can distribute them to their actions.
4. The bot has a limited field of view (the example is highlighted in blue on the logo).
- All objects in the field of view are transferred to the callback actions and can be used in the calculations
5. While the bot can perform 2 actions:
- movement 1 square left, right, up, down, cost 1 OD
- an aimed shot (hits the cage) at a distance of up to 5 cells, costing 2 OD, removes 2 points of life or shield from the enemy or yourself
')
Further detailed rules, example worker and demo.

6. Until the bot has run out OD The engine will request actions from the bot.
- If the bot did not respond 2 seconds after the action request, its turn is completed, the ODs are burned
- If there is not enough ML for an action, then the action is not performed, the bot's turn is completed, OD is burned
- As soon as the action is completed, the bot removes a certain amount of ML
- If the bot has sent a non-existent command, 1 MP is removed
- The bot can not walk on the walls or on other players
- After each action the bot can turn in any direction
7. The first walker that first walked
- In the future, it is supposed to remake: the minimum volume worker will walk first.
8. Each bot has 10 Life Points and 2 Shield Points.
- The shield is regenerated by +1 at the end of each phase (when everyone was leaving), but there can not be more than 2
9. The playing field has a size of 10 to 10 cells of the wall of the field - the wall (the field is actually 8 to 8).
10. At the beginning, the bots are placed at the corners: the first bot in a cell is 1.1 the second - 8.8
11. The game has a limit of 500 moves (2000 OD on all bots)
12. Each bot can receive different events: (onDamage, onHit, onAfterMove ...)
- While their number is limited

While the game is in trial mode, any rule may change.

Example worker


Its logic is simple: a worker walking erratically and turning randomly, looking for the enemy, as soon as the enemy gets into his field of vision, he starts shooting to the last (the example removed almost all comments in the article, the full version either in the demo or in the archive).
/**#nocode+*/ (function (global) { /**#nocode-*/ /** * Callbacks * * @namespace Callbacks */ var Callbacks = { callback: function (state) { // its own id var id = state.player.name, target, i, c, ok = false, fow = []; // looking for enemy in fow for (i = 0, c = state.fow.length; i < c; i += 1) { // push fow elements to 2d array, required for movements if (!fow[state.fow[i].y]) { fow[state.fow[i].y] = []; } fow[state.fow[i].y][state.fow[i].x] = state.fow[i].object; if (typeof state.fow[i].object === 'object' && state.fow[i].object.name !== id) { // found! target = { x: state.fow[i].x, y: state.fow[i].y }; } } if (target) { // target - shoot! Player.post({ action: 'shoot', // shoot action options: target, // enemy xy direction: state.player.direction // do not change direction }); } else { // no target - seeking for target // make random moves if (Math.random() > 0.5) { // move to x while (!ok) { // check if cell not occupied target = { x: state.player.x + (~~(Math.random() * 3) - 1), y: state.player.y }; if (typeof fow[target.y] === 'undefined' || typeof fow[target.y][target.x] === 'undefined' ) { ok = true; // cell is free } } } else { // or move to y while (!ok) { // check if cell not occupied target = { x: state.player.x, y: state.player.y + (~~(Math.random() * 3) - 1) }; if (typeof fow[target.y] === 'undefined' || typeof fow[target.y][target.x] === 'undefined' ) { ok = true; // cell is free } } } Player.post({ action: 'move', // move action options: target, direction: ~~(Math.random() * 5) // random direction }); } }, onHit: function (param) { }, onDamage: function (param) { }, onKill: function (param) { }, onDead: function () { } }; /** * Player * @namespace Player */ var Player = { post: function (data) { global.postMessage(JSON.stringify(data)); } }; /** * Message listener */ global.onmessage = function (e) { var data = JSON.parse(e.data); if (data.call && typeof Callbacks[data.call] === 'function') { Callbacks[data.call](data.arguments); } }; /**#nocode+*/ }(this)); /**#nocode-*/ 

Battle initializer: azproduction.ru/web_worker_wars
Demo 2 bots: azproduction.ru/web_worker_wars/arena.html#ZRV1vYGY&ZRV1vYGY
4 bots azproduction.ru/web_worker_wars/arena.html#ZRV1vYGY&ZRV1vYGY&ZRV1vYGY&ZRV1vYGY

Visualization in the demo - a record of the previous battle of workers. not realtime.

Archive with all files (old version): narod.ru/disk/1947680001/web_worker_wars.rar.html
Google code project code.google.com/p/web-worker-wars

I wonder your opinion about the game, suggestions for improvement.

UPD Now you can upload your bots to pastebin.com and use the pastebin script id (http://pastebin.com/ZRV1vYGY) to launch bots in the battle initializer azproduction.ru/web_worker_wars

Source: https://habr.com/ru/post/110540/


All Articles