home · contact · privacy
Improved error handling, more error catching, error messages.
[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, uint16_t */
9 #include "readwrite.h" /* for [read/write]_uint[8/16/32][_bigendian]() */
10 #include "map_objects.h" /* for struct Monster, write_map_objects(), */
11 #include "map_object_actions.h" /* for is_passable(), move_monster() */
12 #include "map.h" /* for Map struct */
13 #include "main.h" /* for World struct */
14 #include "yx_uint16.h" /* for yx_uint16 struct */
15 #include "rrand.h" /* for rrand(), rrand_seed() */
16 #include "rexit.h" /* for exit_err() */
17
18
19
20 extern uint8_t textfile_sizes(FILE * file, uint16_t * linemax_p,
21                               uint16_t * n_lines_p)
22 {
23     uint16_t n_lines = 0;
24     int c = 0;
25     uint16_t linemax = 0;
26     uint16_t c_count = 0;
27     while (EOF != c)
28     {
29         c_count++;
30         c = getc(file);
31         if ('\n' == c)
32         {
33             if (c_count > linemax)
34             {
35                 linemax = c_count + 1;
36             }
37             c_count = 0;
38             if (n_lines_p)
39             {
40                 n_lines++;
41             }
42         }
43     }
44     if (-1 == fseek(file, 0, SEEK_SET))
45     {
46         return 1;
47     }
48     * linemax_p = linemax;
49     if (n_lines_p)
50     {
51         * n_lines_p = n_lines;
52     }
53     return 0;
54 }
55
56
57
58 extern void update_log(struct World * world, char * text)
59 {
60     static char * last_msg;                 /* TODO: valgrind is dissatisfied */
61     if (0 == last_msg)                      /* with this calloc'd pointer     */
62     {                                       /* never being freed.             */
63         last_msg = calloc(1, sizeof(char)); /* Rectify this ?                 */
64     }
65     char * new_text;
66     uint16_t len_old = strlen(world->log);
67     if (0 == strcmp(last_msg, text))
68     {
69         uint16_t len_whole = len_old + 1;
70         new_text = calloc(len_whole + 1, sizeof(char));
71         memcpy(new_text, world->log, len_old);
72         memcpy(new_text + len_old, ".", 1);
73     }
74     else
75     {
76         uint16_t len_new = strlen(text);
77         uint16_t len_whole = len_old + len_new + 1;
78         new_text = calloc(len_whole, sizeof(char));
79         memcpy(new_text, world->log, len_old);
80         memcpy(new_text + len_old, text, len_new);
81         last_msg = calloc(len_new + 1, sizeof(char));
82         memcpy(last_msg, text, len_new);
83     }
84     free(world->log);
85     world->log = new_text;
86 }
87
88
89
90 extern uint16_t center_offset(uint16_t pos, uint16_t mapsize,
91                               uint16_t framesize)
92 {
93     uint16_t offset = 0;
94     if (mapsize > framesize)
95     {
96         if (pos > framesize / 2)
97         {
98             if (pos < mapsize - (framesize / 2))
99             {
100                 offset = pos - (framesize / 2);
101             }
102             else
103             {
104                 offset = mapsize - framesize;
105             }
106         }
107     }
108     return offset;
109 }
110
111
112
113 extern void turn_over(struct World * world, char action)
114 {
115     char * err_open  = "Trouble in turn_over() with fopen() "
116                        "opening file 'record_tmp' for appending.";
117     char * err_write = "Trouble in turn_over() with write_uint8() "
118                        "writing to opened file 'record_tmp'.";
119     char * err_close = "Trouble in turn_over() with fclose() "
120                        "closing opened file 'record'.";
121     char * err_unl   = "Trouble in turn_over() with unlink() "
122                        "unlinking old file 'record'.";
123     char * err_move  = "Trouble in turn_over() with rename() "
124                        "renaming file 'record_tmp' to 'record'.";
125     char * recordfile_tmp = "record_tmp";
126     char * recordfile     = "record";
127     if (1 == world->interactive)
128     {
129         FILE * file_old = fopen(recordfile, "r");
130         FILE * file_new = fopen(recordfile_tmp, "w");
131         exit_err(0 == file_old, world, err_open);
132         char c = fgetc(file_old);
133         while (EOF != c)
134         {
135             exit_err(write_uint8(c, file_new), world, err_write);
136             c = fgetc(file_old);
137         }
138         exit_err(fclose(file_old), world, err_close);
139         exit_err(write_uint8(action, file_new), world, err_write);
140         err_close = "Trouble in turn_over() with fclose() "
141                     "closing opened file 'record_tmp'.";
142         exit_err(fclose(file_new), world, err_close);
143         exit_err(unlink(recordfile), world, err_unl);
144         exit_err(rename(recordfile_tmp, recordfile), world, err_move);
145     }
146     world->turn++;
147     rrand_seed(world->seed * world->turn);
148     struct Monster * monster;
149     for (monster = world->monster;
150          monster != 0;
151          monster = monster->map_obj.next)
152     {
153         move_monster(world, monster);
154     }
155 }
156
157
158
159 extern void save_game(struct World * world)
160 {
161     char * err_open  = "Trouble in save_game() with fopen() "
162                        "opening file 'savefile_tmp' for writing.";
163     char * err_write = "Trouble in save_game() "
164                        "writing to opened file 'savefile_tmp'.";
165     char * err_close = "Trouble in save_game() with fclose() "
166                        "closing opened file 'savefile_tmp'.";
167     char * err_unl   = "Trouble in save_game() with unlink() "
168                        "unlinking old 'savefile' file.";
169     char * err_move  = "Trouble in save_game() with rename() "
170                        "renaming 'file savefile_tmp' to 'savefile'.";
171     char * savefile_tmp = "savefile_tmp";
172     char * savefile     = "savefile";
173     FILE * file = fopen(savefile_tmp, "w");
174     exit_err(0 == file, world, err_open);
175     if (   write_uint32_bigendian(world->seed, file)
176         || write_uint32_bigendian(world->turn, file)
177         || write_uint16_bigendian(world->score, file)
178         || write_uint16_bigendian(world->player->pos.y + 1, file)
179         || write_uint16_bigendian(world->player->pos.x + 1, file)
180         || write_uint8(world->player->hitpoints, file)
181         || write_map_objects(world, world->monster, file)
182         || write_map_objects(world, world->item, file))
183     {
184         exit_err(1, world, err_write);
185     }
186     exit_err(fclose(file), world, err_close);
187     if (!access(savefile, F_OK))
188     {
189         exit_err(unlink(savefile), world, err_unl);
190     }
191     exit_err(rename(savefile_tmp, savefile), world, err_move);
192 }
193
194
195
196 extern struct yx_uint16 find_passable_pos(struct Map * map)
197 {
198     struct yx_uint16 pos;
199     for (pos.y = pos.x = 0; 0 == is_passable(map, pos);)
200     {
201         pos.y = rrand() % map->size.y;
202         pos.x = rrand() % map->size.x;
203     }
204     return pos;
205 }