Skip to content

Commit af0bfab

Browse files
author
NoFantasy
committed
[1887] Add new class ScriptedPetAI that can be used with certain pet types.
Add new class ScriptedPetAI that can be used with certain pet types. git-svn-id: https://scriptdev2.svn.sourceforge.net/svnroot/scriptdev2@1887 5f9c896b-1e26-0410-94da-f77f675e2462
1 parent df0c1f9 commit af0bfab

7 files changed

Lines changed: 204 additions & 0 deletions

File tree

Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ base/follower_ai.cpp \
3636
base/follower_ai.h \
3737
base/guard_ai.cpp \
3838
base/guard_ai.h \
39+
base/pet_ai.cpp \
40+
base/pet_ai.h \
3941
include/precompiled.cpp \
4042
include/precompiled.h \
4143
include/sc_creature.cpp \

VC100/100ScriptDev2.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@
213213
<ClCompile Include="..\base\escort_ai.cpp" />
214214
<ClCompile Include="..\base\follower_ai.cpp" />
215215
<ClCompile Include="..\base\guard_ai.cpp" />
216+
<ClCompile Include="..\base\pet_ai.cpp" />
216217
<ClCompile Include="..\scripts\battlegrounds\battleground.cpp" />
217218
<ClCompile Include="..\scripts\eastern_kingdoms\alterac_mountains.cpp" />
218219
<ClCompile Include="..\scripts\eastern_kingdoms\arathi_highlands.cpp" />
@@ -685,6 +686,7 @@
685686
<ClInclude Include="..\base\escort_ai.h" />
686687
<ClInclude Include="..\base\follower_ai.h" />
687688
<ClInclude Include="..\base\guard_ai.h" />
689+
<ClInclude Include="..\base\pet_ai.h" />
688690
<ClInclude Include="..\scripts\eastern_kingdoms\blackrock_depths\blackrock_depths.h" />
689691
<ClInclude Include="..\scripts\eastern_kingdoms\blackrock_spire\blackrock_spire.h" />
690692
<ClInclude Include="..\scripts\eastern_kingdoms\deadmines\deadmines.h" />

VC100/100ScriptDev2.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@
309309
<ClCompile Include="..\base\guard_ai.cpp">
310310
<Filter>base</Filter>
311311
</ClCompile>
312+
<ClCompile Include="..\base\pet_ai.cpp">
313+
<Filter>base</Filter>
314+
</ClCompile>
312315
<ClCompile Include="..\scripts\battlegrounds\battleground.cpp">
313316
<Filter>scripts\battlegrounds</Filter>
314317
</ClCompile>
@@ -1704,6 +1707,9 @@
17041707
<ClInclude Include="..\base\guard_ai.h">
17051708
<Filter>base</Filter>
17061709
</ClInclude>
1710+
<ClInclude Include="..\base\pet_ai.h">
1711+
<Filter>base</Filter>
1712+
</ClInclude>
17071713
<ClInclude Include="..\scripts\eastern_kingdoms\blackrock_depths\blackrock_depths.h">
17081714
<Filter>scripts\eastern_kingdoms\blackrock_depths</Filter>
17091715
</ClInclude>

VC80/80ScriptDev2.vcproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,14 @@
381381
RelativePath="..\base\guard_ai.h"
382382
>
383383
</File>
384+
<File
385+
RelativePath="..\base\pet_ai.cpp"
386+
>
387+
</File>
388+
<File
389+
RelativePath="..\base\pet_ai.h"
390+
>
391+
</File>
384392
</Filter>
385393
<Filter
386394
Name="scripts"

VC90/90ScriptDev2.vcproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,14 @@
380380
RelativePath="..\base\guard_ai.h"
381381
>
382382
</File>
383+
<File
384+
RelativePath="..\base\pet_ai.cpp"
385+
>
386+
</File>
387+
<File
388+
RelativePath="..\base\pet_ai.h"
389+
>
390+
</File>
383391
</Filter>
384392
<Filter
385393
Name="scripts"

