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

[Question] Why will my turtle only sometimes attack?

Started by TotalConfusion, 02 April 2013 - 11:08 AM
TotalConfusion #1
Posted 02 April 2013 - 01:08 PM
Long story short, I got tired of mobs getting in the way of my turtle and getting it all confused so I added in some code so it would recognise when it had not moved and would then attack. However when testing the program I would stand in front of it and it would attack me sometimes and not others.

here is the applicable piece of code.

function travel()
	 while dist ~= fdist do
		  if turtle.forward() then
			  dig()
			  dist = dist + 1
		  else
			  turtle.attack()
		 end  
	end
end

function dig()
		while turtle.detect() do
						turtle.dig()
						sleep(0.25)
		end
end


and here is a link to my pastebin of the full program.
http://pastebin.com/ztT90fND

Any help would be vastly appreciated, as while it is not super important, it does bug the hell out of me.
Ray_Anor #2
Posted 02 April 2013 - 07:19 PM

if turtle.forward() then
	dig()
	dist = dist + 1
else
	while turtle.attack() do
		 os.sleep(0.25)
	end
end

try this… and if don't work fine - add also some time in os.sleep(…)
theoriginalbit #3
Posted 02 April 2013 - 07:26 PM
If i remember correctly, I remember something in one of the newer releases saying something about how it can push players or something, if I am remembering correctly, it could just be that you are un-pushable or something. not too sure tho.

really you should do something like this

if turtle.forward() then -- the turtle can move
  dig()
  dist = dist + 1
elseif turtle.detect() then -- there is a block in the way
  -- handle that
elseif turtle.getFuelLevel() == 0 then -- the turtle is out of fuel
  -- handle that
elseif turtle.attack() then
  while turtle.attack() do end -- there is no need for a call to sleep here, turtle.attack yields so you wont get any errors...
else
  error("The turtle cannot move for an unknown reason", 0)
end

also in your dig have the sleep value (for the gravel/sand) as 0.8 as this is the time it takes for a block to fall. no point checking any quicker than that…
Ray_Anor #4
Posted 02 April 2013 - 07:50 PM
really you should do something like this

confirm that - your solution is better.
If need good program for AI - always better do split all possible events handlers.
TotalConfusion #5
Posted 03 April 2013 - 05:01 AM
Thanks for the help guys will give your suggestions a try tonight. I'm a total noob at lua ( and programming in general) only started it 3 weeks ago so I really apreciate the advice on smoothing out the event handlers.
Izodn #6
Posted 04 April 2013 - 12:49 PM
I'd honestly go for something like this.
Spoiler

while dist < maxDist do
	while not turtle.forward() do
		turtle.dig()
		turtle.attack()
		if turtle.getFuelLevel() == 0 then
			--[[ handle this --]]
		end
	end
	dist=dist+1
end
If the turtle can move, continue, else, while it can't move do: dig, attack, check fuel level.

Not sure if it's beside the point, but it's what I use.

Note: Don't worry about gravel, sand, people, this code will definitely handle them if there's enough fuel.
Bubba #7
Posted 04 April 2013 - 01:11 PM
I'd honestly go for something like this.
Spoiler

while dist < maxDist do
	while not turtle.forward() do
		turtle.dig()
		turtle.attack()
		if turtle.getFuelLevel() == 0 then
			--[[ handle this --]]
		end
	end
	dist=dist+1
end
If the turtle can move, continue, else, while it can't move do: dig, attack, check fuel level.

Not sure if it's beside the point, but it's what I use.

Note: Don't worry about gravel, sand, people, this code will definitely handle them if there's enough fuel.

TheOriginalBIT's solution is better, namely because it attacks only when it fails to dig. You may be surprised at how much cumulative time it can take to attack every time the turtle encounters a block. It also handles detecting a block and still failing to dig.
PonyKuu #8
Posted 04 April 2013 - 05:07 PM
AFAIK, their attack range is too small. So, it is possible that turtle cannot move because of entity, but turtle.attack() doesn't reach it.
theoriginalbit #9
Posted 04 April 2013 - 05:40 PM
AFAIK, their attack range is too small. So, it is possible that turtle cannot move because of entity, but turtle.attack() doesn't reach it.
Depends on which version of CC is being used… that bug was fixed in one of the newer ones…