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

Turtle.attack() Rednet Toggle Possible?

Started by Alcheya, 30 September 2013 - 12:22 AM
Alcheya #1
Posted 30 September 2013 - 02:22 AM
I can get wireless turtles to start attacking, but I can't get them to stop once it's started.. I can also just get wait for the signal to attack once, but then they go back to waiting.. but is there a way to "break" the attack loop or even toggle on and off? I did it before with a redstone check, but with the setup I am using now, putting a rednet wire underneath wouldn't be feasible and I'd really like to keep the visible wiring to a minimum.

I just need them to attack, nothing else, but I'd like the option to turn it all off with the press of a button so that it can be in "grinder-only" to collect mob essence at will. Under the turtles are hoppers that keep them empty.





Any help appreciated, thanks.
plazter #2
Posted 30 September 2013 - 03:52 AM
You sure can, i will get back to you once im at a computer:)
plazter #3
Posted 30 September 2013 - 03:54 AM
Can you post your code? Ill just take a look asap:)
Alcheya #4
Posted 30 September 2013 - 03:59 AM
The code for the turtle attack is nothing special, just opens up the modem, turtle waits for "toggleTurtle" then either does "turtle.attack()" and waits for the command again or does "turtle.attack()" in a loop if I add while true do to the beginning. The button on the monitor just sends "toggleTurtle". The only way to stop it from going on is to reset the turtle.
immibis #5
Posted 30 September 2013 - 04:22 AM
Use parallel.waitForAny.
plazter #6
Posted 30 September 2013 - 05:02 AM
 
Rednet.open("right")
While true do
Local id, msg = rednet.receive()

 If id then
  If msg == atk then
    Turtle.attack()
ElseIf not msg == atk then
  Sleep(0.8)
End
 sleep(1)
End


Not sure it will work but im on my phone so im not able to test first:)
Alcheya #7
Posted 30 September 2013 - 05:38 AM
Use parallel.waitForAny.

I looked into this and it seems like the thing I should be looking at, but i have no idea how it works. I'm playing around with it.


Rednet.open("right")
While true do
Local id, msg = rednet.receive()

If id then
  If msg == atk then
	Turtle.attack()
ElseIf not msg == atk then
  Sleep(0.8)
End
sleep(1)
End



Not sure it will work but im on my phone so im not able to test first:)

This is similar to what i have going now. With this I'll have to either keep sending "atk" or add a "while true do" and end up with a never-ending loop and no way to break it but going to the turtle and resetting it.
plazter #8
Posted 30 September 2013 - 06:07 AM
Local latch = true

Function atk()
Turtle.attack()
End

While true do

If msg then

Latch = true
Atk()
Elseif latch = false
Sleep(1)
End

Meaby?:b anyway, can provide a link on my pastebin with a few diffrents code that you could use some parts off i think :)/>
Alcheya #9
Posted 30 September 2013 - 06:16 AM
Local latch = true

Function atk()
Turtle.attack()
End

While true do

If msg then

Latch = true
Atk()
Elseif latch = false
Sleep(1)
End

Meaby?:b anyway, can provide a link on my pastebin with a few diffrents code that you could use some parts off i think :)/>

That would be fine. I do better when I have examples to understand how things work rather than looking at random parts. Thank you.
plazter #10
Posted 30 September 2013 - 06:23 AM
Www.pastebin.com/u/plazter here u go :)/>
Alcheya #11
Posted 30 September 2013 - 07:30 AM

Thanks, I was able to figure out half of my problem with that.. Now the other problem remains where the turtle will not respond to the messages while already in the loop. :P/>


I tried using parallel, but it seems to just turn off the program with no error messages.
plazter #12
Posted 30 September 2013 - 07:32 AM
Now could be a good time to pass on your code :b
Alcheya #13
Posted 30 September 2013 - 07:50 AM
Use parallel.waitForAny.

With this it ends the program when anything is recieved. I want it to continously loop turtle.attack when "attack" is received until it receives "dismissed" then loop back to the beginning of the program to just listening.
Alcheya #14
Posted 30 September 2013 - 08:00 AM
Now could be a good time to pass on your code :b


