home · contact · privacy
Improved error message phrasings.
[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 unlink(), getopt(), optarg */
9 #include <stdint.h> /* for uint8_t */
10 #include "windows.h" /* for structs WinMeta, Win, init_win(), init_win_meta(),
11                       * draw_all_wins()
12                       */
13 #include "draw_wins.h" /* for draw_keys_win(), draw_map_win(), draw_info_win(),
14                         * draw_log_win()
15                         */
16 #include "keybindings.h" /* for initkeybindings(), get_action_key() */
17 #include "readwrite.h" /* for [read/write]_uint[8/16/32][_bigendian]() */
18 #include "map_objects.h" /* for structs Monster, Item, Player,
19                           * init_map_object_defs(), read_map_objects(),
20                           * build_map_objects()
21                           */
22 #include "map_object_actions.h" /* for player_wait(), move_player() */
23 #include "map.h" /* for struct Map, init_map() */
24 #include "misc.h" /* for update_log(), toggle_window(), find_passable_pos(),
25                    * meta_keys(), save_game()
26                    */
27 #include "yx_uint16.h" /* for dir enum */
28 #include "rrand.h" /* for rrand(), rrand_seed() */
29 #include "rexit.h" /* for exit_game() */
30
31 int main(int argc, char *argv[])
32 {
33     struct World world;
34     char * recordfile_tmp = "record_tmp";
35     char * savefile_tmp   = "savefile_tmp";
36     char * err_x = "A file 'recordfile_tmp' exists, probably from a corrupted "
37                    "previous record saving process. To avoid game record "
38                    "corruption, I won't start until it is removed or renamed.";
39     exit_err(!access(recordfile_tmp, F_OK), &world, err_x);
40     err_x        = "A file 'savefile_tmp' exists, probably from a corrupted "
41                    "previous game saving process. To avoid savegame "
42                    "corruption, I won't start until it is removed or renamed.";
43     exit_err(!access(savefile_tmp,   F_OK), &world, err_x);
44
45     /* Read in startup options (i.e. replay option and replay start turn). */
46     int opt;
47     uint32_t start_turn;
48     world.interactive = 1;
49     while ((opt = getopt(argc, argv, "s::")) != -1)
50     {
51         switch (opt)
52         {
53             case 's':
54                 world.interactive = 0;
55                 start_turn = 0;
56                 if (optarg)
57                     start_turn = atoi(optarg);
58                  break;
59             default:
60                 exit(EXIT_FAILURE);
61         }
62     }
63
64     /* Initialize log, player, monster/item definitions and monsters/items. */
65     world.log = calloc(1, sizeof(char));
66     set_cleanup_flag(CLEANUP_LOG);
67     update_log (&world, " ");
68     struct Player player;
69     player.hitpoints = 5;
70     world.player = &player;
71     world.monster = 0;
72     world.item = 0;
73     init_map_object_defs(&world, "defs");
74
75     /* For interactive mode, try to load world state from savefile. */
76     char * err_o = "Trouble loading game (fopen() in main()) / "
77                    "opening 'savefile' for reading.";
78     char * err_r = "Trouble loading game (in main()) / "
79                    "reading from opened 'savefile'.";
80     char * err_c = "Trouble loading game (fclose() in main()) / "
81                    "closing opened 'savefile'.";
82     char * savefile = "savefile";
83     FILE * file;
84     if (1 == world.interactive && 0 == access(savefile, F_OK))
85     {
86         file = fopen(savefile, "r");
87         exit_err(0 == file, &world, err_o);
88         if (   read_uint32_bigendian(file, &world.seed)
89             || read_uint32_bigendian(file, &world.turn)
90             || read_uint16_bigendian(file, &player.pos.y)
91             || read_uint16_bigendian(file, &player.pos.x)
92             || read_uint8(file, &player.hitpoints)
93             || read_map_objects(&world, &world.monster, file)
94             || read_map_objects(&world, &world.item,    file))
95         {
96             exit_err(1, &world, err_r);
97         }
98         exit_err(fclose(file), &world, err_c);
99         player.pos.y--;
100         player.pos.x--;
101     }
102
103     /* For non-interactive mode, try to load world state from record file. */
104     else
105     {
106         err_o = "Trouble loading record file (fopen() in main()) / "
107                 "opening file 'record' for reading.";
108         err_r = "Trouble loading record file (read_uint32_bigendian() in "
109                 "main()) / reading from opened file 'record'.";
110         char * recordfile = "record";
111         world.turn = 1;
112         if (0 == world.interactive)
113         {
114             file = fopen(recordfile, "r");
115             exit_err(0 == file, &world, err_o);
116             exit_err(read_uint32_bigendian(file, &world.seed), &world, err_r);
117         }
118
119         /* For interactive-mode in newly started world, generate a start seed
120          * from the current time.
121          */
122         else
123         {
124             world.seed = time(NULL);
125
126             err_x        = "Trouble recording new seed: "
127                            "A file 'record' already exists, when it shouldn't.";
128             err_o        = "Trouble recording new seed (fopen() in main()) / "
129                            "opening'record_tmp' file for writing.";
130             char * err_w = "Trouble recording new seed "
131                            "(write_uint32_bigendian() in main()) / writing to "
132                            "opened file 'record_tmp'.";
133             err_c        = "Trouble recording new seed (fclose() in main()) / "
134                            "closing opened file 'record_tmp'.";
135             char * err_m = "Trouble recording new seed (rename() in main()) : "
136                            "renaming file 'record_tmp' to 'record'.";
137             exit_err(!access(recordfile, F_OK), &world, err_x);
138             file = fopen(recordfile_tmp, "w");
139             exit_err(0 == file, &world, err_o);
140             exit_err(write_uint32_bigendian(world.seed, file), &world, err_w);
141             exit_err(fclose(file), &world, err_c);
142             exit_err(rename(recordfile_tmp, recordfile), &world, err_m);
143         }
144     }
145
146
147     /* Generate map from seed and, if newly generated world, start positions of
148      * actors.
149      */
150     rrand_seed(world.seed);
151     struct Map map = init_map();
152     world.map = &map;
153     set_cleanup_flag(CLEANUP_MAP);
154     if (1 == world.turn)
155     {
156         player.pos = find_passable_pos(world.map);
157         void * foo;
158         foo = build_map_objects(&world, &world.monster, 1, 1 + rrand() % 27);
159         foo = build_map_objects(&world, foo, 2, 1 + rrand() % 9);
160         build_map_objects(&world, foo, 3, 1 + rrand() % 3);
161         foo = build_map_objects(&world, &world.item, 4, 1 + rrand() % 3);
162         build_map_objects(&world, foo, 5, 1 + rrand() % 3);
163     }
164
165     /* Initialize window system and windows. */
166     WINDOW * screen = initscr();
167     set_cleanup_flag(CLEANUP_NCURSES);
168     noecho();
169     curs_set(0);
170     keypad(screen, TRUE);
171     raw();
172     init_keybindings(&world);
173     set_cleanup_flag(CLEANUP_KEYBINDINGS);
174     struct WinMeta win_meta;
175     char * err_winmem = "Trouble with init_win() or draw_all_wins() in main().";
176     exit_err(init_win_meta(screen, &win_meta), &world, err_winmem);
177     struct Win win_keys = init_win(&win_meta, "Keys",
178                                    0, 29, &world, draw_keys_win);
179     struct Win win_info = init_win(&win_meta, "Info",
180                                    2, 20, &world, draw_info_win);
181     uint16_t height_logwin = win_meta.padframe.size.y
182                              - (2 + win_info.frame.size.y);
183     struct Win win_log = init_win(&win_meta, "Log",
184                                   height_logwin, 20, &world, draw_log_win);
185     uint16_t width_mapwin = win_meta.padframe.size.x - win_keys.frame.size.x
186                              - win_log.frame.size.x - 2;
187     struct Win win_map = init_win(&win_meta, "Map",
188                                   0, width_mapwin, &world, draw_map_win);
189     toggle_window(&win_meta, &win_keys);
190     toggle_window(&win_meta, &win_map);
191     toggle_window(&win_meta, &win_info);
192     toggle_window(&win_meta, &win_log);
193
194     /* Replay mode. */
195     int key;
196     uint8_t quit_called = 0;
197     uint8_t await_actions = 1;
198     if (0 == world.interactive)
199     {
200         int action;
201         while (1)
202         {
203             if (start_turn == world.turn)
204             {
205                 start_turn = 0;
206             }
207             if (0 == start_turn)
208             {
209                 exit_err(draw_all_wins(&win_meta), &world, err_winmem);
210                 key = getch();
211             }
212             if (1 == await_actions
213                 && (world.turn < start_turn
214                     || key == get_action_key(world.keybindings,
215                                              "wait / next turn")) )
216             {
217                 action = getc(file);
218                 if (EOF == action)
219                 {
220                     start_turn = 0;
221                     await_actions = 0;
222                 }
223                 else if (0 == action)
224                 {
225                     player_wait (&world);
226                 }
227                 else if (NORTH == action)
228                 {
229                     move_player(&world, NORTH);
230                 }
231                 else if (EAST  == action)
232                 {
233                     move_player(&world, EAST);
234                 }
235                 else if (SOUTH == action)
236                 {
237                     move_player(&world, SOUTH);
238                 }
239                 else if (WEST == action)
240                 {
241                     move_player(&world, WEST);
242                 }
243             }
244             else
245             {
246                 quit_called = meta_keys(key, &world, &win_meta, &win_keys,
247                                              &win_map, &win_info, &win_log);
248                 if (1 == quit_called)
249                 {
250                     err_c = "Trouble closing 'record' file (fclose() in "
251                             "main()).";
252                     exit_err(fclose(file), &world, err_c);
253                     exit_game(&world);
254                 }
255             }
256         }
257     }
258
259     /* Interactive mode. */
260     else
261     {
262         uint32_t last_turn = 0;
263         while (1)
264         {
265             if (last_turn != world.turn)
266             {
267                 save_game(&world);
268                 last_turn = world.turn;
269             }
270             if (1 == await_actions && 0 == player.hitpoints)
271             {
272                 await_actions = 0;
273             }
274             draw_all_wins (&win_meta);
275             key = getch();
276             if      (1 == await_actions
277                      && key == get_action_key(world.keybindings,
278                                               "player up"))
279             {
280                 move_player(&world, NORTH);
281             }
282             else if (1 == await_actions
283                      && key == get_action_key(world.keybindings,
284                                               "player right"))
285             {
286                 move_player(&world, EAST);
287             }
288             else if (1 == await_actions
289                      && key == get_action_key(world.keybindings,
290                                               "player down"))
291             {
292                 move_player(&world, SOUTH);
293             }
294             else if (1 == await_actions
295                      && key == get_action_key(world.keybindings,
296                                               "player left"))
297             {
298                 move_player(&world, WEST);
299             }
300             else if (1 == await_actions
301                      && key == get_action_key(world.keybindings,
302                                               "wait / next turn"))
303             {
304                 player_wait (&world);
305             }
306             else
307             {
308                 quit_called = meta_keys(key, &world, &win_meta, &win_keys,
309                                              &win_map, &win_info, &win_log);
310                 if (1 == quit_called)
311                 {
312                     exit_game(&world);
313                 }
314             }
315         }
316     }
317 }