added game restart

AuthorKonata <konata@posteo.jp>
Date
Commit0994cd0eb149aed8e3d4819aa3d57aaeaccfad5a
Parentdf62e72
7 files changed, 75 insertions(+), 73 deletions(-)
Minclude/algorithm.h
@@ -3,6 +3,7 @@
33 #include <stddef.h>
44 #include <game.h>
55
6+//creates a game, fills it with walls and empty fields
67 struct game make_maze(size_t y, size_t x);
78 typedef darray(struct point) pointarr;
89
Minclude/game.h
@@ -141,6 +141,7 @@ struct choice
141141 }u;
142142 };
143143
144+//returns a point that is relative to p in the direction d
144145 static inline struct point relative(struct point p, enum direction d)
145146 {
146147 switch(d)
@@ -166,7 +167,8 @@ static inline bool in_bounds(struct game *g, struct point p)
166167 return false;
167168 }
168169
169-static inline void spawn_enemy(struct game *g)
170+//randomly puts an enemy in an empty spot on the map
171+static inline void spawn_enemy(struct game *g)
170172 {
171173 while(true)
172174 {
@@ -179,5 +181,6 @@ static inline void spawn_enemy(struct game *g)
179181 }
180182 }
181183
184+void loop(); //main game loop
182185
183186 #endif
Dinclude/loop.h
-7
@@ -1,7 +0,0 @@
1-#ifndef LOOP_H
2-#define LOOP_H
3-
4-void loop(struct game *g);
5-
6-
7-#endif //LOOP_H
Minclude/tui.h
@@ -6,9 +6,9 @@
66 #include <stddef.h>
77
88 bool setup(); //setups screens, doesn't redaw contents
9-void printlog();
9+void printlog(); //clears & reprints the log window
1010 void logstr(const char *format, ...); //echoes printf string to the logwindow TODO: fix multiline log
11-void charclear();
11+void charclear(); //clears the character screen
1212 void charprint(const char *format, ...); //echoes printf string to the charwindow
1313 void log_scroll(enum direction d); //scrolls the log up or down
1414 void draw_map(const struct game *g, struct point p); //draws the map around the player
@@ -18,8 +18,4 @@ typedef bool(itemselectfn)(struct monster *m, size_t i); //returns true if curso
1818 void itemselect(struct monster *m, itemselectfn f); //provides an inventory screen, calls f when the space is pressed on an item
1919 //TODO: create general select screen
2020
21-extern size_t logindex;
22-extern size_t loglength;
23-extern char **logs;
24-
2521 #endif //TUI_H
Msrc/algorithm.c
@@ -31,10 +31,10 @@ static bool maze_in_bounds(size_t ys, size_t xs, struct point p)
3131
3232 struct game make_maze(size_t y, size_t x)
3333 {
34- bool yeven = (y%2 == 0);
35- bool xeven = (x%2 == 0);
36- y += yeven; //it must be an uneven number to look fine
37- x += xeven;
34+ assert(y >= 5);
35+ assert(y >= 5);
36+ y += (y%2 == 0); //it must be an uneven number to look fine
37+ x += (x%2 == 0);
3838 struct game g = create_game(y, x);
3939
4040 char *maze = xmalloc(y * x + 1);
Msrc/loop.c
@@ -14,12 +14,13 @@
1414 #include <tui.h>
1515 #include <algorithm.h>
1616 #include <config.h>
17+#include <items.h>
1718
1819 #define NOOBJ_OBJECT (struct object){.type = NOOBJ, .symbol = ' '}
1920
2021 unsigned m_id = 0;
2122
22-struct choice get_input(void)
23+static struct choice get_input(void)
2324 {
2425 struct choice c;
2526
@@ -69,6 +70,7 @@ start:;
6970 return c;
7071 }
7172
73+//adds xp to m, increases stats and opens a messagebox
7274 static void addxp(struct monster *m, unsigned xp)
7375 {
7476 m->xp += xp;
@@ -83,8 +85,11 @@ static void addxp(struct monster *m, unsigned xp)
8385 }
8486 }
8587
88+//inflicts src's damage to target, removes targets monster if hp goes to zero
89+//gives src xp if target is killed
8690 static void attack(struct monster *src, struct field *target)
8791 {
92+ assert(target->has_monster);
8893 if(src->isplayer)
8994 logstr("%s hit %s for %d damage", src->name, target->m.name, src->dmg);
9095
@@ -104,6 +109,7 @@ static void attack(struct monster *src, struct field *target)
104109 }
105110 }
106111
112+//returns true if adding was successful, otherwise false
107113 static bool additem(struct monster *m, struct item *it)
108114 {
109115 darray_append(m->items, *it);
@@ -115,7 +121,9 @@ static void rmvitem(struct monster *m, size_t i)
115121 darray_remove(m->items, i);
116122 }
117123
118-bool inventorycallback(struct monster *m, size_t i)
124+//called by itemselect, uses an item if its usable
125+//equips it and does stat recalculation if its an equip
126+static bool inventorycallback(struct monster *m, size_t i)
119127 {
120128 struct item *it = &darray_item(m->items,i);
121129 if(it->type == USE) {
@@ -138,16 +146,17 @@ bool inventorycallback(struct monster *m, size_t i)
138146 return false;
139147 }
140148
141-void inventory(struct monster *m)
149+static void inventory(struct monster *m)
142150 {
143151 itemselect(m, inventorycallback);
144152 }
145153
146-
154+//moves the object in src to target if target is empty
155+//puts an item in the inventory of src if src is a monster and target contains an item
147156 //TODO: player moves on target, create new map
148-struct point move_object(struct game *g, struct point src, struct point target)
157+static struct point move_object(struct game *g, struct point src, struct point target)
149158 {
150- if(game_at(g, target)->u->ground.type == NOOBJ && !game_at(g, target)->has_monster) {
159+ if((game_at(g, target)->u->ground.type == NOOBJ || game_at(g, target)->u->ground.type == TARGET) && !game_at(g, target)->has_monster) {
151160 game_at(g, target)->has_monster = true;
152161 game_at(g, target)->m = game_at(g, src)->m;
153162 game_at(g, src)->has_monster = false;
@@ -180,7 +189,7 @@ static inline struct point attack_move(struct game *g, struct point src, struct
180189 return src;
181190 }
182191
183-void run_ai(struct game *g, struct point player)
192+static void run_ai(struct game *g, struct point player)
184193 {
185194 darray(unsigned) checked = darray_new();
186195 for(size_t y = 0; y < g->ys; y++)
@@ -219,19 +228,12 @@ void run_ai(struct game *g, struct point player)
219228 darray_free(checked);
220229 }
221230
222-
223-void loop(struct game *g)
231+//creates game and spawns loot and enemies
232+static struct game setup_game()
224233 {
225-/*
226-"\
227-███████╗██████╗ ██╗ ██╗ ███╗ ██╗███████╗ ██████╗██╗ ██╗ █████╗ ██████╗ ██████╗ ███████╗███╗ ███╗██╗ ██╗███████╗███╗ ██╗██████╗ \n\
228-██╔════╝██╔══██╗██║ ██║ ████╗ ██║██╔════╝██╔════╝██║ ██╔╝██╔══██╗██╔══██╗██╔════╝ ██╔════╝████╗ ████║██║ ██║██╔════╝████╗ ██║██╔══██╗\n\
229-███████╗██████╔╝███████║ ██╔██╗ ██║█████╗ ██║ █████╔╝ ███████║██████╔╝██║ ███╗█████╗ ██╔████╔██║██║ ██║█████╗ ██╔██╗ ██║██║ ██║\n\
230-╚════██║██╔══██╗██╔══██║ ██║╚██╗██║██╔══╝ ██║ ██╔═██╗ ██╔══██║██╔══██╗██║ ██║██╔══╝ ██║╚██╔╝██║██║ ██║██╔══╝ ██║╚██╗██║██║ ██║\n\
231-███████║██║ ██║██║ ██║ ██║ ╚████║███████╗╚██████╗██║ ██╗██║ ██║██║ ██║╚██████╔╝███████╗██║ ╚═╝ ██║╚██████╔╝███████╗██║ ╚████║██████╔╝\n\
232-"
233-*/
234- game_at(g, (struct point){1,1})->m = (struct monster)
234+ struct game g = make_maze(randrange(GAME_MIN_Y, GAME_MAX_Y), randrange(GAME_MIN_X, GAME_MAX_X));
235+
236+ game_at(&g, (struct point){1,1})->m = (struct monster) //TODO: keep player between levels
235237 {
236238 .symbol = 'P',
237239 .id = -1,
@@ -244,7 +246,27 @@ void loop(struct game *g)
244246 .items = darray_new(),
245247 .isplayer = true
246248 };
247- game_at(g, (struct point){1,1})->has_monster = true;
249+ game_at(&g, (struct point){1,1})->has_monster = true;
250+
251+ size_t num_enemies = randrange(MIN_ENEMIES, MAX_ENEMIES);
252+ for(size_t i = 0; i < num_enemies; i++)
253+ {
254+ spawn_enemy(&g);
255+ }
256+
257+ size_t num_items = randrange(MIN_LOOT, MAX_LOOT);
258+ for(size_t i = 0; i < num_items; i++)
259+ {
260+ spawn_item(&g);
261+ }
262+
263+ return g;
264+}
265+
266+
267+void loop()
268+{
269+ struct game g = setup_game();
248270
249271 struct point player = {1,1};
250272
@@ -252,12 +274,12 @@ void loop(struct game *g)
252274 {
253275 charclear();
254276 charprint("(づ。◕‿‿◕。)づ");
255- charprint("Name: %s", game_at(g, player)->m.name);
256- charprint("HP : %u/%u", game_at(g, player)->m.current_hp, game_at(g, player)->m.max_hp);
257- charprint("DMG: %u", game_at(g, player)->m.dmg);
258- charprint("LVL: %u", game_at(g, player)->m.level);
259- charprint("XP : %u", game_at(g, player)->m.xp);
260- draw_map(g, player);
277+ charprint("Name: %s", game_at(&g, player)->m.name);
278+ charprint("HP : %u/%u", game_at(&g, player)->m.current_hp, game_at(&g, player)->m.max_hp);
279+ charprint("DMG: %u", game_at(&g, player)->m.dmg);
280+ charprint("LVL: %u", game_at(&g, player)->m.level);
281+ charprint("XP : %u", game_at(&g, player)->m.xp);
282+ draw_map(&g, player);
261283
262284 struct choice c = get_input();
263285 switch(c.a)
@@ -266,23 +288,30 @@ void loop(struct game *g)
266288 logstr("Invalid input, try again");
267289 continue;
268290 case QUIT:
291+ delete_game(&g);
269292 return;
270293 case MOVE:;
271294 struct point targetpos = relative(player, c.u.d);
272295
273- if(!in_bounds(g, targetpos)) {
296+ if(!in_bounds(&g, targetpos)) {
274297 logstr("Target not in bounds");
275298 continue;
276299 }
277300
278- player = attack_move(g, player, targetpos);
279- run_ai(g, player);
301+ player = attack_move(&g, player, targetpos);
302+ if(game_at(&g, player)->u->ground.type == TARGET)
303+ {
304+ delete_game(&g);
305+ loop();
306+ return;
307+ }
308+ run_ai(&g, player);
280309 break;
281310 case SPAWN:
282- spawn_enemy(g);
311+ spawn_enemy(&g);
283312 break;
284313 case INVENTORY:
285- inventory(&game_at(g, player)->m);
314+ inventory(&game_at(&g, player)->m);
286315 break;
287316 case REDRAW:
288317 setup();
Msrc/main.c
@@ -7,18 +7,11 @@
77 #include <locale.h>
88 #include <time.h>
99
10-
11-#include <helper.h>
1210 #include <tui.h>
13-#include <loop.h>
1411 #include <game.h>
15-#include <algorithm.h>
16-#include <config.h>
17-#include <items.h>
18-
19-
2012
2113 #include <unistd.h>
14+
2215 int main()
2316 {
2417 srand(time(NULL));
@@ -26,20 +19,7 @@ int main()
2619 setup();
2720 logstr("Game started");
2821
29- struct game g = make_maze(randrange(GAME_MIN_Y, GAME_MAX_Y), randrange(GAME_MIN_X, GAME_MAX_X));
30- size_t num_enemies = randrange(MIN_ENEMIES, MAX_ENEMIES);
31- for(size_t i = 0; i < num_enemies; i++)
32- {
33- spawn_enemy(&g);
34- }
35-
36- size_t num_items = randrange(MIN_LOOT, MAX_LOOT);
37- for(size_t i = 0; i < num_items; i++)
38- {
39- spawn_item(&g);
40- }
41-
42- loop(&g);
43-
44- delete_game(&g);
22+
23+ loop();
24+
4525 }