1 | /* |
---|
2 | * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> |
---|
3 | * |
---|
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. |
---|
8 | * |
---|
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. |
---|
13 | * |
---|
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 |
---|
17 | */ |
---|
18 | |
---|
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 | |
---|
27 | //uint8 loglevel = 0; |
---|
28 | int nrscripts; |
---|
29 | Script *m_scripts[MAX_SCRIPTS]; |
---|
30 | InstanceDataScript* m_instance_scripts[MAX_INSTANCE_SCRIPTS]; |
---|
31 | int num_inst_scripts; |
---|
32 | |
---|
33 | // -- Scripts to be added -- |
---|
34 | extern void AddSC_default(); |
---|
35 | // ------------------- |
---|
36 | |
---|
37 | MANGOS_DLL_EXPORT |
---|
38 | void ScriptsFree() |
---|
39 | { // Free resources before library unload |
---|
40 | for(int i=0;i<nrscripts;i++) |
---|
41 | delete m_scripts[i]; |
---|
42 | |
---|
43 | for(int i=0;i<num_inst_scripts;i++) |
---|
44 | delete m_instance_scripts[i]; |
---|
45 | |
---|
46 | nrscripts = 0; |
---|
47 | num_inst_scripts = 0; |
---|
48 | } |
---|
49 | |
---|
50 | MANGOS_DLL_EXPORT |
---|
51 | void ScriptsInit() |
---|
52 | { |
---|
53 | nrscripts = 0; |
---|
54 | num_inst_scripts = 0; |
---|
55 | for(int i=0;i<MAX_SCRIPTS;i++) |
---|
56 | { |
---|
57 | m_scripts[i]=NULL; |
---|
58 | m_instance_scripts[i]=NULL; |
---|
59 | } |
---|
60 | |
---|
61 | // -- Inicialize the Scripts to be Added -- |
---|
62 | AddSC_default(); |
---|
63 | // ---------------------------------------- |
---|
64 | |
---|
65 | } |
---|
66 | |
---|
67 | Script* GetScriptByName(std::string Name) |
---|
68 | { |
---|
69 | if(Name.empty()) |
---|
70 | return NULL; |
---|
71 | for(int i=0;i<MAX_SCRIPTS;i++) |
---|
72 | { |
---|
73 | if( m_scripts[i] && m_scripts[i]->Name == Name ) |
---|
74 | return m_scripts[i]; |
---|
75 | } |
---|
76 | return NULL; |
---|
77 | } |
---|
78 | |
---|
79 | MANGOS_DLL_EXPORT |
---|
80 | bool GossipHello ( Player * player, Creature *_Creature ) |
---|
81 | { |
---|
82 | Script *tmpscript = GetScriptByName(_Creature->GetScriptName()); |
---|
83 | if(!tmpscript || !tmpscript->pGossipHello) return false; |
---|
84 | |
---|
85 | player->PlayerTalkClass->ClearMenus(); |
---|
86 | return tmpscript->pGossipHello(player,_Creature); |
---|
87 | } |
---|
88 | |
---|
89 | MANGOS_DLL_EXPORT |
---|
90 | bool GossipSelect( Player *player, Creature *_Creature,uint32 sender, uint32 action ) |
---|
91 | { |
---|
92 | debug_log("DEBUG: Gossip selection, sender: %d, action: %d",sender, action); |
---|
93 | |
---|
94 | Script *tmpscript = GetScriptByName(_Creature->GetScriptName()); |
---|
95 | if(!tmpscript || !tmpscript->pGossipSelect) return false; |
---|
96 | |
---|
97 | player->PlayerTalkClass->ClearMenus(); |
---|
98 | return tmpscript->pGossipSelect(player,_Creature,sender,action); |
---|
99 | } |
---|
100 | |
---|
101 | MANGOS_DLL_EXPORT |
---|
102 | bool GossipSelectWithCode( Player *player, Creature *_Creature, uint32 sender, uint32 action, const char* sCode ) |
---|
103 | { |
---|
104 | debug_log("DEBUG: Gossip selection, sender: %d, action: %d",sender, action); |
---|
105 | |
---|
106 | Script *tmpscript = GetScriptByName(_Creature->GetScriptName()); |
---|
107 | if(!tmpscript || !tmpscript->pGossipSelectWithCode) return false; |
---|
108 | |
---|
109 | player->PlayerTalkClass->ClearMenus(); |
---|
110 | return tmpscript->pGossipSelectWithCode(player,_Creature,sender,action,sCode); |
---|
111 | } |
---|
112 | |
---|
113 | MANGOS_DLL_EXPORT |
---|
114 | bool QuestAccept( Player *player, Creature *_Creature, Quest *_Quest ) |
---|
115 | { |
---|
116 | Script *tmpscript = GetScriptByName(_Creature->GetScriptName()); |
---|
117 | if(!tmpscript || !tmpscript->pQuestAccept) return false; |
---|
118 | |
---|
119 | player->PlayerTalkClass->ClearMenus(); |
---|
120 | return tmpscript->pQuestAccept(player,_Creature,_Quest); |
---|
121 | } |
---|
122 | |
---|
123 | MANGOS_DLL_EXPORT |
---|
124 | bool QuestSelect( Player *player, Creature *_Creature, Quest *_Quest ) |
---|
125 | { |
---|
126 | Script *tmpscript = GetScriptByName(_Creature->GetScriptName()); |
---|
127 | if(!tmpscript || !tmpscript->pQuestSelect) return false; |
---|
128 | |
---|
129 | player->PlayerTalkClass->ClearMenus(); |
---|
130 | return tmpscript->pQuestSelect(player,_Creature,_Quest); |
---|
131 | } |
---|
132 | |
---|
133 | MANGOS_DLL_EXPORT |
---|
134 | bool QuestComplete( Player *player, Creature *_Creature, Quest *_Quest ) |
---|
135 | { |
---|
136 | Script *tmpscript = GetScriptByName(_Creature->GetScriptName()); |
---|
137 | if(!tmpscript || !tmpscript->pQuestComplete) return false; |
---|
138 | |
---|
139 | player->PlayerTalkClass->ClearMenus(); |
---|
140 | return tmpscript->pQuestComplete(player,_Creature,_Quest); |
---|
141 | } |
---|
142 | |
---|
143 | MANGOS_DLL_EXPORT |
---|
144 | bool ChooseReward( Player *player, Creature *_Creature, Quest *_Quest, uint32 opt ) |
---|
145 | { |
---|
146 | Script *tmpscript = GetScriptByName(_Creature->GetScriptName()); |
---|
147 | if(!tmpscript || !tmpscript->pChooseReward) return false; |
---|
148 | |
---|
149 | player->PlayerTalkClass->ClearMenus(); |
---|
150 | return tmpscript->pChooseReward(player,_Creature,_Quest,opt); |
---|
151 | } |
---|
152 | |
---|
153 | MANGOS_DLL_EXPORT |
---|
154 | uint32 NPCDialogStatus( Player *player, Creature *_Creature ) |
---|
155 | { |
---|
156 | Script *tmpscript = GetScriptByName(_Creature->GetScriptName()); |
---|
157 | if(!tmpscript || !tmpscript->pNPCDialogStatus) return 100; |
---|
158 | |
---|
159 | player->PlayerTalkClass->ClearMenus(); |
---|
160 | return tmpscript->pNPCDialogStatus(player,_Creature); |
---|
161 | } |
---|
162 | |
---|
163 | MANGOS_DLL_EXPORT |
---|
164 | uint32 GODialogStatus( Player *player, GameObject *_GO ) |
---|
165 | { |
---|
166 | Script *tmpscript = NULL; |
---|
167 | |
---|
168 | tmpscript = GetScriptByName(_GO->GetGOInfo()->ScriptName); |
---|
169 | if(!tmpscript || !tmpscript->pGODialogStatus) return 100; |
---|
170 | |
---|
171 | player->PlayerTalkClass->ClearMenus(); |
---|
172 | return tmpscript->pGODialogStatus(player,_GO); |
---|
173 | } |
---|
174 | |
---|
175 | MANGOS_DLL_EXPORT |
---|
176 | bool ItemHello( Player *player, Item *_Item, Quest *_Quest ) |
---|
177 | { |
---|
178 | Script *tmpscript = NULL; |
---|
179 | |
---|
180 | tmpscript = GetScriptByName(_Item->GetProto()->ScriptName); |
---|
181 | if(!tmpscript || !tmpscript->pItemHello) return false; |
---|
182 | |
---|
183 | player->PlayerTalkClass->ClearMenus(); |
---|
184 | return tmpscript->pItemHello(player,_Item,_Quest); |
---|
185 | } |
---|
186 | |
---|
187 | MANGOS_DLL_EXPORT |
---|
188 | bool ItemQuestAccept( Player *player, Item *_Item, Quest *_Quest ) |
---|
189 | { |
---|
190 | Script *tmpscript = NULL; |
---|
191 | |
---|
192 | tmpscript = GetScriptByName(_Item->GetProto()->ScriptName); |
---|
193 | if(!tmpscript || !tmpscript->pItemQuestAccept) return false; |
---|
194 | |
---|
195 | player->PlayerTalkClass->ClearMenus(); |
---|
196 | return tmpscript->pItemQuestAccept(player,_Item,_Quest); |
---|
197 | } |
---|
198 | |
---|
199 | MANGOS_DLL_EXPORT |
---|
200 | bool GOHello( Player *player, GameObject *_GO ) |
---|
201 | { |
---|
202 | Script *tmpscript = NULL; |
---|
203 | |
---|
204 | tmpscript = GetScriptByName(_GO->GetGOInfo()->ScriptName); |
---|
205 | if(!tmpscript || !tmpscript->pGOHello) return false; |
---|
206 | |
---|
207 | player->PlayerTalkClass->ClearMenus(); |
---|
208 | return tmpscript->pGOHello(player,_GO); |
---|
209 | } |
---|
210 | |
---|
211 | MANGOS_DLL_EXPORT |
---|
212 | bool GOQuestAccept( Player *player, GameObject *_GO, Quest *_Quest ) |
---|
213 | { |
---|
214 | Script *tmpscript = NULL; |
---|
215 | |
---|
216 | tmpscript = GetScriptByName(_GO->GetGOInfo()->ScriptName); |
---|
217 | if(!tmpscript || !tmpscript->pGOQuestAccept) return false; |
---|
218 | |
---|
219 | player->PlayerTalkClass->ClearMenus(); |
---|
220 | return tmpscript->pGOQuestAccept(player,_GO,_Quest); |
---|
221 | } |
---|
222 | |
---|
223 | MANGOS_DLL_EXPORT |
---|
224 | bool GOChooseReward( Player *player, GameObject *_GO, Quest *_Quest, uint32 opt ) |
---|
225 | { |
---|
226 | Script *tmpscript = NULL; |
---|
227 | |
---|
228 | tmpscript = GetScriptByName(_GO->GetGOInfo()->ScriptName); |
---|
229 | if(!tmpscript || !tmpscript->pGOChooseReward) return false; |
---|
230 | |
---|
231 | player->PlayerTalkClass->ClearMenus(); |
---|
232 | return tmpscript->pGOChooseReward(player,_GO,_Quest,opt); |
---|
233 | } |
---|
234 | |
---|
235 | MANGOS_DLL_EXPORT |
---|
236 | bool AreaTrigger ( Player *player, AreaTriggerEntry* atEntry ) |
---|
237 | { |
---|
238 | Script *tmpscript = NULL; |
---|
239 | |
---|
240 | tmpscript = GetScriptByName(GetAreaTriggerScriptNameById(atEntry->id)); |
---|
241 | if(!tmpscript || !tmpscript->pAreaTrigger) return false; |
---|
242 | |
---|
243 | return tmpscript->pAreaTrigger(player, atEntry); |
---|
244 | } |
---|
245 | |
---|
246 | MANGOS_DLL_EXPORT |
---|
247 | bool ReceiveEmote ( Player *player, Creature *_Creature, uint32 emote ) |
---|
248 | { |
---|
249 | Script *tmpscript = GetScriptByName(_Creature->GetScriptName()); |
---|
250 | if(!tmpscript || !tmpscript->pReceiveEmote) return false; |
---|
251 | |
---|
252 | return tmpscript->pReceiveEmote(player,_Creature, emote); |
---|
253 | } |
---|
254 | |
---|
255 | MANGOS_DLL_EXPORT |
---|
256 | bool ItemUse( Player *player, Item* _Item, SpellCastTargets const& targets) |
---|
257 | { |
---|
258 | Script *tmpscript = NULL; |
---|
259 | |
---|
260 | tmpscript = GetScriptByName(_Item->GetProto()->ScriptName); |
---|
261 | if(!tmpscript || !tmpscript->pItemUse) return false; |
---|
262 | |
---|
263 | return tmpscript->pItemUse(player,_Item,targets); |
---|
264 | } |
---|
265 | |
---|
266 | MANGOS_DLL_EXPORT |
---|
267 | CreatureAI* GetAI(Creature *_Creature ) |
---|
268 | { |
---|
269 | Script *tmpscript = GetScriptByName(_Creature->GetScriptName()); |
---|
270 | if(!tmpscript || !tmpscript->GetAI) return NULL; |
---|
271 | |
---|
272 | return tmpscript->GetAI(_Creature); |
---|
273 | } |
---|
274 | |
---|
275 | MANGOS_DLL_EXPORT |
---|
276 | InstanceData* CreateInstanceData(Map *map) |
---|
277 | { |
---|
278 | if(!map->IsDungeon()) return NULL; |
---|
279 | std::string name = ((InstanceMap*)map)->GetScript(); |
---|
280 | if(!name.empty()) |
---|
281 | for(int i=0;i<num_inst_scripts;i++) |
---|
282 | if(m_instance_scripts[i] && m_instance_scripts[i]->name == name) |
---|
283 | return m_instance_scripts[i]->GetInstanceData(map); |
---|
284 | return NULL; |
---|
285 | } |
---|
286 | |
---|
287 | void ScriptedAI::UpdateAI(const uint32) |
---|
288 | { |
---|
289 | //Check if we have a current target |
---|
290 | if( m_creature->isAlive() && m_creature->SelectHostilTarget() && m_creature->getVictim()) |
---|
291 | { |
---|
292 | //If we are within range melee the target |
---|
293 | if( m_creature->IsWithinDistInMap(m_creature->getVictim(), ATTACK_DISTANCE)) |
---|
294 | { |
---|
295 | if( m_creature->isAttackReady() ) |
---|
296 | { |
---|
297 | m_creature->AttackerStateUpdate(m_creature->getVictim()); |
---|
298 | m_creature->resetAttackTimer(); |
---|
299 | } |
---|
300 | } |
---|
301 | } |
---|
302 | } |
---|
303 | |
---|
304 | void ScriptedAI::EnterEvadeMode() |
---|
305 | { |
---|
306 | if( m_creature->isAlive() ) |
---|
307 | DoGoHome(); |
---|
308 | } |
---|
309 | |
---|
310 | void ScriptedAI::DoStartAttack(Unit* victim) |
---|
311 | { |
---|
312 | if( m_creature->Attack(victim, true) ) |
---|
313 | m_creature->GetMotionMaster()->MoveChase(victim); |
---|
314 | } |
---|
315 | |
---|
316 | void ScriptedAI::DoStopAttack() |
---|
317 | { |
---|
318 | if( m_creature->getVictim() != NULL ) |
---|
319 | { |
---|
320 | m_creature->AttackStop(); |
---|
321 | } |
---|
322 | } |
---|
323 | |
---|
324 | void ScriptedAI::DoGoHome() |
---|
325 | { |
---|
326 | if( !m_creature->getVictim() && m_creature->isAlive() ) |
---|
327 | m_creature->GetMotionMaster()->MoveTargetedHome(); |
---|
328 | } |
---|