7 #include "keybindings.h"
9 void toggle_window (struct WinMeta * win_meta, struct Win * win) {
10 // Toggle display of window win.
12 suspend_window(win_meta, win);
14 append_window(win_meta, win); }
16 void growshrink_active_window (struct WinMeta * win_meta, char change) {
17 // Grow or shrink active window horizontally or vertically by one cell size.
18 if (0 != win_meta->active) {
19 int height = win_meta->active->height;
20 int width = win_meta->active->width;
23 else if (change == '+')
25 else if (change == '_')
27 else if (change == '*')
29 resize_active_window (win_meta, height, width); } }
31 struct Map init_map () {
32 // Initialize map with some experimental start values.
38 map.cells = malloc(map.width * map.height);
41 for (y = 0; y < map.height; y++)
42 for (x = 0; x < map.width; x++) {
45 if ( 0 == ran % ((x*x) / 3 + 1)
46 || 0 == ran % ((y*y) / 3 + 1)
47 || 0 == ran % ((map.width - x - 1) * (map.width - x - 1) / 3 + 1)
48 || 0 == ran %((map.height - y - 1) * (map.height - y - 1) / 3 + 1))
50 map.cells[(y * map.width) + x] = terrain; }
53 void map_scroll (struct Map * map, char dir) {
54 // Scroll map into direction dir if possible by changing the offset.
55 if ('n' == dir && map->offset_y > 0)
59 else if ('w' == dir && map->offset_x > 0)
64 void next_turn (struct World * world) {
65 // Increment turn and move enemy.
68 char ty = world->monster->y;
69 char tx = world->monster->x;
78 if (tx == world->player->x && ty == world->player->y)
79 update_log(world, "\nThe monster hits you.");
80 else if (is_passable(world, tx, ty)) {
81 world->monster->y = ty;
82 world->monster->x = tx; } }
84 void update_log (struct World * world, char * text) {
85 // Update log with new text to be appended.
87 int len_old = strlen(world->log);
88 int len_new = strlen(text);
89 int len_whole = len_old + len_new + 1;
90 new_text = calloc(len_whole, sizeof(char));
91 memcpy(new_text, world->log, len_old);
92 memcpy(new_text + len_old, text, len_new);
94 world->log = new_text; }
96 char is_passable (struct World * world, int x, int y) {
97 // Check if coordinate on (or beyond) map is accessible to movement.
99 if (0 <= x && x < world->map->width && 0 <= y && y < world->map->height)
100 if ('.' == world->map->cells[y * world->map->width + x])
104 void move_player (struct World * world, char d) {
105 // Move player in direction d, increment turn counter and update log.
106 static char prev = 0;
109 char ty = world->player->y;
110 char tx = world->player->x;
123 if (ty == world->monster->y && tx == world->monster->x)
125 else if (is_passable(world, tx, ty)) {
127 world->player->y = ty;
128 world->player->x = tx; }
129 if (success * d == prev)
130 update_log (world, ".");
133 update_log (world, "\nYou hit the monster.");
135 char * msg = calloc(25, sizeof(char));
136 char * msg_content = "You fail to move";
138 msg_content = "You move";
139 sprintf(msg, "\n%s %s.", msg_content, dir);
140 update_log (world, msg);
145 void player_wait(struct World * world) {
146 // Make player wait one turn.
148 update_log (world, "\nYou wait."); }
152 init_keybindings(&world);
154 world.log = calloc(1, sizeof(char));
155 update_log (&world, "Start!");
156 struct Map map = init_map();
158 struct Player player;
161 world.player = &player;
162 struct Monster monster;
165 world.monster = &monster;
167 WINDOW * screen = initscr();
170 keypad(screen, TRUE);
172 struct WinMeta win_meta = init_win_meta(screen);
173 struct Win win_keys = init_window(&win_meta, "Keys", &world, draw_keys_win);
174 struct Win win_map = init_window(&win_meta, "Map", &world, draw_map_win);
175 struct Win win_info = init_window(&win_meta, "Info", &world, draw_info_win);
176 struct Win win_log = init_window(&win_meta, "Log", &world, draw_log_win);
180 draw_all_windows (&win_meta);
182 if (key == get_action_key(world.keybindings, "quit"))
184 else if (key == get_action_key(world.keybindings, "scroll pad right"))
185 scroll_pad (&win_meta, '+');
186 else if (key == get_action_key(world.keybindings, "scroll pad left"))
187 scroll_pad (&win_meta, '-');
188 else if (key == get_action_key(world.keybindings, "toggle keys window"))
189 toggle_window(&win_meta, &win_keys);
190 else if (key == get_action_key(world.keybindings, "toggle map window"))
191 toggle_window(&win_meta, &win_map);
192 else if (key == get_action_key(world.keybindings, "toggle info window"))
193 toggle_window(&win_meta, &win_info);
194 else if (key == get_action_key(world.keybindings, "toggle log window"))
195 toggle_window(&win_meta, &win_log);
196 else if (key == get_action_key(world.keybindings, "cycle forwards"))
197 cycle_active_window(&win_meta, 'n');
198 else if (key == get_action_key(world.keybindings, "cycle backwards"))
199 cycle_active_window(&win_meta, 'p');
200 else if (key == get_action_key(world.keybindings, "shift forwards"))
201 shift_active_window(&win_meta, 'f');
202 else if (key == get_action_key(world.keybindings, "shift backwards"))
203 shift_active_window(&win_meta, 'b');
204 else if (key == get_action_key(world.keybindings, "grow horizontally"))
205 growshrink_active_window(&win_meta, '*');
206 else if (key == get_action_key(world.keybindings, "shrink horizontally"))
207 growshrink_active_window(&win_meta, '_');
208 else if (key == get_action_key(world.keybindings, "grow vertically"))
209 growshrink_active_window(&win_meta, '+');
210 else if (key == get_action_key(world.keybindings, "shrink vertically"))
211 growshrink_active_window(&win_meta, '-');
212 else if (key == get_action_key(world.keybindings, "save keys"))
213 save_keybindings(&world);
214 else if (key == get_action_key(world.keybindings, "keys nav up"))
215 keyswin_move_selection (&world, 'u');
216 else if (key == get_action_key(world.keybindings, "keys nav down"))
217 keyswin_move_selection (&world, 'd');
218 else if (key == get_action_key(world.keybindings, "keys mod"))
219 keyswin_mod_key (&world, &win_meta);
220 else if (key == get_action_key(world.keybindings, "map up"))
221 map_scroll (&map, 'n');
222 else if (key == get_action_key(world.keybindings, "map down"))
223 map_scroll (&map, 's');
224 else if (key == get_action_key(world.keybindings, "map right"))
225 map_scroll (&map, 'e');
226 else if (key == get_action_key(world.keybindings, "map left"))
227 map_scroll (&map, 'w');
228 else if (key == get_action_key(world.keybindings, "player down"))
229 move_player(&world, 's');
230 else if (key == get_action_key(world.keybindings, "player up"))
231 move_player(&world, 'n');
232 else if (key == get_action_key(world.keybindings, "player right"))
233 move_player(&world, 'e');
234 else if (key == get_action_key(world.keybindings, "player left"))
235 move_player(&world, 'w');
236 else if (key == get_action_key(world.keybindings, "wait") )
237 player_wait (&world); }
240 for (key = 0; key <= world.keyswindata->max; key++)
241 free(world.keybindings[key].name);
242 free(world.keybindings);
243 free(world.keyswindata);