Brak opisu

project.cpp 55KB

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