aboutsummaryrefslogtreecommitdiff
path: root/Northstar.CustomServers/mod/scripts/vscripts/mp/_loadout_crate.nut
blob: d987c774c331d1d55094123f5003704469ed476b (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
untyped

global function LoadoutCrate_Init

const LOADOUT_CRATE_MODEL				= $"models/containers/pelican_case_ammobox.mdl"
global function AddLoadoutCrate
global function DestroyAllLoadoutCrates

function LoadoutCrate_Init()
{
	level.loadoutCrateManagedEntArrayID <- CreateScriptManagedEntArray()
	PrecacheModel( LOADOUT_CRATE_MODEL )

	AddSpawnCallback( "prop_dynamic", LoadoutCreatePrePlaced )
}

function AddLoadoutCrate( team, vector origin, vector angles, bool showOnMinimap = true, entity crate = null )
{
	expect int( team )

	local crateCount = GetScriptManagedEntArray( level.loadoutCrateManagedEntArrayID ).len()
	Assert( crateCount < MAX_LOADOUT_CRATE_COUNT, "Can't have more than " + MAX_LOADOUT_CRATE_COUNT + " Loadout Crates" )

	angles += Vector( 0, -90, 0 )

	if ( !IsValid( crate ) )
	{
		crate = CreatePropScript( LOADOUT_CRATE_MODEL, origin, angles, 6 )
		SetTargetName( crate, "loadoutCrate" )
	}

	SetTeam( crate, team )
	crate.SetUsable()
	if ( team == TEAM_MILITIA || team == TEAM_IMC )
		crate.SetUsableByGroup( "friendlies pilot" )
	else
		crate.SetUsableByGroup( "pilot" )

	crate.SetUsePrompts( "#LOADOUT_CRATE_HOLD_USE", "#LOADOUT_CRATE_PRESS_USE" )
	crate.SetForceVisibleInPhaseShift( true )

	if ( showOnMinimap )
	{
		#if R1_VGUI_MINIMAP
			crate.Minimap_SetDefaultMaterial( $"vgui/hud/coop/coop_ammo_locker_icon" )
		#endif
		crate.Minimap_SetObjectScale( MINIMAP_LOADOUT_CRATE_SCALE )
		crate.Minimap_SetAlignUpright( true )
		crate.Minimap_AlwaysShow( TEAM_IMC, null )
		crate.Minimap_AlwaysShow( TEAM_MILITIA, null )
		crate.Minimap_SetHeightTracking( true )
		crate.Minimap_SetZOrder( MINIMAP_Z_OBJECT )
		crate.Minimap_SetCustomState( eMinimapObject_prop_script.FD_LOADOUT_CHEST )
	}

	AddToScriptManagedEntArray( level.loadoutCrateManagedEntArrayID, crate )

	//thread LoadoutCrateMarkerThink( "LoadoutCrateMarker" + string( crateCount ), crate )
	thread LoadoutCrateThink( crate )
	thread LoadoutCrateRestockAmmoThink( crate )

	Highlight_SetNeutralHighlight( crate, "interact_object_los" )
}

void function LoadoutCreatePrePlaced( entity ent )
{
	if ( ent.GetTargetName().find( "loot_crate" ) == 0 )
	{
		ent.Destroy()
		return
	}

	if ( ent.GetTargetName().find( "loadout_crate" ) != 0 )
		return

	if ( IsSingleplayer() )
		return

	vector angles = ent.GetAngles() + Vector( 0, 90, 0 )
	AddLoadoutCrate( TEAM_BOTH, ent.GetOrigin(), angles, false, ent )
}

function LoadoutCrateMarkerThink( marker, crate )
{
	crate.EndSignal( "OnDestroy" )
	crate.EndSignal( "OnDeath" )

	OnThreadEnd(
		function() : ( marker )
		{
			ClearMarker( marker )
		}
	)

	while ( 1 )
	{
		if ( GetGameState() <= eGameState.Prematch )
			ClearMarker( marker )
		else
			SetMarker( marker, crate )

		svGlobal.levelEnt.WaitSignal( "GameStateChanged" )
	}
}

function LoadoutCrateThink( crate )
{
	crate.EndSignal( "OnDestroy" )
	while ( true )
	{
		var player = crate.WaitSignal( "OnPlayerUse" ).player

		if ( player.IsPlayer() )
		{
			thread UsingLoadoutCrate( crate, player )
			wait 1	// debounce on using the crate to minimize the risk of using it twice before the menu opens.
		}
	}
}

function LoadoutCrateRestockAmmoThink( crate )
{
	crate.EndSignal( "OnDestroy" )
	local distSqr
	local crateOrigin = crate.GetOrigin()
	local triggerDistSqr = 96 * 96
	local resetDistSqr = 384 * 384

	while ( true )
	{
		wait 1 // check every second
		array<entity> playerArray = GetPlayerArray_Alive()
		foreach( player in playerArray )
		{
			if ( player.IsTitan() )
				continue

			if ( player.ContextAction_IsBusy() )
				continue

			distSqr = DistanceSqr( crateOrigin, player.GetOrigin() )
			if ( distSqr <= triggerDistSqr && player.s.restockAmmoTime < Time() )
			{
				if ( TraceLineSimple( player.EyePosition(), crate.GetOrigin() + Vector( 0.0, 0.0, 24.0 ), crate ) == 1.0 )
				{
					player.s.restockAmmoCrate = crate
					player.s.restockAmmoTime = Time() + 10 // debounce time before you can get new ammo again if you stay next to the crate.
					//MessageToPlayer( player, eEventNotifications.CoopAmmoRefilled, null, null )
					RestockPlayerAmmo( player )
				}
			}

			if ( distSqr > resetDistSqr && player.s.restockAmmoTime > 0 && player.s.restockAmmoCrate == crate )
			{
				player.s.restockAmmoCrate = null
				player.s.restockAmmoTime = 0
			}
		}
	}
}

function UsingLoadoutCrate( crate, player )
{
	expect entity( player )

	player.p.usingLoadoutCrate = true
	player.s.usedLoadoutCrate = true
	EmitSoundOnEntityOnlyToPlayer( player, player, "Coop_AmmoBox_Open" )
	Remote_CallFunction_UI( player, "ServerCallback_OpenPilotLoadoutMenu" )
}

// should be called if we enter an epilogue ... maybe?
function DestroyAllLoadoutCrates()
{
	local crateArray = GetScriptManagedEntArray( level.loadoutCrateManagedEntArrayID )
	foreach( crate in crateArray )
		crate.Destroy()

	//dissolve didn't work
	//Dissolve( ENTITY_DISSOLVE_CHAR, Vector( 0, 0, 0 ), 0 )
	//ENTITY_DISSOLVE_CORE
	//ENTITY_DISSOLVE_NORMAL
}