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

Programming help

Started by random987654321, 20 December 2012 - 03:50 PM
random987654321 #1
Posted 20 December 2012 - 04:50 PM
Ive got a few programs and want to know if they will work and how to improve them.
theoriginalbit #2
Posted 20 December 2012 - 04:51 PM
You forgot to give the programs ;)/>

EDIT: However I can tell you right now that you forgot to use cases correctly. Lua is case sensitive. so using turtle.getitemcount will not work, you need to use turtle.getItemCount (notice that camel case is used there :)/> )
random987654321 #3
Posted 20 December 2012 - 04:52 PM
This is my first one called Desert storm.

 rednet.open("right")
while message ~= "Desert Storm" do
   sender,message,dis=rednet.receive()
end
function a()
   turtle.forward()
   turtle.placeDown()
end
turtle.select(1)
for i = 1, 64 do
   a()
end
For I=1, 64 do while not turtle.forward()
end
rednet.close("right")


I don't think the
 thing worked
theoriginalbit #4
Posted 20 December 2012 - 04:53 PM
I don't think the
 thing worked

you must also add to the end of the code (without the space obviously)
random987654321 #5
Posted 20 December 2012 - 04:55 PM
I would also like to improve this code.

local message = ""

while message ~= "Self destruct" do
   sender,message,dis=rednet.receive()
end

function die()
   redstone.setOutput("bottom",true)
   turtle.select(5)
   turtle.placeDown()
   turtle.select(6)
   turtle.placeDown()
   turtle.down()
end

die()

Orwell #6
Posted 20 December 2012 - 04:56 PM
Put at the bottom.
You can drop the 'local message = "" '. It's in the wrong spot, but it's redundant anyway. (my fault ;)/>)
Also, 'go forward 64' is no valid Lua, it's a CraftOS command/program (not the same thing). You can use this instead:

for i=1,64 do while not turtle.forward() do turtle.dig() end end

Edit: at the bottom,
 at the top...
theoriginalbit #7
Posted 20 December 2012 - 04:59 PM
You are also forgetting to close rednet in the first program, while this doesn't cause an issue its good practise that if you open it, you close it. :)/> ( use rednet.close("right") to close it :)/> )
random987654321 #8
Posted 20 December 2012 - 05:04 PM
I also have a question I can't find the answer to.

How can you amplify a signal or make the signal go through repeaters like vanilla redstone?

I want signals to go long distances.
theoriginalbit #9
Posted 20 December 2012 - 05:08 PM
I also have a question I can't find the answer to.

How can you amplify a signal or make the signal go through repeaters like vanilla redstone?

I want signals to go long distances.

You can change the distance in the config. Or you can make computers that get the signal and send it back out again (be warned, if not done correctly this method can cause propagation of signals to the point of crashing the computer, and in worst case the Minecraft instance)
random987654321 #10
Posted 20 December 2012 - 05:11 PM
Can I have the code for that? I think I have the code to receive,
 rednet.open("back")

while true do
   senderid,message,distance=rednet.receive()
   print(senderid..":D/>"..distance..":"..message)
end
But I don't know how to sent out what it receives.
And possibly to stop the infinite propagation of the signal you could sent it through rednet and target a certain computer.
theoriginalbit #11
Posted 20 December 2012 - 05:25 PM
Can I have the code for that? I think I have the code to receive,
 rednet.open("back")

while true do
   senderid,message,distance=rednet.receive()
   print(senderid..":D/>/>/>"..distance..":"..message)
end
But I don't know how to sent out what it receives.

Yeh that looks fine to receive. to send out you use one of 2 functions:

rednet.broadcast( message ) -- this will send to all computers
rednet.send( computerId, message ) -- this will send to the computer that has the same id

And possibly to stop the infinite propagation of the signal you could sent it through rednet and target a certain computer.

You could send it to a target computer, but imagine this. I have 3 computers and one side, and 1 computer on the other side, I have a line of computers (at a decent distance apart) in the middle to relay the message, now if i'm standing at the 1 computer and I want to send to computer #3 on the other end I need to send directly to a given computer, which would be the next one in the middle so it can try to get to comp #3, but then the signal gets to the end of the line, how does the last computer know where to send the message, there are 3 computers there.
Well the solution would be to add the target computer to the message, however then each computer would need to process the message first just to make sure it wasn't the target or to see what the target is so it can then say "hey, which computer to send this to so it can get there". Using this direct sending to IDs should stop propagation the most reliably, however its probably the hardest to implement.

