Advertisement
NickM13

[ComputerCraft] RC Host

Feb 14th, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.12 KB | None | 0 0
  1. args = {...}
  2.  
  3. command = {}
  4. channel = 13371
  5. modem = nil
  6. connectedtospy = false
  7. monitor = false
  8.  
  9. width = 50
  10. length = 50
  11. height = 10
  12.  
  13. position = {}
  14. position.x = math.floor(width / 2)
  15. position.z = math.floor(length / 2)
  16. position.y = math.floor(height / 2)
  17.  
  18. selecteditem = 0
  19. extramsg = "m"
  20.  
  21. rotation = 0 --0 = fore, 1 = right, 2 = back, 3 = left << relative to original orientation
  22.  
  23. map = {}
  24. for y=1, height do
  25.     map[y] = {}
  26.     for x=1, width do
  27.         map[y][x] = {}
  28.         for z=1, length do
  29.             map[y][x][z] = 0
  30.         end
  31.     end
  32. end
  33.  
  34. if args ~= nil then
  35.     if tonumber(args[1]) then
  36.         channel = math.abs(args[1]%65535)
  37.     end
  38. end
  39.  
  40. local function setCommands()
  41.     command.forward = 17 --W
  42.     command.back = 31 --S
  43.     command.right = 32 --D
  44.     command.left = 30 --A
  45.     command.up = 57 --Space
  46.     command.down = 42 --LShift
  47.     command.dig = 33 --F
  48.     command.digUp = 34 --G
  49.     command.digDown = 35 --H
  50.     command.place = 18 --E
  51.     command.placeUp = 19 --R
  52.     command.placeDown = 20 --T
  53.     command.connect = 16 --Q
  54.     command.pastebin = 25 --P
  55.     command.write = 46 --C
  56.     command.refuel = 47 --V
  57.     command.getfuel = 48 --B
  58.     command.success = 11 --success return
  59.     command.fail = 12 --failed return
  60.     command.unknown = 13 --unknown return
  61.     command.nofuel = 14
  62. end
  63.  
  64. local function setModem()
  65.     for i,v in pairs(rs.getSides()) do
  66.         if (peripheral.getType(v) == "modem") then
  67.             modem = peripheral.wrap(v)
  68.             if modem.isWireless() then
  69.                 return v
  70.             else modem = nil end
  71.         end
  72.     end
  73.     return nil
  74. end
  75.  
  76. local function stepforward()
  77.         if rotation == 0 then   position.z = position.z - 1
  78.     elseif rotation == 1 then   position.x = position.x + 1
  79.     elseif rotation == 2 then   position.z = position.z + 1
  80.     elseif rotation == 3 then   position.x = position.x - 1
  81.     end
  82. end
  83.  
  84. local function stepbackward()
  85.         if rotation == 0 then   position.z = position.z + 1
  86.     elseif rotation == 1 then   position.x = position.x - 1
  87.     elseif rotation == 2 then   position.z = position.z - 1
  88.     elseif rotation == 3 then   position.x = position.x + 1
  89.     end
  90. end
  91.  
  92. local function move(direction, success)
  93.     extramsg = success
  94.     if direction == "forward" then
  95.         if success == command.success then
  96.             stepforward()
  97.             map[position.y][position.x][position.z] = 1
  98.         elseif success == command.fail then
  99.             stepforward()
  100.             map[position.y][position.x][position.z] = 2
  101.             stepbackward()
  102.         elseif success == command.unknown then
  103.             stepforward()
  104.             map[position.y][position.x][position.z] = 0
  105.             stepbackward()
  106.         end
  107.     elseif direction == "back" then
  108.         if success == command.success then
  109.             stepbackward()
  110.             map[position.y][position.x][position.z] = 1
  111.         elseif success == command.fail then
  112.             stepbackward()
  113.             map[position.y][position.x][position.z] = 2
  114.             stepforward()
  115.         elseif success == command.unknown then
  116.             stepbackward()
  117.             map[position.y][position.x][position.z] = 0
  118.             stepforward()
  119.         end
  120.     end
  121. end
  122.  
  123. local function turn(direction)
  124.     if direction == "right" then
  125.         rotation = (rotation + 1) % 4
  126.     elseif direction == "left" then
  127.         rotation = (rotation - 1) % 4
  128.     end
  129. end
  130.  
  131. local function transmit(id)
  132.     modem.transmit(channel, channel, id)
  133. end
  134.  
  135. local function receive()
  136.     local event, side, sentChannel,
  137.         replyChannel, message, distance = os.pullEvent("modem_message")
  138.     return message
  139. end
  140.  
  141. local function checkConnection()
  142.     if connectedtospy == false then
  143.         transmit("ping")
  144.         io.write("Connecting... ")
  145.         if receive() == "pong" then
  146.             connectedtospy = true
  147.             print("Connected to Spy.")
  148.             return true
  149.         end
  150.         print("Unable to connect to Spy.")
  151.         connectedtospy = false
  152.         return false
  153.     end
  154.     return true
  155. end
  156.  
  157. camera = {}
  158. camera.x = 0
  159. camera.z = 0
  160. local function render()
  161.     if position.x < 10 then camera.x = 0
  162.     elseif position.x > width - 10 then camera.x = width - 20
  163.     else camera.x = position.x - 10 end
  164.    
  165.     if position.z < 13 then camera.z = 0
  166.     elseif position.z > length - 13 then camera.z = length - 26
  167.     else camera.z = position.z - 13 end
  168.     for x=1, math.min(width, 26) do
  169.         for z=1, math.min(length, 18) do
  170.             term.setCursorPos(x, z)
  171.                 if (map[position.y][x+camera.x][z+camera.z] == 0) then term.setBackgroundColor(colors.black)
  172.             elseif (map[position.y][x+camera.x][z+camera.z] == 1) then term.setBackgroundColor(colors.green)
  173.             elseif (map[position.y][x+camera.x][z+camera.z] == 2) then term.setBackgroundColor(colors.red)
  174.             end
  175.             io.write(" ")
  176.         end
  177.     end
  178.     term.setBackgroundColor(colors.green)
  179.     term.setTextColor(colors.white)
  180.     term.setCursorPos(position.x - camera.x, position.z - camera.z)
  181.         if rotation == 0 then   io.write("^")
  182.     elseif rotation == 1 then   io.write(">")
  183.     elseif rotation == 2 then   io.write("v")
  184.     elseif rotation == 3 then   io.write("<")
  185.     end
  186.     term.setCursorPos(1, 19)
  187.     --print("Pos: "..position.x..","..position.y..","..position.z)
  188.     io.write("Item: "..selecteditem)
  189.     term.setCursorPos(9, 20)
  190.     io.write(extramsg)
  191. end
  192.  
  193. local function checkInput()
  194.     os.sleep(0.5)
  195.     event, key = os.pullEvent('key')
  196.     if not checkConnection() then
  197.         return
  198.     end
  199.     --print(event.." "..key)
  200.     if monitor then
  201.         if key == command.connect then
  202.             transmit(command.connect)
  203.             monitor = false
  204.         elseif key == command.write then
  205.             if monitor then
  206.                 transmit(command.write)
  207.                 term.setCursorPos(1,20)
  208.                 term.setBackgroundColor(colors.green)
  209.                 for i=1, 26 do
  210.                     io.write(" ")
  211.                 end
  212.                 term.setCursorPos(1, 20)
  213.                 transmit(io.read())
  214.             end
  215.         end
  216.     elseif event == 'key' then
  217.         if key >= 2 and key <= 10 then
  218.             transmit(key-1)
  219.         elseif key == command.forward then
  220.             transmit(command.forward)
  221.             move("forward", receive())
  222.         elseif key == command.back then
  223.             transmit(command.back)
  224.             move("back", receive())
  225.         elseif key == command.right then
  226.             transmit(command.right)
  227.             turn("right")
  228.         elseif key == command.left then
  229.             transmit(command.left)
  230.             turn("left")
  231.         elseif key == command.up then
  232.             transmit(command.up)
  233.         elseif key == command.down then
  234.             transmit(command.down)
  235.         elseif key == command.dig then
  236.             transmit(command.dig)
  237.         elseif key == command.digUp then
  238.             transmit(command.digUp)
  239.         elseif key == command.digDown then
  240.             transmit(command.digDown)
  241.         elseif key == command.place then
  242.             transmit(command.place)
  243.         elseif key == command.placeUp then
  244.             transmit(command.placeUp)
  245.         elseif key == command.placeDown then
  246.             transmit(command.placeDown)
  247.         elseif key == command.refuel then
  248.             transmit(command.refuel)
  249.         elseif key == command.getfuel then
  250.             transmit(command.getfuel)
  251.             extramsg = "Fuel:"..receive()
  252.         elseif key == command.connect then
  253.             transmit(command.connect)
  254.             if receive() == command.success then monitor = true end
  255.         elseif key == command.pastebin then
  256.             transmit(command.pastebin)
  257.             term.setCursorPos(1,20)
  258.             term.setBackgroundColor(colors.green)
  259.             for i=1, 26 do
  260.                 io.write(" ")
  261.             end
  262.             term.setCursorPos(1, 20)
  263.             transmit(io.read())
  264.         end
  265.     end
  266. end
  267.  
  268. local function mainLoop()
  269.     render()
  270.     while true do
  271.         checkInput()
  272.         render()
  273.     end
  274. end
  275.  
  276. side = setModem()
  277. if side ~= nil then
  278.     print("Modem connected to "..side)
  279. else
  280.     print("No wireless modem found")
  281.     return
  282. end
  283. modem.open(channel)
  284. setCommands()
  285.  
  286. if checkConnection() then
  287.  
  288.     mainLoop()
  289.  
  290. end
  291.  
  292. modem.close(channel)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement