refactor + cleanup

This commit is contained in:
kepler155c
2017-10-27 20:25:16 -04:00
parent 9939c75fc3
commit d97a9d7468
17 changed files with 261 additions and 299 deletions

View File

@@ -1,4 +1,4 @@
local Blocks = require('blocks')
local Blocks = require('builder.blocks')
local class = require('class')
local Message = require('message')
local Util = require('util')
@@ -10,13 +10,9 @@ local turtle = _G.turtle
local Builder = class()
Util.merge(Builder, {
isCommandComputer = not turtle,
slots = { },
loc = { },
index = 1,
mode = 'build',
fuelItem = { id = 'minecraft:coal', dmg = 0 },
resourceSlots = 14,
facing = 'south',
})
local BUILDER_DIR = 'usr/builder'
@@ -87,7 +83,7 @@ end
function Builder:saveProgress(index)
Util.writeTable(
fs.combine(BUILDER_DIR, self.schematic.filename .. '.progress'),
{ index = index, facing = self.facing, loc = self.loc }
{ index = index, loc = self.loc }
)
end
@@ -98,7 +94,6 @@ function Builder:loadProgress(filename)
if self.index > #self.schematic.blocks then
self.index = 1
end
self.facing = progress.facing or 'south'
self.loc = progress.loc or { }
end
end

View File

