Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature(HERO) Add randomly selected standard AI to bots #26

Merged
merged 3 commits into from Oct 13, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
94 changes: 92 additions & 2 deletions public/game_classes/Hero.js
Expand Up @@ -49,7 +49,7 @@ Hero.prototype.takeDamage = function(amount) {
this.health -= amount;
if (this.health <= 0) {
this.dead = true;

// Only return the damage actually needed
// to kill this hero
return amount + this.health;
Expand Down Expand Up @@ -116,4 +116,94 @@ Hero.prototype.getCode = function() {
return 'H' + idStr;
};

module.exports = Hero;
Hero.prototype.move = function(gameData, helpers) {
// Select a random brain for this hero
var brains = Object.keys(Hero.brains);
this.move = Hero.brains[brains[ brains.length * Math.random() << 0 ]];
return this.move(gameData, helpers);
};

Hero.brains = {
BlindMan: function(gameData, helpers) {
var myHero = gameData.activeHero;
var choices = ['North', 'South', 'East', 'West'];
return choices[Math.floor(Math.random()*4)];
},
Priest: function(gameData, helpers) {
var myHero = gameData.activeHero;
if (myHero.health < 60) {
return helpers.findNearestHealthWell(gameData);
} else {
return helpers.findNearestTeamMember(gameData);
}
},
UnwiseAssassin: function(gameData, helpers) {
var myHero = gameData.activeHero;
if (myHero.health < 30) {
return helpers.findNearestHealthWell(gameData);
} else {
return helpers.findNearestEnemy(gameData);
}
},
CarefulAssassin: function(gameData, helpers) {
var myHero = gameData.activeHero;
if (myHero.health < 50) {
return helpers.findNearestHealthWell(gameData);
} else {
return helpers.findNearestWeakerEnemy(gameData);
}
},
SafeDiamondMiner: function(gameData, helpers) {
var myHero = gameData.activeHero;

//Get stats on the nearest health well
var healthWellStats = helpers.findNearestObjectDirectionAndDistance(gameData.board, myHero, function(boardTile) {
if (boardTile.type === 'HealthWell') {
return true;
}
});
var distanceToHealthWell = healthWellStats.distance;
var directionToHealthWell = healthWellStats.direction;


if (myHero.health < 40) {
//Heal no matter what if low health
return directionToHealthWell;
} else if (myHero.health < 100 && distanceToHealthWell === 1) {
//Heal if you aren't full health and are close to a health well already
return directionToHealthWell;
} else {
//If healthy, go capture a diamond mine!
return helpers.findNearestNonTeamDiamondMine(gameData);
}
},
SelfishDiamondMiner: function(gameData, helpers) {
var myHero = gameData.activeHero;

//Get stats on the nearest health well
var healthWellStats = helpers.findNearestObjectDirectionAndDistance(gameData.board, myHero, function(boardTile) {
if (boardTile.type === 'HealthWell') {
return true;
}
});

var distanceToHealthWell = healthWellStats.distance;
var directionToHealthWell = healthWellStats.direction;

if (myHero.health < 40) {
Heal no matter what if low health
return directionToHealthWell;
} else if (myHero.health < 100 && distanceToHealthWell === 1) {
//Heal if you aren't full health and are close to a health well already
return directionToHealthWell;
} else {
//If healthy, go capture a diamond mine!
return helpers.findNearestUnownedDiamondMine(gameData);
}
},
Coward: function(gameData, helpers) {
return helpers.findNearestHealthWell(gameData);
}
};

module.exports = Hero;
13 changes: 7 additions & 6 deletions public/js/game/Game.js
Expand Up @@ -68,16 +68,17 @@ var Game = Backbone.Model.extend({
while (gameData.ended === false || turnKeeper < 1010) {
if (gameData.activeHero.id === 0) {
var usersFunction = new Function(move);
var usersMove = (usersFunction(gameData, helpers));
var usersMove = usersFunction(gameData, helpers);
handleHeroTurn.call(gameData, usersMove);
this.clientSideGame[turnKeeper] = JSON.parse(JSON.stringify(gameData));
console.log('----------');
console.log('Turn number: ', (gameData.turn - 1));
console.log('Your hero ' + gameData.moveMessage.slice(7));
console.log('**********');
} else {
var choices = ['North', 'South', 'East', 'West'];
handleHeroTurn.call(gameData, (choices[Math.floor(Math.random()*4)]));
var botsFunction = gameData.activeHero.move;
var botsMove = botsFunction(gameData, helpers);
handleHeroTurn.call(gameData, botsMove);
this.clientSideGame[turnKeeper] = JSON.parse(JSON.stringify(gameData));
}
var max = turnKeeper;
Expand All @@ -92,7 +93,7 @@ var Game = Backbone.Model.extend({
initialize: function() {

},

gameSet: function(gameData) {
this.set('turn', gameData.turn);
this.set('maxTurn', gameData.maxTurn);
Expand Down Expand Up @@ -130,7 +131,7 @@ var Game = Backbone.Model.extend({
teamBlue.add(hero);
});


_.each(_.flatten(gameData.board.tiles), function(tileObject, key, list) {
//The id from our game model was overwriting
tileObject.battleId = tileObject.id || tileObject.battleId;
Expand All @@ -148,4 +149,4 @@ var Game = Backbone.Model.extend({
updateTurn: function(turn) {
this.gameSet(this.clientSideGame[turn]);
}
});
});