root/trunk/src/bindings/interface/ScriptMgr.cpp @ 82

Revision 44, 9.5 kB (checked in by yumileroy, 17 years ago)

[svn] * Merge Temp dev SVN with Assembla.
* Changes include:

  • Implementation of w12x's Outdoor PvP and Game Event Systems.
  • Temporary removal of IRC Chat Bot (until infinite loop when disabled is fixed).
  • All mangos -> trinity (to convert your mangos_string table, please run mangos_string_to_trinity_string.sql).
  • Improved Config cleanup.
  • And many more changes.

Original author: Seline
Date: 2008-10-14 11:57:03-05:00

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