/*
* Copyright (C) 2008-2010 Trinity
*
* Thanks to the original authors: MaNGOS
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include "ScriptMgr.h"
#include "GossipDef.h"
#include "GameObject.h"
#include "Player.h"
#include "Map.h"
#include "ObjectMgr.h"
int num_sc_scripts;
Script *m_scripts[MAX_SCRIPTS];
// -- Scripts to be added --
extern void AddSC_default();
// -------------------
TRINITY_DLL_EXPORT
void ScriptsFree()
{ // Free resources before library unload
for(int i = 0; i < num_sc_scripts; i++)
delete m_scripts[i];
num_sc_scripts = 0;
}
TRINITY_DLL_EXPORT
void ScriptsInit(char const* cfg_file = "trinitycore.conf")
{
num_sc_scripts = 0;
for(int i=0;iName == Name )
return m_scripts[i];
}
return NULL;
}
//*********************************
//*** Functions used internally ***
void Script::RegisterSelf()
{
int id = GetScriptId(Name.c_str());
if(id)
{
m_scripts[id] = this;
++num_sc_scripts;
}
}
//********************************
//*** Functions to be Exported ***
TRINITY_DLL_EXPORT
void OnLogin(Player *pPlayer)
{
Script *tmpscript = m_scripts[GetScriptId("scripted_on_events")];
if (!tmpscript || !tmpscript->pOnLogin) return;
tmpscript->pOnLogin(pPlayer);
}
TRINITY_DLL_EXPORT
void OnLogout(Player *pPlayer)
{
Script *tmpscript = m_scripts[GetScriptId("scripted_on_events")];
if (!tmpscript || !tmpscript->pOnLogout) return;
tmpscript->pOnLogout(pPlayer);
}
TRINITY_DLL_EXPORT
void OnPVPKill(Player *killer, Player *killed)
{
Script *tmpscript = m_scripts[GetScriptId("scripted_on_events")];
if (!tmpscript || !tmpscript->pOnPVPKill) return;
tmpscript->pOnPVPKill(killer, killed);
}
TRINITY_DLL_EXPORT
bool OnSpellCast (Unit *pUnitTarget, Item *pItemTarget, GameObject *pGoTarget, uint32 i, SpellEntry const *spell)
{
Script *tmpscript = m_scripts[GetScriptId("scripted_on_events")];
if (!tmpscript || !tmpscript->pOnSpellCast) return true;
return tmpscript->pOnSpellCast(pUnitTarget,pItemTarget,pGoTarget,i,spell);
}
TRINITY_DLL_EXPORT
uint32 OnGetXP(Player *pPlayer, uint32 amount)
{
Script *tmpscript = m_scripts[GetScriptId("scripted_on_events")];
if (!tmpscript || !tmpscript->pOnGetXP) return amount;
return tmpscript->pOnGetXP(pPlayer,amount);
}
TRINITY_DLL_EXPORT
uint32 OnGetMoney(Player *pPlayer, int32 amount)
{
Script *tmpscript = m_scripts[GetScriptId("scripted_on_events")];
if (!tmpscript || !tmpscript->pOnGetMoney) return amount;
return tmpscript->pOnGetMoney(pPlayer,amount);
}
TRINITY_DLL_EXPORT
bool OnPlayerChat(Player *pPlayer, const char *text)
{
Script *tmpscript = m_scripts[GetScriptId("scripted_on_events")];
if (!tmpscript || !tmpscript->pOnPlayerChat) return true;
return tmpscript->pOnPlayerChat(pPlayer,text);
}
TRINITY_DLL_EXPORT
void OnServerStartup()
{
Script *tmpscript = m_scripts[GetScriptId("scripted_on_events")];
if (!tmpscript || !tmpscript->pOnServerStartup) return;
tmpscript->pOnServerStartup();
}
TRINITY_DLL_EXPORT
void OnServerShutdown()
{
Script *tmpscript = m_scripts[GetScriptId("scripted_on_events")];
if (!tmpscript || !tmpscript->pOnServerShutdown) return;
tmpscript->pOnServerShutdown();
}
TRINITY_DLL_EXPORT
void OnAreaChange(Player *pPlayer, AreaTableEntry const *pArea)
{
Script *tmpscript = m_scripts[GetScriptId("scripted_on_events")];
if (!tmpscript || !tmpscript->pOnAreaChange) return;
tmpscript->pOnAreaChange(pPlayer, pArea);
}
TRINITY_DLL_EXPORT
bool OnItemClick (Player *pPlayer, Item *pItem)
{
Script *tmpscript = m_scripts[GetScriptId("scripted_on_events")];
if (!tmpscript || !tmpscript->pOnItemClick) return true;
return tmpscript->pOnItemClick(pPlayer,pItem);
}
TRINITY_DLL_EXPORT
bool OnItemOpen (Player *pPlayer, Item *pItem)
{
Script *tmpscript = m_scripts[GetScriptId("scripted_on_events")];
if (!tmpscript || !tmpscript->pOnItemOpen) return true;
return tmpscript->pOnItemOpen(pPlayer,pItem);
}
TRINITY_DLL_EXPORT
bool OnGoClick (Player *pPlayer, GameObject *pGameObject)
{
Script *tmpscript = m_scripts[GetScriptId("scripted_on_events")];
if (!tmpscript || !tmpscript->pOnGoClick) return true;
return tmpscript->pOnGoClick(pPlayer,pGameObject);
}
TRINITY_DLL_EXPORT
void OnCreatureKill (Player *pPlayer, Creature *pCreature)
{
Script *tmpscript = m_scripts[GetScriptId("scripted_on_events")];
if (!tmpscript || !tmpscript->pOnCreatureKill) return;
tmpscript->pOnCreatureKill(pPlayer,pCreature);
}
TRINITY_DLL_EXPORT
char const* ScriptsVersion()
{
return "Default Trinity scripting library";
}
TRINITY_DLL_EXPORT
bool GossipHello ( Player * player, Creature *_Creature )
{
Script *tmpscript = m_scripts[_Creature->GetScriptId()];
if (!tmpscript || !tmpscript->pGossipHello) return false;
player->PlayerTalkClass->ClearMenus();
return tmpscript->pGossipHello(player,_Creature);
}
TRINITY_DLL_EXPORT
bool GossipSelect( Player *player, Creature *_Creature, uint32 sender, uint32 action )
{
debug_log("TSCR: Gossip selection, sender: %d, action: %d",sender, action);
Script *tmpscript = m_scripts[_Creature->GetScriptId()];
if (!tmpscript || !tmpscript->pGossipSelect) return false;
player->PlayerTalkClass->ClearMenus();
return tmpscript->pGossipSelect(player,_Creature,sender,action);
}
TRINITY_DLL_EXPORT
bool GossipSelectWithCode( Player *player, Creature *_Creature, uint32 sender, uint32 action, const char* sCode )
{
debug_log("TSCR: Gossip selection with code, sender: %d, action: %d",sender, action);
Script *tmpscript = m_scripts[_Creature->GetScriptId()];
if (!tmpscript || !tmpscript->pGossipSelectWithCode) return false;
player->PlayerTalkClass->ClearMenus();
return tmpscript->pGossipSelectWithCode(player,_Creature,sender,action,sCode);
}
TRINITY_DLL_EXPORT
bool GOSelect( Player *player, GameObject *_GO, uint32 sender, uint32 action )
{
if(!_GO)
return false;
debug_log("TSCR: Gossip selection, sender: %d, action: %d",sender, action);
Script *tmpscript = m_scripts[_GO->GetGOInfo()->ScriptId];
if(!tmpscript || !tmpscript->pGOSelect) return false;
player->PlayerTalkClass->ClearMenus();
return tmpscript->pGOSelect(player,_GO,sender,action);
}
TRINITY_DLL_EXPORT
bool GOSelectWithCode( Player *player, GameObject *_GO, uint32 sender, uint32 action, const char* sCode )
{
if(!_GO)
return false;
debug_log("TSCR: Gossip selection, sender: %d, action: %d",sender, action);
Script *tmpscript = m_scripts[_GO->GetGOInfo()->ScriptId];
if(!tmpscript || !tmpscript->pGOSelectWithCode) return false;
player->PlayerTalkClass->ClearMenus();
return tmpscript->pGOSelectWithCode(player,_GO,sender,action,sCode);
}
TRINITY_DLL_EXPORT
bool QuestAccept( Player *player, Creature *_Creature, Quest const *_Quest )
{
Script *tmpscript = m_scripts[_Creature->GetScriptId()];
if (!tmpscript || !tmpscript->pQuestAccept) return false;
player->PlayerTalkClass->ClearMenus();
return tmpscript->pQuestAccept(player,_Creature,_Quest);
}
TRINITY_DLL_EXPORT
bool QuestSelect( Player *player, Creature *_Creature, Quest const *_Quest )
{
Script *tmpscript = m_scripts[_Creature->GetScriptId()];
if (!tmpscript || !tmpscript->pQuestSelect) return false;
player->PlayerTalkClass->ClearMenus();
return tmpscript->pQuestSelect(player,_Creature,_Quest);
}
TRINITY_DLL_EXPORT
bool QuestComplete( Player *player, Creature *_Creature, Quest const *_Quest )
{
Script *tmpscript = m_scripts[_Creature->GetScriptId()];
if (!tmpscript || !tmpscript->pQuestComplete) return false;
player->PlayerTalkClass->ClearMenus();
return tmpscript->pQuestComplete(player,_Creature,_Quest);
}
TRINITY_DLL_EXPORT
bool ChooseReward( Player *player, Creature *_Creature, Quest const *_Quest, uint32 opt )
{
Script *tmpscript = m_scripts[_Creature->GetScriptId()];
if (!tmpscript || !tmpscript->pChooseReward) return false;
player->PlayerTalkClass->ClearMenus();
return tmpscript->pChooseReward(player,_Creature,_Quest,opt);
}
TRINITY_DLL_EXPORT
uint32 NPCDialogStatus( Player *player, Creature *_Creature )
{
Script *tmpscript = m_scripts[_Creature->GetScriptId()];
if (!tmpscript || !tmpscript->pNPCDialogStatus) return 100;
player->PlayerTalkClass->ClearMenus();
return tmpscript->pNPCDialogStatus(player,_Creature);
}
TRINITY_DLL_EXPORT
uint32 GODialogStatus( Player *player, GameObject *_GO )
{
Script *tmpscript = m_scripts[_GO->GetGOInfo()->ScriptId];
if (!tmpscript || !tmpscript->pGODialogStatus) return 100;
player->PlayerTalkClass->ClearMenus();
return tmpscript->pGODialogStatus(player,_GO);
}
TRINITY_DLL_EXPORT
bool ItemHello( Player *player, Item *_Item, Quest const *_Quest )
{
Script *tmpscript = m_scripts[_Item->GetProto()->ScriptId];
if (!tmpscript || !tmpscript->pItemHello) return false;
player->PlayerTalkClass->ClearMenus();
return tmpscript->pItemHello(player,_Item,_Quest);
}
TRINITY_DLL_EXPORT
bool ItemQuestAccept( Player *player, Item *_Item, Quest const *_Quest )
{
Script *tmpscript = m_scripts[_Item->GetProto()->ScriptId];
if (!tmpscript || !tmpscript->pItemQuestAccept) return false;
player->PlayerTalkClass->ClearMenus();
return tmpscript->pItemQuestAccept(player,_Item,_Quest);
}
TRINITY_DLL_EXPORT
bool GOHello( Player *player, GameObject *_GO )
{
Script *tmpscript = m_scripts[_GO->GetGOInfo()->ScriptId];
if (!tmpscript || !tmpscript->pGOHello) return false;
player->PlayerTalkClass->ClearMenus();
return tmpscript->pGOHello(player,_GO);
}
TRINITY_DLL_EXPORT
bool GOQuestAccept( Player *player, GameObject *_GO, Quest const *_Quest )
{
Script *tmpscript = m_scripts[_GO->GetGOInfo()->ScriptId];
if (!tmpscript || !tmpscript->pGOQuestAccept) return false;
player->PlayerTalkClass->ClearMenus();
return tmpscript->pGOQuestAccept(player,_GO,_Quest);
}
TRINITY_DLL_EXPORT
bool GOChooseReward( Player *player, GameObject *_GO, Quest const *_Quest, uint32 opt )
{
Script *tmpscript = m_scripts[_GO->GetGOInfo()->ScriptId];
if (!tmpscript || !tmpscript->pGOChooseReward) return false;
player->PlayerTalkClass->ClearMenus();
return tmpscript->pGOChooseReward(player,_GO,_Quest,opt);
}
TRINITY_DLL_EXPORT
bool AreaTrigger( Player *player, AreaTriggerEntry * atEntry)
{
Script *tmpscript = m_scripts[GetAreaTriggerScriptId(atEntry->id)];
if (!tmpscript || !tmpscript->pAreaTrigger) return false;
return tmpscript->pAreaTrigger(player, atEntry);
}
TRINITY_DLL_EXPORT
CreatureAI* GetAI(Creature *_Creature)
{
Script *tmpscript = m_scripts[_Creature->GetScriptId()];
if (!tmpscript || !tmpscript->GetAI) return NULL;
return tmpscript->GetAI(_Creature);
}
TRINITY_DLL_EXPORT
bool ItemUse( Player *player, Item* _Item, SpellCastTargets const& targets)
{
Script *tmpscript = m_scripts[_Item->GetProto()->ScriptId];
if (!tmpscript || !tmpscript->pItemUse) return false;
return tmpscript->pItemUse(player,_Item,targets);
}
TRINITY_DLL_EXPORT
bool ItemExpire( Player *player, ItemPrototype const *_ItemProto)
{
Script *tmpscript = m_scripts[_ItemProto->ScriptId];
if (!tmpscript || !tmpscript->pItemExpire) return true;
return tmpscript->pItemExpire(player,_ItemProto);
}
TRINITY_DLL_EXPORT
bool EffectDummyCreature(Unit *caster, uint32 spellId, uint32 effIndex, Creature *crTarget )
{
Script *tmpscript = m_scripts[crTarget->GetScriptId()];
if (!tmpscript || !tmpscript->pEffectDummyCreature) return false;
return tmpscript->pEffectDummyCreature(caster, spellId,effIndex,crTarget);
}
TRINITY_DLL_EXPORT
bool EffectDummyGameObj(Unit *caster, uint32 spellId, uint32 effIndex, GameObject *gameObjTarget )
{
Script *tmpscript = m_scripts[gameObjTarget->GetGOInfo()->ScriptId];
if (!tmpscript || !tmpscript->pEffectDummyGameObj) return false;
return tmpscript->pEffectDummyGameObj(caster, spellId,effIndex,gameObjTarget);
}
TRINITY_DLL_EXPORT
bool EffectDummyItem(Unit *caster, uint32 spellId, uint32 effIndex, Item *itemTarget )
{
Script *tmpscript = m_scripts[itemTarget->GetProto()->ScriptId];
if (!tmpscript || !tmpscript->pEffectDummyItem) return false;
return tmpscript->pEffectDummyItem(caster, spellId,effIndex,itemTarget);
}
TRINITY_DLL_EXPORT
InstanceData* CreateInstanceData(Map *map)
{
if (!map->IsDungeon()) return NULL;
Script *tmpscript = m_scripts[((InstanceMap*)map)->GetScriptId()];
if (!tmpscript || !tmpscript->GetInstanceData) return NULL;
return tmpscript->GetInstanceData(map);
}