Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ===========================
- -- == craftLabo
- -- ===========================
- -- -----------------------
- -- config
- -- -----------------------
- --DATA_FILE_NAME = "cl.dat"
- -- -----------------------
- -- utility
- -- -----------------------
- local function fileReadAll(filePath)
- local hFile = fs.open(filePath, "r")
- if nil == hFile then
- return ""
- end
- local txt = hFile.readAll()
- hFile.close()
- return txt
- end
- local function fileOverwrite(fileName, text)
- local hFile = fs.open(fileName, "w")
- hFile.writeLine(text)
- hFile.close()
- end
- -- ---------------
- -- Logger
- -- ---------------
- -- do logging?
- DEBUG__DO_LOGGING = true
- -- local log
- DEBUG__LOCAL_ENABLED = true
- DEBUG__LOCAL_LOG_FILE_NAME = "debug.log"
- -- remote log
- DEBUG__REMOTE_ENABLED = false
- DEBUG__REMOTE_ADDR = 20
- DEBUG__REMOTE_MODEM_DIR = "right"
- function debug(txt)
- if DEBUG__DO_LOGGING then
- if DEBUG__LOCAL_ENABLED then
- local hFile
- if fs.exists(DEBUG__LOCAL_LOG_FILE_NAME) then
- hFile = fs.open(DEBUG__LOCAL_LOG_FILE_NAME, "a");
- else
- hFile = fs.open(DEBUG__LOCAL_LOG_FILE_NAME, "w");
- end
- hFile.writeLine(txt);
- hFile.close();
- end
- if DEBUG__REMOTE_ENABLED then
- rednet.open(DEBUG__REMOTE_MODEM_DIR)
- rednet.send(DEBUG__REMOTE_ADDR, txt)
- rednet.close()
- end
- end
- end
- -- -----------------------
- -- application
- -- -----------------------
- -- ---------------
- -- makeKey
- -- ---------------
- function makeKey(name, damage)
- return name .. "@" .. damage
- end
- -- ---------------
- -- itemDetail
- -- ---------------
- --[[
- function itemDetail(the_db, slot_no)
- detail = turtle.getItemDetail(slot_no)
- if nil == detail then
- return
- end
- if nil == the_db[detail.name] then
- data = { }
- data.detail = { }
- data.detail.name = detail.name
- data.detail.damage = detail.damage
- the_db[makeKey(detail.name, detail.damage)] = data
- end
- end
- ]]
- -- ---------------
- -- doCraft
- -- ---------------
- function check()
- for _, v in pairs({ 4,8,12,13,14,15,16 }) do
- if 0 < turtle.getItemCount(v) then
- print("Don't use slot 4,8,12,13,14,15,16.")
- return false
- end
- end
- if false == turtle.craft(0) then
- print("not craftable.")
- return false
- end
- return true
- end
- -- ---------------
- -- doCraft
- -- ---------------
- function doCraft()
- bfr = { }
- for i = 1, 16 do
- bfr[i] = turtle.getItemDetail(i)
- end
- turtle.craft(1)
- aft = { }
- for i = 1, 16 do
- aft[i] = turtle.getItemDetail(i)
- end
- return bfr, aft
- end
- -- ---------------
- -- getDetail
- -- ---------------
- function getDetail(bfr, aft)
- crafted_idx = { }
- ingredients_idx = { }
- for i = 1, 16 do
- if nil ~= aft[i]
- and (nil == bfr[i] or (aft[i].name ~= bfr[i].name or aft[i].damage ~= bfr[i].damage)) then
- table.insert(crafted_idx, i)
- end
- if nil ~= bfr[i] then
- table.insert(ingredients_idx, i)
- end
- end
- -- ------------------------------------
- crafted = { }
- for _, idx in ipairs(crafted_idx) do
- key = makeKey(aft[idx].name, aft[idx].damage)
- if nil == crafted[key] then
- crafted[key] = { }
- crafted[key].name = aft[idx].name
- crafted[key].damage = aft[idx].damage
- crafted[key].count = 0
- end
- crafted[key].count = crafted[key].count + aft[idx].count
- end
- -- ------------------------------------
- ingredients = { }
- symbols = { "A","B","C","D","E","F","G","H","I" }
- symbol_index = 0
- for _, idx in ipairs(ingredients_idx) do
- key = makeKey(bfr[idx].name, bfr[idx].damage)
- if nil == ingredients[key] then
- ingredients[key] = { }
- ingredients[key].name = bfr[idx].name
- ingredients[key].damage = bfr[idx].damage
- ingredients[key].count = 0
- symbol_index = symbol_index + 1
- ingredients[key].symbol = symbols[symbol_index]
- end
- if nil == aft[idx] then
- count = bfr[idx].count
- elseif aft[idx].name ~= bfr[idx].name or aft[idx].damage ~= bfr[idx].damage then
- count = bfr[idx].count
- elseif aft[idx].name == bfr[idx].name and aft[idx].damage == bfr[idx].damage then
- count = bfr[idx].count - aft[idx].count
- end
- ingredients[key].count = ingredients[key].count + count
- bfr[idx].symbol = ingredients[key].symbol
- end
- return ingredients, crafted
- end
- -- ---------------
- -- horizontalCheck
- -- ---------------
- function horizontalCheck(recipe, y)
- for x = 1, 3 do
- if "-" ~= recipe[y][x] then
- return true
- end
- end
- return false
- end
- -- ---------------
- -- verticalCheck
- -- ---------------
- function verticalCheck(recipe, x)
- for y = 1, 3 do
- if "-" ~= recipe[y][x] then
- return true
- end
- end
- return false
- end
- -- ---------------
- -- getRecipe
- -- ---------------
- function getRecipe(bfr)
- recipe = { }
- for i = 0, 2 do
- line = { }
- for j = 0, 2 do
- idx = i * 4 + j + 1
- if nil ~= bfr[idx] then
- line[j + 1] = bfr[idx].symbol
- else
- line[j + 1] = "-"
- end
- end
- recipe[i+1] = line
- end
- crop_top = 1
- crop_bottom = 3
- crop_left = 1
- crop_right = 3
- for x = 1, 3 do
- if verticalCheck(recipe, x) then
- crop_left = x
- break
- end
- end
- for x = 3, 1, -1 do
- if verticalCheck(recipe, x) then
- crop_right = x
- break
- end
- end
- for y = 1, 3 do
- if horizontalCheck(recipe, y) then
- crop_top = y
- break
- end
- end
- for y = 3, 1, -1 do
- if horizontalCheck(recipe, y) then
- crop_bottom = y
- break
- end
- end
- recipe_slim = { }
- for y = crop_top, crop_bottom do
- i = y - crop_top + 1
- line = { }
- for x = crop_left, crop_right do
- j = x - crop_left + 1
- line[j] = recipe[y][x]
- end
- recipe_slim[i] = line
- end
- return recipe, recipe_slim
- end
- -- ---------------
- -- craft
- -- ---------------
- --function craft(the_db)
- function craft()
- debug("craft -----------------------------")
- if false == check() then
- return
- end
- bfr, aft = doCraft()
- ingredients, crafted = getDetail(bfr, aft)
- recipe, recipe_slim = getRecipe(bfr)
- debug("ingredients = " .. textutils.serialize(ingredients))
- debug("crafted = " .. textutils.serialize(crafted))
- debug("recipe = " .. textutils.serialize(recipe))
- debug("recipe_slim = " .. textutils.serialize(recipe_slim))
- end
- -- -----------------------
- -- main
- -- -----------------------
- --args = { ... }
- --if 0 == #args then
- -- print("usage")
- -- print(" craftLabo detail|craft|smelt|dig|bucket|spill")
- --end
- --the_db = textutils.unserialize(fileReadAll(DATA_FILE_NAME))
- --if nil == the_db then
- -- the_db = { }
- --end
- --if "detail" == args[1] then
- -- for i = 1, 16 do
- -- itemDetail(the_db, i)
- -- end
- --end
- --if "craft" == args[1] then
- -- craft(the_db)
- craft()
- --end
- --fileOverwrite(DATA_FILE_NAME, textutils.serialize(the_db))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement