CapsAdmin

easylua

Mar 9th, 2011
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.03 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.         copy = function(ent)
  110.             ent = ent or this
  111.            
  112.             if SERVER then
  113.                 me:SendLua(Format("SetClipboardText('Entity(%s)')", this:EntIndex()))
  114.             else
  115.                 SetClipboardText(Format("Entity(%s)", this:EntIndex()))
  116.             end
  117.         end,
  118.    
  119.         you = ent:IsPlayer() and ent or dis:IsPlayer() and dis or NULL
  120.     }
  121.  
  122.     for key, value in pairs(vars) do
  123.         _G[key] = value
  124.     end
  125.  
  126.     e._G_vars = vars
  127. end
  128.  
  129. function easylua.End()
  130.     if e._G_vars then
  131.         for key in pairs(e._G_vars) do
  132.             if _G[key] then
  133.                 _G[key] = nil
  134.             end
  135.         end
  136.     end
  137. end
  138.  
  139. function easylua.RunLua(ply, code)
  140.     check(ply, "Player")
  141.     check(code, "string")
  142.  
  143.     easylua.Start(ply)
  144.         local locals = ""
  145.  
  146.         for key in pairs(e.GetVars()) do
  147.             locals = locals .. " local " .. key .. " = " .. key .. " "
  148.         end
  149.  
  150.         local func = CompileString(locals .. " " .. code, tostring(ply))
  151.         if func then
  152.             local noerr, errormsg = pcall(func)
  153.  
  154.             if noerr == false then
  155.                 ErrorNoHalt("Script from '"..tostring(ply).."' errored: "..tostring(errormsg))
  156.             end
  157.         end
  158.     easylua.End()
  159. end
  160.  
  161. if luadev then
  162.  
  163.     hook.Add("PreLuaDevRun", "easylua_luadev", function(_,_,ply)
  164.         if IsValid(ply) and ply:IsPlayer() then
  165.             easylua.Start(ply)
  166.         end
  167.     end)
  168.  
  169.     hook.Add("PostLuaDevRun", "easylua_luadev", function()
  170.         easylua.End()
  171.     end)
  172. end
  173.  
  174. if not nero then
  175.     -- lazy way
  176.     local function LuaChat(ply, text)
  177.         if ply:IsAdmin() then
  178.             local lua = SERVER and text:find("!l ", nil, true) and text:sub(4) or CLIENT and text:find("!ll ") and text:sub(5)
  179.             if text then
  180.                 easylua.RunLua(ply, text)
  181.             end
  182.         end
  183.     end
  184.  
  185.     hook.Add("PlayerSay", "easylua_chatlua", LuaChat)
  186.     hook.Add("OnPlayerChat", "easylua_chatlua", LuaChat)
  187. end
Advertisement
Add Comment
Please, Sign In to add comment