2009-04-18 19:00:33 +02:00
|
|
|
/* Copyright (C) 2009 Wildfire Games.
|
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2004-06-03 20:38:14 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "ps/Profile.h"
|
2006-04-19 07:30:02 +02:00
|
|
|
|
2006-05-13 20:50:58 +02:00
|
|
|
#include "EntityOrders.h"
|
|
|
|
#include "Entity.h"
|
# Added tool for viewing models and animations outside the game.
Atlas: Added ActorViewer. Moved GL canvas into separate class for shared
use. Disabled message-handling callback while blocked on the game, and
stopped creating dialog boxes inside the game thread in order to avoid
deadlocks (hopefully). Support multiple Views (for independent sets of
camera/update/render code). Recalculate territory boundaries when
necessary. Changed default list of animations to match those currently
used by actors.
# Tidied up more code.
Moved some more #includes out of .h files, to minimise unnecessary
compilation.
MathUtil: Deleted unused/unuseful macros (M_PI (use PI instead), M_PI_2
(use PI/2), MAX3, ABS (use abs)).
ObjectManager: Removed some ScEd-specific things.
Unit: Moved creation out of UnitManager, so units can be created without
adding to the manager. Changed CStr8 to the more conventional CStr.
app_hooks: Removed warning for setting multiple times.
win: Restored SEH catcher.
GameSetup, GameView: Removed RenderNoCull, because it doesn't seem to do
what it says it does ("force renderer to load everything") since we're
loading-on-demand most stuff and it doesn't seem especially useful since
we'd prefer to minimise loading times (but feel free to correct me if
I'm wrong). (And because it crashes when things need to be initialised
in a different order, so it's easier to remove than to understand and
fix it.)
PatchRData, Renderer: Work sensibly when there's no game (hence no LOS
manager, water, etc).
LOSManager: Use entity position instead of actor position when possible.
TerritoryManager: Allow delayed recalculations (so Atlas can issue lots
of move+recalculate commands per frame).
Cinematic: Non-pointer wxTimer, so it doesn't leak and doesn't have to
be deleted manually.
This was SVN commit r4261.
2006-08-28 19:36:42 +02:00
|
|
|
#include "EntityTemplate.h"
|
2004-05-29 05:32:33 +02:00
|
|
|
#include "PathfindEngine.h"
|
2006-10-04 06:47:58 +02:00
|
|
|
#include "graphics/Terrain.h"
|
|
|
|
#include "ps/World.h"
|
2006-04-11 00:05:21 +02:00
|
|
|
|
2004-07-23 12:56:52 +02:00
|
|
|
|
2007-10-09 09:27:45 +02:00
|
|
|
#include "ps/GameSetup/Config.h"
|
|
|
|
|
2008-06-26 01:01:13 +02:00
|
|
|
#include "lib/ogl.h"
|
2007-10-09 09:27:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-03-24 22:40:10 +01:00
|
|
|
#ifdef USE_DCDT
|
|
|
|
|
2007-10-09 09:27:45 +02:00
|
|
|
#define EPSILON 0.00001f
|
|
|
|
|
|
|
|
|
2008-06-25 01:35:46 +02:00
|
|
|
class TriangulationTerrainOverlay : public TerrainOverlay
|
|
|
|
{
|
|
|
|
SrArray<SrPnt2> constr;
|
|
|
|
SrArray<SrPnt2> unconstr;
|
|
|
|
|
|
|
|
//std::vector<CVector2D> path;
|
|
|
|
|
|
|
|
SrPolygon CurPath;
|
|
|
|
public:
|
|
|
|
|
|
|
|
void RenderCurrentPath()
|
|
|
|
{
|
|
|
|
glColor3f(1,1,1);
|
|
|
|
|
|
|
|
for(int i=0; i< CurPath.size()-1; i++)
|
|
|
|
{
|
|
|
|
std::vector<CEntity*> results;
|
|
|
|
g_EntityManager.GetExtant(results);
|
|
|
|
CEntity* tempHandle = results[0];
|
|
|
|
|
|
|
|
glBegin(GL_LINE_LOOP);
|
|
|
|
|
|
|
|
float x1 = CurPath[i].x;
|
|
|
|
float y1 = CurPath[i].y;
|
|
|
|
float x2 = CurPath[i+1].x;
|
|
|
|
float y2 = CurPath[i+1].y;
|
|
|
|
glVertex3f(x1,tempHandle->GetAnchorLevel(x1,y1) + 0.2f,y1);
|
|
|
|
glVertex3f(x2,tempHandle->GetAnchorLevel(x2,y2) + 0.2f,y2);
|
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
//Kai: added function to draw out constrained line segments in triangulation
|
|
|
|
//
|
|
|
|
void RenderConstrainedEdges()
|
|
|
|
{
|
|
|
|
std::vector<CEntity*> results;
|
|
|
|
g_EntityManager.GetExtant(results);
|
|
|
|
CEntity* tempHandle = results[0];
|
|
|
|
|
|
|
|
glColor3f( 1, 1, 1 );
|
|
|
|
|
|
|
|
for(int i=0; i<constr.size()-2; i=i+2)
|
|
|
|
{
|
|
|
|
glBegin( GL_LINE_LOOP );
|
|
|
|
|
|
|
|
SrPnt2 p1 = constr[i];
|
|
|
|
SrPnt2 p2 = constr[i+1];
|
|
|
|
|
|
|
|
float x1 = p1.x;
|
|
|
|
float y1 = p1.y;
|
|
|
|
float x2 = p2.x;
|
|
|
|
float y2 = p2.y;
|
|
|
|
|
|
|
|
glVertex3f( x1, tempHandle->GetAnchorLevel( x1, y1 ) + 0.2f, y1 );
|
|
|
|
glVertex3f( x2, tempHandle->GetAnchorLevel( x2, y2 ) + 0.2f, y2 );
|
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Kai: added function to draw out unconstrained line segments in triangulation
|
|
|
|
//
|
|
|
|
void RenderUnconstrainedEdges()
|
|
|
|
{
|
|
|
|
std::vector<CEntity*> results;
|
|
|
|
g_EntityManager.GetExtant(results);
|
|
|
|
CEntity* tempHandle = results[0];
|
|
|
|
|
|
|
|
glColor3f( 0, 1, 0 );
|
|
|
|
|
|
|
|
for(int i=0; i<unconstr.size()-2; i=i+2)
|
|
|
|
{
|
|
|
|
glBegin( GL_LINE_LOOP );
|
|
|
|
|
|
|
|
SrPnt2 p1 = unconstr[i];
|
|
|
|
SrPnt2 p2 = unconstr[i+1];
|
|
|
|
|
|
|
|
float x1 = p1.x;
|
|
|
|
float y1 = p1.y;
|
|
|
|
float x2 = p2.x;
|
|
|
|
float y2 = p2.y;
|
|
|
|
|
|
|
|
glVertex3f( x1, tempHandle->GetAnchorLevel( x1, y1 ) + 0.2f, y1 );
|
|
|
|
glVertex3f( x2, tempHandle->GetAnchorLevel( x2, y2 ) + 0.2f, y2 );
|
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-17 19:00:00 +02:00
|
|
|
void setCurrentPath(const SrPolygon& _CurPath)
|
2008-06-25 01:35:46 +02:00
|
|
|
{
|
|
|
|
CurPath = _CurPath;
|
|
|
|
}
|
|
|
|
|
2008-07-17 19:00:00 +02:00
|
|
|
void setConstrainedEdges(const SrArray<SrPnt2>& _constr)
|
2008-06-25 01:35:46 +02:00
|
|
|
{
|
|
|
|
constr = _constr;
|
|
|
|
}
|
|
|
|
|
2008-07-17 19:00:00 +02:00
|
|
|
void setUnconstrainedEdges(const SrArray<SrPnt2>& _unconstr)
|
2008-06-25 01:35:46 +02:00
|
|
|
{
|
|
|
|
unconstr = _unconstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Render()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TriangulationTerrainOverlay()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void GetTileExtents(
|
|
|
|
ssize_t& min_i_inclusive, ssize_t& min_j_inclusive,
|
|
|
|
ssize_t& max_i_inclusive, ssize_t& max_j_inclusive)
|
|
|
|
{
|
|
|
|
min_i_inclusive = 1;
|
|
|
|
min_j_inclusive = 1;
|
|
|
|
max_i_inclusive = 2;
|
|
|
|
max_j_inclusive = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void ProcessTile(ssize_t UNUSED(i), ssize_t UNUSED(j))
|
|
|
|
{
|
|
|
|
|
|
|
|
RenderConstrainedEdges();
|
|
|
|
RenderUnconstrainedEdges();
|
|
|
|
RenderCurrentPath();
|
|
|
|
}
|
|
|
|
};
|
2009-03-24 22:40:10 +01:00
|
|
|
#endif // USE_DCDT
|
2008-06-25 01:35:46 +02:00
|
|
|
|
2007-10-09 09:27:45 +02:00
|
|
|
|
2009-03-24 22:40:10 +01:00
|
|
|
#ifdef USE_DCDT
|
2007-12-01 19:05:46 +01:00
|
|
|
CPathfindEngine::CPathfindEngine() : triangulationOverlay(0),
|
2007-12-20 20:57:41 +01:00
|
|
|
OABBBOUNDREDUCTION(0.8f),
|
|
|
|
CIRCLEBOUNDREDUCTION(0.5f),
|
|
|
|
RADIUSINCREMENT(2.0f)
|
2004-07-23 12:56:52 +02:00
|
|
|
{
|
2007-10-14 01:37:23 +02:00
|
|
|
dcdtInitialized = false;
|
|
|
|
|
2007-12-01 19:05:46 +01:00
|
|
|
if (g_ShowPathfindingOverlay)
|
|
|
|
triangulationOverlay = new TriangulationTerrainOverlay();
|
2007-10-09 09:27:45 +02:00
|
|
|
}
|
|
|
|
|
2007-12-01 19:05:46 +01:00
|
|
|
CPathfindEngine::~CPathfindEngine()
|
|
|
|
{
|
|
|
|
if (triangulationOverlay)
|
|
|
|
{
|
|
|
|
delete triangulationOverlay;
|
|
|
|
triangulationOverlay = 0;
|
|
|
|
}
|
|
|
|
}
|
2009-03-24 22:40:10 +01:00
|
|
|
#else // USE_DCDT
|
|
|
|
CPathfindEngine::CPathfindEngine() { }
|
|
|
|
CPathfindEngine::~CPathfindEngine() { }
|
|
|
|
#endif // USE_DCDT
|
|
|
|
|
2007-10-09 09:27:45 +02:00
|
|
|
|
2009-03-24 22:40:10 +01:00
|
|
|
#ifdef USE_DCDT
|
2007-10-09 09:27:45 +02:00
|
|
|
//Todo:
|
|
|
|
// 1; the bouncing problem with the fortress
|
2007-10-14 01:37:23 +02:00
|
|
|
// 2; update obstacles when things vanishes. done
|
2007-10-09 09:27:45 +02:00
|
|
|
|
|
|
|
void CPathfindEngine::initBoundary()
|
|
|
|
{
|
|
|
|
SrPolygon boundary ;
|
|
|
|
|
|
|
|
|
|
|
|
CTerrain* m_Terrain = g_Game->GetWorld()->GetTerrain();
|
|
|
|
int width = m_Terrain->GetVerticesPerSide() * CELL_SIZE ;
|
|
|
|
|
|
|
|
boundary.push().set(0.0f, 0.0f);
|
|
|
|
boundary.push().set(width, 0.0f);
|
|
|
|
boundary.push().set(width, width);
|
|
|
|
boundary.push().set(0.0f, width);
|
|
|
|
dcdtPathfinder.init(boundary, EPSILON,1);
|
|
|
|
dcdtPathfinder.InitializeSectors();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPathfindEngine::insertObstacles()
|
|
|
|
{
|
|
|
|
|
|
|
|
std::vector<CEntity*> results;
|
|
|
|
g_EntityManager.GetExtant(results);
|
|
|
|
SrPolygon poly;
|
|
|
|
|
|
|
|
|
2007-12-20 20:57:41 +01:00
|
|
|
for(size_t i =0 ; i < results.size(); i++)
|
2007-10-09 09:27:45 +02:00
|
|
|
{
|
|
|
|
poly.size(0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CEntity* tempHandle = results[i];
|
2009-11-03 22:46:35 +01:00
|
|
|
//debug_printf(L"Entity position: %f %f %f\n", tempHandle->m_position.X,tempHandle->m_position.Y,tempHandle->m_position.Z);
|
2007-10-09 09:27:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
CVector2D p, q;
|
|
|
|
CVector2D u, v;
|
|
|
|
q.x = tempHandle->m_position.X;
|
|
|
|
q.y = tempHandle->m_position.Z;
|
|
|
|
float d = ((CBoundingBox*)tempHandle->m_bounds)->m_d;
|
|
|
|
float w = ((CBoundingBox*)tempHandle->m_bounds)->m_w;
|
|
|
|
|
|
|
|
u.x = sin( tempHandle->m_graphics_orientation.Y );
|
|
|
|
u.y = cos( tempHandle->m_graphics_orientation.Y );
|
|
|
|
v.x = u.y;
|
|
|
|
v.y = -u.x;
|
|
|
|
|
|
|
|
CBoundingObject* m_bounds = tempHandle->m_bounds;
|
|
|
|
|
|
|
|
switch( m_bounds->m_type )
|
|
|
|
{
|
|
|
|
case CBoundingObject::BOUND_CIRCLE:
|
|
|
|
{
|
|
|
|
if(tempHandle->m_speed == 0)
|
|
|
|
{
|
|
|
|
|
|
|
|
poly.open(false);
|
|
|
|
|
2007-10-14 01:37:23 +02:00
|
|
|
w = CIRCLEBOUNDREDUCTION;
|
|
|
|
d = CIRCLEBOUNDREDUCTION;
|
2007-10-09 09:27:45 +02:00
|
|
|
|
|
|
|
p = q + u * d + v * w;
|
|
|
|
poly.push().set((float)(p.x), (float)(p.y));
|
|
|
|
|
|
|
|
p = q - u * d + v * w ;
|
|
|
|
poly.push().set((float)(p.x), (float)(p.y));
|
|
|
|
|
|
|
|
p = q - u * d - v * w;
|
|
|
|
poly.push().set((float)(p.x), (float)(p.y));
|
|
|
|
|
|
|
|
p = q + u * d - v * w;
|
|
|
|
poly.push().set((float)(p.x), (float)(p.y));
|
|
|
|
|
|
|
|
int dcdtId = dcdtPathfinder.insert_polygon(poly);
|
2007-10-14 01:37:23 +02:00
|
|
|
tempHandle->m_dcdtId = dcdtId;
|
|
|
|
|
2007-10-09 09:27:45 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
case CBoundingObject::BOUND_OABB:
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
poly.open(false);
|
|
|
|
|
|
|
|
// Tighten the bound so the units will not get stuck near the buildings
|
|
|
|
//Note: the triangulation pathfinding code will not find a path for the unit if it is pushed into the bound of a unit.
|
|
|
|
//
|
2007-10-14 01:37:23 +02:00
|
|
|
w = w * OABBBOUNDREDUCTION;
|
|
|
|
d = d * OABBBOUNDREDUCTION;
|
2007-10-09 09:27:45 +02:00
|
|
|
|
|
|
|
p = q + u * d + v * w;
|
|
|
|
poly.push().set((float)(p.x), (float)(p.y));
|
|
|
|
|
|
|
|
p = q - u * d + v * w ;
|
|
|
|
poly.push().set((float)(p.x), (float)(p.y));
|
|
|
|
|
|
|
|
p = q - u * d - v * w;
|
|
|
|
poly.push().set((float)(p.x), (float)(p.y));
|
|
|
|
|
|
|
|
p = q + u * d - v * w;
|
|
|
|
poly.push().set((float)(p.x), (float)(p.y));
|
|
|
|
|
|
|
|
int dcdtId = dcdtPathfinder.insert_polygon(poly);
|
2007-10-14 01:37:23 +02:00
|
|
|
tempHandle->m_dcdtId = dcdtId;
|
2007-10-09 09:27:45 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}//end switch
|
|
|
|
|
|
|
|
|
|
|
|
}//end for loop
|
|
|
|
dcdtPathfinder.DeleteAbstraction();
|
|
|
|
dcdtPathfinder.Abstract();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-10-14 01:37:23 +02:00
|
|
|
void CPathfindEngine::drawTriangulation()
|
2007-10-09 09:27:45 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
int polyNum = dcdtPathfinder.num_polygons();
|
|
|
|
|
2009-11-03 22:46:35 +01:00
|
|
|
//debug_printf(L"Number of polygons: %d",polyNum);
|
2007-10-09 09:27:45 +02:00
|
|
|
|
|
|
|
if(polyNum)
|
|
|
|
{
|
|
|
|
SrArray<SrPnt2> constrainedEdges;
|
|
|
|
SrArray<SrPnt2> unconstrainedEdges;
|
|
|
|
|
|
|
|
dcdtPathfinder.get_mesh_edges(&constrainedEdges, &unconstrainedEdges);
|
|
|
|
|
2007-12-01 19:05:46 +01:00
|
|
|
if (triangulationOverlay)
|
|
|
|
{
|
|
|
|
triangulationOverlay->setConstrainedEdges(constrainedEdges);
|
|
|
|
triangulationOverlay->setUnconstrainedEdges(unconstrainedEdges);
|
|
|
|
}
|
2007-10-09 09:27:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-07-23 12:56:52 +02:00
|
|
|
}
|
2009-03-24 22:40:10 +01:00
|
|
|
#endif // USE_DCDT
|
2004-05-29 05:32:33 +02:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
void CPathfindEngine::RequestPath( HEntity entity, const CVector2D& destination,
|
2006-09-09 02:00:23 +02:00
|
|
|
CEntityOrder::EOrderSource orderSource )
|
2004-05-29 05:32:33 +02:00
|
|
|
{
|
2007-10-09 09:27:45 +02:00
|
|
|
|
2007-10-14 01:37:23 +02:00
|
|
|
|
2006-04-09 02:36:52 +02:00
|
|
|
/* TODO: Add code to generate high level path
|
|
|
|
For now, just the one high level waypoint to the final
|
|
|
|
destination is added
|
|
|
|
*/
|
|
|
|
CEntityOrder waypoint;
|
|
|
|
waypoint.m_type = CEntityOrder::ORDER_GOTO_WAYPOINT;
|
2006-09-09 02:00:23 +02:00
|
|
|
waypoint.m_source = orderSource;
|
2006-12-21 15:57:13 +01:00
|
|
|
waypoint.m_target_location = destination;
|
2009-03-24 22:40:10 +01:00
|
|
|
waypoint.m_pathfinder_radius = 0.0f;
|
2007-10-09 09:27:45 +02:00
|
|
|
|
2009-03-24 22:40:10 +01:00
|
|
|
#ifdef USE_DCDT
|
2007-10-09 09:27:45 +02:00
|
|
|
//Kai: adding radius for pathfinding
|
|
|
|
CBoundingObject* m_bounds = entity->m_bounds;
|
2007-10-14 01:37:23 +02:00
|
|
|
waypoint.m_pathfinder_radius = m_bounds->m_radius + RADIUSINCREMENT;
|
2009-03-24 22:40:10 +01:00
|
|
|
#endif // USE_DCDT
|
2007-10-09 09:27:45 +02:00
|
|
|
|
2006-04-09 02:36:52 +02:00
|
|
|
entity->m_orderQueue.push_front( waypoint );
|
|
|
|
}
|
|
|
|
|
2007-10-09 09:27:45 +02:00
|
|
|
|
2009-03-24 22:40:10 +01:00
|
|
|
#ifdef USE_DCDT
|
2008-07-29 10:22:56 +02:00
|
|
|
void CPathfindEngine::RequestTriangulationPath( HEntity entity, const CVector2D& destination, bool contact,
|
2007-10-09 09:27:45 +02:00
|
|
|
float radius, CEntityOrder::EOrderSource orderSource )
|
|
|
|
{
|
|
|
|
PROFILE_START("Pathfinding");
|
|
|
|
|
2008-07-29 10:22:56 +02:00
|
|
|
CEntityOrder::EOrderType stepType = CEntityOrder::ORDER_GOTO_NOPATHING;
|
|
|
|
if (contact) stepType = CEntityOrder::ORDER_GOTO_NOPATHING_CONTACT;
|
|
|
|
|
2007-10-14 01:37:23 +02:00
|
|
|
if(g_TriPathfind)
|
|
|
|
{
|
|
|
|
/* TODO:1. add code to verify the triangulation. done.
|
|
|
|
2. add code to convert a path from dcdtpathfinder to world waypoints done
|
|
|
|
*/
|
|
|
|
if(!dcdtInitialized)
|
|
|
|
{
|
|
|
|
initBoundary();
|
|
|
|
insertObstacles();
|
|
|
|
dcdtInitialized =true;
|
|
|
|
|
|
|
|
|
|
|
|
//switch on/off triangulation drawing by command line arg "-showOverlay"
|
2007-12-01 19:05:46 +01:00
|
|
|
//it's guarded here to stop setting constrainedEdges and unconstrainedEdges in triangulationOverlay->
|
2007-10-14 01:37:23 +02:00
|
|
|
//(efficiency issue)
|
|
|
|
//the drawing is disable in the render() function in TerraiOverlay.cpp
|
2007-12-01 19:05:46 +01:00
|
|
|
if(g_ShowPathfindingOverlay)
|
2007-10-14 01:37:23 +02:00
|
|
|
{
|
|
|
|
drawTriangulation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-09 09:27:45 +02:00
|
|
|
|
|
|
|
//Kai: added test for terrain information in entityManager
|
|
|
|
//mLowPathfinder.TAStarTest();
|
|
|
|
|
|
|
|
CVector2D source( entity->m_position.X, entity->m_position.Z );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool found = mTriangulationPathfinder.FindPath(source, destination, entity,dcdtPathfinder, radius);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//push the path onto the order process queue.
|
|
|
|
SrPolygon CurPath;
|
|
|
|
SrPolygon CurChannel;
|
|
|
|
|
|
|
|
|
|
|
|
if ( !found )
|
|
|
|
{
|
|
|
|
// If no path was found, then unsolvable
|
|
|
|
// TODO: Figure out what to do in this case
|
|
|
|
|
|
|
|
CurPath.size(0);
|
|
|
|
CurChannel.size(0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CurChannel = dcdtPathfinder.GetChannelBoundary();
|
|
|
|
|
|
|
|
CurPath = dcdtPathfinder.GetPath();
|
|
|
|
|
|
|
|
|
|
|
|
//set and draw the path on the terrain
|
2007-12-01 19:05:46 +01:00
|
|
|
if (triangulationOverlay)
|
|
|
|
{
|
|
|
|
triangulationOverlay->setCurrentPath(CurPath);
|
|
|
|
}
|
2007-10-09 09:27:45 +02:00
|
|
|
|
|
|
|
// Make the path take as few steps as possible by collapsing steps in the same direction together.
|
|
|
|
std::vector<CVector2D> path;
|
|
|
|
|
2009-11-03 22:46:35 +01:00
|
|
|
debug_printf(L"waypoints: %d channel size %d \n ",CurPath.size(),CurChannel.size());
|
2007-10-09 09:27:45 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < CurPath.size(); i++)
|
|
|
|
{
|
|
|
|
CVector2D waypoint ;
|
|
|
|
|
|
|
|
waypoint.x = CurPath[i].x;
|
|
|
|
waypoint.y = CurPath[i].y;
|
|
|
|
|
2009-11-03 22:46:35 +01:00
|
|
|
debug_printf(L"waypoints: %f %f \n",waypoint.x, waypoint.y);
|
2007-10-09 09:27:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
path.push_back(waypoint);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if( path.size() > 0 )
|
|
|
|
{
|
|
|
|
// Push the path onto the front of our order queue in reverse order,
|
|
|
|
// so that we run through it before continuing other orders.
|
|
|
|
|
|
|
|
CEntityOrder node;
|
|
|
|
node.m_source = orderSource;
|
|
|
|
|
|
|
|
// Hack to make pathfinding slightly more precise:
|
|
|
|
// If the radius was 0, make the final node be exactly at the destination
|
|
|
|
// (otherwise, go to wherever the pathfinder tells us since we just want to be in range)
|
|
|
|
CVector2D finalDest = (radius==0 ? destination : path[path.size()-1]);
|
|
|
|
node.m_type = CEntityOrder::ORDER_PATH_END_MARKER; // push end marker (used as a sentinel when repathing)
|
|
|
|
node.m_target_location = finalDest;
|
|
|
|
entity->m_orderQueue.push_front(node);
|
2008-07-29 10:22:56 +02:00
|
|
|
node.m_type = stepType; // push final goto step
|
2007-10-09 09:27:45 +02:00
|
|
|
node.m_target_location = finalDest;
|
|
|
|
entity->m_orderQueue.push_front(node);
|
|
|
|
|
|
|
|
for( int i = ((int) path.size()) - 2; i >= 0; i-- )
|
|
|
|
{
|
2008-07-29 10:22:56 +02:00
|
|
|
node.m_type = stepType; // TODO: For non-contact paths, do we want some other order type?
|
2007-10-09 09:27:45 +02:00
|
|
|
node.m_target_location = path[i];
|
|
|
|
entity->m_orderQueue.push_front(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Hack to make pathfinding slightly more precise:
|
|
|
|
// If radius = 0, we have an empty path but the user still wants us to move
|
|
|
|
// within the same tile, so add a GOTO order anyway
|
|
|
|
if(radius == 0)
|
|
|
|
{
|
|
|
|
CEntityOrder node;
|
|
|
|
node.m_type = CEntityOrder::ORDER_PATH_END_MARKER;
|
|
|
|
node.m_target_location = destination;
|
|
|
|
entity->m_orderQueue.push_front(node);
|
2008-07-29 10:22:56 +02:00
|
|
|
node.m_type = stepType;
|
2007-10-09 09:27:45 +02:00
|
|
|
node.m_target_location = destination;
|
|
|
|
entity->m_orderQueue.push_front(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PROFILE_END("Pathfinding");
|
|
|
|
}
|
2009-03-24 22:40:10 +01:00
|
|
|
#endif // USE_DCDT
|
2007-10-09 09:27:45 +02:00
|
|
|
|
2008-07-29 10:22:56 +02:00
|
|
|
void CPathfindEngine::RequestLowLevelPath( HEntity entity, const CVector2D& destination, bool contact,
|
2006-09-09 02:00:23 +02:00
|
|
|
float radius, CEntityOrder::EOrderSource orderSource )
|
2006-04-09 02:36:52 +02:00
|
|
|
{
|
2006-05-13 20:50:58 +02:00
|
|
|
PROFILE_START("Pathfinding");
|
2007-10-09 09:27:45 +02:00
|
|
|
|
2008-07-29 10:22:56 +02:00
|
|
|
CEntityOrder::EOrderType stepType = CEntityOrder::ORDER_GOTO_NOPATHING;
|
|
|
|
if (contact) stepType = CEntityOrder::ORDER_GOTO_NOPATHING_CONTACT;
|
2007-10-09 09:27:45 +02:00
|
|
|
|
|
|
|
//Kai: added test for terrain information in entityManager
|
|
|
|
//mLowPathfinder.TAStarTest();
|
2006-04-11 00:05:21 +02:00
|
|
|
|
2006-05-13 20:50:58 +02:00
|
|
|
CVector2D source( entity->m_position.X, entity->m_position.Z );
|
2006-04-11 00:05:21 +02:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
if ( mLowPathfinder.FindPath(source, destination, entity, radius) )
|
2006-04-11 00:05:21 +02:00
|
|
|
{
|
2007-07-05 09:33:43 +02:00
|
|
|
std::vector<CVector2D> stepwisePath = mLowPathfinder.GetLastPath();
|
|
|
|
|
|
|
|
// Make the path take as few steps as possible by collapsing steps in the same direction together.
|
|
|
|
std::vector<CVector2D> path;
|
|
|
|
CVector2D lastDir(0, 0);
|
|
|
|
for(size_t i=0; i < stepwisePath.size(); i++)
|
|
|
|
{
|
|
|
|
if(i >= 2 && stepwisePath[i]-stepwisePath[i-1] == lastDir)
|
|
|
|
// We're in a colinear range; just update last point
|
|
|
|
path[path.size()-1] = stepwisePath[i];
|
|
|
|
else
|
|
|
|
path.push_back(stepwisePath[i]);
|
|
|
|
|
|
|
|
if(i >= 1)
|
|
|
|
lastDir = stepwisePath[i] - stepwisePath[i-1];
|
|
|
|
}
|
|
|
|
|
2006-05-14 00:11:46 +02:00
|
|
|
if( path.size() > 0 )
|
2006-04-11 00:05:21 +02:00
|
|
|
{
|
2006-08-25 06:24:06 +02:00
|
|
|
// Push the path onto the front of our order queue in reverse order,
|
|
|
|
// so that we run through it before continuing other orders.
|
|
|
|
|
2006-05-14 00:11:46 +02:00
|
|
|
CEntityOrder node;
|
2006-09-09 02:00:23 +02:00
|
|
|
node.m_source = orderSource;
|
2006-08-25 06:24:06 +02:00
|
|
|
|
2006-08-17 23:09:46 +02:00
|
|
|
// Hack to make pathfinding slightly more precise:
|
|
|
|
// If the radius was 0, make the final node be exactly at the destination
|
|
|
|
// (otherwise, go to wherever the pathfinder tells us since we just want to be in range)
|
2006-08-25 06:24:06 +02:00
|
|
|
CVector2D finalDest = (radius==0 ? destination : path[path.size()-1]);
|
|
|
|
node.m_type = CEntityOrder::ORDER_PATH_END_MARKER; // push end marker (used as a sentinel when repathing)
|
2006-12-21 15:57:13 +01:00
|
|
|
node.m_target_location = finalDest;
|
2006-08-25 06:24:06 +02:00
|
|
|
entity->m_orderQueue.push_front(node);
|
2008-07-29 10:22:56 +02:00
|
|
|
node.m_type = stepType; // push final goto step
|
2006-12-21 15:57:13 +01:00
|
|
|
node.m_target_location = finalDest;
|
2006-08-25 06:24:06 +02:00
|
|
|
entity->m_orderQueue.push_front(node);
|
|
|
|
|
|
|
|
for( int i = ((int) path.size()) - 2; i >= 0; i-- )
|
|
|
|
{
|
2008-07-29 10:22:56 +02:00
|
|
|
node.m_type = stepType;
|
2006-12-21 15:57:13 +01:00
|
|
|
node.m_target_location = path[i];
|
2006-08-25 06:24:06 +02:00
|
|
|
entity->m_orderQueue.push_front(node);
|
|
|
|
}
|
2006-04-11 00:05:21 +02:00
|
|
|
}
|
2006-08-17 23:09:46 +02:00
|
|
|
else {
|
|
|
|
// Hack to make pathfinding slightly more precise:
|
2006-08-25 06:24:06 +02:00
|
|
|
// If radius = 0, we have an empty path but the user still wants us to move
|
|
|
|
// within the same tile, so add a GOTO order anyway
|
2006-08-17 23:09:46 +02:00
|
|
|
if(radius == 0)
|
|
|
|
{
|
|
|
|
CEntityOrder node;
|
|
|
|
node.m_type = CEntityOrder::ORDER_PATH_END_MARKER;
|
2006-12-21 15:57:13 +01:00
|
|
|
node.m_target_location = destination;
|
2006-08-25 06:24:06 +02:00
|
|
|
entity->m_orderQueue.push_front(node);
|
2008-07-29 10:22:56 +02:00
|
|
|
node.m_type = stepType;
|
2006-12-21 15:57:13 +01:00
|
|
|
node.m_target_location = destination;
|
2006-08-25 06:24:06 +02:00
|
|
|
entity->m_orderQueue.push_front(node);
|
2006-08-17 23:09:46 +02:00
|
|
|
}
|
|
|
|
}
|
2006-04-11 00:05:21 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If no path was found, then unsolvable
|
|
|
|
// TODO: Figure out what to do in this case
|
|
|
|
}
|
|
|
|
|
2006-05-13 20:50:58 +02:00
|
|
|
PROFILE_END("Pathfinding");
|
2004-06-02 18:11:32 +02:00
|
|
|
}
|
2004-11-11 08:09:32 +01:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
void CPathfindEngine::RequestContactPath( HEntity entity, CEntityOrder* current, float range )
|
2004-11-11 08:09:32 +01:00
|
|
|
{
|
2006-04-11 00:05:21 +02:00
|
|
|
/* TODO: Same as non-contact: need high-level planner */
|
|
|
|
CEntityOrder waypoint;
|
|
|
|
waypoint.m_type = CEntityOrder::ORDER_GOTO_WAYPOINT_CONTACT;
|
2006-09-09 02:00:23 +02:00
|
|
|
waypoint.m_source = current->m_source;
|
2006-12-21 15:57:13 +01:00
|
|
|
HEntity target = current->m_target_entity;
|
|
|
|
waypoint.m_target_location = target->m_position;
|
|
|
|
waypoint.m_pathfinder_radius = std::max( target->m_bounds->m_radius, range );
|
2006-04-11 00:05:21 +02:00
|
|
|
entity->m_orderQueue.push_front( waypoint );
|
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
//PathSparse( entity, current->m_target_entity->m_position );
|
2006-04-11 00:05:21 +02:00
|
|
|
//// For attack orders, do some additional postprocessing (replace goto/nopathing
|
|
|
|
//// with attack/nopathing, up until the attack order marker)
|
|
|
|
//std::deque<CEntityOrder>::iterator it;
|
|
|
|
//for( it = entity->m_orderQueue.begin(); it != entity->m_orderQueue.end(); it++ )
|
|
|
|
//{
|
|
|
|
// if( it->m_type == CEntityOrder::ORDER_PATH_END_MARKER )
|
|
|
|
// break;
|
|
|
|
// if( it->m_type == CEntityOrder::ORDER_GOTO_NOPATHING )
|
|
|
|
// {
|
|
|
|
// *it = *current;
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
}
|
2006-10-04 06:47:58 +02:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
bool CPathfindEngine::RequestAvoidPath( HEntity entity, CEntityOrder* current, float avoidRange )
|
2006-10-04 06:47:58 +02:00
|
|
|
{
|
|
|
|
/* TODO: Same as non-contact: need high-level planner */
|
|
|
|
|
|
|
|
// TODO: Replace this with a new type of goal which is to avoid some point or line segment
|
|
|
|
// (requires changes to pathfinder to support this type of goal)
|
|
|
|
|
|
|
|
CEntityOrder waypoint;
|
|
|
|
waypoint.m_type = CEntityOrder::ORDER_GOTO_WAYPOINT_CONTACT;
|
|
|
|
waypoint.m_source = current->m_source;
|
|
|
|
|
|
|
|
// Figure out a direction to move
|
2006-12-21 15:57:13 +01:00
|
|
|
HEntity target = current->m_target_entity;
|
2006-10-04 06:47:58 +02:00
|
|
|
CVector3D dir = entity->m_position - target->m_position;
|
|
|
|
if(dir.LengthSquared() == 0) // shouldn't happen, but just in case
|
|
|
|
dir = CVector3D(1, 0, 0);
|
2007-05-02 14:07:08 +02:00
|
|
|
float dist = dir.Length();
|
2006-10-04 06:47:58 +02:00
|
|
|
dir.Normalize();
|
|
|
|
|
2006-12-21 15:57:13 +01:00
|
|
|
waypoint.m_target_location = entity->m_position + dir * (avoidRange - dist);
|
2006-10-04 06:47:58 +02:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
if( !g_Game->GetWorld()->GetTerrain()->IsOnMap( waypoint.m_target_location ) )
|
2006-10-04 06:47:58 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-12-21 15:57:13 +01:00
|
|
|
waypoint.m_pathfinder_radius = 0.0f;
|
2006-10-04 06:47:58 +02:00
|
|
|
entity->m_orderQueue.push_front( waypoint );
|
|
|
|
return true;
|
|
|
|
}
|