No Description

project.cpp 57KB

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