home · contact · privacy
Moved meta_keys() into new library "control" to soon include all key-press processing.
[plomrogue] / src / misc.c
1 /* misc.c */
2
3 #include "misc.h"
4 #include <stdio.h> /* for rename() */
5 #include <unistd.h> /* for unlink(), acess() */
6 #include <stdlib.h> /* for calloc(), free() */
7 #include <string.h> /* for strlen(), strcmp(), memcpy() */
8 #include <stdint.h> /* for uint8_t */
9 #include "windows.h" /* for suspend_win(), append_win(), reset_pad_offset(),
10                       * resize_active_win(), cycle_active_win(),
11                       * shift_active_win(), struct Win, struct WinMeta
12                       */
13 #include "keybindings.h" /* for get_action_key(), save_keybindings(),
14                           * keyswin_move_selection(), keyswin_mod_key()
15                           */
16 #include "readwrite.h" /* for [read/write]_uint[8/16/32][_bigendian]() */
17 #include "map_objects.h" /* for struct Monster, write_map_objects(), */
18 #include "map_object_actions.h" /* for is_passable(), move_monster() */
19 #include "map.h" /* for map_scroll(),map_center_player(), Map struct,dir enum */
20 #include "main.h" /* for World struct */
21 #include "yx_uint16.h" /* for yx_uint16 */
22 #include "rrand.h" /* for rrand(), rrand_seed() */
23 #include "rexit.h" /* for exit_err() */
24
25
26 extern void textfile_sizes(FILE * file, uint16_t * linemax_p,
27                            uint16_t * n_lines_p)
28 {
29     uint16_t n_lines = 0;
30     int c = 0;
31     uint16_t linemax = 0;
32     uint16_t c_count = 0;
33     while (EOF != c)
34     {
35         c_count++;
36         c = getc(file);
37         if ('\n' == c)
38         {
39             if (c_count > linemax)
40             {
41                 linemax = c_count + 1;
42             }
43             c_count = 0;
44             if (n_lines_p)
45             {
46                 n_lines++;
47             }
48         }
49     }
50     fseek(file, 0, SEEK_SET);
51     * linemax_p = linemax;
52     if (n_lines_p)
53     {
54         * n_lines_p = n_lines;
55     }
56 }
57
58
59
60 extern void update_log(struct World * world, char * text)
61 {
62     static char * last_msg;
63     if (0 == last_msg)
64     {
65         last_msg = calloc(1, sizeof(char));
66     }
67     char * new_text;
68     uint16_t len_old = strlen(world->log);
69     if (0 == strcmp(last_msg, text))
70     {
71         uint16_t len_whole = len_old + 1;
72         new_text = calloc(len_whole + 1, sizeof(char));
73         memcpy(new_text, world->log, len_old);
74         memcpy(new_text + len_old, ".", 1);
75     }
76     else
77     {
78         uint16_t len_new = strlen(text);
79         uint16_t len_whole = len_old + len_new + 1;
80         new_text = calloc(len_whole, sizeof(char));
81         memcpy(new_text, world->log, len_old);
82         memcpy(new_text + len_old, text, len_new);
83         last_msg = calloc(len_new + 1, sizeof(char));
84         memcpy(last_msg, text, len_new);
85     }
86     free(world->log);
87     world->log = new_text;
88 }
89
90
91
92 extern uint16_t center_offset(uint16_t pos, uint16_t mapsize,
93                               uint16_t framesize)
94 {
95     uint16_t offset = 0;
96     if (mapsize > framesize)
97     {
98         if (pos > framesize / 2)
99         {
100             if (pos < mapsize - (framesize / 2))
101             {
102                 offset = pos - (framesize / 2);
103             }
104             else
105             {
106                 offset = mapsize - framesize;
107             }
108         }
109     }
110     return offset;
111 }
112
113
114
115 extern void turn_over(struct World * world, char action)
116 {
117     char * err_open  = "Trouble in turn_over() with fopen() "
118                        "opening file 'record_tmp' for appending.";
119     char * err_write = "Trouble in turn_over() with write_uint8() "
120                        "writing to opened file 'record_tmp'.";
121     char * err_close = "Trouble in turn_over() with fclose() "
122                        "closing opened file 'record'.";
123     char * err_unl   = "Trouble in turn_over() with unlink() "
124                        "unlinking old file 'record'.";
125     char * err_move  = "Trouble in turn_over() with rename() "
126                        "renaming file 'record_tmp' to 'record'.";
127     char * recordfile_tmp = "record_tmp";
128     char * recordfile     = "record";
129     if (1 == world->interactive)
130     {
131         FILE * file_old = fopen(recordfile, "r");
132         FILE * file_new = fopen(recordfile_tmp, "w");
133         exit_err(0 == file_old, world, err_open);
134         char c = fgetc(file_old);
135         while (EOF != c)
136         {
137             exit_err(write_uint8(c, file_new), world, err_write);
138             c = fgetc(file_old);
139         }
140         exit_err(fclose(file_old), world, err_close);
141         exit_err(write_uint8(action, file_new), world, err_write);
142         err_close = "Trouble in turn_over() with fclose() "
143                     "closing opened file 'record_tmp'.";
144         exit_err(fclose(file_new), world, err_close);
145         exit_err(unlink(recordfile), world, err_unl);
146         exit_err(rename(recordfile_tmp, recordfile), world, err_move);
147     }
148     world->turn++;
149     rrand_seed(world->seed * world->turn);
150     struct Monster * monster;
151     for (monster = world->monster;
152          monster != 0;
153          monster = monster->map_obj.next)
154     {
155         move_monster(world, monster);
156     }
157 }
158
159
160
161 extern void save_game(struct World * world)
162 {
163     char * err_open  = "Trouble in save_game() with fopen() "
164                        "opening file 'savefile_tmp' for writing.";
165     char * err_write = "Trouble in save_game() "
166                        "writing to opened file 'savefile_tmp'.";
167     char * err_close = "Trouble in save_game() with fclose() "
168                        "closing opened file 'savefile_tmp'.";
169     char * err_unl   = "Trouble in save_game() with unlink() "
170                        "unlinking old 'savefile' file.";
171     char * err_move  = "Trouble in save_game() with rename() "
172                        "renaming 'file savefile_tmp' to 'savefile'.";
173     char * savefile_tmp = "savefile_tmp";
174     char * savefile     = "savefile";
175     FILE * file = fopen(savefile_tmp, "w");
176     exit_err(0 == file, world, err_open);
177     if (   write_uint32_bigendian(world->seed, file)
178         || write_uint32_bigendian(world->turn, file)
179         || write_uint16_bigendian(world->score, file)
180         || write_uint16_bigendian(world->player->pos.y + 1, file)
181         || write_uint16_bigendian(world->player->pos.x + 1, file)
182         || write_uint8(world->player->hitpoints, file)
183         || write_map_objects(world, world->monster, file)
184         || write_map_objects(world, world->item, file))
185     {
186         exit_err(1, world, err_write);
187     }
188     exit_err(fclose(file), world, err_close);
189     if (!access(savefile, F_OK))
190     {
191         exit_err(unlink(savefile), world, err_unl);
192     }
193     exit_err(rename(savefile_tmp, savefile), world, err_move);
194 }
195
196
197
198 extern uint8_t toggle_window(struct WinMeta * win_meta, struct Win * win)
199 {
200     if (0 != win->frame.curses_win)
201     {
202         return suspend_win(win_meta, win);
203     }
204     else
205     {
206         return append_win(win_meta, win);
207     }
208 }
209
210
211
212 extern void scroll_pad(struct WinMeta * win_meta, char dir)
213 {
214     if      ('+' == dir)
215     {
216         reset_pad_offset(win_meta, win_meta->pad_offset + 1);
217     }
218     else if ('-' == dir)
219     {
220         reset_pad_offset(win_meta, win_meta->pad_offset - 1);
221     }
222 }
223
224
225
226 extern uint8_t growshrink_active_window(struct WinMeta * win_meta, char change)
227 {
228     if (0 != win_meta->active)
229     {
230         struct yx_uint16 size = win_meta->active->frame.size;
231         if      (change == '-')
232         {
233             size.y--;
234         }
235         else if (change == '+')
236         {
237             size.y++;
238         }
239         else if (change == '_')
240         {
241             size.x--;
242         }
243         else if (change == '*')
244         {
245             size.x++;
246         }
247         return resize_active_win (win_meta, size);
248     }
249     return 0;
250 }
251
252
253
254 extern struct yx_uint16 find_passable_pos(struct Map * map)
255 {
256     struct yx_uint16 pos;
257     for (pos.y = pos.x = 0; 0 == is_passable(map, pos);)
258     {
259         pos.y = rrand() % map->size.y;
260         pos.x = rrand() % map->size.x;
261     }
262     return pos;
263 }