-
Notifications
You must be signed in to change notification settings - Fork 0
/
e_plc_npccontroller_hb.NSS
440 lines (300 loc) · 16.2 KB
/
e_plc_npccontroller_hb.NSS
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//:://////////////////////////////////////////////////
//:: e_plc_npccontroller_hb
//:://////////////////////////////////////////////////
/*
Description: custom onHeartBeat for Khalidine NPC Controller System
*/
//:://////////////////////////////////////////////////
// 29/12/07 Creation by Alban : hostile creatures management
//:://////////////////////////////////////////////////
/*
*/
//:://////////////////////////////////////////////////
#include "inc_ai_hostile"
void main () {
// We want to get / set a bunch of local variables from controller
object oController = OBJECT_SELF;
int nRound = GetLocalInt( oController, CONTROLLER_TEAM_ROUND_COUNT) + 1;
float fFearFactor = GetLocalFloat( oController, CONTROLLER_TEAM_FEAR_FACTOR );
int nHasBoss = GetLocalInt( oController, CONTROLLER_TEAM_HAS_BOSS );
int nTeamHPCurrent = GetLocalInt( oController, CONTROLLER_TEAM_HP_CURRENT );
int nTeamHPTotal = GetLocalInt( oController, CONTROLLER_TEAM_HP_TOTAL );
// Bug fix : nTeamHPTotal -must- not be null to be used as diviser
if( 0 == nTeamHPTotal ) nTeamHPTotal = 1;
int nPercentHPRemainingTeam = FloatToInt( 100.0 * IntToFloat(nTeamHPCurrent) / IntToFloat(nTeamHPTotal) ) ;
int nMoraleCurrent = GetLocalInt( oController, CONTROLLER_TEAM_MORALE_CURRENT );
int nTeamState = GetLocalInt( oController, CONTROLLER_TEAM_STATE );
object oTeamTarget = GetLocalObject( oController, CONTROLLER_TEAM_TARGET_CURRENT );
float fRetreatMultiplierMalus = 1.0;
string sTeamArrayName = GetTag( oController );
int nTeamArrayCount = ArrayGetCount( sTeamArrayName );
LocalDebug("TEAM HP : " + IntToString( nTeamHPCurrent ) +" / " + IntToString( nTeamHPTotal ) +" => " + IntToString( nPercentHPRemainingTeam ) +"%" );
LocalDebug("TEAM Morale : " + IntToString( nMoraleCurrent ) );
int nCreatureFlags, nCreatureHierarchicFlag, nCreatureHPCurrent, nCreatureHPMax, nPercentHPRemainingCreature, nCreatureState, nCreatureNextState, nCustomCreatureAILevel;
object oCreatureTarget, oTarget, oPerceived, oLastValidTarget;
int bHasNoValidTarget, nCountNoTargets;
LocalDebug("**************************************** ");
LocalDebug("****** Controller heartbeat on Round : " + IntToString( nRound ) + " ******");
LocalDebug("****** Array count : " + IntToString( nTeamArrayCount ) +" ******");
LocalDebug("**************************************** ");
// Let's destroy the array and controller if array is empty
if( nTeamArrayCount == 0 ) {
ArrayDelete( sTeamArrayName );
DestroyObject( oController );
LocalDebug("****** Destroying array : " + sTeamArrayName );
return;
}
// Let's change the team state if necessary
// TODO nTeamState = ChangeTeamState( nTeamState );
if( nTeamState == ACTION_TEAM_FIRST_SIGHT ) {
// pass to combat
// nTeamState = ACTION_TEAM_COMBAT;
SetLocalInt( oController, CONTROLLER_TEAM_STATE, ACTION_TEAM_COMBAT);
//LocalDebug("****** Team will go for fight on next round! ");
}
// :: GO TROUGH CREATURES IN TEAM ARRAY
// :: =====================================
int nNthCreature;
for( nNthCreature = --nTeamArrayCount; nNthCreature >= 0 ; nNthCreature--) {
bHasNoValidTarget = 0;
oTarget = oTeamTarget; // Our default target is team target
object oCreature = ArrayGetObjectElement( sTeamArrayName, nNthCreature );
LocalDebug("------------------------------------------------------------------------ ");
LocalDebug("Controller parsing creature : " + GetResRef( oCreature )+" at index : "+IntToString(nNthCreature) , oCreature );
//LocalDebug("HP : " + IntToString( nCreatureHPCurrent ) +" / " + IntToString( nCreatureHPMax ) +" => " + IntToString( nPercentHPRemainingCreature )+"%" , oCreature );
if ( !GetIsObjectValid( oCreature ) || GetIsDead(oCreature) ){
// :: DEAD ENCOUNTERS
// :: =================
// We're /SO/ dead we get removed from team array
ArrayRemoveElement( sTeamArrayName, nNthCreature );
LocalDebug("****** Removing from array at index " +IntToString(nNthCreature)+", "+IntToString(nTeamArrayCount)+" Creature(s) left in array" );
continue;
}else{
// :: LIVING ENCOUNTERS
// :: ===================
// Let's get local vars on creature
nCreatureFlags = GetLocalInt ( oCreature, CREATURE_FLAGS );
nCreatureHierarchicFlag = GetCreatureHierarchicFlag( nCreatureFlags );
nCreatureHPCurrent = GetLocalInt( oCreature, CREATURE_HP_CURRENT);
nCreatureHPMax = GetLocalInt( oCreature, CREATURE_HP_MAX);
nPercentHPRemainingCreature = FloatToInt( 100.0 * IntToFloat(nCreatureHPCurrent) / IntToFloat(nCreatureHPMax) ) ;
nCreatureState = GetLocalInt( oCreature, CREATURE_STATE );
oCreatureTarget = GetLocalObject( oCreature, CREATURE_TARGET );
nCustomCreatureAILevel = GetLocalInt( oCreature, CREATURE_AI_LEVEL );
AssignCommand( oCreature, ClearAllActions( TRUE ) );
// :: ============================================================
// :: MORALE CHECKS
// :: ============================================================
// If we have the coward flag raised, stop here
// This is not coherent : the creature needs a personal target but receives so far team target
// If we were retreating last round, we have more chances to flee again : 10% x FearFactor
if( nCreatureState == ACTION_CREATURE_RETREAT )
fRetreatMultiplierMalus = 1.0 - ( MORALE_MALUS_FOR_RETREATING * fFearFactor );
// :: ============================================================
// :: COWARD ENCOUNTERS
// :: ============================================================
// Coward creatures get a complete different loop ( no xp for flee )
if( nCreatureFlags & AI_FLAG_WILD ){
LocalDebug(">>>>> I'm a coward ! " , oCreature );
int nFledRounds = GetLocalInt( oCreature, CREATURE_FLEE_COUNT );
// Is there still a valid PC around ?
object oNearestPC = GetNearestCreature( CREATURE_TYPE_PLAYER_CHAR, PLAYER_CHAR_IS_PC, oCreature );
LocalDebug(">>>>> Nearest PC : " + GetName(oNearestPC) , oCreature );
// Are we about to disappear because we fled for too long ?
if( nFledRounds >= MAX_FLEE_ROUNDS && GetDistanceBetween( oCreature, oNearestPC ) > DISTANCE_DISAPPEAR_FLEEING ) {
DestroyTeamCreature( oCreature );
}
// Fleeing for our life
else if ( GetDistanceBetween( oCreature, oNearestPC ) < DISTANCE_FLEE_RETREAT )
{
// The coward creatures have no "wake up" state, hence they need that
if( GetActionMode( oCreature, ACTION_MODE_DETECT ) )
SetActionMode( oCreature, ACTION_MODE_DETECT, FALSE );
// If we're not really fleeing for the moment, it won't increment counter
if( ActionFlee( oCreature, oCreatureTarget ) )
SetLocalInt( oCreature, CREATURE_FLEE_COUNT, GetLocalInt( oCreature, CREATURE_FLEE_COUNT )+1 );
LocalDebug(">>>>> I'm running out of here after " + IntToString( GetLocalInt( oCreature, CREATURE_FLEE_COUNT ) ) + " rounds of fleeing" , oCreature );
bHasNoValidTarget = !GetIsObjectValid( oNearestPC );
}
// Do nothing
else {
AssignCommand( oCreature, ActionRandomWalk() );
}
}
// We want to check if creature succeeds Morale check
else if(
GetCreatureMoraleRollResult ( fFearFactor, nMoraleCurrent, nPercentHPRemainingTeam, nPercentHPRemainingCreature, fRetreatMultiplierMalus )
) {
// :: CONFIDENT ENCOUNTERS
// :: =======================
// We want to switch actions depending on the team state
// :: ==================================================
switch( nTeamState ) {
// =============================================================
// WANDERING
// =============================================================
case ACTION_TEAM_WANDER :
LocalDebug("I'm wandering with my team." , oCreature );
// TODO : Maybe simulate more intelligent activities ?
AssignCommand( oCreature, ActionRandomWalk() );
// AssignCommand( oCreature, ActionMoveToObject( GetNearestObject( OBJECT_TYPE_ALL, oCreature, Random( nTeamArrayCount ) + 3 ), FALSE) );
// If a scout, run long range sighting and don't move
if( nCreatureFlags & AI_FLAG_SCOUT ){
LocalDebug("I'm a scout." , oCreature );
// Todo : this might go into a function
// eventually returning a spotted creature
int nNthScouted = 1;
int nSpotted = 1;
object oScoutSighted = GetNearestCreature(CREATURE_TYPE_PLAYER_CHAR, PLAYER_CHAR_IS_PC, oCreature, nNthScouted);
while( GetIsObjectValid( oScoutSighted ) ) {
LocalDebug("Enemy spotted." , oCreature );
// If we reached a PC out of distance, break
if( GetDistanceBetween( oCreature, oScoutSighted ) > DISTANCE_SCOUT_RANGE ) {
LocalDebug("Enemy too far." , oCreature );
nSpotted = 0;
}
// Does scout detect stealth creature
if( GetActionMode( oScoutSighted, ACTION_MODE_STEALTH ) ){
LocalDebug("Enemy stealthy." , oCreature );
if( GetIsSkillSuccessful( oCreature, SKILL_LISTEN, GetSkillRank( SKILL_MOVE_SILENTLY, oScoutSighted ) )
&& GetIsSkillSuccessful( oCreature, SKILL_SPOT, GetSkillRank( SKILL_HIDE, oScoutSighted ) ) )
oPerceived = oScoutSighted;
nSpotted = 0;
LocalDebug("Enemy not detected." , oCreature );
}
// We got a target
if( nSpotted )
{
LocalDebug("Enemy detected." , oCreature );
oPerceived = oScoutSighted;
oScoutSighted = OBJECT_INVALID;
}
nNthScouted++;
oScoutSighted = GetNearestCreature(CREATURE_TYPE_PLAYER_CHAR, PLAYER_CHAR_IS_PC, oCreature, nNthScouted);
}
} else
// We want to look if there's any enemy around and eventually activate team
oPerceived = GetNearestCreature(CREATURE_TYPE_PLAYER_CHAR, PLAYER_CHAR_IS_PC, oCreature, 1, CREATURE_TYPE_PERCEPTION, PERCEPTION_SEEN);
// If enemy spotted, time to wake team up
if( GetIsValidTarget( oPerceived ) ) {
oTeamTarget = oPerceived;
SetLocalObject( oController, CONTROLLER_TEAM_TARGET_CURRENT, oPerceived );
event eEvent = EventUserDefined(EVENT_HOSTILE_CREATURE_TEAM_WAKE);
SignalEvent( oController, eEvent);
nTeamState = ACTION_TEAM_FIRST_SIGHT; // This will avoid further actions while this round
LocalDebug(">>>>> I've spotted an enemy ! " + GetName( oPerceived ) , oCreature );
}
break;
// =============================================================
// FIGHTING
// =============================================================
case ACTION_TEAM_COMBAT:
LocalDebug(">>>>> I'm fighting with my team." , oCreature );
// :: ============================================================
// :: TARGETING
// :: ============================================================
// :: BOSS
// If we're a boss or a right hand and the boss is gone, our target will become the group's target
if( nCreatureHierarchicFlag == AI_FLAG_BOSS || ( nCreatureHierarchicFlag == AI_FLAG_RIGHTHAND && nHasBoss == 0 ) ) {
bHasNoValidTarget = !GetTarget( oCreature);
oTeamTarget = GetLocalObject( oCreature, CREATURE_TARGET );
SetLocalObject( oController, CONTROLLER_TEAM_TARGET_CURRENT, oTeamTarget );
LocalDebug(">>>>> I chose a target as leader : " + GetName( GetLocalObject( oCreature, CREATURE_TARGET) ), oCreature );
// :: STUPID CREATURE
// If creature too stupid to pick team target, pick its own
} else if ( GetHasNotUnderstoodOrders( nCustomCreatureAILevel, nCreatureHierarchicFlag ) ) {
LocalDebug(">>>>> I didn't undestand orders ! ", oCreature );
// Reattack last creature if valid, else select a new one and attack
if(GetIsValidTarget( oCreatureTarget ) )
oTarget = oCreatureTarget;
else {
bHasNoValidTarget = !GetTarget( oCreature);
oTarget = GetLocalObject( oCreature, CREATURE_TARGET);
}
// :: INVALID TEAM TARGET
// If Invalid team target, get another one
} else if ( !GetIsValidTarget( oTeamTarget ) ) {
LocalDebug(">>>>> Invalid Team Target when picking a target", oCreature );
bHasNoValidTarget = !GetTarget( oCreature);
oTarget = GetLocalObject( oCreature, CREATURE_TARGET);
LocalDebug(">>>>> I chose a target : " + GetName( oTarget ), oCreature );
// :: DEFAULT TEAM TARGET
} else {
SetLocalObject( oCreature, CREATURE_TARGET, oTarget );
LocalDebug(">>>>> I didn't choose a target when picking a target, heriting : " + GetName( oTeamTarget ), oCreature );
}
// At this point if we don't have a valid target we need to get the last valid one
if ( !GetIsValidTarget( oTarget ) ) {
if ( GetIsValidTarget( oLastValidTarget ) ) {
oTarget = oLastValidTarget;
SetLocalObject( oCreature, CREATURE_TARGET, oTarget );
LocalDebug(">>>>> Invalid Target when picking a target, acquiring oLastValidTarget : " + GetName( oLastValidTarget ), oCreature );
}else{
// We have no valid target !!!
bHasNoValidTarget = 1;
}
}else{
oLastValidTarget = oTarget;
}
// :: ============================================================
// :: ACTIONS
// :: ============================================================
if( bHasNoValidTarget ){
}
// We check if we have a special function
else if ( GetCreatureHasFunction( nCreatureFlags ) ) {
UseFunctionInCombat( oCreature, oTarget, nCreatureFlags );
LocalDebug(">>>>> I execute my team function ! ", oCreature );
// We want the creature to attack team target
} else {
AssignCommand( oCreature, ActionCustomAttack( oCreature, oTarget ) );
LocalDebug(">>>>> I attack my target ! ", oCreature );
}
break;
case ACTION_TEAM_RETREAT:
LocalDebug(">>>>> I'm retreating with my team." , oCreature );
break;
}
// We want the creature to flee
} else {
// :: ============================================================
// :: FLEEING ENCOUNTERS
// :: ============================================================
int nFledRounds = GetLocalInt( oCreature, CREATURE_FLEE_COUNT );
// Is there still a valid PC around ?
object oNearestPC = GetNearestPC( );
// Are we about to disappear because we fled for too long ?
if( nFledRounds >= MAX_FLEE_ROUNDS && GetDistanceBetween( oCreature, oNearestPC ) > DISTANCE_DISAPPEAR_FLEEING )
{
DestroyTeamCreature( oCreature );
}
// Fleeing for our life
else
{
// If we were stealthy...
if( GetActionMode( oCreature, ACTION_MODE_STEALTH ) )
SetActionMode( oCreature, ACTION_MODE_STEALTH, FALSE );
bHasNoValidTarget = !GetIsObjectValid( oNearestPC );
// If we're not really fleeing for the moment, it won't increment counter
if( ActionFlee( oCreature, oNearestPC ) )
SetLocalInt( oCreature, CREATURE_FLEE_COUNT, GetLocalInt( oCreature, CREATURE_FLEE_COUNT )+1 );
LocalDebug(">>>>> I'm running out of here after " + IntToString( GetLocalInt( oCreature, CREATURE_FLEE_COUNT ) ) + " rounds of fleeing" , oCreature );
// Eventually distribute XP for flee
if( !GetLocalInt( oCreature, VAR_XP_FLEE_DISTRIBUTED ) && !(nCreatureFlags & AI_FLAG_WILD) ){
SetLocalInt( oCreature, VAR_XP_FLEE_DISTRIBUTED, 1 );
GiveXPForFlee( oCreatureTarget, oCreature );
LocalDebug("++++++ Some XP for flee got distributed" , oCreature );
}
}
}
}
nCountNoTargets += bHasNoValidTarget;
LocalDebug("########### nCountNoTargets : " + IntToString(nCountNoTargets) , oCreature );
}
SetLocalInt( oController, CONTROLLER_TEAM_ROUND_COUNT, nRound);
// Returning to wandering if no more enemies around !
if( ArrayGetCount( sTeamArrayName ) == nCountNoTargets ){
SetLocalInt( oController, CONTROLLER_TEAM_STATE, ACTION_TEAM_WANDER);
LocalDebug("****** Team is returning to wandering state! : " + IntToString( nTeamArrayCount ) +" / "+ IntToString( nCountNoTargets ) );
}
}