Nessuna descrizione

project.cpp 59KB

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