summaryrefslogtreecommitdiff
path: root/gamemode/server/entity_functions.lua
blob: 89fced9b957c650df8bf2e183503e198dfbc0ebb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
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 )