From 530f9293767eb1f7ddaa809f24c0f2821b9736b8 Mon Sep 17 00:00:00 2001 From: Cameron Reikes Date: Sun, 30 Apr 2023 03:53:32 -0700 Subject: [PATCH] Fix printf buff bug, when given item appears in dialog box --- assets/new_level.json | 4 ++-- character_info.h | 5 +++++ main.c | 51 ++++++++++++++++++++++++++++++++++++------- makeprompt.h | 12 ++++++++-- 4 files changed, 60 insertions(+), 12 deletions(-) diff --git a/assets/new_level.json b/assets/new_level.json index 52cc9c8..b127986 100644 --- a/assets/new_level.json +++ b/assets/new_level.json @@ -371,8 +371,8 @@ "rotation":0, "visible":true, "width":32, - "x":1938.66666666667, - "y":2269.33333333333 + "x":1934.66666666667, + "y":2257.33333333333 }, { "class":"", diff --git a/character_info.h b/character_info.h index 7ab0cce..c0237e6 100644 --- a/character_info.h +++ b/character_info.h @@ -41,30 +41,35 @@ typedef struct { char *global_prompt; char *enum_name; + char *name; // talked about like 'The Player gave `item.name` to the NPC' char *possess; char *discard; } ItemInfo; ItemInfo items[] = { { .enum_name = "none", + .name = "Nothing", .global_prompt = "The player isn't holding anything", .possess = "The player is no longer holding anything", .discard = "The player is no longer holding nothing", }, { .enum_name = "WhiteSquare", + .name = "the white square", .global_prompt = "The player is holding a mysterious white square. It is unknown what strange and erotic abilities one has when they possess the square.", .possess = "The player is now holding the white square", .discard = "The player is no longer holding the white square.", }, { .enum_name = "Boots", + .name = "some boots", .global_prompt = "The player is holding the boots of speed. He is a force to be recogned with in this state, he has great speed while holding the boots.", .possess = "The player is now holding the boots of speed", .discard = "The player is no longer holding the boots of speed", }, { .enum_name = "Tripod", + .name = "the tripod", .global_prompt = "The player is holding a tripod used for holding things up. It's got an odd construction, but Block really likes it for some reason.", .possess = "The player is now holding the tripod", .discard = "The player is no longer holding the tripod.", diff --git a/main.c b/main.c index 422648d..e32e51e 100644 --- a/main.c +++ b/main.c @@ -2085,13 +2085,24 @@ Sentence *last_said_sentence(Entity *npc) return 0; } +typedef enum +{ + DELEM_NPC, + DELEM_PLAYER, + DELEM_NPC_ACTION_DESCRIPTION, +} DialogElementKind; + typedef struct { Sentence s; - bool is_player; + DialogElementKind kind; } DialogElement; -typedef BUFF(DialogElement, REMEMBERED_PERCEPTIONS) Dialog; +// Some perceptions can have multiple dialog elements. +// Like item give perceptions that have an action with both dialog +// and an argument. So worst case every perception has 2 dialog +// elements right now is why it's *2 +typedef BUFF(DialogElement, REMEMBERED_PERCEPTIONS*2) Dialog; Dialog produce_dialog(Entity *talking_to, bool character_names) { assert(talking_to->is_npc); @@ -2101,6 +2112,14 @@ Dialog produce_dialog(Entity *talking_to, bool character_names) if(it->type == NPCDialog) { Sentence to_say = (Sentence){0}; + + if(it->npc_action_type == ACT_give_item) + { + DialogElement new = {0}; + printf_buff(&new.s, "%s gave %s to you", characters[talking_to->npc_kind].name, items[it->given_item].name); + new.kind = DELEM_NPC_ACTION_DESCRIPTION; + BUFF_APPEND(&to_return, new); + } if(character_names) { @@ -2120,7 +2139,7 @@ Dialog produce_dialog(Entity *talking_to, bool character_names) { append_str(&to_say, it->npc_dialog.data); } - BUFF_APPEND(&to_return, ((DialogElement){ .s = to_say, .is_player = false }) ); + BUFF_APPEND(&to_return, ((DialogElement){ .s = to_say, .kind = DELEM_NPC }) ); } else if(it->type == PlayerDialog) { @@ -2130,7 +2149,7 @@ Dialog produce_dialog(Entity *talking_to, bool character_names) append_str(&to_say, "Player: "); } append_str(&to_say, it->player_dialog.data); - BUFF_APPEND(&to_return, ((DialogElement){ .s = to_say, .is_player = true }) ); + BUFF_APPEND(&to_return, ((DialogElement){ .s = to_say, .kind = DELEM_PLAYER }) ); } } return to_return; @@ -2189,14 +2208,22 @@ void draw_dialog_panel(Entity *talking_to, float alpha) Color *colors = calloc(sizeof(*colors), it->s.cur_index); for(int char_i = 0; char_i < it->s.cur_index; char_i++) { - if(it->is_player) + if(it->kind == DELEM_PLAYER) { colors[char_i] = BLACK; } - else + else if(it->kind == DELEM_NPC) { colors[char_i] = colhex(0x345e22); } + else if(it->kind == DELEM_NPC_ACTION_DESCRIPTION) + { + colors[char_i] = colhex(0xb5910e); + } + else + { + assert(false); + } colors[char_i] = blendalpha(colors[char_i], alpha); } float measured_line_height = draw_wrapped_text((WrappedTextParams){true, V2(dialog_panel.upper_left.X, new_line_height), dialog_panel.lower_right.X - dialog_panel.upper_left.X, it->s.data, colors, 0.5f, dialog_panel}); @@ -3745,14 +3772,22 @@ F cost: G + H Color *colors = calloc(sizeof(*colors), it->s.cur_index); for(int char_i = 0; char_i < it->s.cur_index; char_i++) { - if(it->is_player) + if(it->kind == DELEM_PLAYER) { colors[char_i] = WHITE; } - else + else if(it->kind == DELEM_NPC) { colors[char_i] = colhex(0x34e05c); } + else if(it->kind == DELEM_NPC_ACTION_DESCRIPTION) + { + colors[char_i] = colhex(0xebc334); + } + else + { + assert(false); + } colors[char_i] = blendalpha(colors[char_i], alpha); } float measured_line_height = draw_wrapped_text((WrappedTextParams){true, V2(dialog_text_aabb.upper_left.X, new_line_height), dialog_text_aabb.lower_right.X - dialog_text_aabb.upper_left.X, it->s.data, colors, dialog_text_scale, dialog_text_aabb, .screen_space = true}); diff --git a/makeprompt.h b/makeprompt.h index bf5533e..ab9d0fe 100644 --- a/makeprompt.h +++ b/makeprompt.h @@ -412,8 +412,16 @@ bool printf_buff_impl(BuffRef into, const char *format, ...) assert(into.data_elem_size == 1); // characters va_list args; va_start (args, format); - int n = info.max_data_elems - *info.cur_index; - int written = vsnprintf((char*)into.data + *info.cur_index, n, format, args); + size_t n = into.max_data_elems - *into.cur_index; + int written = vsnprintf((char*)into.data + *into.cur_index, n, format, args); + + if(written < 0) + { + } + else + { + *into.cur_index += written; + } // https://cplusplus.com/reference/cstdio/vsnprintf/ bool succeeded = true;