attack program - new

This commit is contained in:
kepler155c
2018-11-23 17:03:33 -05:00
parent 4774b6b504
commit d426b80475
2 changed files with 36 additions and 10 deletions

View File

@@ -9,7 +9,13 @@ local os = _G.os
local turtle = _G.turtle local turtle = _G.turtle
local args = { ... } local args = { ... }
local mob = args[1] or error('Syntax: attack <mob name>') local mobType = args[1] or error('Syntax: attack <mob name>')
local Runners = {
Cow = true,
Chicken = true,
Blaze = false,
}
local function equip(side, item, rawName) local function equip(side, item, rawName)
local equipped = Peripheral.lookup('side/' .. side) local equipped = Peripheral.lookup('side/' .. side)
@@ -64,14 +70,17 @@ local function dropOff()
end end
end end
local function normalize(b)
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
end
while true do while true do
local blocks = sensor.sense() local blocks = sensor.sense()
local mobs = Util.filterInplace(blocks, function(b) local mobs = Util.filterInplace(blocks, function(b)
if b.name == mob then if b.name == mobType then
b.x = Util.round(b.x) + turtle.point.x normalize(b)
b.y = Util.round(b.y) + turtle.point.y
b.z = Util.round(b.z) + turtle.point.z
return true return true
end end
end) end)
@@ -83,11 +92,27 @@ while true do
if #mobs == 0 then if #mobs == 0 then
os.sleep(3) os.sleep(3)
else else
if Runners[mobType] then
-- if this mob runs away, just attack next closest
Point.eachClosest(turtle.point, mobs, function(b) Point.eachClosest(turtle.point, mobs, function(b)
if turtle.faceAgainst(b) then if turtle.faceAgainst(b) then
repeat until not turtle.attack() repeat until not turtle.attack()
end end
end) end)
os.sleep(2) --- give a little time for mobs to calm down
else
-- this mob doesn't run, attack and follow until dead
local mob = Point.closest(turtle.point, mobs)
repeat
if turtle.faceAgainst(mob) then
repeat until not turtle.attack()
end
mob = sensor.getMetaByID(mob.id)
if mob then
normalize(mob)
end
until not mob
end
end end
dropOff() dropOff()

View File

@@ -23,6 +23,7 @@ local activityWizardPage = UI.Window {
marginRight = 0, marginRight = 0,
value = string.format(template, Ansi.yellow, Ansi.reset), value = string.format(template, Ansi.yellow, Ansi.reset),
}, },
timestamp = os.clock(),
} }
function activityWizardPage:isValidType(node) function activityWizardPage:isValidType(node)