home · contact · privacy
The player is now a map object like any other. All actor contacts now lead to violenc...
[plomrogue] / src / misc.c
1 /* misc.c */
2
3 #include "misc.h"
4 #include <errno.h> /* for errno */
5 #include <unistd.h> /* for unlink(), acess() */
6 #include <stdlib.h> /* for size_t, calloc(), free() */
7 #include <string.h> /* for strlen(), strcmp(), memcpy() */
8 #include <stdint.h> /* for uint8_t, uint16_t */
9 #include "readwrite.h" /* for [read/write]_uint[8/16/32][_bigendian](),
10                         * try_fopen(), try_fclose()
11                         */
12 #include "map_objects.h" /* for struct MapObj, read_map_objects(),
13                           * write_map_objects()
14                           */
15 #include "map_object_actions.h" /* for is_passable(), move_actor() */
16 #include "map.h" /* for Map struct */
17 #include "main.h" /* for World struct */
18 #include "yx_uint16.h" /* for yx_uint16 struct */
19 #include "rrand.h" /* for rrand(), rrand_seed() */
20 #include "rexit.h" /* for exit_err() */
21 #include "wincontrol.h" /* for init_winconfs(), init_wins(), free_winconfs(),
22                          * sorted_wintoggle_and_activate()
23                          */
24 #include "windows.h" /* for suspend_win() */
25
26
27 extern char * trouble_msg(struct World * w, char * parent, char * child)
28 {
29     char * p1 = "Trouble in ";
30     char * p2 = " with ";
31     char * p3 = ".";
32     uint16_t size = strlen(p1) + strlen(parent) + strlen(p2) + strlen(child)
33                     + strlen(p3) + 1;
34     char * msg = malloc(size);
35     exit_err(NULL == msg, w, "malloc() in trouble_msg() failed.");
36     sprintf(msg, "%s%s%s%s%s", p1, parent, p2, child, p3);
37     return msg;
38 }
39
40
41
42 extern void * try_malloc(size_t size, struct World * w, char * f)
43 {
44     char * msg = trouble_msg(w, f, "malloc()");
45     void * p = malloc(size);
46     exit_err(NULL == p, w, msg);
47     free(msg);
48     return p;
49 }
50
51
52
53 extern void * try_calloc(size_t size1, size_t size2, struct World * w, char * f)
54 {
55     char * msg = trouble_msg(w, f, "calloc()");
56     void * p = calloc(size1, size2);
57     exit_err(NULL == p, w, msg);
58     free(msg);
59     return p;
60 }
61
62
63
64 extern void check_files_xor(char * p1, char * p2, struct World * w)
65 {
66     char * msg1 = "A file '";
67     char * msg2 = "' exists, but no file '";
68     char * msg3 = "'. If everything was in order, both or noe would exist. "
69                   "The game won't start until this is corrected.";
70     uint16_t size = strlen(msg1) + strlen(p1) + strlen(msg2) + strlen(p2)
71                     + strlen(msg3);
72     char msg[size];
73     if      (!access(p1, F_OK) && access(p2, F_OK))
74     {
75         sprintf(msg, "%s%s%s%s%s", msg1, p1, msg2, p2, msg3);
76         errno = 0;
77         exit_err(1, w, msg);
78     }
79     else if (access(p1, F_OK) && !access(p2, F_OK))
80     {
81         sprintf(msg, "%s%s%s%s%s", msg1, p2, msg2, p1, msg3);
82         errno = 0;
83         exit_err(1, w, msg);
84     }
85 }
86
87
88
89 extern void check_tempfile(char * path, struct World * w)
90 {
91     char * msg1 = "A file '";
92     char * msg2 = "' exists, probably from a corrupted previous file saving "
93                 "process. To avoid corruption of game files, the game won't  "
94                 "start until it is removed or renamed.";
95     uint16_t size = strlen(msg1) + strlen(path) + strlen(msg2);
96     char msg[size];
97     sprintf(msg, "%s%s%s", msg1, path, msg2);
98     exit_err(!access(path, F_OK), w, msg);
99 }
100
101
102
103 extern void save_interface_conf(struct World * world)
104 {
105     save_keybindings(world, "config/keybindings_global", &world->kb_global);
106     save_keybindings(world, "config/keybindings_wingeom", &world->kb_wingeom);
107     save_keybindings(world, "config/keybindings_winkeys", &world->kb_winkeys);
108     save_win_configs(world);
109 }
110
111
112
113 extern void load_interface_conf(struct World * world)
114 {
115     init_keybindings(world, "config/keybindings_global",  &world->kb_global);
116     init_keybindings(world, "config/keybindings_wingeom", &world->kb_wingeom);
117     init_keybindings(world, "config/keybindings_winkeys", &world->kb_winkeys);
118     init_winconfs(world);
119     init_wins(world);
120     sorted_wintoggle_and_activate(world);
121 }
122
123
124
125 extern void unload_interface_conf(struct World * world)
126 {
127     free_keybindings(world->kb_global.kbs);
128     free_keybindings(world->kb_wingeom.kbs);
129     free_keybindings(world->kb_winkeys.kbs);
130     while (0 != world->wmeta->active)
131     {
132         suspend_win(world->wmeta, world->wmeta->active);
133     }
134     free_winconfs(world);
135 }
136
137
138
139 extern void update_log(struct World * world, char * text)
140 {
141     char * f_name = "update_log()";
142     static char * last_msg;                 /* TODO: valgrind is dissatisfied */
143     if (0 == last_msg)                      /* with this calloc'd pointer not */
144     {                                       /* being freed. Rectify this?     */
145         last_msg = try_calloc(1, sizeof(char), world, f_name);
146     }
147     char * new_text;
148     uint16_t len_old = strlen(world->log);
149     if (0 == strcmp(last_msg, text))
150     {
151         uint16_t len_whole = len_old + 1;
152         new_text = try_calloc(len_whole + 1, sizeof(char), world, f_name);
153         memcpy(new_text, world->log, len_old);
154         memcpy(new_text + len_old, ".", 1);
155     }
156     else
157     {
158         uint16_t len_new = strlen(text);
159         uint16_t len_whole = len_old + len_new + 1;
160         new_text = try_calloc(len_whole, sizeof(char), world, f_name);
161         memcpy(new_text, world->log, len_old);
162         memcpy(new_text + len_old, text, len_new);
163         last_msg = try_calloc(len_new + 1, sizeof(char), world, f_name);
164         memcpy(last_msg, text, len_new);
165     }
166     free(world->log);
167     world->log = new_text;
168 }
169
170
171
172 extern uint16_t center_offset(uint16_t pos, uint16_t mapsize,
173                               uint16_t framesize)
174 {
175     uint16_t offset = 0;
176     if (mapsize > framesize)
177     {
178         if (pos > framesize / 2)
179         {
180             if (pos < mapsize - (framesize / 2))
181             {
182                 offset = pos - (framesize / 2);
183             }
184             else
185             {
186                 offset = mapsize - framesize;
187             }
188         }
189     }
190     return offset;
191 }
192
193
194
195 extern void turn_over(struct World * world, char action)
196 {
197     char * f_name = "turn_over()";
198     char * err_write = "Trouble in turn_over() with write_uint8() "
199                        "writing to opened file 'record_tmp'.";
200
201     char * recordfile_tmp = "record_tmp";
202     char * recordfile     = "record";
203     if (1 == world->interactive)
204     {
205         FILE * file_old = try_fopen(recordfile,     "r", world, f_name);
206         FILE * file_new = try_fopen(recordfile_tmp, "w", world, f_name);
207         char c = fgetc(file_old);
208         while (EOF != c)
209         {
210             exit_err(write_uint8(c, file_new), world, err_write);
211             c = fgetc(file_old);
212         }
213         try_fclose(file_old, world, f_name);
214         exit_err(write_uint8(action, file_new), world, err_write);
215         try_fclose_unlink_rename(file_new, recordfile_tmp, recordfile,
216                                  world, f_name);
217     }
218     world->turn++;
219     rrand_seed(world->seed * world->turn);
220     struct MapObj * monster;
221     for (monster = world->map_objs;
222          monster != 0;
223          monster = monster->next)
224     {
225         if (0 < monster->lifepoints && 0 != monster->id)
226         {
227             move_actor(world, monster, rrand() % 5);
228         }
229     }
230 }
231
232
233
234 extern void save_game(struct World * world)
235 {
236     char * f_name = "save_game()";
237
238     char * savefile_tmp = "savefile_tmp";
239     char * savefile     = "savefile";
240     FILE * file = try_fopen(savefile_tmp, "w", world, f_name);
241
242     char line[12];
243     sprintf(line, "%d\n", world->seed);
244     try_fwrite(line, strlen(line), 1, file, world, f_name);
245     sprintf(line, "%d\n", world->turn);
246     try_fwrite(line, strlen(line), 1, file, world, f_name);
247     sprintf(line, "%d\n", world->score);
248     try_fwrite(line, strlen(line), 1, file, world, f_name);
249     write_map_objects(world, file);
250
251     try_fclose_unlink_rename(file, savefile_tmp, savefile, world, f_name);
252 }
253
254
255
256 extern void load_game(struct World * world)
257 {
258     char * f_name = "load_game2()";
259     char * filename = "savefile";
260     FILE * file = try_fopen(filename, "r", world, f_name);
261     uint16_t linemax = get_linemax(file, world, f_name);
262     char line[linemax + 1];
263     try_fgets(line, linemax + 1, file, world, f_name);
264     world->seed = atoi(line);
265     try_fgets(line, linemax + 1, file, world, f_name);
266     world->turn = atoi(line);
267     try_fgets(line, linemax + 1, file, world, f_name);
268     world->score = atoi(line);
269     read_map_objects(world, file, line, linemax);
270     try_fclose(file, world, f_name);
271 }
272
273
274
275 extern struct yx_uint16 find_passable_pos(struct Map * map)
276 {
277     struct yx_uint16 pos;
278     for (pos.y = pos.x = 0; 0 == is_passable(map, pos);)
279     {
280         pos.y = rrand() % map->size.y;
281         pos.x = rrand() % map->size.x;
282     }
283     return pos;
284 }