#include #include #include #include #include #include "talkable.h" #include "util.h" #include "event.h" #include "player.h" #define LIMIT 35 struct Talkable default_value = { .x = 0, .y = 0, .name = "default name", .text = "default dialog" }; /* Find the last word boundary between [str] and [limit]. If there is no word boundary (ie. [limit] is in the first word of [str]), returns [limit]. This function always return a position *after* [str] so that we always make progress and don't loop. */ char *word_boundary_before(char *str, char *limit) { char *s = limit; while(s > str) { if(isspace(*s) || !*s) return s; s--; } return limit; } /* Skip spaces at the start of [str] */ char *skip_spaces(char *str) { return str + strspn(str, " \t\n"); } /*draw the dialog of a specified talkable tile*/ void draw_dialog(struct Game *game) { extern bopti_image_t img_dialogue; const char *delim = ";"; int direction = game->player->direction; int dx = (direction == DIR_RIGHT) - (direction == DIR_LEFT); int dy = (direction == DIR_DOWN) - (direction == DIR_UP); struct Talkable *talk = get_dialog_xy(game->map, game->player->pos.x + dx, game->player->pos.y + dy); char *str = strdup(talk->text); char *curr_line = strtok(str, delim); while(curr_line != NULL) { char *event = strchr(curr_line, '~'); if(event) handle_event(game, curr_line+1); else { dimage(43,31,&img_dialogue); dprint(50,40, C_BLACK, "%s", talk->name); int const DIALOG_WIDTH = 295, LINE_HEIGHT = 13; int y = 45 + LINE_HEIGHT; while(*curr_line) { char *end = (char *)drsize(curr_line, NULL, DIALOG_WIDTH, NULL); char *last_word = word_boundary_before(curr_line, end); dtext_opt(50, y, C_BLACK, C_NONE, DTEXT_LEFT, DTEXT_TOP, curr_line, last_word - curr_line); curr_line = skip_spaces(last_word); y += LINE_HEIGHT; } dupdate(); wait_for_input(KEY_SHIFT); } curr_line = strtok(NULL, delim); } } /*find the talkable tile using the player's position*/ struct Talkable* get_dialog_xy(struct Map *map, int x, int y) { int i = 0; while(i < map->dialog_count) { if(map->dialogs[i].x == x && map->dialogs[i].y == y) return &map->dialogs[i]; i++; } return &default_value; }