Skip to content

Commit

Permalink
api messing around
Browse files Browse the repository at this point in the history
  • Loading branch information
goblinhack committed May 13, 2024
1 parent 8e89920 commit 16ba5a0
Show file tree
Hide file tree
Showing 20 changed files with 689 additions and 4,228 deletions.
265 changes: 202 additions & 63 deletions src/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,33 @@
//

#include "my_callstack.hpp"
#include "my_charmap.hpp"
#include "my_game.hpp"
#include "my_game_defs.hpp"
#include "my_level.hpp"
#include "my_main.hpp"
#include "my_ptrcheck.hpp"
#include "my_tile.hpp"
#include "my_tp.hpp"

Levelp level_constructor(void)
{
TRACE_NO_INDENT();

//
// Allocate the level as a flat C structure to allow history rewind
//
Levelp l = (Levelp) myzalloc(sizeof(*l), "l");
if (! l) {
return nullptr;
}

level_dungeon_create_and_place(l);
level_assign_tiles(l);
level_scroll_warp_to_player(l);

return l;
}

#if 0
void level_map_constructor(Levelp l)
Expand Down Expand Up @@ -153,52 +177,47 @@ void level_map_constructor(Levelp l)
}
#endif

Levelp level_constructor(void)
{
TRACE_NO_INDENT();

//
// Allocate the level as a flat C structure to allow history rewind
//
Levelp l = (Levelp) myzalloc(sizeof(*l), "l");
if (! l) {
return nullptr;
}

level_dungeon_create_and_place(l);
level_assign_tiles(l);
level_thing_player_create_and_place(l);
level_scroll_warp_to_player(l);

return l;
}

void level_destructor(Levelp l)
{
TRACE_NO_INDENT();

myfree(l);
}

