home · contact · privacy
88d90d2844dc1e51b48205101933a1791b974bc4
[plomrogue] / src / main.c
1 /* main.c */
2
3 #include "main.h" /* for world global */
4 #include <stdlib.h> /* for atoi(), exit(), EXIT_FAILURE */
5 #include <stdio.h> /* for FILE typedef, F_OK */
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 uint32_t */
10 #include "windows.h" /* for structs WinMeta, Win, init_win_meta(),
11                       * draw_all_wins()
12                       */
13 #include "readwrite.h" /* for read_uint32_bigendian](),
14                         * write_uint32_bigendian(), try_fopen(), try_fclose(),
15                         * try_fclose_unlink_rename()
16                         */
17 #include "map_objects.h" /* for structs MapObj, init_map_object_defs(),
18                           * build_map_objects(), get_player()
19                           */
20 #include "map.h" /* for struct Map, init_map() */
21 #include "misc.h" /* for update_log(), find_passable_pos(), save_game(),
22                    * try_calloc(), check_tempfile(), check_xor_files(),
23                    * load_interface_conf(), load_game()
24                    */
25 #include "wincontrol.h" /* get_win_by_id(), get_winconf_by_win() */
26 #include "rrand.h" /* for rrand(), rrand_seed() */
27 #include "rexit.h" /* for exit_game(), exit_err() */
28 #include "command_db.h" /* for init_command_db(), is_command_id_shortdsc() */
29 #include "control.h" /* for control_by_id(), player_control(),
30                       * get_available_keycode_to_action()
31                       */
32
33
34
35 int main(int argc, char *argv[])
36 {
37     char * f_name = "main()";
38     world.turn = 0;        /* Turns to 1 when map and objects are initalized. */
39
40     init_command_db();
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     check_files_xor(savefile, recordfile);
49     check_tempfile(recordfile_tmp);
50     check_tempfile(savefile_tmp);
51     check_tempfile("config/windows/Win_tmp_k");
52     check_tempfile("config/windows/Win_tmp_m");
53     check_tempfile("config/windows/Win_tmp_i");
54     check_tempfile("config/windows/Win_tmp_l");
55     check_tempfile("config/windows/toggle_order_tmp");
56
57     /* Read in startup options (i.e. replay option and replay start turn). */
58     int opt;
59     uint32_t start_turn;
60     world.interactive = 1;
61     while ((opt = getopt(argc, argv, "s::")) != -1)
62     {
63         switch (opt)
64         {
65             case 's':
66             {
67                 world.interactive = 0;
68                 start_turn = 0;
69                 if (optarg)
70                 {
71                     start_turn = atoi(optarg);
72                 }
73                 break;
74             }
75             default:
76             {
77                 exit(EXIT_FAILURE);
78             }
79         }
80     }
81
82     /* Initialize log and map object definitions. */
83     world.score = 0;
84     world.log = try_calloc(1, sizeof(char), f_name);
85     set_cleanup_flag(CLEANUP_LOG);
86     update_log(" ");
87     init_map_object_defs("config/defs");
88     set_cleanup_flag(CLEANUP_MAP_OBJECT_DEFS);
89     world.map_obj_count = 0;
90
91     /* For interactive mode, try to load world state from savefile. */
92     char * err_r = "Trouble loading game (in main()) / "
93                    "reading from opened 'savefile'.";
94     FILE * file;
95     if (1 == world.interactive && 0 == access(savefile, F_OK))
96     {
97         load_game();
98         set_cleanup_flag(CLEANUP_MAP_OBJECTS);
99     }
100
101     /* For non-interactive mode, try to load world state from record file. */
102     else
103     {
104         err_r = "Trouble reading from 'record' file (read_uint32_bigendian() "
105                 "in main()).";
106         if (0 == world.interactive)
107         {
108             file = try_fopen(recordfile, "r", f_name);
109             exit_err(read_uint32_bigendian(file, &world.seed), err_r);
110         }
111
112         /* For interactive-mode in newly started world, generate a start seed
113          * from the current time.
114          */
115         else
116         {
117             world.seed = time(NULL);
118
119             char * err_w = "Trouble recording new seed "
120                            "(write_uint32_bigendian() in main()) / writing to "
121                            "file 'record_tmp'.";
122             file = try_fopen(recordfile_tmp, "w", f_name);
123             exit_err(write_uint32_bigendian(world.seed, file), err_w);
124             try_fclose_unlink_rename(file, recordfile_tmp, recordfile, f_name);
125         }
126     }
127
128     /* Generate map from seed and, if newly generated world, start positions of
129      * actors.
130      */
131     rrand_seed(world.seed);
132     struct Map map = init_map();
133     world.map = &map;
134     set_cleanup_flag(CLEANUP_MAP);
135     if (0 == world.turn)
136     {
137         world.map_objs = NULL;
138         add_map_objects(0, 1);
139         add_map_objects(1, 1 + rrand() % 27);
140         add_map_objects(2, 1 + rrand() % 9);
141         add_map_objects(3, 1 + rrand() % 3);
142         add_map_objects(4, 1 + rrand() % 3);
143         add_map_objects(5, 1 + rrand() % 3);
144         set_cleanup_flag(CLEANUP_MAP_OBJECTS);
145         world.turn = 1;
146     }
147
148     /* Initialize window system and windows. */
149     WINDOW * screen = initscr();
150     set_cleanup_flag(CLEANUP_NCURSES);
151     noecho();
152     curs_set(0);
153     keypad(screen, TRUE);
154     raw();
155     init_win_meta(screen);
156     set_cleanup_flag(CLEANUP_WIN_META);
157     load_interface_conf();
158     set_cleanup_flag(CLEANUP_INTERFACE_CONF);
159
160     /* Focus map on player. */
161     struct MapObj * player = get_player();
162     struct Win * win_map = get_win_by_id('m');
163     win_map->center = player->pos;
164
165     /* Initialize player's inventory selection index to start position. */
166     world.inventory_select = 0;
167
168     /* Replay mode. */
169     int key;
170     struct WinConf * wc;
171     if (0 == world.interactive)
172     {
173         int action = 0;
174         if (0 != start_turn)
175         {
176             while (world.turn != start_turn)
177             {
178                 action = getc(file);
179                 if (EOF == action)
180                 {
181                     break;
182                 }
183                 if (   is_command_id_shortdsc(action, "drop")
184                     || is_command_id_shortdsc(action, "use"))
185                 {
186                     world.inventory_select = getc(file);
187                 }
188                 player_control_by_id(action);
189             }
190         }
191         while (1)
192         {
193             draw_all_wins();
194             key = getch();
195             wc = get_winconf_by_win(world.wmeta->active);
196             if  (   (1 == wc->view && wingeom_control(key))
197                  || (2 == wc->view && winkeyb_control(key)))
198             {
199                 continue;
200             }
201             if (   EOF != action
202                 && key == get_available_keycode_to_action("wait"))
203             {
204                 action = getc(file);
205                 if (EOF != action)
206                 {
207                     if (   is_command_id_shortdsc(action, "drop")
208                         || is_command_id_shortdsc(action, "use"))
209                     {
210                         world.inventory_select = getc(file);
211                     }
212                     player_control_by_id(action);
213                 }
214             }
215             else if (meta_control(key))
216             {
217                 try_fclose(file, f_name);
218                 exit_game();
219             }
220         }
221     }
222
223     /* Interactive mode. */
224     else
225     {
226         while (1)
227         {
228             save_game();
229             draw_all_wins();
230             key = getch();
231             wc = get_winconf_by_win(world.wmeta->active);
232             if  (   (1 == wc->view && wingeom_control(key))
233                  || (2 == wc->view && winkeyb_control(key))
234                  || (0 != player->lifepoints && player_control_by_key(key)))
235             {
236                 continue;
237             }
238             if (meta_control(key))
239             {
240                 exit_game();
241             }
242         }
243     }
244 }