daily pastebin goal
11%
SHARE
TWEET

ComputerCraft Tutorial: msg_sender_0_6

hevohevo Mar 18th, 2014 1,639 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- ##############################
  2. -- msg_sender
  3. -- version 0.6
  4. -- (a paired program: msg_receiver 0.6 http://pastebin.com/ZsGmzz2T)
  5. -- http://hevohevo.hatenablog.com/
  6.  
  7. rednet.open("right")
  8.  
  9. local args = {...}
  10. assert(args and #args>0, "msg_sender <receiver_id>")
  11. local receiver_id = tonumber(args[1])
  12.  
  13. local trans_tbl = {
  14.   w="turtle.forward()",
  15.   a="turtle.turnLeft(); turtle.forward(); turtle.turnRight()",
  16.   s="turtle.back()",
  17.   d="turtle.turnRight(); turtle.forward(); turtle.turnLeft()",
  18.   space="turtle.up()",
  19.   leftShift="turtle.down()",
  20.   left="turtle.turnRight()",
  21.   right="turtle.turnLeft()",
  22.   z="shell.run('dance')", -- let's dance!
  23.   x="shell.run('boring')", -- run receiver's other program
  24.   c="myRefuelAll()" -- call receiver's function
  25. }
  26.  
  27. function printResults(sender_id, tbl_results)
  28.    local exist_status, run_status, err_msg = unpack(tbl_results)
  29.    if exist_status then -- found a function
  30.       if run_status then -- success for running
  31.          print(" #",sender_id,": ok")
  32.       else -- failed with running or doesn't reply
  33.          if err_msg then
  34.             print(" #",sender_id,": (error) ",err_msg)
  35.          else
  36.             print(" #",sender_id,": no reply")
  37.          end
  38.       end
  39.    else -- not found by wrong string
  40.       print(" #",sender_id,": (NotFound) ")
  41.   end
  42. end
  43.  
  44. function interactiveMode()
  45.     print(" Start: interactive mode")
  46.     write("  > ")
  47.     local message = io.read()
  48.     print(" Exit: interactive mode")
  49.     return message
  50. end
  51.  
  52. print("Press 'q' to terminate")
  53. while true do
  54.   local event, scancode = os.pullEvent("key")
  55.   local keyname = keys.getName(scancode)
  56.   local message = trans_tbl[keyname]
  57.  
  58.   if keyname == "enter" and not message then
  59.      message = interactiveMode()
  60.   end
  61.   print(keyname," => ",message or "none")
  62.  
  63.   -- button for terminating
  64.   if keyname == "q" or message =="q" then break end
  65.  
  66.   -- send an order message and recieve the results of running
  67.   if message then
  68.     rednet.send(receiver_id, message)
  69.     local sender_id, results, distance = rednet.receive(3)
  70.     printResults(sender_id, textutils.unserialize(results))
  71.   end
  72. end
  73. print("Terminated")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top