Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/jladuval/clashjs
Browse files Browse the repository at this point in the history
  • Loading branch information
neftaly committed Sep 24, 2015
2 parents 0d8e736 + 3dc0306 commit 43a44b4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 73 deletions.
8 changes: 4 additions & 4 deletions src/players/george.js
Expand Up @@ -6,7 +6,7 @@ function attackEnemy(playerState, enemiesStates){

var myPosition = playerState.position;
for(var i=0;i<enemiesStates.length;i++){
if(enemiesStates[i].ammo){
if(enemiesStates[i].ammo && enemiesStates[i].alive){
var directionToEnemy = utils.getDirection(
playerState.position,
enemiesStates[i].position
Expand All @@ -24,6 +24,7 @@ var george = {
},
ai: (playerState, enemiesStates, gameEnvironment) => {
var directionToAmmo;
var myPosition = playerState.position;

if (utils.canKill(playerState, enemiesStates) && playerState.ammo) {
return 'shoot';
Expand All @@ -33,10 +34,9 @@ var george = {
if(direction)
return direction;

if(playerState.ammo>=2)
return utils.getDistance(myPosition, enemiesStates[0].position);
if(playerState.ammo)
return utils.getDirection(myPosition, enemiesStates[0].position);

var myPosition = playerState.position;

if (gameEnvironment.ammoPosition.length) {
var distance = 10000;
Expand Down
5 changes: 3 additions & 2 deletions src/players/jgroom33.js
Expand Up @@ -8,8 +8,9 @@ var jgroom33 = {
ai: (playerState, enemiesState, gameEnvironment) => {
var directionToAmmo;

if (Math.random() > 0.9) return 'shoot';

if (utils.canKill(playerState, enemiesStates) && playerState.ammo) {
return 'shoot';
}
if (gameEnvironment.ammoPosition.length) {
directionToAmmo = utils.getDirection(
playerState.position,
Expand Down
88 changes: 21 additions & 67 deletions src/players/rogue_shadow.js
@@ -1,12 +1,5 @@
import utils from '../lib/utils.js';

const goals = [
[0, 0],
[12, 0],
[12, 12],
[0, 12],
];

function closest (from, locations) {
let closest = locations.reduce((result, to) => {
let distance = utils.getDistance(from, to);
Expand All @@ -23,6 +16,16 @@ function closest (from, locations) {
return closest.position;
}

function moveTo (me, position) {
let directionTo = utils.fastGetDirection(me.position, position);

if (directionTo !== me.direction) {
return directionTo;
}

return 'move';
}

function equal (a, b) {
return a[0] === b[0] && a[1] === b[1];
}
Expand All @@ -34,73 +37,24 @@ export default {
style: 1
},

ai: (me, baddies, game) => {
if (utils.canKill(me, baddies) && me.ammo) {
ai: (me, ships, game) => {
if (utils.canKill(me, ships) && me.ammo) {
return 'shoot';
}

if (me.ammo === 0 && game.ammoPosition.length) {
let directionToAmmo = utils.fastGetDirection(
me.position,
closest(me.position, game.ammoPosition)
);
return moveTo(me, closest(me.position, game.ammoPosition));
}

if (directionToAmmo !== me.direction) {
return directionToAmmo;
}
let safe = ships.filter((ship) => {
return ship.ammo === 0;
});

return 'move';
if (safe.length === 0) {
safe = ships;
}

var corner = closest(me.position, goals)

if (equal(me.position, corner)) {

if (equal(corner, [0, 0])) {
if (me.direction === 'south') {
return 'east';
}
if (me.direction === 'east') {
return 'south';
}
return 'south';
}
if (equal(corner, [0, 12])) {
if (me.direction === 'north') {
return 'west';
}
if (me.direction === 'west') {
return 'north';
}
return 'north';
}
if (equal(corner, [12, 12])) {
if (me.direction === 'north') {
return 'east';
}
if (me.direction === 'east') {
return 'north';
}
return 'east';
}
if (equal(corner, [12, 0])) {
if (me.direction === 'south') {
return 'west';
}
if (me.direction === 'west') {
return 'south';
}
return 'west';
}

} else {
var direction = utils.fastGetDirection(me.position, corner);

if (me.direction !== direction) {
return direction;
}

return 'move';
}
let target = closest(me.position, safe.map((ship) => ship.position));
return moveTo(me, target);
}
}

0 comments on commit 43a44b4

Please sign in to comment.