Skip to content

Commit

Permalink
Fix for client global map critters appearing
Browse files Browse the repository at this point in the history
  • Loading branch information
cvet committed Mar 26, 2024
1 parent ccf9109 commit 34215ff
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 64 deletions.
19 changes: 11 additions & 8 deletions Source/Client/Client.cpp
Expand Up @@ -1110,6 +1110,14 @@ void FOClient::Net_OnAddCritter()
const auto* proto = ProtoMngr.GetProtoCritter(pid);
RUNTIME_ASSERT(proto);

const auto it = std::find_if(_worldmapCritters.begin(), _worldmapCritters.end(), [cr_id](const auto* cr2) { return cr2->GetId() == cr_id; });
if (it != _worldmapCritters.end()) {
BreakIntoDebugger();
(*it)->MarkAsDestroyed();
(*it)->Release();
_worldmapCritters.erase(it);
}

cr = new CritterView(this, cr_id, proto);
cr->RestoreData(_tempPropertiesData);
_worldmapCritters.emplace_back(cr);
Expand Down Expand Up @@ -1891,6 +1899,7 @@ void FOClient::Net_OnCritterAttachments()
if (CurMap != nullptr) {
auto* cr = CurMap->GetCritter(cr_id);
if (cr == nullptr) {
BreakIntoDebugger();
return;
}

Expand Down Expand Up @@ -1918,10 +1927,12 @@ void FOClient::Net_OnCritterAttachments()
else {
auto* cr = GetWorldmapCritter(cr_id);
if (cr == nullptr) {
BreakIntoDebugger();
return;
}

cr->SetIsAttached(is_attached);
cr->SetAttachMaster(attach_master);
cr->AttachedCritters = std::move(attached_critters);
}
}
Expand Down Expand Up @@ -2625,14 +2636,6 @@ void FOClient::Net_OnGlobalInfo()
_worldmapFog.Set2Bit(zx, zy, fog);
}

if (IsBitSet(info_flags, GM_INFO_CRITTERS)) {
for (auto* cr : _worldmapCritters) {
cr->MarkAsDestroyed();
cr->Release();
}
_worldmapCritters.clear();
}

CHECK_SERVER_IN_BUF_ERROR(_conn);
}

