Skip to content

Commit

Permalink
level api
Browse files Browse the repository at this point in the history
  • Loading branch information
goblinhack committed Apr 28, 2024
1 parent 3dd51e8 commit 580d080
Show file tree
Hide file tree
Showing 37 changed files with 752 additions and 870 deletions.
58 changes: 23 additions & 35 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class Game
//
// Current level
//
class Level *level {};
Levelp level {};

//
// Used to check for changes in the size of this struct.
Expand Down Expand Up @@ -190,6 +190,7 @@ class Game
Game(void) = default;

void create_level(void);
void destroy_level(void);
void display(void);
void fini(void);
void init(void);
Expand All @@ -198,15 +199,13 @@ class Game
void load_snapshot(void);
void load(uint8_t slot);
void load(void);
void new_game(void);
void save_config(void);
void save_snapshot_check();
void save_snapshot(void);
void save(uint8_t slot);
void save(void);
void set_currently_saving_snapshot(void);
void set_seed(void);
void start(void);
void state_change(uint8_t state, const std::string &);
void state_reset(const std::string &);
bool load(std::string save_file, class Game &target);
Expand Down Expand Up @@ -287,31 +286,21 @@ Game::Game(std::string appdata)
save_file = saved_dir + "saved-slot-" + std::to_string(save_slot);
}

void Game::start(void) { TRACE_NO_INDENT(); }

void Game::init(void)
{
LOG("Game init");
TRACE_AND_INDENT();

set_seed();
start();
}

void game_init(class Game *game) { game->init(); }

void Game::fini(void)
{
LOG("Game fini");
TRACE_AND_INDENT();

if (level) {
auto l = level;
level = nullptr;
delete l;
}
destroy_level();
}

void game_fini(class Game *game) { game->fini(); }

void game_save_config(class Game *game) { game->save_config(); }
Expand All @@ -336,36 +325,37 @@ void Game::create_level(void)
TRACE_AND_INDENT();

set_seed();

if (level) {
TRACE_NO_INDENT();

auto l = level;
if (l) {
LOG("Remove old level");
delete l;
level = nullptr;
}
}
destroy_level();

{
TRACE_NO_INDENT();
LOG("Level create");
level = new Level();
level = level_constructor();
}
}

void game_create_level(class Game *game) { game->create_level(); }

void Game::display(void)
void Game::destroy_level(void)
{
LOG("Game destroy level");
TRACE_AND_INDENT();

if (level) {
level->tick();
level->anim();
level->display();
auto l = level;
level_destructor(l);
level = nullptr;
}
}
void game_destroy_level(class Game *game) { game->destroy_level(); }

void Game::display(void)
{
if (level) {
level_tick(level);
level_anim(level);
level_display(level);
}
}
void game_display(class Game *game) { game->display(); }

std::string gama_state_to_string(int state)
Expand All @@ -392,7 +382,6 @@ void Game::state_reset(const std::string &why)
state_change(STATE_MAIN_MENU, why);
}
}

void game_state_reset(class Game *game, const char *why) { game->state_reset(why); }

void Game::state_change(uint8_t new_state, const std::string &why)
Expand Down Expand Up @@ -444,7 +433,6 @@ void Game::state_change(uint8_t new_state, const std::string &why)
//
state = new_state;
}

