melzneni

ProbabilityProcessing

Oct 8th, 2023 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. #import: AAMw1hB3; // math
  2. #import: A60Ncqat; // database
  3. #import:vCAdipqP; // better peripherals
  4.  
  5. initRecipes:{
  6. $recipes$ = [];
  7. while (true) {
  8. inp = input("in,out,ident,chance");
  9.  
  10. if (inp==""){
  11. return();
  12. };
  13. in,out,identifier,chance = splitText(inp,",");
  14. multiplier = floor(1.0/tonumber(chance) + 1);
  15. $recipes$.add({"in"=in,"out"=out,"multiplier"=multiplier,
  16. "identifier" = identifier});
  17. };
  18. };
  19. // minecraft:soul_sand,minecraft:quartz,minecraft:cobblestone,0.48
  20. // minecraft:soul_sand,minecraft:quartz,minecraft:cobblestone,0.48
  21. // minecraft:red_sand,minecraft:gold_nugget,minecraft:gravel,0.36
  22. estimateToCraft(identifier):{
  23. transferred = 0;
  24. changed = true;
  25. while(changed){
  26. changed = false;
  27. items = $chestCobblestone$.call("list")@table;
  28. slots = items.keys();
  29.  
  30. for(i=0;i<slots.length;i++){
  31. slot = slots[i];
  32. item = items[slot]@table;
  33. if (item["name"]!=identifier) {
  34. continue;
  35. };
  36. t = $chestCobblestone$.call("pushItems", $chestIncOut$.getName(),
  37. slot, 64);
  38. transferred = transferred + t;
  39. if (t>0){
  40. changed = true;
  41. };
  42. };
  43. if (changed){
  44. sleep(1);
  45. };
  46. };
  47. return(transferred);
  48. };
  49.  
  50. transferAll(chestSrc, chestTarget, type):{
  51. transferred = 0;
  52. items = chestSrc.call("list")@table;
  53. slots = items.keys();
  54. for(i=0;i<slots.length;i++){
  55. slot = slots[i];
  56. item = items[slot]@table;
  57. if (item["name"] != type){
  58. continue;
  59. };
  60. transferred = transferred + chestSrc.call("pushItems",
  61. chestTarget.getName(), slot, 64);
  62. };
  63. return(transferred);
  64. };
  65.  
  66. transfer(chestSrc, chestTarget, toTransfer, type):{
  67. while(toTransfer > 0){
  68. items = chestSrc.call("list")@table;
  69. slots = items.keys();
  70. for(i=0;i<slots.length;i++){
  71. slot = slots[i];
  72. item = items[slot]@table;
  73. if (item["name"]!=type) {continue;};
  74. if (toTransfer<=0){
  75. return();
  76. };
  77. t = chestSrc.call("pushItems", chestTarget.getName(),
  78. slot, toTransfer);
  79. toTransfer = toTransfer - t;
  80. };
  81. };
  82. };
  83.  
  84. craft(toCraft, sourceItem, targetItem, multiplier):{
  85. craftedTotal = 0;
  86. while(toCraft>0){
  87. toTransfer = multiplier * toCraft;
  88. transfer($chestInput$, $chestIncIn$, toTransfer, sourceItem);
  89. print("transferred " + toTransfer +" "+sourceItem);
  90. sleep(20);
  91. crafted = transferAll($chestResult$, $chestIncOut$, targetItem);
  92. craftedTotal = craftedTotal + crafted;
  93. print("crafted "+crafted+" "+targetItem);
  94. toCraft = toCraft - crafted;
  95. };
  96. return(craftedTotal);
  97. };
  98.  
  99. craftingJob(recipe):{
  100. in = recipe["in"];
  101. out = recipe["out"];
  102. multiplier = recipe["multiplier"];
  103. identifier = recipe["identifier"];
  104.  
  105. while(true) {
  106. toCraft = estimateToCraft(identifier);
  107. if (toCraft <= 0){
  108. transferAll($chestResult$, $chestIncOut$, out);
  109. sleep(1);
  110. continue;
  111. }else{
  112. print(toCraft+" "+out+" requested");
  113. perCycle = floor(64/multiplier);
  114. while (toCraft > perCycle){
  115. toCraft = toCraft - craft(perCycle, in, out, multiplier);
  116. };
  117. craft(toCraft, in, out, multiplier);
  118. print("crafting done");
  119. };
  120. };
  121.  
  122. };
  123.  
  124. main: {
  125. $recipes$ = loadValue("recipes");
  126. if ($recipes$ == null){
  127. initRecipes();
  128. saveValue("recipes", $recipes$);
  129. };
  130.  
  131. $chestCobblestone$ = wrapPeripheral("top");
  132. $chestInput$ = wrapPeripheral("right");
  133. $chestResult$ = wrapPeripheral("left");
  134. $chestIncIn$ = wrapPeripheral("back");
  135. $chestIncOut$ = wrapPeripheral("front");
  136.  
  137. for(i=0;i<$recipes$.length;i++){
  138. startThread(@craftingJob, $recipes$[i]);
  139. };
  140. };
Add Comment
Please, Sign In to add comment