Skip to content

Commit

Permalink
use some points in apis
Browse files Browse the repository at this point in the history
  • Loading branch information
goblinhack committed May 25, 2024
1 parent 1808f2d commit 698e773
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 64 deletions.
7 changes: 5 additions & 2 deletions src/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/level_anim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions src/level_cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
//
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/level_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
5 changes: 3 additions & 2 deletions src/level_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
3 changes: 3 additions & 0 deletions src/my_level.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 2 additions & 4 deletions src/my_point3d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@
#ifndef _MY_POINT3D_HPP_
#define _MY_POINT3D_HPP_

#include "my_point.hpp"
#include <string>

template < class T > class my_apoint3d
{
template < typename T > struct my_apoint3d {
public:
T x {};
T y {};
T z {};

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 &);
Expand Down
27 changes: 14 additions & 13 deletions src/my_thing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
//
Expand All @@ -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
//
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/point3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright Neil McGill, [email protected]
//

#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)
Expand Down
25 changes: 10 additions & 15 deletions src/thing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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();

Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
41 changes: 18 additions & 23 deletions src/thing_move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 698e773

Please sign in to comment.