EDIT: However that is just my opinion. I'm sure Orwell may have a different one ;)/>
random987654321 #12
Posted 20 December 2012 - 05:34 PM
Would this be a correct relay code if you get a signal from "a" and want to get it to "c"?

rednet.open("back")

while true do
   senderid,message,distance=rednet.receive()
   print(senderid..":D/>"..distance..":"..message)
rednet.send( "c", "..message)
end
Orwell #13
Posted 20 December 2012 - 05:36 PM
Just be sure to use a number as id, not a string like "c". Besides that it looks ok.
random987654321 #14
Posted 20 December 2012 - 05:44 PM
WooHoo!
I'm learning!
ChunLing #15
Posted 20 December 2012 - 06:02 PM
If you have problems getting things to run, go ahead and post the error messages you get.

If you get no error messages but the program doesn't do what you want, describe exactly what you want it to do and what it does instead (i.e. "I want this computer, ID 54 located at -234,73,406, to relay all rednet messages it gets to my computer with ID 32 located at -134,12,502, but instead it just prints the message").
random987654321 #16
Posted 20 December 2012 - 06:17 PM
Another question.
How do I prevent program termination, reboot, etc. for a pass code lock?
theoriginalbit #17
Posted 20 December 2012 - 06:28 PM
Another question.
How do I prevent program termination, reboot, etc. for a pass code lock?

You cannot prevent reboot, thank goodness! So many times I've had to use ctrl + r while testing my code and it locks up or termination is prevented meaning I need some way to get out.
random987654321 #18
Posted 20 December 2012 - 06:30 PM
Do you know how to prevent termination? I guess you could put the passcode as startup to stop rebooting to get around it.
theoriginalbit #19
Posted 20 December 2012 - 06:34 PM
Do you know how to prevent termination? I guess you could put the passcode as startup to stop rebooting to get around it.

You use the os.pullEventRaw() to prevent termination, there is an event "terminated" that is returned as the first value from os.pullEventRaw() which you can use to detect that they have tried to and that they aren't allowed to or cant. :)/>

Also any startup that is on a disk overrides the startup on a computer.
random987654321 #20
Posted 20 December 2012 - 07:13 PM
This is a mine setter code I would like to be looked at and improved.

print("put a preasure plate in slot 1, dirt in slot 2, and tnt in slot 3.")
function mine()
   turtle.digDown()
   turtle.down()
   turtle.digDown()
   turtle.down()
   turtle.dig()
   turtle.select(3)
   turtle.place()
   turtle.select(2)
   turtle.up()
   turtle.placeDown()
   turtle.up()
   turtle.placeDown()
   turtle.select(1)
   turtle.place()
end
mine()
turtle.up()
for i = 1, 3 do
turtle.forward()
end
turtle.down()
end
mine()
ChunLing #21
Posted 21 December 2012 - 01:55 AM
How would you like it improved?
random987654321 #22
Posted 21 December 2012 - 02:09 AM
Mabey have it compacted?
Doyle3694 #23
Posted 21 December 2012 - 02:17 AM

function mine()
   for i = 1,2 do
	 turtle.digDown()
	 turtle.down()
   end
   turtle.dig()
   turtle.select(3)
   turtle.place()
   turtle.select(2)
   for i = 1,2 do
	 turtle.up()
	 turtle.placeDown()
   end
   turtle.select(1)
   turtle.place()
end

It doesn't really shorten the code, but makes it abit more eyepleasing to read.
ChunLing #24
Posted 21 December 2012 - 07:05 AM
Hmm…I'd have thought you'd want the overall program to lay more than two mines. Also, using a sand/gravel (gravel makes stone pressure plates harder to spot, if this is intended for PVP) rather than dirt causes the mine to drop the entity which triggers the mine into a pit, which can be made deeper. But this involves reducing the damage to other entities which may be about (though having the tnt under a layer of dirt also does that).

Using nested for loops would let you easily lay a larger minefield, if that's desired.