Skip to content

Instantly share code, notes, and snippets.

@allaryin
Created August 13, 2015 18:26
Show Gist options
  • Save allaryin/063aadf5274d266b0508 to your computer and use it in GitHub Desktop.
Save allaryin/063aadf5274d266b0508 to your computer and use it in GitHub Desktop.
minecraft recipe cost calculator
#!/usr/bin/env ruby
require 'pp'
RECIPES = {
:excavator => [{
:steel_block => 21,
:steel_scaffolding => 23,
:light_engineering_block => 9,
:heavy_engineering_block => 13
}, 1],
:steel_block => [{
:steel => 9
}, 1],
:steel_scaffolding => [{
:steel => 3,
:steel_fence => 6
}, 6],
:steel_fence => [{
:steel => 6
}, 16],
:light_engineering_block => [{
:iron => 4,
:copper => 3,
:iron_mechanical_component => 2
}, 2],
:iron_mechanical_component => [{
:iron => 4,
:copper => 1
}, 1],
:heavy_engineering_block => [{
:steel => 4,
:piston => 2,
:steel_mechanical_component => 2,
:electrum => 1
}, 2],
:piston => [{
:cobble => 4,
:wood => 3,
:iron => 1,
:redstone => 1
}, 1],
:steel_mechanical_component => [{
:steel => 4,
:copper => 1
}, 1],
:electrum => [{
:silver => 1,
:gold => 1
}, 1],
:steel => [{
:iron => 1,
:coke => 1
}, 1]
}
def cost(recipe, amount = 1)
return nil unless RECIPES.has_key? recipe
total = Hash.new
multiplier = RECIPES[recipe][1]
RECIPES[recipe][0].each do |ingredient, quantity|
ingredient_cost = cost(ingredient, quantity)
if ingredient_cost.nil?
amt = (1.0 * amount / multiplier).ceil * quantity
if total.has_key? ingredient
total[ingredient] += amt
else
total[ingredient] = amt
end
elsif ingredient_cost.is_a? Hash
ingredient_cost.each do |sub_ingredient, sub_quantity|
sub_amt = (1.0 * amount / multiplier).ceil * sub_quantity
if total.has_key? sub_ingredient
total[sub_ingredient] += sub_amt
else
total[sub_ingredient] = sub_amt
end
end
else
puts "huh?"
end
end
return total
end
def print_cost(recipe, amount = 1)
puts "** #{recipe} x #{amount} **"
pp cost(recipe,amount)
end
recipe = ARGV[0]
amount = ARGV[1]
if recipe.nil?
puts "Syntax: cost.rb <recipe> [<amount>]"
elsif recipe == "list"
puts "Known recipes:"
RECIPES.sort.each do |k,v|
puts " #{k}"
end
else
if amount.nil?
amount = 1
else
amount = amount.to_i
end
print_cost recipe.to_sym, amount
end
alauritzen@alauritzen-mpb:~$ ./cost.rb list
Known recipes:
electrum
excavator
heavy_engineering_block
iron_mechanical_component
light_engineering_block
piston
steel
steel_block
steel_fence
steel_mechanical_component
steel_scaffolding
alauritzen@alauritzen-mpb:~$ ./cost.rb steel
** steel x 1 **
{:iron=>1, :coke=>1}
alauritzen@alauritzen-mpb:~$ ./cost.rb steel_block
** steel_block x 1 **
{:iron=>9, :coke=>9}
alauritzen@alauritzen-mpb:~$ ./cost.rb piston 2
** piston x 2 **
{:cobble=>8, :wood=>6, :iron=>2, :redstone=>2}
alauritzen@alauritzen-mpb:~$ ./cost.rb excavator
** excavator x 1 **
{:iron=>383,
:coke=>309,
:copper=>39,
:cobble=>56,
:wood=>42,
:redstone=>14,
:silver=>7,
:gold=>7}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment