Advertisement
Guest User

startup

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