From da944ab528b38291951f9481ddcf3a17d660c1d1 Mon Sep 17 00:00:00 2001 From: hondacrx Date: Tue, 31 May 2022 19:42:40 -0400 Subject: [PATCH] Core/Scripts: Begin unifying triggering gameevents (not the game_event db stuff) Port From (https://github.com/TrinityCore/TrinityCore/commit/41a04a0c496c8c0c521eeaa76e34fbeb531cc1d9) --- Source/Game/Entities/GameObject/GameObject.cs | 52 +++++++--------- Source/Game/Entities/Object/WorldObject.cs | 15 +++-- Source/Game/Entities/Transport.cs | 3 +- Source/Game/Events/GameEventManager.cs | 2 +- Source/Game/Events/GameEventSender.cs | 60 +++++++++++++++++++ .../Generators/FlightPathMovementGenerator.cs | 2 +- Source/Game/Spells/SpellEffects.cs | 9 +-- 7 files changed, 95 insertions(+), 48 deletions(-) create mode 100644 Source/Game/Events/GameEventSender.cs diff --git a/Source/Game/Entities/GameObject/GameObject.cs b/Source/Game/Entities/GameObject/GameObject.cs index c70d86e09..503583938 100644 --- a/Source/Game/Entities/GameObject/GameObject.cs +++ b/Source/Game/Entities/GameObject/GameObject.cs @@ -707,7 +707,8 @@ namespace Game.Entities Battleground bg = map.GetBG(); if (bg != null) { - EventInform(GetGoInfo().CapturePoint.CaptureEventHorde); + if (goInfo.CapturePoint.CaptureEventHorde != 0) + GameEvents.Trigger(goInfo.CapturePoint.CaptureEventHorde, this, this); bg.SendBroadcastText(GetGoInfo().CapturePoint.CaptureBroadcastHorde, ChatMsg.BgSystemHorde); } } @@ -721,7 +722,8 @@ namespace Game.Entities Battleground bg = map.GetBG(); if (bg != null) { - EventInform(GetGoInfo().CapturePoint.CaptureEventAlliance); + if (goInfo.CapturePoint.CaptureEventAlliance != 0) + GameEvents.Trigger(goInfo.CapturePoint.CaptureEventAlliance, this, this); bg.SendBroadcastText(GetGoInfo().CapturePoint.CaptureBroadcastAlliance, ChatMsg.BgSystemAlliance); } } @@ -1830,8 +1832,7 @@ namespace Game.Entities if (info.Goober.eventID != 0) { Log.outDebug(LogFilter.Scripts, "Goober ScriptStart id {0} for GO entry {1} (GUID {2}).", info.Goober.eventID, GetEntry(), GetSpawnId()); - GetMap().ScriptsStart(ScriptsType.Event, info.Goober.eventID, player, this); - EventInform(info.Goober.eventID, user); + GameEvents.Trigger(info.Goober.eventID, player, this); } // possible quest objective for active quests @@ -1893,10 +1894,7 @@ namespace Game.Entities player.SendCinematicStart(info.Camera._camera); if (info.Camera.eventID != 0) - { - GetMap().ScriptsStart(ScriptsType.Event, info.Camera.eventID, player, this); - EventInform(info.Camera.eventID, user); - } + GameEvents.Trigger(info.Camera.eventID, player, this); return; } @@ -2382,23 +2380,6 @@ namespace Game.Entities && dz < info.GeoBoxMax.Z + radius && dz > info.GeoBoxMin.Z - radius; } - public void EventInform(uint eventId, WorldObject invoker = null) - { - if (eventId == 0) - return; - - if (GetAI() != null) - GetAI().EventInform(eventId); - - if (m_zoneScript != null) - m_zoneScript.ProcessEvent(this, eventId, invoker); - - BattlegroundMap bgMap = GetMap().ToBattlegroundMap(); - if (bgMap) - if (bgMap.GetBG()) - bgMap.GetBG().ProcessEvent(this, eventId, invoker); - } - public uint GetScriptId() { GameObjectData gameObjectData = GetGameObjectData(); @@ -2644,7 +2625,8 @@ namespace Game.Entities break; case GameObjectDestructibleState.Damaged: { - EventInform(m_goInfo.DestructibleBuilding.DamagedEvent, attackerOrHealer); + if (GetGoInfo().DestructibleBuilding.DamagedEvent != 0) + GameEvents.Trigger(GetGoInfo().DestructibleBuilding.DamagedEvent, attackerOrHealer, this); GetAI().Damaged(attackerOrHealer, m_goInfo.DestructibleBuilding.DamagedEvent); RemoveFlag(GameObjectFlags.Destroyed); @@ -2670,7 +2652,8 @@ namespace Game.Entities } case GameObjectDestructibleState.Destroyed: { - EventInform(m_goInfo.DestructibleBuilding.DestroyedEvent, attackerOrHealer); + if (GetGoInfo().DestructibleBuilding.DestroyedEvent != 0) + GameEvents.Trigger(GetGoInfo().DestructibleBuilding.DestroyedEvent, attackerOrHealer, this); GetAI().Destroyed(attackerOrHealer, m_goInfo.DestructibleBuilding.DestroyedEvent); Player player = attackerOrHealer != null ? attackerOrHealer.GetCharmerOrOwnerPlayerOrPlayerItself() : null; @@ -2701,7 +2684,8 @@ namespace Game.Entities } case GameObjectDestructibleState.Rebuilding: { - EventInform(m_goInfo.DestructibleBuilding.RebuildingEvent, attackerOrHealer); + if (GetGoInfo().DestructibleBuilding.RebuildingEvent != 0) + GameEvents.Trigger(GetGoInfo().DestructibleBuilding.RebuildingEvent, attackerOrHealer, this); RemoveFlag(GameObjectFlags.Damaged | GameObjectFlags.Destroyed); uint modelId = m_goInfo.displayId; @@ -3115,7 +3099,8 @@ namespace Game.Entities m_goValue.CapturePoint.State = BattlegroundCapturePointState.HordeCaptured; battleground.SendBroadcastText(GetGoInfo().CapturePoint.DefendedBroadcastHorde, ChatMsg.BgSystemHorde, player); UpdateCapturePoint(); - EventInform(GetGoInfo().CapturePoint.DefendedEventHorde, player); + if (GetGoInfo().CapturePoint.DefendedEventHorde != 0) + GameEvents.Trigger(GetGoInfo().CapturePoint.DefendedEventHorde, player, this); return; } @@ -3127,7 +3112,8 @@ namespace Game.Entities m_goValue.CapturePoint.State = BattlegroundCapturePointState.ContestedHorde; battleground.SendBroadcastText(GetGoInfo().CapturePoint.AssaultBroadcastHorde, ChatMsg.BgSystemHorde, player); UpdateCapturePoint(); - EventInform(GetGoInfo().CapturePoint.AssaultBroadcastHorde, player); + if (GetGoInfo().CapturePoint.ContestedEventHorde != 0) + GameEvents.Trigger(GetGoInfo().CapturePoint.ContestedEventHorde, player, this); m_goValue.CapturePoint.AssaultTimer = GetGoInfo().CapturePoint.CaptureTime; break; default: @@ -3142,7 +3128,8 @@ namespace Game.Entities m_goValue.CapturePoint.State = BattlegroundCapturePointState.AllianceCaptured; battleground.SendBroadcastText(GetGoInfo().CapturePoint.DefendedBroadcastAlliance, ChatMsg.BgSystemAlliance, player); UpdateCapturePoint(); - EventInform(GetGoInfo().CapturePoint.DefendedEventAlliance, player); + if (GetGoInfo().CapturePoint.DefendedEventAlliance != 0) + GameEvents.Trigger(GetGoInfo().CapturePoint.DefendedEventAlliance, player, this); return; } @@ -3154,7 +3141,8 @@ namespace Game.Entities m_goValue.CapturePoint.State = BattlegroundCapturePointState.ContestedAlliance; battleground.SendBroadcastText(GetGoInfo().CapturePoint.AssaultBroadcastAlliance, ChatMsg.BgSystemAlliance, player); UpdateCapturePoint(); - EventInform(GetGoInfo().CapturePoint.ContestedEventAlliance, player); + if (GetGoInfo().CapturePoint.ContestedEventAlliance != 0) + GameEvents.Trigger(GetGoInfo().CapturePoint.ContestedEventAlliance, player, this); m_goValue.CapturePoint.AssaultTimer = GetGoInfo().CapturePoint.CaptureTime; break; default: diff --git a/Source/Game/Entities/Object/WorldObject.cs b/Source/Game/Entities/Object/WorldObject.cs index ca6c1d2e9..88e458f91 100644 --- a/Source/Game/Entities/Object/WorldObject.cs +++ b/Source/Game/Entities/Object/WorldObject.cs @@ -1451,23 +1451,30 @@ namespace Game.Entities map.AddObjectToRemoveList(this); } - public void SetZoneScript() + public ZoneScript FindZoneScript() { Map map = GetMap(); if (map != null) { InstanceMap instanceMap = map.ToInstanceMap(); if (instanceMap != null) - m_zoneScript = (ZoneScript)instanceMap.GetInstanceScript(); + return (ZoneScript)instanceMap.GetInstanceScript(); else if (!map.IsBattlegroundOrArena()) { BattleField bf = Global.BattleFieldMgr.GetBattlefieldToZoneId(GetZoneId()); if (bf != null) - m_zoneScript = bf; + return bf; else - m_zoneScript = Global.OutdoorPvPMgr.GetZoneScript(GetZoneId()); + return Global.OutdoorPvPMgr.GetZoneScript(GetZoneId()); } } + + return null; + } + + public void SetZoneScript() + { + m_zoneScript = FindZoneScript(); } public Scenario GetScenario() diff --git a/Source/Game/Entities/Transport.cs b/Source/Game/Entities/Transport.cs index 7b67a4363..ed71b086d 100644 --- a/Source/Game/Entities/Transport.cs +++ b/Source/Game/Entities/Transport.cs @@ -772,8 +772,7 @@ namespace Game.Entities if (eventid != 0) { Log.outDebug(LogFilter.Scripts, "Taxi {0} event {1} of node {2} of {3} path", departure ? "departure" : "arrival", eventid, node.Node.NodeIndex, GetName()); - GetMap().ScriptsStart(ScriptsType.Event, eventid, this, this); - EventInform(eventid); + GameEvents.Trigger(eventid, this, this); } } diff --git a/Source/Game/Events/GameEventManager.cs b/Source/Game/Events/GameEventManager.cs index ba3a89f7c..2c5f8529b 100644 --- a/Source/Game/Events/GameEventManager.cs +++ b/Source/Game/Events/GameEventManager.cs @@ -15,6 +15,7 @@ * along with this program. If not, see . */ +using Framework.Collections; using Framework.Constants; using Framework.Database; using Game.DataStorage; @@ -23,7 +24,6 @@ using Game.Maps; using Game.Networking.Packets; using System; using System.Collections.Generic; -using Framework.Collections; using System.Linq; namespace Game diff --git a/Source/Game/Events/GameEventSender.cs b/Source/Game/Events/GameEventSender.cs new file mode 100644 index 000000000..10e4f82e6 --- /dev/null +++ b/Source/Game/Events/GameEventSender.cs @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2012-2020 CypherCore + * + * 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 3 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, see . + */ + +using Framework.Constants; +using Game.AI; +using Game.Entities; +using Game.Maps; + +namespace Game +{ + class GameEvents + { + public static void Trigger(uint gameEventId, WorldObject source, WorldObject target) + { + Cypher.Assert(source || target, "At least one of [source] or [target] must be provided"); + + WorldObject refForMapAndZoneScript = source ?? target; + + ZoneScript zoneScript = refForMapAndZoneScript.GetZoneScript(); + if (zoneScript == null && refForMapAndZoneScript.IsPlayer()) + zoneScript = refForMapAndZoneScript.FindZoneScript(); + + if (zoneScript != null) + zoneScript.ProcessEvent(target, gameEventId, source); + + Map map = refForMapAndZoneScript.GetMap(); + if (target) + { + GameObject goTarget = target.ToGameObject(); + if (goTarget != null) + { + GameObjectAI goAI = goTarget.GetAI(); + if (goAI != null) + goAI.EventInform(gameEventId); + } + + BattlegroundMap bgMap = map.ToBattlegroundMap(); + if (bgMap != null) + bgMap.GetBG().ProcessEvent(target, gameEventId, source); + } + + map.ScriptsStart(ScriptsType.Event, gameEventId, source, target); + } + + } +} diff --git a/Source/Game/Movement/Generators/FlightPathMovementGenerator.cs b/Source/Game/Movement/Generators/FlightPathMovementGenerator.cs index 6c83a28e0..26980f832 100644 --- a/Source/Game/Movement/Generators/FlightPathMovementGenerator.cs +++ b/Source/Game/Movement/Generators/FlightPathMovementGenerator.cs @@ -248,7 +248,7 @@ namespace Game.Movement if (eventid != 0) { Log.outDebug(LogFilter.MapsScript, $"FlightPathMovementGenerator::DoEventIfAny: taxi {(departure ? "departure" : "arrival")} event {eventid} of node {node.NodeIndex} of path {node.PathID} for player {owner.GetName()}"); - owner.GetMap().ScriptsStart(ScriptsType.Event, eventid, owner, owner); + GameEvents.Trigger(eventid, owner, owner); } } diff --git a/Source/Game/Spells/SpellEffects.cs b/Source/Game/Spells/SpellEffects.cs index fce0f3378..a7f0af2c6 100644 --- a/Source/Game/Spells/SpellEffects.cs +++ b/Source/Game/Spells/SpellEffects.cs @@ -734,14 +734,7 @@ namespace Game.Spells Log.outDebug(LogFilter.Spells, "Spell ScriptStart {0} for spellid {1} in EffectSendEvent ", effectInfo.MiscValue, m_spellInfo.Id); - ZoneScript zoneScript = m_caster.GetZoneScript(); - InstanceScript instanceScript = m_caster.GetInstanceScript(); - if (zoneScript != null) - zoneScript.ProcessEvent(target, (uint)effectInfo.MiscValue, m_caster); - else if (instanceScript != null) // needed in case Player is the caster - instanceScript.ProcessEvent(target, (uint)effectInfo.MiscValue, m_caster); - - m_caster.GetMap().ScriptsStart(ScriptsType.Event, (uint)effectInfo.MiscValue, m_caster, target); + GameEvents.Trigger((uint)effectInfo.MiscValue, m_caster, target); } [SpellEffectHandler(SpellEffectName.PowerBurn)]