annotate src/libmlk-adventure/adventure/action/spawner.c @ 320:8f9937403749

misc: improve loading of data
author David Demelier <markand@malikania.fr>
date Fri, 01 Oct 2021 20:30:00 +0200
parents libmlk-adventure/adventure/action/spawner.c@1a6125ffebff
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
260
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
1 /*
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
2 * spawner.c -- spawn battle while moving
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
3 *
314
d01e83210ca2 misc: update years
David Demelier <markand@malikania.fr>
parents: 299
diff changeset
4 * Copyright (c) 2020-2021 David Demelier <markand@malikania.fr>
260
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
5 *
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
6 * Permission to use, copy, modify, and/or distribute this software for any
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
7 * purpose with or without fee is hereby granted, provided that the above
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
8 * copyright notice and this permission notice appear in all copies.
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
9 *
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
17 */
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
18
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
19 #include <assert.h>
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
20 #include <math.h>
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
21 #include <stdlib.h>
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
22
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
23 #include <core/alloc.h>
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
24 #include <core/game.h>
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
25 #include <core/util.h>
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
26
286
3991779aaba9 adventure: initial test of spawn
David Demelier <markand@malikania.fr>
parents: 275
diff changeset
27 #include <rpg/battle.h>
289
63d9fb56c609 rpg: rework selection
David Demelier <markand@malikania.fr>
parents: 286
diff changeset
28 #include <rpg/character.h>
260
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
29 #include <rpg/map.h>
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
30
286
3991779aaba9 adventure: initial test of spawn
David Demelier <markand@malikania.fr>
parents: 275
diff changeset
31 #include <adventure/molko.h>
3991779aaba9 adventure: initial test of spawn
David Demelier <markand@malikania.fr>
parents: 275
diff changeset
32
3991779aaba9 adventure: initial test of spawn
David Demelier <markand@malikania.fr>
parents: 275
diff changeset
33 #include <adventure/character/black-cat.h>
3991779aaba9 adventure: initial test of spawn
David Demelier <markand@malikania.fr>
parents: 275
diff changeset
34
260
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
35 #include "spawner.h"
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
36
273
e28429dbdaaf adventure: uniform actions
David Demelier <markand@malikania.fr>
parents: 272
diff changeset
37 static inline unsigned int
e28429dbdaaf adventure: uniform actions
David Demelier <markand@malikania.fr>
parents: 272
diff changeset
38 distance(const struct spawner *s)
e28429dbdaaf adventure: uniform actions
David Demelier <markand@malikania.fr>
parents: 272
diff changeset
39 {
e28429dbdaaf adventure: uniform actions
David Demelier <markand@malikania.fr>
parents: 272
diff changeset
40 unsigned int gap_x = fmax(s->last_x, s->map->player_x) -
e28429dbdaaf adventure: uniform actions
David Demelier <markand@malikania.fr>
parents: 272
diff changeset
41 fmin(s->last_x, s->map->player_x);
e28429dbdaaf adventure: uniform actions
David Demelier <markand@malikania.fr>
parents: 272
diff changeset
42 unsigned int gap_y = fmax(s->last_y, s->map->player_y) -
e28429dbdaaf adventure: uniform actions
David Demelier <markand@malikania.fr>
parents: 272
diff changeset
43 fmin(s->last_y, s->map->player_y);
260
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
44
273
e28429dbdaaf adventure: uniform actions
David Demelier <markand@malikania.fr>
parents: 272
diff changeset
45 return fmin(s->steps, gap_x + gap_y);
260
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
46 }
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
47
286
3991779aaba9 adventure: initial test of spawn
David Demelier <markand@malikania.fr>
parents: 275
diff changeset
48 static void
299
648f5f949afb core: implement stackable states
David Demelier <markand@malikania.fr>
parents: 298
diff changeset
49 fight(struct spawner *s)
286
3991779aaba9 adventure: initial test of spawn
David Demelier <markand@malikania.fr>
parents: 275
diff changeset
50 {
316
1a6125ffebff misc: fix many warnings and build on macOS
David Demelier <markand@malikania.fr>
parents: 314
diff changeset
51 (void)s;
1a6125ffebff misc: fix many warnings and build on macOS
David Demelier <markand@malikania.fr>
parents: 314
diff changeset
52
286
3991779aaba9 adventure: initial test of spawn
David Demelier <markand@malikania.fr>
parents: 275
diff changeset
53 struct battle *bt;
3991779aaba9 adventure: initial test of spawn
David Demelier <markand@malikania.fr>
parents: 275
diff changeset
54
3991779aaba9 adventure: initial test of spawn
David Demelier <markand@malikania.fr>
parents: 275
diff changeset
55 bt = alloc_new0(sizeof (*bt));
289
63d9fb56c609 rpg: rework selection
David Demelier <markand@malikania.fr>
parents: 286
diff changeset
56
63d9fb56c609 rpg: rework selection
David Demelier <markand@malikania.fr>
parents: 286
diff changeset
57 bt->enemies[0].ch = alloc_dup(&character_black_cat, sizeof (character_black_cat));
286
3991779aaba9 adventure: initial test of spawn
David Demelier <markand@malikania.fr>
parents: 275
diff changeset
58 bt->enemies[0].x = 400;
3991779aaba9 adventure: initial test of spawn
David Demelier <markand@malikania.fr>
parents: 275
diff changeset
59 bt->enemies[0].y = 50;
289
63d9fb56c609 rpg: rework selection
David Demelier <markand@malikania.fr>
parents: 286
diff changeset
60 bt->enemies[1].ch = alloc_dup(&character_black_cat, sizeof (character_black_cat));
63d9fb56c609 rpg: rework selection
David Demelier <markand@malikania.fr>
parents: 286
diff changeset
61 bt->enemies[1].x = 200;
63d9fb56c609 rpg: rework selection
David Demelier <markand@malikania.fr>
parents: 286
diff changeset
62 bt->enemies[1].y = 100;
63d9fb56c609 rpg: rework selection
David Demelier <markand@malikania.fr>
parents: 286
diff changeset
63
286
3991779aaba9 adventure: initial test of spawn
David Demelier <markand@malikania.fr>
parents: 275
diff changeset
64 bt->inventory = &molko.inventory;
3991779aaba9 adventure: initial test of spawn
David Demelier <markand@malikania.fr>
parents: 275
diff changeset
65
289
63d9fb56c609 rpg: rework selection
David Demelier <markand@malikania.fr>
parents: 286
diff changeset
66 for (size_t i = 0; i < TEAM_MEMBER_MAX; ++i) {
63d9fb56c609 rpg: rework selection
David Demelier <markand@malikania.fr>
parents: 286
diff changeset
67 if (molko.team.members[i]) {
63d9fb56c609 rpg: rework selection
David Demelier <markand@malikania.fr>
parents: 286
diff changeset
68 bt->team[i].ch = alloc_dup(molko.team.members[i], sizeof (*molko.team.members[i]));
63d9fb56c609 rpg: rework selection
David Demelier <markand@malikania.fr>
parents: 286
diff changeset
69 character_reset(bt->team[i].ch);
63d9fb56c609 rpg: rework selection
David Demelier <markand@malikania.fr>
parents: 286
diff changeset
70 bt->team[i].ch->hp = bt->team[i].ch->hpmax;
63d9fb56c609 rpg: rework selection
David Demelier <markand@malikania.fr>
parents: 286
diff changeset
71 bt->team[i].ch->mp = bt->team[i].ch->mpmax;
63d9fb56c609 rpg: rework selection
David Demelier <markand@malikania.fr>
parents: 286
diff changeset
72 }
63d9fb56c609 rpg: rework selection
David Demelier <markand@malikania.fr>
parents: 286
diff changeset
73 }
286
3991779aaba9 adventure: initial test of spawn
David Demelier <markand@malikania.fr>
parents: 275
diff changeset
74
3991779aaba9 adventure: initial test of spawn
David Demelier <markand@malikania.fr>
parents: 275
diff changeset
75 molko_fight(bt);
3991779aaba9 adventure: initial test of spawn
David Demelier <markand@malikania.fr>
parents: 275
diff changeset
76 }
3991779aaba9 adventure: initial test of spawn
David Demelier <markand@malikania.fr>
parents: 275
diff changeset
77
298
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 289
diff changeset
78 static int
260
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
79 update(struct action *act, unsigned int ticks)
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
80 {
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
81 (void)ticks;
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
82
273
e28429dbdaaf adventure: uniform actions
David Demelier <markand@malikania.fr>
parents: 272
diff changeset
83 struct spawner *s = act->data;
260
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
84
273
e28429dbdaaf adventure: uniform actions
David Demelier <markand@malikania.fr>
parents: 272
diff changeset
85 if (s->map->player_movement) {
e28429dbdaaf adventure: uniform actions
David Demelier <markand@malikania.fr>
parents: 272
diff changeset
86 s->steps -= distance(s);
e28429dbdaaf adventure: uniform actions
David Demelier <markand@malikania.fr>
parents: 272
diff changeset
87 s->last_x = s->map->player_x;
e28429dbdaaf adventure: uniform actions
David Demelier <markand@malikania.fr>
parents: 272
diff changeset
88 s->last_y = s->map->player_y;
260
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
89
273
e28429dbdaaf adventure: uniform actions
David Demelier <markand@malikania.fr>
parents: 272
diff changeset
90 if (s->steps == 0) {
299
648f5f949afb core: implement stackable states
David Demelier <markand@malikania.fr>
parents: 298
diff changeset
91 spawner_init(s);
648f5f949afb core: implement stackable states
David Demelier <markand@malikania.fr>
parents: 298
diff changeset
92 fight(s);
260
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
93 }
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
94 }
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
95
298
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 289
diff changeset
96 return 0;
260
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
97 }
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
98
273
e28429dbdaaf adventure: uniform actions
David Demelier <markand@malikania.fr>
parents: 272
diff changeset
99 void
e28429dbdaaf adventure: uniform actions
David Demelier <markand@malikania.fr>
parents: 272
diff changeset
100 spawner_init(struct spawner *s)
260
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
101 {
273
e28429dbdaaf adventure: uniform actions
David Demelier <markand@malikania.fr>
parents: 272
diff changeset
102 assert(s);
e28429dbdaaf adventure: uniform actions
David Demelier <markand@malikania.fr>
parents: 272
diff changeset
103
e28429dbdaaf adventure: uniform actions
David Demelier <markand@malikania.fr>
parents: 272
diff changeset
104 s->last_x = s->map->player_x;
e28429dbdaaf adventure: uniform actions
David Demelier <markand@malikania.fr>
parents: 272
diff changeset
105 s->last_y = s->map->player_y;
e28429dbdaaf adventure: uniform actions
David Demelier <markand@malikania.fr>
parents: 272
diff changeset
106 s->steps = util_nrand(s->low, s->high);
260
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
107 }
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
108
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
109 struct action *
275
f89a53abb314 adventure: cleanup actions and prepare map scenes
David Demelier <markand@malikania.fr>
parents: 273
diff changeset
110 spawner_action(struct spawner *s)
260
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
111 {
273
e28429dbdaaf adventure: uniform actions
David Demelier <markand@malikania.fr>
parents: 272
diff changeset
112 assert(s);
260
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
113
273
e28429dbdaaf adventure: uniform actions
David Demelier <markand@malikania.fr>
parents: 272
diff changeset
114 s->action.data = s;
e28429dbdaaf adventure: uniform actions
David Demelier <markand@malikania.fr>
parents: 272
diff changeset
115 s->action.update = update;
260
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
116
286
3991779aaba9 adventure: initial test of spawn
David Demelier <markand@malikania.fr>
parents: 275
diff changeset
117 spawner_init(s);
3991779aaba9 adventure: initial test of spawn
David Demelier <markand@malikania.fr>
parents: 275
diff changeset
118
273
e28429dbdaaf adventure: uniform actions
David Demelier <markand@malikania.fr>
parents: 272
diff changeset
119 return &s->action;
260
60a214ec1ab4 adventure: change the way states are stored
David Demelier <markand@malikania.fr>
parents:
diff changeset
120 }