Advertisement
kssr3951

craftLabo (unfinished)

May 12th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.89 KB | None | 0 0
  1. -- ===========================
  2. -- == craftLabo
  3. -- ===========================
  4. -- -----------------------
  5. -- config
  6. -- -----------------------
  7. --DATA_FILE_NAME = "cl.dat"
  8. -- -----------------------
  9. -- utility
  10. -- -----------------------
  11. local function fileReadAll(filePath)
  12.   local hFile = fs.open(filePath, "r")
  13.   if nil == hFile then
  14.     return ""
  15.   end
  16.   local txt = hFile.readAll()
  17.   hFile.close()
  18.   return txt
  19. end
  20. local function fileOverwrite(fileName, text)
  21.   local hFile = fs.open(fileName, "w")
  22.   hFile.writeLine(text)
  23.   hFile.close()
  24. end
  25. -- ---------------
  26. -- Logger
  27. -- ---------------
  28. -- do logging?
  29. DEBUG__DO_LOGGING           = true
  30. -- local log
  31. DEBUG__LOCAL_ENABLED        = true
  32. DEBUG__LOCAL_LOG_FILE_NAME  = "debug.log"
  33. -- remote log
  34. DEBUG__REMOTE_ENABLED       = false
  35. DEBUG__REMOTE_ADDR          = 20
  36. DEBUG__REMOTE_MODEM_DIR     = "right"
  37. function debug(txt)
  38.   if DEBUG__DO_LOGGING then
  39.     if DEBUG__LOCAL_ENABLED then
  40.       local hFile
  41.       if fs.exists(DEBUG__LOCAL_LOG_FILE_NAME) then
  42.         hFile = fs.open(DEBUG__LOCAL_LOG_FILE_NAME, "a");
  43.       else
  44.         hFile = fs.open(DEBUG__LOCAL_LOG_FILE_NAME, "w");
  45.       end
  46.       hFile.writeLine(txt);
  47.       hFile.close();
  48.     end
  49.     if DEBUG__REMOTE_ENABLED then
  50.       rednet.open(DEBUG__REMOTE_MODEM_DIR)
  51.       rednet.send(DEBUG__REMOTE_ADDR, txt)
  52.       rednet.close()
  53.     end
  54.   end
  55. end
  56. -- -----------------------
  57. -- application
  58. -- -----------------------
  59. -- ---------------
  60. -- makeKey
  61. -- ---------------
  62. function makeKey(name, damage)
  63.   return name .. "@" .. damage
  64. end
  65. -- ---------------
  66. -- itemDetail
  67. -- ---------------
  68. --[[
  69. function itemDetail(the_db, slot_no)
  70.   detail = turtle.getItemDetail(slot_no)
  71.   if nil == detail then
  72.     return
  73.   end
  74.   if nil == the_db[detail.name] then
  75.     data = { }
  76.     data.detail = { }
  77.     data.detail.name = detail.name
  78.     data.detail.damage = detail.damage
  79.     the_db[makeKey(detail.name, detail.damage)] = data
  80.   end
  81. end
  82. ]]
  83. -- ---------------
  84. -- doCraft
  85. -- ---------------
  86. function check()
  87.   for _, v in pairs({ 4,8,12,13,14,15,16 }) do
  88.     if 0 < turtle.getItemCount(v) then
  89.       print("Don't use slot 4,8,12,13,14,15,16.")
  90.       return false
  91.     end
  92.   end
  93.   if false == turtle.craft(0) then
  94.     print("not craftable.")
  95.     return false
  96.   end
  97.   return true
  98. end
  99. -- ---------------
  100. -- doCraft
  101. -- ---------------
  102. function doCraft()
  103.   bfr = { }
  104.   for i = 1, 16 do
  105.     bfr[i] = turtle.getItemDetail(i)
  106.   end
  107.   turtle.craft(1)
  108.   aft = { }
  109.   for i = 1, 16 do
  110.     aft[i] = turtle.getItemDetail(i)
  111.   end
  112.   return bfr, aft
  113. end
  114. -- ---------------
  115. -- getDetail
  116. -- ---------------
  117. function getDetail(bfr, aft)
  118.   crafted_idx = { }
  119.   ingredients_idx = { }
  120.   for i = 1, 16 do
  121.     if     nil ~= aft[i]
  122.       and (nil == bfr[i] or (aft[i].name ~= bfr[i].name or aft[i].damage ~= bfr[i].damage)) then
  123.       table.insert(crafted_idx, i)
  124.     end
  125.     if nil ~= bfr[i] then
  126.       table.insert(ingredients_idx, i)
  127.     end
  128.   end
  129.   -- ------------------------------------
  130.   crafted = { }
  131.   for _, idx in ipairs(crafted_idx) do
  132.     key = makeKey(aft[idx].name, aft[idx].damage)
  133.     if nil == crafted[key] then
  134.       crafted[key] = { }
  135.       crafted[key].name   = aft[idx].name
  136.       crafted[key].damage = aft[idx].damage
  137.       crafted[key].count  = 0
  138.     end
  139.     crafted[key].count = crafted[key].count + aft[idx].count
  140.   end
  141.   -- ------------------------------------
  142.   ingredients = { }
  143.   symbols = { "A","B","C","D","E","F","G","H","I" }
  144.   symbol_index = 0
  145.   for _, idx in ipairs(ingredients_idx) do
  146.     key = makeKey(bfr[idx].name, bfr[idx].damage)
  147.     if nil == ingredients[key] then
  148.       ingredients[key] = { }
  149.       ingredients[key].name   = bfr[idx].name
  150.       ingredients[key].damage = bfr[idx].damage
  151.       ingredients[key].count  = 0
  152.       symbol_index = symbol_index + 1
  153.       ingredients[key].symbol = symbols[symbol_index]
  154.     end
  155.     if nil == aft[idx] then
  156.       count = bfr[idx].count
  157.     elseif aft[idx].name ~= bfr[idx].name or aft[idx].damage ~= bfr[idx].damage then
  158.       count = bfr[idx].count
  159.     elseif aft[idx].name == bfr[idx].name and aft[idx].damage == bfr[idx].damage then
  160.       count = bfr[idx].count - aft[idx].count
  161.     end
  162.     ingredients[key].count = ingredients[key].count + count
  163.     bfr[idx].symbol = ingredients[key].symbol
  164.   end
  165.   return ingredients, crafted
  166. end
  167. -- ---------------
  168. -- horizontalCheck
  169. -- ---------------
  170. function horizontalCheck(recipe, y)
  171.   for x = 1, 3 do
  172.     if "-" ~= recipe[y][x] then
  173.       return true
  174.     end
  175.   end
  176.   return false
  177. end
  178. -- ---------------
  179. -- verticalCheck
  180. -- ---------------
  181. function verticalCheck(recipe, x)
  182.   for y = 1, 3 do
  183.     if "-" ~= recipe[y][x] then
  184.       return true
  185.     end
  186.   end
  187.   return false
  188. end
  189. -- ---------------
  190. -- getRecipe
  191. -- ---------------
  192. function getRecipe(bfr)
  193.   recipe = { }
  194.   for i = 0, 2 do
  195.     line = { }
  196.     for j = 0, 2 do
  197.       idx = i * 4 + j + 1
  198.       if nil ~= bfr[idx] then
  199.         line[j + 1] = bfr[idx].symbol
  200.       else
  201.         line[j + 1] = "-"
  202.       end
  203.     end
  204.     recipe[i+1] = line
  205.   end
  206.   crop_top = 1
  207.   crop_bottom = 3
  208.   crop_left = 1
  209.   crop_right = 3
  210.   for x = 1, 3 do
  211.     if verticalCheck(recipe, x) then
  212.       crop_left = x
  213.       break
  214.     end
  215.   end
  216.   for x = 3, 1, -1 do
  217.     if verticalCheck(recipe, x) then
  218.       crop_right = x
  219.       break
  220.     end
  221.   end
  222.   for y = 1, 3 do
  223.     if horizontalCheck(recipe, y) then
  224.       crop_top = y
  225.       break
  226.     end
  227.   end
  228.   for y = 3, 1, -1 do
  229.     if horizontalCheck(recipe, y) then
  230.       crop_bottom = y
  231.       break
  232.     end
  233.   end
  234.   recipe_slim = { }
  235.   for y = crop_top, crop_bottom do
  236.     i = y - crop_top + 1
  237.     line = { }
  238.     for x = crop_left, crop_right do
  239.       j = x - crop_left + 1
  240.       line[j] = recipe[y][x]
  241.     end
  242.     recipe_slim[i] = line
  243.   end
  244.   return recipe, recipe_slim
  245. end
  246. -- ---------------
  247. -- craft
  248. -- ---------------
  249. --function craft(the_db)
  250. function craft()
  251.   debug("craft -----------------------------")
  252.   if false == check() then
  253.     return
  254.   end
  255.   bfr, aft = doCraft()
  256.   ingredients, crafted = getDetail(bfr, aft)
  257.   recipe, recipe_slim = getRecipe(bfr)
  258.   debug("ingredients = " .. textutils.serialize(ingredients))
  259.   debug("crafted = " .. textutils.serialize(crafted))
  260.   debug("recipe = " .. textutils.serialize(recipe))
  261.   debug("recipe_slim = " .. textutils.serialize(recipe_slim))
  262. end
  263. -- -----------------------
  264. -- main
  265. -- -----------------------
  266. --args = { ... }
  267. --if 0 == #args then
  268. --  print("usage")
  269. --  print("  craftLabo detail|craft|smelt|dig|bucket|spill")
  270. --end
  271. --the_db = textutils.unserialize(fileReadAll(DATA_FILE_NAME))
  272. --if nil == the_db then
  273. --  the_db = { }
  274. --end
  275. --if "detail" == args[1] then
  276. --  for i = 1, 16 do
  277. --    itemDetail(the_db, i)
  278. --  end
  279. --end
  280. --if "craft" == args[1] then
  281. --  craft(the_db)
  282.   craft()
  283. --end
  284. --fileOverwrite(DATA_FILE_NAME, textutils.serialize(the_db))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement