comparison libmlk-rpg/rpg/selection.c @ 289:63d9fb56c609

rpg: rework selection
author David Demelier <markand@malikania.fr>
date Thu, 07 Jan 2021 15:52:56 +0100
parents
children d01e83210ca2
comparison
equal deleted inserted replaced
288:cc0f02ae9005 289:63d9fb56c609
1 /*
2 * selection.c -- kind of selection
3 *
4 * Copyright (c) 2020 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 struct {
31 const struct battle_entity *entity;
32 size_t position;
33 } table[BATTLE_ENTITY_MAX] = {0};
34
35 size_t tablesz = 0;
36
37 /*
38 * Merge the list of valid entities into the table to select a random
39 * one.
40 */
41 for (size_t i = 0; i < entitiesz; ++i) {
42 if (battle_entity_ok(&entities[i])) {
43 table[tablesz].entity = &entities[i];
44 table[tablesz++].position = i;
45 }
46 }
47
48 slt->index_character = table[util_nrand(0, tablesz)].position;
49 }
50
51 static void
52 first(struct selection *slt, const struct battle *bt, const struct battle_entity *entities, size_t entitiesz)
53 {
54 for (size_t i = 0; i < entitiesz; ++i) {
55 if (battle_entity_ok(&entities[i])) {
56 slt->index_character = i;
57 break;
58 }
59 }
60 }
61
62 void
63 selection_first(struct selection *slt, const struct battle *bt)
64 {
65 assert(slt);
66 assert(bt);
67
68 if (slt->index_side == 0)
69 first(slt, bt, bt->enemies, BATTLE_ENEMY_MAX);
70 else
71 first(slt, bt, bt->team, BATTLE_TEAM_MAX);
72 }
73
74 void
75 selection_random(struct selection *slt, const struct battle *bt)
76 {
77 assert(slt);
78 assert(bt);
79
80 if (slt->index_side == 0)
81 random(slt, bt, bt->enemies, BATTLE_ENEMY_MAX);
82 else
83 random(slt, bt, bt->team, BATTLE_TEAM_MAX);
84 }