aboutsummaryrefslogtreecommitdiff
path: root/Northstar.CustomServers/scripts/vscripts/ai/_titan_npc_behavior.gnut
blob: 347cb64413055bc533abfc4d23ecddc0d06f46bd (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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
untyped

global function TitanNpcBehavior_Init

global function TitanNPC_Think
global function TitanNPC_WaitForBubbleShield_StartAutoTitanBehavior
global function TitanStandUp
global function TitanKneel
global function GetBubbleShieldDuration
global function ShowMainTitanWeapons

global function ChangedStance

global function TitanWaitsToChangeStance
global function ShouldBecomeAutoTitan

function TitanNpcBehavior_Init()
{
	FlagInit( "DisableTitanKneelingEmbark" )

	RegisterSignal( "TitanStopsThinking" )
	RegisterSignal( "RodeoRiderChanged" )

	if ( IsMultiplayer() )
	{
		AddCallback_OnTitanBecomesPilot( OnClassChangeBecomePilot )
		AddCallback_OnPilotBecomesTitan( OnClassChangeBecomeTitan )
	}
}

void function OnClassChangeBecomePilot( entity player, entity titan )
{
	entity soul = titan.GetTitanSoul()
	if ( !SoulHasPassive( soul, ePassives.PAS_ENHANCED_TITAN_AI ) )
	{
		entity ordnanceWeapon = titan.GetOffhandWeapon( OFFHAND_ORDNANCE )
		if ( IsValid( ordnanceWeapon ) )
			ordnanceWeapon.AllowUse( false )

		entity centerWeapon = titan.GetOffhandWeapon( OFFHAND_TITAN_CENTER )
		if ( IsValid( centerWeapon ) )
			centerWeapon.AllowUse( false )
	}
}

void function OnClassChangeBecomeTitan( entity player, entity titan )
{
	entity soul = player.GetTitanSoul()

	entity ordnanceWeapon = player.GetOffhandWeapon( OFFHAND_ORDNANCE )
	if ( IsValid( ordnanceWeapon ) )
		ordnanceWeapon.AllowUse( true )

	entity centerWeapon = player.GetOffhandWeapon( OFFHAND_TITAN_CENTER )
	if ( IsValid( centerWeapon ) )
		centerWeapon.AllowUse( true )
}

float function GetBubbleShieldDuration( entity player )
{
	if ( PlayerHasPassive( player, ePassives.PAS_LONGER_BUBBLE ) )
		return EMBARK_TIMEOUT + 10.0
	else
		return EMBARK_TIMEOUT

	unreachable
}

void function TitanNPC_WaitForBubbleShield_StartAutoTitanBehavior( entity titan )
{
	Assert( IsAlive( titan ) )

	titan.Signal( "TitanStopsThinking" )
	titan.EndSignal( "OnDeath" )
	titan.EndSignal( "TitanStopsThinking" )
	titan.EndSignal( "ContextAction_SetBusy" )

	entity bossPlayer = titan.GetBossPlayer()
	if ( !bossPlayer )
		return

	OnThreadEnd(
		function () : ( titan )
		{
			if ( IsAlive( titan ) )
			{
				titan.SetNoTarget( false )
				thread TitanNPC_Think( titan )
			}
		}
	)

	titan.EndSignal( "ChangedTitanMode" )

	float timeout
	if ( SoulHasPassive( titan.GetTitanSoul(), ePassives.PAS_BUBBLESHIELD ) )
	{
		entity player = titan.GetBossPlayer()
		timeout = GetBubbleShieldDuration( player )
	}
	else
	{
		timeout = 0
	}

	wait timeout
}

function TitanNPC_Think( entity titan )
{
	entity soul = titan.GetTitanSoul()

	// JFS - Shouldn't have to check for presence of soul.
	// The real fix for next game would be to make sure no other script can run between transferring away a titan's soul and destroying the titan.
	// This particular bug occurred if TitanNPC_WaitForBubbleShield_StartAutoTitanBehavior() was called before soul transferred from npc to player,
	// in which case the soul transfer killed the thread via Signal( "TitanStopsThinking" ), which causes the OnThreadEnd() to run TitanNPC_Think().
	if ( !IsValid( soul ) )
		return;

	if ( soul.capturable || !ShouldBecomeAutoTitan( titan ) )
	{
		// capturable titan just kneels
		if ( soul.GetStance() > STANCE_KNEELING )
			thread TitanKneel( titan )
		return
	}

	Assert( IsAlive( titan ) )

	if ( !TitanCanStand( titan ) )// sets the var
	{
		// try to put the titan on the navmesh
		vector ornull clampedPos = NavMesh_ClampPointForAIWithExtents( titan.GetOrigin(), titan, < 100, 100, 100 > )
		if ( clampedPos != null )
		{
			expect vector( clampedPos )
			titan.SetOrigin( clampedPos )
			TitanCanStand( titan )
		}
	}

	if ( !titan.GetBossPlayer() )
	{
		titan.Signal( "TitanStopsThinking" )
		return
	}

	if ( "disableAutoTitanConversation" in titan.s ) //At this point the Titan has stood up and is ready to talk
		delete titan.s.disableAutoTitanConversation

	titan.EndSignal( "TitanStopsThinking" )
	titan.EndSignal( "OnDeath" )
	titan.EndSignal( "player_embarks_titan" )

	// kneel in certain circumstances
	for ( ;; )
	{
		if ( !ChangedStance( titan ) )
			waitthread TitanWaitsToChangeStance( titan )
	}
}

bool function ChangedStance( entity titan )
{
	if ( GetEmbarkDisembarkPlayer( titan ) )
		return false

	local soul = titan.GetTitanSoul()

	// in a scripted sequence?
	if ( IsValid( titan.GetParent() ) )
		return false

	if ( soul.GetStance() > STANCE_KNEELING )
	{
		if ( TitanShouldKneel( titan ) )
		{
			//waitthread PlayAnimGravity( titan, "at_MP_stand2knee_straight" )
			waitthread KneelToShowRider( titan )
			thread PlayAnim( titan, "at_MP_embark_idle_blended" )
			SetStanceKneel( soul )
			return true
		}
	}
	else
	{
		if ( !TitanShouldKneel( titan ) && TitanCanStand( titan ) )
		{
			waitthread TitanStandUp( titan )
			return true
		}

		if ( soul.GetStance() == STANCE_KNEEL )
		{
			thread PlayAnim( titan, "at_MP_embark_idle_blended" )
		}
	}

	return false
}

function TitanShouldKneel( entity titan )
{
	local soul = titan.GetTitanSoul()

	if ( soul.capturable )
		return true

	//if( HasEnemyRodeoRiders( titan ) )
	//	return true
	if ( !TitanCanStand( titan ) )
		return false

	if ( !ShouldBecomeAutoTitan( titan ) )
		return true

	return false
}

function TitanWaitsToChangeStance( titan )
{
	local soul = titan.GetTitanSoul()
	soul.EndSignal( "RodeoRiderChanged" )

	titan.EndSignal( "OnAnimationInterrupted" )
	titan.EndSignal( "OnAnimationDone" )

	WaitForever()
}

function TitanStandUp( titan )
{
	local soul = titan.GetTitanSoul()
	// stand up
	titan.s.standQueued = false
	ShowMainTitanWeapons( titan )
	titan.Anim_Stop()
	waitthread PlayAnimGravity( titan, "at_hotdrop_quickstand" )
	Assert( soul == titan.GetTitanSoul() )
	SetStanceStand( soul )
}


void function TitanKneel( entity titan )
{
	titan.EndSignal( "TitanStopsThinking" )
	titan.EndSignal( "OnDeath" )
	Assert( IsAlive( titan ) )
	local soul = titan.GetTitanSoul()

	waitthread KneelToShowRider( titan )

	thread PlayAnim( titan, "at_MP_embark_idle_blended" )
	SetStanceKneel( soul )
}


/*
function TitanWaittillShouldStand( entity titan )
{
	//Don't wait if player is dead - titan should just stand up immediately
	local player = titan.GetBossPlayer()
	if ( !IsAlive( player ) )
		return

	player.EndSignal( "OnDeath" )

	for ( ;; )
	{
		if ( TitanCanStand( titan ) )
			break

		wait 5
	}
	if ( titan.s.standQueued )
		return

	titan.WaitSignal( "titanStand" )
}
*/

void function KneelToShowRider( entity titan )
{
	entity soul = titan.GetTitanSoul()
	entity player = soul.GetBossPlayer()
	local animation
	local yawDif

	//if ( IsAlive( player ) )
	//{
	//	local table = GetFrontRightDots( titan, player )
    //
	//	local dotForward = Table.dotForward
	//	local dotRight = Table.dotRight
    //
	////	DebugDrawLine( titanOrg, titanOrg + titan.GetForwardVector() * 200, 255, 0, 0, true, 5 )
	////	DebugDrawLine( titanOrg, titanOrg + vecToEnt * 200, 0, 255, 0, true, 5 )
    //
	//	if ( dotForward > 0.88 )
	//	{
	//		animation = "at_MP_stand2knee_L90"
	//		yawDif = 0
	//	}
	//	else
	//	if ( dotForward < -0.88 )
	//	{
	//		animation = "at_MP_stand2knee_R90"
	//		yawDif = 180
	//	}
	//	else
	//	if ( dotRight > 0 )
	//	{
	//		animation = "at_MP_stand2knee_straight"
	//		yawDif = 90
	//	}
	//	else
	//	{
	//		animation = "at_MP_stand2knee_180"
	//		yawDif = -90
	//	}
	//}
	//else
	{
		animation = "at_MP_stand2knee_straight"
		yawDif = 0
	}

	thread HideOgreMainWeaponFromEnemies( titan )

	if ( !IsAlive( player ) )
	{
		waitthread PlayAnimGravity( titan, animation )
		return
	}

	local titanOrg = titan.GetOrigin()
	local playerOrg = player.GetOrigin()

	/*
	local vec = playerOrg - titanOrg
	vec.z = 0

	local angles = VectorToAngles( vec )

	angles.y += yawDif
	*/

	local angles = titan.GetAngles()

	titan.Anim_ScriptedPlayWithRefPoint( animation, titanOrg, angles, 0.5 )
	titan.Anim_EnablePlanting()

	WaittillAnimDone( titan )
}

function HideOgreMainWeaponFromEnemies( titan )
{
	expect entity( titan )

	titan.EndSignal( "OnDeath" )
	titan.EndSignal( "OnDestroy" )

	wait 1.0

	entity soul = titan.GetTitanSoul()

	Assert( IsValid( soul ) )

	local titanSubClass = GetSoulTitanSubClass( soul )
	if ( titanSubClass == "ogre" )
	{
		if ( IsValid( GetEnemyRodeoPilot( titan ) ) )
			HideMainWeaponsFromEnemies( titan )
	}
}

function HideMainWeaponsFromEnemies( titan )
{
	local weapons = titan.GetMainWeapons()
	foreach ( weapon in weapons )
		weapon.kv.visibilityFlags = ENTITY_VISIBLE_TO_FRIENDLY
}

function ShowMainTitanWeapons( titan )
{
	local weapons = titan.GetMainWeapons()
	foreach ( weapon in weapons )
	{
		weapon.kv.visibilityFlags = ENTITY_VISIBLE_TO_EVERYONE
	}
}

bool function ShouldBecomeAutoTitan( entity titan )
{
	entity soul = titan.GetTitanSoul()

	if ( soul != null )
	{
		if ( SoulHasPassive( soul, ePassives.PAS_ENHANCED_TITAN_AI ) )
			return true
	}

	return ( !PROTO_AutoTitansDisabled() )
}