melzneni

BetterProbabilityProcessing

Oct 10th, 2023 (edited)
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. #import: A60Ncqat; // database
  2. #import:vCAdipqP; // better peripherals
  3. #import: AAMw1hB3; // math
  4.  
  5. // minecraft:soul_sand,minecraft:quartz,minecraft:cobblestone,0.48
  6. // minecraft:red_sand,minecraft:gold_nugget,minecraft:gravel,0.36
  7. // minecraft:sand,minecraft:clay_ball,minecraft:sand,0.25
  8. initRecipes:{
  9. $recipes$ = [];
  10. while (true) {
  11. inp = input("in,out,ident,chance");
  12.  
  13. if (inp==""){
  14. return();
  15. };
  16. in,out,identifier,chance = splitText(inp,",");
  17. multiplier = floor(1.0/tonumber(chance) + 1);
  18. $recipes$.add({"in"=in,"out"=out,"multiplier"=multiplier,
  19. "identifier" = identifier});
  20. };
  21. };
  22.  
  23. readCraftingJobs(limit):{
  24. transferred = true;
  25. toCraft = {};
  26. while(transferred){
  27. transferred=false;
  28. items = $chestIdentifierIn$.call("list")@table;
  29. slots = items.keys();
  30. for(i=0;i<slots.length;i++){
  31. slot = slots[i];
  32. item = items[slot]@table;
  33. identifier = item["name"];
  34. recipe = $recipesByIdentifier$[identifier];
  35. result = recipe["out"];
  36. if (recipe==null){
  37. continue;
  38. };
  39. t = $chestIdentifierIn$.call("pushItems", $chestOut$.getName(),
  40. slot, limit);
  41. if (t>0){
  42. transferred = true;
  43. };
  44. limit = limit - t;
  45. if (toCraft[result]==null){
  46. toCraft[result]=t;
  47. }else{
  48. toCraft[result]=toCraft[result]+t;
  49. };
  50. if (limit<=0){
  51. return(toCraft);
  52. };
  53. };
  54. };
  55. return(toCraft);
  56. };
  57.  
  58. waitOnRedstoneLevel(level):{
  59. while(getRedstoneInput("back") != level){
  60. sleep(0.25);
  61. };
  62. return();
  63. };
  64.  
  65. moveIngredientsFor(toCraft):{
  66. toMove = {};
  67. keys = toCraft.keys();
  68. for(i=0;i<keys.length;i++){
  69. key = keys[i];
  70. recipe=$recipesByResult$[key];
  71. toMove[recipe["in"]]=toCraft[key] * recipe["multiplier"];
  72. };
  73.  
  74. movedAny = false;
  75. movedAnyThisRound=true;
  76. while(movedAnyThisRound){
  77. movedAnyThisRound = false;
  78. items = $chestIncIn$.call("list")@table;
  79. slots = items.keys();
  80. for(i=0;i<slots.length;i++){
  81. slot = slots[i];
  82. item = items[slot]@table;
  83. identifier = item["name"];
  84. if (toMove[identifier]!=null){
  85. left = toMove[identifier];
  86. if (left>0){
  87. t=$chestIncIn$.call("pushItems", $chestProcessOut$.getName(),slot, left);
  88. toMove[identifier] = toMove[identifier] - t;
  89. if (t>0){
  90. movedAny = true;
  91. movedAnyThisRound = true;
  92. };
  93. }
  94.  
  95. };
  96. };
  97. };
  98. if (movedAny){
  99. waitOnRedstoneLevel(true);
  100. };
  101. return(movedAny);
  102. };
  103.  
  104. getCraftingResult():{
  105. waitOnRedstoneLevel(false);
  106. craftingResult = {};
  107. items = $chestProcessIn$.call("list")@table;
  108. slots = items.keys();
  109. for(i=0;i<slots.length;i++){
  110. slot = slots[i];
  111. item = items[slot]@table;
  112. identifier = item["name"];
  113. count = $chestProcessIn$.call("pushItems", $chestOut$.getName(),
  114. slot, item["count"]);
  115. if (craftingResult[identifier]==null){
  116. craftingResult[identifier] = count;
  117. }else{
  118. craftingResult[identifier] = craftingResult[identifier] + count;
  119. };
  120. };
  121. return(craftingResult);
  122. };
  123.  
  124. craft(toCraft):{
  125. if (moveIngredientsFor(toCraft)){
  126. print("crafting" ,toCraft);
  127. };
  128. craftingResult = getCraftingResult();
  129.  
  130. results = toCraft.keys();
  131. allDone = true;
  132. for(i=0;i<results.length;i++){
  133. result = results[i];
  134. if(craftingResult[result]!=null){
  135. toCraft[result] = toCraft[result] - craftingResult[result];
  136. if (toCraft[result]<=0){
  137. toCraft[result] = null;
  138. };
  139. };
  140. };
  141. return(toCraft);
  142. };
  143.  
  144. main: {
  145. $recipes$ = loadValue("recipes");
  146. if ($recipes$ == null){
  147. initRecipes();
  148. saveValue("recipes", $recipes$);
  149. };
  150.  
  151. $recipesByIdentifier$ = {};
  152. $recipesByResult$ = {};
  153. for(i=0;i<$recipes$.length;i++){
  154. recipe=$recipes$[i];
  155. $recipesByIdentifier$[recipe["identifier"]] = recipe;
  156. $recipesByResult$[recipe["out"]] = recipe;
  157. };
  158.  
  159. $chestProcessIn$ = wrapPeripheral("left");
  160. $chestProcessOut$ = wrapPeripheral("right");
  161. $chestIdentifierIn$ = wrapPeripheral("bottom");
  162. $chestIncIn$ = wrapPeripheral("front");
  163. $chestOut$ = wrapPeripheral("top");
  164.  
  165. toCraftAll = {};
  166. crafting=false;
  167. while(true) {
  168. toCraftNew = readCraftingJobs(10*64);
  169. keys = toCraftNew.keys();
  170. if (keys.length>0){
  171. print("requested", toCraftNew);
  172. };
  173. for(i=0;i<keys.length;i++){
  174. key = keys[i];
  175. if(toCraftAll[key]==null){
  176. toCraftAll[key] = toCraftNew[key];
  177. }else{
  178. toCraftAll[key] = toCraftAll[key] + toCraftNew[key];
  179. };
  180. };
  181.  
  182. nothingLeft = true;
  183. keys = toCraftAll.keys();
  184. for (i=0;i<keys.length;i++){
  185. if (toCraftAll[keys[i]]>0){
  186. nothingLeft = false;
  187. break;
  188. };
  189. };
  190. if (nothingLeft){
  191. toCraftAll = {};
  192. sleep(1);
  193. if(crafting){
  194. crafting=false;
  195. print("crafting done");
  196. };
  197. continue;
  198. };
  199. crafting=true;
  200.  
  201. toCraftAll = craft(toCraftAll);
  202. };
  203.  
  204. };
Add Comment
Please, Sign In to add comment