Skip to content

Commit

Permalink
use point for apis
Browse files Browse the repository at this point in the history
  • Loading branch information
goblinhack committed May 25, 2024
1 parent bc78df9 commit 48f83aa
Show file tree
Hide file tree
Showing 17 changed files with 304 additions and 271 deletions.
2 changes: 1 addition & 1 deletion src/game_events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ uint8_t game_mouse_motion(class Game *g, int x, int y, int relx, int rely, int w

auto l = game_level_get(g);
if (l) {
level_scroll_delta(l, wheelx, wheely);
level_scroll_delta(l, point(wheelx, -wheely));
}

return true;
Expand Down
76 changes: 44 additions & 32 deletions src/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,26 +189,26 @@ void level_fini(Levelp l)
myfree(l);
}

bool level_set_thing_id(Levelp l, int x, int y, int z, int slot, ThingId id)
bool level_set_thing_id(Levelp l, point3d p, int slot, ThingId id)
{
if (level_is_oob(l, x, y)) {
if (level_is_oob(l, p)) {
return false;
}
l->thing_id[ x ][ y ][ z ][ slot ] = id;
l->thing_id[ p.x ][ p.y ][ p.z ][ slot ] = id;
return true;
}

ThingId level_get_thing_id(Levelp l, int x, int y, int z, int slot)
ThingId level_get_thing_id(Levelp l, point3d p, int slot)
{
if (level_is_oob(l, x, y)) {
if (level_is_oob(l, p)) {
return 0;
}
return l->thing_id[ x ][ y ][ z ][ slot ];
return l->thing_id[ p.x ][ p.y ][ p.z ][ slot ];
}

bool level_flag(Levelp l, ThingFlag f, int x, int y, int z)
bool level_flag(Levelp l, ThingFlag f, point3d p)
{
FOR_ALL_THINGS_AND_TPS_AT(l, it, it_tp, x, y, z)
FOR_ALL_THINGS_AND_TPS_AT(l, it, it_tp, p)
{
if (tp_flag(it_tp, f)) {
return true;
Expand All @@ -217,35 +217,47 @@ bool level_flag(Levelp l, ThingFlag f, int x, int y, int z)
return false;
}

bool level_is_oob(Level *l, int x, int y)
bool level_is_oob(Level *l, point p)
{
if (! l) {
return true;
}
if (x < 0) {
if (p.x < 0) {
return true;
}
if (y < 0) {
if (p.y < 0) {
return true;
}
if (x >= MAP_WIDTH) {
if (p.x >= MAP_WIDTH) {
return true;
}
if (y >= MAP_HEIGHT) {
if (p.y >= MAP_HEIGHT) {
return true;
}
return false;
}

bool level_is_oob(Level *l, int x, int y, int z)
bool level_is_oob(Level *l, point3d p)
{
if (z < 0) {
if (p.z < 0) {
return true;
}
if (z >= MAP_DEPTH) {
if (p.z >= MAP_DEPTH) {
return true;
}
return level_is_oob(l, x, y);
if (p.x < 0) {
return true;
}
if (p.y < 0) {
return true;
}
if (p.x >= MAP_WIDTH) {
return true;
}
if (p.y >= MAP_HEIGHT) {
return true;
}
return false;
}

void level_map_set(Levelp l, int z, const char *in)
Expand Down Expand Up @@ -331,21 +343,21 @@ void level_map_set(Levelp l, int z, const char *in)
}
}

bool level_is_same_type(Levelp l, int x, int y, int z, Tpp tp)
bool level_is_same_type(Levelp l, point3d p, Tpp tp)
{
TRACE_NO_INDENT();

if (! l) {
return false;
}

if (level_is_oob(l, x, y)) {
if (level_is_oob(l, p)) {
return false;
}

for (auto slot = 0; slot < MAP_SLOTS; slot++) {
Tpp it_tp;
Thingp it = thing_and_tp_get(l, x, y, z, slot, &it_tp);
Thingp it = thing_and_tp_get(l, p, slot, &it_tp);
if (! it) {
continue;
}
Expand All @@ -371,11 +383,11 @@ void level_bounds_set(Levelp l)
//
// Set the scroll bounds
//
if (l->pixel_map_at_x < 0) {
l->pixel_map_at_x = 0;
if (l->pixel_map_at.x < 0) {
l->pixel_map_at.x = 0;
}
if (l->pixel_map_at_y < 0) {
l->pixel_map_at_y = 0;
if (l->pixel_map_at.y < 0) {
l->pixel_map_at.y = 0;
}

//
Expand All @@ -384,18 +396,18 @@ void level_bounds_set(Levelp l)
int max_pix_x = (MAP_WIDTH * dw) - game_pix_height_get(game);
int max_pix_y = (MAP_HEIGHT * dh) - game_pix_height_get(game);

if (l->pixel_map_at_x > max_pix_x) {
l->pixel_map_at_x = max_pix_x;
if (l->pixel_map_at.x > max_pix_x) {
l->pixel_map_at.x = max_pix_x;
}
if (l->pixel_map_at_y > max_pix_y) {
l->pixel_map_at_y = max_pix_y;
if (l->pixel_map_at.y > max_pix_y) {
l->pixel_map_at.y = max_pix_y;
}

//
// Set the tile bounds
//
int tmp_minx = l->pixel_map_at_x / dw;
int tmp_miny = l->pixel_map_at_y / dh;
int tmp_minx = l->pixel_map_at.x / dw;
int tmp_miny = l->pixel_map_at.y / dh;
tmp_minx -= clipping_border;
tmp_minx -= clipping_border;

Expand All @@ -406,8 +418,8 @@ void level_bounds_set(Levelp l)
tmp_miny = 0;
}

int tmp_maxx = (l->pixel_map_at_x + game_map_pix_width_get(game)) / dw;
int tmp_maxy = (l->pixel_map_at_y + game_map_pix_height_get(game)) / dh;
int tmp_maxx = (l->pixel_map_at.x + game_map_pix_width_get(game)) / dw;
int tmp_maxy = (l->pixel_map_at.y + game_map_pix_height_get(game)) / dh;

tmp_maxx += clipping_border;
tmp_maxy += clipping_border;
Expand Down
5 changes: 3 additions & 2 deletions src/level_anim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ void level_anim(Levelp l)
for (auto slot = 0; slot < MAP_SLOTS; slot++) {
for (auto y = l->miny; y < l->maxy; y++) {
for (auto x = l->minx; x < l->maxx; x++) {
Tpp tp;
Thingp t = thing_and_tp_get(l, x, y, z, slot, &tp);
Tpp tp;
point3d p(x, y, z);
Thingp t = thing_and_tp_get(l, p, slot, &tp);
if (! t) {
continue;
}
Expand Down
47 changes: 32 additions & 15 deletions src/level_cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,25 @@ 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->at.x, player->at.y, z)) {
if (level_is_cursor_path_hazard_at(l, player->at.x, player->at.y, z)) {
//
// Just map the shortest path outta here
//
for (auto y = miny; y < maxy; y++) {
for (auto x = minx; x < maxx; x++) {
if (level_is_cursor_path_blocker(l, x, y, z)) {
if (level_is_cursor_path_blocker_at(l, x, y, z)) {
d.val[ x ][ y ] = DMAP_IS_WALL;
} else {
d.val[ x ][ y ] = DMAP_IS_PASSABLE;
}
}
}
} else if (level_is_cursor_path_hazard(l, l->cursor_at.x, l->cursor_at.y, z)) {
} else if (level_is_cursor_path_hazard_at(l, l->cursor_at.x, l->cursor_at.y, z)) {
bool got_one = false;
std::initializer_list< ThingFlag > init = {is_lava, is_chasm};

for (auto i : init) {
if (level_flag(l, i, l->cursor_at.x, l->cursor_at.y, z)) {
if (level_flag(l, i, point3d(l->cursor_at.x, l->cursor_at.y, z))) {
got_one = true;

//
Expand All @@ -107,9 +107,10 @@ static std::vector< point > level_cursor_path_draw_line_attempt(Levelp l, Thingp
//
for (auto y = miny; y < maxy; y++) {
for (auto x = minx; x < maxx; x++) {
point3d p(x, y, z);

if (level_is_cursor_path_hazard(l, x, y, z)) {
if (! level_flag(l, i, x, y, z)) {
if (level_is_cursor_path_hazard(l, p)) {
if (! level_flag(l, i, p)) {
d.val[ x ][ y ] = DMAP_IS_WALL;
continue;
}
Expand All @@ -128,7 +129,9 @@ static std::vector< point > level_cursor_path_draw_line_attempt(Levelp l, Thingp
//
for (auto y = miny; y < maxy; y++) {
for (auto x = minx; x < maxx; x++) {
if (level_is_cursor_path_blocker(l, x, y, z) || level_is_cursor_path_hazard(l, x, y, z)) {
point3d p(x, y, z);

if (level_is_cursor_path_blocker(l, p) || level_is_cursor_path_hazard(l, p)) {
d.val[ x ][ y ] = DMAP_IS_WALL;
} else {
d.val[ x ][ y ] = DMAP_IS_PASSABLE;
Expand All @@ -142,7 +145,9 @@ static std::vector< point > level_cursor_path_draw_line_attempt(Levelp l, Thingp
//
for (auto y = miny; y < maxy; y++) {
for (auto x = minx; x < maxx; x++) {
if (level_is_cursor_path_blocker(l, x, y, z) || level_is_cursor_path_hazard(l, x, y, z)) {
point3d p(x, y, z);

if (level_is_cursor_path_blocker(l, p) || level_is_cursor_path_hazard(l, p)) {
d.val[ x ][ y ] = DMAP_IS_WALL;
} else {
d.val[ x ][ y ] = DMAP_IS_PASSABLE;
Expand All @@ -157,14 +162,16 @@ static std::vector< point > level_cursor_path_draw_line_attempt(Levelp l, Thingp
if (attempt == 1) {
for (auto y = miny; y < maxy; y++) {
for (auto x = minx; x < maxx; x++) {
point3d p(x, y, z);

if (! l->is_walked[ x ][ y ][ z ]) {
d.val[ x ][ y ] = DMAP_IS_WALL;
}

//
// Probably best to not use tiles where there is a monster
//
if (level_is_monst(l, x, y, z)) {
if (level_is_monst(l, p)) {
d.val[ x ][ y ] = DMAP_IS_WALL;
}
}
Expand Down Expand Up @@ -255,20 +262,27 @@ static void level_cursor_player_move_path_update(Levelp l, Thingp t, std::vector

void level_cursor_update(Levelp l)
{
if (l->cursor_at == l->old_cursor_at) {
auto player = thing_player(l);
if (! player) {
//
// If no player, clear the cursor
//
memset(l->cursor, 0, SIZEOF(l->cursor));
return;
}

//
// Clear the path if there is no player or we're about to remake it.
// Has something changed?
//
memset(l->cursor, 0, SIZEOF(l->cursor));

auto player = thing_player(l);
if (! player) {
if ((player->at == player->old_at) && (l->cursor_at == l->old_cursor_at)) {
return;
}

//
// Clear the path.
//
memset(l->cursor, 0, SIZEOF(l->cursor));

//
// Draw the path
//
Expand All @@ -280,5 +294,8 @@ void level_cursor_update(Levelp l)

l->old_cursor_at = l->cursor_at;

//
// Update the player with the path.
//
level_cursor_player_move_path_update(l, player, cursor_path);
}
Loading

0 comments on commit 48f83aa

Please sign in to comment.