1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-22 22:13:35 +02:00
vcmi/mapeditor/mapcontroller.cpp

649 lines
17 KiB
C++
Raw Normal View History

2022-10-12 23:51:55 +02: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 05:38:47 +02:00
#include "StdInc.h"
2022-09-18 01:23:17 +02:00
#include "mapcontroller.h"
2023-05-17 16:05:09 +02:00
#include "../lib/ArtifactUtils.h"
2022-09-18 01:23:17 +02:00
#include "../lib/GameConstants.h"
#include "../lib/entities/hero/CHeroClass.h"
#include "../lib/entities/hero/CHeroHandler.h"
#include "../lib/mapObjectConstructors/AObjectTypeHandler.h"
#include "../lib/mapObjectConstructors/CObjectClassesHandler.h"
#include "../lib/mapObjects/ObjectTemplate.h"
2022-09-18 01:23:17 +02:00
#include "../lib/mapping/CMapService.h"
#include "../lib/mapping/CMap.h"
#include "../lib/mapping/CMapEditManager.h"
2023-05-24 01:05:59 +02:00
#include "../lib/mapping/ObstacleProxy.h"
#include "../lib/modding/CModHandler.h"
#include "../lib/modding/CModInfo.h"
#include "../lib/TerrainHandler.h"
2022-09-18 01:23:17 +02:00
#include "../lib/CSkillHandler.h"
#include "../lib/spells/CSpellHandler.h"
#include "../lib/CRandomGenerator.h"
2022-12-04 23:32:50 +02:00
#include "../lib/serializer/CMemorySerializer.h"
2022-09-18 01:23:17 +02:00
#include "mapview.h"
#include "scenelayer.h"
#include "maphandler.h"
#include "mainwindow.h"
#include "inspector/inspector.h"
#include "VCMI_Lib.h"
2022-09-18 01:23:17 +02:00
MapController::MapController(MainWindow * m): main(m)
{
2022-10-12 23:40:52 +02:00
for(int i : {0, 1})
{
_scenes[i].reset(new MapScene(i));
_miniscenes[i].reset(new MinimapScene(i));
}
2022-09-18 01:23:17 +02:00
connectScenes();
}
void MapController::connectScenes()
{
for (int level = 0; level <= 1; level++)
{
//selections for both layers will be handled separately
QObject::connect(_scenes[level].get(), &MapScene::selected, [this, level](bool anythingSelected)
{
main->onSelectionMade(level, anythingSelected);
});
}
}
MapController::~MapController()
{
main = nullptr;
2022-09-18 01:23:17 +02: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());
}
void MapController::repairMap(CMap * map) const
{
if(!map)
return;
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 01:23:17 +02:00
//fix owners for objects
2023-10-01 13:32:35 +02:00
auto allImpactedObjects(map->objects);
allImpactedObjects.insert(allImpactedObjects.end(), map->predefinedHeroes.begin(), map->predefinedHeroes.end());
2023-09-30 04:33:41 +02:00
for(auto obj : allImpactedObjects)
2022-09-18 01:23:17 +02:00
{
2022-09-20 00:38:10 +02:00
//fix flags
2022-09-18 01:23:17 +02:00
if(obj->getOwner() == PlayerColor::UNFLAGGABLE)
{
if(dynamic_cast<CGMine*>(obj.get()) ||
dynamic_cast<CGDwelling*>(obj.get()) ||
dynamic_cast<CGTownInstance*>(obj.get()) ||
dynamic_cast<CGGarrison*>(obj.get()) ||
dynamic_cast<CGShipyard*>(obj.get()) ||
dynamic_cast<FlaggableMapObject*>(obj.get()) ||
2022-09-18 01:23:17 +02:00
dynamic_cast<CGHeroInstance*>(obj.get()))
obj->tempOwner = PlayerColor::NEUTRAL;
}
//fix hero instance
if(auto * nih = dynamic_cast<CGHeroInstance*>(obj.get()))
{
// 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?
map->allowedHeroes.insert(nih->getHeroTypeID());
auto const & type = VLC->heroh->objects[nih->subID];
2022-09-20 00:38:10 +02:00
assert(type->heroClass);
2022-09-20 00:38:10 +02:00
if(nih->ID == Obj::HERO) //not prison
2022-09-18 01:23:17 +02:00
nih->appearance = VLC->objtypeh->getHandlerFor(Obj::HERO, type->heroClass->getIndex())->getTemplates().front();
//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)
nih->putArtifact(ArtifactPosition::SPELLBOOK, ArtifactUtils::createArtifact(ArtifactID::SPELLBOOK));
2023-09-30 04:33:41 +02:00
}
2022-09-18 01:23:17 +02:00
}
//fix town instance
if(auto * tnh = dynamic_cast<CGTownInstance*>(obj.get()))
{
if(tnh->getTown())
{
for(const auto & building : tnh->getBuildings())
2022-09-18 01:23:17 +02:00
{
if(!tnh->getTown()->buildings.count(building))
tnh->removeBuilding(building);
}
2022-09-18 01:23:17 +02:00
vstd::erase_if(tnh->forbiddenBuildings, [tnh](BuildingID bid)
{
return !tnh->getTown()->buildings.count(bid);
});
}
}
2022-09-20 01:34:07 +02:00
//fix spell scrolls
if(auto * art = dynamic_cast<CGArtifact*>(obj.get()))
{
if(art->ID == Obj::SPELL_SCROLL && !art->storedArtifact)
{
std::vector<SpellID> out;
for(auto const & spell : VLC->spellh->objects) //spellh size appears to be greater (?)
2022-09-20 01:34:07 +02:00
{
//if(map->isAllowedSpell(spell->id))
{
out.push_back(spell->id);
}
}
2023-05-17 16:05:09 +02:00
auto a = ArtifactUtils::createScroll(*RandomGeneratorUtil::nextItem(out, CRandomGenerator::getDefault()));
2022-09-20 01:34:07 +02:00
art->storedArtifact = a;
}
}
//fix mines
if(auto * mine = dynamic_cast<CGMine*>(obj.get()))
{
if(!mine->isAbandoned())
{
mine->producedResource = GameResID(mine->subID);
mine->producedQuantity = mine->defaultResProduction();
}
}
2022-09-18 01:23:17 +02:00
}
}
void MapController::setMap(std::unique_ptr<CMap> cmap)
2022-09-18 01:23:17 +02:00
{
_map = std::move(cmap);
repairMap();
2022-10-12 23:40:52 +02:00
for(int i : {0, 1})
{
_scenes[i].reset(new MapScene(i));
_miniscenes[i].reset(new MinimapScene(i));
}
2022-09-18 01:23:17 +02:00
resetMapHandler();
sceneForceUpdate();
connectScenes();
_map->getEditManager()->getUndoManager().setUndoCallback([this](bool allowUndo, bool allowRedo)
{
if(!main)
return;
2022-09-18 01:23:17 +02:00
main->enableUndo(allowUndo);
main->enableRedo(allowRedo);
}
);
2022-12-03 17:52:56 +02:00
_map->getEditManager()->getUndoManager().clearAll();
initObstaclePainters(_map.get());
}
void MapController::initObstaclePainters(CMap * map)
{
for (auto const & terrain : VLC->terrainTypeHandler->objects)
{
auto terrainId = terrain->getId();
_obstaclePainters[terrainId] = std::make_unique<EditorObstaclePlacer>(map);
_obstaclePainters[terrainId]->collectPossibleObstacles(terrainId);
}
2022-09-18 01:23:17 +02:00
}
void MapController::sceneForceUpdate()
{
_scenes[0]->updateViews();
_miniscenes[0]->updateViews();
if(_map->twoLevel)
{
_scenes[1]->updateViews();
_miniscenes[1]->updateViews();
}
}
void MapController::sceneForceUpdate(int level)
{
_scenes[level]->updateViews();
_miniscenes[level]->updateViews();
}
void MapController::resetMapHandler()
{
if(!_mapHandler)
_mapHandler.reset(new MapHandler());
_mapHandler->reset(map());
2022-10-12 23:40:52 +02:00
for(int i : {0, 1})
{
_scenes[i]->initialize(*this);
_miniscenes[i]->initialize(*this);
}
2022-09-18 01:23:17 +02:00
}
2022-10-08 21:54:45 +02:00
void MapController::commitTerrainChange(int level, const TerrainId & terrain)
2022-09-18 01:23:17 +02:00
{
2023-12-22 17:56:43 +02:00
static const int terrainDecorationPercentageLevel = 10;
2022-09-18 01:23:17 +02: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 01:23:17 +02:00
for(auto & t : v)
_scenes[level]->terrainView.setDirty(t);
_scenes[level]->terrainView.draw();
_miniscenes[level]->updateViews();
main->mapChanged();
}
void MapController::commitRoadOrRiverChange(int level, ui8 type, bool isRoad)
2022-09-18 01:23:17 +02: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)
_map->getEditManager()->drawRoad(RoadId(type), &CRandomGenerator::getDefault());
2022-09-18 01:23:17 +02:00
else
_map->getEditManager()->drawRiver(RiverId(type), &CRandomGenerator::getDefault());
2022-09-18 01:23:17 +02: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 01:23:17 +02:00
{
//invalidate tiles under objects
2023-10-20 01:25:06 +02:00
_mapHandler->removeObject(obj);
_scenes[level]->objectsView.setDirty(obj);
2022-09-18 01:23:17 +02:00
}
_scenes[level]->selectionObjectsView.clear();
_scenes[level]->objectsView.draw();
_scenes[level]->selectionObjectsView.draw();
_scenes[level]->passabilityView.update();
_miniscenes[level]->updateViews();
main->mapChanged();
}
2022-12-04 23:32:50 +02: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);
_clipboard.push_back(CMemorySerializer::deepCopy(*obj));
}
}
void MapController::pasteFromClipboard(int level)
{
_scenes[level]->selectionObjectsView.clear();
auto shift = int3::getDirs()[_clipboardShiftIndex++];
if(_clipboardShiftIndex == int3::getDirs().size())
_clipboardShiftIndex = 0;
QStringList errors;
2022-12-27 02:04:09 +02:00
for(auto & objUniquePtr : _clipboard)
2022-12-04 23:32:50 +02:00
{
2022-12-27 02:04:09 +02:00
auto * obj = CMemorySerializer::deepCopy(*objUniquePtr).release();
QString errorMsg;
if (!canPlaceObject(level, obj, errorMsg))
{
errors.push_back(std::move(errorMsg));
continue;
}
2022-12-27 02:04:09 +02:00
auto newPos = objUniquePtr->pos + shift;
if(_map->isInTheMap(newPos))
obj->pos = newPos;
2022-12-04 23:32:50 +02:00
obj->pos.z = level;
Initializer init(obj, defaultPlayer);
_map->getEditManager()->insertObject(obj);
_scenes[level]->selectionObjectsView.selectObject(obj);
_mapHandler->invalidate(obj);
}
if(!errors.isEmpty())
QMessageBox::warning(main, QObject::tr("Can't place object"), errors.join('\n'));
2022-12-04 23:32:50 +02:00
_scenes[level]->objectsView.draw();
_scenes[level]->passabilityView.update();
_scenes[level]->selectionObjectsView.draw();
_miniscenes[level]->updateViews();
main->mapChanged();
}
2022-09-18 01:23:17 +02:00
bool MapController::discardObject(int level) const
{
_scenes[level]->selectionObjectsView.clear();
if(_scenes[level]->selectionObjectsView.newObject)
{
delete _scenes[level]->selectionObjectsView.newObject;
_scenes[level]->selectionObjectsView.newObject = nullptr;
_scenes[level]->selectionObjectsView.shift = QPoint(0, 0);
2022-10-12 23:40:52 +02:00
_scenes[level]->selectionObjectsView.selectionMode = SelectionObjectsLayer::NOTHING;
2022-09-18 01:23:17 +02:00
_scenes[level]->selectionObjectsView.draw();
return true;
}
return false;
}
void MapController::createObject(int level, CGObjectInstance * obj) const
{
_scenes[level]->selectionObjectsView.newObject = obj;
2022-10-12 23:40:52 +02:00
_scenes[level]->selectionObjectsView.selectionMode = SelectionObjectsLayer::MOVEMENT;
2022-09-18 01:23:17 +02:00
_scenes[level]->selectionObjectsView.draw();
}
void MapController::commitObstacleFill(int level)
{
auto selection = _scenes[level]->selectionTerrainView.selection();
if(selection.empty())
return;
//split by zones
for (auto & painter : _obstaclePainters)
{
painter.second->clearBlockedArea();
}
2022-09-18 01:23:17 +02:00
for(auto & t : selection)
{
auto tl = _map->getTile(t);
if(tl.blocked() || tl.visitable())
2022-09-18 01:23:17 +02:00
continue;
auto terrain = tl.getTerrainID();
_obstaclePainters[terrain]->addBlockedTile(t);
2022-09-18 01:23:17 +02:00
}
for(auto & sel : _obstaclePainters)
2022-09-18 01:23:17 +02:00
{
2023-10-20 01:25:06 +02:00
for(auto * o : sel.second->placeObstacles(CRandomGenerator::getDefault()))
{
_mapHandler->invalidate(o);
_scenes[level]->objectsView.setDirty(o);
}
2022-09-18 01:23:17 +02:00
}
_scenes[level]->selectionTerrainView.clear();
_scenes[level]->selectionTerrainView.draw();
2023-10-20 01:25:06 +02:00
_scenes[level]->objectsView.draw();
2022-09-18 01:23:17 +02:00
_scenes[level]->passabilityView.update();
_miniscenes[level]->updateViews();
main->mapChanged();
}
void MapController::commitObjectChange(int level)
{
for( auto * o : _scenes[level]->selectionObjectsView.getSelection())
_scenes[level]->objectsView.setDirty(o);
2022-09-18 01:23:17 +02: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();
_scenes[level]->objectsView.setDirty(obj); //set dirty before movement
2022-09-18 01:23:17 +02:00
_map->getEditManager()->moveObject(obj, pos);
_mapHandler->invalidate(obj);
}
}
_scenes[level]->selectionObjectsView.newObject = nullptr;
_scenes[level]->selectionObjectsView.shift = QPoint(0, 0);
2022-10-12 23:40:52 +02:00
_scenes[level]->selectionObjectsView.selectionMode = SelectionObjectsLayer::NOTHING;
2022-09-18 01:23:17 +02: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)
{
auto * newObj = _scenes[level]->selectionObjectsView.newObject;
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;
Initializer init(newObj, defaultPlayer);
_map->getEditManager()->insertObject(newObj);
_mapHandler->invalidate(newObj);
_scenes[level]->objectsView.setDirty(newObj);
2022-09-18 01:23:17 +02:00
_scenes[level]->selectionObjectsView.newObject = nullptr;
_scenes[level]->selectionObjectsView.shift = QPoint(0, 0);
2022-10-12 23:40:52 +02:00
_scenes[level]->selectionObjectsView.selectionMode = SelectionObjectsLayer::NOTHING;
2022-09-18 01:23:17 +02:00
_scenes[level]->objectsView.draw();
_scenes[level]->selectionObjectsView.draw();
_scenes[level]->passabilityView.update();
_miniscenes[level]->updateViews();
main->mapChanged();
}
bool MapController::canPlaceObject(int level, CGObjectInstance * newObj, QString & error) const
{
//find all objects of such type
int objCounter = 0;
for(auto o : _map->objects)
{
if(o->ID == newObj->ID && o->subID == newObj->subID)
{
++objCounter;
}
}
2022-09-24 22:55:05 +02:00
if(newObj->ID == Obj::GRAIL && objCounter >= 1) //special case for grail
2022-09-18 01:23:17 +02:00
{
error = QObject::tr("There can only be one grail object on the map.");
2022-09-18 01:23:17 +02:00
return false; //maplimit reached
}
2022-09-24 22:55:05 +02:00
2022-09-18 01:23:17 +02:00
if(defaultPlayer == PlayerColor::NEUTRAL && (newObj->ID == Obj::HERO || newObj->ID == Obj::RANDOM_HERO))
{
error = QObject::tr("Hero %1 cannot be created as NEUTRAL.").arg(QString::fromStdString(newObj->instanceName));
2022-09-18 01:23:17 +02:00
return false;
}
return true;
}
void MapController::undo()
{
_map->getEditManager()->getUndoManager().undo();
resetMapHandler();
sceneForceUpdate(); //TODO: use smart invalidation (setDirty)
2022-09-18 01:23:17 +02:00
main->mapChanged();
}
void MapController::redo()
{
_map->getEditManager()->getUndoManager().redo();
resetMapHandler();
sceneForceUpdate(); //TODO: use smart invalidation (setDirty)
2022-09-18 01:23:17 +02:00
main->mapChanged();
}
2023-04-17 01:01:29 +02:00
ModCompatibilityInfo MapController::modAssessmentAll()
{
ModCompatibilityInfo result;
for(auto primaryID : VLC->objtypeh->knownObjects())
{
for(auto secondaryID : VLC->objtypeh->knownSubObjects(primaryID))
{
auto handler = VLC->objtypeh->getHandlerFor(primaryID, secondaryID);
auto modName = QString::fromStdString(handler->getJsonKey()).split(":").at(0).toStdString();
if(modName != "core")
2023-09-21 04:31:08 +02:00
result[modName] = VLC->modh->getModInfo(modName).getVerificationInfo();
2023-04-17 01:01:29 +02:00
}
}
return result;
}
ModCompatibilityInfo MapController::modAssessmentMap(const CMap & map)
{
ModCompatibilityInfo result;
auto extractEntityMod = [&result](const auto & entity)
{
auto modScope = entity->getModScope();
if(modScope != "core")
result[modScope] = VLC->modh->getModInfo(modScope).getVerificationInfo();
};
2023-04-17 01:01:29 +02:00
for(auto obj : map.objects)
{
2023-10-28 11:27:10 +02:00
auto handler = obj->getObjectHandler();
auto modScope = handler->getModScope();
if(modScope != "core")
result[modScope] = VLC->modh->getModInfo(modScope).getVerificationInfo();
if(obj->ID == Obj::TOWN || obj->ID == Obj::RANDOM_TOWN)
{
auto town = dynamic_cast<CGTownInstance *>(obj.get());
for(const auto & spellID : town->possibleSpells)
{
if(spellID == SpellID::PRESET)
continue;
extractEntityMod(spellID.toEntity(VLC));
}
for(const auto & spellID : town->obligatorySpells)
{
extractEntityMod(spellID.toEntity(VLC));
}
}
if(obj->ID == Obj::HERO || obj->ID == Obj::RANDOM_HERO)
{
auto hero = dynamic_cast<CGHeroInstance *>(obj.get());
for(const auto & spellID : hero->getSpellsInSpellbook())
{
if(spellID == SpellID::PRESET || spellID == SpellID::SPELLBOOK_PRESET)
continue;
extractEntityMod(spellID.toEntity(VLC));
}
}
2023-04-17 01:01:29 +02:00
}
//TODO: terrains, artifacts?
2023-04-17 01:01:29 +02:00
return result;
}