#include <stdio.h>
#include <stdint.h>
#include <dlfcn.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
struct CPoint {
int32_t x;
int32_t y;
};
struct CPointArray {
uintptr_t length;
const struct CPoint *data;
uintptr_t cap;
};
typedef void (*InitRuntimeFunc)(uintptr_t);
typedef bool (*SetWalkableFunc)(int64_t, const struct CPoint*, int32_t);
typedef void (*FreePathFunc)(struct CPointArray);
typedef int64_t (*LoadMapFunc)(const char *);
typedef int64_t (*LoadMapFromStringFunc)(const char *file_content);
typedef struct CPointArray (*FindPathFunc)(int64_t, const struct CPoint *, const struct CPoint *);
int main()
{
void* handle = dlopen("./libpathfinding.so", RTLD_LAZY);
LoadMapFunc load_map;
FindPathFunc find_path;
InitRuntimeFunc init_runtime;
SetWalkableFunc set_walkable;
FreePathFunc free_path;
LoadMapFromStringFunc load_map_from_string;
init_runtime = dlsym(handle, "init_runtime");
set_walkable = dlsym(handle, "set_walkable");
free_path = dlsym(handle, "free_path");
load_map_from_string = dlsym(handle, "load_map_from_string");
load_map = dlsym(handle, "load_map");
find_path = dlsym(handle, "find_path");
init_runtime(1);
FILE *file;
char *buffer;
long file_length;
file = fopen("World.json", "rb");
if (file == NULL) {
perror("Error opening file");
return 1;
}
fseek(file, 0, SEEK_END);
file_length = ftell(file);
rewind(file);
buffer = (char *)malloc(file_length + 1);
if (buffer == NULL) {
perror("Error allocating memory");
fclose(file);
return 1;
}
if (fread(buffer, 1, file_length, file) != file_length) {
perror("Error reading file");
free(buffer);
fclose(file);
return 1;
}
buffer[file_length] = '\0';
int64_t map_id = load_map_from_string(buffer);
free(buffer);
fclose(file);
if(map_id == 0)
{
printf("加载地图失败\n");
return 2;
}
for(int x = 0; x < 10; x++)
{
struct CPoint start, end;
start.x = 1;
start.y = 0;
end.x = 14;
end.y = 15;
struct CPointArray path = find_path(map_id, &start, &end);
printf("path len: %d\n", path.length);
if(path.length == 0)
{
printf("寻路失败\n");
sleep(1);
continue;
}
for(int i = 0; i < path.length; i++)
{
printf("(%d, %d)\n", path.data[i].x, path.data[i].y);
}
struct CPoint walkable_point;
walkable_point.x = path.data[3 + x].x;
walkable_point.y = path.data[3 + x].y;
set_walkable(map_id, &walkable_point, 1);
free_path(path);
sleep(1);
}
return 0;
}