+int get_action_key (struct KeyBinding * keybindings, char * name) {
+// Return key matching name in keybindings.
+ int i = 0;
+ while (strcmp(keybindings[i].name, name) )
+ i++;
+ return keybindings[i].key; }
+
+char * get_keyname(int keycode) {
+// Translate some keycodes to readable names of max 9 chars.
+ char * keyname;
+ keyname = malloc(15);
+ if (32 < keycode && keycode < 127)
+ sprintf(keyname, "%c", keycode);
+ else if (keycode == 9)
+ sprintf(keyname, "TAB");
+ else if (keycode == 10)
+ sprintf(keyname, "RETURN");
+ else if (keycode == 27)
+ sprintf(keyname, "ESCAPE");
+ else if (keycode == 32)
+ sprintf(keyname, "SPACE");
+ else if (keycode == KEY_UP)
+ sprintf(keyname, "UP");
+ else if (keycode == KEY_DOWN)
+ sprintf(keyname, "DOWN");
+ else if (keycode == KEY_LEFT)
+ sprintf(keyname, "LEFT");
+ else if (keycode == KEY_RIGHT)
+ sprintf(keyname, "RIGHT");
+ else if (keycode == KEY_HOME)
+ sprintf(keyname, "HOME");
+ else if (keycode == KEY_BACKSPACE)
+ sprintf(keyname, "BACKSPACE");
+ else if (keycode >= KEY_F0 && keycode <= KEY_F(63)) {
+ int f = keycode - KEY_F0;
+ sprintf(keyname, "F%d", f); }
+ else if (keycode == KEY_DC)
+ sprintf(keyname, "DELETE");
+ else if (keycode == KEY_IC)
+ sprintf(keyname, "INSERT");
+ else if (keycode == KEY_NPAGE)
+ sprintf(keyname, "NEXT PAGE");
+ else if (keycode == KEY_PPAGE)
+ sprintf(keyname, "PREV PAGE");
+ else if (keycode == KEY_END)
+ sprintf(keyname, "END");
+ else
+ sprintf(keyname, "(unknown)");
+ return keyname; }
+
+void keyswin_mod_key (struct World * world, struct WinMeta * win_meta) {
+// In keybindings window, mark selection modifiable, modify key. Ensure max of three digits in key code field.
+ world->keyswindata->edit = 1;
+ draw_all_windows (win_meta);
+ int key = getch();
+ if (key < 1000)
+ world->keybindings[world->keyswindata->select].key = key;
+ world->keyswindata->edit = 0; }
+
+void keyswin_move_selection (struct World * world, char dir) {
+// In keybindings window, move selection upwards or downwards (if within limits of list length).
+ if ('u' == dir && world->keyswindata->select > 0)
+ world->keyswindata->select--;
+ else if ('d' == dir && world->keyswindata->select < world->keyswindata->max)
+ world->keyswindata->select++; }
+
+char is_passable (struct World * world, int x, int y) {
+// Check if coordinate on (or beyond) map is accessible to movement.
+ char passable = 0;
+ if (0 <= x && x < world->map->width && 0 <= y && y < world->map->height)
+ if ('.' == world->map->cells[y * world->map->width + x])
+ passable = 1;
+ return passable; }
+
+void move_player (struct World * world, char d) {
+// Move player in direction d, increment turn counter and update log.
+ static char prev = 0;
+ char success = 0;
+ char * dir;
+ char ty = world->player->y;
+ char tx = world->player->x;
+ if ('s' == d) {
+ dir = "south";
+ ty++; }
+ if ('n' == d) {
+ dir = "north";
+ ty--; }
+ if ('w' == d) {
+ dir = "west";
+ tx--; }
+ if ('e' == d) {
+ dir = "east";
+ tx++; }
+ if (ty == world->monster->y && tx == world->monster->x)
+ success = 2;
+ else if (is_passable(world, tx, ty)) {
+ success = 1;
+ world->player->y = ty;
+ world->player->x = tx; }
+ if (success * d == prev)
+ update_log (world, ".");
+ else {
+ if (2 == success)
+ update_log (world, "\nYou hit the monster.");
+ else {
+ char * msg = calloc(25, sizeof(char));
+ char * msg_content = "You fail to move";
+ if (1 == success)
+ msg_content = "You move";
+ sprintf(msg, "\n%s %s.", msg_content, dir);
+ update_log (world, msg);
+ free(msg); } }
+ prev = success * d;
+ next_turn (world); }
+
+void player_wait(struct World * world) {
+// Make player wait one turn.
+ next_turn (world);
+ update_log (world, "\nYou wait."); }
+