Last active
August 4, 2016 06:32
-
-
Save slashman/9f0f6cd461eab42bca9bab2d53fe3cef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Pick the closest portal. If advancing to it puts you in danger, pick the less dangerous portal instead. If it's the same, or it | |
* still endangers you, there's no escape portal | |
*/ | |
Enemy.prototype.getEscapePortal = function(){ | |
console.log('getEscapePortal'); | |
var portal = this.getClosestPortal(); | |
if (!portal){ | |
console.log('no closest portal'); | |
return false; | |
} | |
if (this.isDangerousPosition(portal)){ | |
console.log('closest portal at ('+portal.x+','+portal.y+') is dangerous'); | |
var lessDangerousPortal = this.getLessDangerousPortal(); | |
if (!lessDangerousPortal || lessDangerousPortal == portal){ | |
console.log('and is the same as the less dangerous portal'); | |
return false; | |
} | |
console.log('heading to less dangerous portal at ('+lessDangerousPortal.x+','+lessDangerousPortal.y+')'); | |
return lessDangerousPortal; | |
} else { | |
console.log('using closest portal at ('+portal.x+','+portal.y+')'); | |
return portal; | |
} | |
} | |
// Check if there are enemies around this position | |
Enemy.prototype.isDangerousPosition = function(position){ | |
for(var j = 0; j < this.currentRoom.monsters.length; j++){ | |
var monster = this.currentRoom.monsters[j]; | |
if (monster == this) | |
continue; | |
if (monster.isFriendly != this.isFriendly){ | |
var xDiff = Math.abs(monster.x - position.x); | |
var yDiff = Math.abs(monster.y - position.y); | |
if (xDiff <= 1 && yDiff <= 1){ | |
return true; | |
} | |
} | |
} | |
if (!this.isFriendly){ | |
var xDiff = Math.abs(this.gameController.player.x - position.x); | |
var yDiff = Math.abs(this.gameController.player.y - position.y); | |
if (xDiff <= 1 && yDiff <= 1){ | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* Pick the nearest portal | |
* Special case: If not friendly, calculate how close the player is and discard if player is closer than I | |
*/ | |
Enemy.prototype.getClosestPortal = function(){ | |
var minDistance = 9999; | |
var nearPortal = false; | |
for (var i = 0; i < this.currentRoom.portals.length; i++){ | |
var portal = this.currentRoom.portals[i]; | |
var myDistance = flatDistance(portal.x, portal.y, this.x, this.y); | |
if (!this.isFriendly){ | |
var playerDistance = flatDistance(portal.x, portal.y, this.gameController.player.x, this.gameController.player.y); | |
if (playerDistance < myDistance) | |
continue; | |
} | |
if (myDistance < minDistance){ | |
minDistance = myDistance; | |
nearPortal = portal; | |
} | |
} | |
return nearPortal; | |
} | |
function lowerDanger(a, b){ | |
return b.danger - a.danger; | |
} | |
Enemy.prototype.getLessDangerousPortal = function(){ | |
if (this.currentRoom.portals.length == 1){ | |
return this.currentRoom.portals[0]; | |
} | |
var portalsDanger = []; | |
for (var i = 0; i < this.currentRoom.portals.length; i++){ | |
var portal = this.currentRoom.portals[i]; | |
// Sum the distance of all enemies to the portal | |
var sum = 0; | |
for(var j = 0; j < this.currentRoom.monsters.length; j++){ | |
var monster = this.currentRoom.monsters[j]; | |
if (monster.isFriendly != this.isFriendly){ | |
sum += flatDistance(portal.x, portal.y, monster.x, monster.y); | |
} | |
} | |
if (!this.isFriendly){ | |
sum += flatDistance(portal.x, portal.y, this.gameController.player.x, this.gameController.player.y); | |
} | |
portalsDanger.push({portal: portal, danger: sum}); | |
} | |
portalsDanger.sort(lowerDanger); | |
return portalsDanger[0].portal; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment