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_meta(), draw_all_wins()
12 #include "readwrite.h" /* for [read/write]_uint[8/16/32][_bigendian](),
13 * try_fopen(), try_fclose(), try_fclose_unlink_rename()
15 #include "map_objects.h" /* for structs Monster, Item, Player,
16 * init_map_object_defs(), read_map_objects(),
19 #include "map.h" /* for struct Map, init_map() */
20 #include "misc.h" /* for update_log(), find_passable_pos(), save_game(),
21 * try_calloc(), check_tempfile(), check_xor_files(),
22 * load_interface_conf()
24 #include "wincontrol.h" /* get_win_by_id(), get_winconf_by_win() */
25 #include "rrand.h" /* for rrand(), rrand_seed() */
26 #include "rexit.h" /* for exit_game(), exit_err() */
27 #include "command_db.h" /* for init_command_db() */
28 #include "control.h" /* for *_control(), get_available_keycode_to_action() */
32 int main(int argc, char *argv[])
34 char * f_name = "main()";
36 world.turn = 0; /* Turns to 1 when map and objects are initalized. */
38 init_command_db(&world);
39 set_cleanup_flag(CLEANUP_COMMAND_DB);
41 /* Check for corrupted savefile / recordfile savings. */
42 char * recordfile = "record";
43 char * savefile = "savefile";
44 char * recordfile_tmp = "record_tmp";
45 char * savefile_tmp = "savefile_tmp";
46 check_files_xor(savefile, recordfile, &world);
47 check_tempfile(recordfile_tmp, &world);
48 check_tempfile(savefile_tmp, &world);
49 check_tempfile("config/windows/Win_tmp_k", &world);
50 check_tempfile("config/windows/Win_tmp_m", &world);
51 check_tempfile("config/windows/Win_tmp_i", &world);
52 check_tempfile("config/windows/Win_tmp_l", &world);
53 check_tempfile("config/windows/toggle_order_tmp", &world);
55 /* Read in startup options (i.e. replay option and replay start turn). */
58 world.interactive = 1;
59 while ((opt = getopt(argc, argv, "s::")) != -1)
65 world.interactive = 0;
69 start_turn = atoi(optarg);
80 /* Initialize log, player, monster/item definitions and monsters/items. */
82 world.log = try_calloc(1, sizeof(char), &world, f_name);
83 set_cleanup_flag(CLEANUP_LOG);
84 update_log(&world, " ");
87 world.player = &player;
90 init_map_object_defs(&world, "config/defs");
91 set_cleanup_flag(CLEANUP_MAP_OBJECT_DEFS);
92 world.map_object_count = 1;
94 /* For interactive mode, try to load world state from savefile. */
95 char * err_r = "Trouble loading game (in main()) / "
96 "reading from opened 'savefile'.";
98 if (1 == world.interactive && 0 == access(savefile, F_OK))
100 file = try_fopen(savefile, "r", &world, f_name);
101 if ( read_uint32_bigendian(file, &world.seed)
102 || read_uint32_bigendian(file, &world.turn)
103 || read_uint16_bigendian(file, &world.score)
104 || read_uint16_bigendian(file, &player.pos.y)
105 || read_uint16_bigendian(file, &player.pos.x)
106 || read_uint8(file, &player.hitpoints)
107 || read_map_objects(&world, &world.monster, file)
108 || read_map_objects(&world, &world.item, file))
110 exit_err(1, &world, err_r);
112 set_cleanup_flag(CLEANUP_MAP_OBJECTS);
113 try_fclose(file, &world, f_name);
118 /* For non-interactive mode, try to load world state from record file. */
121 err_r = "Trouble reading from 'record' file (read_uint32_bigendian() "
123 if (0 == world.interactive)
125 file = try_fopen(recordfile, "r", &world, f_name);
126 exit_err(read_uint32_bigendian(file, &world.seed), &world, err_r);
129 /* For interactive-mode in newly started world, generate a start seed
130 * from the current time.
134 world.seed = time(NULL);
136 char * err_w = "Trouble recording new seed "
137 "(write_uint32_bigendian() in main()) / writing to "
138 "file 'record_tmp'.";
139 file = try_fopen(recordfile_tmp, "w", &world, f_name);
140 exit_err(write_uint32_bigendian(world.seed, file), &world, err_w);
141 try_fclose_unlink_rename(file, recordfile_tmp, recordfile,
146 /* Generate map from seed and, if newly generated world, start positions of
149 rrand_seed(world.seed);
150 struct Map map = init_map();
152 set_cleanup_flag(CLEANUP_MAP);
155 player.pos = find_passable_pos(world.map);
157 foo = build_map_objects(&world, &world.monster, 1, 1 + rrand() % 27);
158 foo = build_map_objects(&world, foo, 2, 1 + rrand() % 9);
159 build_map_objects(&world, foo, 3, 1 + rrand() % 3);
160 foo = build_map_objects(&world, &world.item, 4, 1 + rrand() % 3);
161 build_map_objects(&world, foo, 5, 1 + rrand() % 3);
162 set_cleanup_flag(CLEANUP_MAP_OBJECTS);
166 /* Initialize window system and windows. */
167 WINDOW * screen = initscr();
168 set_cleanup_flag(CLEANUP_NCURSES);
171 keypad(screen, TRUE);
173 char * err_winmem = "Trouble with init_win_meta() in main ().";
174 exit_err(init_win_meta(screen, &world.wmeta), &world, err_winmem);
175 set_cleanup_flag(CLEANUP_WIN_META);
176 load_interface_conf(&world);
177 set_cleanup_flag(CLEANUP_INTERFACE_CONF);
178 err_winmem = "Trouble with draw_all_wins() in main().";
180 /* Focus map on player. */
181 struct Win * win_map = get_win_by_id(&world, 'm');
182 map_center_player(&map, &player, win_map->frame.size);
187 if (0 == world.interactive)
192 while (world.turn != start_turn)
199 record_control(action, &world);
204 draw_all_wins(world.wmeta);
206 wc = get_winconf_by_win(&world, world.wmeta->active);
207 if ( (1 == wc->view && wingeom_control(key, &world))
208 || (2 == wc->view && winkeyb_control(key, &world)))
213 && key == get_available_keycode_to_action(&world, "wait"))
218 record_control(action, &world);
221 else if (meta_control(key, &world))
223 try_fclose(file, &world, f_name);
229 /* Interactive mode. */
235 draw_all_wins(world.wmeta);
237 wc = get_winconf_by_win(&world, world.wmeta->active);
238 if (1 == wc->view && wingeom_control(key, &world))
242 else if (2 == wc->view && winkeyb_control(key, &world))
247 if ( (1 == wc->view && wingeom_control(key, &world))
248 || (2 == wc->view && winkeyb_control(key, &world))
249 || (0 != player.hitpoints && player_control(key, &world)))
254 if (meta_control(key, &world))