├── README.md ├── package.json ├── .gitignore ├── LICENSE.txt └── main.js /README.md: -------------------------------------------------------------------------------- 1 | # generals.io Node.js Bot Example 2 | 3 | This is an example of a basic Javascript implementation of a bot for [generals.io](http://generals.io). Read the tutorial associated with this bot at [dev.generals.io/api#tutorial](http://dev.generals.io/api#tutorial). 4 | 5 | ## Usage 6 | 7 | ``` 8 | $ git clone https://github.com/vzhou842/generals.io-Node.js-Bot-example.git 9 | $ cd generals.io-Node.js-Bot-example 10 | $ npm install 11 | $ node main.js 12 | ``` 13 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "generals.io-node.js-bot-example", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "main.js", 6 | "dependencies": { 7 | "socket.io-client": "^1.7.2" 8 | }, 9 | "devDependencies": {}, 10 | "scripts": { 11 | "test": "echo 'No tests implemented.'" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "git+https://github.com/vzhou842/generals.io-Node.js-Bot-example.git" 16 | }, 17 | "keywords": [ 18 | "generals.io", 19 | "bot", 20 | "node.js", 21 | "javascript", 22 | "example" 23 | ], 24 | "author": "Victor Zhou", 25 | "license": "MIT", 26 | "bugs": { 27 | "url": "https://github.com/vzhou842/generals.io-Node.js-Bot-example/issues" 28 | }, 29 | "homepage": "https://github.com/vzhou842/generals.io-Node.js-Bot-example#readme" 30 | } 31 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 18 | .grunt 19 | 20 | # node-waf configuration 21 | .lock-wscript 22 | 23 | # Compiled binary addons (http://nodejs.org/api/addons.html) 24 | build/Release 25 | 26 | # Optional npm cache directory 27 | .npm 28 | 29 | # Optional REPL history 30 | .node_repl_history 31 | 32 | dump.rdb 33 | 34 | .gitignore~ 35 | 36 | # Use the --save option of npm install to alter package.json (e.g., 37 | # “npm install --save my-package-name”). To restore packages after 38 | # checking out, simply run “npm install”. 39 | node_modules 40 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Victor Zhou 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /main.js: -------------------------------------------------------------------------------- 1 | var io = require('socket.io-client'); 2 | 3 | var socket = io('http://botws.generals.io'); 4 | 5 | socket.on('disconnect', function() { 6 | console.error('Disconnected from server.'); 7 | process.exit(1); 8 | }); 9 | 10 | socket.on('connect', function() { 11 | console.log('Connected to server.'); 12 | 13 | /* Don't lose this user_id or let other people see it! 14 | * Anyone with your user_id can play on your bot's account and pretend to be your bot. 15 | * If you plan on open sourcing your bot's code (which we strongly support), we recommend 16 | * replacing this line with something that instead supplies the user_id via an environment variable, e.g. 17 | * var user_id = process.env.BOT_USER_ID; 18 | */ 19 | var user_id = 'my_example_bot_id'; 20 | var username = 'Example Bot'; 21 | 22 | // Set the username for the bot. 23 | // This should only ever be done once. See the API reference for more details. 24 | socket.emit('set_username', user_id, username); 25 | 26 | // Join a custom game and force start immediately. 27 | // Custom games are a great way to test your bot while you develop it because you can play against your bot! 28 | var custom_game_id = 'my_private_game'; 29 | socket.emit('join_private', custom_game_id, user_id); 30 | socket.emit('set_force_start', custom_game_id, true); 31 | console.log('Joined custom game at http://bot.generals.io/games/' + encodeURIComponent(custom_game_id)); 32 | 33 | // When you're ready, you can have your bot join other game modes. 34 | // Here are some examples of how you'd do that: 35 | 36 | // Join the 1v1 queue. 37 | // socket.emit('join_1v1', user_id); 38 | 39 | // Join the FFA queue. 40 | // socket.emit('play', user_id); 41 | 42 | // Join a 2v2 team. 43 | // socket.emit('join_team', 'team_name', user_id); 44 | }); 45 | 46 | // Terrain Constants. 47 | // Any tile with a nonnegative value is owned by the player corresponding to its value. 48 | // For example, a tile with value 1 is owned by the player with playerIndex = 1. 49 | var TILE_EMPTY = -1; 50 | var TILE_MOUNTAIN = -2; 51 | var TILE_FOG = -3; 52 | var TILE_FOG_OBSTACLE = -4; // Cities and Mountains show up as Obstacles in the fog of war. 53 | 54 | // Game data. 55 | var playerIndex; 56 | var generals; // The indicies of generals we have vision of. 57 | var cities = []; // The indicies of cities we have vision of. 58 | var map = []; 59 | 60 | /* Returns a new array created by patching the diff into the old array. 61 | * The diff formatted with alternating matching and mismatching segments: 62 | * 63 | * 64 | * 65 | * ... repeated until the end of diff. 66 | * Example 1: patching a diff of [1, 1, 3] onto [0, 0] yields [0, 3]. 67 | * Example 2: patching a diff of [0, 1, 2, 1] onto [0, 0] yields [2, 0]. 68 | */ 69 | function patch(old, diff) { 70 | var out = []; 71 | var i = 0; 72 | while (i < diff.length) { 73 | if (diff[i]) { // matching 74 | Array.prototype.push.apply(out, old.slice(out.length, out.length + diff[i])); 75 | } 76 | i++; 77 | if (i < diff.length && diff[i]) { // mismatching 78 | Array.prototype.push.apply(out, diff.slice(i + 1, i + 1 + diff[i])); 79 | i += diff[i]; 80 | } 81 | i++; 82 | } 83 | return out; 84 | } 85 | 86 | socket.on('game_start', function(data) { 87 | // Get ready to start playing the game. 88 | playerIndex = data.playerIndex; 89 | var replay_url = 'http://bot.generals.io/replays/' + encodeURIComponent(data.replay_id); 90 | console.log('Game starting! The replay will be available after the game at ' + replay_url); 91 | }); 92 | 93 | socket.on('game_update', function(data) { 94 | // Patch the city and map diffs into our local variables. 95 | cities = patch(cities, data.cities_diff); 96 | map = patch(map, data.map_diff); 97 | generals = data.generals; 98 | 99 | // The first two terms in |map| are the dimensions. 100 | var width = map[0]; 101 | var height = map[1]; 102 | var size = width * height; 103 | 104 | // The next |size| terms are army values. 105 | // armies[0] is the top-left corner of the map. 106 | var armies = map.slice(2, size + 2); 107 | 108 | // The last |size| terms are terrain values. 109 | // terrain[0] is the top-left corner of the map. 110 | var terrain = map.slice(size + 2, size + 2 + size); 111 | 112 | // Make a random move. 113 | while (true) { 114 | // Pick a random tile. 115 | var index = Math.floor(Math.random() * size); 116 | 117 | // If we own this tile, make a random move starting from it. 118 | if (terrain[index] === playerIndex) { 119 | var row = Math.floor(index / width); 120 | var col = index % width; 121 | var endIndex = index; 122 | 123 | var rand = Math.random(); 124 | if (rand < 0.25 && col > 0) { // left 125 | endIndex--; 126 | } else if (rand < 0.5 && col < width - 1) { // right 127 | endIndex++; 128 | } else if (rand < 0.75 && row < height - 1) { // down 129 | endIndex += width; 130 | } else if (row > 0) { //up 131 | endIndex -= width; 132 | } else { 133 | continue; 134 | } 135 | 136 | // Would we be attacking a city? Don't attack cities. 137 | if (cities.indexOf(endIndex) >= 0) { 138 | continue; 139 | } 140 | 141 | socket.emit('attack', index, endIndex); 142 | break; 143 | } 144 | } 145 | }); 146 | 147 | function leaveGame() { 148 | socket.emit('leave_game'); 149 | } 150 | 151 | socket.on('game_lost', leaveGame); 152 | 153 | socket.on('game_won', leaveGame); --------------------------------------------------------------------------------