From e44f57fe6aed9a93edd31623026e4b6ccddc2eda Mon Sep 17 00:00:00 2001 From: MM Date: Mon, 5 Nov 2018 10:07:59 -0500 Subject: [PATCH 1/2] test --- acsg.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/acsg.js b/acsg.js index d5dc4cc..7a9389f 100755 --- a/acsg.js +++ b/acsg.js @@ -12,7 +12,7 @@ const uuidv4 = require('uuid/v4') function ACSG (g) { if (!(this instanceof ACSG)) return new ACSG(g) var self = this - +// // Check if this is a new game or a replay. if (g.id) { // A replay. this.UUID = g.id From 6edc1b480a33181d1a7dddbe914cfb2941c503d9 Mon Sep 17 00:00:00 2001 From: MM Date: Mon, 5 Nov 2018 10:53:16 -0500 Subject: [PATCH 2/2] flocking-bots basic design flocking bot basic design in js --- acsg.js | 246 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- demo.js | 15 ++-- 2 files changed, 246 insertions(+), 15 deletions(-) diff --git a/acsg.js b/acsg.js index 7a9389f..3f3f66c 100755 --- a/acsg.js +++ b/acsg.js @@ -8,11 +8,89 @@ var gaussian = require('gaussian') var Rands = require('rands') var seedrandom = require('seedrandom') const uuidv4 = require('uuid/v4') +NBots=16 +var Mat= new Array(NBots) +for (var i=0;i 0; i--) { + j = Math.floor(Math.random() * (i + 1)); + x = a[i]; + a[i] = a[j]; + a[j] = x; + } + return a; +} function ACSG (g) { if (!(this instanceof ACSG)) return new ACSG(g) var self = this -// + // Check if this is a new game or a replay. if (g.id) { // A replay. this.UUID = g.id @@ -32,14 +110,23 @@ function ACSG (g) { opts.BOT_MOTION_RATE = opts.BOT_MOTION_RATE || 8 opts.BLOCK_SIZE = opts.BLOCK_SIZE || 15 opts.BLOCK_PADDING = opts.BLOCK_PADDING || 1 + opts.MAT_cond=opts.MAT_COND || 0 opts.SEED = opts.SEED || performance.now() opts.BOT_STRATEGY = opts.BOT_STRATEGY || 'random' + opts.COLOR_COND=opts.COLOR_COND||0 this.UUID = uuidv4() replay = false actions = [] actionTimestamps = [] } + if (opts.MAT_cond==0){ + Mat=rewire(Mat1) + } + if (opts.MAT_cond==1){ + Mat=rewire(Mat2) + } + // Seed event RNG. Math.seedrandom(opts.SEED) var r = new Rands() @@ -55,11 +142,19 @@ function ACSG (g) { } GREEN = [0.51, 0.95, 0.61] + if (opts.COLOR_COND==0){ + teamColors = [ + [0.50, 0.86, 1.00], // Blue + [1.00, 0.86, 0.50] // Yellow + ] + } + if (opts.COLOR_COND==1){ + teamColors = [ + [1.00, 0.86, 0.50], // Yellow + [0.50, 0.86, 1.00] // Blue + ] +} - teamColors = [ - [0.50, 0.86, 1.00], // Blue - [1.00, 0.86, 0.50] // Yellow - ] food = [] players = [] @@ -87,7 +182,7 @@ function ACSG (g) { this.serialize = function () { return JSON.stringify({ 'id': this.UUID, - 'data': { + 'daopts.MAT_condta': { 'actions': actions, 'timestamps': actionTimestamps }, @@ -136,7 +231,14 @@ function ACSG (g) { config = config || {} this.id = config.id || players.length this.position = config.position || randomPosition() - this.teamIdx = Math.floor(Math.random() * teamColors.length) + this.direction ='' + if (this.id<8){ + this.teamIdx =0 + }else{ + this.teamIdx=1 + } + + //this.teamIdx = Math.floor(Math.random() * teamColors.length) this.color = config.color || teamColors[this.teamIdx] this.score = config.score || 0 this.bot = config.bot || false @@ -183,6 +285,7 @@ function ACSG (g) { } if (!hasPlayer(newPosition)) { this.position = newPosition + this.direction=direction } } @@ -205,6 +308,8 @@ function ACSG (g) { Bot = function (config) { Player.call(this, config) this.bot = true + this.target=randomPosition() +// console.log('target: '+this.target) } Bot.prototype = Object.create(Player.prototype) @@ -213,9 +318,22 @@ function ACSG (g) { if (opts.BOT_STRATEGY == 'random') { direction = this.strategy.random() } + if (opts.BOT_STRATEGY == 'flockingBot') { + if (this.position[0]==this.target[0] && this.position[1]==this.target[1]){ + // console.log('target reached! new target is set') + this.target=randomPosition() + // console.log('target: '+this.target) + + } + direction = this.strategy.flockingBot(this.id,this.position,this.target) + } + botActions.push(direction) this.history.actions.push(direction) + this.direction=direction Player.prototype.move.call(this, direction) + //console.log('target: '+this.target) + // console.log('position: '+this.position) } Bot.prototype.strategy = {} @@ -225,6 +343,108 @@ function ACSG (g) { return dirs[Math.floor(Math.random() * dirs.length)] } + Bot.prototype.strategy.flockingBot = function (thisID,position,target) { + var hor=10 + var RR=1 + + // console.log('position ' + position) + // console.log('target ' + target) + dir='' + + var food_found=0 + var player_found=0 + var food_positions=[] + var players_idx=[] + + for (var i = 0; i < food.length; i++) { + food_positions.push(food[i].position) + } + shuffle(food_positions) + + for (var i = 0; i < food.length; i++) { + console.log() + if (Math.abs(food_positions[i][0]-position[0])<=hor/2 && Math.abs(food_positions[i][1]-position[1])<=hor/2){ + // console.log('food found!') + + if (position[0]food_positions[i][0]){ + dir='up' + } + if (position[1]food_positions[i][1]){ + dir='left' + } + food_found=1 + break + + } + + } + + if (food_found==0){ + + for (var i = 0; i < players.length; i++) { + players_idx.push(i) + } + + shuffle(players_idx) + + // console.log('player pos: '+players[0].position+ ' self pos: '+position) + + for (var i = 0; i < players.length; i++) { +// console.log(players[players_idx[i]].position) + if (!(players[players_idx[i]].position[0]==position[0] && players[players_idx[i]].position[1]==position[1]) ){ +// console.log('player pos: '+players[players_idx[i]].position+ ' self pos: '+position) + + + + if (Math.abs(players[players_idx[i]].position[0]-position[0])=opts.COLUMNS-1 && players[players_idx[i]].direction=='down') && + !(position[1]<=0 && players[players_idx[i]].direction=='left') && !(position[1]>=opts.ROWS-1 && players[players_idx[i]].direction=='right') + ){ + if (players[players_idx[i]].direction=='up' || players[players_idx[i]].direction=='down' || players[players_idx[i]].direction=='right' ||players[players_idx[i]].direction=='left'){ + // console.log('move with player dir') + if (Mat[thisID][players_idx[i]]==1){ + dir=players[players_idx[i]].direction + player_found=1 + break + } + } + } + + } + } + + } + } + + if (food_found==0 && player_found==0){ + + // console.log('follow target') + if (position[0]target[0]){ + dir='up' + } + if (position[1]target[1]){ + dir='left' + } + } + + return dir + } + // Create the human. if (opts.INCLUDE_HUMAN) { players.push(new Player()) @@ -268,16 +488,26 @@ function ACSG (g) { botActions = [] whichBotMoves = [] t = 0 + var idx =0 humanOffset = opts.INCLUDE_HUMAN ? 1 : 0 + // console.log('wait time '+ opts.BOT_MOTION_RATE * this._NUM_BOTS) while (true) { waitTime = r.exponential(opts.BOT_MOTION_RATE * this._NUM_BOTS) + // waitTime=.1/this._NUM_BOTS if (t + waitTime > opts.DURATION) { break } t += waitTime botActionTimestamps.push(t) idx = Math.floor(Math.random() * this._NUM_BOTS) + humanOffset - whichBotMoves.push(idx) + whichBotMoves.push(idx) +// whichBotMoves.push(idx+humanOffset) + + + // idx+=1 + // if (idx>=this._NUM_BOTS){ + // idx=0 + // } } lastBotActionIdx = -1 lastHumanActionIdx = -1 diff --git a/demo.js b/demo.js index 95be57a..a5c48a8 100644 --- a/demo.js +++ b/demo.js @@ -1,17 +1,18 @@ var ACSG = require('./acsg') - game = ACSG({ 'config': { - NUM_PLAYERS: 9, - DURATION: 6, + NUM_PLAYERS: 16, + DURATION: 120, INCLUDE_HUMAN: true, - BOT_STRATEGY: 'random', - ROWS: 25, - COLUMNS: 25, + BOT_STRATEGY: 'flockingBot', + ROWS: 45, + COLUMNS: 45, NUM_FOOD: 8, VISIBILITY: 50, - BOT_MOTION_RATE: 4, + BOT_MOTION_RATE: 8, BLOCK_SIZE: 12, BLOCK_PADDING: 1, + MAT_COND:1, + COLOR_COND:1, SEED: '19145822646' }})