2 * Copyright (C) 2005-2008 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
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"
29 Script
*m_scripts
[MAX_SCRIPTS
];
30 InstanceDataScript
* m_instance_scripts
[MAX_INSTANCE_SCRIPTS
];
33 // -- Scripts to be added --
34 extern void AddSC_default();
35 // -------------------
39 { // Free resources before library unload
40 for(int i
=0;i
<nrscripts
;i
++)
43 for(int i
=0;i
<num_inst_scripts
;i
++)
44 delete m_instance_scripts
[i
];
55 for(int i
=0;i
<MAX_SCRIPTS
;i
++)
58 m_instance_scripts
[i
]=NULL
;
61 // -- Inicialize the Scripts to be Added --
63 // ----------------------------------------
68 char const* ScriptsVersion()
70 return "Default MaNGOS scripting library";
73 Script
* GetScriptByName(std::string Name
)
77 for(int i
=0;i
<MAX_SCRIPTS
;i
++)
79 if( m_scripts
[i
] && m_scripts
[i
]->Name
== Name
)
86 bool GossipHello ( Player
* player
, Creature
*_Creature
)
88 Script
*tmpscript
= GetScriptByName(_Creature
->GetScriptName());
89 if(!tmpscript
|| !tmpscript
->pGossipHello
) return false;
91 player
->PlayerTalkClass
->ClearMenus();
92 return tmpscript
->pGossipHello(player
,_Creature
);
96 bool GossipSelect( Player
*player
, Creature
*_Creature
,uint32 sender
, uint32 action
)
98 debug_log("DEBUG: Gossip selection, sender: %d, action: %d",sender
, action
);
100 Script
*tmpscript
= GetScriptByName(_Creature
->GetScriptName());
101 if(!tmpscript
|| !tmpscript
->pGossipSelect
) return false;
103 player
->PlayerTalkClass
->ClearMenus();
104 return tmpscript
->pGossipSelect(player
,_Creature
,sender
,action
);
108 bool GossipSelectWithCode( Player
*player
, Creature
*_Creature
, uint32 sender
, uint32 action
, const char* sCode
)
110 debug_log("DEBUG: Gossip selection, sender: %d, action: %d",sender
, action
);
112 Script
*tmpscript
= GetScriptByName(_Creature
->GetScriptName());
113 if(!tmpscript
|| !tmpscript
->pGossipSelectWithCode
) return false;
115 player
->PlayerTalkClass
->ClearMenus();
116 return tmpscript
->pGossipSelectWithCode(player
,_Creature
,sender
,action
,sCode
);
120 bool QuestAccept( Player
*player
, Creature
*_Creature
, Quest
*_Quest
)
122 Script
*tmpscript
= GetScriptByName(_Creature
->GetScriptName());
123 if(!tmpscript
|| !tmpscript
->pQuestAccept
) return false;
125 player
->PlayerTalkClass
->ClearMenus();
126 return tmpscript
->pQuestAccept(player
,_Creature
,_Quest
);
130 bool QuestSelect( Player
*player
, Creature
*_Creature
, Quest
*_Quest
)
132 Script
*tmpscript
= GetScriptByName(_Creature
->GetScriptName());
133 if(!tmpscript
|| !tmpscript
->pQuestSelect
) return false;
135 player
->PlayerTalkClass
->ClearMenus();
136 return tmpscript
->pQuestSelect(player
,_Creature
,_Quest
);
140 bool QuestComplete( Player
*player
, Creature
*_Creature
, Quest
*_Quest
)
142 Script
*tmpscript
= GetScriptByName(_Creature
->GetScriptName());
143 if(!tmpscript
|| !tmpscript
->pQuestComplete
) return false;
145 player
->PlayerTalkClass
->ClearMenus();
146 return tmpscript
->pQuestComplete(player
,_Creature
,_Quest
);
150 bool ChooseReward( Player
*player
, Creature
*_Creature
, Quest
*_Quest
, uint32 opt
)
152 Script
*tmpscript
= GetScriptByName(_Creature
->GetScriptName());
153 if(!tmpscript
|| !tmpscript
->pChooseReward
) return false;
155 player
->PlayerTalkClass
->ClearMenus();
156 return tmpscript
->pChooseReward(player
,_Creature
,_Quest
,opt
);
160 uint32
NPCDialogStatus( Player
*player
, Creature
*_Creature
)
162 Script
*tmpscript
= GetScriptByName(_Creature
->GetScriptName());
163 if(!tmpscript
|| !tmpscript
->pNPCDialogStatus
) return 100;
165 player
->PlayerTalkClass
->ClearMenus();
166 return tmpscript
->pNPCDialogStatus(player
,_Creature
);
170 uint32
GODialogStatus( Player
*player
, GameObject
*_GO
)
172 Script
*tmpscript
= NULL
;
174 tmpscript
= GetScriptByName(_GO
->GetGOInfo()->ScriptName
);
175 if(!tmpscript
|| !tmpscript
->pGODialogStatus
) return 100;
177 player
->PlayerTalkClass
->ClearMenus();
178 return tmpscript
->pGODialogStatus(player
,_GO
);
182 bool ItemHello( Player
*player
, Item
*_Item
, Quest
*_Quest
)
184 Script
*tmpscript
= NULL
;
186 tmpscript
= GetScriptByName(_Item
->GetProto()->ScriptName
);
187 if(!tmpscript
|| !tmpscript
->pItemHello
) return false;
189 player
->PlayerTalkClass
->ClearMenus();
190 return tmpscript
->pItemHello(player
,_Item
,_Quest
);
194 bool ItemQuestAccept( Player
*player
, Item
*_Item
, Quest
*_Quest
)
196 Script
*tmpscript
= NULL
;
198 tmpscript
= GetScriptByName(_Item
->GetProto()->ScriptName
);
199 if(!tmpscript
|| !tmpscript
->pItemQuestAccept
) return false;
201 player
->PlayerTalkClass
->ClearMenus();
202 return tmpscript
->pItemQuestAccept(player
,_Item
,_Quest
);
206 bool GOHello( Player
*player
, GameObject
*_GO
)
208 Script
*tmpscript
= NULL
;
210 tmpscript
= GetScriptByName(_GO
->GetGOInfo()->ScriptName
);
211 if(!tmpscript
|| !tmpscript
->pGOHello
) return false;
213 player
->PlayerTalkClass
->ClearMenus();
214 return tmpscript
->pGOHello(player
,_GO
);
218 bool GOQuestAccept( Player
*player
, GameObject
*_GO
, Quest
*_Quest
)
220 Script
*tmpscript
= NULL
;
222 tmpscript
= GetScriptByName(_GO
->GetGOInfo()->ScriptName
);
223 if(!tmpscript
|| !tmpscript
->pGOQuestAccept
) return false;
225 player
->PlayerTalkClass
->ClearMenus();
226 return tmpscript
->pGOQuestAccept(player
,_GO
,_Quest
);
230 bool GOChooseReward( Player
*player
, GameObject
*_GO
, Quest
*_Quest
, uint32 opt
)
232 Script
*tmpscript
= NULL
;
234 tmpscript
= GetScriptByName(_GO
->GetGOInfo()->ScriptName
);
235 if(!tmpscript
|| !tmpscript
->pGOChooseReward
) return false;
237 player
->PlayerTalkClass
->ClearMenus();
238 return tmpscript
->pGOChooseReward(player
,_GO
,_Quest
,opt
);
242 bool AreaTrigger ( Player
*player
, AreaTriggerEntry
* atEntry
)
244 Script
*tmpscript
= NULL
;
246 tmpscript
= GetScriptByName(GetAreaTriggerScriptNameById(atEntry
->id
));
247 if(!tmpscript
|| !tmpscript
->pAreaTrigger
) return false;
249 return tmpscript
->pAreaTrigger(player
, atEntry
);
253 bool ReceiveEmote ( Player
*player
, Creature
*_Creature
, uint32 emote
)
255 Script
*tmpscript
= GetScriptByName(_Creature
->GetScriptName());
256 if(!tmpscript
|| !tmpscript
->pReceiveEmote
) return false;
258 return tmpscript
->pReceiveEmote(player
,_Creature
, emote
);
262 bool ItemUse( Player
*player
, Item
* _Item
, SpellCastTargets
const& targets
)
264 Script
*tmpscript
= NULL
;
266 tmpscript
= GetScriptByName(_Item
->GetProto()->ScriptName
);
267 if(!tmpscript
|| !tmpscript
->pItemUse
) return false;
269 return tmpscript
->pItemUse(player
,_Item
,targets
);
273 CreatureAI
* GetAI(Creature
*_Creature
)
275 Script
*tmpscript
= GetScriptByName(_Creature
->GetScriptName());
276 if(!tmpscript
|| !tmpscript
->GetAI
) return NULL
;
278 return tmpscript
->GetAI(_Creature
);
282 InstanceData
* CreateInstanceData(Map
*map
)
284 if(!map
->IsDungeon()) return NULL
;
285 std::string name
= ((InstanceMap
*)map
)->GetScript();
287 for(int i
=0;i
<num_inst_scripts
;i
++)
288 if(m_instance_scripts
[i
] && m_instance_scripts
[i
]->name
== name
)
289 return m_instance_scripts
[i
]->GetInstanceData(map
);
293 void ScriptedAI::UpdateAI(const uint32
)
295 //Check if we have a current target
296 if( m_creature
->isAlive() && m_creature
->SelectHostilTarget() && m_creature
->getVictim())
298 //If we are within range melee the target
299 if( m_creature
->IsWithinDistInMap(m_creature
->getVictim(), ATTACK_DISTANCE
))
301 if( m_creature
->isAttackReady() )
303 m_creature
->AttackerStateUpdate(m_creature
->getVictim());
304 m_creature
->resetAttackTimer();
310 void ScriptedAI::EnterEvadeMode()
312 if( m_creature
->isAlive() )
316 void ScriptedAI::DoStartAttack(Unit
* victim
)
318 if( m_creature
->Attack(victim
, true) )
319 m_creature
->GetMotionMaster()->MoveChase(victim
);
322 void ScriptedAI::DoStopAttack()
324 if( m_creature
->getVictim() != NULL
)
326 m_creature
->AttackStop();
330 void ScriptedAI::DoGoHome()
332 if( !m_creature
->getVictim() && m_creature
->isAlive() )
333 m_creature
->GetMotionMaster()->MoveTargetedHome();