diff options
| author | Alexander Pickering <alexandermpickering@gmail.com> | 2016-05-06 20:20:56 -0400 |
|---|---|---|
| committer | Alexander Pickering <alexandermpickering@gmail.com> | 2016-05-06 20:20:56 -0400 |
| commit | 5322c9c6534054c0e2da6441e477d0b2166fbe97 (patch) | |
| tree | c10bba96ddcbb82b000f5cc569cdfff5e5522d08 | |
| parent | 76e9c919b926f35f51e147e042eadc992f06018e (diff) | |
| download | gmstranded-5322c9c6534054c0e2da6441e477d0b2166fbe97.tar.gz gmstranded-5322c9c6534054c0e2da6441e477d0b2166fbe97.tar.bz2 gmstranded-5322c9c6534054c0e2da6441e477d0b2166fbe97.zip | |
Did some refactoring
| -rw-r--r-- | entities/entities/gms_generic_plantable.lua | 96 | ||||
| -rw-r--r-- | gamemode/init.lua | 160 | ||||
| -rw-r--r-- | gamemode/itemsystem/common_plantable.lua | 27 | ||||
| -rw-r--r-- | gamemode/itemsystem/loaditems.lua | 20 |
4 files changed, 110 insertions, 193 deletions
diff --git a/entities/entities/gms_generic_plantable.lua b/entities/entities/gms_generic_plantable.lua index 81f31b1..b5fcdf2 100644 --- a/entities/entities/gms_generic_plantable.lua +++ b/entities/entities/gms_generic_plantable.lua @@ -24,99 +24,3 @@ function ENT:OnInitialize() self:Remove() end) end - -function ENT:Grow() - local ply = self:GetOwner() - local pos = self:GetPos() - - local num = 1 - if ( IsValid( ply ) && ply:HasUnlock( "Adept_Farmer" ) ) then num = num + math.random( 0, 1 ) end - if ( IsValid( ply ) && ply:HasUnlock( "Expert_Farmer" ) ) then num = num + math.random( 0, 2 ) end - - if ( self.Type == "tree" ) then - GAMEMODE.MakeTree( pos ) - elseif ( self.Type == "melon" ) then - GAMEMODE.MakeMelon( pos, num, ply ) - elseif ( self.Type == "banana" ) then - GAMEMODE.MakeBanana( pos, num, ply ) - elseif ( self.Type == "orange" ) then - GAMEMODE.MakeOrange( pos, num, ply ) - elseif ( self.Type == "grain" ) then - GAMEMODE.MakeGrain( pos, ply ) - elseif ( self.Type == "berry" ) then - GAMEMODE.MakeBush( pos, ply ) - end - - self.Grown = true - self:Fadeout() -end - -function ENT:OnRemove() - if ( !self.Grown && self.Type != "tree" && IsValid( self:GetOwner() ) ) then - self:GetOwner():SetNWInt( "plants", self:GetOwner():GetNWInt( "plants" ) - 1 ) - end - timer.Destroy( "GMS_SeedTimers_" .. self:EntIndex() ) -end - -function GAMEMODE.MakeGenericPlant( ply, pos, mdl, isTree ) - local ent = ents.Create( "prop_dynamic" ) - ent:SetAngles( Angle( 0, math.random( 0, 360 ), 0 ) ) - ent:SetSolid( SOLID_VPHYSICS ) - ent:SetModel( mdl ) - ent:SetPos( pos ) - ent:Spawn() - ent.IsPlant = true - ent:SetName( "gms_plant" .. ent:EntIndex() ) - - ent:Fadein() - ent:RiseFromGround( 1, 50 ) - - if ( !isTree && IsValid( ply ) ) then - ent:SetNWEntity( "plantowner", ply ) - SPropProtection.PlayerMakePropOwner( ply, ent ) - else - ent:SetNWString( "Owner", "World" ) - end - - local phys = ent:GetPhysicsObject() - if ( IsValid( phys ) ) then phys:EnableMotion( false ) end - ent.PhysgunDisabled = true - - return ent -end - -function GAMEMODE.MakeTree( pos ) - //GAMEMODE.MakeGenericPlant( ply, pos, GMS.TreeModels[ math.random( 1, #GMS.TreeModels ) ], true ) - local ent = ents.Create( "gms_tree" ) - ent:SetPos( pos ) - ent:Spawn() - ent.GMSAutoSpawned = true - ent:SetNetworkedString( "Owner", "World" ) -end - - -function GAMEMODE.MakeBush( pos, ply ) - GAMEMODE.MakeGenericPlant( ply, pos + Vector( math.random( -10, 10 ), math.random( -10, 10 ), 16 ), "models/props/pi_shrub.mdl" ) -end - -function GAMEMODE.MakeMelon( pos, num, ply ) - local plant = GAMEMODE.MakeGenericPlant( ply, pos + Vector( 0, 0, 13 ), "models/props/CS_militia/fern01.mdl" ) - plant.Children = 0 - - for i = 1, num do - GAMEMODE.MakeGenericPlantChild( ply, pos + Vector( math.random( -25, 25 ), math.random( -25, 25 ), math.random( 5, 7 ) ), "models/props_junk/watermelon01.mdl", plant ) - end -end - -function GAMEMODE.MakeOrange( pos, num, ply ) - local plant = GAMEMODE.MakeGenericPlant( ply, pos + Vector( 0, 0, -12 ), "models/props/cs_office/plant01_p1.mdl" ) - plant.Children = 0 - - plant:SetCollisionGroup( 0 ) - plant:SetSolid( SOLID_NONE ) - plant.Children = 0 - - for i = 1, num do - GAMEMODE.MakeGenericPlantChild( ply, pos + Vector( math.random( -5, 5 ), math.random( -5, 5 ), math.random( 13, 30 ) ), "models/props/cs_italy/orange.mdl", plant ) - end -end diff --git a/gamemode/init.lua b/gamemode/init.lua index 2d15d1f..ce2cddd 100644 --- a/gamemode/init.lua +++ b/gamemode/init.lua @@ -167,108 +167,106 @@ 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 + if ( GetConVarNumber( "gms_ReproduceTrees" ) != 1 ) then return end + 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 ( #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 ( num != 1 ) then continue end + 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 + 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 + 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 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 + local seed = ents.Create( "gms_seed" ) + seed:SetPos( pos ) + seed:DropToGround() + seed:Setup( "tree", 180 ) + seed:SetNetworkedString( "Owner", "World" ) + seed:Spawn() 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 + 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 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 + --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 ) + local groundtrace = util.TraceLine( trace ) - --Assure space - local nearby = ents.FindInSphere( groundtrace.HitPos, 200 ) - info.HasSpace = true + --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 + 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 + --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 - timer.Simple( math.random( 1, 3 ) * 60, function() GM.ReproduceTrees() end ) + timer.Adjust("GMS_ReproduceTreesTimer", math.random(1, 3)*60, 0, GM.ReproduceTrees) end -timer.Simple( 60, function() GAMEMODE.ReproduceTrees() end ) +timer.Create("GMS_ReproduceTreesTimer",60,0,GM.ReproduceTrees) /* ---------------------------------------------------------------------------------------------------- Admin commands diff --git a/gamemode/itemsystem/common_plantable.lua b/gamemode/itemsystem/common_plantable.lua index 5e3ad55..2728b9a 100644 --- a/gamemode/itemsystem/common_plantable.lua +++ b/gamemode/itemsystem/common_plantable.lua @@ -82,3 +82,30 @@ function GAMEMODE.MakeGenericPlantChild( ply, pos, mdl, parent ) return ent end + +function GAMEMODE.MakeGenericPlant( ply, pos, mdl, isTree ) + local ent = ents.Create( "prop_dynamic" ) + ent:SetAngles( Angle( 0, math.random( 0, 360 ), 0 ) ) + ent:SetSolid( SOLID_VPHYSICS ) + ent:SetModel( mdl ) + ent:SetPos( pos ) + ent:Spawn() + ent.IsPlant = true + ent:SetName( "gms_plant" .. ent:EntIndex() ) + + ent:Fadein() + ent:RiseFromGround( 1, 50 ) + + if ( !isTree && IsValid( ply ) ) then + ent:SetNWEntity( "plantowner", ply ) + SPropProtection.PlayerMakePropOwner( ply, ent ) + else + ent:SetNWString( "Owner", "World" ) + end + + local phys = ent:GetPhysicsObject() + if ( IsValid( phys ) ) then phys:EnableMotion( false ) end + ent.PhysgunDisabled = true + + return ent +end diff --git a/gamemode/itemsystem/loaditems.lua b/gamemode/itemsystem/loaditems.lua index a77ffb3..225e56d 100644 --- a/gamemode/itemsystem/loaditems.lua +++ b/gamemode/itemsystem/loaditems.lua @@ -5,29 +5,17 @@ GMS.Resources = {} function GMS.RegisterResource( tbl ) - if(tbl == nil) then - print("/gamemode/itemsystem/loaditems.lua: Attempted to register a nil entity! This might be a bug!") - return - end - if(tbl.Name == nil) then - print("/gamemode/itemsystem/loaditems.lua: Attempted to register an item with no name:") - PrintTable(tbl) - print("This might be a bug!") - return - end + assert(tbl != nil,"Attempted to register a nil entity! This might be a bug!") + assert(tbl.Name != nil,"Attempted to register an item with no name:") if(tbl.UniqueData) then tbl.UniqueDataID = -1 end - print("Registering item:" .. tbl.Name) GMS.Resources[tbl.Name] = tbl end function GMS.GetResourceByName(name) - if(GMS.Resources[name]) then - return GMS.Resources[name] - else - print("Resource " .. name .. " does not exist! This might be a bug!") - end + assert(GMS.Resources[name] != nil, "Resource " .. name .. " does not exist! This might be a bug!") + return GMS.Resources[name] end concommand.Add("gms_printrestable",function(ply,cmd,args) |
