Client

  • Upload
    patt

  • View
    216

  • Download
    0

Embed Size (px)

DESCRIPTION

Turtle Program for Computercraft learned from Gaming with Ben

Citation preview

-- to, from, {message}, {route}run=trueparentID=-1myID = os.getComputerID()numHops=9999status="idle"lastGood = {0,0,0}local function sendSer(to, from, msg, route)local toID = parentIDif to ~= "server" thentoID = table.remove(route)elsetable.insert(route,myID)endrednet.send(tonumber(toID),textutils.serialize({to, from, msg, route}))endrednet.open("right")print("requesting route: "..myID) rednet.broadcast(textutils.serialize({"X", myID, {"getRoute"}, {myID}}))function pingStatus()sendSer("server", myID, {"status", status}, {myID}) --something here)print("Ping: "..status)endfunction repeatStatus()sleep(30)if parentID > -1 thenpingStatus()endwhile status == "working" dosleep(10)endendfunction rednetMsg()local event,sender,message,dist=os.pullEvent("rednet_message")local fullMsg = textutils.unserialize(message)local to = fullMsg[1]local from = fullMsg[2]local msg = fullMsg[3]local route = fullMsg[4] print("Received from "..sender)if sender == myID then--just ignore it--elseif to == myID thenlastGood = t.getLoc()if msg[1] == "routeInfo" thenprint("Checking route...")if msg[2] < numHops thenparentID = fromnumHops = msg[2] + 1print("Got new route, Parent: "..from..". Hops: "..numHops..".")pingStatus()endelseif parentID == -1 thenos.queueEvent(event, sender, message, dist)elseif msg[1] == "command" thenstatus="working"pingStatus()shell.run(msg[2], msg[3])status="idle"pingStatus()endendendwhile run doparallel.waitForAny(rednetMsg, repeatStatus)end