Skip to content

Instantly share code, notes, and snippets.

@lanternwick
Created October 21, 2010 04:14
Show Gist options
  • Save lanternwick/637930 to your computer and use it in GitHub Desktop.
Save lanternwick/637930 to your computer and use it in GitHub Desktop.
Pathfinder Point Buy Optimization!
# racial bonuses and penalties to be applied, array of
# [str, dex, con, int, wis, cha], in that order
racial_bonuses = [0, 2, -2, 2, 0, 0]
# search criteria, array of arrays, [[stat, min value], ...]
search_criteria = [[:str, 15], [:dex, 14], [:con, 14], [:int, 10], [:wis, 14]]
# the number of points available for point buy. Used exactly
points_available = 20
# determine the cost of a set
def set_cost set
costs = {
10 => 0,
11 => 1,
12 => 2,
13 => 3,
14 => 5,
15 => 7,
16 => 10,
17 => 13,
18 => 17
}
set.inject 0 do |cost, i|
cost + costs[i]
end
end
# create an enumerator to generate stat sets with the appropriate cost
def sets_with_cost cost
Enumerator.new do |y|
base = (10..18).to_a
base.each do |str|
base.each do |dex|
base.each do |con|
base.each do |int|
base.each do |wis|
base.each do |cha|
set = [str, dex, con, int, wis, cha]
if set_cost(set) == cost
y.yield set
end
end
end
end
end
end
end
end
end
# get our enumerator
sets = sets_with_cost points_available
# apply racial bonuses
bonuses_applied = sets.collect do |set|
set.zip(racial_bonuses).map do |a|
a.inject { |memo, i| memo + i }
end
end
puts "Applied bonuses to #{bonuses_applied.length} sets"
#convert updated sets into hashes on stat name
hashes = bonuses_applied.collect do |set|
{
:str => set[0],
:dex => set[1],
:con => set[2],
:int => set[3],
:wis => set[4],
:cha => set[5]
}
end
# convert search criteria into a hash (easier to read)
tagged_criteria = search_criteria.collect do |stat, value|
{ :stat => stat, :value => value }
end
# select sets based on criteria
matching_hashes = hashes.select do |hash|
tagged_criteria.inject true do |memo, c|
memo && (hash[c[:stat]] >= c[:value])
end
end
puts "Found #{matching_hashes.length} sets matching criteria " +
"#{search_criteria.collect {|stat, val| "#{stat} > #{val-1}" }.join ", "}."
# print out the set of results
matching_hashes.each do |h|
puts h.inspect
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment