Advertisement
SirBaconBitz

Freaking Magic

Feb 14th, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.76 KB | None | 0 0
  1. --[[
  2. What can I say...
  3. I guess I finally decided to clean this code up.
  4. ]]--
  5. coords = {}
  6. players = {}
  7.  
  8. --Kept this because why the hell not.
  9. username = "Herro"
  10. loc = 1
  11. bacon = true
  12.  
  13. --Beginning of functions section (What, I like to have it all in one file...)
  14.  
  15. --Gets the charge. Just an attempt to keep my code more clean
  16. function getcharge()
  17.         if data.heldItem.energyStored ~= nil then
  18.                 charge = math.ceil((data.heldItem.energyStored / data.heldItem.maxEnergyStored) * 100)
  19.         elseif data.heldItem.electric ~= nil then
  20.                 charge = math.ceil((data.heldItem.electric.charge / data.heldItem.electric.maxCharge) * 100)
  21.         else
  22.                 charge = "N/A"
  23.         end
  24.         if charge ~= "N/A" then
  25.                 charge = charge.."%"
  26.         end
  27.         return charge
  28. end
  29.  
  30. -- Adds Freaking Magical Boxes (Slowly adds gradient boxes for added effect)
  31. function addmagicalBox(x,y,width,height,color,opacity,colorend,opacityend, direction)
  32.         name = terminal.addGradientBox(x,y,1,height,color,opacity,colorend,opacityend,direction)
  33.         for newidth=1,width do
  34.         sleep(0.01)
  35.         name.setWidth(newidth)
  36.         end
  37. end
  38.  
  39. --This is just incase CC derps on me.
  40. for i = 1,10 do
  41. table.remove(players, i)
  42. end
  43.  
  44. --If the coordinates don't exist, it gets the coordinates.
  45. existingcoords = fs.exists("sensorlocation")
  46. if existingcoords == false then
  47.         term.clear()
  48.         term.setCursorPos(1,1)
  49.         term.write("X: ")
  50.         x = read()
  51.         term.setCursorPos(1,2)
  52.         term.write("Y: ")
  53.         y = read()
  54.         term.setCursorPos(1,3)
  55.         term.write("Z: ")
  56.         z = read()
  57.         coords.x = x
  58.         coords.y = y
  59.         coords.z = z
  60.         output = textutils.serialize(coords)
  61.         handle = assert(fs.open("sensorlocation", "w"), "Couldn't save vars")
  62.         handle.write(output)
  63.         handle.close()
  64. end
  65.  
  66. --Loads the coordinates from the file
  67. handle = assert(fs.open("sensorlocation", "r"), "Couldn't load vars")
  68. input = handle.readAll()
  69. coords = textutils.unserialize(input)
  70. locx = coords.x
  71. locy = coords.y
  72. locz = coords.z
  73.  
  74. for k, v in pairs(peripheral.getNames()) do
  75.         type = peripheral.getType(v)
  76.         if type == "openperipheral_sensor" then
  77.                 sensor = peripheral.wrap(v)
  78.                 print("sensor wrapped as sensor on side: "..v)
  79.         elseif type == "openperipheral_glassesbridge" then
  80.                 terminal = peripheral.wrap(v)
  81.                 print("Bridge wrapped as terminal on side: "..v)
  82.         end
  83. end
  84.  
  85. -- Adds Freaking Magical Boxes
  86. function addmagicalBox(x,y,width,height,color,opacity,colorend,opacityend, direction)
  87.         name = terminal.addGradientBox(x,y,1,height,color,opacity,colorend,opacityend,direction)
  88.         for newidth=1,width do
  89.         sleep(0.01)
  90.         name.setWidth(newidth)
  91.         end
  92. end
  93.  
  94. --Adds the boxes and text
  95. function initialize()
  96.         sleep(2)
  97.         terminal.clear()
  98.         text = terminal.addText(12,15, "Loading...", 0xFFFFFF)
  99.         text.setScale(0.5)
  100.         xtext = terminal.addText(12,35, "Loading...", 0xFFFFFF)
  101.         xtext.setScale(0.5)
  102.         ytext = terminal.addText(12,55, "Loading...", 0xFFFFFF)
  103.         ytext.setScale(0.5)
  104.         ztext = terminal.addText(12,75,"Loading...",0xFFFFFF)
  105.         ztext.setScale(0.5)
  106.         dirtext = terminal.addText(242,40,"Loading...",0xFFFFFF)
  107.         dirtext.setScale(0.5)
  108.         healthtext = terminal.addText(42,35, "Loading...",0xFFFFFF)
  109.         healthtext.setScale(0.5)
  110.         holdingtext = terminal.addText(42,55, "Loading...",0xFFFFFF)
  111.         holdingtext.setScale(0.5)
  112.         chargetext = terminal.addText(42,75, "Loading...",0xFFFFFF)
  113.         chargetext.setScale(0.5)
  114. addmagicalBox(10,10,100,10,0x00FFFF,0.5, 0x336699, 0, 1)
  115. addmagicalBox(10,30,25,10,0x00FFFF,0.5, 0x336699, 0, 1)
  116. addmagicalBox(10,50,25,10,0x00FFFF,0.5, 0x336699, 0, 1)
  117. addmagicalBox(10,70,25,10,0x00FFFF,0.5, 0x336699, 0, 1)
  118. addmagicalBox(240,35,50,10,0x00FFFF,0.5, 0x336699, 0, 1)
  119. addmagicalBox(40,30,70,10,0x00FFFF,0.5,0x336699,0,1)
  120. addmagicalBox(40,50,70,10,0x00FFFF,0.5,0x336699,0,1)
  121. addmagicalBox(40,70,70,10,0x00FFFF,0.5,0x336699,0,1)
  122. end
  123.  
  124. --Functions for user input while still displaying dynamic data (Harder than one might think)
  125.  
  126. --Accepts user input
  127. function two()
  128.         e, msg, b, c, a = os.pullEvent("chat_command")
  129.         --Will fetch username
  130.         if msg == "next" then
  131.                 print("Next!")
  132.                 if players[loc + 1] ~= nil then
  133.                         loc = loc + 1
  134.                         username = players[loc]
  135.                         print(username.." : "..tostring(loc))
  136.                 else
  137.                         loc = 1
  138.                 end
  139.         end
  140.         if string.find(msg, "add") then
  141.                 print("ADD!")
  142.                 subloc = string.find(msg, " ")
  143.                 username = string.sub(msg, subloc + 1)
  144.                 table.insert(players, username)
  145.         end
  146. end
  147.  
  148. --Just cuz sleeping is fun
  149. function three()
  150.         sleep(1)
  151. end
  152.  
  153. --Adds players currently using glasses to the table.
  154. for k, v in pairs(terminal.getUsers()) do
  155.         table.insert(players, v)
  156. end
  157. --End of the functions section.
  158. --Just running functions...
  159. initialize()
  160.  
  161. --For debugging purposes
  162. print(loc)
  163. print(players[loc])
  164.  
  165. --Creating coroutines. (This shit is advanced man)
  166. --DONT QUESTION MY NAMING CONVENTIONS BITCH
  167. local uinput = coroutine.create(two)
  168. local sleepin = coroutine.create(three)
  169.  
  170.  
  171. while true do
  172. --dynamic data
  173. username = players[loc]
  174. data = sensor.getPlayerData(username)
  175. --[[You see, the sensor calculates x y and z as the distance from the sensor. So adding the location of the sensor
  176. to the distance rectifies this issue.
  177. That's why I have the "calibration"
  178. ]]--
  179. x = data.position.x + locx
  180. y = data.position.y + locy
  181. z = data.position.z + locz
  182.        
  183.         --calculates direction based on yaw
  184.         yaw = data.yaw
  185.         if yaw<225 and yaw>135 then
  186.     direction = "North"
  187.         elseif yaw>223 and yaw<311 then
  188.     direction = "East"
  189.         elseif yaw>309 then
  190.     direction = "South"
  191.         elseif yaw<46 then
  192.     direction = "South"
  193.         elseif yaw>44 and yaw<135 then
  194.     direction = "West"
  195.         else
  196.     direction = "N/A"
  197.         end
  198.  
  199. --Updates the gui with the data
  200. chargetext.setText("Charge: "..getcharge())
  201. healthtext.setText("Health: "..math.ceil(tostring(math.ceil(data.health)) / tostring(math.ceil(data.maxHealth)) * 100).."%")
  202. holdingtext.setText("Holding: "..data.heldItem.name)
  203. dirtext.setText("Facing: "..direction)
  204. xtext.setText("X: "..math.floor(x))
  205. ytext.setText("Y: "..math.floor(y))
  206. ztext.setText("Z: "..math.floor(z))
  207. text.setText("Username: "..username)
  208. parallel.waitForAny(two, three)
  209. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement