diff --git a/farms/attack.lua b/farms/attack.lua new file mode 100644 index 0000000..c1f1f49 --- /dev/null +++ b/farms/attack.lua @@ -0,0 +1,71 @@ +_G.requireInjector(_ENV) + +local Peripheral = require('peripheral') +local Point = require('point') +local Util = require('util') + +local device = _G.device +local os = _G.os +local turtle = _G.turtle + +local args = { ... } +local mob = args[1] or error('Syntax: attack ') + +local function equip(side, item, rawName) + local equipped = Peripheral.lookup('side/' .. side) + + if equipped and equipped.type == item then + return true + end + + if not turtle.equip(side, rawName or item) then + if not turtle.selectSlotWithQuantity(0) then + error('No slots available') + end + turtle.equip(side) + if not turtle.equip(side, item) then + error('Unable to equip ' .. item) + end + end + + turtle.select(1) +end + +local function setHeading() + equip('right', 'plethora:scanner', 'plethora:module:2') + local scanner = device['plethora:scanner'] + local facing = scanner.getBlockMeta(0, 0, 0).state.facing + turtle.point.heading = Point.facings[facing].heading +end + +equip('left', 'minecraft:diamond_sword') +setHeading() +equip('right', 'plethora:sensor', 'plethora:module:3') + +local sensor = device['plethora:sensor'] + +while true do + local blocks = sensor.sense() + local mobs = Util.filterInplace(blocks, function(b) + if b.name == mob then + b.x = Util.round(b.x) + turtle.point.x + b.y = Util.round(b.y) + turtle.point.y + b.z = Util.round(b.z) + turtle.point.z + return true + end + end) + + if turtle.getFuelLevel() == 0 then + error('Out of fuel') + end + + if #mobs == 0 then + os.sleep(3) + else + Point.eachClosest(turtle.point, mobs, function(b) + if turtle.faceAgainst(b) then + repeat until not turtle.attack() + end + end) + end +end diff --git a/milo/core/listing.lua b/milo/core/listing.lua index 4bdd4f4..03c46b8 100644 --- a/milo/core/listing.lua +++ b/milo/core/listing.lua @@ -245,6 +245,14 @@ function listingPage:eventHandler(event) end function listingPage:enable() + local function updateStatus() + self.statusBar.storageStatus.value = + context.storage:isOnline() and '' or 'offline' + self.statusBar.storageStatus.textColor = + context.storage:isOnline() and colors.lime or colors.red + end + updateStatus() + Event.onTimeout(0, function() self:refresh() self:draw() @@ -259,14 +267,6 @@ function listingPage:enable() self:sync() end) - local function updateStatus() - self.statusBar.storageStatus.value = - context.storage:isOnline() and '' or 'offline' - self.statusBar.storageStatus.textColor = - context.storage:isOnline() and colors.lime or colors.red - end - updateStatus() - self.handler = Event.on({ 'storage_offline', 'storage_online' }, function() updateStatus() self.statusBar.storageStatus:draw()