@@ -12,6 +12,11 @@ function Builder:begin()
local last = #self.schematic.blocks
local throttle = Util.throttle()
local cx, cy, cz = commands.getBlockPosition()
if self.loc.x then
cx, cy, cz = self.loc.rx, self.loc.ry, self.loc.rz
end
if self.mode == 'destroy' then
direction = -1
last = 1
@@ -32,12 +37,10 @@ function Builder:begin()
end
local function placeBlock(bid, dmg, x, y, z)
local cx, _, cz = commands.getBlockPosition()
local command = table.concat({
"setblock",
cx + x + 1,
"~" .. y,
cy + y,
cz + z + 1,
bid,
dmg,

View File

@@ -5,31 +5,11 @@ local Point = require('point')
local bit = _G.bit
local fs = _G.fs
local os = _G.os
local term = _G.term
local headings = {
[ 0 ] = { xd = 1, zd = 0, yd = 0, heading = 0, direction = 'east' },
[ 1 ] = { xd = 0, zd = 1, yd = 0, heading = 1, direction = 'south' },
[ 2 ] = { xd = -1, zd = 0, yd = 0, heading = 2, direction = 'west' },
[ 3 ] = { xd = 0, zd = -1, yd = 0, heading = 3, direction = 'north' },
[ 4 ] = { xd = 0, zd = 0, yd = 1, heading = 4, direction = 'up' },
[ 5 ] = { xd = 0, zd = 0, yd = -1, heading = 5, direction = 'down' }
}
local namedHeadings = {
east = headings[0],
south = headings[1],
west = headings[2],
north = headings[3],
up = headings[4],
down = headings[5]
}
local function getHeadingInfo(heading)
if heading and type(heading) == 'string' then
return namedHeadings[heading]
end
return headings[heading]
return Point.headings[heading]
end
--[[
@@ -86,7 +66,7 @@ function Spinner:stop(text)
end
local Schematic = class()
function Schematic:init(args)
function Schematic:init()
self.blocks = { }
self.damages = { }
self.originalBlocks = { }
@@ -122,7 +102,7 @@ function Schematic:readname(h)
local n = n1*256 + n2
local str = ""
for i=1,n do
for _=1,n do
local c = h:readbyte(h)
if c == nil then
return

View File

@@ -15,7 +15,13 @@ local read = _G.read
local rs = _G.rs
local turtle = _G.turtle
local RESOURCE_SLOTS = 14
local FUEL_ITEM = { id = 'minecraft:coal', dmg = 0 }
local TurtleBuilder = class(Builder)
Util.merge(TurtleBuilder, {
slots = { },
})
-- Temp functions until conversion to new adapters is complete
local function convertSingleForward(item)
@@ -92,7 +98,7 @@ function supplyPage:eventHandler(event)
self.builder:dumpInventory()
--Builder.status = 'idle'
UI:setPage('start')
turtle.status = 'idle'
turtle.setStatus('waiting')
elseif event.type == 'grid_focus_row' then
self.statusBar:setValue('help', event.selected.id .. ':' .. event.selected.dmg)
@@ -164,7 +170,7 @@ function TurtleBuilder:getBlockCounts()
wrench.need = 1
blocks[wrench.id .. ':' .. wrench.dmg] = wrench
local fuel = self.subDB:getSubstitutedItem(Builder.fuelItem.id, Builder.fuelItem.dmg)
local fuel = self.subDB:getSubstitutedItem(FUEL_ITEM.id, FUEL_ITEM.dmg)
fuel.qty = 0
fuel.need = 1
blocks[fuel.id .. ':' .. fuel.dmg] = fuel
@@ -200,7 +206,7 @@ function TurtleBuilder:getAirResupplyList(blockIndex)
local slots = { }
if self.mode == 'destroy' then
for i = 1, self.resourceSlots do
for i = 1, RESOURCE_SLOTS do
slots[i] = {
qty = 0,
need = 0,
@@ -211,7 +217,7 @@ function TurtleBuilder:getAirResupplyList(blockIndex)
slots = self:getGenericSupplyList(blockIndex)
end
local fuel = self.subDB:getSubstitutedItem(self.fuelItem.id, self.fuelItem.dmg)
local fuel = self.subDB:getSubstitutedItem(FUEL_ITEM.id, FUEL_ITEM.dmg)
slots[15] = {
id = 'minecraft:chest',
@@ -260,7 +266,7 @@ end
function TurtleBuilder:getGenericSupplyList(blockIndex)
local slots = { }
for i = 1, self.resourceSlots do
for i = 1, RESOURCE_SLOTS do
slots[i] = {
qty = 0,
need = 0,
@@ -397,13 +403,13 @@ function TurtleBuilder:refuel()
print('Refueling')
turtle.select(1)
local fuel = self.subDB:getSubstitutedItem(self.fuelItem.id, self.fuelItem.dmg)
local fuel = self.subDB:getSubstitutedItem(FUEL_ITEM.id, FUEL_ITEM.dmg)
self.itemAdapter:provide(convertSingleForward(fuel), 64, 1)
if turtle.getItemCount(1) == 0 then
print('Out of fuel, add fuel to chest/ME system')
turtle.setHeading(0)
turtle.status = 'waiting'
turtle.setStatus('waiting')
os.sleep(5)
else
turtle.refuel(64)
@@ -426,7 +432,7 @@ function TurtleBuilder:inAirDropoff()
return false
end
turtle.status = 'waiting'
turtle.setStatus('waiting')
if msg.contents.point then
local pt = msg.contents.point
@@ -488,7 +494,7 @@ function TurtleBuilder:inAirResupply()
return false
end
turtle.status = 'waiting'
turtle.setStatus('waiting')
if msg.contents.point then
local pt = msg.contents.point
@@ -609,7 +615,7 @@ function TurtleBuilder:resupply()
return
end
turtle.status = 'resupplying'
turtle.setStatus('resupplying')
self:log('Resupplying')
self:gotoSupplyPoint()
@@ -653,11 +659,11 @@ end
-- figure out our orientation in the world
function TurtleBuilder:getTurtleFacing()
local directions = { -- reversed directions
[5] = 'west',
[3] = 'north',
[4] = 'east',
[2] = 'south',
local directions = {
[5] = 2,
[3] = 3,
[4] = 0,
[2] = 1,
}
local function getItem(item)
@@ -730,7 +736,7 @@ function TurtleBuilder:wrenchBlock(side, facing, cache)
}
if turtle.getHeadingInfo(facing).heading < 4 then
local offsetDirection = (turtle.getHeadingInfo(self.facing).heading +
local offsetDirection = (self.facing +
turtle.getHeadingInfo(facing).heading) % 4
facing = turtle.getHeadingInfo(offsetDirection).direction
end
@@ -754,17 +760,12 @@ function TurtleBuilder:wrenchBlock(side, facing, cache)
end
function TurtleBuilder:rotateBlock(side, facing)
local s = self:getWrenchSlot()
if not s then
return false
if self:getWrenchSlot() then
for _ = 1, facing do
turtle.getAction(side).place()
end
return true
end
for _ = 1, facing do
turtle.getAction(side).place()
end
return true
end
-- place piston, wrench piston to face downward, extend, remove piston
@@ -813,7 +814,7 @@ function TurtleBuilder:_goto(x, z, y, heading)
print('stuck')
print('Press enter to continue')
os.sleep(1)
turtle.status = 'stuck'
turtle.setStatus('stuck')
read()
end
end
@@ -902,7 +903,7 @@ function TurtleBuilder:placeDirectionalBlock(b, slot, travelPlane)
[ 'west-up' ] = 'east'
}
if stairUpDirections[d] then
local isSouth = (turtle.getHeadingInfo(self.facing).heading +
local isSouth = (self.facing +
turtle.getHeadingInfo(stairUpDirections[d]).heading) % 4 == 1
if not self.stairBug then
@@ -1091,10 +1092,10 @@ function TurtleBuilder:build()
if self.mode == 'destroy' then
direction = -1
last = 1
turtle.status = 'destroying'
turtle.setStatus('destroying')
else
travelPlane = self:findTravelPlane(self.index)
turtle.status = 'building'
turtle.setStatus('building')
end
local pt = self:getBuildingCorner()
@@ -1120,13 +1121,13 @@ function TurtleBuilder:build()
-- if no supplier, then should fill all slots
if turtle.getItemCount(self.resourceSlots) > 0 or turtle.getFuelLevel() < minFuel then
if turtle.getItemCount(RESOURCE_SLOTS) > 0 or turtle.getFuelLevel() < minFuel then
if turtle.getFuelLevel() < minFuel or not self:inAirDropoff() then
self:gotoSupplyPoint()
self:dumpInventoryWithCheck()
self:refuel()
end
turtle.status = 'destroying'
turtle.setStatus('destroying')
end
else -- Build mode
@@ -1177,9 +1178,9 @@ function TurtleBuilder:build()
throttle() -- sleep in case there are a large # of skipped blocks
end
if turtle.abort then
turtle.status = 'aborting'
turtle.abort = false
if turtle.isAborted() then
turtle.setStatus('aborting')
turtle.abort(false)
self:gotoTravelPlane(travelPlane)
self:gotoSupplyPoint()
turtle.setHeading(0)
@@ -1224,8 +1225,7 @@ function TurtleBuilder:begin()
self:refuel()
self:getTurtleFacing()
local facing = turtle.getHeadingInfo(self.facing).heading
Point.rotate(self.supplyPoint, facing)
Point.rotate(self.supplyPoint, self.facing)
turtle.setPoint(self.supplyPoint)
-- reset piston cache in case wrench was substituted

View File

@@ -161,7 +161,7 @@ return function(startPt, endPt, firstPt, verbose)
if not turtle.gotoPoint(node) then
break
end
until turtle.abort
until turtle.isAborted()
turtle.resetState()
turtle.setMoveCallback(oldCallback)