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 "windows.h" /* for structs WinMeta, Win, init_win(), init_win_meta(),
12 #include "draw_wins.h" /* for draw_keys_win(), draw_map_win(), draw_info_win(),
15 #include "keybindings.h" /* for initkeybindings(), get_action_key() */
16 #include "readwrite.h" /* for [read/write]_uint[8/16/32][_bigendian]() */
17 #include "map_objects.h" /* for structs Monster, Item, Player,
18 * init_map_object_defs(), read_map_objects(),
21 #include "map_object_actions.h" /* for player_wait(), move_player() */
22 #include "map.h" /* for struct Map, init_map() */
23 #include "misc.h" /* for update_log(), toggle_window(), find_passable_pos(),
24 * meta_keys(), save_game()
26 #include "yx_uint16.h" /* for dir enum */
27 #include "rrand.h" /* for rrand(), rrand_seed() */
28 #include "rexit.h" /* for exit_game() */
30 int main(int argc, char *argv[])
34 /* Read in startup options (i.e. replay option and replay start turn). */
37 world.interactive = 1;
38 while ((opt = getopt(argc, argv, "s::")) != -1)
43 world.interactive = 0;
46 start_turn = atoi(optarg);
53 /* Initialize log, player, monster/item definitions and monsters/items. */
54 world.log = calloc(1, sizeof(char));
55 set_cleanup_flag(CLEANUP_LOG);
56 update_log (&world, " ");
59 world.player = &player;
62 init_map_object_defs(&world, "defs");
65 /* For interactive mode, try to load world state from savefile. */
67 if (1 == world.interactive && 0 == access("savefile", F_OK))
69 file = fopen("savefile", "r");
70 err = err | read_uint32_bigendian(file, &world.seed);
71 err = err | read_uint32_bigendian(file, &world.turn);
72 err = err | read_uint16_bigendian(file, &player.pos.y);
73 err = err | read_uint16_bigendian(file, &player.pos.x);
76 err = err | read_uint8(file, &player.hitpoints);
77 err = err | read_map_objects(&world, &world.monster, file);
78 err = err | read_map_objects(&world, &world.item, file);
82 /* For non-interactive mode, try to load world state from record file. */
86 if (0 == world.interactive)
88 file = fopen("record", "r");
89 err = err | read_uint32_bigendian(file, &world.seed);
92 /* For interactive-mode in newly started world, generate a start seed
93 * from the current time.
97 file = fopen("record", "w");
98 world.seed = time(NULL);
99 err = err | write_uint32_bigendian(world.seed, file);
104 exit_err(err, &world, "Failure initializing game.");
107 /* Generate map from seed and, if newly generated world, start positions of
110 rrand_seed(world.seed);
111 struct Map map = init_map();
113 set_cleanup_flag(CLEANUP_MAP);
116 player.pos = find_passable_pos(world.map);
118 foo = build_map_objects(&world, &world.monster, 1, 1 + rrand() % 27);
119 foo = build_map_objects(&world, foo, 2, 1 + rrand() % 9);
120 build_map_objects(&world, foo, 3, 1 + rrand() % 3);
121 foo = build_map_objects(&world, &world.item, 4, 1 + rrand() % 3);
122 build_map_objects(&world, foo, 5, 1 + rrand() % 3);
125 /* Initialize window system and windows. */
126 WINDOW * screen = initscr();
127 set_cleanup_flag(CLEANUP_NCURSES);
130 keypad(screen, TRUE);
132 init_keybindings(&world);
133 set_cleanup_flag(CLEANUP_KEYBINDINGS);
134 struct WinMeta win_meta = init_win_meta(screen);
135 struct Win win_keys = init_win(&win_meta, "Keys",
136 0, 29, &world, draw_keys_win);
137 struct Win win_info = init_win(&win_meta, "Info",
138 2, 20, &world, draw_info_win);
139 uint16_t height_logwin = win_meta.padframe.size.y
140 - (2 + win_info.frame.size.y);
141 struct Win win_log = init_win(&win_meta, "Log",
142 height_logwin, 20, &world, draw_log_win);
143 uint16_t width_mapwin = win_meta.padframe.size.x - win_keys.frame.size.x
144 - win_log.frame.size.x - 2;
145 struct Win win_map = init_win(&win_meta, "Map",
146 0, width_mapwin, &world, draw_map_win);
147 toggle_window(&win_meta, &win_keys);
148 toggle_window(&win_meta, &win_map);
149 toggle_window(&win_meta, &win_info);
150 toggle_window(&win_meta, &win_log);
154 unsigned char quit_called = 0;
155 unsigned char await_actions = 1;
156 if (0 == world.interactive)
161 if (start_turn == world.turn)
167 draw_all_wins (&win_meta);
170 if (1 == await_actions
171 && (world.turn < start_turn
172 || key == get_action_key(world.keybindings,
173 "wait / next turn")) )
181 else if (0 == action)
183 player_wait (&world);
185 else if (NORTH == action)
187 move_player(&world, NORTH);
189 else if (EAST == action)
191 move_player(&world, EAST);
193 else if (SOUTH == action)
195 move_player(&world, SOUTH);
197 else if (WEST == action)
199 move_player(&world, WEST);
204 quit_called = meta_keys(key, &world, &win_meta, &win_keys,
205 &win_map, &win_info, &win_log);
206 if (1 == quit_called)
214 /* Interactive mode. */
217 uint32_t last_turn = 0;
220 if (last_turn != world.turn)
223 last_turn = world.turn;
225 if (1 == await_actions && 0 == player.hitpoints)
229 draw_all_wins (&win_meta);
231 if (1 == await_actions
232 && key == get_action_key(world.keybindings,
235 move_player(&world, NORTH);
237 else if (1 == await_actions
238 && key == get_action_key(world.keybindings,
241 move_player(&world, EAST);
243 else if (1 == await_actions
244 && key == get_action_key(world.keybindings,
247 move_player(&world, SOUTH);
249 else if (1 == await_actions
250 && key == get_action_key(world.keybindings,
253 move_player(&world, WEST);
255 else if (1 == await_actions
256 && key == get_action_key(world.keybindings,
259 player_wait (&world);
263 quit_called = meta_keys(key, &world, &win_meta, &win_keys,
264 &win_map, &win_info, &win_log);
265 if (1 == quit_called)