Nav apraksta

project.cpp 58KB

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