turtle api

This commit is contained in:
kepler155c
2017-10-31 02:01:14 -04:00
parent d97a9d7468
commit 2526308eb6
6 changed files with 115 additions and 63 deletions

View File

@@ -586,14 +586,13 @@ local function closestEdgePoint(pt, pts, rpt)
return cpt
end
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 },
function TurtleBuilder:getBuildingCorner(y)
local box = {
x = -1, ex = self.schematic.width,
y = y, ey = y,
z = -1, ez = self.schematic.length,
}
return closestEdgePoint(self.supplyPoint, pts, turtle.getPoint())
return Point.closestPointInBox(turtle.getPoint(), box)
end
function TurtleBuilder:gotoSupplyPoint()
@@ -601,7 +600,7 @@ function TurtleBuilder:gotoSupplyPoint()
-- 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()
local pt = self:getBuildingCorner(turtle.point.y)
turtle._goto(pt.x, pt.z)
turtle.setPolicy('none')
turtle.pathfind(self.supplyPoint)
@@ -1085,7 +1084,6 @@ end
function TurtleBuilder:build()
local direction = 1
local last = #self.schematic.blocks
local travelPlane = 0
local minFuel = self.schematic.height + self.schematic.width + self.schematic.length + 100
local throttle = Util.throttle()
@@ -1094,11 +1092,12 @@ function TurtleBuilder:build()
last = 1
turtle.setStatus('destroying')
else
travelPlane = self:findTravelPlane(self.index)
turtle.setStatus('building')
end
local pt = self:getBuildingCorner()
local travelPlane = self:findTravelPlane(self.index)
local pt = self:getBuildingCorner(travelPlane)
turtle.pathfind({ x = pt.x, z = pt.z, y = travelPlane })
turtle.setPolicy('digAttack')
@@ -1210,22 +1209,21 @@ function TurtleBuilder:build()
end
function TurtleBuilder:begin()
turtle.reset()
self:dumpInventory()
self:refuel()
self:getTurtleFacing()
if self.loc.x then
self.supplyPoint = {
x = self.loc.x - self.loc.rx - 1,
y = self.loc.y - self.loc.ry,
z = self.loc.z - self.loc.rz - 1,
}
Point.rotate(self.supplyPoint, self.facing)
else
self.supplyPoint = { x = -1, y = 0, z = -1 }
end
turtle.reset()
self:dumpInventory()
self:refuel()
self:getTurtleFacing()
Point.rotate(self.supplyPoint, self.facing)
turtle.setPoint(self.supplyPoint)
-- reset piston cache in case wrench was substituted

View File

