Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local modem = peripheral.wrap("top")
- local rChan = 7321
- local sChan = 7322
- local map = {}
- local dir = 0
- local location = {x = 0, y = 0}
- local w, h = term.getSize()
- local running = true
- term.clear()
- local charPos = {
- x = math.floor(w/2),
- y = math.floor(h/2)
- }
- if modem.isWireless() == false then
- printError("A wireless modem is required.")
- return
- end
- if not term.isColor() then
- printError("A terminal supporting color is required.")
- return
- end
- modem.open(rChan)
- local tDirs = {
- [0] = '^',
- [1] = '>',
- [2] = 'V',
- [3] = '<'
- }
- local invColorMappings = {
- [1] = colors.red,
- [2] = colors.green,
- [3] = colors.yellow,
- [4] = colors.blue,
- [5] = colors.magenta,
- [6] = colors.cyan,
- [7] = colors.pink,
- [8] = colors.lime
- }
- -- TODO: Handle messages that are too long with -- More --
- local function displayMessage(msg)
- term.setCursorPos(1, 1)
- term.clearLine()
- write(msg)
- end
- local function updateMap(x, y)
- local sY = location.y - charPos.y + 2
- local eY = location.y + (h - charPos.y)
- local sX = location.x - charPos.x + 1
- local eX = location.x + (w - charPos.x)
- term.setCursorPos(1, 2)
- for i=sY,eY do
- if map[i] ~= nil then
- for j=sX,eX do
- if map[i][j] == nil then
- term.write(" ")
- else
- local c = map[i][j]
- if c == -1 then
- term.setBackgroundColour(colors.black)
- term.setTextColor(colors.lightGray)
- term.write('.')
- elseif c == 0 then
- term.setBackgroundColor(colors.lightGray)
- term.write(' ')
- else
- term.setBackgroundColor(invColorMappings[c - 8])
- term.write(' ')
- end
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- end
- else
- term.clearLine()
- end
- local tw, th = term.getCursorPos()
- term.setCursorPos(1, th + 1)
- end
- end
- local function updateChar()
- term.setCursorPos(charPos.x, charPos.y)
- term.write(tDirs[dir])
- end
- local function split(str)
- local ret = {}
- for i in string.gmatch(str, "%S+") do
- table.insert(ret, i)
- end
- return ret
- end
- local function getTarget()
- local d = {
- [0] = { 0, -1},
- [1] = { 1, 0},
- [2] = { 0, 1},
- [3] = { -1, 0}
- }
- return location.x + d[dir][1], location.y + d[dir][2]
- end
- local function getBlock(x, y)
- if map[y] == nil or map[y][x] == nil then
- return -2
- end
- return map[y][x]
- end
- local function setBlock(x, y, type)
- if map[y] == nil then map[y] = {} end
- map[y][x] = tonumber(type)
- end
- local function updateMapAndChar()
- updateMap()
- updateChar()
- end
- updateMap()
- updateChar()
- local function displayChoices(msg, options)
- displayMessage(msg)
- write(" (")
- for _, v in pairs(options) do
- write(v)
- end
- write(") ")
- end
- local function doItemSelection(msg, options)
- while true do
- displayChoices(msg, options)
- term.setCursorBlink(true)
- local ev, char = os.pullEvent("char")
- term.setCursorBlink(false)
- if ev == "char" then
- if char == "q" then
- displayMessage("Never mind.")
- return false
- end
- for _, v in pairs(options) do
- if v == char then
- term.setCursorPos(1, 1)
- term.clearLine()
- return char
- end
- end
- displayMessage("Unknown choice.")
- sleep(1)
- end
- end
- end
- local function confirm(msg, allowQuit)
- -- FIXME: allowQuit does nothing right now.
- while true do
- displayMessage(msg)
- write(" [yn] ")
- term.setCursorBlink(true)
- local ev, char = os.pullEvent("char")
- term.setCursorBlink(false)
- if char == 'y' then
- return true
- elseif char == 'n' then
- return false
- else
- displayMessage("Unknown option.")
- sleep(1)
- end
- end
- end
- local commands = {
- right = function()
- dir = dir + 1
- if dir == 4 then dir = 0 end
- updateChar()
- end,
- left = function()
- dir = dir - 1
- if dir == -1 then dir = 3 end
- updateChar()
- end,
- forward = function()
- local tx, ty = getTarget()
- location.x = tx
- location.y = ty
- updateMapAndChar()
- end,
- block = function(args)
- local facingX, facingY = getTarget()
- local there = tonumber(args[2])
- local blockThere = getBlock(facingX, facingY)
- if there ~= -1 and blockThere < 0 then
- setBlock(facingX, facingY, there)
- elseif there == -1 then
- setBlock(facingX, facingY, -1)
- end
- updateMap()
- updateChar()
- end,
- iblock = function(args)
- local facingX, facingY = getTarget()
- local there = tonumber(args[2])
- if there ~= -1 then
- setBlock(facingX, facingY, there)
- else
- setBlock(facingX, facingY, -1)
- end
- updateMap()
- updateChar()
- end,
- mine = function(args)
- displayMessage("Put in #" .. args[2] .. " (Has " .. args[3] .. " blocks).")
- end,
- cantmine = function(args)
- displayMessage("Unable to mine.")
- end,
- nospace = function()
- displayMessage("No remaining inventory space.")
- end,
- emptyslot = function()
- displayMessage("No items in slot.")
- end,
- cantplace = function()
- displayMessage("Can't place this here.")
- end
- }
- local keyHandlers = {
- h = 'left',
- j = 'down',
- k = 'up',
- l = 'right',
- s = 'search',
- S = 'isearch',
- a = 'mine',
- p = function()
- local choice = doItemSelection("Place what?", {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'})
- if choice == false then
- return
- end
- local choices = {a = 9, b = 10, c = 11, d = 12, e = 13, f = 14, g = 15, h = 16}
- choice = choices[choice]
- modem.transmit(sChan, rChan, "place " .. choice)
- end,
- q = function()
- local choice = confirm("Really quit", false)
- if choice == true then
- running = false
- term.clear()
- term.setCursorPos(1, 1)
- end
- end
- }
- while running do
- local ev, modemSide, channel, replyChannel, message, senderDistance = os.pullEvent()
- if ev == 'modem_message' then
- local smsg = split(message)
- if commands[smsg[1]] ~= nil then
- commands[smsg[1]](smsg)
- end
- elseif ev == 'char' then
- term.setCursorPos(1, 1)
- term.clearLine()
- local key = modemSide
- if keyHandlers[key] ~= nil then
- if type(keyHandlers[key]) == 'function' then
- keyHandlers[key]()
- else
- modem.transmit(sChan, rChan, keyHandlers[key])
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement