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

Revision 2, 11.6 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

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