porymap/map.cpp

516 lines
17 KiB
C++
Raw Normal View History

2018-09-25 01:12:29 +01:00
#include "history.h"
#include "historyitem.h"
2016-09-07 04:50:47 +01:00
#include "map.h"
2018-09-19 18:35:45 +01:00
#include "project.h"
2016-09-07 04:50:47 +01:00
#include <QTime>
#include <QDebug>
#include <QPainter>
2017-11-28 04:46:27 +00:00
#include <QImage>
#include <QRegularExpression>
2016-09-07 04:50:47 +01:00
2016-09-07 04:50:47 +01:00
Map::Map(QObject *parent) : QObject(parent)
{
}
void Map::setName(QString mapName) {
name = mapName;
constantName = mapConstantFromName(mapName);
}
QString Map::mapConstantFromName(QString mapName) {
// Transform map names of the form 'GraniteCave_B1F` into map constants like 'MAP_GRANITE_CAVE_B1F'.
QString nameWithUnderscores = mapName.replace(QRegularExpression("([a-z])([A-Z])"), "\\1_\\2");
QString withMapAndUppercase = "MAP_" + nameWithUnderscores.toUpper();
QString constantName = withMapAndUppercase.replace(QRegularExpression("_+"), "_");
// Handle special cases.
// SSTidal needs to be SS_TIDAL, rather than SSTIDAL
constantName = constantName.replace("SSTIDAL", "SS_TIDAL");
return constantName;
}
2018-07-07 17:57:44 +01:00
QString Map::objectEventsLabelFromName(QString mapName)
{
return QString("%1_EventObjects").arg(mapName);
}
QString Map::warpEventsLabelFromName(QString mapName)
{
return QString("%1_MapWarps").arg(mapName);
}
QString Map::coordEventsLabelFromName(QString mapName)
{
return QString("%1_MapCoordEvents").arg(mapName);
}
QString Map::bgEventsLabelFromName(QString mapName)
{
return QString("%1_MapBGEvents").arg(mapName);
}
2016-09-07 04:50:47 +01:00
int Map::getWidth() {
2018-06-20 23:43:20 +01:00
return layout->width.toInt(nullptr, 0);
2016-09-07 04:50:47 +01:00
}
int Map::getHeight() {
2018-06-20 23:43:20 +01:00
return layout->height.toInt(nullptr, 0);
2016-09-07 04:50:47 +01:00
}
2018-09-15 00:37:36 +01:00
uint16_t Map::getSelectedBlockIndex(int index) {
2018-06-20 23:43:20 +01:00
if (index < layout->tileset_primary->metatiles->length()) {
2018-09-15 00:37:36 +01:00
return static_cast<uint16_t>(index);
} else {
2018-09-19 18:35:45 +01:00
return static_cast<uint16_t>(Project::getNumMetatilesPrimary() + index - layout->tileset_primary->metatiles->length());
}
}
int Map::getDisplayedBlockIndex(int index) {
2018-06-20 23:43:20 +01:00
if (index < layout->tileset_primary->metatiles->length()) {
return index;
} else {
2018-09-19 18:35:45 +01:00
return index - Project::getNumMetatilesPrimary() + layout->tileset_primary->metatiles->length();
}
}
2016-09-07 04:50:47 +01:00
QImage Map::getCollisionMetatileImage(Block block) {
2018-07-20 04:27:40 +01:00
return getCollisionMetatileImage(block.collision, block.elevation);
2016-09-07 04:50:47 +01:00
}
2018-07-20 04:27:40 +01:00
QImage Map::getCollisionMetatileImage(int collision, int elevation) {
int x = collision * 16;
int y = elevation * 16;
QPixmap collisionImage = QPixmap(":/images/collisions.png").copy(x, y, 16, 16);
return collisionImage.toImage();
2016-09-07 04:50:47 +01:00
}
bool Map::blockChanged(int i, Blockdata *cache) {
2018-09-15 00:37:36 +01:00
if (!cache)
2017-11-28 04:46:27 +00:00
return true;
2018-09-15 00:37:36 +01:00
if (!layout->blockdata)
2017-11-28 04:46:27 +00:00
return true;
2018-09-15 00:37:36 +01:00
if (!cache->blocks)
2017-11-28 04:46:27 +00:00
return true;
2018-09-15 00:37:36 +01:00
if (!layout->blockdata->blocks)
2017-11-28 04:46:27 +00:00
return true;
2018-09-15 00:37:36 +01:00
if (cache->blocks->length() <= i)
2016-09-07 04:50:47 +01:00
return true;
2018-09-15 00:37:36 +01:00
if (layout->blockdata->blocks->length() <= i)
2017-11-28 04:46:27 +00:00
return true;
2018-09-15 00:37:36 +01:00
2018-06-20 23:43:20 +01:00
return layout->blockdata->blocks->value(i) != cache->blocks->value(i);
2016-09-07 04:50:47 +01:00
}
void Map::cacheBorder() {
2018-06-20 23:43:20 +01:00
if (layout->cached_border) delete layout->cached_border;
layout->cached_border = new Blockdata;
if (layout->border && layout->border->blocks) {
for (int i = 0; i < layout->border->blocks->length(); i++) {
Block block = layout->border->blocks->value(i);
layout->cached_border->blocks->append(block);
2017-11-28 04:46:27 +00:00
}
2016-09-07 04:50:47 +01:00
}
}
void Map::cacheBlockdata() {
2018-06-20 23:43:20 +01:00
if (layout->cached_blockdata) delete layout->cached_blockdata;
layout->cached_blockdata = new Blockdata;
if (layout->blockdata && layout->blockdata->blocks) {
for (int i = 0; i < layout->blockdata->blocks->length(); i++) {
Block block = layout->blockdata->blocks->value(i);
layout->cached_blockdata->blocks->append(block);
2017-11-28 04:46:27 +00:00
}
2016-09-07 04:50:47 +01:00
}
}
void Map::cacheCollision() {
2018-06-20 23:43:20 +01:00
if (layout->cached_collision) delete layout->cached_collision;
layout->cached_collision = new Blockdata;
if (layout->blockdata && layout->blockdata->blocks) {
for (int i = 0; i < layout->blockdata->blocks->length(); i++) {
Block block = layout->blockdata->blocks->value(i);
layout->cached_collision->blocks->append(block);
2017-11-28 04:46:27 +00:00
}
2016-09-07 04:50:47 +01:00
}
}
2018-06-20 23:43:20 +01:00
QPixmap Map::renderCollision(bool ignoreCache) {
2016-09-07 04:50:47 +01:00
bool changed_any = false;
int width_ = getWidth();
int height_ = getHeight();
if (
collision_image.isNull()
|| collision_image.width() != width_ * 16
|| collision_image.height() != height_ * 16
) {
collision_image = QImage(width_ * 16, height_ * 16, QImage::Format_RGBA8888);
changed_any = true;
}
2018-06-20 23:43:20 +01:00
if (!(layout->blockdata && layout->blockdata->blocks && width_ && height_)) {
2017-11-28 04:46:27 +00:00
collision_pixmap = collision_pixmap.fromImage(collision_image);
return collision_pixmap;
}
2016-09-07 04:50:47 +01:00
QPainter painter(&collision_image);
2018-06-20 23:43:20 +01:00
for (int i = 0; i < layout->blockdata->blocks->length(); i++) {
if (!ignoreCache && layout->cached_collision && !blockChanged(i, layout->cached_collision)) {
2016-09-07 04:50:47 +01:00
continue;
}
changed_any = true;
2018-06-20 23:43:20 +01:00
Block block = layout->blockdata->blocks->value(i);
QImage metatile_image = Tileset::getMetatileImage(block.tile, layout->tileset_primary, layout->tileset_secondary);
2016-09-07 04:50:47 +01:00
QImage collision_metatile_image = getCollisionMetatileImage(block);
2017-11-28 04:46:27 +00:00
int map_y = width_ ? i / width_ : 0;
int map_x = width_ ? i % width_ : 0;
2016-09-07 04:50:47 +01:00
QPoint metatile_origin = QPoint(map_x * 16, map_y * 16);
painter.setOpacity(1);
painter.drawImage(metatile_origin, metatile_image);
painter.save();
2018-07-20 04:27:40 +01:00
painter.setOpacity(0.55);
2016-09-07 04:50:47 +01:00
painter.drawImage(metatile_origin, collision_metatile_image);
painter.restore();
}
painter.end();
cacheCollision();
if (changed_any) {
collision_pixmap = collision_pixmap.fromImage(collision_image);
}
return collision_pixmap;
}
2018-06-20 23:43:20 +01:00
QPixmap Map::render(bool ignoreCache = false) {
2016-09-07 04:50:47 +01:00
bool changed_any = false;
int width_ = getWidth();
int height_ = getHeight();
if (
image.isNull()
|| image.width() != width_ * 16
|| image.height() != height_ * 16
) {
image = QImage(width_ * 16, height_ * 16, QImage::Format_RGBA8888);
changed_any = true;
}
2018-06-20 23:43:20 +01:00
if (!(layout->blockdata && layout->blockdata->blocks && width_ && height_)) {
2017-11-28 04:46:27 +00:00
pixmap = pixmap.fromImage(image);
return pixmap;
}
2016-09-07 04:50:47 +01:00
QPainter painter(&image);
2018-06-20 23:43:20 +01:00
for (int i = 0; i < layout->blockdata->blocks->length(); i++) {
if (!ignoreCache && !blockChanged(i, layout->cached_blockdata)) {
2016-09-07 04:50:47 +01:00
continue;
}
changed_any = true;
2018-06-20 23:43:20 +01:00
Block block = layout->blockdata->blocks->value(i);
QImage metatile_image = Tileset::getMetatileImage(block.tile, layout->tileset_primary, layout->tileset_secondary);
2017-11-28 04:46:27 +00:00
int map_y = width_ ? i / width_ : 0;
int map_x = width_ ? i % width_ : 0;
2016-09-07 04:50:47 +01:00
QPoint metatile_origin = QPoint(map_x * 16, map_y * 16);
painter.drawImage(metatile_origin, metatile_image);
}
painter.end();
if (changed_any) {
cacheBlockdata();
pixmap = pixmap.fromImage(image);
}
2016-09-07 04:50:47 +01:00
return pixmap;
}
QPixmap Map::renderBorder() {
bool changed_any = false;
int width_ = 2;
int height_ = 2;
2018-06-20 23:43:20 +01:00
if (layout->border_image.isNull()) {
layout->border_image = QImage(width_ * 16, height_ * 16, QImage::Format_RGBA8888);
2016-09-07 04:50:47 +01:00
changed_any = true;
}
2018-06-20 23:43:20 +01:00
if (!(layout->border && layout->border->blocks)) {
layout->border_pixmap = layout->border_pixmap.fromImage(layout->border_image);
return layout->border_pixmap;
2017-11-28 04:46:27 +00:00
}
2018-06-20 23:43:20 +01:00
QPainter painter(&layout->border_image);
for (int i = 0; i < layout->border->blocks->length(); i++) {
if (!blockChanged(i, layout->cached_border)) {
2016-09-07 04:50:47 +01:00
continue;
}
changed_any = true;
2018-06-20 23:43:20 +01:00
Block block = layout->border->blocks->value(i);
QImage metatile_image = Tileset::getMetatileImage(block.tile, layout->tileset_primary, layout->tileset_secondary);
2016-09-07 04:50:47 +01:00
int map_y = i / width_;
int map_x = i % width_;
painter.drawImage(QPoint(map_x * 16, map_y * 16), metatile_image);
}
painter.end();
if (changed_any) {
cacheBorder();
2018-06-20 23:43:20 +01:00
layout->border_pixmap = layout->border_pixmap.fromImage(layout->border_image);
2016-09-07 04:50:47 +01:00
}
2018-06-20 23:43:20 +01:00
return layout->border_pixmap;
2016-09-07 04:50:47 +01:00
}
QPixmap Map::renderConnection(MapConnection connection) {
2016-09-07 04:50:47 +01:00
render();
int x, y, w, h;
if (connection.direction == "up") {
x = 0;
y = getHeight() - 6;
w = getWidth();
h = 6;
} else if (connection.direction == "down") {
x = 0;
y = 0;
w = getWidth();
h = 6;
} else if (connection.direction == "left") {
x = getWidth() - 6;
y = 0;
w = 6;
h = getHeight();
} else if (connection.direction == "right") {
x = 0;
y = 0;
w = 6;
h = getHeight();
} else {
// this should not happen
x = 0;
y = 0;
w = getWidth();
h = getHeight();
}
QImage connection_image = image.copy(x * 16, y * 16, w * 16, h * 16);
//connection_image = connection_image.convertToFormat(QImage::Format_Grayscale8);
return QPixmap::fromImage(connection_image);
}
2018-07-20 04:27:40 +01:00
void Map::drawSelection(int i, int w, int selectionWidth, int selectionHeight, QPainter *painter, int gridWidth) {
2016-09-07 04:50:47 +01:00
int x = i % w;
int y = i / w;
painter->save();
2018-03-06 00:47:00 +00:00
QColor penColor = QColor(0xff, 0xff, 0xff);
2018-03-06 00:47:00 +00:00
painter->setPen(penColor);
2018-07-20 04:27:40 +01:00
int rectWidth = selectionWidth * gridWidth;
int rectHeight = selectionHeight * gridWidth;
painter->drawRect(x * gridWidth, y * gridWidth, rectWidth - 1, rectHeight -1);
2016-09-07 04:50:47 +01:00
painter->setPen(QColor(0, 0, 0));
2018-07-20 04:27:40 +01:00
painter->drawRect(x * gridWidth - 1, y * gridWidth - 1, rectWidth + 1, rectHeight + 1);
painter->drawRect(x * gridWidth + 1, y * gridWidth + 1, rectWidth - 3, rectHeight - 3);
2016-09-07 04:50:47 +01:00
painter->restore();
}
void Map::setNewDimensionsBlockdata(int newWidth, int newHeight) {
2018-07-09 23:40:28 +01:00
int oldWidth = getWidth();
int oldHeight = getHeight();
Blockdata* newBlockData = new Blockdata;
for (int y = 0; y < newHeight; y++)
for (int x = 0; x < newWidth; x++) {
if (x < oldWidth && y < oldHeight) {
int index = y * oldWidth + x;
newBlockData->addBlock(layout->blockdata->blocks->value(index));
} else {
newBlockData->addBlock(0);
}
}
layout->blockdata->copyFrom(newBlockData);
}
void Map::setDimensions(int newWidth, int newHeight, bool setNewBlockdata) {
if (setNewBlockdata) {
setNewDimensionsBlockdata(newWidth, newHeight);
}
2018-07-09 23:40:28 +01:00
layout->width = QString::number(newWidth);
layout->height = QString::number(newHeight);
emit mapChanged(this);
}
2016-09-07 04:50:47 +01:00
Block* Map::getBlock(int x, int y) {
2018-06-20 23:43:20 +01:00
if (layout->blockdata && layout->blockdata->blocks) {
2018-09-15 00:37:36 +01:00
if (x >= 0 && x < getWidth() && y >= 0 && y < getHeight()) {
2017-11-28 04:46:27 +00:00
int i = y * getWidth() + x;
2018-06-20 23:43:20 +01:00
return new Block(layout->blockdata->blocks->value(i));
2017-11-28 04:46:27 +00:00
}
2016-09-07 04:50:47 +01:00
}
2018-09-15 00:37:36 +01:00
return nullptr;
2016-09-07 04:50:47 +01:00
}
void Map::_setBlock(int x, int y, Block block) {
int i = y * getWidth() + x;
2018-06-20 23:43:20 +01:00
if (layout->blockdata && layout->blockdata->blocks) {
layout->blockdata->blocks->replace(i, block);
2017-11-28 04:46:27 +00:00
}
2016-09-07 04:50:47 +01:00
}
2018-09-15 00:37:36 +01:00
void Map::_floodFillCollisionElevation(int x, int y, uint16_t collision, uint16_t elevation) {
2016-09-07 04:50:47 +01:00
QList<QPoint> todo;
todo.append(QPoint(x, y));
while (todo.length()) {
QPoint point = todo.takeAt(0);
x = point.x();
y = point.y();
Block *block = getBlock(x, y);
2018-09-15 00:37:36 +01:00
if (!block) {
2016-09-07 04:50:47 +01:00
continue;
}
uint old_coll = block->collision;
uint old_elev = block->elevation;
if (old_coll == collision && old_elev == elevation) {
continue;
}
2018-09-15 00:37:36 +01:00
2016-09-07 04:50:47 +01:00
block->collision = collision;
block->elevation = elevation;
_setBlock(x, y, *block);
if ((block = getBlock(x + 1, y)) && block->collision == old_coll && block->elevation == old_elev) {
todo.append(QPoint(x + 1, y));
}
if ((block = getBlock(x - 1, y)) && block->collision == old_coll && block->elevation == old_elev) {
todo.append(QPoint(x - 1, y));
}
if ((block = getBlock(x, y + 1)) && block->collision == old_coll && block->elevation == old_elev) {
todo.append(QPoint(x, y + 1));
}
if ((block = getBlock(x, y - 1)) && block->collision == old_coll && block->elevation == old_elev) {
todo.append(QPoint(x, y - 1));
}
}
}
void Map::undo() {
HistoryItem *commit = history.back();
if (!commit)
return;
2018-06-20 23:43:20 +01:00
if (layout->blockdata) {
layout->blockdata->copyFrom(commit->metatiles);
if (commit->layoutWidth != this->getWidth() || commit->layoutHeight != this->getHeight())
{
this->setDimensions(commit->layoutWidth, commit->layoutHeight, false);
2018-09-15 00:37:36 +01:00
emit mapNeedsRedrawing();
2017-11-28 04:46:27 +00:00
}
emit mapChanged(this);
2016-09-07 04:50:47 +01:00
}
}
void Map::redo() {
HistoryItem *commit = history.next();
if (!commit)
return;
2018-06-20 23:43:20 +01:00
if (layout->blockdata) {
layout->blockdata->copyFrom(commit->metatiles);
if (commit->layoutWidth != this->getWidth() || commit->layoutHeight != this->getHeight())
{
this->setDimensions(commit->layoutWidth, commit->layoutHeight, false);
2018-09-15 00:37:36 +01:00
emit mapNeedsRedrawing();
2017-11-28 04:46:27 +00:00
}
emit mapChanged(this);
2016-09-07 04:50:47 +01:00
}
}
void Map::commit() {
2018-06-20 23:43:20 +01:00
if (layout->blockdata) {
HistoryItem *item = history.current();
bool atCurrentHistory = item
&& layout->blockdata->equals(item->metatiles)
&& this->getWidth() == item->layoutWidth
&& this->getHeight() == item->layoutHeight;
if (!atCurrentHistory) {
HistoryItem *commit = new HistoryItem(layout->blockdata->copy(), this->getWidth(), this->getHeight());
2017-11-28 04:46:27 +00:00
history.push(commit);
emit mapChanged(this);
}
}
2016-09-07 04:50:47 +01:00
}
void Map::setBlock(int x, int y, Block block) {
Block *old_block = getBlock(x, y);
if (old_block && (*old_block) != block) {
_setBlock(x, y, block);
commit();
}
}
2018-09-15 00:37:36 +01:00
void Map::floodFillCollisionElevation(int x, int y, uint16_t collision, uint16_t elevation) {
2016-09-07 04:50:47 +01:00
Block *block = getBlock(x, y);
if (block && (block->collision != collision || block->elevation != elevation)) {
_floodFillCollisionElevation(x, y, collision, elevation);
commit();
}
}
2017-11-28 04:46:27 +00:00
QList<Event *> Map::getAllEvents() {
QList<Event*> all;
for (QList<Event*> list : events.values()) {
all += list;
}
return all;
}
void Map::removeEvent(Event *event) {
for (QString key : events.keys()) {
events[key].removeAll(event);
}
}
void Map::addEvent(Event *event) {
2018-07-07 16:58:25 +01:00
events[event->get("event_group_type")].append(event);
2017-11-28 04:46:27 +00:00
}
bool Map::hasUnsavedChanges() {
2018-06-20 23:43:20 +01:00
return !history.isSaved() || !isPersistedToFile || layout->has_unsaved_changes;
2017-11-28 04:46:27 +00:00
}
void Map::hoveredTileChanged(int x, int y, int block) {
emit statusBarMessage(QString("X: %1, Y: %2, Metatile: 0x%3, Scale = %4x")
.arg(x)
.arg(y)
.arg(QString("%1").arg(block, 3, 16, QChar('0')).toUpper())
.arg(QString::number(pow(this->scale_base,this->scale_exp))));
}
void Map::clearHoveredTile() {
emit statusBarMessage(QString(""));
}
void Map::hoveredMetatileSelectionChanged(uint16_t metatileId) {
2018-07-20 04:44:55 +01:00
emit statusBarMessage(QString("Metatile: 0x%1")
.arg(QString("%1").arg(metatileId, 3, 16, QChar('0')).toUpper()));
}
void Map::clearHoveredMetatileSelection() {
emit statusBarMessage(QString(""));
}
2018-07-20 04:27:40 +01:00
void Map::hoveredMovementPermissionTileChanged(int collision, int elevation) {
QString message;
if (collision == 0 && elevation == 0) {
message = "Collision: Transition between elevations";
} else if (collision == 0 && elevation == 15) {
message = "Collision: Multi-Level (Bridge)";
} else if (collision == 0 && elevation == 1) {
message = "Collision: Surf";
} else if (collision == 0) {
message = QString("Collision: Passable, Elevation: %1").arg(elevation);
} else {
message = QString("Collision: Impassable, Elevation: %1").arg(elevation);
}
emit statusBarMessage(message);
}
2018-07-20 04:27:40 +01:00
void Map::clearHoveredMovementPermissionTile() {
emit statusBarMessage(QString(""));
}