Skip to content

Instantly share code, notes, and snippets.

@NanoAi
Created August 2, 2016 08:24
Show Gist options
  • Save NanoAi/85783551ebad9c410ae7497059fca732 to your computer and use it in GitHub Desktop.
Save NanoAi/85783551ebad9c410ae7497059fca732 to your computer and use it in GitHub Desktop.
[Garry's Mod] Spawn Protection for Build and RP servers.
local ents = ents
local player = player
local hook = hook
local _G = _G
local vec = {
Vector(3395.537598,1590.232178,166.751633), -- Coordinate Mins
Vector(2890.368164,769.798523,-194.843491) -- Coordinate Maxs
}
local function isPlayer(ent)
if ent == NULL or ent == nil then return false end
return IsValid(ent) and ( ent.GetClass and ent:GetClass() == "player" or ent:IsPlayer() )
end
hook.Add("PlayerSpawnObject", "optispawn", function(ply)
if IsValid(ply) and ply:GetNWBool("InSpawn", false) then
return false
end
end)
do
if SERVER then
for _,v in next, ents.FindByClass("base_brush") do if IsValid(v) and v.OptiSpawn_Trigger then v:Remove() end end -- There can only be one.
local function match(str,...)
local t = {...}
for _,v in next, t do
if string.find(str,v) then
return true
end
end
return false
end
local trigger = ents.Create("base_brush")
trigger.Type = "brush"
trigger.child = ents.Create("env_entity_dissolver")
function trigger:Initialize()
self:SetSolid(SOLID_BBOX)
self:SetCollisionBoundsWS(vec[1], vec[2])
self.child:SetPos(self:WorldSpaceCenter())
self.child:Spawn()
self.child:Activate()
self.child:SetKeyValue("target", "__killme_optispawn")
self.child:SetKeyValue("dissolvetype", "1")
end
function trigger:OnRemove()
if IsValid(self.child) then self.child:Remove() end
end
function trigger:StartTouch(ent)
if isPlayer(ent) then
ent:SetNWBool("InSpawn", true)
ent:GodEnable()
ent:SetNoTarget(true)
ent.RHealth = CurTime() + 10
local activeweapon = ent:GetActiveWeapon()
ent.WeaponCache = {}
ent.LastActiveWeapon = IsValid(activeweapon) and activeweapon.GetClass and activeweapon:GetClass() or nil
for k,v in next, ent:GetWeapons() do
ent.WeaponCache[k] = v.GetClass and v:GetClass() or nil
end
ent:StripWeapons()
elseif IsValid(ent) and ent.GetClass and match(ent:GetClass(), "prop_physics", "gmod_", "wire_") then
local phys = IsValid(ent:GetPhysicsObject()) and ent:GetPhysicsObject()
if phys then
phys:EnableGravity(false)
phys:EnableMotion(true)
phys:SetVelocityInstantaneous(phys:GetVelocity()*0.5)
phys:AddAngleVelocity(Vector(600,0,600))
end
ent:SetName("__killme_optispawn")
self.child:Fire("Dissolve", "", 0)
end
end
function trigger:Touch(ent)
if IsValid(ent) then
if isPlayer(ent) then
if ent:Health() < 100 and ent.RHealth <= CurTime() then
ent:SetHealth(ent:Health()+1)
ent.RHealth = CurTime() + 0.5
end
elseif ent.GetClass and (match(ent:GetClass(), "prop_physics", "gmod_", "wire_", "vehicle_", "npc_") or ent:IsVehicle() or ent:IsNPC()) then
local direction = ( trigger:WorldSpaceCenter() - ent:GetPos() )
local t = util.QuickTrace( trigger:WorldSpaceCenter(), direction )
local velocity = ((-direction*1.5)*Vector(1,1,0))+Vector(0,0,60)
ent:SetVelocity(velocity)
local phys = ent.GetPhysicsObject and ent:GetPhysicsObject() or nil
if IsValid(phys) then phys:SetVelocityInstantaneous(velocity) end
end
end
end
function trigger:EndTouch(ent)
if isPlayer(ent) then
ent:SetNWBool("InSpawn", false)
if not ent.ULXHasGod then ent:GodDisable() end
ent:SetNoTarget(false)
ent.RHealth = nil
for _,v in next, ent.WeaponCache do
ent:Give(v)
end
if ent.LastActiveWeapon then
ent:SelectWeapon(ent.LastActiveWeapon)
end
ent.WeaponCache = nil
ent.LastActiveWeapon = nil
end
end
trigger:SetTrigger(true)
trigger.OptiSpawn_Trigger = true
trigger:Spawn()
trigger:Activate()
end
end
do
if CLIENT then
local render = render
local ply = LocalPlayer
hooks = {"SetupWorldFog", "SetupSkyFog"}
local Fog, UpdateFog, FogSetOunce, FogSpeed = 0, 0, false, 0.05
for _,v in next, hooks do
hook.Add(v, "optispawn", function()
local isInSpawn = ply():GetNWBool("InSpawn", false)
Fog = math.min(1, (Fog == UpdateFog and Fog) or Lerp(FogSpeed, Fog, UpdateFog))
render.FogMode(1)
render.FogStart(0.0)
render.FogEnd(700)
render.FogColor(0,0,0)
render.FogMaxDensity(Fog)
if(Fog > 0) then
if not isInSpawn then UpdateFog = UpdateFog - 0.1 end
return true
elseif FogSetOnce then
FogSetOnce = false
end
if isInSpawn and not FogSetOnce then
UpdateFog = 1
FogSetOnce = true
end
end)
end
local vector_zero = Vector(0,0,0)
local angle_zero = Angle(0,0,0)
local function DrawBox(width, min, max, col)
render.DrawBox(vector_zero, angle_zero, min, Vector(min.x - width, max.y, min.z - width), col, true) -- Back Face Top
render.DrawBox(vector_zero, angle_zero, min, Vector(max.x, min.y - width, min.z - width), col, true) -- Left Face Top
render.DrawBox(vector_zero, angle_zero, min, Vector(min.x - width, min.y - width, max.z), col, true) -- Left Face Left
render.DrawBox(vector_zero, angle_zero, max, Vector(max.x + width, max.y + width, min.z), col, true) -- Front Face Right
render.DrawBox(vector_zero, angle_zero, max, Vector(max.x + width, min.y, max.z + width), col, true) -- Front Face Bottom
render.DrawBox(vector_zero, angle_zero, max, Vector(min.x, max.y + width, max.z + width), col, true) -- Right Face Bottom
render.DrawBox(vector_zero, angle_zero, Vector( min.x, min.y, max.z ), Vector( min.x - width, max.y + width, max.z + width ), col, true) -- Front Face Bottom
render.DrawBox(vector_zero, angle_zero, Vector( min.x, min.y, max.z ), Vector( max.x + width, min.y - width, max.z + width ), col, true) -- Left Face Bottom
render.DrawBox(vector_zero, angle_zero, Vector( max.x, min.y, max.z ), Vector( max.x + width, min.y - width, min.z - width ), col, true) -- Front Face Left
render.DrawBox(vector_zero, angle_zero, Vector( min.x, max.y, max.z ), Vector( min.x - width, max.y + width, min.z - width ), col, true) -- Back Face Right
render.DrawBox(vector_zero, angle_zero, Vector( max.x, max.y, min.z ), Vector( min.x - width, max.y + width, min.z - width ), col, true) -- Right Face Top
render.DrawBox(vector_zero, angle_zero, Vector( max.x, max.y, min.z ), Vector( max.x + width, min.y - width, min.z - width ), col, true) -- Front Face Top
end
local col = Color(20,40,125,255)
hook.Add("PostDrawOpaqueRenderables", "optispawn", function()
local isInSpawn = ply():GetNWBool("InSpawn", false)
if isInSpawn then
col = Color(20,125,40,255)
else
col = Color(20,40,125,255)
end
render.SuppressEngineLighting(true)
render.SetColorMaterial()
DrawBox(2, vec[1], vec[2], col)
render.SuppressEngineLighting(false)
end)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment