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