comparison src/libmlk-rpg/rpg/selection.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-rpg/rpg/selection.c@1a6125ffebff
children 460c78706989
comparison
equal deleted inserted replaced
319:b843eef4cc35 320:8f9937403749
1 /*
2 * selection.c -- kind of selection
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
21 #include <core/util.h>
22
23 #include "battle.h"
24 #include "character.h"
25 #include "selection.h"
26
27 static void
28 random(struct selection *slt, const struct battle *bt, const struct battle_entity *entities, size_t entitiesz)
29 {
30 (void)bt;
31
32 struct {
33 const struct battle_entity *entity;
34 size_t position;
35 } table[BATTLE_ENTITY_MAX] = {0};
36
37 size_t tablesz = 0;
38
39 /*
40 * Merge the list of valid entities into the table to select a random
41 * one.
42 */
43 for (size_t i = 0; i < entitiesz; ++i) {
44 if (battle_entity_ok(&entities[i])) {
45 table[tablesz].entity = &entities[i];
46 table[tablesz++].position = i;
47 }
48 }
49
50 slt->index_character = table[util_nrand(0, tablesz)].position;
51 }
52
53 static void
54 first(struct selection *slt, const struct battle *bt, const struct battle_entity *entities, size_t entitiesz)
55 {
56 (void)bt;
57
58 for (size_t i = 0; i < entitiesz; ++i) {
59 if (battle_entity_ok(&entities[i])) {
60 slt->index_character = i;
61 break;
62 }
63 }
64 }
65
66 void
67 selection_first(struct selection *slt, const struct battle *bt)
68 {
69 assert(slt);
70 assert(bt);
71
72 if (slt->index_side == 0)
73 first(slt, bt, bt->enemies, BATTLE_ENEMY_MAX);
74 else
75 first(slt, bt, bt->team, BATTLE_TEAM_MAX);
76 }
77
78 void
79 selection_random(struct selection *slt, const struct battle *bt)
80 {
81 assert(slt);
82 assert(bt);
83
84 if (slt->index_side == 0)
85 random(slt, bt, bt->enemies, BATTLE_ENEMY_MAX);
86 else
87 random(slt, bt, bt->team, BATTLE_TEAM_MAX);
88 }