limiting pathfinding for units to their types of cells

This commit is contained in:
devZoGok
2023-09-10 15:48:55 +03:00
parent 9e40cb85f4
commit 21db6584e0
5 changed files with 21 additions and 9 deletions
+3 -3
View File
@@ -2,7 +2,7 @@ numUnits = 4
UnitClass = {VESSEL = 0, ENGINEER = 1, SAMPLE_STRUCTURE = 2};
UnitType = {UNDERWATER = 0, SEA_LEVEL = 1, LAND = 2, AIR = 3};
UnitType = {UNDERWATER = 0, SEA_LEVEL = 1, HOVER = 2, LAND = 3, AIR = 4};
unitClass = {
UnitClass.VESSEL, UnitClass.VESSEL,
@@ -10,8 +10,8 @@ unitClass = {
};
unitType = {
UnitType.SEA_LEVEL, UnitType.UNDERWATER,
UnitType.LAND, UnitType.LAND
UnitType.HOVER, UnitType.SEA_LEVEL,
UnitType.LAND, UnitType.HOVER
}
isVehicle = {
+8 -3
View File
@@ -1,7 +1,7 @@
#include <algorithm>
#include <iostream>
#include "pathfinder.h"
#include "unit.h"
namespace battleship{
using namespace std;
@@ -19,7 +19,7 @@ namespace battleship{
Pathfinder::Pathfinder(){
}
vector<int> Pathfinder::findPath(vector<Map::Cell> &cells, int source, int dest){
vector<int> Pathfinder::findPath(vector<Map::Cell> &cells, int source, int dest, int unitType){
int size = cells.size();
u32 distances[size];
vector<int> paths[size];
@@ -52,8 +52,13 @@ namespace battleship{
for(int i = 0; i < size; i++){
bool isChecked = find(checkedNodes.begin(), checkedNodes.end(), i) != checkedNodes.end();
bool canMoveToStrichCell = true;
bool ship = ((UnitType)unitType == UnitType::UNDERWATER || (UnitType)unitType == UnitType::SEA_LEVEL);
if(!isChecked && (distances[vertStrich] + cells[vertStrich].getEdgeWeight(i) < distances[i])){
if((ship && cells[vertStrich].type != Map::Cell::WATER) || ((UnitType)unitType == UnitType::LAND && cells[vertStrich].type != Map::Cell::LAND))
canMoveToStrichCell = false;
if(!isChecked && (canMoveToStrichCell && distances[vertStrich] + cells[vertStrich].getEdgeWeight(i) < distances[i])){
distances[i] = distances[vertStrich] + cells[vertStrich].getEdgeWeight(i);
paths[i] = paths[vertStrich];
paths[i].push_back(i);
+1 -1
View File
@@ -11,7 +11,7 @@ namespace battleship{
class Pathfinder{
public:
static Pathfinder* getSingleton();
std::vector<int> findPath(std::vector<Map::Cell>&, int, int);
std::vector<int> findPath(std::vector<Map::Cell>&, int, int, int = -1);
inline vb01::u32 getImpassibleNodeVal(){return impassibleNodeVal;}
inline void setImpassibleNodeVal(vb01::u32 val){this->impassibleNodeVal = val;}
private:
+1 -1
View File
@@ -42,7 +42,7 @@ namespace battleship{
enum class MoveDir {LEFT, UP, FORW};
enum class Corner {FRONT_LEFT, FRONT_RIGHT, REAR_LEFT, REAR_RIGHT};
enum class UnitClass {VESSEL, ENGINEER, SAMPLE_BUILDING};
enum class UnitType {UNDERWATER, SEA_LEVEL, LAND, AIR};
enum class UnitType {UNDERWATER, SEA_LEVEL, HOVER, LAND, AIR, NONE = -1};
class Unit {
public:
+8 -1
View File
@@ -153,8 +153,15 @@ namespace battleship{
int source = map->getCellId(pos);
int dest = map->getCellId(order.targets[0].pos);
if(type == UnitType::UNDERWATER || type == UnitType::SEA_LEVEL || type == UnitType::LAND){
Map::Cell::Type cellType = (type == UnitType::LAND ? Map::Cell::LAND : Map::Cell::WATER);
bool canReach = (cells[source].type == cellType && cells[dest].type == cellType);
if(!canReach) return;
}
Pathfinder *pathfinder = Pathfinder::getSingleton();
vector<int> path = pathfinder->findPath(cells, source, dest);
vector<int> path = pathfinder->findPath(cells, source, dest, (int)type);
Node *rootNode = Root::getSingleton()->getRootNode();
for(int p : path){