AssignWaypoints converted to QueryDatabase

This commit is contained in:
Arthur Ice 2014-08-20 13:58:36 -07:00
parent 87efd22394
commit 5fec840f06

View File

@ -869,55 +869,45 @@ void NPC::AssignWaypoints(int32 grid) {
return; return;
} }
char errbuf[MYSQL_ERRMSG_SIZE];
char *query = 0;
MYSQL_RES *result;
MYSQL_ROW row;
bool GridErr = false, WPErr = false;
Waypoints.clear(); Waypoints.clear();
roamer = false;
// Retrieve the wander and pause types for this grid // Retrieve the wander and pause types for this grid
if(database.RunQuery(query,MakeAnyLenString(&query,"SELECT `type`,`type2` FROM `grid` WHERE `id`=%i AND `zoneid`=%i",grid,zone->GetZoneID()),errbuf, &result)) std::string query = StringFormat("SELECT `type`, `type2` FROM `grid` WHERE `id` = %i AND `zoneid` = %i", grid, zone->GetZoneID());
{ auto results = database.QueryDatabase(query);
if((row = mysql_fetch_row(result))) if (!results.Success()) {
{ LogFile->write(EQEMuLog::Error, "MySQL Error while trying to assign grid %u to mob %s: %s", grid, name, results.ErrorMessage().c_str());
if(row[0] != 0) return;
wandertype = atoi(row[0]);
else
wandertype = 0;
if(row[1] != 0)
pausetype = atoi(row[1]);
else
pausetype = 0;
} }
else // No grid record found in this zone for the given ID
GridErr = true;
mysql_free_result(result);
}
else // DB query error!
{
GridErr = true;
LogFile->write(EQEMuLog::Error, "MySQL Error while trying to assign grid %u to mob %s: %s", grid, name, errbuf);
}
safe_delete_array(query);
if(!GridErr) if (results.RowCount() == 0)
{ return;
auto row = results.begin();
wandertype = atoi(row[0]);
pausetype = atoi(row[1]);
this->CastToNPC()->SetGrid(grid); // Assign grid number this->CastToNPC()->SetGrid(grid); // Assign grid number
// Retrieve all waypoints for this grid // Retrieve all waypoints for this grid
if(database.RunQuery(query,MakeAnyLenString(&query,"SELECT `x`,`y`,`z`,`pause`,`heading` FROM grid_entries WHERE `gridid`=%i AND `zoneid`=%i ORDER BY `number`",grid,zone->GetZoneID()),errbuf,&result)) query = StringFormat("SELECT `x`,`y`,`z`,`pause`,`heading` "
{ "FROM grid_entries WHERE `gridid` = %i AND `zoneid` = %i "
roamer = true; "ORDER BY `number`", grid, zone->GetZoneID());
max_wp = -1; // Initialize it; will increment it for each waypoint successfully added to the list results = database.QueryDatabase(query);
if (!results.Success()) {
LogFile->write(EQEMuLog::Error, "MySQL Error while trying to assign waypoints from grid %u to mob %s: %s", grid, name, results.ErrorMessage().c_str());
return;
}
while((row = mysql_fetch_row(result))) roamer = true;
{ max_wp = 0; // Initialize it; will increment it for each waypoint successfully added to the list
if(row[0] != 0 && row[1] != 0 && row[2] != 0 && row[3] != 0)
for (auto row = results.begin(); row != results.end(); ++row, ++max_wp)
{ {
wplist newwp; wplist newwp;
newwp.index = ++max_wp; newwp.index = max_wp;
newwp.x = atof(row[0]); newwp.x = atof(row[0]);
newwp.y = atof(row[1]); newwp.y = atof(row[1]);
newwp.z = atof(row[2]); newwp.z = atof(row[2]);
@ -940,26 +930,17 @@ void NPC::AssignWaypoints(int32 grid) {
newwp.heading = atof(row[4]); newwp.heading = atof(row[4]);
Waypoints.push_back(newwp); Waypoints.push_back(newwp);
} }
}
mysql_free_result(result);
}
else // DB query error!
{
WPErr = true;
LogFile->write(EQEMuLog::Error, "MySQL Error while trying to assign waypoints from grid %u to mob %s: %s", grid, name, errbuf);
}
safe_delete_array(query);
} // end if (!GridErr)
if(Waypoints.size() < 2) { if(Waypoints.size() < 2) {
roamer = false; roamer = false;
} else if(!GridErr && !WPErr) { }
UpdateWaypoint(0); UpdateWaypoint(0);
SetWaypointPause(); SetWaypointPause();
if (wandertype == 1 || wandertype == 2 || wandertype == 5) if (wandertype == 1 || wandertype == 2 || wandertype == 5)
CalculateNewWaypoint(); CalculateNewWaypoint();
} else {
roamer = false;
}
} }
void Mob::SendTo(float new_x, float new_y, float new_z) { void Mob::SendTo(float new_x, float new_y, float new_z) {