Changeset View
Changeset View
Standalone View
Standalone View
source/simulation2/components/CCmpPathfinder.cpp
Show All 30 Lines | |||||
#include "renderer/Scene.h" | #include "renderer/Scene.h" | ||||
#include "simulation2/MessageTypes.h" | #include "simulation2/MessageTypes.h" | ||||
#include "simulation2/components/ICmpObstruction.h" | #include "simulation2/components/ICmpObstruction.h" | ||||
#include "simulation2/components/ICmpObstructionManager.h" | #include "simulation2/components/ICmpObstructionManager.h" | ||||
#include "simulation2/components/ICmpTerrain.h" | #include "simulation2/components/ICmpTerrain.h" | ||||
#include "simulation2/components/ICmpWaterManager.h" | #include "simulation2/components/ICmpWaterManager.h" | ||||
#include "simulation2/helpers/Rasterize.h" | #include "simulation2/helpers/Rasterize.h" | ||||
#include "simulation2/serialization/SerializeTemplates.h" | #include "simulation2/serialization/SerializeTemplates.h" | ||||
#include "simulation2/components/ICmpPosition.h" | |||||
#include "simulation2/components/ICmpUnitMotion.h" | |||||
REGISTER_COMPONENT_TYPE(Pathfinder) | REGISTER_COMPONENT_TYPE(Pathfinder) | ||||
void CCmpPathfinder::Init(const CParamNode& UNUSED(paramNode)) | void CCmpPathfinder::Init(const CParamNode& UNUSED(paramNode)) | ||||
{ | { | ||||
m_MapSize = 0; | m_MapSize = 0; | ||||
m_Grid = NULL; | m_Grid = NULL; | ||||
m_TerrainOnlyGrid = NULL; | m_TerrainOnlyGrid = NULL; | ||||
▲ Show 20 Lines • Show All 163 Lines • ▼ Show 20 Lines | pass_class_t CCmpPathfinder::GetPassabilityClass(const std::string& name) const | ||||
{ | { | ||||
LOGERROR("Invalid passability class name '%s'", name.c_str()); | LOGERROR("Invalid passability class name '%s'", name.c_str()); | ||||
return 0; | return 0; | ||||
} | } | ||||
return it->second; | return it->second; | ||||
} | } | ||||
/** | |||||
* Assuming we're a ship, check if we're near the shore. | |||||
*/ | |||||
bool CCmpPathfinder::CheckNearShore(entity_id_t ship, entity_id_t landUnit, fixed pickupRange) const | |||||
{ | |||||
if (pickupRange == fixed::FromInt(0)) | |||||
return false; | |||||
CmpPtr<ICmpPosition> cmpPositionShip(GetSimContext(), ship); | |||||
if (!cmpPositionShip || !cmpPositionShip->IsInWorld()) | |||||
return false; | |||||
CmpPtr<ICmpPosition> cmpPositionLand(GetSimContext(), landUnit); | |||||
if (!cmpPositionLand || !cmpPositionLand->IsInWorld()) | |||||
return false; | |||||
CmpPtr<ICmpUnitMotion> cmpUnitMotionLand(GetSimContext(), landUnit); | |||||
if (!cmpUnitMotionLand) | |||||
return false; | |||||
CmpPtr<ICmpUnitMotion> cmpUnitMotionShip(GetSimContext(), ship); | |||||
if (!cmpUnitMotionShip) | |||||
return false; | |||||
fixed clearance = cmpUnitMotionShip->GetUnitClearance(); | |||||
wraitii: I'd rather use cmpObstruction::GetSize for that. It would let players construct tunnels (which… | |||||
Done Inline ActionsIt sounds like a stationary tunnel would work more like building garrison, so it wouldn't need to call this function. causative: It sounds like a stationary tunnel would work more like building garrison, so it wouldn't need… | |||||
HierarchicalPathfinder hier = m_LongPathfinder.GetHierarchicalPathfinder(); | |||||
CFixedVector2D posShip = cmpPositionShip->GetPosition2D(); | |||||
CFixedVector2D posLand = cmpPositionLand->GetPosition2D(); | |||||
u16 i, j; | |||||
Pathfinding::NearestNavcell(posLand.X, posLand.Y, i, j, m_TerrainOnlyGrid->m_W, m_TerrainOnlyGrid->m_H); | |||||
pass_class_t passClass = cmpUnitMotionLand->GetPassabilityClass(); | |||||
HierarchicalPathfinder::RegionID landRegion = hier.Get(i, j, passClass); | |||||
std::unordered_set<HierarchicalPathfinder::RegionID, HierarchicalPathfinder::RegionHash> reachable; | |||||
Done Inline ActionsCouldn't we replace set by unordered_set in future? vladislavbelov: Couldn't we replace `set` by `unordered_set` in future? | |||||
hier.FindReachableRegions(landRegion, reachable, passClass); | |||||
Done Inline ActionsI think you should also check right under the "ship" entity, to make this function more general (again, thinking of the "Roc" use-case from AoM) wraitii: I think you should also check right under the "ship" entity, to make this function more general… | |||||
u16 i0, j0, i1, j1; // bounds of square to search for land | |||||
Pathfinding::NearestNavcell(posShip.X, posShip.Y, i, j, m_TerrainOnlyGrid->m_W, m_TerrainOnlyGrid->m_H); | |||||
i16 hw = ((pickupRange + clearance) / Pathfinding::NAVCELL_SIZE).ToInt_RoundToInfinity(); | |||||
i0 = i-hw; | |||||
i1 = i+hw; | |||||
j0 = j-hw; | |||||
j1 = j+hw; | |||||
i = i0; | |||||
Done Inline ActionsSpaces around = and below too. vladislavbelov: Spaces around `=` and below too. | |||||
j = j0; | |||||
u16 offset = j1-j0; | |||||
while (true) | |||||
{ | |||||
landRegion = hier.Get(i, j, passClass); | |||||
if (reachable.find(landRegion) != reachable.end()) | |||||
return true; | |||||
Done Inline ActionsDo not use count if you only search a value, use reachable.find(landRegion) != reachable. vladislavbelov: Do not use count if you only search a value, use `reachable.find(landRegion) != reachable`. | |||||
Done Inline Actions... != reachable.end(). vladislavbelov: `... != reachable.end()`. | |||||
// i,j takes on the values on the border of the square | |||||
if (i == i0 || i == i1) | |||||
Done Inline ActionsUnecessary variable, you could just return a value: if (...) return true; // after loop return false; vladislavbelov: Unecessary variable, you could just return a value:
```lang=cpp
if (...)
return true;
//… | |||||
j++; | |||||
else | |||||
j += offset; | |||||
if (j > j1) | |||||
{ | |||||
i++; | |||||
j=j0; | |||||
} | |||||
if (i > i1) | |||||
break; | |||||
} | |||||
return false; | |||||
} | |||||
void CCmpPathfinder::GetPassabilityClasses(std::map<std::string, pass_class_t>& passClasses) const | void CCmpPathfinder::GetPassabilityClasses(std::map<std::string, pass_class_t>& passClasses) const | ||||
{ | { | ||||
passClasses = m_PassClassMasks; | passClasses = m_PassClassMasks; | ||||
} | } | ||||
void CCmpPathfinder::GetPassabilityClasses(std::map<std::string, pass_class_t>& nonPathfindingPassClasses, std::map<std::string, pass_class_t>& pathfindingPassClasses) const | void CCmpPathfinder::GetPassabilityClasses(std::map<std::string, pass_class_t>& nonPathfindingPassClasses, std::map<std::string, pass_class_t>& pathfindingPassClasses) const | ||||
{ | { | ||||
for (const std::pair<std::string, pass_class_t>& pair : m_PassClassMasks) | for (const std::pair<std::string, pass_class_t>& pair : m_PassClassMasks) | ||||
▲ Show 20 Lines • Show All 663 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
I'd rather use cmpObstruction::GetSize for that. It would let players construct tunnels (which wouldn't have Motion) or something like that.