Xyzzy

CC/MP/OCS - in-progress base control

Feb 20th, 2015
36
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --MoarPeripherals/OCS Random control thingy by wok86
  2. --Uses the ChatBox from MoarPeripherals to get commands
  3. --Uses OCS for the player detection bit
  4.  
  5. local ID = "MB"                     --Identifier of the system. Change to whatever you want
  6. local playerName = "wok86"          --Username of the person the system will respond to
  7. local cbSide = "top"                --Side of the MP chatbox
  8. local sensorSide = "back"           --Side the OCS sensor is on
  9. local sleepTime = 0.5               --Time between cycles, used for lag reduction
  10. local tzone = "+11"                 --GMT timezone for ltime
  11. local rainPCID = 79                 --ID of the PC used for rain control
  12. local minePCID = 82                 --ID of the quarry control pc
  13. local radius = 27                   --Range of the sensor to scan for players, spherical
  14. local mSide = "left"                --Side the Wireless Modem is on
  15. local hX = 895                      --X coordinate of the turtle "home" command
  16. local hY = 34                       --Y coordinate of the turtle "home" command
  17. local hZ = -1818                    --Z coordinate of the turtle "home" command
  18.  
  19. --Offset for the player scan area, from the sensor. Use relative values
  20. local offset = {
  21.   X = 0,
  22.   Y = 0,
  23.   Z = 0
  24. }
  25.  
  26.  
  27. --Don't edit past here
  28. local cb = peripheral.wrap(cbSide)
  29. local pcID = "[ID: "..ID.."]"
  30. os.loadAPI("ocs/apis/sensor")
  31.  
  32. function distance(pos)
  33.   local xd = pos.X - offset.X
  34.   local yd = pos.Y - offset.Y
  35.   local zd = pos.Z - offset.Z
  36.   return math.sqrt(xd*xd + yd*yd + zd*zd)
  37. end
  38.  
  39.  
  40. while true do
  41.   local command = {os.pullEvent("chatbox_command")}
  42.   local cArgs = {}
  43.   local ccommand = command[4]
  44.   local cindex = 1
  45.   for value in string.gmatch(ccommand, "%S+") do
  46.     cArgs[cindex] = value
  47.     cindex = cindex + 1
  48.   end
  49.   if command[3] == playerName then
  50.     print("Received command: "..command[4])
  51.     if cArgs[1] == ID.."-players" then
  52.       local pir = {}
  53.       local s = sensor.wrap(sensorSide)
  54.       local targets = s.getTargets()
  55.       for target in pairs(targets) do
  56.         local info = s.getTargetDetails(target)
  57.         if info.Name == "Player" then
  58.           if distance(info.Position) < radius then
  59.             table.insert(pir, info.Username)  
  60.           end
  61.         end
  62.       end
  63.       cb.tell(command[3], pcID.." Players in range are: "..table.concat(pir,", "))
  64.       print("Players in range are: "..table.concat(pir,", "))
  65.     elseif cArgs[1] == ID.."-gtime" then
  66.       local ostime = os.time()
  67.       local formattedTime = textutils.formatTime(ostime, false)
  68.       cb.tell(command[3], pcID.." Current game time is: "..formattedTime)
  69.       print("Current game time is: "..formattedTime)
  70.     elseif cArgs[1] == ID.."-ltime" then
  71.       if string.find(tzone, "+") then
  72.         sTime = "in"..tzone.."+hours"
  73.       elseif string.find(tzone, "-") then
  74.         sTimet = string.sub(tzone, 2)
  75.         sTime = sTimet.."+hours+ago"
  76.       end
  77.       local localTime = http.get("http://www.timeapi.org/utc/"..sTime.."?format=%25I:%25M%20%25p").readAll()
  78.       cb.tell(command[3], pcID.." Current local time (GMT "..tzone..") is: "..localTime)
  79.       print("Current local time (GMT "..tzone..") is: "..localTime)
  80.     elseif cArgs[1] == ID.."-turtle" then
  81.       if mSide ~= nil then
  82.         rednet.open(mSide)
  83.         local tArgs = {}
  84.         local tcommand = command[4]
  85.         local index = 1
  86.         for value in string.gmatch(tcommand, "%S+") do
  87.           tArgs[index] = value
  88.           index = index + 1
  89.         end
  90.         if tArgs[2] ~= nil then
  91.           if tArgs[3] == "goto" then
  92.             if tArgs[4] ~= nil then
  93.               if tArgs[5] ~= nil then
  94.                 if tArgs[6] ~= nil then
  95.                   local id = tonumber(tArgs[2])
  96.                   cb.tell(command[3], pcID.." Turtle ID "..tArgs[2].." instructed to go to: "..tArgs[4]..", "..tArgs[5]..", "..tArgs[6])
  97.                   print("Turtle ID "..tArgs[2].." instructed to go to: "..tArgs[4]..", "..tArgs[5]..", "..tArgs[6])
  98.                   rednet.send(id, "goto "..tArgs[4].." "..tArgs[5].." "..tArgs[6])
  99.                 else
  100.                   cb.tell(command[3], pcID.." No Z co-ordinate specified")
  101.                   print("No Z co-ordinate specified")
  102.                 end
  103.               else
  104.                 cb.tell(command[3], pcID.." No Y co-ordinate specified")
  105.                 print("No Y co-ordinate specified")
  106.               end
  107.             else
  108.               cb.tell(command[3], pcID.." No X co-ordinate specified")
  109.               print("No X co-ordinate specified")
  110.             end
  111.           elseif tArgs[3] == "dance" then
  112.             local id = tonumber(tArgs[2])
  113.             print("Turtle ID "..id.." has been instructed to DANCE!")
  114.             rednet.send(id, "dance")
  115.             cb.tell(command[3], pcID.." Turtle ID "..id.." is about to DANCE!")
  116.             rednet.close()
  117.           elseif tArgs[3] == "go" then
  118.             if tArgs[5] == nil then
  119.               local id = tonumber(tArgs[2])
  120.               cb.tell(command[3], pcID.." Turtle ID "..id.." sent command go "..tArgs[4])
  121.               print("Turtle ID "..id.." sent command go "..tArgs[4])
  122.               rednet.send(id, "go "..tArgs[4])
  123.               rednet.close()
  124.             else
  125.               local id = tonumber(tArgs[2])
  126.               cb.tell(command[3], pcID.." Turtle ID "..id.." sent command go "..tArgs[4].." "..tArgs[5])
  127.               print("Turtle ID "..id.." sent command go "..tArgs[4].." "..tArgs[5])
  128.               rednet.send(id, "go "..tArgs[4].." "..tArgs[5])
  129.               rednet.close()
  130.             end
  131.           elseif tArgs[3] == "turn" then
  132.             if tArgs[5] == nil then
  133.               local id = tonumber(tArgs[2])
  134.               cb.tell(command[3], pcID.." Turtle ID "..id.." sent command turn "..tArgs[4])
  135.               print("Turtle ID "..id.." sent command turn "..tArgs[4])
  136.               rednet.send(id, "turn "..tArgs[4])
  137.               rednet.close()
  138.             else
  139.               local id = tonumber(tArgs[2])
  140.               cb.tell(command[3], pcID.." Turtle ID "..id.." sent command turn "..tArgs[4].." "..tArgs[5])
  141.               print("Turtle ID "..id.." sent command turn "..tArgs[4].." "..tArgs[5])
  142.               rednet.send(id, "turn "..tArgs[4].." "..tArgs[5])
  143.               rednet.close()
  144.             end
  145.           elseif tArgs[3] == "home" then
  146.             local id = tonumber(tArgs[2])
  147.             print("Turtle ID "..id.." has been instructed to go home ("..hX..", "..hY..", "..hZ..")")
  148.             cb.tell(command[3], pcID.." Turtle ID "..id.." has been instructed to go home ("..hX..", "..hY..", "..hZ..")")
  149.             rednet.send(id, "goto "..hX.." "..hY.." "..hZ)
  150.             rednet.close()
  151.           end
  152.         end
  153.       else
  154.         cb.tell(command[3], pcID.." No modem side set.")
  155.         print("No modem side set")
  156.       end
  157.     elseif cArgs[1] == ID.."-rain" then
  158.       print("rain command")
  159.       if mSide ~= nil then
  160.         rednet.open(mSide)
  161.         if rainPCID ~= nil then
  162.           if cArgs[2] ~= nil then
  163.             if cArgs[2] == "off" then
  164.               cb.tell(command[3], pcID.." Sent command 'off' to RainPC")
  165.               print("Sent command 'off' to RainPC")
  166.               rednet.send(rainPCID, "rainoff")
  167.               local id,msg = rednet.receive()
  168.               if id == rainPCID then
  169.                 if msg == "done" then
  170.                   cb.tell(command[3], pcID.." Rain should now be stopping")
  171.                   print("rain stop confirmed")
  172.                 elseif msg == "norain" then
  173.                   cb.tell(command[3], pcID.." It is not currently raining - no action taken")
  174.                   print("norain confirmed")
  175.                 end
  176.               end
  177.             elseif cArgs[2] == "on" then
  178.               cb.tell(command[3], pcID.." NO POLLEN, SO NO RAIN 4 U")
  179.               print("received on cmd")
  180.             end
  181.           else
  182.             cb.tell(command[3], pcID.." Must specify ON or OFF")
  183.           end
  184.         else
  185.           cb.tell(command[3], pcID.." No rainPCID specified, so this function is disabled")
  186.         end
  187.         rednet.close()
  188.       else
  189.         cb.tell(command[3], pcID.." No modem side set.")
  190.         print("no modem side set")
  191.       end
  192.     elseif cArgs[1] == ID.."-quarry" then
  193.       if mSide ~= nil then
  194.         rednet.open(mSide)
  195.         print("quarry command")
  196.         if minePCID ~= nil then
  197.           if cArgs[2] ~= nil then
  198.             if cArgs[2] == "off" then
  199.               cb.tell(command[3], pcID.." Sent command 'off' to QuarryPC")
  200.               print("sent command 'off' to quarrypc")
  201.               rednet.send(minePCID, "stopmine")
  202.               local id,msg = rednet.receive()
  203.               if id == minePCID then
  204.                 if msg == "nowminestop" then
  205.                   cb.tell(command[3], pcID.." Quarry now stopped")
  206.                 elseif msg == "preminestop" then
  207.                   cb.tell(command[3], pcID.." Quarry already stopped")
  208.                 end
  209.               end
  210.             elseif cArgs[2] == "on" then
  211.               cb.tell(command[3], pcID.." Sent command 'on' to QuarryPC")
  212.               print("sent command 'on' to quarrypc")
  213.               rednet.send(minePCID, "startmine")
  214.               local id,msg = rednet.receive()
  215.               if id == minePCID then
  216.                 if msg == "nowmine" then
  217.                   cb.tell(command[3], pcID.." Quarry now running")
  218.                 elseif msg == "premine" then
  219.                   cb.tell(command[3], pcID.." Quarry already running")
  220.                 end
  221.               end
  222.             elseif cArgs[2] == "status" then
  223.               print("quarry status command")
  224.               rednet.send(minePCID, "statmine")
  225.               local id,msg = rednet.receive()
  226.               if id == minePCID then
  227.                 if msg == "statmineoff" then
  228.                   cb.tell(command[3], pcID.." Quarry not running")
  229.                 elseif msg == "statmineon" then
  230.                   cb.tell(command[3], pcID.. " Quarry currently running")
  231.                 end
  232.               end
  233.             end
  234.           else
  235.             cb.tell(command[3], pcID.." Must specifiy state or status")
  236.           end
  237.         else
  238.           cb.tell(command[3], pcID.." Must specify minePCID for this to work")
  239.         end
  240.       else
  241.         cb.tell(command[3], pcID.." No modem side set")
  242.         print("No modem side set")
  243.       end
  244.     elseif cArgs[1] == ID.."-help" then
  245.       local tArgs = {}
  246.       local tcommand = command[4]
  247.       local index = 1
  248.       for value in string.gmatch(tcommand, "%S+") do
  249.         tArgs[index] = value
  250.         index = index + 1
  251.       end
  252.       print("command list, used ##"..ID.."-help")
  253.       if tArgs[2] ~= nil then
  254.         if tArgs[2] == "players" then
  255.           cb.tell(command[3], pcID.." ##"..ID.."-players - Gets a list of nearby players")
  256.         elseif tArgs[2] == "gtime" then
  257.           cb.tell(command[3], pcID.." ##"..ID.."-gtime - Gets the in-game time")
  258.         elseif tArgs[2] == "ltime" then
  259.           cb.tell(command[3], pcID.." ##"..ID.."-ltime - Gets the local time")
  260.         elseif tArgs[2] == "rain" then
  261.           cb.tell(command[3], pcID.." ##"..ID.."-rain <on/off> - Uses an iodine/dissipation charge on a rainmaker. Requires external setup and rainPCID to be set")
  262.         elseif tArgs[2] == "turtle" then
  263.           if tArgs[3] ~= nil then
  264.             if tArgs[3] == "home" then
  265.               cb.tell(command[3], pcID.." ##"..ID.."-turtle <id> home - Sends the turtle to the home co-ordinates")
  266.             elseif tArgs[3] == "dance" then
  267.               cb.tell(command[3], pcID.." ##"..ID.."-turtle <id> dance - Makes the turtle DANCE~!")
  268.             elseif tArgs[3] == "go" then
  269.               cb.tell(command[3], pcID.." ##"..ID.."-turtle <id> go <direction> <<num>> - Makes the turtle move in specified direction, and how many times (optional)")
  270.             elseif tArgs[3] == "turn" then
  271.               cb.tell(command[3], pcID.." ##"..ID.."-turtle <id> turn <direction> <<num>> - Makes the turtle turn in the specified direction, and how many times (optional)")
  272.             elseif tArgs[3] == "goto" then
  273.               cb.tell(command[3], pcID.." ##"..ID.."-turtle <id> goto <x> <y> <z> - Sends the turtle to the specified co-ordinates")
  274.             end
  275.           elseif tArgs[2] == "quarry" then
  276.             if tArgs[3] ~= nil then
  277.               if tArgs[3] == "on" then
  278.                 cb.tell(command[3], pcID.." ##"..ID.."-quarry on - turns the frame quary on")
  279.               elseif tArgs[3] == "off" then
  280.                 cb.tell(command[3], pcID.." ##"..ID.."-quarry off - turns the frame quarry off")
  281.               elseif tArgs[3] == "status" then
  282.                 cb.tell(command[3], pcID.." ##"..ID.."-quarry status - returns the current status of the frame quarry")
  283.               end
  284.             else
  285.               cb.tell(command[3], pcID.." ##"..ID.."-quarry <off/on/status> - use ##"..ID.."-help quarry <option> for more details")
  286.             end
  287.           else
  288.             cb.tell(command[3], pcID.." ##"..ID.."-turtle <id> <function> - Current functions are home,dance,go,turn,goto")
  289.           end
  290.         end
  291.       else
  292.         cb.tell(command[3], pcID.." The current usable commands are: ##"..ID.."-players, ##"..ID.."-gtime, ##"..ID.."-ltime, ##"..ID.."-turtle, ##"..ID.."-rain, ##"..ID.."-quarry. Use ##"..ID.."-help <command> for more info")
  293.       end
  294.     end
  295.   end
  296.   sleep(0.5)
  297. end
RAW Paste Data