[7297] Fixed profession spells sorting in trainer spell list at client.
[getmangos.git] / src / game / WaypointManager.cpp
blobd8a641cc30adc3843b6b10291378fe9e447687b0
1 /*
2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "Database/DatabaseEnv.h"
20 #include "GridDefines.h"
21 #include "Policies/SingletonImp.h"
22 #include "WaypointManager.h"
23 #include "ProgressBar.h"
24 #include "MapManager.h"
25 #include "ObjectMgr.h"
27 INSTANTIATE_SINGLETON_1(WaypointManager);
29 bool WaypointBehavior::isEmpty()
31 if (emote || spell || model1 || model2)
32 return false;
34 for(int i = 0; i < MAX_WAYPOINT_TEXT; ++i)
35 if(textid[i])
36 return false;
38 return true;
41 WaypointBehavior::WaypointBehavior(const WaypointBehavior &b)
43 emote = b.emote;
44 spell = b.spell;
45 model1 = b.model1;
46 model2 = b.model2;
47 for(int i=0; i < MAX_WAYPOINT_TEXT; ++i)
48 textid[i] = b.textid[i];
51 void WaypointManager::Load()
53 Cleanup();
55 uint32 total_paths = 0;
56 uint32 total_nodes = 0;
57 uint32 total_behaviors = 0;
59 QueryResult *result = WorldDatabase.Query("SELECT id, COUNT(point) FROM creature_movement GROUP BY id");
61 if(!result)
63 barGoLink bar(1);
64 bar.step();
65 sLog.outString();
66 sLog.outString( ">> Loaded 0 paths. DB table `creature_movement` is empty." );
67 return;
68 } else {
69 total_paths = result->GetRowCount();
70 barGoLink bar( total_paths );
73 bar.step();
74 Field *fields = result->Fetch();
75 uint32 id = fields[0].GetUInt32();
76 uint32 count = fields[1].GetUInt32();
77 m_pathMap[id].resize(count);
78 total_nodes += count;
79 } while( result->NextRow() );
80 delete result;
82 sLog.outString();
83 sLog.outString( ">> Paths loaded" );
86 result = WorldDatabase.Query("SELECT position_x, position_y, position_z, orientation, model1, model2, waittime, emote, spell, textid1, textid2, textid3, textid4, textid5, id, point FROM creature_movement");
88 barGoLink bar( result->GetRowCount() );
91 bar.step();
92 Field *fields = result->Fetch();
93 uint32 point = fields[15].GetUInt32();
94 uint32 id = fields[14].GetUInt32();
95 WaypointPath &path = m_pathMap[id];
96 // the cleanup queries make sure the following is true
97 assert(point >= 1 && point <= path.size());
98 WaypointNode &node = path[point-1];
100 node.x = fields[0].GetFloat();
101 node.y = fields[1].GetFloat();
102 node.z = fields[2].GetFloat();
103 node.orientation = fields[3].GetFloat();
104 node.delay = fields[6].GetUInt16();
106 // prevent using invalid coordinates
107 if(!MaNGOS::IsValidMapCoord(node.x, node.y, node.z, node.orientation))
109 QueryResult *result1 = WorldDatabase.PQuery("SELECT id, map FROM creature WHERE guid = '%u'", id);
110 if(result1)
111 sLog.outErrorDb("ERROR: Creature (guidlow %d, entry %d) have invalid coordinates in his waypoint %d (X: %f, Y: %f).",
112 id, result1->Fetch()[0].GetUInt32(), point, node.x, node.y);
113 else
114 sLog.outErrorDb("ERROR: Waypoint path %d, have invalid coordinates in his waypoint %d (X: %f, Y: %f).",
115 id, point, node.x, node.y);
117 MaNGOS::NormalizeMapCoord(node.x);
118 MaNGOS::NormalizeMapCoord(node.y);
119 if(result1)
121 node.z = MapManager::Instance ().GetBaseMap(result1->Fetch()[1].GetUInt32())->GetHeight(node.x, node.y, node.z);
122 delete result1;
124 WorldDatabase.PExecute("UPDATE creature_movement SET position_x = '%f', position_y = '%f', position_z = '%f' WHERE id = '%u' AND point = '%u'", node.x, node.y, node.z, id, point);
126 WaypointBehavior be;
127 be.model1 = fields[4].GetUInt32();
128 be.model2 = fields[5].GetUInt32();
129 be.emote = fields[7].GetUInt32();
130 be.spell = fields[8].GetUInt32();
131 for(int i = 0; i < MAX_WAYPOINT_TEXT; ++i)
133 be.textid[i] = fields[9+i].GetUInt32();
134 if(be.textid[i])
136 if (be.textid[i] < MIN_DB_SCRIPT_STRING_ID || be.textid[i] >= MAX_DB_SCRIPT_STRING_ID)
138 sLog.outErrorDb( "Table `db_script_string` not have string id %u", be.textid[i]);
139 continue;
144 // save memory by not storing empty behaviors
145 if(!be.isEmpty())
147 node.behavior = new WaypointBehavior(be);
148 ++total_behaviors;
150 else
151 node.behavior = NULL;
152 } while( result->NextRow() );
153 delete result;
155 sLog.outString();
156 sLog.outString( ">> Waypoints and behaviors loaded" );
157 sLog.outString();
158 sLog.outString( ">>> Loaded %u paths, %u nodes and %u behaviors", total_paths, total_nodes, total_behaviors);
161 void WaypointManager::Cleanup()
163 // check if points need to be renumbered and do it
164 if(QueryResult *result = WorldDatabase.Query("SELECT 1 from creature_movement As T WHERE point <> (SELECT COUNT(*) FROM creature_movement WHERE id = T.id AND point <= T.point) LIMIT 1"))
166 delete result;
167 WorldDatabase.DirectExecute("CREATE TEMPORARY TABLE temp LIKE creature_movement");
168 WorldDatabase.DirectExecute("INSERT INTO temp SELECT * FROM creature_movement");
169 WorldDatabase.DirectExecute("ALTER TABLE creature_movement DROP PRIMARY KEY");
170 WorldDatabase.DirectExecute("UPDATE creature_movement AS T SET point = (SELECT COUNT(*) FROM temp WHERE id = T.id AND point <= T.point)");
171 WorldDatabase.DirectExecute("ALTER TABLE creature_movement ADD PRIMARY KEY (id, point)");
172 WorldDatabase.DirectExecute("DROP TABLE temp");
173 assert(!(result = WorldDatabase.Query("SELECT 1 from creature_movement As T WHERE point <> (SELECT COUNT(*) FROM creature_movement WHERE id = T.id AND point <= T.point) LIMIT 1")));
177 void WaypointManager::Unload()
179 for(WaypointPathMap::iterator itr = m_pathMap.begin(); itr != m_pathMap.end(); ++itr)
180 _clearPath(itr->second);
181 m_pathMap.clear();
184 void WaypointManager::_clearPath(WaypointPath &path)
186 for(WaypointPath::iterator itr = path.begin(); itr != path.end(); ++itr)
187 if(itr->behavior)
188 delete itr->behavior;
189 path.clear();
192 /// - Insert after the last point
193 void WaypointManager::AddLastNode(uint32 id, float x, float y, float z, float o, uint32 delay, uint32 wpGuid)
195 _addNode(id, GetLastPoint(id, 0) + 1, x, y, z, o, delay, wpGuid);
198 /// - Insert after a certain point
199 void WaypointManager::AddAfterNode(uint32 id, uint32 point, float x, float y, float z, float o, uint32 delay, uint32 wpGuid)
201 for(uint32 i = GetLastPoint(id, 0); i > point; i--)
202 WorldDatabase.PExecuteLog("UPDATE creature_movement SET point=point+1 WHERE id='%u' AND point='%u'", id, i);
204 _addNode(id, point + 1, x, y, z, o, delay, wpGuid);
207 /// - Insert without checking for collision
208 void WaypointManager::_addNode(uint32 id, uint32 point, float x, float y, float z, float o, uint32 delay, uint32 wpGuid)
210 if(point == 0) return; // counted from 1 in the DB
211 WorldDatabase.PExecuteLog("INSERT INTO creature_movement (id,point,position_x,position_y,position_z,orientation,wpguid,waittime) VALUES ('%u','%u','%f', '%f', '%f', '%f', '%d', '%d')", id, point, x, y, z, o, wpGuid, delay);
212 WaypointPathMap::iterator itr = m_pathMap.find(id);
213 if(itr == m_pathMap.end())
214 itr = m_pathMap.insert(WaypointPathMap::value_type(id, WaypointPath())).first;
215 itr->second.insert(itr->second.begin() + (point - 1), WaypointNode(x, y, z, o, delay, NULL));
218 uint32 WaypointManager::GetLastPoint(uint32 id, uint32 default_notfound)
220 uint32 point = default_notfound;
221 /*QueryResult *result = WorldDatabase.PQuery( "SELECT MAX(point) FROM creature_movement WHERE id = '%u'", id);
222 if( result )
224 point = (*result)[0].GetUInt32()+1;
225 delete result;
227 WaypointPathMap::iterator itr = m_pathMap.find(id);
228 if(itr != m_pathMap.end() && itr->second.size() != 0)
229 point = itr->second.size();
230 return point;
233 void WaypointManager::DeleteNode(uint32 id, uint32 point)
235 if(point == 0) return; // counted from 1 in the DB
236 WorldDatabase.PExecuteLog("DELETE FROM creature_movement WHERE id='%u' AND point='%u'", id, point);
237 WorldDatabase.PExecuteLog("UPDATE creature_movement SET point=point-1 WHERE id='%u' AND point>'%u'", id, point);
238 WaypointPathMap::iterator itr = m_pathMap.find(id);
239 if(itr != m_pathMap.end() && point <= itr->second.size())
240 itr->second.erase(itr->second.begin() + (point-1));
243 void WaypointManager::DeletePath(uint32 id)
245 WorldDatabase.PExecuteLog("DELETE FROM creature_movement WHERE id='%u'", id);
246 WaypointPathMap::iterator itr = m_pathMap.find(id);
247 if(itr != m_pathMap.end())
248 _clearPath(itr->second);
249 // the path is not removed from the map, just cleared
250 // WMGs have pointers to the path, so deleting them would crash
251 // this wastes some memory, but these functions are
252 // only meant to be called by GM commands
255 void WaypointManager::SetNodePosition(uint32 id, uint32 point, float x, float y, float z)
257 if(point == 0) return; // counted from 1 in the DB
258 WorldDatabase.PExecuteLog("UPDATE creature_movement SET position_x = '%f',position_y = '%f',position_z = '%f' where id = '%u' AND point='%u'", x, y, z, id, point);
259 WaypointPathMap::iterator itr = m_pathMap.find(id);
260 if(itr != m_pathMap.end() && point <= itr->second.size())
262 itr->second[point-1].x = x;
263 itr->second[point-1].y = y;
264 itr->second[point-1].z = z;
268 void WaypointManager::SetNodeText(uint32 id, uint32 point, const char *text_field, const char *text)
270 if(point == 0) return; // counted from 1 in the DB
271 if(!text_field) return;
272 std::string field = text_field;
273 WorldDatabase.escape_string(field);
275 if(!text)
277 WorldDatabase.PExecuteLog("UPDATE creature_movement SET %s=NULL WHERE id='%u' AND point='%u'", field.c_str(), id, point);
279 else
281 std::string text2 = text;
282 WorldDatabase.escape_string(text2);
283 WorldDatabase.PExecuteLog("UPDATE creature_movement SET %s='%s' WHERE id='%u' AND point='%u'", field.c_str(), text2.c_str(), id, point);
286 WaypointPathMap::iterator itr = m_pathMap.find(id);
287 if(itr != m_pathMap.end() && point <= itr->second.size())
289 WaypointNode &node = itr->second[point-1];
290 if(!node.behavior) node.behavior = new WaypointBehavior();
292 // if(field == "text1") node.behavior->text[0] = text ? text : "";
293 // if(field == "text2") node.behavior->text[1] = text ? text : "";
294 // if(field == "text3") node.behavior->text[2] = text ? text : "";
295 // if(field == "text4") node.behavior->text[3] = text ? text : "";
296 // if(field == "text5") node.behavior->text[4] = text ? text : "";
297 if(field == "emote") node.behavior->emote = text ? atoi(text) : 0;
298 if(field == "spell") node.behavior->spell = text ? atoi(text) : 0;
299 if(field == "model1") node.behavior->model1 = text ? atoi(text) : 0;
300 if(field == "model2") node.behavior->model2 = text ? atoi(text) : 0;
304 void WaypointManager::CheckTextsExistance(std::set<int32>& ids)
306 WaypointPathMap::iterator pmItr = m_pathMap.begin();
307 for ( ; pmItr != m_pathMap.end(); ++pmItr)
309 for (int i = 0; i < pmItr->second.size(); ++i)
311 WaypointBehavior* be = pmItr->second[i].behavior;
312 if (!be)
313 continue;
315 // Now we check text existence and put all zero texts ids to the end of array
317 // Counting leading zeros for futher textid shift
318 int zeroCount = 0;
319 for (int j = 0; j < MAX_WAYPOINT_TEXT; ++j)
321 if (!be->textid[j])
323 ++zeroCount;
324 continue;
326 else
328 if (!objmgr.GetMangosStringLocale(be->textid[j]))
330 sLog.outErrorDb("ERROR: Some waypoint has textid%u with not existing %u text.", j, be->textid[j]);
331 be->textid[j] = 0;
332 ++zeroCount;
333 continue;
335 else
336 ids.erase(be->textid[j]);
338 // Shifting check
339 if (zeroCount)
341 // Correct textid but some zeros leading, so move it forward.
342 be->textid[j-zeroCount] = be->textid[j];
343 be->textid[j] = 0;