Labyrinth
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
path.hpp
1 //////////////////////////////
2 //
3 // File: lxs/game/path.hpp
4 // Author: Maciej Kaminski
5 // E-mail: maciej.a.kaminski@gmail.com
6 // Website: maciejkaminski.pl
7 //
8 // File creation date: Mar 4 2013
9 // This file is part of Project Labyrinth
10 // This file is going to be released under terms of GNU GPL in near future
11 //
12 //////////////////////////////
13 
14 #ifndef __LxS_PATH__
15 #define __LxS_PATH__
16 #include <deque>
17 #include <list>
18 #include <map>
19 #include <vector>
20 #include "lxs/coregraphics/linesegment.hpp"
21 #include "lxs/system/servicethread.hpp"
22 #include <utility>
23 
24 namespace LxS
25 {
26  namespace CoreGraphics
27  {
28  class Renderable;
29  }
30  namespace Game
31  {
32  class GridVertex;
33  typedef std::list<GridVertex>gridvertices;
34 
35  class Map;
36  //! class representing calculated movement path for a character
38  {
39  //! map within which path is calculated
40  Map * parent_map;
41  //! path waypoint
42  class Waypoint
43  {
44  public:
45  //! visualization
47  //! normalized direction to next waypoint (x part)
48  float nd_x;
49  //! normalized direction to next waypoint (y part)
50  float nd_y;
51  //! normalized direction to next waypoint (z part)
52  float nd_z;
53  //! x coord
54  float x;
55  //! y coord
56  float y;
57  //! z coord
58  float z;
59  //! distance to next waypoint
60  float length;//distance to next wp.
61  Waypoint()
62  :ls(0),
63  nd_x(0), nd_y(0), nd_z(0),
64  x(0), y(0), z(0), length(0)
65  {}
66  };
67  //! is waypoint within walkable grid
68  bool WithinGrid(Waypoint * WP);
69  //! is path between two given waypoint wholly within walkable grid
70  bool SegmentWithinGrid(Waypoint * WP1, Waypoint * WP2);
71  //! perform AStar search algorithm
72  bool AStar();
73  //! synchronization object
75 
76  std::vector<LxS::CoreGraphics::LineSegment>DisplayGrid;
77  //! distance remaining to be traversed
78  float remaining;
79  //! type for list of waypoints
80  typedef std::list<Waypoint* > path_type;
81 
82  path_type planned_path;
83  float nd_x, nd_y, nd_z;
84  float s_x, s_y, s_z;
85  float t_x, t_y, t_z;
86  float CalcNd();
87  bool termination_requested;
88  LxS::System::MyMutex termination_mtx;
89 
90  struct AStarNode;
91  typedef std::list<AStarNode*> AStarList;
92  struct AStarNode
93  {
94  gridvertices::const_iterator Loc;
95  float CostFromStart;
96  float CostToGoal;
97  float TotalCost;
98  AStarNode * Parent;
99  };
100  void PathNormalize();
101  void AestheticPathOptimize();
102  float PathCostEstimate( const gridvertices::const_iterator& Loc1,
103  const gridvertices::const_iterator& Loc2)const;
104  public:
105  //! construct path within map m
106  Path(Map * m);
107  virtual ~Path();
108 
109  virtual void Initialize(); // load and set up some resources
110  virtual void Render(Rendermode mode) ;
111  virtual void UnloadResources() ;
112  // dt=delta time in seconds since last call to animate;
113  virtual void Animate(float dt) ;
114  virtual unsigned long GetTexelsCount() ;
115  virtual unsigned long GetFacesCount() ;
116  //! set starting point
117  void SetStart(float x, float y, float z);
118  //! set ending point
119  void SetEnd(float x, float y, float z);
120  //! distance remaining to be traversed
121  float GetRemainingLength();
122  //! move by distance given
123  void Progress(float ds);
124  //! get current position - X
125  float GetX();
126 
127  //! get current position - Y
128  float GetY();
129  //! get current position - Z
130  float GetZ();
131  //! get current normalized direction - X
132  float GetNDX();
133  //! get current normalized direction - Y
134  float GetNDY();
135  //! get current normalized direction - Z
136  float GetNDZ();
137  };
138  // end of class path
139  }
140 }
141 namespace std
142 {
143 }
144 #endif