-void init_keybindings(struct World * world) {
-// Initialize keybindings from file "keybindings".
- FILE * file = fopen("keybindings", "r");
- int lines = 0;
- int c = 0;
- int linemax = 0;
- int c_count = 0;
- while (EOF != c) {
- c_count++;
- c = getc(file);
- if ('\n' == c) {
- if (c_count > linemax)
- linemax = c_count;
- c_count = 0;
- lines++; } }
- struct KeyBinding * keybindings = malloc(lines * sizeof(struct KeyBinding));
- fseek(file, 0, SEEK_SET);
- char * command = malloc(linemax);
- int commcount = 0;
- char * digits = malloc(3);
- int digicount = 0;
- int key, digimax;
- int keycount = 0;
- c = getc(file);
- while (EOF != c) {
- if ('\n' == c) {
- keybindings[keycount].name = calloc(commcount + 1, sizeof(char));
- memcpy(keybindings[keycount].name, command, commcount);
- keybindings[keycount].key = key;
- keycount++;
- digicount = 0;
- commcount = 0; }
- else if (-1 != digicount) {
- if (' ' == c) {
- key = 0;
- digimax = digicount - 1;
- while (digicount > 0) {
- digicount--;
- key = key + ((digits[digicount] - 48) * pow(10, digimax - digicount)); }
- digicount = -1; }
- else {
- digits[digicount] = c;
- digicount++; } }
- else {
- command[commcount] = c;
- commcount++; }
- c = getc(file); }
- free(command);
- free(digits);
- fclose(file);
- struct KeysWinData * keyswindata = malloc(sizeof(struct KeysWinData));
- keyswindata->max = lines - 1;
- keyswindata->select = 0;
- keyswindata->edit = 0;
- world->keybindings = keybindings;
- world->keyswindata = keyswindata; }
-
-void save_keybindings(struct World * world) {
-// Write keybindings to keybindings file.
- struct KeysWinData * keyswindata = (struct KeysWinData *) world->keyswindata;
- struct KeyBinding * keybindings = world->keybindings;
- FILE * file = fopen("keybindings", "w");
- int linemax = 0;
- int i;
- for (i = 0; i <= keyswindata->max; i++)
- if (strlen(keybindings[i].name) > linemax)
- linemax = strlen(keybindings[i].name);
- linemax = linemax + 6; // + 6 = + 3 digits + whitespace + newline + null byte
- char * line = malloc(linemax);
- for (i = 0; i <= keyswindata->max; i++) {
- snprintf(line, linemax, "%d %s\n", keybindings[i].key, keybindings[i].name);
- fwrite(line, sizeof(char), strlen(line), file); }
- free(line);
- fclose(file); }
+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;
+ if ('s' == d) {
+ dir = "south";
+ if (is_passable(world, world->player->x, world->player->y + 1)) {
+ world->player->y++;
+ success = 1; } }
+ else if ('n' == d) {
+ dir = "north";
+ if (is_passable(world, world->player->x, world->player->y - 1)) {
+ world->player->y--;
+ success = 1; } }
+ else if ('w' == d) {
+ dir = "west";
+ if (is_passable(world, world->player->x - 1, world->player->y)) {
+ world->player->x--;
+ success = 1; } }
+ else if ('e' == d) {
+ dir = "east";
+ if (is_passable(world, world->player->x + 1, world->player->y)) {
+ world->player->x++;
+ success = 1; } }
+ if (success * d == prev)
+ update_log (world, ".");
+ else {
+ char * msg = calloc(25, sizeof(char));
+ char * msg_content = "You fail to move";
+ if (success)
+ msg_content = "You move";
+ sprintf(msg, "\n%s %s.", msg_content, dir);
+ update_log (world, msg);
+ free(msg); }
+ prev = success * d;
+ update_info (world); }