설명 없음

project.cpp 58KB

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