GNOOR1S

Pocket Computer - Turtle Leash

Feb 2nd, 2025
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.05 KB | None | 0 0
  1. -- leash for turtle quarry
  2. local width, height = term.getSize()
  3. local turtleAmount = 6
  4. local distance = 300 --needs to be serialized and saved
  5. local turtleFuel = 211
  6. rednet.open("back")
  7.  
  8. function display()
  9.     term.setBackgroundColor(colors.black)
  10.     term.clear()
  11.     term.setCursorPos(1, 1)
  12.     term.setTextColor(colors.lime)
  13.     print("Trtl Leash")
  14.     displayButtons()
  15. end
  16.  
  17. function displayButtons()
  18.     term.setBackgroundColor(colors.lime)
  19.     term.setTextColor(colors.white)
  20.     -- setting distance of the turtle
  21.     term.setCursorPos(width-2, height)
  22.     term.write("-|+")
  23.     term.setCursorPos(width-2, height-1)
  24.     term.write(distance)
  25.     term.setCursorPos(width-3, height-3)
  26.     term.write("coal")
  27.     term.setCursorPos(width-3, height-4)
  28.     term.write(calcFuel())
  29.     term.setCursorPos(width-3, height-6)
  30.     term.setBackgroundColor(colors.red)
  31.     term.write("STOP")
  32.     term.setCursorPos(width-3, height-7)
  33.     term.setBackgroundColor(colors.green)
  34.     term.write("STAR")
  35.     term.setCursorPos(width-3, height-9)
  36.     term.setBackgroundColor(colors.orange)
  37.     term.write("STAT")
  38.     term.setCursorPos(width-3, height-10)
  39.     term.setBackgroundColor(colors.brown)
  40.     term.write("DUMP")
  41.     term.setCursorPos(width-3, height-12)
  42.     term.setBackgroundColor(colors.green)
  43.     term.write("TURN")
  44.     term.setCursorPos(width-3, height-13)
  45.     term.setBackgroundColor(colors.green)
  46.     term.write("DOWN")
  47.    
  48. end
  49.  
  50. function getinput()
  51.     while true do
  52.         local ev, btn, x, y = os.pullEvent("mouse_click")
  53.         checkStatus(x,y)
  54.         checkStart(x,y)
  55.         checkStop(x,y)
  56.         checkSubDistance(x,y)
  57.         checkAddDistance(x,y)
  58.         checkDump(x,y)
  59.         turn(x,y)
  60.         down(x,y)
  61.     end
  62. end
  63.  
  64. function down(x,y)
  65.     if x >= width-3 and y == height-13 then
  66.         rednet.broadcast(createPacket("down", nil), "lbsm")
  67.         term.setTextColor(colors.white)
  68.         term.setCursorPos(1,2)
  69.         term.write("Going down")
  70.         sleep(1)
  71.         display()
  72.     end
  73. end
  74.  
  75. function turn(x,y)
  76.     if x >= width-3 and y == height-12 then
  77.         rednet.broadcast(createPacket("turn", nil), "lbsm")
  78.         term.setTextColor(colors.white)
  79.         term.setCursorPos(1,2)
  80.         term.write("Turning")
  81.         sleep(1)
  82.         display()
  83.     end
  84. end
  85.  
  86. function createPacket(comm, info)
  87.     return {command=comm,msg=info}
  88. end
  89.  
  90. function checkDump(x,y)
  91.     if x >= width-3 and y == height-10 then
  92.         rednet.broadcast(createPacket("dump", nil), "lbsm")
  93.     end
  94. end
  95.  
  96. function checkStart(x, y)
  97.     if x >= width-3 and y == height-7 then
  98.         rednet.broadcast(createPacket("start", distance),"lbsm")
  99.         term.setTextColor(colors.white)
  100.         term.setCursorPos(1,2)
  101.         term.write("Starting")
  102.         sleep(1)
  103.         display()
  104.     end
  105. end
  106.  
  107. function checkStop(x, y)
  108.     if x >= width-3 and y == height-6 then
  109.         rednet.broadcast(createPacket("stop", nil),"lbsm")
  110.         term.setTextColor(colors.white)
  111.         term.setCursorPos(1,2)
  112.         term.write("Stop")
  113.         sleep(1)
  114.         display()
  115.     end
  116.    
  117. end
  118.  
  119. function checkStatus(x, y)
  120.     if x >= width-3 and y == height-9 then
  121.         rednet.broadcast(createPacket("status", nil),"lbsm")
  122.     end
  123. end
  124.  
  125. function checkAddDistance(x, y)
  126.     if x == width and y == height then
  127.         distance = distance + 10
  128.         if distance > 999 then distance = 990 end
  129.         display()
  130.     end
  131. end
  132.  
  133. function checkSubDistance(x, y)
  134.     if x == width-2 and y == height then
  135.         distance = distance - 10
  136.         if distance < 1 then distance = 10 end
  137.         display()
  138.     end
  139. end
  140.  
  141. function calcFuel()
  142.     local blockFuelCost = 3
  143.     local fuelCost = blockFuelCost * distance
  144.     return math.ceil(fuelCost / 80) * turtleAmount
  145. end
  146.  
  147. function getMessage()
  148.     local event, id, msg = os.pullEvent("rednet_message")
  149.     display()
  150.     term.setCursorPos(1,2)
  151.     print(textutils.serialize(msg))
  152.     sleep(1)
  153. end
  154.  
  155. display()
  156. displayButtons()
  157.  
  158. while true do
  159.     parallel.waitForAny(getinput, getMessage)
  160. end
  161.  
  162.  
Advertisement
Add Comment
Please, Sign In to add comment