porymap/src/project.cpp

2690 lines
109 KiB
C++
Raw Normal View History

2018-09-27 00:33:08 +01:00
#include "project.h"
#include "config.h"
2018-09-27 00:33:08 +01:00
#include "history.h"
#include "log.h"
2018-09-27 00:33:08 +01:00
#include "parseutil.h"
#include "paletteutil.h"
2018-09-27 00:33:08 +01:00
#include "tile.h"
#include "tileset.h"
#include "imageexport.h"
#include "map.h"
2018-09-27 00:33:08 +01:00
#include "orderedjson.h"
#include "lib/fex/lexer.h"
#include "lib/fex/parser.h"
2018-09-27 00:33:08 +01:00
#include <QDir>
2019-02-01 17:43:25 +00:00
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
2018-09-27 00:33:08 +01:00
#include <QFile>
#include <QTextStream>
#include <QStandardItem>
#include <QMessageBox>
#include <QRegularExpression>
#include <algorithm>
2018-09-27 00:33:08 +01:00
using OrderedJson = poryjson::Json;
using OrderedJsonDoc = poryjson::JsonDoc;
2018-09-27 00:33:08 +01:00
int Project::num_tiles_primary = 512;
int Project::num_tiles_total = 1024;
int Project::num_metatiles_primary = 512;
int Project::num_metatiles_total = 1024;
int Project::num_pals_primary = 6;
int Project::num_pals_total = 13;
2020-05-16 21:57:03 +01:00
int Project::max_map_data_size = 10240; // 0x2800
int Project::default_map_size = 20;
2020-07-10 21:34:42 +01:00
int Project::max_object_events = 64;
2018-09-27 00:33:08 +01:00
Project::Project(QWidget *parent) :
QObject(parent),
eventScriptLabelModel(this),
eventScriptLabelCompleter(this)
2018-09-27 00:33:08 +01:00
{
initSignals();
}
Project::~Project()
{
2020-04-08 01:25:09 +01:00
clearMapCache();
clearTilesetCache();
2018-09-27 00:33:08 +01:00
}
void Project::initSignals() {
// detect changes to specific filepaths being monitored
QObject::connect(&fileWatcher, &QFileSystemWatcher::fileChanged, [this](QString changed){
if (!porymapConfig.getMonitorFiles()) return;
if (modifiedFileTimestamps.contains(changed)) {
if (QDateTime::currentMSecsSinceEpoch() < modifiedFileTimestamps[changed]) {
return;
}
modifiedFileTimestamps.remove(changed);
}
static bool showing = false;
if (showing) return;
QMessageBox notice(this->parentWidget());
notice.setText("File Changed");
notice.setInformativeText(QString("The file %1 has changed on disk. Would you like to reload the project?")
.arg(changed.remove(this->root + "/")));
notice.setStandardButtons(QMessageBox::No | QMessageBox::Yes);
notice.setIcon(QMessageBox::Question);
QCheckBox showAgainCheck("Do not ask again.");
notice.setCheckBox(&showAgainCheck);
showing = true;
int choice = notice.exec();
if (choice == QMessageBox::Yes) {
emit reloadProject();
} else if (choice == QMessageBox::No) {
if (showAgainCheck.isChecked()) {
porymapConfig.setMonitorFiles(false);
emit uncheckMonitorFilesAction();
}
}
showing = false;
});
}
void Project::set_root(QString dir) {
this->root = dir;
this->parser.set_root(dir);
}
2018-09-27 00:33:08 +01:00
QString Project::getProjectTitle() {
if (!root.isNull()) {
return root.section('/', -1);
} else {
return QString();
}
}
2020-04-08 01:25:09 +01:00
void Project::clearMapCache() {
2021-02-18 22:41:36 +00:00
for (auto *map : mapCache.values()) {
if (map)
delete map;
2020-04-08 01:25:09 +01:00
}
2021-02-18 22:41:36 +00:00
mapCache.clear();
emit mapCacheCleared();
2020-04-08 01:25:09 +01:00
}
void Project::clearTilesetCache() {
2021-02-18 22:41:36 +00:00
for (auto *tileset : tilesetCache.values()) {
if (tileset)
delete tileset;
2020-04-08 01:25:09 +01:00
}
2021-02-18 22:41:36 +00:00
tilesetCache.clear();
2020-04-08 01:25:09 +01:00
}
2018-09-27 00:33:08 +01:00
Map* Project::loadMap(QString map_name) {
Map *map;
if (mapCache.contains(map_name)) {
map = mapCache.value(map_name);
2018-09-27 00:33:08 +01:00
// TODO: uncomment when undo/redo history is fully implemented for all actions.
if (true/*map->hasUnsavedChanges()*/) {
return map;
}
} else {
map = new Map;
map->setName(map_name);
}
if (!(loadMapData(map) && loadMapLayout(map)))
return nullptr;
mapCache.insert(map_name, map);
2018-09-27 00:33:08 +01:00
return map;
}
void Project::setNewMapConnections(Map *map) {
map->connections.clear();
}
static QMap<QString, bool> defaultTopLevelMapFields {
{"id", true},
{"name", true},
{"layout", true},
{"music", true},
{"region_map_section", true},
{"requires_flash", true},
{"weather", true},
{"map_type", true},
{"show_map_name", true},
{"battle_scene", true},
{"connections", true},
{"object_events", true},
{"warp_events", true},
{"coord_events", true},
{"bg_events", true},
{"shared_events_map", true},
{"shared_scripts_map", true},
};
QMap<QString, bool> Project::getTopLevelMapFields() {
QMap<QString, bool> topLevelMapFields = defaultTopLevelMapFields;
if (projectConfig.getBaseGameVersion() != BaseGameVersion::pokeruby) {
topLevelMapFields.insert("allow_cycling", true);
topLevelMapFields.insert("allow_escaping", true);
topLevelMapFields.insert("allow_running", true);
}
if (projectConfig.getFloorNumberEnabled()) {
topLevelMapFields.insert("floor_number", true);
}
return topLevelMapFields;
}
2019-02-01 17:43:25 +00:00
bool Project::loadMapData(Map* map) {
2018-09-27 00:33:08 +01:00
if (!map->isPersistedToFile) {
return true;
2018-09-27 00:33:08 +01:00
}
QString mapFilepath = QString("%1/%3%2/map.json").arg(root).arg(map->name).arg(projectConfig.getFilePath(ProjectFilePath::data_map_folders));
QJsonDocument mapDoc;
if (!parser.tryParseJsonFile(&mapDoc, mapFilepath)) {
logError(QString("Failed to read map data from %1").arg(mapFilepath));
return false;
2018-09-27 00:33:08 +01:00
}
2019-02-01 17:43:25 +00:00
QJsonObject mapObj = mapDoc.object();
map->song = mapObj["music"].toString();
map->layoutId = mapObj["layout"].toString();
map->location = mapObj["region_map_section"].toString();
map->requiresFlash = QString::number(mapObj["requires_flash"].toBool());
map->weather = mapObj["weather"].toString();
map->type = mapObj["map_type"].toString();
map->requiresFlash = QString::number(mapObj["requires_flash"].toBool());
map->show_location = QString::number(mapObj["show_map_name"].toBool());
map->battle_scene = mapObj["battle_scene"].toString();
if (projectConfig.getBaseGameVersion() != BaseGameVersion::pokeruby) {
map->allowBiking = QString::number(mapObj["allow_cycling"].toBool());
map->allowEscapeRope = QString::number(mapObj["allow_escaping"].toBool());
2019-02-01 17:43:25 +00:00
map->allowRunning = QString::number(mapObj["allow_running"].toBool());
}
if (projectConfig.getFloorNumberEnabled()) {
map->floorNumber = mapObj["floor_number"].toInt();
2019-02-01 17:43:25 +00:00
}
map->sharedEventsMap = mapObj["shared_events_map"].toString();
map->sharedScriptsMap = mapObj["shared_scripts_map"].toString();
2019-02-01 17:43:25 +00:00
// Events
2022-02-05 23:53:04 +00:00
map->events[EventGroup::Object].clear();
2019-02-01 17:43:25 +00:00
QJsonArray objectEventsArr = mapObj["object_events"].toArray();
2022-02-06 02:31:54 +00:00
bool hasCloneObjects = projectConfig.getEventCloneObjectEnabled();
2019-02-01 17:43:25 +00:00
for (int i = 0; i < objectEventsArr.size(); i++) {
QJsonObject event = objectEventsArr[i].toObject();
2022-02-06 02:31:54 +00:00
// If clone objects are not enabled then no type field is present
QString type = hasCloneObjects ? event["type"].toString() : "object";
if (type.isEmpty() || type == "object") {
2022-02-06 02:31:54 +00:00
Event *object = new Event(event, EventType::Object);
object->put("map_name", map->name);
object->put("sprite", event["graphics_id"].toString());
object->put("x", QString::number(event["x"].toInt()));
object->put("y", QString::number(event["y"].toInt()));
object->put("elevation", QString::number(event["elevation"].toInt()));
object->put("movement_type", event["movement_type"].toString());
object->put("radius_x", QString::number(event["movement_range_x"].toInt()));
object->put("radius_y", QString::number(event["movement_range_y"].toInt()));
object->put("trainer_type", event["trainer_type"].toString());
object->put("sight_radius_tree_id", event["trainer_sight_or_berry_tree_id"].toString());
object->put("script_label", event["script"].toString());
object->put("event_flag", event["flag"].toString());
object->put("event_group_type", EventGroup::Object);
map->events[EventGroup::Object].append(object);
} else if (type == "clone") {
Event *object = new Event(event, EventType::CloneObject);
object->put("map_name", map->name);
object->put("sprite", event["graphics_id"].toString());
object->put("x", QString::number(event["x"].toInt()));
object->put("y", QString::number(event["y"].toInt()));
object->put("target_local_id", QString::number(event["target_local_id"].toInt()));
// Ensure the target map constant is valid before adding it to the events.
QString mapConstant = event["target_map"].toString();
if (mapConstantsToMapNames.contains(mapConstant)) {
object->put("target_map", mapConstantsToMapNames.value(mapConstant));
object->put("event_group_type", EventGroup::Object);
map->events[EventGroup::Object].append(object);
} else if (mapConstant == NONE_MAP_CONSTANT) {
object->put("target_map", NONE_MAP_NAME);
object->put("event_group_type", EventGroup::Object);
map->events[EventGroup::Object].append(object);
} else {
logError(QString("Destination map constant '%1' is invalid").arg(mapConstant));
}
} else {
logError(QString("Map %1 object_event %2 has invalid type '%3'. Must be 'object' or 'clone'.").arg(map->name).arg(i).arg(type));
}
2019-02-01 17:43:25 +00:00
}
2022-02-05 23:53:04 +00:00
map->events[EventGroup::Warp].clear();
2019-02-01 17:43:25 +00:00
QJsonArray warpEventsArr = mapObj["warp_events"].toArray();
for (int i = 0; i < warpEventsArr.size(); i++) {
QJsonObject event = warpEventsArr[i].toObject();
Event *warp = new Event(event, EventType::Warp);
2019-02-01 17:43:25 +00:00
warp->put("map_name", map->name);
warp->put("x", QString::number(event["x"].toInt()));
warp->put("y", QString::number(event["y"].toInt()));
warp->put("elevation", QString::number(event["elevation"].toInt()));
warp->put("destination_warp", QString::number(event["dest_warp_id"].toInt()));
// Ensure the warp destination map constant is valid before adding it to the warps.
QString mapConstant = event["dest_map"].toString();
if (mapConstantsToMapNames.contains(mapConstant)) {
warp->put("destination_map_name", mapConstantsToMapNames.value(mapConstant));
2022-02-05 23:53:04 +00:00
warp->put("event_group_type", EventGroup::Warp);
map->events[EventGroup::Warp].append(warp);
2019-02-01 17:43:25 +00:00
} else if (mapConstant == NONE_MAP_CONSTANT) {
warp->put("destination_map_name", NONE_MAP_NAME);
2022-02-05 23:53:04 +00:00
warp->put("event_group_type", EventGroup::Warp);
map->events[EventGroup::Warp].append(warp);
} else {
2019-02-01 17:43:25 +00:00
logError(QString("Destination map constant '%1' is invalid for warp").arg(mapConstant));
}
2019-02-01 17:43:25 +00:00
}
2022-02-05 23:53:04 +00:00
map->events[EventGroup::Heal].clear();
for (auto it = healLocations.begin(); it != healLocations.end(); it++) {
2019-02-01 17:43:25 +00:00
HealLocation loc = *it;
//if TRUE map is flyable / has healing location
if (loc.mapName == QString(mapNamesToMapConstants.value(map->name)).remove(0,4)) {
2019-02-01 17:43:25 +00:00
Event *heal = new Event;
heal->put("map_name", map->name);
heal->put("x", loc.x);
heal->put("y", loc.y);
heal->put("loc_name", loc.mapName);
heal->put("id_name", loc.idName);
2019-02-01 17:43:25 +00:00
heal->put("index", loc.index);
heal->put("elevation", 3); // TODO: change this?
heal->put("destination_map_name", mapConstantsToMapNames.value(map->name));
2022-02-05 23:53:04 +00:00
heal->put("event_group_type", EventGroup::Heal);
2019-02-01 17:43:25 +00:00
heal->put("event_type", EventType::HealLocation);
if (projectConfig.getHealLocationRespawnDataEnabled()) {
heal->put("respawn_map", mapConstantsToMapNames.value(QString("MAP_" + loc.respawnMap)));
heal->put("respawn_npc", loc.respawnNPC);
}
2022-02-05 23:53:04 +00:00
map->events[EventGroup::Heal].append(heal);
}
2019-02-01 17:43:25 +00:00
}
2022-02-05 23:53:04 +00:00
map->events[EventGroup::Coord].clear();
2019-02-01 17:43:25 +00:00
QJsonArray coordEventsArr = mapObj["coord_events"].toArray();
for (int i = 0; i < coordEventsArr.size(); i++) {
QJsonObject event = coordEventsArr[i].toObject();
QString type = event["type"].toString();
if (type == "trigger") {
Event *coord = new Event(event, EventType::Trigger);
2019-02-01 17:43:25 +00:00
coord->put("map_name", map->name);
coord->put("x", QString::number(event["x"].toInt()));
coord->put("y", QString::number(event["y"].toInt()));
coord->put("elevation", QString::number(event["elevation"].toInt()));
coord->put("script_var", event["var"].toString());
coord->put("script_var_value", event["var_value"].toString());
2019-02-01 17:43:25 +00:00
coord->put("script_label", event["script"].toString());
2022-02-05 23:53:04 +00:00
coord->put("event_group_type", EventGroup::Coord);
map->events[EventGroup::Coord].append(coord);
2019-02-01 17:43:25 +00:00
} else if (type == "weather") {
Event *coord = new Event(event, EventType::WeatherTrigger);
2019-02-01 17:43:25 +00:00
coord->put("map_name", map->name);
coord->put("x", QString::number(event["x"].toInt()));
coord->put("y", QString::number(event["y"].toInt()));
coord->put("elevation", QString::number(event["elevation"].toInt()));
coord->put("weather", event["weather"].toString());
2022-02-05 23:53:04 +00:00
coord->put("event_group_type", EventGroup::Coord);
2019-02-01 17:43:25 +00:00
coord->put("event_type", EventType::WeatherTrigger);
2022-02-05 23:53:04 +00:00
map->events[EventGroup::Coord].append(coord);
2020-02-12 16:43:17 +00:00
} else {
logError(QString("Map %1 coord_event %2 has invalid type '%3'. Must be 'trigger' or 'weather'.").arg(map->name).arg(i).arg(type));
}
2019-02-01 17:43:25 +00:00
}
2022-02-05 23:53:04 +00:00
map->events[EventGroup::Bg].clear();
2019-02-01 17:43:25 +00:00
QJsonArray bgEventsArr = mapObj["bg_events"].toArray();
for (int i = 0; i < bgEventsArr.size(); i++) {
QJsonObject event = bgEventsArr[i].toObject();
QString type = event["type"].toString();
if (type == "sign") {
Event *bg = new Event(event, EventType::Sign);
2019-02-01 17:43:25 +00:00
bg->put("map_name", map->name);
bg->put("x", QString::number(event["x"].toInt()));
bg->put("y", QString::number(event["y"].toInt()));
bg->put("elevation", QString::number(event["elevation"].toInt()));
bg->put("player_facing_direction", event["player_facing_dir"].toString());
bg->put("script_label", event["script"].toString());
2022-02-05 23:53:04 +00:00
bg->put("event_group_type", EventGroup::Bg);
map->events[EventGroup::Bg].append(bg);
2019-02-01 17:43:25 +00:00
} else if (type == "hidden_item") {
Event *bg = new Event(event, EventType::HiddenItem);
2019-02-01 17:43:25 +00:00
bg->put("map_name", map->name);
bg->put("x", QString::number(event["x"].toInt()));
bg->put("y", QString::number(event["y"].toInt()));
bg->put("elevation", QString::number(event["elevation"].toInt()));
bg->put("item", event["item"].toString());
bg->put("flag", event["flag"].toString());
if (projectConfig.getHiddenItemQuantityEnabled()) {
bg->put("quantity", event["quantity"].toInt());
}
if (projectConfig.getHiddenItemRequiresItemfinderEnabled()) {
bg->put("underfoot", event["underfoot"].toBool());
}
2022-02-05 23:53:04 +00:00
bg->put("event_group_type", EventGroup::Bg);
map->events[EventGroup::Bg].append(bg);
2019-02-01 17:43:25 +00:00
} else if (type == "secret_base") {
Event *bg = new Event(event, EventType::SecretBase);
2019-02-01 17:43:25 +00:00
bg->put("map_name", map->name);
bg->put("x", QString::number(event["x"].toInt()));
bg->put("y", QString::number(event["y"].toInt()));
bg->put("elevation", QString::number(event["elevation"].toInt()));
bg->put("secret_base_id", event["secret_base_id"].toString());
2022-02-05 23:53:04 +00:00
bg->put("event_group_type", EventGroup::Bg);
map->events[EventGroup::Bg].append(bg);
2020-02-12 16:43:17 +00:00
} else {
logError(QString("Map %1 bg_event %2 has invalid type '%3'. Must be 'sign', 'hidden_item', or 'secret_base'.").arg(map->name).arg(i).arg(type));
}
2019-02-01 17:43:25 +00:00
}
2019-02-01 17:43:25 +00:00
map->connections.clear();
QJsonArray connectionsArr = mapObj["connections"].toArray();
if (!connectionsArr.isEmpty()) {
for (int i = 0; i < connectionsArr.size(); i++) {
QJsonObject connectionObj = connectionsArr[i].toObject();
MapConnection *connection = new MapConnection;
connection->direction = connectionObj["direction"].toString();
connection->offset = QString::number(connectionObj["offset"].toInt());
QString mapConstant = connectionObj["map"].toString();
if (mapConstantsToMapNames.contains(mapConstant)) {
connection->map_name = mapConstantsToMapNames.value(mapConstant);
2019-02-01 17:43:25 +00:00
map->connections.append(connection);
} else {
logError(QString("Failed to find connected map for map constant '%1'").arg(mapConstant));
}
}
}
// Check for custom fields
QMap<QString, bool> baseFields = this->getTopLevelMapFields();
for (QString key : mapObj.keys()) {
if (!baseFields.contains(key)) {
map->customHeaders.insert(key, mapObj[key].toString());
}
}
return true;
2018-09-27 00:33:08 +01:00
}
2018-10-05 07:02:40 +01:00
QString Project::readMapLayoutId(QString map_name) {
if (mapCache.contains(map_name)) {
return mapCache.value(map_name)->layoutId;
2018-10-05 07:02:40 +01:00
}
QString mapFilepath = QString("%1/%3%2/map.json").arg(root).arg(map_name).arg(projectConfig.getFilePath(ProjectFilePath::data_map_folders));
QJsonDocument mapDoc;
if (!parser.tryParseJsonFile(&mapDoc, mapFilepath)) {
logError(QString("Failed to read map layout id from %1").arg(mapFilepath));
return QString();
2018-10-05 07:02:40 +01:00
}
2019-02-01 17:43:25 +00:00
QJsonObject mapObj = mapDoc.object();
return mapObj["layout"].toString();
2018-10-05 07:02:40 +01:00
}
QString Project::readMapLocation(QString map_name) {
if (mapCache.contains(map_name)) {
return mapCache.value(map_name)->location;
2018-10-05 07:02:40 +01:00
}
QString mapFilepath = QString("%1/%3%2/map.json").arg(root).arg(map_name).arg(projectConfig.getFilePath(ProjectFilePath::data_map_folders));
QJsonDocument mapDoc;
if (!parser.tryParseJsonFile(&mapDoc, mapFilepath)) {
logError(QString("Failed to read map's region map section from %1").arg(mapFilepath));
return QString();
2018-10-05 07:02:40 +01:00
}
2019-02-01 17:43:25 +00:00
QJsonObject mapObj = mapDoc.object();
return mapObj["region_map_section"].toString();
2018-10-05 07:02:40 +01:00
}
2018-09-27 00:33:08 +01:00
void Project::setNewMapHeader(Map* map, int mapIndex) {
2019-02-01 17:43:25 +00:00
map->layoutId = QString("%1").arg(mapIndex);
2020-03-16 07:57:39 +00:00
map->location = mapSectionValueToName.value(0);
2018-09-27 00:33:08 +01:00
map->requiresFlash = "FALSE";
map->weather = weatherNames.value(0, "WEATHER_NONE");
map->type = mapTypes.value(0, "MAP_TYPE_NONE");
2020-03-16 07:57:39 +00:00
map->song = defaultSong;
if (projectConfig.getBaseGameVersion() == BaseGameVersion::pokeruby) {
map->show_location = "TRUE";
} else {
map->allowBiking = "1";
map->allowEscapeRope = "0";
map->allowRunning = "1";
map->show_location = "1";
}
if (projectConfig.getFloorNumberEnabled()) {
map->floorNumber = 0;
}
map->battle_scene = mapBattleScenes.value(0, "MAP_BATTLE_SCENE_NORMAL");
2018-09-27 00:33:08 +01:00
}
bool Project::loadLayout(MapLayout *layout) {
// Force these to run even if one fails
bool loadedTilesets = loadLayoutTilesets(layout);
bool loadedBlockdata = loadBlockdata(layout);
bool loadedBorder = loadLayoutBorder(layout);
return loadedTilesets
&& loadedBlockdata
&& loadedBorder;
}
bool Project::loadMapLayout(Map* map) {
2018-09-27 00:33:08 +01:00
if (!map->isPersistedToFile) {
return true;
2018-09-27 00:33:08 +01:00
}
if (mapLayouts.contains(map->layoutId)) {
2019-02-01 17:43:25 +00:00
map->layout = mapLayouts[map->layoutId];
} else {
logError(QString("Error: Map '%1' has an unknown layout '%2'").arg(map->name).arg(map->layoutId));
return false;
2018-09-27 00:33:08 +01:00
}
if (map->hasUnsavedChanges()) {
return true;
} else {
return loadLayout(map->layout);
}
2018-09-27 00:33:08 +01:00
}
2020-02-12 00:34:08 +00:00
bool Project::readMapLayouts() {
2018-09-27 00:33:08 +01:00
mapLayouts.clear();
2019-02-01 17:43:25 +00:00
mapLayoutsTable.clear();
2018-09-27 00:33:08 +01:00
QString layoutsFilepath = QString("%1/%2").arg(root).arg(projectConfig.getFilePath(ProjectFilePath::json_layouts));
fileWatcher.addPath(layoutsFilepath);
QJsonDocument layoutsDoc;
if (!parser.tryParseJsonFile(&layoutsDoc, layoutsFilepath)) {
logError(QString("Failed to read map layouts from %1").arg(layoutsFilepath));
2020-02-12 00:34:08 +00:00
return false;
2019-02-01 17:43:25 +00:00
}
QJsonObject layoutsObj = layoutsDoc.object();
2020-02-12 00:34:08 +00:00
QJsonArray layouts = layoutsObj["layouts"].toArray();
if (layouts.size() == 0) {
logError(QString("'layouts' array is missing from %1.").arg(layoutsFilepath));
return false;
}
2019-02-01 17:43:25 +00:00
layoutsLabel = layoutsObj["layouts_table_label"].toString();
2020-02-12 00:34:08 +00:00
if (layoutsLabel.isNull()) {
layoutsLabel = "gMapLayouts";
logWarn(QString("'layouts_table_label' value is missing from %1. Defaulting to %2")
.arg(layoutsFilepath)
.arg(layoutsLabel));
}
2018-09-27 00:33:08 +01:00
2020-02-12 00:34:08 +00:00
QList<QString> requiredFields = QList<QString>{
"id",
"name",
"width",
"height",
"primary_tileset",
"secondary_tileset",
"border_filepath",
"blockdata_filepath",
};
bool useCustomBorderSize = projectConfig.getUseCustomBorderSize();
if (useCustomBorderSize) {
requiredFields.append("border_width");
requiredFields.append("border_height");
}
2019-02-01 17:43:25 +00:00
for (int i = 0; i < layouts.size(); i++) {
QJsonObject layoutObj = layouts[i].toObject();
if (layoutObj.isEmpty())
continue;
2020-02-12 00:34:08 +00:00
if (!parser.ensureFieldsExist(layoutObj, requiredFields)) {
logError(QString("Layout %1 is missing field(s) in %2.").arg(i).arg(layoutsFilepath));
return false;
}
2018-09-27 00:33:08 +01:00
MapLayout *layout = new MapLayout();
2019-02-01 17:43:25 +00:00
layout->id = layoutObj["id"].toString();
2020-02-12 00:34:08 +00:00
if (layout->id.isEmpty()) {
logError(QString("Missing 'id' value on layout %1 in %2").arg(i).arg(layoutsFilepath));
return false;
}
2019-02-01 17:43:25 +00:00
layout->name = layoutObj["name"].toString();
2020-02-12 00:34:08 +00:00
if (layout->name.isEmpty()) {
logError(QString("Missing 'name' value on layout %1 in %2").arg(i).arg(layoutsFilepath));
return false;
}
int lwidth = layoutObj["width"].toInt();
if (lwidth <= 0) {
logError(QString("Invalid layout 'width' value '%1' on layout %2 in %3. Must be greater than 0.").arg(lwidth).arg(i).arg(layoutsFilepath));
return false;
}
layout->width = QString::number(lwidth);
int lheight = layoutObj["height"].toInt();
if (lheight <= 0) {
logError(QString("Invalid layout 'height' value '%1' on layout %2 in %3. Must be greater than 0.").arg(lheight).arg(i).arg(layoutsFilepath));
return false;
}
layout->height = QString::number(lheight);
if (useCustomBorderSize) {
int bwidth = layoutObj["border_width"].toInt();
if (bwidth <= 0) { // 0 is an expected border width/height that should be handled, GF used it for the RS layouts in FRLG
logWarn(QString("Invalid layout 'border_width' value '%1' on layout %2 in %3. Must be greater than 0. Using default (%4) instead.").arg(bwidth).arg(i).arg(layoutsFilepath).arg(DEFAULT_BORDER_WIDTH));
bwidth = DEFAULT_BORDER_WIDTH;
}
layout->border_width = QString::number(bwidth);
int bheight = layoutObj["border_height"].toInt();
if (bheight <= 0) {
2020-03-18 07:12:43 +00:00
logWarn(QString("Invalid layout 'border_height' value '%1' on layout %2 in %3. Must be greater than 0. Using default (%4) instead.").arg(bheight).arg(i).arg(layoutsFilepath).arg(DEFAULT_BORDER_HEIGHT));
bheight = DEFAULT_BORDER_HEIGHT;
}
layout->border_height = QString::number(bheight);
} else {
layout->border_width = QString::number(DEFAULT_BORDER_WIDTH);
layout->border_height = QString::number(DEFAULT_BORDER_HEIGHT);
}
2019-02-01 17:43:25 +00:00
layout->tileset_primary_label = layoutObj["primary_tileset"].toString();
2020-02-12 00:34:08 +00:00
if (layout->tileset_primary_label.isEmpty()) {
logError(QString("Missing 'primary_tileset' value on layout %1 in %2").arg(i).arg(layoutsFilepath));
return false;
}
2019-02-01 17:43:25 +00:00
layout->tileset_secondary_label = layoutObj["secondary_tileset"].toString();
2020-02-12 00:34:08 +00:00
if (layout->tileset_secondary_label.isEmpty()) {
logError(QString("Missing 'secondary_tileset' value on layout %1 in %2").arg(i).arg(layoutsFilepath));
return false;
}
2019-02-01 17:43:25 +00:00
layout->border_path = layoutObj["border_filepath"].toString();
2020-02-12 00:34:08 +00:00
if (layout->border_path.isEmpty()) {
logError(QString("Missing 'border_filepath' value on layout %1 in %2").arg(i).arg(layoutsFilepath));
return false;
}
2019-02-01 17:43:25 +00:00
layout->blockdata_path = layoutObj["blockdata_filepath"].toString();
if (layout->blockdata_path.isEmpty()) {
2020-02-12 00:34:08 +00:00
logError(QString("Missing 'blockdata_filepath' value on layout %1 in %2").arg(i).arg(layoutsFilepath));
return false;
}
2019-02-01 17:43:25 +00:00
mapLayouts.insert(layout->id, layout);
mapLayoutsTable.append(layout->id);
2018-09-27 00:33:08 +01:00
}
// Deep copy
mapLayoutsMaster = mapLayouts;
mapLayoutsMaster.detach();
2019-02-01 17:43:25 +00:00
mapLayoutsTableMaster = mapLayoutsTable;
mapLayoutsTableMaster.detach();
2020-02-12 00:34:08 +00:00
return true;
2018-09-27 00:33:08 +01:00
}
2019-02-01 17:43:25 +00:00
void Project::saveMapLayouts() {
2022-09-27 23:06:43 +01:00
QString layoutsFilepath = root + "/" + projectConfig.getFilePath(ProjectFilePath::json_layouts);
2019-02-01 17:43:25 +00:00
QFile layoutsFile(layoutsFilepath);
if (!layoutsFile.open(QIODevice::WriteOnly)) {
logError(QString("Error: Could not open %1 for writing").arg(layoutsFilepath));
return;
2018-09-27 00:33:08 +01:00
}
OrderedJson::object layoutsObj;
2019-02-01 17:43:25 +00:00
layoutsObj["layouts_table_label"] = layoutsLabel;
bool useCustomBorderSize = projectConfig.getUseCustomBorderSize();
OrderedJson::array layoutsArr;
2019-02-01 17:43:25 +00:00
for (QString layoutId : mapLayoutsTableMaster) {
MapLayout *layout = mapLayouts.value(layoutId);
OrderedJson::object layoutObj;
2019-02-01 17:43:25 +00:00
layoutObj["id"] = layout->id;
layoutObj["name"] = layout->name;
layoutObj["width"] = layout->width.toInt(nullptr, 0);
layoutObj["height"] = layout->height.toInt(nullptr, 0);
if (useCustomBorderSize) {
layoutObj["border_width"] = layout->border_width.toInt(nullptr, 0);
layoutObj["border_height"] = layout->border_height.toInt(nullptr, 0);
}
2019-02-01 17:43:25 +00:00
layoutObj["primary_tileset"] = layout->tileset_primary_label;
layoutObj["secondary_tileset"] = layout->tileset_secondary_label;
layoutObj["border_filepath"] = layout->border_path;
layoutObj["blockdata_filepath"] = layout->blockdata_path;
layoutsArr.push_back(layoutObj);
2019-02-01 17:43:25 +00:00
}
ignoreWatchedFileTemporarily(layoutsFilepath);
2019-02-01 17:43:25 +00:00
layoutsObj["layouts"] = layoutsArr;
OrderedJson layoutJson(layoutsObj);
OrderedJsonDoc jsonDoc(&layoutJson);
jsonDoc.dump(&layoutsFile);
layoutsFile.close();
}
void Project::ignoreWatchedFileTemporarily(QString filepath) {
// Ignore any file-change events for this filepath for the next 5 seconds.
modifiedFileTimestamps.insert(filepath, QDateTime::currentMSecsSinceEpoch() + 5000);
2018-09-27 00:33:08 +01:00
}
void Project::setNewMapLayout(Map* map) {
MapLayout *layout = new MapLayout();
2019-02-01 17:43:25 +00:00
layout->id = MapLayout::layoutConstantFromName(map->name);
layout->name = QString("%1_Layout").arg(map->name);
2020-05-16 21:57:03 +01:00
layout->width = QString::number(getDefaultMapSize());
layout->height = QString::number(getDefaultMapSize());
2021-02-06 00:43:49 +00:00
layout->border_width = QString::number(DEFAULT_BORDER_WIDTH);
layout->border_height = QString::number(DEFAULT_BORDER_HEIGHT);
layout->border_path = QString("%2%1/border.bin").arg(map->name).arg(projectConfig.getFilePath(ProjectFilePath::data_layouts_folders));
layout->blockdata_path = QString("%2%1/map.bin").arg(map->name).arg(projectConfig.getFilePath(ProjectFilePath::data_layouts_folders));
layout->tileset_primary_label = tilesetLabels["primary"].value(0, "gTileset_General");
layout->tileset_secondary_label = tilesetLabels["secondary"].value(0, projectConfig.getBaseGameVersion() == BaseGameVersion::pokefirered ? "gTileset_PalletTown" : "gTileset_Petalburg");
2018-09-27 00:33:08 +01:00
map->layout = layout;
2019-02-01 17:43:25 +00:00
map->layoutId = layout->id;
2018-09-27 00:33:08 +01:00
// Insert new entry into the global map layouts.
2019-02-01 17:43:25 +00:00
mapLayouts.insert(layout->id, layout);
mapLayoutsTable.append(layout->id);
2018-09-27 00:33:08 +01:00
}
2019-02-01 17:43:25 +00:00
void Project::saveMapGroups() {
QString mapGroupsFilepath = QString("%1/%2").arg(root).arg(projectConfig.getFilePath(ProjectFilePath::json_map_groups));
2019-02-01 17:43:25 +00:00
QFile mapGroupsFile(mapGroupsFilepath);
if (!mapGroupsFile.open(QIODevice::WriteOnly)) {
logError(QString("Error: Could not open %1 for writing").arg(mapGroupsFilepath));
return;
}
OrderedJson::object mapGroupsObj;
2019-02-01 17:43:25 +00:00
mapGroupsObj["layouts_table_label"] = layoutsLabel;
OrderedJson::array groupNamesArr;
for (QString groupName : this->groupNames) {
groupNamesArr.push_back(groupName);
2019-02-01 17:43:25 +00:00
}
mapGroupsObj["group_order"] = groupNamesArr;
2018-09-27 00:33:08 +01:00
int groupNum = 0;
for (QStringList mapNames : groupedMapNames) {
OrderedJson::array groupArr;
2018-09-27 00:33:08 +01:00
for (QString mapName : mapNames) {
groupArr.push_back(mapName);
2018-09-27 00:33:08 +01:00
}
mapGroupsObj[this->groupNames.at(groupNum)] = groupArr;
2018-09-27 00:33:08 +01:00
groupNum++;
}
ignoreWatchedFileTemporarily(mapGroupsFilepath);
OrderedJson mapGroupJson(mapGroupsObj);
OrderedJsonDoc jsonDoc(&mapGroupJson);
jsonDoc.dump(&mapGroupsFile);
mapGroupsFile.close();
2018-09-27 00:33:08 +01:00
}
2019-06-15 23:49:30 +01:00
void Project::saveWildMonData() {
if (!userConfig.getEncounterJsonActive()) return;
2019-07-03 21:21:48 +01:00
QString wildEncountersJsonFilepath = QString("%1/%2").arg(root).arg(projectConfig.getFilePath(ProjectFilePath::json_wild_encounters));
2019-06-15 23:49:30 +01:00
QFile wildEncountersFile(wildEncountersJsonFilepath);
if (!wildEncountersFile.open(QIODevice::WriteOnly)) {
logError(QString("Error: Could not open %1 for writing").arg(wildEncountersJsonFilepath));
return;
}
OrderedJson::object wildEncountersObject;
OrderedJson::array wildEncounterGroups;
2019-06-15 23:49:30 +01:00
2019-07-03 02:44:19 +01:00
// gWildMonHeaders label is not mutable
OrderedJson::object monHeadersObject;
2019-06-15 23:49:30 +01:00
monHeadersObject["label"] = "gWildMonHeaders";
monHeadersObject["for_maps"] = true;
OrderedJson::array fieldsInfoArray;
for (EncounterField fieldInfo : wildMonFields) {
OrderedJson::object fieldObject;
OrderedJson::array rateArray;
for (int rate : fieldInfo.encounterRates) {
rateArray.push_back(rate);
}
fieldObject["type"] = fieldInfo.name;
fieldObject["encounter_rates"] = rateArray;
OrderedJson::object groupsObject;
2021-08-13 00:19:21 +01:00
for (auto groupNamePair : fieldInfo.groups) {
QString groupName = groupNamePair.first;
OrderedJson::array subGroupIndices;
std::sort(fieldInfo.groups[groupName].begin(), fieldInfo.groups[groupName].end());
for (int slotIndex : fieldInfo.groups[groupName]) {
subGroupIndices.push_back(slotIndex);
}
groupsObject[groupName] = subGroupIndices;
}
if (!groupsObject.empty()) fieldObject["groups"] = groupsObject;
fieldsInfoArray.append(fieldObject);
}
monHeadersObject["fields"] = fieldsInfoArray;
OrderedJson::array encountersArray;
for (auto keyPair : wildMonData) {
QString key = keyPair.first;
for (auto grouplLabelPair : wildMonData[key]) {
QString groupLabel = grouplLabelPair.first;
OrderedJson::object encounterObject;
encounterObject["map"] = key;
2019-07-03 02:44:19 +01:00
encounterObject["base_label"] = groupLabel;
WildPokemonHeader encounterHeader = wildMonData[key][groupLabel];
2021-08-13 00:19:21 +01:00
for (auto fieldNamePair : encounterHeader.wildMons) {
QString fieldName = fieldNamePair.first;
OrderedJson::object fieldObject;
2021-08-13 00:19:21 +01:00
WildMonInfo monInfo = encounterHeader.wildMons[fieldName];
fieldObject["encounter_rate"] = monInfo.encounterRate;
OrderedJson::array monArray;
for (WildPokemon wildMon : monInfo.wildPokemon) {
OrderedJson::object monEntry;
monEntry["min_level"] = wildMon.minLevel;
monEntry["max_level"] = wildMon.maxLevel;
monEntry["species"] = wildMon.species;
monArray.push_back(monEntry);
}
2019-07-03 02:44:19 +01:00
fieldObject["mons"] = monArray;
encounterObject[fieldName] = fieldObject;
}
encountersArray.push_back(encounterObject);
}
2019-06-15 23:49:30 +01:00
}
monHeadersObject["encounters"] = encountersArray;
wildEncounterGroups.push_back(monHeadersObject);
2019-06-15 23:49:30 +01:00
// add extra Json objects that are not associated with maps to the file
for (auto extraObject : extraEncounterGroups) {
wildEncounterGroups.push_back(extraObject);
2019-06-15 23:49:30 +01:00
}
wildEncountersObject["wild_encounter_groups"] = wildEncounterGroups;
ignoreWatchedFileTemporarily(wildEncountersJsonFilepath);
OrderedJson encounterJson(wildEncountersObject);
OrderedJsonDoc jsonDoc(&encounterJson);
jsonDoc.dump(&wildEncountersFile);
2019-06-15 23:49:30 +01:00
wildEncountersFile.close();
}
2018-09-27 00:33:08 +01:00
void Project::saveMapConstantsHeader() {
2019-02-01 17:43:25 +00:00
QString text = QString("#ifndef GUARD_CONSTANTS_MAP_GROUPS_H\n");
text += QString("#define GUARD_CONSTANTS_MAP_GROUPS_H\n");
2022-09-27 23:06:43 +01:00
text += QString("\n//\n// DO NOT MODIFY THIS FILE! It is auto-generated from %1\n//\n\n")
.arg(projectConfig.getFilePath(ProjectFilePath::json_map_groups));
2018-09-27 00:33:08 +01:00
int groupNum = 0;
for (QStringList mapNames : groupedMapNames) {
2021-10-17 15:35:38 +01:00
text += "// " + groupNames.at(groupNum) + "\n";
2018-09-27 00:33:08 +01:00
int maxLength = 0;
for (QString mapName : mapNames) {
QString mapConstantName = mapNamesToMapConstants.value(mapName);
2018-09-27 00:33:08 +01:00
if (mapConstantName.length() > maxLength)
maxLength = mapConstantName.length();
}
int groupIndex = 0;
for (QString mapName : mapNames) {
QString mapConstantName = mapNamesToMapConstants.value(mapName);
2018-09-27 00:33:08 +01:00
text += QString("#define %1%2(%3 | (%4 << 8))\n")
.arg(mapConstantName)
.arg(QString(" ").repeated(maxLength - mapConstantName.length() + 1))
.arg(groupIndex)
.arg(groupNum);
groupIndex++;
}
text += QString("\n");
groupNum++;
}
2018-12-25 19:27:02 +00:00
text += QString("#define MAP_GROUPS_COUNT %1\n\n").arg(groupNum);
2019-02-16 23:00:38 +00:00
text += QString("#endif // GUARD_CONSTANTS_MAP_GROUPS_H\n");
QString mapGroupFilepath = root + "/" + projectConfig.getFilePath(ProjectFilePath::constants_map_groups);
ignoreWatchedFileTemporarily(mapGroupFilepath);
saveTextFile(mapGroupFilepath, text);
2018-09-27 00:33:08 +01:00
}
// saves heal location coords in root + /src/data/heal_locations.h
// and indexes as defines in root + /include/constants/heal_locations.h
void Project::saveHealLocationStruct(Map *map) {
QString constantPrefix, arrayName;
if (projectConfig.getHealLocationRespawnDataEnabled()) {
constantPrefix = "SPAWN_";
arrayName = "sSpawnPoints";
} else {
constantPrefix = "HEAL_LOCATION_";
arrayName = "sHealLocations";
}
QString data_text = QString("%1%2struct HealLocation %3[] =\n{\n")
2019-04-29 01:20:48 +01:00
.arg(dataQualifiers.value("heal_locations").isStatic ? "static " : "")
.arg(dataQualifiers.value("heal_locations").isConst ? "const " : "")
.arg(arrayName);
2018-09-27 00:33:08 +01:00
QString constants_text = QString("#ifndef GUARD_CONSTANTS_HEAL_LOCATIONS_H\n");
constants_text += QString("#define GUARD_CONSTANTS_HEAL_LOCATIONS_H\n\n");
QMap<QString, int> healLocationsDupes;
QSet<QString> healLocationsUnique;
2018-09-27 00:33:08 +01:00
// set healLocationsDupes and healLocationsUnique
for (auto it = healLocations.begin(); it != healLocations.end(); it++) {
2018-09-27 00:33:08 +01:00
HealLocation loc = *it;
QString xname = loc.idName;
if (healLocationsUnique.contains(xname)) {
healLocationsDupes[xname] = 1;
2018-09-27 00:33:08 +01:00
}
healLocationsUnique.insert(xname);
2018-09-27 00:33:08 +01:00
}
// set new location in healLocations list
2022-02-05 23:53:04 +00:00
if (map->events[EventGroup::Heal].length() > 0) {
for (Event *healEvent : map->events[EventGroup::Heal]) {
2018-09-27 00:33:08 +01:00
HealLocation hl = HealLocation::fromEvent(healEvent);
healLocations[hl.index - 1] = hl;
2018-09-27 00:33:08 +01:00
}
}
int i = 1;
for (auto map_in : healLocations) {
// add numbered suffix for duplicate constants
if (healLocationsDupes.keys().contains(map_in.idName)) {
QString duplicateName = map_in.idName;
map_in.idName += QString("_%1").arg(healLocationsDupes[duplicateName]);
healLocationsDupes[duplicateName]++;
}
// Save first array (heal location coords), only data array in RSE
data_text += QString(" [%1%2 - 1] = {MAP_GROUP(%3), MAP_NUM(%3), %4, %5},\n")
.arg(constantPrefix)
.arg(map_in.idName)
.arg(map_in.mapName)
2018-09-27 00:33:08 +01:00
.arg(map_in.x)
.arg(map_in.y);
// Save constants
2018-09-27 00:33:08 +01:00
if (map_in.index != 0) {
constants_text += QString("#define %1%2 %3\n")
.arg(constantPrefix)
.arg(map_in.idName)
2018-09-27 00:33:08 +01:00
.arg(map_in.index);
} else {
constants_text += QString("#define %1%2 %3\n")
.arg(constantPrefix)
.arg(map_in.idName)
2018-09-27 00:33:08 +01:00
.arg(i);
}
i++;
}
if (projectConfig.getHealLocationRespawnDataEnabled()) {
// Save second array (map where player respawns for each heal location)
data_text += QString("};\n\n%1%2u16 sWhiteoutRespawnHealCenterMapIdxs[][2] =\n{\n")
.arg(dataQualifiers.value("heal_locations").isStatic ? "static " : "")
.arg(dataQualifiers.value("heal_locations").isConst ? "const " : "");
for (auto map_in : healLocations) {
data_text += QString(" [%1%2 - 1] = {MAP_GROUP(%3), MAP_NUM(%3)},\n")
.arg(constantPrefix)
.arg(map_in.idName)
.arg(map_in.respawnMap);
}
// Save third array (object id of NPC player speaks to upon respawning for each heal location)
data_text += QString("};\n\n%1%2u8 sWhiteoutRespawnHealerNpcIds[] =\n{\n")
.arg(dataQualifiers.value("heal_locations").isStatic ? "static " : "")
.arg(dataQualifiers.value("heal_locations").isConst ? "const " : "");
for (auto map_in : healLocations) {
data_text += QString(" [%1%2 - 1] = %3,\n")
.arg(constantPrefix)
.arg(map_in.idName)
.arg(map_in.respawnNPC);
}
}
2018-09-27 00:33:08 +01:00
data_text += QString("};\n");
constants_text += QString("\n#endif // GUARD_CONSTANTS_HEAL_LOCATIONS_H\n");
QString healLocationFilepath = root + "/" + projectConfig.getFilePath(ProjectFilePath::data_heal_locations);
ignoreWatchedFileTemporarily(healLocationFilepath);
saveTextFile(healLocationFilepath, data_text);
QString healLocationConstantsFilepath = root + "/" + projectConfig.getFilePath(ProjectFilePath::constants_heal_locations);
ignoreWatchedFileTemporarily(healLocationConstantsFilepath);
saveTextFile(healLocationConstantsFilepath, constants_text);
2018-09-27 00:33:08 +01:00
}
void Project::saveTilesets(Tileset *primaryTileset, Tileset *secondaryTileset) {
saveTilesetMetatileLabels(primaryTileset, secondaryTileset);
saveTilesetMetatileAttributes(primaryTileset);
saveTilesetMetatileAttributes(secondaryTileset);
saveTilesetMetatiles(primaryTileset);
saveTilesetMetatiles(secondaryTileset);
2018-10-03 01:01:24 +01:00
saveTilesetTilesImage(primaryTileset);
saveTilesetTilesImage(secondaryTileset);
2020-05-03 16:00:56 +01:00
saveTilesetPalettes(primaryTileset);
saveTilesetPalettes(secondaryTileset);
}
void Project::saveTilesetMetatileLabels(Tileset *primaryTileset, Tileset *secondaryTileset) {
QString primaryPrefix = QString("METATILE_%1_").arg(QString(primaryTileset->name).replace("gTileset_", ""));
QString secondaryPrefix = QString("METATILE_%1_").arg(QString(secondaryTileset->name).replace("gTileset_", ""));
QMap<QString, int> defines;
bool definesFileModified = false;
QString metatileLabelsFilename = projectConfig.getFilePath(ProjectFilePath::constants_metatile_labels);
2020-05-22 21:02:11 +01:00
defines = parser.readCDefines(metatileLabelsFilename, (QStringList() << "METATILE_"));
// Purge old entries from the file.
QStringList definesToRemove;
for (QString defineName : defines.keys()) {
if (defineName.startsWith(primaryPrefix) || defineName.startsWith(secondaryPrefix)) {
definesToRemove << defineName;
}
}
for (QString defineName : definesToRemove) {
defines.remove(defineName);
definesFileModified = true;
}
// Add the new labels.
2021-02-17 02:45:54 +00:00
for (int i = 0; i < primaryTileset->metatiles.size(); i++) {
Metatile *metatile = primaryTileset->metatiles.at(i);
if (metatile->label.size() != 0) {
QString defineName = QString("%1%2").arg(primaryPrefix, metatile->label);
defines.insert(defineName, i);
definesFileModified = true;
}
}
2021-02-17 02:45:54 +00:00
for (int i = 0; i < secondaryTileset->metatiles.size(); i++) {
Metatile *metatile = secondaryTileset->metatiles.at(i);
if (metatile->label.size() != 0) {
QString defineName = QString("%1%2").arg(secondaryPrefix, metatile->label);
defines.insert(defineName, i + Project::num_tiles_primary);
definesFileModified = true;
}
}
if (!definesFileModified) {
return;
}
auto getTilesetFromLabel = [](QString labelName) {
return QRegularExpression("METATILE_(?<tileset>[A-Za-z0-9]+)_").match(labelName).captured("tileset");
};
QString outputText = "#ifndef GUARD_METATILE_LABELS_H\n";
outputText += "#define GUARD_METATILE_LABELS_H\n";
for (int i = 0; i < defines.size();) {
QString defineName = defines.keys()[i];
QString currentTileset = getTilesetFromLabel(defineName);
outputText += QString("\n// gTileset_%1\n").arg(currentTileset);
int j = 0, longestLength = 0;
QMap<QString, int> definesOut;
// Setup for pretty formatting.
while (i + j < defines.size() && getTilesetFromLabel(defines.keys()[i + j]) == currentTileset) {
defineName = defines.keys()[i + j];
if (defineName.size() > longestLength)
longestLength = defineName.size();
definesOut.insert(defineName, defines[defineName]);
j++;
}
for (QString defineName : definesOut.keys()) {
int value = defines[defineName];
QString line = QString("#define %1 0x%2\n")
.arg(defineName, -1 * longestLength)
.arg(QString("%1").arg(value, 3, 16, QChar('0')).toUpper());
outputText += line;
}
i += j;
}
outputText += "\n#endif // GUARD_METATILE_LABELS_H\n";
2020-05-22 21:02:11 +01:00
ignoreWatchedFileTemporarily(root + "/" + metatileLabelsFilename);
saveTextFile(root + "/" + metatileLabelsFilename, outputText);
}
void Project::saveTilesetMetatileAttributes(Tileset *tileset) {
QFile attrs_file(tileset->metatile_attrs_path);
if (attrs_file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
QByteArray data;
2022-02-03 23:10:50 +00:00
BaseGameVersion version = projectConfig.getBaseGameVersion();
int attrSize = Metatile::getAttributesSize(version);
for (Metatile *metatile : tileset->metatiles) {
uint32_t attributes = metatile->getAttributes(version);
for (int i = 0; i < attrSize; i++)
data.append(static_cast<char>(attributes >> (8 * i)));
}
attrs_file.write(data);
} else {
logError(QString("Could not save tileset metatile attributes file '%1'").arg(tileset->metatile_attrs_path));
}
}
void Project::saveTilesetMetatiles(Tileset *tileset) {
QFile metatiles_file(tileset->metatiles_path);
if (metatiles_file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
QByteArray data;
2022-08-20 21:08:53 +01:00
int numTiles = projectConfig.getTripleLayerMetatilesEnabled() ? 12 : 8;
2021-02-17 02:45:54 +00:00
for (Metatile *metatile : tileset->metatiles) {
for (int i = 0; i < numTiles; i++) {
2022-02-04 02:32:27 +00:00
uint16_t tile = metatile->tiles.at(i).rawValue();
data.append(static_cast<char>(tile));
data.append(static_cast<char>(tile >> 8));
}
}
metatiles_file.write(data);
} else {
2021-02-17 02:45:54 +00:00
tileset->metatiles.clear();
logError(QString("Could not open tileset metatiles file '%1'").arg(tileset->metatiles_path));
}
}
2018-10-03 01:01:24 +01:00
void Project::saveTilesetTilesImage(Tileset *tileset) {
exportIndexed4BPPPng(tileset->tilesImage, tileset->tilesImagePath);
2018-10-03 01:01:24 +01:00
}
2020-05-03 16:00:56 +01:00
void Project::saveTilesetPalettes(Tileset *tileset) {
for (int i = 0; i < Project::getNumPalettesTotal(); i++) {
2018-10-03 01:01:41 +01:00
QString filepath = tileset->palettePaths.at(i);
2021-02-18 08:25:26 +00:00
PaletteUtil::writeJASC(filepath, tileset->palettes.at(i).toVector(), 0, 16);
2018-10-03 01:01:41 +01:00
}
}
bool Project::loadLayoutTilesets(MapLayout *layout) {
layout->tileset_primary = getTileset(layout->tileset_primary_label);
if (!layout->tileset_primary) {
QString defaultTileset = tilesetLabels["primary"].value(0, "gTileset_General");
logWarn(QString("Map layout %1 has invalid primary tileset '%2'. Using default '%3'").arg(layout->id).arg(layout->tileset_primary_label).arg(defaultTileset));
layout->tileset_primary_label = defaultTileset;
layout->tileset_primary = getTileset(layout->tileset_primary_label);
if (!layout->tileset_primary) {
logError(QString("Failed to set default primary tileset."));
return false;
}
}
layout->tileset_secondary = getTileset(layout->tileset_secondary_label);
if (!layout->tileset_secondary) {
QString defaultTileset = tilesetLabels["secondary"].value(0, projectConfig.getBaseGameVersion() == BaseGameVersion::pokefirered ? "gTileset_PalletTown" : "gTileset_Petalburg");
logWarn(QString("Map layout %1 has invalid secondary tileset '%2'. Using default '%3'").arg(layout->id).arg(layout->tileset_secondary_label).arg(defaultTileset));
layout->tileset_secondary_label = defaultTileset;
layout->tileset_secondary = getTileset(layout->tileset_secondary_label);
if (!layout->tileset_secondary) {
logError(QString("Failed to set default secondary tileset."));
return false;
}
}
return true;
2018-09-27 00:33:08 +01:00
}
2018-10-03 01:01:15 +01:00
Tileset* Project::loadTileset(QString label, Tileset *tileset) {
2022-09-27 23:06:43 +01:00
const QStringList values = parser.getLabelValues(parser.parseAsm(projectConfig.getFilePath(ProjectFilePath::tilesets_headers)), label);
if (values.isEmpty()) {
return nullptr;
}
2018-10-03 01:01:15 +01:00
if (tileset == nullptr) {
tileset = new Tileset;
}
2018-09-27 00:33:08 +01:00
tileset->name = label;
tileset->is_compressed = values.value(0);
tileset->is_secondary = values.value(1);
tileset->padding = values.value(2);
tileset->tiles_label = values.value(3);
tileset->palettes_label = values.value(4);
tileset->metatiles_label = values.value(5);
if (projectConfig.getBaseGameVersion() == BaseGameVersion::pokefirered) {
tileset->callback_label = values.value(6);
tileset->metatile_attrs_label = values.value(7);
} else {
tileset->metatile_attrs_label = values.value(6);
tileset->callback_label = values.value(7);
}
2018-09-27 00:33:08 +01:00
loadTilesetAssets(tileset);
tilesetCache.insert(label, tileset);
2018-09-27 00:33:08 +01:00
return tileset;
}
bool Project::loadBlockdata(MapLayout *layout) {
QString path = QString("%1/%2").arg(root).arg(layout->blockdata_path);
layout->blockdata = readBlockdata(path);
layout->lastCommitBlocks.blocks = layout->blockdata;
layout->lastCommitBlocks.mapDimensions = QSize(layout->getWidth(), layout->getHeight());
if (layout->blockdata.count() != layout->getWidth() * layout->getHeight()) {
logWarn(QString("Layout blockdata length %1 does not match dimensions %2x%3 (should be %4). Resizing blockdata.")
.arg(layout->blockdata.count())
.arg(layout->getWidth())
.arg(layout->getHeight())
.arg(layout->getWidth() * layout->getHeight()));
layout->blockdata.resize(layout->getWidth() * layout->getHeight());
}
return true;
2018-09-27 00:33:08 +01:00
}
void Project::setNewMapBlockdata(Map *map) {
2021-02-14 21:34:17 +00:00
map->layout->blockdata.clear();
2022-09-11 04:30:28 +01:00
int width = map->getWidth();
int height = map->getHeight();
Block block(projectConfig.getNewMapMetatileId(), 0, projectConfig.getNewMapElevation());
for (int i = 0; i < width * height; i++) {
map->layout->blockdata.append(block);
2018-09-27 00:33:08 +01:00
}
map->layout->lastCommitBlocks.blocks = map->layout->blockdata;
2022-09-11 04:30:28 +01:00
map->layout->lastCommitBlocks.mapDimensions = QSize(width, height);
2018-09-27 00:33:08 +01:00
}
bool Project::loadLayoutBorder(MapLayout *layout) {
QString path = QString("%1/%2").arg(root).arg(layout->border_path);
layout->border = readBlockdata(path);
layout->lastCommitBlocks.border = layout->border;
layout->lastCommitBlocks.borderDimensions = QSize(layout->getBorderWidth(), layout->getBorderHeight());
int borderLength = layout->getBorderWidth() * layout->getBorderHeight();
if (layout->border.count() != borderLength) {
logWarn(QString("Layout border blockdata length %1 must be %2. Resizing border blockdata.")
.arg(layout->border.count())
.arg(borderLength));
layout->border.resize(borderLength);
}
return true;
2018-09-27 00:33:08 +01:00
}
void Project::setNewMapBorder(Map *map) {
2021-02-14 21:34:17 +00:00
map->layout->border.clear();
2022-09-11 04:30:28 +01:00
int width = map->getBorderWidth();
int height = map->getBorderHeight();
if (width != DEFAULT_BORDER_WIDTH || height != DEFAULT_BORDER_HEIGHT) {
for (int i = 0; i < width * height; i++) {
2021-02-14 21:34:17 +00:00
map->layout->border.append(0);
2020-03-14 07:44:55 +00:00
}
} else {
2022-09-11 04:30:28 +01:00
QList<int> metatileIds = projectConfig.getNewMapBorderMetatileIds();
for (int i = 0; i < DEFAULT_BORDER_WIDTH * DEFAULT_BORDER_HEIGHT; i++) {
map->layout->border.append(qint16(metatileIds.at(i)));
}
}
map->layout->lastCommitBlocks.border = map->layout->border;
2022-09-11 04:30:28 +01:00
map->layout->lastCommitBlocks.borderDimensions = QSize(width, height);
2018-09-27 00:33:08 +01:00
}
2019-02-01 17:43:25 +00:00
void Project::saveLayoutBorder(Map *map) {
2018-09-27 00:33:08 +01:00
QString path = QString("%1/%2").arg(root).arg(map->layout->border_path);
writeBlockdata(path, map->layout->border);
}
2019-02-01 17:43:25 +00:00
void Project::saveLayoutBlockdata(Map* map) {
2018-09-27 00:33:08 +01:00
QString path = QString("%1/%2").arg(root).arg(map->layout->blockdata_path);
writeBlockdata(path, map->layout->blockdata);
}
2021-02-14 21:34:17 +00:00
void Project::writeBlockdata(QString path, const Blockdata &blockdata) {
2018-09-27 00:33:08 +01:00
QFile file(path);
if (file.open(QIODevice::WriteOnly)) {
2021-02-14 21:34:17 +00:00
QByteArray data = blockdata.serialize();
2018-09-27 00:33:08 +01:00
file.write(data);
} else {
logError(QString("Failed to open blockdata file for writing: '%1'").arg(path));
2018-09-27 00:33:08 +01:00
}
}
void Project::saveAllMaps() {
2021-02-18 22:41:36 +00:00
for (auto *map : mapCache.values())
2018-09-27 00:33:08 +01:00
saveMap(map);
}
void Project::saveMap(Map *map) {
// Create/Modify a few collateral files for brand new maps.
2022-09-27 23:06:43 +01:00
QString basePath = projectConfig.getFilePath(ProjectFilePath::data_map_folders);
QString mapDataDir = root + "/" + basePath + map->name;
2018-09-27 00:33:08 +01:00
if (!map->isPersistedToFile) {
2019-02-01 17:43:25 +00:00
if (!QDir::root().mkdir(mapDataDir)) {
logError(QString("Error: failed to create directory for new map: '%1'").arg(mapDataDir));
2018-09-27 00:33:08 +01:00
}
// Create file data/maps/<map_name>/scripts.inc
QString text = this->getScriptDefaultString(projectConfig.getUsePoryScript(), map->name);
2022-09-27 23:06:43 +01:00
saveTextFile(mapDataDir + "/scripts" + this->getScriptFileExtension(projectConfig.getUsePoryScript()), text);
2018-09-27 00:33:08 +01:00
bool usesTextFile = projectConfig.getCreateMapTextFileEnabled();
if (usesTextFile) {
// Create file data/maps/<map_name>/text.inc
2022-09-27 23:06:43 +01:00
saveTextFile(mapDataDir + "/text" + this->getScriptFileExtension(projectConfig.getUsePoryScript()), "\n");
}
2018-09-27 00:33:08 +01:00
// Simply append to data/event_scripts.s.
2022-09-27 23:06:43 +01:00
text = QString("\n\t.include \"%1%2/scripts.inc\"\n").arg(basePath, map->name);
if (usesTextFile) {
2022-09-27 23:06:43 +01:00
text += QString("\t.include \"%1%2/text.inc\"\n").arg(basePath, map->name);
}
2022-09-27 23:06:43 +01:00
appendTextFile(root + "/" + projectConfig.getFilePath(ProjectFilePath::data_event_scripts), text);
2018-09-27 00:33:08 +01:00
2019-02-01 17:43:25 +00:00
if (map->needsLayoutDir) {
2022-09-27 23:06:43 +01:00
QString newLayoutDir = QString(root + "/%1%2").arg(projectConfig.getFilePath(ProjectFilePath::data_layouts_folders), map->name);
2019-02-01 17:43:25 +00:00
if (!QDir::root().mkdir(newLayoutDir)) {
logError(QString("Error: failed to create directory for new layout: '%1'").arg(newLayoutDir));
}
}
}
2018-09-27 00:33:08 +01:00
2022-09-27 23:06:43 +01:00
QString layoutsFilepath = root + "/" + projectConfig.getFilePath(ProjectFilePath::json_layouts);
QJsonDocument layoutsDoc;
if (!parser.tryParseJsonFile(&layoutsDoc, layoutsFilepath)) {
logError(QString("Failed to read map layouts from %1").arg(layoutsFilepath));
return;
}
2019-02-01 17:43:25 +00:00
QFile layoutsFile(layoutsFilepath);
if (!layoutsFile.open(QIODevice::ReadWrite)) {
logError(QString("Error: Could not open %1 for read/write").arg(layoutsFilepath));
return;
}
// Append to "layouts" array in data/layouts/layouts.json.
2019-02-01 17:43:25 +00:00
QJsonObject layoutsObj = layoutsDoc.object();
QJsonArray layoutsArr = layoutsObj["layouts"].toArray();
QJsonObject newLayoutObj;
newLayoutObj["id"] = map->layout->id;
newLayoutObj["name"] = map->layout->name;
newLayoutObj["width"] = map->layout->width.toInt();
newLayoutObj["height"] = map->layout->height.toInt();
if (projectConfig.getUseCustomBorderSize()) {
newLayoutObj["border_width"] = map->layout->border_width.toInt();
newLayoutObj["border_height"] = map->layout->border_height.toInt();
}
2019-02-01 17:43:25 +00:00
newLayoutObj["primary_tileset"] = map->layout->tileset_primary_label;
newLayoutObj["secondary_tileset"] = map->layout->tileset_secondary_label;
newLayoutObj["border_filepath"] = map->layout->border_path;
newLayoutObj["blockdata_filepath"] = map->layout->blockdata_path;
layoutsArr.append(newLayoutObj);
layoutsFile.write(layoutsDoc.toJson());
layoutsFile.close();
2019-02-01 17:43:25 +00:00
// Create map.json for map data.
QString mapFilepath = QString("%1/map.json").arg(mapDataDir);
QFile mapFile(mapFilepath);
if (!mapFile.open(QIODevice::WriteOnly)) {
logError(QString("Error: Could not open %1 for writing").arg(mapFilepath));
return;
2018-09-27 00:33:08 +01:00
}
OrderedJson::object mapObj;
2019-02-01 17:43:25 +00:00
// Header values.
mapObj["id"] = map->constantName;
mapObj["name"] = map->name;
mapObj["layout"] = map->layout->id;
mapObj["music"] = map->song;
mapObj["region_map_section"] = map->location;
mapObj["requires_flash"] = map->requiresFlash.toInt() > 0 || map->requiresFlash == "TRUE";
mapObj["weather"] = map->weather;
mapObj["map_type"] = map->type;
if (projectConfig.getBaseGameVersion() != BaseGameVersion::pokeruby) {
mapObj["allow_cycling"] = map->allowBiking.toInt() > 0 || map->allowBiking == "TRUE";
mapObj["allow_escaping"] = map->allowEscapeRope.toInt() > 0 || map->allowEscapeRope == "TRUE";
mapObj["allow_running"] = map->allowRunning.toInt() > 0 || map->allowRunning == "TRUE";
}
2019-02-01 17:43:25 +00:00
mapObj["show_map_name"] = map->show_location.toInt() > 0 || map->show_location == "TRUE";
if (projectConfig.getFloorNumberEnabled()) {
2020-03-18 07:12:43 +00:00
mapObj["floor_number"] = map->floorNumber;
}
2019-02-01 17:43:25 +00:00
mapObj["battle_scene"] = map->battle_scene;
// Connections
if (map->connections.length() > 0) {
OrderedJson::array connectionsArr;
2019-02-01 17:43:25 +00:00
for (MapConnection* connection : map->connections) {
if (mapNamesToMapConstants.contains(connection->map_name)) {
OrderedJson::object connectionObj;
2019-02-01 17:43:25 +00:00
connectionObj["direction"] = connection->direction;
connectionObj["offset"] = connection->offset.toInt();
connectionObj["map"] = this->mapNamesToMapConstants.value(connection->map_name);
2019-02-01 17:43:25 +00:00
connectionsArr.append(connectionObj);
} else {
logError(QString("Failed to write map connection. '%1' is not a valid map name").arg(connection->map_name));
}
}
mapObj["connections"] = connectionsArr;
} else {
mapObj["connections"] = QJsonValue::Null;
}
if (map->sharedEventsMap.isEmpty()) {
// Object events
OrderedJson::array objectEventsArr;
2022-02-05 23:53:04 +00:00
for (int i = 0; i < map->events[EventGroup::Object].length(); i++) {
2022-02-06 02:31:54 +00:00
Event *event = map->events[EventGroup::Object].value(i);
QString event_type = event->get("event_type");
OrderedJson::object jsonObj;
if (event_type == EventType::Object) {
jsonObj = event->buildObjectEventJSON();
} else if (event_type == EventType::CloneObject) {
jsonObj = event->buildCloneObjectEventJSON(mapNamesToMapConstants);
}
objectEventsArr.push_back(jsonObj);
2019-02-01 17:43:25 +00:00
}
mapObj["object_events"] = objectEventsArr;
// Warp events
OrderedJson::array warpEventsArr;
2022-02-05 23:53:04 +00:00
for (int i = 0; i < map->events[EventGroup::Warp].length(); i++) {
Event *warp_event = map->events[EventGroup::Warp].value(i);
OrderedJson::object warpObj = warp_event->buildWarpEventJSON(mapNamesToMapConstants);
warpEventsArr.append(warpObj);
}
mapObj["warp_events"] = warpEventsArr;
// Coord events
OrderedJson::array coordEventsArr;
2022-02-05 23:53:04 +00:00
for (int i = 0; i < map->events[EventGroup::Coord].length(); i++) {
Event *event = map->events[EventGroup::Coord].value(i);
QString event_type = event->get("event_type");
if (event_type == EventType::Trigger) {
OrderedJson::object triggerObj = event->buildTriggerEventJSON();
coordEventsArr.append(triggerObj);
} else if (event_type == EventType::WeatherTrigger) {
OrderedJson::object weatherObj = event->buildWeatherTriggerEventJSON();
coordEventsArr.append(weatherObj);
}
}
mapObj["coord_events"] = coordEventsArr;
// Bg Events
OrderedJson::array bgEventsArr;
2022-02-05 23:53:04 +00:00
for (int i = 0; i < map->events[EventGroup::Bg].length(); i++) {
Event *event = map->events[EventGroup::Bg].value(i);
QString event_type = event->get("event_type");
if (event_type == EventType::Sign) {
OrderedJson::object signObj = event->buildSignEventJSON();
bgEventsArr.append(signObj);
} else if (event_type == EventType::HiddenItem) {
OrderedJson::object hiddenItemObj = event->buildHiddenItemEventJSON();
bgEventsArr.append(hiddenItemObj);
} else if (event_type == EventType::SecretBase) {
OrderedJson::object secretBaseObj = event->buildSecretBaseEventJSON();
bgEventsArr.append(secretBaseObj);
}
2019-02-01 17:43:25 +00:00
}
mapObj["bg_events"] = bgEventsArr;
} else {
mapObj["shared_events_map"] = map->sharedEventsMap;
}
if (!map->sharedScriptsMap.isEmpty()) {
mapObj["shared_scripts_map"] = map->sharedScriptsMap;
2019-02-01 17:43:25 +00:00
}
// Custom header fields.
for (QString key : map->customHeaders.keys()) {
mapObj[key] = map->customHeaders[key];
}
OrderedJson mapJson(mapObj);
OrderedJsonDoc jsonDoc(&mapJson);
jsonDoc.dump(&mapFile);
mapFile.close();
2019-02-01 17:43:25 +00:00
saveLayoutBorder(map);
saveLayoutBlockdata(map);
saveMapHealEvents(map);
2018-09-27 00:33:08 +01:00
// Update global data structures with current map data.
updateMapLayout(map);
map->isPersistedToFile = true;
map->hasUnsavedDataChanges = false;
map->editHistory.setClean();
2018-09-27 00:33:08 +01:00
}
void Project::updateMapLayout(Map* map) {
2019-02-01 17:43:25 +00:00
if (!mapLayoutsTableMaster.contains(map->layoutId)) {
mapLayoutsTableMaster.append(map->layoutId);
2018-09-27 00:33:08 +01:00
}
// Deep copy
2019-02-01 17:43:25 +00:00
MapLayout *layout = mapLayouts.value(map->layoutId);
2018-09-27 00:33:08 +01:00
MapLayout *newLayout = new MapLayout();
*newLayout = *layout;
2019-02-01 17:43:25 +00:00
mapLayoutsMaster.insert(map->layoutId, newLayout);
2018-09-27 00:33:08 +01:00
}
void Project::saveAllDataStructures() {
2019-02-01 17:43:25 +00:00
saveMapLayouts();
saveMapGroups();
2018-09-27 00:33:08 +01:00
saveMapConstantsHeader();
2019-06-15 23:49:30 +01:00
saveWildMonData();
2018-09-27 00:33:08 +01:00
}
void Project::loadTilesetAssets(Tileset* tileset) {
QString category = (tileset->is_secondary == "TRUE") ? "secondary" : "primary";
if (tileset->name.isNull()) {
return;
}
2020-03-17 04:29:54 +00:00
QRegularExpression re("([a-z])([A-Z0-9])");
2018-10-03 01:01:15 +01:00
QString tilesetName = tileset->name;
2022-09-27 23:06:43 +01:00
QString basePath = root + "/" + projectConfig.getFilePath(ProjectFilePath::data_tilesets_folders) + category + "/";
QString dir_path = basePath + tilesetName.replace("gTileset_", "").replace(re, "\\1_\\2").toLower();
2018-09-27 00:33:08 +01:00
2022-09-27 23:06:43 +01:00
const QList<QStringList> graphics = parser.parseAsm(projectConfig.getFilePath(ProjectFilePath::tilesets_graphics));
const QStringList tiles_values = parser.getLabelValues(graphics, tileset->tiles_label);
const QStringList palettes_values = parser.getLabelValues(graphics, tileset->palettes_label);
2018-09-27 00:33:08 +01:00
QString tiles_path;
if (!tiles_values.isEmpty()) {
tiles_path = root + '/' + tiles_values.value(0).section('"', 1, 1);
2018-09-27 00:33:08 +01:00
} else {
tiles_path = dir_path + "/tiles.4bpp";
if (tileset->is_compressed == "TRUE") {
tiles_path += ".lz";
}
}
if (!palettes_values.isEmpty()) {
for (const auto &value : palettes_values) {
tileset->palettePaths.append(this->fixPalettePath(root + '/' + value.section('"', 1, 1)));
2018-09-27 00:33:08 +01:00
}
} else {
QString palettes_dir_path = dir_path + "/palettes";
for (int i = 0; i < 16; i++) {
tileset->palettePaths.append(palettes_dir_path + '/' + QString("%1").arg(i, 2, 10, QLatin1Char('0')) + ".pal");
2018-09-27 00:33:08 +01:00
}
}
2022-09-27 23:06:43 +01:00
const QList<QStringList> metatiles_macros = parser.parseAsm(projectConfig.getFilePath(ProjectFilePath::tilesets_metatiles));
const QStringList metatiles_values = parser.getLabelValues(metatiles_macros, tileset->metatiles_label);
if (!metatiles_values.isEmpty()) {
tileset->metatiles_path = root + '/' + metatiles_values.value(0).section('"', 1, 1);
2018-09-27 00:33:08 +01:00
} else {
tileset->metatiles_path = dir_path + "/metatiles.bin";
2018-09-27 00:33:08 +01:00
}
const QStringList metatile_attrs_values = parser.getLabelValues(metatiles_macros, tileset->metatile_attrs_label);
if (!metatile_attrs_values.isEmpty()) {
tileset->metatile_attrs_path = root + '/' + metatile_attrs_values.value(0).section('"', 1, 1);
2018-09-27 00:33:08 +01:00
} else {
tileset->metatile_attrs_path = dir_path + "/metatile_attributes.bin";
2018-09-27 00:33:08 +01:00
}
tiles_path = fixGraphicPath(tiles_path);
2018-10-03 01:01:24 +01:00
tileset->tilesImagePath = tiles_path;
QImage image;
if (QFile::exists(tileset->tilesImagePath)) {
image = QImage(tileset->tilesImagePath);
} else {
image = QImage(8, 8, QImage::Format_Indexed8);
}
2018-10-03 01:01:24 +01:00
this->loadTilesetTiles(tileset, image);
this->loadTilesetMetatiles(tileset);
this->loadTilesetMetatileLabels(tileset);
2018-10-03 01:01:24 +01:00
// palettes
2021-02-17 02:45:54 +00:00
QList<QList<QRgb>> palettes;
QList<QList<QRgb>> palettePreviews;
2018-10-03 01:01:41 +01:00
for (int i = 0; i < tileset->palettePaths.length(); i++) {
2018-10-03 01:01:24 +01:00
QList<QRgb> palette;
2018-10-03 01:01:41 +01:00
QString path = tileset->palettePaths.value(i);
QString text = parser.readTextFile(path);
2018-10-03 01:01:41 +01:00
if (!text.isNull()) {
2021-02-06 00:43:49 +00:00
QStringList lines = text.split(QRegularExpression("[\r\n]"), Qt::SkipEmptyParts);
2018-10-03 01:01:41 +01:00
if (lines.length() == 19 && lines[0] == "JASC-PAL" && lines[1] == "0100" && lines[2] == "16") {
for (int j = 0; j < 16; j++) {
2021-02-06 00:43:49 +00:00
QStringList rgb = lines[j + 3].split(QRegularExpression(" "), Qt::SkipEmptyParts);
2018-10-03 01:01:41 +01:00
if (rgb.length() != 3) {
logWarn(QString("Invalid tileset palette RGB value: '%1'").arg(lines[j + 3]));
2018-10-03 01:01:41 +01:00
palette.append(qRgb((j - 3) * 16, (j - 3) * 16, (j - 3) * 16));
} else {
int red = rgb[0].toInt();
int green = rgb[1].toInt();
int blue = rgb[2].toInt();
QRgb color = qRgb(red, green, blue);
palette.append(color);
}
}
} else {
logError(QString("Invalid JASC-PAL palette file for tileset: '%1'").arg(path));
2018-10-03 01:01:41 +01:00
for (int j = 0; j < 16; j++) {
palette.append(qRgb(j * 16, j * 16, j * 16));
}
2018-10-03 01:01:24 +01:00
}
} else {
for (int j = 0; j < 16; j++) {
palette.append(qRgb(j * 16, j * 16, j * 16));
}
logError(QString("Could not open tileset palette path '%1'").arg(path));
2018-10-03 01:01:24 +01:00
}
2021-02-17 02:45:54 +00:00
palettes.append(palette);
palettePreviews.append(palette);
2018-10-03 01:01:24 +01:00
}
tileset->palettes = palettes;
2020-05-03 16:31:44 +01:00
tileset->palettePreviews = palettePreviews;
2018-10-03 01:01:24 +01:00
}
2018-09-27 00:33:08 +01:00
2018-10-03 01:01:24 +01:00
void Project::loadTilesetTiles(Tileset *tileset, QImage image) {
2021-02-17 02:45:54 +00:00
QList<QImage> tiles;
2018-09-27 00:33:08 +01:00
int w = 8;
int h = 8;
2018-10-03 01:01:24 +01:00
for (int y = 0; y < image.height(); y += h)
for (int x = 0; x < image.width(); x += w) {
QImage tile = image.copy(x, y, w, h);
2021-02-17 02:45:54 +00:00
tiles.append(tile);
2018-09-27 00:33:08 +01:00
}
2018-10-03 01:01:24 +01:00
tileset->tilesImage = image;
2018-09-27 00:33:08 +01:00
tileset->tiles = tiles;
2018-10-03 01:01:24 +01:00
}
2018-09-27 00:33:08 +01:00
2018-10-03 01:01:24 +01:00
void Project::loadTilesetMetatiles(Tileset* tileset) {
QFile metatiles_file(tileset->metatiles_path);
2018-09-27 00:33:08 +01:00
if (metatiles_file.open(QIODevice::ReadOnly)) {
QByteArray data = metatiles_file.readAll();
2022-08-20 17:14:11 +01:00
int tilesPerMetatile = projectConfig.getTripleLayerMetatilesEnabled() ? 12 : 8;
int bytesPerMetatile = 2 * tilesPerMetatile;
int num_metatiles = data.length() / bytesPerMetatile;
2021-02-17 02:45:54 +00:00
QList<Metatile*> metatiles;
2018-09-27 00:33:08 +01:00
for (int i = 0; i < num_metatiles; i++) {
Metatile *metatile = new Metatile;
2022-08-20 17:14:11 +01:00
int index = i * bytesPerMetatile;
for (int j = 0; j < tilesPerMetatile; j++) {
2022-02-04 23:59:57 +00:00
uint16_t tileRaw = static_cast<unsigned char>(data[index++]);
tileRaw |= static_cast<unsigned char>(data[index++]) << 8;
metatile->tiles.append(Tile(tileRaw));
2018-09-27 00:33:08 +01:00
}
2021-02-17 02:45:54 +00:00
metatiles.append(metatile);
2018-09-27 00:33:08 +01:00
}
tileset->metatiles = metatiles;
} else {
2021-02-17 02:45:54 +00:00
tileset->metatiles.clear();
logError(QString("Could not open tileset metatiles file '%1'").arg(tileset->metatiles_path));
2018-09-27 00:33:08 +01:00
}
QFile attrs_file(tileset->metatile_attrs_path);
2018-09-27 00:33:08 +01:00
if (attrs_file.open(QIODevice::ReadOnly)) {
QByteArray data = attrs_file.readAll();
2021-02-17 02:45:54 +00:00
int num_metatiles = tileset->metatiles.count();
2022-02-03 23:10:50 +00:00
BaseGameVersion version = projectConfig.getBaseGameVersion();
int attrSize = Metatile::getAttributesSize(version);
int num_metatileAttrs = data.length() / attrSize;
if (num_metatiles != num_metatileAttrs) {
logWarn(QString("Metatile count %1 does not match metatile attribute count %2 in %3").arg(num_metatiles).arg(num_metatileAttrs).arg(tileset->name));
if (num_metatileAttrs > num_metatiles)
num_metatileAttrs = num_metatiles;
}
for (int i = 0; i < num_metatileAttrs; i++) {
uint32_t attributes = 0;
for (int j = 0; j < attrSize; j++)
attributes |= static_cast<unsigned char>(data.at(i * attrSize + j)) << (8 * j);
tileset->metatiles.at(i)->setAttributes(attributes, version);
}
2018-09-27 00:33:08 +01:00
} else {
logError(QString("Could not open tileset metatile attributes file '%1'").arg(tileset->metatile_attrs_path));
2018-09-27 00:33:08 +01:00
}
}
void Project::loadTilesetMetatileLabels(Tileset* tileset) {
QString tilesetPrefix = QString("METATILE_%1_").arg(QString(tileset->name).replace("gTileset_", ""));
QString metatileLabelsFilename = projectConfig.getFilePath(ProjectFilePath::constants_metatile_labels);
2020-05-22 20:52:34 +01:00
fileWatcher.addPath(root + "/" + metatileLabelsFilename);
QMap<QString, int> labels = parser.readCDefines(metatileLabelsFilename, QStringList() << tilesetPrefix);
for (QString labelName : labels.keys()) {
int metatileId = labels[labelName];
// subtract Project::num_tiles_primary from secondary metatiles
Metatile *metatile = Tileset::getMetatile(metatileId - (tileset->is_secondary == "TRUE" ? Project::num_tiles_primary : 0), tileset, nullptr);
if (metatile) {
metatile->label = labelName.replace(tilesetPrefix, "");
} else {
QString hexString = QString("%1").arg(metatileId, 3, 16, QChar('0')).toUpper();
logError(QString("Metatile 0x%1 cannot be found in tileset '%2'").arg(hexString, tileset->name));
}
}
}
2021-02-14 21:34:17 +00:00
Blockdata Project::readBlockdata(QString path) {
Blockdata blockdata;
2018-09-27 00:33:08 +01:00
QFile file(path);
if (file.open(QIODevice::ReadOnly)) {
QByteArray data = file.readAll();
for (int i = 0; (i + 1) < data.length(); i += 2) {
uint16_t word = static_cast<uint16_t>((data[i] & 0xff) + ((data[i + 1] & 0xff) << 8));
2021-02-14 21:34:17 +00:00
blockdata.append(word);
2018-09-27 00:33:08 +01:00
}
} else {
logError(QString("Failed to open blockdata path '%1'").arg(path));
2018-09-27 00:33:08 +01:00
}
return blockdata;
}
Map* Project::getMap(QString map_name) {
if (mapCache.contains(map_name)) {
return mapCache.value(map_name);
2018-09-27 00:33:08 +01:00
} else {
Map *map = loadMap(map_name);
return map;
}
}
2018-10-03 01:01:15 +01:00
Tileset* Project::getTileset(QString label, bool forceLoad) {
Tileset *existingTileset = nullptr;
if (tilesetCache.contains(label)) {
existingTileset = tilesetCache.value(label);
2018-10-03 01:01:15 +01:00
}
if (existingTileset && !forceLoad) {
2021-02-17 02:45:54 +00:00
return existingTileset;
2018-09-27 00:33:08 +01:00
} else {
2018-10-03 01:01:15 +01:00
Tileset *tileset = loadTileset(label, existingTileset);
2018-09-27 00:33:08 +01:00
return tileset;
}
}
void Project::saveTextFile(QString path, QString text) {
QFile file(path);
if (file.open(QIODevice::WriteOnly)) {
file.write(text.toUtf8());
} else {
logError(QString("Could not open '%1' for writing: ").arg(path) + file.errorString());
2018-09-27 00:33:08 +01:00
}
}
void Project::appendTextFile(QString path, QString text) {
QFile file(path);
if (file.open(QIODevice::Append)) {
file.write(text.toUtf8());
} else {
logError(QString("Could not open '%1' for appending: ").arg(path) + file.errorString());
2018-09-27 00:33:08 +01:00
}
}
void Project::deleteFile(QString path) {
QFile file(path);
if (file.exists() && !file.remove()) {
logError(QString("Could not delete file '%1': ").arg(path) + file.errorString());
2018-09-27 00:33:08 +01:00
}
}
bool Project::readWildMonData() {
extraEncounterGroups.clear();
wildMonFields.clear();
wildMonData.clear();
encounterGroupLabels.clear();
if (!userConfig.getEncounterJsonActive()) {
return true;
}
2019-07-03 21:21:48 +01:00
QString wildMonJsonFilepath = QString("%1/%2").arg(root).arg(projectConfig.getFilePath(ProjectFilePath::json_wild_encounters));
fileWatcher.addPath(wildMonJsonFilepath);
2021-08-13 00:19:21 +01:00
OrderedJson::object wildMonObj;
if (!parser.tryParseOrderedJsonFile(&wildMonObj, wildMonJsonFilepath)) {
// Failing to read wild encounters data is not a critical error, just disable the
// encounter editor and log a warning in case the user intended to have this data.
userConfig.setEncounterJsonActive(false);
emit disableWildEncountersUI();
logWarn(QString("Failed to read wild encounters from %1").arg(wildMonJsonFilepath));
return true;
2019-06-13 03:20:28 +01:00
}
2021-08-13 00:19:21 +01:00
for (OrderedJson subObjectRef : wildMonObj["wild_encounter_groups"].array_items()) {
OrderedJson::object subObject = subObjectRef.object_items();
if (!subObject["for_maps"].bool_value()) {
extraEncounterGroups.push_back(subObject);
2019-06-15 23:49:30 +01:00
continue;
}
2019-06-13 03:20:28 +01:00
2021-08-13 00:19:21 +01:00
for (OrderedJson field : subObject["fields"].array_items()) {
EncounterField encounterField;
2021-08-13 00:19:21 +01:00
OrderedJson::object fieldObj = field.object_items();
encounterField.name = fieldObj["type"].string_value();
for (auto val : fieldObj["encounter_rates"].array_items()) {
encounterField.encounterRates.append(val.int_value());
}
2021-08-13 00:19:21 +01:00
QList<QString> subGroups;
for (auto groupPair : fieldObj["groups"].object_items()) {
subGroups.append(groupPair.first);
}
for (QString group : subGroups) {
OrderedJson::object groupsObj = fieldObj["groups"].object_items();
for (auto slotNum : groupsObj[group].array_items()) {
encounterField.groups[group].append(slotNum.int_value());
}
}
2019-06-15 23:49:30 +01:00
wildMonFields.append(encounterField);
}
2021-08-13 00:19:21 +01:00
auto encounters = subObject["encounters"].array_items();
for (auto encounter : encounters) {
OrderedJson::object encounterObj = encounter.object_items();
QString mapConstant = encounterObj["map"].string_value();
2019-06-15 23:49:30 +01:00
2019-06-13 03:20:28 +01:00
WildPokemonHeader header;
for (EncounterField monField : wildMonFields) {
QString field = monField.name;
2021-08-13 00:19:21 +01:00
if (!encounterObj[field].is_null()) {
OrderedJson::object encounterFieldObj = encounterObj[field].object_items();
header.wildMons[field].active = true;
2021-08-13 00:19:21 +01:00
header.wildMons[field].encounterRate = encounterFieldObj["encounter_rate"].int_value();
for (auto mon : encounterFieldObj["mons"].array_items()) {
WildPokemon newMon;
2021-08-13 00:19:21 +01:00
OrderedJson::object monObj = mon.object_items();
newMon.minLevel = monObj["min_level"].int_value();
newMon.maxLevel = monObj["max_level"].int_value();
newMon.species = monObj["species"].string_value();
header.wildMons[field].wildPokemon.append(newMon);
}
}
}
2021-08-13 00:19:21 +01:00
wildMonData[mapConstant].insert({encounterObj["base_label"].string_value(), header});
encounterGroupLabels.append(encounterObj["base_label"].string_value());
2019-06-13 03:20:28 +01:00
}
}
2021-08-13 00:19:21 +01:00
return true;
2019-06-13 03:20:28 +01:00
}
bool Project::readMapGroups() {
mapConstantsToMapNames.clear();
mapNamesToMapConstants.clear();
mapGroups.clear();
2022-09-27 23:06:43 +01:00
QString mapGroupsFilepath = root + "/" + projectConfig.getFilePath(ProjectFilePath::json_map_groups);
fileWatcher.addPath(mapGroupsFilepath);
QJsonDocument mapGroupsDoc;
if (!parser.tryParseJsonFile(&mapGroupsDoc, mapGroupsFilepath)) {
logError(QString("Failed to read map groups from %1").arg(mapGroupsFilepath));
return false;
2018-09-27 00:33:08 +01:00
}
2019-02-01 17:43:25 +00:00
QJsonObject mapGroupsObj = mapGroupsDoc.object();
QJsonArray mapGroupOrder = mapGroupsObj["group_order"].toArray();
2018-09-27 00:33:08 +01:00
QList<QStringList> groupedMaps;
QStringList maps;
QStringList groups;
2019-02-01 17:43:25 +00:00
for (int groupIndex = 0; groupIndex < mapGroupOrder.size(); groupIndex++) {
QString groupName = mapGroupOrder.at(groupIndex).toString();
QJsonArray mapNames = mapGroupsObj.value(groupName).toArray();
2018-09-27 00:33:08 +01:00
groupedMaps.append(QStringList());
groups.append(groupName);
2019-02-01 17:43:25 +00:00
for (int j = 0; j < mapNames.size(); j++) {
QString mapName = mapNames.at(j).toString();
mapGroups.insert(mapName, groupIndex);
2019-02-01 17:43:25 +00:00
groupedMaps[groupIndex].append(mapName);
maps.append(mapName);
2018-09-27 00:33:08 +01:00
2019-02-01 17:43:25 +00:00
// Build the mapping and reverse mapping between map constants and map names.
QString mapConstant = Map::mapConstantFromName(mapName);
mapConstantsToMapNames.insert(mapConstant, mapName);
mapNamesToMapConstants.insert(mapName, mapConstant);
2018-09-27 00:33:08 +01:00
}
}
mapConstantsToMapNames.insert(NONE_MAP_CONSTANT, NONE_MAP_NAME);
mapNamesToMapConstants.insert(NONE_MAP_NAME, NONE_MAP_CONSTANT);
maps.append(NONE_MAP_NAME);
2018-09-27 00:33:08 +01:00
groupNames = groups;
groupedMapNames = groupedMaps;
mapNames = maps;
return true;
2018-09-27 00:33:08 +01:00
}
Map* Project::addNewMapToGroup(QString mapName, int groupNum, Map *newMap, bool existingLayout, bool importedMap) {
mapNames.append(mapName);
mapGroups.insert(mapName, groupNum);
groupedMapNames[groupNum].append(mapName);
2021-02-18 22:41:36 +00:00
newMap->isPersistedToFile = false;
newMap->setName(mapName);
2021-02-18 22:41:36 +00:00
mapConstantsToMapNames.insert(newMap->constantName, newMap->name);
mapNamesToMapConstants.insert(newMap->name, newMap->constantName);
2019-02-01 17:43:25 +00:00
if (!existingLayout) {
2021-02-18 22:41:36 +00:00
mapLayouts.insert(newMap->layoutId, newMap->layout);
mapLayoutsTable.append(newMap->layoutId);
2021-07-24 00:20:41 +01:00
if (!importedMap) {
setNewMapBlockdata(newMap);
}
if (newMap->layout->border.isEmpty()) {
setNewMapBorder(newMap);
}
}
loadLayoutTilesets(newMap->layout);
2021-02-18 22:41:36 +00:00
setNewMapEvents(newMap);
setNewMapConnections(newMap);
2021-02-18 22:41:36 +00:00
return newMap;
}
2018-09-27 00:33:08 +01:00
QString Project::getNewMapName() {
// Ensure default name doesn't already exist.
int i = 0;
QString newMapName;
do {
newMapName = QString("NewMap%1").arg(++i);
} while (mapNames.contains(newMapName));
2018-09-27 00:33:08 +01:00
return newMapName;
}
QStringList Project::getVisibilities() {
// TODO
QStringList names;
for (int i = 0; i < 16; i++) {
names.append(QString("%1").arg(i));
}
return names;
}
2019-04-29 01:20:48 +01:00
Project::DataQualifiers Project::getDataQualifiers(QString text, QString label) {
Project::DataQualifiers qualifiers;
QRegularExpression regex(QString("\\s*(?<static>static\\s*)?(?<const>const\\s*)?[A-Za-z0-9_\\s]*\\b%1\\b").arg(label));
QRegularExpressionMatch match = regex.match(text);
qualifiers.isStatic = match.captured("static").isNull() ? false : true;
qualifiers.isConst = match.captured("const").isNull() ? false : true;
return qualifiers;
}
2019-03-23 14:49:30 +00:00
QMap<QString, QStringList> Project::getTilesetLabels() {
2018-09-27 00:33:08 +01:00
QMap<QString, QStringList> allTilesets;
QStringList primaryTilesets;
QStringList secondaryTilesets;
allTilesets.insert("primary", primaryTilesets);
allTilesets.insert("secondary", secondaryTilesets);
QList<QString> tilesetLabelsOrdered;
2022-09-27 23:06:43 +01:00
QString filename = projectConfig.getFilePath(ProjectFilePath::tilesets_headers);
QString headers_text = parser.readTextFile(root + "/" + filename);
if (headers_text.isEmpty()) {
logError(QString("Failed to read tileset labels from %1.").arg(filename));
return QMap<QString, QStringList>();
}
2018-09-27 00:33:08 +01:00
QRegularExpression re("(?<label>[A-Za-z0-9_]*):{1,2}[A-Za-z0-9_@ ]*\\s+.+\\s+\\.byte\\s+(?<isSecondary>[A-Za-z0-9_]+)");
QRegularExpressionMatchIterator iter = re.globalMatch(headers_text);
2018-09-27 00:33:08 +01:00
while (iter.hasNext()) {
QRegularExpressionMatch match = iter.next();
QString tilesetLabel = match.captured("label");
QString secondaryTilesetValue = match.captured("isSecondary");
2018-09-27 00:33:08 +01:00
if (secondaryTilesetValue != "1" && secondaryTilesetValue != "TRUE"
&& secondaryTilesetValue != "0" && secondaryTilesetValue != "FALSE") {
logWarn(QString("Unexpected secondary tileset flag found in %1. Expected 'TRUE', 'FALSE', '1', or '0', but found '%2'")
.arg(tilesetLabel).arg(secondaryTilesetValue));
continue;
2018-09-27 00:33:08 +01:00
}
bool isSecondaryTileset = (secondaryTilesetValue == "TRUE" || secondaryTilesetValue == "1");
if (isSecondaryTileset)
allTilesets["secondary"].append(tilesetLabel);
else
allTilesets["primary"].append(tilesetLabel);
tilesetLabelsOrdered.append(tilesetLabel);
2018-09-27 00:33:08 +01:00
}
this->tilesetLabels = allTilesets;
this->tilesetLabelsOrdered = tilesetLabelsOrdered;
2018-09-27 00:33:08 +01:00
return allTilesets;
}
bool Project::readTilesetProperties() {
QStringList definePrefixes;
2021-01-25 16:04:07 +00:00
definePrefixes << "\\bNUM_";
QString filename = projectConfig.getFilePath(ProjectFilePath::constants_fieldmap);
fileWatcher.addPath(root + "/" + filename);
QMap<QString, int> defines = parser.readCDefines(filename, definePrefixes);
2018-09-27 00:33:08 +01:00
auto it = defines.find("NUM_TILES_IN_PRIMARY");
if (it != defines.end()) {
Project::num_tiles_primary = it.value();
}
else {
logWarn(QString("Value for tileset property 'NUM_TILES_IN_PRIMARY' not found. Using default (%1) instead.")
.arg(Project::num_tiles_primary));
}
it = defines.find("NUM_TILES_TOTAL");
if (it != defines.end()) {
Project::num_tiles_total = it.value();
}
else {
logWarn(QString("Value for tileset property 'NUM_TILES_TOTAL' not found. Using default (%1) instead.")
.arg(Project::num_tiles_total));
}
it = defines.find("NUM_METATILES_IN_PRIMARY");
if (it != defines.end()) {
Project::num_metatiles_primary = it.value();
}
else {
logWarn(QString("Value for tileset property 'NUM_METATILES_IN_PRIMARY' not found. Using default (%1) instead.")
.arg(Project::num_metatiles_primary));
}
it = defines.find("NUM_METATILES_TOTAL");
if (it != defines.end()) {
Project::num_metatiles_total = it.value();
}
else {
logWarn(QString("Value for tileset property 'NUM_METATILES_TOTAL' not found. Using default (%1) instead.")
.arg(Project::num_metatiles_total));
}
it = defines.find("NUM_PALS_IN_PRIMARY");
if (it != defines.end()) {
Project::num_pals_primary = it.value();
}
else {
logWarn(QString("Value for tileset property 'NUM_PALS_IN_PRIMARY' not found. Using default (%1) instead.")
.arg(Project::num_pals_primary));
}
it = defines.find("NUM_PALS_TOTAL");
if (it != defines.end()) {
Project::num_pals_total = it.value();
}
else {
logWarn(QString("Value for tileset property 'NUM_PALS_TOTAL' not found. Using default (%1) instead.")
.arg(Project::num_pals_total));
2018-09-27 00:33:08 +01:00
}
return true;
}
bool Project::readMaxMapDataSize() {
QStringList definePrefixes;
2021-01-25 16:04:07 +00:00
definePrefixes << "\\bMAX_";
QString filename = projectConfig.getFilePath(ProjectFilePath::constants_fieldmap); // already in fileWatcher from readTilesetProperties
QMap<QString, int> defines = parser.readCDefines(filename, definePrefixes);
auto it = defines.find("MAX_MAP_DATA_SIZE");
2020-05-16 21:57:03 +01:00
if (it != defines.end()) {
int min = getMapDataSize(1, 1);
if (it.value() >= min) {
Project::max_map_data_size = it.value();
calculateDefaultMapSize();
} else {
// must be large enough to support a 1x1 map
logWarn(QString("Value for map property 'MAX_MAP_DATA_SIZE' is %1, must be at least %2. Using default (%3) instead.")
.arg(it.value())
.arg(min)
.arg(Project::max_map_data_size));
}
}
else {
logWarn(QString("Value for map property 'MAX_MAP_DATA_SIZE' not found. Using default (%1) instead.")
.arg(Project::max_map_data_size));
}
return true;
2018-09-27 00:33:08 +01:00
}
2020-02-12 14:12:12 +00:00
bool Project::readRegionMapSections() {
this->mapSectionNameToValue.clear();
this->mapSectionValueToName.clear();
2021-01-25 16:04:07 +00:00
QStringList prefixes = (QStringList() << "\\bMAPSEC_");
QString filename = projectConfig.getFilePath(ProjectFilePath::constants_region_map_sections);
fileWatcher.addPath(root + "/" + filename);
2020-02-12 14:12:12 +00:00
this->mapSectionNameToValue = parser.readCDefines(filename, prefixes);
if (this->mapSectionNameToValue.isEmpty()) {
logError(QString("Failed to read region map sections from %1.").arg(filename));
return false;
}
for (QString defineName : this->mapSectionNameToValue.keys()) {
this->mapSectionValueToName.insert(this->mapSectionNameToValue[defineName], defineName);
}
2020-02-12 14:12:12 +00:00
return true;
2018-09-27 00:33:08 +01:00
}
bool Project::readHealLocations() {
dataQualifiers.clear();
healLocations.clear();
QString filename = projectConfig.getFilePath(ProjectFilePath::data_heal_locations);
fileWatcher.addPath(root + "/" + filename);
QString text = parser.readTextFile(root + "/" + filename);
text.replace(QRegularExpression("//.*?(\r\n?|\n)|/\\*.*?\\*/", QRegularExpression::DotMatchesEverythingOption), "");
if (projectConfig.getHealLocationRespawnDataEnabled()) {
dataQualifiers.insert("heal_locations", getDataQualifiers(text, "sSpawnPoints"));
QRegularExpression spawnRegex("SPAWN_(?<id>[A-Za-z0-9_]+)\\s*- 1\\]\\s* = \\{MAP_GROUP[\\(\\s]+(?<map>[A-Za-z0-9_]+)[\\s\\)]+,\\s*MAP_NUM[\\(\\s]+(\\2)[\\s\\)]+,\\s*(?<x>[0-9A-Fa-fx]+),\\s*(?<y>[0-9A-Fa-fx]+)");
QRegularExpression respawnMapRegex("SPAWN_(?<id>[A-Za-z0-9_]+)\\s*- 1\\]\\s* = \\{MAP_GROUP[\\(\\s]+(?<map>[A-Za-z0-9_]+)[\\s\\)]+,\\s*MAP_NUM[\\(\\s]+(\\2)[\\s\\)]+}");
QRegularExpression respawnNPCRegex("SPAWN_(?<id>[A-Za-z0-9_]+)\\s*- 1\\]\\s* = (?<npc>[0-9]+)");
QRegularExpressionMatchIterator spawns = spawnRegex.globalMatch(text);
QRegularExpressionMatchIterator respawnMaps = respawnMapRegex.globalMatch(text);
QRegularExpressionMatchIterator respawnNPCs = respawnNPCRegex.globalMatch(text);
// This would be better if idName was used to look up data from the other two arrays
// As it is, element total and order needs to be the same in the 3 arrays to work. This should always be true though
for (int i = 1; spawns.hasNext(); i++) {
QRegularExpressionMatch spawn = spawns.next();
QRegularExpressionMatch respawnMap = respawnMaps.next();
QRegularExpressionMatch respawnNPC = respawnNPCs.next();
QString idName = spawn.captured("id");
QString mapName = spawn.captured("map");
QString respawnMapName = respawnMap.captured("map");
unsigned x = spawn.captured("x").toUShort();
unsigned y = spawn.captured("y").toUShort();
unsigned npc = respawnNPC.captured("npc").toUShort();
healLocations.append(HealLocation(idName, mapName, i, x, y, respawnMapName, npc));
}
} else {
dataQualifiers.insert("heal_locations", getDataQualifiers(text, "sHealLocations"));
QRegularExpression regex("HEAL_LOCATION_(?<id>[A-Za-z0-9_]+)\\s*- 1\\]\\s* = \\{MAP_GROUP[\\(\\s]+(?<map>[A-Za-z0-9_]+)[\\s\\)]+,\\s*MAP_NUM[\\(\\s]+(\\2)[\\s\\)]+,\\s*(?<x>[0-9A-Fa-fx]+),\\s*(?<y>[0-9A-Fa-fx]+)");
QRegularExpressionMatchIterator iter = regex.globalMatch(text);
for (int i = 1; iter.hasNext(); i++) {
QRegularExpressionMatch match = iter.next();
QString idName = match.captured("id");
QString mapName = match.captured("map");
unsigned x = match.captured("x").toUShort();
unsigned y = match.captured("y").toUShort();
healLocations.append(HealLocation(idName, mapName, i, x, y));
}
}
return true;
}
bool Project::readItemNames() {
QStringList prefixes("\\bITEM_(?!(B_)?USE_)"); // Exclude ITEM_USE_ and ITEM_B_USE_ constants
QString filename = projectConfig.getFilePath(ProjectFilePath::constants_items);
fileWatcher.addPath(root + "/" + filename);
itemNames = parser.readCDefinesSorted(filename, prefixes);
if (itemNames.isEmpty()) {
logError(QString("Failed to read item constants from %1").arg(filename));
return false;
}
return true;
2018-09-27 00:33:08 +01:00
}
bool Project::readFlagNames() {
2020-07-13 21:36:43 +01:00
// First read MAX_TRAINERS_COUNT, used to skip over trainer flags
// If this fails flags may simply be out of order, no need to check for success
QString opponentsFilename = projectConfig.getFilePath(ProjectFilePath::constants_opponents);
2020-07-13 21:36:43 +01:00
fileWatcher.addPath(root + "/" + opponentsFilename);
2021-01-25 16:04:07 +00:00
QMap<QString, int> maxTrainers = parser.readCDefines(opponentsFilename, QStringList() << "\\bMAX_");
2020-07-13 21:36:43 +01:00
// Parse flags
QStringList prefixes("\\bFLAG_");
QString flagsFilename = projectConfig.getFilePath(ProjectFilePath::constants_flags);
2020-07-13 21:36:43 +01:00
fileWatcher.addPath(root + "/" + flagsFilename);
flagNames = parser.readCDefinesSorted(flagsFilename, prefixes, maxTrainers);
if (flagNames.isEmpty()) {
2020-07-13 21:36:43 +01:00
logError(QString("Failed to read flag constants from %1").arg(flagsFilename));
return false;
}
return true;
2018-09-27 00:33:08 +01:00
}
bool Project::readVarNames() {
QStringList prefixes("\\bVAR_");
QString filename = projectConfig.getFilePath(ProjectFilePath::constants_vars);
fileWatcher.addPath(root + "/" + filename);
varNames = parser.readCDefinesSorted(filename, prefixes);
if (varNames.isEmpty()) {
logError(QString("Failed to read var constants from %1").arg(filename));
return false;
}
return true;
2018-09-27 00:33:08 +01:00
}
bool Project::readMovementTypes() {
QStringList prefixes("\\bMOVEMENT_TYPE_");
QString filename = projectConfig.getFilePath(ProjectFilePath::constants_obj_event_movement);
fileWatcher.addPath(root + "/" + filename);
movementTypes = parser.readCDefinesSorted(filename, prefixes);
if (movementTypes.isEmpty()) {
logError(QString("Failed to read movement type constants from %1").arg(filename));
return false;
}
return true;
2018-09-27 00:33:08 +01:00
}
bool Project::readInitialFacingDirections() {
QString filename = projectConfig.getFilePath(ProjectFilePath::path_initial_facing_table);
fileWatcher.addPath(root + "/" + filename);
facingDirections = parser.readNamedIndexCArray(filename, "gInitialMovementTypeFacingDirections");
if (facingDirections.isEmpty()) {
logError(QString("Failed to read initial movement type facing directions from %1").arg(filename));
return false;
}
return true;
2019-04-03 00:51:33 +01:00
}
bool Project::readMapTypes() {
QStringList prefixes("\\bMAP_TYPE_");
QString filename = projectConfig.getFilePath(ProjectFilePath::constants_map_types);
fileWatcher.addPath(root + "/" + filename);
mapTypes = parser.readCDefinesSorted(filename, prefixes);
if (mapTypes.isEmpty()) {
logError(QString("Failed to read map type constants from %1").arg(filename));
return false;
}
return true;
2018-09-27 00:33:08 +01:00
}
bool Project::readMapBattleScenes() {
QStringList prefixes("\\bMAP_BATTLE_SCENE_");
QString filename = projectConfig.getFilePath(ProjectFilePath::constants_map_types);
fileWatcher.addPath(root + "/" + filename);
mapBattleScenes = parser.readCDefinesSorted(filename, prefixes);
if (mapBattleScenes.isEmpty()) {
logError(QString("Failed to read map battle scene constants from %1").arg(filename));
return false;
}
return true;
2018-09-27 00:33:08 +01:00
}
bool Project::readWeatherNames() {
QStringList prefixes("\\bWEATHER_");
QString filename = projectConfig.getFilePath(ProjectFilePath::constants_weather);
fileWatcher.addPath(root + "/" + filename);
weatherNames = parser.readCDefinesSorted(filename, prefixes);
if (weatherNames.isEmpty()) {
logError(QString("Failed to read weather constants from %1").arg(filename));
return false;
}
return true;
2018-09-27 00:33:08 +01:00
}
bool Project::readCoordEventWeatherNames() {
if (!projectConfig.getEventWeatherTriggerEnabled())
return true;
QStringList prefixes("\\bCOORD_EVENT_WEATHER_");
QString filename = projectConfig.getFilePath(ProjectFilePath::constants_weather);
fileWatcher.addPath(root + "/" + filename);
coordEventWeatherNames = parser.readCDefinesSorted(filename, prefixes);
if (coordEventWeatherNames.isEmpty()) {
logError(QString("Failed to read coord event weather constants from %1").arg(filename));
return false;
}
return true;
2018-09-27 00:33:08 +01:00
}
bool Project::readSecretBaseIds() {
if (!projectConfig.getEventSecretBaseEnabled())
return true;
QStringList prefixes("\\bSECRET_BASE_[A-Za-z0-9_]*_[0-9]+");
QString filename = projectConfig.getFilePath(ProjectFilePath::constants_secret_bases);
fileWatcher.addPath(root + "/" + filename);
2022-09-26 05:36:42 +01:00
secretBaseIds = parser.readCDefinesSorted(filename, prefixes);
if (secretBaseIds.isEmpty()) {
logError(QString("Failed to read secret base id constants from %1").arg(filename));
return false;
}
return true;
2018-09-27 00:33:08 +01:00
}
bool Project::readBgEventFacingDirections() {
QStringList prefixes("\\bBG_EVENT_PLAYER_FACING_");
QString filename = projectConfig.getFilePath(ProjectFilePath::constants_event_bg);
fileWatcher.addPath(root + "/" + filename);
bgEventFacingDirections = parser.readCDefinesSorted(filename, prefixes);
if (bgEventFacingDirections.isEmpty()) {
logError(QString("Failed to read bg event facing direction constants from %1").arg(filename));
return false;
}
return true;
2018-09-27 00:33:08 +01:00
}
2020-03-27 14:51:57 +00:00
bool Project::readTrainerTypes() {
QStringList prefixes("\\bTRAINER_TYPE_");
QString filename = projectConfig.getFilePath(ProjectFilePath::constants_trainer_types);
fileWatcher.addPath(root + "/" + filename);
trainerTypes = parser.readCDefinesSorted(filename, prefixes);
if (trainerTypes.isEmpty()) {
2020-03-27 14:51:57 +00:00
logError(QString("Failed to read trainer type constants from %1").arg(filename));
return false;
}
return true;
}
bool Project::readMetatileBehaviors() {
this->metatileBehaviorMap.clear();
this->metatileBehaviorMapInverse.clear();
QStringList prefixes("\\bMB_");
QString filename = projectConfig.getFilePath(ProjectFilePath::constants_metatile_behaviors);
fileWatcher.addPath(root + "/" + filename);
this->metatileBehaviorMap = parser.readCDefines(filename, prefixes);
if (this->metatileBehaviorMap.isEmpty()) {
2020-03-27 14:51:57 +00:00
logError(QString("Failed to read metatile behaviors from %1.").arg(filename));
return false;
}
for (QString defineName : this->metatileBehaviorMap.keys()) {
this->metatileBehaviorMapInverse.insert(this->metatileBehaviorMap[defineName], defineName);
}
return true;
}
bool Project::readSongNames() {
QStringList songDefinePrefixes{ "\\bSE_", "\\bMUS_" };
QString filename = projectConfig.getFilePath(ProjectFilePath::constants_songs);
fileWatcher.addPath(root + "/" + filename);
QMap<QString, int> songDefines = parser.readCDefines(filename, songDefinePrefixes);
this->songNames = songDefines.keys();
this->defaultSong = this->songNames.value(0, "MUS_DUMMY");
if (this->songNames.isEmpty()) {
logError(QString("Failed to read song names from %1.").arg(filename));
return false;
}
return true;
2018-09-27 00:33:08 +01:00
}
bool Project::readObjEventGfxConstants() {
QStringList objEventGfxPrefixes("\\bOBJ_EVENT_GFX_");
QString filename = projectConfig.getFilePath(ProjectFilePath::constants_obj_events);
fileWatcher.addPath(root + "/" + filename);
2022-02-06 02:31:54 +00:00
this->gfxDefines = parser.readCDefines(filename, objEventGfxPrefixes);
if (this->gfxDefines.isEmpty()) {
logError(QString("Failed to read object event graphics constants from %1.").arg(filename));
return false;
}
return true;
2018-09-27 00:33:08 +01:00
}
bool Project::readMiscellaneousConstants() {
miscConstants.clear();
if (userConfig.getEncounterJsonActive()) {
QString filename = projectConfig.getFilePath(ProjectFilePath::constants_pokemon);
fileWatcher.addPath(root + "/" + filename);
QMap<QString, int> pokemonDefines = parser.readCDefines(filename, { "MIN_", "MAX_" });
miscConstants.insert("max_level_define", pokemonDefines.value("MAX_LEVEL") > pokemonDefines.value("MIN_LEVEL") ? pokemonDefines.value("MAX_LEVEL") : 100);
miscConstants.insert("min_level_define", pokemonDefines.value("MIN_LEVEL") < pokemonDefines.value("MAX_LEVEL") ? pokemonDefines.value("MIN_LEVEL") : 1);
}
2020-07-10 21:34:42 +01:00
QString filename = projectConfig.getFilePath(ProjectFilePath::constants_global);
2020-07-10 21:34:42 +01:00
fileWatcher.addPath(root + "/" + filename);
QStringList definePrefixes("\\bOBJECT_");
2020-07-10 21:34:42 +01:00
QMap<QString, int> defines = parser.readCDefines(filename, definePrefixes);
auto it = defines.find("OBJECT_EVENT_TEMPLATES_COUNT");
if (it != defines.end()) {
if (it.value() > 0) {
Project::max_object_events = it.value();
} else {
logWarn(QString("Value for 'OBJECT_EVENT_TEMPLATES_COUNT' is %1, must be greater than 0. Using default (%2) instead.")
.arg(it.value())
.arg(Project::max_object_events));
}
}
else {
logWarn(QString("Value for 'OBJECT_EVENT_TEMPLATES_COUNT' not found. Using default (%1) instead.")
.arg(Project::max_object_events));
}
return true;
2019-07-03 03:08:58 +01:00
}
bool Project::readEventScriptLabels() {
for (const auto &filePath : getEventScriptsFilePaths())
globalScriptLabels << ParseUtil::getGlobalScriptLabels(filePath);
eventScriptLabelModel.setStringList(globalScriptLabels);
eventScriptLabelCompleter.setModel(&eventScriptLabelModel);
eventScriptLabelCompleter.setCaseSensitivity(Qt::CaseInsensitive);
eventScriptLabelCompleter.setFilterMode(Qt::MatchContains);
return true;
}
2018-10-03 01:01:41 +01:00
QString Project::fixPalettePath(QString path) {
2021-02-06 00:43:49 +00:00
path = path.replace(QRegularExpression("\\.gbapal$"), ".pal");
2018-10-03 01:01:41 +01:00
return path;
}
2018-09-27 00:33:08 +01:00
QString Project::fixGraphicPath(QString path) {
2021-02-06 00:43:49 +00:00
path = path.replace(QRegularExpression("\\.lz$"), "");
path = path.replace(QRegularExpression("\\.[1248]bpp$"), ".png");
2018-09-27 00:33:08 +01:00
return path;
}
QString Project::getScriptFileExtension(bool usePoryScript) const {
if(usePoryScript) {
return ".pory";
} else {
return ".inc";
}
}
QString Project::getScriptDefaultString(bool usePoryScript, QString mapName) const {
if(usePoryScript)
2021-03-07 15:16:54 +00:00
return QString("mapscripts %1_MapScripts {}\n").arg(mapName);
else
return QString("%1_MapScripts::\n\t.byte 0\n").arg(mapName);
}
QString Project::getMapScriptsFilePath(const QString &mapName) const {
const bool usePoryscript = projectConfig.getUsePoryScript();
auto path = QDir::cleanPath(root + "/" + projectConfig.getFilePath(ProjectFilePath::data_map_folders) + "/" + mapName + "/scripts");
auto extension = getScriptFileExtension(usePoryscript);
if (usePoryscript && !QFile::exists(path + extension))
extension = getScriptFileExtension(false);
path += extension;
return path;
}
QStringList Project::getEventScriptsFilePaths() const {
QStringList filePaths(QDir::cleanPath(root + "/" + projectConfig.getFilePath(ProjectFilePath::data_event_scripts)));
const QString scriptsDir = QDir::cleanPath(root + "/" + projectConfig.getFilePath(ProjectFilePath::data_scripts_folders));
const QString mapsDir = QDir::cleanPath(root + "/" + projectConfig.getFilePath(ProjectFilePath::data_map_folders));
const bool usePoryscript = projectConfig.getUsePoryScript();
if (usePoryscript) {
QDirIterator it_pory_shared(scriptsDir, {"*.pory"}, QDir::Files);
while (it_pory_shared.hasNext())
filePaths << it_pory_shared.next();
QDirIterator it_pory_maps(mapsDir, {"scripts.pory"}, QDir::Files, QDirIterator::Subdirectories);
while (it_pory_maps.hasNext())
filePaths << it_pory_maps.next();
}
QDirIterator it_inc_shared(scriptsDir, {"*.inc"}, QDir::Files);
while (it_inc_shared.hasNext())
filePaths << it_inc_shared.next();
QDirIterator it_inc_maps(mapsDir, {"scripts.inc"}, QDir::Files, QDirIterator::Subdirectories);
while (it_inc_maps.hasNext())
filePaths << it_inc_maps.next();
return filePaths;
}
QCompleter *Project::getEventScriptLabelCompleter(QStringList additionalScriptLabels) {
additionalScriptLabels << globalScriptLabels;
additionalScriptLabels.removeDuplicates();
eventScriptLabelModel.setStringList(additionalScriptLabels);
return &eventScriptLabelCompleter;
}
void Project::setEventPixmap(Event * event, bool forceLoad) {
if (!event || (!event->pixmap.isNull() && !forceLoad))
2018-09-27 00:33:08 +01:00
return;
event->spriteWidth = 16;
event->spriteHeight = 16;
event->usingSprite = false;
2022-02-06 02:31:54 +00:00
QString group_type = event->get("event_group_type");
if (group_type == EventGroup::Object) {
QString gfxName;
QString movement;
QString event_type = event->get("event_type");
if (event_type == EventType::CloneObject) {
// Try to get the targeted object to clone
int eventIndex = event->getInt("target_local_id") - 1;
Map * clonedMap = getMap(event->get("target_map"));
Event * clonedEvent = clonedMap ? clonedMap->events[EventGroup::Object].value(eventIndex, nullptr) : nullptr;
if (clonedEvent && clonedEvent->get("event_type") == EventType::Object) {
// Get graphics data from cloned object
gfxName = clonedEvent->get("sprite");
movement = clonedEvent->get("movement_type");
} else {
// Invalid object specified, use default graphics data (as would be shown in-game)
gfxName = gfxDefines.key(0);
movement = movementTypes.first();
}
// Update clone object's sprite text to match target object
event->put("sprite", gfxName);
} else {
// Get graphics data of regular object
gfxName = event->get("sprite");
movement = event->get("movement_type");
}
EventGraphics * eventGfx = eventGraphicsMap.value(gfxName, nullptr);
if (!eventGfx || eventGfx->spritesheet.isNull()) {
// No sprite associated with this gfx constant.
// Use default sprite instead.
event->pixmap = QPixmap(":/images/Entities_16x16.png").copy(0, 0, 16, 16);
} else {
2022-02-06 02:31:54 +00:00
event->setFrameFromMovement(facingDirections.value(movement));
event->setPixmapFromSpritesheet(eventGfx->spritesheet, eventGfx->spriteWidth, eventGfx->spriteHeight, eventGfx->inanimate);
}
2022-02-06 02:31:54 +00:00
} else if (group_type == EventGroup::Warp) {
event->pixmap = QPixmap(":/images/Entities_16x16.png").copy(16, 0, 16, 16);
2022-02-06 02:31:54 +00:00
} else if (group_type == EventGroup::Coord) {
event->pixmap = QPixmap(":/images/Entities_16x16.png").copy(32, 0, 16, 16);
2022-02-06 02:31:54 +00:00
} else if (group_type == EventGroup::Bg) {
event->pixmap = QPixmap(":/images/Entities_16x16.png").copy(48, 0, 16, 16);
2022-02-06 02:31:54 +00:00
} else if (group_type == EventGroup::Heal) {
event->pixmap = QPixmap(":/images/Entities_16x16.png").copy(64, 0, 16, 16);
2018-09-27 00:33:08 +01:00
}
}
2018-09-27 00:33:08 +01:00
bool Project::readEventGraphics() {
fileWatcher.addPaths(QStringList() << root + "/" + projectConfig.getFilePath(ProjectFilePath::data_obj_event_gfx_pointers)
<< root + "/" + projectConfig.getFilePath(ProjectFilePath::data_obj_event_gfx_info)
<< root + "/" + projectConfig.getFilePath(ProjectFilePath::data_obj_event_pic_tables)
<< root + "/" + projectConfig.getFilePath(ProjectFilePath::data_obj_event_gfx));
QMap<QString, QString> pointerHash = parser.readNamedIndexCArray(projectConfig.getFilePath(ProjectFilePath::data_obj_event_gfx_pointers), "gObjectEventGraphicsInfoPointers");
2019-08-27 16:30:35 +01:00
qDeleteAll(eventGraphicsMap);
eventGraphicsMap.clear();
2022-02-06 02:31:54 +00:00
QStringList gfxNames = gfxDefines.keys();
QMap<QString, QMap<QString, QString>> gfxInfos = readObjEventGfxInfo();
2022-02-06 02:31:54 +00:00
for (QString gfxName : gfxNames) {
EventGraphics * eventGraphics = new EventGraphics;
QString info_label = pointerHash[gfxName].replace("&", "");
if (!gfxInfos.contains(info_label))
continue;
QMap<QString, QString>gfxInfoAttributes = gfxInfos[info_label];
eventGraphics->inanimate = gfxInfoAttributes.value("inanimate") == "TRUE";
QString pic_label = gfxInfoAttributes.value("images");
QString dimensions_label = gfxInfoAttributes.value("oam");
QString subsprites_label = gfxInfoAttributes.value("subspriteTables");
QString gfx_label = parser.readCArray(projectConfig.getFilePath(ProjectFilePath::data_obj_event_pic_tables), pic_label).value(0);
gfx_label = gfx_label.section(QRegularExpression("[\\(\\)]"), 1, 1);
QString path = parser.readCIncbin(projectConfig.getFilePath(ProjectFilePath::data_obj_event_gfx), gfx_label);
if (!path.isNull()) {
path = fixGraphicPath(path);
eventGraphics->spritesheet = QImage(root + "/" + path);
if (!eventGraphics->spritesheet.isNull()) {
// Infer the sprite dimensions from the OAM labels.
QRegularExpression re("\\S+_(\\d+)x(\\d+)");
QRegularExpressionMatch dimensionMatch = re.match(dimensions_label);
QRegularExpressionMatch oamTablesMatch = re.match(subsprites_label);
if (oamTablesMatch.hasMatch()) {
eventGraphics->spriteWidth = oamTablesMatch.captured(1).toInt();
eventGraphics->spriteHeight = oamTablesMatch.captured(2).toInt();
} else if (dimensionMatch.hasMatch()) {
eventGraphics->spriteWidth = dimensionMatch.captured(1).toInt();
eventGraphics->spriteHeight = dimensionMatch.captured(2).toInt();
} else {
eventGraphics->spriteWidth = eventGraphics->spritesheet.width();
eventGraphics->spriteHeight = eventGraphics->spritesheet.height();
2018-09-27 00:33:08 +01:00
}
}
} else {
eventGraphics->spritesheet = QImage();
eventGraphics->spriteWidth = 16;
eventGraphics->spriteHeight = 16;
2018-09-27 00:33:08 +01:00
}
eventGraphicsMap.insert(gfxName, eventGraphics);
2018-09-27 00:33:08 +01:00
}
return true;
2018-09-27 00:33:08 +01:00
}
QMap<QString, QMap<QString, QString>> Project::readObjEventGfxInfo() {
// TODO: refactor this to be more general if we end up directly parsing C
// for more use cases in the future.
auto cParser = fex::Parser();
auto tokens = fex::Lexer().LexFile((root + "/" + projectConfig.getFilePath(ProjectFilePath::data_obj_event_gfx_info)).toStdString());
auto gfxInfoObjects = cParser.ParseTopLevelObjects(tokens);
QMap<QString, QMap<QString, QString>> gfxInfos;
for (auto it = gfxInfoObjects.begin(); it != gfxInfoObjects.end(); it++) {
QMap<QString, QString> values;
int i = 0;
for (const fex::ArrayValue &v : it->second.values()) {
if (v.type() == fex::ArrayValue::Type::kValuePair) {
QString key = QString::fromStdString(v.pair().first);
QString value = QString::fromStdString(v.pair().second->string_value());
values.insert(key, value);
} else {
// This is for backwards compatibility with the old-style version of
// object_event_graphics_info.h, in which the structs didn't use
// attribute names to specify each struct member.
switch (i) {
case 8:
values.insert("inanimate", QString::fromStdString(v.string_value()));
break;
case 11:
values.insert("oam", QString::fromStdString(v.string_value()));
break;
case 12:
values.insert("subspriteTables", QString::fromStdString(v.string_value()));
break;
case 14:
values.insert("images", QString::fromStdString(v.string_value()));
break;
}
}
i++;
}
gfxInfos.insert(QString::fromStdString(it->first), values);
}
return gfxInfos;
}
bool Project::readSpeciesIconPaths() {
speciesToIconPath.clear();
QString srcfilename = projectConfig.getFilePath(ProjectFilePath::path_pokemon_icon_table);
QString incfilename = projectConfig.getFilePath(ProjectFilePath::data_pokemon_gfx);
fileWatcher.addPath(root + "/" + srcfilename);
fileWatcher.addPath(root + "/" + incfilename);
QMap<QString, QString> monIconNames = parser.readNamedIndexCArray(srcfilename, "gMonIconTable");
2019-06-13 03:20:28 +01:00
for (QString species : monIconNames.keys()) {
QString path = parser.readCIncbin(incfilename, monIconNames.value(species));
2019-06-13 03:20:28 +01:00
speciesToIconPath.insert(species, root + "/" + path.replace("4bpp", "png"));
}
return true;
2019-06-13 03:20:28 +01:00
}
2019-02-01 17:43:25 +00:00
void Project::saveMapHealEvents(Map *map) {
2018-09-27 00:33:08 +01:00
// save heal event changes
2022-02-05 23:53:04 +00:00
if (map->events[EventGroup::Heal].length() > 0) {
for (Event *healEvent : map->events[EventGroup::Heal]) {
2018-09-27 00:33:08 +01:00
HealLocation hl = HealLocation::fromEvent(healEvent);
healLocations[hl.index - 1] = hl;
2018-09-27 00:33:08 +01:00
}
}
saveHealLocationStruct(map);
}
void Project::setNewMapEvents(Map *map) {
2022-02-05 23:53:04 +00:00
map->events[EventGroup::Object].clear();
map->events[EventGroup::Warp].clear();
map->events[EventGroup::Heal].clear();
map->events[EventGroup::Coord].clear();
map->events[EventGroup::Bg].clear();
2018-09-27 00:33:08 +01:00
}
int Project::getNumTilesPrimary()
{
return Project::num_tiles_primary;
}
int Project::getNumTilesTotal()
{
return Project::num_tiles_total;
}
int Project::getNumMetatilesPrimary()
{
return Project::num_metatiles_primary;
}
int Project::getNumMetatilesTotal()
{
return Project::num_metatiles_total;
}
int Project::getNumPalettesPrimary()
{
return Project::num_pals_primary;
}
int Project::getNumPalettesTotal()
{
return Project::num_pals_total;
}
2020-05-16 21:57:03 +01:00
int Project::getMaxMapDataSize()
{
return Project::max_map_data_size;
}
int Project::getMapDataSize(int width, int height)
{
// + 15 and + 14 come from fieldmap.c in pokeruby/pokeemerald/pokefirered.
return (width + 15) * (height + 14);
}
int Project::getDefaultMapSize()
{
return Project::default_map_size;
}
int Project::getMaxMapWidth()
{
return (getMaxMapDataSize() / (1 + 14)) - 15;
}
int Project::getMaxMapHeight()
{
return (getMaxMapDataSize() / (1 + 15)) - 14;
}
bool Project::mapDimensionsValid(int width, int height) {
return getMapDataSize(width, height) <= getMaxMapDataSize();
}
2020-05-16 21:57:03 +01:00
// Get largest possible square dimensions for a map up to maximum of 20x20 (arbitrary)
bool Project::calculateDefaultMapSize(){
int max = getMaxMapDataSize();
if (max >= getMapDataSize(20, 20)) {
default_map_size = 20;
} else if (max >= getMapDataSize(1, 1)) {
// Below equation derived from max >= (x + 15) * (x + 14)
2020-05-17 00:06:52 +01:00
// x^2 + 29x + (210 - max), then complete the square and simplify
2020-05-16 21:57:03 +01:00
default_map_size = qFloor((qSqrt(4 * getMaxMapDataSize() + 1) - 29) / 2);
} else {
logError(QString("'MAX_MAP_DATA_SIZE' of %1 is too small to support a 1x1 map. Must be at least %2.")
.arg(max)
.arg(getMapDataSize(1, 1)));
return false;
}
return true;
}
2020-07-10 21:34:42 +01:00
int Project::getMaxObjectEvents()
{
return Project::max_object_events;
}