Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_iso_astar.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47#include <math.h>
48
49#include "nilorea/n_common.h"
50#include "nilorea/n_log.h"
51#include "nilorea/n_astar.h"
55
56#ifndef M_PI
57#define M_PI 3.14159265358979323846
58#endif
59
60/* Map configuration */
61#define MAP_W 20
62#define MAP_H 20
63#define NUM_TERRAINS 4
64#define MAX_HEIGHT 5
65#define TILE_WIDTH 64.0f
66
67static const char* terrain_names[] = {"Grass", "Sand", "Water", "Rock"};
68
69/* Print a simple ASCII view of the map */
70static void print_map_ascii(const ISO_MAP* map, const ASTAR_PATH* path) {
71 /* Build a grid of characters */
72 char grid[MAP_H][MAP_W];
73 for (int y = 0; y < map->height; y++) {
74 for (int x = 0; x < map->width; x++) {
75 int t = iso_map_get_terrain(map, x, y);
76 int h = iso_map_get_height(map, x, y);
77 int ab = iso_map_get_ability(map, x, y);
78 if (ab == BLCK) {
79 grid[y][x] = '#';
80 } else if (t == 2) { /* water */
81 grid[y][x] = '~';
82 } else {
83 grid[y][x] = (char)('0' + h);
84 }
85 }
86 }
87
88 /* Overlay the A* path */
89 if (path) {
90 for (int i = 0; i < path->length; i++) {
91 int px = path->nodes[i].x;
92 int py = path->nodes[i].y;
93 if (px >= 0 && px < MAP_W && py >= 0 && py < MAP_H) {
94 if (i == 0)
95 grid[py][px] = 'S'; /* start */
96 else if (i == path->length - 1)
97 grid[py][px] = 'G'; /* goal */
98 else
99 grid[py][px] = '*'; /* path */
100 }
101 }
102 }
103
104 printf("\n ");
105 for (int x = 0; x < map->width; x++) printf("%d", x % 10);
106 printf("\n");
107 for (int y = 0; y < map->height; y++) {
108 printf("%2d", y);
109 for (int x = 0; x < map->width; x++) {
110 printf("%c", grid[y][x]);
111 }
112 printf("\n");
113 }
114 printf("\nLegend: #=blocked ~=water 0-5=height S=start G=goal *=path\n");
115}
116
117/* Build a test map with varied terrain and obstacles */
118static void build_test_map(ISO_MAP* map) {
119 /* Fill with grass at height 0 */
120 for (int y = 0; y < map->height; y++) {
121 for (int x = 0; x < map->width; x++) {
122 iso_map_set_terrain(map, x, y, 0); /* grass */
123 iso_map_set_height(map, x, y, 0);
124 iso_map_set_ability(map, x, y, WALK);
125 }
126 }
127
128 /* Sand strip along the bottom */
129 for (int x = 0; x < map->width; x++) {
130 for (int y = 15; y < 18; y++) {
131 iso_map_set_terrain(map, x, y, 1); /* sand */
132 }
133 }
134
135 /* Water river through the middle */
136 for (int y = 8; y < 11; y++) {
137 for (int x = 0; x < map->width; x++) {
138 iso_map_set_terrain(map, x, y, 2); /* water */
139 iso_map_set_ability(map, x, y, BLCK);
140 }
141 }
142 /* Bridge over the river */
143 for (int y = 8; y < 11; y++) {
144 iso_map_set_terrain(map, 10, y, 3); /* rock bridge */
145 iso_map_set_height(map, 10, y, 1);
146 iso_map_set_ability(map, 10, y, WALK);
147 }
148
149 /* Rocky hills in the top-right */
150 for (int y = 1; y < 6; y++) {
151 for (int x = 14; x < 19; x++) {
152 iso_map_set_terrain(map, x, y, 3); /* rock */
153 iso_map_set_height(map, x, y, 2 + (x + y) % 3);
154 }
155 }
156
157 /* Wall obstacle */
158 for (int y = 3; y < 7; y++) {
159 iso_map_set_ability(map, 7, y, BLCK);
160 iso_map_set_height(map, 7, y, MAX_HEIGHT);
161 }
162
163 /* Another small wall */
164 for (int x = 3; x < 7; x++) {
165 iso_map_set_ability(map, x, 13, BLCK);
166 iso_map_set_height(map, x, 13, MAX_HEIGHT);
167 }
168}
169
170/* Demo 1: Isometric engine features */
171static void demo_iso_engine(ISO_MAP* map) {
172 printf("=== DEMO 1: Isometric Engine ===\n\n");
173
174 /* Show projection info */
175 printf("Projection: classic 2:1 (%.1f deg)\n", map->proj.angle_deg);
176 printf(" half_w=%.1f half_h=%.1f tile_lift=%.1f\n\n",
177 map->proj.half_w, map->proj.half_h, map->proj.tile_lift);
178
179 /* Map-to-screen conversion */
180 printf("Coordinate conversion examples (map -> screen):\n");
181 int test_coords[][3] = {{0, 0, 0}, {5, 5, 0}, {10, 0, 0}, {0, 10, 0}, {10, 10, 2}};
182 for (int i = 0; i < 5; i++) {
183 float sx, sy;
184 iso_map_to_screen(map, test_coords[i][0], test_coords[i][1],
185 test_coords[i][2], &sx, &sy);
186 printf(" Map(%d,%d) h=%d -> Screen(%.1f, %.1f)\n",
187 test_coords[i][0], test_coords[i][1], test_coords[i][2], sx, sy);
188 }
189
190 /* Screen-to-map conversion */
191 printf("\nReverse conversion examples (screen -> map):\n");
192 float test_screen[][2] = {{32.0f, 16.0f}, {200.0f, 100.0f}, {0.0f, 320.0f}};
193 for (int i = 0; i < 3; i++) {
194 int mx, my;
195 iso_screen_to_map(map, test_screen[i][0], test_screen[i][1], &mx, &my);
196 printf(" Screen(%.0f, %.0f) -> Map(%d, %d)\n",
197 test_screen[i][0], test_screen[i][1], mx, my);
198 }
199
200 /* Height interpolation */
201 printf("\nHeight interpolation at fractional coordinates:\n");
202 float test_frac[][2] = {{10.0f, 9.0f}, {10.5f, 9.5f}, {15.0f, 3.0f}, {15.5f, 3.5f}};
203 for (int i = 0; i < 4; i++) {
204 float h = iso_map_interpolate_height(map, test_frac[i][0], test_frac[i][1]);
205 printf(" Height at (%.1f, %.1f) = %.2f\n", test_frac[i][0], test_frac[i][1], h);
206 }
207
208 /* Terrain transitions */
209 printf("\nTerrain transitions (edge/corner bitmasks):\n");
210 int trans_coords[][2] = {{9, 8}, {10, 7}, {14, 6}, {6, 15}};
211 for (int i = 0; i < 4; i++) {
212 int edge, corner;
213 int tx = trans_coords[i][0], ty = trans_coords[i][1];
214 iso_map_calc_transitions(map, tx, ty, &edge, &corner);
215 printf(" (%2d,%2d) terrain=%s edge=0x%X corner=0x%X\n",
216 tx, ty, terrain_names[iso_map_get_terrain(map, tx, ty)], edge, corner);
217 }
218
219 /* Corner heights for smooth rendering */
220 printf("\nCorner heights for smooth rendering:\n");
221 int corner_coords[][2] = {{10, 9}, {15, 3}, {7, 5}};
222 for (int i = 0; i < 3; i++) {
223 float hn, he, hs, hw;
224 int cx = corner_coords[i][0], cy = corner_coords[i][1];
225 iso_map_corner_heights(map, cx, cy, &hn, &he, &hs, &hw);
226 printf(" (%2d,%2d) h=%d corners: N=%.1f E=%.1f S=%.1f W=%.1f\n",
227 cx, cy, iso_map_get_height(map, cx, cy), hn, he, hs, hw);
228 }
229
230 /* Save and reload */
231 printf("\nMap save/load test:\n");
232 int saved = iso_map_save(map, "test_iso_map.bin");
233 printf(" Save: %s\n", saved ? "OK" : "FAILED");
234 ISO_MAP* loaded = iso_map_load("test_iso_map.bin");
235 if (loaded) {
236 int match = 1;
237 for (int y = 0; y < map->height && match; y++)
238 for (int x = 0; x < map->width && match; x++)
239 if (iso_map_get_terrain(map, x, y) != iso_map_get_terrain(loaded, x, y) ||
240 iso_map_get_height(map, x, y) != iso_map_get_height(loaded, x, y) ||
241 iso_map_get_ability(map, x, y) != iso_map_get_ability(loaded, x, y))
242 match = 0;
243 printf(" Load: OK (data %s)\n", match ? "matches" : "MISMATCH");
244 iso_map_free(&loaded);
245 } else {
246 printf(" Load: FAILED\n");
247 }
248 remove("test_iso_map.bin");
249}
250
251/* Demo 2: A* pathfinding on the isometric map */
252static ASTAR_PATH* demo_astar(const ISO_MAP* map) {
253 printf("\n=== DEMO 2: A* Pathfinding ===\n\n");
254
255 /* Create ASTAR_GRID from the ISO_MAP */
256 ASTAR_GRID* grid = n_astar_grid_new(map->width, map->height, 1);
257 if (!grid) {
258 printf("Failed to create A* grid\n");
259 return NULL;
260 }
261
262 /* Populate walkability from map abilities */
263 for (int y = 0; y < map->height; y++) {
264 for (int x = 0; x < map->width; x++) {
265 int ab = iso_map_get_ability(map, x, y);
266 uint8_t walkable = (ab == WALK || ab == SWIM) ? 1 : 0;
267 n_astar_grid_set_walkable(grid, x, y, 0, walkable);
268
269 /* Higher terrain costs more */
270 int h = iso_map_get_height(map, x, y);
271 n_astar_grid_set_cost(grid, x, y, 0, ASTAR_COST_CARDINAL + h * 200);
272 }
273 }
274
275 /* Find path from top-left area to bottom-right area */
276 int sx = 2, sy = 2, gx = 17, gy = 17;
277 printf("Finding path from (%d,%d) to (%d,%d)...\n", sx, sy, gx, gy);
278
279 ASTAR_PATH* path = n_astar_find_path(grid, sx, sy, 0, gx, gy, 0,
282 if (path) {
283 printf("Path found! Length: %d nodes, Cost: %d (x1000)\n",
284 path->length, path->cost);
285 printf("Path: ");
286 for (int i = 0; i < path->length; i++) {
287 if (i > 0) printf(" -> ");
288 printf("(%d,%d)", path->nodes[i].x, path->nodes[i].y);
289 }
290 printf("\n");
291 } else {
292 printf("No path found!\n");
293 }
294
295 /* Try a second path that must cross the river via the bridge */
296 printf("\nFinding path from (2,5) to (2,15) (must use bridge)...\n");
297 ASTAR_PATH* path2 = n_astar_find_path(grid, 2, 5, 0, 2, 15, 0,
300 if (path2) {
301 printf("Path found! Length: %d nodes, Cost: %d\n", path2->length, path2->cost);
302 printf("Path: ");
303 for (int i = 0; i < path2->length; i++) {
304 if (i > 0) printf(" -> ");
305 printf("(%d,%d)", path2->nodes[i].x, path2->nodes[i].y);
306 }
307 printf("\n");
308 n_astar_path_free(path2);
309 } else {
310 printf("No path found!\n");
311 }
312
313 /* Try an impossible path */
314 printf("\nFinding path from (0,0) to blocked cell (7,4)...\n");
315 ASTAR_PATH* path3 = n_astar_find_path(grid, 0, 0, 0, 7, 4, 0,
318 printf("Result: %s\n", path3 ? "Found (unexpected!)" : "No path (correct - cell is blocked)");
319 if (path3) n_astar_path_free(path3);
320
321 n_astar_grid_free(grid);
322 return path;
323}
324
325/* Demo 3: Dead reckoning simulation along the A* path */
326static void demo_dead_reckoning(const ISO_MAP* map, const ASTAR_PATH* path) {
327 printf("\n=== DEMO 3: Dead Reckoning Along A* Path ===\n\n");
328
329 if (!path || path->length < 2) {
330 printf("No valid path for dead reckoning demo\n");
331 return;
332 }
333
334 /* Create trajectory from A* path waypoints */
336 if (!traj) {
337 printf("Failed to create trajectory\n");
338 return;
339 }
340
341 double move_speed = 3.0; /* tiles per second */
342 double t = 0.0;
343 for (int i = 0; i < path->length; i++) {
344 PHYSICS state;
345 memset(&state, 0, sizeof(state));
346 state.position[0] = (double)path->nodes[i].x;
347 state.position[1] = (double)path->nodes[i].y;
348
349 /* Compute velocity as direction to next waypoint */
350 if (i < path->length - 1) {
351 double dx = (double)(path->nodes[i + 1].x - path->nodes[i].x);
352 double dy = (double)(path->nodes[i + 1].y - path->nodes[i].y);
353 double dist = sqrt(dx * dx + dy * dy);
354 if (dist > 0.001) {
355 state.speed[0] = dx / dist * move_speed;
356 state.speed[1] = dy / dist * move_speed;
357 }
358 }
359
360 trajectory_add_point(traj, &state, t);
361
362 /* Time to next waypoint */
363 if (i < path->length - 1) {
364 double dx = (double)(path->nodes[i + 1].x - path->nodes[i].x);
365 double dy = (double)(path->nodes[i + 1].y - path->nodes[i].y);
366 double dist = sqrt(dx * dx + dy * dy);
367 t += dist / move_speed;
368 }
369 }
370
371 double total_time = t;
372 printf("Trajectory total time: %.2f seconds (%d waypoints)\n\n", total_time, path->length);
373
374 /* Create dead reckoning entity with PVB blending */
376 if (!dr) {
377 printf("Failed to create DR entity\n");
378 trajectory_delete(&traj);
379 return;
380 }
381
382 /* Simulation: "true" entity follows trajectory exactly.
383 * Network sends updates every 0.5 seconds.
384 * DR entity receives updates and extrapolates/blends between them. */
385 double sim_dt = 0.05; /* simulation timestep (50ms) */
386 double send_interval = 0.5; /* network update interval */
387 double last_send = -send_interval; /* force initial send */
388
389 printf("Time | True Pos | DR Pos | Error | Height | Updates\n");
390 printf("------+---------------+---------------+-------+--------+--------\n");
391
392 for (double sim_t = 0.0; sim_t <= total_time + 0.1; sim_t += sim_dt) {
393 /* Compute "true" position from trajectory */
394 VECTOR3D true_pos;
395 trajectory_get_position(traj, sim_t, true_pos);
396
397 /* Compute true velocity */
398 VECTOR3D true_vel;
399 trajectory_get_speed(traj, sim_t, true_vel);
400
401 /* Simulate periodic network updates */
402 if (sim_t - last_send >= send_interval) {
403 DR_VEC3 pos = dr_vec3(true_pos[0], true_pos[1], 0.0);
404 DR_VEC3 vel = dr_vec3(true_vel[0], true_vel[1], 0.0);
405 DR_VEC3 acc = dr_vec3_zero();
406 dr_entity_receive_state(dr, &pos, &vel, &acc, sim_t);
407 last_send = sim_t;
408 }
409
410 /* Compute DR display position */
411 DR_VEC3 dr_pos;
412 dr_entity_compute(dr, sim_t, &dr_pos);
413
414 /* Compute error */
415 double err = sqrt((dr_pos.x - true_pos[0]) * (dr_pos.x - true_pos[0]) +
416 (dr_pos.y - true_pos[1]) * (dr_pos.y - true_pos[1]));
417
418 /* Get terrain height at DR position */
419 float h = iso_map_interpolate_height(map, (float)dr_pos.x, (float)dr_pos.y);
420
421 /* Print at regular intervals */
422 double print_interval = 0.5;
423 double mod = fmod(sim_t, print_interval);
424 if (mod < sim_dt || sim_t < sim_dt) {
425 printf("%5.2f | (%5.2f,%5.2f) | (%5.2f,%5.2f) | %5.3f | %6.2f | %d\n",
426 sim_t,
427 true_pos[0], true_pos[1],
428 dr_pos.x, dr_pos.y,
429 err, h, dr->update_count);
430 }
431 }
432
433 /* Show blending mode comparison */
434 printf("\n--- Convergence Mode Comparison ---\n");
435 const char* mode_names[] = {"Snap", "PVB", "Cubic"};
437
438 for (int m = 0; m < 3; m++) {
439 DR_ENTITY* test_dr = dr_entity_create(DR_ALGO_VEL_ACC, modes[m], 0.5, 0.3);
440 double total_err = 0.0;
441 int samples = 0;
442 last_send = -send_interval;
443
444 for (double sim_t = 0.0; sim_t <= total_time; sim_t += sim_dt) {
445 VECTOR3D tp, tv;
446 trajectory_get_position(traj, sim_t, tp);
447 trajectory_get_speed(traj, sim_t, tv);
448
449 if (sim_t - last_send >= send_interval) {
450 DR_VEC3 p = dr_vec3(tp[0], tp[1], 0.0);
451 DR_VEC3 v = dr_vec3(tv[0], tv[1], 0.0);
452 DR_VEC3 a = dr_vec3_zero();
453 dr_entity_receive_state(test_dr, &p, &v, &a, sim_t);
454 last_send = sim_t;
455 }
456
457 DR_VEC3 dp;
458 dr_entity_compute(test_dr, sim_t, &dp);
459
460 double err = sqrt((dp.x - tp[0]) * (dp.x - tp[0]) +
461 (dp.y - tp[1]) * (dp.y - tp[1]));
462 total_err += err;
463 samples++;
464 }
465
466 printf(" %-6s: avg error = %.4f over %d samples\n",
467 mode_names[m], total_err / (double)samples, samples);
468 dr_entity_destroy(&test_dr);
469 }
470
471 /* Threshold check demo */
472 printf("\n--- Threshold Check Demo ---\n");
474 DR_VEC3 init_pos = dr_vec3(path->nodes[0].x, path->nodes[0].y, 0.0);
475 dr_entity_set_position(owner_dr, &init_pos, NULL, NULL, 0.0);
476
477 int updates_sent = 0;
478 for (double sim_t = 0.0; sim_t <= total_time; sim_t += sim_dt) {
479 VECTOR3D tp, tv;
480 trajectory_get_position(traj, sim_t, tp);
481 trajectory_get_speed(traj, sim_t, tv);
482 DR_VEC3 true_p = dr_vec3(tp[0], tp[1], 0.0);
483 DR_VEC3 true_v = dr_vec3(tv[0], tv[1], 0.0);
484 DR_VEC3 true_a = dr_vec3_zero();
485
486 if (dr_entity_check_threshold(owner_dr, &true_p, &true_v, &true_a, sim_t)) {
487 dr_entity_receive_state(owner_dr, &true_p, &true_v, &true_a, sim_t);
488 updates_sent++;
489 }
490 }
491 printf(" With threshold=1.0 tile: sent %d updates over %.1f seconds\n",
492 updates_sent, total_time);
493 printf(" (vs %d with fixed 0.5s interval)\n", (int)(total_time / send_interval) + 1);
494 dr_entity_destroy(&owner_dr);
495
497 trajectory_delete(&traj);
498}
499
500/* Demo 4: A* pathfinding standalone test */
501static void demo_astar_standalone(void) {
502 printf("\n=== DEMO 4: A* Pathfinding Standalone Tests ===\n\n");
503
504 /* Create a 10x10 grid with some walls */
505 ASTAR_GRID* grid = n_astar_grid_new(10, 10, 1);
506 if (!grid) {
507 printf("Failed to create grid\n");
508 return;
509 }
510
511 /* Build a maze-like layout */
512 n_astar_grid_set_rect_blocked(grid, 2, 0, 0, 2, 5, 0);
513 n_astar_grid_set_rect_blocked(grid, 4, 3, 0, 4, 9, 0);
514 n_astar_grid_set_rect_blocked(grid, 6, 0, 0, 6, 6, 0);
515 n_astar_grid_set_rect_blocked(grid, 8, 3, 0, 8, 9, 0);
516
517 /* Print the grid */
518 printf("10x10 maze grid (. = open, # = wall):\n ");
519 for (int x = 0; x < 10; x++) printf("%d", x);
520 printf("\n");
521 for (int y = 0; y < 10; y++) {
522 printf("%d ", y);
523 for (int x = 0; x < 10; x++) {
524 printf("%c", n_astar_grid_get_walkable(grid, x, y, 0) ? '.' : '#');
525 }
526 printf("\n");
527 }
528
529 /* Test different heuristics */
530 const char* heur_names[] = {"Manhattan", "Euclidean", "Chebyshev"};
531 ASTAR_HEURISTIC heurs[] = {
535
536 printf("\nPaths from (0,0) to (9,9) with different heuristics:\n");
537 for (int h = 0; h < 3; h++) {
538 ASTAR_PATH* p = n_astar_find_path(grid, 0, 0, 0, 9, 9, 0,
539 ASTAR_ALLOW_DIAGONAL, heurs[h]);
540 if (p) {
541 printf(" %-10s: length=%2d cost=%5d ",
542 heur_names[h], p->length, p->cost);
543 for (int i = 0; i < p->length && i < 15; i++) {
544 if (i > 0) printf("->");
545 printf("(%d,%d)", p->nodes[i].x, p->nodes[i].y);
546 }
547 if (p->length > 15) printf("...");
548 printf("\n");
550 } else {
551 printf(" %-10s: no path\n", heur_names[h]);
552 }
553 }
554
555 /* Cardinal-only vs diagonal */
556 printf("\nCardinal-only vs diagonal movement:\n");
557 ASTAR_PATH* p_card = n_astar_find_path(grid, 0, 0, 0, 9, 9, 0,
560 ASTAR_PATH* p_diag = n_astar_find_path(grid, 0, 0, 0, 9, 9, 0,
563 if (p_card) printf(" Cardinal: length=%d cost=%d\n", p_card->length, p_card->cost);
564 if (p_diag) printf(" Diagonal: length=%d cost=%d\n", p_diag->length, p_diag->cost);
565 if (p_card) n_astar_path_free(p_card);
566 if (p_diag) n_astar_path_free(p_diag);
567
568 n_astar_grid_free(grid);
569}
570
571/* Main */
572int main(int argc, char* argv[]) {
573 (void)argc;
574 (void)argv;
575
577
578 printf("Nilorea Library: Isometric + A* + Dead Reckoning Demo\n");
579 printf("======================================================\n");
580
581 /* Create the isometric map */
583 if (!map) {
584 fprintf(stderr, "Failed to create map\n");
585 return EXIT_FAILURE;
586 }
587
588 /* Set projection */
590
591 /* Build the test map */
592 build_test_map(map);
593
594 /* Show ASCII map overview */
595 printf("\nMap overview (%dx%d, %d terrains, max height %d):\n",
597 print_map_ascii(map, NULL);
598
599 /* Demo 1: Isometric engine */
600 demo_iso_engine(map);
601
602 /* Demo 2: A* pathfinding on the map */
603 ASTAR_PATH* path = demo_astar(map);
604
605 /* Show map with path overlaid */
606 if (path) {
607 printf("\nMap with A* path overlaid:\n");
608 print_map_ascii(map, path);
609 }
610
611 /* Demo 3: Dead reckoning along the path */
612 demo_dead_reckoning(map, path);
613
614 /* Demo 4: A* standalone tests */
616
617 /* Cleanup */
618 if (path) n_astar_path_free(path);
619 iso_map_free(&map);
620
621 printf("\n=== All demos complete ===\n");
622 return EXIT_SUCCESS;
623}
int main(void)
#define MAP_H
#define MAP_W
#define MAX_HEIGHT
static const char * terrain_names[8]
#define NUM_TERRAINS
static ASTAR_PATH * demo_astar(const ISO_MAP *map)
static void demo_astar_standalone(void)
static void demo_iso_engine(ISO_MAP *map)
#define TILE_WIDTH
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 x
grid X coordinate
Definition n_astar.h:110
int cost
total path cost (x1000 fixed-point)
Definition n_astar.h:119
ASTAR_NODE * nodes
array of path nodes from start to goal
Definition n_astar.h:117
int y
grid Y coordinate
Definition n_astar.h:111
int length
number of nodes in the path
Definition n_astar.h:118
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.
Definition n_astar.c:477
void n_astar_grid_set_cost(ASTAR_GRID *grid, int x, int y, int z, int cost)
Set a cell's movement cost multiplier.
Definition n_astar.c:296
uint8_t n_astar_grid_get_walkable(const ASTAR_GRID *grid, int x, int y, int z)
Get a cell's walkability.
Definition n_astar.c:283
ASTAR_HEURISTIC
Heuristic function selection for h(n) estimation.
Definition n_astar.h:98
#define ASTAR_ALLOW_DIAGONAL
Movement mode: 8-dir (2D) or 26-dir (3D)
Definition n_astar.h:77
void n_astar_grid_free(ASTAR_GRID *grid)
Free a grid and all its internal data.
Definition n_astar.c:255
#define ASTAR_COST_CARDINAL
Default cost for straight movement (fixed-point x1000)
Definition n_astar.h:80
#define ASTAR_CARDINAL_ONLY
Movement mode: 4-dir (2D) or 6-dir (3D)
Definition n_astar.h:75
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)
Definition n_astar.c:324
void n_astar_path_free(ASTAR_PATH *path)
Free a path returned by n_astar_find_path.
Definition n_astar.c:707
void n_astar_grid_set_walkable(ASTAR_GRID *grid, int x, int y, int z, uint8_t walkable)
Set a cell's walkability.
Definition n_astar.c:270
ASTAR_GRID * n_astar_grid_new(int width, int height, int depth)
Create a new grid for A* pathfinding.
Definition n_astar.c:219
@ ASTAR_HEURISTIC_EUCLIDEAN
straight-line distance
Definition n_astar.h:100
@ ASTAR_HEURISTIC_CHEBYSHEV
max of axis deltas (optimal for 8-dir)
Definition n_astar.h:101
@ ASTAR_HEURISTIC_MANHATTAN
sum of axis deltas (optimal for 4-dir)
Definition n_astar.h:99
Grid structure holding walkability, costs, and dimensions.
Definition n_astar.h:147
The computed path result.
Definition n_astar.h:116
double x
X component.
double y
Y component.
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 )
Definition n_log.c:120
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:79
VECTOR3D speed
vx,vy,vz actual speed
Definition n_3d.h:75
VECTOR3D position
x,y,z actual position
Definition n_3d.h:73
double VECTOR3D[3]
struct of a point
Definition n_3d.h:58
structure of the physics of an object
Definition n_3d.h:69
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...
Generic log system.
Trajectory interpolation and dead reckoning for 2D/3D networked simulations.