1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-03-17 20:58:07 +02:00

Merge pull request #4679 from dydzio0614/map-objects-removal-2

"Backend" part of map objects removal via timed events
This commit is contained in:
Dydzio 2024-09-28 00:09:35 +02:00 committed by GitHub
commit 6a7f58c3d1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 89 additions and 1 deletions

View File

@ -210,6 +210,7 @@ void CGameState::init(const IMapService * mapService, StartInfo * si, Load::Prog
buildBonusSystemTree();
initVisitingAndGarrisonedHeroes();
initFogOfWar();
initTimedEventsRemovableObjects();
for(auto & elem : teams)
{
@ -951,6 +952,23 @@ void CGameState::initMapObjects()
map->calculateGuardingGreaturePositions(); //calculate once again when all the guards are placed and initialized
}
void CGameState::initTimedEventsRemovableObjects()
{
for(auto & timedEvent : map->events)
{
for(int3 coordinate : timedEvent.deletedObjectsCoordinates)
{
if(isInTheMap(coordinate))
{
for(const CGObjectInstance * object : getBlockingObjs(coordinate))
{
timedEvent.deletedObjectsInstances.push_back(object);
}
}
}
}
}
void CGameState::placeHeroesInTowns()
{
for(auto & player : players)

View File

@ -196,6 +196,7 @@ private:
void initTowns();
void initTownNames();
void initMapObjects();
void initTimedEventsRemovableObjects();
void initVisitingAndGarrisonedHeroes();
void initCampaign();

View File

@ -103,6 +103,25 @@ void CMapEvent::serializeJson(JsonSerializeFormat & handler)
handler.serializeInt("firstOccurrence", firstOccurrence);
handler.serializeInt("nextOccurrence", nextOccurrence);
resources.serializeJson(handler, "resources");
JsonNode deletedObjectsJson;
for (const auto & entry : deletedObjectsCoordinates)
{
JsonNode values;
JsonNode valueX;
JsonNode valueY;
JsonNode valueZ;
valueX.Float() = static_cast<int>(entry.x);
valueY.Float() = static_cast<int>(entry.y);
valueZ.Float() = static_cast<int>(entry.z);
values.Vector().push_back(valueX);
values.Vector().push_back(valueY);
values.Vector().push_back(valueZ);
deletedObjectsJson.Vector().push_back(values);
}
handler.serializeRaw("deletedObjectsCoordinates", deletedObjectsJson, std::nullopt);
}
void CCastleEvent::serializeJson(JsonSerializeFormat & handler)

View File

@ -12,6 +12,7 @@
#include "../ResourceSet.h"
#include "../texts/MetaString.h"
#include "../int3.h"
VCMI_LIB_NAMESPACE_BEGIN
@ -42,6 +43,12 @@ public:
ui32 firstOccurrence;
ui32 nextOccurrence; /// specifies after how many days the event will occur the next time; 0 if event occurs only one time
std::vector<int3> deletedObjectsCoordinates;
std::vector<const CGObjectInstance*> deletedObjectsInstances;
std::vector<int3> unused;
std::set<const CGObjectInstance*> unused2;
template <typename Handler>
void serialize(Handler & h)
{
@ -64,6 +71,16 @@ public:
h & computerAffected;
h & firstOccurrence;
h & nextOccurrence;
if(h.version >= Handler::Version::EVENT_OBJECTS_DELETION)
{
h & deletedObjectsCoordinates;
h & deletedObjectsInstances;
}
else
{
h & unused;
h & unused2;
}
}
virtual void serializeJson(JsonSerializeFormat & handler);

View File

@ -61,6 +61,7 @@ enum class ESerializationVersion : int32_t
CAMPAIGN_OUTRO_SUPPORT, // 862 - support for campaign outro video
REWARDABLE_BANKS, // 863 - team state contains list of scouted objects, coast visitable rewardable objects
REGION_LABEL, // 864 - labels for campaign regions
EVENT_OBJECTS_DELETION, //865 - allow events to remove map objects
CURRENT = REGION_LABEL
CURRENT = EVENT_OBJECTS_DELETION
};

View File

@ -14,6 +14,8 @@
#include "../../lib/mapObjects/CGTownInstance.h"
#include "../../lib/mapObjects/CGHeroInstance.h"
Q_DECLARE_METATYPE(int3)
//parses date for lose condition (1m 1w 1d)
int expiredDate(const QString & date);
QString expiredDate(int date);

View File

@ -55,6 +55,28 @@ TResources resourcesFromVariant(const QVariant & v)
return TResources(vJson);
}
QVariant toVariant(std::vector<int3> positions)
{
QVariantList result;
for(int3 position : positions)
{
result.push_back(QVariant::fromValue<int3>(position));
}
return result;
}
std::vector<int3> deletedObjectsPositionsFromVariant(const QVariant & v)
{
std::vector<int3> result;
for (auto positionAsVariant : v.toList())
{
int3 position = positionAsVariant.value<int3>();
result.push_back(position);
}
return result;
}
QVariant toVariant(const CMapEvent & event)
{
QVariantMap result;
@ -66,6 +88,7 @@ QVariant toVariant(const CMapEvent & event)
result["firstOccurrence"] = QVariant::fromValue(event.firstOccurrence);
result["nextOccurrence"] = QVariant::fromValue(event.nextOccurrence);
result["resources"] = toVariant(event.resources);
result["deletedObjectsPositions"] = toVariant(event.deletedObjectsCoordinates);
return QVariant(result);
}
@ -81,6 +104,7 @@ CMapEvent eventFromVariant(CMapHeader & mapHeader, const QVariant & variant)
result.firstOccurrence = v.value("firstOccurrence").toInt();
result.nextOccurrence = v.value("nextOccurrence").toInt();
result.resources = resourcesFromVariant(v.value("resources"));
result.deletedObjectsCoordinates = deletedObjectsPositionsFromVariant(v.value("deletedObjectsPositions"));
return result;
}

View File

@ -60,6 +60,12 @@ void NewTurnProcessor::handleTimeEvents(PlayerColor color)
if (event.resources[i])
iw.components.emplace_back(ComponentType::RESOURCE, i, event.resources[i]);
}
//remove objects specified by event
for(const CGObjectInstance * objectToRemove : event.deletedObjectsInstances)
{
gameHandler->removeObject(objectToRemove, PlayerColor::NEUTRAL);
}
gameHandler->sendAndApply(&iw); //show dialog
}
}