proxy apis over wireless
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
local Blocks = require('blocks')
|
||||
local class = require('class')
|
||||
local Message = require('message')
|
||||
local Util = require('util')
|
||||
|
||||
@@ -6,7 +7,8 @@ local device = _G.device
|
||||
local fs = _G.fs
|
||||
local turtle = _G.turtle
|
||||
|
||||
local Builder = {
|
||||
local Builder = class()
|
||||
Util.merge(Builder, {
|
||||
isCommandComputer = not turtle,
|
||||
slots = { },
|
||||
loc = { },
|
||||
@@ -15,9 +17,7 @@ local Builder = {
|
||||
fuelItem = { id = 'minecraft:coal', dmg = 0 },
|
||||
resourceSlots = 14,
|
||||
facing = 'south',
|
||||
wrenchSucks = false,
|
||||
stairBug = false,
|
||||
}
|
||||
})
|
||||
|
||||
local BUILDER_DIR = 'usr/builder'
|
||||
|
||||
@@ -26,26 +26,6 @@ local blockInfo = Blocks()
|
||||
function Builder:getBlockCounts()
|
||||
local blocks = { }
|
||||
|
||||
-- add a couple essential items to the supply list to allow replacements
|
||||
if not self.isCommandComputer then
|
||||
local wrench = self.subDB:getSubstitutedItem('SubstituteAWrench', 0)
|
||||
wrench.qty = 0
|
||||
wrench.need = 1
|
||||
blocks[wrench.id .. ':' .. wrench.dmg] = wrench
|
||||
|
||||
local fuel = self.subDB:getSubstitutedItem(Builder.fuelItem.id, Builder.fuelItem.dmg)
|
||||
fuel.qty = 0
|
||||
fuel.need = 1
|
||||
blocks[fuel.id .. ':' .. fuel.dmg] = fuel
|
||||
|
||||
blocks['minecraft:piston:0'] = {
|
||||
id = 'minecraft:piston',
|
||||
dmg = 0,
|
||||
qty = 0,
|
||||
need = 1,
|
||||
}
|
||||
end
|
||||
|
||||
for k = self.index, #self.schematic.blocks do
|
||||
local b = self.schematic.blocks[k]
|
||||
local key = tostring(b.id) .. ':' .. b.dmg
|
||||
@@ -107,19 +87,19 @@ end
|
||||
function Builder:saveProgress(index)
|
||||
Util.writeTable(
|
||||
fs.combine(BUILDER_DIR, self.schematic.filename .. '.progress'),
|
||||
{ index = index, facing = Builder.facing, loc = Builder.loc }
|
||||
{ index = index, facing = self.facing, loc = self.loc }
|
||||
)
|
||||
end
|
||||
|
||||
function Builder:loadProgress(filename)
|
||||
local progress = Util.readTable(fs.combine(BUILDER_DIR, filename))
|
||||
if progress then
|
||||
Builder.index = progress.index
|
||||
if Builder.index > #self.schematic.blocks then
|
||||
Builder.index = 1
|
||||
self.index = progress.index
|
||||
if self.index > #self.schematic.blocks then
|
||||
self.index = 1
|
||||
end
|
||||
Builder.facing = progress.facing or 'south'
|
||||
Builder.loc = progress.loc or { }
|
||||
self.facing = progress.facing or 'south'
|
||||
self.loc = progress.loc or { }
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
local Adapter = require('inventoryAdapter')
|
||||
local Builder = require('builder.builder')
|
||||
local class = require('class')
|
||||
local Event = require('event')
|
||||
local itemDB = require('itemDB')
|
||||
local Message = require('message')
|
||||
local Point = require('point')
|
||||
local TableDB = require('tableDB')
|
||||
local UI = require('ui')
|
||||
local Util = require('util')
|
||||
|
||||
@@ -14,14 +15,7 @@ local read = _G.read
|
||||
local rs = _G.rs
|
||||
local turtle = _G.turtle
|
||||
|
||||
local BUILDER_DIR = 'usr/builder'
|
||||
|
||||
local pistonFacings
|
||||
|
||||
local ChestAdapter = require('chestAdapter')
|
||||
if Util.checkMinecraftVersion(1.8) then
|
||||
ChestAdapter = require('chestAdapter18')
|
||||
end
|
||||
local TurtleBuilder = class(Builder)
|
||||
|
||||
-- Temp functions until conversion to new adapters is complete
|
||||
local function convertSingleForward(item)
|
||||
@@ -92,10 +86,10 @@ local supplyPage = UI.Page {
|
||||
|
||||
function supplyPage:eventHandler(event)
|
||||
if event.type == 'build' then
|
||||
Builder:build()
|
||||
self.builder:build()
|
||||
|
||||
elseif event.type == 'menu' then
|
||||
Builder:dumpInventory()
|
||||
self.builder:dumpInventory()
|
||||
--Builder.status = 'idle'
|
||||
UI:setPage('start')
|
||||
turtle.status = 'idle'
|
||||
@@ -111,16 +105,15 @@ function supplyPage:eventHandler(event)
|
||||
return UI.Page.eventHandler(self, event)
|
||||
end
|
||||
|
||||
function supplyPage:enable()
|
||||
function supplyPage:enable(builder)
|
||||
self.builder = builder
|
||||
self.grid:setIndex(1)
|
||||
self.statusBar:setValue('fuel',
|
||||
string.format('Fuel: %dk', math.floor(turtle.getFuelLevel() / 1024)))
|
||||
-- self.statusBar:setValue('block',
|
||||
-- string.format('Block: %d', Builder.index))
|
||||
|
||||
Event.addNamedTimer('supplyRefresh', 6, true, function()
|
||||
if self.enabled then
|
||||
Builder:autocraft(Builder:getSupplies())
|
||||
self.builder:autocraft(self.builder:getSupplies())
|
||||
self:refresh()
|
||||
self.statusBar:timedStatus('Refreshed ', 2)
|
||||
self:sync()
|
||||
@@ -152,26 +145,41 @@ end
|
||||
|
||||
function supplyPage:refresh()
|
||||
self.statusBar:timedStatus('Refreshed ', 3)
|
||||
local supplies = Builder:getSupplies()
|
||||
local supplies = self.builder:getSupplies()
|
||||
if #supplies == 0 then
|
||||
Builder:build()
|
||||
self.builder:build()
|
||||
else
|
||||
self:setSupplies(supplies)
|
||||
self.grid:draw()
|
||||
end
|
||||
end
|
||||
|
||||
--[[-- maxStackDB --]]--
|
||||
local maxStackDB = TableDB({
|
||||
fileName = fs.combine(BUILDER_DIR, 'maxstack.db'),
|
||||
})
|
||||
--[[-- Builder --]]--
|
||||
function TurtleBuilder:getBlockCounts()
|
||||
local blocks = Builder.getBlockCounts(self)
|
||||
|
||||
function maxStackDB:get(id, dmg)
|
||||
return self.data[id .. ':' .. dmg] or 64
|
||||
-- add a couple essential items to the supply list to allow replacements
|
||||
local wrench = self.subDB:getSubstitutedItem('SubstituteAWrench', 0)
|
||||
wrench.qty = 0
|
||||
wrench.need = 1
|
||||
blocks[wrench.id .. ':' .. wrench.dmg] = wrench
|
||||
|
||||
local fuel = self.subDB:getSubstitutedItem(Builder.fuelItem.id, Builder.fuelItem.dmg)
|
||||
fuel.qty = 0
|
||||
fuel.need = 1
|
||||
blocks[fuel.id .. ':' .. fuel.dmg] = fuel
|
||||
|
||||
blocks['minecraft:piston:0'] = {
|
||||
id = 'minecraft:piston',
|
||||
dmg = 0,
|
||||
qty = 0,
|
||||
need = 1,
|
||||
}
|
||||
|
||||
return blocks
|
||||
end
|
||||
|
||||
--[[-- Builder --]]--
|
||||
function Builder:selectItem(id, dmg)
|
||||
function TurtleBuilder:selectItem(id, dmg)
|
||||
for k,s in ipairs(self.slots) do
|
||||
if s.qty > 0 and s.id == id and s.dmg == dmg then
|
||||
-- check to see if someone pulled items from inventory
|
||||
@@ -188,7 +196,7 @@ function Builder:selectItem(id, dmg)
|
||||
end
|
||||
end
|
||||
|
||||
function Builder:getAirResupplyList(blockIndex)
|
||||
function TurtleBuilder:getAirResupplyList(blockIndex)
|
||||
local slots = { }
|
||||
|
||||
if self.mode == 'destroy' then
|
||||
@@ -203,10 +211,10 @@ function Builder:getAirResupplyList(blockIndex)
|
||||
slots = self:getGenericSupplyList(blockIndex)
|
||||
end
|
||||
|
||||
local fuel = self.subDB:getSubstitutedItem(Builder.fuelItem.id, Builder.fuelItem.dmg)
|
||||
local fuel = self.subDB:getSubstitutedItem(self.fuelItem.id, self.fuelItem.dmg)
|
||||
|
||||
slots[15] = {
|
||||
id = 'minecraft:chest', --'ironchest:BlockIronChest', --
|
||||
id = 'minecraft:chest',
|
||||
dmg = 0,
|
||||
qty = 0,
|
||||
need = 1,
|
||||
@@ -216,7 +224,6 @@ function Builder:getAirResupplyList(blockIndex)
|
||||
slots[16] = {
|
||||
id = fuel.id,
|
||||
dmg = fuel.dmg,
|
||||
wrench = true,
|
||||
qty = 0,
|
||||
need = 64,
|
||||
index = 16,
|
||||
@@ -225,7 +232,7 @@ function Builder:getAirResupplyList(blockIndex)
|
||||
return slots
|
||||
end
|
||||
|
||||
function Builder:getSupplyList(blockIndex)
|
||||
function TurtleBuilder:getSupplyList(blockIndex)
|
||||
local slots, lastBlock = self:getGenericSupplyList(blockIndex)
|
||||
|
||||
slots[15] = {
|
||||
@@ -240,7 +247,6 @@ function Builder:getSupplyList(blockIndex)
|
||||
slots[16] = {
|
||||
id = wrench.id,
|
||||
dmg = wrench.dmg,
|
||||
wrench = true,
|
||||
qty = 0,
|
||||
need = 1,
|
||||
index = 16,
|
||||
@@ -251,7 +257,7 @@ function Builder:getSupplyList(blockIndex)
|
||||
return lastBlock
|
||||
end
|
||||
|
||||
function Builder:getGenericSupplyList(blockIndex)
|
||||
function TurtleBuilder:getGenericSupplyList(blockIndex)
|
||||
local slots = { }
|
||||
|
||||
for i = 1, self.resourceSlots do
|
||||
@@ -264,7 +270,7 @@ function Builder:getGenericSupplyList(blockIndex)
|
||||
|
||||
local function getSlot(id, dmg)
|
||||
-- find matching slot
|
||||
local maxStack = maxStackDB:get(id, dmg)
|
||||
local maxStack = itemDB:getMaxCount({ name = id, damage = dmg })
|
||||
for _, s in ipairs(slots) do
|
||||
if s.id == id and s.dmg == dmg and s.need < maxStack then
|
||||
return s
|
||||
@@ -304,7 +310,7 @@ function Builder:getGenericSupplyList(blockIndex)
|
||||
return slots, lastBlock
|
||||
end
|
||||
|
||||
function Builder:dumpInventory()
|
||||
function TurtleBuilder:dumpInventory()
|
||||
local success = true
|
||||
|
||||
for i = 1, 16 do
|
||||
@@ -321,7 +327,7 @@ function Builder:dumpInventory()
|
||||
return success
|
||||
end
|
||||
|
||||
function Builder:dumpInventoryWithCheck()
|
||||
function TurtleBuilder:dumpInventoryWithCheck()
|
||||
while not self:dumpInventory() do
|
||||
print('Storage is full or missing - make space or replace')
|
||||
print('Press enter to continue')
|
||||
@@ -330,7 +336,7 @@ function Builder:dumpInventoryWithCheck()
|
||||
end
|
||||
end
|
||||
|
||||
function Builder:autocraft(supplies)
|
||||
function TurtleBuilder:autocraft(supplies)
|
||||
local t = { }
|
||||
|
||||
for _,s in pairs(supplies) do
|
||||
@@ -350,7 +356,7 @@ function Builder:autocraft(supplies)
|
||||
self.itemAdapter:craftItems(convertForward(t))
|
||||
end
|
||||
|
||||
function Builder:getSupplies()
|
||||
function TurtleBuilder:getSupplies()
|
||||
self.itemAdapter:refresh()
|
||||
|
||||
local t = { }
|
||||
@@ -367,9 +373,6 @@ function Builder:getSupplies()
|
||||
local qty = math.min(s.need - s.qty, item.qty)
|
||||
|
||||
if qty + s.qty > item.max_size then
|
||||
maxStackDB:add({ s.id, s.dmg }, item.max_size)
|
||||
maxStackDB.dirty = true
|
||||
maxStackDB:flush()
|
||||
qty = item.max_size
|
||||
s.need = qty
|
||||
end
|
||||
@@ -389,7 +392,7 @@ function Builder:getSupplies()
|
||||
return t
|
||||
end
|
||||
|
||||
function Builder:refuel()
|
||||
function TurtleBuilder:refuel()
|
||||
while turtle.getFuelLevel() < 4000 do
|
||||
print('Refueling')
|
||||
turtle.select(1)
|
||||
@@ -408,7 +411,7 @@ function Builder:refuel()
|
||||
end
|
||||
end
|
||||
|
||||
function Builder:inAirDropoff()
|
||||
function TurtleBuilder:inAirDropoff()
|
||||
if not device.wireless_modem then
|
||||
return false
|
||||
end
|
||||
@@ -434,9 +437,9 @@ function Builder:inAirDropoff()
|
||||
turtle._goto(pt.x, pt.z, pt.y)
|
||||
os.sleep(.1) -- random computer is not connected error
|
||||
|
||||
local chestAdapter = ChestAdapter({ direction = 'down', wrapSide = 'top' })
|
||||
local chestAdapter = Adapter.wrap({ direction = 'down', wrapSide = 'top' })
|
||||
|
||||
if not chestAdapter:isValid() then
|
||||
if not chestAdapter then
|
||||
self:log('Chests above is not valid')
|
||||
return false
|
||||
end
|
||||
@@ -468,7 +471,7 @@ function Builder:inAirDropoff()
|
||||
end
|
||||
end
|
||||
|
||||
function Builder:inAirResupply()
|
||||
function TurtleBuilder:inAirResupply()
|
||||
if not device.wireless_modem then
|
||||
return false
|
||||
end
|
||||
@@ -496,9 +499,9 @@ function Builder:inAirResupply()
|
||||
turtle._goto(pt.x, pt.z, pt.y)
|
||||
os.sleep(.1) -- random computer is not connected error
|
||||
|
||||
local chestAdapter = ChestAdapter({ direction = 'down', wrapSide = 'top' })
|
||||
local chestAdapter = Adapter.wrap({ direction = 'down', wrapSide = 'top' })
|
||||
|
||||
if not chestAdapter:isValid() then
|
||||
if not chestAdapter then
|
||||
Util.print('not valid')
|
||||
read()
|
||||
end
|
||||
@@ -540,7 +543,7 @@ function Builder:inAirResupply()
|
||||
end
|
||||
end
|
||||
|
||||
function Builder:sendSupplyRequest(lastBlock)
|
||||
function TurtleBuilder:sendSupplyRequest(lastBlock)
|
||||
if device.wireless_modem then
|
||||
local slots = self:getAirResupplyList(lastBlock)
|
||||
self.slotUid = os.clock()
|
||||
@@ -577,32 +580,30 @@ local function closestEdgePoint(pt, pts, rpt)
|
||||
return cpt
|
||||
end
|
||||
|
||||
function Builder:getBuildingCorner()
|
||||
function TurtleBuilder:getBuildingCorner()
|
||||
local pts = {
|
||||
{ x = -1, z = -1, y = 0 },
|
||||
{ x = -1, z = self.schematic.length, y = 0 },
|
||||
{ x = self.schematic.width, z = -1, y = 0 },
|
||||
{ x = self.schematic.width, z = self.schematic.length, y = 0 },
|
||||
}
|
||||
-- local supplyPoint = Util.shallowCopy(self.supplyPoint)
|
||||
-- supplyPoint.heading = turtle.point.heading
|
||||
return closestEdgePoint(self.supplyPoint, pts, turtle.point)
|
||||
return closestEdgePoint(self.supplyPoint, pts, turtle.getPoint())
|
||||
end
|
||||
|
||||
function Builder:gotoSupplyPoint()
|
||||
if not Point.same(turtle.point, self.supplyPoint) then
|
||||
function TurtleBuilder:gotoSupplyPoint()
|
||||
if not Point.same(turtle.getPoint(), self.supplyPoint) then
|
||||
-- so we don't end up pathfinding through a building
|
||||
-- go to the corner closest to the supplies point
|
||||
-- pathfind the rest of the way
|
||||
local pt = self:getBuildingCorner()
|
||||
turtle._goto(pt.x, pt.z)
|
||||
turtle.setPolicy(turtle.policies.none)
|
||||
turtle.setPolicy('none')
|
||||
turtle.pathfind(self.supplyPoint)
|
||||
os.sleep(.1) -- random 'Computer is not connected' error...
|
||||
end
|
||||
end
|
||||
|
||||
function Builder:resupply()
|
||||
function TurtleBuilder:resupply()
|
||||
if self.slotUid and self:inAirResupply() then
|
||||
os.queueEvent('build')
|
||||
return
|
||||
@@ -629,29 +630,29 @@ function Builder:resupply()
|
||||
turtle.setHeading(0)
|
||||
self:autocraft(supplies)
|
||||
supplyPage:setSupplies(supplies)
|
||||
UI:setPage(supplyPage)
|
||||
UI:setPage(supplyPage, self)
|
||||
end
|
||||
end
|
||||
|
||||
function Builder:placeDown(slot)
|
||||
function TurtleBuilder:placeDown(slot)
|
||||
return turtle.placeDown(slot.index)
|
||||
end
|
||||
|
||||
function Builder:placeUp(slot)
|
||||
function TurtleBuilder:placeUp(slot)
|
||||
return turtle.placeUp(slot.index)
|
||||
end
|
||||
|
||||
function Builder:place(slot)
|
||||
function TurtleBuilder:place(slot)
|
||||
return turtle.place(slot.index)
|
||||
end
|
||||
|
||||
function Builder:getWrenchSlot()
|
||||
function TurtleBuilder:getWrenchSlot()
|
||||
local wrench = self.subDB:getSubstitutedItem('SubstituteAWrench', 0)
|
||||
return self:selectItem(wrench.id, wrench.dmg)
|
||||
end
|
||||
|
||||
-- figure out our orientation in the world
|
||||
function Builder:getTurtleFacing()
|
||||
function TurtleBuilder:getTurtleFacing()
|
||||
local directions = { -- reversed directions
|
||||
[5] = 'west',
|
||||
[3] = 'north',
|
||||
@@ -699,14 +700,14 @@ function Builder:getTurtleFacing()
|
||||
self.facing = directions[bi2.metadata]
|
||||
end
|
||||
|
||||
function Builder:wrenchBlock(side, facing, cache)
|
||||
function TurtleBuilder:wrenchBlock(side, facing, cache)
|
||||
local s = self:getWrenchSlot()
|
||||
|
||||
if not s then
|
||||
return false
|
||||
end
|
||||
|
||||
local key = turtle.point.heading .. '-' .. facing
|
||||
local key = turtle.getPoint().heading .. '-' .. facing
|
||||
if cache then
|
||||
local count = cache[side][key]
|
||||
|
||||
@@ -752,7 +753,7 @@ function Builder:wrenchBlock(side, facing, cache)
|
||||
return false
|
||||
end
|
||||
|
||||
function Builder:rotateBlock(side, facing)
|
||||
function TurtleBuilder:rotateBlock(side, facing)
|
||||
local s = self:getWrenchSlot()
|
||||
|
||||
if not s then
|
||||
@@ -767,7 +768,7 @@ function Builder:rotateBlock(side, facing)
|
||||
end
|
||||
|
||||
-- place piston, wrench piston to face downward, extend, remove piston
|
||||
function Builder:placePiston(b)
|
||||
function TurtleBuilder:placePiston(b)
|
||||
local ps = self:selectItem('minecraft:piston', 0)
|
||||
local ws = self:getWrenchSlot()
|
||||
|
||||
@@ -789,7 +790,8 @@ function Builder:placePiston(b)
|
||||
turtle.turnLeft()
|
||||
end
|
||||
|
||||
local success = self:wrenchBlock('forward', 'down', pistonFacings) --wrench piston to point downwards
|
||||
--wrench piston to point downwards
|
||||
local success = self:wrenchBlock('forward', 'down', self.pistonFacings)
|
||||
|
||||
rs.setOutput('front', true)
|
||||
os.sleep(.25)
|
||||
@@ -806,7 +808,7 @@ function Builder:placePiston(b)
|
||||
return success
|
||||
end
|
||||
|
||||
function Builder:_goto(x, z, y, heading)
|
||||
function TurtleBuilder:_goto(x, z, y, heading)
|
||||
if not turtle._goto(x, z, y, heading) then
|
||||
print('stuck')
|
||||
print('Press enter to continue')
|
||||
@@ -818,14 +820,15 @@ end
|
||||
|
||||
-- goto used when turtle could be below travel plane
|
||||
-- if the distance is no more than 1 block, there's no need to pop back to the travel plane
|
||||
function Builder:gotoEx(x, z, y, h, travelPlane)
|
||||
local distance = math.abs(turtle.getPoint().x - x) + math.abs(turtle.getPoint().z - z)
|
||||
function TurtleBuilder:gotoEx(x, z, y, h, travelPlane)
|
||||
local pt = turtle.getPoint()
|
||||
local distance = math.abs(pt.x - x) + math.abs(pt.z - z)
|
||||
|
||||
-- following code could be better
|
||||
if distance == 0 then
|
||||
turtle.gotoY(y)
|
||||
elseif distance == 1 then
|
||||
if turtle.point.y < y then
|
||||
if pt.y < y then
|
||||
turtle.gotoY(y)
|
||||
end
|
||||
elseif distance > 1 then
|
||||
@@ -834,7 +837,7 @@ function Builder:gotoEx(x, z, y, h, travelPlane)
|
||||
self:_goto(x, z, y, h)
|
||||
end
|
||||
|
||||
function Builder:placeDirectionalBlock(b, slot, travelPlane)
|
||||
function TurtleBuilder:placeDirectionalBlock(b, slot, travelPlane)
|
||||
local d = b.direction
|
||||
|
||||
local function getAdjacentPoint(pt, direction)
|
||||
@@ -912,12 +915,13 @@ function Builder:placeDirectionalBlock(b, slot, travelPlane)
|
||||
self:gotoEx(b.x, b.z, b.y, (turtle.getHeadingInfo(stairUpDirections[d]).heading + 2) % 4, travelPlane)
|
||||
if self:placeUp(slot) then
|
||||
turtle.goback()
|
||||
turtle.gotoY(turtle.point.y + 2)
|
||||
turtle.gotoY(turtle.getPoint().y + 2)
|
||||
b.placed = self:placePiston(b)
|
||||
turtle.down()
|
||||
b.placed = self:placePiston(b)
|
||||
|
||||
b.heading = turtle.point.heading -- stop debug message below since we are pointing in wrong direction
|
||||
-- stop debug message below since we are pointing in wrong direction
|
||||
b.heading = turtle.getPoint().heading
|
||||
end
|
||||
else
|
||||
local hi = turtle.getHeadingInfo(stairUpDirections[d])
|
||||
@@ -961,7 +965,7 @@ function Builder:placeDirectionalBlock(b, slot, travelPlane)
|
||||
if pistonDirections[d] then
|
||||
-- why are pistons so broke in cc 1.7 ??????????????????????
|
||||
|
||||
local ws = Builder:getWrenchSlot()
|
||||
local ws = self:getWrenchSlot()
|
||||
|
||||
if not ws then
|
||||
b.needResupply = true
|
||||
@@ -980,12 +984,12 @@ function Builder:placeDirectionalBlock(b, slot, travelPlane)
|
||||
self:gotoEx(b.x, b.z, b.y, nil, travelPlane)
|
||||
|
||||
local heading = rotatedPistonDirections[d]
|
||||
if heading and turtle.point.heading % 2 ~= heading % 2 then
|
||||
if heading and turtle.getPoint().heading % 2 ~= heading % 2 then
|
||||
turtle.setHeading(heading)
|
||||
end
|
||||
|
||||
if self:placeDown(slot) then
|
||||
b.placed = self:wrenchBlock('down', pistonDirections[d], pistonFacings)
|
||||
b.placed = self:wrenchBlock('down', pistonDirections[d], self.pistonFacings)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1051,7 +1055,7 @@ end
|
||||
end
|
||||
|
||||
-- find the highest y in the last 2 planes
|
||||
function Builder:findTravelPlane(index)
|
||||
function TurtleBuilder:findTravelPlane(index)
|
||||
local travelPlane
|
||||
|
||||
for i = index, 1, -1 do
|
||||
@@ -1071,13 +1075,13 @@ function Builder:findTravelPlane(index)
|
||||
return travelPlane or 0
|
||||
end
|
||||
|
||||
function Builder:gotoTravelPlane(travelPlane)
|
||||
if travelPlane > turtle.point.y then
|
||||
function TurtleBuilder:gotoTravelPlane(travelPlane)
|
||||
if travelPlane > turtle.getPoint().y then
|
||||
turtle.gotoY(travelPlane)
|
||||
end
|
||||
end
|
||||
|
||||
function Builder:build()
|
||||
function TurtleBuilder:build()
|
||||
local direction = 1
|
||||
local last = #self.schematic.blocks
|
||||
local travelPlane = 0
|
||||
@@ -1095,7 +1099,7 @@ function Builder:build()
|
||||
|
||||
local pt = self:getBuildingCorner()
|
||||
turtle.pathfind({ x = pt.x, z = pt.z, y = travelPlane })
|
||||
turtle.setPolicy(turtle.policies.digAttack)
|
||||
turtle.setPolicy('digAttack')
|
||||
|
||||
for i = self.index, last, direction do
|
||||
self.index = i
|
||||
@@ -1204,30 +1208,28 @@ function Builder:build()
|
||||
Event.exitPullEvents()
|
||||
end
|
||||
|
||||
function Builder:begin()
|
||||
function TurtleBuilder:begin()
|
||||
if self.loc.x then
|
||||
self.supplyPoint = {
|
||||
x = Builder.loc.x - Builder.loc.rx - 1,
|
||||
y = Builder.loc.y - Builder.loc.ry,
|
||||
z = Builder.loc.z - Builder.loc.rz - 1,
|
||||
x = self.loc.x - self.loc.rx - 1,
|
||||
y = self.loc.y - self.loc.ry,
|
||||
z = self.loc.z - self.loc.rz - 1,
|
||||
}
|
||||
else
|
||||
self.supplyPoint = { x = -1, y = 0, z = -1 }
|
||||
end
|
||||
|
||||
turtle.reset()
|
||||
self:dumpInventory()
|
||||
self:refuel()
|
||||
self:getTurtleFacing()
|
||||
|
||||
if self.mode == 'build' then
|
||||
self:getTurtleFacing()
|
||||
end
|
||||
|
||||
local facing = turtle.getHeadingInfo(Builder.facing).heading
|
||||
local facing = turtle.getHeadingInfo(self.facing).heading
|
||||
Point.rotate(self.supplyPoint, facing)
|
||||
turtle.setPoint(self.supplyPoint)
|
||||
|
||||
-- reset piston cache in case wrench was substituted
|
||||
pistonFacings = {
|
||||
self.pistonFacings = {
|
||||
down = { },
|
||||
forward = { },
|
||||
}
|
||||
@@ -1244,10 +1246,4 @@ function Builder:begin()
|
||||
end
|
||||
end
|
||||
|
||||
maxStackDB:load()
|
||||
|
||||
Event.on('build', function()
|
||||
Builder:build()
|
||||
end)
|
||||
|
||||
return Builder
|
||||
return TurtleBuilder
|
||||
|
||||
24
apis/inventoryAdapter.lua
Normal file
24
apis/inventoryAdapter.lua
Normal file
@@ -0,0 +1,24 @@
|
||||
local ChestAdapter = require('chestAdapter')
|
||||
local ChestAdapter18 = require('chestAdapter18')
|
||||
local MEAdapter = require('meAdapter')
|
||||
|
||||
local Adapter = { }
|
||||
|
||||
function Adapter.wrap(args)
|
||||
local adapter = ChestAdapter18(args)
|
||||
if adapter:isValid() then
|
||||
return adapter
|
||||
end
|
||||
|
||||
adapter = MEAdapter(args)
|
||||
if adapter:isValid() then
|
||||
return adapter
|
||||
end
|
||||
|
||||
adapter = ChestAdapter(args)
|
||||
if adapter:isValid() then
|
||||
return adapter
|
||||
end
|
||||
end
|
||||
|
||||
return Adapter
|
||||
@@ -9,7 +9,6 @@ function itemDB:makeKey(item)
|
||||
end
|
||||
|
||||
function itemDB:splitKey(key, item)
|
||||
|
||||
item = item or { }
|
||||
|
||||
local t = Util.split(key, '(.-):')
|
||||
@@ -23,7 +22,6 @@ function itemDB:splitKey(key, item)
|
||||
end
|
||||
|
||||
function itemDB:get(key)
|
||||
|
||||
local item = TableDB.get(self, key)
|
||||
|
||||
if item then
|
||||
@@ -42,7 +40,6 @@ function itemDB:get(key)
|
||||
end
|
||||
|
||||
function itemDB:add(key, item)
|
||||
|
||||
if item.maxDamage > 0 then
|
||||
key = { key[1], 0, key[3] }
|
||||
end
|
||||
@@ -51,7 +48,6 @@ end
|
||||
|
||||
-- Accepts: "minecraft:stick:0" or { name = 'minecraft:stick', damage = 0 }
|
||||
function itemDB:getName(item)
|
||||
|
||||
if type(item) == 'string' then
|
||||
item = self:splitKey(item)
|
||||
end
|
||||
@@ -65,8 +61,20 @@ function itemDB:getName(item)
|
||||
return nameDB:getName(item.name .. ':' .. item.damage)
|
||||
end
|
||||
|
||||
function itemDB:load()
|
||||
function itemDB:getMaxCount(item)
|
||||
if type(item) == 'string' then
|
||||
item = self:splitKey(item)
|
||||
end
|
||||
|
||||
local detail = self:get(self:makeKey(item))
|
||||
if detail then
|
||||
return detail.maxCount
|
||||
end
|
||||
|
||||
return 64
|
||||
end
|
||||
|
||||
function itemDB:load()
|
||||
TableDB.load(self)
|
||||
|
||||
for key,item in pairs(self.data) do
|
||||
|
||||
@@ -4,10 +4,10 @@ end
|
||||
|
||||
_G.requireInjector()
|
||||
|
||||
local Adapter = require('inventoryAdapter')
|
||||
local Event = require('event')
|
||||
local GPS = require('gps')
|
||||
local itemDB = require('itemDB')
|
||||
local MEAdapter = require('meAdapter')
|
||||
local Schematic = require('schematic')
|
||||
local TableDB = require('tableDB')
|
||||
local UI = require('ui')
|
||||
@@ -18,11 +18,6 @@ local fs = _G.fs
|
||||
local multishell = _ENV.multishell
|
||||
local turtle = _G.turtle
|
||||
|
||||
local ChestAdapter = require('chestAdapter')
|
||||
if Util.checkMinecraftVersion(1.8) then
|
||||
ChestAdapter = require('chestAdapter18')
|
||||
end
|
||||
|
||||
local BUILDER_DIR = 'usr/builder'
|
||||
|
||||
local substitutionPage
|
||||
@@ -34,25 +29,9 @@ else
|
||||
Builder = require('builder.turtle')
|
||||
end
|
||||
|
||||
Builder = Builder()
|
||||
Builder.schematic = Schematic()
|
||||
|
||||
-- Temp functions until conversion to new adapters is complete
|
||||
local function convertSingleForward(item)
|
||||
item.displayName = item.display_name
|
||||
item.name = item.id
|
||||
item.damage = item.dmg
|
||||
item.count = item.qty
|
||||
item.maxCount = item.max_size
|
||||
return item
|
||||
end
|
||||
|
||||
local function convertForward(t)
|
||||
for _,v in pairs(t) do
|
||||
convertSingleForward(v)
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
local function convertSingleBack(item)
|
||||
if item then
|
||||
item.id = item.name
|
||||
@@ -283,10 +262,6 @@ function substitutionPage:enable()
|
||||
UI.Page.enable(self)
|
||||
end
|
||||
|
||||
--function substitutionPage:focusFirst()
|
||||
-- self.menuBar.filter:focus()
|
||||
--end
|
||||
|
||||
function substitutionPage:applySubstitute(id, dmg)
|
||||
self.sub.sid = id
|
||||
self.sub.sdmg = dmg
|
||||
@@ -317,7 +292,6 @@ function substitutionPage:eventHandler(event)
|
||||
end
|
||||
end
|
||||
end
|
||||
--self.grid:adjustWidth()
|
||||
self.grid:update()
|
||||
self.grid:setIndex(1)
|
||||
self.grid:draw()
|
||||
@@ -766,12 +740,9 @@ if #args < 1 then
|
||||
error('supply file name')
|
||||
end
|
||||
|
||||
Builder.itemAdapter = MEAdapter()
|
||||
if not Builder.itemAdapter:isValid() then
|
||||
Builder.itemAdapter = ChestAdapter()
|
||||
if not Builder.itemAdapter:isValid() then
|
||||
error('A chest or ME interface must be below turtle')
|
||||
end
|
||||
Builder.itemAdapter = Adapter.wrap()
|
||||
if not Builder.itemAdapter then
|
||||
error('A chest or ME interface must be below turtle')
|
||||
end
|
||||
|
||||
multishell.setTitle(multishell.getCurrent(), 'Builder')
|
||||
@@ -792,6 +763,10 @@ end
|
||||
|
||||
Builder:loadProgress(Builder.schematic.filename .. '.progress')
|
||||
|
||||
Event.on('build', function()
|
||||
Builder:build()
|
||||
end)
|
||||
|
||||
UI:setPages({
|
||||
listing = listingPage,
|
||||
start = startPage,
|
||||
@@ -803,8 +778,9 @@ UI:setPage('start')
|
||||
if Builder.isCommandComputer then
|
||||
UI:pullEvents()
|
||||
else
|
||||
turtle.run(function()
|
||||
turtle.point.heading = 0
|
||||
UI:pullEvents()
|
||||
end)
|
||||
UI:pullEvents()
|
||||
-- turtle.run(function()
|
||||
-- turtle.reset()
|
||||
-- UI:pullEvents()
|
||||
-- end)
|
||||
end
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
_G.requireInjector()
|
||||
|
||||
local Socket = require('socket')
|
||||
|
||||
if not ... then
|
||||
local turtle = _G.turtle
|
||||
|
||||
while true do
|
||||
print('hijack: waiting for connections')
|
||||
local socket = Socket.server(188)
|
||||
|
||||
print('hijack: connection from ' .. socket.dhost)
|
||||
|
||||
local methods = { }
|
||||
for k,v in pairs(turtle) do
|
||||
if type(v) == 'function' then
|
||||
table.insert(methods, k)
|
||||
end
|
||||
end
|
||||
socket:write(methods)
|
||||
|
||||
while true do
|
||||
local data = socket:read()
|
||||
if not data then
|
||||
break
|
||||
end
|
||||
socket:write({ turtle[data.fn](unpack(data.args)) })
|
||||
end
|
||||
end
|
||||
|
||||
else
|
||||
local remoteId = ({ ... })[1]
|
||||
local socket, msg = Socket.connect(remoteId, 188)
|
||||
|
||||
if not socket then
|
||||
error(msg)
|
||||
end
|
||||
|
||||
local methods = socket:read()
|
||||
|
||||
local turtle = { }
|
||||
for _,method in pairs(methods) do
|
||||
turtle[method] = function(...)
|
||||
socket:write({ fn = method, args = { ... } })
|
||||
local resp = socket:read()
|
||||
return table.unpack(resp)
|
||||
end
|
||||
end
|
||||
|
||||
_G.turtle = turtle
|
||||
os.pullEventRaw('terminate')
|
||||
socket:close()
|
||||
end
|
||||
Reference in New Issue
Block a user