2022-10-13 01:51:55 +04:00
|
|
|
/*
|
|
|
|
* mapcontroller.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2024-06-13 06:38:47 +03:00
|
|
|
#include "StdInc.h"
|
2022-09-18 03:23:17 +04:00
|
|
|
#include "mapcontroller.h"
|
|
|
|
|
|
|
|
#include "../lib/GameConstants.h"
|
2025-04-29 12:10:19 +03:00
|
|
|
#include "../lib/entities/artifact/CArtifact.h"
|
2024-10-11 16:30:16 +00:00
|
|
|
#include "../lib/entities/hero/CHeroClass.h"
|
|
|
|
#include "../lib/entities/hero/CHeroHandler.h"
|
2023-06-02 21:47:37 +03:00
|
|
|
#include "../lib/mapObjectConstructors/AObjectTypeHandler.h"
|
|
|
|
#include "../lib/mapObjectConstructors/CObjectClassesHandler.h"
|
2025-09-28 22:47:41 +02:00
|
|
|
#include "../lib/mapObjectConstructors/CommonConstructors.h"
|
2023-06-02 21:47:37 +03:00
|
|
|
#include "../lib/mapObjects/ObjectTemplate.h"
|
2022-09-18 03:23:17 +04:00
|
|
|
#include "../lib/mapping/CMapService.h"
|
|
|
|
#include "../lib/mapping/CMap.h"
|
|
|
|
#include "../lib/mapping/CMapEditManager.h"
|
2023-05-24 02:05:59 +03:00
|
|
|
#include "../lib/mapping/ObstacleProxy.h"
|
2023-07-30 20:12:25 +03:00
|
|
|
#include "../lib/modding/CModHandler.h"
|
2024-11-09 20:29:07 +00:00
|
|
|
#include "../lib/modding/ModDescription.h"
|
2023-01-09 01:17:37 +02:00
|
|
|
#include "../lib/TerrainHandler.h"
|
2022-09-18 03:23:17 +04:00
|
|
|
#include "../lib/CSkillHandler.h"
|
|
|
|
#include "../lib/spells/CSpellHandler.h"
|
2024-06-01 15:28:17 +00:00
|
|
|
#include "../lib/CRandomGenerator.h"
|
2022-12-05 01:32:50 +04:00
|
|
|
#include "../lib/serializer/CMemorySerializer.h"
|
2025-03-21 15:52:30 +01:00
|
|
|
#include "mapsettings/modsettings.h"
|
2022-09-18 03:23:17 +04:00
|
|
|
#include "mapview.h"
|
|
|
|
#include "scenelayer.h"
|
|
|
|
#include "maphandler.h"
|
|
|
|
#include "mainwindow.h"
|
|
|
|
#include "inspector/inspector.h"
|
2025-02-14 16:23:37 +00:00
|
|
|
#include "GameLibrary.h"
|
2025-03-25 15:39:42 +01:00
|
|
|
#include "PlayerSelectionDialog.h"
|
|
|
|
|
|
|
|
MapController::MapController(QObject * parent)
|
|
|
|
: QObject(parent)
|
|
|
|
{
|
|
|
|
}
|
2022-09-18 03:23:17 +04:00
|
|
|
|
|
|
|
MapController::MapController(MainWindow * m): main(m)
|
|
|
|
{
|
2025-08-02 02:15:05 +02:00
|
|
|
for(int i = 0; i < MAX_LEVELS; i++)
|
2022-10-13 01:40:52 +04:00
|
|
|
{
|
|
|
|
_scenes[i].reset(new MapScene(i));
|
|
|
|
_miniscenes[i].reset(new MinimapScene(i));
|
|
|
|
}
|
2022-09-18 03:23:17 +04:00
|
|
|
connectScenes();
|
2025-06-02 15:30:08 +02:00
|
|
|
_cb = std::make_unique<EditorCallback>(nullptr);
|
2022-09-18 03:23:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapController::connectScenes()
|
|
|
|
{
|
2025-08-02 02:15:05 +02:00
|
|
|
for(int i = 0; i < MAX_LEVELS; i++)
|
2022-09-18 03:23:17 +04:00
|
|
|
{
|
|
|
|
//selections for both layers will be handled separately
|
2025-08-02 02:15:05 +02:00
|
|
|
QObject::connect(_scenes[i].get(), &MapScene::selected, [this, i](bool anythingSelected)
|
2022-09-18 03:23:17 +04:00
|
|
|
{
|
2025-08-02 02:15:05 +02:00
|
|
|
main->onSelectionMade(i, anythingSelected);
|
2022-09-18 03:23:17 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MapController::~MapController()
|
|
|
|
{
|
2024-06-13 06:46:41 +03:00
|
|
|
main = nullptr;
|
2022-09-18 03:23:17 +04:00
|
|
|
}
|
|
|
|
|
2025-06-02 15:30:08 +02:00
|
|
|
void MapController::setCallback(std::unique_ptr<EditorCallback> cb)
|
|
|
|
{
|
|
|
|
_cb = std::move(cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
EditorCallback * MapController::getCallback()
|
|
|
|
{
|
|
|
|
return _cb.get();
|
|
|
|
}
|
|
|
|
|
2022-09-18 03:23:17 +04:00
|
|
|
const std::unique_ptr<CMap> & MapController::getMapUniquePtr() const
|
|
|
|
{
|
|
|
|
return _map;
|
|
|
|
}
|
|
|
|
|
|
|
|
CMap * MapController::map()
|
|
|
|
{
|
|
|
|
return _map.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
MapHandler * MapController::mapHandler()
|
|
|
|
{
|
|
|
|
return _mapHandler.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
MapScene * MapController::scene(int level)
|
|
|
|
{
|
|
|
|
return _scenes[level].get();
|
|
|
|
}
|
|
|
|
|
|
|
|
MinimapScene * MapController::miniScene(int level)
|
|
|
|
{
|
|
|
|
return _miniscenes[level].get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapController::repairMap()
|
|
|
|
{
|
2023-10-01 13:32:35 +02:00
|
|
|
repairMap(map());
|
|
|
|
}
|
|
|
|
|
2025-03-23 19:52:21 +01:00
|
|
|
void MapController::repairMap(CMap * map)
|
2023-10-01 13:32:35 +02:00
|
|
|
{
|
|
|
|
if(!map)
|
|
|
|
return;
|
2025-06-02 15:30:08 +02:00
|
|
|
|
|
|
|
assert(map->cb);
|
2023-10-01 13:32:35 +02:00
|
|
|
|
2023-10-01 13:38:16 +02:00
|
|
|
//make sure events/rumors has name to have proper identifiers
|
|
|
|
int emptyNameId = 1;
|
|
|
|
for(auto & e : map->events)
|
|
|
|
if(e.name.empty())
|
|
|
|
e.name = "event_" + std::to_string(emptyNameId++);
|
|
|
|
emptyNameId = 1;
|
|
|
|
for(auto & e : map->rumors)
|
|
|
|
if(e.name.empty())
|
|
|
|
e.name = "rumor_" + std::to_string(emptyNameId++);
|
|
|
|
|
2022-09-18 03:23:17 +04:00
|
|
|
//fix owners for objects
|
2025-03-14 17:07:30 +00:00
|
|
|
std::vector<CGObjectInstance*> allImpactedObjects;
|
2025-03-13 19:42:57 +00:00
|
|
|
|
2025-03-14 17:07:30 +00:00
|
|
|
for (const auto & object : map->objects)
|
|
|
|
allImpactedObjects.push_back(object.get());
|
|
|
|
|
|
|
|
for (const auto & hero : map->getHeroesInPool())
|
|
|
|
allImpactedObjects.push_back(map->tryGetFromHeroPool(hero));
|
2025-03-13 19:42:57 +00:00
|
|
|
|
2023-09-30 04:33:41 +02:00
|
|
|
for(auto obj : allImpactedObjects)
|
2022-09-18 03:23:17 +04:00
|
|
|
{
|
2025-06-18 12:07:30 +03:00
|
|
|
if(obj == nullptr)
|
|
|
|
continue;
|
|
|
|
|
2022-09-20 02:38:10 +04:00
|
|
|
//fix flags
|
2025-04-01 19:11:12 +03:00
|
|
|
if(obj->asOwnable() != nullptr && obj->getOwner() == PlayerColor::UNFLAGGABLE)
|
2022-09-18 03:23:17 +04:00
|
|
|
{
|
2025-04-01 19:11:12 +03:00
|
|
|
obj->tempOwner = PlayerColor::NEUTRAL;
|
2022-09-18 03:23:17 +04:00
|
|
|
}
|
|
|
|
//fix hero instance
|
2025-03-14 17:07:30 +00:00
|
|
|
if(auto * nih = dynamic_cast<CGHeroInstance*>(obj))
|
2022-09-18 03:23:17 +04:00
|
|
|
{
|
2024-01-15 07:41:29 +01:00
|
|
|
// All heroes present on map or in prisons need to be allowed to rehire them after they are defeated
|
|
|
|
|
|
|
|
// FIXME: How about custom scenarios where defeated hero cannot be hired again?
|
|
|
|
|
2024-10-05 19:37:52 +00:00
|
|
|
map->allowedHeroes.insert(nih->getHeroTypeID());
|
2024-01-15 07:41:29 +01:00
|
|
|
|
2025-02-14 16:23:37 +00:00
|
|
|
auto const & type = LIBRARY->heroh->objects[nih->subID];
|
2022-09-20 02:38:10 +04:00
|
|
|
assert(type->heroClass);
|
2024-10-13 13:05:50 +00:00
|
|
|
|
2022-09-20 02:38:10 +04:00
|
|
|
if(nih->ID == Obj::HERO) //not prison
|
2025-02-14 16:23:37 +00:00
|
|
|
nih->appearance = LIBRARY->objtypeh->getHandlerFor(Obj::HERO, type->heroClass->getIndex())->getTemplates().front();
|
2024-03-24 10:01:20 +01:00
|
|
|
//fix spellbook
|
2023-09-30 04:33:41 +02:00
|
|
|
if(nih->spellbookContainsSpell(SpellID::SPELLBOOK_PRESET))
|
|
|
|
{
|
|
|
|
nih->removeSpellFromSpellbook(SpellID::SPELLBOOK_PRESET);
|
|
|
|
if(!nih->getArt(ArtifactPosition::SPELLBOOK) && type->haveSpellBook)
|
2025-03-10 16:20:40 +00:00
|
|
|
nih->putArtifact(ArtifactPosition::SPELLBOOK, map->createArtifact(ArtifactID::SPELLBOOK));
|
2023-09-30 04:33:41 +02:00
|
|
|
}
|
|
|
|
|
2022-09-18 03:23:17 +04:00
|
|
|
}
|
|
|
|
//fix town instance
|
2025-03-14 17:07:30 +00:00
|
|
|
if(auto * tnh = dynamic_cast<CGTownInstance*>(obj))
|
2022-09-18 03:23:17 +04:00
|
|
|
{
|
|
|
|
if(tnh->getTown())
|
|
|
|
{
|
2024-08-17 22:06:48 +03:00
|
|
|
for(const auto & building : tnh->getBuildings())
|
2022-09-18 03:23:17 +04:00
|
|
|
{
|
2024-08-17 22:06:48 +03:00
|
|
|
if(!tnh->getTown()->buildings.count(building))
|
|
|
|
tnh->removeBuilding(building);
|
|
|
|
}
|
2022-09-18 03:23:17 +04:00
|
|
|
vstd::erase_if(tnh->forbiddenBuildings, [tnh](BuildingID bid)
|
|
|
|
{
|
|
|
|
return !tnh->getTown()->buildings.count(bid);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-09-20 03:34:07 +04:00
|
|
|
//fix spell scrolls
|
2025-03-14 17:07:30 +00:00
|
|
|
if(auto * art = dynamic_cast<CGArtifact*>(obj))
|
2022-09-20 03:34:07 +04:00
|
|
|
{
|
2025-03-18 20:38:12 +00:00
|
|
|
if(art->ID == Obj::SPELL_SCROLL && !art->getArtifactInstance())
|
2022-09-20 03:34:07 +04:00
|
|
|
{
|
|
|
|
std::vector<SpellID> out;
|
2025-02-14 16:23:37 +00:00
|
|
|
for(auto const & spell : LIBRARY->spellh->objects) //spellh size appears to be greater (?)
|
2022-09-20 03:34:07 +04:00
|
|
|
{
|
|
|
|
//if(map->isAllowedSpell(spell->id))
|
|
|
|
{
|
|
|
|
out.push_back(spell->id);
|
|
|
|
}
|
|
|
|
}
|
2025-03-10 16:20:40 +00:00
|
|
|
auto a = map->createScroll(*RandomGeneratorUtil::nextItem(out, CRandomGenerator::getDefault()));
|
2025-03-18 20:38:12 +00:00
|
|
|
art->setArtifactInstance(a);
|
2022-09-20 03:34:07 +04:00
|
|
|
}
|
|
|
|
}
|
2024-02-03 11:30:09 +01:00
|
|
|
//fix mines
|
2025-03-14 17:07:30 +00:00
|
|
|
if(auto * mine = dynamic_cast<CGMine*>(obj))
|
2024-02-03 11:30:09 +01:00
|
|
|
{
|
|
|
|
if(!mine->isAbandoned())
|
|
|
|
{
|
2025-09-28 22:47:41 +02:00
|
|
|
if(mine->getResourceHandler()->getResourceType() == GameResID::NONE) // fallback
|
|
|
|
mine->producedResource = GameResID(mine->subID);
|
|
|
|
else
|
|
|
|
mine->producedResource = mine->getResourceHandler()->getResourceType();
|
2024-02-03 11:30:09 +01:00
|
|
|
mine->producedQuantity = mine->defaultResProduction();
|
|
|
|
}
|
|
|
|
}
|
2022-09-18 03:23:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 02:42:12 +04:00
|
|
|
void MapController::setMap(std::unique_ptr<CMap> cmap)
|
2022-09-18 03:23:17 +04:00
|
|
|
{
|
2025-06-02 15:30:08 +02:00
|
|
|
cmap->cb = _cb.get();
|
2022-09-18 03:23:17 +04:00
|
|
|
_map = std::move(cmap);
|
2025-06-02 15:30:08 +02:00
|
|
|
_cb->setMap(_map.get());
|
2022-09-18 03:23:17 +04:00
|
|
|
|
|
|
|
repairMap();
|
|
|
|
|
2025-08-02 02:15:05 +02:00
|
|
|
for(int i = 0; i < _map->mapLevels; i++)
|
2022-10-13 01:40:52 +04:00
|
|
|
{
|
|
|
|
_scenes[i].reset(new MapScene(i));
|
|
|
|
_miniscenes[i].reset(new MinimapScene(i));
|
|
|
|
}
|
2022-09-18 03:23:17 +04:00
|
|
|
resetMapHandler();
|
|
|
|
sceneForceUpdate();
|
|
|
|
|
|
|
|
connectScenes();
|
|
|
|
|
|
|
|
_map->getEditManager()->getUndoManager().setUndoCallback([this](bool allowUndo, bool allowRedo)
|
|
|
|
{
|
2024-06-13 06:46:41 +03:00
|
|
|
if(!main)
|
|
|
|
return;
|
2022-09-18 03:23:17 +04:00
|
|
|
main->enableUndo(allowUndo);
|
|
|
|
main->enableRedo(allowRedo);
|
|
|
|
}
|
|
|
|
);
|
2022-12-03 19:52:56 +04:00
|
|
|
_map->getEditManager()->getUndoManager().clearAll();
|
2023-05-20 10:57:05 +02:00
|
|
|
|
|
|
|
initObstaclePainters(_map.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapController::initObstaclePainters(CMap * map)
|
|
|
|
{
|
2025-02-14 16:23:37 +00:00
|
|
|
for (auto const & terrain : LIBRARY->terrainTypeHandler->objects)
|
2023-05-20 10:57:05 +02:00
|
|
|
{
|
|
|
|
auto terrainId = terrain->getId();
|
|
|
|
_obstaclePainters[terrainId] = std::make_unique<EditorObstaclePlacer>(map);
|
|
|
|
_obstaclePainters[terrainId]->collectPossibleObstacles(terrainId);
|
|
|
|
}
|
2022-09-18 03:23:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapController::sceneForceUpdate()
|
|
|
|
{
|
2025-08-02 02:15:05 +02:00
|
|
|
for(int i = 0; i < _map->mapLevels; i++)
|
2022-09-18 03:23:17 +04:00
|
|
|
{
|
2025-08-02 02:15:05 +02:00
|
|
|
_scenes[i]->updateViews();
|
|
|
|
_miniscenes[i]->updateViews();
|
2022-09-18 03:23:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapController::sceneForceUpdate(int level)
|
|
|
|
{
|
|
|
|
_scenes[level]->updateViews();
|
|
|
|
_miniscenes[level]->updateViews();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapController::resetMapHandler()
|
|
|
|
{
|
|
|
|
if(!_mapHandler)
|
|
|
|
_mapHandler.reset(new MapHandler());
|
|
|
|
_mapHandler->reset(map());
|
2025-08-02 02:15:05 +02:00
|
|
|
for(int i = 0; i < MAX_LEVELS; i++)
|
2022-10-13 01:40:52 +04:00
|
|
|
{
|
|
|
|
_scenes[i]->initialize(*this);
|
|
|
|
_miniscenes[i]->initialize(*this);
|
|
|
|
}
|
2022-09-18 03:23:17 +04:00
|
|
|
}
|
|
|
|
|
2022-10-08 23:54:45 +04:00
|
|
|
void MapController::commitTerrainChange(int level, const TerrainId & terrain)
|
2022-09-18 03:23:17 +04:00
|
|
|
{
|
2023-12-22 17:56:43 +02:00
|
|
|
static const int terrainDecorationPercentageLevel = 10;
|
|
|
|
|
2022-09-18 03:23:17 +04:00
|
|
|
std::vector<int3> v(_scenes[level]->selectionTerrainView.selection().begin(),
|
|
|
|
_scenes[level]->selectionTerrainView.selection().end());
|
|
|
|
if(v.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
_scenes[level]->selectionTerrainView.clear();
|
|
|
|
_scenes[level]->selectionTerrainView.draw();
|
|
|
|
|
|
|
|
_map->getEditManager()->getTerrainSelection().setSelection(v);
|
2023-12-22 17:56:43 +02:00
|
|
|
_map->getEditManager()->drawTerrain(terrain, terrainDecorationPercentageLevel, &CRandomGenerator::getDefault());
|
2022-09-18 03:23:17 +04:00
|
|
|
|
|
|
|
for(auto & t : v)
|
|
|
|
_scenes[level]->terrainView.setDirty(t);
|
|
|
|
_scenes[level]->terrainView.draw();
|
|
|
|
|
|
|
|
_miniscenes[level]->updateViews();
|
|
|
|
main->mapChanged();
|
|
|
|
}
|
|
|
|
|
2022-10-09 00:11:29 +04:00
|
|
|
void MapController::commitRoadOrRiverChange(int level, ui8 type, bool isRoad)
|
2022-09-18 03:23:17 +04:00
|
|
|
{
|
|
|
|
std::vector<int3> v(_scenes[level]->selectionTerrainView.selection().begin(),
|
|
|
|
_scenes[level]->selectionTerrainView.selection().end());
|
|
|
|
if(v.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
_scenes[level]->selectionTerrainView.clear();
|
|
|
|
_scenes[level]->selectionTerrainView.draw();
|
|
|
|
|
|
|
|
_map->getEditManager()->getTerrainSelection().setSelection(v);
|
|
|
|
if(isRoad)
|
2022-10-09 00:11:29 +04:00
|
|
|
_map->getEditManager()->drawRoad(RoadId(type), &CRandomGenerator::getDefault());
|
2022-09-18 03:23:17 +04:00
|
|
|
else
|
2022-10-09 00:11:29 +04:00
|
|
|
_map->getEditManager()->drawRiver(RiverId(type), &CRandomGenerator::getDefault());
|
2022-09-18 03:23:17 +04:00
|
|
|
|
|
|
|
for(auto & t : v)
|
|
|
|
_scenes[level]->terrainView.setDirty(t);
|
|
|
|
_scenes[level]->terrainView.draw();
|
|
|
|
|
|
|
|
_miniscenes[level]->updateViews();
|
|
|
|
main->mapChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapController::commitObjectErase(int level)
|
|
|
|
{
|
|
|
|
auto selectedObjects = _scenes[level]->selectionObjectsView.getSelection();
|
|
|
|
if (selectedObjects.size() > 1)
|
|
|
|
{
|
|
|
|
//mass erase => undo in one operation
|
|
|
|
_map->getEditManager()->removeObjects(selectedObjects);
|
|
|
|
}
|
|
|
|
else if (selectedObjects.size() == 1)
|
|
|
|
{
|
|
|
|
_map->getEditManager()->removeObject(*selectedObjects.begin());
|
|
|
|
}
|
|
|
|
else //nothing to erase - shouldn't be here
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-20 01:25:06 +02:00
|
|
|
for (auto & obj : selectedObjects)
|
2022-09-18 03:23:17 +04:00
|
|
|
{
|
|
|
|
//invalidate tiles under objects
|
2023-10-20 01:25:06 +02:00
|
|
|
_mapHandler->removeObject(obj);
|
2022-12-03 23:11:40 +04:00
|
|
|
_scenes[level]->objectsView.setDirty(obj);
|
2022-09-18 03:23:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
_scenes[level]->selectionObjectsView.clear();
|
|
|
|
_scenes[level]->objectsView.draw();
|
|
|
|
_scenes[level]->selectionObjectsView.draw();
|
|
|
|
_scenes[level]->passabilityView.update();
|
|
|
|
|
|
|
|
_miniscenes[level]->updateViews();
|
|
|
|
main->mapChanged();
|
|
|
|
}
|
|
|
|
|
2022-12-05 01:32:50 +04:00
|
|
|
void MapController::copyToClipboard(int level)
|
|
|
|
{
|
|
|
|
_clipboard.clear();
|
|
|
|
_clipboardShiftIndex = 0;
|
|
|
|
auto selectedObjects = _scenes[level]->selectionObjectsView.getSelection();
|
|
|
|
for(auto * obj : selectedObjects)
|
|
|
|
{
|
|
|
|
assert(obj->pos.z == level);
|
2025-06-04 00:05:18 +02:00
|
|
|
_clipboard.push_back(CMemorySerializer::deepCopy(*obj, _cb.get()));
|
2022-12-05 01:32:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapController::pasteFromClipboard(int level)
|
|
|
|
{
|
|
|
|
_scenes[level]->selectionObjectsView.clear();
|
|
|
|
|
|
|
|
auto shift = int3::getDirs()[_clipboardShiftIndex++];
|
|
|
|
if(_clipboardShiftIndex == int3::getDirs().size())
|
|
|
|
_clipboardShiftIndex = 0;
|
|
|
|
|
2024-08-20 00:53:48 +02:00
|
|
|
QStringList errors;
|
2022-12-27 04:04:09 +04:00
|
|
|
for(auto & objUniquePtr : _clipboard)
|
2022-12-05 01:32:50 +04:00
|
|
|
{
|
2025-06-04 00:05:18 +02:00
|
|
|
auto obj = CMemorySerializer::deepCopyShared(*objUniquePtr, _cb.get());
|
2024-08-20 00:53:48 +02:00
|
|
|
QString errorMsg;
|
2025-03-25 15:39:42 +01:00
|
|
|
if(!canPlaceObject(obj.get(), errorMsg))
|
2024-08-20 00:53:48 +02:00
|
|
|
{
|
|
|
|
errors.push_back(std::move(errorMsg));
|
2024-10-26 12:20:13 +02:00
|
|
|
continue;
|
2024-08-20 00:53:48 +02:00
|
|
|
}
|
2022-12-27 04:04:09 +04:00
|
|
|
auto newPos = objUniquePtr->pos + shift;
|
|
|
|
if(_map->isInTheMap(newPos))
|
|
|
|
obj->pos = newPos;
|
2022-12-05 01:32:50 +04:00
|
|
|
obj->pos.z = level;
|
2025-05-18 00:44:08 +02:00
|
|
|
|
|
|
|
obj->id = {};
|
2025-03-13 19:42:57 +00:00
|
|
|
Initializer init(*this, obj.get(), defaultPlayer);
|
2022-12-05 01:32:50 +04:00
|
|
|
_map->getEditManager()->insertObject(obj);
|
2025-03-13 19:42:57 +00:00
|
|
|
_scenes[level]->selectionObjectsView.selectObject(obj.get());
|
|
|
|
_mapHandler->invalidate(obj.get());
|
2022-12-05 01:32:50 +04:00
|
|
|
}
|
2024-10-26 12:20:13 +02:00
|
|
|
if(!errors.isEmpty())
|
|
|
|
QMessageBox::warning(main, QObject::tr("Can't place object"), errors.join('\n'));
|
2022-12-05 01:32:50 +04:00
|
|
|
|
|
|
|
_scenes[level]->objectsView.draw();
|
|
|
|
_scenes[level]->passabilityView.update();
|
|
|
|
_scenes[level]->selectionObjectsView.draw();
|
|
|
|
|
|
|
|
_miniscenes[level]->updateViews();
|
|
|
|
main->mapChanged();
|
|
|
|
}
|
|
|
|
|
2022-09-18 03:23:17 +04:00
|
|
|
bool MapController::discardObject(int level) const
|
|
|
|
{
|
|
|
|
_scenes[level]->selectionObjectsView.clear();
|
|
|
|
if(_scenes[level]->selectionObjectsView.newObject)
|
|
|
|
{
|
2025-03-13 19:42:57 +00:00
|
|
|
_scenes[level]->selectionObjectsView.newObject.reset();
|
2022-09-18 03:23:17 +04:00
|
|
|
_scenes[level]->selectionObjectsView.shift = QPoint(0, 0);
|
2022-10-13 01:40:52 +04:00
|
|
|
_scenes[level]->selectionObjectsView.selectionMode = SelectionObjectsLayer::NOTHING;
|
2022-09-18 03:23:17 +04:00
|
|
|
_scenes[level]->selectionObjectsView.draw();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2025-03-13 19:42:57 +00:00
|
|
|
void MapController::createObject(int level, std::shared_ptr<CGObjectInstance> obj) const
|
2022-09-18 03:23:17 +04:00
|
|
|
{
|
|
|
|
_scenes[level]->selectionObjectsView.newObject = obj;
|
2022-10-13 01:40:52 +04:00
|
|
|
_scenes[level]->selectionObjectsView.selectionMode = SelectionObjectsLayer::MOVEMENT;
|
2022-09-18 03:23:17 +04:00
|
|
|
_scenes[level]->selectionObjectsView.draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapController::commitObstacleFill(int level)
|
|
|
|
{
|
|
|
|
auto selection = _scenes[level]->selectionTerrainView.selection();
|
|
|
|
if(selection.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
//split by zones
|
2023-05-20 10:57:05 +02:00
|
|
|
for (auto & painter : _obstaclePainters)
|
|
|
|
{
|
|
|
|
painter.second->clearBlockedArea();
|
|
|
|
}
|
|
|
|
|
2022-09-18 03:23:17 +04:00
|
|
|
for(auto & t : selection)
|
|
|
|
{
|
|
|
|
auto tl = _map->getTile(t);
|
2024-07-13 18:37:13 +00:00
|
|
|
if(tl.blocked() || tl.visitable())
|
2022-09-18 03:23:17 +04:00
|
|
|
continue;
|
|
|
|
|
2024-07-13 18:37:13 +00:00
|
|
|
auto terrain = tl.getTerrainID();
|
2023-05-20 10:57:05 +02:00
|
|
|
_obstaclePainters[terrain]->addBlockedTile(t);
|
2022-09-18 03:23:17 +04:00
|
|
|
}
|
|
|
|
|
2023-05-20 10:57:05 +02:00
|
|
|
for(auto & sel : _obstaclePainters)
|
2022-09-18 03:23:17 +04:00
|
|
|
{
|
2025-03-13 19:42:57 +00:00
|
|
|
for(auto o : sel.second->placeObstacles(CRandomGenerator::getDefault()))
|
2023-10-20 01:25:06 +02:00
|
|
|
{
|
2025-03-13 19:42:57 +00:00
|
|
|
_mapHandler->invalidate(o.get());
|
|
|
|
_scenes[level]->objectsView.setDirty(o.get());
|
2023-10-20 01:25:06 +02:00
|
|
|
}
|
2022-09-18 03:23:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
_scenes[level]->selectionTerrainView.clear();
|
|
|
|
_scenes[level]->selectionTerrainView.draw();
|
2023-10-20 01:25:06 +02:00
|
|
|
_scenes[level]->objectsView.draw();
|
2022-09-18 03:23:17 +04:00
|
|
|
_scenes[level]->passabilityView.update();
|
|
|
|
|
|
|
|
_miniscenes[level]->updateViews();
|
|
|
|
main->mapChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapController::commitObjectChange(int level)
|
|
|
|
{
|
2022-12-03 23:11:40 +04:00
|
|
|
for( auto * o : _scenes[level]->selectionObjectsView.getSelection())
|
|
|
|
_scenes[level]->objectsView.setDirty(o);
|
2022-09-18 03:23:17 +04:00
|
|
|
|
|
|
|
_scenes[level]->objectsView.draw();
|
|
|
|
_scenes[level]->selectionObjectsView.draw();
|
|
|
|
_scenes[level]->passabilityView.update();
|
|
|
|
|
|
|
|
_miniscenes[level]->updateViews();
|
|
|
|
main->mapChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MapController::commitChangeWithoutRedraw()
|
|
|
|
{
|
|
|
|
//DO NOT REDRAW
|
|
|
|
main->mapChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapController::commitObjectShift(int level)
|
|
|
|
{
|
|
|
|
auto shift = _scenes[level]->selectionObjectsView.shift;
|
|
|
|
bool makeShift = !shift.isNull();
|
|
|
|
if(makeShift)
|
|
|
|
{
|
|
|
|
for(auto * obj : _scenes[level]->selectionObjectsView.getSelection())
|
|
|
|
{
|
|
|
|
int3 pos = obj->pos;
|
|
|
|
pos.z = level;
|
|
|
|
pos.x += shift.x(); pos.y += shift.y();
|
|
|
|
|
2022-12-03 23:11:40 +04:00
|
|
|
_scenes[level]->objectsView.setDirty(obj); //set dirty before movement
|
2022-09-18 03:23:17 +04:00
|
|
|
_map->getEditManager()->moveObject(obj, pos);
|
|
|
|
_mapHandler->invalidate(obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_scenes[level]->selectionObjectsView.newObject = nullptr;
|
|
|
|
_scenes[level]->selectionObjectsView.shift = QPoint(0, 0);
|
2022-10-13 01:40:52 +04:00
|
|
|
_scenes[level]->selectionObjectsView.selectionMode = SelectionObjectsLayer::NOTHING;
|
2022-09-18 03:23:17 +04:00
|
|
|
|
|
|
|
if(makeShift)
|
|
|
|
{
|
|
|
|
_scenes[level]->objectsView.draw();
|
|
|
|
_scenes[level]->selectionObjectsView.draw();
|
|
|
|
_scenes[level]->passabilityView.update();
|
|
|
|
|
|
|
|
_miniscenes[level]->updateViews();
|
|
|
|
main->mapChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapController::commitObjectCreate(int level)
|
|
|
|
{
|
2025-03-13 19:42:57 +00:00
|
|
|
auto newObj = _scenes[level]->selectionObjectsView.newObject;
|
2022-09-18 03:23:17 +04:00
|
|
|
if(!newObj)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto shift = _scenes[level]->selectionObjectsView.shift;
|
|
|
|
|
|
|
|
int3 pos = newObj->pos;
|
|
|
|
pos.z = level;
|
|
|
|
pos.x += shift.x(); pos.y += shift.y();
|
|
|
|
|
|
|
|
newObj->pos = pos;
|
|
|
|
|
2025-03-13 19:42:57 +00:00
|
|
|
Initializer init(*this, newObj.get(), defaultPlayer);
|
2022-09-18 03:23:17 +04:00
|
|
|
|
|
|
|
_map->getEditManager()->insertObject(newObj);
|
2025-03-13 19:42:57 +00:00
|
|
|
_mapHandler->invalidate(newObj.get());
|
|
|
|
_scenes[level]->objectsView.setDirty(newObj.get());
|
2022-09-18 03:23:17 +04:00
|
|
|
|
|
|
|
_scenes[level]->selectionObjectsView.newObject = nullptr;
|
|
|
|
_scenes[level]->selectionObjectsView.shift = QPoint(0, 0);
|
2022-10-13 01:40:52 +04:00
|
|
|
_scenes[level]->selectionObjectsView.selectionMode = SelectionObjectsLayer::NOTHING;
|
2022-09-18 03:23:17 +04:00
|
|
|
_scenes[level]->objectsView.draw();
|
|
|
|
_scenes[level]->selectionObjectsView.draw();
|
|
|
|
_scenes[level]->passabilityView.update();
|
|
|
|
|
|
|
|
_miniscenes[level]->updateViews();
|
|
|
|
main->mapChanged();
|
|
|
|
}
|
|
|
|
|
2025-03-25 15:39:42 +01:00
|
|
|
bool MapController::canPlaceObject(const CGObjectInstance * newObj, QString & error) const
|
2025-03-21 15:47:45 +01:00
|
|
|
{
|
|
|
|
if(newObj->ID == Obj::GRAIL) //special case for grail
|
2025-03-25 15:39:42 +01:00
|
|
|
return canPlaceGrail(newObj, error);
|
|
|
|
|
|
|
|
if(defaultPlayer == PlayerColor::NEUTRAL && (newObj->ID == Obj::HERO || newObj->ID == Obj::RANDOM_HERO))
|
|
|
|
return canPlaceHero(newObj, error);
|
|
|
|
|
|
|
|
return checkRequiredMods(newObj, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MapController::canPlaceGrail(const CGObjectInstance * grailObj, QString & error) const
|
|
|
|
{
|
|
|
|
assert(grailObj->ID == Obj::GRAIL);
|
|
|
|
|
|
|
|
//find all objects of such type
|
|
|
|
int objCounter = 0;
|
|
|
|
for(auto o : _map->objects)
|
2022-09-18 03:23:17 +04:00
|
|
|
{
|
2025-03-25 15:39:42 +01:00
|
|
|
if(o->ID == grailObj->ID && o->subID == grailObj->subID)
|
2022-09-18 03:23:17 +04:00
|
|
|
{
|
2025-03-25 15:39:42 +01:00
|
|
|
++objCounter;
|
2025-03-21 15:47:45 +01:00
|
|
|
}
|
2025-03-25 15:39:42 +01:00
|
|
|
}
|
2025-03-21 15:47:45 +01:00
|
|
|
|
2025-03-25 15:39:42 +01:00
|
|
|
if(objCounter >= 1)
|
|
|
|
{
|
|
|
|
error = QObject::tr("There can only be one grail object on the map.");
|
|
|
|
return false; //maplimit reached
|
2022-09-18 03:23:17 +04:00
|
|
|
}
|
2022-09-25 00:55:05 +04:00
|
|
|
|
2025-03-25 15:39:42 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MapController::canPlaceHero(const CGObjectInstance * heroObj, QString & error) const
|
|
|
|
{
|
|
|
|
assert(heroObj->ID == Obj::HERO || heroObj->ID == Obj::RANDOM_HERO);
|
|
|
|
|
|
|
|
PlayerSelectionDialog dialog(main);
|
|
|
|
if(dialog.exec() == QDialog::Accepted)
|
2022-09-18 03:23:17 +04:00
|
|
|
{
|
2025-03-25 15:39:42 +01:00
|
|
|
main->switchDefaultPlayer(dialog.getSelectedPlayer());
|
|
|
|
return true;
|
2022-09-18 03:23:17 +04:00
|
|
|
}
|
2025-03-25 15:39:42 +01:00
|
|
|
|
2025-04-05 18:13:24 +02:00
|
|
|
error = tr("Hero %1 cannot be created as NEUTRAL.").arg(QString::fromStdString(heroObj->instanceName));
|
2025-03-25 15:39:42 +01:00
|
|
|
return false;
|
|
|
|
}
|
2025-03-21 15:52:30 +01:00
|
|
|
|
2025-03-25 15:39:42 +01:00
|
|
|
bool MapController::checkRequiredMods(const CGObjectInstance * obj, QString & error) const
|
|
|
|
{
|
2025-03-21 15:52:30 +01:00
|
|
|
ModCompatibilityInfo modsInfo;
|
2025-03-25 15:39:42 +01:00
|
|
|
modAssessmentObject(obj, modsInfo);
|
2025-03-21 15:52:30 +01:00
|
|
|
|
|
|
|
for(auto & mod : modsInfo)
|
|
|
|
{
|
|
|
|
if(!_map->mods.count(mod.first))
|
|
|
|
{
|
2025-03-25 15:39:42 +01:00
|
|
|
auto reply = QMessageBox::question(main,
|
2025-05-13 12:51:31 +02:00
|
|
|
tr("Missing Required Mod"), modMissingMessage(mod.second) + tr("\n\nDo you want to do that now ?"),
|
2025-03-21 15:52:30 +01:00
|
|
|
QMessageBox::Yes | QMessageBox::No);
|
|
|
|
|
|
|
|
if(reply == QMessageBox::Yes)
|
2025-05-13 12:51:31 +02:00
|
|
|
{
|
|
|
|
_map->mods[mod.first] = LIBRARY->modh->getModInfo(mod.first).getVerificationInfo();
|
2025-04-05 18:13:24 +02:00
|
|
|
Q_EMIT requestModsUpdate(modsInfo, true); // signal for MapSettings
|
2025-05-13 12:51:31 +02:00
|
|
|
}
|
2025-03-21 15:52:30 +01:00
|
|
|
else
|
|
|
|
{
|
2025-03-25 15:39:42 +01:00
|
|
|
error = tr("This object's mod is mandatory for map to remain valid.");
|
2025-03-21 15:52:30 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-18 03:23:17 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2025-05-13 12:51:31 +02:00
|
|
|
QString MapController::modMissingMessage(const ModVerificationInfo & info)
|
|
|
|
{
|
|
|
|
QString modName = QString::fromStdString(info.name);
|
|
|
|
QString submod;
|
|
|
|
if(!info.parent.empty())
|
|
|
|
submod = QObject::tr(" (submod of %1)").arg(QString::fromStdString(info.parent));
|
|
|
|
|
|
|
|
return QObject::tr("The mod '%1'%2, is required by an object on the map.\n"
|
|
|
|
"Add it to the map's required mods in Map->General settings.",
|
|
|
|
"should be consistent with Map->General menu entry translation")
|
|
|
|
.arg(modName, submod);
|
|
|
|
}
|
|
|
|
|
2022-09-18 03:23:17 +04:00
|
|
|
void MapController::undo()
|
|
|
|
{
|
|
|
|
_map->getEditManager()->getUndoManager().undo();
|
|
|
|
resetMapHandler();
|
2022-12-03 23:11:40 +04:00
|
|
|
sceneForceUpdate(); //TODO: use smart invalidation (setDirty)
|
2022-09-18 03:23:17 +04:00
|
|
|
main->mapChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapController::redo()
|
|
|
|
{
|
|
|
|
_map->getEditManager()->getUndoManager().redo();
|
|
|
|
resetMapHandler();
|
2022-12-03 23:11:40 +04:00
|
|
|
sceneForceUpdate(); //TODO: use smart invalidation (setDirty)
|
2022-09-18 03:23:17 +04:00
|
|
|
main->mapChanged();
|
|
|
|
}
|
2023-04-17 03:01:29 +04:00
|
|
|
|
|
|
|
ModCompatibilityInfo MapController::modAssessmentAll()
|
|
|
|
{
|
|
|
|
ModCompatibilityInfo result;
|
2025-02-14 16:23:37 +00:00
|
|
|
for(auto primaryID : LIBRARY->objtypeh->knownObjects())
|
2023-04-17 03:01:29 +04:00
|
|
|
{
|
2025-02-14 16:23:37 +00:00
|
|
|
for(auto secondaryID : LIBRARY->objtypeh->knownSubObjects(primaryID))
|
2023-04-17 03:01:29 +04:00
|
|
|
{
|
2025-02-14 16:23:37 +00:00
|
|
|
auto handler = LIBRARY->objtypeh->getHandlerFor(primaryID, secondaryID);
|
2025-03-21 15:52:30 +01:00
|
|
|
auto modScope = handler->getModScope();
|
|
|
|
if(modScope != "core")
|
|
|
|
result[modScope] = LIBRARY->modh->getModInfo(modScope).getVerificationInfo();
|
2023-04-17 03:01:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2025-03-25 15:39:42 +01:00
|
|
|
void MapController::modAssessmentObject(const CGObjectInstance * obj, ModCompatibilityInfo & result)
|
2023-04-17 03:01:29 +04:00
|
|
|
{
|
2025-03-21 15:52:30 +01:00
|
|
|
auto extractEntityMod = [&result](const auto & entity)
|
2024-08-07 21:29:21 +02:00
|
|
|
{
|
2024-08-10 16:08:04 +02:00
|
|
|
auto modScope = entity->getModScope();
|
|
|
|
if(modScope != "core")
|
2025-02-14 16:23:37 +00:00
|
|
|
result[modScope] = LIBRARY->modh->getModInfo(modScope).getVerificationInfo();
|
2024-08-07 21:29:21 +02:00
|
|
|
};
|
|
|
|
|
2025-03-21 15:52:30 +01:00
|
|
|
auto handler = obj->getObjectHandler();
|
|
|
|
auto modScope = handler->getModScope();
|
|
|
|
if(modScope != "core")
|
|
|
|
result[modScope] = LIBRARY->modh->getModInfo(modScope).getVerificationInfo();
|
2024-08-07 21:29:21 +02:00
|
|
|
|
2025-03-21 15:52:30 +01:00
|
|
|
if(obj->ID == Obj::TOWN || obj->ID == Obj::RANDOM_TOWN)
|
|
|
|
{
|
2025-03-25 15:39:42 +01:00
|
|
|
auto town = dynamic_cast<const CGTownInstance *>(obj);
|
2025-03-21 15:52:30 +01:00
|
|
|
for(const auto & spellID : town->possibleSpells)
|
2024-08-07 21:29:21 +02:00
|
|
|
{
|
2025-03-21 15:52:30 +01:00
|
|
|
if(spellID == SpellID::PRESET)
|
|
|
|
continue;
|
|
|
|
extractEntityMod(spellID.toEntity(LIBRARY));
|
2024-08-07 21:29:21 +02:00
|
|
|
}
|
|
|
|
|
2025-03-21 15:52:30 +01:00
|
|
|
for(const auto & spellID : town->obligatorySpells)
|
2024-08-07 21:29:21 +02:00
|
|
|
{
|
2025-05-04 17:31:42 +02:00
|
|
|
extractEntityMod(spellID.toEntity(LIBRARY));
|
2024-08-07 21:29:21 +02:00
|
|
|
}
|
2023-04-17 03:01:29 +04:00
|
|
|
}
|
2024-08-07 21:29:21 +02:00
|
|
|
|
2025-03-21 15:52:30 +01:00
|
|
|
if(obj->ID == Obj::HERO || obj->ID == Obj::RANDOM_HERO)
|
|
|
|
{
|
2025-03-25 15:39:42 +01:00
|
|
|
auto hero = dynamic_cast<const CGHeroInstance *>(obj);
|
2025-03-21 15:52:30 +01:00
|
|
|
for(const auto & spellID : hero->getSpellsInSpellbook())
|
|
|
|
{
|
|
|
|
if(spellID == SpellID::PRESET || spellID == SpellID::SPELLBOOK_PRESET)
|
|
|
|
continue;
|
|
|
|
extractEntityMod(spellID.toEntity(LIBRARY));
|
|
|
|
}
|
|
|
|
|
|
|
|
for(const auto & [_, slotInfo] : hero->artifactsWorn)
|
|
|
|
{
|
2025-05-04 17:31:42 +02:00
|
|
|
extractEntityMod(slotInfo.getArt()->getTypeId().toEntity(LIBRARY));
|
2025-03-21 15:52:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for(const auto & art : hero->artifactsInBackpack)
|
|
|
|
{
|
2025-05-04 17:31:42 +02:00
|
|
|
extractEntityMod(art.getArt()->getTypeId().toEntity(LIBRARY));
|
2025-03-21 15:52:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: terrains?
|
|
|
|
}
|
|
|
|
|
|
|
|
ModCompatibilityInfo MapController::modAssessmentMap(const CMap & map)
|
|
|
|
{
|
|
|
|
ModCompatibilityInfo result;
|
|
|
|
|
|
|
|
for(auto obj : map.objects)
|
|
|
|
{
|
2025-09-18 10:57:56 +02:00
|
|
|
if(!obj)
|
|
|
|
continue;
|
2025-03-21 15:52:30 +01:00
|
|
|
modAssessmentObject(obj.get(), result);
|
|
|
|
}
|
2023-04-17 03:01:29 +04:00
|
|
|
return result;
|
|
|
|
}
|