Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- args = {...}
- command = {}
- channel = 13371
- modem = nil
- connectedtospy = false
- monitor = false
- width = 50
- length = 50
- height = 10
- position = {}
- position.x = math.floor(width / 2)
- position.z = math.floor(length / 2)
- position.y = math.floor(height / 2)
- selecteditem = 0
- extramsg = "m"
- rotation = 0 --0 = fore, 1 = right, 2 = back, 3 = left << relative to original orientation
- map = {}
- for y=1, height do
- map[y] = {}
- for x=1, width do
- map[y][x] = {}
- for z=1, length do
- map[y][x][z] = 0
- end
- end
- end
- if args ~= nil then
- if tonumber(args[1]) then
- channel = math.abs(args[1]%65535)
- end
- end
- local function setCommands()
- command.forward = 17 --W
- command.back = 31 --S
- command.right = 32 --D
- command.left = 30 --A
- command.up = 57 --Space
- command.down = 42 --LShift
- command.dig = 33 --F
- command.digUp = 34 --G
- command.digDown = 35 --H
- command.place = 18 --E
- command.placeUp = 19 --R
- command.placeDown = 20 --T
- command.connect = 16 --Q
- command.pastebin = 25 --P
- command.write = 46 --C
- command.refuel = 47 --V
- command.getfuel = 48 --B
- command.success = 11 --success return
- command.fail = 12 --failed return
- command.unknown = 13 --unknown return
- command.nofuel = 14
- end
- local function setModem()
- for i,v in pairs(rs.getSides()) do
- if (peripheral.getType(v) == "modem") then
- modem = peripheral.wrap(v)
- if modem.isWireless() then
- return v
- else modem = nil end
- end
- end
- return nil
- end
- local function stepforward()
- if rotation == 0 then position.z = position.z - 1
- elseif rotation == 1 then position.x = position.x + 1
- elseif rotation == 2 then position.z = position.z + 1
- elseif rotation == 3 then position.x = position.x - 1
- end
- end
- local function stepbackward()
- if rotation == 0 then position.z = position.z + 1
- elseif rotation == 1 then position.x = position.x - 1
- elseif rotation == 2 then position.z = position.z - 1
- elseif rotation == 3 then position.x = position.x + 1
- end
- end
- local function move(direction, success)
- extramsg = success
- if direction == "forward" then
- if success == command.success then
- stepforward()
- map[position.y][position.x][position.z] = 1
- elseif success == command.fail then
- stepforward()
- map[position.y][position.x][position.z] = 2
- stepbackward()
- elseif success == command.unknown then
- stepforward()
- map[position.y][position.x][position.z] = 0
- stepbackward()
- end
- elseif direction == "back" then
- if success == command.success then
- stepbackward()
- map[position.y][position.x][position.z] = 1
- elseif success == command.fail then
- stepbackward()
- map[position.y][position.x][position.z] = 2
- stepforward()
- elseif success == command.unknown then
- stepbackward()
- map[position.y][position.x][position.z] = 0
- stepforward()
- end
- end
- end
- local function turn(direction)
- if direction == "right" then
- rotation = (rotation + 1) % 4
- elseif direction == "left" then
- rotation = (rotation - 1) % 4
- end
- end
- local function transmit(id)
- modem.transmit(channel, channel, id)
- end
- local function receive()
- local event, side, sentChannel,
- replyChannel, message, distance = os.pullEvent("modem_message")
- return message
- end
- local function checkConnection()
- if connectedtospy == false then
- transmit("ping")
- io.write("Connecting... ")
- if receive() == "pong" then
- connectedtospy = true
- print("Connected to Spy.")
- return true
- end
- print("Unable to connect to Spy.")
- connectedtospy = false
- return false
- end
- return true
- end
- camera = {}
- camera.x = 0
- camera.z = 0
- local function render()
- if position.x < 10 then camera.x = 0
- elseif position.x > width - 10 then camera.x = width - 20
- else camera.x = position.x - 10 end
- if position.z < 13 then camera.z = 0
- elseif position.z > length - 13 then camera.z = length - 26
- else camera.z = position.z - 13 end
- for x=1, math.min(width, 26) do
- for z=1, math.min(length, 18) do
- term.setCursorPos(x, z)
- if (map[position.y][x+camera.x][z+camera.z] == 0) then term.setBackgroundColor(colors.black)
- elseif (map[position.y][x+camera.x][z+camera.z] == 1) then term.setBackgroundColor(colors.green)
- elseif (map[position.y][x+camera.x][z+camera.z] == 2) then term.setBackgroundColor(colors.red)
- end
- io.write(" ")
- end
- end
- term.setBackgroundColor(colors.green)
- term.setTextColor(colors.white)
- term.setCursorPos(position.x - camera.x, position.z - camera.z)
- if rotation == 0 then io.write("^")
- elseif rotation == 1 then io.write(">")
- elseif rotation == 2 then io.write("v")
- elseif rotation == 3 then io.write("<")
- end
- term.setCursorPos(1, 19)
- --print("Pos: "..position.x..","..position.y..","..position.z)
- io.write("Item: "..selecteditem)
- term.setCursorPos(9, 20)
- io.write(extramsg)
- end
- local function checkInput()
- os.sleep(0.5)
- event, key = os.pullEvent('key')
- if not checkConnection() then
- return
- end
- --print(event.." "..key)
- if monitor then
- if key == command.connect then
- transmit(command.connect)
- monitor = false
- elseif key == command.write then
- if monitor then
- transmit(command.write)
- term.setCursorPos(1,20)
- term.setBackgroundColor(colors.green)
- for i=1, 26 do
- io.write(" ")
- end
- term.setCursorPos(1, 20)
- transmit(io.read())
- end
- end
- elseif event == 'key' then
- if key >= 2 and key <= 10 then
- transmit(key-1)
- elseif key == command.forward then
- transmit(command.forward)
- move("forward", receive())
- elseif key == command.back then
- transmit(command.back)
- move("back", receive())
- elseif key == command.right then
- transmit(command.right)
- turn("right")
- elseif key == command.left then
- transmit(command.left)
- turn("left")
- elseif key == command.up then
- transmit(command.up)
- elseif key == command.down then
- transmit(command.down)
- elseif key == command.dig then
- transmit(command.dig)
- elseif key == command.digUp then
- transmit(command.digUp)
- elseif key == command.digDown then
- transmit(command.digDown)
- elseif key == command.place then
- transmit(command.place)
- elseif key == command.placeUp then
- transmit(command.placeUp)
- elseif key == command.placeDown then
- transmit(command.placeDown)
- elseif key == command.refuel then
- transmit(command.refuel)
- elseif key == command.getfuel then
- transmit(command.getfuel)
- extramsg = "Fuel:"..receive()
- elseif key == command.connect then
- transmit(command.connect)
- if receive() == command.success then monitor = true end
- elseif key == command.pastebin then
- transmit(command.pastebin)
- term.setCursorPos(1,20)
- term.setBackgroundColor(colors.green)
- for i=1, 26 do
- io.write(" ")
- end
- term.setCursorPos(1, 20)
- transmit(io.read())
- end
- end
- end
- local function mainLoop()
- render()
- while true do
- checkInput()
- render()
- end
- end
- side = setModem()
- if side ~= nil then
- print("Modem connected to "..side)
- else
- print("No wireless modem found")
- return
- end
- modem.open(channel)
- setCommands()
- if checkConnection() then
- mainLoop()
- end
- modem.close(channel)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement