changeset 384:c458441ff472

rpg: more asserts, const and cleanups
author David Demelier <markand@malikania.fr>
date Sun, 13 Feb 2022 11:34:19 +0100
parents b944cd41e8f9
children 3f13dc6c0e37
files src/libmlk-rpg/CMakeLists.txt src/libmlk-rpg/rpg/battle-entity-state-attacking.c src/libmlk-rpg/rpg/battle-entity-state-blinking.c src/libmlk-rpg/rpg/battle-entity-state-moving.c src/libmlk-rpg/rpg/battle-entity-state-moving.h src/libmlk-rpg/rpg/battle-entity-state-normal.c src/libmlk-rpg/rpg/battle-entity-state.c src/libmlk-rpg/rpg/battle-state-ai.c src/libmlk-rpg/rpg/battle-state-attacking.c src/libmlk-rpg/rpg/battle-state-check.c src/libmlk-rpg/rpg/battle-state-closing.c src/libmlk-rpg/rpg/battle-state-closing.h src/libmlk-rpg/rpg/battle-state-item.c src/libmlk-rpg/rpg/battle-state-item.h src/libmlk-rpg/rpg/battle-state-lost.c src/libmlk-rpg/rpg/battle-state-menu.c src/libmlk-rpg/rpg/battle-state-opening.c src/libmlk-rpg/rpg/battle-state-opening.h src/libmlk-rpg/rpg/battle-state-selection.c src/libmlk-rpg/rpg/battle-state-selection.h src/libmlk-rpg/rpg/battle-state-sub.c src/libmlk-rpg/rpg/battle-state-victory.c src/libmlk-rpg/rpg/battle-state-victory.h
diffstat 23 files changed, 71 insertions(+), 59 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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 <core/panic.h>
 #include <core/sprite.h>
 
-#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;
--- 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 <core/sprite.h>
 #include <core/texture.h>
 
-#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;
 
--- 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 <core/alloc.h>
 #include <core/panic.h>
 
-#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;
 
--- 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 <rpg/walksprite.h>
 
+struct battle_entity;
+
 struct battle_entity_state_moving {
 	struct walksprite ws;
 	int x;
--- 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 <assert.h>
 
-#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;
--- 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);
--- 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 <core/alloc.h>
 
-#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
--- 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 <core/panic.h>
 #include <core/sprite.h>
 
-#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;
--- 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 <core/sprite.h>
 #include <core/texture.h>
 
-#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 {
--- 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 <core/texture.h>
 #include <core/window.h>
 
+#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);
 
--- 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 *);
--- 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 <rpg/inventory.h>
 #include <rpg/item.h>
 
-#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);
 
--- 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;
 };
--- 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 <core/panic.h>
 #include <core/window.h>
 
-#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 {
--- 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 <core/alloc.h>
 
-#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) {
--- 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 <core/panic.h>
 #include <core/window.h>
 
-#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;
--- 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 *);
--- 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 <ui/theme.h>
 
-#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;
 
--- 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 *);
--- 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 <rpg/inventory.h>
 #include <rpg/item.h>
 
-#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"
 
--- 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 <core/panic.h>
 #include <core/window.h>
 
-#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);
 
--- 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 *);