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