2018-09-27 00:33:08 +01:00
|
|
|
#include "project.h"
|
2018-12-26 18:20:51 +00:00
|
|
|
#include "config.h"
|
2018-09-27 00:33:08 +01:00
|
|
|
#include "history.h"
|
|
|
|
#include "historyitem.h"
|
2018-12-20 23:30:35 +00:00
|
|
|
#include "log.h"
|
2018-09-27 00:33:08 +01:00
|
|
|
#include "parseutil.h"
|
2019-04-07 01:58:38 +01:00
|
|
|
#include "paletteutil.h"
|
2018-09-27 00:33:08 +01:00
|
|
|
#include "tile.h"
|
|
|
|
#include "tileset.h"
|
|
|
|
|
|
|
|
#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>
|
2019-10-01 00:54:27 +01:00
|
|
|
#include <algorithm>
|
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;
|
|
|
|
|
|
|
|
Project::Project()
|
|
|
|
{
|
|
|
|
groupNames = new QStringList;
|
|
|
|
map_groups = new QMap<QString, int>;
|
|
|
|
mapNames = new QStringList;
|
|
|
|
itemNames = new QStringList;
|
|
|
|
flagNames = new QStringList;
|
|
|
|
varNames = new QStringList;
|
|
|
|
movementTypes = new QStringList;
|
|
|
|
mapTypes = new QStringList;
|
|
|
|
mapBattleScenes = new QStringList;
|
|
|
|
weatherNames = new QStringList;
|
|
|
|
coordEventWeatherNames = new QStringList;
|
|
|
|
secretBaseIds = new QStringList;
|
|
|
|
bgEventFacingDirections = new QStringList;
|
|
|
|
map_cache = new QMap<QString, Map*>;
|
|
|
|
mapConstantsToMapNames = new QMap<QString, QString>;
|
|
|
|
mapNamesToMapConstants = new QMap<QString, QString>;
|
|
|
|
tileset_cache = new QMap<QString, Tileset*>;
|
|
|
|
}
|
|
|
|
|
2019-05-06 17:42:09 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Map* Project::loadMap(QString map_name) {
|
|
|
|
Map *map;
|
|
|
|
if (map_cache->contains(map_name)) {
|
|
|
|
map = map_cache->value(map_name);
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2019-02-01 17:43:25 +00:00
|
|
|
if (!loadMapData(map))
|
2018-12-20 23:30:35 +00:00
|
|
|
return nullptr;
|
|
|
|
|
2019-02-01 17:43:25 +00:00
|
|
|
loadMapLayout(map);
|
2018-09-27 00:33:08 +01:00
|
|
|
map->commit();
|
|
|
|
map->metatileHistory.save();
|
|
|
|
map_cache->insert(map_name, map);
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Project::setNewMapConnections(Map *map) {
|
|
|
|
map->connections.clear();
|
|
|
|
}
|
|
|
|
|
2019-02-03 16:38:45 +00:00
|
|
|
QMap<QString, bool> Project::getTopLevelMapFields() {
|
|
|
|
if (projectConfig.getBaseGameVersion() == BaseGameVersion::pokeemerald) {
|
|
|
|
return QMap<QString, bool>
|
|
|
|
{
|
|
|
|
{"id", true},
|
|
|
|
{"name", true},
|
|
|
|
{"layout", true},
|
|
|
|
{"music", true},
|
|
|
|
{"region_map_section", true},
|
|
|
|
{"requires_flash", true},
|
|
|
|
{"weather", true},
|
|
|
|
{"map_type", true},
|
|
|
|
{"allow_bike", true},
|
|
|
|
{"allow_escape_rope", true},
|
|
|
|
{"allow_running", 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},
|
|
|
|
};
|
|
|
|
} else if (projectConfig.getBaseGameVersion() == BaseGameVersion::pokeruby) {
|
|
|
|
return QMap<QString, bool>
|
|
|
|
{
|
|
|
|
{"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},
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
logError("Invalid game version");
|
|
|
|
return QMap<QString, bool>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-01 17:43:25 +00:00
|
|
|
bool Project::loadMapData(Map* map) {
|
2018-09-27 00:33:08 +01:00
|
|
|
if (!map->isPersistedToFile) {
|
2018-12-20 23:30:35 +00:00
|
|
|
return true;
|
2018-09-27 00:33:08 +01:00
|
|
|
}
|
|
|
|
|
2019-02-01 17:43:25 +00:00
|
|
|
QString mapFilepath = QString("%1/data/maps/%2/map.json").arg(root).arg(map->name);
|
2019-04-20 15:06:59 +01:00
|
|
|
QJsonDocument mapDoc;
|
2019-05-06 17:42:09 +01:00
|
|
|
if (!parser.tryParseJsonFile(&mapDoc, mapFilepath)) {
|
2019-04-20 15:06:59 +01:00
|
|
|
logError(QString("Failed to read map data from %1").arg(mapFilepath));
|
2018-12-20 23:30:35 +00:00
|
|
|
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::pokeemerald) {
|
|
|
|
map->allowBiking = QString::number(mapObj["allow_bike"].toBool());
|
|
|
|
map->allowEscapeRope = QString::number(mapObj["allow_escape_rope"].toBool());
|
|
|
|
map->allowRunning = QString::number(mapObj["allow_running"].toBool());
|
|
|
|
}
|
2019-02-02 20:56:05 +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
|
|
|
|
map->events["object_event_group"].clear();
|
|
|
|
QJsonArray objectEventsArr = mapObj["object_events"].toArray();
|
|
|
|
for (int i = 0; i < objectEventsArr.size(); i++) {
|
|
|
|
QJsonObject event = objectEventsArr[i].toObject();
|
2019-02-03 18:26:27 +00:00
|
|
|
Event *object = new Event(event, EventType::Object);
|
2019-02-01 17:43:25 +00:00
|
|
|
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()));
|
2019-02-16 22:50:39 +00:00
|
|
|
object->put("trainer_type", event["trainer_type"].toString());
|
|
|
|
object->put("sight_radius_tree_id", event["trainer_sight_or_berry_tree_id"].toString());
|
2019-02-01 17:43:25 +00:00
|
|
|
object->put("script_label", event["script"].toString());
|
|
|
|
object->put("event_flag", event["flag"].toString());
|
|
|
|
object->put("event_group_type", "object_event_group");
|
|
|
|
map->events["object_event_group"].append(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
map->events["warp_event_group"].clear();
|
|
|
|
QJsonArray warpEventsArr = mapObj["warp_events"].toArray();
|
|
|
|
for (int i = 0; i < warpEventsArr.size(); i++) {
|
|
|
|
QJsonObject event = warpEventsArr[i].toObject();
|
2019-02-03 18:26:27 +00:00
|
|
|
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));
|
|
|
|
warp->put("event_group_type", "warp_event_group");
|
|
|
|
map->events["warp_event_group"].append(warp);
|
|
|
|
} else if (mapConstant == NONE_MAP_CONSTANT) {
|
|
|
|
warp->put("destination_map_name", NONE_MAP_NAME);
|
|
|
|
warp->put("event_group_type", "warp_event_group");
|
|
|
|
map->events["warp_event_group"].append(warp);
|
2018-12-26 18:20:51 +00:00
|
|
|
} else {
|
2019-02-01 17:43:25 +00:00
|
|
|
logError(QString("Destination map constant '%1' is invalid for warp").arg(mapConstant));
|
2018-12-26 18:20:51 +00:00
|
|
|
}
|
2019-02-01 17:43:25 +00:00
|
|
|
}
|
2018-12-26 18:20:51 +00:00
|
|
|
|
2019-02-01 17:43:25 +00:00
|
|
|
map->events["heal_event_group"].clear();
|
|
|
|
for (auto it = flyableMaps.begin(); it != flyableMaps.end(); it++) {
|
|
|
|
|
|
|
|
HealLocation loc = *it;
|
|
|
|
|
|
|
|
//if TRUE map is flyable / has healing location
|
|
|
|
if (loc.name == QString(mapNamesToMapConstants->value(map->name)).remove(0,4)) {
|
|
|
|
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.name);
|
|
|
|
heal->put("index", loc.index);
|
|
|
|
heal->put("elevation", 3); // TODO: change this?
|
|
|
|
heal->put("destination_map_name", mapConstantsToMapNames->value(map->name));
|
|
|
|
heal->put("event_group_type", "heal_event_group");
|
|
|
|
heal->put("event_type", EventType::HealLocation);
|
|
|
|
map->events["heal_event_group"].append(heal);
|
2018-12-26 18:20:51 +00:00
|
|
|
}
|
|
|
|
|
2019-02-01 17:43:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
map->events["coord_event_group"].clear();
|
|
|
|
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") {
|
2019-02-03 18:26:27 +00:00
|
|
|
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());
|
2019-02-16 20:04:20 +00:00
|
|
|
coord->put("script_var_value", event["var_value"].toString());
|
2019-02-01 17:43:25 +00:00
|
|
|
coord->put("script_label", event["script"].toString());
|
|
|
|
coord->put("event_group_type", "coord_event_group");
|
|
|
|
map->events["coord_event_group"].append(coord);
|
|
|
|
} else if (type == "weather") {
|
2019-02-03 18:26:27 +00:00
|
|
|
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());
|
|
|
|
coord->put("event_group_type", "coord_event_group");
|
|
|
|
coord->put("event_type", EventType::WeatherTrigger);
|
|
|
|
map->events["coord_event_group"].append(coord);
|
2018-12-26 18:20:51 +00:00
|
|
|
}
|
2019-02-01 17:43:25 +00:00
|
|
|
}
|
2018-12-26 18:20:51 +00:00
|
|
|
|
2019-02-01 17:43:25 +00:00
|
|
|
map->events["bg_event_group"].clear();
|
|
|
|
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") {
|
2019-02-03 18:26:27 +00:00
|
|
|
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());
|
|
|
|
bg->put("event_group_type", "bg_event_group");
|
|
|
|
map->events["bg_event_group"].append(bg);
|
|
|
|
} else if (type == "hidden_item") {
|
2019-02-03 18:26:27 +00:00
|
|
|
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());
|
|
|
|
bg->put("event_group_type", "bg_event_group");
|
|
|
|
map->events["bg_event_group"].append(bg);
|
|
|
|
} else if (type == "secret_base") {
|
2019-02-03 18:26:27 +00:00
|
|
|
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());
|
|
|
|
bg->put("event_group_type", "bg_event_group");
|
|
|
|
map->events["bg_event_group"].append(bg);
|
2018-12-26 18:20:51 +00:00
|
|
|
}
|
2019-02-01 17:43:25 +00:00
|
|
|
}
|
2018-12-26 18:20:51 +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);
|
|
|
|
map->connections.append(connection);
|
|
|
|
} else {
|
|
|
|
logError(QString("Failed to find connected map for map constant '%1'").arg(mapConstant));
|
|
|
|
}
|
|
|
|
}
|
2018-12-26 18:20:51 +00:00
|
|
|
}
|
|
|
|
|
2019-02-03 16:38:45 +00:00
|
|
|
// 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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-20 23:30:35 +00:00
|
|
|
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 (map_cache->contains(map_name)) {
|
2019-02-01 17:43:25 +00:00
|
|
|
return map_cache->value(map_name)->layoutId;
|
2018-10-05 07:02:40 +01:00
|
|
|
}
|
|
|
|
|
2019-02-01 17:43:25 +00:00
|
|
|
QString mapFilepath = QString("%1/data/maps/%2/map.json").arg(root).arg(map_name);
|
2019-04-20 15:06:59 +01:00
|
|
|
QJsonDocument mapDoc;
|
2019-05-06 17:42:09 +01:00
|
|
|
if (!parser.tryParseJsonFile(&mapDoc, mapFilepath)) {
|
2019-04-20 15:06:59 +01:00
|
|
|
logError(QString("Failed to read map layout id from %1").arg(mapFilepath));
|
2019-09-09 23:24:30 +01:00
|
|
|
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 (map_cache->contains(map_name)) {
|
|
|
|
return map_cache->value(map_name)->location;
|
|
|
|
}
|
|
|
|
|
2019-02-01 17:43:25 +00:00
|
|
|
QString mapFilepath = QString("%1/data/maps/%2/map.json").arg(root).arg(map_name);
|
2019-04-20 15:06:59 +01:00
|
|
|
QJsonDocument mapDoc;
|
2019-05-06 17:42:09 +01:00
|
|
|
if (!parser.tryParseJsonFile(&mapDoc, mapFilepath)) {
|
2019-04-20 15:06:59 +01:00
|
|
|
logError(QString("Failed to read map's region map section from %1").arg(mapFilepath));
|
2019-09-09 23:24:30 +01:00
|
|
|
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) {
|
|
|
|
map->song = "MUS_DAN02";
|
2019-02-01 17:43:25 +00:00
|
|
|
map->layoutId = QString("%1").arg(mapIndex);
|
2018-09-27 00:33:08 +01:00
|
|
|
map->location = "MAPSEC_LITTLEROOT_TOWN";
|
|
|
|
map->requiresFlash = "FALSE";
|
|
|
|
map->weather = "WEATHER_SUNNY";
|
|
|
|
map->type = "MAP_TYPE_TOWN";
|
2018-12-26 20:15:35 +00:00
|
|
|
if (projectConfig.getBaseGameVersion() == BaseGameVersion::pokeruby) {
|
|
|
|
map->show_location = "TRUE";
|
|
|
|
} else if (projectConfig.getBaseGameVersion() == BaseGameVersion::pokeemerald) {
|
|
|
|
map->allowBiking = "1";
|
|
|
|
map->allowEscapeRope = "0";
|
|
|
|
map->allowRunning = "1";
|
|
|
|
map->show_location = "1";
|
|
|
|
}
|
|
|
|
|
2018-09-27 00:33:08 +01:00
|
|
|
map->battle_scene = "MAP_BATTLE_SCENE_NORMAL";
|
|
|
|
}
|
|
|
|
|
2019-02-01 17:43:25 +00:00
|
|
|
void Project::loadMapLayout(Map* map) {
|
2018-09-27 00:33:08 +01:00
|
|
|
if (!map->isPersistedToFile) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-01 17:43:25 +00:00
|
|
|
if (!mapLayouts.contains(map->layoutId)) {
|
|
|
|
logError(QString("Error: Map '%1' has an unknown layout '%2'").arg(map->name).arg(map->layoutId));
|
|
|
|
return;
|
2018-09-27 00:33:08 +01:00
|
|
|
} else {
|
2019-02-01 17:43:25 +00:00
|
|
|
map->layout = mapLayouts[map->layoutId];
|
2018-09-27 00:33:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
loadMapTilesets(map);
|
|
|
|
loadBlockdata(map);
|
|
|
|
loadMapBorder(map);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-02-01 17:43:25 +00:00
|
|
|
QString layoutsFilepath = QString("%1/data/layouts/layouts.json").arg(root);
|
2019-04-20 15:06:59 +01:00
|
|
|
QJsonDocument layoutsDoc;
|
2019-05-06 17:42:09 +01:00
|
|
|
if (!parser.tryParseJsonFile(&layoutsDoc, layoutsFilepath)) {
|
2019-04-20 15:06:59 +01:00
|
|
|
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",
|
|
|
|
};
|
2019-02-01 17:43:25 +00:00
|
|
|
for (int i = 0; i < layouts.size(); i++) {
|
|
|
|
QJsonObject layoutObj = layouts[i].toObject();
|
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);
|
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();
|
2020-02-12 00:34:08 +00:00
|
|
|
if (layout->border_path.isEmpty()) {
|
|
|
|
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() {
|
|
|
|
QString layoutsFilepath = QString("%1/data/layouts/layouts.json").arg(root);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-02-01 17:43:25 +00:00
|
|
|
QJsonObject layoutsObj;
|
|
|
|
layoutsObj["layouts_table_label"] = layoutsLabel;
|
|
|
|
|
|
|
|
QJsonArray layoutsArr;
|
|
|
|
for (QString layoutId : mapLayoutsTableMaster) {
|
|
|
|
MapLayout *layout = mapLayouts.value(layoutId);
|
|
|
|
QJsonObject layoutObj;
|
|
|
|
layoutObj["id"] = layout->id;
|
|
|
|
layoutObj["name"] = layout->name;
|
|
|
|
layoutObj["width"] = layout->width.toInt(nullptr, 0);
|
|
|
|
layoutObj["height"] = layout->height.toInt(nullptr, 0);
|
|
|
|
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.append(layoutObj);
|
|
|
|
}
|
|
|
|
|
|
|
|
layoutsObj["layouts"] = layoutsArr;
|
|
|
|
QJsonDocument layoutsDoc(layoutsObj);
|
|
|
|
layoutsFile.write(layoutsDoc.toJson());
|
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);
|
2018-09-27 00:33:08 +01:00
|
|
|
layout->width = "20";
|
|
|
|
layout->height = "20";
|
|
|
|
layout->border_path = QString("data/layouts/%1/border.bin").arg(map->name);
|
|
|
|
layout->blockdata_path = QString("data/layouts/%1/map.bin").arg(map->name);
|
|
|
|
layout->tileset_primary_label = "gTileset_General";
|
|
|
|
layout->tileset_secondary_label = "gTileset_Petalburg";
|
|
|
|
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/data/maps/map_groups.json").arg(root);
|
|
|
|
QFile mapGroupsFile(mapGroupsFilepath);
|
|
|
|
if (!mapGroupsFile.open(QIODevice::WriteOnly)) {
|
|
|
|
logError(QString("Error: Could not open %1 for writing").arg(mapGroupsFilepath));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject mapGroupsObj;
|
|
|
|
mapGroupsObj["layouts_table_label"] = layoutsLabel;
|
|
|
|
|
|
|
|
QJsonArray groupNamesArr;
|
|
|
|
for (QString groupName : *this->groupNames) {
|
|
|
|
groupNamesArr.append(groupName);
|
|
|
|
}
|
|
|
|
mapGroupsObj["group_order"] = groupNamesArr;
|
|
|
|
|
2018-09-27 00:33:08 +01:00
|
|
|
int groupNum = 0;
|
|
|
|
for (QStringList mapNames : groupedMapNames) {
|
2019-02-01 17:43:25 +00:00
|
|
|
QJsonArray groupArr;
|
2018-09-27 00:33:08 +01:00
|
|
|
for (QString mapName : mapNames) {
|
2019-02-01 17:43:25 +00:00
|
|
|
groupArr.append(mapName);
|
2018-09-27 00:33:08 +01:00
|
|
|
}
|
2019-02-01 17:43:25 +00:00
|
|
|
mapGroupsObj[this->groupNames->at(groupNum)] = groupArr;
|
2018-09-27 00:33:08 +01:00
|
|
|
groupNum++;
|
|
|
|
}
|
|
|
|
|
2019-02-01 17:43:25 +00:00
|
|
|
QJsonDocument mapGroupsDoc(mapGroupsObj);
|
|
|
|
mapGroupsFile.write(mapGroupsDoc.toJson());
|
2018-09-27 00:33:08 +01:00
|
|
|
}
|
|
|
|
|
2019-06-15 23:49:30 +01:00
|
|
|
void Project::saveWildMonData() {
|
2019-07-03 21:21:48 +01:00
|
|
|
if (!projectConfig.getEncounterJsonActive()) return;
|
|
|
|
|
2019-06-20 17:40:36 +01:00
|
|
|
QString wildEncountersJsonFilepath = QString("%1/src/data/wild_encounters.json").arg(root);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject wildEncountersObject;
|
|
|
|
QJsonArray wildEncounterGroups = QJsonArray();
|
|
|
|
|
2019-07-03 02:44:19 +01:00
|
|
|
// gWildMonHeaders label is not mutable
|
2019-06-15 23:49:30 +01:00
|
|
|
QJsonObject monHeadersObject;
|
|
|
|
monHeadersObject["label"] = "gWildMonHeaders";
|
|
|
|
monHeadersObject["for_maps"] = true;
|
2019-06-20 17:40:36 +01:00
|
|
|
|
|
|
|
QJsonArray fieldsInfoArray;
|
2019-09-30 00:07:34 +01:00
|
|
|
for (EncounterField fieldInfo : wildMonFields) {
|
2019-06-20 17:40:36 +01:00
|
|
|
QJsonObject fieldObject;
|
|
|
|
QJsonArray rateArray;
|
|
|
|
|
2019-09-30 00:07:34 +01:00
|
|
|
for (int rate : fieldInfo.encounterRates) {
|
2019-06-20 17:40:36 +01:00
|
|
|
rateArray.append(rate);
|
2019-09-30 00:07:34 +01:00
|
|
|
}
|
2019-06-20 17:40:36 +01:00
|
|
|
|
2019-09-30 00:07:34 +01:00
|
|
|
fieldObject["type"] = fieldInfo.name;
|
2019-06-20 17:40:36 +01:00
|
|
|
fieldObject["encounter_rates"] = rateArray;
|
|
|
|
|
2019-09-30 00:07:34 +01:00
|
|
|
QJsonObject groupsObject;
|
|
|
|
for (QString groupName : fieldInfo.groups.keys()) {
|
|
|
|
QJsonArray subGroupIndices;
|
2019-10-01 00:54:27 +01:00
|
|
|
std::sort(fieldInfo.groups[groupName].begin(), fieldInfo.groups[groupName].end());
|
2019-09-30 00:07:34 +01:00
|
|
|
for (int slotIndex : fieldInfo.groups[groupName]) {
|
|
|
|
subGroupIndices.append(slotIndex);
|
|
|
|
}
|
|
|
|
groupsObject[groupName] = subGroupIndices;
|
|
|
|
}
|
2019-10-14 18:15:18 +01:00
|
|
|
if (!groupsObject.isEmpty()) fieldObject["groups"] = groupsObject;
|
2019-09-30 00:07:34 +01:00
|
|
|
|
2019-06-20 17:40:36 +01:00
|
|
|
fieldsInfoArray.append(fieldObject);
|
|
|
|
}
|
|
|
|
monHeadersObject["fields"] = fieldsInfoArray;
|
|
|
|
|
2019-06-15 23:49:30 +01:00
|
|
|
QJsonArray encountersArray = QJsonArray();
|
|
|
|
for (QString key : wildMonData.keys()) {
|
2019-06-20 17:40:36 +01:00
|
|
|
for (QString groupLabel : wildMonData.value(key).keys()) {
|
|
|
|
QJsonObject encounterObject;
|
|
|
|
encounterObject["map"] = key;
|
2019-07-03 02:44:19 +01:00
|
|
|
encounterObject["base_label"] = groupLabel;
|
2019-06-20 17:40:36 +01:00
|
|
|
|
|
|
|
WildPokemonHeader encounterHeader = wildMonData.value(key).value(groupLabel);
|
|
|
|
for (QString fieldName : encounterHeader.wildMons.keys()) {
|
|
|
|
QJsonObject fieldObject;
|
|
|
|
WildMonInfo monInfo = encounterHeader.wildMons.value(fieldName);
|
|
|
|
fieldObject["encounter_rate"] = monInfo.encounterRate;
|
|
|
|
QJsonArray monArray;
|
|
|
|
for (WildPokemon wildMon : monInfo.wildPokemon) {
|
|
|
|
QJsonObject monEntry;
|
|
|
|
monEntry["min_level"] = wildMon.minLevel;
|
|
|
|
monEntry["max_level"] = wildMon.maxLevel;
|
|
|
|
monEntry["species"] = wildMon.species;
|
|
|
|
monArray.append(monEntry);
|
|
|
|
}
|
2019-07-03 02:44:19 +01:00
|
|
|
fieldObject["mons"] = monArray;
|
2019-06-20 17:40:36 +01:00
|
|
|
encounterObject[fieldName] = fieldObject;
|
|
|
|
}
|
|
|
|
encountersArray.append(encounterObject);
|
|
|
|
}
|
2019-06-15 23:49:30 +01:00
|
|
|
}
|
|
|
|
monHeadersObject["encounters"] = encountersArray;
|
|
|
|
wildEncounterGroups.append(monHeadersObject);
|
|
|
|
|
|
|
|
// add extra Json objects that are not associated with maps to the file
|
|
|
|
for (QString label : extraEncounterGroups.keys()) {
|
|
|
|
wildEncounterGroups.append(extraEncounterGroups[label]);
|
|
|
|
}
|
|
|
|
|
|
|
|
wildEncountersObject["wild_encounter_groups"] = wildEncounterGroups;
|
|
|
|
QJsonDocument wildEncountersDoc(wildEncountersObject);
|
|
|
|
wildEncountersFile.write(wildEncountersDoc.toJson());
|
|
|
|
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");
|
2018-09-27 00:33:08 +01:00
|
|
|
text += QString("\n");
|
|
|
|
|
|
|
|
int groupNum = 0;
|
|
|
|
for (QStringList mapNames : groupedMapNames) {
|
|
|
|
text += QString("// Map Group %1\n").arg(groupNum);
|
|
|
|
int maxLength = 0;
|
|
|
|
for (QString mapName : mapNames) {
|
|
|
|
QString mapConstantName = mapNamesToMapConstants->value(mapName);
|
|
|
|
if (mapConstantName.length() > maxLength)
|
|
|
|
maxLength = mapConstantName.length();
|
|
|
|
}
|
|
|
|
int groupIndex = 0;
|
|
|
|
for (QString mapName : mapNames) {
|
|
|
|
QString mapConstantName = mapNamesToMapConstants->value(mapName);
|
|
|
|
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");
|
2019-02-01 17:43:25 +00:00
|
|
|
saveTextFile(root + "/include/constants/map_groups.h", 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) {
|
2019-04-29 01:20:48 +01:00
|
|
|
QString data_text = QString("%1%2struct HealLocation sHealLocations[] =\n{\n")
|
|
|
|
.arg(dataQualifiers.value("heal_locations").isStatic ? "static " : "")
|
|
|
|
.arg(dataQualifiers.value("heal_locations").isConst ? "const " : "");
|
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> flyableMapsDupes;
|
|
|
|
QSet<QString> flyableMapsUnique;
|
|
|
|
|
|
|
|
// set flyableMapsDupes and flyableMapsUnique
|
|
|
|
for (auto it = flyableMaps.begin(); it != flyableMaps.end(); it++) {
|
|
|
|
HealLocation loc = *it;
|
|
|
|
QString xname = loc.name;
|
|
|
|
if (flyableMapsUnique.contains(xname)) {
|
|
|
|
flyableMapsDupes[xname] = 1;
|
|
|
|
}
|
|
|
|
flyableMapsUnique.insert(xname);
|
|
|
|
}
|
|
|
|
|
|
|
|
// set new location in flyableMapsList
|
|
|
|
if (map->events["heal_event_group"].length() > 0) {
|
|
|
|
for (Event *healEvent : map->events["heal_event_group"]) {
|
|
|
|
HealLocation hl = HealLocation::fromEvent(healEvent);
|
|
|
|
flyableMaps[hl.index - 1] = hl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = 1;
|
|
|
|
|
|
|
|
for (auto map_in : flyableMaps) {
|
|
|
|
data_text += QString(" {MAP_GROUP(%1), MAP_NUM(%1), %2, %3},\n")
|
|
|
|
.arg(map_in.name)
|
|
|
|
.arg(map_in.x)
|
|
|
|
.arg(map_in.y);
|
|
|
|
|
|
|
|
QString ending = QString("");
|
|
|
|
|
|
|
|
// must add _1 / _2 for maps that have duplicates
|
|
|
|
if (flyableMapsDupes.keys().contains(map_in.name)) {
|
|
|
|
// map contains multiple heal locations
|
|
|
|
ending += QString("_%1").arg(flyableMapsDupes[map_in.name]);
|
|
|
|
flyableMapsDupes[map_in.name]++;
|
|
|
|
}
|
|
|
|
if (map_in.index != 0) {
|
|
|
|
constants_text += QString("#define HEAL_LOCATION_%1 %2\n")
|
|
|
|
.arg(map_in.name + ending)
|
|
|
|
.arg(map_in.index);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
constants_text += QString("#define HEAL_LOCATION_%1 %2\n")
|
|
|
|
.arg(map_in.name + ending)
|
|
|
|
.arg(i);
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
data_text += QString("};\n");
|
|
|
|
constants_text += QString("\n#endif // GUARD_CONSTANTS_HEAL_LOCATIONS_H\n");
|
|
|
|
|
|
|
|
saveTextFile(root + "/src/data/heal_locations.h", data_text);
|
|
|
|
saveTextFile(root + "/include/constants/heal_locations.h", constants_text);
|
|
|
|
}
|
|
|
|
|
2018-10-03 01:01:09 +01:00
|
|
|
void Project::saveTilesets(Tileset *primaryTileset, Tileset *secondaryTileset) {
|
2019-04-04 06:44:31 +01:00
|
|
|
saveTilesetMetatileLabels(primaryTileset, secondaryTileset);
|
2018-10-03 01:01:09 +01:00
|
|
|
saveTilesetMetatileAttributes(primaryTileset);
|
|
|
|
saveTilesetMetatileAttributes(secondaryTileset);
|
|
|
|
saveTilesetMetatiles(primaryTileset);
|
|
|
|
saveTilesetMetatiles(secondaryTileset);
|
2018-10-03 01:01:24 +01:00
|
|
|
saveTilesetTilesImage(primaryTileset);
|
|
|
|
saveTilesetTilesImage(secondaryTileset);
|
2018-10-03 01:01:41 +01:00
|
|
|
saveTilesetPalettes(primaryTileset, true);
|
|
|
|
saveTilesetPalettes(secondaryTileset, false);
|
2018-10-03 01:01:09 +01:00
|
|
|
}
|
|
|
|
|
2019-04-04 06:44:31 +01:00
|
|
|
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;
|
2019-09-09 23:24:30 +01:00
|
|
|
|
|
|
|
defines = parser.readCDefines("include/constants/metatile_labels.h", (QStringList() << "METATILE_"));
|
|
|
|
|
|
|
|
// Purge old entries from the file.
|
|
|
|
QStringList definesToRemove;
|
|
|
|
for (QString defineName : defines.keys()) {
|
|
|
|
if (defineName.startsWith(primaryPrefix) || defineName.startsWith(secondaryPrefix)) {
|
|
|
|
definesToRemove << defineName;
|
2019-04-04 06:44:31 +01:00
|
|
|
}
|
|
|
|
}
|
2019-09-09 23:24:30 +01:00
|
|
|
for (QString defineName : definesToRemove) {
|
|
|
|
defines.remove(defineName);
|
|
|
|
definesFileModified = true;
|
|
|
|
}
|
2019-04-04 06:44:31 +01:00
|
|
|
|
|
|
|
// Add the new labels.
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
2019-07-18 21:35:00 +01:00
|
|
|
defines.insert(defineName, i + Project::num_tiles_primary);
|
2019-04-04 06:44:31 +01:00
|
|
|
definesFileModified = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!definesFileModified) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-18 21:35:00 +01:00
|
|
|
auto getTilesetFromLabel = [](QString labelName) {
|
|
|
|
return QRegularExpression("METATILE_(?<tileset>[A-Za-z0-9]+)_").match(labelName).captured("tileset");
|
|
|
|
};
|
2019-04-04 06:44:31 +01:00
|
|
|
|
|
|
|
QString outputText = "#ifndef GUARD_METATILE_LABELS_H\n";
|
2019-07-18 21:35:00 +01:00
|
|
|
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;
|
2019-04-04 06:44:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
outputText += "\n#endif // GUARD_METATILE_LABELS_H\n";
|
2019-09-09 23:24:30 +01:00
|
|
|
saveTextFile(root + "/include/constants/metatile_labels.h", outputText);
|
2019-04-04 06:44:31 +01:00
|
|
|
}
|
|
|
|
|
2018-10-03 01:01:09 +01:00
|
|
|
void Project::saveTilesetMetatileAttributes(Tileset *tileset) {
|
|
|
|
QFile attrs_file(tileset->metatile_attrs_path);
|
|
|
|
if (attrs_file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
|
|
|
QByteArray data;
|
|
|
|
for (Metatile *metatile : *tileset->metatiles) {
|
|
|
|
data.append(static_cast<char>(metatile->behavior));
|
|
|
|
data.append(static_cast<char>((metatile->layerType << 4) & 0xF0));
|
|
|
|
}
|
|
|
|
attrs_file.write(data);
|
|
|
|
} else {
|
2018-12-20 23:30:35 +00:00
|
|
|
logError(QString("Could not save tileset metatile attributes file '%1'").arg(tileset->metatile_attrs_path));
|
2018-10-03 01:01:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Project::saveTilesetMetatiles(Tileset *tileset) {
|
|
|
|
QFile metatiles_file(tileset->metatiles_path);
|
|
|
|
if (metatiles_file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
|
|
|
QByteArray data;
|
|
|
|
for (Metatile *metatile : *tileset->metatiles) {
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
Tile tile = metatile->tiles->at(i);
|
|
|
|
uint16_t value = static_cast<uint16_t>((tile.tile & 0x3ff)
|
|
|
|
| ((tile.xflip & 1) << 10)
|
|
|
|
| ((tile.yflip & 1) << 11)
|
|
|
|
| ((tile.palette & 0xf) << 12));
|
|
|
|
data.append(static_cast<char>(value & 0xff));
|
|
|
|
data.append(static_cast<char>((value >> 8) & 0xff));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
metatiles_file.write(data);
|
|
|
|
} else {
|
|
|
|
tileset->metatiles = new QList<Metatile*>;
|
2018-12-20 23:30:35 +00:00
|
|
|
logError(QString("Could not open tileset metatiles file '%1'").arg(tileset->metatiles_path));
|
2018-10-03 01:01:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-03 01:01:24 +01:00
|
|
|
void Project::saveTilesetTilesImage(Tileset *tileset) {
|
|
|
|
tileset->tilesImage.save(tileset->tilesImagePath);
|
|
|
|
}
|
|
|
|
|
2019-08-07 04:35:02 +01:00
|
|
|
void Project::saveTilesetPalettes(Tileset *tileset, bool /*primary*/) {
|
2019-05-06 17:42:09 +01:00
|
|
|
PaletteUtil paletteParser;
|
2019-03-22 00:56:00 +00:00
|
|
|
for (int i = 0; i < Project::getNumPalettesTotal(); i++) {
|
2018-10-03 01:01:41 +01:00
|
|
|
QString filepath = tileset->palettePaths.at(i);
|
2019-05-06 17:42:09 +01:00
|
|
|
paletteParser.writeJASC(filepath, tileset->palettes->at(i).toVector(), 0, 16);
|
2018-10-03 01:01:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-27 00:33:08 +01:00
|
|
|
void Project::loadMapTilesets(Map* map) {
|
|
|
|
if (map->layout->has_unsaved_changes) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
map->layout->tileset_primary = getTileset(map->layout->tileset_primary_label);
|
|
|
|
map->layout->tileset_secondary = getTileset(map->layout->tileset_secondary_label);
|
|
|
|
}
|
|
|
|
|
2018-10-03 01:01:15 +01:00
|
|
|
Tileset* Project::loadTileset(QString label, Tileset *tileset) {
|
2019-05-06 17:42:09 +01:00
|
|
|
QStringList *values = parser.getLabelValues(parser.parseAsm("data/tilesets/headers.inc"), label);
|
|
|
|
if (values->isEmpty()) {
|
2018-12-20 23:30:35 +00:00
|
|
|
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);
|
|
|
|
tileset->metatile_attrs_label = values->value(6);
|
|
|
|
tileset->callback_label = values->value(7);
|
|
|
|
|
|
|
|
loadTilesetAssets(tileset);
|
|
|
|
|
|
|
|
tileset_cache->insert(label, tileset);
|
|
|
|
return tileset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Project::loadBlockdata(Map* map) {
|
|
|
|
if (!map->isPersistedToFile || map->layout->has_unsaved_changes) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString path = QString("%1/%2").arg(root).arg(map->layout->blockdata_path);
|
|
|
|
map->layout->blockdata = readBlockdata(path);
|
2019-01-07 18:06:25 +00:00
|
|
|
|
|
|
|
if (map->layout->blockdata->blocks->count() != map->getWidth() * map->getHeight()) {
|
|
|
|
logWarn(QString("Layout blockdata length %1 does not match dimensions %2x%3 (should be %4). Resizing blockdata.")
|
|
|
|
.arg(map->layout->blockdata->blocks->count())
|
|
|
|
.arg(map->getWidth())
|
|
|
|
.arg(map->getHeight())
|
|
|
|
.arg(map->getWidth() * map->getHeight()));
|
|
|
|
map->layout->blockdata->blocks->resize(map->getWidth() * map->getHeight());
|
|
|
|
}
|
2018-09-27 00:33:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Project::setNewMapBlockdata(Map* map) {
|
|
|
|
Blockdata *blockdata = new Blockdata;
|
|
|
|
for (int i = 0; i < map->getWidth() * map->getHeight(); i++) {
|
|
|
|
blockdata->addBlock(qint16(0x3001));
|
|
|
|
}
|
|
|
|
map->layout->blockdata = blockdata;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Project::loadMapBorder(Map *map) {
|
|
|
|
if (!map->isPersistedToFile || map->layout->has_unsaved_changes) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString path = QString("%1/%2").arg(root).arg(map->layout->border_path);
|
|
|
|
map->layout->border = readBlockdata(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Project::setNewMapBorder(Map *map) {
|
|
|
|
Blockdata *blockdata = new Blockdata;
|
|
|
|
blockdata->addBlock(qint16(0x01D4));
|
|
|
|
blockdata->addBlock(qint16(0x01D5));
|
|
|
|
blockdata->addBlock(qint16(0x01DC));
|
|
|
|
blockdata->addBlock(qint16(0x01DD));
|
|
|
|
map->layout->border = blockdata;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
map->metatileHistory.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Project::writeBlockdata(QString path, Blockdata *blockdata) {
|
|
|
|
QFile file(path);
|
|
|
|
if (file.open(QIODevice::WriteOnly)) {
|
|
|
|
QByteArray data = blockdata->serialize();
|
|
|
|
file.write(data);
|
|
|
|
} else {
|
2018-12-20 23:30:35 +00:00
|
|
|
logError(QString("Failed to open blockdata file for writing: '%1'").arg(path));
|
2018-09-27 00:33:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Project::saveAllMaps() {
|
|
|
|
QList<QString> keys = map_cache->keys();
|
|
|
|
for (int i = 0; i < keys.length(); i++) {
|
|
|
|
QString key = keys.value(i);
|
|
|
|
Map* map = map_cache->value(key);
|
|
|
|
saveMap(map);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Project::saveMap(Map *map) {
|
|
|
|
// Create/Modify a few collateral files for brand new maps.
|
2019-02-01 17:43:25 +00:00
|
|
|
QString mapDataDir = QString(root + "/data/maps/%1").arg(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
|
2019-10-22 13:48:41 +01:00
|
|
|
QString text = this->getScriptDefaultString(projectConfig.getUsePoryScript(), map->name);
|
|
|
|
saveTextFile(root + "/data/maps/" + map->name + "/scripts" + this->getScriptFileExtension(projectConfig.getUsePoryScript()), text);
|
2018-09-27 00:33:08 +01:00
|
|
|
|
2019-01-08 14:59:43 +00:00
|
|
|
if (projectConfig.getBaseGameVersion() == BaseGameVersion::pokeruby) {
|
|
|
|
// Create file data/maps/<map_name>/text.inc
|
2019-10-22 13:48:41 +01:00
|
|
|
saveTextFile(root + "/data/maps/" + map->name + "/text" + this->getScriptFileExtension(projectConfig.getUsePoryScript()), "\n");
|
2019-01-08 14:59:43 +00:00
|
|
|
}
|
2018-09-27 00:33:08 +01:00
|
|
|
|
|
|
|
// Simply append to data/event_scripts.s.
|
|
|
|
text = QString("\n\t.include \"data/maps/%1/scripts.inc\"\n").arg(map->name);
|
2019-01-08 14:59:43 +00:00
|
|
|
if (projectConfig.getBaseGameVersion() == BaseGameVersion::pokeruby) {
|
|
|
|
text += QString("\t.include \"data/maps/%1/text.inc\"\n").arg(map->name);
|
|
|
|
}
|
2018-09-27 00:33:08 +01:00
|
|
|
appendTextFile(root + "/data/event_scripts.s", text);
|
|
|
|
|
2019-02-01 17:43:25 +00:00
|
|
|
if (map->needsLayoutDir) {
|
|
|
|
QString newLayoutDir = QString(root + "/data/layouts/%1").arg(map->name);
|
|
|
|
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
|
|
|
|
2019-02-01 17:43:25 +00:00
|
|
|
QString layoutsFilepath = QString("%1/data/layouts/layouts.json").arg(root);
|
2019-04-20 15:06:59 +01:00
|
|
|
QJsonDocument layoutsDoc;
|
2019-05-06 17:42:09 +01:00
|
|
|
if (!parser.tryParseJsonFile(&layoutsDoc, layoutsFilepath)) {
|
2019-04-20 15:06:59 +01:00
|
|
|
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;
|
|
|
|
}
|
2019-04-20 15:06:59 +01:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
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());
|
2019-04-20 15:06:59 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-02-01 17:43:25 +00:00
|
|
|
QJsonObject mapObj;
|
|
|
|
// 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;
|
|
|
|
mapObj["allow_bike"] = map->allowBiking.toInt() > 0 || map->allowBiking == "TRUE";
|
|
|
|
mapObj["allow_escape_rope"] = map->allowEscapeRope.toInt() > 0 || map->allowEscapeRope == "TRUE";
|
|
|
|
mapObj["allow_running"] = map->allowRunning.toInt() > 0 || map->allowRunning == "TRUE";
|
|
|
|
mapObj["show_map_name"] = map->show_location.toInt() > 0 || map->show_location == "TRUE";
|
|
|
|
mapObj["battle_scene"] = map->battle_scene;
|
|
|
|
|
|
|
|
// Connections
|
|
|
|
if (map->connections.length() > 0) {
|
|
|
|
QJsonArray connectionsArr;
|
|
|
|
for (MapConnection* connection : map->connections) {
|
|
|
|
if (mapNamesToMapConstants->contains(connection->map_name)) {
|
|
|
|
QJsonObject connectionObj;
|
|
|
|
connectionObj["direction"] = connection->direction;
|
|
|
|
connectionObj["offset"] = connection->offset.toInt();
|
|
|
|
connectionObj["map"] = this->mapNamesToMapConstants->value(connection->map_name);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-02-02 20:56:05 +00:00
|
|
|
if (map->sharedEventsMap.isEmpty()) {
|
|
|
|
// Object events
|
|
|
|
QJsonArray objectEventsArr;
|
|
|
|
for (int i = 0; i < map->events["object_event_group"].length(); i++) {
|
|
|
|
Event *object_event = map->events["object_event_group"].value(i);
|
|
|
|
QJsonObject eventObj = object_event->buildObjectEventJSON();
|
|
|
|
objectEventsArr.append(eventObj);
|
2019-02-01 17:43:25 +00:00
|
|
|
}
|
2019-02-02 20:56:05 +00:00
|
|
|
mapObj["object_events"] = objectEventsArr;
|
|
|
|
|
|
|
|
// Warp events
|
|
|
|
QJsonArray warpEventsArr;
|
|
|
|
for (int i = 0; i < map->events["warp_event_group"].length(); i++) {
|
|
|
|
Event *warp_event = map->events["warp_event_group"].value(i);
|
|
|
|
QJsonObject warpObj = warp_event->buildWarpEventJSON(mapNamesToMapConstants);
|
|
|
|
warpEventsArr.append(warpObj);
|
|
|
|
}
|
|
|
|
mapObj["warp_events"] = warpEventsArr;
|
|
|
|
|
|
|
|
// Coord events
|
|
|
|
QJsonArray coordEventsArr;
|
|
|
|
for (int i = 0; i < map->events["coord_event_group"].length(); i++) {
|
|
|
|
Event *event = map->events["coord_event_group"].value(i);
|
|
|
|
QString event_type = event->get("event_type");
|
|
|
|
if (event_type == EventType::Trigger) {
|
|
|
|
QJsonObject triggerObj = event->buildTriggerEventJSON();
|
|
|
|
coordEventsArr.append(triggerObj);
|
|
|
|
} else if (event_type == EventType::WeatherTrigger) {
|
|
|
|
QJsonObject weatherObj = event->buildWeatherTriggerEventJSON();
|
|
|
|
coordEventsArr.append(weatherObj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mapObj["coord_events"] = coordEventsArr;
|
|
|
|
|
|
|
|
// Bg Events
|
|
|
|
QJsonArray bgEventsArr;
|
|
|
|
for (int i = 0; i < map->events["bg_event_group"].length(); i++) {
|
|
|
|
Event *event = map->events["bg_event_group"].value(i);
|
|
|
|
QString event_type = event->get("event_type");
|
|
|
|
if (event_type == EventType::Sign) {
|
|
|
|
QJsonObject signObj = event->buildSignEventJSON();
|
|
|
|
bgEventsArr.append(signObj);
|
|
|
|
} else if (event_type == EventType::HiddenItem) {
|
|
|
|
QJsonObject hiddenItemObj = event->buildHiddenItemEventJSON();
|
|
|
|
bgEventsArr.append(hiddenItemObj);
|
|
|
|
} else if (event_type == EventType::SecretBase) {
|
|
|
|
QJsonObject secretBaseObj = event->buildSecretBaseEventJSON();
|
|
|
|
bgEventsArr.append(secretBaseObj);
|
|
|
|
}
|
2019-02-01 17:43:25 +00:00
|
|
|
}
|
2019-02-02 20:56:05 +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
|
|
|
}
|
|
|
|
|
2019-02-03 16:38:45 +00:00
|
|
|
// Custom header fields.
|
|
|
|
for (QString key : map->customHeaders.keys()) {
|
|
|
|
mapObj[key] = map->customHeaders[key];
|
|
|
|
}
|
|
|
|
|
2019-02-01 17:43:25 +00:00
|
|
|
QJsonDocument mapDoc(mapObj);
|
|
|
|
mapFile.write(mapDoc.toJson());
|
2019-04-20 15:06:59 +01:00
|
|
|
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->layout->has_unsaved_changes = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2018-10-03 01:01:15 +01:00
|
|
|
QString tilesetName = tileset->name;
|
|
|
|
QString dir_path = root + "/data/tilesets/" + category + "/" + tilesetName.replace("gTileset_", "").toLower();
|
2018-09-27 00:33:08 +01:00
|
|
|
|
2019-05-06 17:42:09 +01:00
|
|
|
QList<QStringList> *graphics = parser.parseAsm("data/tilesets/graphics.inc");
|
|
|
|
QStringList *tiles_values = parser.getLabelValues(graphics, tileset->tiles_label);
|
|
|
|
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);
|
|
|
|
} else {
|
|
|
|
tiles_path = dir_path + "/tiles.4bpp";
|
|
|
|
if (tileset->is_compressed == "TRUE") {
|
|
|
|
tiles_path += ".lz";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!palettes_values->isEmpty()) {
|
|
|
|
for (int i = 0; i < palettes_values->length(); i++) {
|
|
|
|
QString value = palettes_values->value(i);
|
2018-10-03 01:01:41 +01:00
|
|
|
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++) {
|
2018-10-03 01:01:41 +01:00
|
|
|
tileset->palettePaths.append(palettes_dir_path + "/" + QString("%1").arg(i, 2, 10, QLatin1Char('0')) + ".pal");
|
2018-09-27 00:33:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-06 17:42:09 +01:00
|
|
|
QList<QStringList> *metatiles_macros = parser.parseAsm("data/tilesets/metatiles.inc");
|
|
|
|
QStringList *metatiles_values = parser.getLabelValues(metatiles_macros, tileset->metatiles_label);
|
2018-09-27 00:33:08 +01:00
|
|
|
if (!metatiles_values->isEmpty()) {
|
2018-10-03 01:01:09 +01:00
|
|
|
tileset->metatiles_path = root + "/" + metatiles_values->value(0).section('"', 1, 1);
|
2018-09-27 00:33:08 +01:00
|
|
|
} else {
|
2018-10-03 01:01:09 +01:00
|
|
|
tileset->metatiles_path = dir_path + "/metatiles.bin";
|
2018-09-27 00:33:08 +01:00
|
|
|
}
|
2019-05-06 17:42:09 +01:00
|
|
|
QStringList *metatile_attrs_values = parser.getLabelValues(metatiles_macros, tileset->metatile_attrs_label);
|
2018-09-27 00:33:08 +01:00
|
|
|
if (!metatile_attrs_values->isEmpty()) {
|
2018-10-03 01:01:09 +01:00
|
|
|
tileset->metatile_attrs_path = root + "/" + metatile_attrs_values->value(0).section('"', 1, 1);
|
2018-09-27 00:33:08 +01:00
|
|
|
} else {
|
2018-10-03 01:01:09 +01:00
|
|
|
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 = QImage(tileset->tilesImagePath);
|
|
|
|
this->loadTilesetTiles(tileset, image);
|
|
|
|
this->loadTilesetMetatiles(tileset);
|
2019-04-04 06:44:31 +01:00
|
|
|
this->loadTilesetMetatileLabels(tileset);
|
2018-10-03 01:01:24 +01:00
|
|
|
|
|
|
|
// palettes
|
|
|
|
QList<QList<QRgb>> *palettes = new QList<QList<QRgb>>;
|
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);
|
2019-05-06 17:42:09 +01:00
|
|
|
QString text = parser.readTextFile(path);
|
2018-10-03 01:01:41 +01:00
|
|
|
if (!text.isNull()) {
|
|
|
|
QStringList lines = text.split(QRegExp("[\r\n]"),QString::SkipEmptyParts);
|
|
|
|
if (lines.length() == 19 && lines[0] == "JASC-PAL" && lines[1] == "0100" && lines[2] == "16") {
|
|
|
|
for (int j = 0; j < 16; j++) {
|
|
|
|
QStringList rgb = lines[j + 3].split(QRegExp(" "), QString::SkipEmptyParts);
|
|
|
|
if (rgb.length() != 3) {
|
2018-12-20 23:30:35 +00:00
|
|
|
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 {
|
2018-12-20 23:30:35 +00:00
|
|
|
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));
|
|
|
|
}
|
2018-12-20 23:30:35 +00:00
|
|
|
logError(QString("Could not open tileset palette path '%1'").arg(path));
|
2018-10-03 01:01:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
palettes->append(palette);
|
|
|
|
}
|
|
|
|
tileset->palettes = palettes;
|
|
|
|
}
|
2018-09-27 00:33:08 +01:00
|
|
|
|
2018-10-03 01:01:24 +01:00
|
|
|
void Project::loadTilesetTiles(Tileset *tileset, QImage image) {
|
2018-09-27 00:33:08 +01:00
|
|
|
QList<QImage> *tiles = new QList<QImage>;
|
|
|
|
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);
|
2018-09-27 00:33:08 +01:00
|
|
|
tiles->append(tile);
|
|
|
|
}
|
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) {
|
2018-10-03 01:01:09 +01:00
|
|
|
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();
|
|
|
|
int num_metatiles = data.length() / 16;
|
|
|
|
int num_layers = 2;
|
|
|
|
QList<Metatile*> *metatiles = new QList<Metatile*>;
|
|
|
|
for (int i = 0; i < num_metatiles; i++) {
|
|
|
|
Metatile *metatile = new Metatile;
|
|
|
|
int index = i * (2 * 4 * num_layers);
|
|
|
|
for (int j = 0; j < 4 * num_layers; j++) {
|
|
|
|
uint16_t word = data[index++] & 0xff;
|
|
|
|
word += (data[index++] & 0xff) << 8;
|
|
|
|
Tile tile;
|
|
|
|
tile.tile = word & 0x3ff;
|
|
|
|
tile.xflip = (word >> 10) & 1;
|
|
|
|
tile.yflip = (word >> 11) & 1;
|
|
|
|
tile.palette = (word >> 12) & 0xf;
|
|
|
|
metatile->tiles->append(tile);
|
|
|
|
}
|
|
|
|
metatiles->append(metatile);
|
|
|
|
}
|
|
|
|
tileset->metatiles = metatiles;
|
|
|
|
} else {
|
|
|
|
tileset->metatiles = new QList<Metatile*>;
|
2018-12-20 23:30:35 +00:00
|
|
|
logError(QString("Could not open tileset metatiles file '%1'").arg(tileset->metatiles_path));
|
2018-09-27 00:33:08 +01:00
|
|
|
}
|
|
|
|
|
2018-10-03 01:01:09 +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();
|
|
|
|
int num_metatiles = tileset->metatiles->count();
|
|
|
|
int num_metatileAttrs = data.length() / 2;
|
|
|
|
if (num_metatiles != num_metatileAttrs) {
|
2018-12-20 23:30:35 +00:00
|
|
|
logWarn(QString("Metatile count %1 does not match metatile attribute count %2 in %3").arg(num_metatiles).arg(num_metatileAttrs).arg(tileset->name));
|
2018-10-05 19:26:25 +01:00
|
|
|
if (num_metatileAttrs > num_metatiles)
|
|
|
|
num_metatileAttrs = num_metatiles;
|
2018-09-27 00:33:08 +01:00
|
|
|
}
|
2018-10-03 01:01:09 +01:00
|
|
|
for (int i = 0; i < num_metatileAttrs; i++) {
|
|
|
|
int value = (static_cast<unsigned char>(data.at(i * 2 + 1)) << 8) | static_cast<unsigned char>(data.at(i * 2));
|
|
|
|
tileset->metatiles->at(i)->behavior = value & 0xFF;
|
|
|
|
tileset->metatiles->at(i)->layerType = (value & 0xF000) >> 12;
|
|
|
|
}
|
2018-09-27 00:33:08 +01:00
|
|
|
} else {
|
2018-12-20 23:30:35 +00:00
|
|
|
logError(QString("Could not open tileset metatile attributes file '%1'").arg(tileset->metatile_attrs_path));
|
2018-09-27 00:33:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-04 06:44:31 +01:00
|
|
|
void Project::loadTilesetMetatileLabels(Tileset* tileset) {
|
2019-09-09 23:24:30 +01:00
|
|
|
QString tilesetPrefix = QString("METATILE_%1_").arg(QString(tileset->name).replace("gTileset_", ""));
|
|
|
|
QMap<QString, int> labels = parser.readCDefines("include/constants/metatile_labels.h", 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));
|
2019-04-04 06:44:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-27 00:33:08 +01:00
|
|
|
Blockdata* Project::readBlockdata(QString path) {
|
|
|
|
Blockdata *blockdata = new Blockdata;
|
|
|
|
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));
|
|
|
|
blockdata->addBlock(word);
|
|
|
|
}
|
|
|
|
} else {
|
2018-12-20 23:30:35 +00:00
|
|
|
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 (map_cache->contains(map_name)) {
|
|
|
|
return map_cache->value(map_name);
|
|
|
|
} 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;
|
2018-09-27 00:33:08 +01:00
|
|
|
if (tileset_cache->contains(label)) {
|
2018-10-03 01:01:15 +01:00
|
|
|
existingTileset = tileset_cache->value(label);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (existingTileset && !forceLoad) {
|
2018-09-27 00:33:08 +01:00
|
|
|
return tileset_cache->value(label);
|
|
|
|
} 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 {
|
2018-12-20 23:30:35 +00:00
|
|
|
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 {
|
2018-12-20 23:30:35 +00:00
|
|
|
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()) {
|
2018-12-20 23:30:35 +00:00
|
|
|
logError(QString("Could not delete file '%1': ").arg(path) + file.errorString());
|
2018-09-27 00:33:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 15:13:58 +00:00
|
|
|
bool Project::readWildMonData() {
|
|
|
|
extraEncounterGroups.clear();
|
|
|
|
wildMonFields.clear();
|
|
|
|
wildMonData.clear();
|
|
|
|
encounterGroupLabels.clear();
|
|
|
|
if (!projectConfig.getEncounterJsonActive()) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-03 21:21:48 +01:00
|
|
|
|
2019-06-13 03:20:28 +01:00
|
|
|
QString wildMonJsonFilepath = QString("%1/src/data/wild_encounters.json").arg(root);
|
|
|
|
QJsonDocument wildMonsJsonDoc;
|
|
|
|
if (!parser.tryParseJsonFile(&wildMonsJsonDoc, wildMonJsonFilepath)) {
|
|
|
|
logError(QString("Failed to read wild encounters from %1").arg(wildMonJsonFilepath));
|
2020-02-12 15:13:58 +00:00
|
|
|
return false;
|
2019-06-13 03:20:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject wildMonObj = wildMonsJsonDoc.object();
|
|
|
|
|
|
|
|
for (auto subObjectRef : wildMonObj["wild_encounter_groups"].toArray()) {
|
|
|
|
QJsonObject subObject = subObjectRef.toObject();
|
2019-06-15 23:49:30 +01:00
|
|
|
if (!subObject["for_maps"].toBool()) {
|
|
|
|
extraEncounterGroups.insert(subObject["label"].toString(), subObject);
|
|
|
|
continue;
|
|
|
|
}
|
2019-06-13 03:20:28 +01:00
|
|
|
|
2019-06-13 17:14:49 +01:00
|
|
|
for (auto field : subObject["fields"].toArray()) {
|
2019-09-30 00:07:34 +01:00
|
|
|
EncounterField encounterField;
|
|
|
|
encounterField.name = field.toObject()["type"].toString();
|
|
|
|
for (auto val : field.toObject()["encounter_rates"].toArray()) {
|
|
|
|
encounterField.encounterRates.append(val.toInt());
|
|
|
|
}
|
|
|
|
for (QString group : field.toObject()["groups"].toObject().keys()) {
|
|
|
|
for (auto slotNum : field.toObject()["groups"].toObject()[group].toArray()) {
|
|
|
|
encounterField.groups[group].append(slotNum.toInt());
|
|
|
|
}
|
|
|
|
}
|
2019-06-15 23:49:30 +01:00
|
|
|
wildMonFields.append(encounterField);
|
2019-06-13 17:14:49 +01:00
|
|
|
}
|
|
|
|
|
2019-06-13 03:20:28 +01:00
|
|
|
QJsonArray encounters = subObject["encounters"].toArray();
|
|
|
|
for (QJsonValue encounter : encounters) {
|
2019-09-21 23:22:51 +01:00
|
|
|
QString mapConstant = encounter.toObject().value("map").toString();
|
2019-06-15 23:49:30 +01:00
|
|
|
|
2019-06-13 03:20:28 +01:00
|
|
|
WildPokemonHeader header;
|
|
|
|
|
2019-09-30 00:07:34 +01:00
|
|
|
for (EncounterField monField : wildMonFields) {
|
|
|
|
QString field = monField.name;
|
2019-09-21 23:22:51 +01:00
|
|
|
if (encounter.toObject().value(field) != QJsonValue::Undefined) {
|
2019-06-13 17:14:49 +01:00
|
|
|
header.wildMons[field].active = true;
|
2019-09-21 23:22:51 +01:00
|
|
|
header.wildMons[field].encounterRate = encounter.toObject().value(field).toObject().value("encounter_rate").toInt();
|
|
|
|
for (QJsonValue mon : encounter.toObject().value(field).toObject().value("mons").toArray()) {
|
2019-09-22 01:48:53 +01:00
|
|
|
WildPokemon newMon;
|
|
|
|
newMon.minLevel = mon.toObject().value("min_level").toInt();
|
|
|
|
newMon.maxLevel = mon.toObject().value("max_level").toInt();
|
|
|
|
newMon.species = mon.toObject().value("species").toString();
|
|
|
|
header.wildMons[field].wildPokemon.append(newMon);
|
2019-06-13 17:14:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-21 23:22:51 +01:00
|
|
|
wildMonData[mapConstant].insert(encounter.toObject().value("base_label").toString(), header);
|
|
|
|
encounterGroupLabels.append(encounter.toObject().value("base_label").toString());
|
2019-06-13 03:20:28 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-12 15:13:58 +00:00
|
|
|
return true;
|
2019-06-13 03:20:28 +01:00
|
|
|
}
|
|
|
|
|
2018-09-27 00:33:08 +01:00
|
|
|
void Project::readMapGroups() {
|
2019-02-01 17:43:25 +00:00
|
|
|
QString mapGroupsFilepath = QString("%1/data/maps/map_groups.json").arg(root);
|
2019-04-20 15:06:59 +01:00
|
|
|
QJsonDocument mapGroupsDoc;
|
2019-05-06 17:42:09 +01:00
|
|
|
if (!parser.tryParseJsonFile(&mapGroupsDoc, mapGroupsFilepath)) {
|
2019-04-20 15:06:59 +01:00
|
|
|
logError(QString("Failed to read map groups from %1").arg(mapGroupsFilepath));
|
2018-09-27 00:33:08 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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;
|
2019-02-01 17:43:25 +00:00
|
|
|
QStringList *maps = new QStringList;
|
|
|
|
QStringList *groups = new QStringList;
|
|
|
|
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());
|
2019-02-01 17:43:25 +00:00
|
|
|
groups->append(groupName);
|
|
|
|
for (int j = 0; j < mapNames.size(); j++) {
|
|
|
|
QString mapName = mapNames.at(j).toString();
|
|
|
|
map_groups->insert(mapName, groupIndex);
|
|
|
|
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);
|
|
|
|
|
|
|
|
groupNames = groups;
|
|
|
|
groupedMapNames = groupedMaps;
|
|
|
|
mapNames = maps;
|
|
|
|
}
|
|
|
|
|
|
|
|
Map* Project::addNewMapToGroup(QString mapName, int groupNum) {
|
|
|
|
// Setup new map in memory, but don't write to file until map is actually saved later.
|
|
|
|
mapNames->append(mapName);
|
|
|
|
map_groups->insert(mapName, groupNum);
|
|
|
|
groupedMapNames[groupNum].append(mapName);
|
|
|
|
|
|
|
|
Map *map = new Map;
|
|
|
|
map->isPersistedToFile = false;
|
|
|
|
map->setName(mapName);
|
|
|
|
mapConstantsToMapNames->insert(map->constantName, map->name);
|
|
|
|
mapNamesToMapConstants->insert(map->name, map->constantName);
|
|
|
|
setNewMapHeader(map, mapLayoutsTable.size() + 1);
|
|
|
|
setNewMapLayout(map);
|
|
|
|
loadMapTilesets(map);
|
|
|
|
setNewMapBlockdata(map);
|
|
|
|
setNewMapBorder(map);
|
|
|
|
setNewMapEvents(map);
|
|
|
|
setNewMapConnections(map);
|
|
|
|
map->commit();
|
|
|
|
map->metatileHistory.save();
|
|
|
|
map_cache->insert(mapName, map);
|
|
|
|
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2019-02-01 17:43:25 +00:00
|
|
|
Map* Project::addNewMapToGroup(QString mapName, int groupNum, Map *newMap, bool existingLayout) {
|
2019-01-07 23:14:44 +00:00
|
|
|
mapNames->append(mapName);
|
|
|
|
map_groups->insert(mapName, groupNum);
|
|
|
|
groupedMapNames[groupNum].append(mapName);
|
|
|
|
|
|
|
|
Map *map = new Map;
|
|
|
|
map = newMap;
|
|
|
|
|
|
|
|
map->isPersistedToFile = false;
|
|
|
|
map->setName(mapName);
|
|
|
|
|
|
|
|
mapConstantsToMapNames->insert(map->constantName, map->name);
|
|
|
|
mapNamesToMapConstants->insert(map->name, map->constantName);
|
2019-02-01 17:43:25 +00:00
|
|
|
if (!existingLayout) {
|
|
|
|
mapLayouts.insert(map->layoutId, map->layout);
|
|
|
|
mapLayoutsTable.append(map->layoutId);
|
2019-01-07 23:14:44 +00:00
|
|
|
setNewMapBlockdata(map);
|
|
|
|
setNewMapBorder(map);
|
|
|
|
}
|
|
|
|
|
|
|
|
loadMapTilesets(map);
|
|
|
|
setNewMapEvents(map);
|
|
|
|
setNewMapConnections(map);
|
|
|
|
|
|
|
|
map->commit();
|
|
|
|
map->metatileHistory.save();
|
|
|
|
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
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);
|
2019-05-06 17:42:09 +01:00
|
|
|
|
2020-02-12 15:13:58 +00:00
|
|
|
QString filename = "data/tilesets/headers.inc";
|
|
|
|
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
|
|
|
|
2019-05-05 16:45:03 +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
|
|
|
|
2019-05-05 16:45:03 +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
|
|
|
|
2019-05-05 16:45:03 +01:00
|
|
|
if (secondaryTilesetValue != "1" && secondaryTilesetValue != "TRUE"
|
|
|
|
&& secondaryTilesetValue != "0" && secondaryTilesetValue != "FALSE") {
|
2019-05-05 21:11:00 +01:00
|
|
|
logWarn(QString("Unexpected secondary tileset flag found in %1. Expected 'TRUE', 'FALSE', '1', or '0', but found '%2'")
|
|
|
|
.arg(tilesetLabel).arg(secondaryTilesetValue));
|
2019-05-05 16:45:03 +01:00
|
|
|
continue;
|
2018-09-27 00:33:08 +01:00
|
|
|
}
|
|
|
|
|
2019-05-05 16:45:03 +01:00
|
|
|
bool isSecondaryTileset = (secondaryTilesetValue == "TRUE" || secondaryTilesetValue == "1");
|
|
|
|
if (isSecondaryTileset)
|
|
|
|
allTilesets["secondary"].append(tilesetLabel);
|
|
|
|
else
|
|
|
|
allTilesets["primary"].append(tilesetLabel);
|
2018-09-27 00:33:08 +01:00
|
|
|
}
|
2019-09-21 21:59:14 +01:00
|
|
|
this->tilesetLabels = allTilesets;
|
2018-09-27 00:33:08 +01:00
|
|
|
return allTilesets;
|
|
|
|
}
|
|
|
|
|
2020-02-12 15:13:58 +00:00
|
|
|
bool Project::readTilesetProperties() {
|
2019-05-05 21:11:00 +01:00
|
|
|
QStringList definePrefixes;
|
|
|
|
definePrefixes << "NUM_";
|
2019-05-06 17:42:09 +01:00
|
|
|
QMap<QString, int> defines = parser.readCDefines("include/fieldmap.h", definePrefixes);
|
2018-09-27 00:33:08 +01:00
|
|
|
|
2019-05-05 21:11:00 +01:00
|
|
|
auto it = defines.find("NUM_TILES_IN_PRIMARY");
|
|
|
|
if (it != defines.end()) {
|
|
|
|
Project::num_tiles_primary = it.value();
|
|
|
|
}
|
|
|
|
else {
|
2019-05-06 17:42:09 +01:00
|
|
|
logWarn(QString("Value for tileset property 'NUM_TILES_IN_PRIMARY' not found. Using default (%1) instead.")
|
|
|
|
.arg(Project::num_tiles_primary));
|
2019-05-05 21:11:00 +01:00
|
|
|
}
|
|
|
|
it = defines.find("NUM_TILES_TOTAL");
|
|
|
|
if (it != defines.end()) {
|
|
|
|
Project::num_tiles_total = it.value();
|
|
|
|
}
|
|
|
|
else {
|
2019-05-06 17:42:09 +01:00
|
|
|
logWarn(QString("Value for tileset property 'NUM_TILES_TOTAL' not found. Using default (%1) instead.")
|
|
|
|
.arg(Project::num_tiles_total));
|
2019-05-05 21:11:00 +01:00
|
|
|
}
|
|
|
|
it = defines.find("NUM_METATILES_IN_PRIMARY");
|
|
|
|
if (it != defines.end()) {
|
|
|
|
Project::num_metatiles_primary = it.value();
|
|
|
|
}
|
|
|
|
else {
|
2019-05-06 17:42:09 +01:00
|
|
|
logWarn(QString("Value for tileset property 'NUM_METATILES_IN_PRIMARY' not found. Using default (%1) instead.")
|
|
|
|
.arg(Project::num_metatiles_primary));
|
2019-05-05 21:11:00 +01:00
|
|
|
}
|
|
|
|
it = defines.find("NUM_METATILES_TOTAL");
|
|
|
|
if (it != defines.end()) {
|
|
|
|
Project::num_metatiles_total = it.value();
|
|
|
|
}
|
|
|
|
else {
|
2019-05-06 17:42:09 +01:00
|
|
|
logWarn(QString("Value for tileset property 'NUM_METATILES_TOTAL' not found. Using default (%1) instead.")
|
|
|
|
.arg(Project::num_metatiles_total));
|
2019-05-05 21:11:00 +01:00
|
|
|
}
|
|
|
|
it = defines.find("NUM_PALS_IN_PRIMARY");
|
|
|
|
if (it != defines.end()) {
|
|
|
|
Project::num_pals_primary = it.value();
|
|
|
|
}
|
|
|
|
else {
|
2019-05-06 17:42:09 +01:00
|
|
|
logWarn(QString("Value for tileset property 'NUM_PALS_IN_PRIMARY' not found. Using default (%1) instead.")
|
|
|
|
.arg(Project::num_pals_primary));
|
2019-05-05 21:11:00 +01:00
|
|
|
}
|
|
|
|
it = defines.find("NUM_PALS_TOTAL");
|
|
|
|
if (it != defines.end()) {
|
|
|
|
Project::num_pals_total = it.value();
|
|
|
|
}
|
|
|
|
else {
|
2019-05-06 17:42:09 +01:00
|
|
|
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
|
|
|
}
|
2020-02-12 15:13:58 +00:00
|
|
|
return true;
|
2018-09-27 00:33:08 +01:00
|
|
|
}
|
|
|
|
|
2020-02-12 14:12:12 +00:00
|
|
|
bool Project::readRegionMapSections() {
|
2019-02-25 18:31:34 +00:00
|
|
|
this->mapSectionNameToValue.clear();
|
|
|
|
this->mapSectionValueToName.clear();
|
2019-05-05 21:11:00 +01:00
|
|
|
|
|
|
|
QStringList prefixes = (QStringList() << "MAPSEC_");
|
2020-02-12 14:12:12 +00:00
|
|
|
QString filename = "include/constants/region_map_sections.h";
|
|
|
|
this->mapSectionNameToValue = parser.readCDefines(filename, prefixes);
|
|
|
|
if (this->mapSectionNameToValue.isEmpty()) {
|
|
|
|
logError(QString("Failed to read region map sections from %1.").arg(filename));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-05 21:11:00 +01:00
|
|
|
for (QString defineName : this->mapSectionNameToValue.keys()) {
|
|
|
|
this->mapSectionValueToName.insert(this->mapSectionNameToValue[defineName], defineName);
|
2019-02-25 18:31:34 +00:00
|
|
|
}
|
2020-02-12 14:12:12 +00:00
|
|
|
return true;
|
2018-09-27 00:33:08 +01:00
|
|
|
}
|
|
|
|
|
2020-02-12 15:13:58 +00:00
|
|
|
bool Project::readHealLocations() {
|
|
|
|
dataQualifiers.clear();
|
|
|
|
flyableMaps.clear();
|
|
|
|
|
|
|
|
QString filename = "src/data/heal_locations.h";
|
|
|
|
QString text = parser.readTextFile(root + "/" + filename);
|
2019-04-28 14:05:37 +01:00
|
|
|
text.replace(QRegularExpression("//.*?(\r\n?|\n)|/\\*.*?\\*/", QRegularExpression::DotMatchesEverythingOption), "");
|
|
|
|
|
2019-04-29 01:20:48 +01:00
|
|
|
dataQualifiers.insert("heal_locations", getDataQualifiers(text, "sHealLocations"));
|
|
|
|
|
2019-08-18 14:42:46 +01:00
|
|
|
QRegularExpression regex("MAP_GROUP[\\(\\s]+(?<map>[A-Za-z0-9_]+)[\\s\\)]+,\\s*MAP_NUM[\\(\\s]+(\\1)[\\s\\)]+,\\s*(?<x>[0-9A-Fa-fx]+),\\s*(?<y>[0-9A-Fa-fx]+)");
|
2019-04-28 14:05:37 +01:00
|
|
|
QRegularExpressionMatchIterator iter = regex.globalMatch(text);
|
|
|
|
for (int i = 1; iter.hasNext(); i++) {
|
|
|
|
QRegularExpressionMatch match = iter.next();
|
|
|
|
QString mapName = match.captured("map");
|
|
|
|
unsigned x = match.captured("x").toUShort();
|
|
|
|
unsigned y = match.captured("y").toUShort();
|
|
|
|
flyableMaps.append(HealLocation(mapName, i, x, y));
|
|
|
|
}
|
2020-02-12 15:13:58 +00:00
|
|
|
return true;
|
2019-04-28 14:05:37 +01:00
|
|
|
}
|
|
|
|
|
2020-02-12 15:13:58 +00:00
|
|
|
bool Project::readItemNames() {
|
|
|
|
itemNames->clear();
|
2018-09-27 00:33:08 +01:00
|
|
|
QStringList prefixes = (QStringList() << "ITEM_");
|
2020-02-12 15:13:58 +00:00
|
|
|
QString filename = "include/constants/items.h";
|
|
|
|
parser.readCDefinesSorted(filename, prefixes, itemNames);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-12 15:13:58 +00:00
|
|
|
bool Project::readFlagNames() {
|
|
|
|
flagNames->clear();
|
2018-09-27 00:33:08 +01:00
|
|
|
QStringList prefixes = (QStringList() << "FLAG_");
|
2020-02-12 15:13:58 +00:00
|
|
|
QString filename = "include/constants/flags.h";
|
|
|
|
parser.readCDefinesSorted(filename, prefixes, flagNames);
|
|
|
|
if (flagNames->isEmpty()) {
|
|
|
|
logError(QString("Failed to read flag constants from %1").arg(filename));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2018-09-27 00:33:08 +01:00
|
|
|
}
|
|
|
|
|
2020-02-12 15:13:58 +00:00
|
|
|
bool Project::readVarNames() {
|
|
|
|
varNames->clear();
|
2018-09-27 00:33:08 +01:00
|
|
|
QStringList prefixes = (QStringList() << "VAR_");
|
2020-02-12 15:13:58 +00:00
|
|
|
QString filename = "include/constants/vars.h";
|
|
|
|
parser.readCDefinesSorted(filename, prefixes, varNames);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-12 15:13:58 +00:00
|
|
|
bool Project::readMovementTypes() {
|
|
|
|
movementTypes->clear();
|
2018-09-27 00:33:08 +01:00
|
|
|
QStringList prefixes = (QStringList() << "MOVEMENT_TYPE_");
|
2020-02-12 15:13:58 +00:00
|
|
|
QString filename = "include/constants/event_object_movement_constants.h";
|
|
|
|
parser.readCDefinesSorted(filename, prefixes, movementTypes);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-12 15:13:58 +00:00
|
|
|
bool Project::readInitialFacingDirections() {
|
|
|
|
QString filename = "src/event_object_movement.c";
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-12 15:13:58 +00:00
|
|
|
bool Project::readMapTypes() {
|
|
|
|
mapTypes->clear();
|
2019-05-06 17:42:09 +01:00
|
|
|
QStringList prefixes = (QStringList() << "MAP_TYPE_");
|
2020-02-12 15:13:58 +00:00
|
|
|
QString filename = "include/constants/map_types.h";
|
|
|
|
parser.readCDefinesSorted(filename, prefixes, mapTypes);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-12 15:13:58 +00:00
|
|
|
bool Project::readMapBattleScenes() {
|
|
|
|
mapBattleScenes->clear();
|
2018-09-27 00:33:08 +01:00
|
|
|
QStringList prefixes = (QStringList() << "MAP_BATTLE_SCENE_");
|
2020-02-12 15:13:58 +00:00
|
|
|
QString filename = "include/constants/map_types.h";
|
2019-05-06 17:42:09 +01:00
|
|
|
parser.readCDefinesSorted("include/constants/map_types.h", prefixes, mapBattleScenes);
|
2020-02-12 15:13:58 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-12 15:13:58 +00:00
|
|
|
bool Project::readWeatherNames() {
|
|
|
|
weatherNames->clear();
|
2019-08-19 18:16:58 +01:00
|
|
|
QStringList prefixes = (QStringList() << "\\bWEATHER_");
|
2020-02-12 15:13:58 +00:00
|
|
|
QString filename = "include/constants/weather.h";
|
|
|
|
parser.readCDefinesSorted(filename, prefixes, weatherNames);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-12 15:13:58 +00:00
|
|
|
bool Project::readCoordEventWeatherNames() {
|
|
|
|
coordEventWeatherNames->clear();
|
2018-09-27 00:33:08 +01:00
|
|
|
QStringList prefixes = (QStringList() << "COORD_EVENT_WEATHER_");
|
2020-02-12 15:13:58 +00:00
|
|
|
QString filename = "include/constants/weather.h";
|
|
|
|
parser.readCDefinesSorted(filename, prefixes, coordEventWeatherNames);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-12 15:13:58 +00:00
|
|
|
bool Project::readSecretBaseIds() {
|
|
|
|
secretBaseIds->clear();
|
2019-04-29 01:57:25 +01:00
|
|
|
QStringList prefixes = (QStringList() << "SECRET_BASE_[A-Za-z0-9_]*_[0-9]+");
|
2020-02-12 15:13:58 +00:00
|
|
|
QString filename = "include/constants/secret_bases.h";
|
|
|
|
parser.readCDefinesSorted(filename, prefixes, secretBaseIds);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-12 15:13:58 +00:00
|
|
|
bool Project::readBgEventFacingDirections() {
|
|
|
|
bgEventFacingDirections->clear();
|
2018-09-27 00:33:08 +01:00
|
|
|
QStringList prefixes = (QStringList() << "BG_EVENT_PLAYER_FACING_");
|
2020-02-12 15:13:58 +00:00
|
|
|
QString filename = "include/constants/bg_event_constants.h";
|
|
|
|
parser.readCDefinesSorted(filename, prefixes, bgEventFacingDirections);
|
|
|
|
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-02-12 15:13:58 +00:00
|
|
|
bool Project::readMetatileBehaviors() {
|
2018-10-03 01:01:09 +01:00
|
|
|
this->metatileBehaviorMap.clear();
|
|
|
|
this->metatileBehaviorMapInverse.clear();
|
2019-05-05 21:11:00 +01:00
|
|
|
|
|
|
|
QStringList prefixes = (QStringList() << "MB_");
|
2020-02-12 15:13:58 +00:00
|
|
|
QString filename = "include/constants/metatile_behaviors.h";
|
|
|
|
this->metatileBehaviorMap = parser.readCDefines(filename, prefixes);
|
|
|
|
if (this->metatileBehaviorMap.isEmpty()) {
|
|
|
|
logError(QString("Failed to metatile behaviors from %1.").arg(filename));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-05 21:11:00 +01:00
|
|
|
for (QString defineName : this->metatileBehaviorMap.keys()) {
|
|
|
|
this->metatileBehaviorMapInverse.insert(this->metatileBehaviorMap[defineName], defineName);
|
2018-10-03 01:01:09 +01:00
|
|
|
}
|
2020-02-12 15:13:58 +00:00
|
|
|
return true;
|
2018-10-03 01:01:09 +01:00
|
|
|
}
|
|
|
|
|
2018-09-27 00:33:08 +01:00
|
|
|
QStringList Project::getSongNames() {
|
2019-05-05 21:11:00 +01:00
|
|
|
QStringList songDefinePrefixes;
|
|
|
|
songDefinePrefixes << "SE_" << "MUS_";
|
2019-05-06 17:42:09 +01:00
|
|
|
QMap<QString, int> songDefines = parser.readCDefines("include/constants/songs.h", songDefinePrefixes);
|
|
|
|
QStringList names = songDefines.keys();
|
2019-05-05 21:11:00 +01:00
|
|
|
|
2018-09-27 00:33:08 +01:00
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
|
|
|
QMap<QString, int> Project::getEventObjGfxConstants() {
|
2019-05-05 21:11:00 +01:00
|
|
|
QStringList eventObjGfxPrefixes;
|
|
|
|
eventObjGfxPrefixes << "EVENT_OBJ_GFX_";
|
2019-05-06 17:42:09 +01:00
|
|
|
|
|
|
|
QMap<QString, int> constants = parser.readCDefines("include/constants/event_objects.h", eventObjGfxPrefixes);
|
2019-05-05 21:11:00 +01:00
|
|
|
|
2018-09-27 00:33:08 +01:00
|
|
|
return constants;
|
|
|
|
}
|
|
|
|
|
2020-02-12 15:13:58 +00:00
|
|
|
bool Project::readMiscellaneousConstants() {
|
|
|
|
miscConstants.clear();
|
|
|
|
if (projectConfig.getEncounterJsonActive()) {
|
|
|
|
QMap<QString, int> pokemonDefines = parser.readCDefines("include/constants/pokemon.h", QStringList() << "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);
|
|
|
|
}
|
|
|
|
return true;
|
2019-07-03 03:08:58 +01:00
|
|
|
}
|
|
|
|
|
2018-10-03 01:01:41 +01:00
|
|
|
QString Project::fixPalettePath(QString path) {
|
|
|
|
path = path.replace(QRegExp("\\.gbapal$"), ".pal");
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2018-09-27 00:33:08 +01:00
|
|
|
QString Project::fixGraphicPath(QString path) {
|
|
|
|
path = path.replace(QRegExp("\\.lz$"), "");
|
|
|
|
path = path.replace(QRegExp("\\.[1248]bpp$"), ".png");
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2019-10-22 13:48:41 +01:00
|
|
|
QString Project::getScriptFileExtension(bool usePoryScript) {
|
|
|
|
if(usePoryScript) {
|
|
|
|
return ".pory";
|
|
|
|
} else {
|
|
|
|
return ".inc";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Project::getScriptDefaultString(bool usePoryScript, QString mapName) {
|
|
|
|
if(usePoryScript)
|
|
|
|
return QString("mapscripts %1_MapScripts {}").arg(mapName);
|
|
|
|
else
|
|
|
|
return QString("%1_MapScripts::\n\t.byte 0\n").arg(mapName);
|
|
|
|
}
|
|
|
|
|
2018-09-27 00:33:08 +01:00
|
|
|
void Project::loadEventPixmaps(QList<Event*> objects) {
|
|
|
|
bool needs_update = false;
|
|
|
|
for (Event *object : objects) {
|
|
|
|
if (object->pixmap.isNull()) {
|
|
|
|
needs_update = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!needs_update) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QMap<QString, int> constants = getEventObjGfxConstants();
|
|
|
|
|
2019-06-08 19:05:01 +01:00
|
|
|
QMap<QString, QString> pointerHash = parser.readNamedIndexCArray("src/data/field_event_obj/event_object_graphics_info_pointers.h", "gEventObjectGraphicsInfoPointers");
|
2019-08-27 16:30:35 +01:00
|
|
|
|
2018-09-27 00:33:08 +01:00
|
|
|
for (Event *object : objects) {
|
|
|
|
if (!object->pixmap.isNull()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
object->spriteWidth = 16;
|
|
|
|
object->spriteHeight = 16;
|
2019-01-11 01:59:41 +00:00
|
|
|
object->usingSprite = false;
|
2018-09-27 00:33:08 +01:00
|
|
|
QString event_type = object->get("event_type");
|
|
|
|
if (event_type == EventType::Object) {
|
|
|
|
object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(0, 0, 16, 16);
|
|
|
|
} else if (event_type == EventType::Warp) {
|
|
|
|
object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(16, 0, 16, 16);
|
2018-12-26 20:15:35 +00:00
|
|
|
} else if (event_type == EventType::Trigger || event_type == EventType::WeatherTrigger) {
|
2018-09-27 00:33:08 +01:00
|
|
|
object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(32, 0, 16, 16);
|
|
|
|
} else if (event_type == EventType::Sign || event_type == EventType::HiddenItem || event_type == EventType::SecretBase) {
|
|
|
|
object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(48, 0, 16, 16);
|
|
|
|
} else if (event_type == EventType::HealLocation) {
|
|
|
|
object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(64, 0, 16, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event_type == EventType::Object) {
|
2019-06-08 19:05:01 +01:00
|
|
|
QString info_label = pointerHash[object->get("sprite")].replace("&", "");
|
2019-05-06 17:42:09 +01:00
|
|
|
QStringList gfx_info = parser.readCArray("src/data/field_event_obj/event_object_graphics_info.h", info_label);
|
2018-09-27 00:33:08 +01:00
|
|
|
QString pic_label = gfx_info.value(14);
|
|
|
|
QString dimensions_label = gfx_info.value(11);
|
|
|
|
QString subsprites_label = gfx_info.value(12);
|
2019-05-06 17:42:09 +01:00
|
|
|
QString gfx_label = parser.readCArray("src/data/field_event_obj/event_object_pic_tables.h", pic_label).value(0);
|
2018-09-27 00:33:08 +01:00
|
|
|
gfx_label = gfx_label.section(QRegExp("[\\(\\)]"), 1, 1);
|
2019-05-06 17:42:09 +01:00
|
|
|
QString path = parser.readCIncbin("src/data/field_event_obj/event_object_graphics.h", gfx_label);
|
2018-09-27 00:33:08 +01:00
|
|
|
|
|
|
|
if (!path.isNull()) {
|
|
|
|
path = fixGraphicPath(path);
|
|
|
|
QImage spritesheet(root + "/" + path);
|
|
|
|
if (!spritesheet.isNull()) {
|
|
|
|
// Infer the sprite dimensions from the OAM labels.
|
|
|
|
int spriteWidth = spritesheet.width();
|
|
|
|
int spriteHeight = spritesheet.height();
|
|
|
|
QRegularExpression re("\\S+_(\\d+)x(\\d+)");
|
|
|
|
QRegularExpressionMatch dimensionMatch = re.match(dimensions_label);
|
|
|
|
if (dimensionMatch.hasMatch()) {
|
|
|
|
QRegularExpressionMatch oamTablesMatch = re.match(subsprites_label);
|
|
|
|
if (oamTablesMatch.hasMatch()) {
|
|
|
|
spriteWidth = dimensionMatch.captured(1).toInt();
|
|
|
|
spriteHeight = dimensionMatch.captured(2).toInt();
|
|
|
|
}
|
|
|
|
}
|
2019-04-03 00:51:33 +01:00
|
|
|
object->setPixmapFromSpritesheet(spritesheet, spriteWidth, spriteHeight, object->frame, object->hFlip);
|
2018-09-27 00:33:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 15:13:58 +00:00
|
|
|
bool Project::readSpeciesIconPaths() {
|
|
|
|
speciesToIconPath.clear();
|
|
|
|
QString filename = "src/pokemon_icon.c";
|
|
|
|
QMap<QString, QString> monIconNames = parser.readNamedIndexCArray(filename, "gMonIconTable");
|
2019-06-13 03:20:28 +01:00
|
|
|
for (QString species : monIconNames.keys()) {
|
|
|
|
QString path = parser.readCIncbin("src/data/graphics/pokemon.h", monIconNames.value(species));
|
|
|
|
speciesToIconPath.insert(species, root + "/" + path.replace("4bpp", "png"));
|
|
|
|
}
|
2020-02-12 15:13:58 +00:00
|
|
|
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
|
|
|
|
if (map->events["heal_event_group"].length() > 0) {
|
|
|
|
for (Event *healEvent : map->events["heal_event_group"]) {
|
|
|
|
HealLocation hl = HealLocation::fromEvent(healEvent);
|
|
|
|
flyableMaps[hl.index - 1] = hl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
saveHealLocationStruct(map);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Project::setNewMapEvents(Map *map) {
|
|
|
|
map->events["object_event_group"].clear();
|
|
|
|
map->events["warp_event_group"].clear();
|
|
|
|
map->events["heal_event_group"].clear();
|
|
|
|
map->events["coord_event_group"].clear();
|
|
|
|
map->events["bg_event_group"].clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|