From 698e773c360b6de727981d8bc99e9da5be8ed3e3 Mon Sep 17 00:00:00 2001 From: Neil McGill Date: Sat, 25 May 2024 09:12:04 +0100 Subject: [PATCH] use some points in apis --- src/level.cpp | 7 +++++-- src/level_anim.cpp | 2 +- src/level_cursor.cpp | 7 ++++--- src/level_display.cpp | 2 +- src/level_player.cpp | 5 +++-- src/my_level.hpp | 3 +++ src/my_point3d.hpp | 6 ++---- src/my_thing.hpp | 27 ++++++++++++++------------- src/point3d.cpp | 1 + src/thing.cpp | 25 ++++++++++--------------- src/thing_move.cpp | 41 ++++++++++++++++++----------------------- 11 files changed, 62 insertions(+), 64 deletions(-) diff --git a/src/level.cpp b/src/level.cpp index 3b0a122..3e381fa 100644 --- a/src/level.cpp +++ b/src/level.cpp @@ -19,6 +19,9 @@ Levelp level_new(void) // // Allocate the level as a flat C structure to allow history rewind // + // NOTE: if we use "new" here, the initialization is visibly slower. + // DO NOT USE C++ classes or types + // Levelp l = (Levelp) myzalloc(SIZEOF(*l), "l"); if (! l) { return nullptr; @@ -310,7 +313,7 @@ void level_map_set(Levelp l, int z, const char *in) if (need_floor) { auto tp_add = tp_floor; - auto t = thing_init(l, tp_add, x, y, z); + auto t = thing_init(l, tp_add, point3d(x, y, z)); if (t) { thing_push(l, t); } @@ -320,7 +323,7 @@ void level_map_set(Levelp l, int z, const char *in) continue; } - auto t = thing_init(l, tp, x, y, z); + auto t = thing_init(l, tp, point3d(x, y, z)); if (t) { thing_push(l, t); } diff --git a/src/level_anim.cpp b/src/level_anim.cpp index 6b38792..6ff6526 100644 --- a/src/level_anim.cpp +++ b/src/level_anim.cpp @@ -20,7 +20,7 @@ void level_anim(Levelp l) if (! player) { return; } - int z = player->z; + int z = player->at.z; auto ts = time_ms(); static uint32_t last_ts; diff --git a/src/level_cursor.cpp b/src/level_cursor.cpp index 9c535e2..d285b01 100644 --- a/src/level_cursor.cpp +++ b/src/level_cursor.cpp @@ -34,7 +34,7 @@ static std::vector< point > level_cursor_path_draw_line_attempt(Levelp l, Thingp static std::vector< point > empty; - int z = player->z; + int z = player->at.z; Dmap d {}; point dmap_start = start; @@ -78,7 +78,7 @@ static std::vector< point > level_cursor_path_draw_line_attempt(Levelp l, Thingp // // If standing on a hazard, then plot a course that allows travel over hazards. // - if (level_is_cursor_path_hazard(l, player->x, player->y, z)) { + if (level_is_cursor_path_hazard(l, player->at.x, player->at.y, z)) { // // Just map the shortest path outta here // @@ -273,7 +273,8 @@ void level_cursor_update(Levelp l) // // Draw the path // - auto cursor_path = level_cursor_path_draw_line(l, point(player->x, player->y), point(l->cursor_x, l->cursor_y)); + auto cursor_path + = level_cursor_path_draw_line(l, point(player->at.x, player->at.y), point(l->cursor_x, l->cursor_y)); for (auto p : cursor_path) { l->cursor[ p.x ][ p.y ] = CURSOR_PATH; } diff --git a/src/level_display.cpp b/src/level_display.cpp index c93f4be..9ce8603 100644 --- a/src/level_display.cpp +++ b/src/level_display.cpp @@ -191,7 +191,7 @@ void level_display(Levelp l) if (! player) { return; } - auto z = player->z; + auto z = player->at.z; // // Soft scroll to the player/ diff --git a/src/level_player.cpp b/src/level_player.cpp index 7116aee..29c6f65 100644 --- a/src/level_player.cpp +++ b/src/level_player.cpp @@ -38,8 +38,9 @@ void thing_player_move_delta(Levelp l, int dx, int dy, int dz) return; } - if (thing_can_move_to(l, t, t->x + dx, t->y + dy, t->z + dz)) { - thing_move(l, t, t->x + dx, t->y + dy, t->z + dz); + point3d to(t->at.x + dx, t->at.y + dy, t->at.z + dz); + if (thing_can_move_to(l, t, to)) { + thing_move(l, t, to); level_tick_begin_requested(l, "player moved"); } diff --git a/src/my_level.hpp b/src/my_level.hpp index 8c19f32..9b0af5e 100644 --- a/src/my_level.hpp +++ b/src/my_level.hpp @@ -19,6 +19,9 @@ enum { typedef struct Level_ { ////////////////////////////////////////////////////////////// // No c++ types can be used here, to allow easy level replay + // + // Why C types only ? For large data structures it is visibly + // faster to malloc and memset versus default construction. ////////////////////////////////////////////////////////////// // // Level number. diff --git a/src/my_point3d.hpp b/src/my_point3d.hpp index 7267284..9918d95 100644 --- a/src/my_point3d.hpp +++ b/src/my_point3d.hpp @@ -6,10 +6,9 @@ #ifndef _MY_POINT3D_HPP_ #define _MY_POINT3D_HPP_ -#include "my_point.hpp" +#include -template < class T > class my_apoint3d -{ +template < typename T > struct my_apoint3d { public: T x {}; T y {}; @@ -17,7 +16,6 @@ template < class T > class my_apoint3d my_apoint3d(void) : x(0), y(0), z(0) {} my_apoint3d(T vx, T vy, T vz) : x(vx), y(vy), z(vz) {} - my_apoint3d(const my_apoint3d &a) : x(a.x), y(a.y), z(a.z) {} void operator+=(my_apoint3d< T > const &); void operator-=(my_apoint3d< T > const &); diff --git a/src/my_thing.hpp b/src/my_thing.hpp index bdbc28f..24a9a8a 100644 --- a/src/my_thing.hpp +++ b/src/my_thing.hpp @@ -8,6 +8,8 @@ #include "my_enums.hpp" #include "my_minimal.hpp" +#include "my_point.hpp" // does not seem to make the compile speed much worse +#include "my_point3d.hpp" // does not seem to make the compile speed much worse // // Entropy is always > 0 for Thing IDs to distinguish them @@ -33,15 +35,18 @@ typedef struct ThingAi_ { // uint8_t in_use : 1; struct { - struct { - int8_t x; - int8_t y; - } points[ THING_MOVE_PATH_MAX ]; + point points[ THING_MOVE_PATH_MAX ]; int16_t size; } move_path; } ThingAi; typedef struct Thing_ { + ////////////////////////////////////////////////////////////// + // No c++ types can be used here, to allow easy level replay + // + // Why C types only ? For large data structures it is visibly + // faster to malloc and memset versus default construction. + ////////////////////////////////////////////////////////////// // // Unique ID // @@ -53,15 +58,11 @@ typedef struct Thing_ { // // Map co-ords. // - int8_t x; - int8_t y; - int8_t z; + point3d at; // // Old map co-ords used for interpolation when moving. // - int8_t old_x; - int8_t old_y; - int8_t old_z; + point3d old; // // Direction // @@ -111,7 +112,7 @@ Thingp thing_and_tp_get(Levelp, int x, int y, int z, int slot, Tpp * = nullptr); Thingp thing_find(Levelp, ThingId id); Thingp thing_find_optional(Levelp, ThingId id); Thingp thing_get(Levelp, int x, int y, int z, int slot); -Thingp thing_init(Levelp, Tpp, int x, int y, int z); +Thingp thing_init(Levelp, Tpp, point3d); Thingp thing_player(Levelp); ThingAip thing_ai(Levelp, Thingp); @@ -126,7 +127,7 @@ void thing_dir_tr_set(Thingp, uint8_t); void thing_dir_up_set(Thingp, uint8_t); void thing_fini(Levelp, Thingp); void thing_interpolate(Levelp, Thingp, float dt); -void thing_move(Levelp, Thingp, int new_x, int new_y, int new_z); +void thing_move(Levelp, Thingp, point3d to); void thing_player_map_center(Levelp); void thing_player_move_accum(Levelp, bool up, bool down, bool left, bool right); void thing_player_move_delta(Levelp, int dx, int dy, int dz); @@ -136,7 +137,7 @@ void thing_push(Levelp, Thingp); void thing_set_dir_from_delta(Thingp, int dx, int dy); void thing_update(Levelp, Thingp); -bool thing_can_move_to(Levelp, Thingp, int new_loc_x, int new_loc_y, int new_loc_z); +bool thing_can_move_to(Levelp, Thingp, point3d to); bool thing_player_move_request(Levelp, bool up, bool down, bool left, bool right); bool thing_is_dir_down(Thingp t); bool thing_is_dir_tr(Thingp t); diff --git a/src/point3d.cpp b/src/point3d.cpp index 37c8395..3f04322 100644 --- a/src/point3d.cpp +++ b/src/point3d.cpp @@ -2,6 +2,7 @@ // Copyright Neil McGill, goblinhack@gmail.com // +#include "my_point.hpp" #include "my_point3d.hpp" template < typename T > my_apoint3d< T > operator+(my_apoint3d< T > const &a, my_apoint3d< T > const &b) diff --git a/src/thing.cpp b/src/thing.cpp index 8b40b88..2f4a859 100644 --- a/src/thing.cpp +++ b/src/thing.cpp @@ -20,7 +20,7 @@ ENUM_DEF_C(THING_FLAG_ENUM, ThingFlag) -static Thingp thing_alloc(Levelp l, Tpp tp, int x, int y, int z); +static Thingp thing_alloc(Levelp l, Tpp tp, point3d); static void thing_free(Levelp l, Thingp t); static ThingAip thing_ai_alloc(Levelp l, Thingp t); @@ -35,25 +35,20 @@ Tpp thing_tp(Thingp t) return nullptr; } -Thingp thing_init(Levelp l, Tpp tp, int x, int y, int z) +Thingp thing_init(Levelp l, Tpp tp, point3d at) { TRACE_NO_INDENT(); - auto t = thing_alloc(l, tp, x, y, z); + auto t = thing_alloc(l, tp, at); if (! t) { return nullptr; } - t->x = x; - t->y = y; - t->z = z; + t->at = at; + t->old = at; - t->old_x = x; - t->old_y = y; - t->old_z = z; - - t->pix_x = t->x * TILE_WIDTH; - t->pix_y = t->y * TILE_HEIGHT; + t->pix_x = t->at.x * TILE_WIDTH; + t->pix_y = t->at.y * TILE_HEIGHT; // // Assign an initial tile @@ -87,7 +82,7 @@ void thing_fini(Levelp l, Thingp t) thing_free(l, t); } -static Thingp thing_alloc(Levelp l, Tpp tp, int x, int y, int z) +static Thingp thing_alloc(Levelp l, Tpp tp, point3d) { TRACE_NO_INDENT(); @@ -104,7 +99,7 @@ static Thingp thing_alloc(Levelp l, Tpp tp, int x, int y, int z) entropy++; } - memset(t, 0, SIZEOF(*t)); + memset((void *) t, 0, SIZEOF(*t)); ThingId thing_id; thing_id = (entropy << THING_COMMON_ID_BITS) | index; @@ -132,7 +127,7 @@ static void thing_free(Levelp l, Thingp t) thing_ai_free(l, t); - memset(t, 0, SIZEOF(*t)); + memset((void *) t, 0, SIZEOF(*t)); } static ThingAip thing_ai_alloc(Levelp l, Thingp t) diff --git a/src/thing_move.cpp b/src/thing_move.cpp index 8475559..62635bd 100644 --- a/src/thing_move.cpp +++ b/src/thing_move.cpp @@ -184,49 +184,44 @@ void thing_set_dir_from_delta(Thingp t, int dx, int dy) } } -void thing_move(Levelp l, Thingp t, int new_x, int new_y, int new_z) +void thing_move(Levelp l, Thingp t, point3d to) { - if (level_is_oob(l, new_x, new_y)) { + if (level_is_oob(l, to.x, to.y)) { return; } - if ((new_x == t->x) && (new_y == t->y) && (new_z == t->z)) { + if (to == t->at) { return; } thing_pop(l, t); - t->pix_x = t->x * TILE_WIDTH; - t->pix_y = t->y * TILE_HEIGHT; + t->pix_x = t->at.x * TILE_WIDTH; + t->pix_y = t->at.y * TILE_HEIGHT; - t->old_x = t->x; - t->old_y = t->y; - t->old_z = t->z; - - t->x = new_x; - t->y = new_y; - t->z = new_z; + t->old = t->at; + t->at = to; thing_push(l, t); } -bool thing_can_move_to(Levelp l, Thingp t, int new_loc_x, int new_loc_y, int new_loc_z) +bool thing_can_move_to(Levelp l, Thingp t, point3d to) { - if (level_is_oob(l, new_loc_x, new_loc_y)) { + if (level_is_oob(l, to.x, to.y)) { return false; } - if ((new_loc_x == t->x) && (new_loc_y == t->y)) { + if (to == t->at) { return true; } - auto dx = new_loc_x - t->x; - auto dy = new_loc_y - t->y; + auto dx = to.x - t->at.x; + auto dy = to.y - t->at.y; thing_set_dir_from_delta(t, dx, dy); auto my_tp = thing_tp(t); - FOR_ALL_THINGS_AND_TPS_AT(l, it, it_tp, new_loc_x, new_loc_y, new_loc_z) + FOR_ALL_THINGS_AND_TPS_AT(l, it, it_tp, to.x, to.y, to.z) { if (tp_is_player(my_tp) && tp_is_obs_player(it_tp)) { return false; @@ -242,12 +237,12 @@ bool thing_can_move_to(Levelp l, Thingp t, int new_loc_x, int new_loc_y, int new void thing_interpolate(Level *l, Thingp t, float dt) { - if ((t->old_x == t->x) && (t->old_y == t->y)) { + if (t->old == t->at) { return; } - float pix_x = (float) t->old_x + (((float) (t->x - t->old_x)) * dt); - float pix_y = (float) t->old_y + (((float) (t->y - t->old_y)) * dt); + float pix_x = (float) t->old.x + (((float) (t->at.x - t->old.x)) * dt); + float pix_y = (float) t->old.y + (((float) (t->at.y - t->old.y)) * dt); t->pix_x = pix_x * TILE_WIDTH; t->pix_y = pix_y * TILE_HEIGHT; @@ -259,7 +254,7 @@ void thing_push(Levelp l, Thingp t) int x = t->pix_x / TILE_WIDTH; int y = t->pix_y / TILE_HEIGHT; - int z = t->z; + int z = t->at.z; if (level_is_oob(l, x, y)) { return; @@ -316,7 +311,7 @@ void thing_pop(Levelp l, Thingp t) uint8_t x = t->pix_x / TILE_WIDTH; uint8_t y = t->pix_y / TILE_HEIGHT; - uint8_t z = t->z; + uint8_t z = t->at.z; if (level_is_oob(l, x, y)) { return;