diff --git a/Source/stores.cpp b/Source/stores.cpp index fcead5648..e2f66e311 100644 --- a/Source/stores.cpp +++ b/Source/stores.cpp @@ -2026,7 +2026,9 @@ void TalkEnter() } if (stextsel == sn - 2) { - InitQTextMsg(Towners[talker].gossip); + Towner *target = GetTowner(talker); + assert(target != nullptr); + InitQTextMsg(target->gossip); return; } diff --git a/Source/towners.cpp b/Source/towners.cpp index ea5aaa5ea..4881c0e1b 100644 --- a/Source/towners.cpp +++ b/Source/towners.cpp @@ -841,6 +841,15 @@ bool IsTownerPresent(_talker_id npc) } } +Towner* GetTowner(_talker_id type) +{ + for (Towner& towner : Towners) { + if (towner._ttype == type) + return &towner; + } + return nullptr; +} + void InitTowners() { assert(!CowSprites); diff --git a/Source/towners.h b/Source/towners.h index 0d3b8afad..7e545cbf1 100644 --- a/Source/towners.h +++ b/Source/towners.h @@ -68,6 +68,12 @@ struct Towner { }; extern Towner Towners[NUM_TOWNERS]; +/** + * @brief Maps from a _talker_id value to a pointer to the Towner object, if they have been initialised + * @param type enum constant identifying the towner + * @return Pointer to the Towner or nullptr if they are not available + */ +Towner *GetTowner(_talker_id type); void InitTowners(); void FreeTownerGFX();