Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Toggle main menu visibility
Loading...
Searching...
No Matches
CreatureAIImpl.h
Go to the documentation of this file.
1
/*
2
* This file is part of Project SkyFire https://www.projectskyfire.org.
3
* See LICENSE.md file for Copyright information
4
*/
5
6
#ifndef CREATUREAIIMPL_H
7
#define CREATUREAIIMPL_H
8
9
#include "
Common.h
"
10
#include "
CreatureAI.h
"
11
#include "
Define.h
"
12
#include "
SpellMgr.h
"
13
#include "
TemporarySummon.h
"
14
15
template
<
class
T>
16
inline
17
const
T&
RAND
(
const
T& v1,
const
T& v2)
18
{
19
return
(std::rand() % 1) ? v1 : v2;
20
}
21
22
template
<
class
T>
23
inline
24
const
T&
RAND
(
const
T& v1,
const
T& v2,
const
T& v3)
25
{
26
switch
(std::rand() % 2)
27
{
28
default
:
29
case
0:
return
v1;
30
case
1:
return
v2;
31
case
2:
return
v3;
32
}
33
}
34
35
template
<
class
T>
36
inline
37
const
T&
RAND
(
const
T& v1,
const
T& v2,
const
T& v3,
const
T& v4)
38
{
39
switch
(std::rand() % 3)
40
{
41
default
:
42
case
0:
return
v1;
43
case
1:
return
v2;
44
case
2:
return
v3;
45
case
3:
return
v4;
46
}
47
}
48
49
template
<
class
T>
50
inline
51
const
T&
RAND
(
const
T& v1,
const
T& v2,
const
T& v3,
const
T& v4,
const
T& v5)
52
{
53
switch
(std::rand() % 4)
54
{
55
default
:
56
case
0:
return
v1;
57
case
1:
return
v2;
58
case
2:
return
v3;
59
case
3:
return
v4;
60
case
4:
return
v5;
61
}
62
}
63
64
template
<
class
T>
65
inline
66
const
T&
RAND
(
const
T& v1,
const
T& v2,
const
T& v3,
const
T& v4,
const
T& v5,
const
T& v6)
67
{
68
switch
(std::rand() % 5)
69
{
70
default
:
71
case
0:
return
v1;
72
case
1:
return
v2;
73
case
2:
return
v3;
74
case
3:
return
v4;
75
case
4:
return
v5;
76
case
5:
return
v6;
77
}
78
}
79
80
template
<
class
T>
81
inline
82
const
T&
RAND
(
const
T& v1,
const
T& v2,
const
T& v3,
const
T& v4,
const
T& v5,
const
T& v6,
const
T& v7)
83
{
84
switch
(std::rand() % 6)
85
{
86
default
:
87
case
0:
return
v1;
88
case
1:
return
v2;
89
case
2:
return
v3;
90
case
3:
return
v4;
91
case
4:
return
v5;
92
case
5:
return
v6;
93
case
6:
return
v7;
94
}
95
}
96
97
template
<
class
T>
98
inline
99
const
T&
RAND
(
const
T& v1,
const
T& v2,
const
T& v3,
const
T& v4,
const
T& v5,
const
T& v6,
const
T& v7,
const
T& v8)
100
{
101
switch
(std::rand() % 7)
102
{
103
default
:
104
case
0:
return
v1;
105
case
1:
return
v2;
106
case
2:
return
v3;
107
case
3:
return
v4;
108
case
4:
return
v5;
109
case
5:
return
v6;
110
case
6:
return
v7;
111
case
7:
return
v8;
112
}
113
}
114
115
template
<
class
T>
116
inline
117
const
T&
RAND
(
const
T& v1,
const
T& v2,
const
T& v3,
const
T& v4,
const
T& v5,
const
T& v6,
const
T& v7,
const
T& v8,
118
const
T& v9)
119
{
120
switch
(std::rand() % 8)
121
{
122
default
:
123
case
0:
return
v1;
124
case
1:
return
v2;
125
case
2:
return
v3;
126
case
3:
return
v4;
127
case
4:
return
v5;
128
case
5:
return
v6;
129
case
6:
return
v7;
130
case
7:
return
v8;
131
case
8:
return
v9;
132
}
133
}
134
135
template
<
class
T>
136
inline
137
const
T&
RAND
(
const
T& v1,
const
T& v2,
const
T& v3,
const
T& v4,
const
T& v5,
const
T& v6,
const
T& v7,
const
T& v8,
138
const
T& v9,
const
T& v10)
139
{
140
switch
(std::rand() % 9)
141
{
142
default
:
143
case
0:
return
v1;
144
case
1:
return
v2;
145
case
2:
return
v3;
146
case
3:
return
v4;
147
case
4:
return
v5;
148
case
5:
return
v6;
149
case
6:
return
v7;
150
case
7:
return
v8;
151
case
8:
return
v9;
152
case
9:
return
v10;
153
}
154
}
155
156
template
<
class
T>
157
inline
158
const
T&
RAND
(
const
T& v1,
const
T& v2,
const
T& v3,
const
T& v4,
const
T& v5,
const
T& v6,
const
T& v7,
const
T& v8,
159
const
T& v9,
const
T& v10,
const
T& v11)
160
{
161
switch
(std::rand() % 10)
162
{
163
default
:
164
case
0:
return
v1;
165
case
1:
return
v2;
166
case
2:
return
v3;
167
case
3:
return
v4;
168
case
4:
return
v5;
169
case
5:
return
v6;
170
case
6:
return
v7;
171
case
7:
return
v8;
172
case
8:
return
v9;
173
case
9:
return
v10;
174
case
10:
return
v11;
175
}
176
}
177
178
template
<
class
T>
179
inline
180
const
T&
RAND
(
const
T& v1,
const
T& v2,
const
T& v3,
const
T& v4,
const
T& v5,
const
T& v6,
const
T& v7,
const
T& v8,
181
const
T& v9,
const
T& v10,
const
T& v11,
const
T& v12)
182
{
183
switch
(std::rand() % 11)
184
{
185
default
:
186
case
0:
return
v1;
187
case
1:
return
v2;
188
case
2:
return
v3;
189
case
3:
return
v4;
190
case
4:
return
v5;
191
case
5:
return
v6;
192
case
6:
return
v7;
193
case
7:
return
v8;
194
case
8:
return
v9;
195
case
9:
return
v10;
196
case
10:
return
v11;
197
case
11:
return
v12;
198
}
199
}
200
201
template
<
class
T>
202
inline
203
const
T&
RAND
(
const
T& v1,
const
T& v2,
const
T& v3,
const
T& v4,
const
T& v5,
const
T& v6,
const
T& v7,
const
T& v8,
204
const
T& v9,
const
T& v10,
const
T& v11,
const
T& v12,
const
T& v13)
205
{
206
switch
(std::rand() % 12)
207
{
208
default
:
209
case
0:
return
v1;
210
case
1:
return
v2;
211
case
2:
return
v3;
212
case
3:
return
v4;
213
case
4:
return
v5;
214
case
5:
return
v6;
215
case
6:
return
v7;
216
case
7:
return
v8;
217
case
8:
return
v9;
218
case
9:
return
v10;
219
case
10:
return
v11;
220
case
11:
return
v12;
221
case
12:
return
v13;
222
}
223
}
224
225
template
<
class
T>
226
inline
227
const
T&
RAND
(
const
T& v1,
const
T& v2,
const
T& v3,
const
T& v4,
const
T& v5,
const
T& v6,
const
T& v7,
const
T& v8,
228
const
T& v9,
const
T& v10,
const
T& v11,
const
T& v12,
const
T& v13,
const
T& v14)
229
{
230
switch
(std::rand() % 13)
231
{
232
default
:
233
case
0:
return
v1;
234
case
1:
return
v2;
235
case
2:
return
v3;
236
case
3:
return
v4;
237
case
4:
return
v5;
238
case
5:
return
v6;
239
case
6:
return
v7;
240
case
7:
return
v8;
241
case
8:
return
v9;
242
case
9:
return
v10;
243
case
10:
return
v11;
244
case
11:
return
v12;
245
case
12:
return
v13;
246
case
13:
return
v14;
247
}
248
}
249
250
template
<
class
T>
251
inline
252
const
T&
RAND
(
const
T& v1,
const
T& v2,
const
T& v3,
const
T& v4,
const
T& v5,
const
T& v6,
const
T& v7,
const
T& v8,
253
const
T& v9,
const
T& v10,
const
T& v11,
const
T& v12,
const
T& v13,
const
T& v14,
const
T& v15)
254
{
255
switch
(std::rand() % 14)
256
{
257
default
:
258
case
0:
return
v1;
259
case
1:
return
v2;
260
case
2:
return
v3;
261
case
3:
return
v4;
262
case
4:
return
v5;
263
case
5:
return
v6;
264
case
6:
return
v7;
265
case
7:
return
v8;
266
case
8:
return
v9;
267
case
9:
return
v10;
268
case
10:
return
v11;
269
case
11:
return
v12;
270
case
12:
return
v13;
271
case
13:
return
v14;
272
case
14:
return
v15;
273
}
274
}
275
276
template
<
class
T>
277
inline
278
const
T&
RAND
(
const
T& v1,
const
T& v2,
const
T& v3,
const
T& v4,
const
T& v5,
const
T& v6,
const
T& v7,
const
T& v8,
279
const
T& v9,
const
T& v10,
const
T& v11,
const
T& v12,
const
T& v13,
const
T& v14,
const
T& v15,
const
T& v16)
280
{
281
switch
(std::rand() % 15)
282
{
283
default
:
284
case
0:
return
v1;
285
case
1:
return
v2;
286
case
2:
return
v3;
287
case
3:
return
v4;
288
case
4:
return
v5;
289
case
5:
return
v6;
290
case
6:
return
v7;
291
case
7:
return
v8;
292
case
8:
return
v9;
293
case
9:
return
v10;
294
case
10:
return
v11;
295
case
11:
return
v12;
296
case
12:
return
v13;
297
case
13:
return
v14;
298
case
14:
return
v15;
299
case
15:
return
v16;
300
}
301
}
302
303
class
EventMap
304
{
316
typedef
std::multimap<uint32, uint32>
EventStore
;
317
318
public
:
319
EventMap
() :
_time
(0),
_phase
(0) { }
320
325
void
Reset
()
326
{
327
_eventMap
.clear();
328
_time
= 0;
329
_phase
= 0;
330
}
331
337
void
Update
(
uint32
time)
338
{
339
_time
+= time;
340
}
341
346
uint32
GetTimer
()
const
347
{
348
return
_time
;
349
}
350
355
uint8
GetPhaseMask
()
const
356
{
357
return
_phase
;
358
}
359
364
bool
Empty
()
const
365
{
366
return
_eventMap
.empty();
367
}
368
374
void
SetPhase
(
uint8
phase)
375
{
376
if
(!phase)
377
_phase
= 0;
378
else
if
(phase <= 8)
379
_phase
= (1 << (phase - 1));
380
}
381
387
void
AddPhase
(
uint8
phase)
388
{
389
if
(phase && phase <= 8)
390
_phase
|= (1 << (phase - 1));
391
}
392
398
void
RemovePhase
(
uint8
phase)
399
{
400
if
(phase && phase <= 8)
401
_phase
&= ~(1 << (phase - 1));
402
}
403
412
void
ScheduleEvent
(
uint32
eventId,
uint32
time,
uint32
group = 0,
uint8
phase = 0)
413
{
414
if
(group && group <= 8)
415
eventId |= (1 << (group + 15));
416
417
if
(phase && phase <= 8)
418
eventId |= (1 << (phase + 23));
419
420
_eventMap
.insert(EventStore::value_type(
_time
+ time, eventId));
421
}
422
431
void
RescheduleEvent
(
uint32
eventId,
uint32
time,
uint32
group = 0,
uint8
phase = 0)
432
{
433
CancelEvent
(eventId);
434
ScheduleEvent
(eventId, time, group, phase);
435
}
436
442
void
RepeatEvent
(
uint32
time)
443
{
444
if
(
Empty
())
445
return
;
446
447
uint32
eventId =
_eventMap
.begin()->second;
448
_eventMap
.erase(
_eventMap
.begin());
449
ScheduleEvent
(eventId, time);
450
}
451
456
void
PopEvent
()
457
{
458
if
(!
Empty
())
459
_eventMap
.erase(
_eventMap
.begin());
460
}
461
467
uint32
ExecuteEvent
()
468
{
469
while
(!
Empty
())
470
{
471
EventStore::iterator itr =
_eventMap
.begin();
472
473
if
(itr->first >
_time
)
474
return
0;
475
else
if
(
_phase
&& (itr->second & 0xFF000000) && !((itr->second >> 24) &
_phase
))
476
_eventMap
.erase(itr);
477
else
478
{
479
uint32
eventId = (itr->second & 0x0000FFFF);
480
_eventMap
.erase(itr);
481
return
eventId;
482
}
483
}
484
485
return
0;
486
}
487
493
uint32
GetEvent
()
494
{
495
while
(!
Empty
())
496
{
497
EventStore::iterator itr =
_eventMap
.begin();
498
499
if
(itr->first >
_time
)
500
return
0;
501
else
if
(
_phase
&& (itr->second & 0xFF000000) && !(itr->second & (
_phase
<< 24)))
502
_eventMap
.erase(itr);
503
else
504
return
(itr->second & 0x0000FFFF);
505
}
506
507
return
0;
508
}
509
515
void
DelayEvents
(
uint32
delay)
516
{
517
_time
= delay <
_time
?
_time
- delay : 0;
518
}
519
526
void
DelayEvents
(
uint32
delay,
uint32
group)
527
{
528
if
(!group || group > 8 ||
Empty
())
529
return
;
530
531
EventStore
delayed;
532
533
for
(EventStore::iterator itr =
_eventMap
.begin(); itr !=
_eventMap
.end();)
534
{
535
if
(itr->second & (1 << (group + 15)))
536
{
537
delayed.insert(EventStore::value_type(itr->first + delay, itr->second));
538
_eventMap
.erase(itr++);
539
}
540
else
541
++itr;
542
}
543
544
_eventMap
.insert(delayed.begin(), delayed.end());
545
}
546
552
void
CancelEvent
(
uint32
eventId)
553
{
554
if
(
Empty
())
555
return
;
556
557
for
(EventStore::iterator itr =
_eventMap
.begin(); itr !=
_eventMap
.end();)
558
{
559
if
(eventId == (itr->second & 0x0000FFFF))
560
_eventMap
.erase(itr++);
561
else
562
++itr;
563
}
564
}
565
571
void
CancelEventGroup
(
uint32
group)
572
{
573
if
(!group || group > 8 ||
Empty
())
574
return
;
575
576
for
(EventStore::iterator itr =
_eventMap
.begin(); itr !=
_eventMap
.end();)
577
{
578
if
(itr->second & (1 << (group + 15)))
579
_eventMap
.erase(itr++);
580
else
581
++itr;
582
}
583
}
584
591
uint32
GetNextEventTime
(
uint32
eventId)
const
592
{
593
if
(
Empty
())
594
return
0;
595
596
for
(EventStore::const_iterator itr =
_eventMap
.begin(); itr !=
_eventMap
.end(); ++itr)
597
if
(eventId == (itr->second & 0x0000FFFF))
598
return
itr->first;
599
600
return
0;
601
}
602
607
uint32
GetNextEventTime
()
const
608
{
609
return
Empty
() ? 0 :
_eventMap
.begin()->first;
610
}
611
618
bool
IsInPhase
(
uint8
phase)
const
619
{
620
return
phase <= 8 && (!phase ||
_phase
& (1 << (phase - 1)));
621
}
622
623
private
:
634
uint32
_time
;
635
644
uint8
_phase
;
645
653
EventStore
_eventMap
;
654
};
655
656
enum
AITarget
657
{
658
AITARGET_SELF
,
659
AITARGET_VICTIM
,
660
AITARGET_ENEMY
,
661
AITARGET_ALLY
,
662
AITARGET_BUFF
,
663
AITARGET_DEBUFF
664
};
665
666
enum
AICondition
667
{
668
AICOND_AGGRO
,
669
AICOND_COMBAT
,
670
AICOND_DIE
671
};
672
673
#define AI_DEFAULT_COOLDOWN 5000
674
675
struct
AISpellInfoType
676
{
677
AISpellInfoType
() :
target
(
AITARGET_SELF
),
condition
(
AICOND_COMBAT
),
678
cooldown
(
AI_DEFAULT_COOLDOWN
),
realCooldown
(0),
maxRange
(0.0f) { }
679
AITarget
target
;
680
AICondition
condition
;
681
uint32
cooldown
;
682
uint32
realCooldown
;
683
float
maxRange
;
684
};
685
686
AISpellInfoType
*
GetAISpellInfo
(
uint32
i);
687
688
#endif
Common.h
CreatureAI.h
AITarget
AITarget
Definition
CreatureAIImpl.h:657
AITARGET_ALLY
@ AITARGET_ALLY
Definition
CreatureAIImpl.h:661
AITARGET_BUFF
@ AITARGET_BUFF
Definition
CreatureAIImpl.h:662
AITARGET_SELF
@ AITARGET_SELF
Definition
CreatureAIImpl.h:658
AITARGET_VICTIM
@ AITARGET_VICTIM
Definition
CreatureAIImpl.h:659
AITARGET_ENEMY
@ AITARGET_ENEMY
Definition
CreatureAIImpl.h:660
AITARGET_DEBUFF
@ AITARGET_DEBUFF
Definition
CreatureAIImpl.h:663
GetAISpellInfo
AISpellInfoType * GetAISpellInfo(uint32 i)
Definition
CreatureAI.cpp:26
AI_DEFAULT_COOLDOWN
#define AI_DEFAULT_COOLDOWN
Definition
CreatureAIImpl.h:673
RAND
const T & RAND(const T &v1, const T &v2)
Definition
CreatureAIImpl.h:17
AICondition
AICondition
Definition
CreatureAIImpl.h:667
AICOND_COMBAT
@ AICOND_COMBAT
Definition
CreatureAIImpl.h:669
AICOND_AGGRO
@ AICOND_AGGRO
Definition
CreatureAIImpl.h:668
AICOND_DIE
@ AICOND_DIE
Definition
CreatureAIImpl.h:670
Define.h
uint8
std::uint8_t uint8
Definition
Define.h:79
uint32
std::uint32_t uint32
Definition
Define.h:77
SpellMgr.h
TemporarySummon.h
EventMap::AddPhase
void AddPhase(uint8 phase)
Definition
CreatureAIImpl.h:387
EventMap::ScheduleEvent
void ScheduleEvent(uint32 eventId, uint32 time, uint32 group=0, uint8 phase=0)
Definition
CreatureAIImpl.h:412
EventMap::ExecuteEvent
uint32 ExecuteEvent()
Definition
CreatureAIImpl.h:467
EventMap::EventMap
EventMap()
Definition
CreatureAIImpl.h:319
EventMap::_phase
uint8 _phase
Definition
CreatureAIImpl.h:644
EventMap::Update
void Update(uint32 time)
Definition
CreatureAIImpl.h:337
EventMap::Empty
bool Empty() const
Definition
CreatureAIImpl.h:364
EventMap::_eventMap
EventStore _eventMap
Definition
CreatureAIImpl.h:653
EventMap::GetTimer
uint32 GetTimer() const
Definition
CreatureAIImpl.h:346
EventMap::_time
uint32 _time
Definition
CreatureAIImpl.h:634
EventMap::DelayEvents
void DelayEvents(uint32 delay, uint32 group)
Definition
CreatureAIImpl.h:526
EventMap::CancelEventGroup
void CancelEventGroup(uint32 group)
Definition
CreatureAIImpl.h:571
EventMap::DelayEvents
void DelayEvents(uint32 delay)
Definition
CreatureAIImpl.h:515
EventMap::GetNextEventTime
uint32 GetNextEventTime() const
Definition
CreatureAIImpl.h:607
EventMap::IsInPhase
bool IsInPhase(uint8 phase) const
Definition
CreatureAIImpl.h:618
EventMap::RepeatEvent
void RepeatEvent(uint32 time)
Definition
CreatureAIImpl.h:442
EventMap::GetPhaseMask
uint8 GetPhaseMask() const
Definition
CreatureAIImpl.h:355
EventMap::PopEvent
void PopEvent()
Definition
CreatureAIImpl.h:456
EventMap::RemovePhase
void RemovePhase(uint8 phase)
Definition
CreatureAIImpl.h:398
EventMap::RescheduleEvent
void RescheduleEvent(uint32 eventId, uint32 time, uint32 group=0, uint8 phase=0)
Definition
CreatureAIImpl.h:431
EventMap::EventStore
std::multimap< uint32, uint32 > EventStore
Definition
CreatureAIImpl.h:316
EventMap::CancelEvent
void CancelEvent(uint32 eventId)
Definition
CreatureAIImpl.h:552
EventMap::SetPhase
void SetPhase(uint8 phase)
Definition
CreatureAIImpl.h:374
EventMap::GetNextEventTime
uint32 GetNextEventTime(uint32 eventId) const
Definition
CreatureAIImpl.h:591
EventMap::Reset
void Reset()
Definition
CreatureAIImpl.h:325
EventMap::GetEvent
uint32 GetEvent()
Definition
CreatureAIImpl.h:493
AISpellInfoType
Definition
CreatureAIImpl.h:676
AISpellInfoType::AISpellInfoType
AISpellInfoType()
Definition
CreatureAIImpl.h:677
AISpellInfoType::realCooldown
uint32 realCooldown
Definition
CreatureAIImpl.h:682
AISpellInfoType::cooldown
uint32 cooldown
Definition
CreatureAIImpl.h:681
AISpellInfoType::target
AITarget target
Definition
CreatureAIImpl.h:679
AISpellInfoType::maxRange
float maxRange
Definition
CreatureAIImpl.h:683
AISpellInfoType::condition
AICondition condition
Definition
CreatureAIImpl.h:680
src
server
game
AI
CreatureAIImpl.h
Generated on
for Project SkyFire Core by
1.17.0