Combine attribute shift/mask data
This commit is contained in:
parent
fa2b4d3edb
commit
1641ac00b0
5 changed files with 123 additions and 107 deletions
|
@ -47,27 +47,37 @@ public:
|
|||
uint32_t unusedAttributes;
|
||||
QString label;
|
||||
|
||||
enum Attr {
|
||||
Behavior,
|
||||
TerrainType,
|
||||
EncounterType,
|
||||
LayerType,
|
||||
};
|
||||
|
||||
struct AttrLayout {
|
||||
uint32_t mask;
|
||||
int shift;
|
||||
};
|
||||
|
||||
static const QHash<Metatile::Attr, Metatile::AttrLayout> defaultLayoutFRLG;
|
||||
static const QHash<Metatile::Attr, Metatile::AttrLayout> defaultLayoutRSE;
|
||||
|
||||
uint32_t getAttributes();
|
||||
void setAttributes(uint32_t data);
|
||||
void setAttributes(uint32_t data, BaseGameVersion version);
|
||||
void convertAttributes(uint32_t data, BaseGameVersion version);
|
||||
|
||||
static int getIndexInTileset(int);
|
||||
static QPoint coordFromPixmapCoord(const QPointF &pixelCoord);
|
||||
static int getAttributesSize(BaseGameVersion version);
|
||||
static void calculateAttributeLayout();
|
||||
static int getDefaultAttributesSize(BaseGameVersion version);
|
||||
static void setCustomLayout();
|
||||
|
||||
private:
|
||||
static uint32_t behaviorMask;
|
||||
static uint32_t terrainTypeMask;
|
||||
static uint32_t encounterTypeMask;
|
||||
static uint32_t layerTypeMask;
|
||||
static QHash<Metatile::Attr, Metatile::AttrLayout> customLayout;
|
||||
static uint32_t unusedAttrMask;
|
||||
static int behaviorShift;
|
||||
static int terrainTypeShift;
|
||||
static int encounterTypeShift;
|
||||
static int layerTypeShift;
|
||||
|
||||
static int getShiftValue(uint32_t mask);
|
||||
void setAttributes(uint32_t, const QHash<Metatile::Attr, Metatile::AttrLayout>*);
|
||||
static void setCustomAttributeLayout(Metatile::AttrLayout *, uint32_t, uint32_t, QString);
|
||||
static bool doMasksOverlap(QList<uint32_t>);
|
||||
};
|
||||
|
||||
inline bool operator==(const Metatile &a, const Metatile &b) {
|
||||
|
|
|
@ -563,7 +563,7 @@ void ProjectConfig::parseConfigKeyValue(QString key, QString value) {
|
|||
int size = getConfigInteger(key, value, 1, 4, 2);
|
||||
if (size & (size - 1)) {
|
||||
logWarn(QString("Invalid config value for %1: must be 1, 2, or 4").arg(key));
|
||||
size = 2;
|
||||
return; // Don't set a default now, it will be set later based on the base game version
|
||||
}
|
||||
this->metatileAttributesSize = size;
|
||||
} else if (key == "metatile_behavior_mask") {
|
||||
|
@ -624,11 +624,12 @@ void ProjectConfig::setUnreadKeys() {
|
|||
if (!readKeys.contains("create_map_text_file")) this->createMapTextFile = (this->baseGameVersion != BaseGameVersion::pokeemerald);
|
||||
if (!readKeys.contains("new_map_border_metatiles")) this->newMapBorderMetatileIds = isPokefirered ? DEFAULT_BORDER_FRLG : DEFAULT_BORDER_RSE;
|
||||
if (!readKeys.contains("default_secondary_tileset")) this->defaultSecondaryTileset = isPokefirered ? "gTileset_PalletTown" : "gTileset_Petalburg";
|
||||
if (!readKeys.contains("metatile_attributes_size")) this->metatileAttributesSize = isPokefirered ? 4 : 2;
|
||||
if (!readKeys.contains("metatile_behavior_mask")) this->metatileBehaviorMask = isPokefirered ? 0x000001FF : 0x00FF;
|
||||
if (!readKeys.contains("metatile_terrain_type_mask")) this->metatileTerrainTypeMask = isPokefirered ? 0x00003E00 : 0;
|
||||
if (!readKeys.contains("metatile_encounter_type_mask")) this->metatileEncounterTypeMask = isPokefirered ? 0x07000000 : 0;
|
||||
if (!readKeys.contains("metatile_layer_type_mask")) this-> metatileLayerTypeMask = isPokefirered ? 0x60000000 : 0xF000;
|
||||
if (!readKeys.contains("metatile_attributes_size")) this->metatileAttributesSize = Metatile::getDefaultAttributesSize(this->baseGameVersion);
|
||||
const QHash<Metatile::Attr, Metatile::AttrLayout> layout = isPokefirered ? Metatile::defaultLayoutFRLG : Metatile::defaultLayoutRSE;
|
||||
if (!readKeys.contains("metatile_behavior_mask")) this->metatileBehaviorMask = layout[Metatile::Attr::Behavior].mask;
|
||||
if (!readKeys.contains("metatile_terrain_type_mask")) this->metatileTerrainTypeMask = layout[Metatile::Attr::TerrainType].mask;
|
||||
if (!readKeys.contains("metatile_encounter_type_mask")) this->metatileEncounterTypeMask = layout[Metatile::Attr::EncounterType].mask;
|
||||
if (!readKeys.contains("metatile_layer_type_mask")) this-> metatileLayerTypeMask = layout[Metatile::Attr::LayerType].mask;
|
||||
}
|
||||
|
||||
QMap<QString, QString> ProjectConfig::getKeyValueMap() {
|
||||
|
|
|
@ -2,16 +2,22 @@
|
|||
#include "tileset.h"
|
||||
#include "project.h"
|
||||
|
||||
uint32_t Metatile::behaviorMask = 0;
|
||||
uint32_t Metatile::terrainTypeMask = 0;
|
||||
uint32_t Metatile::encounterTypeMask = 0;
|
||||
uint32_t Metatile::layerTypeMask = 0;
|
||||
QHash<Metatile::Attr, Metatile::AttrLayout> Metatile::customLayout = {};
|
||||
uint32_t Metatile::unusedAttrMask = 0;
|
||||
|
||||
int Metatile::behaviorShift = 0;
|
||||
int Metatile::terrainTypeShift = 0;
|
||||
int Metatile::encounterTypeShift = 0;
|
||||
int Metatile::layerTypeShift = 0;
|
||||
const QHash<Metatile::Attr, Metatile::AttrLayout> Metatile::defaultLayoutFRLG = {
|
||||
{Metatile::Attr::Behavior, { .mask = 0x000001FF, .shift = 0} },
|
||||
{Metatile::Attr::TerrainType, { .mask = 0x00003E00, .shift = 9} },
|
||||
{Metatile::Attr::EncounterType, { .mask = 0x07000000, .shift = 24} },
|
||||
{Metatile::Attr::LayerType, { .mask = 0x60000000, .shift = 29} },
|
||||
};
|
||||
|
||||
const QHash<Metatile::Attr, Metatile::AttrLayout> Metatile::defaultLayoutRSE = {
|
||||
{Metatile::Attr::Behavior, { .mask = 0x000000FF, .shift = 0} },
|
||||
{Metatile::Attr::TerrainType, { .mask = 0x00000000, .shift = 0} },
|
||||
{Metatile::Attr::EncounterType, { .mask = 0x00000000, .shift = 0} },
|
||||
{Metatile::Attr::LayerType, { .mask = 0x0000F000, .shift = 12} },
|
||||
};
|
||||
|
||||
Metatile::Metatile() :
|
||||
behavior(0),
|
||||
|
@ -42,19 +48,37 @@ int Metatile::getIndexInTileset(int metatileId) {
|
|||
}
|
||||
}
|
||||
|
||||
// Get the vanilla attribute sizes based on version.
|
||||
// Used as a default in the config and for AdvanceMap import.
|
||||
int Metatile::getDefaultAttributesSize(BaseGameVersion version) {
|
||||
return (version == BaseGameVersion::pokefirered) ? 4 : 2;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// Returns the position of the rightmost set bit
|
||||
int Metatile::getShiftValue(uint32_t mask) {
|
||||
return log2(mask & ~(mask - 1));;
|
||||
void Metatile::setCustomAttributeLayout(Metatile::AttrLayout * layout, uint32_t mask, uint32_t max, QString errName) {
|
||||
if (mask > max) {
|
||||
logWarn(QString("Metatile %1 mask '%2' exceeds maximum size '%3'").arg(errName).arg(mask).arg(max));
|
||||
mask &= max;
|
||||
}
|
||||
layout->mask = mask;
|
||||
layout->shift = log2(mask & ~(mask - 1)); // Get the position of the rightmost set bit
|
||||
}
|
||||
|
||||
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::calculateAttributeLayout() {
|
||||
void Metatile::setCustomLayout() {
|
||||
// Get the maximum size of any attribute mask
|
||||
const QHash<int, uint32_t> maxMasks = {
|
||||
{1, 0xFF},
|
||||
|
@ -63,100 +87,81 @@ void Metatile::calculateAttributeLayout() {
|
|||
};
|
||||
const uint32_t maxMask = maxMasks.value(projectConfig.getMetatileAttributesSize(), 0);
|
||||
|
||||
// Behavior
|
||||
uint32_t mask = projectConfig.getMetatileBehaviorMask();
|
||||
if (mask > maxMask) {
|
||||
logWarn(QString("Metatile behavior mask '%1' exceeds maximum size '%2'").arg(mask).arg(maxMask));
|
||||
mask &= maxMask;
|
||||
}
|
||||
Metatile::behaviorMask = mask;
|
||||
Metatile::behaviorShift = getShiftValue(mask);
|
||||
// Set custom attribute masks from the config file
|
||||
setCustomAttributeLayout(&customLayout[Attr::Behavior], projectConfig.getMetatileBehaviorMask(), maxMask, "behavior");
|
||||
setCustomAttributeLayout(&customLayout[Attr::TerrainType], projectConfig.getMetatileTerrainTypeMask(), maxMask, "terrain type");
|
||||
setCustomAttributeLayout(&customLayout[Attr::EncounterType], projectConfig.getMetatileEncounterTypeMask(), maxMask, "encounter type");
|
||||
setCustomAttributeLayout(&customLayout[Attr::LayerType], projectConfig.getMetatileLayerTypeMask(), maxMask, "layer type");
|
||||
|
||||
// Terrain Type
|
||||
mask = projectConfig.getMetatileTerrainTypeMask();
|
||||
if (mask > maxMask) {
|
||||
logWarn(QString("Metatile terrain type mask '%1' exceeds maximum size '%2'").arg(mask).arg(maxMask));
|
||||
mask &= maxMask;
|
||||
}
|
||||
Metatile::terrainTypeMask = mask;
|
||||
Metatile::terrainTypeShift = getShiftValue(mask);
|
||||
|
||||
// Encounter Type
|
||||
mask = projectConfig.getMetatileEncounterTypeMask();
|
||||
if (mask > maxMask) {
|
||||
logWarn(QString("Metatile encounter type mask '%1' exceeds maximum size '%2'").arg(mask).arg(maxMask));
|
||||
mask &= maxMask;
|
||||
}
|
||||
Metatile::encounterTypeMask = mask;
|
||||
Metatile::encounterTypeShift = getShiftValue(mask);
|
||||
|
||||
// Layer Type
|
||||
mask = projectConfig.getMetatileLayerTypeMask();
|
||||
if (mask > maxMask) {
|
||||
logWarn(QString("Metatile layer type mask '%1' exceeds maximum size '%2'").arg(mask).arg(maxMask));
|
||||
mask &= maxMask;
|
||||
}
|
||||
Metatile::layerTypeMask = mask;
|
||||
Metatile::layerTypeShift = getShiftValue(mask);
|
||||
|
||||
Metatile::unusedAttrMask = ~(Metatile::behaviorMask | Metatile::terrainTypeMask | Metatile::layerTypeMask | Metatile::encounterTypeMask);
|
||||
// Set mask for preserving any attribute bits not used by Porymap
|
||||
Metatile::unusedAttrMask = ~(customLayout[Attr::Behavior].mask
|
||||
| customLayout[Attr::TerrainType].mask
|
||||
| customLayout[Attr::EncounterType].mask
|
||||
| customLayout[Attr::LayerType].mask);
|
||||
Metatile::unusedAttrMask &= maxMask;
|
||||
|
||||
// Warn user if any mask overlaps
|
||||
if (Metatile::behaviorMask & Metatile::terrainTypeMask
|
||||
|| Metatile::behaviorMask & Metatile::encounterTypeMask
|
||||
|| Metatile::behaviorMask & Metatile::layerTypeMask
|
||||
|| Metatile::terrainTypeMask & Metatile::encounterTypeMask
|
||||
|| Metatile::terrainTypeMask & Metatile::layerTypeMask
|
||||
|| Metatile::encounterTypeMask & Metatile::layerTypeMask) {
|
||||
// Overlapping masks are legal, but probably not intended
|
||||
if (doMasksOverlap({customLayout[Attr::Behavior].mask,
|
||||
customLayout[Attr::TerrainType].mask,
|
||||
customLayout[Attr::EncounterType].mask,
|
||||
customLayout[Attr::LayerType].mask})) {
|
||||
logWarn("Metatile attribute masks are overlapping.");
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t Metatile::getAttributes() {
|
||||
uint32_t attributes = this->unusedAttributes & Metatile::unusedAttrMask;
|
||||
attributes |= (behavior << Metatile::behaviorShift) & Metatile::behaviorMask;
|
||||
attributes |= (terrainType << Metatile::terrainTypeShift) & Metatile::terrainTypeMask;
|
||||
attributes |= (encounterType << Metatile::encounterTypeShift) & Metatile::encounterTypeMask;
|
||||
attributes |= (layerType << Metatile::layerTypeShift) & Metatile::layerTypeMask;
|
||||
|
||||
// Behavior
|
||||
Metatile::AttrLayout attr = Metatile::customLayout[Attr::Behavior];
|
||||
attributes |= (this->behavior << attr.shift) & attr.mask;
|
||||
|
||||
// Terrain Type
|
||||
attr = Metatile::customLayout[Attr::TerrainType];
|
||||
attributes |= (this->terrainType << attr.shift) & attr.mask;
|
||||
|
||||
// Encounter Type
|
||||
attr = Metatile::customLayout[Attr::EncounterType];
|
||||
attributes |= (this->encounterType << attr.shift) & attr.mask;
|
||||
|
||||
// Layer Type
|
||||
attr = Metatile::customLayout[Attr::LayerType];
|
||||
attributes |= (this->layerType << attr.shift) & attr.mask;
|
||||
|
||||
return attributes;
|
||||
}
|
||||
|
||||
void Metatile::setAttributes(uint32_t data) {
|
||||
this->behavior = (data & Metatile::behaviorMask) >> Metatile::behaviorShift;
|
||||
this->terrainType = (data & Metatile::terrainTypeMask) >> Metatile::terrainTypeShift;
|
||||
this->encounterType = (data & Metatile::encounterTypeMask) >> Metatile::encounterTypeShift;
|
||||
this->layerType = (data & Metatile::layerTypeMask) >> Metatile::layerTypeShift;
|
||||
void Metatile::setAttributes(uint32_t data, const QHash<Metatile::Attr, Metatile::AttrLayout> * layout) {
|
||||
// Behavior
|
||||
Metatile::AttrLayout attr = layout->value(Attr::Behavior);
|
||||
this->behavior = (data & attr.mask) >> attr.shift;
|
||||
|
||||
// Terrain Type
|
||||
attr = layout->value(Attr::TerrainType);
|
||||
this->terrainType = (data & attr.mask) >> attr.shift;
|
||||
|
||||
// Encounter Type
|
||||
attr = layout->value(Attr::EncounterType);
|
||||
this->encounterType = (data & attr.mask) >> attr.shift;
|
||||
|
||||
// Layer Type
|
||||
attr = layout->value(Attr::LayerType);
|
||||
this->layerType = (data & attr.mask) >> attr.shift;
|
||||
|
||||
this->unusedAttributes = data & Metatile::unusedAttrMask;
|
||||
}
|
||||
|
||||
// Get the vanilla attribute sizes based on version. For AdvanceMap import
|
||||
int Metatile::getAttributesSize(BaseGameVersion version) {
|
||||
return (version == BaseGameVersion::pokefirered) ? 4 : 2;
|
||||
void Metatile::setAttributes(uint32_t data) {
|
||||
this->setAttributes(data, &Metatile::customLayout);
|
||||
}
|
||||
|
||||
// Set the attributes using the vanilla layout based on version. For AdvanceMap import
|
||||
void Metatile::setAttributes(uint32_t data, BaseGameVersion version) {
|
||||
// Read attributes using a vanilla layout, then set them using the user's layout. For AdvanceMap import
|
||||
void Metatile::convertAttributes(uint32_t data, BaseGameVersion version) {
|
||||
if (version == BaseGameVersion::pokefirered) {
|
||||
const uint32_t behaviorMask = 0x000001FF;
|
||||
const uint32_t terrainTypeMask = 0x00003E00;
|
||||
const uint32_t encounterTypeMask = 0x07000000;
|
||||
const uint32_t layerTypeMask = 0x60000000;
|
||||
|
||||
this->behavior = data & behaviorMask;
|
||||
this->terrainType = (data & terrainTypeMask) >> 9;
|
||||
this->encounterType = (data & encounterTypeMask) >> 24;
|
||||
this->layerType = (data & layerTypeMask) >> 29;
|
||||
this->unusedAttributes = data & ~(behaviorMask | terrainTypeMask | layerTypeMask | encounterTypeMask);
|
||||
this->setAttributes(data, &Metatile::defaultLayoutFRLG);
|
||||
} else {
|
||||
const uint16_t behaviorMask = 0x00FF;
|
||||
const uint16_t layerTypeMask = 0xF000;
|
||||
|
||||
this->behavior = data & behaviorMask;
|
||||
this->layerType = (data & layerTypeMask) >> 12;
|
||||
this->unusedAttributes = data & ~(behaviorMask | layerTypeMask);
|
||||
this->setAttributes(data, &Metatile::defaultLayoutRSE);
|
||||
}
|
||||
|
||||
// Clean data to fit the user's custom masks
|
||||
this->setAttributes(this->getAttributes());
|
||||
}
|
||||
|
|
|
@ -42,7 +42,7 @@ QList<Metatile*> MetatileParser::parse(QString filepath, bool *error, bool prima
|
|||
return { };
|
||||
}
|
||||
|
||||
int attrSize = Metatile::getAttributesSize(version);
|
||||
int attrSize = Metatile::getDefaultAttributesSize(version);
|
||||
int maxMetatiles = primaryTileset ? Project::getNumMetatilesPrimary() : Project::getNumMetatilesTotal() - Project::getNumMetatilesPrimary();
|
||||
int numMetatiles = static_cast<unsigned char>(in.at(0)) |
|
||||
(static_cast<unsigned char>(in.at(1)) << 8) |
|
||||
|
@ -82,7 +82,7 @@ QList<Metatile*> MetatileParser::parse(QString filepath, bool *error, bool prima
|
|||
uint32_t attributes = 0;
|
||||
for (int j = 0; j < attrSize; j++)
|
||||
attributes |= static_cast<unsigned char>(in.at(attrOffset + j)) << (8 * j);
|
||||
metatile->setAttributes(attributes, version);
|
||||
metatile->convertAttributes(attributes, version);
|
||||
metatile->tiles = tiles;
|
||||
metatiles.append(metatile);
|
||||
}
|
||||
|
|
|
@ -528,7 +528,7 @@ bool MainWindow::openProject(QString dir) {
|
|||
userConfig.load();
|
||||
projectConfig.setProjectDir(dir);
|
||||
projectConfig.load();
|
||||
Metatile::calculateAttributeLayout();
|
||||
Metatile::setCustomLayout();
|
||||
|
||||
this->closeSupplementaryWindows();
|
||||
this->setProjectSpecificUIVisibility();
|
||||
|
|
Loading…
Reference in a new issue