Browse Source

More init clean up (#2262)

pull/2265/head
Anders Jenbo 5 years ago committed by GitHub
parent
commit
b1d237c823
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 30
      Source/effects.cpp
  2. 243
      Source/items.cpp
  3. 95
      Source/lighting.cpp
  4. 69
      Source/mpqapi.cpp
  5. 53
      Source/msg.cpp

30
Source/effects.cpp

@ -1115,17 +1115,15 @@ static void stream_update()
void InitMonsterSND(int monst)
{
char path[MAX_PATH];
int mtype, i, j;
if (!gbSndInited) {
return;
}
mtype = Monsters[monst].mtype;
for (i = 0; i < 4; i++) {
const int mtype = Monsters[monst].mtype;
for (int i = 0; i < 4; i++) {
if (MonstSndChar[i] != 's' || monsterdata[mtype].snd_special) {
for (j = 0; j < 2; j++) {
for (int j = 0; j < 2; j++) {
char path[MAX_PATH];
sprintf(path, monsterdata[mtype].sndfile, MonstSndChar[i], j + 1);
Monsters[monst].Snds[i][j] = sound_file_load(path);
}
@ -1146,15 +1144,13 @@ void FreeMonsterSnd()
bool calc_snd_position(Point soundPosition, int *plVolume, int *plPan)
{
int pan, volume;
const auto &playerPosition = plr[myplr].position.tile;
const auto delta = soundPosition - playerPosition;
pan = (delta.deltaX - delta.deltaY) * 256;
int pan = (delta.deltaX - delta.deltaY) * 256;
*plPan = clamp(pan, PAN_MIN, PAN_MAX);
volume = playerPosition.ApproxDistance(soundPosition);
int volume = playerPosition.ApproxDistance(soundPosition);
volume *= -64;
if (volume <= ATTENUATION_MIN)
@ -1167,8 +1163,6 @@ bool calc_snd_position(Point soundPosition, int *plVolume, int *plPan)
static void PlaySFX_priv(TSFX *pSFX, bool loc, Point position)
{
int lPan, lVolume;
if (plr[myplr].pLvlLoad != 0 && gbIsMultiplayer) {
return;
}
@ -1180,8 +1174,8 @@ static void PlaySFX_priv(TSFX *pSFX, bool loc, Point position)
return;
}
lPan = 0;
lVolume = 0;
int lVolume = 0;
int lPan = 0;
if (loc && !calc_snd_position(position, &lVolume, &lPan)) {
return;
}
@ -1200,23 +1194,23 @@ static void PlaySFX_priv(TSFX *pSFX, bool loc, Point position)
void PlayEffect(int i, int mode)
{
int sndIdx, mi, lVolume, lPan;
if (plr[myplr].pLvlLoad != 0) {
return;
}
sndIdx = GenerateRnd(2);
int sndIdx = GenerateRnd(2);
if (!gbSndInited || !gbSoundOn || gbBufferMsgs != 0) {
return;
}
mi = monster[i]._mMTidx;
int mi = monster[i]._mMTidx;
TSnd *snd = Monsters[mi].Snds[mode][sndIdx].get();
if (snd == nullptr || snd->isPlaying()) {
return;
}
int lVolume = 0;
int lPan = 0;
if (!calc_snd_position(monster[i].position.tile, &lVolume, &lPan))
return;

243
Source/items.cpp

@ -464,11 +464,9 @@ Point GetRandomAvailableItemPosition()
void AddInitItems()
{
int j, rnd;
int curlv = items_get_currlevel();
rnd = GenerateRnd(3) + 3;
for (j = 0; j < rnd; j++) {
int rnd = GenerateRnd(3) + 3;
for (int j = 0; j < rnd; j++) {
int ii = AllocateItem();
Point position = GetRandomAvailableItemPosition();
@ -921,16 +919,11 @@ void CalcPlrItemVals(int playerId, bool Loadgfx)
void CalcSelfItems(PlayerStruct &player)
{
int i;
ItemStruct *pi;
bool sf, changeflag;
int sa, ma, da;
sa = 0;
ma = 0;
da = 0;
pi = player.InvBody;
for (i = 0; i < NUM_INVLOC; i++, pi++) {
int sa = 0;
int ma = 0;
int da = 0;
ItemStruct *pi = player.InvBody;
for (int i = 0; i < NUM_INVLOC; i++, pi++) {
if (!pi->isEmpty()) {
pi->_iStatFlag = true;
if (pi->_iIdentified) {
@ -940,12 +933,14 @@ void CalcSelfItems(PlayerStruct &player)
}
}
}
bool changeflag;
do {
changeflag = false;
pi = player.InvBody;
for (i = 0; i < NUM_INVLOC; i++, pi++) {
for (int i = 0; i < NUM_INVLOC; i++, pi++) {
if (!pi->isEmpty() && pi->_iStatFlag) {
sf = true;
bool sf = true;
if (sa + player._pBaseStr < pi->_iMinStr)
sf = false;
if (ma + player._pBaseMag < pi->_iMinMag)
@ -1091,19 +1086,19 @@ void GetPlrHandSeed(ItemStruct *h)
*/
void GetGoldSeed(int pnum, ItemStruct *h)
{
int i, ii, s;
bool doneflag;
int s = 0;
bool doneflag;
do {
doneflag = true;
s = AdvanceRndSeed();
for (i = 0; i < numitems; i++) {
ii = itemactive[i];
for (int i = 0; i < numitems; i++) {
int ii = itemactive[i];
if (items[ii]._iSeed == s)
doneflag = false;
}
if (pnum == myplr) {
for (i = 0; i < plr[pnum]._pNumInv; i++) {
for (int i = 0; i < plr[pnum]._pNumInv; i++) {
if (plr[pnum].InvList[i]._iSeed == s)
doneflag = false;
}
@ -1307,11 +1302,8 @@ bool ItemSpaceOk(Point position)
static bool GetItemSpace(Point position, int8_t inum)
{
int rs;
int xx, yy;
bool savail;
yy = 0;
int xx = 0;
int yy = 0;
for (int j = position.y - 1; j <= position.y + 1; j++) {
xx = 0;
for (int i = position.x - 1; i <= position.x + 1; i++) {
@ -1321,7 +1313,7 @@ static bool GetItemSpace(Point position, int8_t inum)
yy++;
}
savail = false;
bool savail = false;
for (int j = 0; j < 3; j++) {
for (int i = 0; i < 3; i++) { // NOLINT(modernize-loop-convert)
if (itemhold[i][j])
@ -1329,7 +1321,7 @@ static bool GetItemSpace(Point position, int8_t inum)
}
}
rs = GenerateRnd(15) + 1;
int rs = GenerateRnd(15) + 1;
if (!savail)
return false;
@ -1475,16 +1467,11 @@ static bool control_WriteStringToBuffer(const char *str)
void GetStaffPower(int i, int lvl, int bs, bool onlygood)
{
int l[256];
char istr[128];
int nl, j, preidx;
int tmp;
tmp = GenerateRnd(10);
preidx = -1;
if (tmp == 0 || onlygood) {
nl = 0;
for (j = 0; PL_Prefix[j].PLPower != IPL_INVALID; j++) {
int preidx = -1;
if (GenerateRnd(10) == 0 || onlygood) {
int nl = 0;
int l[256];
for (int j = 0; PL_Prefix[j].PLPower != IPL_INVALID; j++) {
if (!IsPrefixValidForItemType(j, PLT_STAFF) || PL_Prefix[j].PLMinLvl > lvl)
continue;
if (onlygood && !PL_Prefix[j].PLOk)
@ -1498,6 +1485,7 @@ void GetStaffPower(int i, int lvl, int bs, bool onlygood)
}
if (nl != 0) {
preidx = l[GenerateRnd(nl)];
char istr[128];
sprintf(istr, "%s %s", _(PL_Prefix[preidx].PLName), items[i]._iIName);
strcpy(items[i]._iIName, istr);
items[i]._iMagical = ITEM_QUALITY_MAGIC;
@ -1514,6 +1502,7 @@ void GetStaffPower(int i, int lvl, int bs, bool onlygood)
}
if (!control_WriteStringToBuffer(items[i]._iIName)) {
strcpy(items[i]._iIName, _(AllItemsList[items[i].IDidx].iSName));
char istr[128];
if (preidx != -1) {
sprintf(istr, "%s %s", _(PL_Prefix[preidx].PLName), items[i]._iIName);
strcpy(items[i]._iIName, istr);
@ -1528,79 +1517,77 @@ void GetStaffPower(int i, int lvl, int bs, bool onlygood)
void GetStaffSpell(int i, int lvl, bool onlygood)
{
int l, rv, minc, maxc, v;
char istr[68];
if (!gbIsHellfire && GenerateRnd(4) == 0) {
GetItemPower(i, lvl / 2, lvl, PLT_STAFF, onlygood);
} else {
int maxSpells = gbIsHellfire ? MAX_SPELLS : 37;
l = lvl / 2;
if (l == 0)
l = 1;
rv = GenerateRnd(maxSpells) + 1;
if (gbIsSpawn && lvl > 10)
lvl = 10;
int s = SPL_FIREBOLT;
enum spell_id bs = SPL_NULL;
while (rv > 0) {
int sLevel = GetSpellStaffLevel(static_cast<spell_id>(s));
if (sLevel != -1 && l >= sLevel) {
rv--;
bs = static_cast<spell_id>(s);
}
s++;
if (!gbIsMultiplayer && s == SPL_RESURRECT)
s = SPL_TELEKINESIS;
if (!gbIsMultiplayer && s == SPL_HEALOTHER)
s = SPL_FLARE;
if (s == maxSpells)
s = SPL_FIREBOLT;
}
if (!control_WriteStringToBuffer(istr))
strcpy(istr, fmt::format(_("{:s} of {:s}"), items[i]._iName, _(spelldata[bs].sNameText)).c_str());
strcpy(istr, fmt::format(_("Staff of {:s}"), _(spelldata[bs].sNameText)).c_str());
strcpy(items[i]._iName, istr);
strcpy(items[i]._iIName, istr);
return;
}
minc = spelldata[bs].sStaffMin;
maxc = spelldata[bs].sStaffMax - minc + 1;
items[i]._iSpell = bs;
items[i]._iCharges = minc + GenerateRnd(maxc);
items[i]._iMaxCharges = items[i]._iCharges;
int maxSpells = gbIsHellfire ? MAX_SPELLS : 37;
int l = lvl / 2;
if (l == 0)
l = 1;
int rv = GenerateRnd(maxSpells) + 1;
items[i]._iMinMag = spelldata[bs].sMinInt;
v = items[i]._iCharges * spelldata[bs].sStaffCost / 5;
items[i]._ivalue += v;
items[i]._iIvalue += v;
GetStaffPower(i, lvl, bs, onlygood);
if (gbIsSpawn && lvl > 10)
lvl = 10;
int s = SPL_FIREBOLT;
enum spell_id bs = SPL_NULL;
while (rv > 0) {
int sLevel = GetSpellStaffLevel(static_cast<spell_id>(s));
if (sLevel != -1 && l >= sLevel) {
rv--;
bs = static_cast<spell_id>(s);
}
s++;
if (!gbIsMultiplayer && s == SPL_RESURRECT)
s = SPL_TELEKINESIS;
if (!gbIsMultiplayer && s == SPL_HEALOTHER)
s = SPL_FLARE;
if (s == maxSpells)
s = SPL_FIREBOLT;
}
char istr[68];
if (!control_WriteStringToBuffer(istr))
strcpy(istr, fmt::format(_("{:s} of {:s}"), items[i]._iName, _(spelldata[bs].sNameText)).c_str());
strcpy(istr, fmt::format(_("Staff of {:s}"), _(spelldata[bs].sNameText)).c_str());
strcpy(items[i]._iName, istr);
strcpy(items[i]._iIName, istr);
int minc = spelldata[bs].sStaffMin;
int maxc = spelldata[bs].sStaffMax - minc + 1;
items[i]._iSpell = bs;
items[i]._iCharges = minc + GenerateRnd(maxc);
items[i]._iMaxCharges = items[i]._iCharges;
items[i]._iMinMag = spelldata[bs].sMinInt;
int v = items[i]._iCharges * spelldata[bs].sStaffCost / 5;
items[i]._ivalue += v;
items[i]._iIvalue += v;
GetStaffPower(i, lvl, bs, onlygood);
}
void GetOilType(int i, int max_lvl)
{
int cnt, t, j, r;
char rnd[32];
int cnt = 2;
char rnd[32] = { 5, 6 };
if (!gbIsMultiplayer) {
if (max_lvl == 0)
max_lvl = 1;
cnt = 0;
for (j = 0; j < (int)(sizeof(OilLevels) / sizeof(OilLevels[0])); j++) {
cnt = 0;
for (size_t j = 0; j < sizeof(OilLevels) / sizeof(OilLevels[0]); j++) {
if (OilLevels[j] <= max_lvl) {
rnd[cnt] = j;
cnt++;
}
}
r = GenerateRnd(cnt);
t = rnd[r];
} else {
r = GenerateRnd(2);
t = (r != 0 ? 6 : 5);
}
int t = rnd[GenerateRnd(cnt)];
strcpy(items[i]._iName, _(OilNames[t]));
strcpy(items[i]._iIName, _(OilNames[t]));
items[i]._iMiscId = OilMagic[t];
@ -1681,9 +1668,9 @@ int PLVal(int pv, int p1, int p2, int minv, int maxv)
void SaveItemPower(int i, item_effect_type power, int param1, int param2, int minval, int maxval, int multval)
{
int r, r2;
int r = RndPL(param1, param2);
int r2;
r = RndPL(param1, param2);
switch (power) {
case IPL_TOHIT:
items[i]._iPLToHit += r;
@ -2230,9 +2217,6 @@ void SetupItem(int i)
int RndItem(int m)
{
int i, ri, r;
int ril[512];
if ((monster[m].MData->mTreasure & 0x8000) != 0)
return -((monster[m].MData->mTreasure & 0xFFF) + 1);
@ -2245,8 +2229,10 @@ int RndItem(int m)
if (GenerateRnd(100) > 25)
return IDI_GOLD + 1;
ri = 0;
for (i = 0; AllItemsList[i].iLoc != ILOC_INVALID; i++) {
int ril[512];
int ri = 0;
for (int i = 0; AllItemsList[i].iLoc != ILOC_INVALID; i++) {
if (!IsItemAvailable(i))
continue;
@ -2266,25 +2252,24 @@ int RndItem(int m)
ri--;
}
r = GenerateRnd(ri);
int r = GenerateRnd(ri);
return ril[r] + 1;
}
int RndUItem(int m)
{
int ril[512];
bool okflag;
if (m != -1 && (monster[m].MData->mTreasure & 0x8000) != 0 && !gbIsMultiplayer)
return -((monster[m].MData->mTreasure & 0xFFF) + 1);
int ril[512];
int curlv = items_get_currlevel();
int ri = 0;
for (int i = 0; AllItemsList[i].iLoc != ILOC_INVALID; i++) {
if (!IsItemAvailable(i))
continue;
okflag = true;
bool okflag = true;
if (AllItemsList[i].iRnd == IDROP_NEVER)
okflag = false;
if (m != -1) {
@ -2315,11 +2300,11 @@ int RndUItem(int m)
int RndAllItems()
{
int ril[512];
if (GenerateRnd(100) > 25)
return 0;
int ril[512];
int curlv = items_get_currlevel();
int ri = 0;
for (int i = 0; AllItemsList[i].iLoc != ILOC_INVALID; i++) {
@ -3872,15 +3857,13 @@ void PrintItemDur(ItemStruct *x)
void UseItem(int p, item_misc_id Mid, spell_id spl)
{
int l, j;
auto &player = plr[p];
switch (Mid) {
case IMISC_HEAL:
case IMISC_FOOD:
j = player._pMaxHP >> 8;
l = ((j / 2) + GenerateRnd(j)) << 6;
case IMISC_FOOD: {
int j = player._pMaxHP >> 8;
int l = ((j / 2) + GenerateRnd(j)) << 6;
if (player._pClass == HeroClass::Warrior || player._pClass == HeroClass::Barbarian)
l *= 2;
if (player._pClass == HeroClass::Rogue || player._pClass == HeroClass::Monk || player._pClass == HeroClass::Bard)
@ -3888,15 +3871,15 @@ void UseItem(int p, item_misc_id Mid, spell_id spl)
player._pHitPoints = std::min(player._pHitPoints + l, player._pMaxHP);
player._pHPBase = std::min(player._pHPBase + l, player._pMaxHPBase);
drawhpflag = true;
break;
} break;
case IMISC_FULLHEAL:
player._pHitPoints = player._pMaxHP;
player._pHPBase = player._pMaxHPBase;
drawhpflag = true;
break;
case IMISC_MANA:
j = player._pMaxMana >> 8;
l = ((j / 2) + GenerateRnd(j)) << 6;
case IMISC_MANA: {
int j = player._pMaxMana >> 8;
int l = ((j / 2) + GenerateRnd(j)) << 6;
if (player._pClass == HeroClass::Sorcerer)
l *= 2;
if (player._pClass == HeroClass::Rogue || player._pClass == HeroClass::Monk || player._pClass == HeroClass::Bard)
@ -3906,7 +3889,7 @@ void UseItem(int p, item_misc_id Mid, spell_id spl)
player._pManaBase = std::min(player._pManaBase + l, player._pMaxManaBase);
drawmanaflag = true;
}
break;
} break;
case IMISC_FULLMANA:
if ((player._pIFlags & ISPL_NOMANA) == 0) {
player._pMana = player._pMaxMana;
@ -3936,9 +3919,9 @@ void UseItem(int p, item_misc_id Mid, spell_id spl)
drawhpflag = true;
}
break;
case IMISC_REJUV:
j = player._pMaxHP >> 8;
l = ((j / 2) + GenerateRnd(j)) << 6;
case IMISC_REJUV: {
int j = player._pMaxHP >> 8;
int l = ((j / 2) + GenerateRnd(j)) << 6;
if (player._pClass == HeroClass::Warrior || player._pClass == HeroClass::Barbarian)
l *= 2;
if (player._pClass == HeroClass::Rogue)
@ -3957,7 +3940,7 @@ void UseItem(int p, item_misc_id Mid, spell_id spl)
player._pManaBase = std::min(player._pManaBase + l, player._pMaxManaBase);
drawmanaflag = true;
}
break;
} break;
case IMISC_FULLREJUV:
player._pHitPoints = player._pMaxHP;
player._pHPBase = player._pMaxHPBase;
@ -4167,17 +4150,14 @@ void SpawnSmith(int lvl)
{
constexpr int PinnedItemCount = 0;
int maxValue, maxItems;
ItemStruct holditem;
holditem = items[0];
int maxValue = 140000;
int maxItems = 20;
if (gbIsHellfire) {
maxValue = 200000;
maxItems = 25;
} else {
maxValue = 140000;
maxItems = 20;
}
int iCnt = GenerateRnd(maxItems - 10) + 10;
@ -4394,9 +4374,6 @@ void SpawnWitch(int lvl)
{
constexpr int PinnedItemCount = 3;
int iCnt;
int idata, maxlvl, maxValue;
int j = PinnedItemCount;
memset(&items[0], 0, sizeof(*items));
@ -4415,9 +4392,11 @@ void SpawnWitch(int lvl)
witchitem[2]._iCreateInfo = lvl;
witchitem[2]._iStatFlag = true;
int maxValue = 140000;
int reservedItems = 12;
if (gbIsHellfire) {
iCnt = GenerateRnd(WITCH_ITEMS - 10) + 10;
maxValue = 200000;
reservedItems = 10;
int books = GenerateRnd(4);
for (int i = 114, bCnt = 0; i <= 117 && bCnt < books; ++i) {
@ -4439,18 +4418,16 @@ void SpawnWitch(int lvl)
j++;
bCnt++;
}
} else {
iCnt = GenerateRnd(WITCH_ITEMS - 12) + 10;
maxValue = 140000;
}
int iCnt = GenerateRnd(WITCH_ITEMS - reservedItems) + 10;
for (int i = j; i < iCnt; i++) {
do {
memset(&items[0], 0, sizeof(*items));
items[0]._iSeed = AdvanceRndSeed();
idata = RndWitchItem(lvl) - 1;
int idata = RndWitchItem(lvl) - 1;
GetItemAttrs(0, idata, lvl);
maxlvl = -1;
int maxlvl = -1;
if (GenerateRnd(100) <= 5)
maxlvl = 2 * lvl;
if (maxlvl == -1 && items[0]._iMiscId == IMISC_STAFF)

95
Source/lighting.cpp

@ -493,10 +493,6 @@ char GetLight(int x, int y)
void DoLighting(Point position, int nRadius, int Lnum)
{
int x, y, v, mult, radius_block;
int min_x, max_x, min_y, max_y;
int dist_x, dist_y, temp_x, temp_y;
int xoff = 0;
int yoff = 0;
int light_x = 0;
@ -517,28 +513,24 @@ void DoLighting(Point position, int nRadius, int Lnum)
}
}
dist_x = xoff;
dist_y = yoff;
int dist_x = xoff;
int dist_y = yoff;
int min_x = 15;
if (position.x - 15 < 0) {
min_x = position.x + 1;
} else {
min_x = 15;
}
int max_x = 15;
if (position.x + 15 > MAXDUNX) {
max_x = MAXDUNX - position.x;
} else {
max_x = 15;
}
int min_y = 15;
if (position.y - 15 < 0) {
min_y = position.y + 1;
} else {
min_y = 15;
}
int max_y = 15;
if (position.y + 15 > MAXDUNY) {
max_y = MAXDUNY - position.y;
} else {
max_y = 15;
}
if (position.x >= 0 && position.x < MAXDUNX && position.y >= 0 && position.y < MAXDUNY) {
@ -549,14 +541,14 @@ void DoLighting(Point position, int nRadius, int Lnum)
}
}
mult = xoff + 8 * yoff;
for (y = 0; y < min_y; y++) {
for (x = 1; x < max_x; x++) {
radius_block = lightblock[mult][y][x];
int mult = xoff + 8 * yoff;
for (int y = 0; y < min_y; y++) {
for (int x = 1; x < max_x; x++) {
int radius_block = lightblock[mult][y][x];
if (radius_block < 128) {
temp_x = position.x + x;
temp_y = position.y + y;
v = lightradius[nRadius][radius_block];
int temp_x = position.x + x;
int temp_y = position.y + y;
int v = lightradius[nRadius][radius_block];
if (temp_x >= 0 && temp_x < MAXDUNX && temp_y >= 0 && temp_y < MAXDUNY)
if (v < GetLight(temp_x, temp_y))
SetLight(temp_x, temp_y, v);
@ -565,13 +557,13 @@ void DoLighting(Point position, int nRadius, int Lnum)
}
RotateRadius(&xoff, &yoff, &dist_x, &dist_y, &light_x, &light_y, &block_x, &block_y);
mult = xoff + 8 * yoff;
for (y = 0; y < max_y; y++) {
for (x = 1; x < max_x; x++) {
radius_block = lightblock[mult][y + block_y][x + block_x];
for (int y = 0; y < max_y; y++) {
for (int x = 1; x < max_x; x++) {
int radius_block = lightblock[mult][y + block_y][x + block_x];
if (radius_block < 128) {
temp_x = position.x + y;
temp_y = position.y - x;
v = lightradius[nRadius][radius_block];
int temp_x = position.x + y;
int temp_y = position.y - x;
int v = lightradius[nRadius][radius_block];
if (temp_x >= 0 && temp_x < MAXDUNX && temp_y >= 0 && temp_y < MAXDUNY)
if (v < GetLight(temp_x, temp_y))
SetLight(temp_x, temp_y, v);
@ -580,13 +572,13 @@ void DoLighting(Point position, int nRadius, int Lnum)
}
RotateRadius(&xoff, &yoff, &dist_x, &dist_y, &light_x, &light_y, &block_x, &block_y);
mult = xoff + 8 * yoff;
for (y = 0; y < max_y; y++) {
for (x = 1; x < min_x; x++) {
radius_block = lightblock[mult][y + block_y][x + block_x];
for (int y = 0; y < max_y; y++) {
for (int x = 1; x < min_x; x++) {
int radius_block = lightblock[mult][y + block_y][x + block_x];
if (radius_block < 128) {
temp_x = position.x - x;
temp_y = position.y - y;
v = lightradius[nRadius][radius_block];
int temp_x = position.x - x;
int temp_y = position.y - y;
int v = lightradius[nRadius][radius_block];
if (temp_x >= 0 && temp_x < MAXDUNX && temp_y >= 0 && temp_y < MAXDUNY)
if (v < GetLight(temp_x, temp_y))
SetLight(temp_x, temp_y, v);
@ -595,13 +587,13 @@ void DoLighting(Point position, int nRadius, int Lnum)
}
RotateRadius(&xoff, &yoff, &dist_x, &dist_y, &light_x, &light_y, &block_x, &block_y);
mult = xoff + 8 * yoff;
for (y = 0; y < min_y; y++) {
for (x = 1; x < min_x; x++) {
radius_block = lightblock[mult][y + block_y][x + block_x];
for (int y = 0; y < min_y; y++) {
for (int x = 1; x < min_x; x++) {
int radius_block = lightblock[mult][y + block_y][x + block_x];
if (radius_block < 128) {
temp_x = position.x - y;
temp_y = position.y + x;
v = lightradius[nRadius][radius_block];
int temp_x = position.x - y;
int temp_y = position.y + x;
int v = lightradius[nRadius][radius_block];
if (temp_x >= 0 && temp_x < MAXDUNX && temp_y >= 0 && temp_y < MAXDUNY)
if (v < GetLight(temp_x, temp_y))
SetLight(temp_x, temp_y, v);
@ -663,9 +655,6 @@ void DoUnVision(Point position, int nRadius)
void DoVision(Point position, int nRadius, bool doautomap, bool visible)
{
bool nBlockerFlag;
int nCrawlX, nCrawlY, nLineLen, nTrans;
int j, k, v, x1adj, x2adj, y1adj, y2adj;
if (position.x >= 0 && position.x <= MAXDUNX && position.y >= 0 && position.y <= MAXDUNY) {
if (doautomap) {
@ -680,15 +669,17 @@ void DoVision(Point position, int nRadius, bool doautomap, bool visible)
dFlags[position.x][position.y] |= BFLAG_VISIBLE;
}
for (v = 0; v < 4; v++) {
for (j = 0; j < 23; j++) {
nBlockerFlag = false;
nLineLen = 2 * (nRadius - RadiusAdj[j]);
for (k = 0; k < nLineLen && !nBlockerFlag; k += 2) {
x1adj = 0;
x2adj = 0;
y1adj = 0;
y2adj = 0;
for (int v = 0; v < 4; v++) {
for (int j = 0; j < 23; j++) {
bool nBlockerFlag = false;
int nLineLen = 2 * (nRadius - RadiusAdj[j]);
for (int k = 0; k < nLineLen && !nBlockerFlag; k += 2) {
int x1adj = 0;
int x2adj = 0;
int y1adj = 0;
int y2adj = 0;
int nCrawlX = 0;
int nCrawlY = 0;
switch (v) {
case 0:
nCrawlX = position.x + vCrawlTable[j][k];
@ -740,7 +731,7 @@ void DoVision(Point position, int nRadius, bool doautomap, bool visible)
}
dFlags[nCrawlX][nCrawlY] |= BFLAG_VISIBLE;
if (!nBlockerFlag) {
nTrans = dTransVal[nCrawlX][nCrawlY];
int nTrans = dTransVal[nCrawlX][nCrawlY];
if (nTrans != 0) {
TransList[nTrans] = true;
}

69
Source/mpqapi.cpp

@ -448,10 +448,8 @@ int mpqapi_find_free_block(uint32_t size, uint32_t *block_size)
static int mpqapi_get_hash_index(int index, uint32_t hash_a, uint32_t hash_b)
{
DWORD idx, i;
i = INDEX_ENTRIES;
for (idx = index & 0x7FF; cur_archive.sgpHashTbl[idx].block != -1; idx = (idx + 1) & 0x7FF) {
int i = INDEX_ENTRIES;
for (int idx = index & 0x7FF; cur_archive.sgpHashTbl[idx].block != -1; idx = (idx + 1) & 0x7FF) {
if (i-- == 0)
break;
if (cur_archive.sgpHashTbl[idx].hashcheck[0] != hash_a)
@ -474,21 +472,19 @@ static int FetchHandle(const char *pszName)
void mpqapi_remove_hash_entry(const char *pszName)
{
_HASHENTRY *pHashTbl;
_BLOCKENTRY *blockEntry;
int hIdx, block_offset, block_size;
hIdx = FetchHandle(pszName);
if (hIdx != -1) {
pHashTbl = &cur_archive.sgpHashTbl[hIdx];
blockEntry = &cur_archive.sgpBlockTbl[pHashTbl->block];
pHashTbl->block = -2;
block_offset = blockEntry->offset;
block_size = blockEntry->sizealloc;
memset(blockEntry, 0, sizeof(*blockEntry));
mpqapi_alloc_block(block_offset, block_size);
cur_archive.modified = true;
}
int hIdx = FetchHandle(pszName);
if (hIdx == -1) {
return;
}
_HASHENTRY *pHashTbl = &cur_archive.sgpHashTbl[hIdx];
_BLOCKENTRY *blockEntry = &cur_archive.sgpBlockTbl[pHashTbl->block];
pHashTbl->block = -2;
int block_offset = blockEntry->offset;
int block_size = blockEntry->sizealloc;
memset(blockEntry, 0, sizeof(*blockEntry));
mpqapi_alloc_block(block_offset, block_size);
cur_archive.modified = true;
}
void mpqapi_remove_hash_entries(bool (*fnGetName)(uint8_t, char *))
@ -502,22 +498,19 @@ void mpqapi_remove_hash_entries(bool (*fnGetName)(uint8_t, char *))
static _BLOCKENTRY *mpqapi_add_file(const char *pszName, _BLOCKENTRY *pBlk, int block_index)
{
DWORD h1, h2, h3;
int hIdx;
h1 = Hash(pszName, 0);
h2 = Hash(pszName, 1);
h3 = Hash(pszName, 2);
uint32_t h1 = Hash(pszName, 0);
uint32_t h2 = Hash(pszName, 1);
uint32_t h3 = Hash(pszName, 2);
if (mpqapi_get_hash_index(h1, h2, h3) != -1)
app_fatal("Hash collision between \"%s\" and existing file\n", pszName);
hIdx = h1 & 0x7FF;
int hIdx = h1 & 0x7FF;
bool hasSpace = false;
for (int i = 0; i < INDEX_ENTRIES; i++) {
if (cur_archive.sgpHashTbl[hIdx].block == -1 || cur_archive.sgpHashTbl[hIdx].block == -2) {
hasSpace = true;
break;
}
}
hIdx = (hIdx + 1) & 0x7FF;
}
if (!hasSpace)
@ -630,19 +623,17 @@ bool mpqapi_write_file(const char *pszName, const byte *pbData, size_t dwLen)
void mpqapi_rename(char *pszOld, char *pszNew)
{
int index, block;
_HASHENTRY *hashEntry;
_BLOCKENTRY *blockEntry;
index = FetchHandle(pszOld);
if (index != -1) {
hashEntry = &cur_archive.sgpHashTbl[index];
block = hashEntry->block;
blockEntry = &cur_archive.sgpBlockTbl[block];
hashEntry->block = -2;
mpqapi_add_file(pszNew, blockEntry, block);
cur_archive.modified = true;
int index = FetchHandle(pszOld);
if (index == -1) {
return;
}
_HASHENTRY *hashEntry = &cur_archive.sgpHashTbl[index];
int block = hashEntry->block;
_BLOCKENTRY *blockEntry = &cur_archive.sgpBlockTbl[block];
hashEntry->block = -2;
mpqapi_add_file(pszNew, blockEntry, block);
cur_archive.modified = true;
}
bool mpqapi_has_file(const char *pszName)

53
Source/msg.cpp

@ -283,18 +283,17 @@ static byte *DeltaImportMonster(byte *src, DMonsterStr *dst)
static byte *DeltaExportJunk(byte *dst)
{
int i, q;
for (i = 0; i < MAXPORTAL; i++) {
if (sgJunk.portal[i].x == 0xFF) {
for (auto &portal : sgJunk.portal) {
if (portal.x == 0xFF) {
*dst++ = byte { 0xFF };
} else {
memcpy(dst, &sgJunk.portal[i], sizeof(DPortal));
memcpy(dst, &portal, sizeof(DPortal));
dst += sizeof(DPortal);
}
}
for (i = 0, q = 0; i < MAXQUESTS; i++) {
int q = 0;
for (int i = 0; i < MAXQUESTS; i++) {
if (!questlist[i].isSinglePlayerOnly) {
sgJunk.quests[q].qlog = quests[i]._qlog ? 1 : 0;
sgJunk.quests[q].qstate = quests[i]._qactive;
@ -310,9 +309,7 @@ static byte *DeltaExportJunk(byte *dst)
static void DeltaImportJunk(byte *src)
{
int i, q;
for (i = 0; i < MAXPORTAL; i++) {
for (int i = 0; i < MAXPORTAL; i++) {
if (*src == byte { 0xFF }) {
memset(&sgJunk.portal[i], 0xFF, sizeof(DPortal));
src++;
@ -330,7 +327,8 @@ static void DeltaImportJunk(byte *src)
}
}
for (i = 0, q = 0; i < MAXQUESTS; i++) {
int q = 0;
for (int i = 0; i < MAXQUESTS; i++) {
if (!questlist[i].isSinglePlayerOnly) {
memcpy(&sgJunk.quests[q], src, sizeof(MultiQuests));
src += sizeof(MultiQuests);
@ -705,21 +703,16 @@ void DeltaSaveLevel()
void DeltaLoadLevel()
{
int ot;
int i, j, k, l;
int x, y, xx, yy;
bool done;
if (!gbIsMultiplayer)
return;
deltaload = true;
if (currlevel != 0) {
for (i = 0; i < nummonsters; i++) {
for (int i = 0; i < nummonsters; i++) {
if (sgLevels[currlevel].monster[i]._mx != 0xFF) {
M_ClearSquares(i);
x = sgLevels[currlevel].monster[i]._mx;
y = sgLevels[currlevel].monster[i]._my;
int x = sgLevels[currlevel].monster[i]._mx;
int y = sgLevels[currlevel].monster[i]._my;
monster[i].position.tile = { x, y };
monster[i].position.old = { x, y };
monster[i].position.future = { x, y };
@ -754,7 +747,7 @@ void DeltaLoadLevel()
memcpy(AutomapView, &sgLocals[currlevel], sizeof(AutomapView));
}
for (i = 0; i < MAXITEMS; i++) {
for (int i = 0; i < MAXITEMS; i++) {
if (sgLevels[currlevel].item[i].bCmd != 0xFF) {
if (sgLevels[currlevel].item[i].bCmd == CMD_WALKXY) {
int ii = FindGetItem(
@ -804,15 +797,15 @@ void DeltaLoadLevel()
items[ii]._iAC = sgLevels[currlevel].item[i].bAC;
items[ii].dwBuff = sgLevels[currlevel].item[i].dwBuff;
}
x = sgLevels[currlevel].item[i].x;
y = sgLevels[currlevel].item[i].y;
int x = sgLevels[currlevel].item[i].x;
int y = sgLevels[currlevel].item[i].y;
if (!CanPut({ x, y })) {
done = false;
for (k = 1; k < 50 && !done; k++) {
for (j = -k; j <= k && !done; j++) {
yy = y + j;
for (l = -k; l <= k && !done; l++) {
xx = x + l;
bool done = false;
for (int k = 1; k < 50 && !done; k++) {
for (int j = -k; j <= k && !done; j++) {
int yy = y + j;
for (int l = -k; l <= k && !done; l++) {
int xx = x + l;
if (CanPut({ xx, yy })) {
done = true;
x = xx;
@ -830,7 +823,7 @@ void DeltaLoadLevel()
}
if (currlevel != 0) {
for (i = 0; i < MAXOBJECTS; i++) {
for (int i = 0; i < MAXOBJECTS; i++) {
switch (sgLevels[currlevel].object[i].bCmd) {
case CMD_OPENDOOR:
case CMD_CLOSEDOOR:
@ -846,8 +839,8 @@ void DeltaLoadLevel()
}
}
for (i = 0; i < nobjects; i++) {
ot = object[objectactive[i]]._otype;
for (int i = 0; i < nobjects; i++) {
int ot = object[objectactive[i]]._otype;
if (ot == OBJ_TRAPL || ot == OBJ_TRAPR)
Obj_Trap(objectactive[i]);
}

Loading…
Cancel
Save