Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_gui_isometric.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
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49#include <math.h>
50#include <time.h>
51#include <stdint.h>
52
53/* Allegro5 */
54#include <allegro5/allegro.h>
55#include <allegro5/allegro_image.h>
56#include <allegro5/allegro_primitives.h>
57#include <allegro5/allegro_font.h>
58#include <allegro5/allegro_ttf.h>
59
60/* nilorea-library modules */
61#include "nilorea/n_common.h"
62#include "nilorea/n_log.h"
63#include "nilorea/n_list.h"
64#include "nilorea/n_str.h"
65#include "nilorea/n_astar.h"
68#include "nilorea/n_gui.h"
69
70/* Constants */
71#define SCREEN_W 1280
72#define SCREEN_H 720
73#define FPS 60.0
74
75/* Tile dimensions (isometric diamond) */
76#define TILE_W 66
77#define TILE_H 34
78
79/* Map size */
80#define MAP_W 20
81#define MAP_H 20
82
83/* Number of terrain types */
84#define NUM_TERRAINS 8
85
86/* Height / elevation settings */
87#define MAX_HEIGHT 5
88#define TILE_LIFT 16
89
90/* Player movement settings */
91#define PLAYER_MOVE_SPEED 4.0f
92#define JUMP_VELOCITY 9.0f /* initial upward velocity (height units/sec) */
93#define JUMP_GRAVITY 20.0f /* gravity for jump (height units/sec^2) */
94#define CAM_SMOOTHING 8.0f /* camera lerp speed (higher = snappier) */
95#define PLAYER_MAX_HEIGH_DIFF 1 /* max height diff player can walk over */
96
97/* Terrain type IDs, ordered by visual precedence (higher = drawn on top) */
98#define TERRAIN_EAU 0 /* water - lowest precedence, base terrain */
99#define TERRAIN_LAVE 1 /* lava */
100#define TERRAIN_SABLE 2 /* sand */
101#define TERRAIN_CHEMIN 3 /* road */
102#define TERRAIN_TEMPLE3 4 /* temple floor light */
103#define TERRAIN_TEMPLE4 5 /* temple floor dark */
104#define TERRAIN_PAILLE 6 /* straw */
105#define TERRAIN_ROCAILLE 7 /* rock - highest precedence */
106
107/* Terrain names for HUD */
108static const char* terrain_names[NUM_TERRAINS] = {
109 "Water", "Lava", "Sand", "Road", "Temple Light", "Temple Dark", "Straw", "Rock"};
110
111/* Terrain file names */
112static const char* terrain_files[NUM_TERRAINS] = {
113 "DATAS/tiles/eau_centre.bmp",
114 "DATAS/tiles/lave.bmp",
115 "DATAS/tiles/sable_centre.bmp",
116 "DATAS/tiles/chemin_centre.bmp",
117 "DATAS/tiles/temple3.bmp",
118 "DATAS/tiles/temple4.bmp",
119 "DATAS/tiles/paille.bmp",
120 "DATAS/tiles/rocaille.bmp"};
121
122/* Projection presets, use library defines (ISO_PROJ_CLASSIC, etc.) */
123#define NUM_PROJECTIONS ISO_NUM_PROJECTIONS
124
125/* Transition mask count alias */
126#define NUM_MASKS ISO_NUM_MASKS
127
128/* State */
130
131static ISO_MAP* isomap = NULL;
132static N_ISO_CAMERA* camera = NULL;
133
134static ALLEGRO_BITMAP* tile_bitmaps[NUM_TERRAINS] = {NULL};
135static ALLEGRO_BITMAP* transition_masks[NUM_MASKS] = {NULL};
136static ALLEGRO_BITMAP* transition_tiles[NUM_TERRAINS][NUM_MASKS] = {{NULL}};
137
138/* Dynamic screen dimensions */
139static int screen_w = SCREEN_W;
140static int screen_h = SCREEN_H;
141
142/* UI state */
144static int paint_height = 0;
145static bool height_mode = false;
146static bool show_grid = false;
147static bool running = true;
148static bool redraw_needed = true;
149static bool smooth_height = false;
150static int smooth_slope_max = 1;
151
152/* Player state: free movement with continuous float position */
153static bool player_mode = false; /* true = player control, false = editor */
154static float player_fx = 10.5f; /* continuous map X (tile center = int + 0.5) */
155static float player_fy = 10.5f; /* continuous map Y */
156static int player_mx = 10; /* current tile X (derived) */
157static int player_my = 10; /* current tile Y (derived) */
158static float player_screen_x = 0.0f; /* derived screen position */
159static float player_screen_y = 0.0f;
160static float player_z = 0.0f; /* absolute height position (height units) */
161static float player_vz = 0.0f; /* vertical velocity (height units/sec) */
162static bool player_on_ground = true; /* true when touching the ground */
163
164/* Dead Reckoning ghost: simulated remote player on circular path */
165static bool ghost_enabled = false;
166static DR_ENTITY* ghost_dr = NULL;
167static float ghost_screen_x = 0.0f;
168static float ghost_screen_y = 0.0f;
169static float ghost_true_x = 0.0f;
170static float ghost_true_y = 0.0f;
171static float ghost_path_cx = 10.0f;
172static float ghost_path_cy = 10.0f;
173static float ghost_path_radius = 6.0f;
174static float ghost_path_speed = 0.4f;
175static float ghost_path_angle = 0.0f;
176static double ghost_last_send = 0.0;
177static double ghost_send_interval = 0.25;
178
179/* Mouse hover tile */
180static int hover_mx = -1;
181static int hover_my = -1;
182static int last_mouse_x = 0;
183static int last_mouse_y = 0;
184
185/* GUI context and widget IDs */
186static N_GUI_CTX* gui_ctx = NULL;
187static int gui_win_modes = -1;
188static int gui_win_tiles = -1;
189static int gui_win_proj = -1;
190static int gui_win_info = -1;
191static int gui_btn_player = -1;
192static int gui_btn_height = -1;
193static int gui_btn_grid = -1;
194static int gui_btn_smooth = -1;
195static int gui_btn_ghost = -1;
196static int gui_btn_reset = -1;
197static int gui_lbl_height = -1;
198static int gui_sld_height = -1;
199static int gui_lst_tiles = -1;
200static int gui_lbl_slope = -1;
201static int gui_sld_slope = -1;
202static int gui_btn_proj[NUM_PROJECTIONS] = {-1, -1, -1, -1};
203static int gui_lbl_status = -1;
204static int gui_lbl_proj = -1;
205static int gui_lbl_hover = -1;
206
207/* Ghost dead reckoning GUI */
208static int gui_win_ghost_dr = -1;
209static int gui_lbl_dr_algo = -1;
210static int gui_rad_dr_algo = -1;
211static int gui_lbl_dr_blend = -1;
212static int gui_rad_dr_blend = -1;
213static int gui_lbl_dr_interval = -1;
214static int gui_sld_dr_interval = -1;
215static int gui_lbl_dr_blend_time = -1;
216static int gui_sld_dr_blend_time = -1;
217static int gui_lbl_dr_threshold = -1;
218static int gui_sld_dr_threshold = -1;
219
220/* A* pathfinding for click-to-move */
221static ASTAR_PATH* player_path = NULL;
222static int player_path_idx = 0;
223static float player_path_progress = 0.0f;
224/* Exact fractional click destination within the final tile */
225static float player_click_fx = 0.0f;
226static float player_click_fy = 0.0f;
227
228#define MAP_FILE "iso_gui_map.isom"
229
230/* Diamond helpers, coordinate conversion, projection interpolation,
231 * transition mask/tile generation, and diamond masking are all provided
232 * by the nilorea-library n_iso_engine API. */
233
234/* Bilinear height interpolation with cliff-clamping */
235static float interpolate_height_at(float fx, float fy) {
236 if (fx < 0.0f) fx = 0.0f;
237 if (fy < 0.0f) fy = 0.0f;
238 if (fx > (float)MAP_W - 1e-4f) fx = (float)MAP_W - 1e-4f;
239 if (fy > (float)MAP_H - 1e-4f) fy = (float)MAP_H - 1e-4f;
240 return iso_map_interpolate_height(isomap, fx, fy);
241}
242
243/* Collision-aware height interpolation: clamp neighbor tile heights so the
244 * bilinear blend does not pull the player upward into wall tiles.
245 * The standard interpolation samples (ix,iy), (ix+1,iy), (ix,iy+1),
246 * (ix+1,iy+1). When one of those neighbors is a tall wall, the smooth
247 * blend makes the player "climb" the wall before the tile-boundary
248 * collision check fires. Clamping each neighbor to at most
249 * base_height + max_climb prevents that. */
250static float interpolate_height_at_clamped(float fx, float fy, int max_climb) {
251 if (fx < 0.0f) fx = 0.0f;
252 if (fy < 0.0f) fy = 0.0f;
253 if (fx > (float)MAP_W - 1e-4f) fx = (float)MAP_W - 1e-4f;
254 if (fy > (float)MAP_H - 1e-4f) fy = (float)MAP_H - 1e-4f;
255
256 int ix = (int)floorf(fx);
257 int iy = (int)floorf(fy);
258
259 float h00 = (float)iso_map_get_height(isomap, ix, iy);
260
261 /* In CUT mode, return flat tile height, no interpolation */
262 if (!smooth_height) {
263 return h00;
264 }
265
266 float frac_x = fx - (float)ix;
267 float frac_y = fy - (float)iy;
268
269 /* Clamp neighbor indices to valid range for edge tiles */
270 int ix1 = (ix + 1 < MAP_W) ? ix + 1 : ix;
271 int iy1 = (iy + 1 < MAP_H) ? iy + 1 : iy;
272
273 float h10 = (float)iso_map_get_height(isomap, ix1, iy);
274 float h01 = (float)iso_map_get_height(isomap, ix, iy1);
275 float h11 = (float)iso_map_get_height(isomap, ix1, iy1);
276
277 float max_h = h00 + (float)max_climb;
278 if (h10 > max_h) h10 = max_h;
279 if (h01 > max_h) h01 = max_h;
280 if (h11 > max_h) h11 = max_h;
281
282 float top = h00 + (h10 - h00) * frac_x;
283 float bot = h01 + (h11 - h01) * frac_x;
284 return top + (bot - top) * frac_y;
285}
286
287/* Object draw callbacks for N_ISO_OBJECT depth-sorted rendering */
288
289/* Player draw callback: green circle with shadow */
290static void draw_player_object(float sx, float sy, float zoom, float alpha, void* user_data) {
291 (void)user_data;
292 al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA);
293 float cy = sy - 8.0f * zoom; /* offset up from feet */
294
295 /* Shadow at ground level (only for normal draw, not overlay) */
296 if (alpha >= 1.0f) {
298 float gs_sx, gs_sy;
299 iso_map_to_screen_f(isomap, player_fx, player_fy, ground_h, &gs_sx, &gs_sy);
300 float shadow_cx, shadow_cy;
301 n_iso_camera_world_to_screen(camera, gs_sx, gs_sy, &shadow_cx, &shadow_cy);
302 al_draw_filled_ellipse(sx, shadow_cy, 4.0f * zoom, 2.0f * zoom,
303 al_map_rgba(0, 0, 0, 60));
304 }
305
306 al_draw_filled_circle(sx, cy, 5.0f * zoom,
307 al_map_rgba(50, 200, 50, (unsigned char)(220.0f * alpha)));
308 al_draw_circle(sx, cy, 5.0f * zoom,
309 al_map_rgba(255, 255, 255, (unsigned char)(200.0f * alpha)), 1.5f);
310}
311
312/* Ghost draw callback: blue circle (DR position) + red outline (true position) */
313static void draw_ghost_object(float sx, float sy, float zoom, float alpha, void* user_data) {
314 (void)user_data;
315 al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA);
316 float cy = sy - 8.0f * zoom;
317
318 /* DR-computed position (blue) */
319 al_draw_filled_circle(sx, cy, 5.0f * zoom,
320 al_map_rgba(80, 120, 255, (unsigned char)(220.0f * alpha)));
321 al_draw_circle(sx, cy, 5.0f * zoom,
322 al_map_rgba(200, 200, 255, (unsigned char)(200.0f * alpha)), 1.5f);
323 if (alpha >= 1.0f) {
324 al_draw_filled_ellipse(sx, cy + 8.0f * zoom, 4.0f * zoom, 2.0f * zoom,
325 al_map_rgba(0, 0, 0, 60));
326
327 /* True position: red outline (only in normal draw) */
328 float tcx, tcy;
330 tcy -= 8.0f * zoom;
331 al_draw_circle(tcx, tcy, 5.0f * zoom, al_map_rgba(255, 80, 80, 180), 1.5f);
332 al_draw_filled_ellipse(tcx, tcy + 8.0f * zoom, 4.0f * zoom, 2.0f * zoom,
333 al_map_rgba(255, 0, 0, 30));
334 }
335}
336
337/* Randomize the map */
338static void randomize_map(void) {
339 srand((unsigned)time(NULL));
340
341 for (int y = 0; y < MAP_H; y++)
342 for (int x = 0; x < MAP_W; x++) {
344 iso_map_set_height(isomap, x, y, 0);
345 }
346
347 for (int patch = 0; patch < 15; patch++) {
348 int t = rand() % NUM_TERRAINS;
349 int cx = rand() % MAP_W;
350 int cy = rand() % MAP_H;
351 int radius = 2 + rand() % 4;
352 for (int dy = -radius; dy <= radius; dy++)
353 for (int dx = -radius; dx <= radius; dx++) {
354 int x = cx + dx, y = cy + dy;
355 if (x < 0 || x >= MAP_W || y < 0 || y >= MAP_H) continue;
356 float dist = sqrtf((float)(dx * dx + dy * dy));
357 if (dist <= radius && (rand() % 100) < (int)(100.0f * (1.0f - dist / (float)(radius + 1))))
358 iso_map_set_terrain(isomap, x, y, t);
359 }
360 }
361
362 for (int hill = 0; hill < 8; hill++) {
363 int peak_h = 1 + rand() % MAX_HEIGHT;
364 int cx = rand() % MAP_W;
365 int cy = rand() % MAP_H;
366 int radius = 2 + rand() % 3;
367 for (int dy = -radius; dy <= radius; dy++)
368 for (int dx = -radius; dx <= radius; dx++) {
369 int x = cx + dx, y = cy + dy;
370 if (x < 0 || x >= MAP_W || y < 0 || y >= MAP_H) continue;
371 float dist = sqrtf((float)(dx * dx + dy * dy));
372 if (dist > radius) continue;
373 int h = (int)((float)peak_h * (1.0f - dist / (float)(radius + 1)) + 0.5f);
374 if (h > iso_map_get_height(isomap, x, y))
375 iso_map_set_height(isomap, x, y, h);
376 }
377 }
378}
379
380/* Button callbacks for n_gui_button_set_keycode bindings */
381
382/* Toggle player mode (P key) */
383static void on_player_toggle(int id, void* data) {
384 (void)id;
385 (void)data;
387 if (player_mode) {
388 if (height_mode) {
389 height_mode = false;
391 }
392 int drop_max_h = 0;
393 for (int y = 0; y < MAP_H; y++)
394 for (int x = 0; x < MAP_W; x++) {
395 int h = iso_map_get_height(isomap, x, y);
396 if (h > drop_max_h) drop_max_h = h;
397 }
398 player_z = (float)(drop_max_h + 1);
399 player_vz = 0.0f;
400 player_on_ground = false;
405 }
406}
407
408/* Toggle height editing mode (H key) */
409static void on_height_toggle(int id, void* data) {
410 (void)id;
411 (void)data;
413 if (height_mode && player_mode) {
414 player_mode = false;
416 }
417}
418
419/* Toggle grid overlay (G key) */
420static void on_grid_toggle(int id, void* data) {
421 (void)id;
422 (void)data;
424}
425
426/* Toggle smooth height (V key) */
427static void on_smooth_toggle(int id, void* data) {
428 (void)id;
429 (void)data;
431}
432
433/* Toggle ghost dead reckoning display (N key) */
434static void on_ghost_toggle(int id, void* data) {
435 (void)id;
436 (void)data;
438 if (ghost_enabled) {
440 if (ghost_dr) {
441 ghost_last_send = al_get_time();
442 ghost_path_angle = 0.0f;
443 int ghost_max_h = 0;
444 for (int y = 0; y < MAP_H; y++)
445 for (int x = 0; x < MAP_W; x++) {
446 int h = iso_map_get_height(isomap, x, y);
447 if (h > ghost_max_h) ghost_max_h = h;
448 }
450 ghost_path_cy, (double)(ghost_max_h + 1));
451 DR_VEC3 init_vel = dr_vec3(0.0, ghost_path_radius * ghost_path_speed, 0.0);
452 dr_entity_set_position(ghost_dr, &init_pos, &init_vel, NULL,
453 al_get_time());
454 }
455 } else {
457 }
458}
459
460/* reset terrain (R key) */
461static void on_reset_click(int id, void* data) {
462 (void)id;
463 (void)data;
466}
467
468/* Select projection (F1-F4 keys) */
469static void on_proj_select(int id, void* data) {
470 (void)data;
471 for (int p = 0; p < NUM_PROJECTIONS; p++) {
472 if (gui_btn_proj[p] == id) {
473 current_proj = p;
475 }
477 }
478}
479
480/* Main */
481int main(int argc, char* argv[]) {
482 (void)argc;
483 (void)argv;
484
486
487 if (!al_init()) {
488 n_abort("Could not init Allegro.\n");
489 }
490 if (!al_init_image_addon()) {
491 n_abort("image addon\n");
492 }
493 if (!al_init_primitives_addon()) {
494 n_abort("primitives addon\n");
495 }
496 if (!al_init_font_addon()) {
497 n_abort("font addon\n");
498 }
499 al_init_ttf_addon();
500 al_install_keyboard();
501 al_install_mouse();
502
503 al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_WINDOWED | ALLEGRO_RESIZABLE);
504 ALLEGRO_DISPLAY* display = al_create_display(SCREEN_W, SCREEN_H);
505 if (!display) {
506 n_abort("Unable to create display\n");
507 }
508 al_set_window_title(display, "Nilorea Isometric Engine + GUI Demo");
509
510 ALLEGRO_FONT* font = al_create_builtin_font();
511 if (!font) {
512 n_abort("Unable to create builtin font\n");
513 }
514
515 /* Create ISO_MAP with projection */
517 if (!isomap) {
518 n_abort("Failed to create ISO_MAP!\n");
519 return -1;
520 }
522 isomap->proj.tile_lift = (float)TILE_LIFT;
523
524 /* Create camera */
525 camera = n_iso_camera_new(0.5f, 6.0f);
526 if (!camera) {
527 n_abort("Failed to create camera!\n");
528 }
529 camera->zoom = 2.0f;
530
531 /* Initialize GUI */
532 gui_ctx = n_gui_new_ctx(font);
534 // n_gui_set_virtual_size(gui_ctx, (float)SCREEN_W, (float)SCREEN_H);
535
536 /* Modes window */
537 gui_win_modes = n_gui_add_window(gui_ctx, "Modes", 5, 5, 200, 310);
539 10, 10, 180, 24, N_GUI_SHAPE_ROUNDED, 0, on_player_toggle, NULL);
542 10, 40, 180, 24, N_GUI_SHAPE_ROUNDED, 0, on_height_toggle, NULL);
545 10, 70, 180, 24, N_GUI_SHAPE_ROUNDED, 0, on_grid_toggle, NULL);
548 10, 100, 180, 24, N_GUI_SHAPE_ROUNDED, 0, on_smooth_toggle, NULL);
551 10, 130, 180, 24, N_GUI_SHAPE_ROUNDED, 0, on_ghost_toggle, NULL);
554 10, 165, 180, 16, N_GUI_ALIGN_LEFT);
556 10, 185, 150, 20, 0.0, (double)MAX_HEIGHT, 0.0,
557 N_GUI_SLIDER_VALUE, NULL, NULL);
558 gui_btn_reset = n_gui_add_button(gui_ctx, gui_win_modes, "Reset terrain [R]",
559 10, 250, 180, 24, N_GUI_SHAPE_ROUNDED, on_reset_click, NULL);
563
564 /* Tiles window */
565 gui_win_tiles = n_gui_add_window(gui_ctx, "Tiles", 5, 325, 200, 200);
567 N_GUI_SELECT_SINGLE, NULL, NULL);
568 for (int t = 0; t < NUM_TERRAINS; t++)
572 10, 115, 180, 16, N_GUI_ALIGN_LEFT);
574 10, 135, 150, 20, 1.0, (double)MAX_HEIGHT, (double)smooth_slope_max,
575 N_GUI_SLIDER_VALUE, NULL, NULL);
576
577 /* Projection window */
578 gui_win_proj = n_gui_add_window(gui_ctx, "Projection", 5, 535, 200, 160);
580 10, 10, 180, 24, N_GUI_SHAPE_ROUNDED, 1, on_proj_select, NULL);
581 n_gui_button_set_keycode(gui_ctx, gui_btn_proj[0], ALLEGRO_KEY_F1, 0);
583 10, 40, 180, 24, N_GUI_SHAPE_ROUNDED, 0, on_proj_select, NULL);
584 n_gui_button_set_keycode(gui_ctx, gui_btn_proj[1], ALLEGRO_KEY_F2, 0);
586 10, 70, 180, 24, N_GUI_SHAPE_ROUNDED, 0, on_proj_select, NULL);
587 n_gui_button_set_keycode(gui_ctx, gui_btn_proj[2], ALLEGRO_KEY_F3, 0);
589 10, 100, 180, 24, N_GUI_SHAPE_ROUNDED, 0, on_proj_select, NULL);
590 n_gui_button_set_keycode(gui_ctx, gui_btn_proj[3], ALLEGRO_KEY_F4, 0);
591
592 /* Info window */
593 gui_win_info = n_gui_add_window(gui_ctx, "Info", 210, 5, 420, 90);
595 gui_lbl_proj = n_gui_add_label(gui_ctx, gui_win_info, "Projection: Classic 2:1", 10, 22, 400, 16, N_GUI_ALIGN_LEFT);
597
598 /* Ghost dead reckoning window */
599 gui_win_ghost_dr = n_gui_add_window(gui_ctx, "Ghost DR", 1070, 5, 200, 340);
601 10, 5, 180, 16, N_GUI_ALIGN_LEFT);
603 10, 23, 180, 66, NULL, NULL);
608
610 10, 93, 180, 16, N_GUI_ALIGN_LEFT);
612 10, 111, 180, 66, NULL, NULL);
617
619 10, 181, 180, 16, N_GUI_ALIGN_LEFT);
621 10, 199, 180, 20, 0.05, 2.0, ghost_send_interval,
622 N_GUI_SLIDER_VALUE, NULL, NULL);
623
625 10, 225, 180, 16, N_GUI_ALIGN_LEFT);
627 10, 243, 180, 20, 0.01, 2.0, 0.2,
628 N_GUI_SLIDER_VALUE, NULL, NULL);
629
631 10, 269, 180, 16, N_GUI_ALIGN_LEFT);
633 10, 287, 180, 20, 0.01, 5.0, 0.5,
634 N_GUI_SLIDER_VALUE, NULL, NULL);
635
636 /* Start hidden; shown when ghost is enabled */
638
639 /* Load tile images */
640 int saved_bmp_flags = al_get_new_bitmap_flags();
641 al_set_new_bitmap_flags(saved_bmp_flags & ~(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR));
642 for (int i = 0; i < NUM_TERRAINS; i++) {
643 tile_bitmaps[i] = al_load_bitmap(terrain_files[i]);
644 if (!tile_bitmaps[i]) {
645 n_log(LOG_ERR, "Failed to load tile: %s", terrain_files[i]);
646 return EXIT_FAILURE;
647 }
649 }
650
651 /* Generate transition data (nilorea-library) */
653 {
654 ALLEGRO_BITMAP** trans_ptrs[NUM_TERRAINS];
655 for (int t = 0; t < NUM_TERRAINS; t++)
656 trans_ptrs[t] = transition_tiles[t];
659 }
660 al_set_new_bitmap_flags(saved_bmp_flags);
661
662 /* Initialize map: try to load from file, else randomize */
663 {
664 ISO_MAP* loaded = iso_map_load(MAP_FILE);
665 if (loaded) {
666 for (int y = 0; y < MAP_H && y < loaded->height; y++)
667 for (int x = 0; x < MAP_W && x < loaded->width; x++) {
668 iso_map_set_terrain(isomap, x, y, iso_map_get_terrain(loaded, x, y));
669 iso_map_set_height(isomap, x, y, iso_map_get_height(loaded, x, y));
670 }
671 iso_map_free(&loaded);
672 } else {
675 }
676 }
677
678 /* Center camera */
679 {
680 float sx_center, sy_center;
681 iso_map_to_screen(isomap, MAP_W / 2, MAP_H / 2, 0, &sx_center, &sy_center);
682 n_iso_camera_center_on(camera, sx_center, sy_center, screen_w, screen_h);
683 }
684
685 /* Initialize dead reckoning ghost entity */
687 if (ghost_dr) {
689 dr_entity_set_position(ghost_dr, &init_pos, NULL, NULL, 0.0);
690 }
691
692 /* Event queue and timer */
693 ALLEGRO_TIMER* timer = al_create_timer(1.0 / FPS);
694 ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue();
695 al_register_event_source(queue, al_get_display_event_source(display));
696 al_register_event_source(queue, al_get_timer_event_source(timer));
697 al_register_event_source(queue, al_get_keyboard_event_source());
698 al_register_event_source(queue, al_get_mouse_event_source());
699
700 al_start_timer(timer);
701
702 bool key_up = false, key_down = false, key_left = false, key_right = false;
703 bool key_space = false;
704 float scroll_speed = 4.0f;
705 float dt = 1.0f / (float)FPS;
706
707 /* Initialize player position */
708 {
710 player_z = interp_h;
712 }
713
714 /* Main loop */
715 while (running) {
716 ALLEGRO_EVENT ev;
717 al_wait_for_event(queue, &ev);
718
719 /* pass events to the GUI.
720 * n_gui_process_event returns 1 when the event was consumed by GUI.
721 * n_gui_wants_mouse() is also checked below before game mouse actions. */
722 int gui_consumed = n_gui_process_event(gui_ctx, ev);
723 (void)gui_consumed;
724
725 switch (ev.type) {
726 case ALLEGRO_EVENT_TIMER: {
727 /* Smooth projection transition */
729
730 /* A* path following */
731 if (player_mode && player_path && player_path_idx < player_path->length) {
732 /* For the last node, target the exact click position;
733 * for intermediate nodes, target tile centers. */
734 float target_fx, target_fy;
735 int is_last_node = (player_path_idx == player_path->length - 1);
736 if (is_last_node) {
737 target_fx = player_click_fx;
738 target_fy = player_click_fy;
739 } else {
740 target_fx = (float)player_path->nodes[player_path_idx].x + 0.5f;
741 target_fy = (float)player_path->nodes[player_path_idx].y + 0.5f;
742 }
743 float path_dx = target_fx - player_fx;
744 float path_dy = target_fy - player_fy;
745 float path_dist = sqrtf(path_dx * path_dx + path_dy * path_dy);
746 float step = PLAYER_MOVE_SPEED * dt;
747 if (path_dist <= step) {
748 player_fx = target_fx;
749 player_fy = target_fy;
755 player_path = NULL;
756 }
757 } else {
758 player_fx += (path_dx / path_dist) * step;
759 player_fy += (path_dy / path_dist) * step;
760 player_mx = (int)floorf(player_fx);
761 player_my = (int)floorf(player_fy);
762 }
763 }
764
765 /* Player free movement update */
766 if (player_mode) {
767 float move_dx = 0.0f, move_dy = 0.0f;
768 if (key_up) move_dy -= 1.0f;
769 if (key_down) move_dy += 1.0f;
770 if (key_left) move_dx -= 1.0f;
771 if (key_right) move_dx += 1.0f;
772
773 if (move_dx != 0.0f && move_dy != 0.0f) {
774 move_dx *= 0.70710678f;
775 move_dy *= 0.70710678f;
776 }
777
778 /* Jump */
779 if (key_space && player_on_ground) {
781 player_on_ground = false;
782 }
783
785
786 /* Vertical physics */
787 if (!player_on_ground) {
788 player_vz -= JUMP_GRAVITY * dt;
789 player_z += player_vz * dt;
790 if (player_z <= ground_h) {
791 player_z = ground_h;
792 player_vz = 0.0f;
793 player_on_ground = true;
794 }
795 } else {
796 if (player_z - ground_h > 1.0f) {
797 player_on_ground = false;
798 player_vz = 0.0f;
799 } else {
800 player_z = ground_h;
801 }
802 }
803
804 /* Horizontal movement with collision */
805 if (move_dx != 0.0f || move_dy != 0.0f) {
806 float speed = PLAYER_MOVE_SPEED * dt;
807 float new_fx = player_fx + move_dx * speed;
808 float new_fy = player_fy + move_dy * speed;
809
810 if (new_fx < 0.1f) new_fx = 0.1f;
811 if (new_fx > (float)MAP_W - 0.1f) new_fx = (float)MAP_W - 0.1f;
812 if (new_fy < 0.1f) new_fy = 0.1f;
813 if (new_fy > (float)MAP_H - 0.1f) new_fy = (float)MAP_H - 0.1f;
814
815 int new_tile_x = (int)floorf(new_fx);
816 int new_tile_y = (int)floorf(new_fy);
817
818 bool can_move = true;
819 if (new_tile_x != player_mx || new_tile_y != player_my) {
820 if (new_tile_x >= 0 && new_tile_x < MAP_W &&
821 new_tile_y >= 0 && new_tile_y < MAP_H) {
822 int target_h = iso_map_get_height(isomap, new_tile_x, new_tile_y);
823 int current_h = iso_map_get_height(isomap, player_mx, player_my);
824 if (player_on_ground) {
825 if (target_h - current_h > PLAYER_MAX_HEIGH_DIFF)
826 can_move = false;
827 } else {
828 if ((float)target_h > player_z + 0.5f)
829 can_move = false;
830 }
831 } else {
832 can_move = false;
833 }
834 }
835
836 if (can_move) {
837 player_fx = new_fx;
838 player_fy = new_fy;
839 player_mx = new_tile_x;
840 player_my = new_tile_y;
841 } else {
842 /* Wall sliding: try each axis independently */
843 float slide_fx = player_fx + move_dx * speed;
844 if (slide_fx < 0.1f) slide_fx = 0.1f;
845 if (slide_fx > (float)MAP_W - 0.1f) slide_fx = (float)MAP_W - 0.1f;
846 int slide_tx = (int)floorf(slide_fx);
847 bool can_x = true;
848 if (slide_tx != player_mx) {
849 if (slide_tx >= 0 && slide_tx < MAP_W) {
850 int th = iso_map_get_height(isomap, slide_tx, player_my);
852 if (player_on_ground) {
853 if (th - ch > PLAYER_MAX_HEIGH_DIFF) can_x = false;
854 } else {
855 if ((float)th > player_z + 0.5f) can_x = false;
856 }
857 } else {
858 can_x = false;
859 }
860 }
861 if (can_x) {
862 player_fx = slide_fx;
863 player_mx = slide_tx;
864 }
865
866 float slide_fy = player_fy + move_dy * speed;
867 if (slide_fy < 0.1f) slide_fy = 0.1f;
868 if (slide_fy > (float)MAP_H - 0.1f) slide_fy = (float)MAP_H - 0.1f;
869 int slide_ty = (int)floorf(slide_fy);
870 bool can_y = true;
871 if (slide_ty != player_my) {
872 if (slide_ty >= 0 && slide_ty < MAP_H) {
873 int th = iso_map_get_height(isomap, player_mx, slide_ty);
875 if (player_on_ground) {
876 if (th - ch > PLAYER_MAX_HEIGH_DIFF) can_y = false;
877 } else {
878 if ((float)th > player_z + 0.5f) can_y = false;
879 }
880 } else {
881 can_y = false;
882 }
883 }
884 if (can_y) {
885 player_fy = slide_fy;
886 player_my = slide_ty;
887 }
888 }
889 }
890
891 /* Recompute ground height after movement */
893 if (player_on_ground) {
894 if (player_z - ground_h > 1.0f) {
895 player_on_ground = false;
896 player_vz = 0.0f;
897 } else {
898 player_z = ground_h;
899 }
900 } else if (player_z <= ground_h) {
901 player_z = ground_h;
902 player_vz = 0.0f;
903 player_on_ground = true;
904 }
905
906 /* Update screen position */
908 }
909
910 /* Camera: smoothly follow player in player mode */
911 if (player_mode) {
914 }
915
916 /* Editor mode: scroll camera */
917 if (!player_mode) {
918 float sdx = 0.0f, sdy = 0.0f;
919 if (key_left) sdx += scroll_speed / camera->zoom;
920 if (key_right) sdx -= scroll_speed / camera->zoom;
921 if (key_up) sdy += scroll_speed / camera->zoom;
922 if (key_down) sdy -= scroll_speed / camera->zoom;
923 if (sdx != 0.0f || sdy != 0.0f)
924 n_iso_camera_scroll(camera, sdx, sdy);
925 }
926
927 /* Dead reckoning ghost update */
928 if (ghost_enabled && ghost_dr) {
929 double now = al_get_time();
930
932 if (ghost_path_angle > 2.0f * (float)M_PI)
933 ghost_path_angle -= 2.0f * (float)M_PI;
934
935 float true_fx = ghost_path_cx + ghost_path_radius * cosf(ghost_path_angle);
936 float true_fy = ghost_path_cy + ghost_path_radius * sinf(ghost_path_angle);
937
938 float true_vx = -ghost_path_radius * ghost_path_speed * sinf(ghost_path_angle);
939 float true_vy = ghost_path_radius * ghost_path_speed * cosf(ghost_path_angle);
940
942 float true_ax = -w2r * cosf(ghost_path_angle);
943 float true_ay = -w2r * sinf(ghost_path_angle);
944
945 {
946 DR_VEC3 pos = dr_vec3(true_fx, true_fy, 0.0);
947 DR_VEC3 vel = dr_vec3(true_vx, true_vy, 0.0);
948 DR_VEC3 acc = dr_vec3(true_ax, true_ay, 0.0);
950 dr_entity_check_threshold(ghost_dr, &pos, &vel, &acc, now)) {
951 dr_entity_receive_state(ghost_dr, &pos, &vel, &acc, now);
952 ghost_last_send = now;
953 }
954 }
955
956 DR_VEC3 dr_pos;
957 dr_entity_compute(ghost_dr, now, &dr_pos);
958
959 float dr_h = interpolate_height_at((float)dr_pos.x, (float)dr_pos.y);
960 iso_map_to_screen_f(isomap, (float)dr_pos.x, (float)dr_pos.y, dr_h,
962
963 float true_h = interpolate_height_at(true_fx, true_fy);
964 iso_map_to_screen_f(isomap, true_fx, true_fy, true_h,
966 }
967
968 /* Update hover tile (skip when mouse is over a GUI window) */
970 float wx, wy;
972 (float)last_mouse_y, &wx, &wy);
974 &hover_mx, &hover_my);
975 } else {
976 hover_mx = -1;
977 hover_my = -1;
978 }
979
980 /* GUI <-> variable sync */
981 {
982 bool gui_player = n_gui_button_is_toggled(gui_ctx, gui_btn_player) != 0;
983 bool gui_height = n_gui_button_is_toggled(gui_ctx, gui_btn_height) != 0;
984 bool gui_grid = n_gui_button_is_toggled(gui_ctx, gui_btn_grid) != 0;
985 bool gui_smooth = n_gui_button_is_toggled(gui_ctx, gui_btn_smooth) != 0;
986 bool gui_ghost = n_gui_button_is_toggled(gui_ctx, gui_btn_ghost) != 0;
987
988 if (gui_player != player_mode) {
989 player_mode = gui_player;
990 if (player_mode) {
991 int drop_max_h = 0;
992 for (int y = 0; y < MAP_H; y++)
993 for (int x = 0; x < MAP_W; x++) {
994 int h = iso_map_get_height(isomap, x, y);
995 if (h > drop_max_h) drop_max_h = h;
996 }
997 player_z = (float)(drop_max_h + 1);
998 player_vz = 0.0f;
999 player_on_ground = false;
1004 }
1005 }
1006 if (gui_height != height_mode) {
1007 height_mode = gui_height;
1008 if (height_mode && player_mode)
1009 player_mode = false;
1010 }
1011 if (gui_grid != show_grid) show_grid = gui_grid;
1012 if (gui_smooth != smooth_height) smooth_height = gui_smooth;
1013 if (gui_ghost != ghost_enabled) {
1014 ghost_enabled = gui_ghost;
1015 if (ghost_enabled) {
1017 if (ghost_dr) {
1018 ghost_last_send = al_get_time();
1019 ghost_path_angle = 0.0f;
1020 int ghost_max_h = 0;
1021 for (int y = 0; y < MAP_H; y++)
1022 for (int x = 0; x < MAP_W; x++) {
1023 int h = iso_map_get_height(isomap, x, y);
1024 if (h > ghost_max_h) ghost_max_h = h;
1025 }
1027 ghost_path_cy, (double)(ghost_max_h + 1));
1028 DR_VEC3 init_vel = dr_vec3(0.0, ghost_path_radius * ghost_path_speed, 0.0);
1029 dr_entity_set_position(ghost_dr, &init_pos, &init_vel, NULL,
1030 al_get_time());
1031 }
1032 } else {
1034 }
1035 }
1036
1037 /* Ghost DR window sync */
1038 if (ghost_enabled && ghost_dr) {
1039 /* Algorithm radiolist */
1041 if (gui_algo >= 0 && gui_algo != (int)ghost_dr->algo)
1044
1045 /* Blend radiolist */
1047 if (gui_blend >= 0 && gui_blend != (int)ghost_dr->blend_mode)
1050
1051 /* Send interval slider */
1052 {
1053 double gui_interval = n_gui_slider_get_value(gui_ctx, gui_sld_dr_interval);
1054 if (fabs(gui_interval - ghost_send_interval) > 0.001)
1055 ghost_send_interval = gui_interval;
1057 char ibuf[48];
1058 snprintf(ibuf, sizeof(ibuf), "Interval [,/.]: %.2fs", ghost_send_interval);
1060 }
1061
1062 /* Blend time slider */
1063 {
1065 if (fabs(gui_bt - ghost_dr->blend_time) > 0.001)
1068 char btbuf[48];
1069 snprintf(btbuf, sizeof(btbuf), "Blend time: %.2fs", ghost_dr->blend_time);
1071 }
1072
1073 /* Threshold slider */
1074 {
1076 if (fabs(gui_th - ghost_dr->pos_threshold) > 0.001)
1079 char thbuf[48];
1080 snprintf(thbuf, sizeof(thbuf), "Threshold: %.2f", ghost_dr->pos_threshold);
1082 }
1083 }
1084
1085 /* Height slider */
1086 {
1087 int gui_h = (int)(n_gui_slider_get_value(gui_ctx, gui_sld_height) + 0.5);
1088 if (gui_h != paint_height) paint_height = gui_h;
1090 char hbuf[32];
1091 snprintf(hbuf, sizeof(hbuf), "Height: %d", paint_height);
1095 }
1096
1097 /* Tile listbox */
1098 {
1100 if (gui_sel >= 0 && gui_sel < NUM_TERRAINS && gui_sel != paint_terrain)
1101 paint_terrain = gui_sel;
1103 }
1104
1105 /* Slope slider */
1106 {
1107 int gui_slope = (int)(n_gui_slider_get_value(gui_ctx, gui_sld_slope) + 0.5);
1108 if (gui_slope != smooth_slope_max) smooth_slope_max = gui_slope;
1110 char sbuf2[32];
1111 snprintf(sbuf2, sizeof(sbuf2), "Slope max: %d", smooth_slope_max);
1113 }
1114
1115 /* Sync variables -> GUI */
1121
1122 /* Projection radio group */
1123 for (int p = 0; p < NUM_PROJECTIONS; p++) {
1125 current_proj = p;
1127 break;
1128 }
1129 }
1130 for (int p = 0; p < NUM_PROJECTIONS; p++)
1132 }
1133
1134 redraw_needed = true;
1135 break;
1136 }
1137
1138 case ALLEGRO_EVENT_KEY_DOWN:
1139 switch (ev.keyboard.keycode) {
1140 /* Movement keys (not GUI buttons) */
1141 case ALLEGRO_KEY_LEFT:
1142 case ALLEGRO_KEY_A:
1143 key_left = true;
1144 break;
1145 case ALLEGRO_KEY_RIGHT:
1146 case ALLEGRO_KEY_D:
1147 key_right = true;
1148 break;
1149 case ALLEGRO_KEY_UP:
1150 case ALLEGRO_KEY_W:
1151 key_up = true;
1152 break;
1153 case ALLEGRO_KEY_DOWN:
1154 case ALLEGRO_KEY_S:
1155 key_down = true;
1156 break;
1157 case ALLEGRO_KEY_SPACE:
1158 key_space = true;
1159 break;
1160 case ALLEGRO_KEY_ESCAPE:
1161 running = false;
1162 break;
1163 case ALLEGRO_KEY_PGDN:
1166 break;
1167 case ALLEGRO_KEY_PGUP:
1170 break;
1171 case ALLEGRO_KEY_EQUALS:
1172 case ALLEGRO_KEY_PAD_PLUS:
1174 break;
1175 case ALLEGRO_KEY_MINUS:
1176 case ALLEGRO_KEY_PAD_MINUS:
1177 if (paint_height > 0) paint_height--;
1178 break;
1179 case ALLEGRO_KEY_M:
1180 if (ghost_dr) {
1181 int a = (int)((ghost_dr->algo + 1) % 3);
1184 }
1185 break;
1186 case ALLEGRO_KEY_B:
1187 if (ghost_dr) {
1188 int b = (int)((ghost_dr->blend_mode + 1) % 3);
1191 }
1192 break;
1193 case ALLEGRO_KEY_COMMA:
1194 ghost_send_interval += 0.05;
1197 break;
1198 case ALLEGRO_KEY_FULLSTOP:
1199 ghost_send_interval -= 0.05;
1200 if (ghost_send_interval < 0.05) ghost_send_interval = 0.05;
1202 break;
1203 /* Keys P, H, G, V, N, F1-F4 are handled via
1204 n_gui_button_set_keycode bindings */
1205 }
1206 break;
1207
1208 case ALLEGRO_EVENT_KEY_UP:
1209 switch (ev.keyboard.keycode) {
1210 case ALLEGRO_KEY_LEFT:
1211 case ALLEGRO_KEY_A:
1212 key_left = false;
1213 break;
1214 case ALLEGRO_KEY_RIGHT:
1215 case ALLEGRO_KEY_D:
1216 key_right = false;
1217 break;
1218 case ALLEGRO_KEY_UP:
1219 case ALLEGRO_KEY_W:
1220 key_up = false;
1221 break;
1222 case ALLEGRO_KEY_DOWN:
1223 case ALLEGRO_KEY_S:
1224 key_down = false;
1225 break;
1226 case ALLEGRO_KEY_SPACE:
1227 key_space = false;
1228 break;
1229 }
1230 break;
1231
1232 case ALLEGRO_EVENT_MOUSE_AXES:
1233 last_mouse_x = ev.mouse.x;
1234 last_mouse_y = ev.mouse.y;
1235 if (ev.mouse.dz != 0 && !n_gui_wants_mouse(gui_ctx)) {
1236 n_iso_camera_zoom(camera, (float)ev.mouse.dz * 0.25f,
1237 (float)ev.mouse.x, (float)ev.mouse.y);
1238 }
1239 break;
1240
1241 case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
1242 if (ev.mouse.button == 1 && !n_gui_wants_mouse(gui_ctx)) {
1243 float wx, wy;
1244 n_iso_camera_screen_to_world(camera, (float)ev.mouse.x,
1245 (float)ev.mouse.y, &wx, &wy);
1246 int map_x, map_y;
1247 float click_fx = 0.0f, click_fy = 0.0f;
1249 &map_x, &map_y, &click_fx, &click_fy);
1250 if (map_x >= 0 && map_x < MAP_W && map_y >= 0 && map_y < MAP_H) {
1251 if (player_mode) {
1252 /* Click-to-move via A* pathfinding */
1253 if (player_path) {
1255 player_path = NULL;
1256 }
1257 ASTAR_GRID* grid = iso_map_to_astar_grid(isomap, PLAYER_MAX_HEIGH_DIFF, player_mx, player_my);
1258 if (grid) {
1259 int dest_x = map_x, dest_y = map_y;
1260 /* If destination is blocked, find nearest walkable tile */
1261 if (!n_astar_grid_get_walkable(grid, dest_x, dest_y, 0)) {
1262 int found = 0;
1263 for (int ring = 1; ring <= MAX(MAP_W, MAP_H) && !found; ring++) {
1264 for (int ry = -ring; ry <= ring && !found; ry++)
1265 for (int rx = -ring; rx <= ring && !found; rx++) {
1266 if (abs(rx) != ring && abs(ry) != ring) continue;
1267 int nx = map_x + rx, ny = map_y + ry;
1268 if (nx >= 0 && nx < MAP_W && ny >= 0 && ny < MAP_H &&
1269 n_astar_grid_get_walkable(grid, nx, ny, 0)) {
1270 dest_x = nx;
1271 dest_y = ny;
1272 found = 1;
1273 }
1274 }
1275 }
1276 }
1278 player_mx, player_my, 0,
1279 dest_x, dest_y, 0,
1281 n_astar_grid_free(grid);
1282 if (player_path && player_path->length > 1) {
1283 player_path_idx = 1;
1284 player_path_progress = 0.0f;
1285 /* If dest tile matches the clicked tile, use exact
1286 * click position; otherwise center of dest tile. */
1287 int final_tx = player_path->nodes[player_path->length - 1].x;
1288 int final_ty = player_path->nodes[player_path->length - 1].y;
1289 if (final_tx == map_x && final_ty == map_y) {
1290 player_click_fx = click_fx;
1291 player_click_fy = click_fy;
1292 } else {
1293 player_click_fx = (float)final_tx + 0.5f;
1294 player_click_fy = (float)final_ty + 0.5f;
1295 }
1296 /* Clamp to valid map range for border tiles.
1297 * Allow the full extent of the last tile so the
1298 * player can walk anywhere within border tiles. */
1299 if (player_click_fx < 0.0f) player_click_fx = 0.0f;
1300 if (player_click_fy < 0.0f) player_click_fy = 0.0f;
1301 if (player_click_fx > (float)MAP_W - 1e-4f) player_click_fx = (float)MAP_W - 1e-4f;
1302 if (player_click_fy > (float)MAP_H - 1e-4f) player_click_fy = (float)MAP_H - 1e-4f;
1303 } else {
1304 if (player_path) {
1306 player_path = NULL;
1307 }
1308 }
1309 }
1310 } else if (height_mode) {
1311 iso_map_set_height(isomap, map_x, map_y, paint_height);
1313 } else {
1316 }
1317 }
1318 }
1319 break;
1320
1321 case ALLEGRO_EVENT_DISPLAY_CLOSE:
1322 running = false;
1323 break;
1324
1325 case ALLEGRO_EVENT_DISPLAY_RESIZE:
1326 al_acknowledge_resize(display);
1327 screen_w = al_get_display_width(display);
1328 screen_h = al_get_display_height(display);
1330 break;
1331 }
1332
1333 /* Render */
1334 if (redraw_needed && al_is_event_queue_empty(queue)) {
1335 redraw_needed = false;
1336 al_set_target_backbuffer(display);
1337 al_clear_to_color(al_map_rgb(20, 25, 30));
1338
1339 /* Sync ISO_MAP rendering flags */
1342 isomap->show_grid = show_grid ? 1 : 0;
1345
1346 /* Build object list for depth-sorted rendering */
1347 N_ISO_OBJECT iso_objects[2];
1348 int iso_num_objects = 0;
1349
1350 if (player_mode) {
1351 iso_objects[iso_num_objects].fx = player_fx;
1352 iso_objects[iso_num_objects].fy = player_fy;
1353 iso_objects[iso_num_objects].fz = player_z;
1354 iso_objects[iso_num_objects].occluded_alpha = 0.35f;
1355 iso_objects[iso_num_objects].is_occluded = 0;
1356 iso_objects[iso_num_objects].draw = draw_player_object;
1357 iso_objects[iso_num_objects].user_data = NULL;
1358 iso_num_objects++;
1359 }
1360
1361 if (ghost_enabled && ghost_dr) {
1362 /* ghost uses the DR-computed screen pos; derive map pos */
1363 DR_VEC3 dr_pos;
1364 dr_entity_compute(ghost_dr, al_get_time(), &dr_pos);
1365 float dr_h = interpolate_height_at((float)dr_pos.x, (float)dr_pos.y);
1366 iso_objects[iso_num_objects].fx = (float)dr_pos.x;
1367 iso_objects[iso_num_objects].fy = (float)dr_pos.y;
1368 iso_objects[iso_num_objects].fz = dr_h;
1369 iso_objects[iso_num_objects].occluded_alpha = 0.35f;
1370 iso_objects[iso_num_objects].is_occluded = 0;
1371 iso_objects[iso_num_objects].draw = draw_ghost_object;
1372 iso_objects[iso_num_objects].user_data = NULL;
1373 iso_num_objects++;
1374 }
1375
1376 /* Draw map with depth-sorted objects */
1377 {
1378 ALLEGRO_BITMAP** trans_ptrs[NUM_TERRAINS];
1379 for (int t = 0; t < NUM_TERRAINS; t++)
1380 trans_ptrs[t] = transition_tiles[t];
1382 NULL, 0,
1383 floorf(camera->x * camera->zoom),
1384 floorf(camera->y * camera->zoom),
1385 camera->zoom,
1386 screen_w, screen_h, player_mode ? 1 : 0,
1387 iso_num_objects > 0 ? iso_objects : NULL,
1388 iso_num_objects);
1389 }
1390
1391 /* Update GUI info labels */
1392 {
1393 char sbuf[256];
1394 if (player_mode) {
1396 snprintf(sbuf, sizeof(sbuf), "Pos:(%.1f,%.1f) Z:%.1f GndH:%.1f %s | Zoom:%.1fx",
1398 player_on_ground ? "GND" : "AIR", camera->zoom);
1399 } else if (height_mode) {
1400 snprintf(sbuf, sizeof(sbuf), "Paint height:%d | Zoom:%.1fx | Slope max:%d",
1402 } else {
1403 snprintf(sbuf, sizeof(sbuf), "Brush:[%s] | Zoom:%.1fx | Slope max:%d",
1405 }
1407
1408 snprintf(sbuf, sizeof(sbuf), "Proj: %s (%.1f deg)",
1411
1412 if (hover_mx >= 0 && hover_mx < MAP_W && hover_my >= 0 && hover_my < MAP_H) {
1413 snprintf(sbuf, sizeof(sbuf), "Hover: (%d,%d) H:%d T:%s",
1418 }
1419 }
1420
1421 /* Draw GUI overlay */
1423
1424 al_flip_display();
1425 }
1426 }
1427
1428 /* Cleanup */
1430 if (player_path) {
1432 player_path = NULL;
1433 }
1436 if (isomap) iso_map_free(&isomap);
1437
1438 for (int t = 0; t < NUM_TERRAINS; t++) {
1439 for (int m = 0; m < NUM_MASKS; m++)
1440 if (transition_tiles[t][m]) al_destroy_bitmap(transition_tiles[t][m]);
1441 if (tile_bitmaps[t]) al_destroy_bitmap(tile_bitmaps[t]);
1442 }
1443 for (int m = 0; m < NUM_MASKS; m++)
1444 if (transition_masks[m]) al_destroy_bitmap(transition_masks[m]);
1445
1446 al_destroy_font(font);
1447 al_destroy_timer(timer);
1448 al_destroy_event_queue(queue);
1449 al_destroy_display(display);
1450
1451 return EXIT_SUCCESS;
1452}
int main(void)
ALLEGRO_DISPLAY * display
Definition ex_fluid.c:53
void on_grid_toggle(int widget_id, void *user_data)
Definition ex_gui.c:159
void on_player_toggle(int widget_id, void *user_data)
Definition ex_gui.c:147
static int smooth_height
Definition ex_gui.c:57
void on_smooth_toggle(int widget_id, void *user_data)
Definition ex_gui.c:165
static int show_grid
Definition ex_gui.c:56
void on_ghost_toggle(int widget_id, void *user_data)
Definition ex_gui.c:171
void on_proj_select(int widget_id, void *user_data)
Definition ex_gui.c:178
static int player_mode
Definition ex_gui.c:54
void on_height_toggle(int widget_id, void *user_data)
Definition ex_gui.c:153
static int ghost_enabled
Definition ex_gui.c:58
static int height_mode
Definition ex_gui.c:55
static int current_proj
Definition ex_gui.c:59
#define MAP_H
static int gui_btn_ghost
static float interpolate_height_at_clamped(float fx, float fy, int max_climb)
static int paint_terrain
#define TILE_LIFT
static int last_mouse_y
static int gui_btn_smooth
static void draw_ghost_object(float sx, float sy, float zoom, float alpha, void *user_data)
static float ghost_path_angle
#define TILE_W
static int player_path_idx
#define TERRAIN_LAVE
static ASTAR_PATH * player_path
static void on_reset_click(int id, void *data)
static int gui_sld_dr_blend_time
static ALLEGRO_BITMAP * transition_tiles[8][(16+16)]
#define MAP_FILE
static int gui_sld_dr_threshold
static int hover_mx
#define SCREEN_H
static double ghost_last_send
static int gui_sld_height
static int gui_win_proj
static int gui_win_modes
static int gui_sld_slope
static float ghost_path_radius
static bool running
static void draw_player_object(float sx, float sy, float zoom, float alpha, void *user_data)
static N_ISO_CAMERA * camera
static void randomize_map(void)
#define MAP_W
static int gui_sld_dr_interval
static float player_path_progress
static int screen_w
static int gui_win_ghost_dr
static float player_click_fy
static ISO_MAP * isomap
static int smooth_slope_max
#define JUMP_GRAVITY
static int player_mx
static ALLEGRO_BITMAP * transition_masks[(16+16)]
static int gui_lbl_dr_threshold
static float player_screen_y
static int gui_lbl_slope
static float ghost_path_speed
static float player_screen_x
static float ghost_path_cy
static int gui_btn_player
static ALLEGRO_BITMAP * tile_bitmaps[8]
static int gui_win_info
static int player_my
static bool redraw_needed
static const char * terrain_files[8]
static float ghost_screen_x
static int gui_btn_reset
static int gui_lbl_hover
static int gui_lbl_dr_algo
#define NUM_PROJECTIONS
static int gui_lst_tiles
static float ghost_true_y
static int gui_rad_dr_blend
#define MAX_HEIGHT
#define JUMP_VELOCITY
static int gui_btn_height
#define SCREEN_W
static float ghost_path_cx
static float player_click_fx
static int gui_lbl_proj
#define PLAYER_MOVE_SPEED
#define PLAYER_MAX_HEIGH_DIFF
static int gui_lbl_dr_interval
static int hover_my
static DR_ENTITY * ghost_dr
#define TERRAIN_TEMPLE3
#define TILE_H
static float player_fy
static float ghost_screen_y
static float player_z
static int gui_btn_proj[4]
static int last_mouse_x
static int paint_height
static double ghost_send_interval
#define FPS
static float player_vz
static float ghost_true_x
#define CAM_SMOOTHING
static const char * terrain_names[8]
static float player_fx
static int screen_h
static int gui_lbl_dr_blend_time
static int gui_lbl_dr_blend
#define NUM_TERRAINS
static int gui_win_tiles
static int gui_lbl_height
static int gui_rad_dr_algo
static int gui_btn_grid
static float interpolate_height_at(float fx, float fy)
#define NUM_MASKS
static int gui_lbl_status
static N_GUI_CTX * gui_ctx
static bool player_on_ground
#define M_PI
int x
grid X coordinate
Definition n_astar.h:110
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
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
#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
void n_astar_path_free(ASTAR_PATH *path)
Free a path returned by n_astar_find_path.
Definition n_astar.c:707
@ ASTAR_HEURISTIC_CHEBYSHEV
max of axis deltas (optimal for 8-dir)
Definition n_astar.h:101
Grid structure holding walkability, costs, and dimensions.
Definition n_astar.h:147
The computed path result.
Definition n_astar.h:116
void n_abort(char const *format,...)
abort program with a text
Definition n_common.c:52
double x
X component.
double y
Y component.
DR_ALGO algo
Extrapolation algorithm.
double blend_time
Duration of convergence blend in seconds.
DR_BLEND blend_mode
Convergence blending mode.
double pos_threshold
Position error threshold for sending updates (distance)
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_set_threshold(DR_ENTITY *entity, double threshold)
Set the position error threshold for triggering network updates.
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.
void dr_entity_set_blend_time(DR_ENTITY *entity, double blend_time)
Set the convergence blend duration.
DR_BLEND
Dead reckoning convergence/blending mode.
void dr_entity_set_blend_mode(DR_ENTITY *entity, DR_BLEND blend_mode)
Set the 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...
DR_ALGO
Dead reckoning extrapolation algorithm.
void dr_entity_set_algo(DR_ENTITY *entity, DR_ALGO algo)
Set the extrapolation algorithm.
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_PVB
Projective Velocity Blending (recommended)
@ 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
void n_gui_set_display_size(N_GUI_CTX *ctx, float w, float h)
Set the display (viewport) size for global scrollbar computation.
Definition n_gui.c:5176
int n_gui_button_is_toggled(N_GUI_CTX *ctx, int widget_id)
Check if a toggle button is currently in the "on" state.
Definition n_gui.c:1086
#define N_GUI_ALIGN_LEFT
left aligned text
Definition n_gui.h:156
int n_gui_add_slider(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, double min_val, double max_val, double initial, int mode, void(*on_change)(int, double, void *), void *user_data)
Add a slider widget.
Definition n_gui.c:1160
int n_gui_wants_mouse(N_GUI_CTX *ctx)
Check if the mouse is currently over any open GUI window.
Definition n_gui.c:7344
int n_gui_radiolist_add_item(N_GUI_CTX *ctx, int widget_id, const char *text)
add an item to a radiolist widget
Definition n_gui.c:2028
void n_gui_label_set_text(N_GUI_CTX *ctx, int widget_id, const char *text)
set the text of a label widget
Definition n_gui.c:2178
int n_gui_add_listbox(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, int selection_mode, void(*on_select)(int, int, int, void *), void *user_data)
Add a listbox widget.
Definition n_gui.c:1336
#define N_GUI_SLIDER_VALUE
slider uses raw start/end values
Definition n_gui.h:122
int n_gui_process_event(N_GUI_CTX *ctx, ALLEGRO_EVENT event)
Process an allegro event through the GUI system.
Definition n_gui.c:5767
int n_gui_add_label(N_GUI_CTX *ctx, int window_id, const char *text, float x, float y, float w, float h, int align)
Add a static text label.
Definition n_gui.c:1497
int n_gui_add_radiolist(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, void(*on_select)(int, int, void *), void *user_data)
Add a radio list widget (single selection with radio bullets)
Definition n_gui.c:1371
void n_gui_close_window(N_GUI_CTX *ctx, int window_id)
Close (hide) a window.
Definition n_gui.c:646
void n_gui_listbox_set_selected(N_GUI_CTX *ctx, int widget_id, int index, int selected)
set the selection state of a listbox item
Definition n_gui.c:1990
double n_gui_slider_get_value(N_GUI_CTX *ctx, int widget_id)
get the current value of a slider widget
Definition n_gui.c:1667
int n_gui_listbox_add_item(N_GUI_CTX *ctx, int widget_id, const char *text)
add an item to a listbox widget
Definition n_gui.c:1860
int n_gui_radiolist_get_selected(N_GUI_CTX *ctx, int widget_id)
get the selected item index in a radiolist widget
Definition n_gui.c:2065
#define N_GUI_SELECT_SINGLE
single item selection
Definition n_gui.h:142
void n_gui_button_set_toggled(N_GUI_CTX *ctx, int widget_id, int toggled)
Set the toggle state of a button.
Definition n_gui.c:1097
int n_gui_add_toggle_button(N_GUI_CTX *ctx, int window_id, const char *label, float x, float y, float w, float h, int shape, int initial_state, void(*on_click)(int, void *), void *user_data)
Add a toggle button widget (stays clicked/unclicked on single click)
Definition n_gui.c:1070
int n_gui_listbox_get_selected(N_GUI_CTX *ctx, int widget_id)
get the index of the first selected item in a listbox
Definition n_gui.c:1954
void n_gui_draw(N_GUI_CTX *ctx)
Draw all visible windows and their widgets.
Definition n_gui.c:4800
void n_gui_button_set_keycode(N_GUI_CTX *ctx, int widget_id, int keycode, int modifiers)
Bind a keyboard key with optional modifier requirements to a button.
Definition n_gui.c:1129
int n_gui_add_window(N_GUI_CTX *ctx, const char *title, float x, float y, float w, float h)
Add a new pseudo-window to the context.
Definition n_gui.c:581
N_GUI_CTX * n_gui_new_ctx(ALLEGRO_FONT *default_font)
Create a new GUI context.
Definition n_gui.c:466
void n_gui_destroy_ctx(N_GUI_CTX **ctx)
Destroy a GUI context and all its windows/widgets.
Definition n_gui.c:523
void n_gui_open_window(N_GUI_CTX *ctx, int window_id)
Open (show) a window.
Definition n_gui.c:654
void n_gui_slider_set_value(N_GUI_CTX *ctx, int widget_id, double value)
set the value of a slider widget
Definition n_gui.c:1681
#define N_GUI_SHAPE_ROUNDED
rounded rectangle shape
Definition n_gui.h:116
void n_gui_radiolist_set_selected(N_GUI_CTX *ctx, int widget_id, int index)
set the selected item in a radiolist widget
Definition n_gui.c:2079
void n_gui_set_widget_visible(N_GUI_CTX *ctx, int widget_id, int visible)
Show or hide a widget.
Definition n_gui.c:1583
int n_gui_add_button(N_GUI_CTX *ctx, int window_id, const char *label, float x, float y, float w, float h, int shape, void(*on_click)(int, void *), void *user_data)
Add a button widget to a window.
Definition n_gui.c:1001
The top-level GUI context that holds all windows.
Definition n_gui.h:882
float fz
height in map units (elevation)
float fy
fractional map Y position
int height
map height in tiles (Y axis)
float occluded_alpha
overlay alpha when behind tiles [0..1], 0=hidden, 0.35=ghost, 1=always visible
float tile_lift
vertical pixel offset per height unit
float angle_deg
current projection angle in degrees
int smooth_height
0 = CUT mode (cliff walls), 1 = SMOOTH mode (per-corner slopes)
int hover_mx
hovered tile X (-1 = none)
float fx
fractional map X position
void * user_data
user data passed to draw callback
N_ISO_OBJECT_DRAW_FN draw
draw callback
float zoom
zoom factor (1.0 = no zoom)
int is_occluded
OUTPUT: set to 1 if behind tiles during last iso_map_draw() call.
float y
camera Y offset (world units, pre-zoom)
int width
map width in tiles (X axis)
int show_grid
1 = draw grid overlay
int smooth_slope_max
max height diff rendered as slope (default 1)
int hover_my
hovered tile Y (-1 = none)
float x
camera X offset (world units, pre-zoom)
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])
ISO_MAP * iso_map_new(int width, int height, int num_terrains, int max_height)
Create a new height-aware isometric map.
void n_iso_camera_zoom(N_ISO_CAMERA *cam, float dz, float mouse_x, float mouse_y)
Zoom the camera toward a screen-space point.
void n_iso_camera_center_on(N_ISO_CAMERA *cam, float world_x, float world_y, int screen_w, int screen_h)
Center the camera so that a world point is at screen center.
int iso_map_save(const ISO_MAP *map, const char *filename)
Save ISO_MAP to a binary file.
void iso_map_lerp_projection(ISO_MAP *map, float dt)
Smoothly interpolate the projection angle toward the target.
void iso_mask_tile_to_diamond(ALLEGRO_BITMAP *bmp, int tile_w, int tile_h)
Mask a tile bitmap to the isometric diamond shape.
ISO_MAP * iso_map_load(const char *filename)
Load ISO_MAP from a binary file.
void n_iso_camera_free(N_ISO_CAMERA **cam)
Free a camera and set the pointer to NULL.
const char * iso_projection_name(int preset)
Get the display name for a projection preset.
void n_iso_camera_follow(N_ISO_CAMERA *cam, float target_x, float target_y, int screen_w, int screen_h, float smoothing, float dt)
Smoothly follow a world-space target.
int iso_map_get_terrain(const ISO_MAP *map, int mx, int my)
Get the terrain type at a cell.
void iso_map_draw(const ISO_MAP *map, ALLEGRO_BITMAP **tile_bitmaps, ALLEGRO_BITMAP ***transition_tiles, int num_masks, ALLEGRO_BITMAP **overlay_bitmaps, int num_overlay_tiles, float cam_px, float cam_py, float zoom, int screen_w, int screen_h, int player_mode, N_ISO_OBJECT *objects, int num_objects)
Draw the full ISO_MAP with segment-sorted rendering.
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_set_projection(ISO_MAP *map, int preset, float tile_width)
Set projection parameters from a preset and tile width.
void n_iso_camera_screen_to_world(const N_ISO_CAMERA *cam, float sx, float sy, float *wx, float *wy)
Convert screen pixel coordinates to world coordinates.
void iso_map_to_screen_f(const ISO_MAP *map, float fmx, float fmy, float h, float *screen_x, float *screen_y)
Convert map coordinates to screen coordinates (float version).
void iso_map_free(ISO_MAP **map_ptr)
Free an ISO_MAP and set the pointer to NULL.
void iso_generate_transition_tiles(ALLEGRO_BITMAP ***tiles, ALLEGRO_BITMAP **masks, ALLEGRO_BITMAP **tile_bitmaps, int num_terrains, int tile_w, int tile_h)
Pre-composite transition tiles (terrain texture * alpha mask).
void iso_map_set_projection_target(ISO_MAP *map, int preset)
Set the target projection for smooth interpolation.
void iso_map_set_terrain(ISO_MAP *map, int mx, int my, int terrain)
Set the terrain type at a cell.
void iso_generate_transition_masks(ALLEGRO_BITMAP **masks, int tile_w, int tile_h)
Generate the 32 procedural transition alpha masks (16 edge + 16 corner).
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)
void n_iso_camera_world_to_screen(const N_ISO_CAMERA *cam, float wx, float wy, float *sx, float *sy)
Convert world coordinates to screen pixel coordinates.
void iso_screen_to_map_height_f(const ISO_MAP *map, float screen_x, float screen_y, int tile_w, int tile_h, int *mx, int *my, float *out_fx, float *out_fy)
Height-aware screen-to-map conversion returning fractional tile coordinates.
void iso_screen_to_map_height(const ISO_MAP *map, float screen_x, float screen_y, int tile_w, int tile_h, int *mx, int *my)
Height-aware screen-to-map conversion with diamond hit testing.
void n_iso_camera_scroll(N_ISO_CAMERA *cam, float dx, float dy)
Scroll the camera by (dx, dy) world units.
N_ISO_CAMERA * n_iso_camera_new(float zoom_min, float zoom_max)
Create a new 2D isometric camera.
Height-aware isometric map with terrain and height layers.
2D isometric camera for viewport management.
Drawable object for depth-sorted isometric rendering.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:88
#define LOG_ERR
error conditions
Definition n_log.h:75
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_INFO
informational
Definition n_log.h:81
A* Pathfinding API for 2D and 3D grids.
Common headers and low-level functions & define.
Dead Reckoning API for latency hiding in networked games.
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.
Isometric/axonometric tile engine with height maps, terrain transitions, and A* pathfinding integrati...
List structures and definitions.
Generic log system.
N_STR and string function declaration.