Keine Beschreibung

project.cpp 50KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  1. #include "asm.h"
  2. #include "project.h"
  3. #include "tile.h"
  4. #include "tileset.h"
  5. #include "metatile.h"
  6. #include "event.h"
  7. #include <QDebug>
  8. #include <QDir>
  9. #include <QFile>
  10. #include <QTextStream>
  11. #include <QStandardItem>
  12. #include <QMessageBox>
  13. #include <QRegularExpression>
  14. Project::Project()
  15. {
  16. groupNames = new QStringList;
  17. map_groups = new QMap<QString, int>;
  18. groupedMapNames = new QList<QStringList*>;
  19. mapNames = new QStringList;
  20. map_cache = new QMap<QString, Map*>;
  21. mapConstantsToMapNames = new QMap<QString, QString>;
  22. mapNamesToMapConstants = new QMap<QString, QString>;
  23. mapAttributesTable = new QMap<int, QString>;
  24. tileset_cache = new QMap<QString, Tileset*>;
  25. }
  26. QString Project::getProjectTitle() {
  27. if (!root.isNull()) {
  28. return root.section('/', -1);
  29. } else {
  30. return QString();
  31. }
  32. }
  33. Map* Project::loadMap(QString map_name) {
  34. // New maps are saved to actual files yet, so we need to fetch their data from the map_cache.
  35. Map *map;
  36. if (map_cache->contains(map_name) && !map_cache->value(map_name)->isPersistedToFile) {
  37. map = map_cache->value(map_name);
  38. } else {
  39. map = new Map;
  40. map->setName(map_name);
  41. }
  42. readMapHeader(map);
  43. readMapAttributes(map);
  44. getTilesets(map);
  45. loadBlockdata(map);
  46. loadMapBorder(map);
  47. readMapEvents(map);
  48. loadMapConnections(map);
  49. map->commit();
  50. map->history.save();
  51. map_cache->insert(map_name, map);
  52. return map;
  53. }
  54. void Project::loadMapConnections(Map *map) {
  55. if (!map->isPersistedToFile) {
  56. return;
  57. }
  58. map->connections.clear();
  59. if (!map->connections_label.isNull()) {
  60. QString path = root + QString("/data/maps/%1/connections.inc").arg(map->name);
  61. QString text = readTextFile(path);
  62. if (!text.isNull()) {
  63. QList<QStringList> *commands = parse(text);
  64. QStringList *list = getLabelValues(commands, map->connections_label);
  65. //// Avoid using this value. It ought to be generated instead.
  66. //int num_connections = list->value(0).toInt(nullptr, 0);
  67. QString connections_list_label = list->value(1);
  68. QList<QStringList> *connections = getLabelMacros(commands, connections_list_label);
  69. for (QStringList command : *connections) {
  70. QString macro = command.value(0);
  71. if (macro == "connection") {
  72. Connection *connection = new Connection;
  73. connection->direction = command.value(1);
  74. connection->offset = command.value(2);
  75. QString mapConstant = command.value(3);
  76. if (mapConstantsToMapNames->contains(mapConstant)) {
  77. connection->map_name = mapConstantsToMapNames->value(mapConstant);
  78. map->connections.append(connection);
  79. } else {
  80. qDebug() << QString("Failed to find connected map for map constant '%1'").arg(mapConstant);
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }
  87. void Project::setNewMapConnections(Map *map) {
  88. map->connections.clear();
  89. }
  90. QList<QStringList>* Project::getLabelMacros(QList<QStringList> *list, QString label) {
  91. bool in_label = false;
  92. QList<QStringList> *new_list = new QList<QStringList>;
  93. for (int i = 0; i < list->length(); i++) {
  94. QStringList params = list->value(i);
  95. QString macro = params.value(0);
  96. if (macro == ".label") {
  97. if (params.value(1) == label) {
  98. in_label = true;
  99. } else if (in_label) {
  100. // If nothing has been read yet, assume the label
  101. // we're looking for is in a stack of labels.
  102. if (new_list->length() > 0) {
  103. break;
  104. }
  105. }
  106. } else if (in_label) {
  107. new_list->append(params);
  108. }
  109. }
  110. return new_list;
  111. }
  112. // For if you don't care about filtering by macro,
  113. // and just want all values associated with some label.
  114. QStringList* Project::getLabelValues(QList<QStringList> *list, QString label) {
  115. list = getLabelMacros(list, label);
  116. QStringList *values = new QStringList;
  117. for (int i = 0; i < list->length(); i++) {
  118. QStringList params = list->value(i);
  119. QString macro = params.value(0);
  120. // Ignore .align
  121. if (macro == ".align") {
  122. continue;
  123. }
  124. for (int j = 1; j < params.length(); j++) {
  125. values->append(params.value(j));
  126. }
  127. }
  128. return values;
  129. }
  130. void Project::readMapHeader(Map* map) {
  131. if (!map->isPersistedToFile) {
  132. return;
  133. }
  134. QString label = map->name;
  135. Asm *parser = new Asm;
  136. QString header_text = readTextFile(root + "/data/maps/" + label + "/header.inc");
  137. if (header_text.isNull()) {
  138. return;
  139. }
  140. QStringList *header = getLabelValues(parser->parse(header_text), label);
  141. map->attributes_label = header->value(0);
  142. map->events_label = header->value(1);
  143. map->scripts_label = header->value(2);
  144. map->connections_label = header->value(3);
  145. map->song = header->value(4);
  146. map->index = header->value(5);
  147. map->location = header->value(6);
  148. map->visibility = header->value(7);
  149. map->weather = header->value(8);
  150. map->type = header->value(9);
  151. map->unknown = header->value(10);
  152. map->show_location = header->value(11);
  153. map->battle_scene = header->value(12);
  154. }
  155. void Project::setNewMapHeader(Map* map, int mapIndex) {
  156. map->attributes_label = QString("%1_MapAttributes").arg(map->name);
  157. map->events_label = QString("%1_MapEvents").arg(map->name);;
  158. map->scripts_label = QString("%1_MapScripts").arg(map->name);;
  159. map->connections_label = "0x0";
  160. map->song = "BGM_DAN02";
  161. map->index = mapIndex;
  162. map->location = "0";
  163. map->visibility = "0";
  164. map->weather = "2";
  165. map->type = "1";
  166. map->unknown = "0";
  167. map->show_location = "1";
  168. map->battle_scene = "0";
  169. }
  170. void Project::saveMapHeader(Map *map) {
  171. QString label = map->name;
  172. QString header_path = root + "/data/maps/" + label + "/header.inc";
  173. QString text = "";
  174. text += QString("%1::\n").arg(label);
  175. text += QString("\t.4byte %1\n").arg(map->attributes_label);
  176. text += QString("\t.4byte %1\n").arg(map->events_label);
  177. text += QString("\t.4byte %1\n").arg(map->scripts_label);
  178. text += QString("\t.4byte %1\n").arg(map->connections_label);
  179. text += QString("\t.2byte %1\n").arg(map->song);
  180. text += QString("\t.2byte %1\n").arg(map->index);
  181. text += QString("\t.byte %1\n").arg(map->location);
  182. text += QString("\t.byte %1\n").arg(map->visibility);
  183. text += QString("\t.byte %1\n").arg(map->weather);
  184. text += QString("\t.byte %1\n").arg(map->type);
  185. text += QString("\t.2byte %1\n").arg(map->unknown);
  186. text += QString("\t.byte %1\n").arg(map->show_location);
  187. text += QString("\t.byte %1\n").arg(map->battle_scene);
  188. saveTextFile(header_path, text);
  189. }
  190. void Project::readMapAttributesTable() {
  191. int curMapIndex = 1;
  192. QString attributesText = readTextFile(getMapAttributesTableFilepath());
  193. QList<QStringList>* values = parse(attributesText);
  194. bool inAttributePointers = false;
  195. for (int i = 0; i < values->length(); i++) {
  196. QStringList params = values->value(i);
  197. QString macro = params.value(0);
  198. if (macro == ".label") {
  199. if (inAttributePointers) {
  200. break;
  201. }
  202. if (params.value(1) == "gMapAttributes") {
  203. inAttributePointers = true;
  204. }
  205. } else if (macro == ".4byte" && inAttributePointers) {
  206. QString mapName = params.value(1);
  207. if (!mapName.contains("UnknownMapAttributes")) {
  208. // Strip off "_MapAttributes" from the label if it's a real map label.
  209. mapName = mapName.remove(mapName.length() - 14, 14);
  210. }
  211. mapAttributesTable->insert(curMapIndex, mapName);
  212. curMapIndex++;
  213. }
  214. }
  215. }
  216. void Project::saveMapAttributesTable() {
  217. QString text = "";
  218. text += QString("\t.align 2\n");
  219. text += QString("gMapAttributes::\n");
  220. for (int i = 0; i < mapAttributesTable->count(); i++) {
  221. int mapIndex = i + 1;
  222. QString mapName = mapAttributesTable->value(mapIndex);
  223. if (!mapName.contains("UnknownMapAttributes")) {
  224. text += QString("\t.4byte %1_MapAttributes\n").arg(mapName);
  225. } else {
  226. text += QString("\t.4byte %1\n").arg(mapName);
  227. }
  228. }
  229. saveTextFile(getMapAttributesTableFilepath(), text);
  230. }
  231. QString Project::getMapAttributesTableFilepath() {
  232. return QString("%1/data/maps/attributes_table.inc").arg(root);
  233. }
  234. void Project::readMapAttributes(Map* map) {
  235. if (!map->isPersistedToFile) {
  236. return;
  237. }
  238. Asm *parser = new Asm;
  239. QString assets_text = readTextFile(root + "/data/maps/_assets.inc");
  240. if (assets_text.isNull()) {
  241. return;
  242. }
  243. QStringList *attributes = getLabelValues(parser->parse(assets_text), map->attributes_label);
  244. map->width = attributes->value(0);
  245. map->height = attributes->value(1);
  246. map->border_label = attributes->value(2);
  247. map->blockdata_label = attributes->value(3);
  248. map->tileset_primary_label = attributes->value(4);
  249. map->tileset_secondary_label = attributes->value(5);
  250. }
  251. void Project::setNewMapAttributes(Map* map) {
  252. map->width = "20";
  253. map->height = "20";
  254. map->border_label = QString("%1_MapBorder").arg(map->name);
  255. map->blockdata_label = QString("%1_MapBlockdata").arg(map->name);
  256. map->tileset_primary_label = "gTileset_General";
  257. map->tileset_secondary_label = "gTileset_Petalburg";
  258. }
  259. void Project::getTilesets(Map* map) {
  260. map->tileset_primary = getTileset(map->tileset_primary_label);
  261. map->tileset_secondary = getTileset(map->tileset_secondary_label);
  262. }
  263. Tileset* Project::loadTileset(QString label) {
  264. Asm *parser = new Asm;
  265. QString headers_text = readTextFile(root + "/data/tilesets/headers.inc");
  266. QStringList *values = getLabelValues(parser->parse(headers_text), label);
  267. Tileset *tileset = new Tileset;
  268. tileset->name = label;
  269. tileset->is_compressed = values->value(0);
  270. tileset->is_secondary = values->value(1);
  271. tileset->padding = values->value(2);
  272. tileset->tiles_label = values->value(3);
  273. tileset->palettes_label = values->value(4);
  274. tileset->metatiles_label = values->value(5);
  275. tileset->metatile_attrs_label = values->value(6);
  276. tileset->callback_label = values->value(7);
  277. loadTilesetAssets(tileset);
  278. tileset_cache->insert(label, tileset);
  279. return tileset;
  280. }
  281. QString Project::getBlockdataPath(Map* map) {
  282. QString text = readTextFile(root + "/data/maps/_assets.inc");
  283. QStringList *values = getLabelValues(parse(text), map->blockdata_label);
  284. QString path;
  285. if (!values->isEmpty()) {
  286. path = root + "/" + values->value(0).section('"', 1, 1);
  287. } else {
  288. path = root + "/data/maps/" + map->name + "/map.bin";
  289. }
  290. return path;
  291. }
  292. QString Project::getMapBorderPath(Map *map) {
  293. QString text = readTextFile(root + "/data/maps/_assets.inc");
  294. QStringList *values = getLabelValues(parse(text), map->border_label);
  295. QString path;
  296. if (!values->isEmpty()) {
  297. path = root + "/" + values->value(0).section('"', 1, 1);
  298. } else {
  299. path = root + "/data/maps/" + map->name + "/border.bin";
  300. }
  301. return path;
  302. }
  303. void Project::loadBlockdata(Map* map) {
  304. if (!map->isPersistedToFile) {
  305. return;
  306. }
  307. QString path = getBlockdataPath(map);
  308. map->blockdata = readBlockdata(path);
  309. }
  310. void Project::setNewMapBlockdata(Map* map) {
  311. Blockdata *blockdata = new Blockdata;
  312. for (int i = 0; i < map->getWidth() * map->getHeight(); i++) {
  313. blockdata->addBlock(qint16(0x3001));
  314. }
  315. map->blockdata = blockdata;
  316. }
  317. void Project::loadMapBorder(Map *map) {
  318. if (!map->isPersistedToFile) {
  319. return;
  320. }
  321. QString path = getMapBorderPath(map);
  322. map->border = readBlockdata(path);
  323. }
  324. void Project::setNewMapBorder(Map *map) {
  325. Blockdata *blockdata = new Blockdata;
  326. blockdata->addBlock(qint16(0x01D4));
  327. blockdata->addBlock(qint16(0x01D5));
  328. blockdata->addBlock(qint16(0x01DC));
  329. blockdata->addBlock(qint16(0x01DD));
  330. map->border = blockdata;
  331. }
  332. void Project::saveBlockdata(Map* map) {
  333. QString path = getBlockdataPath(map);
  334. writeBlockdata(path, map->blockdata);
  335. map->history.save();
  336. }
  337. void Project::writeBlockdata(QString path, Blockdata *blockdata) {
  338. QFile file(path);
  339. if (file.open(QIODevice::WriteOnly)) {
  340. QByteArray data = blockdata->serialize();
  341. file.write(data);
  342. }
  343. }
  344. void Project::saveAllMaps() {
  345. QList<QString> keys = map_cache->keys();
  346. for (int i = 0; i < keys.length(); i++) {
  347. QString key = keys.value(i);
  348. Map* map = map_cache->value(key);
  349. saveMap(map);
  350. }
  351. }
  352. void Project::saveMap(Map *map) {
  353. saveBlockdata(map);
  354. saveMapHeader(map);
  355. saveMapEvents(map);
  356. }
  357. void Project::saveAllDataStructures() {
  358. saveMapAttributesTable();
  359. }
  360. void Project::loadTilesetAssets(Tileset* tileset) {
  361. Asm* parser = new Asm;
  362. QString category = (tileset->is_secondary == "TRUE") ? "secondary" : "primary";
  363. if (tileset->name.isNull()) {
  364. return;
  365. }
  366. QString dir_path = root + "/data/tilesets/" + category + "/" + tileset->name.replace("gTileset_", "").toLower();
  367. QString graphics_text = readTextFile(root + "/data/tilesets/graphics.inc");
  368. QList<QStringList> *graphics = parser->parse(graphics_text);
  369. QStringList *tiles_values = getLabelValues(graphics, tileset->tiles_label);
  370. QStringList *palettes_values = getLabelValues(graphics, tileset->palettes_label);
  371. QString tiles_path;
  372. if (!tiles_values->isEmpty()) {
  373. tiles_path = root + "/" + tiles_values->value(0).section('"', 1, 1);
  374. } else {
  375. tiles_path = dir_path + "/tiles.4bpp";
  376. if (tileset->is_compressed == "TRUE") {
  377. tiles_path += ".lz";
  378. }
  379. }
  380. QStringList *palette_paths = new QStringList;
  381. if (!palettes_values->isEmpty()) {
  382. for (int i = 0; i < palettes_values->length(); i++) {
  383. QString value = palettes_values->value(i);
  384. palette_paths->append(root + "/" + value.section('"', 1, 1));
  385. }
  386. } else {
  387. QString palettes_dir_path = dir_path + "/palettes";
  388. for (int i = 0; i < 16; i++) {
  389. palette_paths->append(palettes_dir_path + "/" + QString("%1").arg(i, 2, 10, QLatin1Char('0')) + ".gbapal");
  390. }
  391. }
  392. QString metatiles_path;
  393. QString metatile_attrs_path;
  394. QString metatiles_text = readTextFile(root + "/data/tilesets/metatiles.inc");
  395. QList<QStringList> *metatiles_macros = parser->parse(metatiles_text);
  396. QStringList *metatiles_values = getLabelValues(metatiles_macros, tileset->metatiles_label);
  397. if (!metatiles_values->isEmpty()) {
  398. metatiles_path = root + "/" + metatiles_values->value(0).section('"', 1, 1);
  399. } else {
  400. metatiles_path = dir_path + "/metatiles.bin";
  401. }
  402. QStringList *metatile_attrs_values = getLabelValues(metatiles_macros, tileset->metatile_attrs_label);
  403. if (!metatile_attrs_values->isEmpty()) {
  404. metatile_attrs_path = root + "/" + metatile_attrs_values->value(0).section('"', 1, 1);
  405. } else {
  406. metatile_attrs_path = dir_path + "/metatile_attributes.bin";
  407. }
  408. // tiles
  409. tiles_path = fixGraphicPath(tiles_path);
  410. QImage *image = new QImage(tiles_path);
  411. //image->setColor(0, qRgb(0xff, 0, 0)); // debug
  412. QList<QImage> *tiles = new QList<QImage>;
  413. int w = 8;
  414. int h = 8;
  415. for (int y = 0; y < image->height(); y += h)
  416. for (int x = 0; x < image->width(); x += w) {
  417. QImage tile = image->copy(x, y, w, h);
  418. tiles->append(tile);
  419. }
  420. tileset->tiles = tiles;
  421. // metatiles
  422. //qDebug() << metatiles_path;
  423. QFile metatiles_file(metatiles_path);
  424. if (metatiles_file.open(QIODevice::ReadOnly)) {
  425. QByteArray data = metatiles_file.readAll();
  426. int num_metatiles = data.length() / 16;
  427. int num_layers = 2;
  428. QList<Metatile*> *metatiles = new QList<Metatile*>;
  429. for (int i = 0; i < num_metatiles; i++) {
  430. Metatile *metatile = new Metatile;
  431. int index = i * (2 * 4 * num_layers);
  432. for (int j = 0; j < 4 * num_layers; j++) {
  433. uint16_t word = data[index++] & 0xff;
  434. word += (data[index++] & 0xff) << 8;
  435. Tile tile;
  436. tile.tile = word & 0x3ff;
  437. tile.xflip = (word >> 10) & 1;
  438. tile.yflip = (word >> 11) & 1;
  439. tile.palette = (word >> 12) & 0xf;
  440. metatile->tiles->append(tile);
  441. }
  442. metatiles->append(metatile);
  443. }
  444. tileset->metatiles = metatiles;
  445. } else {
  446. tileset->metatiles = new QList<Metatile*>;
  447. qDebug() << QString("Could not open '%1'").arg(metatiles_path);
  448. }
  449. QFile attrs_file(metatile_attrs_path);
  450. //qDebug() << metatile_attrs_path;
  451. if (attrs_file.open(QIODevice::ReadOnly)) {
  452. QByteArray data = attrs_file.readAll();
  453. int num_metatiles = data.length() / 2;
  454. for (int i = 0; i < num_metatiles; i++) {
  455. uint16_t word = data[i*2] & 0xff;
  456. word += (data[i*2 + 1] & 0xff) << 8;
  457. tileset->metatiles->value(i)->attr = word;
  458. }
  459. } else {
  460. qDebug() << QString("Could not open '%1'").arg(metatile_attrs_path);
  461. }
  462. // palettes
  463. QList<QList<QRgb>> *palettes = new QList<QList<QRgb>>;
  464. for (int i = 0; i < palette_paths->length(); i++) {
  465. QString path = palette_paths->value(i);
  466. // the palettes are not compressed. this should never happen. it's only a precaution.
  467. path = path.replace(QRegExp("\\.lz$"), "");
  468. // TODO default to .pal (JASC-PAL)
  469. // just use .gbapal for now
  470. QFile file(path);
  471. QList<QRgb> palette;
  472. if (file.open(QIODevice::ReadOnly)) {
  473. QByteArray data = file.readAll();
  474. for (int j = 0; j < 16; j++) {
  475. uint16_t word = data[j*2] & 0xff;
  476. word += (data[j*2 + 1] & 0xff) << 8;
  477. int red = word & 0x1f;
  478. int green = (word >> 5) & 0x1f;
  479. int blue = (word >> 10) & 0x1f;
  480. QRgb color = qRgb(red * 8, green * 8, blue * 8);
  481. palette.prepend(color);
  482. }
  483. } else {
  484. for (int j = 0; j < 16; j++) {
  485. palette.append(qRgb(j * 16, j * 16, j * 16));
  486. }
  487. qDebug() << QString("Could not open '%1'").arg(path);
  488. }
  489. //qDebug() << path;
  490. palettes->append(palette);
  491. }
  492. tileset->palettes = palettes;
  493. }
  494. Blockdata* Project::readBlockdata(QString path) {
  495. Blockdata *blockdata = new Blockdata;
  496. //qDebug() << path;
  497. QFile file(path);
  498. if (file.open(QIODevice::ReadOnly)) {
  499. QByteArray data = file.readAll();
  500. for (int i = 0; (i + 1) < data.length(); i += 2) {
  501. uint16_t word = (data[i] & 0xff) + ((data[i + 1] & 0xff) << 8);
  502. blockdata->addBlock(word);
  503. }
  504. }
  505. return blockdata;
  506. }
  507. Map* Project::getMap(QString map_name) {
  508. if (map_cache->contains(map_name)) {
  509. return map_cache->value(map_name);
  510. } else {
  511. Map *map = loadMap(map_name);
  512. return map;
  513. }
  514. }
  515. Tileset* Project::getTileset(QString label) {
  516. if (tileset_cache->contains(label)) {
  517. return tileset_cache->value(label);
  518. } else {
  519. Tileset *tileset = loadTileset(label);
  520. return tileset;
  521. }
  522. }
  523. QString Project::readTextFile(QString path) {
  524. QFile file(path);
  525. if (!file.open(QIODevice::ReadOnly)) {
  526. //QMessageBox::information(0, "Error", QString("Could not open '%1': ").arg(path) + file.errorString());
  527. qDebug() << QString("Could not open '%1': ").arg(path) + file.errorString();
  528. return QString();
  529. }
  530. QTextStream in(&file);
  531. QString text = "";
  532. while (!in.atEnd()) {
  533. text += in.readLine() + "\n";
  534. }
  535. return text;
  536. }
  537. void Project::saveTextFile(QString path, QString text) {
  538. QFile file(path);
  539. if (file.open(QIODevice::WriteOnly)) {
  540. file.write(text.toUtf8());
  541. } else {
  542. qDebug() << QString("Could not open '%1' for writing: ").arg(path) + file.errorString();
  543. }
  544. }
  545. void Project::readMapGroups() {
  546. QString text = readTextFile(root + "/data/maps/_groups.inc");
  547. if (text.isNull()) {
  548. return;
  549. }
  550. Asm *parser = new Asm;
  551. QList<QStringList> *commands = parser->parse(text);
  552. bool in_group_pointers = false;
  553. QStringList *groups = new QStringList;
  554. for (int i = 0; i < commands->length(); i++) {
  555. QStringList params = commands->value(i);
  556. QString macro = params.value(0);
  557. if (macro == ".label") {
  558. if (in_group_pointers) {
  559. break;
  560. }
  561. if (params.value(1) == "gMapGroups") {
  562. in_group_pointers = true;
  563. }
  564. } else if (macro == ".4byte") {
  565. if (in_group_pointers) {
  566. for (int j = 1; j < params.length(); j++) {
  567. groups->append(params.value(j));
  568. }
  569. }
  570. }
  571. }
  572. QList<QStringList*> *groupedMaps = new QList<QStringList*>;
  573. for (int i = 0; i < groups->length(); i++) {
  574. QStringList *list = new QStringList;
  575. groupedMaps->append(list);
  576. }
  577. QStringList *maps = new QStringList;
  578. int group = -1;
  579. for (int i = 0; i < commands->length(); i++) {
  580. QStringList params = commands->value(i);
  581. QString macro = params.value(0);
  582. if (macro == ".label") {
  583. group = groups->indexOf(params.value(1));
  584. } else if (macro == ".4byte") {
  585. if (group != -1) {
  586. for (int j = 1; j < params.length(); j++) {
  587. QString mapName = params.value(j);
  588. QStringList *list = groupedMaps->value(group);
  589. list->append(mapName);
  590. maps->append(mapName);
  591. map_groups->insert(mapName, group);
  592. // Build the mapping and reverse mapping between map constants and map names.
  593. QString mapConstant = Map::mapConstantFromName(mapName);
  594. mapConstantsToMapNames->insert(mapConstant, mapName);
  595. mapNamesToMapConstants->insert(mapName, mapConstant);
  596. }
  597. }
  598. }
  599. }
  600. groupNames = groups;
  601. groupedMapNames = groupedMaps;
  602. mapNames = maps;
  603. }
  604. void Project::addNewMapToGroup(QString mapName, int groupNum) {
  605. // Write new map to project files.
  606. // 1. Create directory data/maps/<map_name>/
  607. // 2. Create file data/maps/<map_name>/border.bin
  608. // 3. Create file data/maps/<map_name>/header.inc
  609. // 4. Create file data/maps/<map_name>/map.bin
  610. // 5. Create file data/maps/events/<map_name>.inc
  611. // 6. Create file data/scripts/maps/<map_name>.inc
  612. // 7. Create file data/text/maps/<map_name>.inc
  613. // 8. Modify data/event_scripts.s:
  614. // .include "data/scripts/maps/<map_name>.inc"
  615. // .include "data/text/maps/<map_name>.inc"
  616. // 9. Modify data/map_events.s:
  617. // .include "data/maps/events/<map_name>.inc"
  618. // 10. Modify data/maps/_assets.inc
  619. // 11. Modify data/maps/_groups.inc
  620. // 12. Modify data/maps/attributes_table.inc
  621. // 13. Modify data/maps/headers.inc
  622. int mapIndex = mapAttributesTable->count() + 1;
  623. mapAttributesTable->insert(mapIndex, mapName);
  624. // Setup new map in memory, but don't write to file until map is actually saved later.
  625. mapNames->append(mapName);
  626. map_groups->insert(mapName, groupNum);
  627. groupedMapNames->value(groupNum)->append(mapName);
  628. Map *map = new Map;
  629. map->isPersistedToFile = false;
  630. map->setName(mapName);
  631. setNewMapHeader(map, mapIndex);
  632. setNewMapAttributes(map);
  633. getTilesets(map);
  634. setNewMapBlockdata(map);
  635. setNewMapBorder(map);
  636. setNewMapEvents(map);
  637. setNewMapConnections(map);
  638. map->commit();
  639. map->history.save();
  640. map_cache->insert(mapName, map);
  641. // QString dataDir = QString("%1/data/").arg(root);
  642. // QString dataMapsDir = QString("%1maps/").arg(dataDir);
  643. // QString newMapDataDir = QString("%1%2/").arg(dataMapsDir).arg(mapName);
  644. // // 1. Create directory data/maps/<map_name>/
  645. // if (!QDir::root().mkdir(newMapDataDir)) {
  646. // qDebug() << "Error: failed to create directory for new map. " << newMapDataDir;
  647. // return;
  648. // }
  649. // // 2. Create file data/maps/<map_name>/border.bin
  650. // QFile borderFile(newMapDataDir + "border.bin");
  651. // borderFile.open(QIODevice::WriteOnly);
  652. // QDataStream borderStream(&borderFile);
  653. // borderStream.setByteOrder(QDataStream::LittleEndian);
  654. // borderStream << qint16(0x01D4) << qint16(0x01D5) << qint16(0x01DC) << qint16(0x01DD);
  655. // borderFile.close();
  656. // // 3. Create file data/maps/<map_name>/header.inc
  657. // QFile headerFile(newMapDataDir + "header.inc");
  658. // headerFile.open(QIODevice::WriteOnly);
  659. // QTextStream headerStream(&headerFile);
  660. // headerStream << mapName << "::" << endl
  661. // << "\t.4byte " << mapName << "_MapAttributes" << endl
  662. // << "\t.4byte " << mapName << "_MapEvents" << endl
  663. // << "\t.4byte " << mapName << "_MapScripts" << endl
  664. // << "\t.4byte 0x0" << endl
  665. // << "\t.2byte BGM_DAN02" << endl
  666. // << "\t.2byte " << mapIndex << endl
  667. // << "\t.byte 0" << endl
  668. // << "\t.byte 0" << endl
  669. // << "\t.byte 11" << endl
  670. // << "\t.byte 4" << endl
  671. // << "\t.2byte 0" << endl
  672. // << "\t.byte 1" << endl
  673. // << "\t.byte 0" << endl;
  674. // headerFile.close();
  675. // // 4. Create file data/maps/<map_name>/map.bin
  676. // QFile mapFile(newMapDataDir + "map.bin");
  677. // mapFile.open(QIODevice::WriteOnly);
  678. // QDataStream mapStream(&mapFile);
  679. // mapStream.setByteOrder(QDataStream::LittleEndian);
  680. // for (int i = 0; i < 20 * 20; i++) {
  681. // mapStream << qint16(0x3001);
  682. // }
  683. // mapFile.close();
  684. // // 5. Create file data/maps/events/<map_name>.inc
  685. // QFile eventsFile(dataMapsDir + "events/" + mapName + ".inc");
  686. // eventsFile.open(QIODevice::WriteOnly);
  687. // QTextStream eventsStream(&eventsFile);
  688. // eventsStream << mapName << "_MapEvents::" << endl
  689. // << "\tmap_events 0x0, 0x0, 0x0, 0x0" << endl;
  690. // eventsFile.close();
  691. // // 6. Create file data/scripts/maps/<map_name>.inc
  692. // QFile scriptsFile(dataDir + "scripts/maps/" + mapName + ".inc");
  693. // scriptsFile.open(QIODevice::WriteOnly);
  694. // QTextStream scriptsStream(&scriptsFile);
  695. // scriptsStream << mapName << "_MapScripts::" << endl
  696. // << "\t.byte 0" << endl;
  697. // scriptsFile.close();
  698. // // 7. Create file data/text/maps/<map_name>.inc
  699. // QFile textFile(dataDir + "text/maps/" + mapName + ".inc");
  700. // textFile.open(QIODevice::WriteOnly);
  701. // QTextStream textStream(&textFile);
  702. // textStream << endl;
  703. // textFile.close();
  704. // // 8. Modify data/event_scripts.s:
  705. // QFile eventScriptsFile(dataDir + "event_scripts.s");
  706. // eventScriptsFile.open(QIODevice::Append);
  707. // QTextStream eventScriptsStream(&eventScriptsFile);
  708. // eventScriptsStream << endl
  709. // << "\t.include \"data/scripts/maps/" << mapName << ".inc\"" << endl
  710. // << "\t.include \"data/text/maps/" << mapName << ".inc\"" << endl;
  711. // eventScriptsFile.close();
  712. // // 9. Modify data/map_events.s:
  713. // QFile mapEventsFile(dataDir + "map_events.s");
  714. // mapEventsFile.open(QIODevice::Append);
  715. // QTextStream mapEventsStream(&mapEventsFile);
  716. // mapEventsStream << endl
  717. // << "\t.include \"data/maps/events/" << mapName << ".inc\"" << endl;
  718. // mapEventsFile.close();
  719. // // 10. Modify data/maps/_assets.inc
  720. // QFile assetsFile(dataMapsDir + "_assets.inc");
  721. // assetsFile.open(QIODevice::Append);
  722. // QTextStream assetsStream(&assetsFile);
  723. // assetsStream << endl
  724. // << mapName << "_MapBorder::" << endl
  725. // << "\t.incbin \"data/maps/" << mapName << "/border.bin\"" << endl
  726. // << endl
  727. // << mapName << "_MapBlockdata::" << endl
  728. // << "\t.incbin \"data/maps/" << mapName << "/map.bin\"" << endl
  729. // << endl
  730. // << "\t.align 2" << endl
  731. // << mapName << "_MapAttributes::" << endl
  732. // << "\t.4byte 0x14" << endl
  733. // << "\t.4byte 0x14" << endl
  734. // << "\t.4byte " << mapName << "_MapBorder" << endl
  735. // << "\t.4byte " << mapName << "_MapBlockdata" << endl
  736. // << "\t.4byte gTileset_General" << endl
  737. // << "\t.4byte gTileset_Pacifidlog" << endl
  738. // << endl;
  739. // assetsFile.close();
  740. // // 11. Modify data/maps/_groups.inc
  741. // // TODO:
  742. // // 12. Modify data/maps/attributes_table.inc
  743. // QFile attributesFile(dataMapsDir + "attributes_table.inc");
  744. // attributesFile.open(QIODevice::Append);
  745. // QTextStream attributesStream(&attributesFile);
  746. // attributesStream << endl
  747. // << "\t.4byte " << mapName << "_MapAttributes" << endl;
  748. // attributesFile.close();
  749. // // 13. Modify data/maps/headers.inc
  750. // QFile headersFile(dataMapsDir + "headers.inc");
  751. // headersFile.open(QIODevice::Append);
  752. // QTextStream headersStream(&headersFile);
  753. // headersStream << endl
  754. // << "\t.include \"data/maps/" << mapName << "/header.inc\"" << endl;
  755. // headersFile.close();
  756. }
  757. QString Project::getNewMapName() {
  758. // Ensure default name doesn't already exist.
  759. int i = 0;
  760. QString newMapName;
  761. do {
  762. newMapName = QString("NewMap%1").arg(++i);
  763. } while (mapNames->contains(newMapName));
  764. return newMapName;
  765. }
  766. QList<QStringList>* Project::parse(QString text) {
  767. Asm *parser = new Asm;
  768. return parser->parse(text);
  769. }
  770. QStringList Project::getLocations() {
  771. // TODO
  772. QStringList names;
  773. for (int i = 0; i < 88; i++) {
  774. names.append(QString("%1").arg(i));
  775. }
  776. return names;
  777. }
  778. QStringList Project::getVisibilities() {
  779. // TODO
  780. QStringList names;
  781. for (int i = 0; i < 16; i++) {
  782. names.append(QString("%1").arg(i));
  783. }
  784. return names;
  785. }
  786. QStringList Project::getWeathers() {
  787. // TODO
  788. QStringList names;
  789. for (int i = 0; i < 16; i++) {
  790. names.append(QString("%1").arg(i));
  791. }
  792. return names;
  793. }
  794. QStringList Project::getMapTypes() {
  795. // TODO
  796. QStringList names;
  797. for (int i = 0; i < 16; i++) {
  798. names.append(QString("%1").arg(i));
  799. }
  800. return names;
  801. }
  802. QStringList Project::getBattleScenes() {
  803. // TODO
  804. QStringList names;
  805. for (int i = 0; i < 16; i++) {
  806. names.append(QString("%1").arg(i));
  807. }
  808. return names;
  809. }
  810. QStringList Project::getSongNames() {
  811. QStringList names;
  812. QString text = readTextFile(root + "/include/constants/songs.h");
  813. if (!text.isNull()) {
  814. QStringList songDefinePrefixes;
  815. songDefinePrefixes << "SE_" << "BGM_";
  816. QMap<QString, int> songDefines = readCDefines(text, songDefinePrefixes);
  817. names = songDefines.keys();
  818. }
  819. return names;
  820. }
  821. QString Project::getSongName(int songNumber) {
  822. QStringList names;
  823. QString text = readTextFile(root + "/include/constants/songs.h");
  824. if (!text.isNull()) {
  825. QStringList songDefinePrefixes;
  826. songDefinePrefixes << "SE_" << "BGM_";
  827. QMap<QString, int> songDefines = readCDefines(text, songDefinePrefixes);
  828. // Loop through song defines, and fine the one with the matching song number.
  829. QMap<QString, int>::iterator iter = songDefines.begin();
  830. while (iter != songDefines.end()) {
  831. if (iter.value() == songNumber) {
  832. return iter.key();
  833. }
  834. iter++;
  835. }
  836. }
  837. return "";
  838. }
  839. QMap<QString, int> Project::getMapObjGfxConstants() {
  840. QMap<QString, int> constants;
  841. QString text = readTextFile(root + "/include/constants/map_objects.h");
  842. if (!text.isNull()) {
  843. QStringList mapObjGfxPrefixes;
  844. mapObjGfxPrefixes << "MAP_OBJ_GFX_";
  845. constants = readCDefines(text, mapObjGfxPrefixes);
  846. }
  847. return constants;
  848. }
  849. QString Project::fixGraphicPath(QString path) {
  850. path = path.replace(QRegExp("\\.lz$"), "");
  851. path = path.replace(QRegExp("\\.[1248]bpp$"), ".png");
  852. return path;
  853. }
  854. void Project::loadObjectPixmaps(QList<Event*> objects) {
  855. bool needs_update = false;
  856. for (Event *object : objects) {
  857. if (object->pixmap.isNull()) {
  858. needs_update = true;
  859. break;
  860. }
  861. }
  862. if (!needs_update) {
  863. return;
  864. }
  865. QMap<QString, int> constants = getMapObjGfxConstants();
  866. QString pointers_text = readTextFile(root + "/src/data/field_map_obj/map_object_graphics_info_pointers.h");
  867. QString info_text = readTextFile(root + "/src/data/field_map_obj/map_object_graphics_info.h");
  868. QString pic_text = readTextFile(root + "/src/data/field_map_obj/map_object_pic_tables.h");
  869. QString assets_text = readTextFile(root + "/src/field/field_map_obj.c");
  870. QStringList pointers = readCArray(pointers_text, "gMapObjectGraphicsInfoPointers");
  871. for (Event *object : objects) {
  872. if (!object->pixmap.isNull()) {
  873. continue;
  874. }
  875. QString event_type = object->get("event_type");
  876. if (event_type == "object") {
  877. object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(0, 0, 16, 16);
  878. } else if (event_type == "warp") {
  879. object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(16, 0, 16, 16);
  880. } else if (event_type == "trap" || event_type == "trap_weather") {
  881. object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(32, 0, 16, 16);
  882. } else if (event_type == "sign" || event_type == "event_hidden_item" || event_type == "event_secret_base") {
  883. object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(48, 0, 16, 16);
  884. }
  885. if (event_type == "object") {
  886. int sprite_id = constants.value(object->get("sprite"));
  887. QString info_label = pointers.value(sprite_id).replace("&", "");
  888. QString pic_label = readCArray(info_text, info_label).value(14);
  889. QString gfx_label = readCArray(pic_text, pic_label).value(0);
  890. gfx_label = gfx_label.section(QRegExp("[\\(\\)]"), 1, 1);
  891. QString path = readCIncbin(assets_text, gfx_label);
  892. if (!path.isNull()) {
  893. path = fixGraphicPath(path);
  894. QPixmap pixmap(root + "/" + path);
  895. if (!pixmap.isNull()) {
  896. object->pixmap = pixmap;
  897. }
  898. }
  899. }
  900. }
  901. }
  902. void Project::saveMapEvents(Map *map) {
  903. QString path = root + QString("/data/maps/events/%1.inc").arg(map->name);
  904. QString text = "";
  905. if (map->events["object"].length() > 0) {
  906. text += QString("%1::\n").arg(map->object_events_label);
  907. for (int i = 0; i < map->events["object"].length(); i++) {
  908. Event *object_event = map->events["object"].value(i);
  909. int radius_x = object_event->getInt("radius_x");
  910. int radius_y = object_event->getInt("radius_y");
  911. QString radius = QString("%1").arg((radius_x & 0xf) + ((radius_y & 0xf) << 4));
  912. uint16_t x = object_event->getInt("x");
  913. uint16_t y = object_event->getInt("y");
  914. text += QString("\tobject_event %1").arg(i + 1);
  915. text += QString(", %1").arg(object_event->get("sprite"));
  916. text += QString(", %1").arg(object_event->get("replacement"));
  917. text += QString(", %1").arg(x & 0xff);
  918. text += QString(", %1").arg((x >> 8) & 0xff);
  919. text += QString(", %1").arg(y & 0xff);
  920. text += QString(", %1").arg((y >> 8) & 0xff);
  921. text += QString(", %1").arg(object_event->get("elevation"));
  922. text += QString(", %1").arg(object_event->get("behavior"));
  923. text += QString(", %1").arg(radius);
  924. text += QString(", 0");
  925. text += QString(", %1").arg(object_event->get("property"));
  926. text += QString(", 0");
  927. text += QString(", %1").arg(object_event->get("sight_radius"));
  928. text += QString(", 0");
  929. text += QString(", %1").arg(object_event->get("script_label"));
  930. text += QString(", %1").arg(object_event->get("event_flag"));
  931. text += QString(", 0");
  932. text += QString(", 0");
  933. text += "\n";
  934. }
  935. text += "\n";
  936. }
  937. if (map->events["warp"].length() > 0) {
  938. text += QString("%1::\n").arg(map->warps_label);
  939. for (Event *warp : map->events["warp"]) {
  940. text += QString("\twarp_def %1").arg(warp->get("x"));
  941. text += QString(", %1").arg(warp->get("y"));
  942. text += QString(", %1").arg(warp->get("elevation"));
  943. text += QString(", %1").arg(warp->get("destination_warp"));
  944. text += QString(", %1").arg(mapNamesToMapConstants->value(warp->get("destination_map_name")));
  945. text += "\n";
  946. }
  947. text += "\n";
  948. }
  949. if (map->events["trap"].length() + map->events["trap_weather"].length() > 0) {
  950. text += QString("%1::\n").arg(map->coord_events_label);
  951. for (Event *coords : map->events["trap"]) {
  952. text += QString("\tcoord_event %1").arg(coords->get("x"));
  953. text += QString(", %1").arg(coords->get("y"));
  954. text += QString(", %1").arg(coords->get("elevation"));
  955. text += QString(", 0");
  956. text += QString(", %1").arg(coords->get("coord_unknown1"));
  957. text += QString(", %1").arg(coords->get("coord_unknown2"));
  958. text += QString(", 0");
  959. text += QString(", %1").arg(coords->get("script_label"));
  960. text += "\n";
  961. }
  962. for (Event *coords : map->events["trap_weather"]) {
  963. text += QString("\tcoord_weather_event %1").arg(coords->get("x"));
  964. text += QString(", %1").arg(coords->get("y"));
  965. text += QString(", %1").arg(coords->get("elevation"));
  966. text += QString(", %1").arg(coords->get("weather"));
  967. text += "\n";
  968. }
  969. text += "\n";
  970. }
  971. if (map->events["sign"].length() +
  972. map->events["event_hidden_item"].length() +
  973. map->events["event_secret_base"].length() > 0)
  974. {
  975. text += QString("%1::\n").arg(map->bg_events_label);
  976. for (Event *sign : map->events["sign"]) {
  977. text += QString("\tbg_event %1").arg(sign->get("x"));
  978. text += QString(", %1").arg(sign->get("y"));
  979. text += QString(", %1").arg(sign->get("elevation"));
  980. text += QString(", %1").arg(sign->get("type"));
  981. text += QString(", 0");
  982. text += QString(", %1").arg(sign->get("script_label"));
  983. text += "\n";
  984. }
  985. for (Event *item : map->events["event_hidden_item"]) {
  986. text += QString("\tbg_hidden_item_event %1").arg(item->get("x"));
  987. text += QString(", %1").arg(item->get("y"));
  988. text += QString(", %1").arg(item->get("elevation"));
  989. text += QString(", %1").arg(item->get("item"));
  990. text += QString(", %1").arg(item->get("flag"));
  991. text += "\n";
  992. }
  993. for (Event *item : map->events["event_secret_base"]) {
  994. text += QString("\tbg_secret_base_event %1").arg(item->get("x"));
  995. text += QString(", %1").arg(item->get("y"));
  996. text += QString(", %1").arg(item->get("elevation"));
  997. text += QString(", %1").arg(item->get("secret_base_map"));
  998. text += "\n";
  999. }
  1000. text += "\n";
  1001. }
  1002. text += QString("%1::\n").arg(map->events_label);
  1003. text += QString("\tmap_events %1, %2, %3, %4\n")
  1004. .arg(map->object_events_label)
  1005. .arg(map->warps_label)
  1006. .arg(map->coord_events_label)
  1007. .arg(map->bg_events_label);
  1008. saveTextFile(path, text);
  1009. }
  1010. void Project::readMapEvents(Map *map) {
  1011. if (!map->isPersistedToFile) {
  1012. return;
  1013. }
  1014. // lazy
  1015. QString path = root + QString("/data/maps/events/%1.inc").arg(map->name);
  1016. QString text = readTextFile(path);
  1017. if (text.isNull()) {
  1018. return;
  1019. }
  1020. QStringList *labels = getLabelValues(parse(text), map->events_label);
  1021. map->object_events_label = labels->value(0);
  1022. map->warps_label = labels->value(1);
  1023. map->coord_events_label = labels->value(2);
  1024. map->bg_events_label = labels->value(3);
  1025. QList<QStringList> *object_events = getLabelMacros(parse(text), map->object_events_label);
  1026. map->events["object"].clear();
  1027. for (QStringList command : *object_events) {
  1028. if (command.value(0) == "object_event") {
  1029. Event *object = new Event;
  1030. object->put("map_name", map->name);
  1031. // This macro is not fixed as of writing, but it should take fewer args.
  1032. bool old_macro = false;
  1033. if (command.length() >= 20) {
  1034. command.removeAt(19);
  1035. command.removeAt(18);
  1036. command.removeAt(15);
  1037. command.removeAt(13);
  1038. command.removeAt(11);
  1039. command.removeAt(1); // id. not 0, but is just the index in the list of objects
  1040. old_macro = true;
  1041. }
  1042. int i = 1;
  1043. object->put("sprite", command.value(i++));
  1044. object->put("replacement", command.value(i++));
  1045. int16_t x = command.value(i++).toInt(nullptr, 0) | (command.value(i++).toInt(nullptr, 0) << 8);
  1046. int16_t y = command.value(i++).toInt(nullptr, 0) | (command.value(i++).toInt(nullptr, 0) << 8);
  1047. object->put("x", x);
  1048. object->put("y", y);
  1049. object->put("elevation", command.value(i++));
  1050. object->put("behavior", command.value(i++));
  1051. if (old_macro) {
  1052. int radius = command.value(i++).toInt(nullptr, 0);
  1053. object->put("radius_x", radius & 0xf);
  1054. object->put("radius_y", (radius >> 4) & 0xf);
  1055. } else {
  1056. object->put("radius_x", command.value(i++));
  1057. object->put("radius_y", command.value(i++));
  1058. }
  1059. object->put("property", command.value(i++));
  1060. object->put("sight_radius", command.value(i++));
  1061. object->put("script_label", command.value(i++));
  1062. object->put("event_flag", command.value(i++));
  1063. object->put("event_type", "object");
  1064. map->events["object"].append(object);
  1065. }
  1066. }
  1067. QList<QStringList> *warps = getLabelMacros(parse(text), map->warps_label);
  1068. map->events["warp"].clear();
  1069. for (QStringList command : *warps) {
  1070. if (command.value(0) == "warp_def") {
  1071. Event *warp = new Event;
  1072. warp->put("map_name", map->name);
  1073. int i = 1;
  1074. warp->put("x", command.value(i++));
  1075. warp->put("y", command.value(i++));
  1076. warp->put("elevation", command.value(i++));
  1077. warp->put("destination_warp", command.value(i++));
  1078. // Ensure the warp destination map constant is valid before adding it to the warps.
  1079. QString mapConstant = command.value(i++);
  1080. if (mapConstantsToMapNames->contains(mapConstant)) {
  1081. warp->put("destination_map_name", mapConstantsToMapNames->value(mapConstant));
  1082. warp->put("event_type", "warp");
  1083. map->events["warp"].append(warp);
  1084. } else {
  1085. qDebug() << QString("Destination map constant '%1' is invalid for warp").arg(mapConstant);
  1086. }
  1087. }
  1088. }
  1089. QList<QStringList> *coords = getLabelMacros(parse(text), map->coord_events_label);
  1090. map->events["trap"].clear();
  1091. map->events["trap_weather"].clear();
  1092. for (QStringList command : *coords) {
  1093. if (command.value(0) == "coord_event") {
  1094. Event *coord = new Event;
  1095. coord->put("map_name", map->name);
  1096. bool old_macro = false;
  1097. if (command.length() >= 9) {
  1098. command.removeAt(7);
  1099. command.removeAt(4);
  1100. old_macro = true;
  1101. }
  1102. int i = 1;
  1103. coord->put("x", command.value(i++));
  1104. coord->put("y", command.value(i++));
  1105. coord->put("elevation", command.value(i++));
  1106. coord->put("coord_unknown1", command.value(i++));
  1107. coord->put("coord_unknown2", command.value(i++));
  1108. coord->put("script_label", command.value(i++));
  1109. //coord_unknown3
  1110. //coord_unknown4
  1111. coord->put("event_type", "trap");
  1112. map->events["trap"].append(coord);
  1113. } else if (command.value(0) == "coord_weather_event") {
  1114. Event *coord = new Event;
  1115. coord->put("map_name", map->name);
  1116. int i = 1;
  1117. coord->put("x", command.value(i++));
  1118. coord->put("y", command.value(i++));
  1119. coord->put("elevation", command.value(i++));
  1120. coord->put("weather", command.value(i++));
  1121. coord->put("event_type", "trap_weather");
  1122. map->events["trap_weather"].append(coord);
  1123. }
  1124. }
  1125. QList<QStringList> *bgs = getLabelMacros(parse(text), map->bg_events_label);
  1126. map->events["sign"].clear();
  1127. map->events["event_hidden_item"].clear();
  1128. map->events["event_secret_base"].clear();
  1129. for (QStringList command : *bgs) {
  1130. if (command.value(0) == "bg_event") {
  1131. Event *bg = new Event;
  1132. bg->put("map_name", map->name);
  1133. int i = 1;
  1134. bg->put("x", command.value(i++));
  1135. bg->put("y", command.value(i++));
  1136. bg->put("elevation", command.value(i++));
  1137. bg->put("type", command.value(i++));
  1138. i++;
  1139. bg->put("script_label", command.value(i++));
  1140. //sign_unknown7
  1141. bg->put("event_type", "sign");
  1142. map->events["sign"].append(bg);
  1143. } else if (command.value(0) == "bg_hidden_item_event") {
  1144. Event *bg = new Event;
  1145. bg->put("map_name", map->name);
  1146. int i = 1;
  1147. bg->put("x", command.value(i++));
  1148. bg->put("y", command.value(i++));
  1149. bg->put("elevation", command.value(i++));
  1150. bg->put("item", command.value(i++));
  1151. bg->put("flag", command.value(i++));
  1152. bg->put("event_type", "event_hidden_item");
  1153. map->events["event_hidden_item"].append(bg);
  1154. } else if (command.value(0) == "bg_secret_base_event") {
  1155. Event *bg = new Event;
  1156. bg->put("map_name", map->name);
  1157. int i = 1;
  1158. bg->put("x", command.value(i++));
  1159. bg->put("y", command.value(i++));
  1160. bg->put("elevation", command.value(i++));
  1161. bg->put("secret_base_map", command.value(i++));
  1162. bg->put("event_type", "event_secret_base");
  1163. map->events["event_secret_base"].append(bg);
  1164. }
  1165. }
  1166. }
  1167. void Project::setNewMapEvents(Map *map) {
  1168. map->object_events_label = "0x0";
  1169. map->warps_label = "0x0";
  1170. map->coord_events_label = "0x0";
  1171. map->bg_events_label = "0x0";
  1172. map->events["object"].clear();
  1173. map->events["warp"].clear();
  1174. map->events["trap"].clear();
  1175. map->events["trap_weather"].clear();
  1176. map->events["sign"].clear();
  1177. map->events["event_hidden_item"].clear();
  1178. map->events["event_secret_base"].clear();
  1179. }
  1180. QStringList Project::readCArray(QString text, QString label) {
  1181. QStringList list;
  1182. if (label.isNull()) {
  1183. return list;
  1184. }
  1185. QRegExp *re = new QRegExp(QString("\\b%1\\b\\s*\\[?\\s*\\]?\\s*=\\s*\\{([^\\}]*)\\}").arg(label));
  1186. int pos = re->indexIn(text);
  1187. if (pos != -1) {
  1188. QString body = re->cap(1);
  1189. body = body.replace(QRegExp("\\s*"), "");
  1190. list = body.split(',');
  1191. /*
  1192. QRegExp *inner = new QRegExp("&?\\b([A-Za-z0-9_\\(\\)]*)\\b,");
  1193. int pos = 0;
  1194. while ((pos = inner->indexIn(body, pos)) != -1) {
  1195. list << inner->cap(1);
  1196. pos += inner->matchedLength();
  1197. }
  1198. */
  1199. }
  1200. return list;
  1201. }
  1202. QString Project::readCIncbin(QString text, QString label) {
  1203. QString path;
  1204. if (label.isNull()) {
  1205. return path;
  1206. }
  1207. QRegExp *re = new QRegExp(QString(
  1208. "\\b%1\\b"
  1209. "\\s*\\[?\\s*\\]?\\s*=\\s*"
  1210. "INCBIN_[US][0-9][0-9]?"
  1211. "\\(\"([^\"]*)\"\\)").arg(label));
  1212. int pos = re->indexIn(text);
  1213. if (pos != -1) {
  1214. path = re->cap(1);
  1215. }
  1216. return path;
  1217. }
  1218. QMap<QString, int> Project::readCDefines(QString text, QStringList prefixes) {
  1219. QMap<QString, int> defines;
  1220. QString combinedPrefixes = "[" + prefixes.join('|') + "]";
  1221. QRegularExpression re(QString("#define\\s+(?<defineName>%1\\w+)\\s(?<defineValue>\\w+)").arg(combinedPrefixes));
  1222. QRegularExpressionMatchIterator iter = re.globalMatch(text);
  1223. while (iter.hasNext()) {
  1224. QRegularExpressionMatch match = iter.next();
  1225. QString name = match.captured("defineName");
  1226. QString value = match.captured("defineValue");
  1227. bool valid;
  1228. int parsedValue = value.startsWith("0x") ? value.toInt(&valid, 16) : value.toInt(&valid, 10);
  1229. if (valid) {
  1230. if (!defines.contains(name)) {
  1231. defines.insert(name, parsedValue);
  1232. } else {
  1233. qDebug() << QString("Define '%1' is defined multiple times'").arg(name);
  1234. }
  1235. } else {
  1236. qDebug() << QString("Failed to parse define '%1' value '%2' as base 10 or hexadecimal value").arg(name, value);
  1237. }
  1238. }
  1239. return defines;
  1240. }