Updated Copyright year to 2013
[getmangos.git] / src / bindings / universal / ScriptMgr.cpp
blob2b5b696e265432fb546dfdee4692e31d3f045ae4
1 /*
2 * Copyright (C) 2005-2013 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"
26 #include "../../game/ScriptMgr.h"
27 #include "../../game/SpellAuras.h"
29 // uint8 loglevel = 0;
30 int nrscripts;
31 Script* m_scripts[MAX_SCRIPTS];
33 // -- Scripts to be added --
34 extern void AddSC_default();
35 // -------------------
37 MANGOS_DLL_EXPORT
38 void ScriptsFree()
40 // Free resources before library unload
41 for (int i = 0; i < nrscripts; ++i)
42 delete m_scripts[i];
44 nrscripts = 0;
47 MANGOS_DLL_EXPORT
48 void ScriptsInit()
50 nrscripts = GetScriptNames().size();
51 for (int i = 0; i < MAX_SCRIPTS; ++i)
52 m_scripts[i] = NULL;
54 // -- Inicialize the Scripts to be Added --
55 AddSC_default();
56 // ----------------------------------------
60 MANGOS_DLL_EXPORT
61 char const* ScriptsVersion()
63 return "Default MaNGOS scripting library";
66 void Script::registerSelf()
68 if (int id = GetScriptId(Name.c_str()))
69 m_scripts[id] = this;
72 MANGOS_DLL_EXPORT
73 bool GossipHello(Player* player, Creature* _Creature)
75 Script* tmpscript = m_scripts[_Creature->GetScriptId()];
76 if (!tmpscript || !tmpscript->pGossipHello)
77 return false;
79 player->PlayerTalkClass->ClearMenus();
81 return tmpscript->pGossipHello(player, _Creature);
84 MANGOS_DLL_EXPORT
85 bool GOGossipHello(Player* pPlayer, GameObject* pGo)
87 Script* tmpscript = m_scripts[pGo->GetGOInfo()->ScriptId];
88 if (!tmpscript || !tmpscript->pGOGossipHello)
89 return false;
91 pPlayer->PlayerTalkClass->ClearMenus();
93 return tmpscript->pGOGossipHello(pPlayer, pGo);
96 MANGOS_DLL_EXPORT
97 bool GossipSelect(Player* player, Creature* _Creature, uint32 sender, uint32 action)
99 debug_log("DEBUG: Gossip selection, sender: %d, action: %d", sender, action);
101 Script* tmpscript = m_scripts[_Creature->GetScriptId()];
102 if (!tmpscript || !tmpscript->pGossipSelect)
103 return false;
105 player->PlayerTalkClass->ClearMenus();
107 return tmpscript->pGossipSelect(player, _Creature, sender, action);
110 MANGOS_DLL_EXPORT
111 bool GOGossipSelect(Player* pPlayer, GameObject* pGo, uint32 sender, uint32 action)
113 debug_log("DEBUG: GO Gossip selection, sender: %u, action: %u", sender, action);
115 Script* tmpscript = m_scripts[pGo->GetGOInfo()->ScriptId];
116 if (!tmpscript || !tmpscript->pGOGossipSelect)
117 return false;
119 pPlayer->PlayerTalkClass->ClearMenus();
121 return tmpscript->pGOGossipSelect(pPlayer, pGo, sender, action);
124 MANGOS_DLL_EXPORT
125 bool GossipSelectWithCode(Player* player, Creature* _Creature, uint32 sender, uint32 action, const char* sCode)
127 debug_log("DEBUG: Gossip selection, sender: %d, action: %d", sender, action);
129 Script* tmpscript = m_scripts[_Creature->GetScriptId()];
130 if (!tmpscript || !tmpscript->pGossipSelectWithCode)
131 return false;
133 player->PlayerTalkClass->ClearMenus();
135 return tmpscript->pGossipSelectWithCode(player, _Creature, sender, action, sCode);
138 MANGOS_DLL_EXPORT
139 bool GOGossipSelectWithCode(Player* pPlayer, GameObject* pGo, uint32 sender, uint32 action, const char* sCode)
141 debug_log("DEBUG: GO Gossip selection, sender: %u, action: %u", sender, action);
143 Script* tmpscript = m_scripts[pGo->GetGOInfo()->ScriptId];
144 if (!tmpscript || !tmpscript->pGOGossipSelectWithCode)
145 return false;
147 pPlayer->PlayerTalkClass->ClearMenus();
149 return tmpscript->pGOGossipSelectWithCode(pPlayer, pGo, sender, action, sCode);
152 MANGOS_DLL_EXPORT
153 bool QuestAccept(Player* player, Creature* _Creature, Quest* _Quest)
155 Script* tmpscript = m_scripts[_Creature->GetScriptId()];
156 if (!tmpscript || !tmpscript->pQuestAccept)
157 return false;
159 player->PlayerTalkClass->ClearMenus();
161 return tmpscript->pQuestAccept(player, _Creature, _Quest);
164 MANGOS_DLL_EXPORT
165 bool QuestSelect(Player* player, Creature* _Creature, Quest* _Quest)
167 Script* tmpscript = m_scripts[_Creature->GetScriptId()];
168 if (!tmpscript || !tmpscript->pQuestSelect)
169 return false;
171 player->PlayerTalkClass->ClearMenus();
173 return tmpscript->pQuestSelect(player, _Creature, _Quest);
176 MANGOS_DLL_EXPORT
177 bool QuestComplete(Player* player, Creature* _Creature, Quest* _Quest)
179 Script* tmpscript = m_scripts[_Creature->GetScriptId()];
180 if (!tmpscript || !tmpscript->pQuestComplete)
181 return false;
183 player->PlayerTalkClass->ClearMenus();
185 return tmpscript->pQuestComplete(player, _Creature, _Quest);
188 MANGOS_DLL_EXPORT
189 bool ChooseReward(Player* player, Creature* _Creature, Quest* _Quest, uint32 opt)
191 Script* tmpscript = m_scripts[_Creature->GetScriptId()];
192 if (!tmpscript || !tmpscript->pChooseReward)
193 return false;
195 player->PlayerTalkClass->ClearMenus();
197 return tmpscript->pChooseReward(player, _Creature, _Quest, opt);
200 MANGOS_DLL_EXPORT
201 uint32 NPCDialogStatus(Player* player, Creature* _Creature)
203 Script* tmpscript = m_scripts[_Creature->GetScriptId()];
204 if (!tmpscript || !tmpscript->pNPCDialogStatus)
205 return 100;
207 player->PlayerTalkClass->ClearMenus();
209 return tmpscript->pNPCDialogStatus(player, _Creature);
212 MANGOS_DLL_EXPORT
213 uint32 GODialogStatus(Player* player, GameObject* _GO)
215 Script* tmpscript = m_scripts[_GO->GetGOInfo()->ScriptId];
216 if (!tmpscript || !tmpscript->pGODialogStatus)
217 return 100;
219 player->PlayerTalkClass->ClearMenus();
221 return tmpscript->pGODialogStatus(player, _GO);
224 MANGOS_DLL_EXPORT
225 bool ItemHello(Player* player, Item* _Item, Quest* _Quest)
227 Script* tmpscript = m_scripts[_Item->GetProto()->ScriptId];
228 if (!tmpscript || !tmpscript->pItemHello)
229 return false;
231 player->PlayerTalkClass->ClearMenus();
233 return tmpscript->pItemHello(player, _Item, _Quest);
236 MANGOS_DLL_EXPORT
237 bool ItemQuestAccept(Player* player, Item* _Item, Quest* _Quest)
239 Script* tmpscript = m_scripts[_Item->GetProto()->ScriptId];
240 if (!tmpscript || !tmpscript->pItemQuestAccept)
241 return false;
243 player->PlayerTalkClass->ClearMenus();
245 return tmpscript->pItemQuestAccept(player, _Item, _Quest);
248 MANGOS_DLL_EXPORT
249 bool GOHello(Player* player, GameObject* _GO)
251 Script* tmpscript = m_scripts[_GO->GetGOInfo()->ScriptId];
252 if (!tmpscript || !tmpscript->pGOHello)
253 return false;
255 player->PlayerTalkClass->ClearMenus();
257 return tmpscript->pGOHello(player, _GO);
260 MANGOS_DLL_EXPORT
261 bool GOQuestAccept(Player* player, GameObject* _GO, Quest* _Quest)
263 Script* tmpscript = m_scripts[_GO->GetGOInfo()->ScriptId];
264 if (!tmpscript || !tmpscript->pGOQuestAccept)
265 return false;
267 player->PlayerTalkClass->ClearMenus();
269 return tmpscript->pGOQuestAccept(player, _GO, _Quest);
272 MANGOS_DLL_EXPORT
273 bool GOChooseReward(Player* player, GameObject* _GO, Quest* _Quest, uint32 opt)
275 Script* tmpscript = m_scripts[_GO->GetGOInfo()->ScriptId];
276 if (!tmpscript || !tmpscript->pGOChooseReward)
277 return false;
279 player->PlayerTalkClass->ClearMenus();
280 return tmpscript->pGOChooseReward(player, _GO, _Quest, opt);
283 MANGOS_DLL_EXPORT
284 bool AreaTrigger(Player* player, AreaTriggerEntry const* atEntry)
286 Script* tmpscript = m_scripts[GetAreaTriggerScriptId(atEntry->id)];
287 if (!tmpscript || !tmpscript->pAreaTrigger)
288 return false;
290 return tmpscript->pAreaTrigger(player, atEntry);
293 MANGOS_DLL_EXPORT
294 bool ProcessEventId(uint32 eventId, Object* source, Object* target, bool isStart)
296 Script* tmpscript = m_scripts[GetEventIdScriptId(eventId)];
297 if (!tmpscript || !tmpscript->pProcessEventId)
298 return false;
300 // isStart are normally true. For taxi event id at arrival, it's false
301 return tmpscript->pProcessEventId(eventId, source, target, isStart);
304 MANGOS_DLL_EXPORT
305 bool ItemUse(Player* player, Item* _Item, SpellCastTargets const& targets)
307 Script* tmpscript = m_scripts[_Item->GetProto()->ScriptId];
308 if (!tmpscript || !tmpscript->pItemUse)
309 return false;
311 return tmpscript->pItemUse(player, _Item, targets);
314 MANGOS_DLL_EXPORT
315 CreatureAI* GetAI(Creature* _Creature)
317 Script* tmpscript = m_scripts[_Creature->GetScriptId()];
318 if (!tmpscript || !tmpscript->GetAI)
319 return NULL;
321 return tmpscript->GetAI(_Creature);
324 MANGOS_DLL_EXPORT
325 InstanceData* CreateInstanceData(Map* map)
327 if (!map->IsDungeon())
328 return NULL;
330 Script* tmpscript = m_scripts[((InstanceMap*)map)->GetScriptId()];
331 if (!tmpscript || !tmpscript->GetInstanceData)
332 return NULL;
334 return tmpscript->GetInstanceData(map);
337 MANGOS_DLL_EXPORT
338 bool EffectDummyGameObj(Unit* caster, uint32 spellId, SpellEffectIndex effIndex, GameObject* gameObjTarget)
340 Script* tmpscript = m_scripts[gameObjTarget->GetGOInfo()->ScriptId];
341 if (!tmpscript || !tmpscript->pEffectDummyGameObj)
342 return false;
344 return tmpscript->pEffectDummyGameObj(caster, spellId, effIndex, gameObjTarget);
347 MANGOS_DLL_EXPORT
348 bool EffectDummyCreature(Unit* caster, uint32 spellId, SpellEffectIndex effIndex, Creature* crTarget)
350 Script* tmpscript = m_scripts[crTarget->GetScriptId()];
351 if (!tmpscript || !tmpscript->pEffectDummyCreature)
352 return false;
354 return tmpscript->pEffectDummyCreature(caster, spellId, effIndex, crTarget);
357 MANGOS_DLL_EXPORT
358 bool EffectDummyItem(Unit* caster, uint32 spellId, SpellEffectIndex effIndex, Item* itemTarget)
360 Script* tmpscript = m_scripts[itemTarget->GetProto()->ScriptId];
361 if (!tmpscript || !tmpscript->pEffectDummyItem)
362 return false;
364 return tmpscript->pEffectDummyItem(caster, spellId, effIndex, itemTarget);
367 MANGOS_DLL_EXPORT
368 bool EffectAuraDummy(const Aura* pAura, bool apply)
370 Script* tmpscript = m_scripts[((Creature*)pAura->GetTarget())->GetScriptId()];
371 if (!tmpscript || !tmpscript->pEffectAuraDummy)
372 return false;
374 return tmpscript->pEffectAuraDummy(pAura, apply);
377 void ScriptedAI::UpdateAI(const uint32)
379 // Check if we have a current target
380 if (m_creature->isAlive() && m_creature->SelectHostileTarget() && m_creature->getVictim())
382 // If we are within range melee the target
383 if (m_creature->IsWithinDistInMap(m_creature->getVictim(), ATTACK_DISTANCE))
385 if (m_creature->isAttackReady())
387 m_creature->AttackerStateUpdate(m_creature->getVictim());
388 m_creature->resetAttackTimer();
394 void ScriptedAI::EnterEvadeMode()
396 m_creature->CombatStop(true);
397 if (m_creature->isAlive())
398 DoGoHome();
401 void ScriptedAI::DoStartAttack(Unit* victim)
403 if (m_creature->Attack(victim, true))
404 m_creature->GetMotionMaster()->MoveChase(victim);
407 void ScriptedAI::DoStopAttack()
409 if (m_creature->getVictim() != NULL)
410 m_creature->AttackStop();
413 void ScriptedAI::DoGoHome()
415 if (!m_creature->getVictim() && m_creature->isAlive())
416 m_creature->GetMotionMaster()->MoveTargetedHome();