paramus

Tardis interface 2.0

Sep 1st, 2026 (edited)
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.64 KB | Gaming | 0 0
  1. local receiverID = 1 -- Make a better system that this, maybe develop an API like system?
  2. local TardisData = {
  3.     Name = "",
  4.     Artron = 100,
  5.     MaxArtron = 1000,
  6.     Progress = 0,
  7.     LastPing = 0,
  8.     DoorState = "OPEN",
  9.     IsRefueling = false,
  10.     Location = {
  11.         x = -1000000,
  12.         y = 0,
  13.         z = 0,
  14.         dimension = "minecraft:overworld",
  15.         direction = 1
  16.     },
  17.     Destination = {
  18.         x = 0,
  19.         y = 0,
  20.         z = 0,
  21.         dimension = "minecraft:overworld",
  22.         direction = 1
  23.     }
  24. }
  25. local Buttons = {}
  26. local TextFields = {}
  27. local WindowWidth, WindowHeight = term.getSize()
  28. local BackColor = colors.lightGray
  29. --WindowWidth, WindowHeight = 26, 20 --Debug ONLY
  30.  
  31. local function directionToText(direction)
  32.     if (direction == 1) then
  33.         return "NORTH"
  34.     elseif (direction == 2) then
  35.         return "EAST"
  36.     elseif (direction == 3) then
  37.         return "SOUTH"
  38.     elseif (direction == 4) then
  39.         return "WEST"
  40.     end
  41. end
  42.  
  43. local function buttonAction(name, button)
  44.     if (name == "go") then
  45.         rednet.send(receiverID, "Go")
  46.        
  47.     elseif (name == "refuel") then
  48.         rednet.send(receiverID, "Refuel")
  49.        
  50.     elseif (name == "dimension") then
  51.         if (button == 1) then
  52.             rednet.send(receiverID, "NextDimension")
  53.         else
  54.             rednet.send(receiverID, "PreviousDimension")
  55.         end
  56.        
  57.     elseif (name == "door") then
  58.         rednet.send(receiverID, "Door")
  59.        
  60.     elseif (name == "direction") then
  61.         rednet.send(receiverID, "turn")
  62.        
  63.     end
  64. end
  65.  
  66. local function getTardisData()    
  67.     rednet.send(receiverID, "GetData")
  68.     local ID,Msg = rednet.receive(nil, 1)
  69.     if (ID == receiverID) then
  70.         TardisData = textutils.unserialiseJSON(Msg)
  71.        
  72.         -- Bid fields
  73.         TextFields[1].text = TardisData.Destination.x
  74.         TextFields[2].text = TardisData.Destination.y
  75.         TextFields[3].text = TardisData.Destination.z
  76.        
  77.         -- Bind buttons
  78.         Buttons[2].text = TardisData.Destination.dimension
  79.         Buttons[3].text = TardisData.DoorState
  80.        
  81.         if (TardisData.DoorState == "CLOSED") then
  82.             Buttons[3].color = colors.red
  83.         else
  84.             Buttons[3].color = colors.green
  85.         end
  86.        
  87.         if (TardisData.Destination.direction == 1) then
  88.             Buttons[5].text = "\24"
  89.         elseif (TardisData.Destination.direction == 2) then
  90.             Buttons[5].text = "\26"
  91.         elseif (TardisData.Destination.direction == 3) then
  92.             Buttons[5].text = "\25"
  93.         elseif (TardisData.Destination.direction == 4) then
  94.             Buttons[5].text = "\27"
  95.         end
  96.     end
  97. end
  98.  
  99. local function load()
  100.     peripheral.find("modem", rednet.open)
  101.    
  102.     table.insert(TextFields, {name="x", isSelected=false, x=3, y=3, w=8, h=1, text=tostring(TardisData.Destination.x)}) --1
  103.     table.insert(TextFields, {name="y", isSelected=false, x=3, y=5, w=8, h=1, text=tostring(TardisData.Destination.y)}) --2
  104.     table.insert(TextFields, {name="z", isSelected=false, x=3, y=7, w=8, h=1, text=tostring(TardisData.Destination.z)}) --3
  105.    
  106.     table.insert(Buttons, {name="go", x=2, y=WindowHeight-1, w=4, h=1, color=colors.green, text="Go"}) --1
  107.     table.insert(Buttons, {name="dimension", x=3, y=9, w=WindowWidth-3, h=1, color=colors.orange, text=TardisData.Destination.dimension}) --2
  108.     table.insert(Buttons, {name="door", x=3, y=12, w=6, h=1, color=colors.green, text=TardisData.DoorState}) --3
  109.     table.insert(Buttons, {name="refuel", x=10, y=12, w=7, h=1, color=colors.red, text="Refuel"}) --4
  110.     table.insert(Buttons, {name="direction", x=18, y=12, w=1, h=1, color=colors.orange, text="\24"}) --5
  111.     table.insert(Buttons, {name="alarm", x=3, y=14, w=5, h=1, color=colors.red, text="Alarm"}) --6
  112.     table.insert(Buttons, {name="break", x=9, y=14, w=5, h=1, color=colors.red, text="Break"}) --6
  113.    
  114.     getTardisData() -- This has to happen after so the values can be propperly assigned to the controls
  115. end
  116.  
  117. local function update()
  118.     while (true) do
  119.         getTardisData()
  120.         TardisData.LastPing = TardisData.LastPing + 1
  121.         --TardisData.Progress = TardisData.Progress + 5 --Debug ONLY
  122.         sleep(0.1)
  123.     end
  124. end
  125.  
  126. local function draw()
  127.     while (true) do
  128.         --term.setBackgroundColor(BackColor)
  129.         term.setTextColor(colors.black)
  130.         term.clear()
  131.        
  132.         -- Background
  133.         paintutils.drawFilledBox(1,1, WindowWidth, WindowHeight, BackColor)
  134.        
  135.         -- buttons container
  136.         paintutils.drawFilledBox(2, 11, WindowWidth-1, WindowHeight-3, colors.gray)
  137.        
  138.         -- Ping thingy
  139.         if (TardisData.LastPing == nil or TardisData.LastPing >= 3) then
  140.             TardisData.LastPing = 3 -- Just in case the program crashes for too long a number...
  141.             paintutils.drawPixel(1,1, colors.red)
  142.             term.setCursorPos(1, 1)
  143.             term.write("NO SIGNAL")
  144.         end
  145.         paintutils.drawPixel(1,2, BackColor) -- Just so text background isn't red
  146.        
  147.         --Artron
  148.         local artronString = "AU: " .. TardisData.Artron .. "/" .. TardisData.MaxArtron
  149.         term.setCursorPos(WindowWidth - #artronString, 1)
  150.         term.write(artronString)
  151.        
  152.        
  153.         -- Destination stuff
  154.         term.setCursorPos(1, 3)
  155.         term.write("X:")
  156.         term.setCursorPos(1, 5)
  157.         term.write("Y:")
  158.         term.setCursorPos(1, 7)
  159.         term.write("Z:")
  160.         term.setCursorPos(1, 9)
  161.         term.write("D:")
  162.        
  163.         -- Location stuff
  164.         term.setCursorPos(14, 3)
  165.         term.write(TardisData.Location.x)
  166.         term.setCursorPos(14, 5)
  167.         term.write(TardisData.Location.y)
  168.         term.setCursorPos(14, 7)
  169.         term.write(TardisData.Location.z)
  170.        
  171.         -- Progress bar
  172.         local barX = 9
  173.         local maxX = (WindowWidth-barX-1)*(TardisData.Progress/100)
  174.         for ix=1, (WindowWidth-barX-1) do
  175.             paintutils.drawPixel(barX + ix, WindowHeight-1, BackColor)
  176.             term.setCursorPos(barX + ix, WindowHeight-1)
  177.             if (maxX <= ix) then
  178.                 term.write("\127")
  179.             else
  180.                 paintutils.drawPixel(barX + ix, WindowHeight-1, colors.black)
  181.                 --term.write("\143")
  182.             end
  183.         end
  184.        
  185.         -- Buttons
  186.         term.setTextColor(colors.white)
  187.         for i,b in ipairs(Buttons) do
  188.             paintutils.drawBox(b.x, b.y, b.x-1+b.w, b.y-1+b.h, b.color)
  189.             term.setCursorPos(b.x, b.y)
  190.             term.write(b.text)
  191.         end
  192.        
  193.         -- TextFields
  194.         term.setTextColor(colors.white)
  195.         for i,b in ipairs(TextFields) do
  196.             paintutils.drawBox(b.x, b.y, b.x-1+b.w, b.y-1+b.h, colors.black)
  197.             term.setCursorPos(b.x, b.y)
  198.             if (b.isSelected) then
  199.                 term.write(b.text .. "_")
  200.             else
  201.                 term.write(b.text)
  202.             end
  203.         end
  204.  
  205.         sleep(0.1)
  206.     end
  207. end
  208.  
  209. local function onClick()
  210.     while true do
  211.         local event, button, x, y = os.pullEvent("mouse_click")
  212.        
  213.         -- Buttons
  214.         for i,b in ipairs(Buttons) do
  215.             if ((x >= b.x and x <= b.x-1+b.w) and (y >= b.y and y <= b.y-1+b.h)) then
  216.                 buttonAction(b.name, button)
  217.             end
  218.         end
  219.        
  220.         -- TextFields
  221.         for i,b in ipairs(TextFields) do
  222.             if ((x >= b.x and x <= b.x-1+b.w) and (y >= b.y and y <= b.y-1+b.h)) then
  223.                 for _,t in ipairs(TextFields) do
  224.                     t.isSelected = false
  225.                 end
  226.                 b.isSelected = true
  227.             end
  228.         end
  229.     end
  230. end
  231.  
  232. local function onKey()
  233.     while (true) do
  234.         local event, key, is_held = os.pullEvent("key")
  235.         local keyString = keys.getName(key)
  236.        
  237.         if (keyString == "one") then
  238.             keyString = "1"
  239.         elseif (keyString == "two") then
  240.             keyString = "2"
  241.         elseif (keyString == "three") then
  242.             keyString = "3"
  243.         elseif (keyString == "four") then
  244.             keyString = "4"
  245.         elseif (keyString == "five") then
  246.             keyString = "5"
  247.         elseif (keyString == "six") then
  248.             keyString = "6"
  249.         elseif (keyString == "seven") then
  250.             keyString = "7"
  251.         elseif (keyString == "eight") then
  252.             keyString = "8"
  253.         elseif (keyString == "nine") then
  254.             keyString = "9"
  255.         elseif (keyString == "zero") then
  256.             keyString = "0"
  257.         elseif (keyString == "backspace" or keyString == "enter" or keyString == "slash") then
  258.             keyString = keyString
  259.         else
  260.             goto continue
  261.         end
  262.        
  263.         for i,b in ipairs(TextFields) do
  264.             if (b.isSelected) then
  265.                 if (keyString == "backspace") then
  266.                     b.text = b.text:sub(1, -2)
  267.                     if (#b.text <= 0) then
  268.                         b.text = "0"
  269.                     end
  270.                 elseif (keyString == "enter") then
  271.                     b.isSelected = false
  272.                 elseif (keyString == "slash") then
  273.                     b.text = "-" .. b.text
  274.                 else
  275.                     if (b.text == "0") then
  276.                         b.text = keyString
  277.                     else
  278.                         b.text = b.text .. keyString
  279.                     end
  280.                 end
  281.                
  282.                 -- send new target coords
  283.                 rednet.send(receiverID, "SetDestination " .. TextFields[1].text .. " " .. TextFields[2].text .. " " .. TextFields[3].text)
  284.                
  285.                 break
  286.             end
  287.         end
  288.        
  289.         ::continue::
  290.     end
  291. end
  292.  
  293. load()
  294. parallel.waitForAny(update, draw, onClick, onKey)
Advertisement
Add Comment
Please, Sign In to add comment