TrinityCore
Loading...
Searching...
No Matches
BattlegroundScript.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "BattlegroundScript.h"
19#include "BattlegroundMgr.h"
20#include "Creature.h"
21#include "GameEventSender.h"
22#include "GameObject.h"
23#include "Log.h"
24#include "Map.h"
25#include "ObjectAccessor.h"
26#include "ObjectMgr.h"
27#include "ScriptMgr.h"
28#include "WorldStateMgr.h"
29
30BattlegroundScript::BattlegroundScript(BattlegroundMap* map) noexcept : battlegroundMap(map), battleground(map->GetBG())
31{
32#ifdef TRINITY_API_USE_DYNAMIC_LINKING
33 BattlegroundScriptTemplate const* scriptTemplate = sBattlegroundMgr->FindBattlegroundScriptTemplate(battlegroundMap->GetId(), battlegroundMap->GetBG()->GetTypeID());
34 if (!scriptTemplate)
35 return;
36
37 auto const scriptname = sObjectMgr->GetScriptName(scriptTemplate->ScriptId);
38 ASSERT(!scriptname.empty());
39 // Acquire a strong reference from the script module
40 // to keep it loaded until this object is destroyed.
41 module_reference = sScriptMgr->AcquireModuleReferenceOfScriptName(scriptname);
42#endif // #ifndef TRINITY_API_USE_DYNAMIC_LINKING
43}
44
55
57{
58 ProcessEvent(target, gameEventId, source);
59 GameEvents::TriggerForMap(gameEventId, battlegroundMap, source, target);
60 for (auto const& [playerGuid, _] : battleground->GetPlayers())
61 if (Player* player = ObjectAccessor::FindPlayer(playerGuid))
62 GameEvents::TriggerForPlayer(gameEventId, player);
63}
64
65void BattlegroundScript::UpdateWorldState(int32 worldStateId, int32 value, bool hidden) const
66{
67 WorldStateMgr::SetValue(worldStateId, value, hidden, battlegroundMap);
68}
69
74
75GameObject* ArenaScript::CreateObject(uint32 entry, float x, float y, float z, float o, float rotation0, float rotation1, float rotation2, float rotation3, GOState goState) const
76{
77 QuaternionData rot(rotation0, rotation1, rotation2, rotation3);
78 // Temporally add safety check for bad spawns and send log (object rotations need to be rechecked in sniff)
79 if (!rotation0 && !rotation1 && !rotation2 && !rotation3)
80 {
81 TC_LOG_DEBUG("bg.battleground", "Battleground::AddObject: gameoobject [entry: {}] for BG (map: {}) has zeroed rotation fields, "
82 "orientation used temporally, but please fix the spawn", entry, battlegroundMap->GetId());
83
84 rot = QuaternionData::fromEulerAnglesZYX(o, 0.f, 0.f);
85 }
86
87 // Must be created this way, adding to godatamap would add it to the base map of the instance
88 // and when loading it (in go::LoadFromDB()), a new guid would be assigned to the object, and a new object would be created
89 // So we must create it specific for this instance
90 GameObject* go = GameObject::CreateGameObject(entry, battlegroundMap, Position(x, y, z, o), rot, 255, goState);
91 if (!go)
92 {
93 TC_LOG_ERROR("bg.battleground", "Battleground::AddObject: cannot create gameobject (entry: {}) for BG (map: {}, instance id: {})!",
95 return nullptr;
96 }
97
98 if (!battlegroundMap->AddToMap(go))
99 {
100 delete go;
101 return nullptr;
102 }
103
104 return go;
105}
106
107Creature* ArenaScript::CreateCreature(uint32 entry, float x, float y, float z, float o) const
108{
109 if (!sObjectMgr->GetCreatureTemplate(entry))
110 {
111 TC_LOG_ERROR("bg.battleground", "Battleground::AddCreature: creature template (entry: {}) does not exist for BG (map: {}, instance id: {})!",
113 return nullptr;
114 }
115
116 Position pos = { x, y, z, o };
117
118 Creature* creature = Creature::CreateCreature(entry, battlegroundMap, pos);
119 if (!creature)
120 {
121 TC_LOG_ERROR("bg.battleground", "Battleground::AddCreature: cannot create creature (entry: {}) for BG (map: {}, instance id: {})!",
123 return nullptr;
124 }
125
126 creature->SetHomePosition(pos);
127
128 if (!battlegroundMap->AddToMap(creature))
129 {
130 delete creature;
131 return nullptr;
132 }
133
134 return creature;
135}
#define sBattlegroundMgr
int32_t int32
Definition Define.h:150
uint32_t uint32
Definition Define.h:154
#define ASSERT
Definition Errors.h:80
#define TC_LOG_DEBUG(filterType__, message__,...)
Definition Log.h:181
#define TC_LOG_ERROR(filterType__, message__,...)
Definition Log.h:190
#define sObjectMgr
Definition ObjectMgr.h:1885
#define sScriptMgr
Definition ScriptMgr.h:1449
@ TEAM_OTHER
@ ALLIANCE
@ HORDE
GOState
GameObject * CreateObject(uint32 entry, float x, float y, float z, float o, float rotation0, float rotation1, float rotation2, float rotation3, GOState goState=GO_STATE_READY) const
ArenaScript(BattlegroundMap *map)
Creature * CreateCreature(uint32 entry, float x, float y, float z, float o) const
Battleground * battleground
void TriggerGameEvent(uint32 gameEventId, WorldObject *source=nullptr, WorldObject *target=nullptr) override
BattlegroundMap * battlegroundMap
BattlegroundScript(BattlegroundMap *map) noexcept
virtual Team GetPrematureWinner()
void UpdateWorldState(int32 worldStateId, int32 value, bool hidden=false) const
uint32 GetMinPlayersPerTeam() const
uint32 GetInstanceID() const
BattlegroundPlayerMap const & GetPlayers() const
uint32 GetPlayersCountByTeam(Team team) const
void SetHomePosition(float x, float y, float z, float o)
Definition Creature.h:386
static Creature * CreateCreature(uint32 entry, Map *map, Position const &pos, uint32 vehId=0)
static GameObject * CreateGameObject(uint32 entry, Map *map, Position const &pos, QuaternionData const &rotation, uint32 animProgress, GOState goState, uint32 artKit=0)
bool AddToMap(T *)
Definition Map.cpp:517
uint32 GetId() const
Definition Map.cpp:3257
virtual void ProcessEvent(WorldObject *obj, uint32 eventId, WorldObject *invoker)
Definition ZoneScript.h:103
TC_GAME_API void TriggerForMap(uint32 gameEventId, Map *map, WorldObject *source=nullptr, WorldObject *target=nullptr)
TC_GAME_API void TriggerForPlayer(uint32 gameEventId, Player *source)
TC_GAME_API Player * FindPlayer(ObjectGuid const &)
TC_GAME_API void SetValue(int32 worldStateId, int32 value, bool hidden, Map *map)
static QuaternionData fromEulerAnglesZYX(float Z, float Y, float X)