Expand Down
1 change: 0 additions & 1 deletion Source/Client/MapView.cpp
Expand Up @@ -1420,7 +1420,6 @@ void MapView::RebuildMapOffset(int ox, int oy)
// Items on hex
if (!field.Items.empty()) {
for (auto* item : field.Items) {

if (!_mapperMode) {
if (item->GetAlwaysHideSprite()) {
continue;
Expand Down
2 changes: 0 additions & 2 deletions Source/Common/Common.h
Expand Up @@ -1492,9 +1492,7 @@ static constexpr uint8 GM_FOG_NONE = 3;

// GM Info
static constexpr uint8 GM_INFO_LOCATIONS = 0x01;
static constexpr uint8 GM_INFO_CRITTERS = 0x02;
static constexpr uint8 GM_INFO_ZONES_FOG = 0x08;
static constexpr uint8 GM_INFO_ALL = 0x0F;
static constexpr uint8 GM_INFO_FOG = 0x10;
static constexpr uint8 GM_INFO_LOCATION = 0x20;

Expand Down
7 changes: 4 additions & 3 deletions Source/Frontend/Application.cpp
Expand Up @@ -1058,10 +1058,11 @@ void Application::RequestQuit()
{
STACK_TRACE_ENTRY();

WriteLog("Quit requested");
if (bool expected = false; _quit.compare_exchange_strong(expected, true)) {
WriteLog("Quit requested");

_quit = true;
_quitEvent.notify_all();
_quitEvent.notify_all();
}
}

void Application::WaitForRequestedQuit()
Expand Down
7 changes: 4 additions & 3 deletions Source/Frontend/ApplicationHeadless.cpp
Expand Up @@ -249,10 +249,11 @@ void Application::RequestQuit()
{
STACK_TRACE_ENTRY();

WriteLog("Quit requested");
if (bool expected = false; _quit.compare_exchange_strong(expected, true)) {
WriteLog("Quit requested");

_quit = true;
_quitEvent.notify_all();
_quitEvent.notify_all();
}
}

void Application::WaitForRequestedQuit()
Expand Down
4 changes: 2 additions & 2 deletions Source/Server/Critter.cpp
Expand Up @@ -1078,14 +1078,14 @@ void Critter::Send_EraseItem(const Item* item)
}
}

void Critter::Send_GlobalInfo(uint8 flags)
void Critter::Send_GlobalInfo()
{
STACK_TRACE_ENTRY();

NON_CONST_METHOD_HINT();

if (_player != nullptr) {
_player->Send_GlobalInfo(flags);
_player->Send_GlobalInfo();
}
}

Expand Down
2 changes: 1 addition & 1 deletion Source/Server/Critter.h
Expand Up @@ -158,7 +158,7 @@ class Critter final : public ServerEntity, public EntityWithProto, public Critte
void Send_AnimateItem(const Item* item, hstring anim_name, bool looped, bool reversed);
void Send_AddItem(const Item* item);
void Send_EraseItem(const Item* item);
void Send_GlobalInfo(uint8 flags);
void Send_GlobalInfo();
void Send_GlobalLocation(const Location* loc, bool add);
void Send_GlobalMapFog(uint16 zx, uint16 zy, uint8 fog);
void Send_Teleport(const Critter* cr, uint16 to_hx, uint16 to_hy);
Expand Down
36 changes: 14 additions & 22 deletions Source/Server/MapManager.cpp
Expand Up @@ -1394,8 +1394,6 @@ void MapManager::Transit(Critter* cr, Map* map, uint16 hx, uint16 hy, uint8 dir,
cr->LockMapTransfers++;
auto restore_transfers = ScopeCallback([cr]() noexcept { cr->LockMapTransfers--; });

const auto map_id = map != nullptr ? map->GetId() : ident_t {};

const auto prev_map_id = cr->GetMapId();
auto* prev_map = prev_map_id ? GetMap(prev_map_id) : nullptr;
RUNTIME_ASSERT(!prev_map_id || !!prev_map);
Expand Down Expand Up @@ -1486,10 +1484,18 @@ void MapManager::Transit(Critter* cr, Map* map, uint16 hx, uint16 hy, uint8 dir,
return;
}

cr->Send_LoadMap(nullptr);
cr->Send_LoadMap(map);
cr->Send_AddCritter(cr);
cr->Send_AddAllItems();

if (map == nullptr) {
for (const auto* group_cr : *cr->GlobalMapGroup) {
if (group_cr != cr) {
cr->Send_AddCritter(group_cr);
}
}
}

ProcessVisibleCritters(cr);
ProcessVisibleItems(cr);

Expand Down Expand Up @@ -1661,25 +1667,8 @@ void MapManager::ProcessVisibleCritters(Critter* cr)
return;
}

if (!cr->GetMapId()) {
// Global map
RUNTIME_ASSERT(cr->GlobalMapGroup);

if (cr->GetIsControlledByPlayer()) {
for (const auto* group_cr : *cr->GlobalMapGroup) {
if (cr == group_cr) {
cr->Send_AddCritter(cr);
cr->Send_AddAllItems();
}
else {
cr->Send_AddCritter(group_cr);
}
}
}
}
else {
// Local map
auto* map = GetMap(cr->GetMapId());
if (const auto map_id = cr->GetMapId()) {
auto* map = GetMap(map_id);
RUNTIME_ASSERT(map);

for (auto* target : copy_hold_ref(map->GetCritters())) {
Expand All @@ -1688,6 +1677,9 @@ void MapManager::ProcessVisibleCritters(Critter* cr)
ProcessCritterLook(map, target, cr, trace_result);
}
}
else {
RUNTIME_ASSERT(cr->GlobalMapGroup);
}
}

void MapManager::ProcessCritterLook(Map* map, Critter* cr, Critter* target, optional<bool>& trace_result)
Expand Down
15 changes: 4 additions & 11 deletions Source/Server/Player.cpp
Expand Up @@ -158,17 +158,11 @@ void Player::Send_LoadMap(const Map* map)

NON_CONST_METHOD_HINT();

RUNTIME_ASSERT(_controlledCr);

const Location* loc = nullptr;
hstring pid_map;
hstring pid_loc;
uint8 map_index_in_loc = 0;

if (map == nullptr) {
map = _engine->MapMngr.GetMap(_controlledCr->GetMapId());
}

if (map != nullptr) {
loc = map->GetLocation();
pid_map = map->GetProtoId();
Expand All @@ -180,6 +174,7 @@ void Player::Send_LoadMap(const Map* map)
vector<uint>* map_data_sizes = nullptr;
vector<const uint8*>* loc_data = nullptr;
vector<uint>* loc_data_sizes = nullptr;

if (map != nullptr) {
map->StoreData(false, &map_data, &map_data_sizes);
loc->StoreData(false, &loc_data, &loc_data_sizes);
Expand Down Expand Up @@ -528,14 +523,16 @@ void Player::Send_EraseItem(const Item* item)
CONNECTION_OUTPUT_END(Connection);
}

void Player::Send_GlobalInfo(uint8 info_flags)
void Player::Send_GlobalInfo()
{
STACK_TRACE_ENTRY();

NON_CONST_METHOD_HINT();

RUNTIME_ASSERT(_controlledCr);

constexpr uint8 info_flags = GM_INFO_LOCATIONS | GM_INFO_ZONES_FOG;

const auto known_locs = _controlledCr->GetKnownLocations();

auto loc_count = static_cast<uint16>(known_locs.size());
Expand Down Expand Up @@ -586,10 +583,6 @@ void Player::Send_GlobalInfo(uint8 info_flags)

Connection->OutBuf.EndMsg();
CONNECTION_OUTPUT_END(Connection);

if (IsBitSet(info_flags, GM_INFO_CRITTERS)) {
_engine->MapMngr.ProcessVisibleCritters(_controlledCr);
}
}

void Player::Send_GlobalLocation(const Location* loc, bool add)
Expand Down
2 changes: 1 addition & 1 deletion Source/Server/Player.h
Expand Up @@ -78,7 +78,7 @@ class Player final : public ServerEntity, public PlayerProperties
void Send_AnimateItem(const Item* item, hstring anim_name, bool looped, bool reversed);
void Send_AddItem(const Item* item);
void Send_EraseItem(const Item* item);
void Send_GlobalInfo(uint8 flags);
void Send_GlobalInfo();
void Send_GlobalLocation(const Location* loc, bool add);
void Send_GlobalMapFog(uint16 zx, uint16 zy, uint8 fog);
void Send_Teleport(const Critter* cr, uint16 to_hx, uint16 to_hy);
Expand Down
22 changes: 12 additions & 10 deletions Source/Server/Server.cpp
Expand Up @@ -2181,20 +2181,22 @@ void FOServer::SendCritterInitialInfo(Critter* cr, Critter* prev_cr)

cr->Broadcast_Action(CritterAction::Connect, 0, nullptr);

if (!cr->GetMapId()) {
cr->Send_AddCritter(cr);
cr->Send_AddAllItems();
cr->Send_AllAutomapsInfo();

if (map == nullptr) {
RUNTIME_ASSERT(cr->GlobalMapGroup);

cr->Send_GlobalInfo(GM_INFO_ALL);
cr->Send_AllAutomapsInfo();
for (const auto* group_cr : *cr->GlobalMapGroup) {
if (group_cr != cr) {
cr->Send_AddCritter(group_cr);
}
}

cr->Send_GlobalInfo();
}
else {
RUNTIME_ASSERT(map);

// Send chosen
cr->Send_AddCritter(cr);
cr->Send_AddAllItems();
cr->Send_AllAutomapsInfo();

// Send current critters
for (const auto* visible_cr : cr->VisCrSelf) {
if (same_map && prev_cr->VisCrSelfMap.count(visible_cr->GetId()) != 0) {
Expand Down

0 comments on commit 34215ff

Please sign in to comment.