helapordo 1.4.20
Loading...
Searching...
No Matches
game_utils.h
Go to the documentation of this file.
1// jgabaut @ github.com/jgabaut
2// SPDX-License-Identifier: GPL-3.0-only
3/*
4 Copyright (C) 2022-2026 jgabaut
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, version 3 of the License.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
17*/
18
19#ifndef GAME_UTILS_H
20#define GAME_UTILS_H
21
22#ifndef _WIN32
23#ifndef _POSIX_C_SOURCE
24#define _POSIX_C_SOURCE 200809L // Needed for getopt
25#endif // _POSIX_C_SOURCE
26#endif // _WIN32
27
28#ifdef HELAPORDO_CURSES_BUILD
29#include "../core/sprites.h"
30#ifdef _WIN32
31#include <ncursesw/panel.h>
32#include <ncursesw/menu.h>
33#else
34#include <menu.h>
35#include <panel.h>
36#endif // _WIN32
37#else
38#ifndef HELAPORDO_RAYLIB_BUILD
39#error "HELAPORDO_CURSES_BUILD and HELAPORDO_RAYLIB_BUILD are both undefined."
40#else
41
42#endif // HELAPORDO_RAYLIB_BUILD
43
44#endif // HELAPORDO_CURSES_BUILD
45
46#include <stdio.h>
47#include <stdlib.h>
48#include <unistd.h>
49
50
51#ifdef _WIN32 // From https://stackoverflow.com/a/230068
52#include <io.h>
53#define F_OK 0
54#define access _access
55#endif
56
57#include <string.h>
58#include <time.h>
59#include <inttypes.h>
60#include <signal.h>
61#include "../core/game_core.h"
62#include "../core/game_log.h"
64#include "../core/game_rng.h"
65#include "../core/game_fight.h"
66#ifdef HELAPORDO_CURSES_BUILD
67#endif // HELAPORDO_CURSES_BUILD
68#ifdef ANVIL_BUILD
69#include "../anvil__helapordo.h"
70#endif // ANVIL_BUILD
71
72#include "../release_data/release_data.h"
73
74void* s4c_gui_malloc(size_t size);
75void* s4c_gui_calloc(size_t count, size_t size);
76void hlpd_sigint_handler(int signum);
77void initWincon(Wincon * w, Path * p, winconClass class);
78void printGlobVars(void);
79
80#ifdef _WIN32
81void printWin_EnvVars(void);
82#endif
83
84void red(void);
85void lightRed(void);
86void strongWhite(void);
87void white(void);
88void green(void);
89void lightGreen(void);
90void yellow(void);
91void lightYellow(void);
92void blue(void);
93void lightBlue(void);
94void purple(void);
95void lightPurple(void);
96void cyan(void);
97void lightCyan(void);
98
100
101void dbg_print_floor_layout(Floor * floor);
102void dbg_print_explored_layout(Floor * floor);
104void dbg_Gamestate(Gamestate * gmst);
105void dbg_GameScreen(GameScreen * scr);
106void dbg_GameOptions(GameOptions * options);
107void dbg_Fighter(Fighter * fighter);
108void dbg_countStats(countStats * stats);
109void dbg_Wincon(Wincon * wc);
110void dbg_Path(Path * path);
111void dbg_Saveslot(Saveslot * saveslot);
112
113void update_Gamestate(Gamestate * gmst, int current_fighters,
114 roomClass current_roomtype, int current_room_index,
115 int current_enemy_index, Floor * current_floor, Room* current_room, GameOptions* game_options);
116
118
119void loadLore(char **lore_strings, int loreKind);
120
121void setRoomType(Path * path, int *roadFork_value, roomClass * room_type,
122 int roomsDone);
123void freeRoom(Room * room);
124
125void printTitle(void);
126
127void printVersion(void);
128void printFormattedVersion(const char *progname);
129void hlpd_dbg_features(void);
130
131void usage(const char *progname);
132
134
135void scanName(Fighter * player);
136
137void printClasses(void);
138int scanClass(void);
139void pickClass(Fighter * player);
140
141void printWincons(void);
142int scanWincon(void);
143void pickWincon(Wincon * w);
144
145void pickName(Fighter * player);
146
147void copy_fighter(Fighter * source, Fighter * dest);
148
149void resetPermboosts(Fighter * f);
150void applyPermboosts(Fighter * f);
151
153
154void printSpawnMessage(Enemy * e, int roomIndex, int enemyIndex);
155int dropConsumable(Fighter * player);
156int dropArtifact(Fighter * player);
157
158int getConsumableQty(Fighter * f, int n);
159void emptyConsumables(Fighter * player);
160void emptyArtifacts(Fighter * player);
161void emptyEquips(Fighter * player);
162Path *randomise_path(char* seed, Koliseo * kls, const char *path_to_savefile);
163void printStats(Fighter * f);
164void printEStats(Enemy * e);
167void dropEquip(Fighter * player, int beast, Koliseo * kls, RingaBuf* rb_notifications);
168
169#ifdef HELAPORDO_CURSES_BUILD
170void test_game_color_pairs(WINDOW * win, int colors_per_row);
171void init_Gamestate(Gamestate * gmst, clock_t start_time, countStats * stats, Wincon * wincon,
172 Path * path, Fighter * player, Gamemode gamemode, GameScreen* screen, GameOptions* options, bool is_seeded);
173turnOP_args *init_turnOP_args(Gamestate * gmst, Fighter * actor, Path * path,
174 Room * room, loadInfo * load_info, Enemy * enemy,
175 Boss * boss, FILE * save_file,
176 WINDOW * notify_win, Koliseo_Temp * t_kls,
177 foeTurnOption_OP foe_op, skillType picked_skill, RingaBuf* rb_notifications);
178void print_label(WINDOW * win, int starty, int startx, int width, char *string,
179 chtype color);
180void printEquipStats(Equip * e);
181void printQualityColor(quality q);
182void unlockSpecial(Fighter * f);
183void printCounters(Turncounter * counters[]);
184void printActivePerks(Fighter * f);
185int retry(char* seed);
186void getParams(int argc, char **argv, Fighter * player, Path * path, int optTot,
187 Koliseo * kls);
188#else
189#ifndef HELAPORDO_RAYLIB_BUILD
190#error "HELAPORDO_CURSES_BUILD and HELAPORDO_RAYLIB_BUILD are both undefined.\n"
191#else
192void test_game_color_pairs(Rectangle * win, int colors_per_row);
193void init_Gamestate(Gamestate * gmst, clock_t start_time, countStats * stats, Wincon * wincon,
194 Path * path, Fighter * player, Gamemode gamemode);
195turnOP_args *init_turnOP_args(Gamestate * gmst, Fighter * actor, Path * path,
196 Room * room, loadInfo * load_info, Enemy * enemy,
197 Boss * boss, FILE * save_file,
198 Rectangle * notification_area, Koliseo_Temp * t_kls,
199 foeTurnOption_OP foe_op, skillType picked_skill, RingaBuf* rb_notifications);
200void unlockSpecial(Fighter * f);
201#endif // HELAPORDO_RAYLIB_BUILD
202#endif // HELAPORDO_CURSES_BUILD
203
204void death(Fighter * player, loadInfo * load_info);
205void e_death(Enemy * e);
206void b_death(Boss * b);
207
208void updateCounters(Turncounter * counters[], int isEnemy, Fighter * f,
209 Enemy * e);
210void updateCounters_Boss(Turncounter * counters[], int isBoss, Fighter * f,
211 Boss * b);
212
213int checkremainder(Fighter * player, int xp);
214int giveXp(Fighter * player, Enemy * e);
215int giveXp_Boss(Fighter * player, Boss * b);
216int getEnemyXpGain(Enemy * e);
217int getBossXpGain(Boss * b);
218int onLevelUp(Fighter * player);
219void sell_all_equips(Fighter * f);
220
221turnOption getTurnChoice(char *ch);
222void quit(Fighter * p, Room * room, loadInfo * load_info, Koliseo_Temp * t_kls);
223void useConsumable(Fighter * f, Enemy * e, Boss * b, char *string, int isBoss);
224
225#define hlpd_d_keyval(key) ((((key) >= 0) && ((key) <= HLPD_KEYCLASS_MAX)) ? (hlpd_default_keybinds[(key)].val) : -1 )
226void hlpd_use_forced_flags(const char* whoami);
227int display_colorpairs(void);
228int hlpd_getopt(size_t argc, char** argv, const char* whoami);
229#endif
turnOption
The different kinds of turnOption.
Definition game_core.h:1949
Gamemode
The different kinds of Gamemode.
Definition game_core.h:452
saveType
The different kinds of saveType.
Definition game_core.h:1852
quality
The different grades of Equip.
Definition game_core.h:1393
foeTurnOption_OP
The different kinds of foeTurnOption_OP.
Definition game_core.h:2030
roomClass
The different kinds of Room.
Definition game_core.h:1661
skillType
Definition game_core.h:859
winconClass
The different kinds of Wincon.
Definition game_core.h:1324
char * lore_strings[6]
Declares lore array for the game.
Definition game_lore.c:20
void * s4c_gui_calloc(size_t count, size_t size)
Definition game_utils.c:30
void pickWincon(Wincon *w)
Takes a Wincon pointer and sets its class after asking the user for input.
Definition game_utils.c:1424
void * s4c_gui_malloc(size_t size)
Definition game_utils.c:21
void purple(void)
Prints an ASCII escape code that makes subsequent output a thick purple color.
Definition game_utils.c:777
int scanClass(void)
Asks for an integer on stdin and returns it if it's a valid fighterClass index.
Definition game_utils.c:1236
void hlpd_sigint_handler(int signum)
Function to handle Ctrl+C signal.
Definition game_utils.c:45
void dbg_Fighter(Fighter *fighter)
Debugs the passed (preallocated) Fighter with log_tag().
Definition game_utils.c:197
void yellow(void)
Prints an ASCII escape code that makes subsequent output a thick yellow color.
Definition game_utils.c:745
void dbg_GameOptions(GameOptions *options)
Definition game_utils.c:182
void printEStats(Enemy *e)
Takes a Enemy pointer and prints most of its values formatted.
Definition game_utils.c:1855
void white(void)
Prints an ASCII escape code that makes subsequent output a light white color.
Definition game_utils.c:721
void useConsumable(Fighter *f, Enemy *e, Boss *b, char *string, int isBoss)
Takes a Fighter, an Enemy and a Boss pointers, a string denoting the consumableClass and an int for u...
Definition game_utils.c:3713
void printGlobVars(void)
Prints global vars to stdout.
Definition game_utils.c:145
void lightYellow(void)
Prints an ASCII escape code that makes subsequent output a light yellow color.
Definition game_utils.c:753
void strongWhite(void)
Prints an ASCII escape code that makes subsequent output a thick white color.
Definition game_utils.c:713
void applyPermboosts(Fighter *f)
Takes a Fighter pointer and applies all of its permboost_STAT values by adding them to the current st...
Definition game_utils.c:1565
void usage(const char *progname)
Prints correct argument syntax for command line invocation.
Definition game_utils.c:1169
void printSpawnMessage(Enemy *e, int roomIndex, int enemyIndex)
Takes a Enemy pointer and two integers for current room index and current enemy index,...
Definition game_utils.c:1619
void blue(void)
Prints an ASCII escape code that makes subsequent output a thick blue color.
Definition game_utils.c:761
void sell_all_equips(Fighter *f)
Takes a Fighter pointer and deleted all the equips not in use, granting a payment to the Fighter bala...
Definition game_utils.c:3533
void update_Equipslots(Fighter *f)
Updates the passed Fighter's Equipslots item fields, by reading the equipsBag field and setting the e...
Definition game_utils.c:657
int checkremainder(Fighter *player, int xp)
Takes a Fighter pointer and the amount of xp to add.
Definition game_utils.c:3378
void quit(Fighter *p, Room *room, loadInfo *load_info, Koliseo_Temp *t_kls)
Takes a Fighter, a Room and a loadInfo pointers, and prints fighter stats and a quitting message,...
Definition game_utils.c:3664
void e_death(Enemy *e)
Takes a Enemy pointer and frees its allocated memory.
Definition game_utils.c:3170
void lightCyan(void)
Prints an ASCII escape code that makes subsequent output a light cyan color.
Definition game_utils.c:801
void printArtifactStats(Artifact *a)
Takes a Artifact pointer and prints most of its values formatted.
Definition game_utils.c:1889
void updateCounters(Turncounter *counters[], int isEnemy, Fighter *f, Enemy *e)
Takes a Turncounter array, an integer, a Fighter pointer and an Enemy pointer.
Definition game_utils.c:3250
int scanWincon(void)
Asks for an integer on stdin and returns it if it's a valid winconClass index.
Definition game_utils.c:1345
void pickClass(Fighter *player)
Takes a Fighter pointer and sets the class value after asking the user for input.
Definition game_utils.c:1258
int giveXp(Fighter *player, Enemy *e)
Takes a Fighter and a Enemy pointers and handles xp gain by fighter.
Definition game_utils.c:3423
void dbg_Path(Path *path)
Debugs the passed (preallocated) Path with log_tag().
Definition game_utils.c:315
void dropEquip(Fighter *player, int beast, Koliseo *kls, RingaBuf *rb_notifications)
Takes a Fighter pointer value and an integer indicating if the drop was from a beast enemy,...
Definition game_utils.c:1919
void printVersion(void)
Prints version string.
Definition game_utils.c:1032
int dropConsumable(Fighter *player)
Takes a Fighter pointer value and adds a random Consumable to consumablesBag.
Definition game_utils.c:1648
void red(void)
Prints an ASCII escape code that makes subsequent output a thick red color.
Definition game_utils.c:697
void dbg_countStats(countStats *stats)
Debugs the passed (preallocated) countStats with log_tag().
Definition game_utils.c:352
void freeRoom(Room *room)
Frees the memory allocated for the passed room pointer.
Definition game_utils.c:913
int giveXp_Boss(Fighter *player, Boss *b)
Takes a Fighter and a Boss pointers and handles xp gain by fighter.
Definition game_utils.c:3440
int hlpd_getopt(size_t argc, char **argv, const char *whoami)
Parse options from CLI arguments.
Definition game_utils.c:3897
void lightPurple(void)
Prints an ASCII escape code that makes subsequent output a light purple color.
Definition game_utils.c:785
void hlpd_dbg_features(void)
Prints configuration info.
Definition game_utils.c:1049
void dbg_print_explored_layout(Floor *floor)
Logs explored layout for passed Floor.
Definition game_utils.c:417
int dropArtifact(Fighter *player)
Takes a Fighter pointer value and adds a random Artifact to artifactsBag.
Definition game_utils.c:1680
void updateCounters_Boss(Turncounter *counters[], int isBoss, Fighter *f, Boss *b)
Takes a Turncounter array, an integer, a Fighter pointer and a Boss pointer.
Definition game_utils.c:3320
void pickName(Fighter *player)
Takes a Fighter pointer and sets the name value after asking the user for input.
Definition game_utils.c:1367
int display_colorpairs(void)
Runs some shell commands to see all color pairs, then returns exitcode Prints the encoded value of th...
Definition game_utils.c:3877
saveType saveTypeFrom_string(char *s)
Takes a string and returns the corresponding saveType.
Definition game_utils.c:677
void initWincon(Wincon *w, Path *p, winconClass class)
Takes a Wincon and a Path pointers and a winconClass and initialises the passed Wincon.
Definition game_utils.c:110
int getEnemyXpGain(Enemy *e)
Takes a Enemy pointer and returns its xp gain as sum of xp field value and level.
Definition game_utils.c:3454
void hlpd_use_forced_flags(const char *whoami)
Definition game_utils.c:3787
void resetPermboosts(Fighter *f)
Takes a Fighter pointer and resets all of its permboost_STAT values to 0, also correctly updating the...
Definition game_utils.c:1519
void dbg_print_roomclass_layout(Floor *floor)
Logs roomclass layout for passed Floor.
Definition game_utils.c:441
Path * randomise_path(char *seed, Koliseo *kls, const char *path_to_savefile)
Takes an integer seed and returns a Path pointer.
Definition game_utils.c:1780
void loadLore(char **lore_strings, int loreKind)
Loads the lore strings from the appropriate lore file.
Definition game_utils.c:844
void dbg_Wincon(Wincon *wc)
Debugs the passed (preallocated) Wincon with log_tag().
Definition game_utils.c:334
void lightRed(void)
Prints an ASCII escape code that makes subsequent output a light red color.
Definition game_utils.c:705
void dbg_GameScreen(GameScreen *scr)
Debugs the passed (preallocated) GameScreen with log_tag().
Definition game_utils.c:582
void printClasses(void)
Prints all the fighterClass values as integers and as strings.
Definition game_utils.c:1218
void resetArtifactsState(Fighter *f)
Takes a Fighter pointer and Resets the active value for each Artifact in the fighter's artifactsBag a...
Definition game_utils.c:1597
void copy_fighter(Fighter *source, Fighter *dest)
Takes two Fighter pointers, with all their fields already allocated, and copies all valaues from sour...
Definition game_utils.c:1448
void green(void)
Prints an ASCII escape code that makes subsequent output a thick green color.
Definition game_utils.c:729
void lightBlue(void)
Prints an ASCII escape code that makes subsequent output a light blue color.
Definition game_utils.c:769
int getConsumableQty(Fighter *f, int n)
Takes a Fighter pointer and an integer denoting the consumableClass and returns the respective qty va...
Definition game_utils.c:1704
int onLevelUp(Fighter *player)
Takes a Fighter pointer and updated its stats.
Definition game_utils.c:3490
void printWincons(void)
Prints all the winconClass values as integers and as strings.
Definition game_utils.c:1327
void b_death(Boss *b)
Takes a Boss pointer and frees its allocated memory.
Definition game_utils.c:3209
void printConsumablesStats(Consumable *c)
Takes a Consumable pointer and prints most of its values formatted.
Definition game_utils.c:1879
void emptyConsumables(Fighter *player)
Sets the qty value to 0 for all Consumable in f's consumablesBag with positive qty.
Definition game_utils.c:1718
void dbg_Gamestate(Gamestate *gmst)
Debugs the passed (preallocated) Gamestate with log_tag().
Definition game_utils.c:501
void cyan(void)
Prints an ASCII escape code that makes subsequent output a thick cyan color.
Definition game_utils.c:793
void lightGreen(void)
Prints an ASCII escape code that makes subsequent output a light green color.
Definition game_utils.c:737
void dbg_print_floor_layout(Floor *floor)
Logs floor layout for passed Floor.
Definition game_utils.c:393
void update_Gamestate(Gamestate *gmst, int current_fighters, roomClass current_roomtype, int current_room_index, int current_enemy_index, Floor *current_floor, Room *current_room, GameOptions *game_options)
Updates the passed (preallocated) Gamestate with the passed int values.
Definition game_utils.c:628
void printFormattedVersion(const char *progname)
Prints formatted version string.
Definition game_utils.c:1041
void setRoomType(Path *path, int *roadFork_value, roomClass *room_type, int roomsDone)
Takes a path pointer, a roadFork value pointer, a room type pointer, and an integer.
Definition game_utils.c:890
void emptyArtifacts(Fighter *player)
Takes a Fighter pointer and sets the qty value to 0 and the active flag to false for all Artifacts in...
Definition game_utils.c:1735
void printTitle(void)
Prints an hardcoded title screen.
Definition game_utils.c:1017
turnOption getTurnChoice(char *ch)
Returns the chosen option as a turnOption.
Definition game_utils.c:3597
void init_game_color_pairs(void)
void death(Fighter *player, loadInfo *load_info)
Takes a Fighter and loadInfo pointers and prints fighter stats and a game over message.
Definition game_utils.c:3035
void emptyEquips(Fighter *player)
Takes a Fighter pointer and, for all Equip in equipsBag field with positive qty, sets qty to 0 and fr...
Definition game_utils.c:1751
void scanName(Fighter *player)
Takes a Fighter pointer and sets its name value to the string provided on stdin.
Definition game_utils.c:1203
void printStats(Fighter *f)
Takes a Fighter pointer and prints most of its values formatted.
Definition game_utils.c:1831
void dbg_Saveslot(Saveslot *saveslot)
Debugs the passed (preallocated) Saveslot with log_tag().
Definition game_utils.c:298
int getBossXpGain(Boss *b)
Takes a Boss pointer and returns its xp gain as sum of xp field value and level.
Definition game_utils.c:3467
Represents the entity initialised from a artifactClass.
Definition game_core.h:551
Represents the entity initialised from a bossClass.
Definition game_core.h:1204
Holds a certain qty of a consumableClass.
Definition game_core.h:502
Represents the entity initialised from a enemyClass.
Definition game_core.h:1163
Represents the entity initialised from a equipClass.
Definition game_core.h:1416
Represents the entity initialised from a fighterClass.
Definition game_core.h:1093
Represents the entity initialised from a floorClass.
Definition game_core.h:1825
Holds options useful for user runtime preferences.
Definition game_core.h:201
Holds arguments for a game.
Definition game_core.h:1899
Holds the state of game progression.
Definition game_core.h:1357
Represents the entity initialised from a roomClass.
Definition game_core.h:1770
Holds arguments for a saveslot.
Definition game_core.h:410
Represents the entity initialised from a CounterType.
Definition game_core.h:1303
Defines the entity initialised from a winconClass.
Definition game_core.h:1338
Holds the lifetime stats of the player.
Definition game_core.h:781
Contains loaded values to pass to initRoom().
Definition game_core.h:1878
Holds arguments for a turnOption_OP.
Definition game_core.h:2104