home · contact · privacy
Negative values may now be passed to init_win() for sizes in negative relation to...
[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
107     /* For interactive mode, try to load world state from savefile. */
108     char * err_o = "Trouble loading game (fopen() in main()) / "
109                    "opening 'savefile' for reading.";
110     char * err_r = "Trouble loading game (in main()) / "
111                    "reading from opened 'savefile'.";
112     char * err_c = "Trouble loading game (fclose() in main()) / "
113                    "closing opened 'savefile'.";
114     FILE * file;
115     if (1 == world.interactive && 0 == access(savefile, F_OK))
116     {
117         file = fopen(savefile, "r");
118         exit_err(0 == file, &world, err_o);
119         if (   read_uint32_bigendian(file, &world.seed)
120             || read_uint32_bigendian(file, &world.turn)
121             || read_uint16_bigendian(file, &world.score)
122             || read_uint16_bigendian(file, &player.pos.y)
123             || read_uint16_bigendian(file, &player.pos.x)
124             || read_uint8(file, &player.hitpoints)
125             || read_map_objects(&world, &world.monster, file)
126             || read_map_objects(&world, &world.item,    file))
127         {
128             exit_err(1, &world, err_r);
129         }
130         exit_err(fclose(file), &world, err_c);
131         player.pos.y--;
132         player.pos.x--;
133     }
134
135     /* For non-interactive mode, try to load world state from record file. */
136     else
137     {
138         err_o = "Trouble loading record file (fopen() in main()) / "
139                 "opening file 'record' for reading.";
140         err_r = "Trouble loading record file (read_uint32_bigendian() in "
141                 "main()) / reading from opened file 'record'.";
142         world.turn = 1;
143         if (0 == world.interactive)
144         {
145             file = fopen(recordfile, "r");
146             exit_err(NULL == file, &world, err_o);
147             exit_err(read_uint32_bigendian(file, &world.seed), &world, err_r);
148         }
149
150         /* For interactive-mode in newly started world, generate a start seed
151          * from the current time.
152          */
153         else
154         {
155             world.seed = time(NULL);
156
157             err_o        = "Trouble recording new seed (fopen() in main()) / "
158                            "opening 'record_tmp' file for writing.";
159             char * err_w = "Trouble recording new seed "
160                            "(write_uint32_bigendian() in main()) / writing to "
161                            "opened file 'record_tmp'.";
162             err_c        = "Trouble recording new seed (fclose() in main()) / "
163                            "closing opened file 'record_tmp'.";
164             char * err_m = "Trouble recording new seed (rename() in main()) : "
165                            "renaming file 'record_tmp' to 'record'.";
166             file = fopen(recordfile_tmp, "w");
167             exit_err(0 == file, &world, err_o);
168             exit_err(write_uint32_bigendian(world.seed, file), &world, err_w);
169             exit_err(fclose(file), &world, err_c);
170             exit_err(rename(recordfile_tmp, recordfile), &world, err_m);
171         }
172     }
173
174
175     /* Generate map from seed and, if newly generated world, start positions of
176      * actors.
177      */
178     rrand_seed(world.seed);
179     struct Map map = init_map();
180     world.map = &map;
181     set_cleanup_flag(CLEANUP_MAP);
182     if (1 == world.turn)
183     {
184         player.pos = find_passable_pos(world.map);
185         void * foo;
186         foo = build_map_objects(&world, &world.monster, 1, 1 + rrand() % 27);
187         foo = build_map_objects(&world, foo, 2, 1 + rrand() % 9);
188         build_map_objects(&world, foo, 3, 1 + rrand() % 3);
189         foo = build_map_objects(&world, &world.item, 4, 1 + rrand() % 3);
190         build_map_objects(&world, foo, 5, 1 + rrand() % 3);
191     }
192
193     /* Initialize window system and windows. */
194     WINDOW * screen = initscr();
195     set_cleanup_flag(CLEANUP_NCURSES);
196     noecho();
197     curs_set(0);
198     keypad(screen, TRUE);
199     raw();
200     init_keybindings(&world);
201     set_cleanup_flag(CLEANUP_KEYBINDINGS);
202     struct WinMeta win_meta;
203     char * err_winmem = "Trouble with init_win_meta() or draw_all_wins() in "
204                         "main().";
205     exit_err(init_win_meta(screen, &win_meta), &world, err_winmem);
206     world.wins.meta = &win_meta;
207     struct Win win_keys = init_win(&win_meta,
208                                    "Keys", 0,  29, &world, draw_keys_win);
209     struct Win win_info = init_win(&win_meta,
210                                    "Info", 3,  20, &world, draw_info_win);
211     struct Win win_log  = init_win(&win_meta,
212                                   "Log",  -4,  20, &world, draw_log_win);
213     struct Win win_map  = init_win(&win_meta,
214                                   "Map",   0, -51, &world, draw_map_win);
215     world.wins.keys = &win_keys;
216     world.wins.log = &win_log;
217     world.wins.info = &win_info;
218     world.wins.map = &win_map;
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(&win_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(&win_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 }