# HG changeset patch # User David Demelier # Date 1644748459 -3600 # Node ID c458441ff472f73a10864674edaedf7aab4877fc # Parent b944cd41e8f9a4c6b7bd6e88608403c4dcf00c61 rpg: more asserts, const and cleanups diff -r b944cd41e8f9 -r c458441ff472 src/libmlk-rpg/CMakeLists.txt --- a/src/libmlk-rpg/CMakeLists.txt Sun Feb 13 11:13:17 2022 +0100 +++ b/src/libmlk-rpg/CMakeLists.txt Sun Feb 13 11:34:19 2022 +0100 @@ -57,7 +57,9 @@ ${libmlk-rpg_SOURCE_DIR}/rpg/battle-state-selection.c ${libmlk-rpg_SOURCE_DIR}/rpg/battle-state-selection.h ${libmlk-rpg_SOURCE_DIR}/rpg/battle-state-sub.c + ${libmlk-rpg_SOURCE_DIR}/rpg/battle-state-sub.h ${libmlk-rpg_SOURCE_DIR}/rpg/battle-state-victory.c + ${libmlk-rpg_SOURCE_DIR}/rpg/battle-state-victory.h ${libmlk-rpg_SOURCE_DIR}/rpg/battle-state.c ${libmlk-rpg_SOURCE_DIR}/rpg/battle-state.h ${libmlk-rpg_SOURCE_DIR}/rpg/battle.c diff -r b944cd41e8f9 -r c458441ff472 src/libmlk-rpg/rpg/battle-entity-state-attacking.c --- a/src/libmlk-rpg/rpg/battle-entity-state-attacking.c Sun Feb 13 11:13:17 2022 +0100 +++ b/src/libmlk-rpg/rpg/battle-entity-state-attacking.c Sun Feb 13 11:34:19 2022 +0100 @@ -24,9 +24,9 @@ #include #include -#include "battle-entity.h" +#include "battle-entity-state-attacking.h" #include "battle-entity-state.h" -#include "battle-entity-state-attacking.h" +#include "battle-entity.h" struct self { struct battle_entity_state_attacking data; diff -r b944cd41e8f9 -r c458441ff472 src/libmlk-rpg/rpg/battle-entity-state-blinking.c --- a/src/libmlk-rpg/rpg/battle-entity-state-blinking.c Sun Feb 13 11:13:17 2022 +0100 +++ b/src/libmlk-rpg/rpg/battle-entity-state-blinking.c Sun Feb 13 11:34:19 2022 +0100 @@ -24,9 +24,9 @@ #include #include -#include "battle-entity.h" +#include "battle-entity-state-blinking.h" #include "battle-entity-state.h" -#include "battle-entity-state-blinking.h" +#include "battle-entity.h" #include "character.h" #define TRANSPARENT (150) @@ -83,7 +83,7 @@ void battle_entity_state_blinking(struct battle_entity *et) { - assert(et); + assert(battle_entity_ok(et)); struct self *self; diff -r b944cd41e8f9 -r c458441ff472 src/libmlk-rpg/rpg/battle-entity-state-moving.c --- a/src/libmlk-rpg/rpg/battle-entity-state-moving.c Sun Feb 13 11:13:17 2022 +0100 +++ b/src/libmlk-rpg/rpg/battle-entity-state-moving.c Sun Feb 13 11:34:19 2022 +0100 @@ -23,9 +23,9 @@ #include #include -#include "battle-entity.h" +#include "battle-entity-state-moving.h" #include "battle-entity-state.h" -#include "battle-entity-state-moving.h" +#include "battle-entity.h" #include "character.h" #include "walksprite.h" @@ -70,7 +70,7 @@ battle_entity_state_moving_init(struct battle_entity_state_moving *mv, struct battle_entity *et, int dstx, int dsty) { assert(mv); - assert(et); + assert(battle_entity_ok(et)); walksprite_init(&mv->ws, et->ch->sprites[CHARACTER_SPRITE_NORMAL], 40); mv->x = dstx; @@ -81,7 +81,7 @@ battle_entity_state_moving_update(struct battle_entity_state_moving *mv, struct battle_entity *et, unsigned int ticks) { assert(mv); - assert(et); + assert(battle_entity_ok(et)); int step_x, step_y, delta_x, delta_y; @@ -114,7 +114,7 @@ void battle_entity_state_moving(struct battle_entity *et, int dstx, int dsty) { - assert(et); + assert(battle_entity_ok(et)); struct self *self; diff -r b944cd41e8f9 -r c458441ff472 src/libmlk-rpg/rpg/battle-entity-state-moving.h --- a/src/libmlk-rpg/rpg/battle-entity-state-moving.h Sun Feb 13 11:13:17 2022 +0100 +++ b/src/libmlk-rpg/rpg/battle-entity-state-moving.h Sun Feb 13 11:34:19 2022 +0100 @@ -21,6 +21,8 @@ #include +struct battle_entity; + struct battle_entity_state_moving { struct walksprite ws; int x; diff -r b944cd41e8f9 -r c458441ff472 src/libmlk-rpg/rpg/battle-entity-state-normal.c --- a/src/libmlk-rpg/rpg/battle-entity-state-normal.c Sun Feb 13 11:13:17 2022 +0100 +++ b/src/libmlk-rpg/rpg/battle-entity-state-normal.c Sun Feb 13 11:34:19 2022 +0100 @@ -18,16 +18,16 @@ #include -#include "battle-entity.h" +#include "battle-entity-state-normal.h" #include "battle-entity-state.h" -#include "battle-entity-state-normal.h" +#include "battle-entity.h" /* TODO: animate characters when they are inactive. */ void battle_entity_state_normal(struct battle_entity *et) { - assert(et); + assert(battle_entity_ok(et)); /* Not needed yet. */ static struct battle_entity_state st; diff -r b944cd41e8f9 -r c458441ff472 src/libmlk-rpg/rpg/battle-entity-state.c --- a/src/libmlk-rpg/rpg/battle-entity-state.c Sun Feb 13 11:13:17 2022 +0100 +++ b/src/libmlk-rpg/rpg/battle-entity-state.c Sun Feb 13 11:34:19 2022 +0100 @@ -25,7 +25,7 @@ battle_entity_state_update(struct battle_entity_state *st, struct battle_entity *et, unsigned int ticks) { assert(st); - assert(et); + assert(battle_entity_ok(et)); if (st->update) return st->update(st, et, ticks); @@ -37,6 +37,7 @@ battle_entity_state_draw(const struct battle_entity_state *st, const struct battle_entity *et) { assert(st); + assert(battle_entity_ok(et)); if (st->draw) st->draw(st, et); @@ -47,7 +48,7 @@ void battle_entity_state_finish(struct battle_entity_state *st, struct battle_entity *et) { - assert(et); + assert(battle_entity_ok(et)); if (st->finish) st->finish(st, et); diff -r b944cd41e8f9 -r c458441ff472 src/libmlk-rpg/rpg/battle-state-ai.c --- a/src/libmlk-rpg/rpg/battle-state-ai.c Sun Feb 13 11:13:17 2022 +0100 +++ b/src/libmlk-rpg/rpg/battle-state-ai.c Sun Feb 13 11:34:19 2022 +0100 @@ -21,9 +21,9 @@ #include -#include "battle.h" +#include "battle-state-ai.h" #include "battle-state.h" -#include "battle-state-ai.h" +#include "battle.h" #include "character.h" static int diff -r b944cd41e8f9 -r c458441ff472 src/libmlk-rpg/rpg/battle-state-attacking.c --- a/src/libmlk-rpg/rpg/battle-state-attacking.c Sun Feb 13 11:13:17 2022 +0100 +++ b/src/libmlk-rpg/rpg/battle-state-attacking.c Sun Feb 13 11:34:19 2022 +0100 @@ -23,15 +23,15 @@ #include #include -#include "battle.h" -#include "battle-state.h" -#include "battle-state-attacking.h" -#include "battle-state-check.h" -#include "battle-entity-state.h" #include "battle-entity-state-attacking.h" #include "battle-entity-state-blinking.h" #include "battle-entity-state-moving.h" #include "battle-entity-state-normal.h" +#include "battle-entity-state.h" +#include "battle-state-attacking.h" +#include "battle-state-check.h" +#include "battle-state.h" +#include "battle.h" #include "character.h" struct self { @@ -75,8 +75,8 @@ struct battle_entity *target) { assert(atk); - assert(source); - assert(target); + assert(battle_entity_ok(source)); + assert(battle_entity_ok(target)); int x, y; @@ -101,6 +101,9 @@ int battle_state_attacking_update(struct battle_state_attacking *atk, struct battle *bt) { + assert(atk); + assert(bt); + if (!battle_entity_update(atk->source, 0)) return 0; @@ -139,8 +142,8 @@ void battle_state_attacking(struct battle_entity *source, struct battle_entity *target, struct battle *bt) { - assert(source); - assert(target); + assert(battle_entity_ok(source)); + assert(battle_entity_ok(target)); assert(bt); struct self *self; diff -r b944cd41e8f9 -r c458441ff472 src/libmlk-rpg/rpg/battle-state-check.c --- a/src/libmlk-rpg/rpg/battle-state-check.c Sun Feb 13 11:13:17 2022 +0100 +++ b/src/libmlk-rpg/rpg/battle-state-check.c Sun Feb 13 11:34:19 2022 +0100 @@ -25,11 +25,11 @@ #include #include -#include "battle.h" -#include "battle-state.h" #include "battle-state-check.h" #include "battle-state-lost.h" #include "battle-state-victory.h" +#include "battle-state.h" +#include "battle.h" #include "character.h" struct fadeout { diff -r b944cd41e8f9 -r c458441ff472 src/libmlk-rpg/rpg/battle-state-closing.c --- a/src/libmlk-rpg/rpg/battle-state-closing.c Sun Feb 13 11:13:17 2022 +0100 +++ b/src/libmlk-rpg/rpg/battle-state-closing.c Sun Feb 13 11:34:19 2022 +0100 @@ -27,8 +27,8 @@ #include #include +#include "battle-state-closing.h" #include "battle.h" -#include "battle-state-closing.h" struct self { struct battle_state_closing data; @@ -105,7 +105,7 @@ } void -battle_state_closing_draw(struct battle_state_closing *cls) +battle_state_closing_draw(const struct battle_state_closing *cls) { assert(cls); diff -r b944cd41e8f9 -r c458441ff472 src/libmlk-rpg/rpg/battle-state-closing.h --- a/src/libmlk-rpg/rpg/battle-state-closing.h Sun Feb 13 11:13:17 2022 +0100 +++ b/src/libmlk-rpg/rpg/battle-state-closing.h Sun Feb 13 11:34:19 2022 +0100 @@ -36,7 +36,7 @@ battle_state_closing_update(struct battle_state_closing *, unsigned int); void -battle_state_closing_draw(struct battle_state_closing *); +battle_state_closing_draw(const struct battle_state_closing *); void battle_state_closing_finish(struct battle_state_closing *); diff -r b944cd41e8f9 -r c458441ff472 src/libmlk-rpg/rpg/battle-state-item.c --- a/src/libmlk-rpg/rpg/battle-state-item.c Sun Feb 13 11:13:17 2022 +0100 +++ b/src/libmlk-rpg/rpg/battle-state-item.c Sun Feb 13 11:34:19 2022 +0100 @@ -25,12 +25,12 @@ #include #include -#include "battle-entity-state.h" #include "battle-entity-state-moving.h" #include "battle-entity-state-normal.h" +#include "battle-entity-state.h" #include "battle-message.h" +#include "battle-state-item.h" #include "battle-state.h" -#include "battle-state-item.h" #include "battle.h" struct self { @@ -69,8 +69,8 @@ { assert(it); assert(bt); - assert(source); - assert(target); + assert(battle_entity_ok(source)); + assert(battle_entity_ok(target)); assert(slot); it->source = source; @@ -130,8 +130,8 @@ struct battle_entity *target, struct inventory_slot *slot) { - assert(source); - assert(target); + assert(battle_entity_ok(source)); + assert(battle_entity_ok(target)); assert(slot); assert(bt); diff -r b944cd41e8f9 -r c458441ff472 src/libmlk-rpg/rpg/battle-state-item.h --- a/src/libmlk-rpg/rpg/battle-state-item.h Sun Feb 13 11:13:17 2022 +0100 +++ b/src/libmlk-rpg/rpg/battle-state-item.h Sun Feb 13 11:34:19 2022 +0100 @@ -36,7 +36,6 @@ struct battle_message msg; struct battle_entity *source; struct battle_entity *target; - struct battle_state state; struct inventory_slot *slot; int origin_x; }; diff -r b944cd41e8f9 -r c458441ff472 src/libmlk-rpg/rpg/battle-state-lost.c --- a/src/libmlk-rpg/rpg/battle-state-lost.c Sun Feb 13 11:13:17 2022 +0100 +++ b/src/libmlk-rpg/rpg/battle-state-lost.c Sun Feb 13 11:34:19 2022 +0100 @@ -23,10 +23,10 @@ #include #include -#include "battle.h" -#include "battle-state.h" #include "battle-state-closing.h" #include "battle-state-lost.h" +#include "battle-state.h" +#include "battle.h" #include "rpg_p.h" struct self { diff -r b944cd41e8f9 -r c458441ff472 src/libmlk-rpg/rpg/battle-state-menu.c --- a/src/libmlk-rpg/rpg/battle-state-menu.c Sun Feb 13 11:13:17 2022 +0100 +++ b/src/libmlk-rpg/rpg/battle-state-menu.c Sun Feb 13 11:34:19 2022 +0100 @@ -21,12 +21,12 @@ #include -#include "battle.h" #include "battle-bar.h" -#include "battle-state.h" #include "battle-state-menu.h" #include "battle-state-selection.h" #include "battle-state-sub.h" +#include "battle-state.h" +#include "battle.h" #include "character.h" #include "spell.h" @@ -75,6 +75,7 @@ battle_state_menu_handle(struct battle *bt, const union event *ev) { assert(bt); + assert(ev); if (battle_bar_handle(&bt->bar, bt, ev)) { switch (bt->bar.menu) { diff -r b944cd41e8f9 -r c458441ff472 src/libmlk-rpg/rpg/battle-state-opening.c --- a/src/libmlk-rpg/rpg/battle-state-opening.c Sun Feb 13 11:13:17 2022 +0100 +++ b/src/libmlk-rpg/rpg/battle-state-opening.c Sun Feb 13 11:34:19 2022 +0100 @@ -24,10 +24,10 @@ #include #include -#include "battle.h" -#include "battle-state.h" #include "battle-state-check.h" #include "battle-state-opening.h" +#include "battle-state.h" +#include "battle.h" #define DELAY (1000U) @@ -48,7 +48,9 @@ static void draw(const struct battle_state *st, const struct battle *bt) { - battle_state_opening_draw(st->data, bt); + (void)bt; + + battle_state_opening_draw(st->data); } static void @@ -75,10 +77,8 @@ } void -battle_state_opening_draw(const struct battle_state_opening *op, const struct battle *bt) +battle_state_opening_draw(const struct battle_state_opening *op) { - (void)bt; - const unsigned int w = window.w; const unsigned int h = window.h / 2; const unsigned int ch = op->elapsed * h / DELAY; diff -r b944cd41e8f9 -r c458441ff472 src/libmlk-rpg/rpg/battle-state-opening.h --- a/src/libmlk-rpg/rpg/battle-state-opening.h Sun Feb 13 11:13:17 2022 +0100 +++ b/src/libmlk-rpg/rpg/battle-state-opening.h Sun Feb 13 11:34:19 2022 +0100 @@ -29,7 +29,7 @@ battle_state_opening_update(struct battle_state_opening *, struct battle *, unsigned int); void -battle_state_opening_draw(const struct battle_state_opening *, const struct battle *); +battle_state_opening_draw(const struct battle_state_opening *); void battle_state_opening(struct battle *); diff -r b944cd41e8f9 -r c458441ff472 src/libmlk-rpg/rpg/battle-state-selection.c --- a/src/libmlk-rpg/rpg/battle-state-selection.c Sun Feb 13 11:13:17 2022 +0100 +++ b/src/libmlk-rpg/rpg/battle-state-selection.c Sun Feb 13 11:34:19 2022 +0100 @@ -28,13 +28,13 @@ #include -#include "battle.h" #include "battle-bar.h" -#include "battle-state.h" #include "battle-state-item.h" #include "battle-state-menu.h" #include "battle-state-selection.h" #include "battle-state-sub.h" +#include "battle-state.h" +#include "battle.h" #include "character.h" #include "inventory.h" #include "selection.h" @@ -252,8 +252,11 @@ } void -battle_state_selection_draw(struct battle_state_selection *stl, const struct battle *bt) +battle_state_selection_draw(const struct battle_state_selection *stl, const struct battle *bt) { + assert(stl); + assert(bt); + if (stl->select.index_character == -1U) { /* All selected. */ if (stl->select.index_side == 0) @@ -273,6 +276,7 @@ battle_state_selection(struct battle *bt, const struct selection *select) { assert(bt); + assert(select); struct self *self; diff -r b944cd41e8f9 -r c458441ff472 src/libmlk-rpg/rpg/battle-state-selection.h --- a/src/libmlk-rpg/rpg/battle-state-selection.h Sun Feb 13 11:13:17 2022 +0100 +++ b/src/libmlk-rpg/rpg/battle-state-selection.h Sun Feb 13 11:34:19 2022 +0100 @@ -36,7 +36,7 @@ battle_state_selection_handle(struct battle_state_selection *, struct battle *, const union event *); void -battle_state_selection_draw(struct battle_state_selection *, const struct battle *); +battle_state_selection_draw(const struct battle_state_selection *, const struct battle *); void battle_state_selection(struct battle *, const struct selection *); diff -r b944cd41e8f9 -r c458441ff472 src/libmlk-rpg/rpg/battle-state-sub.c --- a/src/libmlk-rpg/rpg/battle-state-sub.c Sun Feb 13 11:13:17 2022 +0100 +++ b/src/libmlk-rpg/rpg/battle-state-sub.c Sun Feb 13 11:34:19 2022 +0100 @@ -29,12 +29,12 @@ #include #include -#include "battle.h" #include "battle-bar.h" -#include "battle-state.h" #include "battle-state-menu.h" #include "battle-state-selection.h" #include "battle-state-sub.h" +#include "battle-state.h" +#include "battle.h" #include "character.h" #include "spell.h" diff -r b944cd41e8f9 -r c458441ff472 src/libmlk-rpg/rpg/battle-state-victory.c --- a/src/libmlk-rpg/rpg/battle-state-victory.c Sun Feb 13 11:13:17 2022 +0100 +++ b/src/libmlk-rpg/rpg/battle-state-victory.c Sun Feb 13 11:34:19 2022 +0100 @@ -23,10 +23,10 @@ #include #include -#include "battle.h" -#include "battle-state.h" #include "battle-state-closing.h" #include "battle-state-victory.h" +#include "battle-state.h" +#include "battle.h" #include "rpg_p.h" struct self { @@ -105,7 +105,7 @@ } void -battle_state_victory_draw(struct battle_state_victory *vic) +battle_state_victory_draw(const struct battle_state_victory *vic) { assert(vic); diff -r b944cd41e8f9 -r c458441ff472 src/libmlk-rpg/rpg/battle-state-victory.h --- a/src/libmlk-rpg/rpg/battle-state-victory.h Sun Feb 13 11:13:17 2022 +0100 +++ b/src/libmlk-rpg/rpg/battle-state-victory.h Sun Feb 13 11:34:19 2022 +0100 @@ -39,7 +39,7 @@ battle_state_victory_update(struct battle_state_victory *, struct battle *, unsigned int); void -battle_state_victory_draw(struct battle_state_victory *); +battle_state_victory_draw(const struct battle_state_victory *); void battle_state_victory(struct battle *);