bool is_oob(Level *l, int x, int y)
bool level_set_thing_id(Levelp l, int x, int y, uint8_t slot, ThingId id)
{
if (x < 0) {
return true;
}
if (y < 0) {
return true;
if (level_is_oob(l, x, y)) {
return false;
}
if (x >= MAP_WIDTH) {
return true;
l->thing_id[ x ][ y ][ slot ] = id;
return true;
}

ThingId level_get_thing_id(Levelp l, int x, int y, uint8_t slot)
{
if (level_is_oob(l, x, y)) {
return 0;
}
if (y >= MAP_HEIGHT) {
return true;
return l->thing_id[ x ][ y ][ slot ];
}

bool level_flag(Levelp l, ThingFlag f, int x, int y)
{
FOR_ALL_THINGS_AND_TPS_AT(l, it, it_tp, x, y)
{
auto it_tp = level_thing_tp(l, it);
if (tp_flag(it_tp, f)) {
return true;
}
}
return false;
}

bool level_is_oob(Levelp l, int x, int y)
bool level_is_oob(Level *l, int x, int y)
{
if (! l) {
return true;
}
if (x < 0) {
return true;
}
Expand All @@ -214,54 +233,174 @@ bool level_is_oob(Levelp l, int x, int y)
return false;
}

bool level_set_id(Levelp l, int x, int y, uint8_t slot, Id id)
void level_map_set(Levelp l, const char *in)
{
if (level_is_oob(l, x, y)) {
return false;
TRACE_NO_INDENT();
const auto row_len = MAP_WIDTH;
auto expected_len = row_len * MAP_HEIGHT;

if ((int) strlen(in) != expected_len) {
DIE("bad map size, expected %d, got %d", (int) strlen(in), (int) expected_len);
}
l->obj[ x ][ y ][ slot ].id = id;
return true;
}

void level_set_id_no_check(Levelp l, int x, int y, uint8_t slot, Id id) { l->obj[ x ][ y ][ slot ].id = id; }
auto tp_wall = tp_random(is_wall);
auto tp_door = tp_find_mand("door");
auto tp_floor = tp_find_mand("floor");
auto tp_exit = tp_find_mand("exit");
auto tp_player1 = tp_find_mand("player1");
auto tp_player2 = tp_find_mand("player2");
auto tp_player3 = tp_find_mand("player3");
auto tp_player4 = tp_find_mand("player4");

Id level_get_id(Levelp l, int x, int y, uint8_t slot)
{
if (level_is_oob(l, x, y)) {
return 0;
for (auto y = 0; y < MAP_HEIGHT; y++) {
for (auto x = 0; x < MAP_WIDTH; x++) {
auto offset = (row_len * y) + x;
auto c = in[ offset ];
Tpp tp = nullptr;

bool need_floor = false;

switch (c) {
case CHARMAP_WALL :
need_floor = true;
tp = tp_wall;
break;
case CHARMAP_DOOR :
need_floor = true;
tp = tp_door;
break;
case CHARMAP_TREASURE : break;
case CHARMAP_MONST1 : break;
case CHARMAP_PLAYER1 :
need_floor = true;
tp = tp_player1;
break;
case CHARMAP_PLAYER2 :
need_floor = true;
tp = tp_player2;
break;
case CHARMAP_PLAYER3 :
need_floor = true;
tp = tp_player3;
break;
case CHARMAP_PLAYER4 :
need_floor = true;
tp = tp_player4;
break;
case CHARMAP_EXIT :
need_floor = true;
tp = tp_exit;
break;
case CHARMAP_KEY : need_floor = true; break;
case CHARMAP_EMPTY : need_floor = true; break;
default : DIE("unexpected map char '%c'", c);
}

if (need_floor) {
auto tp = tp_floor;
auto t = level_thing_init(l, tp, x, y);
if (t) {
level_thing_push(l, t);
}
}

if (! tp) {
continue;
}

auto t = level_thing_init(l, tp, x, y);
if (t) {
level_thing_push(l, t);
}
}
}
return l->obj[ x ][ y ][ slot ].id;
}

Id level_get_id_no_check(Levelp l, int x, int y, uint8_t slot) { return l->obj[ x ][ y ][ slot ].id; }

bool level_set_tile(Levelp l, int x, int y, uint8_t slot, Tilep tile)
bool level_is_same_type(Levelp l, int x, int y, Tpp tp)
{
TRACE_NO_INDENT();

if (! l) {
return false;
}

if (level_is_oob(l, x, y)) {
return false;
}
if (tile) {
l->obj[ x ][ y ][ slot ].tile = tile_global_index(tile);
} else {
l->obj[ x ][ y ][ slot ].tile = 0;

auto id = l->thing_id[ x ][ y ][ tp_z_depth_get(tp) ];
if (! id) {
return false;
}
return true;
}

void level_set_tile_no_check(Levelp l, int x, int y, uint8_t slot, Tilep tile)
{
l->obj[ x ][ y ][ slot ].tile = tile_global_index(tile);
return id == tp_id_get(tp);
}

Tilep level_get_tile(Levelp l, int x, int y, uint8_t slot)
void level_bounds_set(Levelp l)
{
if (level_is_oob(l, x, y)) {
return nullptr;
TRACE_NO_INDENT();

auto dw = TILE_WIDTH;
auto dh = TILE_HEIGHT;

//
// The number of tiles additionally to draw to avoid clipping
//
auto clipping_border = 2;

//
// Set the scroll bounds
//
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;
}
return tile_index_to_tile(l->obj[ x ][ y ][ slot ].tile);
}

Tilep level_get_tile_no_check(Levelp l, int x, int y, uint8_t slot)
{
return tile_index_to_tile(l->obj[ x ][ y ][ slot ].tile);
//
// Square map
//
auto max_pix_x = (MAP_WIDTH * dw) - game_pix_height_get(game);
auto 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_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;
tmp_minx -= clipping_border;
tmp_minx -= clipping_border;

if (tmp_minx < 0) {
tmp_minx = 0;
}
if (tmp_miny < 0) {
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;

tmp_maxx += clipping_border;
tmp_maxy += clipping_border;

if (tmp_maxx >= MAP_WIDTH) {
tmp_maxx = MAP_WIDTH;
}
if (tmp_maxy >= MAP_HEIGHT) {
tmp_maxy = MAP_HEIGHT;
}

l->minx = tmp_minx;
l->miny = tmp_miny;
l->maxx = tmp_maxx;
l->maxy = tmp_maxy;
}
31 changes: 15 additions & 16 deletions src/level_anim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ 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;
level_thing_or_tp_get(l, x, y, slot, &tp);
if (! tp) {
Tpp tp;
Thingp t = level_thing_and_tp_get(l, x, y, slot, &tp);
if (! t) {
continue;
}

auto obj = &l->obj[ x ][ y ][ slot ];
auto tile_index = obj->tile;
auto tile_index = t->tile_index;
if (! tile_index) {
continue;
}
Expand All @@ -51,24 +50,24 @@ void level_anim(Levelp l)
//
// Decrement the remaining time
//
if (obj->anim_ms_remaining > 0) {
obj->anim_ms_remaining -= time_step;
if (obj->anim_ms_remaining > 0) {
if (t->anim_ms_remaining > 0) {
t->anim_ms_remaining -= time_step;
if (t->anim_ms_remaining > 0) {
continue;
}
}

obj->anim_index++;
if (obj->anim_index >= tp_tiles_size(tp)) {
obj->anim_index = 0;
t->anim_index++;
if (t->anim_index >= tp_tiles_size(tp)) {
t->anim_index = 0;
}

tile = tp_tiles_get(tp, obj->anim_index);
obj->tile = tile_global_index(tile);
tile = tp_tiles_get(tp, t->anim_index);
t->tile_index = tile_global_index(tile);

obj->anim_ms_remaining += tile_delay_ms(tile);
if (obj->anim_ms_remaining < 0) {
obj->anim_ms_remaining = tile_delay_ms(tile);
t->anim_ms_remaining += tile_delay_ms(tile);
if (t->anim_ms_remaining < 0) {
t->anim_ms_remaining = tile_delay_ms(tile);
}
}
}
Expand Down
Loading

0 comments on commit 16ba5a0

Please sign in to comment.