Core/World: implement database support for default map and realm wide world states

Port From (https://github.com/TrinityCore/TrinityCore/commit/737d94d7efe0b6c308ac1bf3692b6aa2e43f5adb)
This commit is contained in:
hondacrx
2022-06-25 22:52:20 -04:00
parent ed7a07d893
commit 7be2eb52cc
9 changed files with 217 additions and 0 deletions
+2
View File
@@ -2913,6 +2913,8 @@ namespace Game.Entities
packet.AreaID = zoneid; packet.AreaID = zoneid;
packet.SubareaID = areaid; packet.SubareaID = areaid;
Global.WorldStateMgr.FillInitialWorldStates(packet, GetMap());
packet.AddState(2264, 0); // SCOURGE_EVENT_WORLDSTATE_EASTERN_PLAGUELANDS packet.AddState(2264, 0); // SCOURGE_EVENT_WORLDSTATE_EASTERN_PLAGUELANDS
packet.AddState(2263, 0); // SCOURGE_EVENT_WORLDSTATE_TANARIS packet.AddState(2263, 0); // SCOURGE_EVENT_WORLDSTATE_TANARIS
packet.AddState(2262, 0); // SCOURGE_EVENT_WORLDSTATE_BURNING_STEPPES packet.AddState(2262, 0); // SCOURGE_EVENT_WORLDSTATE_BURNING_STEPPES
+2
View File
@@ -115,4 +115,6 @@ public static class Global
public static SupportManager SupportMgr { get { return SupportManager.Instance; } } public static SupportManager SupportMgr { get { return SupportManager.Instance; } }
public static WardenCheckManager WardenCheckMgr { get { return WardenCheckManager.Instance; } } public static WardenCheckManager WardenCheckMgr { get { return WardenCheckManager.Instance; } }
public static BlackMarketManager BlackMarketMgr { get { return BlackMarketManager.Instance; } } public static BlackMarketManager BlackMarketMgr { get { return BlackMarketManager.Instance; } }
public static WorldStateManager WorldStateMgr { get { return WorldStateManager.Instance; } }
} }
+27
View File
@@ -87,6 +87,8 @@ namespace Game.Maps
Global.MMapMgr.LoadMapInstance(Global.WorldMgr.GetDataPath(), GetId(), i_InstanceId); Global.MMapMgr.LoadMapInstance(Global.WorldMgr.GetDataPath(), GetId(), i_InstanceId);
_worldStateValues = Global.WorldStateMgr.GetInitialWorldStatesForMap(this);
Global.ScriptMgr.OnCreateMap(this); Global.ScriptMgr.OnCreateMap(this);
} }
@@ -574,6 +576,29 @@ namespace Game.Maps
Cell cell = new(player.GetPositionX(), player.GetPositionY()); Cell cell = new(player.GetPositionX(), player.GetPositionY());
GetMultiPersonalPhaseTracker().OnOwnerPhaseChanged(player, GetGrid(cell.GetGridX(), cell.GetGridY()), this, cell); GetMultiPersonalPhaseTracker().OnOwnerPhaseChanged(player, GetGrid(cell.GetGridX(), cell.GetGridY()), this, cell);
} }
public int GetWorldStateValue(int worldStateId)
{
return _worldStateValues.LookupByKey(worldStateId);
}
public Dictionary<int, int> GetWorldStateValues() { return _worldStateValues; }
public void SetWorldStateValue(int worldStateId, int value)
{
int oldValue = _worldStateValues.LookupByKey(worldStateId);
_worldStateValues[worldStateId] = value;
WorldStateTemplate worldStateTemplate = Global.WorldStateMgr.GetWorldStateTemplate(worldStateId);
if (worldStateTemplate != null)
Global.ScriptMgr.OnWorldStateValueChange(worldStateTemplate, oldValue, value, this);
// Broadcast update to all players on the map
UpdateWorldState updateWorldState = new();
updateWorldState.VariableID = (uint)worldStateId;
updateWorldState.Value = value;
SendToPlayers(updateWorldState);
}
void InitializeObject(WorldObject obj) void InitializeObject(WorldObject obj)
{ {
@@ -5257,6 +5282,8 @@ namespace Game.Maps
Queue<FarSpellCallback> _farSpellCallbacks = new(); Queue<FarSpellCallback> _farSpellCallbacks = new();
MultiPersonalPhaseTracker _multiPersonalPhaseTracker = new(); MultiPersonalPhaseTracker _multiPersonalPhaseTracker = new();
Dictionary<int, int> _worldStateValues = new();
#endregion #endregion
} }
@@ -48,6 +48,11 @@ namespace Game.Networking.Packets
Worldstates.Add(new WorldStateInfo(variableID, (int)value)); Worldstates.Add(new WorldStateInfo(variableID, (int)value));
} }
public void AddState(int variableID, int value)
{
Worldstates.Add(new WorldStateInfo((uint)variableID, value));
}
public void AddState(WorldStates variableID, bool value) public void AddState(WorldStates variableID, bool value)
{ {
AddState((uint)variableID, value); AddState((uint)variableID, value);
+13
View File
@@ -843,4 +843,17 @@ namespace Game.Scripting
// Called when a quest objective data change // Called when a quest objective data change
public virtual void OnQuestObjectiveChange(Player player, Quest quest, QuestObjective objective, int oldAmount, int newAmount) { } public virtual void OnQuestObjectiveChange(Player player, Quest quest, QuestObjective objective, int oldAmount, int newAmount) { }
} }
public class WorldStateScript : ScriptObject
{
public WorldStateScript(string name) : base(name)
{
Global.ScriptMgr.AddScript(this);
}
public override bool IsDatabaseBound() { return true; }
// Called when worldstate changes value, map is optional
public virtual void OnValueChange(int worldStateId, int oldValue, int newValue, Map map) { }
}
} }
+8
View File
@@ -1198,6 +1198,14 @@ namespace Game.Scripting
RunScript<QuestScript>(script => script.OnQuestObjectiveChange(player, quest, objective, oldAmount, newAmount), quest.ScriptId); RunScript<QuestScript>(script => script.OnQuestObjectiveChange(player, quest, objective, oldAmount, newAmount), quest.ScriptId);
} }
// WorldState
public void OnWorldStateValueChange(WorldStateTemplate worldStateTemplate, int oldValue, int newValue, Map map)
{
Cypher.Assert(worldStateTemplate != null);
RunScript<WorldStateScript>(script => script.OnValueChange(worldStateTemplate.Id, oldValue, newValue, map), worldStateTemplate.ScriptId);
}
public void ForEach<T>(Action<T> a) where T : ScriptObject public void ForEach<T>(Action<T> a) where T : ScriptObject
{ {
var reg = GetScriptRegistry<T>(); var reg = GetScriptRegistry<T>();
@@ -889,6 +889,9 @@ namespace Game
Log.outInfo(LogFilter.ServerLoading, "Loading Creature Formations..."); Log.outInfo(LogFilter.ServerLoading, "Loading Creature Formations...");
FormationMgr.LoadCreatureFormations(); FormationMgr.LoadCreatureFormations();
Log.outInfo(LogFilter.ServerLoading, "Loading World State templates...");
Global.WorldStateMgr.LoadFromDB();
Log.outInfo(LogFilter.ServerLoading, "Loading World States..."); // must be loaded before Battleground, outdoor PvP and conditions Log.outInfo(LogFilter.ServerLoading, "Loading World States..."); // must be loaded before Battleground, outdoor PvP and conditions
LoadWorldStates(); LoadWorldStates();
+138
View File
@@ -0,0 +1,138 @@
/*
* Copyright (C) 2012-2020 CypherCore <http://github.com/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 <http://www.gnu.org/licenses/>.
*/
using Framework.Database;
using Game.Maps;
using Game.Networking.Packets;
using System.Collections.Generic;
namespace Game
{
public class WorldStateManager : Singleton<WorldStateManager>
{
Dictionary<int, WorldStateTemplate> _worldStateTemplates = new();
Dictionary<int, int> _realmWorldStateValues = new();
Dictionary<uint, Dictionary<int, int>> _worldStatesByMap = new();
WorldStateManager() { }
public void LoadFromDB()
{
uint oldMSTime = Time.GetMSTime();
// 0 1 2 3
SQLResult result = DB.World.Query("SELECT ID, DefaultValue, MapID, ScriptName FROM world_state");
if (result.IsEmpty())
return;
do
{
int id = result.Read<int>(0);
WorldStateTemplate worldState = new();
worldState.Id = id;
worldState.DefaultValue = result.Read<int>(1);
if (!result.IsNull(2))
worldState.MapId = result.Read<uint>(2);
worldState.ScriptId = Global.ObjectMgr.GetScriptId(result.Read<string>(3));
if (worldState.MapId.HasValue)
{
if (!_worldStatesByMap.ContainsKey(worldState.MapId.Value))
_worldStatesByMap[worldState.MapId.Value] = new();
_worldStatesByMap[worldState.MapId.Value][id] = worldState.DefaultValue;
}
else
_realmWorldStateValues[id] = worldState.DefaultValue;
_worldStateTemplates[id] = worldState;
} while (result.NextRow());
Log.outInfo(LogFilter.ServerLoading, $"Loaded {_worldStateTemplates.Count} world state templates {Time.GetMSTimeDiffToNow(oldMSTime)} ms");
}
public WorldStateTemplate GetWorldStateTemplate(int worldStateId)
{
return _worldStateTemplates.LookupByKey(worldStateId);
}
public int GetValue(int worldStateId, Map map)
{
WorldStateTemplate worldStateTemplate = GetWorldStateTemplate(worldStateId);
if (worldStateTemplate == null || !worldStateTemplate.MapId.HasValue)
return _realmWorldStateValues.LookupByKey(worldStateId);
if (map.GetId() != worldStateTemplate.MapId)
return 0;
return map.GetWorldStateValue(worldStateId);
}
public void SetValue(int worldStateId, int value, Map map)
{
WorldStateTemplate worldStateTemplate = GetWorldStateTemplate(worldStateId);
if (worldStateTemplate == null || !worldStateTemplate.MapId.HasValue)
{
int oldValue = _realmWorldStateValues.LookupByKey(worldStateId);
_realmWorldStateValues[worldStateId] = value;
if (worldStateTemplate != null)
Global.ScriptMgr.OnWorldStateValueChange(worldStateTemplate, oldValue, value, null);
// Broadcast update to all players on the server
UpdateWorldState updateWorldState = new();
updateWorldState.VariableID = (uint)worldStateId;
updateWorldState.Value = value;
Global.WorldMgr.SendGlobalMessage(updateWorldState);
return;
}
if (map.GetId() != worldStateTemplate.MapId)
return;
map.SetWorldStateValue(worldStateId, value);
}
public Dictionary<int, int> GetInitialWorldStatesForMap(Map map)
{
if (_worldStatesByMap.TryGetValue(map.GetId(), out Dictionary<int, int> initialValues))
return initialValues;
return new Dictionary<int, int>();
}
public void FillInitialWorldStates(InitWorldStates initWorldStates, Map map)
{
foreach (var (worldStateId, value) in _realmWorldStateValues)
initWorldStates.AddState(worldStateId, value);
foreach (var (worldStateId, value) in map.GetWorldStateValues())
initWorldStates.AddState(worldStateId, value);
}
}
public class WorldStateTemplate
{
public int Id;
public int DefaultValue;
public uint ScriptId;
public uint? MapId;
}
}
@@ -0,0 +1,19 @@
--
-- Table structure for table `world_state`
--
DROP TABLE IF EXISTS `world_state`;
CREATE TABLE `world_state` (
`ID` int NOT NULL,
`DefaultValue` int NOT NULL,
`MapID` int unsigned DEFAULT NULL,
`ScriptName` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`Comment` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT INTO `world_state` (`ID`, `DefaultValue`, `MapID`, `Comment`) VALUES
(5644, 0, 669, 'Blackwing Descent - Omnotron Defense System - Achieve-a-tron'),
(5645, 0, 669, 'Blackwing Descent - Omnotron Defense System - Achieve-a-tron'),
(5646, 0, 669, 'Blackwing Descent - Omnotron Defense System - Achieve-a-tron'),
(5647, 0, 669, 'Blackwing Descent - Omnotron Defense System - Achieve-a-tron'),
(5117, 0, 670, 'Grim Batol - Erudax - Don\'t need to Break Eggs to Make an Omelet');