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_uint16_bigendian, read_uint32_bigendian,
17 * write_uint32_bigendian
19 #include "map_objects.h" /* for structs Monster, Item, Player,
20 * init_map_object_defs(), read_map_objects(),
21 * read_map_objects_monsterdata,
22 * read_map_objects_itemdata, build_map_objects()
24 #include "map_object_actions.h" /* for player_wait(), move_player() */
25 #include "map.h" /* for struct Map, init_map() */
26 #include "misc.h" /* for rrand(), update_log(), toggle_window(), exit_game(),
27 * find_passable_pos(), meta_keys(), save_game()
29 #include "yx_uint16.h" /* for dir enum */
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 update_log (&world, " ");
59 world.player = &player;
62 init_map_object_defs(&world, "defs");
64 /* For interactive mode, try to load world state from savefile. */
66 if (1 == world.interactive && 0 == access("savefile", F_OK))
68 file = fopen("savefile", "r");
69 world.seed = read_uint32_bigendian(file);
70 world.turn = read_uint32_bigendian(file);
71 player.pos.y = read_uint16_bigendian(file) - 1;
72 player.pos.x = read_uint16_bigendian(file) - 1;
73 player.hitpoints = fgetc(file);
74 read_map_objects(&world.monster, file, sizeof(struct Monster),
75 read_map_objects_monsterdata);
76 read_map_objects(&world.item, file, sizeof(struct Item), NULL);
80 /* For non-interactive mode, try to load world state from record file. */
84 if (0 == world.interactive)
86 file = fopen("record", "r");
87 world.seed = read_uint32_bigendian(file);
90 /* For interactive-mode in newly started world, generate a start seed
91 * from the current time.
95 file = fopen("record", "w");
96 world.seed = time(NULL);
97 write_uint32_bigendian(world.seed, file);
102 /* Generate map from seed and, if newly generated world, start positions of
105 rrand(1, world.seed);
106 struct Map map = init_map();
110 player.pos = find_passable_pos(&map);
111 void * foo = build_map_objects(&world, &world.monster,
112 0, 1 + rrand(0,0) % 27,
113 sizeof(struct Monster),
114 build_map_objects_monsterdata);
115 foo = build_map_objects(&world, foo, 1, 1 + rrand(0,0) % 9,
116 sizeof(struct Monster),
117 build_map_objects_monsterdata);
118 build_map_objects(&world, foo, 2, 1 + rrand(0,0) % 3,
119 sizeof(struct Monster),
120 build_map_objects_monsterdata);
121 foo = build_map_objects(&world, &world.item, 3, 1 + rrand(0,0) % 3,
123 build_map_objects_itemdata);
124 build_map_objects(&world, foo, 4, 1 + rrand(0,0) % 3,
125 sizeof(struct Item), build_map_objects_itemdata);
128 /* Initialize window system and windows. */
129 WINDOW * screen = initscr();
132 keypad(screen, TRUE);
134 init_keybindings(&world);
135 struct WinMeta win_meta = init_win_meta(screen);
136 struct Win win_keys = init_win(&win_meta, "Keys", &world, draw_keys_win);
137 struct Win win_map = init_win(&win_meta, "Map", &world, draw_map_win);
138 struct Win win_info = init_win(&win_meta, "Info", &world, draw_info_win);
139 struct Win win_log = init_win(&win_meta, "Log", &world, draw_log_win);
140 win_keys.frame.size.x = 29;
141 win_map.frame.size.x = win_meta.padframe.size.x - win_keys.frame.size.x
142 - win_log.frame.size.x - 2;
143 win_info.frame.size.y = 2;
144 win_log.frame.size.y = win_meta.padframe.size.y
145 - (2 + win_info.frame.size.y);
146 toggle_window(&win_meta, &win_keys);
147 toggle_window(&win_meta, &win_map);
148 toggle_window(&win_meta, &win_info);
149 toggle_window(&win_meta, &win_log);
153 unsigned char quit_called = 0;
154 unsigned char await_actions = 1;
155 if (0 == world.interactive)
160 if (start_turn == world.turn)
166 draw_all_wins (&win_meta);
169 if (1 == await_actions
170 && (world.turn < start_turn
171 || key == get_action_key(world.keybindings,
172 "wait / next turn")) )
180 else if (0 == action)
182 player_wait (&world);
184 else if (NORTH == action)
186 move_player(&world, NORTH);
188 else if (EAST == action)
190 move_player(&world, EAST);
192 else if (SOUTH == action)
194 move_player(&world, SOUTH);
196 else if (WEST == action)
198 move_player(&world, WEST);
203 quit_called = meta_keys(key, &world, &win_meta, &win_keys,
204 &win_map, &win_info, &win_log);
205 if (1 == quit_called)
207 exit_game(&world, &map);
213 /* Interactive mode. */
216 uint32_t last_turn = 0;
219 if (last_turn != world.turn)
222 last_turn = world.turn;
224 if (1 == await_actions && 0 == player.hitpoints)
228 draw_all_wins (&win_meta);
230 if (1 == await_actions
231 && key == get_action_key(world.keybindings,
234 move_player(&world, NORTH);
236 else if (1 == await_actions
237 && key == get_action_key(world.keybindings,
240 move_player(&world, EAST);
242 else if (1 == await_actions
243 && key == get_action_key(world.keybindings,
246 move_player(&world, SOUTH);
248 else if (1 == await_actions
249 && key == get_action_key(world.keybindings,
252 move_player(&world, WEST);
254 else if (1 == await_actions
255 && key == get_action_key(world.keybindings,
258 player_wait (&world);
262 quit_called = meta_keys(key, &world, &win_meta, &win_keys,
263 &win_map, &win_info, &win_log);
264 if (1 == quit_called)
266 exit_game(&world, &map);