From eaafd5febba07495f0d92245dd3b39d5de9a6ddd Mon Sep 17 00:00:00 2001 From: Vishwas_N_S Date: Mon, 14 Jan 2019 20:26:06 +0530 Subject: [PATCH] Inital bots --- JS/robot.js | 60 +++++++++++++++++++++++++++ Python/robot.py | 68 +++++++++++++++++++++++++++++++ README.md | 3 +- specs.txt | 106 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 236 insertions(+), 1 deletion(-) create mode 100644 JS/robot.js create mode 100644 Python/robot.py create mode 100644 specs.txt diff --git a/JS/robot.js b/JS/robot.js new file mode 100644 index 0000000..b873014 --- /dev/null +++ b/JS/robot.js @@ -0,0 +1,60 @@ +import {BCAbstractRobot, SPECS} from 'battlecode'; + +var built = false; +var step = -1; + +class MyRobot extends BCAbstractRobot { + turn() { + step++; + + if (this.me.unit === SPECS.CRUSADER) { + this.log("START TURN " + step) + this.log("Crusader health: " + this.me.health) + + var visible = this.getVisibleRobots() + + // this sucks I'm sorry... + var self = this // 'this' fails to properly identify MyRobot when used inside of anonymous function below :( + + // get attackable robots + var attackable = visible.filter(function(r){ + if (! self.isVisible(r)){ + return false + } + var dist = (r.x-self.me.x)**2 + (r.y-self.me.y)**2 + if (r.team !== self.me.team + && SPECS.UNITS[SPECS.CRUSADER].ATTACK_RADIUS[0] <= dist + && dist <= SPECS.UNITS[SPECS.CRUSADER].ATTACK_RADIUS[1] ){ + return true + } + return false + }) + this.log(attackable) + + if (attackable.length>0){ + // attack first robot + var r = attackable[0] + this.log(""+r) + this.log('attacking! ' + r + ' at loc ' + (r.x - this.me.x, r.y - this.me.y)) + return this.attack(r.x - this.me.x, r.y - this.me.y) + } + + // this.log("Crusader health: " + this.me.health); + const choices = [[0,-1], [1, -1], [1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1]]; + const choice = choices[Math.floor(Math.random()*choices.length)] + return this.move(...choice); + } + + else if (this.me.unit === SPECS.CASTLE) { + if (step % 10 === 0) { + this.log("Building a crusader at " + (this.me.x+1) + ", " + (this.me.y+1)); + return this.buildUnit(SPECS.CRUSADER, 1, 1); + } else { + return // this.log("Castle health: " + this.me.health); + } + } + + } +} + +var robot = new MyRobot(); diff --git a/Python/robot.py b/Python/robot.py new file mode 100644 index 0000000..5a8d6ce --- /dev/null +++ b/Python/robot.py @@ -0,0 +1,68 @@ +from battlecode import BCAbstractRobot, SPECS +import battlecode as bc +import random + +__pragma__('iconv') +__pragma__('tconv') +__pragma__('opov') + +# don't try to use global variables!! +class MyRobot(BCAbstractRobot): + step = -1 + + def turn(self): + + if self.step == 0: + self.log(SPECS) + self.step += 1 + self.log("START TURN " + self.step) + + if self.me['unit'] == SPECS['CASTLE']: + if self.step < 10: + self.log("Building a crusader at " + str(self.me['x']+1) + ", " + str(self.me['y']+1)) + return self.build_unit(SPECS['CRUSADER'], 1, 1) + else: + self.log("Castle health: " + self.me['health']) + + elif self.me['unit'] == SPECS['CHURCH']: + pass #TODO + + elif self.me['unit'] == SPECS['PILGRIM']: + pass #TODO + + elif self.me['unit'] == SPECS['CRUSADER']: + self.log("Crusader health: " + str(self.me['health'])) + + visible = self.get_visible_robots() + + # get attackable robots + attackable = [] + for r in visible: + # x = 5 + # if not self.is_visible(r): + if 'x' not in r: #not visible. hacky. do not use at home + continue + # now all in vision range, can see x, y etc + dist = (r['x'] - self.me['x'])**2 + (r['y'] - self.me['y'])**2 + if r['team'] != self.me['team'] and SPECS['UNITS'][SPECS["CRUSADER"]]['ATTACK_RADIUS'][0] <= dist <= SPECS['UNITS'][SPECS["CRUSADER"]]['ATTACK_RADIUS'][1]: + attackable.append(r) + + if attackable: + # attack first robot + r = attackable[0] + self.log('attacking! ' + str(r) + ' at loc ' + (r['x'] - self.me['x'], r['y'] - self.me['y'])) + return self.attack(r['x'] - self.me['x'], r['y'] - self.me['y']) + + # The directions: North, NorthEast, East, SouthEast, South, SouthWest, West, NorthWest + choices = [(0,-1), (1, -1), (1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1)] + choice = random.choice(choices) + self.log('TRYING TO MOVE IN DIRECTION ' + str(choice)) + return self.move(*choice) + + elif self.me['unit'] == SPECS['PROPHET']: + pass #TODO + + elif self.me['unit'] == SPECS['PREACHER']: + pass #TODO + +robot = MyRobot() diff --git a/README.md b/README.md index 2bcc26d..6cab5ec 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ -# BC_2019 Battlecode 2019 + +To run, use `bc19run -b JS -r Python` diff --git a/specs.txt b/specs.txt new file mode 100644 index 0000000..845f8ec --- /dev/null +++ b/specs.txt @@ -0,0 +1,106 @@ +{ + 'COMMUNICATION_BITS': 16, + 'CASTLE_TALK_BITS': 8, + 'MAX_ROUNDS': 1000, + 'TRICKLE_FUEL': 25, + 'INITIAL_KARBONITE': 100, + 'INITIAL_FUEL': 500, + 'MINE_FUEL_COST': 1, + 'KARBONITE_YIELD': 2, + 'FUEL_YIELD': 10, + 'MAX_TRADE': 1024, + 'MAX_BOARD_SIZE': 64, + 'MAX_ID': 4096, + 'CASTLE': 0, + 'CHURCH': 1, + 'PILGRIM': 2, + 'CRUSADER': 3, + 'PROPHET': 4, + 'PREACHER': 5, + 'RED':0, + 'BLUE': 1, + 'CHESS_INITIAL': 100, + 'CHESS_EXTRA': 20, + 'TURN_MAX_TIME': 200, + 'MAX_MEMORY': 50000000, + 'UNITS': [ + { + 'CONSTRUCTION_KARBONITE': None, + 'CONSTRUCTION_FUEL': None, + 'KARBONITE_CAPACITY': None, + 'FUEL_CAPACITY': None, + 'SPEED': 0, + 'FUEL_PER_MOVE': None, + 'STARTING_HP': 100, + 'VISION_RADIUS': 100, + 'ATTACK_DAMAGE': None, + 'ATTACK_RADIUS': None, + 'ATTACK_FUEL_COST': None, + 'DAMAGE_SPREAD': None}, + { + 'CONSTRUCTION_KARBONITE': 50, + 'CONSTRUCTION_FUEL': 200, + 'KARBONITE_CAPACITY': None, + 'FUEL_CAPACITY': None, + 'SPEED': 0, + 'FUEL_PER_MOVE': None, + 'STARTING_HP': 50, + 'VISION_RADIUS': 100, + 'ATTACK_DAMAGE': None, + 'ATTACK_RADIUS': None, + 'ATTACK_FUEL_COST': None, + 'DAMAGE_SPREAD': None}, + { + 'CONSTRUCTION_KARBONITE': 10, + 'CONSTRUCTION_FUEL': 50, + 'KARBONITE_CAPACITY': 20, + 'FUEL_CAPACITY': 100, + 'SPEED': 4, + 'FUEL_PER_MOVE': 1, + 'STARTING_HP': 10, + 'VISION_RADIUS':100, + 'ATTACK_DAMAGE': None, + 'ATTACK_RADIUS': None, + 'ATTACK_FUEL_COST': None, + 'DAMAGE_SPREAD': None}, + { + 'CONSTRUCTION_KARBONITE': 20, + 'CONSTRUCTION_FUEL': 50, + 'KARBONITE_CAPACITY': 20, + 'FUEL_CAPACITY': 100, + 'SPEED': 9, + 'FUEL_PER_MOVE': 1, + 'STARTING_HP': 40, + 'VISION_RADIUS': 36, + 'ATTACK_DAMAGE': 10, + 'ATTACK_RADIUS': [1, 16], + 'ATTACK_FUEL_COST': 10, + 'DAMAGE_SPREAD': 0}, + { + 'CONSTRUCTION_KARBONITE': 25, + 'CONSTRUCTION_FUEL': 50, + 'KARBONITE_CAPACITY': 20, + 'FUEL_CAPACITY': 100, + 'SPEED': 4, + 'FUEL_PER_MOVE': 2, + 'STARTING_HP': 20, + 'VISION_RADIUS': 64, + 'ATTACK_DAMAGE': 10, + 'ATTACK_RADIUS': [16, 64], + 'ATTACK_FUEL_COST': 25, + 'DAMAGE_SPREAD': 0}, + { + 'CONSTRUCTION_KARBONITE': 30, + 'CONSTRUCTION_FUEL': 50, + 'KARBONITE_CAPACITY': 20, + 'FUEL_CAPACITY': 100, + 'SPEED': 4, + 'FUEL_PER_MOVE': 3, + 'STARTING_HP': 60, + 'VISION_RADIUS': 16, + 'ATTACK_DAMAGE': 20, + 'ATTACK_RADIUS': [1, 16], + 'ATTACK_FUEL_COST': 15, + 'DAMAGE_SPREAD': 3 + }] +} \ No newline at end of file