暂无描述

project.cpp 57KB

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