Skip to content

Commit

Permalink
Merge pull request #651 from Wargus/pathfinding
Browse files Browse the repository at this point in the history
Pathfinding
  • Loading branch information
Jarod42 committed Apr 6, 2024
2 parents f5467c6 + 5dc5ea4 commit 15fb3b5
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 112 deletions.
2 changes: 1 addition & 1 deletion src/action/action_move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ int DoActionMove(CUnit &unit)
}

UnmarkUnitFieldFlags(unit);
d = NextPathElement(unit, &posd.x, &posd.y);
d = NextPathElement(unit, &posd);
MarkUnitFieldFlags(unit);
switch (d) {
case PF_UNREACHABLE: // Can't reach, stop
Expand Down
3 changes: 3 additions & 0 deletions src/include/actions.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
#include "unitptr.h"
#include "vec2i.h"

#include <memory>
#include <string_view>

/*----------------------------------------------------------------------------
-- Declarations
----------------------------------------------------------------------------*/
Expand Down
3 changes: 2 additions & 1 deletion src/include/pathfinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

//@{

void DrawLastAStar(const class CViewport &vp);
#if defined(DEBUG_ASTAR)
#define AstarDebugPrint(format, ...) DebugPrint(format, ##__VA_ARGS__)
#else
Expand Down Expand Up @@ -225,7 +226,7 @@ extern void InitPathfinder();
extern void FreePathfinder();

/// Returns the next element of the path
extern int NextPathElement(CUnit &unit, short int *xdp, short int *ydp);
extern int NextPathElement(CUnit &unit, Vec2i *dir);
/// Return path length to unit 'dst'.
extern int UnitReachable(const CUnit &src, const CUnit &dst, int range, bool from_outside_container);
/// Return path length to unit 'dst' or error code.
Expand Down
19 changes: 9 additions & 10 deletions src/include/tile.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class CMapFieldPlayerInfo
class CMapField
{
public:
CMapField();
CMapField() = default;

void Save(CFile &file) const;
void parse(lua_State *l);
Expand Down Expand Up @@ -231,30 +231,29 @@ class CMapField
unsigned int getFlag() const { return Flags; }
void setGraphicTile(unsigned int tile) { this->tile = tile; }
#ifdef DEBUG
int64_t lastAStarCost; /// debugging pathfinder
int64_t lastAStarCost = 0; /// debugging pathfinder
#endif

uint8_t getElevation() const { return this->ElevationLevel; }
void setElevation(const uint8_t newLevel) { this->ElevationLevel = newLevel; }
void setElevation(const uint8_t newLevel) { this->ElevationLevel = newLevel; }

private:
#ifdef DEBUG
tile_index tilesetTile; /// tileset tile number
tile_index tilesetTile = 0; /// tileset tile number
#endif
graphic_index tile; /// graphic tile number
graphic_index tile = 0; /// graphic tile number
public:
tile_flags Flags; /// field flags
tile_flags Flags = 0; /// field flags
private:
unsigned char cost; /// unit cost to move in this tile
unsigned char cost = 0; /// unit cost to move in this tile
public:
unsigned int Value; /// HP for walls/Wood Regeneration, value of stored resource for forest or harvestable terrain
unsigned int Value = 0; /// HP for walls/Wood Regeneration, value of stored resource for forest or harvestable terrain
std::vector<CUnit *> UnitCache; /// A unit on the map field.

CMapFieldPlayerInfo playerInfo; /// stuff related to player

private:
uint8_t ElevationLevel {0}; /// highground elevation level

uint8_t ElevationLevel {0}; /// highground elevation level
};

extern PixelSize PixelTileSize; /// Size of a tile in pixels
Expand Down
1 change: 1 addition & 0 deletions src/map/map_draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ void CViewport::DrawMapBackgroundInViewport(const fieldHighlightChecker highligh
}
}
}
DrawLastAStar(*this);
#endif
/// Highlight layer if needed (editor stuff)
if (highlightChecker && highlightChecker(mf)) {
Expand Down
11 changes: 0 additions & 11 deletions src/map/mapfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,6 @@
#include "unit.h"
#include "unit_manager.h"

CMapField::CMapField() :
#ifdef DEBUG
tilesetTile(0),
#endif
tile(0),
Flags(0),
cost(0),
Value(0),
UnitCache()
{}

bool CMapField::IsTerrainResourceOnMap(int resource) const
{
switch (resource) {
Expand Down
Loading

0 comments on commit 15fb3b5

Please sign in to comment.