Advertisement
skypop

CC Test Laser

Aug 11th, 2018
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.22 KB | None | 0 0
  1. --Laser test
  2. --[[
  3. minimumPotency=0.5:
  4. The minimum potency a laser can have. Raising this prevents computers spamming a large number of lasers in a short amount of time.
  5.  
  6. maximumPotency=5:
  7. The maximum potency a laser can have.
  8.  
  9. cost=10:
  10. The “energy” cost per potency for a laser. By default a computer will gain 10 energy points each tick (read about the cost system for more information).
  11.  
  12. damage=4:
  13. The damage done to an entity for each potency. Players and most mobs have 20 health, meaning a fully charged laser can kill an unarmoured creature with one hit.
  14. --]]
  15.  
  16. local posR = { x = 0, y=0, z=5 }
  17. -- Shots per potency
  18. local shotCount = 1
  19.  
  20.  
  21. local modules = peripheral.find("manipulator")
  22. if not modules then
  23.     error("Cannot find manipulator", 0)
  24. end
  25.  
  26. function scan(target)
  27.     return modules.getBlockMeta(target.x, target.y, target.z)
  28. end
  29.  
  30. local function fire(target, potency)
  31.     potency = math.min(5,math.max(.5,potency))
  32.     local delay = math.max(potency * .1 + .1, .1)
  33.     local x, y, z = target.x, target.y, target.z
  34.     local pitch = -math.atan2(y, math.sqrt(x * x + z * z))
  35.     local yaw = math.atan2(-x, z)
  36.  
  37.     modules.fire(math.deg(yaw), math.deg(pitch), potency)
  38.     sleep(delay)
  39. end
  40.  
  41. local function keyPress(k)
  42.     while true do
  43.         local e,p = os.pullEvent("key")
  44.         if p == k then
  45.             e,p = os.pullEvent("key_up")
  46.             if p == k then
  47.                 break
  48.             end
  49.         end
  50.     end
  51. end
  52.  
  53. local function display(blockData)
  54.     local x,y = term.getCursorPos()
  55.     term.setCursorPos(1,1)
  56.     term.clearLine()
  57.     term.write("Target: "..blockData.name)
  58.     term.setCursorPos(x,y)
  59. end
  60.  
  61. settings.load("log")
  62.  
  63. while true do
  64.     term.setCursorPos(1,1)
  65.     term.clear()
  66.     print("Press [S] to start")
  67.     keyPress(keys.s)
  68.     local blockData = scan(posR)
  69.     print("Target:", blockData.name)
  70.     if blockData.name == "minecraft:air" then
  71.         print("Please place a block on target position")
  72.         print("at X:", posR.x, "Y:", posR.y, "Z:", posR.z)
  73.         print("Then, press any key")
  74.         os.pullEvent("key")
  75.         os.pullEvent("key_up")
  76.     else
  77.         term.setCursorPos(1,2)
  78.         term.clear()
  79.         local maxPower = 0
  80.         local newBlockData
  81.         for power=0.5,5,0.1 do
  82.             maxPower = power
  83.             term.write(string.format("  fire potency: %s shots: ", power))
  84.             display(blockData)
  85.             for i=1,shotCount do --test de 5 tirs
  86.                 term.write("*")
  87.                 fire(posR, power)
  88.                 newBlockData = scan(posR)
  89.                 if newBlockData.name ~= blockData.name then
  90.                     break
  91.                 end
  92.             end
  93.             print("")
  94.             if newBlockData.name ~= blockData.name then
  95.                 print("Target diff.\nBefore:", blockData.name, "\nAfter:", newBlockData.name)
  96.                 display(blockData)
  97.                 break
  98.            
  99.             end
  100.         end
  101.         settings.set(blockData.name, newBlockData.name ~= blockData.name and maxPower or math.huge)
  102.         settings.save("log")
  103.         print("Test over, press any key")
  104.         os.pullEvent("key")
  105.         os.pullEvent("key_up")
  106.        
  107.         if commands then
  108.             print("Replace block", blockData.name, "? Y/N")
  109.             local x,y,confirm = term.getCursorPos()
  110.             repeat
  111.                 term.setCursorPos(x,y)
  112.                 term.clearLine()
  113.                 confirm = string.lower(read())
  114.             until confirm == "y" or confirm == "n"
  115.             if confirm == "y" then
  116.                 local ax,ay,az = commands.getBlockPosition()
  117.                 commands.setblock(ax+posR.x, ay+posR.y+1, az+posR.z, blockData.name)
  118.             end
  119.         end
  120.     end
  121.     sleep(.2)
  122. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement