forked from ElunaLuaEngine/Eluna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreatureMethods.h
1282 lines (1157 loc) · 35.9 KB
/
CreatureMethods.h
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2010 - 2016 Eluna Lua Engine <http://emudevs.com/>
* This program is free software licensed under GPL version 3
* Please see the included DOCS/LICENSE.md for more information
*/
#ifndef CREATUREMETHODS_H
#define CREATUREMETHODS_H
/***
* Non-[Player] controlled [Unit]s (i.e. NPCs).
*
* Inherits all methods from: [Object], [WorldObject], [Unit]
*/
namespace LuaCreature
{
/**
* Returns `true` if the [Creature] is set to not give reputation when killed,
* and returns `false` otherwise.
*
* @return bool reputationDisabled
*/
int IsReputationGainDisabled(Eluna* /*E*/, lua_State* L, Creature* creature)
{
Eluna::Push(L, creature->IsReputationGainDisabled());
return 1;
}
/**
* Returns `true` if the [Creature] can regenerate its health out-of-combat,
* and returns `false` otherwise.
*
* @return bool regeneratesHealth
*/
int CanRegenerateHealth(Eluna* /*E*/, lua_State* L, Creature* creature)
{
#ifndef TRINITY
Eluna::Push(L, creature->IsRegeneratingHealth());
#else
Eluna::Push(L, creature->isRegeneratingHealth());
#endif
return 1;
}
/**
* Returns `true` if the [Creature] completes the [Quest] with the ID `questID`,
* and returns `false` otherwise.
*
* @param uint32 questID : the ID of a [Quest]
* @return bool completesQuest
*/
int CanCompleteQuest(Eluna* /*E*/, lua_State* L, Creature* creature)
{
uint32 quest_id = Eluna::CHECKVAL<uint32>(L, 2);
#ifndef TRINITY
Eluna::Push(L, creature->HasInvolvedQuest(quest_id));
#else
Eluna::Push(L, creature->hasInvolvedQuest(quest_id));
#endif
return 1;
}
/**
* Returns `true` if the [Creature] can be targeted for attack,
* and returns `false` otherwise.
*
* @param bool mustBeDead = false : if `true`, only returns `true` if the [Creature] is also dead. Otherwise, it must be alive.
* @return bool targetable
*/
int IsTargetableForAttack(Eluna* /*E*/, lua_State* L, Creature* creature)
{
bool mustBeDead = Eluna::CHECKVAL<bool>(L, 2, false);
#ifdef MANGOS
Eluna::Push(L, creature->IsTargetableForAttack(mustBeDead));
#else
Eluna::Push(L, creature->isTargetableForAttack(mustBeDead));
#endif
return 1;
}
/**
* Returns `true` if the [Creature] can assist `friend` in combat against `enemy`,
* and returns `false` otherwise.
*
* @param [Unit] friend : the Unit we will be assisting
* @param [Unit] enemy : the Unit that we would attack if we assist `friend`
* @param bool checkFaction = true : if `true`, the [Creature] must be the same faction as `friend` to assist
* @return bool canAssist
*/
int CanAssistTo(Eluna* /*E*/, lua_State* L, Creature* creature)
{
Unit* u = Eluna::CHECKOBJ<Unit>(L, 2);
Unit* enemy = Eluna::CHECKOBJ<Unit>(L, 3);
bool checkfaction = Eluna::CHECKVAL<bool>(L, 4, true);
Eluna::Push(L, creature->CanAssistTo(u, enemy, checkfaction));
return 1;
}
/**
* Returns `true` if the [Creature] has searched for combat assistance already,
* and returns `false` otherwise.
*
* @return bool searchedForAssistance
*/
int HasSearchedAssistance(Eluna* /*E*/, lua_State* L, Creature* creature)
{
Eluna::Push(L, creature->HasSearchedAssistance());
return 1;
}
/**
* Returns `true` if the [Creature] will give its loot to `player`,
* and returns `false` otherwise.
*
* @return bool tapped
*/
int IsTappedBy(Eluna* /*E*/, lua_State* L, Creature* creature)
{
Player* player = Eluna::CHECKOBJ<Player>(L, 2);
#ifdef MANGOS
Eluna::Push(L, creature->IsTappedBy(player));
#else
Eluna::Push(L, creature->isTappedBy(player));
#endif
return 1;
}
/**
* Returns `true` if the [Creature] will give its loot to a [Player] or [Group],
* and returns `false` otherwise.
*
* @return bool hasLootRecipient
*/
int HasLootRecipient(Eluna* /*E*/, lua_State* L, Creature* creature)
{
#ifndef TRINITY
Eluna::Push(L, creature->HasLootRecipient());
#else
Eluna::Push(L, creature->hasLootRecipient());
#endif
return 1;
}
/**
* Returns `true` if the [Creature] can start attacking nearby hostile [Unit]s,
* and returns `false` otherwise.
*
* @return bool canAggro
*/
int CanAggro(Eluna* /*E*/, lua_State* L, Creature* creature)
{
#ifdef TRINITY
Eluna::Push(L, !creature->HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_IMMUNE_TO_NPC));
#else
// Eluna::Push(L, creature->CanInitiateAttack());
Eluna::Push(L, !creature->HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_PASSIVE));
#endif
return 1;
}
/**
* Returns `true` if the [Creature] can move through deep water,
* and returns `false` otherwise.
*
* @return bool canSwim
*/
int CanSwim(Eluna* /*E*/, lua_State* L, Creature* creature)
{
Eluna::Push(L, creature->CanSwim());
return 1;
}
/**
* Returns `true` if the [Creature] can move on land,
* and returns `false` otherwise.
*
* @return bool canWalk
*/
int CanWalk(Eluna* /*E*/, lua_State* L, Creature* creature)
{
Eluna::Push(L, creature->CanWalk());
return 1;
}
/**
* Returns `true` if the [Creature] is returning to its spawn position from combat,
* and returns `false` otherwise.
*
* @return bool inEvadeMode
*/
int IsInEvadeMode(Eluna* /*E*/, lua_State* L, Creature* creature)
{
Eluna::Push(L, creature->IsInEvadeMode());
return 1;
}
/**
* Returns `true` if the [Creature]'s rank is Elite or Rare Elite,
* and returns `false` otherwise.
*
* @return bool isElite
*/
int IsElite(Eluna* /*E*/, lua_State* L, Creature* creature)
{
#ifndef TRINITY
Eluna::Push(L, creature->IsElite());
#else
Eluna::Push(L, creature->isElite());
#endif
return 1;
}
/**
* Returns `true` if the [Creature] is a city guard,
* and returns `false` otherwise.
*
* @return bool isGuard
*/
int IsGuard(Eluna* /*E*/, lua_State* L, Creature* creature)
{
Eluna::Push(L, creature->IsGuard());
return 1;
}
/**
* Returns `true` if the [Creature] is a civilian,
* and returns `false` otherwise.
*
* @return bool isCivilian
*/
int IsCivilian(Eluna* /*E*/, lua_State* L, Creature* creature)
{
Eluna::Push(L, creature->IsCivilian());
return 1;
}
/**
* Returns `true` if the [Creature] is the leader of a player faction,
* and returns `false` otherwise.
*
* @return bool isLeader
*/
int IsRacialLeader(Eluna* /*E*/, lua_State* L, Creature* creature)
{
Eluna::Push(L, creature->IsRacialLeader());
return 1;
}
/**
* Returns `true` if the [Creature]'s rank is Boss,
* and returns `false` otherwise.
*
* @return bool isWorldBoss
*/
int IsWorldBoss(Eluna* /*E*/, lua_State* L, Creature* creature)
{
#ifndef TRINITY
Eluna::Push(L, creature->IsWorldBoss());
#else
Eluna::Push(L, creature->isWorldBoss());
#endif
return 1;
}
/**
* Returns `true` if the [Creature] cannot cast `spellId` due to a category cooldown,
* and returns `false` otherwise.
*
* @param uint32 spellId : the ID of a [Spell]
* @return bool hasCooldown
*/
int HasCategoryCooldown(Eluna* /*E*/, lua_State* L, Creature* creature)
{
uint32 spell = Eluna::CHECKVAL<uint32>(L, 2);
#ifdef TRINITY
if (const SpellInfo* info = sSpellMgr->GetSpellInfo(spell))
Eluna::Push(L, info->GetCategory() && creature->GetSpellHistory()->HasCooldown(spell));
else
Eluna::Push(L, false);
#else
Eluna::Push(L, creature->HasCategoryCooldown(spell));
#endif
return 1;
}
/**
* Returns `true` if the [Creature] can cast `spellId` when mind-controlled,
* and returns `false` otherwise.
*
* @param uint32 spellId : the ID of a [Spell]
* @return bool hasSpell
*/
int HasSpell(Eluna* /*E*/, lua_State* L, Creature* creature)
{
uint32 id = Eluna::CHECKVAL<uint32>(L, 2);
Eluna::Push(L, creature->HasSpell(id));
return 1;
}
/**
* Returns `true` if the [Creature] starts the [Quest] `questId`,
* and returns `false` otherwise.
*
* @param uint32 questId : the ID of a [Quest]
* @return bool hasQuest
*/
int HasQuest(Eluna* /*E*/, lua_State* L, Creature* creature)
{
uint32 questId = Eluna::CHECKVAL<uint32>(L, 2);
#ifndef TRINITY
Eluna::Push(L, creature->HasQuest(questId));
#else
Eluna::Push(L, creature->hasQuest(questId));
#endif
return 1;
}
/**
* Returns `true` if the [Creature] has `spellId` on cooldown,
* and returns `false` otherwise.
*
* @param uint32 spellId : the ID of a [Spell]
* @return bool hasCooldown
*/
int HasSpellCooldown(Eluna* /*E*/, lua_State* L, Creature* creature)
{
uint32 spellId = Eluna::CHECKVAL<uint32>(L, 2);
#ifdef TRINITY
Eluna::Push(L, creature->GetSpellHistory()->HasCooldown(spellId));
#else
Eluna::Push(L, creature->HasSpellCooldown(spellId));
#endif
return 1;
}
/**
* Returns `true` if the [Creature] can fly,
* and returns `false` otherwise.
*
* @return bool canFly
*/
int CanFly(Eluna* /*E*/, lua_State* L, Creature* creature)
{
Eluna::Push(L, creature->CanFly());
return 1;
}
#ifdef TRINITY
/**
* Returns `true` if the [Creature] is an invisible trigger,
* and returns `false` otherwise.
*
* @return bool canFly
*/
int IsTrigger(Eluna* /*E*/, lua_State* L, Creature* creature)
{
Eluna::Push(L, creature->IsTrigger());
return 1;
}
/**
* Returns true if the [Creature] is damaged enough for looting
*
* @return bool isDamagedEnough
*/
int IsDamageEnoughForLootingAndReward(Eluna* /*E*/, lua_State* L, Creature* creature)
{
Eluna::Push(L, creature->IsDamageEnoughForLootingAndReward());
return 1;
}
/**
* Returns true if the [Creature] can start attacking specified target
*
* Does not work on most targets
*
* @param [Unit] target
* @param bool force = true : force [Creature] to attack
*/
int CanStartAttack(Eluna* /*E*/, lua_State* L, Creature* creature) // TODO: Implement core side
{
Unit* target = Eluna::CHECKOBJ<Unit>(L, 2);
bool force = Eluna::CHECKVAL<bool>(L, 3, true);
Eluna::Push(L, creature->CanStartAttack(target, force));
return 1;
}
/**
* Returns true if [Creature] has the specified loot mode
*
* @param uint16 lootMode
* @return bool hasLootMode
*/
int HasLootMode(Eluna* /*E*/, lua_State* L, Creature* creature) // TODO: Implement LootMode features
{
uint16 lootMode = Eluna::CHECKVAL<uint16>(L, 2);
Eluna::Push(L, creature->HasLootMode(lootMode));
return 1;
}
#endif
/**
* Returns the time it takes for this [Creature] to respawn once killed.
*
* This value does not usually change over a [Creature]'s lifespan,
* but can be modified by [Creature:SetRespawnDelay].
*
* @return uint32 respawnDelay : the respawn delay, in seconds
*/
int GetRespawnDelay(Eluna* /*E*/, lua_State* L, Creature* creature)
{
Eluna::Push(L, creature->GetRespawnDelay());
return 1;
}
/**
* Returns the radius the [Creature] is permitted to wander from its
* respawn point.
*
* @return float wanderRadius
*/
int GetWanderRadius(Eluna* /*E*/, lua_State* L, Creature* creature)
{
Eluna::Push(L, creature->GetRespawnRadius());
return 1;
}
#ifdef TRINITY
/**
* Returns the current waypoint path ID of the [Creature].
*
* @return uint32 pathId
*/
int GetWaypointPath(Eluna* /*E*/, lua_State* L, Creature* creature)
{
Eluna::Push(L, creature->GetWaypointPath());
return 1;
}
#endif
/**
* Returns the current waypoint ID of the [Creature].
*
* @return uint32 wpId
*/
int GetCurrentWaypointId(Eluna* /*E*/, lua_State* L, Creature* creature)
{
#ifdef TRINITY
Eluna::Push(L, creature->GetCurrentWaypointID());
#else
Eluna::Push(L, creature->GetMotionMaster()->getLastReachedWaypoint());
#endif
return 1;
}
/**
* Returns the default movement type for this [Creature].
*
* @return [MovementGeneratorType] defaultMovementType
*/
int GetDefaultMovementType(Eluna* /*E*/, lua_State* L, Creature* creature)
{
Eluna::Push(L, creature->GetDefaultMovementType());
return 1;
}
/**
* Returns the aggro range of the [Creature] for `target`.
*
* @param [Unit] target
* @return float aggroRange
*/
int GetAggroRange(Eluna* /*E*/, lua_State* L, Creature* creature)
{
Unit* target = Eluna::CHECKOBJ<Unit>(L, 2);
#ifndef TRINITY
float AttackDist = creature->GetAttackDistance(target);
float ThreatRadius = sWorld.getConfig(CONFIG_FLOAT_THREAT_RADIUS);
Eluna::Push(L, ThreatRadius > AttackDist ? ThreatRadius : AttackDist);
#else
Eluna::Push(L, creature->GetAggroRange(target));
#endif
return 1;
}
/**
* Returns the effective aggro range of the [Creature] for `target`.
*
* If this is smaller than the minimum aggro range set in the config file,
* that is used as the aggro range instead.
*
* @param [Unit] target
* @return float attackDistance
*/
int GetAttackDistance(Eluna* /*E*/, lua_State* L, Creature* creature)
{
Unit* target = Eluna::CHECKOBJ<Unit>(L, 2);
Eluna::Push(L, creature->GetAttackDistance(target));
return 1;
}
/**
* Returns the [Group] that can loot this [Creature].
*
* @return [Group] lootRecipientGroup : the group or `nil`
*/
int GetLootRecipientGroup(Eluna* /*E*/, lua_State* L, Creature* creature)
{
#ifndef TRINITY
Eluna::Push(L, creature->GetGroupLootRecipient());
#else
Eluna::Push(L, creature->GetLootRecipientGroup());
#endif
return 1;
}
/**
* Returns the [Player] that can loot this [Creature].
*
* @return [Player] lootRecipient : the player or `nil`
*/
int GetLootRecipient(Eluna* /*E*/, lua_State* L, Creature* creature)
{
Eluna::Push(L, creature->GetLootRecipient());
return 1;
}
/**
* Returns the [Creature]'s script name.
*
* This is used by the core to apply C++ scripts to the Creature.
*
* It is not used by Eluna. Eluna will override AI scripts.
*
* @return string scriptName
*/
int GetScriptName(Eluna* /*E*/, lua_State* L, Creature* creature)
{
Eluna::Push(L, creature->GetScriptName());
return 1;
}
/**
* Returns the [Creature]'s AI name.
*
* This is used by the core to assign the Creature's default AI.
*
* If the Creature is scripted by Eluna, the AI is overriden.
*
* @return string AIName
*/
int GetAIName(Eluna* /*E*/, lua_State* L, Creature* creature)
{
Eluna::Push(L, creature->GetAIName());
return 1;
}
/**
* Returns the [Creature]'s script ID.
*
* Every C++ script name is assigned a unique ID by the core.
* This returns the ID for this [Creature]'s script name.
*
* @return uint32 scriptID
*/
int GetScriptId(Eluna* /*E*/, lua_State* L, Creature* creature)
{
Eluna::Push(L, creature->GetScriptId());
return 1;
}
/**
* Returns the [Creature]'s cooldown for `spellID`.
*
* @param uint32 spellID
* @return uint32 cooldown : the cooldown, in milliseconds
*/
int GetCreatureSpellCooldownDelay(Eluna* /*E*/, lua_State* L, Creature* creature)
{
uint32 spell = Eluna::CHECKVAL<uint32>(L, 2);
#ifdef TRINITY
if (SpellInfo const* spellInfo = sSpellMgr->GetSpellInfo(spell))
Eluna::Push(L, creature->GetSpellHistory()->GetRemainingCooldown(spellInfo));
else
Eluna::Push(L, 0);
#else
Eluna::Push(L, creature->GetCreatureSpellCooldownDelay(spell));
#endif
return 1;
}
/**
* Returns the delay between when the [Creature] dies and when its body despawns.
*
* @return uint32 corpseDelay : the delay, in seconds
*/
int GetCorpseDelay(Eluna* /*E*/, lua_State* L, Creature* creature)
{
Eluna::Push(L, creature->GetCorpseDelay());
return 1;
}
/**
* Returns position the [Creature] returns to when evading from combat
* or respawning.
*
* @return float x
* @return float y
* @return float z
* @return float o
*/
int GetHomePosition(Eluna* /*E*/, lua_State* L, Creature* creature)
{
float x, y, z, o;
#ifndef TRINITY
creature->GetRespawnCoord(x, y, z, &o);
#else
creature->GetHomePosition(x, y, z, o);
#endif
Eluna::Push(L, x);
Eluna::Push(L, y);
Eluna::Push(L, z);
Eluna::Push(L, o);
return 4;
}
/**
* Sets the position the [Creature] returns to when evading from combat
* or respawning.
*
* @param float x
* @param float y
* @param float z
* @param float o
*/
int SetHomePosition(Eluna* /*E*/, lua_State* L, Creature* creature)
{
float x = Eluna::CHECKVAL<float>(L, 2);
float y = Eluna::CHECKVAL<float>(L, 3);
float z = Eluna::CHECKVAL<float>(L, 4);
float o = Eluna::CHECKVAL<float>(L, 5);
#ifndef TRINITY
creature->SetRespawnCoord(x, y, z, o);
#else
creature->SetHomePosition(x, y, z, o);
#endif
return 0;
}
/**
* Returns a target from the [Creature]'s threat list based on the
* supplied arguments.
*
* enum SelectAggroTarget
* {
* SELECT_TARGET_RANDOM = 0, //Just selects a random target
* SELECT_TARGET_TOPAGGRO, //Selects targets from top aggro to bottom
* SELECT_TARGET_BOTTOMAGGRO, //Selects targets from bottom aggro to top
* SELECT_TARGET_NEAREST,
* SELECT_TARGET_FARTHEST
* };
*
* For example, if you wanted to select the third-farthest [Player]
* within 50 yards that has the [Aura] "Corrupted Blood" (ID 24328),
* you could use this function like so:
*
* target = creature:GetAITarget(4, true, 3, 50, 24328)
*
* @param [SelectAggroTarget] targetType : how the threat list should be sorted
* @param bool playerOnly = false : if `true`, skips targets that aren't [Player]s
* @param uint32 position = 0 : used as an offset into the threat list. If `targetType` is random, used as the number of players from top of aggro to choose from
* @param float distance = 0.0 : if positive, the maximum distance for the target. If negative, the minimum distance
* @param int32 aura = 0 : if positive, the target must have this [Aura]. If negative, the the target must not have this Aura
* @return [Unit] target : the target, or `nil`
*/
int GetAITarget(Eluna* /*E*/, lua_State* L, Creature* creature)
{
uint32 targetType = Eluna::CHECKVAL<uint32>(L, 2);
bool playerOnly = Eluna::CHECKVAL<bool>(L, 3, false);
uint32 position = Eluna::CHECKVAL<uint32>(L, 4, 0);
float dist = Eluna::CHECKVAL<float>(L, 5, 0.0f);
int32 aura = Eluna::CHECKVAL<int32>(L, 6, 0);
#ifdef MANGOS
ThreatList const& threatlist = creature->GetThreatManager().getThreatList();
#else
ThreatList const& threatlist = creature->getThreatManager().getThreatList();
#endif
if (threatlist.empty())
return 1;
if (position >= threatlist.size())
return 1;
std::list<Unit*> targetList;
for (ThreatList::const_iterator itr = threatlist.begin(); itr != threatlist.end(); ++itr)
{
Unit* target = (*itr)->getTarget();
if (!target)
continue;
if (playerOnly && target->GetTypeId() != TYPEID_PLAYER)
continue;
if (aura > 0 && !target->HasAura(aura))
continue;
else if (aura < 0 && target->HasAura(-aura))
continue;
if (dist > 0.0f && !creature->IsWithinDist(target, dist))
continue;
else if (dist < 0.0f && creature->IsWithinDist(target, -dist))
continue;
targetList.push_back(target);
}
if (targetList.empty())
return 1;
if (position >= targetList.size())
return 1;
if (targetType == SELECT_TARGET_NEAREST || targetType == SELECT_TARGET_FARTHEST)
targetList.sort(ElunaUtil::ObjectDistanceOrderPred(creature));
switch (targetType)
{
case SELECT_TARGET_NEAREST:
case SELECT_TARGET_TOPAGGRO:
{
std::list<Unit*>::const_iterator itr = targetList.begin();
if (position)
std::advance(itr, position);
Eluna::Push(L, *itr);
}
break;
case SELECT_TARGET_FARTHEST:
case SELECT_TARGET_BOTTOMAGGRO:
{
std::list<Unit*>::reverse_iterator ritr = targetList.rbegin();
if (position)
std::advance(ritr, position);
Eluna::Push(L, *ritr);
}
break;
case SELECT_TARGET_RANDOM:
{
std::list<Unit*>::const_iterator itr = targetList.begin();
if (position)
std::advance(itr, urand(0, position));
else
std::advance(itr, urand(0, targetList.size() - 1));
Eluna::Push(L, *itr);
}
break;
default:
luaL_argerror(L, 2, "SelectAggroTarget expected");
break;
}
return 1;
}
/**
* Returns all [Unit]s in the [Creature]'s threat list.
*
* @return table targets
*/
int GetAITargets(Eluna* /*E*/, lua_State* L, Creature* creature)
{
lua_newtable(L);
int tbl = lua_gettop(L);
uint32 i = 0;
#ifdef MANGOS
ThreatList const& threatlist = creature->GetThreatManager().getThreatList();
#else
ThreatList const& threatlist = creature->getThreatManager().getThreatList();
#endif
if (threatlist.empty())
return 1;
for (ThreatList::const_iterator itr = threatlist.begin(); itr != threatlist.end(); ++itr)
{
Unit* target = (*itr)->getTarget();
if (!target)
continue;
++i;
Eluna::Push(L, i);
Eluna::Push(L, target);
lua_settable(L, tbl);
}
lua_settop(L, tbl);
return 1;
}
/**
* Returns the number of [Unit]s in this [Creature]'s threat list.
*
* @return int targetsCount
*/
int GetAITargetsCount(Eluna* /*E*/, lua_State* L, Creature* creature)
{
#ifdef MANGOS
Eluna::Push(L, creature->GetThreatManager().getThreatList().size());
#else
Eluna::Push(L, creature->getThreatManager().getThreatList().size());
#endif
return 1;
}
/**
* Returns the [Creature]'s NPC flags.
*
* These are used to control whether the NPC is a vendor, can repair items,
* can give quests, etc.
*
* @return [NPCFlags] npcFlags
*/
int GetNPCFlags(Eluna* /*E*/, lua_State* L, Creature* creature)
{
Eluna::Push(L, creature->GetUInt32Value(UNIT_NPC_FLAGS));
return 1;
}
#ifndef CATA
/**
* Returns the [Creature]'s shield block value.
*
* @return uint32 shieldBlockValue
*/
int GetShieldBlockValue(Eluna* /*E*/, lua_State* L, Creature* creature)
{
Eluna::Push(L, creature->GetShieldBlockValue());
return 1;
}
#endif
#ifdef TRINITY
int GetLootMode(Eluna* /*E*/, lua_State* L, Creature* creature) // TODO: Implement LootMode features
{
Eluna::Push(L, creature->GetLootMode());
return 1;
}
#endif
/**
* Returns the guid of the [Creature] that is used as the ID in the database
*
* @return uint32 dbguid
*/
int GetDBTableGUIDLow(Eluna* /*E*/, lua_State* L, Creature* creature)
{
#ifdef TRINITY
Eluna::Push(L, creature->GetSpawnId());
#else
// on mangos based this is same as lowguid
Eluna::Push(L, creature->GetGUIDLow());
#endif
return 1;
}
/**
* Sets the [Creature]'s NPC flags to `flags`.
*
* @param [NPCFlags] flags
*/
int SetNPCFlags(Eluna* /*E*/, lua_State* L, Creature* creature)
{
uint32 flags = Eluna::CHECKVAL<uint32>(L, 2);
creature->SetUInt32Value(UNIT_NPC_FLAGS, flags);
return 0;
}
/**
* Makes the [Creature] able to fly if enabled.
*
* @param bool enable = true
*/
int SetDisableGravity(Eluna* /*E*/, lua_State* L, Creature* creature)
{
bool enable = Eluna::CHECKVAL<bool>(L, 2, true);
#ifdef TRINITY
creature->SetDisableGravity(!enable);
#else
creature->SetLevitate(enable);
#endif
return 0;
}
#ifdef TRINITY
int SetLootMode(Eluna* /*E*/, lua_State* L, Creature* creature) // TODO: Implement LootMode features
{
uint16 lootMode = Eluna::CHECKVAL<uint16>(L, 2);
creature->SetLootMode(lootMode);
return 0;
}
#endif
/**
* Sets the [Creature]'s death state to `deathState`.
*
* @param [DeathState] deathState
*/
int SetDeathState(Eluna* /*E*/, lua_State* L, Creature* creature)
{
int32 state = Eluna::CHECKVAL<int32>(L, 2);
#ifndef TRINITY
creature->SetDeathState((DeathState)state);
#else
creature->setDeathState((DeathState)state);
#endif
return 0;
}
/**
* Sets whether the [Creature] is currently walking or running.
*
* @param bool enable = true : `true` to enable walking, `false` for running
*/
int SetWalk(Eluna* /*E*/, lua_State* L, Creature* creature) // TODO: Move same to Player ?
{
bool enable = Eluna::CHECKVAL<bool>(L, 2, true);
creature->SetWalk(enable);
return 0;
}
/**
* Equips given [Item]s to the [Unit]. Using 0 removes the equipped [Item]
*
* @param uint32 main_hand : main hand [Item]'s entry
* @param uint32 off_hand : off hand [Item]'s entry
* @param uint32 ranged : ranged [Item]'s entry
*/
int SetEquipmentSlots(Eluna* /*E*/, lua_State* L, Creature* creature)
{
uint32 main_hand = Eluna::CHECKVAL<uint32>(L, 2);
uint32 off_hand = Eluna::CHECKVAL<uint32>(L, 3);
uint32 ranged = Eluna::CHECKVAL<uint32>(L, 4);
#ifdef TRINITY
creature->SetUInt32Value(UNIT_VIRTUAL_ITEM_SLOT_ID + 0, main_hand);
creature->SetUInt32Value(UNIT_VIRTUAL_ITEM_SLOT_ID + 1, off_hand);
creature->SetUInt32Value(UNIT_VIRTUAL_ITEM_SLOT_ID + 2, ranged);
#else
creature->SetVirtualItem(VIRTUAL_ITEM_SLOT_0, main_hand);
creature->SetVirtualItem(VIRTUAL_ITEM_SLOT_1, off_hand);
creature->SetVirtualItem(VIRTUAL_ITEM_SLOT_2, ranged);
#endif
return 0;
}
/**
* Sets whether the [Creature] can be aggroed.
*
* @param bool allow = true : `true` to allow aggro, `false` to disable aggro
*/
int SetAggroEnabled(Eluna* /*E*/, lua_State* L, Creature* creature)
{
bool allow = Eluna::CHECKVAL<bool>(L, 2, true);
#ifdef TRINITY
if (allow)
creature->RemoveFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_IMMUNE_TO_NPC);
else
creature->SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_IMMUNE_TO_NPC);
#else
if (allow)
creature->RemoveFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_PASSIVE);
else
creature->SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_PASSIVE);
#endif
return 0;
}
/**
* Sets whether the [Creature] gives reputation or not.
*
* @param bool disable = true : `true` to disable reputation, `false` to enable
*/
int SetDisableReputationGain(Eluna* /*E*/, lua_State* L, Creature* creature)
{