forked from ElunaLuaEngine/Eluna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WorldObjectMethods.h
914 lines (839 loc) · 31.7 KB
/
WorldObjectMethods.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
/*
* 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 WORLDOBJECTMETHODS_H
#define WORLDOBJECTMETHODS_H
/***
* Inherits all methods from: [Object]
*/
namespace LuaWorldObject
{
/**
* Returns the name of the [WorldObject]
*
* @return string name
*/
int GetName(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
Eluna::Push(L, obj->GetName());
return 1;
}
/**
* Returns the current [Map] object of the [WorldObject]
*
* @return [Map] mapObject
*/
int GetMap(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
Eluna::Push(L, obj->GetMap());
return 1;
}
#if (!defined(TBC) && !defined(CLASSIC))
/**
* Returns the current phase of the [WorldObject]
*
* @return uint32 phase
*/
int GetPhaseMask(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
Eluna::Push(L, obj->GetPhaseMask());
return 1;
}
#endif
/**
* Returns the current instance ID of the [WorldObject]
*
* @return uint32 instanceId
*/
int GetInstanceId(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
Eluna::Push(L, obj->GetInstanceId());
return 1;
}
/**
* Returns the current area ID of the [WorldObject]
*
* @return uint32 areaId
*/
int GetAreaId(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
Eluna::Push(L, obj->GetAreaId());
return 1;
}
/**
* Returns the current zone ID of the [WorldObject]
*
* @return uint32 zoneId
*/
int GetZoneId(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
Eluna::Push(L, obj->GetZoneId());
return 1;
}
/**
* Returns the current map ID of the [WorldObject]
*
* @return uint32 mapId
*/
int GetMapId(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
Eluna::Push(L, obj->GetMapId());
return 1;
}
/**
* Returns the current X coordinate of the [WorldObject]
*
* @return float x
*/
int GetX(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
Eluna::Push(L, obj->GetPositionX());
return 1;
}
/**
* Returns the current Y coordinate of the [WorldObject]
*
* @return float y
*/
int GetY(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
Eluna::Push(L, obj->GetPositionY());
return 1;
}
/**
* Returns the current Z coordinate of the [WorldObject]
*
* @return float z
*/
int GetZ(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
Eluna::Push(L, obj->GetPositionZ());
return 1;
}
/**
* Returns the current orientation of the [WorldObject]
*
* @return float orientation / facing
*/
int GetO(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
Eluna::Push(L, obj->GetOrientation());
return 1;
}
/**
* Returns the coordinates and orientation of the [WorldObject]
*
* @return float x : x coordinate of the [WorldObject]
* @return float y : y coordinate of the [WorldObject]
* @return float z : z coordinate (height) of the [WorldObject]
* @return float o : facing / orientation of the [WorldObject]
*/
int GetLocation(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
Eluna::Push(L, obj->GetPositionX());
Eluna::Push(L, obj->GetPositionY());
Eluna::Push(L, obj->GetPositionZ());
Eluna::Push(L, obj->GetOrientation());
return 4;
}
/**
* Returns the nearest [Player] object in sight of the [WorldObject] or within the given range
*
* @param float range = 533.33333 : optionally set range. Default range is grid size
* @param uint32 hostile = 0 : 0 both, 1 hostile, 2 friendly
* @param uint32 dead = 1 : 0 both, 1 alive, 2 dead
*
* @return [Player] nearestPlayer
*/
int GetNearestPlayer(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
float range = Eluna::CHECKVAL<float>(L, 2, SIZE_OF_GRIDS);
uint32 hostile = Eluna::CHECKVAL<uint32>(L, 3, 0);
uint32 dead = Eluna::CHECKVAL<uint32>(L, 4, 1);
Unit* target = NULL;
ElunaUtil::WorldObjectInRangeCheck checker(true, obj, range, TYPEMASK_PLAYER, 0, hostile, dead);
#ifndef TRINITY
MaNGOS::UnitLastSearcher<ElunaUtil::WorldObjectInRangeCheck> searcher(target, checker);
Cell::VisitWorldObjects(obj, searcher, range);
#else
Trinity::UnitLastSearcher<ElunaUtil::WorldObjectInRangeCheck> searcher(obj, target, checker);
obj->VisitNearbyObject(range, searcher);
#endif
Eluna::Push(L, target);
return 1;
}
/**
* Returns the nearest [GameObject] object in sight of the [WorldObject] or within the given range and/or with a specific entry ID
*
* @param float range = 533.33333 : optionally set range. Default range is grid size
* @param uint32 entryId = 0 : optionally set entry ID of game object to find
* @param uint32 hostile = 0 : 0 both, 1 hostile, 2 friendly
*
* @return [GameObject] nearestGameObject
*/
int GetNearestGameObject(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
float range = Eluna::CHECKVAL<float>(L, 2, SIZE_OF_GRIDS);
uint32 entry = Eluna::CHECKVAL<uint32>(L, 3, 0);
uint32 hostile = Eluna::CHECKVAL<uint32>(L, 4, 0);
GameObject* target = NULL;
ElunaUtil::WorldObjectInRangeCheck checker(true, obj, range, TYPEMASK_GAMEOBJECT, entry, hostile);
#ifndef TRINITY
MaNGOS::GameObjectLastSearcher<ElunaUtil::WorldObjectInRangeCheck> searcher(target, checker);
Cell::VisitGridObjects(obj, searcher, range);
#else
Trinity::GameObjectLastSearcher<ElunaUtil::WorldObjectInRangeCheck> searcher(obj, target, checker);
obj->VisitNearbyObject(range, searcher);
#endif
Eluna::Push(L, target);
return 1;
}
/**
* Returns the nearest [Creature] object in sight of the [WorldObject] or within the given range and/or with a specific entry ID
*
* @param float range = 533.33333 : optionally set range. Default range is grid size
* @param uint32 entryId = 0 : optionally set entry ID of creature to find
* @param uint32 hostile = 0 : 0 both, 1 hostile, 2 friendly
* @param uint32 dead = 1 : 0 both, 1 alive, 2 dead
*
* @return [Creature] nearestCreature
*/
int GetNearestCreature(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
float range = Eluna::CHECKVAL<float>(L, 2, SIZE_OF_GRIDS);
uint32 entry = Eluna::CHECKVAL<uint32>(L, 3, 0);
uint32 hostile = Eluna::CHECKVAL<uint32>(L, 4, 0);
uint32 dead = Eluna::CHECKVAL<uint32>(L, 5, 1);
Creature* target = NULL;
ElunaUtil::WorldObjectInRangeCheck checker(true, obj, range, TYPEMASK_UNIT, entry, hostile, dead);
#ifndef TRINITY
MaNGOS::CreatureLastSearcher<ElunaUtil::WorldObjectInRangeCheck> searcher(target, checker);
Cell::VisitGridObjects(obj, searcher, range);
#else
Trinity::CreatureLastSearcher<ElunaUtil::WorldObjectInRangeCheck> searcher(obj, target, checker);
obj->VisitNearbyObject(range, searcher);
#endif
Eluna::Push(L, target);
return 1;
}
/**
* Returns a table of [Player] objects in sight of the [WorldObject] or within the given range
*
* @param float range = 533.33333 : optionally set range. Default range is grid size
* @param uint32 hostile = 0 : 0 both, 1 hostile, 2 friendly
* @param uint32 dead = 1 : 0 both, 1 alive, 2 dead
*
* @return table playersInRange : table of [Player]s
*/
int GetPlayersInRange(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
float range = Eluna::CHECKVAL<float>(L, 2, SIZE_OF_GRIDS);
uint32 hostile = Eluna::CHECKVAL<uint32>(L, 3, 0);
uint32 dead = Eluna::CHECKVAL<uint32>(L, 4, 1);
std::list<Player*> list;
ElunaUtil::WorldObjectInRangeCheck checker(false, obj, range, TYPEMASK_PLAYER, 0, hostile, dead);
#ifndef TRINITY
MaNGOS::PlayerListSearcher<ElunaUtil::WorldObjectInRangeCheck> searcher(list, checker);
Cell::VisitWorldObjects(obj, searcher, range);
#else
Trinity::PlayerListSearcher<ElunaUtil::WorldObjectInRangeCheck> searcher(obj, list, checker);
obj->VisitNearbyObject(range, searcher);
#endif
lua_newtable(L);
int tbl = lua_gettop(L);
uint32 i = 0;
for (std::list<Player*>::const_iterator it = list.begin(); it != list.end(); ++it)
{
Eluna::Push(L, ++i);
Eluna::Push(L, *it);
lua_settable(L, tbl);
}
lua_settop(L, tbl);
return 1;
}
/**
* Returns a table of [Creature] objects in sight of the [WorldObject] or within the given range and/or with a specific entry ID
*
* @param float range = 533.33333 : optionally set range. Default range is grid size
* @param uint32 entryId = 0 : optionally set entry ID of creatures to find
* @param uint32 hostile = 0 : 0 both, 1 hostile, 2 friendly
* @param uint32 dead = 1 : 0 both, 1 alive, 2 dead
*
* @return table creaturesInRange : table of [Creature]s
*/
int GetCreaturesInRange(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
float range = Eluna::CHECKVAL<float>(L, 2, SIZE_OF_GRIDS);
uint32 entry = Eluna::CHECKVAL<uint32>(L, 3, 0);
uint32 hostile = Eluna::CHECKVAL<uint32>(L, 4, 0);
uint32 dead = Eluna::CHECKVAL<uint32>(L, 5, 1);
std::list<Creature*> list;
ElunaUtil::WorldObjectInRangeCheck checker(false, obj, range, TYPEMASK_UNIT, entry, hostile, dead);
#ifndef TRINITY
MaNGOS::CreatureListSearcher<ElunaUtil::WorldObjectInRangeCheck> searcher(list, checker);
Cell::VisitGridObjects(obj, searcher, range);
#else
Trinity::CreatureListSearcher<ElunaUtil::WorldObjectInRangeCheck> searcher(obj, list, checker);
obj->VisitNearbyObject(range, searcher);
#endif
lua_newtable(L);
int tbl = lua_gettop(L);
uint32 i = 0;
for (std::list<Creature*>::const_iterator it = list.begin(); it != list.end(); ++it)
{
Eluna::Push(L, ++i);
Eluna::Push(L, *it);
lua_settable(L, tbl);
}
lua_settop(L, tbl);
return 1;
}
/**
* Returns a table of [GameObject] objects in sight of the [WorldObject] or within the given range and/or with a specific entry ID
*
* @param float range = 533.33333 : optionally set range. Default range is grid size
* @param uint32 entryId = 0 : optionally set entry ID of game objects to find
* @param uint32 hostile = 0 : 0 both, 1 hostile, 2 friendly
*
* @return table gameObjectsInRange : table of [GameObject]s
*/
int GetGameObjectsInRange(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
float range = Eluna::CHECKVAL<float>(L, 2, SIZE_OF_GRIDS);
uint32 entry = Eluna::CHECKVAL<uint32>(L, 3, 0);
uint32 hostile = Eluna::CHECKVAL<uint32>(L, 4, 0);
std::list<GameObject*> list;
ElunaUtil::WorldObjectInRangeCheck checker(false, obj, range, TYPEMASK_GAMEOBJECT, entry, hostile);
#ifndef TRINITY
MaNGOS::GameObjectListSearcher<ElunaUtil::WorldObjectInRangeCheck> searcher(list, checker);
Cell::VisitGridObjects(obj, searcher, range);
#else
Trinity::GameObjectListSearcher<ElunaUtil::WorldObjectInRangeCheck> searcher(obj, list, checker);
obj->VisitNearbyObject(range, searcher);
#endif
lua_newtable(L);
int tbl = lua_gettop(L);
uint32 i = 0;
for (std::list<GameObject*>::const_iterator it = list.begin(); it != list.end(); ++it)
{
Eluna::Push(L, ++i);
Eluna::Push(L, *it);
lua_settable(L, tbl);
}
lua_settop(L, tbl);
return 1;
}
/**
* Returns nearest [WorldObject] in sight of the [WorldObject].
* The distance, type, entry and hostility requirements the [WorldObject] must match can be passed.
*
* @param float range = 533.33333 : optionally set range. Default range is grid size
* @param [TypeMask] type = 0 : the [TypeMask] that the [WorldObject] must be. This can contain multiple types. 0 will be ingored
* @param uint32 entry = 0 : the entry of the [WorldObject], 0 will be ingored
* @param uint32 hostile = 0 : specifies whether the [WorldObject] needs to be 1 hostile, 2 friendly or 0 either
* @param uint32 dead = 1 : 0 both, 1 alive, 2 dead
*
* @return [WorldObject] worldObject
*/
int GetNearObject(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
float range = Eluna::CHECKVAL<float>(L, 2, SIZE_OF_GRIDS);
uint16 type = Eluna::CHECKVAL<uint16>(L, 3, 0); // TypeMask
uint32 entry = Eluna::CHECKVAL<uint32>(L, 4, 0);
uint32 hostile = Eluna::CHECKVAL<uint32>(L, 5, 0); // 0 none, 1 hostile, 2 friendly
uint32 dead = Eluna::CHECKVAL<uint32>(L, 6, 1); // 0 both, 1 alive, 2 dead
float x, y, z;
obj->GetPosition(x, y, z);
ElunaUtil::WorldObjectInRangeCheck checker(true, obj, range, type, entry, hostile);
WorldObject* target = NULL;
#ifndef TRINITY
MaNGOS::WorldObjectLastSearcher<ElunaUtil::WorldObjectInRangeCheck> searcher(target, checker);
Cell::VisitAllObjects(obj, searcher, range);
#else
Trinity::WorldObjectLastSearcher<ElunaUtil::WorldObjectInRangeCheck> searcher(obj, target, checker);
obj->VisitNearbyObject(range, searcher);
#endif
Eluna::Push(L, target);
return 1;
}
/**
* Returns a table of [WorldObject]s in sight of the [WorldObject].
* The distance, type, entry and hostility requirements the [WorldObject] must match can be passed.
*
* @param float range = 533.33333 : optionally set range. Default range is grid size
* @param [TypeMask] type = 0 : the [TypeMask] that the [WorldObject] must be. This can contain multiple types. 0 will be ingored
* @param uint32 entry = 0 : the entry of the [WorldObject], 0 will be ingored
* @param uint32 hostile = 0 : specifies whether the [WorldObject] needs to be 1 hostile, 2 friendly or 0 either
* @param uint32 dead = 1 : 0 both, 1 alive, 2 dead
*
* @return table worldObjectList : table of [WorldObject]s
*/
int GetNearObjects(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
float range = Eluna::CHECKVAL<float>(L, 2, SIZE_OF_GRIDS);
uint16 type = Eluna::CHECKVAL<uint16>(L, 3, 0); // TypeMask
uint32 entry = Eluna::CHECKVAL<uint32>(L, 4, 0);
uint32 hostile = Eluna::CHECKVAL<uint32>(L, 5, 0); // 0 none, 1 hostile, 2 friendly
uint32 dead = Eluna::CHECKVAL<uint32>(L, 6, 1); // 0 both, 1 alive, 2 dead
float x, y, z;
obj->GetPosition(x, y, z);
ElunaUtil::WorldObjectInRangeCheck checker(false, obj, range, type, entry, hostile, dead);
std::list<WorldObject*> list;
#ifndef TRINITY
MaNGOS::WorldObjectListSearcher<ElunaUtil::WorldObjectInRangeCheck> searcher(list, checker);
Cell::VisitAllObjects(obj, searcher, range);
#else
Trinity::WorldObjectListSearcher<ElunaUtil::WorldObjectInRangeCheck> searcher(obj, list, checker);
obj->VisitNearbyObject(range, searcher);
#endif
lua_newtable(L);
int tbl = lua_gettop(L);
uint32 i = 0;
for (std::list<WorldObject*>::const_iterator it = list.begin(); it != list.end(); ++it)
{
Eluna::Push(L, ++i);
Eluna::Push(L, *it);
lua_settable(L, tbl);
}
lua_settop(L, tbl);
return 1;
}
/**
* Returns the distance from this [WorldObject] to another [WorldObject], or from this [WorldObject] to a point in 3d space.
*
* The function takes into account the given object sizes. See also [WorldObject:GetExactDistance], [WorldObject:GetDistance2d]
*
* @proto dist = (obj)
* @proto dist = (x, y, z)
*
* @param [WorldObject] obj
* @param float x : the X-coordinate of the point
* @param float y : the Y-coordinate of the point
* @param float z : the Z-coordinate of the point
*
* @return float dist : the distance in yards
*/
int GetDistance(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
WorldObject* target = Eluna::CHECKOBJ<WorldObject>(L, 2, false);
if (target && target->IsInWorld())
Eluna::Push(L, obj->GetDistance(target));
else
{
float X = Eluna::CHECKVAL<float>(L, 2);
float Y = Eluna::CHECKVAL<float>(L, 3);
float Z = Eluna::CHECKVAL<float>(L, 4);
Eluna::Push(L, obj->GetDistance(X, Y, Z));
}
return 1;
}
/**
* Returns the distance from this [WorldObject] to another [WorldObject], or from this [WorldObject] to a point in 3d space.
*
* The function does not take into account the given object sizes, which means only the object coordinates are compared. See also [WorldObject:GetDistance], [WorldObject:GetDistance2d]
*
* @proto dist = (obj)
* @proto dist = (x, y, z)
*
* @param [WorldObject] obj
* @param float x : the X-coordinate of the point
* @param float y : the Y-coordinate of the point
* @param float z : the Z-coordinate of the point
*
* @return float dist : the distance in yards
*/
int GetExactDistance(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
float x, y, z;
obj->GetPosition(x, y, z);
WorldObject* target = Eluna::CHECKOBJ<WorldObject>(L, 2, false);
if (target && target->IsInWorld())
{
float x2, y2, z2;
target->GetPosition(x2, y2, z2);
x -= x2;
y -= y2;
z -= z2;
}
else
{
x -= Eluna::CHECKVAL<float>(L, 2);
y -= Eluna::CHECKVAL<float>(L, 3);
z -= Eluna::CHECKVAL<float>(L, 4);
}
Eluna::Push(L, std::sqrt(x*x + y*y + z*z));
return 1;
}
/**
* Returns the distance from this [WorldObject] to another [WorldObject], or from this [WorldObject] to a point in 2d space.
*
* The function takes into account the given object sizes. See also [WorldObject:GetDistance], [WorldObject:GetExactDistance2d]
*
* @proto dist = (obj)
* @proto dist = (x, y)
*
* @param [WorldObject] obj
* @param float x : the X-coordinate of the point
* @param float y : the Y-coordinate of the point
*
* @return float dist : the distance in yards
*/
int GetDistance2d(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
WorldObject* target = Eluna::CHECKOBJ<WorldObject>(L, 2, false);
if (target && target->IsInWorld())
Eluna::Push(L, obj->GetDistance2d(target));
else
{
float X = Eluna::CHECKVAL<float>(L, 2);
float Y = Eluna::CHECKVAL<float>(L, 3);
Eluna::Push(L, obj->GetDistance2d(X, Y));
}
return 1;
}
/**
* Returns the distance from this [WorldObject] to another [WorldObject], or from this [WorldObject] to a point in 2d space.
*
* The function does not take into account the given object sizes, which means only the object coordinates are compared. See also [WorldObject:GetDistance], [WorldObject:GetDistance2d]
*
* @proto dist = (obj)
* @proto dist = (x, y)
*
* @param [WorldObject] obj
* @param float x : the X-coordinate of the point
* @param float y : the Y-coordinate of the point
*
* @return float dist : the distance in yards
*/
int GetExactDistance2d(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
float x, y, z;
obj->GetPosition(x, y, z);
WorldObject* target = Eluna::CHECKOBJ<WorldObject>(L, 2, false);
if (target && target->IsInWorld())
{
float x2, y2, z2;
target->GetPosition(x2, y2, z2);
x -= x2;
y -= y2;
}
else
{
x -= Eluna::CHECKVAL<float>(L, 2);
y -= Eluna::CHECKVAL<float>(L, 3);
}
Eluna::Push(L, std::sqrt(x*x + y*y));
return 1;
}
/**
* Returns the x, y and z of a point dist away from the [WorldObject].
*
* @param float distance : specifies the distance of the point from the [WorldObject] in yards
* @param float angle : specifies the angle of the point relative to the orientation / facing of the [WorldObject] in radians
*
* @return float x
* @return float y
* @return float z
*/
int GetRelativePoint(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
float dist = Eluna::CHECKVAL<float>(L, 2);
float rad = Eluna::CHECKVAL<float>(L, 3);
float x, y, z;
obj->GetClosePoint(x, y, z, 0.0f, dist, rad);
Eluna::Push(L, x);
Eluna::Push(L, y);
Eluna::Push(L, z);
return 3;
}
/**
* Returns the angle between this [WorldObject] and another [WorldObject] or a point.
*
* The angle is the angle between two points and orientation will be ignored.
*
* @proto dist = (obj)
* @proto dist = (x, y)
*
* @param [WorldObject] object
* @param float x
* @param float y
*
* @return float angle : angle in radians in range 0..2*pi
*/
int GetAngle(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
WorldObject* target = Eluna::CHECKOBJ<WorldObject>(L, 2, false);
if (target && target->IsInWorld())
Eluna::Push(L, obj->GetAngle(target));
else
{
float x = Eluna::CHECKVAL<float>(L, 2);
float y = Eluna::CHECKVAL<float>(L, 3);
Eluna::Push(L, obj->GetAngle(x, y));
}
return 1;
}
/**
* Sends a [WorldPacket] to [Player]s in sight of the [WorldObject].
*
* @param [WorldPacket] packet
*/
int SendPacket(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
WorldPacket* data = Eluna::CHECKOBJ<WorldPacket>(L, 2);
obj->SendMessageToSet(data, true);
return 0;
}
/**
* Spawns a [GameObject] at specified location.
*
* @param uint32 entry : [GameObject] entry ID
* @param float x
* @param float y
* @param float z
* @param float o
* @param uint32 respawnDelay = 30 : respawn time in seconds
* @return [GameObject] gameObject
*/
int SummonGameObject(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
uint32 entry = Eluna::CHECKVAL<uint32>(L, 2);
float x = Eluna::CHECKVAL<float>(L, 3);
float y = Eluna::CHECKVAL<float>(L, 4);
float z = Eluna::CHECKVAL<float>(L, 5);
float o = Eluna::CHECKVAL<float>(L, 6);
uint32 respawnDelay = Eluna::CHECKVAL<uint32>(L, 7, 30);
#ifndef TRINITY
Eluna::Push(L, obj->SummonGameObject(entry, x, y, z, o, respawnDelay));
#else
Eluna::Push(L, obj->SummonGameObject(entry, x, y, z, o, 0, 0, 0, 0, respawnDelay));
#endif
return 1;
}
/**
* Spawns the creature at specified location.
*
* enum TempSummonType
* {
* TEMPSUMMON_TIMED_OR_DEAD_DESPAWN = 1, // despawns after a specified time OR when the creature disappears
* TEMPSUMMON_TIMED_OR_CORPSE_DESPAWN = 2, // despawns after a specified time OR when the creature dies
* TEMPSUMMON_TIMED_DESPAWN = 3, // despawns after a specified time
* TEMPSUMMON_TIMED_DESPAWN_OUT_OF_COMBAT = 4, // despawns after a specified time after the creature is out of combat
* TEMPSUMMON_CORPSE_DESPAWN = 5, // despawns instantly after death
* TEMPSUMMON_CORPSE_TIMED_DESPAWN = 6, // despawns after a specified time after death
* TEMPSUMMON_DEAD_DESPAWN = 7, // despawns when the creature disappears
* TEMPSUMMON_MANUAL_DESPAWN = 8, // despawns when UnSummon() is called
* TEMPSUMMON_TIMED_OOC_OR_CORPSE_DESPAWN = 9, // despawns after a specified time (OOC) OR when the creature dies
* TEMPSUMMON_TIMED_OOC_OR_DEAD_DESPAWN = 10 // despawns after a specified time (OOC) OR when the creature disappears
* };
*
* @param uint32 entry : [Creature]'s entry ID
* @param float x
* @param float y
* @param float z
* @param float o
* @param [TempSummonType] spawnType = MANUAL_DESPAWN : defines how and when the creature despawns
* @param uint32 despawnTimer = 0 : despawn time in milliseconds
* @return [Creature] spawnedCreature
*/
int SpawnCreature(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
uint32 entry = Eluna::CHECKVAL<uint32>(L, 2);
float x = Eluna::CHECKVAL<float>(L, 3);
float y = Eluna::CHECKVAL<float>(L, 4);
float z = Eluna::CHECKVAL<float>(L, 5);
float o = Eluna::CHECKVAL<float>(L, 6);
uint32 spawnType = Eluna::CHECKVAL<uint32>(L, 7, 8);
uint32 despawnTimer = Eluna::CHECKVAL<uint32>(L, 8, 0);
TempSummonType type;
switch (spawnType)
{
case 1:
type = TEMPSUMMON_TIMED_OR_DEAD_DESPAWN;
break;
case 2:
type = TEMPSUMMON_TIMED_OR_CORPSE_DESPAWN;
break;
case 3:
type = TEMPSUMMON_TIMED_DESPAWN;
break;
case 4:
#ifdef TRINITY
type = TEMPSUMMON_TIMED_DESPAWN_OUT_OF_COMBAT;
#else
type = TEMPSUMMON_TIMED_OOC_DESPAWN;
#endif
break;
case 5:
type = TEMPSUMMON_CORPSE_DESPAWN;
break;
case 6:
type = TEMPSUMMON_CORPSE_TIMED_DESPAWN;
break;
case 7:
type = TEMPSUMMON_DEAD_DESPAWN;
break;
case 8:
type = TEMPSUMMON_MANUAL_DESPAWN;
break;
#ifndef TRINITY
case 9:
type = TEMPSUMMON_TIMED_OOC_OR_CORPSE_DESPAWN;
break;
case 10:
type = TEMPSUMMON_TIMED_OOC_OR_DEAD_DESPAWN;
break;
#endif
default:
return luaL_argerror(L, 7, "valid SpawnType expected");
}
Eluna::Push(L, obj->SummonCreature(entry, x, y, z, o, type, despawnTimer));
return 1;
}
/**
* Registers a timed event to the [WorldObject]
* When the passed function is called, the parameters `(eventId, delay, repeats, worldobject)` are passed to it.
* Repeats will decrease on each call if the event does not repeat indefinitely
*
* Note that for [Creature] and [GameObject] the timed event timer ticks only if the creature is in sight of someone
* For all [WorldObject]s the timed events are removed when the object is destoryed. This means that for example a [Player]'s events are removed on logout.
*
* @param function function : function to trigger when the time has passed
* @param uint32 delay : set time in milliseconds for the event to trigger
* @param uint32 repeats : how many times for the event to repeat, 0 is infinite
* @return int eventId : unique ID for the timed event used to cancel it or nil
*/
int RegisterEvent(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
luaL_checktype(L, 2, LUA_TFUNCTION);
uint32 delay = Eluna::CHECKVAL<uint32>(L, 3);
uint32 repeats = Eluna::CHECKVAL<uint32>(L, 4);
lua_pushvalue(L, 2);
int functionRef = luaL_ref(L, LUA_REGISTRYINDEX);
if (functionRef != LUA_REFNIL && functionRef != LUA_NOREF)
{
obj->elunaEvents->AddEvent(functionRef, delay, repeats);
Eluna::Push(L, functionRef);
}
return 1;
}
/**
* Removes the timed event from a [WorldObject] by the specified event ID
*
* @param int eventId : event Id to remove
*/
int RemoveEventById(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
int eventId = Eluna::CHECKVAL<int>(L, 2);
obj->elunaEvents->SetState(eventId, LUAEVENT_STATE_ABORT);
return 0;
}
/**
* Removes all timed events from a [WorldObject]
*
*/
int RemoveEvents(Eluna* /*E*/, lua_State* /*L*/, WorldObject* obj)
{
obj->elunaEvents->SetStates(LUAEVENT_STATE_ABORT);
return 0;
}
/**
* Returns true if the given [WorldObject] or coordinates are in the [WorldObject]'s line of sight
*
* @proto isInLoS = (worldobject)
* @proto isInLoS = (x, y, z)
*
* @param [WorldObject] worldobject
* @param float x
* @param float y
* @param float z
* @return bool isInLoS
*/
int IsWithinLoS(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
WorldObject* target = Eluna::CHECKOBJ<WorldObject>(L, 2, false);
if (target)
Eluna::Push(L, obj->IsWithinLOSInMap(target));
else
{
float x = Eluna::CHECKVAL<float>(L, 2);
float y = Eluna::CHECKVAL<float>(L, 3);
float z = Eluna::CHECKVAL<float>(L, 4);
Eluna::Push(L, obj->IsWithinLOS(x, y, z));
}
return 1;
}
/**
* The [WorldObject] plays music to a [Player]
*
* If no [Player] provided it will play the music to everyone near.
* This method does not interrupt previously played music.
*
* See also [WorldObject:PlayDistanceSound], [WorldObject:PlayDirectSound]
*
* @param uint32 music : entry of a music
* @param [Player] player = nil : [Player] to play the music to
*/
int PlayMusic(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
uint32 musicid = Eluna::CHECKVAL<uint32>(L, 2);
Player* player = Eluna::CHECKOBJ<Player>(L, 3, false);
WorldPacket data(SMSG_PLAY_MUSIC, 4);
data << uint32(musicid);
if (player)
player->SendDirectMessage(&data);
else
obj->SendMessageToSet(&data, true);
return 0;
}
/**
* The [WorldObject] plays a sound to a [Player]
*
* If no [Player] provided it will play the sound to everyone near.
* This method will play sound and does not interrupt prvious sound.
*
* See also [WorldObject:PlayDistanceSound], [WorldObject:PlayMusic]
*
* @param uint32 sound : entry of a sound
* @param [Player] player = nil : [Player] to play the sound to
*/
int PlayDirectSound(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
uint32 soundId = Eluna::CHECKVAL<uint32>(L, 2);
Player* player = Eluna::CHECKOBJ<Player>(L, 3, false);
if (!sSoundEntriesStore.LookupEntry(soundId))
return 0;
if (player)
obj->PlayDirectSound(soundId, player);
else
obj->PlayDirectSound(soundId);
return 0;
}
/**
* The [WorldObject] plays a sound to a [Player]
*
* If no [Player] it will play the sound to everyone near.
* Sound will fade the further you are from the [WorldObject].
* This method interrupts previously playing sound.
*
* See also [WorldObject:PlayDirectSound], [WorldObject:PlayMusic]
*
* @param uint32 sound : entry of a sound
* @param [Player] player = nil : [Player] to play the sound to
*/
int PlayDistanceSound(Eluna* /*E*/, lua_State* L, WorldObject* obj)
{
uint32 soundId = Eluna::CHECKVAL<uint32>(L, 2);
Player* player = Eluna::CHECKOBJ<Player>(L, 3, false);
if (!sSoundEntriesStore.LookupEntry(soundId))
return 0;
if (player)
obj->PlayDistanceSound(soundId, player);
else
obj->PlayDistanceSound(soundId);
return 0;
}
};
#endif