Skip to content

Instantly share code, notes, and snippets.

@smurphy8
Created June 23, 2014 15:03
Show Gist options
  • Save smurphy8/1ca4e64019d363234ffe to your computer and use it in GitHub Desktop.
Save smurphy8/1ca4e64019d363234ffe to your computer and use it in GitHub Desktop.
munchkin battle code
var type = 'munchkin';
var go = true;
if (this.gold > 100 || this.flush) {
if (this.built.length % 6 === 1) {
type = 'thrower';
}
else if (this.built.length % 6 === 5 ){
type = 'shaman';
}
if (this.gold > 50) {
this.flush = true;
}
else {
this.flush = false;
}
this.build(type);
}
else {
this.flush = false;
}
var enemy = this.getNearestEnemy();
var enemies = this.getEnemies();
var friends = this.getFriends();
// ===============================
// Calculatino seciton
// -----------
this.makeAbsProps = function (d,s,c) {
var unit = {};
unit.dps = d;
unit.speed = s;
unit.cost = c;
return unit;
};
this.soldier = this.makeAbsProps (5 , 10 , 14 );
this.archer = this.makeAbsProps (17 , 12 , 20 );
this.artillery = this.makeAbsProps (10 , 8 , 30 );
this.arrowThrower = this.makeAbsProps (25 , 0 , 1 );
this.munchkin = this.makeAbsProps (4 , 12 , 14 );
this.thrower = this.makeAbsProps (18 , 12 , 20 );
this.shaman = this.makeAbsProps (7 , 10 , 30 );
this.beamTower = this.makeAbsProps (18 , 0 , 1 );
this.burl = this.makeAbsProps (100,10,0);
this.getUnitProps = function (unit) {
var t = unit.type;
if (t == 'munchkin') {
return this.munchkin;
}
else if (t == 'thrower') {
return this.spear;
}
else if (t == 'shaman') {
return this.shaman;
}
else if (t == 'beam-tower') {
return this.beamTower;
}
else if (t == 'soldier') {
return this.soldier;
}
else if (t == 'archer') {
return this.archer;
}
else if (t == 'artillery') {
return this.artillery;
}
else if (t == 'arrow-tower') {
return this.arrowThrower;
}
else if (t == 'burl') {
return this.burl;
}
else {
return this.makeAbsProps(0,0,0) ;
}
};
// abs props are ones that can't be written
// and aren't directly available from model
// dps speed range gold
// totalUnits creates a fake unit with the combined health and damage properties of all the units in an array
this.totalUnits = function (units) {
var punit = {};
punit.health = 0;
punit.dps = 0;
for (var i = 0; i < units.length; ++i ) {
var tunit = this.getUnitProps(units[i]);
if (tunit) {
punit.health = punit.health + units[i].health;
punit.dps = punit.dps + tunit.dps;
}
}
return punit;
};
// onVsOneFunction should determine who would win in a one on one fight between two units.
this.iWin = function (myUnit, targetUnit ) {
var myTurnCount = myUnit.health/targetUnit.dps;
var targetTurnCount = targetUnit.health/myUnit.dps;
return myTurnCount > targetTurnCount;
};
this.calculateAttack = function (nearEnemy,es,fs) {
var fUnit = null;
var eUnit = null;
var iwin = null;
if (fs && es) {
fUnit = this.totalUnits(fs);
eUnit = this.totalUnits(es);
iwin = this.iWin(fUnit,eUnit);
}
else if (es) {
fUnit = this.getUnitProps(this);
eUnit = this.totalUnits(es);
iwin = this.iWin(fUnit,eUnit);
}
if(nearEnemy && iwin ) {
this.attack(nearEnemy);
}
else if (nearEnemy.type === 'artillery' && nearEnemy.action !== 'die') {
this.attack(nearEnemy);
}
else {
if (this.buildIndex %2 == 1) {
this.move({x:67,y:43});
}
else {
this.move({x:43,y:67});
}
}
};
if(enemy&&enemies&&friends) {
this.calculateAttack(enemy,enemies,friends);
}
else if (friends) {
this.move({x:0,y:0});
}
else {
this.say("Hold tight");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment