porymap/src/core/metatile.cpp

194 lines
7.8 KiB
C++
Raw Normal View History

2018-09-25 01:12:29 +01:00
#include "metatile.h"
#include "tileset.h"
#include "project.h"
2022-10-26 09:13:53 +01:00
const QHash<QString, MetatileAttr> Metatile::defaultLayoutFRLG = {
{"behavior", MetatileAttr(0x000001FF, 0) },
{"terrainType", MetatileAttr(0x00003E00, 9) },
{"encounterType", MetatileAttr(0x07000000, 24) },
{"layerType", MetatileAttr(0x60000000, 29) },
};
2022-10-26 09:13:53 +01:00
const QHash<QString, MetatileAttr> Metatile::defaultLayoutRSE = {
{"behavior", MetatileAttr(0x00FF, 0) },
{"terrainType", MetatileAttr() },
{"encounterType", MetatileAttr() },
{"layerType", MetatileAttr(0xF000, 12) },
2022-10-26 05:14:55 +01:00
};
2022-10-26 09:13:53 +01:00
const QHash<BaseGameVersion, const QHash<QString, MetatileAttr>*> Metatile::defaultLayouts = {
{ BaseGameVersion::pokeruby, &defaultLayoutRSE },
{ BaseGameVersion::pokefirered, &defaultLayoutFRLG },
{ BaseGameVersion::pokeemerald, &defaultLayoutRSE },
2022-10-26 05:14:55 +01:00
};
2022-10-26 09:13:53 +01:00
MetatileAttr Metatile::behaviorAttr;
MetatileAttr Metatile::terrainTypeAttr;
MetatileAttr Metatile::encounterTypeAttr;
MetatileAttr Metatile::layerTypeAttr;
uint32_t Metatile::unusedAttrMask = 0;
MetatileAttr::MetatileAttr() :
mask(0),
shift(0)
{ }
MetatileAttr::MetatileAttr(uint32_t mask, int shift) :
mask(mask),
shift(shift)
{ }
2021-02-16 17:14:27 +00:00
Metatile::Metatile() :
behavior(0),
2022-02-03 23:10:50 +00:00
terrainType(0),
encounterType(0),
layerType(0),
2022-02-03 23:10:50 +00:00
unusedAttributes(0)
2021-02-16 17:14:27 +00:00
{ }
Metatile::Metatile(const int numTiles) :
behavior(0),
terrainType(0),
encounterType(0),
layerType(0),
unusedAttributes(0)
{
Tile tile = Tile();
for (int i = 0; i < numTiles; i++) {
this->tiles.append(tile);
}
}
int Metatile::getIndexInTileset(int metatileId) {
if (metatileId < Project::getNumMetatilesPrimary()) {
return metatileId;
} else {
return metatileId - Project::getNumMetatilesPrimary();
}
}
QPoint Metatile::coordFromPixmapCoord(const QPointF &pixelCoord) {
int x = static_cast<int>(pixelCoord.x()) / 16;
int y = static_cast<int>(pixelCoord.y()) / 16;
return QPoint(x, y);
}
2022-02-03 23:10:50 +00:00
2022-10-26 09:13:53 +01:00
// Set the layout of a metatile attribute using the mask read from the config file
void Metatile::setCustomAttributeLayout(MetatileAttr * attr, uint32_t mask, uint32_t max) {
2022-10-26 05:14:55 +01:00
if (mask > max) {
uint32_t oldMask = mask;
2022-10-26 05:14:55 +01:00
mask &= max;
logWarn(QString("Metatile attribute mask '0x%1' has been truncated to '0x%2'")
.arg(QString::number(oldMask, 16).toUpper())
.arg(QString::number(mask, 16).toUpper()));
2022-10-26 05:14:55 +01:00
}
2022-10-26 09:13:53 +01:00
attr->mask = mask;
attr->shift = mask ? log2(mask & ~(mask - 1)) : 0; // Get position of the least significant set bit
2022-02-03 23:10:50 +00:00
}
2022-10-26 09:13:53 +01:00
// For checking whether a metatile attribute mask can contain all the available hard-coded options
bool Metatile::isMaskTooSmall(MetatileAttr * attr, int max) {
if (attr->mask == 0 || max <= 0) return false;
// Get position of the most significant set bit
uint32_t n = log2(max);
// Get a mask for all values 0 to max.
// This may fail for n > 30, but that's not possible here.
2022-10-26 09:13:53 +01:00
uint32_t rangeMask = (1 << (n + 1)) - 1;
return attr->getClamped(rangeMask) != rangeMask;
}
2022-10-26 05:14:55 +01:00
bool Metatile::doMasksOverlap(QList<uint32_t> masks) {
for (int i = 0; i < masks.length(); i++)
for (int j = i + 1; j < masks.length(); j++) {
if (masks.at(i) & masks.at(j))
return true;
}
return false;
}
void Metatile::setCustomLayout(Project * project) {
// Get the maximum size of any attribute mask
const QHash<int, uint32_t> maxMasks = {
{1, 0xFF},
{2, 0xFFFF},
{4, 0xFFFFFFFF},
};
const uint32_t maxMask = maxMasks.value(projectConfig.getMetatileAttributesSize(), 0);
2022-10-26 05:14:55 +01:00
// Set custom attribute masks from the config file
2022-10-26 09:13:53 +01:00
setCustomAttributeLayout(&Metatile::behaviorAttr, projectConfig.getMetatileBehaviorMask(), maxMask);
setCustomAttributeLayout(&Metatile::terrainTypeAttr, projectConfig.getMetatileTerrainTypeMask(), maxMask);
setCustomAttributeLayout(&Metatile::encounterTypeAttr, projectConfig.getMetatileEncounterTypeMask(), maxMask);
setCustomAttributeLayout(&Metatile::layerTypeAttr, projectConfig.getMetatileLayerTypeMask(), maxMask);
2022-10-26 05:14:55 +01:00
// Set mask for preserving any attribute bits not used by Porymap
2022-10-26 09:13:53 +01:00
Metatile::unusedAttrMask = ~(getBehaviorMask() | getTerrainTypeMask() | getEncounterTypeMask() | getLayerTypeMask());
Metatile::unusedAttrMask &= maxMask;
// Overlapping masks are technically ok, but probably not intended.
// Additionally, Porymap will not properly reflect that the values are linked.
2022-10-26 09:13:53 +01:00
if (doMasksOverlap({getBehaviorMask(), getTerrainTypeMask(), getEncounterTypeMask(), getLayerTypeMask()})) {
logWarn("Metatile attribute masks are overlapping. This may result in unexpected attribute values.");
}
// Warn the user if they have set a nonzero mask that is too small to contain its available options.
// They'll be allowed to select the options, but they'll be truncated to a different value when revisited.
if (!project->metatileBehaviorMapInverse.isEmpty()) {
int maxBehavior = project->metatileBehaviorMapInverse.lastKey();
if (isMaskTooSmall(&Metatile::behaviorAttr, maxBehavior))
logWarn(QString("Metatile Behavior mask is too small to contain all %1 available options.").arg(maxBehavior));
}
2022-10-26 09:13:53 +01:00
if (isMaskTooSmall(&Metatile::terrainTypeAttr, NUM_METATILE_TERRAIN_TYPES - 1))
logWarn(QString("Metatile Terrain Type mask is too small to contain all %1 available options.").arg(NUM_METATILE_TERRAIN_TYPES));
2022-10-26 09:13:53 +01:00
if (isMaskTooSmall(&Metatile::encounterTypeAttr, NUM_METATILE_ENCOUNTER_TYPES - 1))
logWarn(QString("Metatile Encounter Type mask is too small to contain all %1 available options.").arg(NUM_METATILE_ENCOUNTER_TYPES));
2022-10-26 09:13:53 +01:00
if (isMaskTooSmall(&Metatile::layerTypeAttr, NUM_METATILE_LAYER_TYPES - 1))
logWarn(QString("Metatile Layer Type mask is too small to contain all %1 available options.").arg(NUM_METATILE_LAYER_TYPES));
}
uint32_t Metatile::getAttributes() {
uint32_t attributes = this->unusedAttributes & Metatile::unusedAttrMask;
2022-10-26 09:13:53 +01:00
attributes |= Metatile::behaviorAttr.toRaw(this->behavior);
attributes |= Metatile::terrainTypeAttr.toRaw(this->terrainType);
attributes |= Metatile::encounterTypeAttr.toRaw(this->encounterType);
attributes |= Metatile::layerTypeAttr.toRaw(this->layerType);
2022-02-03 23:10:50 +00:00
return attributes;
}
2022-10-26 05:14:55 +01:00
void Metatile::setAttributes(uint32_t data) {
2022-10-26 09:13:53 +01:00
this->behavior = Metatile::behaviorAttr.fromRaw(data);
this->terrainType = Metatile::terrainTypeAttr.fromRaw(data);
this->encounterType = Metatile::encounterTypeAttr.fromRaw(data);
this->layerType = Metatile::layerTypeAttr.fromRaw(data);
this->unusedAttributes = data & Metatile::unusedAttrMask;
}
2022-10-26 05:14:55 +01:00
// Read attributes using a vanilla layout, then set them using the user's layout. For AdvanceMap import
2022-10-26 09:13:53 +01:00
void Metatile::setAttributes(uint32_t data, BaseGameVersion version) {
const auto defaultLayout = Metatile::defaultLayouts.value(version);
this->setBehavior(defaultLayout->value("behavior").fromRaw(data));
this->setTerrainType(defaultLayout->value("terrainType").fromRaw(data));
this->setEncounterType(defaultLayout->value("encounterType").fromRaw(data));
this->setLayerType(defaultLayout->value("layerType").fromRaw(data));
2022-02-03 23:10:50 +00:00
}
2022-10-26 09:13:53 +01:00
int Metatile::getDefaultAttributesSize(BaseGameVersion version) {
return (version == BaseGameVersion::pokefirered) ? 4 : 2;
}
2022-10-26 09:13:53 +01:00
uint32_t Metatile::getBehaviorMask(BaseGameVersion version) {
return Metatile::defaultLayouts.value(version)->value("behavior").mask;
}
2022-10-26 09:13:53 +01:00
uint32_t Metatile::getTerrainTypeMask(BaseGameVersion version) {
return Metatile::defaultLayouts.value(version)->value("terrainType").mask;
}
2022-10-26 09:13:53 +01:00
uint32_t Metatile::getEncounterTypeMask(BaseGameVersion version) {
return Metatile::defaultLayouts.value(version)->value("encounterType").mask;
}
uint32_t Metatile::getLayerTypeMask(BaseGameVersion version) {
return Metatile::defaultLayouts.value(version)->value("layerType").mask;
}