Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- easylua = easylua or {} local e = easylua
- function easylua.GetClosestToAim(ply)
- check(ply, "Player")
- local aim = ply:GetAimVector()
- local eye = ply:EyePos()
- local dot_ents = {}
- for key, entity in pairs(ents.GetAll()) do
- if
- not entity:GetOwner():IsPlayer() and
- not entity:GetParent():IsPlayer() and
- entity ~= ply and
- entity:EntIndex() ~= 0 and
- entity:GetClass() ~= "env_sprite"
- then
- local pos = entity:LocalToWorld(entity:OBBCenter())
- table.insert(dot_ents, {
- entity = entity,
- dot = (math.Clamp(-aim:Dot((eye - pos):Normalize()), 0, 1) ^ 10) / (eye:Distance(pos) + 100)
- }
- )
- end
- end
- table.SortByMember(dot_ents, "dot", function(a,b) return a < b end)
- return dot_ents[1].entity or NULL
- end
- function easylua.GetClosestFromPos(vec)
- check(vec, "Vector")
- local found = {}
- for key, entity in pairs(ents.GetAll()) do
- local distance = entity:GetPos():Distance(vec)
- if distance < 50 then
- table.insert(found, {entity = entity, distance = distance})
- end
- end
- table.SortByMember(found, "distance", function(a, b) return a > b end)
- return found[1].entity or NULL
- end
- function easylua.GetVars()
- return e._G_vars
- end
- function easylua.Start(ply)
- check(ply, "Player")
- local trace = util.QuickTrace(ply:GetShootPos(), ply:GetAimVector() * 32000, {ply})
- local dis = e.GetClosestToAim(ply)
- local ent = not trace.Entity:IsWorld() and trace.Entity or dis
- local vars = {
- me = ply,
- trace = trace,
- tr = trace,
- there = trace.HitPos,
- here = ply:GetPos(),
- eyepos = trace.StartPos,
- eyeang = ply:EyeAngles(),
- aim = ply:GetAimVector(),
- wep = ply:GetActiveWeapon(),
- length = trace.StartPos:Distance(trace.HitPos),
- dis = dis,
- this = ent,
- phys = util.IsValidPhysicsObject(ent) and ent:GetPhysicsObject(),
- create = function(class)
- local mdl = "error.mdl"
- if IsEntity(class) and class:IsValid() then
- this = class
- elseif class:find(".mdl", nil, true) then
- mdl = class
- class = "prop_physics"
- this = ents.Create(class)
- this:SetModel(mdl)
- else
- this = ents.Create(class)
- end
- this:Spawn()
- this:SetPos(there + Vector(0,0,this:BoundingRadius() * 2))
- this:DropToFloor()
- this:PhysWake()
- undo.Create(class)
- undo.SetPlayer(me)
- undo.AddEntity(this)
- undo.Finish()
- end,
- you = ent:IsPlayer() and ent or dis:IsPlayer() and dis or NULL
- }
- for key, value in pairs(vars) do
- _G[key] = value
- end
- e._G_vars = vars
- end
- function easylua.End()
- if e._G_vars then
- for key in pairs(e._G_vars) do
- if _G[key] then
- _G[key] = nil
- end
- end
- end
- end
- function easylua.RunLua(ply, code)
- check(ply, "Player")
- check(code, "string")
- easylua.Start(ply)
- local locals = ""
- for key in pairs(e.GetVars()) do
- locals = locals .. " local " .. key .. " = " .. key .. " "
- end
- local func = CompileString(locals .. " " .. code, tostring(ply))
- if func then
- local noerr, errormsg = pcall(func)
- if noerr == false then
- ErrorNoHalt("Script from '"..tostring(ply).."' errored: "..tostring(errormsg))
- end
- end
- easylua.End()
- end
- hook.Add("PreLuaDevRun", "easylua_luadev", function(_,_,ply)
- if IsValid(ply) and ply:IsPlayer() then
- easylua.Start(ply)
- end
- end)
- hook.Add("PostLuaDevRun", "easylua_luadev", function()
- easylua.End()
- end)
Advertisement
Add Comment
Please, Sign In to add comment