Nilorea Library: Isometric engine + A* pathfinding + Dead Reckoning demo.
Nilorea Library: Isometric engine + A* pathfinding + Dead Reckoning demo Console-only demonstration showing how the three new modules work together:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define MAP_W 20
#define MAP_H 20
#define NUM_TERRAINS 4
#define MAX_HEIGHT 5
#define TILE_WIDTH 64.0f
static const char*
terrain_names[] = {
"Grass",
"Sand",
"Water",
"Rock"};
for (
int y = 0; y < map->
height; y++) {
for (
int x = 0; x < map->
width; x++) {
grid[y][x] = '#';
} else if (t == 2) {
grid[y][x] = '~';
} else {
grid[y][x] = (char)('0' + h);
}
}
}
if (path) {
for (
int i = 0; i < path->
length; i++) {
if (px >= 0 && px < MAP_W && py >= 0 && py <
MAP_H) {
if (i == 0)
grid[py][px] = 'S';
else if (i == path->
length - 1)
grid[py][px] = 'G';
else
grid[py][px] = '*';
}
}
}
printf("\n ");
for (
int x = 0; x < map->
width; x++) printf(
"%d", x % 10);
printf("\n");
for (
int y = 0; y < map->
height; y++) {
printf("%2d", y);
for (
int x = 0; x < map->
width; x++) {
printf("%c", grid[y][x]);
}
printf("\n");
}
printf("\nLegend: #=blocked ~=water 0-5=height S=start G=goal *=path\n");
}
for (
int y = 0; y < map->
height; y++) {
for (
int x = 0; x < map->
width; x++) {
}
}
for (
int x = 0; x < map->
width; x++) {
for (int y = 15; y < 18; y++) {
}
}
for (int y = 8; y < 11; y++) {
for (
int x = 0; x < map->
width; x++) {
}
}
for (int y = 8; y < 11; y++) {
}
for (int y = 1; y < 6; y++) {
for (int x = 14; x < 19; x++) {
}
}
for (int y = 3; y < 7; y++) {
}
for (int x = 3; x < 7; x++) {
}
}
printf("=== DEMO 1: Isometric Engine ===\n\n");
printf(
"Projection: classic 2:1 (%.1f deg)\n", map->
proj.
angle_deg);
printf(" half_w=%.1f half_h=%.1f tile_lift=%.1f\n\n",
printf("Coordinate conversion examples (map -> screen):\n");
int test_coords[][3] = {{0, 0, 0}, {5, 5, 0}, {10, 0, 0}, {0, 10, 0}, {10, 10, 2}};
for (int i = 0; i < 5; i++) {
float sx, sy;
test_coords[i][2], &sx, &sy);
printf(" Map(%d,%d) h=%d -> Screen(%.1f, %.1f)\n",
test_coords[i][0], test_coords[i][1], test_coords[i][2], sx, sy);
}
printf("\nReverse conversion examples (screen -> map):\n");
float test_screen[][2] = {{32.0f, 16.0f}, {200.0f, 100.0f}, {0.0f, 320.0f}};
for (int i = 0; i < 3; i++) {
int mx, my;
printf(" Screen(%.0f, %.0f) -> Map(%d, %d)\n",
test_screen[i][0], test_screen[i][1], mx, my);
}
printf("\nHeight interpolation at fractional coordinates:\n");
float test_frac[][2] = {{10.0f, 9.0f}, {10.5f, 9.5f}, {15.0f, 3.0f}, {15.5f, 3.5f}};
for (int i = 0; i < 4; i++) {
printf(" Height at (%.1f, %.1f) = %.2f\n", test_frac[i][0], test_frac[i][1], h);
}
printf("\nTerrain transitions (edge/corner bitmasks):\n");
int trans_coords[][2] = {{9, 8}, {10, 7}, {14, 6}, {6, 15}};
for (int i = 0; i < 4; i++) {
int edge, corner;
int tx = trans_coords[i][0], ty = trans_coords[i][1];
printf(" (%2d,%2d) terrain=%s edge=0x%X corner=0x%X\n",
}
printf("\nCorner heights for smooth rendering:\n");
int corner_coords[][2] = {{10, 9}, {15, 3}, {7, 5}};
for (int i = 0; i < 3; i++) {
float hn, he, hs, hw;
int cx = corner_coords[i][0], cy = corner_coords[i][1];
printf(" (%2d,%2d) h=%d corners: N=%.1f E=%.1f S=%.1f W=%.1f\n",
}
printf("\nMap save/load test:\n");
printf(" Save: %s\n", saved ? "OK" : "FAILED");
if (loaded) {
int match = 1;
for (
int y = 0; y < map->
height && match; y++)
for (
int x = 0; x < map->
width && match; x++)
match = 0;
printf(" Load: OK (data %s)\n", match ? "matches" : "MISMATCH");
} else {
printf(" Load: FAILED\n");
}
remove("test_iso_map.bin");
}
printf("\n=== DEMO 2: A* Pathfinding ===\n\n");
if (!grid) {
printf("Failed to create A* grid\n");
return NULL;
}
for (
int y = 0; y < map->
height; y++) {
for (
int x = 0; x < map->
width; x++) {
uint8_t walkable = (ab ==
WALK || ab ==
SWIM) ? 1 : 0;
}
}
int sx = 2, sy = 2, gx = 17, gy = 17;
printf("Finding path from (%d,%d) to (%d,%d)...\n", sx, sy, gx, gy);
if (path) {
printf("Path found! Length: %d nodes, Cost: %d (x1000)\n",
printf("Path: ");
for (
int i = 0; i < path->
length; i++) {
if (i > 0) printf(" -> ");
}
printf("\n");
} else {
printf("No path found!\n");
}
printf("\nFinding path from (2,5) to (2,15) (must use bridge)...\n");
if (path2) {
printf(
"Path found! Length: %d nodes, Cost: %d\n", path2->
length, path2->
cost);
printf("Path: ");
for (
int i = 0; i < path2->
length; i++) {
if (i > 0) printf(" -> ");
}
printf("\n");
} else {
printf("No path found!\n");
}
printf("\nFinding path from (0,0) to blocked cell (7,4)...\n");
printf("Result: %s\n", path3 ? "Found (unexpected!)" : "No path (correct - cell is blocked)");
return path;
}
printf("\n=== DEMO 3: Dead Reckoning Along A* Path ===\n\n");
if (!path || path->
length < 2) {
printf("No valid path for dead reckoning demo\n");
return;
}
if (!traj) {
printf("Failed to create trajectory\n");
return;
}
double move_speed = 3.0;
double t = 0.0;
for (
int i = 0; i < path->
length; i++) {
memset(&state, 0, sizeof(state));
if (i < path->length - 1) {
double dx = (double)(path->
nodes[i + 1].
x - path->
nodes[i].
x);
double dy = (double)(path->
nodes[i + 1].
y - path->
nodes[i].
y);
double dist = sqrt(dx * dx + dy * dy);
if (dist > 0.001) {
state.speed[0] = dx / dist * move_speed;
state.speed[1] = dy / dist * move_speed;
}
}
if (i < path->length - 1) {
double dx = (double)(path->
nodes[i + 1].
x - path->
nodes[i].
x);
double dy = (double)(path->
nodes[i + 1].
y - path->
nodes[i].
y);
double dist = sqrt(dx * dx + dy * dy);
t += dist / move_speed;
}
}
double total_time = t;
printf(
"Trajectory total time: %.2f seconds (%d waypoints)\n\n", total_time, path->
length);
if (!dr) {
printf("Failed to create DR entity\n");
return;
}
double sim_dt = 0.05;
double send_interval = 0.5;
double last_send = -send_interval;
printf("Time | True Pos | DR Pos | Error | Height | Updates\n");
printf("------+---------------+---------------+-------+--------+--------\n");
for (double sim_t = 0.0; sim_t <= total_time + 0.1; sim_t += sim_dt) {
if (sim_t - last_send >= send_interval) {
last_send = sim_t;
}
double err = sqrt((dr_pos.
x - true_pos[0]) * (dr_pos.
x - true_pos[0]) +
(dr_pos.
y - true_pos[1]) * (dr_pos.
y - true_pos[1]));
double print_interval = 0.5;
double mod = fmod(sim_t, print_interval);
if (mod < sim_dt || sim_t < sim_dt) {
printf("%5.2f | (%5.2f,%5.2f) | (%5.2f,%5.2f) | %5.3f | %6.2f | %d\n",
sim_t,
true_pos[0], true_pos[1],
}
}
printf("\n--- Convergence Mode Comparison ---\n");
const char* mode_names[] = {"Snap", "PVB", "Cubic"};
for (int m = 0; m < 3; m++) {
double total_err = 0.0;
int samples = 0;
last_send = -send_interval;
for (double sim_t = 0.0; sim_t <= total_time; sim_t += sim_dt) {
if (sim_t - last_send >= send_interval) {
last_send = sim_t;
}
double err = sqrt((dp.
x - tp[0]) * (dp.
x - tp[0]) +
(dp.
y - tp[1]) * (dp.
y - tp[1]));
total_err += err;
samples++;
}
printf(" %-6s: avg error = %.4f over %d samples\n",
mode_names[m], total_err / (double)samples, samples);
}
printf("\n--- Threshold Check Demo ---\n");
int updates_sent = 0;
for (double sim_t = 0.0; sim_t <= total_time; sim_t += sim_dt) {
updates_sent++;
}
}
printf(" With threshold=1.0 tile: sent %d updates over %.1f seconds\n",
updates_sent, total_time);
printf(" (vs %d with fixed 0.5s interval)\n", (int)(total_time / send_interval) + 1);
}
printf("\n=== DEMO 4: A* Pathfinding Standalone Tests ===\n\n");
if (!grid) {
printf("Failed to create grid\n");
return;
}
printf("10x10 maze grid (. = open, # = wall):\n ");
for (int x = 0; x < 10; x++) printf("%d", x);
printf("\n");
for (int y = 0; y < 10; y++) {
printf("%d ", y);
for (int x = 0; x < 10; x++) {
}
printf("\n");
}
const char* heur_names[] = {"Manhattan", "Euclidean", "Chebyshev"};
printf("\nPaths from (0,0) to (9,9) with different heuristics:\n");
for (int h = 0; h < 3; h++) {
if (p) {
printf(" %-10s: length=%2d cost=%5d ",
for (
int i = 0; i < p->
length && i < 15; i++) {
if (i > 0) printf("->");
}
if (p->
length > 15) printf(
"...");
printf("\n");
} else {
printf(" %-10s: no path\n", heur_names[h]);
}
}
printf("\nCardinal-only vs diagonal movement:\n");
if (p_card) printf(
" Cardinal: length=%d cost=%d\n", p_card->
length, p_card->
cost);
if (p_diag) printf(
" Diagonal: length=%d cost=%d\n", p_diag->
length, p_diag->
cost);
}
int main(
int argc,
char* argv[]) {
(void)argc;
(void)argv;
printf("Nilorea Library: Isometric + A* + Dead Reckoning Demo\n");
printf("======================================================\n");
if (!map) {
fprintf(stderr, "Failed to create map\n");
return EXIT_FAILURE;
}
printf("\nMap overview (%dx%d, %d terrains, max height %d):\n",
if (path) {
printf("\nMap with A* path overlaid:\n");
}
printf("\n=== All demos complete ===\n");
return EXIT_SUCCESS;
}
static const char * terrain_names[8]
static ASTAR_PATH * demo_astar(const ISO_MAP *map)
static void demo_astar_standalone(void)
static void demo_iso_engine(ISO_MAP *map)
static void build_test_map(ISO_MAP *map)
static void print_map_ascii(const ISO_MAP *map, const ASTAR_PATH *path)
static void demo_dead_reckoning(const ISO_MAP *map, const ASTAR_PATH *path)
int cost
total path cost (x1000 fixed-point)
ASTAR_NODE * nodes
array of path nodes from start to goal
int length
number of nodes in the path
ASTAR_PATH * n_astar_find_path(const ASTAR_GRID *grid, int sx, int sy, int sz, int gx, int gy, int gz, int diagonal, ASTAR_HEURISTIC heuristic)
Find a path using A* search.
void n_astar_grid_set_cost(ASTAR_GRID *grid, int x, int y, int z, int cost)
Set a cell's movement cost multiplier.
uint8_t n_astar_grid_get_walkable(const ASTAR_GRID *grid, int x, int y, int z)
Get a cell's walkability.
ASTAR_HEURISTIC
Heuristic function selection for h(n) estimation.
#define ASTAR_ALLOW_DIAGONAL
Movement mode: 8-dir (2D) or 26-dir (3D)
void n_astar_grid_free(ASTAR_GRID *grid)
Free a grid and all its internal data.
#define ASTAR_COST_CARDINAL
Default cost for straight movement (fixed-point x1000)
#define ASTAR_CARDINAL_ONLY
Movement mode: 4-dir (2D) or 6-dir (3D)
void n_astar_grid_set_rect_blocked(ASTAR_GRID *grid, int x1, int y1, int z1, int x2, int y2, int z2)
Set a rectangular region as blocked (wall)
void n_astar_path_free(ASTAR_PATH *path)
Free a path returned by n_astar_find_path.
void n_astar_grid_set_walkable(ASTAR_GRID *grid, int x, int y, int z, uint8_t walkable)
Set a cell's walkability.
ASTAR_GRID * n_astar_grid_new(int width, int height, int depth)
Create a new grid for A* pathfinding.
@ ASTAR_HEURISTIC_EUCLIDEAN
straight-line distance
@ ASTAR_HEURISTIC_CHEBYSHEV
max of axis deltas (optimal for 8-dir)
@ ASTAR_HEURISTIC_MANHATTAN
sum of axis deltas (optimal for 4-dir)
Grid structure holding walkability, costs, and dimensions.
The computed path result.
int update_count
Number of state updates received.
void dr_entity_destroy(DR_ENTITY **entity_ptr)
Destroy a dead reckoning entity and set the pointer to NULL.
static DR_VEC3 dr_vec3(double x, double y, double z)
Create a DR_VEC3 from components.
void dr_entity_compute(DR_ENTITY *entity, double time, DR_VEC3 *out_pos)
Compute the dead reckoned display position at a given time.
DR_ENTITY * dr_entity_create(DR_ALGO algo, DR_BLEND blend_mode, double pos_threshold, double blend_time)
Create a new dead reckoning entity.
DR_BLEND
Dead reckoning convergence/blending mode.
bool dr_entity_check_threshold(const DR_ENTITY *entity, const DR_VEC3 *true_pos, const DR_VEC3 *true_vel, const DR_VEC3 *true_acc, double time)
Check whether the owner's true state has diverged from the dead reckoned prediction beyond the config...
static DR_VEC3 dr_vec3_zero(void)
Zero vector.
void dr_entity_receive_state(DR_ENTITY *entity, const DR_VEC3 *pos, const DR_VEC3 *vel, const DR_VEC3 *acc, double time)
Receive a new authoritative state update from the network.
void dr_entity_set_position(DR_ENTITY *entity, const DR_VEC3 *pos, const DR_VEC3 *vel, const DR_VEC3 *acc, double time)
Force-set entity position without triggering convergence blending.
@ DR_BLEND_CUBIC
Cubic Bezier spline convergence.
@ DR_BLEND_PVB
Projective Velocity Blending (recommended)
@ DR_BLEND_SNAP
Snap instantly to new state (no smoothing)
@ DR_ALGO_VEL_ACC
Velocity + acceleration: P(t) = P0 + V0*t + 0.5*A0*t^2.
Dead reckoned entity with extrapolation and convergence state.
3D vector used for position, velocity, and acceleration
int height
map height in tiles (Y axis)
float tile_lift
vertical pixel offset per height unit
float angle_deg
current projection angle in degrees
float half_w
half-width of a tile in pixels (horizontal extent)
float half_h
half-height of a tile in pixels (vertical extent)
int width
map width in tiles (X axis)
ISO_PROJECTION proj
current projection parameters
void iso_map_set_height(ISO_MAP *map, int mx, int my, int h)
Set the height at a cell (clamped to [0, max_height])
#define SWIM
FLAG of a swimmable tile.
ISO_MAP * iso_map_new(int width, int height, int num_terrains, int max_height)
Create a new height-aware isometric map.
int iso_map_save(const ISO_MAP *map, const char *filename)
Save ISO_MAP to a binary file.
#define WALK
FLAG of a walkable tile.
ISO_MAP * iso_map_load(const char *filename)
Load ISO_MAP from a binary file.
void iso_map_set_ability(ISO_MAP *map, int mx, int my, int ab)
Set the ability at a cell.
int iso_map_get_terrain(const ISO_MAP *map, int mx, int my)
Get the terrain type at a cell.
int iso_map_get_ability(const ISO_MAP *map, int mx, int my)
Get the ability at a cell.
void iso_map_to_screen(const ISO_MAP *map, int mx, int my, int h, float *screen_x, float *screen_y)
Convert map tile coordinates to screen pixel coordinates.
void iso_map_calc_transitions(const ISO_MAP *map, int mx, int my, int *edge_bits, int *corner_bits)
Compute terrain transition bitmasks for a cell (Article 934).
void iso_map_set_projection(ISO_MAP *map, int preset, float tile_width)
Set projection parameters from a preset and tile width.
void iso_map_free(ISO_MAP **map_ptr)
Free an ISO_MAP and set the pointer to NULL.
void iso_map_set_terrain(ISO_MAP *map, int mx, int my, int terrain)
Set the terrain type at a cell.
void iso_screen_to_map(const ISO_MAP *map, float screen_x, float screen_y, int *mx, int *my)
Convert screen pixel coordinates to map tile coordinates.
int iso_map_get_height(const ISO_MAP *map, int mx, int my)
Get the height at a cell.
float iso_map_interpolate_height(const ISO_MAP *map, float fx, float fy)
Bilinear height interpolation at fractional map coordinates.
#define ISO_PROJ_CLASSIC
Projection ID: classic 2:1 isometric (~26.565 degree angle)
#define BLCK
FLAG of a stopping tile.
void iso_map_corner_heights(const ISO_MAP *map, int mx, int my, float *h_n, float *h_e, float *h_s, float *h_w)
Compute average corner heights for smooth tile rendering (Article 2026).
Height-aware isometric map with terrain and height layers.
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
#define LOG_NOTICE
normal but significant condition
VECTOR3D position
x,y,z actual position
double VECTOR3D[3]
struct of a point
structure of the physics of an object
int trajectory_get_position(TRAJECTORY *traj, double time_val, VECTOR3D out)
Compute position at a given time.
TRAJECTORY * trajectory_new(int nb_components)
Allocate and initialize a new TRAJECTORY.
int trajectory_add_point(TRAJECTORY *traj, const PHYSICS *state, double time_val)
Add a waypoint to the multi-point trajectory path.
void trajectory_delete(TRAJECTORY **traj)
Free a TRAJECTORY and set the pointer to NULL.
#define TRAJECTORY_2D
use 2 components (x,y) for trajectory computation
int trajectory_get_speed(TRAJECTORY *traj, double time_val, VECTOR3D out)
Compute velocity at a given time.
structure holding all data for trajectory interpolation / extrapolation
A* Pathfinding API for 2D and 3D grids.
Common headers and low-level functions & define.
Dead Reckoning API for latency hiding in networked games.
Isometric/axonometric tile engine with height maps, terrain transitions, and A* pathfinding integrati...
Trajectory interpolation and dead reckoning for 2D/3D networked simulations.