Bulk load grids at repop and zone init instead of 2 costly individual selects per NPC, create repositories to decouple database logic from business logic

This commit is contained in:
Akkadius
2020-03-30 05:49:43 -05:00
parent 128cc458fd
commit 43716332aa
7 changed files with 298 additions and 40 deletions
+1 -1
View File
@@ -303,7 +303,7 @@ public:
int GetMaxWp() const { return max_wp; }
void DisplayWaypointInfo(Client *to);
void CalculateNewWaypoint();
void AssignWaypoints(int32 grid, int start_wp = 0);
void AssignWaypoints(int32 grid_id, int start_wp = 0);
void SetWaypointPause();
void UpdateWaypoint(int wp_index);
+32 -39
View File
@@ -561,62 +561,54 @@ void Mob::StopNavigation() {
mMovementManager->StopNavigation(this);
}
void NPC::AssignWaypoints(int32 grid, int start_wp)
void NPC::AssignWaypoints(int32 grid_id, int start_wp)
{
if (grid == 0)
if (grid_id == 0)
return; // grid ID 0 not supported
if (grid < 0) {
if (grid_id < 0) {
// Allow setting negative grid values for pausing pathing
this->CastToNPC()->SetGrid(grid);
this->CastToNPC()->SetGrid(grid_id);
return;
}
Waypoints.clear();
roamer = false;
// Retrieve the wander and pause types for this grid
std::string query = StringFormat("SELECT `type`, `type2` FROM `grid` WHERE `id` = %i AND `zoneid` = %i", grid,
zone->GetZoneID());
auto results = content_db.QueryDatabase(query);
if (!results.Success()) {
auto grid_entry = GridRepository::GetGrid(zone->grids, grid_id);
if (grid_entry.id == 0) {
return;
}
if (results.RowCount() == 0)
return;
wandertype = grid_entry.type;
pausetype = grid_entry.type2;
auto row = results.begin();
wandertype = atoi(row[0]);
pausetype = atoi(row[1]);
SetGrid(grid); // Assign grid number
// Retrieve all waypoints for this grid
query = StringFormat("SELECT `x`,`y`,`z`,`pause`,`heading`, `centerpoint` "
"FROM grid_entries WHERE `gridid` = %i AND `zoneid` = %i "
"ORDER BY `number`", grid, zone->GetZoneID());
results = content_db.QueryDatabase(query);
if (!results.Success()) {
return;
}
SetGrid(grid_id); // Assign grid number
roamer = true;
max_wp = 0; // Initialize it; will increment it for each waypoint successfully added to the list
for (auto row = results.begin(); row != results.end(); ++row, ++max_wp)
{
wplist newwp;
newwp.index = max_wp;
newwp.x = atof(row[0]);
newwp.y = atof(row[1]);
newwp.z = atof(row[2]);
for (auto &entry : zone->grid_entries) {
if (entry.gridid == grid_id) {
wplist new_waypoint{};
new_waypoint.index = max_wp;
new_waypoint.x = entry.x;
new_waypoint.y = entry.y;
new_waypoint.z = entry.z;
new_waypoint.pause = entry.pause;
new_waypoint.heading = entry.heading;
new_waypoint.centerpoint = entry.centerpoint;
newwp.pause = atoi(row[3]);
newwp.heading = atof(row[4]);
newwp.centerpoint = atobool(row[5]);
Waypoints.push_back(newwp);
LogPathing(
"Loading Grid [{}] number [{}] name [{}]",
grid_id,
entry.number,
GetCleanName()
);
Waypoints.push_back(new_waypoint);
max_wp++;
}
}
cur_wp = start_wp;
@@ -628,8 +620,9 @@ void NPC::AssignWaypoints(int32 grid, int start_wp)
patrol = cur_wp;
}
if (wandertype == GridRandom10 || wandertype == GridRandom || wandertype == GridRand5LoS)
if (wandertype == GridRandom10 || wandertype == GridRandom || wandertype == GridRand5LoS) {
CalculateNewWaypoint();
}
}
@@ -737,7 +730,7 @@ void Mob::FixZ(int32 z_find_offset /*= 5*/, bool fix_client_z /*= false*/) {
if (IsClient() && !fix_client_z) {
return;
}
if (flymode == GravityBehavior::Flying) {
return;
}
+9
View File
@@ -1023,6 +1023,7 @@ bool Zone::Init(bool iStaticZone) {
LogInfo("Init Finished: ZoneID = [{}], Time Offset = [{}]", zoneid, zone->zone_time.getEQTimeZone());
LoadGrids();
LoadTickItems();
//MODDING HOOK FOR ZONE INIT
@@ -1610,6 +1611,8 @@ void Zone::Repop(uint32 delay)
if (!content_db.PopulateZoneSpawnList(zoneid, spawn2_list, GetInstanceVersion(), delay))
LogDebug("Error in Zone::Repop: database.PopulateZoneSpawnList failed");
LoadGrids();
initgrids_timer.Start();
entity_list.UpdateAllTraps(true, true);
@@ -2481,3 +2484,9 @@ void Zone::SetQuestHotReloadQueued(bool in_quest_hot_reload_queued)
{
quest_hot_reload_queued = in_quest_hot_reload_queued;
}
void Zone::LoadGrids()
{
grids = GridRepository::GetZoneGrids(GetZoneID());
grid_entries = GridEntriesRepository::GetZoneGridEntries(GetZoneID());
}
+7
View File
@@ -24,6 +24,9 @@
#include "../common/types.h"
#include "../common/random.h"
#include "../common/string_util.h"
#include "zonedb.h"
#include "../common/repositories/grid_repository.h"
#include "../common/repositories/grid_entries_repository.h"
#include "qglobals.h"
#include "spawn2.h"
#include "spawngroup.h"
@@ -202,6 +205,9 @@ public:
std::unordered_map<int, std::unique_ptr<AA::Ability>> aa_abilities;
std::unordered_map<int, std::unique_ptr<AA::Rank>> aa_ranks;
std::vector<GridRepository::Grid> grids;
std::vector<GridEntriesRepository::GridEntry> grid_entries;
time_t weather_timer;
Timer spawn2_timer;
Timer hot_reload_timer;
@@ -239,6 +245,7 @@ public:
void LoadLDoNTrapEntries();
void LoadLDoNTraps();
void LoadLevelEXPMods();
void LoadGrids();
void LoadMercSpells();
void LoadMercTemplates();
void LoadNewMerchantData(uint32 merchantid);