Created
June 19, 2014 20:19
-
-
Save ryancole/97930f370e81ee7bd405 to your computer and use it in GitHub Desktop.
gold rush mode human
This file contains hidden or 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
// get all items in this frame split into regions | |
function getRegionItems (items) { | |
var result = []; | |
items.forEach(function (item) { | |
var x = Math.floor(item.pos.x / 28); | |
var y = Math.floor(item.pos.y / 28); | |
var region = (x * 4) + y; | |
if (result[region] === undefined) { | |
result[region] = [item]; | |
} else { | |
result[region].push(item); | |
} | |
}); | |
return result; | |
} | |
// get most valuable region index | |
function getValuableRegionIndex (regions) { | |
var index = 0; | |
var previous = 0; | |
regions.forEach(function (region, i) { | |
if (region !== undefined) { | |
var sum = region.reduce(function (a, b) { | |
return a.bountyGold + b.bountyGold; | |
}); | |
if (sum > previous) { | |
index = i; | |
} | |
} | |
}); | |
return index; | |
} | |
// initialize state if its empty | |
var state = this.state || { }; | |
// update the items grid | |
state.items = getRegionItems(this.getItems()); | |
// update the most valuable cell | |
state.region = getValuableRegionIndex.call(this, state.items); | |
this.say(state.region); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment