From b24ba036889a644bb9505f61e3feeda370299c98 Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Sat, 16 Apr 2016 12:18:37 -0400 Subject: Moved entity function from init.lua to it's own file --- gamemode/init.lua | 291 +---------------------------------- gamemode/server/entity_functions.lua | 290 ++++++++++++++++++++++++++++++++++ 2 files changed, 291 insertions(+), 290 deletions(-) create mode 100644 gamemode/server/entity_functions.lua diff --git a/gamemode/init.lua b/gamemode/init.lua index de31a00..339c2ab 100644 --- a/gamemode/init.lua +++ b/gamemode/init.lua @@ -206,296 +206,7 @@ end Entity Functions ---------------------------------------------------------------------------------------------------- */ -function EntityMeta:SetResourceDropInfo( strType, int ) - timer.Simple( 0.5, function() self:SetResourceDropInfoInstant( strType, int ) end ) -end - -function EntityMeta:SetResourceDropInfoInstant( strType, int ) - for k, v in pairs( player.GetAll() ) do - local strType = strType or "Error" - umsg.Start( "gms_SetResourceDropInfo", v ) - umsg.String( self:EntIndex() ) - umsg.String( string.gsub( strType, "_", " " ) ) - umsg.Short( self.Amount ) - umsg.End() - end -end - -function EntityMeta:SetResPackInfo( strType, int ) - for k, v in pairs( player.GetAll() ) do - local strType = strType or "Error" - umsg.Start( "gms_SetResPackInfo", v ) - umsg.String( self:EntIndex() ) - umsg.String( string.gsub( strType, "_", " " ) ) - umsg.Short( int ) - umsg.End() - end -end - - -function EntityMeta:SetFoodInfo( strType ) - timer.Simple( 0.5, function() self:SetFoodInfoInstant( strType ) end ) -end - -function EntityMeta:SetFoodInfoInstant( strType ) - for k, v in pairs( player.GetAll() ) do - local strType = strType or "Error" - umsg.Start( "gms_SetFoodDropInfo", v ) - umsg.String( self:EntIndex() ) - umsg.String( string.gsub( strType, "_", " " ) ) - umsg.End() - end -end - -function EntityMeta:DropToGround() - local trace = {} - trace.start = self:GetPos() - trace.endpos = trace.start + Vector( 0, 0, -100000 ) - trace.mask = MASK_SOLID_BRUSHONLY - trace.filter = self - - local tr = util.TraceLine( trace ) - - self:SetPos( tr.HitPos ) -end - -function GM.ReproduceTrees() - local GM = GAMEMODE - if ( GetConVarNumber( "gms_ReproduceTrees" ) == 1 ) then - local trees = {} - for k, v in pairs( ents.GetAll() ) do - if ( v:IsTreeModel() ) then - table.insert( trees, v ) - end - end - - if ( #trees < GetConVarNumber( "gms_MaxReproducedTrees" ) ) then - for k, ent in pairs( trees ) do - local num = math.random( 1, 3 ) - - if ( num == 1 ) then - local nearby = {} - for k, v in pairs( ents.FindInSphere( ent:GetPos(), 50 ) ) do - if ( v:GetClass() == "gms_seed" or v:IsProp() ) then - table.insert( nearby, v ) - end - end - - if ( #nearby < 3 ) then - local pos = ent:GetPos() + Vector( math.random( -500, 500 ), math.random( -500, 500 ), 0 ) - local retries = 50 - - while ( ( pos:Distance( ent:GetPos() ) < 200 or GMS.ClassIsNearby( pos, "prop_physics", 100 ) ) and retries > 0 ) do - pos = ent:GetPos() + Vector( math.random( -300, 300 ),math.random( -300, 300 ), 0 ) - retries = retries - 1 - end - - local pos = pos + Vector( 0, 0, 500 ) - - local seed = ents.Create( "gms_seed" ) - seed:SetPos( pos ) - seed:DropToGround() - seed:Setup( "tree", 180 ) - seed:SetNetworkedString( "Owner", "World" ) - seed:Spawn() - end - end - end - end - if ( #trees == 0 ) then - local info = {} - for i = 1, 20 do - info.pos = Vector( math.random( -10000, 10000 ), math.random( -10000, 10000 ), 1000 ) - info.Retries = 50 - - --Find pos in world - while ( util.IsInWorld( info.pos ) == false and info.Retries > 0 ) do - info.pos = Vector( math.random( -10000, 10000 ),math.random( -10000, 10000 ), 1000 ) - info.Retries = info.Retries - 1 - end - - --Find ground - local trace = {} - trace.start = info.pos - trace.endpos = trace.start + Vector( 0, 0, -100000 ) - trace.mask = MASK_SOLID_BRUSHONLY - - local groundtrace = util.TraceLine( trace ) - - --Assure space - local nearby = ents.FindInSphere( groundtrace.HitPos, 200 ) - info.HasSpace = true - - for k, v in pairs( nearby ) do - if ( v:IsProp() ) then - info.HasSpace = false - end - end - - --Find sky - local trace = {} - trace.start = groundtrace.HitPos - trace.endpos = trace.start + Vector( 0, 0, 100000 ) - - local skytrace = util.TraceLine( trace ) - - --Find water? - local trace = {} - trace.start = groundtrace.HitPos - trace.endpos = trace.start + Vector( 0, 0, 1 ) - trace.mask = MASK_WATER - - local watertrace = util.TraceLine( trace ) - - --All a go, make entity - if ( info.HasSpace and skytrace.HitSky and !watertrace.Hit and ( groundtrace.MatType == MAT_DIRT or groundtrace.MatType == MAT_GRASS or groundtrace.MatType == MAT_SAND ) ) then - local seed = ents.Create( "gms_seed" ) - seed:SetPos( groundtrace.HitPos ) - seed:DropToGround() - seed:Setup( "tree", 180 + math.random( -20, 20 ) ) - seed:SetNetworkedString( "Owner", "World" ) - seed:Spawn() - end - end - end - end - - timer.Simple( math.random( 1, 3 ) * 60, function() GM.ReproduceTrees() end ) -end -timer.Simple( 60, function() GAMEMODE.ReproduceTrees() end ) - -GMS.LootableNPCs = { "npc_antlion", "npc_antlionguard", "npc_crow", "npc_seagull", "npc_pigeon", "npc_zombie" } - -function EntityMeta:IsLootableNPC() - return table.HasValue( GMS.LootableNPCs, self:GetClass() ) -end - -function EntityMeta:MakeCampfire() - if ( GetConVarNumber( "gms_campfire" ) <= 0 ) then return end - - local min, max = self:OBBMins(), self:OBBMaxs() - local vol = math.abs( max.x - min.x ) * math.abs( max.y - min.y ) * math.abs( max.z - min.z ) - local mul = math.min( math.sqrt( vol ) / 200, 1 ) - - if ( !self.CampFire ) then self:SetHealth( 1337 ) end - self.CampFire = true - - timer.Create( "gms_removecampfire_" .. self:EntIndex(), 480 * mul, 1, function() if ( IsValid( self ) ) then self:Fadeout() end end ) - - if ( GetConVarNumber( "gms_SpreadFire" ) >= 1 ) then - self:Ignite( 360, ( self:OBBMins() - self:OBBMaxs() ):Length() + 10 ) - else - self:Ignite( 360, 0.001 ) - end -end - -/* ---------------------------------------------------------------------------------------------------- - Entity Fading ----------------------------------------------------------------------------------------------------- */ - -GMS.FadingOutProps = {} -GMS.FadingInProps = {} - -function EntityMeta:Fadeout( speed ) - if ( !IsValid( self ) ) then return end - local speed = speed or 1 - - for k, v in pairs( player.GetAll() ) do - umsg.Start( "gms_CreateFadingProp", v ) - umsg.String( self:GetModel() ) - umsg.Vector( self:GetPos() ) - local ang = self:GetAngles() - umsg.Vector( Vector( ang.p, ang.y, ang.r ) ) - local col = self:GetColor() - umsg.Vector( Vector( col.r, col.g, col.b ) ) - umsg.Short( math.Round( speed ) ) - umsg.End() - end - - self:Remove() -end - ---Fadein is serverside -function EntityMeta:Fadein( speed ) - self.AlphaFade = 0 - self:SetRenderMode( RENDERMODE_TRANSALPHA ) - self:SetColor( Color( 255, 255, 255, 0 ) ) - self.FadeInSpeed = speed or 4 - table.insert( GMS.FadingInProps, self ) -end - -hook.Add( "Think", "gms_FadePropsThink", function() - for k, ent in pairs( GMS.FadingInProps ) do - if ( !ent or ent == NULL ) then - table.remove( GMS.FadingInProps, k ) - elseif ( !IsValid( ent ) ) then - table.remove( GMS.FadingInProps, k ) - elseif ( ent.AlphaFade >= 255 ) then - table.remove( GMS.FadingInProps, k ) - else - ent.AlphaFade = ent.AlphaFade + ent.FadeInSpeed - ent:SetColor( Color( 255, 255, 255, ent.AlphaFade ) ) - end - end -end ) - -/* ---------------------------------------------------------------------------------------------------- - Entity rising / lowering ( Used by gms_seed ) ----------------------------------------------------------------------------------------------------- */ - -GM.RisingProps = {} -GM.SinkingProps = {} - -function EntityMeta:RiseFromGround( speed, altmax ) - local speed = speed or 1 - local max; - - if ( !altmax ) then - min, max = self:WorldSpaceAABB() - max = max.z - else - max = altmax - end - - local tbl = {} - tbl.Origin = self:GetPos().z - tbl.Speed = speed - tbl.Entity = self - - self:SetPos( self:GetPos() + Vector( 0, 0, -max + 10 ) ) - table.insert( GAMEMODE.RisingProps, tbl ) -end - -function EntityMeta:SinkIntoGround( speed ) - local speed = speed or 1 - - local tbl = {} - tbl.Origin = self:GetPos().z - tbl.Speed = speed - tbl.Entity = self - tbl.Height = max - - table.insert( GAMEMODE.SinkingProps, tbl ) -end - -hook.Add( "Think", "gms_RiseAndSinkPropsHook", function() - for k, tbl in pairs( GAMEMODE.RisingProps ) do - if ( !IsValid( tbl.Entity ) || tbl.Entity:GetPos().z >= tbl.Origin ) then - table.remove( GAMEMODE.RisingProps, k ) - else - tbl.Entity:SetPos( tbl.Entity:GetPos() + Vector( 0, 0, 1 * tbl.Speed ) ) - end - end - - for k, tbl in pairs( GAMEMODE.SinkingProps ) do - if ( !IsValid( tbl.Entity ) || tbl.Entity:GetPos().z <= tbl.Origin - tbl.Height ) then - table.remove( GAMEMODE.SinkingProps, k ) - tbl.Entity:Remove() - else - tbl.Entity:SetPos( tbl.Entity:GetPos() + Vector( 0, 0, -1 * tbl.Speed ) ) - end - end -end ) +//Moved to entity_functions.lua /* ---------------------------------------------------------------------------------------------------- Admin commands diff --git a/gamemode/server/entity_functions.lua b/gamemode/server/entity_functions.lua new file mode 100644 index 0000000..89fced9 --- /dev/null +++ b/gamemode/server/entity_functions.lua @@ -0,0 +1,290 @@ +function EntityMeta:SetResourceDropInfo( strType, int ) + timer.Simple( 0.5, function() self:SetResourceDropInfoInstant( strType, int ) end ) +end + +function EntityMeta:SetResourceDropInfoInstant( strType, int ) + for k, v in pairs( player.GetAll() ) do + local strType = strType or "Error" + umsg.Start( "gms_SetResourceDropInfo", v ) + umsg.String( self:EntIndex() ) + umsg.String( string.gsub( strType, "_", " " ) ) + umsg.Short( self.Amount ) + umsg.End() + end +end + +function EntityMeta:SetResPackInfo( strType, int ) + for k, v in pairs( player.GetAll() ) do + local strType = strType or "Error" + umsg.Start( "gms_SetResPackInfo", v ) + umsg.String( self:EntIndex() ) + umsg.String( string.gsub( strType, "_", " " ) ) + umsg.Short( int ) + umsg.End() + end +end + + +function EntityMeta:SetFoodInfo( strType ) + timer.Simple( 0.5, function() self:SetFoodInfoInstant( strType ) end ) +end + +function EntityMeta:SetFoodInfoInstant( strType ) + for k, v in pairs( player.GetAll() ) do + local strType = strType or "Error" + umsg.Start( "gms_SetFoodDropInfo", v ) + umsg.String( self:EntIndex() ) + umsg.String( string.gsub( strType, "_", " " ) ) + umsg.End() + end +end + +function EntityMeta:DropToGround() + local trace = {} + trace.start = self:GetPos() + trace.endpos = trace.start + Vector( 0, 0, -100000 ) + trace.mask = MASK_SOLID_BRUSHONLY + trace.filter = self + + local tr = util.TraceLine( trace ) + + self:SetPos( tr.HitPos ) +end + +function GM.ReproduceTrees() + local GM = GAMEMODE + if ( GetConVarNumber( "gms_ReproduceTrees" ) == 1 ) then + local trees = {} + for k, v in pairs( ents.GetAll() ) do + if ( v:IsTreeModel() ) then + table.insert( trees, v ) + end + end + + if ( #trees < GetConVarNumber( "gms_MaxReproducedTrees" ) ) then + for k, ent in pairs( trees ) do + local num = math.random( 1, 3 ) + + if ( num == 1 ) then + local nearby = {} + for k, v in pairs( ents.FindInSphere( ent:GetPos(), 50 ) ) do + if ( v:GetClass() == "gms_seed" or v:IsProp() ) then + table.insert( nearby, v ) + end + end + + if ( #nearby < 3 ) then + local pos = ent:GetPos() + Vector( math.random( -500, 500 ), math.random( -500, 500 ), 0 ) + local retries = 50 + + while ( ( pos:Distance( ent:GetPos() ) < 200 or GMS.ClassIsNearby( pos, "prop_physics", 100 ) ) and retries > 0 ) do + pos = ent:GetPos() + Vector( math.random( -300, 300 ),math.random( -300, 300 ), 0 ) + retries = retries - 1 + end + + local pos = pos + Vector( 0, 0, 500 ) + + local seed = ents.Create( "gms_seed" ) + seed:SetPos( pos ) + seed:DropToGround() + seed:Setup( "tree", 180 ) + seed:SetNetworkedString( "Owner", "World" ) + seed:Spawn() + end + end + end + end + if ( #trees == 0 ) then + local info = {} + for i = 1, 20 do + info.pos = Vector( math.random( -10000, 10000 ), math.random( -10000, 10000 ), 1000 ) + info.Retries = 50 + + --Find pos in world + while ( util.IsInWorld( info.pos ) == false and info.Retries > 0 ) do + info.pos = Vector( math.random( -10000, 10000 ),math.random( -10000, 10000 ), 1000 ) + info.Retries = info.Retries - 1 + end + + --Find ground + local trace = {} + trace.start = info.pos + trace.endpos = trace.start + Vector( 0, 0, -100000 ) + trace.mask = MASK_SOLID_BRUSHONLY + + local groundtrace = util.TraceLine( trace ) + + --Assure space + local nearby = ents.FindInSphere( groundtrace.HitPos, 200 ) + info.HasSpace = true + + for k, v in pairs( nearby ) do + if ( v:IsProp() ) then + info.HasSpace = false + end + end + + --Find sky + local trace = {} + trace.start = groundtrace.HitPos + trace.endpos = trace.start + Vector( 0, 0, 100000 ) + + local skytrace = util.TraceLine( trace ) + + --Find water? + local trace = {} + trace.start = groundtrace.HitPos + trace.endpos = trace.start + Vector( 0, 0, 1 ) + trace.mask = MASK_WATER + + local watertrace = util.TraceLine( trace ) + + --All a go, make entity + if ( info.HasSpace and skytrace.HitSky and !watertrace.Hit and ( groundtrace.MatType == MAT_DIRT or groundtrace.MatType == MAT_GRASS or groundtrace.MatType == MAT_SAND ) ) then + local seed = ents.Create( "gms_seed" ) + seed:SetPos( groundtrace.HitPos ) + seed:DropToGround() + seed:Setup( "tree", 180 + math.random( -20, 20 ) ) + seed:SetNetworkedString( "Owner", "World" ) + seed:Spawn() + end + end + end + end + + timer.Simple( math.random( 1, 3 ) * 60, function() GM.ReproduceTrees() end ) +end +timer.Simple( 60, function() GAMEMODE.ReproduceTrees() end ) + +GMS.LootableNPCs = { "npc_antlion", "npc_antlionguard", "npc_crow", "npc_seagull", "npc_pigeon", "npc_zombie" } + +function EntityMeta:IsLootableNPC() + return table.HasValue( GMS.LootableNPCs, self:GetClass() ) +end + +function EntityMeta:MakeCampfire() + if ( GetConVarNumber( "gms_campfire" ) <= 0 ) then return end + + local min, max = self:OBBMins(), self:OBBMaxs() + local vol = math.abs( max.x - min.x ) * math.abs( max.y - min.y ) * math.abs( max.z - min.z ) + local mul = math.min( math.sqrt( vol ) / 200, 1 ) + + if ( !self.CampFire ) then self:SetHealth( 1337 ) end + self.CampFire = true + + timer.Create( "gms_removecampfire_" .. self:EntIndex(), 480 * mul, 1, function() if ( IsValid( self ) ) then self:Fadeout() end end ) + + if ( GetConVarNumber( "gms_SpreadFire" ) >= 1 ) then + self:Ignite( 360, ( self:OBBMins() - self:OBBMaxs() ):Length() + 10 ) + else + self:Ignite( 360, 0.001 ) + end +end + +/* ---------------------------------------------------------------------------------------------------- + Entity Fading +---------------------------------------------------------------------------------------------------- */ + +GMS.FadingOutProps = {} +GMS.FadingInProps = {} + +function EntityMeta:Fadeout( speed ) + if ( !IsValid( self ) ) then return end + local speed = speed or 1 + + for k, v in pairs( player.GetAll() ) do + umsg.Start( "gms_CreateFadingProp", v ) + umsg.String( self:GetModel() ) + umsg.Vector( self:GetPos() ) + local ang = self:GetAngles() + umsg.Vector( Vector( ang.p, ang.y, ang.r ) ) + local col = self:GetColor() + umsg.Vector( Vector( col.r, col.g, col.b ) ) + umsg.Short( math.Round( speed ) ) + umsg.End() + end + + self:Remove() +end + +--Fadein is serverside +function EntityMeta:Fadein( speed ) + self.AlphaFade = 0 + self:SetRenderMode( RENDERMODE_TRANSALPHA ) + self:SetColor( Color( 255, 255, 255, 0 ) ) + self.FadeInSpeed = speed or 4 + table.insert( GMS.FadingInProps, self ) +end + +hook.Add( "Think", "gms_FadePropsThink", function() + for k, ent in pairs( GMS.FadingInProps ) do + if ( !ent or ent == NULL ) then + table.remove( GMS.FadingInProps, k ) + elseif ( !IsValid( ent ) ) then + table.remove( GMS.FadingInProps, k ) + elseif ( ent.AlphaFade >= 255 ) then + table.remove( GMS.FadingInProps, k ) + else + ent.AlphaFade = ent.AlphaFade + ent.FadeInSpeed + ent:SetColor( Color( 255, 255, 255, ent.AlphaFade ) ) + end + end +end ) + +/* ---------------------------------------------------------------------------------------------------- + Entity rising / lowering ( Used by gms_seed ) +---------------------------------------------------------------------------------------------------- */ + +GM.RisingProps = {} +GM.SinkingProps = {} + +function EntityMeta:RiseFromGround( speed, altmax ) + local speed = speed or 1 + local max; + + if ( !altmax ) then + min, max = self:WorldSpaceAABB() + max = max.z + else + max = altmax + end + + local tbl = {} + tbl.Origin = self:GetPos().z + tbl.Speed = speed + tbl.Entity = self + + self:SetPos( self:GetPos() + Vector( 0, 0, -max + 10 ) ) + table.insert( GAMEMODE.RisingProps, tbl ) +end + +function EntityMeta:SinkIntoGround( speed ) + local speed = speed or 1 + + local tbl = {} + tbl.Origin = self:GetPos().z + tbl.Speed = speed + tbl.Entity = self + tbl.Height = max + + table.insert( GAMEMODE.SinkingProps, tbl ) +end + +hook.Add( "Think", "gms_RiseAndSinkPropsHook", function() + for k, tbl in pairs( GAMEMODE.RisingProps ) do + if ( !IsValid( tbl.Entity ) || tbl.Entity:GetPos().z >= tbl.Origin ) then + table.remove( GAMEMODE.RisingProps, k ) + else + tbl.Entity:SetPos( tbl.Entity:GetPos() + Vector( 0, 0, 1 * tbl.Speed ) ) + end + end + + for k, tbl in pairs( GAMEMODE.SinkingProps ) do + if ( !IsValid( tbl.Entity ) || tbl.Entity:GetPos().z <= tbl.Origin - tbl.Height ) then + table.remove( GAMEMODE.SinkingProps, k ) + tbl.Entity:Remove() + else + tbl.Entity:SetPos( tbl.Entity:GetPos() + Vector( 0, 0, -1 * tbl.Speed ) ) + end + end +end ) -- cgit v1.2.3-70-g09d2