DEv0on

recipe

May 13th, 2020
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.43 KB | None | 0 0
  1. function version()
  2.   return 0.1
  3. end
  4.  
  5. -- returns (array) recipe, (array) result or false, false
  6. function load(_path)
  7.   if not fs.exists(_path) then
  8.     return false, false
  9.   end
  10.  
  11.   f = fs.open(_path, "r")
  12.  
  13.   -- check header
  14.   if(f.readLine() ~= "#!RCP") then
  15.     print("ERROR: Unsupported file")
  16.     f.close()
  17.     return false, false
  18.   end
  19.  
  20.   -- get recipe
  21.   tmp = nil
  22.   tmp = f.readLine()
  23.   if(tmp == nil) then
  24.     f.close()
  25.     return false, false
  26.   end
  27.   recipe = json.decode(tmp)
  28.  
  29.   if(#recipe == 0) then
  30.     print("ERROR: Corrupted file")
  31.     f.close()
  32.     return false, false
  33.   end
  34.  
  35.   -- get result
  36.   tmp = nil
  37.   tmp = f.readLine()
  38.   if(tmp == nil) then
  39.     f.close()
  40.     return false, false
  41.   end
  42.   f.close()
  43.  
  44.   result = json.decode(tmp)
  45.  
  46.   if(result["name"] == nil or result["damage"] == nil or result["damage"] == nil) then
  47.     print("ERROR: Corrupted file")
  48.     return false, false
  49.   end
  50.  
  51.   return recipe, result
  52. end
  53.  
  54. function create()
  55.   if not turtle then
  56.     print("ERROR: recipe:create - need turtle!")
  57.     return false, false
  58.   end
  59.   arr = {}
  60.   c = 1
  61.  
  62.   for i=1, 11 do
  63.  
  64.     if(i % 4 ~= 0) then
  65.       tmp = nil
  66.       tmp = turtle.getItemDetail(i)
  67.       if tmp ~= nil then
  68.         arr[c] = {
  69.           name = tmp.name,
  70.           damage = tmp.damage,
  71.           count = tmp.count
  72.         }
  73.         c = c + 1
  74.       end
  75.     end
  76.   end
  77.  
  78.   if(#arr == 0) then
  79.     arr = false
  80.   end
  81.  
  82.   tmp = nil
  83.   tmp = turtle.getItemDetail(16)
  84.   arr_out = {}
  85.  
  86.   if tmp ~= nil then
  87.     arr_out = {
  88.       name = tmp.name,
  89.       damage = tmp.damage,
  90.       count = tmp.count
  91.     }
  92.   else
  93.     arr_out = false
  94.   end
  95.  
  96.   return arr, arr_out
  97. end
  98.  
  99. function check(_recipe)
  100.   if not turtle then
  101.     print("ERROR: recipe:check - need turtle!")
  102.     return false, false
  103.   end
  104.  
  105.   bool_arr = {}
  106.   bool_pos = 1
  107.   for i=1, #_recipe do
  108.     bool_arr[i] = false
  109.   end
  110.  
  111.   drop_arr = {}
  112.  
  113.   for i=1, 16 do
  114.  
  115.     tmp = nil
  116.     tmp = turtle.getItemDetail(i)
  117.     drop_arr[i] = 0
  118.     if tmp ~= nil then
  119.    
  120.       for c=1, #_recipe do
  121.         if((tmp.name == _recipe[c]["name"]) and (tmp.damage == _recipe[c]["damage"])) then
  122.           if(tmp.count - _recipe[c]["count"] >= 0) then
  123.             tmp2 = nil
  124.             tmp2 = tmp.count - _recipe[c]["count"]
  125.             _recipe[c]["count"] = _recipe[c]["count"] - tmp.count
  126.             tmp.count = tmp2
  127.             _recipe[c]["name"] = "##DONE"
  128.            
  129.             bool_arr[bool_pos] = true
  130.             bool_pos = bool_pos + 1
  131.           else
  132.             _recipe[c]["count"] = _recipe[c]["count"] - tmp.count
  133.             tmp.count = 0
  134.           end
  135.         end
  136.       end
  137.      
  138.       drop_arr[i] = tmp.count
  139.     end
  140.   end
  141.  
  142.   success = true
  143.  
  144.   for i=1, #bool_arr do
  145.     if(bool_arr[i] == false) then
  146.       success = false
  147.     end
  148.   end
  149.  
  150.   return success, drop_arr
  151. end
  152.  
  153. function load_chest(side)
  154.   if peripheral.getType(side) == "chest" then
  155.     local chest = peripheral.wrap(side)
  156.     local slot_arr = {}
  157.     local c = 1
  158.    
  159.     -- reload and normalize names
  160.     for i=1, chest.getInventorySize() do
  161.       slot = chest.getStackInSlot(i)
  162.      
  163.       if slot then
  164.         slot_arr[c] = {
  165.           name = slot.id,
  166.           damage = slot.dmg,
  167.           count = slot.qty,
  168.           slot = i
  169.         }
  170.         c = c + 1
  171.       end
  172.     end
  173.    
  174.     return slot_arr
  175.   else
  176.     return false
  177.   end
  178. end
Advertisement
Add Comment
Please, Sign In to add comment