Skip to content

Commit

Permalink
♻️Refactor towner initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
AJenbo committed May 6, 2021
1 parent e4e924e commit 4b57732
Show file tree
Hide file tree
Showing 11 changed files with 379 additions and 547 deletions.
2 changes: 1 addition & 1 deletion Source/inv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2280,7 +2280,7 @@ void DoTelekinesis()
NetSendCmdParam1(true, CMD_OPOBJT, pcursobj);
if (pcursitem != -1)
NetSendCmdGItem(true, CMD_REQUESTAGITEM, myplr, myplr, pcursitem);
if (pcursmonst != -1 && !M_Talker(pcursmonst) && monster[pcursmonst].mtalkmsg == 0)
if (pcursmonst != -1 && !M_Talker(pcursmonst) && monster[pcursmonst].mtalkmsg == TEXT_NONE)
NetSendCmdParam1(true, CMD_KNOCKBACK, pcursmonst);
NewCursor(CURSOR_HAND);
}
Expand Down
6 changes: 4 additions & 2 deletions Source/loadsave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,9 @@ static void LoadMonster(LoadHelper *file, int i)
pMonster->mMagicRes = file->nextLE<uint16_t>();
file->skip(2); // Alignment

pMonster->mtalkmsg = file->nextLE<int32_t>();
pMonster->mtalkmsg = static_cast<_speech_id>(file->nextLE<int32_t>());
if (pMonster->mtalkmsg == TEXT_KING1) // Fix original bad mapping of NONE for monsters
pMonster->mtalkmsg = TEXT_NONE;
pMonster->leader = file->nextLE<uint8_t>();
pMonster->leaderflag = file->nextLE<uint8_t>();
pMonster->packsize = file->nextLE<uint8_t>();
Expand Down Expand Up @@ -1582,7 +1584,7 @@ static void SaveMonster(SaveHelper *file, int i)
file->writeLE<uint16_t>(pMonster->mMagicRes);
file->skip(2); // Alignment

file->writeLE<int32_t>(pMonster->mtalkmsg);
file->writeLE<int32_t>(pMonster->mtalkmsg == TEXT_NONE ? 0 : pMonster->mtalkmsg); // Replicate original bad mapping of none for monsters
file->writeLE<uint8_t>(pMonster->leader);
file->writeLE<uint8_t>(pMonster->leaderflag);
file->writeLE<uint8_t>(pMonster->packsize);
Expand Down
3 changes: 2 additions & 1 deletion Source/minitext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "dx.h"
#include "engine.h"
#include "engine/render/cel_render.hpp"
#include "textdat.h"
#include "utils/language.h"
#include "utils/stdcompat/optional.hpp"

