Updated Copyright year to 2013
[getmangos.git] / src / game / Path.h
blob1dd8d83d7f9d875a1b1ebc568b19e631dacdf2e4
1 /*
2 * Copyright (C) 2005-2013 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 #ifndef MANGOSSERVER_PATH_H
20 #define MANGOSSERVER_PATH_H
22 #include "Common.h"
23 #include <deque>
25 struct PathNode
27 PathNode(): x(0.0f), y(0.0f), z(0.0f) { }
28 PathNode(float _x, float _y, float _z): x(_x), y(_y), z(_z) { }
29 float x, y, z;
32 template<typename PathElem, typename PathNode = PathElem>
33 class Path
35 public:
36 size_t size() const { return i_nodes.size(); }
37 bool empty() const { return i_nodes.empty(); }
38 void resize(unsigned int sz) { i_nodes.resize(sz); }
39 void crop(unsigned int start, unsigned int end)
41 while (start && !i_nodes.empty())
43 i_nodes.pop_front();
44 --start;
47 while (end && !i_nodes.empty())
49 i_nodes.pop_back();
50 --end;
54 void clear() { i_nodes.clear(); }
56 float GetTotalLength(uint32 start, uint32 end) const
58 float len = 0.0f;
59 for (unsigned int idx = start + 1; idx < end; ++idx)
61 PathNode const& node = i_nodes[idx];
62 PathNode const& prev = i_nodes[idx - 1];
63 float xd = node.x - prev.x;
64 float yd = node.y - prev.y;
65 float zd = node.z - prev.z;
66 len += sqrtf(xd * xd + yd * yd + zd * zd);
68 return len;
71 float GetTotalLength() const { return GetTotalLength(0, size()); }
73 float GetPassedLength(uint32 curnode, float x, float y, float z) const
75 float len = GetTotalLength(0, curnode);
77 if (curnode > 0)
79 PathNode const& node = i_nodes[curnode - 1];
80 float xd = x - node.x;
81 float yd = y - node.y;
82 float zd = z - node.z;
83 len += sqrtf(xd * xd + yd * yd + zd * zd);
86 return len;
89 PathNode& operator[](size_t idx) { return i_nodes[idx]; }
90 PathNode const& operator[](size_t idx) const { return i_nodes[idx]; }
92 void set(size_t idx, PathElem elem) { i_nodes[idx] = elem; }
94 protected:
95 std::deque<PathElem> i_nodes;
98 typedef Path<PathNode> PointPath;
100 #endif