Advertisement
LDDestroier

Phaneron/LDD Laser Defense System (CC/Manipulator)

Apr 23rd, 2017
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Manipulator Laser Defense System
  2. -- Originally made by Phaneron
  3. -- Modified by LDD (scrollable log, automatic laser detection, design change)
  4.  
  5. if (not fs.exists("json")) and (not json) then
  6.     print("No JSON API found! Downloading...")
  7.     local prog = http.get("https://pastebin.com/raw/4nRg9CHU")
  8.     if not prog then
  9.         error("Couldn't download JSON API...")
  10.     else
  11.         local file = fs.open("json","w")
  12.         file.write(prog.readAll())
  13.         file.close()
  14.         os.loadAPI("json")
  15.     end
  16. elseif fs.exists("json") and (not json) then   
  17.     os.loadAPI("json")
  18. end
  19.  
  20. local manips,guns,sensor
  21. local scr_x,scr_y = term.getSize()
  22. log = {}
  23. local logscroll = 0
  24.  
  25. local logadd = function(input)
  26.   log[#log+1] = {
  27.     txt = input,
  28.     time = os.time(),
  29.     day = os.day(),
  30.   }
  31. end
  32.  
  33. local ts = tostring
  34.  
  35. local assignGuns = function()
  36.   manips = {peripheral.find("manipulator")}
  37.   guns = {}
  38.   for a = 1, #manips do
  39.     if manips[a].fire then
  40.       guns[#guns+1] = manips[a]
  41.     elseif manips[a].sense then
  42.         sensor = manips[a]
  43.     end
  44.   end
  45. end
  46.  
  47. assignGuns()
  48.  
  49. if #guns == 0 then
  50.   error("you've got no guns")
  51. end
  52.  
  53. local tps = function(vis)
  54.   if term.current().setVisible then
  55.     term.current().setVisible(vis)
  56.     return true
  57.   else
  58.     return false
  59.   end
  60. end
  61.  
  62. local cwrite = function(txt,y)
  63.   local cx,cy = term.getCursorPos()
  64.   term.setCursorPos((scr_x/2)-math.ceil(#txt/2),y or cy)
  65.   return write(txt)
  66. end
  67.  
  68. local function fire(gun, entity)
  69.   local x = entity.x
  70.   local y = entity.y
  71.   local z = entity.z
  72.  
  73.   local pitch = -math.atan2(y, math.sqrt(x * x + z * z))
  74.   local yaw = math.atan2(-x, y)
  75.   gun.fire(math.deg(yaw), math.deg(pitch), 5)
  76.   sleep(0.2)
  77. end
  78.  
  79. local fuckEmUp = function(target)
  80.   assignGuns()
  81.   for a = 1, #guns do
  82.     fire(guns[a],target)
  83.   end
  84. end
  85.  
  86. local safeNames = {
  87.   ["Phaneron"] = true,
  88.   ["EldidiStroyrr"] = true, --teehee
  89.   ["Chicken"] = true,
  90.   ["Cow"] = true,
  91.   ["Sheep"] = true,
  92.   ["Horse"] = true,
  93.   ["Rabbit"] = true,
  94.   ["MushroomCow"] = true,
  95.   ["Item"] = true,
  96.   ["XPOrb"] = true,
  97.   ["Painting"] = true,
  98.   ["Arrow"] = true,
  99.   ["plethora:laser"] = true,
  100. }
  101.  
  102. function isATarget(name)
  103.   if safeNames[name] then
  104.     return false
  105.   else
  106.     return true
  107.   end
  108. end
  109.  
  110. term.setBackgroundColor(colors.black)
  111. term.clear()
  112. term.setTextColor(term.isColor() and colors.yellow or colors.lightGray)
  113. cwrite("Phaneron Laser Defence System",2)
  114. term.setTextColor(colors.gray)
  115. cwrite("-- None shall pass! --",3)
  116. term.setTextColor(colors.lightGray)
  117. cwrite("(lasers: "..#guns..")\n",4)
  118. term.setTextColor(colors.white)
  119. print(" Log of worthless peons:\n")
  120. local doTheSense = function()
  121.   while true do
  122.     local report = sensor.sense()
  123.     for idx, r in pairs(report) do
  124.       if isATarget(r.name) and r.x ~= nil then
  125.         if r.x > 6 then
  126.           logadd("Engaged: '"..r.name.."'")
  127.           os.queueEvent("laserlog_update")
  128.           fuckEmUp(r)
  129.         end
  130.       end
  131.     end
  132.   end
  133. end
  134.  
  135. local renderLog = function(scroll,topY)
  136.   tps(false)
  137.   for a = topY,scr_y do
  138.     term.setCursorPos(1,a)
  139.     term.clearLine()
  140.     local lpos = scroll+(a-(topY-1))
  141.     if log[lpos] then
  142.       term.blit(ts(lpos)..(" "):rep(#ts(#log)-#ts(lpos)),("8"):rep(#ts(#log)),("7"):rep(#ts(#log)))
  143.       term.write(" "..log[lpos].txt)
  144.     end
  145.   end
  146.   tps(true)
  147. end
  148.  
  149. local endbuf = 6
  150. local handleGUI = function()
  151.   while true do
  152.     local evt,dir = os.pullEvent()
  153.     if evt == "mouse_scroll" then
  154.       if dir+logscroll >= 0 and dir+logscroll <= (#log-(scr_y-endbuf)) then
  155.         logscroll = dir+logscroll
  156.         renderLog(logscroll,6)
  157.       end
  158.     elseif evt == "laserlog_update" then
  159.       if logscroll >= (#log-(scr_y-endbuf)-1) then
  160.         logscroll = #log-(scr_y-endbuf)
  161.       end
  162.       renderLog(logscroll,6)
  163.     elseif evt == "key" then
  164.       local scrolldist
  165.       if dir == keys.up then
  166.         scrolldist = -1
  167.       elseif dir == keys.down then
  168.         scrolldist = 1
  169.       elseif dir == keys.pageUp then
  170.         scrolldist = -(scr_y-endbuf)
  171.       elseif dir == keys.pageDown then
  172.         scrolldist = scr_y-endbuf
  173.       elseif dir == keys.home then
  174.         scrolldist = -#log
  175.       elseif dir == keys["end"] then
  176.         scrolldist = #log
  177.       else
  178.         scrolldist = 0
  179.       end
  180.       logscroll = math.max(0,math.min(logscroll+scrolldist,#log-(scr_y-endbuf)))
  181.     end
  182.   end
  183. end
  184.  
  185. parallel.waitForAny(doTheSense,handleGUI)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement