[6838] [2008_11_18_01_mangos_creature_movement.sql 2008_11_18_02_mangos_mangos_string...
[getmangos.git] / src / game / WaypointManager.cpp
blob6b58a2234ccbe570a3ad2320c9a9775513c8befc
1 /*
2 * Copyright (C) 2005-2008 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");
60 if(result)
62 total_paths = result->GetRowCount();
63 barGoLink bar( total_paths );
66 Field *fields = result->Fetch();
67 uint32 id = fields[0].GetUInt32();
68 uint32 count = fields[1].GetUInt32();
69 m_pathMap[id].resize(count);
71 total_nodes += count;
72 bar.step();
73 } while( result->NextRow() );
74 delete result;
77 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");
78 if(result)
80 barGoLink bar( result->GetRowCount() );
83 Field *fields = result->Fetch();
84 uint32 point = fields[15].GetUInt32();
85 uint32 id = fields[14].GetUInt32();
87 WaypointPath &path = m_pathMap[id];
88 // the cleanup queries make sure the following is true
89 assert(point >= 1 && point <= path.size());
90 WaypointNode &node = path[point-1];
92 node.x = fields[0].GetFloat();
93 node.y = fields[1].GetFloat();
94 node.z = fields[2].GetFloat();
95 node.orientation = fields[3].GetFloat();
96 node.delay = fields[6].GetUInt16();
98 // prevent using invalid coordinates
99 if(!MaNGOS::IsValidMapCoord(node.x, node.y, node.z, node.orientation))
101 QueryResult *result1 = WorldDatabase.PQuery("SELECT id, map FROM creature WHERE guid = '%u'", id);
102 if(result1)
103 sLog.outErrorDb("ERROR: Creature (guidlow %d, entry %d) have invalid coordinates in his waypoint %d (X: %f, Y: %f).",
104 id, result1->Fetch()[0].GetUInt32(), point, node.x, node.y);
105 else
106 sLog.outErrorDb("ERROR: Waypoint path %d, have invalid coordinates in his waypoint %d (X: %f, Y: %f).",
107 id, point, node.x, node.y);
109 MaNGOS::NormalizeMapCoord(node.x);
110 MaNGOS::NormalizeMapCoord(node.y);
111 if(result1)
113 node.z = MapManager::Instance ().GetBaseMap(result1->Fetch()[1].GetUInt32())->GetHeight(node.x, node.y, node.z);
114 delete result1;
116 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);
119 WaypointBehavior be;
120 be.model1 = fields[4].GetUInt32();
121 be.model2 = fields[5].GetUInt32();
122 be.emote = fields[7].GetUInt32();
123 be.spell = fields[8].GetUInt32();
125 // load and store without holes in array
126 int j = 0;
127 for(int i = 0; i < MAX_WAYPOINT_TEXT; ++i)
129 be.textid[j] = fields[9+i].GetUInt32();
130 if(be.textid[j])
132 if (be.textid[j] < MIN_DB_SCRIPT_STRING_ID || be.textid[j] >= MAX_DB_SCRIPT_STRING_ID)
134 sLog.outErrorDb( "Table `db_script_string` not have string id %u", be.textid[j]);
135 continue;
138 if (!objmgr.GetMangosStringLocale (be.textid[j]))
140 sLog.outErrorDb("ERROR: Waypoint path %d (point %d), have invalid text id (%i) in `textid%d, ignored.",
141 id, point, be.textid[j], i+1);
142 continue;
145 ++j; // to next internal field
148 // fill array tail
149 for(; j < MAX_WAYPOINT_TEXT; ++j)
150 be.textid[j] = 0;
152 // save memory by not storing empty behaviors
153 if(!be.isEmpty())
155 node.behavior = new WaypointBehavior(be);
156 ++total_behaviors;
158 else
159 node.behavior = NULL;
160 bar.step();
161 } while( result->NextRow() );
162 delete result;
164 sLog.outString( ">> Loaded %u paths, %u nodes and %u behaviors", total_paths, total_nodes, total_behaviors);
167 void WaypointManager::Cleanup()
169 // check if points need to be renumbered and do it
170 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"))
172 delete result;
173 WorldDatabase.DirectExecute("CREATE TEMPORARY TABLE temp LIKE creature_movement");
174 WorldDatabase.DirectExecute("INSERT INTO temp SELECT * FROM creature_movement");
175 WorldDatabase.DirectExecute("ALTER TABLE creature_movement DROP PRIMARY KEY");
176 WorldDatabase.DirectExecute("UPDATE creature_movement AS T SET point = (SELECT COUNT(*) FROM temp WHERE id = T.id AND point <= T.point)");
177 WorldDatabase.DirectExecute("ALTER TABLE creature_movement ADD PRIMARY KEY (id, point)");
178 WorldDatabase.DirectExecute("DROP TABLE temp");
179 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")));
183 void WaypointManager::Unload()
185 for(WaypointPathMap::iterator itr = m_pathMap.begin(); itr != m_pathMap.end(); ++itr)
186 _clearPath(itr->second);
187 m_pathMap.clear();
190 void WaypointManager::_clearPath(WaypointPath &path)
192 for(WaypointPath::iterator itr = path.begin(); itr != path.end(); ++itr)
193 if(itr->behavior)
194 delete itr->behavior;
195 path.clear();
198 /// - Insert after the last point
199 void WaypointManager::AddLastNode(uint32 id, float x, float y, float z, float o, uint32 delay, uint32 wpGuid)
201 _addNode(id, GetLastPoint(id, 0) + 1, x, y, z, o, delay, wpGuid);
204 /// - Insert after a certain point
205 void WaypointManager::AddAfterNode(uint32 id, uint32 point, float x, float y, float z, float o, uint32 delay, uint32 wpGuid)
207 for(uint32 i = GetLastPoint(id, 0); i > point; i--)
208 WorldDatabase.PExecuteLog("UPDATE creature_movement SET point=point+1 WHERE id='%u' AND point='%u'", id, i);
210 _addNode(id, point + 1, x, y, z, o, delay, wpGuid);
213 /// - Insert without checking for collision
214 void WaypointManager::_addNode(uint32 id, uint32 point, float x, float y, float z, float o, uint32 delay, uint32 wpGuid)
216 if(point == 0) return; // counted from 1 in the DB
217 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);
218 WaypointPathMap::iterator itr = m_pathMap.find(id);
219 if(itr == m_pathMap.end())
220 itr = m_pathMap.insert(WaypointPathMap::value_type(id, WaypointPath())).first;
221 itr->second.insert(itr->second.begin() + (point - 1), WaypointNode(x, y, z, o, delay, NULL));
224 uint32 WaypointManager::GetLastPoint(uint32 id, uint32 default_notfound)
226 uint32 point = default_notfound;
227 /*QueryResult *result = WorldDatabase.PQuery( "SELECT MAX(point) FROM creature_movement WHERE id = '%u'", id);
228 if( result )
230 point = (*result)[0].GetUInt32()+1;
231 delete result;
233 WaypointPathMap::iterator itr = m_pathMap.find(id);
234 if(itr != m_pathMap.end() && itr->second.size() != 0)
235 point = itr->second.size();
236 return point;
239 void WaypointManager::DeleteNode(uint32 id, uint32 point)
241 if(point == 0) return; // counted from 1 in the DB
242 WorldDatabase.PExecuteLog("DELETE FROM creature_movement WHERE id='%u' AND point='%u'", id, point);
243 WorldDatabase.PExecuteLog("UPDATE creature_movement SET point=point-1 WHERE id='%u' AND point>'%u'", id, point);
244 WaypointPathMap::iterator itr = m_pathMap.find(id);
245 if(itr != m_pathMap.end() && point <= itr->second.size())
246 itr->second.erase(itr->second.begin() + (point-1));
249 void WaypointManager::DeletePath(uint32 id)
251 WorldDatabase.PExecuteLog("DELETE FROM creature_movement WHERE id='%u'", id);
252 WaypointPathMap::iterator itr = m_pathMap.find(id);
253 if(itr != m_pathMap.end())
254 _clearPath(itr->second);
255 // the path is not removed from the map, just cleared
256 // WMGs have pointers to the path, so deleting them would crash
257 // this wastes some memory, but these functions are
258 // only meant to be called by GM commands
261 void WaypointManager::SetNodePosition(uint32 id, uint32 point, float x, float y, float z)
263 if(point == 0) return; // counted from 1 in the DB
264 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);
265 WaypointPathMap::iterator itr = m_pathMap.find(id);
266 if(itr != m_pathMap.end() && point <= itr->second.size())
268 itr->second[point-1].x = x;
269 itr->second[point-1].y = y;
270 itr->second[point-1].z = z;
274 void WaypointManager::SetNodeText(uint32 id, uint32 point, const char *text_field, const char *text)
276 if(point == 0) return; // counted from 1 in the DB
277 if(!text_field) return;
278 std::string field = text_field;
279 WorldDatabase.escape_string(field);
281 if(!text)
283 WorldDatabase.PExecuteLog("UPDATE creature_movement SET %s=NULL WHERE id='%u' AND point='%u'", field.c_str(), id, point);
285 else
287 std::string text2 = text;
288 WorldDatabase.escape_string(text2);
289 WorldDatabase.PExecuteLog("UPDATE creature_movement SET %s='%s' WHERE id='%u' AND point='%u'", field.c_str(), text2.c_str(), id, point);
292 WaypointPathMap::iterator itr = m_pathMap.find(id);
293 if(itr != m_pathMap.end() && point <= itr->second.size())
295 WaypointNode &node = itr->second[point-1];
296 if(!node.behavior) node.behavior = new WaypointBehavior();
298 // if(field == "text1") node.behavior->text[0] = text ? text : "";
299 // if(field == "text2") node.behavior->text[1] = text ? text : "";
300 // if(field == "text3") node.behavior->text[2] = text ? text : "";
301 // if(field == "text4") node.behavior->text[3] = text ? text : "";
302 // if(field == "text5") node.behavior->text[4] = text ? text : "";
303 if(field == "emote") node.behavior->emote = text ? atoi(text) : 0;
304 if(field == "spell") node.behavior->spell = text ? atoi(text) : 0;
305 if(field == "model1") node.behavior->model1 = text ? atoi(text) : 0;
306 if(field == "model2") node.behavior->model2 = text ? atoi(text) : 0;