comparison 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
comparison
equal deleted inserted replaced
319:b843eef4cc35 320:8f9937403749
1 /*
2 * spawner.c -- spawn battle while moving
3 *
4 * Copyright (c) 2020-2021 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <assert.h>
20 #include <math.h>
21 #include <stdlib.h>
22
23 #include <core/alloc.h>
24 #include <core/game.h>
25 #include <core/util.h>
26
27 #include <rpg/battle.h>
28 #include <rpg/character.h>
29 #include <rpg/map.h>
30
31 #include <adventure/molko.h>
32
33 #include <adventure/character/black-cat.h>
34
35 #include "spawner.h"
36
37 static inline unsigned int
38 distance(const struct spawner *s)
39 {
40 unsigned int gap_x = fmax(s->last_x, s->map->player_x) -
41 fmin(s->last_x, s->map->player_x);
42 unsigned int gap_y = fmax(s->last_y, s->map->player_y) -
43 fmin(s->last_y, s->map->player_y);
44
45 return fmin(s->steps, gap_x + gap_y);
46 }
47
48 static void
49 fight(struct spawner *s)
50 {
51 (void)s;
52
53 struct battle *bt;
54
55 bt = alloc_new0(sizeof (*bt));
56
57 bt->enemies[0].ch = alloc_dup(&character_black_cat, sizeof (character_black_cat));
58 bt->enemies[0].x = 400;
59 bt->enemies[0].y = 50;
60 bt->enemies[1].ch = alloc_dup(&character_black_cat, sizeof (character_black_cat));
61 bt->enemies[1].x = 200;
62 bt->enemies[1].y = 100;
63
64 bt->inventory = &molko.inventory;
65
66 for (size_t i = 0; i < TEAM_MEMBER_MAX; ++i) {
67 if (molko.team.members[i]) {
68 bt->team[i].ch = alloc_dup(molko.team.members[i], sizeof (*molko.team.members[i]));
69 character_reset(bt->team[i].ch);
70 bt->team[i].ch->hp = bt->team[i].ch->hpmax;
71 bt->team[i].ch->mp = bt->team[i].ch->mpmax;
72 }
73 }
74
75 molko_fight(bt);
76 }
77
78 static int
79 update(struct action *act, unsigned int ticks)
80 {
81 (void)ticks;
82
83 struct spawner *s = act->data;
84
85 if (s->map->player_movement) {
86 s->steps -= distance(s);
87 s->last_x = s->map->player_x;
88 s->last_y = s->map->player_y;
89
90 if (s->steps == 0) {
91 spawner_init(s);
92 fight(s);
93 }
94 }
95
96 return 0;
97 }
98
99 void
100 spawner_init(struct spawner *s)
101 {
102 assert(s);
103
104 s->last_x = s->map->player_x;
105 s->last_y = s->map->player_y;
106 s->steps = util_nrand(s->low, s->high);
107 }
108
109 struct action *
110 spawner_action(struct spawner *s)
111 {
112 assert(s);
113
114 s->action.data = s;
115 s->action.update = update;
116
117 spawner_init(s);
118
119 return &s->action;
120 }