Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
mob_generic_creature.cpp
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/* ScriptData
7SDName: Generic_Creature
8SD%Complete: 80
9SDComment: Should be replaced with core based AI
10SDCategory: Creatures
11EndScriptData */
12
13#include "ScriptMgr.h"
14#include "ScriptedCreature.h"
15#include "PassiveAI.h"
16
17#define GENERIC_CREATURE_COOLDOWN 5000
18
20{
21public:
22 generic_creature() : CreatureScript("generic_creature") { }
23
25 {
26 generic_creatureAI(Creature* creature) : ScriptedAI(creature) { }
27
28 uint32 GlobalCooldown; //This variable acts like the global cooldown that players have (1.5 seconds)
29 uint32 BuffTimer; //This variable keeps track of buffs
31
33 {
35 BuffTimer = 0; //Rebuff as soon as we can
36 IsSelfRooted = false;
37 }
38
40 {
41 if (!me->IsWithinMeleeRange(who))
42 IsSelfRooted = true;
43 }
44
46 {
47 //Always decrease our global cooldown first
48 if (GlobalCooldown > diff)
49 GlobalCooldown -= diff;
50 else GlobalCooldown = 0;
51
52 //Buff timer (only buff when we are alive and not in combat
53 if (!me->IsInCombat() && me->IsAlive())
54 {
55 if (BuffTimer <= diff)
56 {
57 //Find a spell that targets friendly and applies an aura (these are generally buffs)
58 SpellInfo const* info = SelectSpell(me, 0, 0, SELECT_TARGET_ANY_FRIEND, 0, 0, 0, 0, SELECT_EFFECT_AURA);
59
60 if (info && !GlobalCooldown)
61 {
62 //Cast the buff spell
63 DoCastSpell(me, info);
64
65 //Set our global cooldown
67
68 //Set our timer to 10 minutes before rebuff
69 BuffTimer = 600000;
70 }//Try agian in 30 seconds
71 else BuffTimer = 30000;
72 } else BuffTimer -= diff;
73 }
74
75 //Return since we have no target
76 if (!UpdateVictim())
77 return;
78
79 //If we are within range melee the target
80 if (me->IsWithinMeleeRange(me->GetVictim()))
81 {
82 //Make sure our attack is ready and we arn't currently casting
83 if (me->isAttackReady() && !me->IsNonMeleeSpellCasted(false))
84 {
85 bool Healing = false;
86 SpellInfo const* info = NULL;
87
88 //Select a healing spell if less than 30% hp
89 if (HealthBelowPct(30))
91
92 //No healing spell available, select a hostile spell
93 if (info) Healing = true;
94 else info = SelectSpell(me->GetVictim(), 0, 0, SELECT_TARGET_ANY_ENEMY, 0, 0, 0, 0, SELECT_EFFECT_DONTCARE);
95
96 //50% chance if elite or higher, 20% chance if not, to replace our white hit with a spell
97 if (info && (rand() % (me->GetCreatureTemplate()->rank > 1 ? 2 : 5) == 0) && !GlobalCooldown)
98 {
99 //Cast the spell
100 if (Healing)DoCastSpell(me, info);
101 else DoCastSpell(me->GetVictim(), info);
102
103 //Set our global cooldown
105 }
106 else me->AttackerStateUpdate(me->GetVictim());
107
108 me->resetAttackTimer();
109 }
110 }
111 else
112 {
113 //Only run this code if we arn't already casting
114 if (!me->IsNonMeleeSpellCasted(false))
115 {
116 bool Healing = false;
117 SpellInfo const* info = NULL;
118
119 //Select a healing spell if less than 30% hp ONLY 33% of the time
120 if (HealthBelowPct(30) && rand() % 3 == 0)
122
123 //No healing spell available, See if we can cast a ranged spell (Range must be greater than ATTACK_DISTANCE)
124 if (info) Healing = true;
125 else info = SelectSpell(me->GetVictim(), 0, 0, SELECT_TARGET_ANY_ENEMY, 0, 0, NOMINAL_MELEE_RANGE, 0, SELECT_EFFECT_DONTCARE);
126
127 //Found a spell, check if we arn't on cooldown
128 if (info && !GlobalCooldown)
129 {
130 //If we are currently moving stop us and set the movement generator
131 if (!IsSelfRooted)
132 IsSelfRooted = true;
133
134 //Cast spell
135 if (Healing) DoCastSpell(me, info);
136 else DoCastSpell(me->GetVictim(), info);
137
138 //Set our global cooldown
140 }//If no spells available and we arn't moving run to target
141 else if (IsSelfRooted)
142 {
143 //Cancel our current spell and then allow movement agian
144 me->InterruptNonMeleeSpells(false);
145 IsSelfRooted = false;
146 }
147 }
148 }
149 }
150 };
151
153 {
154 return new generic_creatureAI(creature);
155 }
156};
157
159{
160public:
161 trigger_periodic() : CreatureScript("trigger_periodic") { }
162
164 {
166 {
167 spell = me->m_spells[0] ? sSpellMgr->GetSpellInfo(me->m_spells[0]) : NULL;
168 interval = me->GetAttackTime(WeaponAttackType::BASE_ATTACK);
169 timer = interval;
170 }
171
174
176 {
177 if (timer <= diff)
178 {
179 if (spell)
180 me->CastSpell(me, spell, true);
181 timer = interval;
182 }
183 else
184 timer -= diff;
185 }
186 };
187
189 {
190 return new trigger_periodicAI(creature);
191 }
192};
193
195{
196public:
197 trigger_death() : CreatureScript("trigger_death") { }
198
200 {
201 trigger_deathAI(Creature* creature) : NullCreatureAI(creature) { }
202 void JustDied(Unit* killer) OVERRIDE
203 {
204 if (me->m_spells[0])
205 me->CastSpell(killer, me->m_spells[0], true);
206 }
207 };
208
210 {
211 return new trigger_deathAI(creature);
212 }
213};
214
216{
217 //new generic_creature;
219 //new trigger_death;
220}
@ SELECT_TARGET_ANY_FRIEND
Definition CreatureAI.h:35
@ SELECT_TARGET_ANY_ENEMY
Definition CreatureAI.h:31
@ SELECT_EFFECT_DONTCARE
Definition CreatureAI.h:41
@ SELECT_EFFECT_AURA
Definition CreatureAI.h:44
@ SELECT_EFFECT_HEALING
Definition CreatureAI.h:43
std::uint32_t uint32
Definition Define.h:77
#define OVERRIDE
Definition Define.h:60
#define NOMINAL_MELEE_RANGE
Definition Object.h:32
#define sSpellMgr
Definition SpellMgr.h:744
bool UpdateVictim()
Creature *const me
Definition CreatureAI.h:56
CreatureScript(const char *name)
NullCreatureAI(Creature *c)
Definition PassiveAI.cpp:12
Definition Unit.h:1367
CreatureAI * GetAI(Creature *creature) const OVERRIDE
CreatureAI * GetAI(Creature *creature) const OVERRIDE
CreatureAI * GetAI(Creature *creature) const OVERRIDE
void AddSC_generic_creature()
#define GENERIC_CREATURE_COOLDOWN
Creature * me
bool HealthBelowPct(uint32 pct) const
SpellInfo const * SelectSpell(Unit *target, uint32 school, uint32 mechanic, SelectTargetType targets, uint32 powerCostMin, uint32 powerCostMax, float rangeMin, float rangeMax, SelectEffect effect)
void DoCastSpell(Unit *target, SpellInfo const *spellInfo, bool triggered=false)
ScriptedAI(Creature *creature)
void UpdateAI(uint32 diff) OVERRIDE
== Triggered Actions Requested ==================
void JustDied(Unit *killer) OVERRIDE
void UpdateAI(uint32 diff) OVERRIDE
== Triggered Actions Requested ==================