aboutsummaryrefslogtreecommitdiff
path: root/Northstar.CustomServers/mod/scripts/vscripts/_global_entities.gnut
blob: 767436d9c257b0cdc8c2fdf51344c5bd18821f72 (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
untyped

globalize_all_functions

//=========================================================
//	_global_entities
//  Create/initialize various global entities
//=========================================================


// if you set this to zero, various things that spam developer 2 won't run
const SPAMS_DEVELOPER2 = 1

void function MP_PlayerPostInit( entity self )
{
	entity player = self
	Assert( !player.hasSpawned )

	player.InitMPClasses()

	player.hasSpawned = true
}

array _PlayerDidSpawnCallbacks = []

function __PlayerDidSpawn( entity player )
{
	if ( player.GetPlayerName() == "Replay" )
		return

	foreach( callback in _PlayerDidSpawnCallbacks )
	{
		thread callback()
	}

	svGlobal.levelEnt.Signal( "PlayerDidSpawn", { player=player } )
	FlagSet( "PlayerDidSpawn" )
}

function AddCallback_PlayerDidSpawn( callback )
{
	_PlayerDidSpawnCallbacks.append( callback )
}

function ClientCommand( client, command, delay = 0 )
{
	EntFireByHandle( _cc, "Command", command, delay, client, null )
}

function ServerCommand( command, delay = 0 )
{
	EntFireByHandle( _sc, "Command", command, delay, null, null )
}

table __trackedRefs = {}

function AddTrackRef( ref )
{
	//printl( "Adding ref for: " + string( ref ) )
	__trackedRefs[string( ref )] <- ref.weakref()
}

function RefTrackerThink()
{
	foreach ( refName, entity refObj in __trackedRefs )
	{
		if ( !refObj || !refObj.ref() )
		{
			delete __trackedRefs[refName]
			if ( SPAMS_DEVELOPER2 )
				svGlobal.levelEnt.Fire( "CallScriptFunction", "RefTrackerThink", 0.033 )
			return
		}

		if ( IsValid_ThisFrame( refObj ) )
			continue

		printl( "UNFREED REFERENCE (use weakref for entities): " + refName )
		__trackedRefs[ refName ] = null
	}

	if ( SPAMS_DEVELOPER2 )
		svGlobal.levelEnt.Fire( "CallScriptFunction", "RefTrackerThink", 2.0 )
}

function DumpTrackRefs()
{
	foreach ( refName, refObj in __trackedRefs )
	{
		if ( !refObj || !refObj.ref() )
			continue

		printl( "TRACKREF: " + refName + " " + refObj.ref() )

	}
}

function MapRequiresFullFlightpathSupport()
{
	return false //GetMapName().find( "mp_" ) == 0
}

function AINFileIsUpToDate_Wrapper()
{
	if ( GetAINScriptVersion() != AIN_REV )
		return false

	return AINFileIsUpToDate()
}


function Hud_Hide( __t__, __tt__ )
{
}

function Hud_Show( __t__, __tt__ )
{
}

function PathsOutOfDate( player )
{
	SendHudMessage( player, "Paths Out of Date. Type buildainfile at console.", -1, 0.4, 255, 255, 0, 255, 0.0, 0.5, 0.0 )
	//	, x_pos, y_pos, R, G, B, A, fade_in_time, hold_time, fade_out_time )
}

function NavmeshOutOfDate( player )
{
	SendHudMessage( player, "Navmesh Out of Date. Build in LevelEd", -1, 0.6, 192, 255, 0, 255, 0.0, 0.5, 0.0 )
	//	, x_pos, y_pos, R, G, B, A, fade_in_time, hold_time, fade_out_time )
}


function NavmeshUpToDateCheck()
{
	FlagWait( "PlayerDidSpawn" )

	if ( NavMesh_IsUpToDate() )
		return

	for ( int i = 0; i < 5; i++ )
	{
		wait 1

		array<entity> players = GetPlayerArray()
		// let's not spam the whole server
		if ( players.len() )
			NavmeshOutOfDate( players[0] )
	}
}


// paths out of date
function AINFileIsUpToDateCheck()
{
	FlagWait( "PlayerDidSpawn" )

	if ( AINFileIsUpToDate_Wrapper() )
		return

	if ( !AINExists() )
		return

	for ( int i = 0; i < 5; i++ )
	{
		wait 1

		array<entity> players = GetPlayerArray()
		// let's not spam the whole server
		if ( players.len() )
			PathsOutOfDate( players[0] )
	}
}

function PlayerSeesGraphWarning( player )
{
	player.EndSignal( "OnDestroy" )
	local i
	float minWait = 0.03
	float maxWait = 0.7
	local result
	int max = 15
	for ( i = 0; i < max; i++ )
	{
		result = Graph( i, 0, max, maxWait, minWait )

		wait result
		if ( !IsValid( player ) )
			return

		if ( GetNPCArray().len() == 0 )
			continue

		wait result * 0.5
		if ( !IsValid( player ) )
			return

		PathsOutOfDate( player )
	}

	for ( ;; )
	{
		wait 0.5
		if ( !IsValid( player ) )
			return

		if ( GetNPCArray().len() == 0 )
			continue

		wait 0.3
		if ( !IsValid( player ) )
			return

		PathsOutOfDate( player )
	}
}



// Look up and set damageSourceIds for environmental damage triggers
// This works this way so maps don't have to be recompiled if any damageSourceIds change
void function InitDamageTriggers( entity self )
{
	if ( !self.HasKey( "damageSourceName" ) )
		return

	switch ( self.GetValueForKey( "damageSourceName" ) )
	{
		case "fall":
			self.kv.damageSourceId = eDamageSourceId.fall
			break

		case "splat":
			self.kv.damageSourceId = eDamageSourceId.splat
			break

		case "burn":
			self.kv.damageSourceId = eDamageSourceId.burn
			break

		case "submerged":
			self.kv.damageSourceId = eDamageSourceId.submerged
			break

		case "electric_conduit":
			self.kv.damageSourceId = eDamageSourceId.electric_conduit
			break

		case "turbine":
			self.kv.damageSourceId = eDamageSourceId.turbine
			break

		case "lasergrid":
			self.kv.damageSourceId = eDamageSourceId.lasergrid
			break

		case "crush":
			self.kv.damageSourceId = eDamageSourceId.damagedef_crush
			break

		case "toxic_sludge":
			self.kv.damageSourceId = eDamageSourceId.toxic_sludge
			break

		default:
			Assert( false, "Unsupported damage source name on trigger_hurt: " + self.GetValueForKey( "damageSourceName" ) )
	}
}

bool function IsDamageFromDamageTrigger( damageInfo )
{
	switch ( DamageInfo_GetDamageSourceIdentifier( damageInfo ) )
	{
		case eDamageSourceId.fall:
		case eDamageSourceId.splat:
		case eDamageSourceId.burn:
		case eDamageSourceId.submerged:
			return true
	}

	return false
}

function ScriptLeakDetector()
{
	svGlobal.levelEnt.Signal( "GameEnd" )
	OnThreadEnd(
		function() : ()
		{
			TotalEnts()
		}
	)

	WaitFrame()
	TotalEnts()

	for ( ;; )
	{
		wait 60
		TotalEnts()
	}
}

void function NavmeshSeparatorThink( entity separator )
{
	bool connected = true
	if ( separator.HasKey( "startDisconnected" ) && separator.kv.startDisconnected == "1" )
		connected = false

	ToggleNPCPathsForEntity( separator, connected )
	if ( connected )
		separator.NotSolid()
	else
		separator.Solid()

	if ( separator.HasKey( "script_flag" ) && separator.kv.script_flag != "" )
	{
		string flag = string( separator.kv.script_flag )
		FlagInit( flag, connected )

		while( IsValid( separator ) )
		{
			if ( connected )
				FlagWaitClear( flag )
			else
				FlagWait( flag )

			connected = !connected
			ToggleNPCPathsForEntity( separator, connected )
			if ( connected )
				separator.NotSolid()
			else
				separator.Solid()
		}
	}
}

void function DevDebugText( entity node )
{
	Assert( node.HasKey( "text" ) && node.kv.text != "", "info_debug_text at " + node.GetOrigin() + " doesn't have text set on it." )
	// Debug text doesn't work right away becuase of code, this delay makes them show up
	wait 3.0
	DebugDrawText( node.GetOrigin(), string( node.kv.text ), true, 999999.9 )
}