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