rednet.open("right")


function f1()
senderID, msg, dis = rednet.receive()
if msg == "attack" then
  while true do
   turtle.attack()
  end
end
end

function f2()
senderID, msg, dis = rednet.receive()
if msg == "dismissed" then
   sleep(.5)
   shell.run("fight")
  end
end

parallel.waitForAny(f1, f2)

I changed it because I thought after reading around that this might work.. regardless, still the same issue. If take out the functions and just have it listen for "attack" and "dismissed" once it gets the signal to attack, it will ignore anything else.
Wojbie #15
Posted 30 September 2013 - 08:04 AM
I would make 2 functions with while true do loops inside and one local toggle

1) if toggle=true then attack else sleep
2) listen and set toggle depending on if "attack" or "dismiss"

Run both on same time with parallel.waitForAny. or parallel.waitForAll. - no matter cause both functions have while true do loops inside with no breaks - never end

No need to run program again cause it stores if it needs to attack or not in toggle -
2nd just listens and sets toggle depending on orders.
1st just attacks if toggled else sleeps.
plazter #16
Posted 30 September 2013 - 08:10 AM

Rednet.open("right")

Function rednet()
Id, sg = rednet.receive()
If msg =="atk" then 
Turtle.attack()
End

Repeat rednet() 

Untill local id , sg = rednet.receive() 
If msg == "stop" then
Sleep(1)
End


If this doesnt work i will figure it out at Home:)
LordIkol #17
Posted 30 September 2013 - 08:16 AM
Untested but should give you a clue about how it can work


rednet.open("right")
local running = true

local function atk()
while true do
if running == true then
turtle.attack()
print("attacking")
end
sleep(0.8)
end
end

local function switchit()
event, id, text = os.pullEvent()
if text == "on" then
running = true
elseif text =="off" then
running = false
print("Stop attack")
end
end

while true do
parallel.waitForAny(switchit,atk)
end
end

edit: corrected an error now it should work ;D
Edited on 30 September 2013 - 06:25 AM
Alcheya #18
Posted 30 September 2013 - 08:55 AM
Untested but should give you a clue about how it can work


rednet.open("right")
local running = true

local function atk()
while true do
if running == true then
turtle.attack()
print("attacking")
end
sleep(0.8)
end
end

local function switchit()
event, id, text = os.pullEvent()
if text == "on" then
running = true
elseif text =="off" then
running = false
print("Stop attack")
end
end

while true do
parallel.waitForAny(switchit,atk)
end
end

edit: corrected an error now it should work ;D

It's working, but making me lag horribly, but it might be because I did the first one (pre-correction) and it crashed me horribly.

2013-09-30 08:46:10 [INFO] [STDOUT] ComputerCraft: Error running task.
Spoiler
[color="#FF7070"]2013-09-30 08:46:10 [INFO] [STDERR] java.lang.ThreadDeath[/color]
[color="#FF7070"]2013-09-30 08:46:10 [INFO] [STDERR] at java.lang.Thread.stop(Unknown Source)[/color]
[color="#FF7070"]2013-09-30 08:46:10 [INFO] [STDERR] at dan200.computer.core.ComputerThread$1.run(ComputerThread.java:153)[/color]
[color="#FF7070"]2013-09-30 08:46:10 [INFO] [STDERR] at java.lang.Thread.run(Unknown Source)[/color]

Going to give it a go after I restart.
jay5476 #19
Posted 30 September 2013 - 09:19 AM
this would be simpler

atk = false
rednet.open(sidehere)
function listen()
  _,mes,_ = rednet.receive()
  if mes == "off" then
    atk = false
  elseif mes == "on" then
    atk = true
  end
end

function attack()
  while atk do
    turtle.attack()
    sleep(0)
  end
end

parallel.waitForAny(attack,listen)
Wojbie #20
Posted 30 September 2013 - 09:32 AM
this would be simpler

atk = false
rednet.open(sidehere)
function listen()
  _,mes,_ = rednet.receive()
  if mes == "off" then
	atk = false
  elseif mes == "on" then
	atk = true
  end
end

