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