-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathmob_generic_creature.cpp
More file actions
174 lines (146 loc) · 6.34 KB
/
mob_generic_creature.cpp
File metadata and controls
174 lines (146 loc) · 6.34 KB
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
/* This file is part of the ScriptDev2 Project. See AUTHORS file for Copyright information
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* ScriptData
SDName: Generic_Creature
SD%Complete: 80
SDComment: Should be replaced with core based AI
SDCategory: Creatures
EndScriptData */
#include "precompiled.h"
#define GENERIC_CREATURE_COOLDOWN 5000
struct generic_creatureAI : public ScriptedAI
{
generic_creatureAI(Creature* pCreature) : ScriptedAI(pCreature) {Reset();}
uint32 GlobalCooldown; // This variable acts like the global cooldown that players have (1.5 seconds)
uint32 BuffTimer; // This variable keeps track of buffs
bool IsSelfRooted;
void Reset() override
{
GlobalCooldown = 0;
BuffTimer = 0; // Rebuff as soon as we can
IsSelfRooted = false;
}
void Aggro(Unit* who) override
{
if (!m_creature->CanReachWithMeleeAttack(who))
{
IsSelfRooted = true;
}
}
void UpdateAI(const uint32 diff) override
{
// Always decrease our global cooldown first
if (GlobalCooldown > diff)
GlobalCooldown -= diff;
else GlobalCooldown = 0;
// Buff timer (only buff when we are alive and not in combat
if (!m_creature->isInCombat() && m_creature->isAlive())
{
if (BuffTimer < diff)
{
// Find a spell that targets friendly and applies an aura (these are generally buffs)
SpellEntry const* info = SelectSpell(m_creature, -1, -1, SELECT_TARGET_ANY_FRIEND, 0, 0, 0, 0, SELECT_EFFECT_AURA);
if (info && !GlobalCooldown)
{
// Cast the buff spell
DoCastSpell(m_creature, info);
// Set our global cooldown
GlobalCooldown = GENERIC_CREATURE_COOLDOWN;
// Set our timer to 10 minutes before rebuff
BuffTimer = 600000;
}// Try agian in 30 seconds
else BuffTimer = 30000;
}
else BuffTimer -= diff;
}
// Return since we have no target
if (!m_creature->SelectHostileTarget() || !m_creature->getVictim())
return;
// Return if we already cast a spell
if (m_creature->IsNonMeleeSpellCasted(false))
return;
// If we are within range melee the target
if (m_creature->CanReachWithMeleeAttack(m_creature->getVictim()))
{
// Make sure our attack is ready
if (m_creature->isAttackReady())
{
bool Healing = false;
SpellEntry const* info = NULL;
// Select a healing spell if less than 30% hp
if (m_creature->GetHealthPercent() < 30.0f)
info = SelectSpell(m_creature, -1, -1, SELECT_TARGET_ANY_FRIEND, 0, 0, 0, 0, SELECT_EFFECT_HEALING);
// No healing spell available, select a hostile spell
if (info) Healing = true;
else info = SelectSpell(m_creature->getVictim(), -1, -1, SELECT_TARGET_ANY_ENEMY, 0, 0, 0, 0, SELECT_EFFECT_DONTCARE);
// 50% chance if elite or higher, 20% chance if not, to replace our white hit with a spell
if (info && (rand() % (m_creature->GetCreatureInfo()->Rank > 1 ? 2 : 5) == 0) && !GlobalCooldown)
{
// Cast the spell
if (Healing)DoCastSpell(m_creature, info);
else DoCastSpell(m_creature->getVictim(), info);
// Set our global cooldown
GlobalCooldown = GENERIC_CREATURE_COOLDOWN;
}
else m_creature->AttackerStateUpdate(m_creature->getVictim());
m_creature->resetAttackTimer();
}
}
else
{
bool Healing = false;
SpellEntry const* info = NULL;
// Select a healing spell if less than 30% hp ONLY 33% of the time
if (m_creature->GetHealthPercent() < 30.0f && !urand(0, 2))
info = SelectSpell(m_creature, -1, -1, SELECT_TARGET_ANY_FRIEND, 0, 0, 0, 0, SELECT_EFFECT_HEALING);
// No healing spell available, See if we can cast a ranged spell (Range must be greater than ATTACK_DISTANCE)
if (info) Healing = true;
else info = SelectSpell(m_creature->getVictim(), -1, -1, SELECT_TARGET_ANY_ENEMY, 0, 0, ATTACK_DISTANCE, 0, SELECT_EFFECT_DONTCARE);
// Found a spell, check if we arn't on cooldown
if (info && !GlobalCooldown)
{
// If we are currently moving stop us and set the movement generator
if (!IsSelfRooted)
{
IsSelfRooted = true;
}
// Cast spell
if (Healing) DoCastSpell(m_creature, info);
else DoCastSpell(m_creature->getVictim(), info);
// Set our global cooldown
GlobalCooldown = GENERIC_CREATURE_COOLDOWN;
}// If no spells available and we arn't moving run to target
else if (IsSelfRooted)
{
// Cancel our current spell and then allow movement agian
m_creature->InterruptNonMeleeSpells(false);
IsSelfRooted = false;
}
}
}
};
CreatureAI* GetAI_generic_creature(Creature* pCreature)
{
return new generic_creatureAI(pCreature);
}
void AddSC_generic_creature()
{
Script* pNewScript;
pNewScript = new Script;
pNewScript->Name = "generic_creature";
pNewScript->GetAI = &GetAI_generic_creature;
pNewScript->RegisterSelf(false);
}