134 lines
4.7 KiB
C
134 lines
4.7 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdbool.h>
|
|
#include <math.h>
|
|
#include "data.h"
|
|
|
|
#define MAX_UNITS 100
|
|
#define MAX_BUILDINGS 50
|
|
|
|
typedef struct Node {
|
|
int x, y;
|
|
int gCost; // Cost from start node
|
|
int hCost; // Heuristic cost to target node
|
|
struct Node* parent;
|
|
} Node;
|
|
|
|
// Function to calculate the distance between two points using Manhattan distance
|
|
int calculateDistance(int x1, int y1, int x2, int y2) {
|
|
return abs(x1 - x2) + abs(y1 - y2);
|
|
}
|
|
|
|
bool isUnitOrBuildingAtPosition(GameData* data, int x, int y) {
|
|
for (int i = 0; i < MAX_UNITS; i++) {
|
|
if (data->units[i].unitID != -1 && data->units[i].x == x && data->units[i].y == y) {
|
|
// Found a unit at the position
|
|
return true;
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < MAX_BUILDINGS; i++) {
|
|
if (data->buildings[i].buildingID != -1 && data->buildings[i].x == x && data->buildings[i].y == y) {
|
|
// Found a building at the position
|
|
return true;
|
|
}
|
|
}
|
|
|
|
// No unit or building found at the position
|
|
return false;
|
|
}
|
|
|
|
// A* algorithm implementation
|
|
void aStarPathfinding(GameData* map, int startX, int startY, int targetX, int targetY) {
|
|
// Create start and target nodes
|
|
Node startNode = {startX, startY, 0, 0, NULL};
|
|
Node targetNode = {targetX, targetY, 0, 0, NULL};
|
|
|
|
// Create open and closed lists
|
|
Node* openList = malloc(sizeof(Node) * MAP_SIZE * MAP_SIZE);
|
|
Node* closedList = malloc(sizeof(Node) * MAP_SIZE * MAP_SIZE);
|
|
int openListCount = 0;
|
|
int closedListCount = 0;
|
|
|
|
// Add start node to open list
|
|
openList[openListCount++] = startNode;
|
|
|
|
while (openListCount > 0) {
|
|
// Find the node with the lowest fCost in the open list
|
|
int currentIndex = 0;
|
|
for (int i = 0; i < openListCount; i++) {
|
|
if (openList[i].gCost + openList[i].hCost < openList[currentIndex].gCost + openList[currentIndex].hCost) {
|
|
currentIndex = i;
|
|
}
|
|
}
|
|
|
|
// Move current node to the closed list
|
|
Node currentNode = openList[currentIndex];
|
|
openList[currentIndex] = openList[--openListCount];
|
|
closedList[closedListCount++] = currentNode;
|
|
|
|
// Check if we have reached the target node
|
|
if (currentNode.x == targetNode.x && currentNode.y == targetNode.y) {
|
|
// Path found, reconstruct and print the path
|
|
while (currentNode.parent != NULL) {
|
|
printf("(%d, %d) -> ", currentNode.x, currentNode.y);
|
|
currentNode = *currentNode.parent;
|
|
}
|
|
printf("(%d, %d)\n", startX, startY);
|
|
break;
|
|
}
|
|
|
|
// Generate neighboring nodes
|
|
for (int i = -1; i <= 1; i++) {
|
|
for (int j = -1; j <= 1; j++) {
|
|
if (i == 0 && j == 0) continue;
|
|
|
|
int neighborX = currentNode.x + i;
|
|
int neighborY = currentNode.y + j;
|
|
|
|
// Ignore invalid neighbors or obstacles
|
|
if (neighborX < 0 || neighborX >= MAP_SIZE || neighborY < 0 || neighborY >= MAP_SIZE ||
|
|
map->currentMap.tileData[neighborX][neighborY] == 1 || isUnitOrBuildingAtPosition(map, neighborX, neighborY)) {
|
|
continue;
|
|
}
|
|
|
|
// Calculate neighbor costs
|
|
int gCost = currentNode.gCost + 1; // Assuming uniform movement cost
|
|
int hCost = calculateDistance(neighborX, neighborY, targetNode.x, targetNode.y);
|
|
|
|
// Check if neighbor is in the closed list
|
|
bool inClosedList = false;
|
|
for (int k = 0; k < closedListCount; k++) {
|
|
if (closedList[k].x == neighborX && closedList[k].y == neighborY) {
|
|
inClosedList = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!inClosedList) {
|
|
// Check if neighbor is in the open list
|
|
bool inOpenList = false;
|
|
for (int k = 0; k < openListCount; k++) {
|
|
if (openList[k].x == neighborX && openList[k].y == neighborY) {
|
|
inOpenList = true;
|
|
if (gCost < openList[k].gCost) {
|
|
openList[k].gCost = gCost;
|
|
openList[k].parent = ¤tNode;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!inOpenList) {
|
|
// Add neighbor to open list
|
|
openList[openListCount++] = (Node){neighborX, neighborY, gCost, hCost, ¤tNode};
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
free(openList);
|
|
free(closedList);
|
|
}
|