Expand Down Expand Up @@ -248,7 +249,7 @@ void InitQuestText()
* @brief Start the given naration
* @param m Index of narration from the alltext table
*/
void InitQTextMsg(int m)
void InitQTextMsg(_speech_id m)
{
if (alltext[m].scrlltxt) {
questlog = false;
Expand Down
3 changes: 2 additions & 1 deletion Source/minitext.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
#pragma once

#include "engine.h"
#include "textdat.h"

namespace devilution {

extern bool qtextflag;

void FreeQuestText();
void InitQuestText();
void InitQTextMsg(int m);
void InitQTextMsg(_speech_id m);

/**
* @brief Draw the quest dialog window decoration and background.
Expand Down
186 changes: 93 additions & 93 deletions Source/monstdat.cpp

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion Source/monstdat.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include <cstdint>

#include "textdat.h"

namespace devilution {

enum _mai_id : int8_t {
Expand Down Expand Up @@ -276,7 +278,7 @@ struct UniqMonstStruct {
uint16_t mUnqAttr; // TODO create enum
uint8_t mUnqVar1;
uint8_t mUnqVar2;
int mtalkmsg;
_speech_id mtalkmsg;
};

extern const MonsterData monsterdata[];
Expand Down
34 changes: 18 additions & 16 deletions Source/monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ void InitMonster(int i, direction rd, int mtype, int x, int y)
monster[i].leader = 0;
monster[i].leaderflag = 0;
monster[i]._mFlags = monst->MData->mFlags;
monster[i].mtalkmsg = 0;
monster[i].mtalkmsg = TEXT_NONE;

if (monster[i]._mAi == AI_GARG) {
monster[i]._mAnimData = monst->Anims[MA_SPECIAL].Data[rd];
Expand Down Expand Up @@ -814,13 +814,13 @@ void PlaceUniqueMonst(int uniqindex, int miniontype, int bosspacksize)

if (gbIsMultiplayer) {
if (Monst->_mAi == AI_LAZHELP)
Monst->mtalkmsg = 0;
Monst->mtalkmsg = TEXT_NONE;
if (Monst->_mAi == AI_LAZURUS && quests[Q_BETRAYER]._qvar1 > 3) {
Monst->_mgoal = MGOAL_NORMAL;
} else if (Monst->mtalkmsg) {
} else if (Monst->mtalkmsg != TEXT_NONE) {
Monst->_mgoal = MGOAL_INQUIRING;
}
} else if (Monst->mtalkmsg) {
} else if (Monst->mtalkmsg != TEXT_NONE) {
Monst->_mgoal = MGOAL_INQUIRING;
}

Expand Down Expand Up @@ -1338,7 +1338,7 @@ void M_Enemy(int i)
continue;
if (monster[mi].position.tile.x == 1 && monster[mi].position.tile.y == 0)
continue;
if (M_Talker(mi) && monster[mi].mtalkmsg)
if (M_Talker(mi) && monster[mi].mtalkmsg != TEXT_NONE)
continue;
if ((Monst->_mFlags & MFLAG_GOLEM) && (monster[mi]._mFlags & MFLAG_GOLEM)) // prevent golems from fighting each other
continue;
Expand Down Expand Up @@ -2458,7 +2458,7 @@ bool M_DoTalk(int i)
quests[Q_BETRAYER]._qvar1 = 6;
monster[i]._mgoal = MGOAL_NORMAL;
monster[i]._msquelch = UINT8_MAX;
monster[i].mtalkmsg = 0;
monster[i].mtalkmsg = TEXT_NONE;
}
return false;
}
Expand Down Expand Up @@ -4231,6 +4231,8 @@ void MAI_Garbud(int i)
case TEXT_GARBUD3:
Monst->mtalkmsg = TEXT_GARBUD4;
break;
default:
break;
}
}

Expand All @@ -4239,7 +4241,7 @@ void MAI_Garbud(int i)
if (!effect_is_playing(USFX_GARBUD4) && Monst->_mgoal == MGOAL_TALKING) {
Monst->_mgoal = MGOAL_NORMAL;
Monst->_msquelch = UINT8_MAX;
Monst->mtalkmsg = 0;
Monst->mtalkmsg = TEXT_NONE;
}
}
}
Expand Down Expand Up @@ -4277,7 +4279,7 @@ void MAI_Zhar(int i)
if (Monst->mtalkmsg == TEXT_ZHAR2) {
if (!effect_is_playing(USFX_ZHAR2) && Monst->_mgoal == MGOAL_TALKING) {
Monst->_msquelch = UINT8_MAX;
Monst->mtalkmsg = 0;
Monst->mtalkmsg = TEXT_NONE;
Monst->_mgoal = MGOAL_NORMAL;
}
}
Expand Down Expand Up @@ -4314,7 +4316,7 @@ void MAI_SnotSpil(int i)
}

if (Monst->mtalkmsg == TEXT_BANNER11 && quests[Q_LTBANNER]._qvar1 == 3) {
Monst->mtalkmsg = 0;
Monst->mtalkmsg = TEXT_NONE;
Monst->_mgoal = MGOAL_NORMAL;
}

Expand All @@ -4325,7 +4327,7 @@ void MAI_SnotSpil(int i)
quests[Q_LTBANNER]._qvar1 = 3;
RedoPlayerVision();
Monst->_msquelch = UINT8_MAX;
Monst->mtalkmsg = 0;
Monst->mtalkmsg = TEXT_NONE;
Monst->_mgoal = MGOAL_NORMAL;
}
}
Expand Down Expand Up @@ -4370,7 +4372,7 @@ void MAI_Lazurus(int i)
quests[Q_BETRAYER]._qvar1 = 6;
Monst->_mgoal = MGOAL_NORMAL;
Monst->_msquelch = UINT8_MAX;
Monst->mtalkmsg = 0;
Monst->mtalkmsg = TEXT_NONE;
}
}

Expand All @@ -4380,12 +4382,12 @@ void MAI_Lazurus(int i)
}

if (Monst->_mgoal == MGOAL_NORMAL || Monst->_mgoal == MGOAL_RETREAT || Monst->_mgoal == MGOAL_MOVE) {
if (!gbIsMultiplayer && quests[Q_BETRAYER]._qvar1 == 4 && Monst->mtalkmsg == 0) { // Fix save games affected by teleport bug
if (!gbIsMultiplayer && quests[Q_BETRAYER]._qvar1 == 4 && Monst->mtalkmsg == TEXT_NONE) { // Fix save games affected by teleport bug
ObjChangeMapResync(1, 18, 20, 24);
RedoPlayerVision();
quests[Q_BETRAYER]._qvar1 = 6;
}
Monst->mtalkmsg = 0;
Monst->mtalkmsg = TEXT_NONE;
MAI_Counselor(i);
}

Expand Down Expand Up @@ -4415,7 +4417,7 @@ void MAI_Lazhelp(int i)
Monst->_mgoal = MGOAL_INQUIRING;
} else {
Monst->_mgoal = MGOAL_NORMAL;
Monst->mtalkmsg = 0;
Monst->mtalkmsg = TEXT_NONE;
}
} else
Monst->_mgoal = MGOAL_NORMAL;
Expand Down Expand Up @@ -4451,7 +4453,7 @@ void MAI_Lachdanan(int i)
if (dFlags[_mx][_my] & BFLAG_VISIBLE) {
if (Monst->mtalkmsg == TEXT_VEIL11) {
if (!effect_is_playing(USFX_LACH3) && Monst->_mgoal == MGOAL_TALKING) {
Monst->mtalkmsg = 0;
Monst->mtalkmsg = TEXT_NONE;
quests[Q_VEIL]._qactive = QUEST_DONE;
M_StartKill(i, -1);
}
Expand Down Expand Up @@ -4484,7 +4486,7 @@ void MAI_Warlord(int i)
Monst->_mmode = MM_TALK;
if (Monst->mtalkmsg == TEXT_WARLRD9 && !effect_is_playing(USFX_WARLRD1) && Monst->_mgoal == MGOAL_TALKING) {
Monst->_msquelch = UINT8_MAX;
Monst->mtalkmsg = 0;
Monst->mtalkmsg = TEXT_NONE;
Monst->_mgoal = MGOAL_NORMAL;
}
}
Expand Down
3 changes: 2 additions & 1 deletion Source/monster.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "engine.h"
#include "miniwin/miniwin.h"
#include "monstdat.h"
#include "textdat.h"

#ifndef NOSOUND
#include "sound.h"
Expand Down Expand Up @@ -184,7 +185,7 @@ struct MonsterStruct { // note: missing field _mAFNum
uint8_t mMaxDamage2;
uint8_t mArmorClass;
uint16_t mMagicRes;
int mtalkmsg;
_speech_id mtalkmsg;
uint8_t leader;
uint8_t leaderflag;
uint8_t packsize;
Expand Down
5 changes: 3 additions & 2 deletions Source/objects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4769,6 +4769,7 @@ void OperateStoryBook(int pnum, int i)
if (object[i]._oSelFlag != 0 && !deltaload && !qtextflag && pnum == myplr) {
object[i]._oAnimFrame = object[i]._oVar4;
PlaySfxLoc(IS_ISCROL, object[i].position.x, object[i].position.y);
auto msg = static_cast<_speech_id>(object[i]._oVar2);
if (object[i]._oVar8 != 0 && currlevel == 24) {
if (IsUberLeverActivated != 1 && quests[Q_NAKRUL]._qactive != QUEST_DONE && objects_lv_24_454B04(object[i]._oVar8)) {
NetSendCmd(false, CMD_NAKRUL);
Expand All @@ -4777,9 +4778,9 @@ void OperateStoryBook(int pnum, int i)
} else if (currlevel >= 21) {
quests[Q_NAKRUL]._qactive = QUEST_ACTIVE;
quests[Q_NAKRUL]._qlog = true;
quests[Q_NAKRUL]._qmsg = static_cast<_speech_id>(object[i]._oVar2);
quests[Q_NAKRUL]._qmsg = msg;
}
InitQTextMsg(object[i]._oVar2);
InitQTextMsg(msg);
NetSendCmdParam1(false, CMD_OPERATEOBJ, i);
}
}
Expand Down
12 changes: 5 additions & 7 deletions Source/stores.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1966,35 +1966,33 @@ void S_SIDEnter()

void S_TalkEnter()
{
int i, tq, sn, la;

if (stextsel == 22) {
StartStore(stextshold);
stextsel = stextlhold;
return;
}

sn = 0;
for (i = 0; i < MAXQUESTS; i++) {
int sn = 0;
for (int i = 0; i < MAXQUESTS; i++) {
if (quests[i]._qactive == QUEST_ACTIVE && Qtalklist[talker][i] != TEXT_NONE && quests[i]._qlog)
sn++;
}
int la = 2;
if (sn > 6) {
sn = 14 - (sn / 2);
la = 1;
} else {
sn = 15 - sn;
la = 2;
}

if (stextsel == sn - 2) {
SetRndSeed(towners[talker]._tSeed);
tq = gossipstart + GenerateRnd(gossipend - gossipstart + 1);
auto tq = static_cast<_speech_id>(gossipstart + GenerateRnd(gossipend - gossipstart + 1));
InitQTextMsg(tq);
return;
}

for (i = 0; i < MAXQUESTS; i++) {
for (int i = 0; i < MAXQUESTS; i++) {
if (quests[i]._qactive == QUEST_ACTIVE && Qtalklist[talker][i] != TEXT_NONE && quests[i]._qlog) {
if (sn == stextsel) {
InitQTextMsg(Qtalklist[talker][i]);
Expand Down
Loading

0 comments on commit 4b57732

Please sign in to comment.