Python1320

CapsAdmin

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