Skip to content

Commit

Permalink
Decouple Golems from Players and use SpawnMonster for Golem
Browse files Browse the repository at this point in the history
  • Loading branch information
obligaron committed Jan 22, 2025
1 parent 9dca30f commit 7062fc5
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 119 deletions.
5 changes: 2 additions & 3 deletions Source/missiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2343,8 +2343,7 @@ void AddGolem(Missile &missile, AddMissileParameter &parameter)

// Is Golem alive?
if (golem != nullptr) {
if (&player == MyPlayer)
KillGolem(*golem);
KillGolem(*golem);
return;
}

Expand All @@ -2357,7 +2356,7 @@ void AddGolem(Missile &missile, AddMissileParameter &parameter)
if (!spawnPosition)
return;

SpawnGolem(player, *golem, *spawnPosition, missile);
SpawnGolem(player, *spawnPosition, missile);
}

void AddApocalypseBoom(Missile &missile, AddMissileParameter &parameter)
Expand Down
98 changes: 68 additions & 30 deletions Source/monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ void InitMonster(Monster &monster, Direction rd, size_t typeIndex, Point positio
monster.goalVar2 = 0;
monster.goalVar3 = 0;
monster.pathCount = 0;
monster.enemy = 0;
monster.isInvalid = false;
monster.uniqueType = UniqueMonsterType::None;
monster.activeForTicks = 0;
Expand Down Expand Up @@ -3132,6 +3133,20 @@ void EnsureMonsterIndexIsActive(size_t monsterId)
}
}

void InitGolem(devilution::Monster &monster, uint8_t golemOwnerPlayerId, int16_t golemSpellLevel)
{
monster.flags |= MFLAG_GOLEM;
monster.goalVar3 = static_cast<int8_t>(golemOwnerPlayerId);
const Player &player = Players[golemOwnerPlayerId];
monster.maxHitPoints = 2 * (320 * golemSpellLevel + player._pMaxMana / 3);
monster.hitPoints = monster.maxHitPoints;
monster.armorClass = 25;
monster.golemToHit = 5 * (golemSpellLevel + 8) + 2 * player.getCharacterLevel();
monster.minDamage = 2 * (golemSpellLevel + 4);
monster.maxDamage = 2 * (golemSpellLevel + 8);
UpdateEnemy(monster);
}

} // namespace

tl::expected<size_t, std::string> AddMonsterType(_monster_id type, placeflag placeflag)
Expand Down Expand Up @@ -3640,21 +3655,24 @@ void SpawnMonster(Point position, Direction dir, size_t typeIndex, bool startSpe
ActiveMonsterCount += 1;
uint32_t seed = GetLCGEngineState();
// Update local state immediately to increase ActiveMonsterCount instantly (this allows multiple monsters to be spawned in one game tick)
InitializeSpawnedMonster(position, dir, typeIndex, monsterIndex, seed);
NetSendCmdSpawnMonster(position, dir, static_cast<uint16_t>(typeIndex), static_cast<uint16_t>(monsterIndex), seed);
InitializeSpawnedMonster(position, dir, typeIndex, monsterIndex, seed, 0, 0);
NetSendCmdSpawnMonster(position, dir, static_cast<uint16_t>(typeIndex), static_cast<uint16_t>(monsterIndex), seed, 0, 0);
}

void LoadDeltaSpawnedMonster(size_t typeIndex, size_t monsterId, uint32_t seed)
void LoadDeltaSpawnedMonster(size_t typeIndex, size_t monsterId, uint32_t seed, uint8_t golemOwnerPlayerId, int16_t golemSpellLevel)
{
SetRndSeed(seed);
EnsureMonsterIndexIsActive(monsterId);
WorldTilePosition position = GolemHoldingCell;
Monster &monster = Monsters[monsterId];
M_ClearSquares(monster);
InitMonster(monster, Direction::South, typeIndex, position);
if (monster.type().type == MT_GOLEM) {
InitGolem(monster, golemOwnerPlayerId, golemSpellLevel);
}
}

