2012-06-29 16:44:26 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
// This file is part of Remere's Map Editor
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
2020-07-30 11:42:28 -03:00
|
|
|
// Remere's Map Editor is free software: you can redistribute it and/or modify
|
2012-06-29 16:44:26 +02:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
2020-07-30 11:42:28 -03:00
|
|
|
// Remere's Map Editor is distributed in the hope that it will be useful,
|
2012-06-29 16:44:26 +02:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2020-07-30 11:42:28 -03:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2012-06-29 16:44:26 +02:00
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2020-07-30 11:42:28 -03:00
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2012-06-29 16:44:26 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
|
|
#include "action.h"
|
|
|
|
|
#include "settings.h"
|
|
|
|
|
#include "map.h"
|
|
|
|
|
#include "editor.h"
|
|
|
|
|
#include "gui.h"
|
|
|
|
|
|
2014-01-22 20:16:17 +01:00
|
|
|
Change::Change() : type(CHANGE_NONE), data(nullptr)
|
2012-06-29 16:44:26 +02:00
|
|
|
{
|
2015-12-06 11:42:37 -03:00
|
|
|
////
|
2012-06-29 16:44:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Change::Change(Tile* t) : type(CHANGE_TILE)
|
|
|
|
|
{
|
|
|
|
|
ASSERT(t);
|
|
|
|
|
data = t;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-01 14:44:11 +01:00
|
|
|
Change* Change::Create(House* house, const Position& where)
|
2012-06-29 16:44:26 +02:00
|
|
|
{
|
|
|
|
|
Change* c = newd Change();
|
|
|
|
|
c->type = CHANGE_MOVE_HOUSE_EXIT;
|
|
|
|
|
std::pair<uint32_t, Position>* p = newd std::pair<uint32_t, Position>;
|
|
|
|
|
p->first = house->id;
|
|
|
|
|
p->second = where;
|
|
|
|
|
c->data = p;
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-01 14:44:11 +01:00
|
|
|
Change* Change::Create(Waypoint* wp, const Position& where)
|
2012-06-29 16:44:26 +02:00
|
|
|
{
|
|
|
|
|
Change* c = newd Change();
|
|
|
|
|
c->type = CHANGE_MOVE_WAYPOINT;
|
|
|
|
|
std::pair<std::string, Position>* p = newd std::pair<std::string, Position>;
|
|
|
|
|
p->first = wp->name;
|
|
|
|
|
p->second = where;
|
|
|
|
|
c->data = p;
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Change::~Change()
|
|
|
|
|
{
|
|
|
|
|
clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Change::clear()
|
|
|
|
|
{
|
2015-12-06 11:42:37 -03:00
|
|
|
switch(type) {
|
2012-06-29 16:44:26 +02:00
|
|
|
case CHANGE_TILE:
|
|
|
|
|
ASSERT(data);
|
|
|
|
|
delete reinterpret_cast<Tile*>(data);
|
|
|
|
|
break;
|
|
|
|
|
case CHANGE_MOVE_HOUSE_EXIT:
|
|
|
|
|
ASSERT(data);
|
|
|
|
|
delete reinterpret_cast<std::pair<uint32_t, Position>* >(data);
|
|
|
|
|
break;
|
|
|
|
|
case CHANGE_MOVE_WAYPOINT:
|
|
|
|
|
ASSERT(data);
|
|
|
|
|
delete reinterpret_cast<std::pair<std::string, Position>* >(data);
|
|
|
|
|
break;
|
|
|
|
|
case CHANGE_NONE:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
#ifdef __DEBUG_MODE__
|
|
|
|
|
if(data)
|
|
|
|
|
printf("UNHANDLED CHANGE TYPE! Leak!");
|
|
|
|
|
#endif
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
type = CHANGE_NONE;
|
2014-01-22 20:16:17 +01:00
|
|
|
data = nullptr;
|
2012-06-29 16:44:26 +02:00
|
|
|
}
|
|
|
|
|
|
2014-02-28 22:34:25 +01:00
|
|
|
uint32_t Change::memsize() const
|
2012-06-29 16:44:26 +02:00
|
|
|
{
|
2014-02-28 22:34:25 +01:00
|
|
|
uint32_t mem = sizeof(*this);
|
2012-06-29 16:44:26 +02:00
|
|
|
switch(type) {
|
|
|
|
|
case CHANGE_TILE:
|
|
|
|
|
ASSERT(data);
|
|
|
|
|
mem += reinterpret_cast<Tile*>(data)->memsize();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return mem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Action::Action(Editor& editor, ActionIdentifier ident) :
|
|
|
|
|
commited(false),
|
|
|
|
|
editor(editor),
|
|
|
|
|
type(ident)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Action::~Action()
|
|
|
|
|
{
|
|
|
|
|
ChangeList::const_reverse_iterator it = changes.rbegin();
|
|
|
|
|
while(it != changes.rend()) {
|
|
|
|
|
delete *it;
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t Action::approx_memsize() const
|
|
|
|
|
{
|
2014-02-28 22:34:25 +01:00
|
|
|
uint32_t mem = sizeof(*this);
|
2012-06-29 16:44:26 +02:00
|
|
|
mem += changes.size() * (sizeof(Change) + sizeof(Tile) + sizeof(Item) + 6/* approx overhead*/);
|
|
|
|
|
return mem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t Action::memsize() const
|
|
|
|
|
{
|
2014-02-28 22:34:25 +01:00
|
|
|
uint32_t mem = sizeof(*this);
|
2012-06-29 16:44:26 +02:00
|
|
|
mem += sizeof(Change*) * 3 * changes.size();
|
|
|
|
|
ChangeList::const_iterator it = changes.begin();
|
2015-12-06 11:42:37 -03:00
|
|
|
while(it != changes.end()) {
|
2012-06-29 16:44:26 +02:00
|
|
|
Change* c = *it;
|
2015-12-06 11:42:37 -03:00
|
|
|
switch(c->type) {
|
2012-06-29 16:44:26 +02:00
|
|
|
case CHANGE_TILE:
|
|
|
|
|
{
|
|
|
|
|
ASSERT(c->data);
|
|
|
|
|
mem += reinterpret_cast<Tile*>(c->data)->memsize();
|
2015-12-06 11:42:37 -03:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-29 16:44:26 +02:00
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
return mem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Action::commit(DirtyList* dirty_list)
|
|
|
|
|
{
|
|
|
|
|
editor.selection.start(Selection::INTERNAL);
|
|
|
|
|
ChangeList::const_iterator it = changes.begin();
|
2015-12-06 11:42:37 -03:00
|
|
|
while(it != changes.end()) {
|
2012-06-29 16:44:26 +02:00
|
|
|
Change* c = *it;
|
|
|
|
|
switch(c->type) {
|
2015-12-06 11:42:37 -03:00
|
|
|
case CHANGE_TILE: {
|
2012-06-29 16:44:26 +02:00
|
|
|
void** data = &c->data;
|
|
|
|
|
Tile* newtile = reinterpret_cast<Tile*>(*data);
|
|
|
|
|
ASSERT(newtile);
|
|
|
|
|
Position pos = newtile->getPosition();
|
2016-09-29 19:24:45 -03:00
|
|
|
|
2015-12-06 11:42:37 -03:00
|
|
|
if(editor.IsLiveClient()) {
|
2012-06-29 16:44:26 +02:00
|
|
|
QTreeNode* nd = editor.map.getLeaf(pos.x, pos.y);
|
2016-02-25 14:21:27 -03:00
|
|
|
if(!nd || !nd->isVisible(pos.z > GROUND_LAYER)) {
|
2012-06-29 16:44:26 +02:00
|
|
|
// Delete all changes that affect tiles outside our view
|
|
|
|
|
c->clear();
|
|
|
|
|
++it;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Tile* oldtile = editor.map.swapTile(pos, newtile);
|
|
|
|
|
TileLocation* location = newtile->getLocation();
|
2016-09-29 19:24:45 -03:00
|
|
|
|
2012-06-29 16:44:26 +02:00
|
|
|
// Update other nodes in the network
|
|
|
|
|
if(editor.IsLiveServer() && dirty_list)
|
|
|
|
|
dirty_list->AddPosition(pos.x, pos.y, pos.z);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
newtile->update();
|
|
|
|
|
|
|
|
|
|
//std::cout << "\tSwitched tile at " << pos.x << ";" << pos.y << ";" << pos.z << " from " << (void*)oldtile << " to " << *data << std::endl;
|
|
|
|
|
if(newtile->isSelected())
|
|
|
|
|
editor.selection.addInternal(newtile);
|
|
|
|
|
|
2015-12-06 11:42:37 -03:00
|
|
|
if(oldtile) {
|
|
|
|
|
if(newtile->getHouseID() != oldtile->getHouseID()) {
|
2012-06-29 16:44:26 +02:00
|
|
|
// oooooomggzzz we need to add it to the appropriate house!
|
|
|
|
|
House* house = editor.map.houses.getHouse(oldtile->getHouseID());
|
|
|
|
|
if(house)
|
|
|
|
|
house->removeTile(oldtile);
|
|
|
|
|
|
|
|
|
|
house = editor.map.houses.getHouse(newtile->getHouseID());
|
|
|
|
|
if(house)
|
|
|
|
|
house->addTile(newtile);
|
|
|
|
|
}
|
2021-05-25 00:29:12 -03:00
|
|
|
if(oldtile->spawnMonster) {
|
|
|
|
|
if(newtile->spawnMonster) {
|
|
|
|
|
if(*oldtile->spawnMonster != *newtile->spawnMonster) {
|
|
|
|
|
editor.map.removeSpawnMonster(oldtile);
|
|
|
|
|
editor.map.addSpawnMonster(newtile);
|
2012-06-29 16:44:26 +02:00
|
|
|
}
|
2015-12-06 11:42:37 -03:00
|
|
|
} else {
|
2021-05-25 00:29:12 -03:00
|
|
|
// Monster spawn has been removed
|
|
|
|
|
editor.map.removeSpawnMonster(oldtile);
|
2012-06-29 16:44:26 +02:00
|
|
|
}
|
2021-05-25 00:29:12 -03:00
|
|
|
} else if(newtile->spawnMonster) {
|
|
|
|
|
editor.map.addSpawnMonster(newtile);
|
|
|
|
|
}
|
|
|
|
|
if(oldtile->spawnNpc) {
|
|
|
|
|
if(newtile->spawnNpc) {
|
|
|
|
|
if(*oldtile->spawnNpc != *newtile->spawnNpc) {
|
|
|
|
|
editor.map.removeSpawnNpc(oldtile);
|
|
|
|
|
editor.map.addSpawnNpc(newtile);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// SpawnMonster has been removed
|
|
|
|
|
editor.map.removeSpawnNpc(oldtile);
|
|
|
|
|
}
|
|
|
|
|
} else if(newtile->spawnNpc) {
|
|
|
|
|
editor.map.addSpawnNpc(newtile);
|
2012-06-29 16:44:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//oldtile->update();
|
|
|
|
|
if(oldtile->isSelected())
|
|
|
|
|
editor.selection.removeInternal(oldtile);
|
|
|
|
|
|
|
|
|
|
*data = oldtile;
|
2015-12-06 11:42:37 -03:00
|
|
|
} else {
|
2012-06-29 16:44:26 +02:00
|
|
|
*data = editor.map.allocator(location);
|
2015-12-06 11:42:37 -03:00
|
|
|
if(newtile->getHouseID() != 0) {
|
2012-06-29 16:44:26 +02:00
|
|
|
// oooooomggzzz we need to add it to the appropriate house!
|
|
|
|
|
House* house = editor.map.houses.getHouse(newtile->getHouseID());
|
2015-12-06 11:42:37 -03:00
|
|
|
if(house) {
|
2012-06-29 16:44:26 +02:00
|
|
|
house->addTile(newtile);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-25 00:29:12 -03:00
|
|
|
if(newtile->spawnMonster)
|
|
|
|
|
editor.map.addSpawnMonster(newtile);
|
|
|
|
|
|
|
|
|
|
if(newtile->spawnNpc)
|
|
|
|
|
editor.map.addSpawnNpc(newtile);
|
2012-06-29 16:44:26 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
// Mark the tile as modified
|
|
|
|
|
newtile->modify();
|
|
|
|
|
|
|
|
|
|
// Update client dirty list
|
2015-12-06 11:42:37 -03:00
|
|
|
if(editor.IsLiveClient() && dirty_list && type != ACTION_REMOTE) {
|
2012-06-29 16:44:26 +02:00
|
|
|
// Local action, assemble changes
|
|
|
|
|
dirty_list->AddChange(c);
|
|
|
|
|
}
|
2015-12-06 11:42:37 -03:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case CHANGE_MOVE_HOUSE_EXIT: {
|
2012-06-29 16:44:26 +02:00
|
|
|
std::pair<uint32_t, Position>* p = reinterpret_cast<std::pair<uint32_t, Position>* >(c->data);
|
|
|
|
|
ASSERT(p);
|
|
|
|
|
House* whathouse = editor.map.houses.getHouse(p->first);
|
|
|
|
|
|
2015-12-06 11:42:37 -03:00
|
|
|
if(whathouse) {
|
2012-06-29 16:44:26 +02:00
|
|
|
Position oldpos = whathouse->getExit();
|
|
|
|
|
whathouse->setExit(p->second);
|
|
|
|
|
p->second = oldpos;
|
|
|
|
|
}
|
2015-12-06 11:42:37 -03:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case CHANGE_MOVE_WAYPOINT: {
|
2012-06-29 16:44:26 +02:00
|
|
|
std::pair<std::string, Position>* p = reinterpret_cast<std::pair<std::string, Position>* >(c->data);
|
|
|
|
|
ASSERT(p);
|
|
|
|
|
Waypoint* wp = editor.map.waypoints.getWaypoint(p->first);
|
|
|
|
|
|
|
|
|
|
if(wp) {
|
|
|
|
|
// Change the tiles
|
|
|
|
|
TileLocation* oldtile = editor.map.getTileL(wp->pos);
|
|
|
|
|
TileLocation* newtile = editor.map.getTileL(p->second);
|
2016-09-29 19:24:45 -03:00
|
|
|
|
2012-06-29 16:44:26 +02:00
|
|
|
// Only need to remove from old if it actually exists
|
|
|
|
|
if(p->second != Position())
|
|
|
|
|
if(oldtile && oldtile->getWaypointCount() > 0)
|
|
|
|
|
oldtile->decreaseWaypointCount();
|
|
|
|
|
|
|
|
|
|
newtile->increaseWaypointCount();
|
|
|
|
|
|
|
|
|
|
// Update shit
|
|
|
|
|
Position oldpos = wp->pos;
|
|
|
|
|
wp->pos = p->second;
|
|
|
|
|
p->second = oldpos;
|
|
|
|
|
}
|
2015-12-06 11:42:37 -03:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-29 16:44:26 +02:00
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
editor.selection.finish(Selection::INTERNAL);
|
|
|
|
|
commited = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Action::undo(DirtyList* dirty_list)
|
|
|
|
|
{
|
|
|
|
|
if(changes.empty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
editor.selection.start(Selection::INTERNAL);
|
|
|
|
|
ChangeList::reverse_iterator it = changes.rbegin();
|
|
|
|
|
|
2015-12-06 11:42:37 -03:00
|
|
|
while(it != changes.rend()) {
|
2012-06-29 16:44:26 +02:00
|
|
|
Change* c = *it;
|
2015-12-06 11:42:37 -03:00
|
|
|
switch(c->type) {
|
|
|
|
|
case CHANGE_TILE: {
|
2012-06-29 16:44:26 +02:00
|
|
|
void** data = &c->data;
|
|
|
|
|
Tile* oldtile = reinterpret_cast<Tile*>(*data);
|
|
|
|
|
ASSERT(oldtile);
|
|
|
|
|
Position pos = oldtile->getPosition();
|
2016-09-29 19:24:45 -03:00
|
|
|
|
2015-12-06 11:42:37 -03:00
|
|
|
if(editor.IsLiveClient()) {
|
2012-06-29 16:44:26 +02:00
|
|
|
QTreeNode* nd = editor.map.getLeaf(pos.x, pos.y);
|
2016-02-25 14:21:27 -03:00
|
|
|
if(!nd || !nd->isVisible(pos.z > GROUND_LAYER)) {
|
2012-06-29 16:44:26 +02:00
|
|
|
// Delete all changes that affect tiles outside our view
|
|
|
|
|
c->clear();
|
|
|
|
|
++it;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Tile* newtile = editor.map.swapTile(pos, oldtile);
|
|
|
|
|
|
|
|
|
|
// Update server side change list (for broadcast)
|
|
|
|
|
if(editor.IsLiveServer() && dirty_list)
|
|
|
|
|
dirty_list->AddPosition(pos.x, pos.y, pos.z);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(oldtile->isSelected())
|
|
|
|
|
editor.selection.addInternal(oldtile);
|
|
|
|
|
if(newtile->isSelected())
|
|
|
|
|
editor.selection.removeInternal(newtile);
|
|
|
|
|
|
2015-12-06 11:42:37 -03:00
|
|
|
if(newtile->getHouseID() != oldtile->getHouseID()) {
|
2012-06-29 16:44:26 +02:00
|
|
|
// oooooomggzzz we need to remove it from the appropriate house!
|
|
|
|
|
House* house = editor.map.houses.getHouse(newtile->getHouseID());
|
2015-12-06 11:42:37 -03:00
|
|
|
if(house) {
|
2012-06-29 16:44:26 +02:00
|
|
|
house->removeTile(newtile);
|
2015-12-06 11:42:37 -03:00
|
|
|
} else {
|
2012-06-29 16:44:26 +02:00
|
|
|
// Set tile house to 0, house has been removed
|
2014-01-22 20:16:17 +01:00
|
|
|
newtile->setHouse(nullptr);
|
2012-06-29 16:44:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
house = editor.map.houses.getHouse(oldtile->getHouseID());
|
2015-12-06 11:42:37 -03:00
|
|
|
if(house) {
|
2012-06-29 16:44:26 +02:00
|
|
|
house->addTile(oldtile);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-06 11:42:37 -03:00
|
|
|
|
2021-05-25 00:29:12 -03:00
|
|
|
if(oldtile->spawnMonster) {
|
|
|
|
|
if(newtile->spawnMonster) {
|
|
|
|
|
if(*oldtile->spawnMonster != *newtile->spawnMonster) {
|
|
|
|
|
editor.map.removeSpawnMonster(newtile);
|
|
|
|
|
editor.map.addSpawnMonster(oldtile);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
editor.map.addSpawnMonster(oldtile);
|
|
|
|
|
}
|
|
|
|
|
} else if(newtile->spawnMonster) {
|
|
|
|
|
editor.map.removeSpawnMonster(newtile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(oldtile->spawnNpc) {
|
|
|
|
|
if(newtile->spawnNpc) {
|
|
|
|
|
if(*oldtile->spawnNpc != *newtile->spawnNpc) {
|
|
|
|
|
editor.map.removeSpawnNpc(newtile);
|
|
|
|
|
editor.map.addSpawnNpc(oldtile);
|
2012-06-29 16:44:26 +02:00
|
|
|
}
|
2015-12-06 11:42:37 -03:00
|
|
|
} else {
|
2021-05-25 00:29:12 -03:00
|
|
|
editor.map.addSpawnNpc(oldtile);
|
2012-06-29 16:44:26 +02:00
|
|
|
}
|
2021-05-25 00:29:12 -03:00
|
|
|
} else if(newtile->spawnNpc) {
|
|
|
|
|
editor.map.removeSpawnNpc(newtile);
|
2012-06-29 16:44:26 +02:00
|
|
|
}
|
|
|
|
|
*data = newtile;
|
2016-09-29 19:24:45 -03:00
|
|
|
|
|
|
|
|
|
2012-06-29 16:44:26 +02:00
|
|
|
// Update client dirty list
|
2015-12-06 11:42:37 -03:00
|
|
|
if(editor.IsLiveClient() && dirty_list && type != ACTION_REMOTE) {
|
2012-06-29 16:44:26 +02:00
|
|
|
// Local action, assemble changes
|
|
|
|
|
dirty_list->AddChange(c);
|
|
|
|
|
}
|
2015-12-06 11:42:37 -03:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case CHANGE_MOVE_HOUSE_EXIT: {
|
2012-06-29 16:44:26 +02:00
|
|
|
std::pair<uint32_t, Position>* p = reinterpret_cast<std::pair<uint32_t, Position>* >(c->data);
|
|
|
|
|
ASSERT(p);
|
|
|
|
|
House* whathouse = editor.map.houses.getHouse(p->first);
|
2015-12-06 11:42:37 -03:00
|
|
|
if(whathouse) {
|
2012-06-29 16:44:26 +02:00
|
|
|
Position oldpos = whathouse->getExit();
|
|
|
|
|
whathouse->setExit(p->second);
|
|
|
|
|
p->second = oldpos;
|
|
|
|
|
}
|
2015-12-06 11:42:37 -03:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case CHANGE_MOVE_WAYPOINT: {
|
2012-06-29 16:44:26 +02:00
|
|
|
std::pair<std::string, Position>* p = reinterpret_cast<std::pair<std::string, Position>* >(c->data);
|
|
|
|
|
ASSERT(p);
|
|
|
|
|
Waypoint* wp = editor.map.waypoints.getWaypoint(p->first);
|
|
|
|
|
|
2015-12-06 11:42:37 -03:00
|
|
|
if(wp) {
|
2012-06-29 16:44:26 +02:00
|
|
|
// Change the tiles
|
|
|
|
|
TileLocation* oldtile = editor.map.getTileL(wp->pos);
|
|
|
|
|
TileLocation* newtile = editor.map.getTileL(p->second);
|
2016-09-29 19:24:45 -03:00
|
|
|
|
2012-06-29 16:44:26 +02:00
|
|
|
// Only need to remove from old if it actually exists
|
|
|
|
|
if(p->second != Position())
|
|
|
|
|
if(oldtile && oldtile->getWaypointCount() > 0)
|
|
|
|
|
oldtile->decreaseWaypointCount();
|
|
|
|
|
|
|
|
|
|
newtile->increaseWaypointCount();
|
|
|
|
|
|
|
|
|
|
// Update shit
|
|
|
|
|
Position oldpos = wp->pos;
|
|
|
|
|
wp->pos = p->second;
|
|
|
|
|
p->second = oldpos;
|
|
|
|
|
}
|
2015-12-06 11:42:37 -03:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-29 16:44:26 +02:00
|
|
|
default:
|
2015-12-06 11:42:37 -03:00
|
|
|
break;
|
2012-06-29 16:44:26 +02:00
|
|
|
}
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
editor.selection.finish(Selection::INTERNAL);
|
|
|
|
|
commited = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BatchAction::BatchAction(Editor& editor, ActionIdentifier ident) :
|
|
|
|
|
editor(editor),
|
|
|
|
|
timestamp(0),
|
|
|
|
|
memory_size(0),
|
|
|
|
|
type(ident)
|
|
|
|
|
{
|
2015-12-06 11:42:37 -03:00
|
|
|
////
|
2012-06-29 16:44:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BatchAction::~BatchAction()
|
|
|
|
|
{
|
2015-12-06 11:42:37 -03:00
|
|
|
for(Action* action : batch) {
|
2014-03-01 14:44:11 +01:00
|
|
|
delete action;
|
|
|
|
|
}
|
|
|
|
|
batch.clear();
|
2012-06-29 16:44:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t BatchAction::memsize(bool recalc) const
|
|
|
|
|
{
|
|
|
|
|
// Expensive operation, only evaluate once (won't change anyways)
|
2015-12-06 11:42:37 -03:00
|
|
|
if(!recalc && memory_size > 0) {
|
2014-03-01 14:44:11 +01:00
|
|
|
return memory_size;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-28 22:34:25 +01:00
|
|
|
uint32_t mem = sizeof(*this);
|
2012-06-29 16:44:26 +02:00
|
|
|
mem += sizeof(Action*) * 3 * batch.size();
|
|
|
|
|
|
2015-12-06 11:42:37 -03:00
|
|
|
for(Action* action : batch) {
|
2012-06-29 16:44:26 +02:00
|
|
|
#ifdef __USE_EXACT_MEMSIZE__
|
2014-03-01 14:44:11 +01:00
|
|
|
mem += action->memsize();
|
2012-06-29 16:44:26 +02:00
|
|
|
#else
|
|
|
|
|
// Less exact but MUCH faster
|
2014-03-01 14:44:11 +01:00
|
|
|
mem += action->approx_memsize();
|
2012-06-29 16:44:26 +02:00
|
|
|
#endif
|
|
|
|
|
}
|
2014-03-01 14:44:11 +01:00
|
|
|
|
2012-06-29 16:44:26 +02:00
|
|
|
const_cast<BatchAction*>(this)->memory_size = mem;
|
|
|
|
|
return mem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BatchAction::addAction(Action* action)
|
|
|
|
|
{
|
|
|
|
|
// If empty, do nothing.
|
2015-12-06 11:42:37 -03:00
|
|
|
if(action->size() == 0) {
|
2012-06-29 16:44:26 +02:00
|
|
|
delete action;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ASSERT(action->getType() == type);
|
2016-09-29 19:24:45 -03:00
|
|
|
|
2012-06-29 16:44:26 +02:00
|
|
|
if(!editor.CanEdit()) {
|
|
|
|
|
delete action;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add it!
|
|
|
|
|
batch.push_back(action);
|
2014-01-22 20:16:17 +01:00
|
|
|
timestamp = time(nullptr);
|
2012-06-29 16:44:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BatchAction::addAndCommitAction(Action* action)
|
|
|
|
|
{
|
|
|
|
|
// If empty, do nothing.
|
2015-12-06 11:42:37 -03:00
|
|
|
if(action->size() == 0) {
|
2012-06-29 16:44:26 +02:00
|
|
|
delete action;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-09-29 19:24:45 -03:00
|
|
|
|
2012-06-29 16:44:26 +02:00
|
|
|
if(!editor.CanEdit()) {
|
|
|
|
|
delete action;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add it!
|
2014-01-22 20:16:17 +01:00
|
|
|
action->commit(nullptr);
|
2012-06-29 16:44:26 +02:00
|
|
|
batch.push_back(action);
|
2014-01-22 20:16:17 +01:00
|
|
|
timestamp = time(nullptr);
|
2012-06-29 16:44:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BatchAction::commit()
|
|
|
|
|
{
|
2015-12-06 11:42:37 -03:00
|
|
|
for(Action* action : batch) {
|
|
|
|
|
if(!action->isCommited()) {
|
2014-03-01 14:44:11 +01:00
|
|
|
action->commit(nullptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-06-29 16:44:26 +02:00
|
|
|
}
|
|
|
|
|
|
2014-03-01 14:44:11 +01:00
|
|
|
void BatchAction::undo()
|
|
|
|
|
{
|
2015-12-06 11:42:37 -03:00
|
|
|
for(Action* action : boost::adaptors::reverse(batch)) {
|
2014-03-01 14:44:11 +01:00
|
|
|
action->undo(nullptr);
|
|
|
|
|
}
|
2012-06-29 16:44:26 +02:00
|
|
|
}
|
|
|
|
|
|
2014-03-01 14:44:11 +01:00
|
|
|
void BatchAction::redo()
|
|
|
|
|
{
|
2015-12-06 11:42:37 -03:00
|
|
|
for(Action* action : batch) {
|
2014-03-01 14:44:11 +01:00
|
|
|
action->redo(nullptr);
|
|
|
|
|
}
|
2012-06-29 16:44:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BatchAction::merge(BatchAction* other)
|
|
|
|
|
{
|
2014-03-01 14:44:11 +01:00
|
|
|
batch.insert(batch.end(), other->batch.begin(), other->batch.end());
|
2012-06-29 16:44:26 +02:00
|
|
|
other->batch.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-01 14:44:11 +01:00
|
|
|
ActionQueue::ActionQueue(Editor& editor) :
|
|
|
|
|
current(0), memory_size(0), editor(editor)
|
2012-06-29 16:44:26 +02:00
|
|
|
{
|
2015-12-06 11:42:37 -03:00
|
|
|
////
|
2012-06-29 16:44:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ActionQueue::~ActionQueue()
|
|
|
|
|
{
|
2015-12-06 11:42:37 -03:00
|
|
|
for(auto it = actions.begin(); it != actions.end(); it = actions.erase(it)) {
|
2012-06-29 16:44:26 +02:00
|
|
|
delete *it;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Action* ActionQueue::createAction(ActionIdentifier ident)
|
|
|
|
|
{
|
|
|
|
|
return newd Action(editor, ident);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Action* ActionQueue::createAction(BatchAction* batch)
|
|
|
|
|
{
|
|
|
|
|
return newd Action(editor, batch->getType());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BatchAction* ActionQueue::createBatch(ActionIdentifier ident)
|
|
|
|
|
{
|
|
|
|
|
return newd BatchAction(editor, ident);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ActionQueue::resetTimer()
|
|
|
|
|
{
|
|
|
|
|
if(!actions.empty())
|
|
|
|
|
actions.back()->resetTimer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ActionQueue::addBatch(BatchAction* batch, int stacking_delay)
|
|
|
|
|
{
|
|
|
|
|
ASSERT(batch);
|
|
|
|
|
ASSERT(current <= actions.size());
|
|
|
|
|
|
2015-12-06 11:42:37 -03:00
|
|
|
if(batch->size() == 0) {
|
2012-06-29 16:44:26 +02:00
|
|
|
delete batch;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-12-06 11:42:37 -03:00
|
|
|
|
2012-06-29 16:44:26 +02:00
|
|
|
// Commit any uncommited actions...
|
|
|
|
|
batch->commit();
|
|
|
|
|
|
|
|
|
|
// Update title
|
|
|
|
|
if(editor.map.doChange())
|
2016-09-29 20:30:54 -03:00
|
|
|
g_gui.UpdateTitle();
|
2016-09-29 19:24:45 -03:00
|
|
|
|
2015-12-06 11:42:37 -03:00
|
|
|
if(batch->type == ACTION_REMOTE) {
|
2012-06-29 16:44:26 +02:00
|
|
|
delete batch;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-06 11:42:37 -03:00
|
|
|
while(current != actions.size()) {
|
2012-06-29 16:44:26 +02:00
|
|
|
memory_size -= actions.back()->memsize();
|
|
|
|
|
BatchAction* todelete = actions.back();
|
|
|
|
|
actions.pop_back();
|
|
|
|
|
delete todelete;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-29 20:37:43 -03:00
|
|
|
while(memory_size > size_t(1024 * 1024 * g_settings.getInteger(Config::UNDO_MEM_SIZE)) && !actions.empty()) {
|
2012-06-29 16:44:26 +02:00
|
|
|
memory_size -= actions.front()->memsize();
|
|
|
|
|
delete actions.front();
|
|
|
|
|
actions.pop_front();
|
|
|
|
|
current--;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-29 20:37:43 -03:00
|
|
|
if(actions.size() > size_t(g_settings.getInteger(Config::UNDO_SIZE)) && !actions.empty()) {
|
2012-06-29 16:44:26 +02:00
|
|
|
memory_size -= actions.front()->memsize();
|
|
|
|
|
BatchAction* todelete = actions.front();
|
|
|
|
|
actions.pop_front();
|
|
|
|
|
delete todelete;
|
|
|
|
|
current--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
do {
|
2015-12-06 11:42:37 -03:00
|
|
|
if(!actions.empty()) {
|
2012-06-29 16:44:26 +02:00
|
|
|
BatchAction* lastAction = actions.back();
|
2016-09-29 20:37:43 -03:00
|
|
|
if(lastAction->type == batch->type && g_settings.getInteger(Config::GROUP_ACTIONS) && time(nullptr) - stacking_delay < lastAction->timestamp) {
|
2012-06-29 16:44:26 +02:00
|
|
|
lastAction->merge(batch);
|
2014-01-22 20:16:17 +01:00
|
|
|
lastAction->timestamp = time(nullptr);
|
2012-06-29 16:44:26 +02:00
|
|
|
memory_size -= lastAction->memsize();
|
|
|
|
|
memory_size += lastAction->memsize(true);
|
|
|
|
|
delete batch;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
memory_size += batch->memsize();
|
|
|
|
|
actions.push_back(batch);
|
2014-01-22 20:16:17 +01:00
|
|
|
batch->timestamp = time(nullptr);
|
2012-06-29 16:44:26 +02:00
|
|
|
current++;
|
|
|
|
|
} while(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ActionQueue::addAction(Action* action, int stacking_delay)
|
|
|
|
|
{
|
|
|
|
|
BatchAction* batch = createBatch(action->getType());
|
|
|
|
|
batch->addAndCommitAction(action);
|
2015-12-06 11:42:37 -03:00
|
|
|
if(batch->size() == 0) {
|
2012-06-29 16:44:26 +02:00
|
|
|
delete batch;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addBatch(batch, stacking_delay);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ActionQueue::undo()
|
|
|
|
|
{
|
2015-12-06 11:42:37 -03:00
|
|
|
if(current > 0) {
|
2012-06-29 16:44:26 +02:00
|
|
|
current--;
|
|
|
|
|
BatchAction* batch = actions[current];
|
|
|
|
|
batch->undo();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ActionQueue::redo()
|
|
|
|
|
{
|
2015-12-06 11:42:37 -03:00
|
|
|
if(current < actions.size()) {
|
2012-06-29 16:44:26 +02:00
|
|
|
BatchAction* batch = actions[current];
|
|
|
|
|
batch->redo();
|
|
|
|
|
current++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ActionQueue::clear()
|
|
|
|
|
{
|
2015-12-06 11:42:37 -03:00
|
|
|
for(ActionList::iterator it = actions.begin(); it != actions.end();) {
|
2012-06-29 16:44:26 +02:00
|
|
|
delete *it;
|
|
|
|
|
it = actions.erase(it);
|
|
|
|
|
}
|
|
|
|
|
current = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DirtyList::DirtyList() :
|
|
|
|
|
owner(0)
|
|
|
|
|
{
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DirtyList::~DirtyList()
|
|
|
|
|
{
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DirtyList::AddPosition(int x, int y, int z)
|
|
|
|
|
{
|
|
|
|
|
uint32_t m = ((x >> 2) << 18) | ((y >> 2) << 4);
|
|
|
|
|
ValueType fi = {m, 0};
|
|
|
|
|
SetType::iterator s = iset.find(fi);
|
2015-12-06 11:42:37 -03:00
|
|
|
if(s != iset.end()) {
|
2012-06-29 16:44:26 +02:00
|
|
|
ValueType v = *s;
|
|
|
|
|
iset.erase(s);
|
|
|
|
|
v.floors = (1 << z) | v.floors;
|
|
|
|
|
iset.insert(v);
|
2015-12-06 11:42:37 -03:00
|
|
|
} else {
|
2014-01-23 21:06:52 -02:00
|
|
|
ValueType v = {m, (uint32_t)(1 << z)};
|
2012-06-29 16:44:26 +02:00
|
|
|
iset.insert(v);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DirtyList::AddChange(Change* c)
|
|
|
|
|
{
|
|
|
|
|
ichanges.push_back(c);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DirtyList::SetType& DirtyList::GetPosList()
|
|
|
|
|
{
|
|
|
|
|
return iset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ChangeList& DirtyList::GetChanges()
|
|
|
|
|
{
|
|
|
|
|
return ichanges;
|
|
|
|
|
}
|