1
0
forked from 0ad/0ad
0ad/source/simulation/PathfindEngine.cpp

115 lines
3.9 KiB
C++
Raw Normal View History

#include "precompiled.h"
#include "ps/Profile.h"
#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"
#include "PathfindEngine.h"
CPathfindEngine::CPathfindEngine()
{
}
void CPathfindEngine::requestPath( HEntity entity, const CVector2D& destination,
CEntityOrder::EOrderSource orderSource )
{
/* 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;
waypoint.m_source = orderSource;
waypoint.m_data[0].location = destination;
# Housekeeping and pathfinder enhancements / optimization when dealing with ranged actions. - Modified Xeromyces to no longer automatically convert element and attribute names to lowercase, so that we can have camelCase names. We should now be able to convert all the multi-word entity properties, like pass_through_allies, to camelCase, like passThroughAllies, which is more consistent with the rest of our JavaScript naming conventions. To support the existing code that assumes lowercase element names, I made the getElementID and getAttributeID methods (used in the EL and AT macros) ignore case, and I changed any code that directly accessed element names to use the right case. CEntityTemplate now converts Names_LikeThis to names_likeThis (changing each separate "word" in the name to camelCase). Changed the version letter in XMB filenames from A to B to support this without requiring people to delete old XMBs. - Enhanced the pathfinder's handling of contact paths, resulting in a very large speedup for actions like attacking, construction, etc. The problem was that the pathfinder used to not count a given state as the goal unless it was exactly coincident with the target location. This is fine when you order a unit to go exactly to a certain spot, but if you're ordering a unit to build, gather or attack something, then the target tile is impassable (because your target is there) and therefore the pathfinder never declares a state final. As a result, the pathfinder tries hundreds of extra tiles in case there is a long path that gets to the goal, and after failing to find any path that reaches the goal, it gives you one to the closest point it got to. To fix it, I made the pathfinder take into account a radius around the goal in which it's OK to be, which depends on the size of the target unit and the range of your action. This was SVN commit r4186.
2006-08-01 05:41:21 +02:00
*((float*)&waypoint.m_data[0].data) = 0.0f;
entity->m_orderQueue.push_front( waypoint );
}
void CPathfindEngine::requestLowLevelPath( HEntity entity, const CVector2D& destination, bool UNUSED(contact),
float radius, CEntityOrder::EOrderSource orderSource )
{
PROFILE_START("Pathfinding");
CVector2D source( entity->m_position.X, entity->m_position.Z );
if ( mLowPathfinder.findPath(source, destination, entity->GetPlayer(), radius) )
{
std::vector<CVector2D> path = mLowPathfinder.getLastPath();
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_data[0].location = finalDest;
entity->m_orderQueue.push_front(node);
node.m_type = CEntityOrder::ORDER_GOTO_NOPATHING; // push final goto step
node.m_data[0].location = finalDest;
entity->m_orderQueue.push_front(node);
for( int i = ((int) path.size()) - 2; i >= 0; i-- )
{
node.m_type = CEntityOrder::ORDER_GOTO_NOPATHING; // TODO: For non-contact paths, do we want some other order type?
node.m_data[0].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_data[0].location = destination;
entity->m_orderQueue.push_front(node);
node.m_type = CEntityOrder::ORDER_GOTO_NOPATHING;
node.m_data[0].location = destination;
entity->m_orderQueue.push_front(node);
}
}
}
else
{
// If no path was found, then unsolvable
// TODO: Figure out what to do in this case
}
PROFILE_END("Pathfinding");
}
# Housekeeping and pathfinder enhancements / optimization when dealing with ranged actions. - Modified Xeromyces to no longer automatically convert element and attribute names to lowercase, so that we can have camelCase names. We should now be able to convert all the multi-word entity properties, like pass_through_allies, to camelCase, like passThroughAllies, which is more consistent with the rest of our JavaScript naming conventions. To support the existing code that assumes lowercase element names, I made the getElementID and getAttributeID methods (used in the EL and AT macros) ignore case, and I changed any code that directly accessed element names to use the right case. CEntityTemplate now converts Names_LikeThis to names_likeThis (changing each separate "word" in the name to camelCase). Changed the version letter in XMB filenames from A to B to support this without requiring people to delete old XMBs. - Enhanced the pathfinder's handling of contact paths, resulting in a very large speedup for actions like attacking, construction, etc. The problem was that the pathfinder used to not count a given state as the goal unless it was exactly coincident with the target location. This is fine when you order a unit to go exactly to a certain spot, but if you're ordering a unit to build, gather or attack something, then the target tile is impassable (because your target is there) and therefore the pathfinder never declares a state final. As a result, the pathfinder tries hundreds of extra tiles in case there is a long path that gets to the goal, and after failing to find any path that reaches the goal, it gives you one to the closest point it got to. To fix it, I made the pathfinder take into account a radius around the goal in which it's OK to be, which depends on the size of the target unit and the range of your action. This was SVN commit r4186.
2006-08-01 05:41:21 +02:00
void CPathfindEngine::requestContactPath( HEntity entity, CEntityOrder* current, float range )
{
/* TODO: Same as non-contact: need high-level planner */
CEntityOrder waypoint;
waypoint.m_type = CEntityOrder::ORDER_GOTO_WAYPOINT_CONTACT;
waypoint.m_source = current->m_source;
waypoint.m_data[0].location = current->m_data[0].entity->m_position;
# Housekeeping and pathfinder enhancements / optimization when dealing with ranged actions. - Modified Xeromyces to no longer automatically convert element and attribute names to lowercase, so that we can have camelCase names. We should now be able to convert all the multi-word entity properties, like pass_through_allies, to camelCase, like passThroughAllies, which is more consistent with the rest of our JavaScript naming conventions. To support the existing code that assumes lowercase element names, I made the getElementID and getAttributeID methods (used in the EL and AT macros) ignore case, and I changed any code that directly accessed element names to use the right case. CEntityTemplate now converts Names_LikeThis to names_likeThis (changing each separate "word" in the name to camelCase). Changed the version letter in XMB filenames from A to B to support this without requiring people to delete old XMBs. - Enhanced the pathfinder's handling of contact paths, resulting in a very large speedup for actions like attacking, construction, etc. The problem was that the pathfinder used to not count a given state as the goal unless it was exactly coincident with the target location. This is fine when you order a unit to go exactly to a certain spot, but if you're ordering a unit to build, gather or attack something, then the target tile is impassable (because your target is there) and therefore the pathfinder never declares a state final. As a result, the pathfinder tries hundreds of extra tiles in case there is a long path that gets to the goal, and after failing to find any path that reaches the goal, it gives you one to the closest point it got to. To fix it, I made the pathfinder take into account a radius around the goal in which it's OK to be, which depends on the size of the target unit and the range of your action. This was SVN commit r4186.
2006-08-01 05:41:21 +02:00
*((float*)&waypoint.m_data[0].data) = std::max( current->m_data[0].entity->m_bounds->m_radius, range );
entity->m_orderQueue.push_front( waypoint );
//pathSparse( entity, current->m_data[0].entity->m_position );
//// 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;
// }
//}
}