home · contact · privacy
At clean-up, free memory of map object definitions, too.
[plomrogue] / src / main.c
1 /* main.c */
2
3 #include "main.h"
4 #include <stdlib.h> /* for atoi(), exit(), EXIT_FAILURE, calloc() */
5 #include <stdio.h> /* for FILE typedef, F_OK, rename() */
6 #include <ncurses.h> /* for initscr(), noecho(), curs_set(), keypad(), raw() */
7 #include <time.h> /* for time() */
8 #include <unistd.h> /* for getopt(), optarg */
9 #include <stdint.h> /* for uint16_t, uint32_t */
10 #include <errno.h> /* for errno */
11 #include "windows.h" /* for structs WinMeta, Win, init_win(), init_win_meta(),
12                       * draw_all_wins()
13                       */
14 #include "draw_wins.h" /* for draw_keys_win(), draw_map_win(), draw_info_win(),
15                         * draw_log_win()
16                         */
17 #include "keybindings.h" /* for init_keybindings(), get_action_key() */
18 #include "readwrite.h" /* for [read/write]_uint[8/16/32][_bigendian]() */
19 #include "map_objects.h" /* for structs Monster, Item, Player,
20                           * init_map_object_defs(), read_map_objects(),
21                           * build_map_objects()
22                           */
23 #include "map.h" /* for struct Map, init_map() */
24 #include "misc.h" /* for update_log(), find_passable_pos(), save_game() */
25 #include "wincontrol.h" /* for toggle_window() */
26 #include "rrand.h" /* for rrand(), rrand_seed() */
27 #include "rexit.h" /* for exit_game() */
28 #include "control.h" /* for meta_control() */
29 #include "command_db.h" /* for init_command_db() */
30
31
32
33 int main(int argc, char *argv[])
34 {
35     struct World world;
36
37     init_command_db(&world);
38     set_cleanup_flag(CLEANUP_COMMAND_DB);
39
40     /* Check for corrupted savefile / recordfile savings. */
41     char * recordfile = "record";
42     char * savefile = "savefile";
43     char * recordfile_tmp = "record_tmp";
44     char * savefile_tmp   = "savefile_tmp";
45     char * err_x = "A file 'record' exists, but no 'savefile'. If everything "
46                    "was in order, both or none would exist. I won't start "
47                    "until this is corrected.";
48     if (!access(recordfile, F_OK) && access(savefile, F_OK))
49     {
50         errno = 0;
51         exit_err(1, &world, err_x);
52     }
53     err_x        = "A 'savefile' exists, but no file 'record'. If everything "
54                    "was in order, both or none would exist. I won't start "
55                    "until this is corrected.";
56     if (!access(savefile, F_OK) && access(recordfile, F_OK))
57     {
58         errno = 0;
59         exit_err(1, &world, err_x);
60     }
61     err_x        = "A file 'recordfile_tmp' exists, probably from a corrupted "
62                    "previous record saving process. To avoid game record "
63                    "corruption, I won't start until it is removed or renamed.";
64     exit_err(!access(recordfile_tmp, F_OK), &world, err_x);
65     err_x        = "A file 'savefile_tmp' exists, probably from a corrupted "
66                    "previous game saving process. To avoid savegame "
67                    "corruption, I won't start until it is removed or renamed.";
68     exit_err(!access(savefile_tmp,   F_OK), &world, err_x);
69
70     /* Read in startup options (i.e. replay option and replay start turn). */
71     int opt;
72     uint32_t start_turn;
73     world.interactive = 1;
74     while ((opt = getopt(argc, argv, "s::")) != -1)
75     {
76         switch (opt)
77         {
78             case 's':
79             {
80                 world.interactive = 0;
81                 start_turn = 0;
82                 if (optarg)
83                 {
84                     start_turn = atoi(optarg);
85                 }
86                 break;
87             }
88             default:
89             {
90                 exit(EXIT_FAILURE);
91             }
92         }
93     }
94
95     /* Initialize log, player, monster/item definitions and monsters/items. */
96     world.score = 0;
97     world.log = calloc(1, sizeof(char));
98     set_cleanup_flag(CLEANUP_LOG);
99     update_log(&world, " ");
100     struct Player player;
101     player.hitpoints = 5;
102     world.player = &player;
103     world.monster = 0;
104     world.item = 0;
105     init_map_object_defs(&world, "config/defs");
106     set_cleanup_flag(CLEANUP_MAP_OBJECT_DEFS);
107     exit_err(1, &world, NULL);
108
109     /* For interactive mode, try to load world state from savefile. */
110     char * err_o = "Trouble loading game (fopen() in main()) / "
111                    "opening 'savefile' for reading.";
112     char * err_r = "Trouble loading game (in main()) / "
113                    "reading from opened 'savefile'.";
114     char * err_c = "Trouble loading game (fclose() in main()) / "
115                    "closing opened 'savefile'.";
116     FILE * file;
117     if (1 == world.interactive && 0 == access(savefile, F_OK))
118     {
119         file = fopen(savefile, "r");
120         exit_err(0 == file, &world, err_o);
121         if (   read_uint32_bigendian(file, &world.seed)
122             || read_uint32_bigendian(file, &world.turn)
123             || read_uint16_bigendian(file, &world.score)
124             || read_uint16_bigendian(file, &player.pos.y)
125             || read_uint16_bigendian(file, &player.pos.x)
126             || read_uint8(file, &player.hitpoints)
127             || read_map_objects(&world, &world.monster, file)
128             || read_map_objects(&world, &world.item,    file))
129         {
130             exit_err(1, &world, err_r);
131         }
132         exit_err(fclose(file), &world, err_c);
133         player.pos.y--;
134         player.pos.x--;
135     }
136
137     /* For non-interactive mode, try to load world state from record file. */
138     else
139     {
140         err_o = "Trouble loading record file (fopen() in main()) / "
141                 "opening file 'record' for reading.";
142         err_r = "Trouble loading record file (read_uint32_bigendian() in "
143                 "main()) / reading from opened file 'record'.";
144         world.turn = 1;
145         if (0 == world.interactive)
146         {
147             file = fopen(recordfile, "r");
148             exit_err(NULL == file, &world, err_o);
149             exit_err(read_uint32_bigendian(file, &world.seed), &world, err_r);
150         }
151
152         /* For interactive-mode in newly started world, generate a start seed
153          * from the current time.
154          */
155         else
156         {
157             world.seed = time(NULL);
158
159             err_o        = "Trouble recording new seed (fopen() in main()) / "
160                            "opening 'record_tmp' file for writing.";
161             char * err_w = "Trouble recording new seed "
162                            "(write_uint32_bigendian() in main()) / writing to "
163                            "opened file 'record_tmp'.";
164             err_c        = "Trouble recording new seed (fclose() in main()) / "
165                            "closing opened file 'record_tmp'.";
166             char * err_m = "Trouble recording new seed (rename() in main()) : "
167                            "renaming file 'record_tmp' to 'record'.";
168             file = fopen(recordfile_tmp, "w");
169             exit_err(0 == file, &world, err_o);
170             exit_err(write_uint32_bigendian(world.seed, file), &world, err_w);
171             exit_err(fclose(file), &world, err_c);
172             exit_err(rename(recordfile_tmp, recordfile), &world, err_m);
173         }
174     }
175
176
177     /* Generate map from seed and, if newly generated world, start positions of
178      * actors.
179      */
180     rrand_seed(world.seed);
181     struct Map map = init_map();
182     world.map = &map;
183     set_cleanup_flag(CLEANUP_MAP);
184     if (1 == world.turn)
185     {
186         player.pos = find_passable_pos(world.map);
187         void * foo;
188         foo = build_map_objects(&world, &world.monster, 1, 1 + rrand() % 27);
189         foo = build_map_objects(&world, foo, 2, 1 + rrand() % 9);
190         build_map_objects(&world, foo, 3, 1 + rrand() % 3);
191         foo = build_map_objects(&world, &world.item, 4, 1 + rrand() % 3);
192         build_map_objects(&world, foo, 5, 1 + rrand() % 3);
193     }
194
195     /* Initialize window system and windows. */
196     WINDOW * screen = initscr();
197     set_cleanup_flag(CLEANUP_NCURSES);
198     noecho();
199     curs_set(0);
200     keypad(screen, TRUE);
201     raw();
202     init_keybindings(&world);
203     set_cleanup_flag(CLEANUP_KEYBINDINGS);
204     struct WinMeta win_meta;
205     char * err_winmem = "Trouble with init_win_meta() or draw_all_wins() in "
206                         "main().";
207     exit_err(init_win_meta(screen, &win_meta), &world, err_winmem);
208     world.wins.meta = &win_meta;
209     world.wins.keys = init_win_from_file(&world, "Keys", draw_keys_win);
210     set_cleanup_flag(CLEANUP_WIN_KEYS);
211     world.wins.info = init_win_from_file(&world, "Info", draw_info_win);
212     set_cleanup_flag(CLEANUP_WIN_INFO);
213     world.wins.log = init_win_from_file(&world, "Log", draw_log_win);
214     set_cleanup_flag(CLEANUP_WIN_LOG);
215     world.wins.map = init_win_from_file(&world, "Map", draw_map_win);
216     set_cleanup_flag(CLEANUP_WIN_MAP);
217
218     sorted_wintoggle(&world);
219
220     /* Replay mode. */
221     int key;
222     if (0 == world.interactive)
223     {
224         int action = 0;
225         if (0 != start_turn)
226         {
227             while (world.turn != start_turn)
228             {
229                 action = getc(file);
230                 if (EOF == action)
231                 {
232                     break;
233                 }
234                 record_control(action, &world);
235             }
236         }
237         while (1)
238         {
239             draw_all_wins(&win_meta);
240             key = getch();
241             if (   EOF != action
242                 && key == get_action_key(world.keybindings, "wait"))
243             {
244                 action = getc(file);
245                 if (EOF != action)
246                 {
247                     record_control(action, &world);
248                 }
249             }
250             else if (meta_control(key, &world))
251             {
252                 err_c = "Trouble closing 'record' file (fclose() in main()).";
253                 exit_err(fclose(file), &world, err_c);
254                 exit_game(&world);
255             }
256         }
257     }
258
259     /* Interactive mode. */
260     else
261     {
262         while (1)
263         {
264             save_game(&world);
265             draw_all_wins(&win_meta);
266             key = getch();
267             if (0 != player.hitpoints && 0 == player_control(key, &world))
268             {
269                 continue;
270             }
271             if (meta_control(key, &world))
272             {
273                 exit_game(&world);
274             }
275         }
276     }
277 }