aboutsummaryrefslogtreecommitdiff
path: root/Northstar.CustomServers/mod/scripts/vscripts/ai/_droppod_fireteam.gnut
blob: b93631ac83703cbe23d889c31de26bc4daa843f2 (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
global function DropPodFireteam_Init

global function InitFireteamDropPod
global function ActivateFireteamDropPod
global function DropPodActiveThink

global function CreateDropPodDoor

const DP_ARM_MODEL = $"models/vehicle/droppod_fireteam/droppod_fireteam_arm.mdl"
const DP_DOOR_MODEL = $"models/vehicle/droppod_fireteam/droppod_fireteam_door.mdl"

global enum eDropPodFlag
{
	DISSOLVE_AFTER_DISEMBARKS =		(1<<0)
}

struct DroppodStruct
{
	entity door
	bool openDoor = false
	int numGuys = 0
	int flags = 0
}

struct
{
	table<entity, DroppodStruct> droppodTable
}
file

void function DropPodFireteam_Init()
{
	RegisterSignal( "OpenDoor" )

	PrecacheModel( DP_DOOR_MODEL )
	PrecacheModel( DP_ARM_MODEL )
}

void function InitFireteamDropPod( entity pod, int flags = 0 )
{
	pod.NotSolid()

	DroppodStruct droppodData
	droppodData.flags = flags
	droppodData.door = CreateDropPodDoor( pod )
	file.droppodTable[ pod ] <- droppodData

	pod.Anim_Play( "idle" )
}

void function ActivateFireteamDropPod( entity pod, array<entity> guys )
{
	DroppodStruct droppodData = file.droppodTable[ pod ]
	droppodData.openDoor = true
	pod.Signal( "OpenDoor" )

	if ( guys.len() >= 1 )
	{
		SetAnim( guys[0], "drop_pod_exit_anim", "pt_dp_exit_a" )
		SetAnim( guys[0], "drop_pod_idle_anim", "pt_dp_idle_a" )
	}

	if ( guys.len() >= 2 )
	{
		SetAnim( guys[1], "drop_pod_exit_anim", "pt_dp_exit_b" )
		SetAnim( guys[1], "drop_pod_idle_anim", "pt_dp_idle_b" )
	}

	if ( guys.len() >= 3 )
	{
		SetAnim( guys[2], "drop_pod_exit_anim", "pt_dp_exit_c" )
		SetAnim( guys[2], "drop_pod_idle_anim", "pt_dp_idle_c" )
	}

	if ( guys.len() >= 4 )
	{
		SetAnim( guys[3], "drop_pod_exit_anim", "pt_dp_exit_d" )
		SetAnim( guys[3], "drop_pod_idle_anim", "pt_dp_idle_d" )
	}

	foreach ( guy in guys )
	{
		if ( IsAlive( guy ) )
		{
			guy.MakeVisible()
			entity weapon = guy.GetActiveWeapon()
			if ( IsValid( weapon ) )
				weapon.MakeVisible()

			thread GuyHangsInPod( guy, pod )
		}
	}

	thread DropPodActiveThink( pod )
}

void function DropPodActiveThink( entity pod )
{
	DroppodStruct droppodData = file.droppodTable[ pod ]

	OnThreadEnd(
		function() : ( pod )
		{
			DroppodStruct droppodData = file.droppodTable[pod]
			if ( droppodData.flags & eDropPodFlag.DISSOLVE_AFTER_DISEMBARKS )
				CleanupFireteamPod( pod )
			else
				delaythread( 10 ) CleanupFireteamPod( pod )
		}
	)

	pod.EndSignal( "OnDestroy" )

	if ( DropPodDoorInGround( pod ) )
		droppodData.door.Destroy()
	else
		DropPodOpenDoor( pod, droppodData.door )

	while ( droppodData.numGuys )
		WaitFrame()
}

bool function DropPodDoorInGround( entity pod )
{
	string attachment = "hatch"
	int attachIndex = pod.LookupAttachment( attachment )
	vector end = pod.GetAttachmentOrigin( attachIndex )

	string originAttachment = "origin"
	int originAttachIndex = pod.LookupAttachment( originAttachment )
	vector start = pod.GetAttachmentOrigin( originAttachIndex )

	TraceResults result = TraceLine( start, end, pod, TRACE_MASK_SOLID, TRACE_COLLISION_GROUP_NONE )

	return result.fraction < 1.0
}

void function CleanupFireteamPod( entity pod )
{
	DroppodStruct droppodData = file.droppodTable[ pod ]

	if ( !IsValid( pod ) )
		return

	if ( IsValid( droppodData.door ) )
		droppodData.door.Dissolve( ENTITY_DISSOLVE_CORE, Vector( 0, 0, 0 ), 500 )

	EmitSoundAtPosition( TEAM_UNASSIGNED, pod.GetOrigin(), "droppod_dissolve" )

	delete file.droppodTable[ pod ]

	pod.NotSolid()
	foreach( ent in pod.e.attachedEnts )
	{
		ent.NotSolid()
	}
	pod.Dissolve( ENTITY_DISSOLVE_CORE, Vector( 0, 0, 0 ), 500 )
}

entity function CreateDropPodDoor( entity pod )
{
	string attachment = "hatch"
	int attachIndex = pod.LookupAttachment( attachment )
	vector origin = pod.GetAttachmentOrigin( attachIndex )
	vector angles = pod.GetAttachmentAngles( attachIndex )

	entity prop_physics = CreateEntity( "prop_physics" )
	SetTargetName( prop_physics, "door" + UniqueString() )
	prop_physics.SetValueForModelKey( DP_DOOR_MODEL )
	// Start Asleep
	// Debris - Don't collide with the player or other debris
	// Generate output on +USE
	prop_physics.kv.spawnflags = 261 // non solid for now
	prop_physics.kv.fadedist = -1
	prop_physics.kv.physdamagescale = 0.1
	prop_physics.kv.inertiaScale = 1.0
	prop_physics.kv.renderamt = 0
	prop_physics.kv.rendercolor = "255 255 255"

	DispatchSpawn( prop_physics )

	prop_physics.SetOrigin( origin )
	prop_physics.SetAngles( angles )
	prop_physics.SetParent( pod, "HATCH", false )
	prop_physics.MarkAsNonMovingAttachment()

	return prop_physics
}

void function DropPodOpenDoor( entity pod, entity door )
{
	door.ClearParent()
	door.SetVelocity( door.GetForwardVector() * 500 )
	EmitSoundOnEntity( pod, "droppod_door_open" )
}

void function GuyHangsInPod( entity guy, entity pod )
{
	DroppodStruct droppodData = file.droppodTable[ pod ]

	guy.EndSignal( "OnDeath" )
	guy.EndSignal( "OnDestroy" )

	OnThreadEnd(
		function() : ( droppodData )
		{
			droppodData.numGuys--
		}
	)

	droppodData.numGuys++

	string idleAnim
	string exitAnim

	if ( !droppodData.openDoor )
	{
		guy.SetEfficientMode( true )

		guy.SetParent( pod, "ATTACH", false )

		idleAnim = expect string( GetAnim( guy, "drop_pod_idle_anim" ) )
		if ( guy.LookupSequence( idleAnim ) != -1 )
			guy.Anim_ScriptedPlay( idleAnim )

		pod.WaitSignal( "OpenDoor" )

		//wait POST_TURRET_DELAY

		guy.SetEfficientMode( false )
	}


	guy.SetParent( pod, "ATTACH", false )

	exitAnim =  expect string ( GetAnim( guy, "drop_pod_exit_anim" ) )
	bool exitAnimExists = guy.LookupSequence( exitAnim ) != -1
	if ( exitAnimExists )
		guy.Anim_ScriptedPlay( exitAnim )

	guy.ClearParent()

	if ( exitAnimExists )
		WaittillAnimDone( guy )
	guy.Signal( "npc_deployed" )
}