From bbddb8626a7cf04fac797e1a732df03a659c7b7a Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Wed, 21 Dec 2022 10:43:39 +0000 Subject: [PATCH 01/20] [create-pull-request] automated change --- min/movescript.lua | 1 + minify.lua => minify.lua.1 | 0 2 files changed, 1 insertion(+) create mode 100644 min/movescript.lua rename minify.lua => minify.lua.1 (100%) diff --git a/min/movescript.lua b/min/movescript.lua new file mode 100644 index 0000000..f8c8ee2 --- /dev/null +++ b/min/movescript.lua @@ -0,0 +1 @@ +a="0.0.1"local d=turtle local e={}e.defaultSettings={debug=false,safe=true,destructive=false,fuels={"minecraft:coal","minecraft:charcoal"}}local function f(t,u)if u and u.debug then print("[MS] "..t)end end function d.digBack(t)d.turnRight()d.turnRight()d.dig(t)d.turnRight()d.turnRight()end function d.detectBack()d.turnRight()d.turnRight()local t=d.detect()d.turnRight()d.turnRight()return t end local function g(t,u,v,w)w=w or e.defaultSettings b=w.safe or e.defaultSettings.safe c=w.destructive or e.defaultSettings.destructive local x=t()if not b then return end while not x do f("Unable to move.",w)if c and v()then f("Detected a block in the way; attempting to remove it.",w)u()end x=t()end end local function h(t)f("Moving up.",t)g(d.up,d.digUp,d.detectUp,t)end local function j(t)f("Moving down.",t)g(d.down,d.digDown,d.detectDown,t)end local function k(t)f("Moving forward.",t)g(d.forward,d.dig,d.detect,t)end local function l(t)f("Moving back.",t)g(d.back,d.digBack,d.detectBack,t)end local function m(t)f("Turning right.",t)d.turnRight()end local function n(t)f("Turning left.",t)d.turnLeft()end local o={["U"]={f=h,needsFuel=true},["D"]={f=j,needsFuel=true},["L"]={f=n,needsFuel=false},["R"]={f=m,needsFuel=false},["F"]={f=k,needsFuel=true},["B"]={f=l,needsFuel=true},["P"]={f=d.place,needsFuel=false},["Pu"]={f=d.placeUp,needsFuel=false},["Pd"]={f=d.placeDown,needsFuel=false},["A"]={f=d.attack,needsFuel=false},["Au"]={f=d.attackUp,needsFuel=false},["Ad"]={f=d.attackDown,needsFuel=false}}local function p(t)f("Refueling...",t)local u=t.fuels or e.defaultSettings.fuels local v=false for w=1,16 do local x=d.getItemDetail(w)for y,z in pairs(u)do if x.name==z then d.select(i)if d.refuel(x.count)then v=true end break end end end return v end local function q(t,u)p(u)while d.getFuelLeveld.getFuelLevel()))if w then local x=t.count q(x,u)end for x=1,t.count do v.f()end end end local function s(t,u)local v={}for w in string.gfind(t,"%W*(%d*%u%l*)%W*")do local x,y=string.find(w,"%d+")local z,A=string.find(w,"%u%l*")local B=1 if x~=nil then B=tonumber(string.sub(w,x,y))end local C=string.sub(w,z,A)if B<1 or B>d.getFuelLimit()then error("Instruction at index "..z.." has an invalid count of "..B..". It should be >= 1 and <= "..d.getFuelLimit())end if o[C]==nil then error("Instruction at index "..z..", \""..C.."\", does not refer to a valid action.")end table.insert(v,{action=C,count=B})f("Parsed instruction: "..w,u)end return v end function e.run(t,u)u=u or e.defaultSettings t=t or""f("Executing script: "..t,u)local v=s(t,u)for w,x in pairs(v)do r(x,u)end end return e \ No newline at end of file diff --git a/minify.lua b/minify.lua.1 similarity index 100% rename from minify.lua rename to minify.lua.1 -- 2.34.1 From e970d3fb8c2f411ccd9da177485db885a9849364 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Wed, 21 Dec 2022 11:43:50 +0100 Subject: [PATCH 02/20] Update minify.yml Changed to only minify on change to src. --- .github/workflows/minify.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/minify.yml b/.github/workflows/minify.yml index 7898a12..c2b500e 100644 --- a/.github/workflows/minify.yml +++ b/.github/workflows/minify.yml @@ -3,6 +3,8 @@ name: Minify Scripts on: push: branches: [ "main" ] + paths: + - src/** workflow_dispatch: -- 2.34.1 From 0e5086c6ee01a444a8f4a738d24280b73060084e Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Wed, 21 Dec 2022 11:48:21 +0100 Subject: [PATCH 03/20] Update minify.yml Added itemscript. --- .github/workflows/minify.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/minify.yml b/.github/workflows/minify.yml index c2b500e..25740f4 100644 --- a/.github/workflows/minify.yml +++ b/.github/workflows/minify.yml @@ -29,8 +29,8 @@ jobs: run: | rm -rf min mkdir min - touch min/movescript.lua lua minify.lua minify src/movescript.lua > min/movescript.lua + lua minify.lua minify src/itemscript.lua > min/itemscript.lua - name: Clean Up Repository run: | -- 2.34.1 From 5dd09e62347a4836711ae00b12a3d75887935338 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Wed, 21 Dec 2022 12:27:05 +0100 Subject: [PATCH 04/20] Updated docs to add reference, improved itemscript. --- docs/src/.vuepress/config.js | 3 +- docs/src/guide/movescript/README.md | 2 +- docs/src/guide/movescript/reference.md | 24 ++++++ src/itemscript.lua | 103 +++++++++++++++++-------- src/movescript.lua | 7 ++ 5 files changed, 104 insertions(+), 35 deletions(-) create mode 100644 docs/src/guide/movescript/reference.md diff --git a/docs/src/.vuepress/config.js b/docs/src/.vuepress/config.js index c187fde..7c96c4f 100755 --- a/docs/src/.vuepress/config.js +++ b/docs/src/.vuepress/config.js @@ -56,7 +56,8 @@ module.exports = { children: [ '', 'spec', - 'settings' + 'settings', + 'reference' ] } ] diff --git a/docs/src/guide/movescript/README.md b/docs/src/guide/movescript/README.md index 54c9f20..68957d3 100644 --- a/docs/src/guide/movescript/README.md +++ b/docs/src/guide/movescript/README.md @@ -7,7 +7,7 @@ The Movescript module provides a simple interface for executing *movescript sour To install this module, run the following command from your turtle's console: ```shell -wget https://raw.githubusercontent.com/andrewlalis/movescript/main/src/movescript.lua movescript.lua +wget https://raw.githubusercontent.com/andrewlalis/movescript/main/min/movescript.lua movescript.lua ``` And then use it in a script: diff --git a/docs/src/guide/movescript/reference.md b/docs/src/guide/movescript/reference.md new file mode 100644 index 0000000..dbca63e --- /dev/null +++ b/docs/src/guide/movescript/reference.md @@ -0,0 +1,24 @@ +# Module Reference + +The following is a complete reference of the **movescript** module. All symbols defined here belong to the `movescript` module, and can be accessed via an instance of that module. For example: + +```lua +local ms = require("movescript") +ms.run("2F") +``` + +## Functions + +### `run(script, settings)` + +Runs the given `script` string as a movescript, and optionally a `settings` table can be provided. Otherwise, [default settings](settings.md) will be used. + +### `runFile(filename, settings)` + +Reads content from the given filename and executes it as a script. Just like with `run`, an optional `settings` table can be provided. + +## Variables + +### `defaultSettings` + +A table containing the default settings for any script executed by the movescript module. \ No newline at end of file diff --git a/src/itemscript.lua b/src/itemscript.lua index 4ec60de..d469a41 100644 --- a/src/itemscript.lua +++ b/src/itemscript.lua @@ -12,7 +12,9 @@ local t = turtle -- The itemscript module. Functions defined within this table are exported. local itemscript = {} -local function itemStackMatches(itemStack, name, fuzzy) +-- Determines if an item stack matches the given name. +-- If fuzzy, then the item name will be matched against the given name. +local function stackMatches(itemStack, name, fuzzy) return itemStack ~= nil and ( (not fuzzy and itemStack.name == name) or @@ -20,14 +22,66 @@ local function itemStackMatches(itemStack, name, fuzzy) ) end +--[[ +The following describes an item filter: +A table containing a filter mechanism. +{ + filterFunction = stackMatches, + fuzzy = false, + whitelist = true +} +The filterFunction is defined like so: +function filterFunction(item, filter) + return true | false +end +]]-- + +local function makeItemFilter(var, fuzzy, whitelist) + local filter = { + filterFunction = nil, + fuzzy = fuzzy or false, + whitelist = whitelist + } + if type(var) == "string" then + -- If the filter is a single item name, define a single-item filter that matches against the name. + filter.filterFunction = function (item, filter) + local matches = stackMatches(item, var, filter.fuzzy) + if filter.whitelist then + return matches + else + return not matches + end + end + elseif type(var) == "table" then + -- If the filter is a list of item names, define a multi-item filter. + filter.filterFunction = function (item, filter) + for _, itemName in pairs(var) do + if filter.whitelist and stackMatches(item, itemName, filter.fuzzy) then + return true + elseif not filter.whitelist and not stackMatches(item, itemName, filter.fuzzy) then + return false + end + end + -- If whitelist and we couldn't find a match, return false. + -- If blacklist and we couldn't find a non-match, return true. + return not filter.whitelist + end + elseif type(var) == "function" then + -- Otherwise, just use the provided filter. + filter.filterFunction = var + end + filter.apply = function(item) + return filter.filterFunction(item, filter) + end + return filter +end + -- Gets the total number of items of a certain type in the turtle's inventory. --- If fuzzy is set as true, then it'll match substrings matching the given name. -function itemscript.totalCount(name, fuzzy) - fuzzy = fuzzy or false +function itemscript.totalCount(filter) local count = 0 for i = 1, 16 do local item = t.getItemDetail(i) - if itemStackMatches(item, name, fuzzy) then + if filter.apply(item) then count = count + item.count end end @@ -36,11 +90,10 @@ end -- Selects a slot containing at least one of the given item type. -- Returns a boolean indicating whether we could find and select the item. -function itemscript.select(name, fuzzy) - fuzzy = fuzzy or false +function itemscript.select(filter) for i = 1, 16 do local item = t.getItemDetail(i) - if itemStackMatches(item, name, fuzzy) then + if filter.apply(item) then t.select(i) return true end @@ -48,43 +101,27 @@ function itemscript.select(name, fuzzy) return false end -local function itemMatchesFilter(item, name, fuzzy) - fuzzy = fuzzy or false - return (not fuzzy and item.name == name) or string.find(item.name, name) -end - -local function itemNotMatchesFilter(item, name, fuzzy) - return not itemMatchesFilter(item, name, fuzzy) -end - -local function dropFiltered(name, fuzzy, dropFunction, filterFunction) +-- Helper function to drop items in a flexible way, using a drop function and filtering function. +local function dropFiltered(dropFunction, filter) for i = 1, 16 do local item = t.getItemDetail(i) - if filterFunction(item, name, fuzzy) then + if filter.apply(item) then t.select(i) dropFunction() end end end -function itemscript.dropAll(name, fuzzy) - dropFiltered(name, fuzzy or false, t.drop, itemMatchesFilter) +function itemscript.dropAll(filter) + dropFiltered(t.drop, filter) end -function itemscript.dropAllDown(name, fuzzy) - dropFiltered(name, fuzzy or false, t.dropDown, itemMatchesFilter) +function itemscript.dropAllDown(filter) + dropFiltered(t.dropDown, filter) end -function itemscript.dropAllUp(name, fuzzy) - dropFiltered(name, fuzzy or false, t.dropUp, itemMatchesFilter) -end - -function itemscript.dropAllExcept(name, fuzzy) - dropFiltered(name, fuzzy or false, t.drop, itemNotMatchesFilter) -end - -function itemscript.dropAllDownExcept(name, fuzzy) - dropFiltered(name, fuzzy or false, t.dropDown, itemNotMatchesFilter) +function itemscript.dropAllUp(filter) + dropFiltered(t.dropUp, filter) end -- Cleans up the turtle's inventory by compacting all stacks of items. diff --git a/src/movescript.lua b/src/movescript.lua index 21d8661..c63e816 100644 --- a/src/movescript.lua +++ b/src/movescript.lua @@ -198,4 +198,11 @@ function movescript.run(script, settings) end end +function movescript.runFile(filename, settings) + local f = fs.open(filename, "r") + local script = f.readAll() + f.close() + movescript.run(script, settings) +end + return movescript -- 2.34.1 From e862048c56af91a19952887c754b37c0714acc72 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Wed, 21 Dec 2022 12:28:33 +0100 Subject: [PATCH 05/20] Updated minify workflow. --- .github/workflows/minify.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/minify.yml b/.github/workflows/minify.yml index 25740f4..a6b5cf9 100644 --- a/.github/workflows/minify.yml +++ b/.github/workflows/minify.yml @@ -22,9 +22,6 @@ jobs: lua-version: 5.3 install-luarocks: true - - name: Install Minifier - run: wget https://raw.githubusercontent.com/stravant/lua-minify/master/minify.lua - - name: Minify Scripts run: | rm -rf min @@ -37,7 +34,6 @@ jobs: rm -rf .lua rm -rf .luarocks rm -rf .source - rm -f minify.lua - name: Create Pull Request uses: peter-evans/create-pull-request@v4 -- 2.34.1 From 54693f1e695ef899097e70bc5ce249929589a26e Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Wed, 21 Dec 2022 11:29:17 +0000 Subject: [PATCH 06/20] [create-pull-request] automated change --- min/itemscript.lua | 1 + min/movescript.lua | 1 + 2 files changed, 2 insertions(+) create mode 100644 min/itemscript.lua create mode 100644 min/movescript.lua diff --git a/min/itemscript.lua b/min/itemscript.lua new file mode 100644 index 0000000..4a98c54 --- /dev/null +++ b/min/itemscript.lua @@ -0,0 +1 @@ +a="0.0.1"local b=turtle local c={}local function d(g,h,i)return g~=nil and((not i and g.name==h)or string.find(g.name,h))end local function e(g,h,i)local j={filterFunction=nil,fuzzy=h or false,whitelist=i}if type(g)=="string"then j.filterFunction=function(k,l)local m=d(k,g,l.fuzzy)if l.whitelist then return m else return not m end end elseif type(g)=="table"then j.filterFunction=function(k,l)for m,n in pairs(g)do if l.whitelist and d(k,n,l.fuzzy)then return true elseif not l.whitelist and not d(k,n,l.fuzzy)then return false end end return not l.whitelist end elseif type(g)=="function"then j.filterFunction=g end j.apply=function(k)return j.filterFunction(k,j)end return j end function c.totalCount(g)local h=0 for i=1,16 do local j=b.getItemDetail(i)if g.apply(j)then h=h+j.count end end return h end function c.select(g)for h=1,16 do local i=b.getItemDetail(h)if g.apply(i)then b.select(h)return true end end return false end local function f(g,h)for i=1,16 do local j=b.getItemDetail(i)if h.apply(j)then b.select(i)g()end end end function c.dropAll(g)f(b.drop,g)end function c.dropAllDown(g)f(b.dropDown,g)end function c.dropAllUp(g)f(b.dropUp,g)end function c.organize()error("Not yet implemented.")end return c \ No newline at end of file diff --git a/min/movescript.lua b/min/movescript.lua new file mode 100644 index 0000000..1a98dc3 --- /dev/null +++ b/min/movescript.lua @@ -0,0 +1 @@ +a="0.0.1"local d=turtle local e={}e.defaultSettings={debug=false,safe=true,destructive=false,fuels={"minecraft:coal","minecraft:charcoal"}}local function f(t,u)if u and u.debug then print("[MS] "..t)end end function d.digBack(t)d.turnRight()d.turnRight()d.dig(t)d.turnRight()d.turnRight()end function d.detectBack()d.turnRight()d.turnRight()local t=d.detect()d.turnRight()d.turnRight()return t end local function g(t,u,v,w)w=w or e.defaultSettings b=w.safe or e.defaultSettings.safe c=w.destructive or e.defaultSettings.destructive local x=t()if not b then return end while not x do f("Unable to move.",w)if c and v()then f("Detected a block in the way; attempting to remove it.",w)u()end x=t()end end local function h(t)f("Moving up.",t)g(d.up,d.digUp,d.detectUp,t)end local function j(t)f("Moving down.",t)g(d.down,d.digDown,d.detectDown,t)end local function k(t)f("Moving forward.",t)g(d.forward,d.dig,d.detect,t)end local function l(t)f("Moving back.",t)g(d.back,d.digBack,d.detectBack,t)end local function m(t)f("Turning right.",t)d.turnRight()end local function n(t)f("Turning left.",t)d.turnLeft()end local o={["U"]={f=h,needsFuel=true},["D"]={f=j,needsFuel=true},["L"]={f=n,needsFuel=false},["R"]={f=m,needsFuel=false},["F"]={f=k,needsFuel=true},["B"]={f=l,needsFuel=true},["P"]={f=d.place,needsFuel=false},["Pu"]={f=d.placeUp,needsFuel=false},["Pd"]={f=d.placeDown,needsFuel=false},["A"]={f=d.attack,needsFuel=false},["Au"]={f=d.attackUp,needsFuel=false},["Ad"]={f=d.attackDown,needsFuel=false}}local function p(t)f("Refueling...",t)local u=t.fuels or e.defaultSettings.fuels local v=false for w=1,16 do local x=d.getItemDetail(w)for y,z in pairs(u)do if x.name==z then d.select(i)if d.refuel(x.count)then v=true end break end end end return v end local function q(t,u)p(u)while d.getFuelLeveld.getFuelLevel()))if w then local x=t.count q(x,u)end for x=1,t.count do v.f()end end end local function s(t,u)local v={}for w in string.gfind(t,"%W*(%d*%u%l*)%W*")do local x,y=string.find(w,"%d+")local z,A=string.find(w,"%u%l*")local B=1 if x~=nil then B=tonumber(string.sub(w,x,y))end local C=string.sub(w,z,A)if B<1 or B>d.getFuelLimit()then error("Instruction at index "..z.." has an invalid count of "..B..". It should be >= 1 and <= "..d.getFuelLimit())end if o[C]==nil then error("Instruction at index "..z..", \""..C.."\", does not refer to a valid action.")end table.insert(v,{action=C,count=B})f("Parsed instruction: "..w,u)end return v end function e.run(t,u)u=u or e.defaultSettings t=t or""f("Executing script: "..t,u)local v=s(t,u)for w,x in pairs(v)do r(x,u)end end function e.runFile(t,u)local v=fs.open(t,"r")local w=v.readAll()v.close()e.run(w,u)end return e \ No newline at end of file -- 2.34.1 From ad6a7d1be7732383f5cd6b0841ddbafe92ea91e1 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Wed, 21 Dec 2022 12:33:19 +0100 Subject: [PATCH 07/20] Made makeFilter public. --- src/itemscript.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/itemscript.lua b/src/itemscript.lua index d469a41..8cca245 100644 --- a/src/itemscript.lua +++ b/src/itemscript.lua @@ -36,7 +36,7 @@ function filterFunction(item, filter) end ]]-- -local function makeItemFilter(var, fuzzy, whitelist) +function itemscript.makeFilter(var, fuzzy, whitelist) local filter = { filterFunction = nil, fuzzy = fuzzy or false, -- 2.34.1 From 0874164abe333036e4f1d3bdb68c65049a7002c0 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Wed, 21 Dec 2022 11:34:01 +0000 Subject: [PATCH 08/20] [create-pull-request] automated change --- min/itemscript.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/min/itemscript.lua b/min/itemscript.lua index 4a98c54..e6bbdd6 100644 --- a/min/itemscript.lua +++ b/min/itemscript.lua @@ -1 +1 @@ -a="0.0.1"local b=turtle local c={}local function d(g,h,i)return g~=nil and((not i and g.name==h)or string.find(g.name,h))end local function e(g,h,i)local j={filterFunction=nil,fuzzy=h or false,whitelist=i}if type(g)=="string"then j.filterFunction=function(k,l)local m=d(k,g,l.fuzzy)if l.whitelist then return m else return not m end end elseif type(g)=="table"then j.filterFunction=function(k,l)for m,n in pairs(g)do if l.whitelist and d(k,n,l.fuzzy)then return true elseif not l.whitelist and not d(k,n,l.fuzzy)then return false end end return not l.whitelist end elseif type(g)=="function"then j.filterFunction=g end j.apply=function(k)return j.filterFunction(k,j)end return j end function c.totalCount(g)local h=0 for i=1,16 do local j=b.getItemDetail(i)if g.apply(j)then h=h+j.count end end return h end function c.select(g)for h=1,16 do local i=b.getItemDetail(h)if g.apply(i)then b.select(h)return true end end return false end local function f(g,h)for i=1,16 do local j=b.getItemDetail(i)if h.apply(j)then b.select(i)g()end end end function c.dropAll(g)f(b.drop,g)end function c.dropAllDown(g)f(b.dropDown,g)end function c.dropAllUp(g)f(b.dropUp,g)end function c.organize()error("Not yet implemented.")end return c \ No newline at end of file +a="0.0.1"local b=turtle local c={}local function d(f,g,h)return f~=nil and((not h and f.name==g)or string.find(f.name,g))end function c.makeFilter(f,g,h)local i={filterFunction=nil,fuzzy=g or false,whitelist=h}if type(f)=="string"then i.filterFunction=function(j,k)local l=d(j,f,k.fuzzy)if k.whitelist then return l else return not l end end elseif type(f)=="table"then i.filterFunction=function(j,k)for l,m in pairs(f)do if k.whitelist and d(j,m,k.fuzzy)then return true elseif not k.whitelist and not d(j,m,k.fuzzy)then return false end end return not k.whitelist end elseif type(f)=="function"then i.filterFunction=f end i.apply=function(j)return i.filterFunction(j,i)end return i end function c.totalCount(f)local g=0 for h=1,16 do local i=b.getItemDetail(h)if f.apply(i)then g=g+i.count end end return g end function c.select(f)for g=1,16 do local h=b.getItemDetail(g)if f.apply(h)then b.select(g)return true end end return false end local function e(f,g)for h=1,16 do local i=b.getItemDetail(h)if g.apply(i)then b.select(h)f()end end end function c.dropAll(f)e(b.drop,f)end function c.dropAllDown(f)e(b.dropDown,f)end function c.dropAllUp(f)e(b.dropUp,f)end function c.organize()error("Not yet implemented.")end return c \ No newline at end of file -- 2.34.1 From dd97d16e05519af4a1660dc68df56b58ae9ce0ca Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Wed, 21 Dec 2022 14:30:44 +0100 Subject: [PATCH 09/20] Added advanced filter stuff. --- src/itemscript.lua | 167 +++++++++++++++++++++++++++++---------------- 1 file changed, 108 insertions(+), 59 deletions(-) diff --git a/src/itemscript.lua b/src/itemscript.lua index 8cca245..5e9c57d 100644 --- a/src/itemscript.lua +++ b/src/itemscript.lua @@ -22,66 +22,114 @@ local function stackMatches(itemStack, name, fuzzy) ) end ---[[ -The following describes an item filter: -A table containing a filter mechanism. -{ - filterFunction = stackMatches, - fuzzy = false, - whitelist = true -} -The filterFunction is defined like so: -function filterFunction(item, filter) - return true | false -end -]]-- - -function itemscript.makeFilter(var, fuzzy, whitelist) - local filter = { - filterFunction = nil, - fuzzy = fuzzy or false, - whitelist = whitelist - } - if type(var) == "string" then - -- If the filter is a single item name, define a single-item filter that matches against the name. - filter.filterFunction = function (item, filter) - local matches = stackMatches(item, var, filter.fuzzy) - if filter.whitelist then - return matches - else - return not matches +-- Creates a filter function that filters on items whose names match the given list of names. +local function makeItemNamesFilter(names, fuzzy) + return function(item) + for _, itemName in pairs(names) do + if stackMatches(item, itemName, fuzzy) then + return true end end - elseif type(var) == "table" then - -- If the filter is a list of item names, define a multi-item filter. - filter.filterFunction = function (item, filter) - for _, itemName in pairs(var) do - if filter.whitelist and stackMatches(item, itemName, filter.fuzzy) then - return true - elseif not filter.whitelist and not stackMatches(item, itemName, filter.fuzzy) then - return false - end - end - -- If whitelist and we couldn't find a match, return false. - -- If blacklist and we couldn't find a non-match, return true. - return not filter.whitelist - end - elseif type(var) == "function" then - -- Otherwise, just use the provided filter. - filter.filterFunction = var + return false end - filter.apply = function(item) - return filter.filterFunction(item, filter) +end + +local function notFilter(filter) + return function(item) + return not filter(item) + end +end + +local function andFilter(filters) + return function(item) + for _, filter in pairs(filters) do + if not filter(item) then + return false + end + end + return true + end +end + +local function orFilter(filters) + return function(item) + for _, filter in pairs(filters) do + if filter(item) then + return true + end + end + return false + end +end + +-- Parses a filter expression string and returns a filter that implements it. +--[[ + Item Filter Expressions: + + A filter expression is a way to define a complex method of matching item + stacks. + + Prepending ! will match any item stack whose name does not match. + Prepending # will do a fuzzy match using string.find. +]]-- +local function parseItemFilterExpression(expr) + local prefixIdx, prefixIdxEnd = string.find(expr, "^[!#]*") + local fuzzy = false + local negated = false + if prefixIdx ~= nil then + for i = prefixIdx, i <= prefixIdxEnd do + if expr[i] == "!" then + negated = true + elseif expr[i] == "#" then + fuzzy = true + end + end + expr = string.sub(expr, prefixIdxEnd + 1, string.len(expr)) + end + local namespaceSeparatorIdx = string.find(expr, ":") + if namespaceSeparatorIdx == nil and not fuzzy then + expr = "minecraft:" .. expr + end + local filter = makeItemNamesFilter({expr}, fuzzy) + if negated then + filter = notFilter(filter) end return filter end --- Gets the total number of items of a certain type in the turtle's inventory. -function itemscript.totalCount(filter) +-- Converts an arbitrary variable into a filter; useful for any function that's public, so users can supply any filter. +-- It converts the following: +-- filter function tables directly. +-- strings and lists of strings are translated into an item names filter. +-- Functions are added with default fuzzy and whitelist parameters. +local function convertToFilter(var) + if type(var) == "table" and #var > 0 and type(var[1]) == "string" then + local filters = {} + for _, expr in pairs(var) do + table.insert(filters, parseFilterExpression(expr)) + end + return orFilter(filters) + elseif type(var) == "string" then + return parseFilterExpression(var) + elseif type(var) == "function" then + return var + else + error("Unsupported filter type: " .. type(var)) + end +end + +-- Convenience function for creating a filter function that allows specifying fuzziness. +function itemscript.nameFilter(name, fuzzy) + return makeItemNamesFilter({name}, fuzzy) +end + +-- Gets the total number of items in the turtle's inventory that match the given expression. +function itemscript.totalCount(filterExpr) + local filter = convertToFilter(filterExpr) local count = 0 for i = 1, 16 do local item = t.getItemDetail(i) - if filter.apply(item) then + if filter(item) then count = count + item.count end end @@ -90,10 +138,11 @@ end -- Selects a slot containing at least one of the given item type. -- Returns a boolean indicating whether we could find and select the item. -function itemscript.select(filter) +function itemscript.select(filterExpr) + local filter = convertToFilter(filterExpr) for i = 1, 16 do local item = t.getItemDetail(i) - if filter.apply(item) then + if filter(item) then t.select(i) return true end @@ -105,23 +154,23 @@ end local function dropFiltered(dropFunction, filter) for i = 1, 16 do local item = t.getItemDetail(i) - if filter.apply(item) then + if filter(item) then t.select(i) dropFunction() end end end -function itemscript.dropAll(filter) - dropFiltered(t.drop, filter) +function itemscript.dropAll(filterExpr) + dropFiltered(t.drop, convertToFilter(filterExpr)) end -function itemscript.dropAllDown(filter) - dropFiltered(t.dropDown, filter) +function itemscript.dropAllDown(filterExpr) + dropFiltered(t.dropDown, convertToFilter(filterExpr)) end -function itemscript.dropAllUp(filter) - dropFiltered(t.dropUp, filter) +function itemscript.dropAllUp(filterExpr) + dropFiltered(t.dropUp, convertToFilter(filterExpr)) end -- Cleans up the turtle's inventory by compacting all stacks of items. -- 2.34.1 From 9fa52dce96e41ae8c7638bebed2bb74b3260cd9d Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Wed, 21 Dec 2022 14:32:28 +0100 Subject: [PATCH 10/20] Fixed convertToFilter. --- src/itemscript.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/itemscript.lua b/src/itemscript.lua index 5e9c57d..febd4e0 100644 --- a/src/itemscript.lua +++ b/src/itemscript.lua @@ -106,11 +106,11 @@ local function convertToFilter(var) if type(var) == "table" and #var > 0 and type(var[1]) == "string" then local filters = {} for _, expr in pairs(var) do - table.insert(filters, parseFilterExpression(expr)) + table.insert(filters, parseItemFilterExpression(expr)) end return orFilter(filters) elseif type(var) == "string" then - return parseFilterExpression(var) + return parseItemFilterExpression(var) elseif type(var) == "function" then return var else -- 2.34.1 From 0d3688d7fc92f74d9e7b6548f857aee55a073f06 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Wed, 21 Dec 2022 14:39:55 +0100 Subject: [PATCH 11/20] Fixed parseItemFilterExpression. --- src/itemscript.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/itemscript.lua b/src/itemscript.lua index febd4e0..26d1897 100644 --- a/src/itemscript.lua +++ b/src/itemscript.lua @@ -73,7 +73,7 @@ end Prepending # will do a fuzzy match using string.find. ]]-- local function parseItemFilterExpression(expr) - local prefixIdx, prefixIdxEnd = string.find(expr, "^[!#]*") + local prefixIdx, prefixIdxEnd = string.find(expr, "^[!#]+") local fuzzy = false local negated = false if prefixIdx ~= nil then -- 2.34.1 From 57b9c384982b0e15b0ecab758aaeeebd26d9e2f7 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Wed, 21 Dec 2022 14:47:02 +0100 Subject: [PATCH 12/20] Fixed parser again. --- src/itemscript.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/itemscript.lua b/src/itemscript.lua index 26d1897..538934e 100644 --- a/src/itemscript.lua +++ b/src/itemscript.lua @@ -77,7 +77,7 @@ local function parseItemFilterExpression(expr) local fuzzy = false local negated = false if prefixIdx ~= nil then - for i = prefixIdx, i <= prefixIdxEnd do + for i = prefixIdx, prefixIdxEnd do if expr[i] == "!" then negated = true elseif expr[i] == "#" then -- 2.34.1 From 74821942cbe8b5f14a0f77b704c8e6bc20a2fd54 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Wed, 21 Dec 2022 14:52:19 +0100 Subject: [PATCH 13/20] Inlined item matching function. --- src/itemscript.lua | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/src/itemscript.lua b/src/itemscript.lua index 538934e..305e9ac 100644 --- a/src/itemscript.lua +++ b/src/itemscript.lua @@ -22,18 +22,6 @@ local function stackMatches(itemStack, name, fuzzy) ) end --- Creates a filter function that filters on items whose names match the given list of names. -local function makeItemNamesFilter(names, fuzzy) - return function(item) - for _, itemName in pairs(names) do - if stackMatches(item, itemName, fuzzy) then - return true - end - end - return false - end -end - local function notFilter(filter) return function(item) return not filter(item) @@ -90,11 +78,13 @@ local function parseItemFilterExpression(expr) if namespaceSeparatorIdx == nil and not fuzzy then expr = "minecraft:" .. expr end - local filter = makeItemNamesFilter({expr}, fuzzy) - if negated then - filter = notFilter(filter) + return function(item) + local matches = stackMatches(item, expr, fuzzy) + if negated then + matches = not matches + end + return matches end - return filter end -- Converts an arbitrary variable into a filter; useful for any function that's public, so users can supply any filter. @@ -118,11 +108,6 @@ local function convertToFilter(var) end end --- Convenience function for creating a filter function that allows specifying fuzziness. -function itemscript.nameFilter(name, fuzzy) - return makeItemNamesFilter({name}, fuzzy) -end - -- Gets the total number of items in the turtle's inventory that match the given expression. function itemscript.totalCount(filterExpr) local filter = convertToFilter(filterExpr) -- 2.34.1 From 035c7a4a70025565e92e1aa338fde9eb25f725e1 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Wed, 21 Dec 2022 14:56:53 +0100 Subject: [PATCH 14/20] Fixed parser again... --- src/itemscript.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/itemscript.lua b/src/itemscript.lua index 305e9ac..0927cd3 100644 --- a/src/itemscript.lua +++ b/src/itemscript.lua @@ -66,9 +66,10 @@ local function parseItemFilterExpression(expr) local negated = false if prefixIdx ~= nil then for i = prefixIdx, prefixIdxEnd do - if expr[i] == "!" then + local char = string.sub(expr, i, i) + if char == "!" then negated = true - elseif expr[i] == "#" then + elseif char == "#" then fuzzy = true end end -- 2.34.1 From 6de1a7b361d59ff308fe48d4dc2a0769a252a906 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Wed, 21 Dec 2022 15:00:28 +0100 Subject: [PATCH 15/20] Added check for nil items in parser filter function. --- src/itemscript.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/src/itemscript.lua b/src/itemscript.lua index 0927cd3..545fdb7 100644 --- a/src/itemscript.lua +++ b/src/itemscript.lua @@ -80,6 +80,7 @@ local function parseItemFilterExpression(expr) expr = "minecraft:" .. expr end return function(item) + if item == nil then return false end local matches = stackMatches(item, expr, fuzzy) if negated then matches = not matches -- 2.34.1 From 216cdf799a2c3207e63bd051eb16a6e6e72f66e2 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Wed, 21 Dec 2022 14:01:03 +0000 Subject: [PATCH 16/20] [create-pull-request] automated change --- min/itemscript.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/min/itemscript.lua b/min/itemscript.lua index e6bbdd6..998ad87 100644 --- a/min/itemscript.lua +++ b/min/itemscript.lua @@ -1 +1 @@ -a="0.0.1"local b=turtle local c={}local function d(f,g,h)return f~=nil and((not h and f.name==g)or string.find(f.name,g))end function c.makeFilter(f,g,h)local i={filterFunction=nil,fuzzy=g or false,whitelist=h}if type(f)=="string"then i.filterFunction=function(j,k)local l=d(j,f,k.fuzzy)if k.whitelist then return l else return not l end end elseif type(f)=="table"then i.filterFunction=function(j,k)for l,m in pairs(f)do if k.whitelist and d(j,m,k.fuzzy)then return true elseif not k.whitelist and not d(j,m,k.fuzzy)then return false end end return not k.whitelist end elseif type(f)=="function"then i.filterFunction=f end i.apply=function(j)return i.filterFunction(j,i)end return i end function c.totalCount(f)local g=0 for h=1,16 do local i=b.getItemDetail(h)if f.apply(i)then g=g+i.count end end return g end function c.select(f)for g=1,16 do local h=b.getItemDetail(g)if f.apply(h)then b.select(g)return true end end return false end local function e(f,g)for h=1,16 do local i=b.getItemDetail(h)if g.apply(i)then b.select(h)f()end end end function c.dropAll(f)e(b.drop,f)end function c.dropAllDown(f)e(b.dropDown,f)end function c.dropAllUp(f)e(b.dropUp,f)end function c.organize()error("Not yet implemented.")end return c \ No newline at end of file +a="0.0.1"local b=turtle local c={}local function d(k,l,m)return k~=nil and((not m and k.name==l)or string.find(k.name,l))end local function e(k)return function(l)return not k(l)end end local function f(k)return function(l)for m,n in pairs(k)do if not n(l)then return false end end return true end end local function g(k)return function(l)for m,n in pairs(k)do if n(l)then return true end end return false end end local function h(k)local l,m=string.find(k,"^[!#]+")local n=false local o=false if l~=nil then for q=l,m do local r=string.sub(k,q,q)if r=="!"then o=true elseif r=="#"then n=true end end k=string.sub(k,m+1,string.len(k))end local p=string.find(k,":")if p==nil and not n then k="minecraft:"..k end return function(q)if q==nil then return false end local r=d(q,k,n)if o then r=not r end return r end end local function i(k)if type(k)=="table"and#k>0 and type(k[1])=="string"then local l={}for m,n in pairs(k)do table.insert(l,h(n))end return g(l)elseif type(k)=="string"then return h(k)elseif type(k)=="function"then return k else error("Unsupported filter type: "..type(k))end end function c.totalCount(k)local l=i(k)local m=0 for n=1,16 do local o=b.getItemDetail(n)if l(o)then m=m+o.count end end return m end function c.select(k)local l=i(k)for m=1,16 do local n=b.getItemDetail(m)if l(n)then b.select(m)return true end end return false end local function j(k,l)for m=1,16 do local n=b.getItemDetail(m)if l(n)then b.select(m)k()end end end function c.dropAll(k)j(b.drop,i(k))end function c.dropAllDown(k)j(b.dropDown,i(k))end function c.dropAllUp(k)j(b.dropUp,i(k))end function c.organize()error("Not yet implemented.")end return c \ No newline at end of file -- 2.34.1 From ad37bdfa0e0ffe241df590beb44b4cb66fa9d7f5 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Wed, 21 Dec 2022 16:20:47 +0100 Subject: [PATCH 17/20] Updated docs. --- docs/src/.vuepress/config.js | 13 ++++- docs/src/.vuepress/styles/palette.styl | 4 +- docs/src/guide/itemscript/README.md | 17 ++++++- docs/src/guide/itemscript/filters.md | 66 ++++++++++++++++++++++++++ docs/src/guide/itemscript/reference.md | 28 +++++++++++ docs/src/guide/movescript/README.md | 2 +- docs/src/guide/movescript/reference.md | 10 ++-- 7 files changed, 128 insertions(+), 12 deletions(-) create mode 100644 docs/src/guide/itemscript/filters.md create mode 100644 docs/src/guide/itemscript/reference.md diff --git a/docs/src/.vuepress/config.js b/docs/src/.vuepress/config.js index 7c96c4f..dae83da 100755 --- a/docs/src/.vuepress/config.js +++ b/docs/src/.vuepress/config.js @@ -18,7 +18,7 @@ module.exports = { * ref:https://v1.vuepress.vuejs.org/config/#head */ head: [ - ['meta', { name: 'theme-color', content: '#3eaf7c' }], + ['meta', { name: 'theme-color', content: '#de9502' }], ['meta', { name: 'apple-mobile-web-app-capable', content: 'yes' }], ['meta', { name: 'apple-mobile-web-app-status-bar-style', content: 'black' }] ], @@ -60,6 +60,17 @@ module.exports = { 'reference' ] } + ], + '/guide/itemscript/': [ + { + title: 'Itemscript Module', + collapsable: false, + children: [ + '', + 'filters', + 'reference' + ] + } ] } }, diff --git a/docs/src/.vuepress/styles/palette.styl b/docs/src/.vuepress/styles/palette.styl index 6490cb3..47cd36a 100755 --- a/docs/src/.vuepress/styles/palette.styl +++ b/docs/src/.vuepress/styles/palette.styl @@ -4,7 +4,7 @@ * ref:https://v1.vuepress.vuejs.org/zh/config/#palette-styl */ -$accentColor = #3eaf7c +$accentColor = #de9502 $textColor = #2c3e50 $borderColor = #eaecef -$codeBgColor = #282c34 +$codeBgColor = #233536 diff --git a/docs/src/guide/itemscript/README.md b/docs/src/guide/itemscript/README.md index ded1c7f..153e48d 100644 --- a/docs/src/guide/itemscript/README.md +++ b/docs/src/guide/itemscript/README.md @@ -1,3 +1,18 @@ # Getting Started -> This page is a work-in-progress. \ No newline at end of file +The Itemscript module provides a flexible, powerful interface for managing a turtle's inventory, and any connected inventories. + +## Installing + +To install this module, run the following command from your turtle's console: + +```shell +wget https://raw.githubusercontent.com/andrewlalis/movescript/main/min/itemscript.lua +``` + +And then use it in a script: + +```lua +local is = require("itemscript") +print("Non-log items: " .. is.totalCount("!log")) +``` diff --git a/docs/src/guide/itemscript/filters.md b/docs/src/guide/itemscript/filters.md new file mode 100644 index 0000000..afe43a8 --- /dev/null +++ b/docs/src/guide/itemscript/filters.md @@ -0,0 +1,66 @@ +# Filters + +Most of the functions provided by Itemscript make use of *filter expressions*. You'll often see it denoted in function parameters as `filterExpr`. A filter expression can be one of several things: + +- A function that takes an `item` (as obtained from `getItemDetail`) and returns a boolean `true` if the item matches, or `false` if it doesn't. +- A string or list of strings (see [filter expression strings](#filter-expression-strings)). + +## Filter Functions + +Filter functions **must** follow these rules to avoid errors or undefined behavior: + +- The function accepts a single `item` parameter, which may be `nil`, or a table like `{ name = "item_name", count = 32 }`. +- The function returns `true` if the given item should pass the filter or `false` if not. + +Below is a simple example of a filter function that only allows item stacks with more than 10 items. + +```lua +function myFilterFunction(item) + return item ~= nil and item.count > 10 +end +``` + +## Filter Expression Strings + +In the case of strings, a **filter expression string** is a string that can be used to match against an item with some advanced options. + +The most basic form of an expression string is just an item name, like `"minecraft:dirt"`, or `"create:train_door"`. Most normal items will begin with the `minecraft:` *namespace* prefix. If you don't include such a prefix, and you're not doing a [fuzzy match](#fuzzy-match), itemscript will add `minecraft:` for you. + +For example, we can count the number of stone items in our inventory like this: + +```lua +local is = require("itemscript") +print(is.totalCount("minecraft:stone")) +print(is.totalCount("stone")) -- "minecraft:" is added for us. +``` + +### Negation + +If `!` is added to the beginning of the string, only items that **don't** match will pass the filter. + +For example, suppose we want to drop everything except for oak planks: + +```lua +local is = require("itemscript") +is.dropAll("!oak_planks") +``` + +### Fuzzy Match + +If `#` is added to the beginning of the string, a *fuzzy* match will be performed, instead of a normal one. That is, instead of looking for an item whose name exactly matches, we look for the first item whose name we can find a given pattern in. In other words, normally when matching we check if `item.name == your_text`, and in a fuzzy match, we check if `string.find(item.name, your_text)` is not `nil`. + +For example, suppose we want to count the total number of logs of any type. This would be quite tedious to do normally, but with fuzzy matching, it's trivial: + +```lua +local is = require("itemscript") +print(is.totalCount("#log")) +``` + +Because a fuzzy match is nothing more than passing your text to Lua's `string.find` function, you can also take advantage of the more advanced *character classes* to define matching patterns. [Read about Lua's pattern matching documentation here.](https://www.lua.org/pil/20.2.html) + +In the example below, we filter to all items that begin with the text `minecraft:red_` by using the special `^` character. + +```lua +local is = require("itemscript") +print(is.totalCount("#^minecraft:red_")) +``` \ No newline at end of file diff --git a/docs/src/guide/itemscript/reference.md b/docs/src/guide/itemscript/reference.md new file mode 100644 index 0000000..0a1e0cb --- /dev/null +++ b/docs/src/guide/itemscript/reference.md @@ -0,0 +1,28 @@ +# Module Reference + +The following is a complete reference of the **itemscript** module. All symbols defined here belong to the `itemscript` module, and can be accessed via an instance of that module. For example: + +```lua +local is = require("itemscript") +is.dropAll("stone") +``` + +## `totalCount(filterExpr)` + +Computes the total number of items matching the given [filter expression](./filters.md). + +## `select(filterExpr)` + +Selects the first inventory slot containing an item that matches the given [filter expression](./filters.md). Returns `true` if a slot was selected successfully, or `false` if no matching item could be found. + +## `dropAll(filterExpr)` + +Drops all items from the turtle's inventory matching the given [filter expression](./filters.md). + +## `dropAllDown(filterExpr)` + +Variant of [dropAll](#dropall-filterexpr) which drops items downward. + +## `dropAllUp(filterExpr)` + +Variant of [dropAll](#dropall-filterexpr) which drops items upward. \ No newline at end of file diff --git a/docs/src/guide/movescript/README.md b/docs/src/guide/movescript/README.md index 68957d3..560fce4 100644 --- a/docs/src/guide/movescript/README.md +++ b/docs/src/guide/movescript/README.md @@ -7,7 +7,7 @@ The Movescript module provides a simple interface for executing *movescript sour To install this module, run the following command from your turtle's console: ```shell -wget https://raw.githubusercontent.com/andrewlalis/movescript/main/min/movescript.lua movescript.lua +wget https://raw.githubusercontent.com/andrewlalis/movescript/main/min/movescript.lua ``` And then use it in a script: diff --git a/docs/src/guide/movescript/reference.md b/docs/src/guide/movescript/reference.md index dbca63e..4fccbd8 100644 --- a/docs/src/guide/movescript/reference.md +++ b/docs/src/guide/movescript/reference.md @@ -7,18 +7,14 @@ local ms = require("movescript") ms.run("2F") ``` -## Functions - -### `run(script, settings)` +## `run(script, settings)` Runs the given `script` string as a movescript, and optionally a `settings` table can be provided. Otherwise, [default settings](settings.md) will be used. -### `runFile(filename, settings)` +## `runFile(filename, settings)` Reads content from the given filename and executes it as a script. Just like with `run`, an optional `settings` table can be provided. -## Variables - -### `defaultSettings` +## `defaultSettings` A table containing the default settings for any script executed by the movescript module. \ No newline at end of file -- 2.34.1 From d3f64ed19178979c33a8967c852b282d93c0d55b Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Wed, 21 Dec 2022 16:26:26 +0100 Subject: [PATCH 18/20] Updated readme. --- README.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4cbb5b6..7e0a6d2 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,13 @@ -# movescript +# Movescript Simplified robot actions for Minecraft's ComputerCraft mod. + +For **installation, starter guides, examples, and reference documentation**, please refer to the [Movescript Documentation Pages](https://andrewlalis.github.io/movescript/). + +## Development + +All sources are developed under `src`, and automatic minified versions of them are added to the `min` directory. + +There are two main GitHub Actions that can run: + +1. On pushes to `main` with changes under `docs/`, the VuePress documentation site will be rebuilt and deployed to GitHub pages. +2. On pushes to `main` with changes under `src/`, all sources will be minified and an automated pull-request will be opened, to be manually merged by an administrator. -- 2.34.1 From 1c95cad34aa4740d25a982998ce513c5db08f7f4 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Thu, 22 Dec 2022 10:48:42 +0100 Subject: [PATCH 19/20] Fixed issue with nil fuels. --- src/movescript.lua | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/movescript.lua b/src/movescript.lua index c63e816..534e2a1 100644 --- a/src/movescript.lua +++ b/src/movescript.lua @@ -115,11 +115,13 @@ local function refuelAll(settings) local refueled = false for slot = 1, 16 do local item = t.getItemDetail(slot) - for _, fuelName in pairs(fuels) do - if item.name == fuelName then - t.select(i) - if t.refuel(item.count) then refueled = true end - break + if item ~= nil then + for _, fuelName in pairs(fuels) do + if item.name == fuelName then + t.select(i) + if t.refuel(item.count) then refueled = true end + break + end end end end -- 2.34.1 From f332969b2934fb4536cf7dd90dd4666e38b84de8 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Thu, 22 Dec 2022 09:49:15 +0000 Subject: [PATCH 20/20] [create-pull-request] automated change --- min/movescript.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/min/movescript.lua b/min/movescript.lua index 1a98dc3..1210e63 100644 --- a/min/movescript.lua +++ b/min/movescript.lua @@ -1 +1 @@ -a="0.0.1"local d=turtle local e={}e.defaultSettings={debug=false,safe=true,destructive=false,fuels={"minecraft:coal","minecraft:charcoal"}}local function f(t,u)if u and u.debug then print("[MS] "..t)end end function d.digBack(t)d.turnRight()d.turnRight()d.dig(t)d.turnRight()d.turnRight()end function d.detectBack()d.turnRight()d.turnRight()local t=d.detect()d.turnRight()d.turnRight()return t end local function g(t,u,v,w)w=w or e.defaultSettings b=w.safe or e.defaultSettings.safe c=w.destructive or e.defaultSettings.destructive local x=t()if not b then return end while not x do f("Unable to move.",w)if c and v()then f("Detected a block in the way; attempting to remove it.",w)u()end x=t()end end local function h(t)f("Moving up.",t)g(d.up,d.digUp,d.detectUp,t)end local function j(t)f("Moving down.",t)g(d.down,d.digDown,d.detectDown,t)end local function k(t)f("Moving forward.",t)g(d.forward,d.dig,d.detect,t)end local function l(t)f("Moving back.",t)g(d.back,d.digBack,d.detectBack,t)end local function m(t)f("Turning right.",t)d.turnRight()end local function n(t)f("Turning left.",t)d.turnLeft()end local o={["U"]={f=h,needsFuel=true},["D"]={f=j,needsFuel=true},["L"]={f=n,needsFuel=false},["R"]={f=m,needsFuel=false},["F"]={f=k,needsFuel=true},["B"]={f=l,needsFuel=true},["P"]={f=d.place,needsFuel=false},["Pu"]={f=d.placeUp,needsFuel=false},["Pd"]={f=d.placeDown,needsFuel=false},["A"]={f=d.attack,needsFuel=false},["Au"]={f=d.attackUp,needsFuel=false},["Ad"]={f=d.attackDown,needsFuel=false}}local function p(t)f("Refueling...",t)local u=t.fuels or e.defaultSettings.fuels local v=false for w=1,16 do local x=d.getItemDetail(w)for y,z in pairs(u)do if x.name==z then d.select(i)if d.refuel(x.count)then v=true end break end end end return v end local function q(t,u)p(u)while d.getFuelLeveld.getFuelLevel()))if w then local x=t.count q(x,u)end for x=1,t.count do v.f()end end end local function s(t,u)local v={}for w in string.gfind(t,"%W*(%d*%u%l*)%W*")do local x,y=string.find(w,"%d+")local z,A=string.find(w,"%u%l*")local B=1 if x~=nil then B=tonumber(string.sub(w,x,y))end local C=string.sub(w,z,A)if B<1 or B>d.getFuelLimit()then error("Instruction at index "..z.." has an invalid count of "..B..". It should be >= 1 and <= "..d.getFuelLimit())end if o[C]==nil then error("Instruction at index "..z..", \""..C.."\", does not refer to a valid action.")end table.insert(v,{action=C,count=B})f("Parsed instruction: "..w,u)end return v end function e.run(t,u)u=u or e.defaultSettings t=t or""f("Executing script: "..t,u)local v=s(t,u)for w,x in pairs(v)do r(x,u)end end function e.runFile(t,u)local v=fs.open(t,"r")local w=v.readAll()v.close()e.run(w,u)end return e \ No newline at end of file +a="0.0.1"local d=turtle local e={}e.defaultSettings={debug=false,safe=true,destructive=false,fuels={"minecraft:coal","minecraft:charcoal"}}local function f(t,u)if u and u.debug then print("[MS] "..t)end end function d.digBack(t)d.turnRight()d.turnRight()d.dig(t)d.turnRight()d.turnRight()end function d.detectBack()d.turnRight()d.turnRight()local t=d.detect()d.turnRight()d.turnRight()return t end local function g(t,u,v,w)w=w or e.defaultSettings b=w.safe or e.defaultSettings.safe c=w.destructive or e.defaultSettings.destructive local x=t()if not b then return end while not x do f("Unable to move.",w)if c and v()then f("Detected a block in the way; attempting to remove it.",w)u()end x=t()end end local function h(t)f("Moving up.",t)g(d.up,d.digUp,d.detectUp,t)end local function j(t)f("Moving down.",t)g(d.down,d.digDown,d.detectDown,t)end local function k(t)f("Moving forward.",t)g(d.forward,d.dig,d.detect,t)end local function l(t)f("Moving back.",t)g(d.back,d.digBack,d.detectBack,t)end local function m(t)f("Turning right.",t)d.turnRight()end local function n(t)f("Turning left.",t)d.turnLeft()end local o={["U"]={f=h,needsFuel=true},["D"]={f=j,needsFuel=true},["L"]={f=n,needsFuel=false},["R"]={f=m,needsFuel=false},["F"]={f=k,needsFuel=true},["B"]={f=l,needsFuel=true},["P"]={f=d.place,needsFuel=false},["Pu"]={f=d.placeUp,needsFuel=false},["Pd"]={f=d.placeDown,needsFuel=false},["A"]={f=d.attack,needsFuel=false},["Au"]={f=d.attackUp,needsFuel=false},["Ad"]={f=d.attackDown,needsFuel=false}}local function p(t)f("Refueling...",t)local u=t.fuels or e.defaultSettings.fuels local v=false for w=1,16 do local x=d.getItemDetail(w)if x~=nil then for y,z in pairs(u)do if x.name==z then d.select(i)if d.refuel(x.count)then v=true end break end end end end return v end local function q(t,u)p(u)while d.getFuelLeveld.getFuelLevel()))if w then local x=t.count q(x,u)end for x=1,t.count do v.f()end end end local function s(t,u)local v={}for w in string.gfind(t,"%W*(%d*%u%l*)%W*")do local x,y=string.find(w,"%d+")local z,A=string.find(w,"%u%l*")local B=1 if x~=nil then B=tonumber(string.sub(w,x,y))end local C=string.sub(w,z,A)if B<1 or B>d.getFuelLimit()then error("Instruction at index "..z.." has an invalid count of "..B..". It should be >= 1 and <= "..d.getFuelLimit())end if o[C]==nil then error("Instruction at index "..z..", \""..C.."\", does not refer to a valid action.")end table.insert(v,{action=C,count=B})f("Parsed instruction: "..w,u)end return v end function e.run(t,u)u=u or e.defaultSettings t=t or""f("Executing script: "..t,u)local v=s(t,u)for w,x in pairs(v)do r(x,u)end end function e.runFile(t,u)local v=fs.open(t,"r")local w=v.readAll()v.close()e.run(w,u)end return e \ No newline at end of file -- 2.34.1