rework turtle policies

This commit is contained in:
kepler155c@gmail.com 2019-01-17 13:31:05 -05:00
parent eabfde64e9
commit 644fd0352f
3 changed files with 111 additions and 101 deletions

View File

@ -19,7 +19,7 @@ if turtle and modem then
s = turtle.enableGPS(2) s = turtle.enableGPS(2)
end end
if not s and config.destructive then if not s and config.destructive then
turtle.setPolicy('turtleSafe') turtle.set({ attackPolicy = 'attack', digPolicy = 'turtleSafe' })
s = turtle.enableGPS(2) s = turtle.enableGPS(2)
end end
@ -28,7 +28,7 @@ if turtle and modem then
end end
if config.destructive then if config.destructive then
turtle.setPolicy('turtleSafe') turtle.set({ attackPolicy = 'attack', digPolicy = 'turtleSafe' })
end end
if not turtle.pathfind(config.home) then if not turtle.pathfind(config.home) then

View File

@ -103,6 +103,11 @@
\030 \031f\030f\0310\136\140\140\030 ", \030 \031f\030f\0310\136\140\140\030 ",
run = "https://gist.github.com/LDDestroier/c7528d95bc0103545c2a/raw", run = "https://gist.github.com/LDDestroier/c7528d95bc0103545c2a/raw",
}, },
[ "785af2a4ad3c4ee912623c6e0b6d4299ea305bf6" ] = {
title = "Pipes",
category = "Games",
run = "https://pastebin.com/raw/skcs9x1s",
},
[ "53a5d150062b1e03206b9e15854b81060e3c7552" ] = { [ "53a5d150062b1e03206b9e15854b81060e3c7552" ] = {
title = "Minesweeper", title = "Minesweeper",
category = "Games", category = "Games",

View File

@ -2,8 +2,6 @@ if not _G.turtle then
return return
end end
_G.requireInjector(_ENV)
local Pathing = require('turtle.pathfind') local Pathing = require('turtle.pathfind')
local GPS = require('gps') local GPS = require('gps')
local Point = require('point') local Point = require('point')
@ -69,8 +67,6 @@ function turtle.reset()
return true return true
end end
turtle.reset()
local function _dig(name, inspect, dig) local function _dig(name, inspect, dig)
if name then if name then
local s, b = inspect() local s, b = inspect()
@ -158,6 +154,24 @@ if type(turtle.getFuelLevel()) ~= 'number' then
end end
end end
-- [[ Policies ]] --
turtle.policies = { }
function turtle.addPolicy(name, policy)
turtle.policies[name] = policy
end
function turtle.getPolicy(policy)
if type(policy) == 'function' then
return policy
end
local p = turtle.policies[policy]
if not p then
error('Invalid policy: ' .. tostring(policy))
end
return p
end
-- [[ Basic turtle actions ]] -- -- [[ Basic turtle actions ]] --
local function inventoryAction(fn, name, qty) local function inventoryAction(fn, name, qty)
local slots = turtle.getFilledSlots() local slots = turtle.getFilledSlots()
@ -191,18 +205,15 @@ local function _attack(action)
return false return false
end end
turtle.attackPolicies = {
none = noop,
attack = function(action)
return _attack(action)
end,
}
function turtle.attack() return _attack(actions.forward) end function turtle.attack() return _attack(actions.forward) end
function turtle.attackUp() return _attack(actions.up) end function turtle.attackUp() return _attack(actions.up) end
function turtle.attackDown() return _attack(actions.down) end function turtle.attackDown() return _attack(actions.down) end
turtle.addPolicy('attackNone', noop)
turtle.addPolicy('attack', function(action)
return _attack(action)
end)
function turtle.setAttackPolicy(policy) state.attackPolicy = policy end function turtle.setAttackPolicy(policy) state.attackPolicy = policy end
-- [[ Place ]] -- -- [[ Place ]] --
@ -240,6 +251,7 @@ function turtle.place(slot) return _place(actions.forward, slot) end
function turtle.placeUp(slot) return _place(actions.up, slot) end function turtle.placeUp(slot) return _place(actions.up, slot) end
function turtle.placeDown(slot) return _place(actions.down, slot) end function turtle.placeDown(slot) return _place(actions.down, slot) end
-- [[ Drop ]] --
local function _drop(action, qtyOrName, qty) local function _drop(action, qtyOrName, qty)
if not qtyOrName or type(qtyOrName) == 'number' then if not qtyOrName or type(qtyOrName) == 'number' then
return action.drop(qtyOrName or 64) return action.drop(qtyOrName or 64)
@ -251,6 +263,64 @@ function turtle.drop(count, slot) return _drop(actions.forward, count, slot)
function turtle.dropUp(count, slot) return _drop(actions.up, count, slot) end function turtle.dropUp(count, slot) return _drop(actions.up, count, slot) end
function turtle.dropDown(count, slot) return _drop(actions.down, count, slot) end function turtle.dropDown(count, slot) return _drop(actions.down, count, slot) end
-- [[ Dig ]] --
turtle.addPolicy('digNone', noop)
turtle.addPolicy('dig', function(action)
return action.dig()
end)
turtle.addPolicy('turtleSafe', function(action)
if action.side == 'back' then
return false
end
if not turtle.isTurtleAtSide(action.side) then
return action.dig()
end
return Util.tryTimes(6, function()
os.sleep(.25)
if not action.detect() then
return true
end
end)
end)
turtle.addPolicy('digAndDrop', function(action)
if action.detect() then
local slots = turtle.getInventory()
if action.dig() then
turtle.reconcileInventory(slots)
return true
end
end
return false
end)
function turtle.setDigPolicy(policy) state.digPolicy = policy end
-- [[ Move ]] --
turtle.addPolicy('moveNone', noop)
turtle.addPolicy('moveDefault', _defaultMove)
turtle.addPolicy('moveAssured', function(action)
if not _defaultMove(action) then
if action.side == 'back' then
return false
end
local oldStatus = state.status
print('assured move: stuck')
state.status = 'stuck'
repeat
os.sleep(1)
until _defaultMove(action)
state.status = oldStatus
end
return true
end)
function turtle.setMoveCallback(cb) state.moveCallback = cb end
function turtle.clearMoveCallback() state.moveCallback = noop end
function turtle.getMoveCallback() return state.moveCallback end
function turtle.refuel(qtyOrName, qty) function turtle.refuel(qtyOrName, qty)
if not qtyOrName or type(qtyOrName) == 'number' then if not qtyOrName or type(qtyOrName) == 'number' then
return turtle.native.refuel(qtyOrName or 64) return turtle.native.refuel(qtyOrName or 64)
@ -263,104 +333,36 @@ function turtle.isTurtleAtSide(side)
return sideType and sideType == 'turtle' return sideType and sideType == 'turtle'
end end
turtle.digPolicies = { function turtle.set(args)
none = noop, for k,v in pairs(args) do
dig = function(action) if k == 'attackPolicy' then
return action.dig() turtle.setAttackPolicy(turtle.getPolicy(v))
end,
turtleSafe = function(action) elseif k == 'digPolicy' then
if action.side == 'back' then turtle.setDigPolicy(turtle.getPolicy(v))
return false
end
if not turtle.isTurtleAtSide(action.side) then
return action.dig()
end
return Util.tryTimes(6, function()
-- if not turtle.isTurtleAtSide(action.side) then
-- return true --action.dig()
-- end
os.sleep(.25)
if not action.detect() then
return true
end
end)
end,
digAndDrop = function(action) elseif k == 'movePolicy' then
if action.detect() then turtle.setMovePolicy(turtle.getPolicy(v))
local slots = turtle.getInventory()
if action.dig() then
turtle.reconcileInventory(slots)
return true
end
end
return false
end
}
turtle.movePolicies = { elseif k == 'movementStrategy' then
none = noop, turtle.setMovementStrategy(v)
default = _defaultMove,
assured = function(action)
if not _defaultMove(action) then
if action.side == 'back' then
return false
end
local oldStatus = state.status
print('assured move: stuck')
state.status = 'stuck'
repeat
os.sleep(1)
until _defaultMove(action)
state.status = oldStatus
end
return true
end,
}
turtle.policies = { elseif k == 'pathingBox' then
none = { dig = turtle.digPolicies.none, attack = turtle.attackPolicies.none }, turtle.setPathingBox(v)
digOnly = { dig = turtle.digPolicies.dig, attack = turtle.attackPolicies.none },
attackOnly = { dig = turtle.digPolicies.none, attack = turtle.attackPolicies.attack },
digAttack = { dig = turtle.digPolicies.dig, attack = turtle.attackPolicies.attack },
turtleSafe = { dig = turtle.digPolicies.turtleSafe, attack = turtle.attackPolicies.attack },
attack = { attack = turtle.attackPolicies.attack }, elseif k == 'point' then
turtle.setPoint(v)
defaultMove = { move = turtle.movePolicies.default }, elseif k == 'moveCallback' then
assuredMove = { move = turtle.movePolicies.assured }, turtle.setMoveCallback(v)
}
function turtle.setPolicy(...) else
local args = { ... } error('Invalid turle.set: ' .. tostring(k))
for _, policy in pairs(args) do
if type(policy) == 'string' then
policy = turtle.policies[policy]
end
if not policy then
error('Invalid policy')
-- return false, 'Invalid policy'
end
if policy.dig then
state.digPolicy = policy.dig
end
if policy.attack then
state.attackPolicy = policy.attack
end
if policy.move then
state.movePolicy = policy.move
end end
end end
return true
end end
function turtle.setDigPolicy(policy) state.digPolicy = policy end
function turtle.setMoveCallback(cb) state.moveCallback = cb end
function turtle.clearMoveCallback() state.moveCallback = noop end
function turtle.getMoveCallback() return state.moveCallback end
-- [[ Heading ]] -- -- [[ Heading ]] --
function turtle.getHeading() function turtle.getHeading()
return turtle.point.heading return turtle.point.heading
@ -732,7 +734,7 @@ function turtle.gotoY(dy)
return true return true
end end
-- [[ Slot management ]] -- -- [[ Inventory ]] --
function turtle.getSlot(indexOrId, slots) function turtle.getSlot(indexOrId, slots)
if type(indexOrId) == 'string' then if type(indexOrId) == 'string' then
slots = slots or turtle.getInventory() slots = slots or turtle.getInventory()
@ -936,6 +938,7 @@ function turtle.getItemCount(idOrName)
return count return count
end end
-- [[ Equipment ]] --
function turtle.equip(side, item) function turtle.equip(side, item)
if item then if item then
if not turtle.select(item) then if not turtle.select(item) then
@ -1235,3 +1238,5 @@ function turtle.addFeatures(...)
require('turtle.' .. feature) require('turtle.' .. feature)
end end
end end
turtle.reset()