You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

278 lines
6.7 KiB

/**
* @file sync.cpp
*
* Implementation of functionality for syncing game state with other players.
*/
#include <climits>
#include "gendung.h"
#include "monster.h"
#include "player.h"
8 years ago
namespace devilution {
namespace {
uint16_t sgnMonsterPriority[MAXMONSTERS];
int sgnMonsters;
uint16_t sgwLRU[MAXMONSTERS];
int sgnSyncItem;
int sgnSyncPInv;
8 years ago
void SyncOneMonster()
8 years ago
{
for (int i = 0; i < ActiveMonsterCount; i++) {
int m = ActiveMonsters[i];
auto &monster = Monsters[m];
sgnMonsterPriority[m] = Players[MyPlayerId].position.tile.ManhattanDistance(monster.position.tile);
if (monster._msquelch == 0) {
sgnMonsterPriority[m] += 0x1000;
7 years ago
} else if (sgwLRU[m] != 0) {
sgwLRU[m]--;
8 years ago
}
}
}
void SyncMonsterPos(TSyncMonster *p, int ndx)
5 years ago
{
auto &monster = Monsters[ndx];
5 years ago
p->_mndx = ndx;
p->_mx = monster.position.tile.x;
p->_my = monster.position.tile.y;
p->_menemy = encode_enemy(monster);
p->_mdelta = sgnMonsterPriority[ndx] > 255 ? 255 : sgnMonsterPriority[ndx];
5 years ago
sgnMonsterPriority[ndx] = 0xFFFF;
sgwLRU[ndx] = monster._msquelch == 0 ? 0xFFFF : 0xFFFE;
5 years ago
}
bool SyncMonsterActive(TSyncMonster *p)
8 years ago
{
int ndx = -1;
uint32_t lru = 0xFFFFFFFF;
for (int i = 0; i < ActiveMonsterCount; i++) {
int m = ActiveMonsters[i];
if (sgnMonsterPriority[m] < lru && sgwLRU[m] < 0xFFFE) {
lru = sgnMonsterPriority[m];
ndx = ActiveMonsters[i];
8 years ago
}
}
7 years ago
if (ndx == -1) {
return false;
}
SyncMonsterPos(p, ndx);
return true;
8 years ago
}
bool SyncMonsterActive2(TSyncMonster *p)
8 years ago
{
int ndx = -1;
uint32_t lru = 0xFFFE;
for (int i = 0; i < ActiveMonsterCount; i++) {
if (sgnMonsters >= ActiveMonsterCount) {
sgnMonsters = 0;
}
int m = ActiveMonsters[sgnMonsters];
7 years ago
if (sgwLRU[m] < lru) {
lru = sgwLRU[m];
ndx = ActiveMonsters[sgnMonsters];
8 years ago
}
sgnMonsters++;
}
7 years ago
if (ndx == -1) {
return false;
}
SyncMonsterPos(p, ndx);
return true;
8 years ago
}
void SyncPlrInv(TSyncHeader *pHdr)
8 years ago
{
pHdr->bItemI = -1;
if (ActiveItemCount > 0) {
if (sgnSyncItem >= ActiveItemCount) {
sgnSyncItem = 0;
}
pHdr->bItemI = ActiveItems[sgnSyncItem];
sgnSyncItem++;
auto &item = Items[pHdr->bItemI];
pHdr->bItemX = item.position.x;
pHdr->bItemY = item.position.y;
pHdr->wItemIndx = item.IDidx;
if (item.IDidx == IDI_EAR) {
pHdr->wItemCI = (item._iName[7] << 8) | item._iName[8];
pHdr->dwItemSeed = (item._iName[9] << 24) | (item._iName[10] << 16) | (item._iName[11] << 8) | item._iName[12];
pHdr->bItemId = item._iName[13];
pHdr->bItemDur = item._iName[14];
pHdr->bItemMDur = item._iName[15];
pHdr->bItemCh = item._iName[16];
pHdr->bItemMCh = item._iName[17];
pHdr->wItemVal = (item._iName[18] << 8) | ((item._iCurs - ICURS_EAR_SORCERER) << 6) | item._ivalue;
pHdr->dwItemBuff = (item._iName[19] << 24) | (item._iName[20] << 16) | (item._iName[21] << 8) | item._iName[22];
} else {
pHdr->wItemCI = item._iCreateInfo;
pHdr->dwItemSeed = item._iSeed;
pHdr->bItemId = item._iIdentified ? 1 : 0;
pHdr->bItemDur = item._iDurability;
pHdr->bItemMDur = item._iMaxDur;
pHdr->bItemCh = item._iCharges;
pHdr->bItemMCh = item._iMaxCharges;
if (item.IDidx == IDI_GOLD) {
pHdr->wItemVal = item._ivalue;
}
8 years ago
}
}
pHdr->bPInvLoc = -1;
assert(sgnSyncPInv > -1 && sgnSyncPInv < NUM_INVLOC);
const auto &item = Players[MyPlayerId].InvBody[sgnSyncPInv];
if (!item.isEmpty()) {
pHdr->bPInvLoc = sgnSyncPInv;
pHdr->wPInvIndx = item.IDidx;
pHdr->wPInvCI = item._iCreateInfo;
pHdr->dwPInvSeed = item._iSeed;
pHdr->bPInvId = item._iIdentified ? 1 : 0;
8 years ago
}
sgnSyncPInv++;
7 years ago
if (sgnSyncPInv >= NUM_INVLOC) {
8 years ago
sgnSyncPInv = 0;
}
8 years ago
}
5 years ago
void SyncMonster(int pnum, const TSyncMonster *p)
8 years ago
{
int ndx = p->_mndx;
auto &monster = Monsters[ndx];
if (monster._mhitpoints <= 0) {
return;
}
uint32_t delta = Players[MyPlayerId].position.tile.ManhattanDistance(monster.position.tile);
7 years ago
if (delta > 255) {
delta = 255;
}
if (delta < p->_mdelta || (delta == p->_mdelta && pnum > MyPlayerId)) {
return;
}
if (monster.position.future.x == p->_mx && monster.position.future.y == p->_my) {
return;
}
if (monster._mmode == MM_CHARGE || monster._mmode == MM_STONE) {
return;
}
if (monster.position.tile.WalkingDistance({ p->_mx, p->_my }) <= 2) {
if (monster._mmode < MM_WALK || monster._mmode > MM_WALK3) {
Direction md = GetDirection(monster.position.tile, { p->_mx, p->_my });
7 years ago
if (DirOK(ndx, md)) {
M_ClearSquares(ndx);
dMonster[monster.position.tile.x][monster.position.tile.y] = ndx + 1;
M_WalkDir(ndx, md);
monster._msquelch = UINT8_MAX;
8 years ago
}
}
7 years ago
} else if (dMonster[p->_mx][p->_my] == 0) {
M_ClearSquares(ndx);
dMonster[p->_mx][p->_my] = ndx + 1;
monster.position.tile = { p->_mx, p->_my };
decode_enemy(monster, p->_menemy);
Direction md = GetDirection({ p->_mx, p->_my }, monster.enemyPosition);
M_StartStand(monster, md);
monster._msquelch = UINT8_MAX;
8 years ago
}
decode_enemy(monster, p->_menemy);
8 years ago
}
} // namespace
uint32_t sync_all_monsters(const byte *pbBuf, uint32_t dwMaxLen)
{
if (ActiveMonsterCount < 1) {
return dwMaxLen;
}
if (dwMaxLen < sizeof(TSyncHeader) + sizeof(TSyncMonster)) {
return dwMaxLen;
}
auto *pHdr = (TSyncHeader *)pbBuf;
pbBuf += sizeof(TSyncHeader);
dwMaxLen -= sizeof(TSyncHeader);
pHdr->bCmd = CMD_SYNCDATA;
pHdr->bLevel = currlevel;
pHdr->wLen = 0;
SyncPlrInv(pHdr);
assert(dwMaxLen <= 0xffff);
SyncOneMonster();
for (int i = 0; i < ActiveMonsterCount && dwMaxLen >= sizeof(TSyncMonster); i++) {
bool sync = false;
if (i < 2) {
sync = SyncMonsterActive2((TSyncMonster *)pbBuf);
}
if (!sync) {
sync = SyncMonsterActive((TSyncMonster *)pbBuf);
}
if (!sync) {
break;
}
pbBuf += sizeof(TSyncMonster);
pHdr->wLen += sizeof(TSyncMonster);
dwMaxLen -= sizeof(TSyncMonster);
}
return dwMaxLen;
}
uint32_t sync_update(int pnum, const byte *pbBuf)
5 years ago
{
uint16_t wLen;
5 years ago
auto *pHdr = (TSyncHeader *)pbBuf;
5 years ago
pbBuf += sizeof(*pHdr);
if (pHdr->bCmd != CMD_SYNCDATA) {
app_fatal("bad sync command");
}
assert(gbBufferMsgs != 2);
5 years ago
if (gbBufferMsgs == 1) {
return pHdr->wLen + sizeof(*pHdr);
}
if (pnum == MyPlayerId) {
5 years ago
return pHdr->wLen + sizeof(*pHdr);
}
for (wLen = pHdr->wLen; wLen >= sizeof(TSyncMonster); wLen -= sizeof(TSyncMonster)) {
if (currlevel == pHdr->bLevel) {
SyncMonster(pnum, (TSyncMonster *)pbBuf);
5 years ago
}
delta_sync_monster((TSyncMonster *)pbBuf, pHdr->bLevel);
pbBuf += sizeof(TSyncMonster);
}
assert(wLen == 0);
return pHdr->wLen + sizeof(*pHdr);
}
void sync_init()
8 years ago
{
sgnMonsters = 16 * MyPlayerId;
memset(sgwLRU, 255, sizeof(sgwLRU));
8 years ago
}
} // namespace devilution