void game_state_change(class Game *game, uint8_t new_state, const char *why)
{
game->state_change(new_state, std::string(why));
Expand Down Expand Up @@ -547,8 +535,8 @@ void game_sdl_delay_set(class Game *, uint32_t val) { game->config.sdl_delay
uint32_t game_sound_volume_get(class Game *) { return game->config.sound_volume; }
void game_sound_volume_set(class Game *, uint32_t val) { game->config.sound_volume = val; }

class Level *game_level_get(class Game *game) { return game->level; }
void game_level_set(class Game *game, class Level *val) { game->level = val; }
Levelp game_level_get(class Game *game) { return game->level; }
void game_level_set(class Game *game, Levelp val) { game->level = val; }

const char *game_seed_name_get(class Game *game) { return game->seed_name.c_str(); }
void game_seed_name_set(class Game *game, const char *val) { game->seed_name = std::string(val); }
Expand Down
12 changes: 6 additions & 6 deletions src/game_events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ uint8_t game_mouse_motion(class Game *game, int x, int y, int relx, int rely, in
return false;
}

auto level = game_level_get(game);
if (level) {
level->scroll_delta(wheelx, wheely);
auto l = game_level_get(game);
if (l) {
level_scroll_delta(l, wheelx, wheely);
}

return true;
Expand All @@ -59,9 +59,9 @@ uint8_t game_input(class Game *game, const SDL_Keysym *key)
return false;
}

auto level = game_level_get(game);
if (! level) {
DBG("INF: Pressed a key; no level");
auto l = game_level_get(game);
if (! l) {
DBG("INF: Pressed a key; no l");
return false;
}

Expand Down
173 changes: 162 additions & 11 deletions src/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,177 @@

#include "my_callstack.hpp"
#include "my_level.hpp"
#include "my_ptrcheck.hpp"

Level::Level()
#if 0
void level_map_constructor(Levelp l)
{
TRACE_NO_INDENT();

data = level_data_constructor();
for (;;) {

dungeon_create_and_place();
assign_tiles();
thing_player_create_and_place();
scroll_warp_to_player();
#if 0
/*
* Phase 1: create a framework for the level solution e.g.
*
* v v * v
* 1----1----1---S1
* > <|> <|> <|
* | | |
* | | |
* | *|v *|
* s. 1----1----1
* ^?> <|> <
* ? |
* ? |
* v ? *| * v
* 2???s1???K1---D2
* | ^ > <|
* | |
* | |
* | * * *|
* 2---E2----2----2
* ^ > < < <
*/
auto ph1 = level_ph1();
if (! ph1.ok) {
CON("COULD NOT SOLVE PH1");
continue;
}
ph1.dump();

/*
* Phase 2: create a set of rooms that satisfy the solution e.g.:
*
* 1111111111 1........1 1111111111 1111111111
* 1111111111 1........1 1111111111 1111111111
* 11........ 11...S..11 11......11 ........11
* 11........ 11..111.11 11......11 ........11
* 11........ 11..111.11 11......11 ........11
* 11........ .......... .......... ........11
* 11111..111 1111111111 11..111111 11..111111
* 111111.111 1111111111 111.111111 11.1111111
*
* 111111s111 1111111111 111.111111 11.1111111
* 111111..11 1111111111 111.111111 11.1111111
* 11......11 11........ 11......11 11......11
* 11......11 11........ 11......11 11......11
* 11......11 11........ 11......11 11......11
* 11......11 11........ .......... ........11
* 111...1111 1111111111 1111..1111 11111..111
* 1111s11111 1111111111 11111.1111 11111.1111
*
* 1111.11111 1111111111 11111.1111 11111.1111
* 1111.11111 1111111111 11111.1111 1111....11
* 11......11 11......11 11......11 11......11
* 11......11 11......11 11......11 11......11
* 11......11 11......11 11......11 11......11
* 11........ .......... ........11 11......11
* ...1111111 1111111111 1111111... 1111111111
* .111111111 1111111111 111111111. 1111111111
*
* ..11111111 1111111111 111111111. 1111111111
* ....111111 1111111111 111111111. 1111111111
* 11......11 11........ 11........ ........11
* 11......11 11........ 11......11 ........11
* 11......11 11...E.... 11......11 ........11
* 11......11 11..111... .......... ........11
* 1111111111 1111111111 1111111111 1111111111
* 1111111111 1111111111 1111111111 1111111111
*/
auto ph2 = level_ph2(ph1);
if (! ph2.ok) {
CON("COULD NOT SOLVE PH2");
continue;
}
ph2.dump();

/*
* Phase 3: Blocks joined and obstacles merged
*
* RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
* R11111...k1........1.11111111111111111111R
* R......1111.111..S...11111111111111111111R
* R...............111.111......11........11R
* R.111...1..11.......111................11R
* R.111................11................11R
* R.....1111.................1...........11R
* R1.........11111...1.1111.11|.11111111..1R
* R1111......1111111111111111111111111111.1R
* R1......H..1111111111111111111111111111.1R
* R111...1H111.......11111111111111111111.1R
* R1......H....11.11..111........11.......1R
* R11111..H...........111........11......11R
* R1...1..H....11111.1111........11......11R
* R1...D..H...........11.................11R
* R1..11111111........1...11111111..1111111R
* R1.111111111111111111.1111111111.11111111R
* R..111111111111111111.1111111111.11111111R
* R...11111111111111111...11111111.11111111R
* R1.........11......1111......111.......11R
* R11........11......1111......1111......11R
* R11........11......1111......1111......11R
* R11....................................11R
* R11111111111111111..11111111111111111..11R
* R11111111111111111.111111111111111111.111R
* R11111111111111111.111111111111111111s111R
* R11......111111111.111111111111111111.111R
* R1........111......1111......111.......11R
* R1.........11......1111......111.......11R
* R1E........11......1111......111.......11R
* R1111..........................s.......11R
* R11.1.1111.111111111111111111111111111111R
* R11.1......111111111111111111111111111111R
* RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
*/
auto ph3 = level_ph3(ph2);
if (! ph3.ok) {
CON("COULD NOT SOLVE PH3");
continue;
}

/*
* Phase 4: replace all chars with objects
*/
auto ph4 = level_ph4(ph3);
if (! ph4.ok) {
CON("COULD NOT SOLVE PH4");
continue;
}
#endif

#if 0
*level = ph4.data;
#endif

return level;
}
}
#endif

Level::~Level()
Levelp level_constructor(void)
{
TRACE_NO_INDENT();

auto d = data;
if (d) {
data = nullptr;
level_data_destructor(d);
//
// 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);
}
10 changes: 5 additions & 5 deletions src/level_anim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "my_time.hpp"
#include "my_tp.hpp"

void Level::anim(void)
void level_anim(Levelp l)
{
TRACE_NO_INDENT();

Expand All @@ -25,15 +25,15 @@ void Level::anim(void)
last_ts = ts;

for (auto slot = 0; slot < MAP_SLOTS; slot++) {
for (auto y = miny; y < maxy; y++) {
for (auto x = minx; x < maxx; x++) {
for (auto y = l->miny; y < l->maxy; y++) {
for (auto x = l->minx; x < l->maxx; x++) {
Tpp tp;
thing_get(x, y, slot, &tp);
level_thing_get(l, x, y, slot, &tp);
if (! tp) {
continue;
}

auto obj = &data->obj[ x ][ y ][ slot ];
auto obj = &l->obj[ x ][ y ][ slot ];
auto tile_index = obj->tile;
if (! tile_index) {
continue;
Expand Down
Loading

0 comments on commit 580d080

Please sign in to comment.