root/trunk/src/game/WaypointManager.cpp @ 207

Revision 207, 11.8 kB (checked in by yumileroy, 17 years ago)

[svn] * Improve some arena team related DB access
* Cache GM tickets on server startup.
* Remove unused src/game/HateMatrix.h and references.
* Better check client inventory pos data received in some client packets to
skip invalid cases

Original author: KingPin?
Date: 2008-11-10 09:04:23-06:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
4 * Copyright (C) 2008 Trinity <http://www.trinitycore.org/>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include "Database/DatabaseEnv.h"
22#include "GridDefines.h"
23#include "Policies/SingletonImp.h"
24#include "WaypointManager.h"
25#include "ProgressBar.h"
26#include "MapManager.h"
27
28INSTANTIATE_SINGLETON_1(WaypointManager);
29
30bool WaypointBehavior::isEmpty()
31{
32    return emote == 0 && spell == 0 && model1 == 0 && model2 == 0 && text[0].empty() &&
33        text[1].empty() && text[2].empty() && text[3].empty() && text[4].empty();
34}
35
36WaypointBehavior::WaypointBehavior(const WaypointBehavior &b)
37{
38    emote = b.emote; spell = b.spell; model1 = b.model1; model2 = b.model2;
39    text[0] = b.text[0]; text[1] = b.text[1]; text[2] = b.text[2];
40    text[3] = b.text[3]; text[4] = b.text[4];
41}
42
43void WaypointManager::Load()
44{
45    Cleanup();
46
47    uint32 total_paths = 0;
48    uint32 total_nodes = 0;
49    uint32 total_behaviors = 0;
50
51    QueryResult *result = WorldDatabase.Query("SELECT id, COUNT(point) FROM creature_movement GROUP BY id");
52    if(result)
53    {
54        total_paths = result->GetRowCount();
55        barGoLink bar( total_paths );
56        do
57        {
58            Field *fields = result->Fetch();
59            uint32 id    = fields[0].GetUInt32();
60            uint32 count = fields[1].GetUInt32();
61            m_pathMap[id].resize(count);
62
63            total_nodes += count;
64            bar.step();
65        } while( result->NextRow() );
66        delete result;
67    }
68
69    result = WorldDatabase.Query("SELECT position_x, position_y, position_z, orientation, model1, model2, waittime, emote, spell, text1, text2, text3, text4, text5, id, point FROM creature_movement");
70    if(result)
71    {
72        barGoLink bar( result->GetRowCount() );
73        do
74        {
75            Field *fields = result->Fetch();
76            uint32 point        = fields[15].GetUInt32();
77            uint32 id           = fields[14].GetUInt32();
78
79            WaypointPath &path  = m_pathMap[id];
80            // the cleanup queries make sure the following is true
81            assert(point >= 1 && point <= path.size());
82            WaypointNode &node  = path[point-1];
83
84            node.x              = fields[0].GetFloat();
85            node.y              = fields[1].GetFloat();
86            node.z              = fields[2].GetFloat();
87            node.orientation    = fields[3].GetFloat();
88            node.delay          = fields[6].GetUInt16();
89
90            // prevent using invalid coordinates
91            if(!Trinity::IsValidMapCoord(node.x, node.y, node.z, node.orientation))
92            {
93                QueryResult *result1 = WorldDatabase.PQuery("SELECT id, map FROM creature WHERE guid = '%u'", id);
94                if(result1)
95                    sLog.outErrorDb("ERROR: Creature (guidlow %d, entry %d) have invalid coordinates in his waypoint %d (X: %f, Y: %f).",
96                        id, result1->Fetch()[0].GetUInt32(), point, node.x, node.y);
97                else
98                    sLog.outErrorDb("ERROR: Waypoint path %d, have invalid coordinates in his waypoint %d (X: %f, Y: %f).",
99                        id, point, node.x, node.y);
100
101                Trinity::NormalizeMapCoord(node.x);
102                Trinity::NormalizeMapCoord(node.y);
103                if(result1)
104                {
105                    node.z = MapManager::Instance ().GetBaseMap(result1->Fetch()[1].GetUInt32())->GetHeight(node.x, node.y, node.z);
106                    delete result1;
107                }
108                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);
109            }
110
111            WaypointBehavior be;
112            be.model1           = fields[4].GetUInt32();
113            be.model2           = fields[5].GetUInt32();
114            be.emote            = fields[7].GetUInt32();
115            be.spell            = fields[8].GetUInt32();
116            be.text[0]          = fields[9].GetCppString();
117            be.text[1]          = fields[10].GetCppString();
118            be.text[2]          = fields[11].GetCppString();
119            be.text[3]          = fields[12].GetCppString();
120            be.text[4]          = fields[13].GetCppString();
121
122            // save memory by not storing empty behaviors
123            if(!be.isEmpty())
124            {
125                node.behavior   = new WaypointBehavior(be);
126                ++total_behaviors;
127            }
128            else
129                node.behavior   = NULL;
130            bar.step();
131        } while( result->NextRow() );
132        delete result;
133    }
134    sLog.outString( ">> Loaded %u paths, %u nodes and %u behaviors", total_paths, total_nodes, total_behaviors);
135}
136
137void WaypointManager::Cleanup()
138{
139    // check if points need to be renumbered and do it
140    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"))
141    {
142        delete result;
143        WorldDatabase.DirectExecute("CREATE TEMPORARY TABLE temp LIKE creature_movement");
144        WorldDatabase.DirectExecute("INSERT INTO temp SELECT * FROM creature_movement");
145        WorldDatabase.DirectExecute("ALTER TABLE creature_movement DROP PRIMARY KEY");
146        WorldDatabase.DirectExecute("UPDATE creature_movement AS T SET point = (SELECT COUNT(*) FROM temp WHERE id = T.id AND point <= T.point)");
147        WorldDatabase.DirectExecute("ALTER TABLE creature_movement ADD PRIMARY KEY (id, point)");
148        WorldDatabase.DirectExecute("DROP TABLE temp");
149        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")));
150    }
151}
152
153void WaypointManager::Unload()
154{
155    for(WaypointPathMap::iterator itr = m_pathMap.begin(); itr != m_pathMap.end(); ++itr)
156        _clearPath(itr->second);
157    m_pathMap.clear();
158}
159
160void WaypointManager::_clearPath(WaypointPath &path)
161{
162    for(WaypointPath::iterator itr = path.begin(); itr != path.end(); ++itr)
163        if(itr->behavior)
164            delete itr->behavior;
165    path.clear();
166}
167
168/// - Insert after the last point
169void WaypointManager::AddLastNode(uint32 id, float x, float y, float z, float o, uint32 delay, uint32 wpGuid)
170{
171    _addNode(id, GetLastPoint(id, 0) + 1, x, y, z, o, delay, wpGuid);
172}
173
174/// - Insert after a certain point
175void WaypointManager::AddAfterNode(uint32 id, uint32 point, float x, float y, float z, float o, uint32 delay, uint32 wpGuid)
176{
177    for(uint32 i = GetLastPoint(id, 0); i > point; i--)
178        WorldDatabase.PExecuteLog("UPDATE creature_movement SET point=point+1 WHERE id='%u' AND point='%u'", id, i);
179
180    _addNode(id, point + 1, x, y, z, o, delay, wpGuid);
181}
182
183/// - Insert without checking for collision
184void WaypointManager::_addNode(uint32 id, uint32 point, float x, float y, float z, float o, uint32 delay, uint32 wpGuid)
185{
186    if(point == 0) return;                                  // counted from 1 in the DB
187    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);
188    WaypointPathMap::iterator itr = m_pathMap.find(id);
189    if(itr == m_pathMap.end())
190        itr = m_pathMap.insert(WaypointPathMap::value_type(id, WaypointPath())).first;
191    itr->second.insert(itr->second.begin() + (point - 1), WaypointNode(x, y, z, o, delay, NULL));
192}
193
194uint32 WaypointManager::GetLastPoint(uint32 id, uint32 default_notfound)
195{
196    uint32 point = default_notfound;
197    /*QueryResult *result = WorldDatabase.PQuery( "SELECT MAX(point) FROM creature_movement WHERE id = '%u'", id);
198    if( result )
199    {
200        point = (*result)[0].GetUInt32()+1;
201        delete result;
202    }*/
203    WaypointPathMap::iterator itr = m_pathMap.find(id);
204    if(itr != m_pathMap.end() && itr->second.size() != 0)
205        point = itr->second.size();
206    return point;
207}
208
209void WaypointManager::DeleteNode(uint32 id, uint32 point)
210{
211    if(point == 0) return;                                  // counted from 1 in the DB
212    WorldDatabase.PExecuteLog("DELETE FROM creature_movement WHERE id='%u' AND point='%u'", id, point);
213    WorldDatabase.PExecuteLog("UPDATE creature_movement SET point=point-1 WHERE id='%u' AND point>'%u'", id, point);
214    WaypointPathMap::iterator itr = m_pathMap.find(id);
215    if(itr != m_pathMap.end() && point <= itr->second.size())
216        itr->second.erase(itr->second.begin() + (point-1));
217}
218
219void WaypointManager::DeletePath(uint32 id)
220{
221    WorldDatabase.PExecuteLog("DELETE FROM creature_movement WHERE id='%u'", id);
222    WaypointPathMap::iterator itr = m_pathMap.find(id);
223    if(itr != m_pathMap.end())
224        _clearPath(itr->second);
225    // the path is not removed from the map, just cleared
226    // WMGs have pointers to the path, so deleting them would crash
227    // this wastes some memory, but these functions are
228    // only meant to be called by GM commands
229}
230
231void WaypointManager::SetNodePosition(uint32 id, uint32 point, float x, float y, float z)
232{
233    if(point == 0) return;                                  // counted from 1 in the DB
234    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);
235    WaypointPathMap::iterator itr = m_pathMap.find(id);
236    if(itr != m_pathMap.end() && point <= itr->second.size())
237    {
238        itr->second[point-1].x = x;
239        itr->second[point-1].y = y;
240        itr->second[point-1].z = z;
241    }
242}
243
244void WaypointManager::SetNodeText(uint32 id, uint32 point, const char *text_field, const char *text)
245{
246    if(point == 0) return;                                  // counted from 1 in the DB
247    if(!text_field) return;
248    std::string field = text_field;
249    WorldDatabase.escape_string(field);
250
251    if(!text)
252    {
253        WorldDatabase.PExecuteLog("UPDATE creature_movement SET %s=NULL WHERE id='%u' AND point='%u'", field.c_str(), id, point);
254    }
255    else
256    {
257        std::string text2 = text;
258        WorldDatabase.escape_string(text2);
259        WorldDatabase.PExecuteLog("UPDATE creature_movement SET %s='%s' WHERE id='%u' AND point='%u'", field.c_str(), text2.c_str(), id, point);
260    }
261
262    WaypointPathMap::iterator itr = m_pathMap.find(id);
263    if(itr != m_pathMap.end() && point <= itr->second.size())
264    {
265        WaypointNode &node = itr->second[point-1];
266        if(!node.behavior) node.behavior = new WaypointBehavior();
267
268        if(field == "text1") node.behavior->text[0] = text ? text : "";
269        if(field == "text2") node.behavior->text[1] = text ? text : "";
270        if(field == "text3") node.behavior->text[2] = text ? text : "";
271        if(field == "text4") node.behavior->text[3] = text ? text : "";
272        if(field == "text5") node.behavior->text[4] = text ? text : "";
273        if(field == "emote") node.behavior->emote   = text ? atoi(text) : 0;
274        if(field == "spell") node.behavior->spell   = text ? atoi(text) : 0;
275        if(field == "model1") node.behavior->model1 = text ? atoi(text) : 0;
276        if(field == "model2") node.behavior->model2 = text ? atoi(text) : 0;
277    }
278}
Note: See TracBrowser for help on using the browser.