aboutsummaryrefslogtreecommitdiff
path: root/Northstar.CustomServers/mod/scripts/vscripts/_loadouts_mp.gnut
blob: 76cb4ac44859c360ad68bdd9c89f300ab143f34d (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
untyped
global function SvLoadoutsMP_Init

global function SetLoadoutGracePeriodEnabled
global function SetWeaponDropsEnabled

global function AddCallback_OnTryGetTitanLoadout
global function GetTitanLoadoutForPlayer

global struct sTryGetTitanLoadoutCallbackReturn
{
	bool wasChanged = false
	bool runMoreCallbacks = true
	TitanLoadoutDef& loadout
}

typedef TryGetTitanLoadoutCallbackType sTryGetTitanLoadoutCallbackReturn functionref( entity player, TitanLoadoutDef loadout, bool wasChanged )

struct {
	bool loadoutGracePeriodEnabled = true
	bool weaponDropsEnabled = true
	array< TryGetTitanLoadoutCallbackType > onTryGetTitanLoadoutCallbacks
	
	array<entity> dirtyLoadouts
} file

void function SvLoadoutsMP_Init()
{
	InitDefaultLoadouts() // titan loadout code relies on this, not called on server by default
		
	// most of these are fairly insecure right now, could break pdata if called maliciously, need fixing eventually
	RegisterSignal( "EndUpdateCachedLoadouts" )
	RegisterSignal( "GracePeriodDone" ) // temp to get weapons\_weapon_utility.nut:2271 to behave
	
	AddCallback_OnClientConnected( LoadoutsMPInitPlayer )
	
	AddClientCommandCallback( "RequestPilotLoadout", ClientCommandCallback_RequestPilotLoadout )
	AddClientCommandCallback( "RequestTitanLoadout", ClientCommandCallback_RequestTitanLoadout )
	AddClientCommandCallback( "SetPersistentLoadoutValue", ClientCommandCallback_SetPersistentLoadoutValue )
	AddClientCommandCallback( "SwapSecondaryAndWeapon3PersistentLoadoutData", ClientCommandCallback_SwapSecondaryAndWeapon3PersistentLoadoutData )
	AddClientCommandCallback( "SetBurnCardPersistenceSlot", ClientCommandCallback_SetBurnCardPersistenceSlot )
	
	if ( IsLobby() ) // can't usually set these in real games
	{
		AddClientCommandCallback( "SetCallsignIcon", ClientCommandCallback_SetCallsignIcon )
		AddClientCommandCallback( "SetCallsignCard", ClientCommandCallback_SetCallsignCard )
		AddClientCommandCallback( "SetFactionChoicePersistenceSlot", ClientCommandCallback_SetFactionChoicePersistenceSlot )
	}
	else
	{
		AddClientCommandCallback( "InGameMPMenuClosed", ClientCommandCallback_InGameMPMenuClosed )
		AddClientCommandCallback( "LoadoutMenuClosed", ClientCommandCallback_LoadoutMenuClosed )
	}
	
	AddCallback_OnPlayerKilled( DestroyDroppedWeapon )
}

void function SetLoadoutGracePeriodEnabled( bool enabled )
{
	file.loadoutGracePeriodEnabled = enabled
}

void function SetWeaponDropsEnabled( bool enabled )
{
	file.weaponDropsEnabled = enabled
}

void function DestroyDroppedWeapon( entity victim, entity attacker, var damageInfo )
{
	if ( !file.weaponDropsEnabled && IsValid( victim.GetActiveWeapon() ) )
		thread DelayDestroyDroppedWeapon( victim.GetActiveWeapon() )
}

void function DelayDestroyDroppedWeapon( entity weapon )
{
	WaitEndFrame()
	if ( IsValid( weapon ) )
		weapon.Destroy()
}

void function AddCallback_OnTryGetTitanLoadout( TryGetTitanLoadoutCallbackType callback )
{
	file.onTryGetTitanLoadoutCallbacks.append( callback )
}

TitanLoadoutDef function GetTitanLoadoutForPlayer( entity player )
{
	SetActiveTitanLoadout( player ) // set right loadout
	TitanLoadoutDef loadout = GetActiveTitanLoadout( player )

	// fix bug with titan weapons having null mods
	// null mods aren't valid and crash if we try to give them to npc
	loadout.primaryMods.removebyvalue( "null" )

	// allow scripts to modify loadouts
	bool wasChanged = false
	foreach ( TryGetTitanLoadoutCallbackType callback in file.onTryGetTitanLoadoutCallbacks )
	{
		sTryGetTitanLoadoutCallbackReturn callbackRet = callback( player, loadout, wasChanged )
		
		// whether the callback has changed the player's titan loadout
		wasChanged = wasChanged || callbackRet.wasChanged 
		if ( callbackRet.wasChanged )
			loadout = callbackRet.loadout
		
		// whether the callback has indicated that we should run no more callbacks ( e.g. if we're forcing a given loadout to be chosen, we shouldn't run any more )
		if ( !callbackRet.runMoreCallbacks )
			break
	}
	
	// do this again just in case
	loadout.primaryMods.removebyvalue( "null" )
	
	return loadout
}

void function LoadoutsMPInitPlayer( entity player )
{
	player.s.loadoutDirty <- false

	// these netints are required for callsigns and such to display correctly on other clients
	player.SetPlayerNetInt( "activeCallingCardIndex", player.GetPersistentVarAsInt( "activeCallingCardIndex" ) )
	player.SetPlayerNetInt( "activeCallsignIconIndex", player.GetPersistentVarAsInt( "activeCallsignIconIndex" ) )
}

// loadout clientcommands
bool function ClientCommandCallback_RequestPilotLoadout( entity player, array<string> args )
{
	if ( args.len() != 1 )
		return true
	
	print( player + " RequestPilotLoadout " + args[0] )
			
	// insecure, could be used to set invalid spawnloadout index potentially
	SetPersistentSpawnLoadoutIndex( player, "pilot", args[0].tointeger() )
	
	SetPlayerLoadoutDirty( player )
	
	return true
}

bool function ClientCommandCallback_RequestTitanLoadout( entity player, array<string> args )
{
	if ( args.len() != 1 )
		return true

	print( player + " RequestTitanLoadoutLoadout " + args[0] )
	
	// insecure, could be used to set invalid spawnloadout index potentially
	SetPersistentSpawnLoadoutIndex( player, "titan", args[0].tointeger() )
	
	if ( !IsLobby() )
		EarnMeterMP_SetTitanLoadout( player )
	
	return true
}

bool function ClientCommandCallback_SetPersistentLoadoutValue( entity player, array<string> args )
{
	//if ( args.len() != 4 )
	//	return true

	if ( args.len() < 4 )
		return true 
		
	string val = args[ 3 ]
	if ( args.len() > 4 ) // concat args after 3 into last arg so we can do strings with spaces and such
		for ( int i = 4; i < args.len(); i++ )
			val += " " + args[ i ]
	
	val = strip( val ) // remove any tailing whitespace

	print( player + " SetPersistentLoadoutValue " + args[0] + " " + args[1] + " " + args[2] + " " + val )
	
	// VERY temp and insecure
	SetPersistentLoadoutValue( player, args[0], args[1].tointeger(), args[2], val )
	
	print( args[ 0 ] )
	if ( args[0] == "pilot" )
		SetPlayerLoadoutDirty( player ) 
	
	UnlockAchievement( player, achievements.CUSTOMIZE_LOADOUT )
	
	return true
}

bool function ClientCommandCallback_SwapSecondaryAndWeapon3PersistentLoadoutData( entity player, array<string> args )
{
	if ( args.len() != 1 )
		return true
		
	print( "SwapSecondaryAndWeapon3PersistentLoadoutData " + args[0] )
	
	// get loadout
	int index = args[0].tointeger()
	PilotLoadoutDef loadout = GetPilotLoadoutFromPersistentData( player, index )

	// swap loadouts
	// is this a good way of doing it? idk i think this is the best way of doing it
	// can't use validation because when you swap, you'll have a secondary/weapon3 in 2 slots at once at one point, which fails validation
	SetPlayerPersistentVarWithoutValidation( player, "pilot", index, "secondary", loadout.weapon3 )
	SetPlayerPersistentVarWithoutValidation( player, "pilot", index, "secondaryMod1", loadout.weapon3Mod1 )
	SetPlayerPersistentVarWithoutValidation( player, "pilot", index, "secondaryMod2", loadout.weapon3Mod2 )
	SetPlayerPersistentVarWithoutValidation( player, "pilot", index, "secondaryMod3", loadout.weapon3Mod3 )
	SetPlayerPersistentVarWithoutValidation( player, "pilot", index, "secondarySkinIndex", loadout.weapon3SkinIndex.tostring() )
	SetPlayerPersistentVarWithoutValidation( player, "pilot", index, "secondaryCamoIndex", loadout.weapon3CamoIndex.tostring() )
	
	SetPlayerPersistentVarWithoutValidation( player, "pilot", index, "weapon3", loadout.secondary )
	SetPlayerPersistentVarWithoutValidation( player, "pilot", index, "weapon3Mod1", loadout.secondaryMod1 )
	SetPlayerPersistentVarWithoutValidation( player, "pilot", index, "weapon3Mod2", loadout.secondaryMod2 )
	SetPlayerPersistentVarWithoutValidation( player, "pilot", index, "weapon3Mod3", loadout.secondaryMod3 )
	SetPlayerPersistentVarWithoutValidation( player, "pilot", index, "weapon3SkinIndex", loadout.secondarySkinIndex.tostring() )
	SetPlayerPersistentVarWithoutValidation( player, "pilot", index, "weapon3CamoIndex", loadout.secondaryCamoIndex.tostring() )
		
	SetPlayerLoadoutDirty( player )
	
	return true
}

bool function ClientCommandCallback_SetBurnCardPersistenceSlot( entity player, array<string> args )
{
	if ( args.len() != 1 || GetGameState() >= eGameState.Playing )
		return true
	
	print( player + " SetBurnCardPersistenceSlot " + args[0] )
	
	if ( IsRefValidAndOfType( args[0], eItemTypes.BURN_METER_REWARD ) )
		player.SetPersistentVar( "burnmeterSlot", BurnReward_GetByRef( args[0] ).id )
	else
		print( player + " invalid ref " + args[0] )
	
	return true
}

// lobby clientcommands
bool function ClientCommandCallback_SetCallsignIcon( entity player, array<string> args )
{
	print( player + " SetCallsignIcon " + args[0] )

	if ( IsRefValidAndOfType( args[0], eItemTypes.CALLSIGN_ICON ) )
		PlayerCallsignIcon_SetActiveByRef( player, args[0] )
	else
		print( player + " invalid ref " + args[0] )
	
	return true
}

bool function ClientCommandCallback_SetCallsignCard( entity player, array<string> args )
{
	print( player + " SetCallsignIcon " + args[0] )

	if ( IsRefValidAndOfType( args[0], eItemTypes.CALLING_CARD ) )
		PlayerCallingCard_SetActiveByRef( player, args[0] )
	else
		print( player + " invalid ref " + args[0] )
	
	return true
}

bool function ClientCommandCallback_SetFactionChoicePersistenceSlot( entity player, array<string> args )
{
	print( player + " SetFactionChoicePersistenceSlot " + args[0] )

	if ( IsRefValidAndOfType( args[0], eItemTypes.FACTION ) )
		player.SetPersistentVar( "factionChoice", args[0] ) // no function for this so gotta set directly lol
	
	return true
}

bool function ClientCommandCallback_LoadoutMenuClosed( entity player, array<string> args )
{
	TryGivePilotLoadoutForGracePeriod( player )
	return true
}

bool function ClientCommandCallback_InGameMPMenuClosed( entity player, array<string> args )
{
	TryGivePilotLoadoutForGracePeriod( player )
	return true
}

bool function IsRefValidAndOfType( string ref, int itemType )
{
	return IsRefValid( ref ) && GetItemType( ref ) == itemType 
}

void function SetPlayerLoadoutDirty( entity player )
{
	if ( !IsLobby() )
		player.s.loadoutDirty = true
}

void function TryGivePilotLoadoutForGracePeriod( entity player )
{
	if ( !IsLobby() && IsAlive( player ) && player.s.loadoutDirty && !player.IsTitan() && !player.ContextAction_IsActive() )
	{
		player.s.loadoutDirty = false
	
		// for intros
		float respawnTimeReal
		if ( GetGameState() == eGameState.Playing && Time() - expect float( GetServerVar( "gameStateChangeTime" ) ) <= CLASS_CHANGE_GRACE_PERIOD )
			respawnTimeReal = expect float( GetServerVar( "gameStateChangeTime" ) )
		else
			respawnTimeReal = expect float( player.s.respawnTime )
		
		if ( ( ( Time() - respawnTimeReal <= CLASS_CHANGE_GRACE_PERIOD || GetGameState() < eGameState.Playing ) && file.loadoutGracePeriodEnabled ) || player.p.usingLoadoutCrate )
		{
			if ( !Loadouts_CanGivePilotLoadout( player ) && player.GetParent() != null && ( HasCinematicFlag( player, CE_FLAG_INTRO ) || HasCinematicFlag( player, CE_FLAG_CLASSIC_MP_SPAWNING ) || HasCinematicFlag( player, CE_FLAG_WAVE_SPAWNING ) ) )
				thread GiveLoadoutWhenIntroOver( player )
			else
				Loadouts_TryGivePilotLoadout( player )
			
			player.p.usingLoadoutCrate = false
		}
		else
			SendHudMessage( player, "#LOADOUT_CHANGE_NEXT_BOTH", -1, 0.4, 255, 255, 255, 255, 0.15, 3.0, 0.5 ) // like 90% sure this is innacurate lol
	}
}

void function GiveLoadoutWhenIntroOver( entity player )
{
	player.EndSignal( "OnDestroy" )
	player.EndSignal( "OnDeath" )
	
	while ( player.GetParent() != null && ( HasCinematicFlag( player, CE_FLAG_INTRO ) || HasCinematicFlag( player, CE_FLAG_CLASSIC_MP_SPAWNING ) || HasCinematicFlag( player, CE_FLAG_WAVE_SPAWNING ) ) )
		WaitFrame()
	
	Loadouts_TryGivePilotLoadout( player )
}