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