Skip to content

Commit

Permalink
Some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
cvet committed Apr 22, 2024
1 parent 6e51c41 commit 4548781
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 30 deletions.
6 changes: 1 addition & 5 deletions Source/Common/EntityProperties.h
Expand Up @@ -273,9 +273,7 @@ class CritterProperties : public EntityProperties
ENTITY_PROPERTY(Protected, uint16, WorldX);
///@ ExportProperty
ENTITY_PROPERTY(Protected, uint16, WorldY);
///@ ExportProperty ReadOnly
ENTITY_PROPERTY(Protected, ident_t, GlobalMapLeaderId);
///@ ExportProperty ReadOnly
///@ ExportProperty ReadOnly Temporary
ENTITY_PROPERTY(PrivateServer, uint, GlobalMapTripId);
///@ ExportProperty ReadOnly
ENTITY_PROPERTY(PrivateServer, ident_t, LastMapId);
Expand All @@ -286,8 +284,6 @@ class CritterProperties : public EntityProperties
///@ ExportProperty ReadOnly
ENTITY_PROPERTY(PrivateServer, hstring, LastLocationPid);
///@ ExportProperty ReadOnly
ENTITY_PROPERTY(PrivateServer, ident_t, LastGlobalMapLeaderId);
///@ ExportProperty ReadOnly
ENTITY_PROPERTY(PrivateServer, uint16, MapLeaveHexX);
///@ ExportProperty ReadOnly
ENTITY_PROPERTY(PrivateServer, uint16, MapLeaveHexY);
Expand Down
19 changes: 8 additions & 11 deletions Source/Scripting/ClientGlobalScriptMethods.cpp
Expand Up @@ -145,20 +145,17 @@
}

const auto* hex_cr1 = dynamic_cast<CritterHexView*>(cr1);
if (hex_cr1 == nullptr) {
throw ScriptException("Critter 1 is not on map");
}

const auto* hex_cr2 = dynamic_cast<CritterHexView*>(cr2);
if (hex_cr2 == nullptr) {
throw ScriptException("Critter 2 is not on map");
}

if (hex_cr1->GetMap()->GetId() != hex_cr2->GetMap()->GetId()) {
throw ScriptException("Critters different maps");
if (hex_cr1 != nullptr && hex_cr2 != nullptr) {
return GeometryHelper::DistGame(hex_cr1->GetHexX(), hex_cr1->GetHexY(), hex_cr2->GetHexX(), hex_cr2->GetHexY());
}
else if (hex_cr1 == nullptr && hex_cr2 == nullptr) {
return 0;
}
else {
throw ScriptException("Critters unknown positions");
}

return GeometryHelper::DistGame(hex_cr1->GetHexX(), hex_cr1->GetHexY(), hex_cr2->GetHexX(), hex_cr2->GetHexY());
}

///# ...
Expand Down
16 changes: 13 additions & 3 deletions Source/Scripting/ServerGlobalScriptMethods.cpp
Expand Up @@ -179,11 +179,21 @@
throw ScriptException("Critter2 arg is null");
}
if (cr1->GetMapId() != cr2->GetMapId()) {
throw ScriptException("Differernt maps");
throw ScriptException("Critters different maps");
}

const auto dist = GeometryHelper::DistGame(cr1->GetHexX(), cr1->GetHexY(), cr2->GetHexX(), cr2->GetHexY());
return static_cast<int>(dist);
if (!cr1->GetMapId()) {
if (cr1->GlobalMapGroup != cr2->GlobalMapGroup) {
throw ScriptException("Critters different group on global map");
}

return 0;
}
else {
const auto dist = GeometryHelper::DistGame(cr1->GetHexX(), cr1->GetHexY(), cr2->GetHexX(), cr2->GetHexY());

return dist;
}
}

///# ...
Expand Down
11 changes: 0 additions & 11 deletions Source/Server/MapManager.cpp
Expand Up @@ -1568,11 +1568,8 @@ void MapManager::AddCrToMap(Critter* cr, Map* map, uint16 hx, uint16 hy, uint8 d
const auto* global_cr = global_cr_id && global_cr_id != cr->GetId() ? _engine->CrMngr.GetCritter(global_cr_id) : nullptr;

if (global_cr == nullptr || global_cr->GetMapId()) {
cr->SetGlobalMapLeaderId(cr->GetId());
cr->SetGlobalMapTripId(cr->GetGlobalMapTripId() + 1);

cr->SetLastGlobalMapLeaderId(cr->GetId());

cr->GlobalMapGroup = new vector<Critter*>();
cr->GlobalMapGroup->push_back(cr);
}
Expand All @@ -1581,11 +1578,8 @@ void MapManager::AddCrToMap(Critter* cr, Map* map, uint16 hx, uint16 hy, uint8 d

cr->SetWorldX(global_cr->GetWorldX());
cr->SetWorldY(global_cr->GetWorldY());
cr->SetGlobalMapLeaderId(global_cr_id);
cr->SetGlobalMapTripId(global_cr->GetGlobalMapTripId());

cr->SetLastGlobalMapLeaderId(global_cr_id);

for (auto* group_cr : *global_cr->GlobalMapGroup) {
group_cr->Send_AddCritter(cr);
}
Expand Down Expand Up @@ -1640,10 +1634,7 @@ void MapManager::EraseCrFromMap(Critter* cr, Map* map)
cr->GlobalMapGroup->erase(it);

if (!cr->GlobalMapGroup->empty()) {
const auto* new_leader = *cr->GlobalMapGroup->begin();
for (auto* group_cr : *cr->GlobalMapGroup) {
group_cr->SetGlobalMapLeaderId(new_leader->GetId());
group_cr->SetLastGlobalMapLeaderId(new_leader->GetId());
group_cr->Send_RemoveCritter(cr);
}
}
Expand All @@ -1652,8 +1643,6 @@ void MapManager::EraseCrFromMap(Critter* cr, Map* map)
}

cr->GlobalMapGroup = nullptr;
cr->SetGlobalMapLeaderId(ident_t {});
cr->SetLastGlobalMapLeaderId(ident_t {});
}
}

Expand Down

0 comments on commit 4548781

Please sign in to comment.