Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented fallback checking for creature_template_difficulty entries #847

Merged
merged 3 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions WowPacketParser/SQL/SQLDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static class SQLDatabase
public static Dictionary<uint /*broadcastText*/, List<uint> /*npc_text ids*/> BroadcastToNPCTexts { get; } = new();
public static Dictionary<int /*menuID*/, List<uint> /*npc_text ids*/> GossipMenuToNPCTexts { get; } = new();
public static Dictionary<int /*worldStateID*/, string> WorldStateNames { get; } = new();
public static Dictionary<(uint /*CreatureId*/, uint /*DifficultyID*/), CreatureTemplateDifficultyWDB> CreatureTemplateDifficultyWDBData = new();
public static List<POIData> POIs { get; } = new List<POIData>();

private static readonly StoreNameType[] ObjectTypes =
Expand Down Expand Up @@ -109,6 +110,7 @@ public static void LoadSQL()
LoadBroadcastText();
LoadPointsOfinterest();
LoadCreatureEquipment();
LoadCreatureTemplateDifficultyWDBData();
LoadNPCTexts();
LoadGossipMenuNPCTexts();
LoadWorldStates();
Expand Down Expand Up @@ -285,6 +287,70 @@ private static void LoadCreatureEquipment()
}
}

private static void LoadCreatureTemplateDifficultyWDBData()
{
if (Settings.TargetedDatabase < TargetedDatabase.Dragonflight || !Settings.DBEnabled)
return;

string columns = "Entry, DifficultyID, HealthScalingExpansion, HealthModifier, ManaModifier, CreatureDifficultyID, TypeFlags, TypeFlags2";
string query = $"SELECT {columns} FROM {Settings.TDBDatabase}.creature_template_difficulty";

using (var command = SQLConnector.CreateCommand(query))
{
if (command == null)
return;
using (MySqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
var data = new CreatureTemplateDifficultyWDB
{
Entry = reader.GetUInt32("Entry"),
DifficultyID = reader.GetUInt32("DifficultyID"),
HealthScalingExpansion = (ClientType)reader.GetInt32("HealthScalingExpansion"),
HealthModifier = reader.GetFloat("HealthModifier"),
ManaModifier = reader.GetFloat("ManaModifier"),
CreatureDifficultyID = reader.GetInt32("CreatureDifficultyID"),
TypeFlags = (CreatureTypeFlag)reader.GetUInt32("TypeFlags"),
TypeFlags2 = reader.GetUInt32("TypeFlags2")
};
CreatureTemplateDifficultyWDBData.Add((data.Entry.Value, data.DifficultyID.Value), data);
}
}
}
}

public static CreatureTemplateDifficultyWDB CheckCreatureTemplateDifficultyWDBFallbacks(CreatureTemplateDifficultyWDB sniffData, uint difficulty)
{
// if db disabled/empty simply return sniff data
if (CreatureTemplateDifficultyWDBData.Count == 0)
return sniffData;

// entry with same difficulty already exists
if (CreatureTemplateDifficultyWDBData.TryGetValue((sniffData.Entry.Value, difficulty), out var dbData))
{
// data is equal, return sniffData to update
if (sniffData.WDBEqualsSkipDifficultySkipHealthScalingExpansion(dbData))
{
sniffData.DifficultyID = dbData.DifficultyID;
return sniffData;
}

// data is not equal, insert new row
return sniffData;
}
// entry with same difficulty does not exist, check fallback difficulties recursively
else
{
if (!Settings.UseDBC || DBC.DBC.Difficulty == null)
return sniffData;

if (DBC.DBC.Difficulty.TryGetValue((int)difficulty, out var difficultyEntry))
return CheckCreatureTemplateDifficultyWDBFallbacks(sniffData, difficultyEntry.FallbackDifficultyID);
}
return sniffData;
}

private static void LoadNPCTexts()
{
if (Settings.TargetedDatabase == TargetedDatabase.TheBurningCrusade)
Expand Down
14 changes: 14 additions & 0 deletions WowPacketParser/Store/Objects/CreatureTemplateDifficulty.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using WowPacketParser.Enums;
using WowPacketParser.Misc;
using WowPacketParser.SQL;
Expand Down Expand Up @@ -31,6 +32,19 @@ public sealed record CreatureTemplateDifficultyWDB : IDataModel

[DBFieldName("TypeFlags2", TargetedDatabaseFlag.SinceDragonflight | TargetedDatabaseFlag.WotlkClassic)]
public uint? TypeFlags2;

public bool WDBEqualsSkipDifficultySkipHealthScalingExpansion(CreatureTemplateDifficultyWDB rhs)
{
if (rhs == null)
return false;

return Entry == rhs.Entry &&
Math.Abs(HealthModifier.Value - rhs.HealthModifier.Value) < 0.01f &&
Math.Abs(ManaModifier.Value - rhs.ManaModifier.Value) < 0.01f &&
CreatureDifficultyID == rhs.CreatureDifficultyID &&
TypeFlags == rhs.TypeFlags &&
TypeFlags2 == rhs.TypeFlags2;
}
}

[DBTableName("creature_template_scaling", TargetedDatabaseFlag.TillShadowlands)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public static void HandleCreatureQueryResponse(Packet packet)
TypeFlags = creature.TypeFlags,
TypeFlags2 = creature.TypeFlags2
};
creatureTemplateDifficultyWDB = WowPacketParser.SQL.SQLDatabase.CheckCreatureTemplateDifficultyWDBFallbacks(creatureTemplateDifficultyWDB, creatureTemplateDifficultyWDB.DifficultyID.Value);
Storage.CreatureTemplateDifficultiesWDB.Add(creatureTemplateDifficultyWDB);

ObjectName objectName = new ObjectName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ public static void HandleCreatureQueryResponse(Packet packet)
TypeFlags = creature.TypeFlags,
TypeFlags2 = creature.TypeFlags2
};
creatureTemplateDifficultyWDB = WowPacketParser.SQL.SQLDatabase.CheckCreatureTemplateDifficultyWDBFallbacks(creatureTemplateDifficultyWDB, creatureTemplateDifficultyWDB.DifficultyID.Value);
Storage.CreatureTemplateDifficultiesWDB.Add(creatureTemplateDifficultyWDB);

ObjectName objectName = new ObjectName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ public static void HandleCreatureQueryResponse(Packet packet)
TypeFlags = creature.TypeFlags,
TypeFlags2 = creature.TypeFlags2
};
creatureTemplateDifficultyWDB = WowPacketParser.SQL.SQLDatabase.CheckCreatureTemplateDifficultyWDBFallbacks(creatureTemplateDifficultyWDB, creatureTemplateDifficultyWDB.DifficultyID.Value);
Storage.CreatureTemplateDifficultiesWDB.Add(creatureTemplateDifficultyWDB);

ObjectName objectName = new ObjectName
Expand Down
Loading