CapsAdmin

Untitled

Mar 9th, 2011
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.50 KB | None | 0 0
  1. easylua = easylua or {} local e = easylua
  2.  
  3. function easylua.GetClosestToAim(ply)
  4.     local aim = ply:GetAimVector()
  5.     local eye = ply:EyePos()
  6.  
  7.     local dot_ents = {}
  8.     for key, entity in pairs(ents.GetAll()) do
  9.         if
  10.             not entity:GetOwner():IsPlayer() and
  11.             not entity:GetParent():IsPlayer() and
  12.             entity ~= ply and
  13.             entity:EntIndex() ~= 0 and
  14.             entity:GetClass() ~= "env_sprite"
  15.         then
  16.             local pos = entity:LocalToWorld(entity:OBBCenter())
  17.             table.insert(dot_ents, {
  18.                     entity = entity,
  19.                     dot = (math.Clamp(-aim:Dot((eye - pos):Normalize()), 0, 1) ^ 10) / (eye:Distance(pos) + 100)
  20.                 }
  21.             )
  22.         end
  23.     end
  24.  
  25.     table.SortByMember(dot_ents, "dot", function(a,b) return a < b end)
  26.  
  27.     return dot_ents[1].entity or NULL
  28. end
  29.  
  30. function easylua.GetClosestFromPos(vec)
  31.  
  32.     local found = {}
  33.  
  34.     for key, entity in pairs(ents.GetAll()) do
  35.         local distance = entity:GetPos():Distance(vec)
  36.         if distance < 50 then
  37.             table.insert(found, {entity = entity, distance = distance})
  38.         end
  39.     end
  40.  
  41.     table.SortByMember(found, "distance", function(a, b) return a > b end)
  42.  
  43.     return found[1].entity or NULL
  44. end
  45.  
  46. function easylua.GetVars()
  47.     return e._G_vars
  48. end
  49.  
  50. function easylua.Start(ply)
  51.     local trace = util.QuickTrace(ply:GetShootPos(), ply:GetAimVector() * 32000, {ply})
  52.     local dis = e.GetClosestToAim(ply)
  53.     local ent = not trace.Entity:IsWorld() and trace.Entity or dis
  54.  
  55.  
  56.     local vars = {
  57.         me = ply,
  58.  
  59.         trace = trace,
  60.         tr = trace,
  61.  
  62.         there = trace.HitPos,
  63.         here = ply:GetPos(),
  64.  
  65.         eyepos = trace.StartPos,
  66.         eyeang = ply:EyeAngles(),
  67.  
  68.         aim = ply:GetAimVector(),
  69.  
  70.         wep = ply:GetActiveWeapon(),
  71.  
  72.         length = trace.StartPos:Distance(trace.HitPos),
  73.  
  74.         dis = dis,
  75.         this = ent,
  76.         phys = util.IsValidPhysicsObject(ent) and ent:GetPhysicsObject(),
  77.  
  78.         create = function(class)
  79.             local mdl = "error.mdl"
  80.            
  81.             if IsEntity(class) and class:IsValid() then
  82.                 this = class
  83.             elseif class:find(".mdl", nil, true) then
  84.                 mdl = class
  85.                 class = "prop_physics"
  86.                
  87.                 this = ents.Create(class)
  88.                 this:SetModel(mdl)
  89.             else
  90.                 this = ents.Create(class)
  91.             end
  92.            
  93.             this:Spawn()
  94.             this:SetPos(there + Vector(0,0,this:BoundingRadius() * 2))
  95.             this:DropToFloor()
  96.             this:PhysWake()
  97.  
  98.             undo.Create(class)
  99.                 undo.SetPlayer(me)
  100.                 undo.AddEntity(this)
  101.             undo.Finish()
  102.         end,
  103.        
  104.         copy = function(ent)
  105.             ent = ent or this
  106.            
  107.             if SERVER then
  108.                 me:SendLua(Format("SetClipboardText('Entity(%s)')", this:EntIndex()))
  109.             else
  110.                 SetClipboardText(Format("Entity(%s)", this:EntIndex()))
  111.             end
  112.         end,
  113.    
  114.         you = ent:IsPlayer() and ent or dis:IsPlayer() and dis or NULL
  115.     }
  116.  
  117.     for key, value in pairs(vars) do
  118.         _G[key] = value
  119.     end
  120.  
  121.     e._G_vars = vars
  122. end
  123.  
  124. function easylua.End()
  125.     if e._G_vars then
  126.         for key in pairs(e._G_vars) do
  127.             if _G[key] then
  128.                 _G[key] = nil
  129.             end
  130.         end
  131.     end
  132. end
  133.  
  134. function easylua.RunLua(ply, code)
  135.  
  136.     easylua.Start(ply)
  137.         local locals = ""
  138.  
  139.         for key in pairs(e.GetVars()) do
  140.             locals = locals .. " local " .. key .. " = " .. key .. " "
  141.         end
  142.  
  143.         local func = CompileString(locals .. " " .. code, tostring(ply))
  144.         if func then
  145.             local noerr, errormsg = pcall(func)
  146.  
  147.             if noerr == false then
  148.                 ErrorNoHalt("Script from '"..tostring(ply).."' errored: "..tostring(errormsg))
  149.             end
  150.         end
  151.     easylua.End()
  152. end
  153.  
  154. hook.Add("PreLuaDevRun", "easylua_luadev", function(_,_,ply)
  155.     if IsValid(ply) and ply:IsPlayer() then
  156.         easylua.Start(ply)
  157.     end
  158. end)
  159.  
  160. hook.Add("PostLuaDevRun", "easylua_luadev", function()
  161.     easylua.End()
  162. end)
Advertisement
Add Comment
Please, Sign In to add comment