base/pet_ai.cpp

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/* Copyright (C) 2006 - 2010 ScriptDev2 <https://scriptdev2.svn.sourceforge.net/>
2+
* This program is free software licensed under GPL version 2
3+
* Please see the included DOCS/LICENSE.TXT for more information */
4+
5+
/* ScriptData
6+
SDName: ScriptedPetAI
7+
SD%Complete: 50
8+
SDComment: Intended to be used with Guardian/Protector/Minipets. Little/no control over when pet enter/leave combat. Must be considered to be under development.
9+
SDCategory: Npc
10+
EndScriptData */
11+
12+
#include "precompiled.h"
13+
#include "pet_ai.h"
14+
15+
ScriptedPetAI::ScriptedPetAI(Creature* pCreature) : CreatureAI(pCreature)
16+
{}
17+
18+
void ScriptedPetAI::MoveInLineOfSight(Unit* pWho)
19+
{
20+
if (m_creature->getVictim())
21+
return;
22+
23+
if (!m_creature->GetCharmInfo() || !m_creature->GetCharmInfo()->HasReactState(REACT_AGGRESSIVE))
24+
return;
25+
26+
if (m_creature->CanInitiateAttack() && pWho->isTargetableForAttack() &&
27+
m_creature->IsHostileTo(pWho) && pWho->isInAccessablePlaceFor(m_creature))
28+
{
29+
if (!m_creature->CanFly() && m_creature->GetDistanceZ(pWho) > CREATURE_Z_ATTACK_RANGE)
30+
return;
31+
32+
if (m_creature->IsWithinDistInMap(pWho, m_creature->GetAttackDistance(pWho)) && m_creature->IsWithinLOSInMap(pWho))
33+
{
34+
pWho->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH);
35+
AttackStart(pWho);
36+
}
37+
}
38+
}
39+
40+
void ScriptedPetAI::AttackStart(Unit* pWho)
41+
{
42+
if (pWho && m_creature->Attack(pWho, true))
43+
m_creature->GetMotionMaster()->MoveChase(pWho);
44+
}
45+
46+
void ScriptedPetAI::AttackedBy(Unit* pAttacker)
47+
{
48+
if (m_creature->getVictim())
49+
return;
50+
51+
if (m_creature->GetCharmInfo() && !m_creature->GetCharmInfo()->HasReactState(REACT_PASSIVE) &&
52+
m_creature->canReachWithAttack(pAttacker))
53+
AttackStart(pAttacker);
54+
}
55+
56+
void ScriptedPetAI::ResetPetCombat()
57+
{
58+
Unit* pOwner = m_creature->GetCharmerOrOwner();
59+
60+
if (pOwner && m_creature->GetCharmInfo() && m_creature->GetCharmInfo()->HasCommandState(COMMAND_FOLLOW))
61+
{
62+
m_creature->GetMotionMaster()->MoveFollow(pOwner, PET_FOLLOW_DIST, PET_FOLLOW_ANGLE);
63+
}
64+
else
65+
{
66+
m_creature->GetMotionMaster()->Clear(false);
67+
m_creature->GetMotionMaster()->MoveIdle();
68+
}
69+
70+
m_creature->AttackStop();
71+
72+
debug_log("SD2: ScriptedPetAI reset pet combat and stop attack.");
73+
Reset();
74+
}
75+
76+
void ScriptedPetAI::DoMeleeAttackIfReady()
77+
{
78+
if (m_creature->isAttackReady())
79+
{
80+
if (m_creature->IsWithinDistInMap(m_creature->getVictim(), ATTACK_DISTANCE))
81+
{
82+
m_creature->AttackerStateUpdate(m_creature->getVictim());
83+
m_creature->resetAttackTimer();
84+
}
85+
}
86+
}
87+
88+
void ScriptedPetAI::UpdatePetAI(const uint32 uiDiff)
89+
{
90+
DoMeleeAttackIfReady();
91+
}
92+
93+
void ScriptedPetAI::UpdateAI(const uint32 uiDiff)
94+
{
95+
if (!m_creature->isAlive()) // should not be needed, isAlive is checked in mangos before calling UpdateAI
96+
return;
97+
98+
// UpdateAllies() is done in the generic PetAI in Mangos, but we can't do this from script side.
99+
// Unclear what side effects this has, but is something to be resolved from Mangos.
100+
101+
if (m_creature->getVictim()) // in combat
102+
{
103+
if (!m_creature->getVictim()->isTargetableForAttack())
104+
{
105+
// target no longer valid for pet, so either attack stops or new target are selected
106+
// doesn't normally reach this, because of how petAi is designed in Mangos. CombatStop
107+
// are called before this update diff, and then pet will already have no victim.
108+
ResetPetCombat();
109+
return;
110+
}
111+
112+
// update when in combat
113+
UpdatePetAI(uiDiff);
114+
}
115+
else if (m_creature->GetCharmInfo())
116+
{
117+
Unit* pOwner = m_creature->GetCharmerOrOwner();
118+
119+
if (!pOwner)
120+
return;
121+
122+
if (pOwner->isInCombat() && !m_creature->GetCharmInfo()->HasReactState(REACT_PASSIVE))
123+
{
124+
// Not correct in all cases.
125+
// When mob initiate attack by spell, pet should not start attack before spell landed.
126+
AttackStart(pOwner->getAttackerForHelper());
127+
}
128+
else if (m_creature->GetCharmInfo()->HasCommandState(COMMAND_FOLLOW))
129+
{
130+
// not following, so start follow
131+
if (!m_creature->hasUnitState(UNIT_STAT_FOLLOW))
132+
m_creature->GetMotionMaster()->MoveFollow(pOwner, PET_FOLLOW_DIST, PET_FOLLOW_ANGLE);
133+
134+
// update when not in combat
135+
UpdatePetOOCAI(uiDiff);
136+
}
137+
}
138+
}

base/pet_ai.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* Copyright (C) 2006 - 2010 ScriptDev2 <https://scriptdev2.svn.sourceforge.net/>
2+
* This program is free software licensed under GPL version 2
3+
* Please see the included DOCS/LICENSE.TXT for more information */
4+
5+
#ifndef SC_PET_H
6+
#define SC_PET_H
7+
8+
// Using CreatureAI for now. Might change later and use PetAI (need to export for dll first)
9+
class MANGOS_DLL_DECL ScriptedPetAI : public CreatureAI
10+
{
11+
public:
12+
explicit ScriptedPetAI(Creature* pCreature);
13+
~ScriptedPetAI() {}
14+
15+
void MoveInLineOfSight(Unit* /*pWho*/);
16+
17+
void AttackStart(Unit* /*pWho*/);
18+
19+
void AttackedBy(Unit* /*pAttacker*/);
20+
21+
void KilledUnit(Unit* /*pVictim*/) {}
22+
23+
void OwnerKilledUnit(Unit* /*pVictim*/) {}
24+
25+
void UpdateAI(const uint32 uiDiff);
26+
27+
// extras
28+
void DoMeleeAttackIfReady();
29+
30+
virtual void Reset() {}
31+
32+
virtual void UpdatePetAI(const uint32 uiDiff); // while in combat
33+
34+
virtual void UpdatePetOOCAI(const uint32 uiDiff) {} // when not in combat
35+
36+
protected:
37+
void ResetPetCombat();
38+
};
39+
40+
#endif

0 commit comments

Comments
 (0)