@@ -2,7 +2,9 @@ local Util = require('util')
local turtle = _G.turtle
local Craft = { }
local Craft = {
recipes = Util.readTable('usr/etc/recipes.db') or { },
}
local function clearGrid(inventoryAdapter)
for i = 1, 16 do
@@ -41,7 +43,6 @@ local function getItemCount(items, key)
end
local function turtleCraft(recipe, qty, inventoryAdapter)
clearGrid(inventoryAdapter)
for k,v in pairs(recipe.ingredients) do
@@ -57,6 +58,12 @@ local function turtleCraft(recipe, qty, inventoryAdapter)
end
function Craft.craftRecipe(recipe, count, inventoryAdapter)
if type(recipe) == 'string' then
recipe = Craft.recipes[recipe]
if not recipe then
return false, 'No recipe'
end
end
local items = inventoryAdapter:listItems()
@@ -103,18 +110,16 @@ end
-- given a certain quantity, return how many of those can be crafted
function Craft.getCraftableAmount(recipe, count, items, missing)
local function sumItems(recipe, items, summedItems, count)
local function sumItems(recipe, summedItems, count)
local canCraft = 0
for i = 1, count do
for _ = 1, count do
for _,item in pairs(recipe.ingredients) do
local summedItem = summedItems[item] or getItemCount(items, item)
local irecipe = Craft.recipes[item]
if irecipe and summedItem <= 0 then
summedItem = summedItem + sumItems(irecipe, items, summedItems, 1)
summedItem = summedItem + sumItems(irecipe, summedItems, 1)
end
if summedItem <= 0 then
if missing then
@@ -130,7 +135,7 @@ function Craft.getCraftableAmount(recipe, count, items, missing)
return canCraft
end
return sumItems(recipe, items, { }, math.ceil(count / recipe.count), missing)
return sumItems(recipe, { }, math.ceil(count / recipe.count))
end
function Craft.canCraft(item, count, items)
@@ -149,13 +154,15 @@ function Craft.getCraftableAmountTest()
{ name = 'minecraft:planks', damage = 0, count = 5 },
{ name = 'minecraft:log', damage = 0, count = 2 },
}
results[1] = { item = 'chest', expected = 1, got = Craft.getCraftableAmount(Craft.recipes['minecraft:chest:0'], 2, items) }
results[1] = { item = 'chest', expected = 1,
got = Craft.getCraftableAmount(Craft.recipes['minecraft:chest:0'], 2, items) }
items = {
{ name = 'minecraft:log', damage = 0, count = 1 },
{ name = 'minecraft:coal', damage = 1, count = 1 },
}
results[2] = { item = 'torch', expected = 4, got = Craft.getCraftableAmount(Craft.recipes['minecraft:torch:0'], 4, items) }
results[2] = { item = 'torch', expected = 4,
got = Craft.getCraftableAmount(Craft.recipes['minecraft:torch:0'], 4, items) }
return results
end

64
apis/turtle/crafting.lua Normal file
View File

@@ -0,0 +1,64 @@
local Adapter = require('inventoryAdapter')
local Craft = require('turtle.craft')
local turtle = _G.turtle
local CRAFTING_TABLE = 'minecraft:crafting_table'
local function clearGrid(inventory)
for i = 1, 16 do
local count = turtle.getItemCount(i)
if count > 0 then
inventory:insert(i, count)
if turtle.getItemCount(i) ~= 0 then
return false
end
end
end
return true
end
function turtle.craftItem(item, count, inventoryInfo)
local success
local inventory = Adapter.wrap(inventoryInfo)
if not inventory then
return false, 'Invalid inventory'
end
local equipped, side
if not turtle.isEquipped('workbench') then
local modemSide = turtle.isEquipped('modem') or 'right'
local osides = { left = 'right', right = 'left' }
side = osides[modemSide]
if not turtle.select(CRAFTING_TABLE) then
clearGrid(inventory)
if not turtle.selectOpenSlot() then
return false, 'Inventory is full'
end
if not inventory:provide({ name = CRAFTING_TABLE, damage = 0 }, 1) then
return false, 'Missing crafting table'
end
end
local slot = turtle.select(CRAFTING_TABLE)
turtle.equip(side, CRAFTING_TABLE)
equipped = turtle.getItemDetail(slot)
end
success = Craft.craftRecipe(item, count or 1, inventory)
if equipped then
turtle.selectOpenSlot()
inventory:provide({ name = equipped.name, damage = equipped.damage }, 1)
turtle.equip(side, equipped.name .. ':' .. equipped.damage)
end
return success
end
function turtle.canCraft(item, count, items)
return Craft.canCraft(item, count, items)
end
return true

View File

@@ -113,7 +113,7 @@ local function getAdjacentPoint(pt)
return closestPoint(turtle.getPoint(), t)
end
return function(startPt, endPt, firstPt, verbose)
function turtle.level(startPt, endPt, firstPt, verbose)
checkedNodes = { }
nodes = { }
box = { }
@@ -166,3 +166,5 @@ return function(startPt, endPt, firstPt, verbose)
turtle.resetState()
turtle.setMoveCallback(oldCallback)
end
return true