void InitializeSpawnedMonster(Point position, Direction dir, size_t typeIndex, size_t monsterId, uint32_t seed)
void InitializeSpawnedMonster(Point position, Direction dir, size_t typeIndex, size_t monsterId, uint32_t seed, uint8_t golemOwnerPlayerId, int16_t golemSpellLevel)
{
SetRndSeed(seed);
EnsureMonsterIndexIsActive(monsterId);
Expand All @@ -3677,10 +3695,14 @@ void InitializeSpawnedMonster(Point position, Direction dir, size_t typeIndex, s
monster.occupyTile(position, false);
InitMonster(monster, dir, typeIndex, position);

if (IsSkel(monster.type().type))
if (monster.type().type == MT_GOLEM) {
InitGolem(monster, golemOwnerPlayerId, golemSpellLevel);
StartSpecialStand(monster, dir);
else
} else if (IsSkel(monster.type().type)) {
StartSpecialStand(monster, dir);
} else {
M_StartStand(monster, dir);
}
}

void AddDoppelganger(Monster &monster)
Expand Down Expand Up @@ -4656,32 +4678,48 @@ void TalktoMonster(Player &player, Monster &monster)
}
}

void SpawnGolem(Player &player, Monster &golem, Point position, Missile &missile)
void SpawnGolem(Player &player, Point position, Missile &missile)
{
golem.occupyTile(position, false);
golem.position.tile = position;
golem.position.future = position;
golem.position.old = position;
golem.pathCount = 0;
golem.maxHitPoints = 2 * (320 * missile._mispllvl + player._pMaxMana / 3);
golem.hitPoints = golem.maxHitPoints;
golem.armorClass = 25;
golem.golemToHit = 5 * (missile._mispllvl + 8) + 2 * player.getCharacterLevel();
golem.minDamage = 2 * (missile._mispllvl + 4);
golem.maxDamage = 2 * (missile._mispllvl + 8);
golem.flags |= MFLAG_GOLEM;
golem.goalVar3 = player.getId();
StartSpecialStand(golem, Direction::South);
UpdateEnemy(golem);
if (&player == MyPlayer) {
NetSendCmdGolem(
golem.position.tile.x,
golem.position.tile.y,
golem.direction,
golem.enemy,
golem.hitPoints,
GetLevelForMultiplayer(player));
// The command is only executed for the level owner, to prevent desyncs in multiplayer.
if (!MyPlayer->isLevelOwnedByLocalClient())
return;

// Search monster index to use for the new golem
Monster *golem = nullptr;
// 1. Prefer MonsterIndex = PlayerIndex for vanilla compatibility
if (player.getId() < ReservedMonsterSlotsForGolems) {
Monster &reservedGolem = Monsters[player.getId()];
if (reservedGolem.position.tile == GolemHoldingCell || reservedGolem.hitPoints == 0)
golem = &reservedGolem;
}
// 2. Use reserved slots, so additional Monsters can spawn
if (golem == nullptr) {
for (int i = 0; i < ReservedMonsterSlotsForGolems; i++) {
Monster &reservedGolem = Monsters[player.getId()];
if (reservedGolem.position.tile == GolemHoldingCell || reservedGolem.hitPoints == 0) {
golem = &reservedGolem;
break;
}
}
}
// 3. Use normal monster slot
if (golem == nullptr) {
if (ActiveMonsterCount >= MaxMonsters)
return;
size_t monsterIndex = ActiveMonsters[ActiveMonsterCount];
ActiveMonsterCount += 1;
golem = &Monsters[monsterIndex];
}

if (golem == nullptr)
return;

size_t monsterIndex = golem->getId();
uint32_t seed = GetLCGEngineState();

// Update local state immediately to increase ActiveMonsterCount instantly (this allows multiple monsters to be spawned in one game tick)
InitializeSpawnedMonster(position, Direction::South, 0, monsterIndex, seed, player.getId(), missile._mispllvl);
NetSendCmdSpawnMonster(position, Direction::South, 0, static_cast<uint16_t>(monsterIndex), seed, player.getId(), static_cast<uint8_t>(missile._mispllvl));
}

bool CanTalkToMonst(const Monster &monster)
Expand Down
6 changes: 3 additions & 3 deletions Source/monster.h
Original file line number Diff line number Diff line change
Expand Up @@ -514,11 +514,11 @@ void SpawnMonster(Point position, Direction dir, size_t typeIndex, bool startSpe
/**
* @brief Loads data for a dynamically spawned monster when entering a level in multiplayer.
*/
void LoadDeltaSpawnedMonster(size_t typeIndex, size_t monsterId, uint32_t seed);
void LoadDeltaSpawnedMonster(size_t typeIndex, size_t monsterId, uint32_t seed, uint8_t golemOwnerPlayerId, int16_t golemSpellLevel);
/**
* @brief Initialize a spanwed monster (from a network message or from SpawnMonster-function).
*/
void InitializeSpawnedMonster(Point position, Direction dir, size_t typeIndex, size_t monsterId, uint32_t seed);
void InitializeSpawnedMonster(Point position, Direction dir, size_t typeIndex, size_t monsterId, uint32_t seed, uint8_t golemOwnerPlayerId, int16_t golemSpellLevel);
void AddDoppelganger(Monster &monster);
void ApplyMonsterDamage(DamageType damageType, Monster &monster, int damage);
bool M_Talker(const Monster &monster);
Expand Down Expand Up @@ -569,7 +569,7 @@ bool IsGoat(_monster_id mt);
void ActivateSkeleton(Monster &monster, Point position);
Monster *PreSpawnSkeleton();
void TalktoMonster(Player &player, Monster &monster);
void SpawnGolem(Player &player, Monster &golem, Point position, Missile &missile);
void SpawnGolem(Player &player, Point position, Missile &missile);
bool CanTalkToMonst(const Monster &monster);
uint8_t encode_enemy(Monster &monster);
void decode_enemy(Monster &monster, uint8_t enemyId);
Expand Down
72 changes: 14 additions & 58 deletions Source/msg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ struct DObjectStr {
struct DSpawnedMonster {
size_t typeIndex;
uint32_t seed;
uint8_t golemOwnerPlayerId;
int16_t golemSpellLevel;
};

struct DLevel {
Expand Down Expand Up @@ -733,19 +735,6 @@ size_t OnLevelData(uint8_t pnum, const TCmd *pCmd)
return wBytes + sizeof(message);
}

void DeltaSyncGolem(const TCmdGolem &message, const Player &player, uint8_t level)
{
if (!gbIsMultiplayer)
return;

DMonsterStr &monster = GetDeltaLevel(level).monster[player.getId()];
monster.position.x = message._mx;
monster.position.y = message._my;
monster._mactive = UINT8_MAX;
monster._menemy = message._menemy;
monster.hitPoints = SDL_SwapLE32(message._mhitpoints);
}

void DeltaLeaveSync(uint8_t bLevel)
{
if (!gbIsMultiplayer)
Expand Down Expand Up @@ -1770,31 +1759,6 @@ size_t OnMonstDeath(const TCmd *pCmd, Player &player)
return sizeof(message);
}

size_t OnAwakeGolem(const TCmd *pCmd, Player &player)
{
const auto &message = *reinterpret_cast<const TCmdGolem *>(pCmd);
const Point position { message._mx, message._my };

if (gbBufferMsgs == 1) {
SendPacket(player, &message, sizeof(message));
} else if (InDungeonBounds(position)) {
if (!player.isOnActiveLevel()) {
DeltaSyncGolem(message, player, message._currlevel);
} else if (&player != MyPlayer) {
// Check if this player already has an active golem
for (auto &missile : Missiles) {
if (missile._mitype == MissileID::Golem && &Players[missile._misource] == &player) {
return sizeof(message);
}
}

AddMissile(player.position.tile, position, message._mdir, MissileID::Golem, TARGET_MONSTERS, player, 0, 1);
}
}

return sizeof(message);
}

size_t OnMonstDamage(const TCmd *pCmd, Player &player)
{
const auto &message = *reinterpret_cast<const TCmdMonDamage *>(pCmd);
Expand Down Expand Up @@ -2370,10 +2334,15 @@ size_t OnSpawnMonster(const TCmd *pCmd, const Player &player)

size_t typeIndex = static_cast<size_t>(SDL_SwapLE16(message.typeIndex));
size_t monsterId = static_cast<size_t>(SDL_SwapLE16(message.monsterId));
uint8_t golemOwnerPlayerId = message.golemOwnerPlayerId;
if (golemOwnerPlayerId >= Players.size()) {
return sizeof(message);
}
uint8_t golemSpellLevel = std::min(message.golemSpellLevel, static_cast<uint8_t>(MaxSpellLevel + Players[golemOwnerPlayerId]._pISplLvlAdd));

DLevel &deltaLevel = GetDeltaLevel(player);

deltaLevel.spawnedMonsters[monsterId] = { typeIndex, message.seed };
deltaLevel.spawnedMonsters[monsterId] = { typeIndex, message.seed, golemOwnerPlayerId, golemSpellLevel };
// Override old monster delta information
auto &deltaMonster = deltaLevel.monster[monsterId];
deltaMonster.position = position;
Expand All @@ -2382,7 +2351,7 @@ size_t OnSpawnMonster(const TCmd *pCmd, const Player &player)
deltaMonster._mactive = 0;

if (player.isOnActiveLevel() && &player != MyPlayer)
InitializeSpawnedMonster(position, message.dir, typeIndex, monsterId, message.seed);
InitializeSpawnedMonster(position, message.dir, typeIndex, monsterId, message.seed, golemOwnerPlayerId, golemSpellLevel);
return sizeof(message);
}

Expand Down Expand Up @@ -2678,7 +2647,8 @@ void DeltaLoadLevel()
DLevel &deltaLevel = GetDeltaLevel(localLevel);
if (leveltype != DTYPE_TOWN) {
for (auto &deltaSpawnedMonster : deltaLevel.spawnedMonsters) {
LoadDeltaSpawnedMonster(deltaSpawnedMonster.second.typeIndex, deltaSpawnedMonster.first, deltaSpawnedMonster.second.seed);
auto &monsterData = deltaSpawnedMonster.second;
LoadDeltaSpawnedMonster(deltaSpawnedMonster.second.typeIndex, deltaSpawnedMonster.first, monsterData.seed, monsterData.golemOwnerPlayerId, monsterData.golemSpellLevel);
assert(deltaLevel.monster[deltaSpawnedMonster.first].position.x != 0xFF);
}
for (size_t i = 0; i < MaxMonsters; i++) {
Expand Down Expand Up @@ -2807,21 +2777,7 @@ void NetSendCmd(bool bHiPri, _cmd_id bCmd)
NetSendLoPri(MyPlayerId, (std::byte *)&cmd, sizeof(cmd));
}

void NetSendCmdGolem(uint8_t mx, uint8_t my, Direction dir, uint8_t menemy, int hp, uint8_t cl)
{
TCmdGolem cmd;

cmd.bCmd = CMD_AWAKEGOLEM;
cmd._mx = mx;
cmd._my = my;
cmd._mdir = dir;
cmd._menemy = menemy;
cmd._mhitpoints = hp;
cmd._currlevel = cl;
NetSendLoPri(MyPlayerId, (std::byte *)&cmd, sizeof(cmd));
}

void NetSendCmdSpawnMonster(Point position, Direction dir, uint16_t typeIndex, uint16_t monsterId, uint32_t seed)
void NetSendCmdSpawnMonster(Point position, Direction dir, uint16_t typeIndex, uint16_t monsterId, uint32_t seed, uint8_t golemOwnerPlayerId, uint8_t golemSpellLevel)
{
TCmdSpawnMonster cmd;

Expand All @@ -2832,6 +2788,8 @@ void NetSendCmdSpawnMonster(Point position, Direction dir, uint16_t typeIndex, u
cmd.typeIndex = SDL_SwapLE16(typeIndex);
cmd.monsterId = SDL_SwapLE16(monsterId);
cmd.seed = SDL_SwapLE32(seed);
cmd.golemOwnerPlayerId = golemOwnerPlayerId;
cmd.golemSpellLevel = golemSpellLevel;
NetSendHiPri(MyPlayerId, (std::byte *)&cmd, sizeof(cmd));
}

Expand Down Expand Up @@ -3246,8 +3204,6 @@ size_t ParseCmd(uint8_t pnum, const TCmd *pCmd)
return OnWarp(pCmd, player);
case CMD_MONSTDEATH:
return OnMonstDeath(pCmd, player);
case CMD_AWAKEGOLEM:
return OnAwakeGolem(pCmd, player);
case CMD_MONSTDAMAGE:
return OnMonstDamage(pCmd, player);
case CMD_PLRDEAD:
Expand Down
19 changes: 3 additions & 16 deletions Source/msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,6 @@ enum _cmd_id : uint8_t {
//
// body (TCmdQuest)
CMD_SYNCQUEST,
// Spawn golem at target location.
//
// body (TCmdGolem)
CMD_AWAKEGOLEM,
// Enable mana shield of player (render).
//
// body (TCmd)
Expand Down Expand Up @@ -500,16 +496,6 @@ struct TCmdParam4 {
uint16_t wParam4;
};

struct TCmdGolem {
_cmd_id bCmd;
uint8_t _mx;
uint8_t _my;
Direction _mdir;
int8_t _menemy;
int32_t _mhitpoints;
uint8_t _currlevel;
};

struct TCmdSpawnMonster {
_cmd_id bCmd;
uint8_t x;
Expand All @@ -518,6 +504,8 @@ struct TCmdSpawnMonster {
uint16_t typeIndex;
uint16_t monsterId;
uint32_t seed;
uint8_t golemOwnerPlayerId;
uint8_t golemSpellLevel;
};

struct TCmdQuest {
Expand Down Expand Up @@ -743,8 +731,7 @@ void DeltaLoadLevel();
/** @brief Clears last sent player command for the local player. This is used when a game tick changes. */
void ClearLastSentPlayerCmd();
void NetSendCmd(bool bHiPri, _cmd_id bCmd);
void NetSendCmdGolem(uint8_t mx, uint8_t my, Direction dir, uint8_t menemy, int hp, uint8_t cl);
void NetSendCmdSpawnMonster(Point position, Direction dir, uint16_t typeIndex, uint16_t monsterId, uint32_t seed);
void NetSendCmdSpawnMonster(Point position, Direction dir, uint16_t typeIndex, uint16_t monsterId, uint32_t seed, uint8_t golemOwnerPlayerId, uint8_t golemSpellLevel);
void NetSendCmdLoc(uint8_t playerId, bool bHiPri, _cmd_id bCmd, Point position);
void NetSendCmdLocParam1(bool bHiPri, _cmd_id bCmd, Point position, uint16_t wParam1);
void NetSendCmdLocParam2(bool bHiPri, _cmd_id bCmd, Point position, uint16_t wParam1, uint16_t wParam2);
Expand Down
12 changes: 3 additions & 9 deletions Source/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2809,16 +2809,10 @@ void SyncPlrKill(Player &player, DeathReason deathReason)

void RemovePlrMissiles(const Player &player)
{
if (leveltype != DTYPE_TOWN && &player == MyPlayer) {
Monster *golem = FindGolemForPlayer(player);
if (golem != nullptr) {
if (leveltype != DTYPE_TOWN) {
Monster *golem;
while ((golem = FindGolemForPlayer(player)) != nullptr) {
KillGolem(*golem);
AddCorpse(golem->position.tile, golem->type().corpseId, golem->direction);
int mx = golem->position.tile.x;
int my = golem->position.tile.y;
dMonster[mx][my] = 0;
golem->isInvalid = true;
DeleteMonsterList();
}
}

Expand Down

0 comments on commit 7062fc5

Please sign in to comment.