function attack()
  while atk do
	turtle.attack()
	sleep(0)
  end
end

parallel.waitForAny(attack,listen)

But in this code function attack() would end if atk is false - and it would end parralel - and program would end :P/>
same with listen() - ends after one message ending whole program.
Alcheya #21
Posted 30 September 2013 - 09:56 AM
Untested but should give you a clue about how it can work
Spoiler

rednet.open("right")
local running = true

local function atk()
while true do
if running == true then
turtle.attack()
print("attacking")
end
sleep(0.8)
end
end

local function switchit()
event, id, text = os.pullEvent()
if text == "on" then
running = true
elseif text =="off" then
running = false
print("Stop attack")
end
end

while true do
parallel.waitForAny(switchit,atk)
end
end
edit: corrected an error now it should work ;D


Yes, it works and works well, but any reason why it would be incredibly more laggy than the standard "while true do" turtle attack program?

(with the normal program and all my machines/etc running, I'm normally sitting at about 50 fps.. with them running I dropped down to 6/7, even in another dimension with no mobs spawned)
Alcheya #22
Posted 30 September 2013 - 04:45 PM
Yep, something is definitely not right there. 60 fps without, down to 40 fps while the code is running, but when they go into attack mode, instant 6 fps. Sad times. :(/>
Wojbie #23
Posted 30 September 2013 - 04:55 PM
I don't see how it could cause lag - I am using similar setup with more complicated code on turtles and more turtles - and no lag - what mods are you using/version of minecraft/SSP or SMP?
Alcheya #24
Posted 30 September 2013 - 05:09 PM
I don't see how it could cause lag - I am using similar setup with more complicated code on turtles and more turtles - and no lag - what mods are you using/version of minecraft/SSP or SMP?

I'm sorry about that.

Using FTB Unleashed 1.1.4 SSP, but you're right, I don't know whats causing it. I was using 8 turtles with attack loop just fine the other day, but I guess something I did in the last 2 days ruined CC. I tried just doing basic attack loop on 1 turtle and I go from 60-70 fps down to 20. Ugh..
Engineer #25
Posted 01 October 2013 - 08:43 AM
To the fps problem, I would suggest to make a more reliable setup. Make the mobs move to one place via water or something, and then have one or two turtles just killing them. Saves space in your world and it should save a lot of 'lag'.

Thats the point of a mob grinder, have it as easy as possible and kill it. With your current setup. how are you going to collect the loot while you have the grinder running? Then the turtle is similair to lava, only there is no xp dropped.
LordIkol #26
Posted 02 October 2013 - 03:17 AM
Hi there sorry have not much time at the moment

I checked my Code again and see that you can reduce it to one while loop cause the parralell is executed in a while true loop you dont need the loop in the atk function.
Depending on your setup you can play around with the sleep command a bit to increase performance.
to make it easier for you to play around with the delay i added the delay as argument
if you start your program without arguments the delay is 1 second. if you want to start it with another delay just use <yourprogramname> x
where x is delay between attacks in seconds


rednet.open("right")
local running = true
args = ... or 1

local function atk()
if running == true then
turtle.attack()
print("attacking")
sleep(tonumber(args))
else
print("wait for command")
sleep(10)
end
end

local function switchit()
event, id, text = os.pullEvent()
if text == "on" then
running = true
elseif text =="off" then
running = false
print("Stop attack")
end
end

while true do
parallel.waitForAny(switchit,atk)
end

Greets Loki
Edited on 02 October 2013 - 01:34 AM
keyboardhack #27
Posted 04 October 2013 - 07:40 PM
do this

cake = true
while true do
os.startTimer(1)
e,s,msg,dis = os.pullEvent()
if (e == "timer" and cake == true) then
turtle.attack()
elseif e == "rednet_message" then
if msg == "stop attacking" then
  cake = false
elseif msg == "start attacking" then
  cake = true
end
end
will stop the turtels when it receives a rednet message saying "stop attacking" and start again when it receives a rednet message saying "start attacking"
MudkipTheEpic #28
Posted 04 October 2013 - 07:58 PM
The last

cake==true

needs to be:

cake=true