feat: create utils lib

This commit is contained in:
Guillaume ARM 2024-05-20 20:26:43 +02:00
parent b1c3eac1b9
commit e9ebf63c55
3 changed files with 53 additions and 47 deletions

View File

@ -1,3 +1,4 @@
local utils = require('libs/utils')
local turtleUtils = require('libs/turtle-utils') local turtleUtils = require('libs/turtle-utils')
local config = require('config/harvesting') local config = require('config/harvesting')
@ -27,38 +28,6 @@ local localPlan = nil
-- UTILS -- UTILS
local function sizeof(t)
if not t then
return 0
end
local size = 0
for k,v in pairs(t) do
size = size + 1
end
return size
end
function difference(a, b)
local aa = {}
for k,v in pairs(a) do aa[v]=true end
for k,v in pairs(b) do aa[v]=nil end
local ret = {}
local n = 0
for k,v in pairs(a) do
if aa[v] then n=n+1 ret[n]=v end
end
return ret
end
local function shallowClone(t)
local res = {}
for k,v in pairs(t) do
res[k] = v
end
return res
end
local function removeFirst(t, x) local function removeFirst(t, x)
local res = {} local res = {}
local removed = false local removed = false
@ -74,14 +43,6 @@ local function removeFirst(t, x)
return res return res
end end
local function find(t, predicate)
for k,v in pairs(t) do
if predicate(v, k) then
return v, k
end
end
end
local function getIndexedCount(t) local function getIndexedCount(t)
local res = {} local res = {}
@ -324,7 +285,7 @@ local function removeSeeds(seeds)
local ok, block = turtle.inspectDown() local ok, block = turtle.inspectDown()
local blockName = block and block.name local blockName = block and block.name
local found = find(stateSeeds, function(seedName) return seedName == getSeedNameFromCropName(blockName) end) local found = utils.find(stateSeeds, function(seedName) return seedName == getSeedNameFromCropName(blockName) end)
if found then if found then
local digOk = turtle.digDown() local digOk = turtle.digDown()
@ -424,18 +385,18 @@ local function replantProcedure()
error('cannot fetch the remote plan') error('cannot fetch the remote plan')
end end
local seedsToRemove = difference(localPlan, remotePlan) local seedsToRemove = utils.difference(localPlan, remotePlan)
local seedsToPlant = difference(remotePlan, localPlan) local seedsToPlant = utils.difference(remotePlan, localPlan)
if sizeof(seedsToRemove) > 0 then if utils.sizeof(seedsToRemove) > 0 then
print('nb seeds to remove: ' .. sizeof(seedsToRemove)) print('nb seeds to remove: ' .. utils.sizeof(seedsToRemove))
removeSeeds(seedsToRemove) removeSeeds(seedsToRemove)
else else
print('no seeds to remove') print('no seeds to remove')
end end
if sizeof(seedsToPlant) > 0 then if utils.sizeof(seedsToPlant) > 0 then
print('nb seeds to plants: ' .. sizeof(seedsToPlant)) print('nb seeds to plants: ' .. utils.sizeof(seedsToPlant))
retrieveSeeds(seedsToPlant) retrieveSeeds(seedsToPlant)
replantSeeds() replantSeeds()
else else

View File

@ -7,6 +7,7 @@ local LIST_FILES = {
}; };
local LIST_LIBS_FILES = { local LIST_LIBS_FILES = {
'libs/utils.lua',
'libs/turtle-utils.lua', 'libs/turtle-utils.lua',
'libs/robot.lua' 'libs/robot.lua'
} }

44
libs/utils.lua Normal file
View File

@ -0,0 +1,44 @@
local utils = {}
utils.sizeof = function(t)
if not t then
return 0
end
local size = 0
for k,v in pairs(t) do
size = size + 1
end
return size
end
utils.difference = function(a, b)
local aa = {}
for k,v in pairs(a) do aa[v]=true end
for k,v in pairs(b) do aa[v]=nil end
local ret = {}
local n = 0
for k,v in pairs(a) do
if aa[v] then n=n+1 ret[n]=v end
end
return ret
end
utils.shallowClone = function(t)
local res = {}
for k,v in pairs(t) do
res[k] = v
end
return res
end
utils.find = function(t, predicate)
for k,v in pairs(t) do
if predicate(v, k) then
return v, k
end
end
end
return utils