4 #include <stdlib.h> /* for atoi(), exit(), EXIT_FAILURE, calloc() */
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 uint8_t */
10 #include "windows.h" /* for structs WinMeta, Win, init_win(), init_win_meta(),
13 #include "draw_wins.h" /* for draw_keys_win(), draw_map_win(), draw_info_win(),
16 #include "keybindings.h" /* for initkeybindings(), get_action_key() */
17 #include "readwrite.h" /* for [read/write]_uint[8/16/32][_bigendian]() */
18 #include "map_objects.h" /* for structs Monster, Item, Player,
19 * init_map_object_defs(), read_map_objects(),
22 #include "map_object_actions.h" /* for player_wait(), move_player() */
23 #include "map.h" /* for struct Map, init_map() */
24 #include "misc.h" /* for update_log(), toggle_window(), find_passable_pos(),
25 * meta_keys(), save_game()
27 #include "yx_uint16.h" /* for dir enum */
28 #include "rrand.h" /* for rrand(), rrand_seed() */
29 #include "rexit.h" /* for exit_game() */
31 int main(int argc, char *argv[])
35 /* Read in startup options (i.e. replay option and replay start turn). */
38 world.interactive = 1;
39 while ((opt = getopt(argc, argv, "s::")) != -1)
44 world.interactive = 0;
47 start_turn = atoi(optarg);
54 /* Initialize log, player, monster/item definitions and monsters/items. */
55 world.log = calloc(1, sizeof(char));
56 set_cleanup_flag(CLEANUP_LOG);
57 update_log (&world, " ");
60 world.player = &player;
63 init_map_object_defs(&world, "defs");
66 /* For interactive mode, try to load world state from savefile. */
68 if (1 == world.interactive && 0 == access("savefile", F_OK))
70 file = fopen("savefile", "r");
71 err = err | read_uint32_bigendian(file, &world.seed);
72 err = err | read_uint32_bigendian(file, &world.turn);
73 err = err | read_uint16_bigendian(file, &player.pos.y);
74 err = err | read_uint16_bigendian(file, &player.pos.x);
77 err = err | read_uint8(file, &player.hitpoints);
78 err = err | read_map_objects(&world, &world.monster, file);
79 err = err | read_map_objects(&world, &world.item, file);
83 /* For non-interactive mode, try to load world state from record file. */
87 if (0 == world.interactive)
89 file = fopen("record", "r");
90 err = err | read_uint32_bigendian(file, &world.seed);
93 /* For interactive-mode in newly started world, generate a start seed
94 * from the current time.
98 file = fopen("record", "w");
99 world.seed = time(NULL);
100 err = err | write_uint32_bigendian(world.seed, file);
105 exit_err(err, &world, "Failure initializing game.");
108 /* Generate map from seed and, if newly generated world, start positions of
111 rrand_seed(world.seed);
112 struct Map map = init_map();
114 set_cleanup_flag(CLEANUP_MAP);
117 player.pos = find_passable_pos(world.map);
119 foo = build_map_objects(&world, &world.monster, 1, 1 + rrand() % 27);
120 foo = build_map_objects(&world, foo, 2, 1 + rrand() % 9);
121 build_map_objects(&world, foo, 3, 1 + rrand() % 3);
122 foo = build_map_objects(&world, &world.item, 4, 1 + rrand() % 3);
123 build_map_objects(&world, foo, 5, 1 + rrand() % 3);
126 /* Initialize window system and windows. */
127 WINDOW * screen = initscr();
128 set_cleanup_flag(CLEANUP_NCURSES);
131 keypad(screen, TRUE);
133 init_keybindings(&world);
134 set_cleanup_flag(CLEANUP_KEYBINDINGS);
135 struct WinMeta win_meta = init_win_meta(screen);
136 struct Win win_keys = init_win(&win_meta, "Keys",
137 0, 29, &world, draw_keys_win);
138 struct Win win_info = init_win(&win_meta, "Info",
139 2, 20, &world, draw_info_win);
140 uint16_t height_logwin = win_meta.padframe.size.y
141 - (2 + win_info.frame.size.y);
142 struct Win win_log = init_win(&win_meta, "Log",
143 height_logwin, 20, &world, draw_log_win);
144 uint16_t width_mapwin = win_meta.padframe.size.x - win_keys.frame.size.x
145 - win_log.frame.size.x - 2;
146 struct Win win_map = init_win(&win_meta, "Map",
147 0, width_mapwin, &world, draw_map_win);
148 toggle_window(&win_meta, &win_keys);
149 toggle_window(&win_meta, &win_map);
150 toggle_window(&win_meta, &win_info);
151 toggle_window(&win_meta, &win_log);
155 uint8_t quit_called = 0;
156 uint8_t await_actions = 1;
157 if (0 == world.interactive)
162 if (start_turn == world.turn)
168 draw_all_wins (&win_meta);
171 if (1 == await_actions
172 && (world.turn < start_turn
173 || key == get_action_key(world.keybindings,
174 "wait / next turn")) )
182 else if (0 == action)
184 player_wait (&world);
186 else if (NORTH == action)
188 move_player(&world, NORTH);
190 else if (EAST == action)
192 move_player(&world, EAST);
194 else if (SOUTH == action)
196 move_player(&world, SOUTH);
198 else if (WEST == action)
200 move_player(&world, WEST);
205 quit_called = meta_keys(key, &world, &win_meta, &win_keys,
206 &win_map, &win_info, &win_log);
207 if (1 == quit_called)
215 /* Interactive mode. */
218 uint32_t last_turn = 0;
221 if (last_turn != world.turn)
224 last_turn = world.turn;
226 if (1 == await_actions && 0 == player.hitpoints)
230 draw_all_wins (&win_meta);
232 if (1 == await_actions
233 && key == get_action_key(world.keybindings,
236 move_player(&world, NORTH);
238 else if (1 == await_actions
239 && key == get_action_key(world.keybindings,
242 move_player(&world, EAST);
244 else if (1 == await_actions
245 && key == get_action_key(world.keybindings,
248 move_player(&world, SOUTH);
250 else if (1 == await_actions
251 && key == get_action_key(world.keybindings,
254 move_player(&world, WEST);
256 else if (1 == await_actions
257 && key == get_action_key(world.keybindings,
260 player_wait (&world);
264 quit_called = meta_keys(key, &world, &win_meta, &win_keys,
265 &win_map, &win_info, &win_log);
266 if (1 == quit_called)