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

Rednet Help

Started by Andrew2060, 02 December 2015 - 04:44 PM
Andrew2060 #1
Posted 02 December 2015 - 05:47 PM
Hey,

Overview:
I generally design defense programs for whomever that requires it, like myself.
Currently i am working on perfecting a program that utilizes information from a radar to act on.
The program has one purpose and one purpose only, to shoot down incoming missiles.

Functionality:
-1. Server program utilizes radar data.
-2. Incoming target coordinates are serialized and sent to silo's.
-3. The server will continue sending the latest coordinates of target(s) to silos.
-4. Once the target breaches a failsafe, the radar will output a redstone signal.
-5. The redstone signal goes into the left of the PC, triggering firing action.
-6. The server tells the silos to fire at the missile's last sent coordinates.
-7. each silo should have a delay so that they do not fire at the same time.

What i am having an issue with in this program:
(The host is this program im working on)
(The Host controls a network of slave PC's)
  • Not sure if it is serializing the coordinates gained by the radar "x, y, z"
  • Not sure if it is communicating to the other slave silo computers.
  • Have no idea why it seems to error when it detects a missile.
The Code:
Spoiler
  • –[[Anti-Ballistic Missile Server by Andrew2060]]
  • –[[Settings]]
  • local modemSide = "top"
  • local waitDelay = 2
  • allDat = 0
  • –[[Init]]
  • rednet.open(modemSide)
  • local silos = {}
  • –[[Functions]]
  • local function clear()
  • term.clear()
  • term.setCursorPos(1, 1)
  • end
  • term.setBackgroundColor(colors.blue)
  • clear()
  • local function findSilos()
  • rednet.broadcast("ping silo")
  • local timerID = os.startTimer(waitDelay)
  • while true do
  • event, id, msg, distance = os.pullEvent()
  • if event == "rednet_message" and msg == "pong" then
  • table.insert(silos, id)
  • timerID = os.startTimer(waitDelay)
  • elseif event == "timer" and id == timerID then
  • return
  • end
  • end
  • end
  • local function printSilos()
  • clear()
  • print("===============================")
  • print(" [Detected silos] ")
  • for k, v in ipairs(silos) do
  • print(" silo #" .. k .. " id = "..v)
  • end
  • print("===============================")
  • term.setBackgroundColor(colors.red)
  • print(" ")
  • print(" ")
  • term.setBackgroundColor(colors.blue)
  • end
  • –[[Main program]]
  • findSilos()
  • while true do
  • printSilos()
  • if redstone.getInput("left") then
  • term.clear()
  • term.setCursorPos(1,1)
  • term.setTextColor(colors.red)
  • print("Incomming targets:")
  • term.setTextColor(colors.white)
  • maptab = peripheral.call("back","getEntities")
  • maptxt = textutils.serialize(maptab)
  • if maptxt ~= "{}" then
  • allDat = 0
  • for num in pairs(maptab) do
  • allDat = allDat+1
  • end
  • targets = allDat/3
  • for i=0,targets-1 do
  • local x = math.floor(tonumber(maptab["x_"..i])/1)
  • local y = math.floor(tonumber(maptab["y_"..i])/1)
  • local z = math.floor(tonumber(maptab["z_"..i])/1)
  • local msg = {x,y,z}
  • rednet.send(silos[i], msg)
  • end
  • print("Target #"..i.." at X:"..x.." Y:"..y.." Z:"..z.")
  • end
  • end
  • sleep(.0001)
  • end

Would be appreciated if someone can instruct me as to where my error lies.
As well as propose the way it could be fixed or re rewritten.

Pastebin links:
Master = http://pastebin.com/vK6jGNge
Slave = http://pastebin.com/exm7HRhr

The problem lies somewhere in obtaining/generating values for the ""Msg"" to silos.

The program works fine until it detects a missile, then it tells me there is something wrong on the line i serialize it on.
{I screwed up the BB code some how in that post, so it wont show the line numbers and what not.}

The error is the maptxt = textutils.serialize(maptab)

Thanks in advance!
Edited on 02 December 2015 - 04:49 PM
Link149 #2
Posted 02 December 2015 - 06:35 PM
  • Not sure if it is serializing the coordinates gained by the radar "x, y, z"

According to the wiki, the type of message returned by rednet.receive() is "any".
So I don't think it is getting serialized. Either that or everything happens behind the scenes and so the receiving computer reconverts it back into a table.

EDIT:
Yeah, I just tested that in CCEmu Redux and the table seems not to be serialized.
I did not even know you could now send types other than strings across rednet.
Edited on 02 December 2015 - 05:39 PM
Bomb Bloke #3
Posted 02 December 2015 - 11:17 PM
The program works fine until it detects a missile, then it tells me there is something wrong on the line i serialize it on.
{I screwed up the BB code some how in that post, so it wont show the line numbers and what not.}

The error is the maptxt = textutils.serialize(maptab)

So that's the line, but… what's the error say??

It would be helpful if you have some examples as to the possible formats of the maptab table. Seems to me that there's no reason for you to attempt to serialise it in the first place (and that pairs loop afterwards has certainly got problems), but it's difficult to suggest fixes without knowing the structure of the data the sensor gives you.

One thing I can tell you is that this line:

local msg = {x,y,z}

… builds a table such that:

msg[1] == whatever x was
msg[2] == whatever y was
msg[3] == whatever z was

Looks like you were going for:

local msg = {["x"] = x, ["y"] = y, ["z"] = z}

… which'll result in:

msg.x == whatever x was
msg.y == whatever y was
msg.z == whatever z was

According to the wiki, the type of message returned by rednet.receive() is "any".
So I don't think it is getting serialized. Either that or everything happens behind the scenes and so the receiving computer reconverts it back into a table.

EDIT:
Yeah, I just tested that in CCEmu Redux and the table seems not to be serialized.
I did not even know you could now send types other than strings across rednet.

Yes, as of about CC 1.53 you can send tables directly through modems / rednet. Though I believe Andrew was wondering about the line where he specifically called textutils.serialise(), as opposed to the one where he tried to transmit the table.