Skip to content

Commit

Permalink
Read/write MAPSEC values using the region map json
Browse files Browse the repository at this point in the history
  • Loading branch information
GriffinRichards committed Nov 8, 2024
1 parent 7d89031 commit d448765
Show file tree
Hide file tree
Showing 18 changed files with 141 additions and 239 deletions.
2 changes: 0 additions & 2 deletions docsrc/manual/project-files.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ The filepath that Porymap expects for each file can be overridden on the ``Files
include/constants/event_object_movement.h, yes, no, ``constants_obj_event_movement``,
include/constants/event_objects.h, yes, no, ``constants_obj_events``,
include/constants/event_bg.h, yes, no, ``constants_event_bg``,
include/constants/region_map_sections.h, yes, no, ``constants_region_map_sections``,
include/constants/metatile_labels.h, yes, yes, ``constants_metatile_labels``,
include/constants/metatile_behaviors.h, yes, no, ``constants_metatile_behaviors``,
include/constants/species.h, yes, no, ``constants_metatile_behaviors``, for the Wild Pokémon tab
Expand Down Expand Up @@ -122,7 +121,6 @@ In addition to these files, there are some specific symbol and macro names that
``define_map_empty``, ``UNDEFINED``, macro name after prefix for empty maps
``define_map_section_prefix``, ``MAPSEC_``, expected prefix for location macro names
``define_map_section_empty``, ``NONE``, macro name after prefix for empty region map sections
``define_map_section_count``, ``COUNT``, macro name after prefix for total number of region map sections
``define_species_prefix``, ``SPECIES_``, expected prefix for species macro names
``regex_behaviors``, ``\bMB_``, regex to find metatile behavior macro names
``regex_obj_event_gfx``, ``\bOBJ_EVENT_GFX_``, regex to find Object Event graphics ID macro names
Expand Down
2 changes: 0 additions & 2 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ enum ProjectIdentifier {
define_map_empty,
define_map_section_prefix,
define_map_section_empty,
define_map_section_count,
define_species_prefix,
regex_behaviors,
regex_obj_event_gfx,
Expand Down Expand Up @@ -269,7 +268,6 @@ enum ProjectFilePath {
constants_obj_event_movement,
constants_obj_events,
constants_event_bg,
constants_region_map_sections,
constants_metatile_labels,
constants_metatile_behaviors,
constants_species,
Expand Down
10 changes: 3 additions & 7 deletions include/core/regionmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ class RegionMap : public QObject
bool loadLayout(poryjson::Json);
bool loadEntries();

void setEntries(tsl::ordered_map<QString, MapSectionEntry> *entries) { this->region_map_entries = entries; }
void setEntries(tsl::ordered_map<QString, MapSectionEntry> entries) { *(this->region_map_entries) = entries; }
void setEntries(QMap<QString, MapSectionEntry> *entries) { this->region_map_entries = entries; }
void setEntries(const QMap<QString, MapSectionEntry> &entries) { *(this->region_map_entries) = entries; }
void clearEntries() { this->region_map_entries->clear(); }
MapSectionEntry getEntry(QString section);
void setEntry(QString section, MapSectionEntry entry);
Expand Down Expand Up @@ -114,8 +114,6 @@ class RegionMap : public QObject
void setLayer(QString layer) { this->current_layer = layer; }
QString getLayer() { return this->current_layer; }

QString fixCase(QString);

int padLeft() { return this->offset_left; }
int padTop() { return this->offset_top; }
int padRight() { return this->tilemap_width - this->layout_width - this->offset_left; }
Expand Down Expand Up @@ -149,14 +147,12 @@ class RegionMap : public QObject

const QString section_prefix;
const QString default_map_section;
const QString count_map_section;

signals:
void mapNeedsDisplaying();

private:
// TODO: defaults needed?
tsl::ordered_map<QString, MapSectionEntry> *region_map_entries = nullptr;
QMap<QString, MapSectionEntry> *region_map_entries = nullptr;

QString alias = "";

Expand Down
4 changes: 2 additions & 2 deletions include/core/regionmapeditcommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class ResizeTilemap : public EditTilemap {
/// ClearEntries
class ClearEntries : public QUndoCommand {
public:
ClearEntries(RegionMap *map, tsl::ordered_map<QString, MapSectionEntry>, QUndoCommand *parent = nullptr);
ClearEntries(RegionMap *map, QMap<QString, MapSectionEntry>, QUndoCommand *parent = nullptr);

void undo() override;
void redo() override;
Expand All @@ -163,7 +163,7 @@ class ClearEntries : public QUndoCommand {

private:
RegionMap *map;
tsl::ordered_map<QString, MapSectionEntry> entries;
QMap<QString, MapSectionEntry> entries;
};

#endif // REGIONMAPEDITCOMMANDS_H
2 changes: 1 addition & 1 deletion include/core/wildmoninfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
struct WildPokemon {
int minLevel = 5;
int maxLevel = 5;
QString species = "SPECIES_NONE";
QString species = "SPECIES_NONE"; // TODO: Use define_species_prefix
};

struct WildMonInfo {
Expand Down
13 changes: 5 additions & 8 deletions include/project.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ class Project : public QObject
QMap<QString, QString> layoutIdsToNames;
QMap<QString, Layout*> mapLayouts;
QMap<QString, Layout*> mapLayoutsMaster;
QMap<QString, QString> mapSecToMapHoverName;
QMap<QString, int> mapSectionNameToValue;
QMap<int, QString> mapSectionValueToName;
QMap<QString, EventGraphics*> eventGraphicsMap;
QMap<QString, int> gfxDefines;
QString defaultSong;
Expand All @@ -68,6 +65,8 @@ class Project : public QObject
QStringList bgEventFacingDirections;
QStringList trainerTypes;
QStringList globalScriptLabels;
QStringList mapSectionIdNames;
QMap<QString, MapSectionEntry> regionMapEntries;
QMap<QString, QMap<QString, uint16_t>> metatileLabelsMap;
QMap<QString, uint16_t> unusedMetatileLabels;
QMap<QString, uint32_t> metatileBehaviorMap;
Expand All @@ -82,9 +81,7 @@ class Project : public QObject
int pokemonMaxLevel;
int maxEncounterRate;
bool wildEncountersLoaded;

// For files that are read and could contain extra text
QMap<QString, QString> extraFileText;
bool saveEmptyMapsec;

void set_root(QString);

Expand Down Expand Up @@ -142,7 +139,7 @@ class Project : public QObject
bool readSpeciesIconPaths();
QMap<QString, QString> speciesToIconPath;

int appendMapsec(QString name);
void addNewMapsec(QString name);

bool hasUnsavedChanges();
bool hasUnsavedDataChanges = false;
Expand Down Expand Up @@ -172,7 +169,7 @@ class Project : public QObject
void saveConfig();
void saveMapLayouts();
void saveMapGroups();
void saveMapSections();
void saveRegionMapSections();
void saveWildMonData();
void saveMapConstantsHeader();
void saveHealLocations(Map*);
Expand Down
2 changes: 1 addition & 1 deletion include/ui/maplistmodels.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class MapAreaModel : public MapListModel {
public:
void setMap(QString mapName) { this->openMap = mapName; }

QStandardItem *createAreaItem(QString areaName, int areaIndex);
QStandardItem *createAreaItem(QString areaName);
QStandardItem *createMapItem(QString mapName, int areaIndex, int mapIndex);

QStandardItem *insertAreaItem(QString areaName);
Expand Down
3 changes: 1 addition & 2 deletions include/ui/regionmapeditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public slots:
tsl::ordered_map<QString, RegionMap *> region_maps;

QString configFilepath;
QString mapSectionFilepath;

poryjson::Json rmConfigJson;

Expand Down Expand Up @@ -96,7 +95,7 @@ public slots:
void saveConfig();
bool loadRegionMapEntries();
bool saveRegionMapEntries();
tsl::ordered_map<QString, MapSectionEntry> region_map_entries;
QMap<QString, MapSectionEntry> region_map_entries;

bool buildConfigDialog();
poryjson::Json configRegionMapDialog();
Expand Down
2 changes: 0 additions & 2 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ const QMap<ProjectIdentifier, QPair<QString, QString>> ProjectConfig::defaultIde
{ProjectIdentifier::define_map_empty, {"define_map_empty", "UNDEFINED"}},
{ProjectIdentifier::define_map_section_prefix, {"define_map_section_prefix", "MAPSEC_"}},
{ProjectIdentifier::define_map_section_empty, {"define_map_section_empty", "NONE"}},
{ProjectIdentifier::define_map_section_count, {"define_map_section_count", "COUNT"}},
{ProjectIdentifier::define_species_prefix, {"define_species_prefix", "SPECIES_"}},
// Regex
{ProjectIdentifier::regex_behaviors, {"regex_behaviors", "\\bMB_"}},
Expand Down Expand Up @@ -167,7 +166,6 @@ const QMap<ProjectFilePath, QPair<QString, QString>> ProjectConfig::defaultPaths
{ProjectFilePath::constants_obj_event_movement, { "constants_obj_event_movement", "include/constants/event_object_movement.h"}},
{ProjectFilePath::constants_obj_events, { "constants_obj_events", "include/constants/event_objects.h"}},
{ProjectFilePath::constants_event_bg, { "constants_event_bg", "include/constants/event_bg.h"}},
{ProjectFilePath::constants_region_map_sections, { "constants_region_map_sections", "include/constants/region_map_sections.h"}},
{ProjectFilePath::constants_metatile_labels, { "constants_metatile_labels", "include/constants/metatile_labels.h"}},
{ProjectFilePath::constants_metatile_behaviors, { "constants_metatile_behaviors", "include/constants/metatile_behaviors.h"}},
{ProjectFilePath::constants_species, { "constants_species", "include/constants/species.h"}},
Expand Down
37 changes: 6 additions & 31 deletions src/core/regionmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ using std::make_shared;

RegionMap::RegionMap(Project *project) :
section_prefix(projectConfig.getIdentifier(ProjectIdentifier::define_map_section_prefix)),
default_map_section(section_prefix + projectConfig.getIdentifier(ProjectIdentifier::define_map_section_empty)),
count_map_section(section_prefix + projectConfig.getIdentifier(ProjectIdentifier::define_map_section_count))
default_map_section(project->getEmptyMapsecName())
{
this->project = project;
}
Expand Down Expand Up @@ -157,7 +156,7 @@ bool RegionMap::loadLayout(poryjson::Json layoutJson) {
for (int x = 0; x < this->layout_width; x++) {
int bin_index = x + y * this->layout_width;
uint8_t square_section_id = mapBinData.at(bin_index);
QString square_section_name = project->mapSectionValueToName.value(square_section_id);
QString square_section_name = project->mapSectionIdNames.value(square_section_id, this->default_map_section);

LayoutSquare square;
square.map_section = square_section_name;
Expand Down Expand Up @@ -401,7 +400,7 @@ void RegionMap::saveLayout() {
for (int m = 0; m < this->layout_height; m++) {
for (int n = 0; n < this->layout_width; n++) {
int i = n + this->layout_width * m;
data.append(this->project->mapSectionNameToValue.value(this->layouts["main"][i].map_section));
data.append(this->project->mapSectionIdNames.indexOf(this->layouts["main"][i].map_section));
}
}
QFile bfile(fullPath(this->layout_path));
Expand Down Expand Up @@ -760,18 +759,15 @@ bool RegionMap::squareInLayout(int x, int y) {
}

MapSectionEntry RegionMap::getEntry(QString section) {
if (this->region_map_entries->contains(section))
return this->region_map_entries->operator[](section);
else
return MapSectionEntry();
return this->region_map_entries->value(section, MapSectionEntry());
}

void RegionMap::setEntry(QString section, MapSectionEntry entry) {
this->region_map_entries->operator[](section) = entry;
this->region_map_entries->insert(section, entry);
}

void RegionMap::removeEntry(QString section) {
this->region_map_entries->erase(section);
this->region_map_entries->remove(section);
}

QString RegionMap::palPath() {
Expand All @@ -788,27 +784,6 @@ int RegionMap::getMapSquareIndex(int x, int y) {
return ((index < tilemap.length()) && (index >= 0)) ? index : 0;
}

// For turning a MAPSEC_NAME into a unique identifier sMapName-style variable.
// CAPS_WITH_UNDERSCORE to CamelCase
QString RegionMap::fixCase(QString caps) {
bool big = true;
QString camel;

static const QRegularExpression re_braced("({.*})");
for (auto ch : caps.remove(re_braced).remove(this->section_prefix)) {
if (ch == '_' || ch == ' ') {
big = true;
continue;
}
if (big) {
camel += ch.toUpper();
big = false;
}
else camel += ch.toLower();
}
return camel;
}

QString RegionMap::fullPath(QString local) {
return this->project->root + "/" + local;
}
2 changes: 1 addition & 1 deletion src/core/regionmapeditcommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ void ResizeTilemap::undo() {

///

ClearEntries::ClearEntries(RegionMap *map, tsl::ordered_map<QString, MapSectionEntry> entries, QUndoCommand *parent)
ClearEntries::ClearEntries(RegionMap *map, QMap<QString, MapSectionEntry> entries, QUndoCommand *parent)
: QUndoCommand(parent) {
setText("Clear Entries");

Expand Down
4 changes: 2 additions & 2 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1177,7 +1177,7 @@ bool MainWindow::setProjectUI() {
ui->comboBox_Song->clear();
ui->comboBox_Song->addItems(project->songNames);
ui->comboBox_Location->clear();
ui->comboBox_Location->addItems(project->mapSectionValueToName.values());
ui->comboBox_Location->addItems(project->mapSectionIdNames);
ui->comboBox_PrimaryTileset->clear();
ui->comboBox_PrimaryTileset->addItems(project->primaryTilesetLabels);
ui->comboBox_SecondaryTileset->clear();
Expand Down Expand Up @@ -1546,7 +1546,7 @@ void MainWindow::mapListAddArea() {

connect(&newItemButtonBox, &QDialogButtonBox::accepted, [&](){
const QString newAreaName = newNameDisplay->text();
if (this->editor->project->mapSectionNameToValue.contains(newAreaName)){
if (this->editor->project->mapSectionIdNames.contains(newAreaName)){
errorMessageLabel->setText(QString("An area with the name '%1' already exists").arg(newAreaName));
errorMessageLabel->setVisible(true);
} else {
Expand Down
Loading

0 comments on commit d448765

Please sign in to comment.