comparison src/libmlk-adventure/adventure/spell/fire-minor.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/spell/fire-minor.c@d01e83210ca2
children
comparison
equal deleted inserted replaced
319:b843eef4cc35 320:8f9937403749
1 /*
2 * fire-minor.c -- minor fire
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 <math.h>
20 #include <stdlib.h>
21
22 #include <core/action.h>
23 #include <core/alloc.h>
24 #include <core/animation.h>
25 #include <core/maths.h>
26
27 #include <rpg/battle.h>
28 #include <rpg/character.h>
29 #include <rpg/selection.h>
30
31 #include <adventure/adventure_p.h>
32 #include <adventure/assets.h>
33
34 #include "fire-minor.h"
35
36 struct rendering {
37 struct battle *battle;
38 struct battle_entity *source;
39 struct battle_entity *target;
40 struct action action;
41 struct animation animation;
42 };
43
44 static int
45 update(struct action *act, unsigned int ticks)
46 {
47 struct rendering *rdr = act->data;
48
49 return animation_update(&rdr->animation, ticks);
50 }
51
52 static void
53 draw(struct action *act)
54 {
55 const struct rendering *rdr = act->data;
56
57 animation_draw(&rdr->animation, rdr->target->x, rdr->target->y);
58 }
59
60 static void
61 end(struct action *act)
62 {
63 struct rendering *rdr = act->data;
64 float base;
65
66 /* Compute damage. */
67 /* TODO: move this into a general maths computation. */
68 /* TODO: move min/max limits outside. */
69 base = util_nrand(50, 70);
70 base += base * (maths_scale(rdr->source->ch->atk + rdr->source->ch->atkbonus, 0, 1000, 0, 100) / 100);
71
72 /* Reduce damage taken. */
73 base -= base * (maths_scale(rdr->target->ch->atk + rdr->target->ch->atkbonus, 0, 1000, 0, 100) / 200);
74 base = base < 0 ? 0 : base;
75
76 /* TODO: add battle_damage function*/
77 rdr->target->ch->hp = fmax(rdr->target->ch->hp - base, rdr->target->ch->hp);
78 battle_indicator_hp(rdr->battle, rdr->target->ch, base);
79 }
80
81 static void
82 finish(struct action *act)
83 {
84 free(act->data);
85 }
86
87 static void
88 select(const struct battle *bt, struct selection *slt)
89 {
90 slt->index_side = 0;
91
92 selection_first(slt, bt);
93 }
94
95 static void
96 action(struct battle *bt, struct character *owner, const struct selection *slt)
97 {
98 struct rendering *rdr;
99
100 /* Action. */
101 rdr = alloc_new0(sizeof (*rdr));
102 rdr->action.data = rdr;
103 rdr->action.update = update;
104 rdr->action.draw = draw;
105 rdr->action.end = end;
106 rdr->action.finish = finish;
107
108 /* Battle and target. */
109 rdr->battle = bt;
110 rdr->source = battle_find(bt, owner);
111 rdr->target = &bt->enemies[slt->index_character];
112
113 /* Animation. */
114 rdr->animation.delay = 10;
115 rdr->animation.sprite = &assets_sprites[ASSETS_SPRITE_EXPLOSION];
116 animation_start(&rdr->animation);
117
118 action_stack_add(&bt->actions[0], &rdr->action);
119 }
120
121 const struct spell spell_fire_minor = {
122 .name = N_("Fire Minor"),
123 .description = N_("A small amount of fire balls"),
124 .mp = 10,
125 .type = SPELL_TYPE_FIRE,
126 .select_kind = SELECTION_KIND_ONE,
127 .select_side = SELECTION_SIDE_ENEMY,
128 .select = select,
129 .action = action
130 };