Created
December 5, 2016 07:03
-
-
Save stasikos/ddc0d4c0a46a2ce21a2432fb94fd9593 to your computer and use it in GitHub Desktop.
Recipe-based calculator for craft planning in minecraft
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
#!/usr/bin/perl -w | |
use Data::Dumper; | |
my $cItem = $ARGV[0]; | |
my $cCount = $ARGV[1]; | |
open(DATA, 'calc.dat'); | |
my %recipes; | |
while ($string = <DATA>) { | |
chomp $string; | |
$string =~ /(.*) = (.*)/; | |
@parts = split(/ \+ /, $1); | |
$itemString = $2; | |
$itemString =~ /\((.*) x "(.*)"\)/; | |
$itemName = $2; | |
$itemCount = $1; | |
$recipes{$itemName}{"recipe"} = []; | |
$recipes{$itemName}{"count"} = $itemCount; | |
foreach $part (@parts) { | |
chomp $part; | |
$part =~ /\((.*) x "(.*)"\)/; | |
$compName = $2; | |
$compCount = $1; | |
push ($recipes{$itemName}{"recipe"}, {"item" => $compName, "count" => $compCount}); | |
} | |
} | |
close(DATA); | |
print "----------------------------------------------------------\n"; | |
foreach $item (keys %recipes) { | |
print $recipes{$item}{"count"}. " x ".$item." = \n"; | |
@recipe = @{$recipes{$item}{"recipe"}}; | |
# print Dumper @recipe; | |
foreach $comp (@recipe) { | |
%c = %{$comp}; | |
print "\t\t".$c{"item"}." x ". $c{"count"}."\n"; | |
} | |
} | |
print "================================= Calc result =================================\n"; | |
my %totals; | |
sub calculate($$$) { | |
local ($calcCount, $calcItem, $level); | |
($calcCount, $calcItem, $level) = ($_[0], $_[1], $_[2]); | |
#print "Calculate called for $calcItem\n"; | |
if (exists $recipes{$calcItem}) { | |
# print "calcCount = $calcCount, modulo is ".($calcCount % $recipes{$calcItem}{"count"}). " recipe output = ".$recipes{$calcItem}{"count"} . " "; | |
if (($calcCount % $recipes{$calcItem}{"count"}) > 0) { | |
$calcCount += $recipes{$calcItem}{"count"} - ($calcCount % $recipes{$calcItem}{"count"}); | |
} | |
local $craftCount = $calcCount / $recipes{$calcItem}{"count"}; | |
# print "CraftCount = $craftCount when calcCount = $calcCount\n"; | |
print "-"x$level."+".$calcCount . " x " . $calcItem . " = \n"; | |
local @recipe = @{$recipes{$calcItem}{"recipe"}}; | |
local $comp; | |
foreach $comp(@recipe) { | |
local %c = %{$comp}; | |
if (!exists $recipes{$c{"item"}}) { | |
$a = " [!]"; | |
} else { | |
$a = ""; | |
} | |
print " "x$level."|->".$c{"item"}." x ".$c{"count"} * $craftCount."$a\n"; | |
} | |
foreach $comp(@recipe) { | |
local %c = %{$comp}; | |
calculate($c{"count"} * $craftCount, $c{"item"}, $level + 1); | |
} | |
} else { | |
$totals{$calcItem} += $calcCount; | |
} | |
} | |
calculate($cCount, $cItem, 0); | |
print "\nBasic resources needed: for crafting $cCount of $cItem: \n"; | |
foreach $item (keys %totals) { | |
print "$item x ".$totals{$item}."\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment