1 /*
2  * game.h
3  * Copyright (C) 2009-2020 Joachim de Groot <jdegroot@web.de>
4  *
5  * NLarn is free software: you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * NLarn is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef __GAME_H_
20 #define __GAME_H_
21 
22 #include "inventory.h"
23 #include "items.h"
24 #include "map.h"
25 #include "player.h"
26 #include "spheres.h"
27 
28 #define TIMELIMIT 30000 /* maximum number of moves before the game is called */
29 
30 /* internal counter for save file compatibility */
31 #define SAVEFILE_VERSION    26
32 
33 /* the world as we know it */
34 typedef struct game
35 {
36     player *p;                  /* the player */
37     map *maps[MAP_MAX];         /* the dungeon */
38     guint8 version;             /* save compatibility value */
39     guint64 time_start;         /* start time */
40     guint32 gtime;              /* turn count */
41     guint8 difficulty;          /* game difficulty */
42     message_log *log;           /* game message log */
43 
44     /* stock of the dnd store */
45     inventory *store_stock;
46     /* stock of the monastery */
47     inventory *monastery_stock;
48 
49     /* storage of player's home */
50     inventory *player_home;
51 
52     /* item / monster status */
53     int amulet_created[AM_MAX];
54     int armour_created[AT_MAX];
55     int weapon_created[WT_MAX];
56     int monster_genocided[MT_MAX];
57 
58     /* Item obfuscation mappings */
59     int amulet_material_mapping[AM_MAX];
60     int potion_desc_mapping[PO_MAX];
61     int ring_material_mapping[RT_MAX];
62     int scroll_desc_mapping[ST_MAX];
63     int book_desc_mapping[SP_MAX];
64 
65     /* these are the item ids assigned to new objects of the latter types */
66 
67     guint item_max_id;
68     guint effect_max_id;
69     guint monster_max_id;
70 
71     /* every object of the types item, effect and monster will be registered
72        in these hashed when created and unregistered when destroyed. */
73 
74     GHashTable *items;
75     GHashTable *effects;
76     GHashTable *monsters;
77 
78     /* Monsters that died during a turn have to be added to this array
79        to allow destroying them after all monsters have been moved.
80        The functions used to iterate over the GHashTable *monsters above
81        do not allow to modify the hash table while iterating over it,
82        giving the most nasty effects when doing so.
83      */
84     GPtrArray *dead_monsters;
85 
86     /* spheres do not need to be referenced, thus a pointer array is sufficient */
87     GPtrArray *spheres;
88 
89     /* flags */
90     guint32
91         player_stats_set: 1, /* the player's stats have been assigned */
92         cure_dianthr_created: 1, /* the potion of cure dianthroritis is a unique item */
93         wizard: 1, /* wizard mode */
94         fullvis: 1, /* show entire map in wizard mode */
95         autosave: 1; /* save the game when entering a new map */
96 } game;
97 
98 
99 /* forward declarations */
100 
101 struct game_config;
102 
103 
104 /* function declarations */
105 
106 /**
107  * @brief Initialise the game. This function will try to restore a saved game;
108  *        if it fails it will start a new game.
109  *
110  * @param pointer to a parsed command line configuration
111  */
112 void game_init(struct game_config *config);
113 
114 game *game_destroy(game *g);
115 
116 /**
117  * @brief Save a game.
118  * @param The game to save
119  * @param The name of the file to be saved. Defaults to "nlarn.sav",
120  *        if a NULL has been supplied.
121  */
122 int game_save(game *g);
123 
124 map *game_map(game *g, guint nmap);
125 void game_spin_the_wheel(game *g);
126 void game_remove_dead_monsters(game *g);
127 
128 /* functions to store game data */
129 gpointer game_inventory_register(game *g, inventory *inv);
130 void game_inventory_unregister(game *g, gpointer inv);
131 inventory *game_inventory_get(game *g, gpointer id);
132 
133 gpointer game_item_register(game *g, item *it);
134 void game_item_unregister(game *g, gpointer it);
135 item *game_item_get(game *g, gpointer id);
136 
137 gpointer game_effect_register(game *g, effect *e);
138 void game_effect_unregister(game *g, gpointer e);
139 effect *game_effect_get(game *g, gpointer id);
140 
141 gpointer game_monster_register(game *g, monster *m);
142 void game_monster_unregister(game *g, gpointer m);
143 monster *game_monster_get(game *g, gpointer id);
144 
145 void game_delete_savefile();
146 
147 /* macros */
148 
149 #define game_difficulty(g) ((g)->difficulty)
150 #define game_wizardmode(g) ((g)->wizard)
151 #define game_fullvis(g)    ((g)->fullvis)
152 #define game_autosave(g)   ((g)->autosave)
153 
154 #define game_turn(g)            ((g)->gtime)
155 #define game_remaining_turns(g) (((g)->gtime > TIMELIMIT) ? 0 : TIMELIMIT - (g)->gtime)
156 
157 /* gtime <> mobuls conversion */
158 #define gtime2mobuls(gtime)  ((abs(((int)gtime)) + 99) / 100)
159 #define mobuls2gtime(mobuls) ((int)(mobuls) * 100)
160 
161 #endif
162