Keine Beschreibung

project.cpp 58KB

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