Guest User

Untitled

a guest
May 14th, 2013
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.25 KB | None | 0 0
  1. local rChan = 7322
  2. local sChan = 7321
  3. local modem = peripheral.wrap("right")
  4. modem.open(rChan)
  5. local slot = 1
  6. turtle.select(1)
  7. local function select(s)
  8.     slot = s
  9.     turtle.select(s)
  10. end
  11. -- TODO: Check if it really IS a modem
  12.  
  13. local function send(msg)
  14.     modem.transmit(sChan, rChan, msg)
  15. end
  16.  
  17. local dir = 0
  18. local dirs = {
  19.     up = 0,
  20.     right = 1,
  21.     down = 2,
  22.     left = 3
  23. }
  24.  
  25. local function turnRight()
  26.     dir = dir + 1
  27.     if dir == 4 then dir = 0 end
  28.     turtle.turnRight()
  29.     send("right")
  30. end
  31.  
  32. local function turnLeft()
  33.     dir = dir - 1
  34.     if dir == -1 then dir = 3 end
  35.     turtle.turnLeft()
  36.     send("left")
  37. end
  38.  
  39.  
  40. local function faceDirection(d)
  41.     if d == dir then return
  42.     else
  43.         if (d == 0 and dir == 3) or (d > dir and not (d == 3 and dir == 0)) then
  44.             while d ~= dir do
  45.                 turnRight()
  46.             end
  47.         else
  48.             while d ~= dir do
  49.                 turnLeft()
  50.             end
  51.         end
  52.     end
  53. end
  54.  
  55. local function quickLook()
  56.     local found = turtle.detect()
  57.     if found == true then found = 0 else found = -1 end
  58.     send("block " .. found)
  59. end
  60.  
  61. local function identifyLook()
  62.     if turtle.detect() == false then return end
  63.     for i=9,16 do
  64.         select(i)
  65.         -- turtle.compare() returns true if comparing nothing with nothing, we don't want that.
  66.         if turtle.compare() and turtle.getItemCount(slot) > 0 then
  67.             send("iblock " .. slot)
  68.             return
  69.         end
  70.     end
  71.     quickLook()
  72. end
  73.  
  74. local function search()
  75.     for i=1,4 do quickLook() turnRight()
  76.     end
  77. end
  78.  
  79. local function identifySearch()
  80.     for i=1,4 do
  81.         identifyLook()
  82.         turnRight()
  83.     end
  84. end
  85.  
  86. local function forward()
  87.     quickLook()
  88.     if turtle.forward() then
  89.         send("forward")
  90.         quickLook()
  91.     end
  92. end
  93.  
  94. local function mine()
  95.     for i=9,16 do
  96.         select(i)
  97.         if turtle.getItemSpace(i) ~= 0 and (turtle.getItemCount(i) == 0 or turtle.compare() == true) then
  98.             if turtle.dig() then
  99.                 send("mine " .. slot .. " " .. turtle.getItemCount(slot))
  100.                 quickLook()
  101.             else
  102.                 send("cantmine")
  103.             end
  104.             return
  105.         end
  106.     end
  107.     send("nospace")
  108. end
  109.  
  110. local function place(sl)
  111.     if turtle.getItemCount(sl) == 0 then
  112.         send("emptyslot")
  113.         return
  114.     end
  115.     select(sl)
  116.    
  117.     if turtle.place() then
  118.         send("place")
  119.         identifyLook()
  120.     else
  121.         send("cantplace")
  122.     end
  123. end
  124.  
  125. local commands = {
  126.     search = search,
  127.     isearch = identifySearch,
  128.     up = function()
  129.         faceDirection(0)
  130.         forward()
  131.     end,
  132.     down = function()
  133.         faceDirection(2)
  134.         forward()
  135.     end,
  136.     left = function()
  137.         faceDirection(3)
  138.         forward()
  139.     end,
  140.     right = function()
  141.         faceDirection(1)
  142.         forward()
  143.     end,
  144.     mine = mine,
  145.     place = function(args)
  146.         place(tonumber(args[2]))
  147.     end
  148. }
  149.  
  150. local function split(str)
  151.     ret = {}
  152.     for i in string.gmatch(str, "%S+") do
  153.         table.insert(ret, i)
  154.     end
  155.     return ret
  156. end
  157.  
  158. term.clear()
  159. term.setCursorPos(1, 1)
  160. write([[Welcome to the turtle "Roguelike"
  161.  
  162. Run the other program on an advanced computer in order to control this turtle.
  163.  
  164. The computer needs to have a modem attached at the top.
  165.  
  166. (Modem channels 7322 and 7311 are used)
  167.  
  168.  
  169. To quit, press 'q'.]])
  170.  
  171. while true do
  172.     local ev, side, c, rc, msg, dist = os.pullEvent()
  173.     if ev == "modem_message" then
  174.         local smsg = split(msg)
  175.         if commands[smsg[1]] ~= nil then
  176.             commands[smsg[1]](smsg)
  177.         end
  178.     elseif ev == "char" and side == "q" then
  179.         term.clear()
  180.         term.setCursorPos(1, 1)
  181.         return
  182.     end
  183. end
Advertisement
Add Comment
Please, Sign In to add comment