Advertisement
Guest User

Untitled

a guest
May 14th, 2013
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.03 KB | None | 0 0
  1. local modem = peripheral.wrap("top")
  2. local rChan = 7321
  3. local sChan = 7322
  4. local map = {}
  5. local dir = 0
  6. local location = {x = 0, y = 0}
  7. local w, h = term.getSize()
  8. local running = true
  9.  
  10. term.clear()
  11.  
  12. local charPos = {
  13.     x = math.floor(w/2),
  14.     y = math.floor(h/2)
  15. }
  16.  
  17. if modem.isWireless() == false then
  18.     printError("A wireless modem is required.")
  19.     return
  20. end
  21.  
  22. if not term.isColor() then
  23.     printError("A terminal supporting color is required.")
  24.     return
  25. end
  26.  
  27. modem.open(rChan)
  28.  
  29. local tDirs = {
  30.     [0] = '^',
  31.     [1] = '>',
  32.     [2] = 'V',
  33.     [3] = '<'
  34. }
  35.  
  36. local invColorMappings = {
  37.     [1] = colors.red,
  38.     [2] = colors.green,
  39.     [3] = colors.yellow,
  40.     [4] = colors.blue,
  41.     [5] = colors.magenta,
  42.     [6] = colors.cyan,
  43.     [7] = colors.pink,
  44.     [8] = colors.lime
  45. }
  46.  
  47. -- TODO: Handle messages that are too long with -- More --
  48. local function displayMessage(msg)
  49.     term.setCursorPos(1, 1)
  50.     term.clearLine()
  51.     write(msg)
  52. end
  53.  
  54. local function updateMap(x, y)
  55.     local sY = location.y - charPos.y + 2
  56.     local eY = location.y + (h - charPos.y)
  57.     local sX = location.x - charPos.x + 1
  58.     local eX = location.x + (w - charPos.x)
  59.    
  60.     term.setCursorPos(1, 2)
  61.     for i=sY,eY do
  62.         if map[i] ~= nil then
  63.             for j=sX,eX do
  64.                 if map[i][j] == nil then
  65.                     term.write(" ")
  66.                 else
  67.                     local c = map[i][j]
  68.                     if c == -1 then
  69.                         term.setBackgroundColour(colors.black)
  70.                         term.setTextColor(colors.lightGray)
  71.                         term.write('.')
  72.                     elseif c == 0 then
  73.                         term.setBackgroundColor(colors.lightGray)
  74.                         term.write(' ')
  75.                     else
  76.                         term.setBackgroundColor(invColorMappings[c - 8])
  77.                         term.write(' ')
  78.                     end
  79.                 end
  80.                 term.setBackgroundColor(colors.black)
  81.                 term.setTextColor(colors.white)
  82.             end
  83.         else
  84.             term.clearLine()
  85.         end
  86.         local tw, th = term.getCursorPos()
  87.         term.setCursorPos(1, th + 1)
  88.     end
  89. end
  90.  
  91. local function updateChar()
  92.     term.setCursorPos(charPos.x, charPos.y)
  93.     term.write(tDirs[dir])
  94. end
  95.  
  96. local function split(str)
  97.     local ret = {}
  98.     for i in string.gmatch(str, "%S+") do
  99.         table.insert(ret, i)
  100.     end
  101.     return ret
  102. end
  103.  
  104. local function getTarget()
  105.     local d = {
  106.         [0] = { 0, -1},
  107.         [1] = { 1, 0},
  108.         [2] = { 0, 1},
  109.         [3] = { -1, 0}
  110.     }
  111.    
  112.     return location.x + d[dir][1], location.y + d[dir][2]
  113. end
  114.  
  115. local function getBlock(x, y)
  116.     if map[y] == nil or map[y][x] == nil then
  117.         return -2
  118.     end
  119.     return map[y][x]
  120. end
  121.  
  122. local function setBlock(x, y, type)
  123.     if map[y] == nil then map[y] = {} end
  124.     map[y][x] = tonumber(type)
  125. end
  126.  
  127. local function updateMapAndChar()
  128.     updateMap()
  129.     updateChar()
  130. end
  131.  
  132. updateMap()
  133. updateChar()
  134.  
  135. local function displayChoices(msg, options)
  136.     displayMessage(msg)
  137.     write(" (")
  138.     for _, v in pairs(options) do
  139.         write(v)
  140.     end
  141.     write(") ")
  142. end
  143.  
  144. local function doItemSelection(msg, options)
  145.     while true do
  146.         displayChoices(msg, options)
  147.         term.setCursorBlink(true)
  148.         local ev, char = os.pullEvent("char")
  149.         term.setCursorBlink(false)
  150.         if ev == "char" then
  151.             if char == "q" then
  152.                 displayMessage("Never mind.")
  153.                 return false
  154.             end
  155.             for _, v in pairs(options) do
  156.                 if v == char then
  157.                     term.setCursorPos(1, 1)
  158.                     term.clearLine()
  159.                     return char
  160.                 end
  161.             end
  162.             displayMessage("Unknown choice.")
  163.             sleep(1)
  164.         end
  165.     end
  166. end
  167.  
  168. local function confirm(msg, allowQuit)
  169.     -- FIXME: allowQuit does nothing right now.
  170.     while true do
  171.         displayMessage(msg)
  172.         write(" [yn] ")
  173.        
  174.         term.setCursorBlink(true)
  175.         local ev, char = os.pullEvent("char")
  176.         term.setCursorBlink(false)
  177.         if char == 'y' then
  178.             return true
  179.         elseif char == 'n' then
  180.             return false
  181.         else
  182.             displayMessage("Unknown option.")
  183.             sleep(1)
  184.         end
  185.     end
  186. end
  187.  
  188. local commands = {
  189.     right = function()
  190.         dir = dir + 1
  191.         if dir == 4 then dir = 0 end
  192.         updateChar()
  193.     end,
  194.     left = function()
  195.         dir = dir - 1
  196.         if dir == -1 then dir = 3 end
  197.         updateChar()
  198.     end,
  199.     forward = function()
  200.         local tx, ty = getTarget()
  201.         location.x = tx
  202.         location.y = ty
  203.         updateMapAndChar()
  204.     end,
  205.     block = function(args)
  206.         local facingX, facingY = getTarget()
  207.         local there = tonumber(args[2])
  208.         local blockThere = getBlock(facingX, facingY)
  209.         if there ~= -1 and blockThere < 0 then
  210.             setBlock(facingX, facingY, there)
  211.         elseif there == -1 then
  212.             setBlock(facingX, facingY, -1)
  213.         end
  214.         updateMap()
  215.         updateChar()
  216.     end,
  217.     iblock = function(args)
  218.         local facingX, facingY = getTarget()
  219.         local there = tonumber(args[2])
  220.         if there ~= -1 then
  221.             setBlock(facingX, facingY, there)
  222.         else
  223.             setBlock(facingX, facingY, -1)
  224.         end
  225.         updateMap()
  226.         updateChar()
  227.     end,
  228.     mine = function(args)
  229.         displayMessage("Put in #" .. args[2] .. " (Has " .. args[3] .. " blocks).")
  230.     end,
  231.     cantmine = function(args)
  232.         displayMessage("Unable to mine.")
  233.     end,
  234.     nospace = function()
  235.         displayMessage("No remaining inventory space.")
  236.     end,
  237.     emptyslot = function()
  238.         displayMessage("No items in slot.")
  239.     end,
  240.     cantplace = function()
  241.         displayMessage("Can't place this here.")
  242.     end
  243. }
  244.  
  245. local keyHandlers = {
  246.     h = 'left',
  247.     j = 'down',
  248.     k = 'up',
  249.     l = 'right',
  250.     s = 'search',
  251.     S = 'isearch',
  252.     a = 'mine',
  253.     p = function()
  254.         local choice = doItemSelection("Place what?", {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'})
  255.         if choice == false then
  256.             return
  257.         end
  258.         local choices = {a = 9, b = 10, c = 11, d = 12, e = 13, f = 14, g = 15, h = 16}
  259.         choice = choices[choice]
  260.         modem.transmit(sChan, rChan, "place " .. choice)
  261.     end,
  262.     q = function()
  263.         local choice = confirm("Really quit", false)
  264.         if choice == true then
  265.             running = false
  266.             term.clear()
  267.             term.setCursorPos(1, 1)
  268.         end
  269.     end
  270. }
  271.  
  272. while running do
  273.     local ev, modemSide, channel, replyChannel, message, senderDistance = os.pullEvent()
  274.     if ev == 'modem_message' then
  275.         local smsg = split(message)
  276.         if commands[smsg[1]] ~= nil then
  277.             commands[smsg[1]](smsg)
  278.         end
  279.     elseif ev == 'char' then
  280.         term.setCursorPos(1, 1)
  281.         term.clearLine()
  282.         local key = modemSide
  283.         if keyHandlers[key] ~= nil then
  284.             if type(keyHandlers[key]) == 'function' then
  285.                 keyHandlers[key]()
  286.             else
  287.                 modem.transmit(sChan, rChan, keyHandlers[key])
  288.             end
  289.         end
  290.     end
  291. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement