[8583] Prevent crash at casting DK spells by non-DK characters.
[getmangos.git] / src / bindings / universal / ScriptMgr.cpp
blobb27111639c5a83a67417caf976dc386518adc657
1 /*
2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "config.h"
20 #include "ScriptMgr.h"
21 #include "../../game/GossipDef.h"
22 #include "../../game/GameObject.h"
23 #include "../../game/Player.h"
24 #include "../../game/Map.h"
25 #include "../../game/ObjectMgr.h"
27 //uint8 loglevel = 0;
28 int nrscripts;
29 Script *m_scripts[MAX_SCRIPTS];
31 // -- Scripts to be added --
32 extern void AddSC_default();
33 // -------------------
35 MANGOS_DLL_EXPORT
36 void ScriptsFree()
37 { // Free resources before library unload
38 for(int i=0;i<nrscripts;i++)
39 delete m_scripts[i];
41 nrscripts = 0;
44 MANGOS_DLL_EXPORT
45 void ScriptsInit()
47 nrscripts = GetScriptNames().size();
48 for(int i=0;i<MAX_SCRIPTS;i++)
50 m_scripts[i]=NULL;
53 // -- Inicialize the Scripts to be Added --
54 AddSC_default();
55 // ----------------------------------------
59 MANGOS_DLL_EXPORT
60 char const* ScriptsVersion()
62 return "Default MaNGOS scripting library";
65 void Script::registerSelf()
67 int id = GetScriptId(Name.c_str());
68 if(id != 0) m_scripts[id] = this;
71 MANGOS_DLL_EXPORT
72 bool GossipHello ( Player * player, Creature *_Creature )
74 Script *tmpscript = m_scripts[_Creature->GetScriptId()];
75 if(!tmpscript || !tmpscript->pGossipHello) return false;
77 player->PlayerTalkClass->ClearMenus();
78 return tmpscript->pGossipHello(player,_Creature);
81 MANGOS_DLL_EXPORT
82 bool GossipSelect( Player *player, Creature *_Creature,uint32 sender, uint32 action )
84 debug_log("DEBUG: Gossip selection, sender: %d, action: %d",sender, action);
86 Script *tmpscript = m_scripts[_Creature->GetScriptId()];
87 if(!tmpscript || !tmpscript->pGossipSelect) return false;
89 player->PlayerTalkClass->ClearMenus();
90 return tmpscript->pGossipSelect(player,_Creature,sender,action);
93 MANGOS_DLL_EXPORT
94 bool GossipSelectWithCode( Player *player, Creature *_Creature, uint32 sender, uint32 action, const char* sCode )
96 debug_log("DEBUG: Gossip selection, sender: %d, action: %d",sender, action);
98 Script *tmpscript = m_scripts[_Creature->GetScriptId()];
99 if(!tmpscript || !tmpscript->pGossipSelectWithCode) return false;
101 player->PlayerTalkClass->ClearMenus();
102 return tmpscript->pGossipSelectWithCode(player,_Creature,sender,action,sCode);
105 MANGOS_DLL_EXPORT
106 bool QuestAccept( Player *player, Creature *_Creature, Quest *_Quest )
108 Script *tmpscript = m_scripts[_Creature->GetScriptId()];
109 if(!tmpscript || !tmpscript->pQuestAccept) return false;
111 player->PlayerTalkClass->ClearMenus();
112 return tmpscript->pQuestAccept(player,_Creature,_Quest);
115 MANGOS_DLL_EXPORT
116 bool QuestSelect( Player *player, Creature *_Creature, Quest *_Quest )
118 Script *tmpscript = m_scripts[_Creature->GetScriptId()];
119 if(!tmpscript || !tmpscript->pQuestSelect) return false;
121 player->PlayerTalkClass->ClearMenus();
122 return tmpscript->pQuestSelect(player,_Creature,_Quest);
125 MANGOS_DLL_EXPORT
126 bool QuestComplete( Player *player, Creature *_Creature, Quest *_Quest )
128 Script *tmpscript = m_scripts[_Creature->GetScriptId()];
129 if(!tmpscript || !tmpscript->pQuestComplete) return false;
131 player->PlayerTalkClass->ClearMenus();
132 return tmpscript->pQuestComplete(player,_Creature,_Quest);
135 MANGOS_DLL_EXPORT
136 bool ChooseReward( Player *player, Creature *_Creature, Quest *_Quest, uint32 opt )
138 Script *tmpscript = m_scripts[_Creature->GetScriptId()];
139 if(!tmpscript || !tmpscript->pChooseReward) return false;
141 player->PlayerTalkClass->ClearMenus();
142 return tmpscript->pChooseReward(player,_Creature,_Quest,opt);
145 MANGOS_DLL_EXPORT
146 uint32 NPCDialogStatus( Player *player, Creature *_Creature )
148 Script *tmpscript = m_scripts[_Creature->GetScriptId()];
149 if(!tmpscript || !tmpscript->pNPCDialogStatus) return 100;
151 player->PlayerTalkClass->ClearMenus();
152 return tmpscript->pNPCDialogStatus(player,_Creature);
155 MANGOS_DLL_EXPORT
156 uint32 GODialogStatus( Player *player, GameObject *_GO )
158 Script *tmpscript = NULL;
160 tmpscript = m_scripts[_GO->GetGOInfo()->ScriptId];
161 if(!tmpscript || !tmpscript->pGODialogStatus) return 100;
163 player->PlayerTalkClass->ClearMenus();
164 return tmpscript->pGODialogStatus(player,_GO);
167 MANGOS_DLL_EXPORT
168 bool ItemHello( Player *player, Item *_Item, Quest *_Quest )
170 Script *tmpscript = NULL;
172 tmpscript = m_scripts[_Item->GetProto()->ScriptId];
173 if(!tmpscript || !tmpscript->pItemHello) return false;
175 player->PlayerTalkClass->ClearMenus();
176 return tmpscript->pItemHello(player,_Item,_Quest);
179 MANGOS_DLL_EXPORT
180 bool ItemQuestAccept( Player *player, Item *_Item, Quest *_Quest )
182 Script *tmpscript = NULL;
184 tmpscript = m_scripts[_Item->GetProto()->ScriptId];
185 if(!tmpscript || !tmpscript->pItemQuestAccept) return false;
187 player->PlayerTalkClass->ClearMenus();
188 return tmpscript->pItemQuestAccept(player,_Item,_Quest);
191 MANGOS_DLL_EXPORT
192 bool GOHello( Player *player, GameObject *_GO )
194 Script *tmpscript = NULL;
196 tmpscript = m_scripts[_GO->GetGOInfo()->ScriptId];
197 if(!tmpscript || !tmpscript->pGOHello) return false;
199 player->PlayerTalkClass->ClearMenus();
200 return tmpscript->pGOHello(player,_GO);
203 MANGOS_DLL_EXPORT
204 bool GOQuestAccept( Player *player, GameObject *_GO, Quest *_Quest )
206 Script *tmpscript = NULL;
208 tmpscript = m_scripts[_GO->GetGOInfo()->ScriptId];
209 if(!tmpscript || !tmpscript->pGOQuestAccept) return false;
211 player->PlayerTalkClass->ClearMenus();
212 return tmpscript->pGOQuestAccept(player,_GO,_Quest);
215 MANGOS_DLL_EXPORT
216 bool GOChooseReward( Player *player, GameObject *_GO, Quest *_Quest, uint32 opt )
218 Script *tmpscript = NULL;
220 tmpscript = m_scripts[_GO->GetGOInfo()->ScriptId];
221 if(!tmpscript || !tmpscript->pGOChooseReward) return false;
223 player->PlayerTalkClass->ClearMenus();
224 return tmpscript->pGOChooseReward(player,_GO,_Quest,opt);
227 MANGOS_DLL_EXPORT
228 bool AreaTrigger ( Player *player, AreaTriggerEntry* atEntry )
230 Script *tmpscript = NULL;
232 tmpscript = m_scripts[GetAreaTriggerScriptId(atEntry->id)];
233 if(!tmpscript || !tmpscript->pAreaTrigger) return false;
235 return tmpscript->pAreaTrigger(player, atEntry);
238 MANGOS_DLL_EXPORT
239 bool ItemUse( Player *player, Item* _Item, SpellCastTargets const& targets)
241 Script *tmpscript = NULL;
243 tmpscript = m_scripts[_Item->GetProto()->ScriptId];
244 if(!tmpscript || !tmpscript->pItemUse) return false;
246 return tmpscript->pItemUse(player,_Item,targets);
249 MANGOS_DLL_EXPORT
250 CreatureAI* GetAI(Creature *_Creature )
252 Script *tmpscript = m_scripts[_Creature->GetScriptId()];
253 if(!tmpscript || !tmpscript->GetAI) return NULL;
255 return tmpscript->GetAI(_Creature);
258 MANGOS_DLL_EXPORT
259 InstanceData* CreateInstanceData(Map *map)
261 if(!map->IsDungeon()) return NULL;
262 Script *tmpscript = m_scripts[((InstanceMap*)map)->GetScriptId()];
263 if(!tmpscript || !tmpscript->GetInstanceData) return NULL;
265 return tmpscript->GetInstanceData(map);
268 MANGOS_DLL_EXPORT
269 bool EffectDummyGameObj(Unit *caster, uint32 spellId, uint32 effIndex, GameObject *gameObjTarget )
271 Script *tmpscript = m_scripts[gameObjTarget->GetGOInfo()->ScriptId];
273 if (!tmpscript || !tmpscript->pEffectDummyGameObj) return false;
275 return tmpscript->pEffectDummyGameObj(caster, spellId,effIndex,gameObjTarget);
278 MANGOS_DLL_EXPORT
279 bool EffectDummyCreature(Unit *caster, uint32 spellId, uint32 effIndex, Creature *crTarget )
281 Script *tmpscript = m_scripts[crTarget->GetScriptId()];
283 if (!tmpscript || !tmpscript->pEffectDummyCreature) return false;
285 return tmpscript->pEffectDummyCreature(caster, spellId,effIndex,crTarget);
288 MANGOS_DLL_EXPORT
289 bool EffectDummyItem(Unit *caster, uint32 spellId, uint32 effIndex, Item *itemTarget )
291 Script *tmpscript = m_scripts[itemTarget->GetProto()->ScriptId];
293 if (!tmpscript || !tmpscript->pEffectDummyItem) return false;
295 return tmpscript->pEffectDummyItem(caster, spellId,effIndex,itemTarget);
298 void ScriptedAI::UpdateAI(const uint32)
300 //Check if we have a current target
301 if( m_creature->isAlive() && m_creature->SelectHostilTarget() && m_creature->getVictim())
303 //If we are within range melee the target
304 if( m_creature->IsWithinDistInMap(m_creature->getVictim(), ATTACK_DISTANCE))
306 if( m_creature->isAttackReady() )
308 m_creature->AttackerStateUpdate(m_creature->getVictim());
309 m_creature->resetAttackTimer();
315 void ScriptedAI::EnterEvadeMode()
317 m_creature->CombatStop(true);
318 if( m_creature->isAlive() )
319 DoGoHome();
322 void ScriptedAI::DoStartAttack(Unit* victim)
324 if( m_creature->Attack(victim, true) )
325 m_creature->GetMotionMaster()->MoveChase(victim);
328 void ScriptedAI::DoStopAttack()
330 if( m_creature->getVictim() != NULL )
332 m_creature->AttackStop();
336 void ScriptedAI::DoGoHome()
338 if( !m_creature->getVictim() && m_creature->isAlive() )
339 m_creature->GetMotionMaster()->MoveTargetedHome();