Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import: A60Ncqat; // database
- #import:vCAdipqP; // better peripherals
- #import: AAMw1hB3; // math
- // minecraft:soul_sand,minecraft:quartz,minecraft:cobblestone,0.48
- // minecraft:red_sand,minecraft:gold_nugget,minecraft:gravel,0.36
- // minecraft:sand,minecraft:clay_ball,minecraft:sand,0.25
- initRecipes:{
- $recipes$ = [];
- while (true) {
- inp = input("in,out,ident,chance");
- if (inp==""){
- return();
- };
- in,out,identifier,chance = splitText(inp,",");
- multiplier = floor(1.0/tonumber(chance) + 1);
- $recipes$.add({"in"=in,"out"=out,"multiplier"=multiplier,
- "identifier" = identifier});
- };
- };
- readCraftingJobs(limit):{
- transferred = true;
- toCraft = {};
- while(transferred){
- transferred=false;
- items = $chestIdentifierIn$.call("list")@table;
- slots = items.keys();
- for(i=0;i<slots.length;i++){
- slot = slots[i];
- item = items[slot]@table;
- identifier = item["name"];
- recipe = $recipesByIdentifier$[identifier];
- result = recipe["out"];
- if (recipe==null){
- continue;
- };
- t = $chestIdentifierIn$.call("pushItems", $chestOut$.getName(),
- slot, limit);
- if (t>0){
- transferred = true;
- };
- limit = limit - t;
- if (toCraft[result]==null){
- toCraft[result]=t;
- }else{
- toCraft[result]=toCraft[result]+t;
- };
- if (limit<=0){
- return(toCraft);
- };
- };
- };
- return(toCraft);
- };
- waitOnRedstoneLevel(level):{
- while(getRedstoneInput("back") != level){
- sleep(0.25);
- };
- return();
- };
- moveIngredientsFor(toCraft):{
- toMove = {};
- keys = toCraft.keys();
- for(i=0;i<keys.length;i++){
- key = keys[i];
- recipe=$recipesByResult$[key];
- toMove[recipe["in"]]=toCraft[key] * recipe["multiplier"];
- };
- movedAny = false;
- movedAnyThisRound=true;
- while(movedAnyThisRound){
- movedAnyThisRound = false;
- items = $chestIncIn$.call("list")@table;
- slots = items.keys();
- for(i=0;i<slots.length;i++){
- slot = slots[i];
- item = items[slot]@table;
- identifier = item["name"];
- if (toMove[identifier]!=null){
- left = toMove[identifier];
- if (left>0){
- t=$chestIncIn$.call("pushItems", $chestProcessOut$.getName(),slot, left);
- toMove[identifier] = toMove[identifier] - t;
- if (t>0){
- movedAny = true;
- movedAnyThisRound = true;
- };
- }
- };
- };
- };
- if (movedAny){
- waitOnRedstoneLevel(true);
- };
- return(movedAny);
- };
- getCraftingResult():{
- waitOnRedstoneLevel(false);
- craftingResult = {};
- items = $chestProcessIn$.call("list")@table;
- slots = items.keys();
- for(i=0;i<slots.length;i++){
- slot = slots[i];
- item = items[slot]@table;
- identifier = item["name"];
- count = $chestProcessIn$.call("pushItems", $chestOut$.getName(),
- slot, item["count"]);
- if (craftingResult[identifier]==null){
- craftingResult[identifier] = count;
- }else{
- craftingResult[identifier] = craftingResult[identifier] + count;
- };
- };
- return(craftingResult);
- };
- craft(toCraft):{
- if (moveIngredientsFor(toCraft)){
- print("crafting" ,toCraft);
- };
- craftingResult = getCraftingResult();
- results = toCraft.keys();
- allDone = true;
- for(i=0;i<results.length;i++){
- result = results[i];
- if(craftingResult[result]!=null){
- toCraft[result] = toCraft[result] - craftingResult[result];
- if (toCraft[result]<=0){
- toCraft[result] = null;
- };
- };
- };
- return(toCraft);
- };
- main: {
- $recipes$ = loadValue("recipes");
- if ($recipes$ == null){
- initRecipes();
- saveValue("recipes", $recipes$);
- };
- $recipesByIdentifier$ = {};
- $recipesByResult$ = {};
- for(i=0;i<$recipes$.length;i++){
- recipe=$recipes$[i];
- $recipesByIdentifier$[recipe["identifier"]] = recipe;
- $recipesByResult$[recipe["out"]] = recipe;
- };
- $chestProcessIn$ = wrapPeripheral("left");
- $chestProcessOut$ = wrapPeripheral("right");
- $chestIdentifierIn$ = wrapPeripheral("bottom");
- $chestIncIn$ = wrapPeripheral("front");
- $chestOut$ = wrapPeripheral("top");
- toCraftAll = {};
- crafting=false;
- while(true) {
- toCraftNew = readCraftingJobs(10*64);
- keys = toCraftNew.keys();
- if (keys.length>0){
- print("requested", toCraftNew);
- };
- for(i=0;i<keys.length;i++){
- key = keys[i];
- if(toCraftAll[key]==null){
- toCraftAll[key] = toCraftNew[key];
- }else{
- toCraftAll[key] = toCraftAll[key] + toCraftNew[key];
- };
- };
- nothingLeft = true;
- keys = toCraftAll.keys();
- for (i=0;i<keys.length;i++){
- if (toCraftAll[keys[i]]>0){
- nothingLeft = false;
- break;
- };
- };
- if (nothingLeft){
- toCraftAll = {};
- sleep(1);
- if(crafting){
- crafting=false;
- print("crafting done");
- };
- continue;
- };
- crafting=true;
- toCraftAll = craft(toCraftAll);
- };
- };
Add Comment
Please, Sign In to add comment