shooting gallery

This commit is contained in:
kepler155c@gmail.com
2019-03-23 02:17:04 -04:00
parent 8ca0071786
commit d25e160452
4 changed files with 91 additions and 70 deletions

View File

@@ -405,6 +405,7 @@ local function rawExport(source, target, item, qty, slot)
end end
function Storage:export(target, slot, count, item) function Storage:export(target, slot, count, item)
local timer = Util.timer()
local total = 0 local total = 0
local key = item.key or table.concat({ item.name, item.damage, item.nbtHash }, ':') local key = item.key or table.concat({ item.name, item.damage, item.nbtHash }, ':')
@@ -414,9 +415,9 @@ function Storage:export(target, slot, count, item)
if amount > 0 then if amount > 0 then
self:updateCache(adapter, item, -amount) self:updateCache(adapter, item, -amount)
_G._debug('EXT: %s(%d): %s -> %s%s', _G._debug('EXT: %s(%d): %s -> %s%s in %s',
item.displayName or item.name, amount, self:_sn(adapter.name), self:_sn(target.name), item.displayName or item.name, amount, self:_sn(adapter.name), self:_sn(target.name),
slot and string.format('[%d]', slot) or '[*]') slot and string.format('[%d]', slot) or '[*]', Util.round(timer(), 2))
end end
count = count - amount count = count - amount
total = total + amount total = total + amount
@@ -472,6 +473,7 @@ function Storage:import(source, slot, count, item)
if not source then error('Storage:import: source is required') end if not source then error('Storage:import: source is required') end
if not slot then error('Storage:import: slot is required') end if not slot then error('Storage:import: slot is required') end
local timer = Util.timer()
local total = 0 local total = 0
local key = item.key or table.concat({ item.name, item.damage, item.nbtHash }, ':') local key = item.key or table.concat({ item.name, item.damage, item.nbtHash }, ':')
@@ -498,9 +500,9 @@ function Storage:import(source, slot, count, item)
if amount > 0 then if amount > 0 then
self:updateCache(adapter, item, amount) self:updateCache(adapter, item, amount)
_G._debug('INS: %s(%d): %s[%d] -> %s', _G._debug('INS: %s(%d): %s[%d] -> %s in %s',
item.displayName or item.name, amount, item.displayName or item.name, amount,
self:_sn(source.name), slot, self:_sn(adapter.name)) self:_sn(source.name), slot, self:_sn(adapter.name), Util.round(timer(), 2))
-- record that we have imported this item into storage during this cycle -- record that we have imported this item into storage during this cycle
self.activity[key] = (self.activity[key] or 0) + amount self.activity[key] = (self.activity[key] or 0) + amount
@@ -556,13 +558,14 @@ end
-- When importing items into a locked chest, trash any remaining items if full -- When importing items into a locked chest, trash any remaining items if full
-- TODO: use all available trashcans -- TODO: use all available trashcans
function Storage:trash(source, slot, count, item) function Storage:trash(source, slot, count, item)
local timer = Util.timer()
local target = Util.find(self.nodes, 'mtype', 'trashcan') local target = Util.find(self.nodes, 'mtype', 'trashcan')
local amount = 0 local amount = 0
if target and target.adapter and target.adapter.online then if target and target.adapter and target.adapter.online then
local s, m = pcall(function() local s, m = pcall(function()
_G._debug('TRA: %s(%d): %s%s -> %s', _G._debug('TRA: %s(%d): %s%s -> %s in %s',
item.displayName or item.name, count, self:_sn(source.name), item.displayName or item.name, count, self:_sn(source.name),
slot and string.format('[%d]', slot) or '[*]', self:_sn(target.name)) slot and string.format('[%d]', slot) or '[*]', self:_sn(target.name), Util.round(timer(), 2))
--_G._debug('TRA: %s[%d] (%d)', self:_sn(source.name), slot, count or 64) --_G._debug('TRA: %s[%d] (%d)', self:_sn(source.name), slot, count or 64)
if isValidTransfer(source.adapter, target.name) then if isValidTransfer(source.adapter, target.name) then

View File

@@ -12,8 +12,8 @@ Event.addRoutine(function()
local lastTransfer local lastTransfer
while true do while true do
local sleepTime = 1.5 local sleepTime = 1.5
if lastTransfer and os.clock() - lastTransfer < 3 then if lastTransfer and os.clock() - lastTransfer < 2 then
sleepTime = .25 sleepTime = .1
end end
os.sleep(context.socket and sleepTime or 5) os.sleep(context.socket and sleepTime or 5)

View File

@@ -10,7 +10,6 @@ local glasses = device['plethora:glasses']
local scanner = device['plethora:scanner'] or local scanner = device['plethora:scanner'] or
error('Plethora scanner must be equipped') error('Plethora scanner must be equipped')
local target
local projecting = { } local projecting = { }
local function getPoint() local function getPoint()
@@ -47,7 +46,7 @@ local page = UI.Page {
detail = UI.SlideOut { detail = UI.SlideOut {
menuBar = UI.MenuBar { menuBar = UI.MenuBar {
buttons = { buttons = {
{ text = 'Cancel', event = 'cancel' }, { text = 'Back', event = 'cancel' },
}, },
}, },
grid = UI.ScrollingGrid { grid = UI.ScrollingGrid {
@@ -60,6 +59,9 @@ local page = UI.Page {
{ heading = ' Z', key = 'z', width = 3, align = 'right' }, { heading = ' Z', key = 'z', width = 3, align = 'right' },
}, },
sortColumn = 'name', sortColumn = 'name',
accelerators = {
grid_select = 'noop',
},
}, },
}, },
} }
@@ -107,9 +109,59 @@ end
function page.detail:show(blocks, entry) function page.detail:show(blocks, entry)
self.grid:setValues(Util.filter(blocks, function(b) return b.key == entry.key end)) self.grid:setValues(Util.filter(blocks, function(b) return b.key == entry.key end))
self.target = entry
if canvas then
self.handler = Event.onInterval(.5, function()
if not self.target then
Event.off(self.handler)
projecting = { }
canvas.clear()
else
local t = self.target
local scanned = scanner.scan()
local pos = getPoint()
blocks = Util.reduce(scanned, function(acc, b)
if b.name == t.name and b.metadata == t.damage then
-- track block's world position
b.id = table.concat({
math.floor(pos.x + b.x),
math.floor(pos.y + b.y),
math.floor(pos.z + b.z) }, ':')
acc[b.id] = b
end
return acc
end, { })
for _, b in pairs(blocks) do
if not projecting[b.id] then
projecting[b.id] = b
b.box = canvas.addBox(
pos.x - offset.x + b.x + -(pos.x % 1) + .25,
pos.y - offset.y + b.y + -(pos.y % 1) + .25,
pos.z - offset.z + b.z + -(pos.z % 1) + .25,
.5, .5, .5)
b.box.setDepthTested(false)
end
end
for _, b in pairs(projecting) do
if not blocks[b.id] then
b.box.remove()
projecting[b.id] = nil
end
end
end
end)
end
return UI.SlideOut.show(self) return UI.SlideOut.show(self)
end end
function page.detail:hide()
self.target = nil
return UI.SlideOut.hide(self)
end
function page:eventHandler(event) function page:eventHandler(event)
if event.type == 'quit' then if event.type == 'quit' then
Event.exitPullEvents() Event.exitPullEvents()
@@ -117,62 +169,16 @@ function page:eventHandler(event)
elseif event.type == 'scan' then elseif event.type == 'scan' then
self:scan() self:scan()
elseif event.type == 'grid_select' then elseif event.type == 'grid_select' and event.element == page.grid then
target = self.grid:getSelected()
self.detail:show(self.blocks, self.grid:getSelected()) self.detail:show(self.blocks, self.grid:getSelected())
elseif event.type == 'cancel' then elseif event.type == 'cancel' then
if canvas then
canvas.clear()
target = nil
projecting = { }
end
self.detail:hide() self.detail:hide()
end end
UI.Page.eventHandler(self, event) UI.Page.eventHandler(self, event)
end end
if canvas then
Event.onInterval(.5, function()
if target then
local scanned = scanner.scan()
local pos = getPoint()
local blocks = Util.reduce(scanned, function(acc, b)
if b.name == target.name and b.metadata == target.damage then
-- track block's world position
b.id = table.concat({
math.floor(pos.x + b.x),
math.floor(pos.y + b.y),
math.floor(pos.z + b.z) }, ':')
acc[b.id] = b
end
return acc
end, { })
for _, b in pairs(blocks) do
if not projecting[b.id] then
projecting[b.id] = b
b.box = canvas.addBox(
pos.x - offset.x + b.x + -(pos.x % 1) + .25,
pos.y - offset.y + b.y + -(pos.y % 1) + .25,
pos.z - offset.z + b.z + -(pos.z % 1) + .25,
.5, .5, .5)
b.box.setDepthTested(false)
end
end
for _, b in pairs(projecting) do
if not blocks[b.id] then
b.box.remove()
projecting[b.id] = nil
end
end
end
end)
end
UI:setPage(page) UI:setPage(page)
Event.onTimeout(0, function() Event.onTimeout(0, function()

View File

@@ -1,18 +1,35 @@
local Angle = require('neural.angle')
local Mobs = require('neural.mobs') local Mobs = require('neural.mobs')
local ni = require('neural.interface')
local Point = require('point') local Point = require('point')
local os = _G.os local device = _G.device
local os = _G.os
if not ni.look then local sensor = device['plethora:sensor'] or error('Sensor is required')
error('neuralInterface required') local weapon = device['plethora:laser']
local uid = ''
local function shootAt(pt)
local yaw, pitch = Angle.towards(pt.x, pt.y, pt.z)
weapon.fire(yaw, pitch, 4)
end end
local uid = ni.getID and ni.getID() or error('Introspection module is required') if not weapon then
weapon = device['plethora:introspection']
if not weapon or not weapon.shoot then
error('Either a laser or a skeleton with introspection module is required')
end
uid = weapon.getID()
shootAt = function(pt)
local yaw, pitch = Angle.towards(pt.x, pt.y, pt.z)
weapon.look(yaw, pitch)
weapon.shoot(1)
end
end
local function findTargets() local function findTargets()
local pos = { x = 0, y = 0, z = 0 } local pos = { x = 0, y = 0, z = 0 }
local l = ni.sense() local l = sensor.sense()
table.sort(l, function(e1, e2) table.sort(l, function(e1, e2)
return Point.distance(e1, pos) < Point.distance(e2, pos) return Point.distance(e1, pos) < Point.distance(e2, pos)
end) end)
@@ -20,7 +37,7 @@ local function findTargets()
local targets = { } local targets = { }
for _,v in ipairs(l) do for _,v in ipairs(l) do
if v.id ~= uid and Mobs.getNames()[v.name] then if v.id ~= uid and Mobs.getNames()[v.name] then
if math.abs(v.y) < 2 then -- pitch is broken if v.y >= 0 and v.y < 1 then
table.insert(targets, v) table.insert(targets, v)
end end
end end
@@ -28,16 +45,11 @@ local function findTargets()
return #targets > 0 and targets return #targets > 0 and targets
end end
print('Targets:')
for _,v in pairs(ni.sense()) do
print(v.name)
end
while true do while true do
local targets = findTargets() local targets = findTargets()
if targets then if targets then
for _, entity in ipairs(targets) do for _, entity in ipairs(targets) do
ni.shootAt(entity, 1) shootAt(entity, 1)
end end
end end
os.sleep(.5) os.sleep(.5)