home · contact · privacy
Improved windows cleaning up: free win_meta, ncurses windows/pads.
[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     world.turn = 0;        /* Turns to 1 when map and objects are initalized. */
37
38     init_command_db(&world);
39     set_cleanup_flag(CLEANUP_COMMAND_DB);
40
41     /* Check for corrupted savefile / recordfile savings. */
42     char * recordfile = "record";
43     char * savefile = "savefile";
44     char * recordfile_tmp = "record_tmp";
45     char * savefile_tmp   = "savefile_tmp";
46     char * err_x = "A file 'record' exists, but no 'savefile'. If everything "
47                    "was in order, both or none would exist. I won't start "
48                    "until this is corrected.";
49     if (!access(recordfile, F_OK) && access(savefile, F_OK))
50     {
51         errno = 0;
52         exit_err(1, &world, err_x);
53     }
54     err_x        = "A 'savefile' exists, but no file 'record'. If everything "
55                    "was in order, both or none would exist. I won't start "
56                    "until this is corrected.";
57     if (!access(savefile, F_OK) && access(recordfile, F_OK))
58     {
59         errno = 0;
60         exit_err(1, &world, err_x);
61     }
62     err_x        = "A file 'recordfile_tmp' exists, probably from a corrupted "
63                    "previous record saving process. To avoid game record "
64                    "corruption, I won't start until it is removed or renamed.";
65     exit_err(!access(recordfile_tmp, F_OK), &world, err_x);
66     err_x        = "A file 'savefile_tmp' exists, probably from a corrupted "
67                    "previous game saving process. To avoid savegame "
68                    "corruption, I won't start until it is removed or renamed.";
69     exit_err(!access(savefile_tmp,   F_OK), &world, err_x);
70
71     /* Read in startup options (i.e. replay option and replay start turn). */
72     int opt;
73     uint32_t start_turn;
74     world.interactive = 1;
75     while ((opt = getopt(argc, argv, "s::")) != -1)
76     {
77         switch (opt)
78         {
79             case 's':
80             {
81                 world.interactive = 0;
82                 start_turn = 0;
83                 if (optarg)
84                 {
85                     start_turn = atoi(optarg);
86                 }
87                 break;
88             }
89             default:
90             {
91                 exit(EXIT_FAILURE);
92             }
93         }
94     }
95
96     /* Initialize log, player, monster/item definitions and monsters/items. */
97     world.score = 0;
98     world.log = calloc(1, sizeof(char));
99     set_cleanup_flag(CLEANUP_LOG);
100     update_log(&world, " ");
101     struct Player player;
102     player.hitpoints = 5;
103     world.player = &player;
104     world.monster = 0;
105     world.item = 0;
106     init_map_object_defs(&world, "config/defs");
107     set_cleanup_flag(CLEANUP_MAP_OBJECT_DEFS);
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         set_cleanup_flag(CLEANUP_MAP_OBJECTS);
133         exit_err(fclose(file), &world, err_c);
134         player.pos.y--;
135         player.pos.x--;
136         world.turn = 1;
137     }
138
139     /* For non-interactive mode, try to load world state from record file. */
140     else
141     {
142         err_o = "Trouble loading record file (fopen() in main()) / "
143                 "opening file 'record' for reading.";
144         err_r = "Trouble loading record file (read_uint32_bigendian() in "
145                 "main()) / reading from opened file 'record'.";
146         if (0 == world.interactive)
147         {
148             file = fopen(recordfile, "r");
149             exit_err(NULL == file, &world, err_o);
150             exit_err(read_uint32_bigendian(file, &world.seed), &world, err_r);
151         }
152
153         /* For interactive-mode in newly started world, generate a start seed
154          * from the current time.
155          */
156         else
157         {
158             world.seed = time(NULL);
159
160             err_o        = "Trouble recording new seed (fopen() in main()) / "
161                            "opening 'record_tmp' file for writing.";
162             char * err_w = "Trouble recording new seed "
163                            "(write_uint32_bigendian() in main()) / writing to "
164                            "opened file 'record_tmp'.";
165             err_c        = "Trouble recording new seed (fclose() in main()) / "
166                            "closing opened file 'record_tmp'.";
167             char * err_m = "Trouble recording new seed (rename() in main()) : "
168                            "renaming file 'record_tmp' to 'record'.";
169             file = fopen(recordfile_tmp, "w");
170             exit_err(0 == file, &world, err_o);
171             exit_err(write_uint32_bigendian(world.seed, file), &world, err_w);
172             exit_err(fclose(file), &world, err_c);
173             exit_err(rename(recordfile_tmp, recordfile), &world, err_m);
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 (0 == 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         set_cleanup_flag(CLEANUP_MAP_OBJECTS);
194         world.turn = 1;
195     }
196
197     /* Initialize window system and windows. */
198     WINDOW * screen = initscr();
199     set_cleanup_flag(CLEANUP_NCURSES);
200     noecho();
201     curs_set(0);
202     keypad(screen, TRUE);
203     raw();
204     init_keybindings(&world);
205     set_cleanup_flag(CLEANUP_KEYBINDINGS);
206     char * err_winmem = "Trouble with init_win_meta() or draw_all_wins() in "
207                         "main().";
208     exit_err(init_win_meta(screen, &world.wins.meta), &world, err_winmem);
209     set_cleanup_flag(CLEANUP_WIN_META);
210     world.wins.keys = init_win_from_file(&world, "Keys", draw_keys_win);
211     set_cleanup_flag(CLEANUP_WIN_KEYS);
212     world.wins.info = init_win_from_file(&world, "Info", draw_info_win);
213     set_cleanup_flag(CLEANUP_WIN_INFO);
214     world.wins.log = init_win_from_file(&world, "Log", draw_log_win);
215     set_cleanup_flag(CLEANUP_WIN_LOG);
216     world.wins.map = init_win_from_file(&world, "Map", draw_map_win);
217     set_cleanup_flag(CLEANUP_WIN_MAP);
218
219     sorted_wintoggle(&world);
220
221     /* Replay mode. */
222     int key;
223     if (0 == world.interactive)
224     {
225         int action = 0;
226         if (0 != start_turn)
227         {
228             while (world.turn != start_turn)
229             {
230                 action = getc(file);
231                 if (EOF == action)
232                 {
233                     break;
234                 }
235                 record_control(action, &world);
236             }
237         }
238         while (1)
239         {
240             draw_all_wins(world.wins.meta);
241             key = getch();
242             if (   EOF != action
243                 && key == get_action_key(world.keybindings, "wait"))
244             {
245                 action = getc(file);
246                 if (EOF != action)
247                 {
248                     record_control(action, &world);
249                 }
250             }
251             else if (meta_control(key, &world))
252             {
253                 err_c = "Trouble closing 'record' file (fclose() in main()).";
254                 exit_err(fclose(file), &world, err_c);
255                 exit_game(&world);
256             }
257         }
258     }
259
260     /* Interactive mode. */
261     else
262     {
263         while (1)
264         {
265             save_game(&world);
266             draw_all_wins(world.wins.meta);
267             key = getch();
268             if (0 != player.hitpoints && 0 == player_control(key, &world))
269             {
270                 continue;
271             }
272             if (meta_control(key, &world))
273             {
274                 exit_game(&world);
275             }
276         }
277     }
278 }