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

@@ -10,7 +10,6 @@ local glasses = device['plethora:glasses']
local scanner = device['plethora:scanner'] or
error('Plethora scanner must be equipped')
local target
local projecting = { }
local function getPoint()
@@ -47,7 +46,7 @@ local page = UI.Page {
detail = UI.SlideOut {
menuBar = UI.MenuBar {
buttons = {
{ text = 'Cancel', event = 'cancel' },
{ text = 'Back', event = 'cancel' },
},
},
grid = UI.ScrollingGrid {
@@ -60,6 +59,9 @@ local page = UI.Page {
{ heading = ' Z', key = 'z', width = 3, align = 'right' },
},
sortColumn = 'name',
accelerators = {
grid_select = 'noop',
},
},
},
}
@@ -107,9 +109,59 @@ end
function page.detail:show(blocks, entry)
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)
end
function page.detail:hide()
self.target = nil
return UI.SlideOut.hide(self)
end
function page:eventHandler(event)
if event.type == 'quit' then
Event.exitPullEvents()
@@ -117,62 +169,16 @@ function page:eventHandler(event)
elseif event.type == 'scan' then
self:scan()
elseif event.type == 'grid_select' then
target = self.grid:getSelected()
elseif event.type == 'grid_select' and event.element == page.grid then
self.detail:show(self.blocks, self.grid:getSelected())
elseif event.type == 'cancel' then
if canvas then
canvas.clear()
target = nil
projecting = { }
end
self.detail:hide()
end
UI.Page.eventHandler(self, event)
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)
Event.onTimeout(0, function()

View File

@@ -1,18 +1,35 @@
local Angle = require('neural.angle')
local Mobs = require('neural.mobs')
local ni = require('neural.interface')
local Point = require('point')
local os = _G.os
local device = _G.device
local os = _G.os
if not ni.look then
error('neuralInterface required')
local sensor = device['plethora:sensor'] or error('Sensor is 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
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 pos = { x = 0, y = 0, z = 0 }
local l = ni.sense()
local l = sensor.sense()
table.sort(l, function(e1, e2)
return Point.distance(e1, pos) < Point.distance(e2, pos)
end)
@@ -20,7 +37,7 @@ local function findTargets()
local targets = { }
for _,v in ipairs(l) do
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)
end
end
@@ -28,16 +45,11 @@ local function findTargets()
return #targets > 0 and targets
end
print('Targets:')
for _,v in pairs(ni.sense()) do
print(v.name)
end
while true do
local targets = findTargets()
if targets then
for _, entity in ipairs(targets) do
ni.shootAt(entity, 1)
shootAt(entity, 1)
end
end
os.sleep(.5)