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