zootsuitman

Turtle Remote Control

Jan 19th, 2016
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.72 KB | None | 0 0
  1. --Remote
  2. --zootsuitman 1/19/16
  3.  
  4. local settings ={}
  5. local place ="Front"
  6. local current
  7. local selectedInv =1
  8. local loop =true
  9.  
  10. function clear()
  11.     term.clear()
  12.     term.setCursorPos(1,1)
  13. end
  14.  
  15. function split(pString, pPattern)
  16.    local Table = {}
  17.    local fpat = "(.-)" .. pPattern
  18.    local last_end = 1
  19.    local s, e, cap = pString:find(fpat, 1)
  20.    while s do
  21.           if s ~= 1 or cap ~= "" then
  22.          table.insert(Table,cap)
  23.           end
  24.           last_end = e+1
  25.           s, e, cap = pString:find(fpat, last_end)
  26.    end
  27.    if last_end <= #pString then
  28.           cap = pString:sub(last_end)
  29.           table.insert(Table, cap)
  30.    end
  31.    return Table
  32. end
  33.  
  34. function screen1()
  35.     clear()
  36.     current =1
  37.  
  38.     clear()
  39.     print("Welcome to " .. settings.name .. " net.")
  40.     print("Move Forward:   w\nMove Backwards: s")
  41.     print("Turn Left:      a\nTurn Right:     d")
  42.     print("Go Up:          e\nGo Down:        q")
  43.     print("Mine/Attack:    space\nPlace:          tab")
  44.     print("Pick up:        lshift")
  45.     print("Use Fuel:       f")
  46.     print("Inventory Slot: 1-9")
  47.     print("Over 9 press rshift + num")
  48.     print("Placement Mode: NUM 1,2,3")
  49.     print("View Inventory: enter")
  50.     write("Exit: backspace")
  51.  
  52.     term.setCursorPos(1,18)
  53.     print("Fuel: " .. settings.fuel)
  54.     print("Inventory Slot: " .. selectedInv)
  55.     write("Placement Selection: " .. place)
  56. end
  57.  
  58. function screen2()
  59.     clear()
  60.     current =2
  61.  
  62.     rednet.send(settings.id, "getInv", settings.name)
  63.     local senderId, get, protocol =rednet.receive()
  64.     local inv =textutils.unserialize(get)
  65.    
  66.     for i=1,16 do
  67.         term.setCursorPos(1,i)
  68.         write(i)
  69.        
  70.         if inv[i] then 
  71.             term.setCursorPos(4,i)
  72.            
  73.             local cut =split(inv[i].name,":")
  74.             write(cut[2])
  75.            
  76.             term.setCursorPos(24,i)
  77.             write("-" .. inv[i].count)
  78.         end
  79.     end
  80.    
  81.     term.setCursorPos(1,selectedInv)
  82.     write("> ")
  83.    
  84.     term.setCursorPos(1,20)
  85.     write("Control Screen: enter")
  86. end
  87.  
  88. function move()
  89.     if settings.fuel ~= "unlimited" then
  90.         settings.fuel = settings.fuel -1
  91.    
  92.         if current == 1 then
  93.             term.setCursorPos(7,18)
  94.             write(settings.fuel .. "                      ")
  95.         end
  96.     end
  97. end
  98.  
  99.  
  100. --Start Program--
  101. rednet.open("back")
  102.  
  103. clear()
  104. write("What is the turtles name?\n> ")
  105. settings.name =read()
  106.  
  107. clear()
  108. write("Is it an attack turtle? (y/n)\n> ")
  109. local input =read()
  110.  
  111. if input == "y" or input == "Y" then
  112.     settings.attack =true
  113. end
  114.  
  115. if settings.attack then
  116.     rednet.broadcast("connect2", settings.name)
  117. else
  118.     rednet.broadcast("connect", settings.name)
  119. end
  120.  
  121. local senderId, msg, protocol =rednet.receive()
  122. settings.id =senderId
  123. settings.fuel =msg
  124.  
  125. screen1()
  126.  
  127. while loop do
  128.     while true do
  129.     function resetInv()
  130.         if current ==2 then
  131.             sleep(1)
  132.             screen2()
  133.         end
  134.     end
  135.    
  136.     function fuel()
  137.         local id, newFuel, probTrash =rednet.receive()
  138.        
  139.         settings.fuel =newFuel
  140.        
  141.         if current == 1 then
  142.             term.setCursorPos(7,18)
  143.             write(settings.fuel .. "                      ")
  144.         else
  145.             resetInv()
  146.         end
  147.     end
  148.    
  149.         local event, key = os.pullEvent("key")
  150.        
  151.     --Movement--
  152.         if key == 17 then rednet.send(settings.id, "f", settings.name) move() break end
  153.         if key == 31 then rednet.send(settings.id, "b", settings.name) move() break end
  154.         if key == 32 then rednet.send(settings.id, "r", settings.name) break end
  155.         if key == 30 then rednet.send(settings.id, "l", settings.name) break end
  156.         if key == 16 then rednet.send(settings.id, "d", settings.name) move() break end
  157.         if key == 18 then rednet.send(settings.id, "u", settings.name) move() break end
  158.  
  159.     --Actions--
  160.         if key == 57 then rednet.send(settings.id, "m", settings.name) resetInv() break end
  161.         if key == 15 then rednet.send(settings.id, "place", settings.name) resetInv() break end
  162.         if key == 42 then rednet.send(settings.id, "pickup", settings.name) resetInv() break end
  163.        
  164.         if key == 33 then rednet.send(settings.id, "fuel", settings.name) fuel() break end
  165.  
  166.     --Inventory--
  167.  
  168.         function changeCursor(num)
  169.             term.setCursorPos(1,selectedInv)
  170.             write(selectedInv)
  171.            
  172.             term.setCursorPos(1,num)
  173.             write("> ")
  174.         end
  175.        
  176.         function changeSelec(num)
  177.             term.setCursorPos(17,19)
  178.             write(num .. "  ")
  179.         end
  180.        
  181.         function changeInv(num, plus)
  182.             if num == 11 then num =1 end
  183.             if plus then num = num+10 end
  184.            
  185.             rednet.send(settings.id, num-1, settings.name)
  186.             if current == 2 then
  187.                 changeCursor(num-1)
  188.             else
  189.                 changeSelec(num-1)
  190.             end
  191.             selectedInv = num-1
  192.         end
  193.  
  194.         if key == 2 then changeInv(key,false) break end
  195.         if key == 3 then changeInv(key,false) break end
  196.         if key == 4 then changeInv(key,false) break end
  197.         if key == 5 then changeInv(key,false) break end
  198.         if key == 6 then changeInv(key,false) break end
  199.         if key == 7 then changeInv(key,false) break end
  200.         if key == 8 then changeInv(key,false) break end
  201.         if key == 9 then changeInv(key,false) break end
  202.         if key == 10 then changeInv(key,false) break end
  203.        
  204.         if key == 54 then
  205.             event, key = os.pullEvent("key")
  206.             if key < 8 then
  207.                 changeInv(key,true)
  208.             else
  209.                 break
  210.             end
  211.         end
  212.        
  213.     --Directions--
  214.         if key == 79 then
  215.             rednet.send(settings.id, "mf", settings.name)
  216.             term.setCursorPos(22,20)
  217.             if current == 1 then write("Front ") end
  218.             break
  219.         end
  220.        
  221.         if key == 80 then
  222.             rednet.send(settings.id, "mu", settings.name)
  223.             term.setCursorPos(22,20)
  224.             if current == 1 then write("Up    ") end
  225.             break
  226.         end
  227.        
  228.         if key == 81 then
  229.             rednet.send(settings.id, "md", settings.name)
  230.             term.setCursorPos(22,20)
  231.             if current == 1 then write("Down  ") end
  232.             break
  233.         end
  234.        
  235.     --Screens--
  236.         if key == 28 then
  237.             if current == 1 then
  238.                 screen2()
  239.             else
  240.                 screen1()
  241.             end
  242.            
  243.             break
  244.         end
  245.        
  246.         if key == 14 then
  247.             loop =false
  248.             rednet.send(settings.id, "reset", settings.name)
  249.             os.reboot()
  250.         end
  251.     end
  252. end
Advertisement
Add Comment
Please, Sign In to add comment