From: Olav Bakke Svendsen Date: Wed, 15 May 2024 17:58:12 +0000 (+0200) Subject: dofile instead of require etc X-Git-Tag: skystone~9 X-Git-Url: http://git.olavbs.no/?a=commitdiff_plain;h=75923a11b30c08be03d3dce1faf7ae5ebd734610;p=cc.git dofile instead of require etc --- diff --git a/lib/turtle-utils.lua b/lib/turtle-utils.lua index 081acb1..b860555 100644 --- a/lib/turtle-utils.lua +++ b/lib/turtle-utils.lua @@ -24,10 +24,12 @@ local oneOf = function(t, a) end -------------------------------------- +local lib = {} + -- findItem : list of item names -> index | false message -- returns the first inventory index containing an item from list t -- if no item is found, returns nil -local findItem = function(t) +lib.findItem = function(t) for i = 1, 16 do local item = turtle.getItemDetail(i) if item and oneOf(t, item.name) then return i end @@ -38,7 +40,7 @@ end -- findEmpty : index | false message -- returns the first empty slot -- if no empty slot is found, returns nil -local findEmpty = function() +lib.findEmpty = function() for i = 1, 16 do if not turtle.getItemDetail(i) then return i end end return false, "No empty slot" end @@ -46,7 +48,7 @@ end -- count : list of item names -> count -- counts how many slots contain an item from list t -- (does not count individual items) -local count = function(t) +lib.count = function(t) local c = 0 for i = 1, 16 do local item = turtle.getItemDetail(i) @@ -60,7 +62,7 @@ end -- selectItem : list of item names -> true | false message -- select first slot containing an item from list t if not already selected. -- returns bool. -local selectItem = function(t) +lib.selectItem = function(t) local item = turtle.getItemDetail() if item and oneOf(t, item.name) then return true @@ -78,7 +80,7 @@ end -- like selectItem, selects item from list, but tries to move it to the first slot. -- requires an empty slot to move items around. -- (this function exists because weak automata cannot digBlock with tools in slots >9) -local transferAndSelect = function(i, t) +lib.transferAndSelect = function(i, t) if i < 1 or i > 16 then return false, msg .. "Index out of range" end -- if a wanted item already is in the wanted slot, select it and return early local item = turtle.getItemDetail(i) @@ -116,7 +118,7 @@ end -- "u1 5r" = invalid -- commandMap : t f -local commandMap = +lib.commandMap = { ["u"] = turtle.up , ["d"] = turtle.down , ["f"] = turtle.forward @@ -126,19 +128,19 @@ local commandMap = } -- runStringCommand : string -> true | false message -local runCommandString = function(str, map) +lib.runStringCommand = function(str, map) local c, n, rest = string.match(string.gsub(str, "^%s+", ""), "^(%w)(%d*)(.*)") if not c then return true end if n == "" then n = 1 else n = tonumber(n) end for i = 1, n do map[c]() end -- print(cmd, tostring(i).."/"..tostring(n)) -- end - return turtle.runCommandString(rest) + return lib.runStringCommand(rest) end -- rep : number of repeats -> string -> string | nil -- repeats string n times -local rep = function(n, str) +lib.rep = function(n, str) if not n or n < 0 then return nil end local s = "" for i = 1, n do @@ -146,3 +148,5 @@ local rep = function(n, str) end return s end + +return lib diff --git a/vineyard/turtle/harvest.lua b/vineyard/turtle/harvest.lua index c57a9c0..c5bbea1 100644 --- a/vineyard/turtle/harvest.lua +++ b/vineyard/turtle/harvest.lua @@ -10,7 +10,7 @@ -- index -- repeat -local tu = require("lib/turtle-utils.lua") +local tu = dofile("/lib/turtle-utils.lua") local automata = peripheral.wrap("left") local shears = diff --git a/vineyard/turtle/package b/vineyard/turtle/package index b62febb..5b91eee 100644 --- a/vineyard/turtle/package +++ b/vineyard/turtle/package @@ -1,2 +1,2 @@ -lib/turtle-utils.lua:/lib/turle-utils.lua:o -vineyard/turtle/harvest.lua:/bin/harvest.lua:o +cc lib/turtle-utils.lua:/lib/turtle-utils.lua:o +cc vineyard/turtle/harvest.lua:/bin/harvest.lua:o