This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Brian87's profile picture

melee turtle

Started by Brian87, 21 February 2014 - 06:06 AM
Brian87 #1
Posted 21 February 2014 - 07:06 AM
Hello!
I want the turtle to attack a mob (if there is a mob there) until it's dead, then sleep for a second and check again. If there is no mob there it does something else, and if it is a mob there, it will attack again until it's dead and so on…

I have this code:

local function attack()
while turtle.attack() do
  while turtle.attack() do end
  turtle.suck()
  sleep(1)
end
end

local function moveForward()
for i = 1,2 do
  turtle.forward()
  attack()
  turtle.turnLeft()
  attack()
  turtle.turnRight()
  turtle.turnRight()
  attack()
  turtle.turnLeft()
end
turtle.turnRight()
turtle.turnRight()
end

local function moveBack()
turtle.forward()
attack()
turtle.forward()
attack()
end

while true do
moveForward()
moveBack()
moveForward()
moveBack()
end

But what this does, is attack the mob once, then sleep for a second, then attack once and sleep etc. until the mob is dead.
Can someone help me with my problem? :)/>
TilionDC #2
Posted 21 February 2014 - 07:59 AM
The turtle. isn't detecting if there is a mob in front o fit. I would do a program that moves forward and if the moving stopped it will attack until it can move. when it has moven it moves back.
But in what context do you want to use this melee turtle? Is it a mobspawner?

This is how i would do it like this.

function attack()
  local checkMove = turtle.forward()
  while not checkMove do
    turtle.attack()
    checkMove = turtle.forward()
  end
  turtle.back()
  sleep(1)
end
while 1 == 1 do
  function attack()
end
This will move the turtle forward every secound. But if there is a mob in the way it will attack until the mob is dead then it will move forward and then it will move back again wait for a secound and do the same thing over again.