No Description

project.cpp 36KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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 <QFile>
  9. #include <QTextStream>
  10. #include <QMessageBox>
  11. #include <QRegularExpression>
  12. Project::Project()
  13. {
  14. groupNames = new QStringList;
  15. groupedMapNames = new QList<QStringList*>;
  16. mapNames = new QStringList;
  17. map_cache = new QMap<QString, Map*>;
  18. tileset_cache = new QMap<QString, Tileset*>;
  19. }
  20. QString Project::getProjectTitle() {
  21. if (!root.isNull()) {
  22. return root.section('/', -1);
  23. } else {
  24. return QString();
  25. }
  26. }
  27. Map* Project::loadMap(QString map_name) {
  28. Map *map = new Map;
  29. map->setName(map_name);
  30. readMapHeader(map);
  31. readMapAttributes(map);
  32. getTilesets(map);
  33. loadBlockdata(map);
  34. loadMapBorder(map);
  35. readMapEvents(map);
  36. loadMapConnections(map);
  37. map->commit();
  38. map->history.save();
  39. map_cache->insert(map_name, map);
  40. return map;
  41. }
  42. void Project::loadMapConnections(Map *map) {
  43. map->connections.clear();
  44. if (!map->connections_label.isNull()) {
  45. QString path = root + QString("/data/maps/%1/connections.inc").arg(map->name);
  46. QString text = readTextFile(path);
  47. if (!text.isNull()) {
  48. QList<QStringList> *commands = parse(text);
  49. QStringList *list = getLabelValues(commands, map->connections_label);
  50. //// Avoid using this value. It ought to be generated instead.
  51. //int num_connections = list->value(0).toInt(nullptr, 0);
  52. QString connections_list_label = list->value(1);
  53. QList<QStringList> *connections = getLabelMacros(commands, connections_list_label);
  54. for (QStringList command : *connections) {
  55. QString macro = command.value(0);
  56. if (macro == "connection") {
  57. Connection *connection = new Connection;
  58. connection->direction = command.value(1);
  59. connection->offset = command.value(2);
  60. QString mapConstant = command.value(3);
  61. if (mapConstantsToMapNames.contains(mapConstant)) {
  62. connection->map_name = mapConstantsToMapNames[mapConstant];
  63. map->connections.append(connection);
  64. } else {
  65. qDebug() << QString("Failed to find connected map for map constant '%1'").arg(mapConstant);
  66. }
  67. }
  68. }
  69. }
  70. }
  71. }
  72. QList<QStringList>* Project::getLabelMacros(QList<QStringList> *list, QString label) {
  73. bool in_label = false;
  74. QList<QStringList> *new_list = new QList<QStringList>;
  75. for (int i = 0; i < list->length(); i++) {
  76. QStringList params = list->value(i);
  77. QString macro = params.value(0);
  78. if (macro == ".label") {
  79. if (params.value(1) == label) {
  80. in_label = true;
  81. } else if (in_label) {
  82. // If nothing has been read yet, assume the label
  83. // we're looking for is in a stack of labels.
  84. if (new_list->length() > 0) {
  85. break;
  86. }
  87. }
  88. } else if (in_label) {
  89. new_list->append(params);
  90. }
  91. }
  92. return new_list;
  93. }
  94. // For if you don't care about filtering by macro,
  95. // and just want all values associated with some label.
  96. QStringList* Project::getLabelValues(QList<QStringList> *list, QString label) {
  97. list = getLabelMacros(list, label);
  98. QStringList *values = new QStringList;
  99. for (int i = 0; i < list->length(); i++) {
  100. QStringList params = list->value(i);
  101. QString macro = params.value(0);
  102. // Ignore .align
  103. if (macro == ".align") {
  104. continue;
  105. }
  106. for (int j = 1; j < params.length(); j++) {
  107. values->append(params.value(j));
  108. }
  109. }
  110. return values;
  111. }
  112. void Project::readMapHeader(Map* map) {
  113. QString label = map->name;
  114. Asm *parser = new Asm;
  115. QString header_text = readTextFile(root + "/data/maps/" + label + "/header.inc");
  116. if (header_text.isNull()) {
  117. return;
  118. }
  119. QStringList *header = getLabelValues(parser->parse(header_text), label);
  120. map->attributes_label = header->value(0);
  121. map->events_label = header->value(1);
  122. map->scripts_label = header->value(2);
  123. map->connections_label = header->value(3);
  124. map->song = header->value(4);
  125. map->index = header->value(5);
  126. map->location = header->value(6);
  127. map->visibility = header->value(7);
  128. map->weather = header->value(8);
  129. map->type = header->value(9);
  130. map->unknown = header->value(10);
  131. map->show_location = header->value(11);
  132. map->battle_scene = header->value(12);
  133. }
  134. void Project::saveMapHeader(Map *map) {
  135. QString label = map->name;
  136. QString header_path = root + "/data/maps/" + label + "/header.inc";
  137. QString text = "";
  138. text += QString("%1::\n").arg(label);
  139. text += QString("\t.4byte %1\n").arg(map->attributes_label);
  140. text += QString("\t.4byte %1\n").arg(map->events_label);
  141. text += QString("\t.4byte %1\n").arg(map->scripts_label);
  142. text += QString("\t.4byte %1\n").arg(map->connections_label);
  143. text += QString("\t.2byte %1\n").arg(map->song);
  144. text += QString("\t.2byte %1\n").arg(map->index);
  145. text += QString("\t.byte %1\n").arg(map->location);
  146. text += QString("\t.byte %1\n").arg(map->visibility);
  147. text += QString("\t.byte %1\n").arg(map->weather);
  148. text += QString("\t.byte %1\n").arg(map->type);
  149. text += QString("\t.2byte %1\n").arg(map->unknown);
  150. text += QString("\t.byte %1\n").arg(map->show_location);
  151. text += QString("\t.byte %1\n").arg(map->battle_scene);
  152. saveTextFile(header_path, text);
  153. }
  154. void Project::readMapAttributes(Map* map) {
  155. Asm *parser = new Asm;
  156. QString assets_text = readTextFile(root + "/data/maps/_assets.inc");
  157. if (assets_text.isNull()) {
  158. return;
  159. }
  160. QStringList *attributes = getLabelValues(parser->parse(assets_text), map->attributes_label);
  161. map->width = attributes->value(0);
  162. map->height = attributes->value(1);
  163. map->border_label = attributes->value(2);
  164. map->blockdata_label = attributes->value(3);
  165. map->tileset_primary_label = attributes->value(4);
  166. map->tileset_secondary_label = attributes->value(5);
  167. }
  168. void Project::getTilesets(Map* map) {
  169. map->tileset_primary = getTileset(map->tileset_primary_label);
  170. map->tileset_secondary = getTileset(map->tileset_secondary_label);
  171. }
  172. Tileset* Project::loadTileset(QString label) {
  173. Asm *parser = new Asm;
  174. QString headers_text = readTextFile(root + "/data/tilesets/headers.inc");
  175. QStringList *values = getLabelValues(parser->parse(headers_text), label);
  176. Tileset *tileset = new Tileset;
  177. tileset->name = label;
  178. tileset->is_compressed = values->value(0);
  179. tileset->is_secondary = values->value(1);
  180. tileset->padding = values->value(2);
  181. tileset->tiles_label = values->value(3);
  182. tileset->palettes_label = values->value(4);
  183. tileset->metatiles_label = values->value(5);
  184. tileset->metatile_attrs_label = values->value(6);
  185. tileset->callback_label = values->value(7);
  186. loadTilesetAssets(tileset);
  187. tileset_cache->insert(label, tileset);
  188. return tileset;
  189. }
  190. QString Project::getBlockdataPath(Map* map) {
  191. QString text = readTextFile(root + "/data/maps/_assets.inc");
  192. QStringList *values = getLabelValues(parse(text), map->blockdata_label);
  193. QString path;
  194. if (!values->isEmpty()) {
  195. path = root + "/" + values->value(0).section('"', 1, 1);
  196. } else {
  197. path = root + "/data/maps/" + map->name + "/map.bin";
  198. }
  199. return path;
  200. }
  201. QString Project::getMapBorderPath(Map *map) {
  202. QString text = readTextFile(root + "/data/maps/_assets.inc");
  203. QStringList *values = getLabelValues(parse(text), map->border_label);
  204. QString path;
  205. if (!values->isEmpty()) {
  206. path = root + "/" + values->value(0).section('"', 1, 1);
  207. } else {
  208. path = root + "/data/maps/" + map->name + "/border.bin";
  209. }
  210. return path;
  211. }
  212. void Project::loadBlockdata(Map* map) {
  213. QString path = getBlockdataPath(map);
  214. map->blockdata = readBlockdata(path);
  215. }
  216. void Project::loadMapBorder(Map *map) {
  217. QString path = getMapBorderPath(map);
  218. map->border = readBlockdata(path);
  219. }
  220. void Project::saveBlockdata(Map* map) {
  221. QString path = getBlockdataPath(map);
  222. writeBlockdata(path, map->blockdata);
  223. map->history.save();
  224. }
  225. void Project::writeBlockdata(QString path, Blockdata *blockdata) {
  226. QFile file(path);
  227. if (file.open(QIODevice::WriteOnly)) {
  228. QByteArray data = blockdata->serialize();
  229. file.write(data);
  230. }
  231. }
  232. void Project::saveAllMaps() {
  233. QList<QString> keys = map_cache->keys();
  234. for (int i = 0; i < keys.length(); i++) {
  235. QString key = keys.value(i);
  236. Map* map = map_cache->value(key);
  237. saveMap(map);
  238. }
  239. }
  240. void Project::saveMap(Map *map) {
  241. saveBlockdata(map);
  242. saveMapHeader(map);
  243. saveMapEvents(map);
  244. }
  245. void Project::loadTilesetAssets(Tileset* tileset) {
  246. Asm* parser = new Asm;
  247. QString category = (tileset->is_secondary == "TRUE") ? "secondary" : "primary";
  248. if (tileset->name.isNull()) {
  249. return;
  250. }
  251. QString dir_path = root + "/data/tilesets/" + category + "/" + tileset->name.replace("gTileset_", "").toLower();
  252. QString graphics_text = readTextFile(root + "/data/tilesets/graphics.inc");
  253. QList<QStringList> *graphics = parser->parse(graphics_text);
  254. QStringList *tiles_values = getLabelValues(graphics, tileset->tiles_label);
  255. QStringList *palettes_values = getLabelValues(graphics, tileset->palettes_label);
  256. QString tiles_path;
  257. if (!tiles_values->isEmpty()) {
  258. tiles_path = root + "/" + tiles_values->value(0).section('"', 1, 1);
  259. } else {
  260. tiles_path = dir_path + "/tiles.4bpp";
  261. if (tileset->is_compressed == "TRUE") {
  262. tiles_path += ".lz";
  263. }
  264. }
  265. QStringList *palette_paths = new QStringList;
  266. if (!palettes_values->isEmpty()) {
  267. for (int i = 0; i < palettes_values->length(); i++) {
  268. QString value = palettes_values->value(i);
  269. palette_paths->append(root + "/" + value.section('"', 1, 1));
  270. }
  271. } else {
  272. QString palettes_dir_path = dir_path + "/palettes";
  273. for (int i = 0; i < 16; i++) {
  274. palette_paths->append(palettes_dir_path + "/" + QString("%1").arg(i, 2, 10, QLatin1Char('0')) + ".gbapal");
  275. }
  276. }
  277. QString metatiles_path;
  278. QString metatile_attrs_path;
  279. QString metatiles_text = readTextFile(root + "/data/tilesets/metatiles.inc");
  280. QList<QStringList> *metatiles_macros = parser->parse(metatiles_text);
  281. QStringList *metatiles_values = getLabelValues(metatiles_macros, tileset->metatiles_label);
  282. if (!metatiles_values->isEmpty()) {
  283. metatiles_path = root + "/" + metatiles_values->value(0).section('"', 1, 1);
  284. } else {
  285. metatiles_path = dir_path + "/metatiles.bin";
  286. }
  287. QStringList *metatile_attrs_values = getLabelValues(metatiles_macros, tileset->metatile_attrs_label);
  288. if (!metatile_attrs_values->isEmpty()) {
  289. metatile_attrs_path = root + "/" + metatile_attrs_values->value(0).section('"', 1, 1);
  290. } else {
  291. metatile_attrs_path = dir_path + "/metatile_attributes.bin";
  292. }
  293. // tiles
  294. tiles_path = fixGraphicPath(tiles_path);
  295. QImage *image = new QImage(tiles_path);
  296. //image->setColor(0, qRgb(0xff, 0, 0)); // debug
  297. QList<QImage> *tiles = new QList<QImage>;
  298. int w = 8;
  299. int h = 8;
  300. for (int y = 0; y < image->height(); y += h)
  301. for (int x = 0; x < image->width(); x += w) {
  302. QImage tile = image->copy(x, y, w, h);
  303. tiles->append(tile);
  304. }
  305. tileset->tiles = tiles;
  306. // metatiles
  307. //qDebug() << metatiles_path;
  308. QFile metatiles_file(metatiles_path);
  309. if (metatiles_file.open(QIODevice::ReadOnly)) {
  310. QByteArray data = metatiles_file.readAll();
  311. int num_metatiles = data.length() / 16;
  312. int num_layers = 2;
  313. QList<Metatile*> *metatiles = new QList<Metatile*>;
  314. for (int i = 0; i < num_metatiles; i++) {
  315. Metatile *metatile = new Metatile;
  316. int index = i * (2 * 4 * num_layers);
  317. for (int j = 0; j < 4 * num_layers; j++) {
  318. uint16_t word = data[index++] & 0xff;
  319. word += (data[index++] & 0xff) << 8;
  320. Tile tile;
  321. tile.tile = word & 0x3ff;
  322. tile.xflip = (word >> 10) & 1;
  323. tile.yflip = (word >> 11) & 1;
  324. tile.palette = (word >> 12) & 0xf;
  325. metatile->tiles->append(tile);
  326. }
  327. metatiles->append(metatile);
  328. }
  329. tileset->metatiles = metatiles;
  330. } else {
  331. tileset->metatiles = new QList<Metatile*>;
  332. qDebug() << QString("Could not open '%1'").arg(metatiles_path);
  333. }
  334. QFile attrs_file(metatile_attrs_path);
  335. //qDebug() << metatile_attrs_path;
  336. if (attrs_file.open(QIODevice::ReadOnly)) {
  337. QByteArray data = attrs_file.readAll();
  338. int num_metatiles = data.length() / 2;
  339. for (int i = 0; i < num_metatiles; i++) {
  340. uint16_t word = data[i*2] & 0xff;
  341. word += (data[i*2 + 1] & 0xff) << 8;
  342. tileset->metatiles->value(i)->attr = word;
  343. }
  344. } else {
  345. qDebug() << QString("Could not open '%1'").arg(metatile_attrs_path);
  346. }
  347. // palettes
  348. QList<QList<QRgb>> *palettes = new QList<QList<QRgb>>;
  349. for (int i = 0; i < palette_paths->length(); i++) {
  350. QString path = palette_paths->value(i);
  351. // the palettes are not compressed. this should never happen. it's only a precaution.
  352. path = path.replace(QRegExp("\\.lz$"), "");
  353. // TODO default to .pal (JASC-PAL)
  354. // just use .gbapal for now
  355. QFile file(path);
  356. QList<QRgb> palette;
  357. if (file.open(QIODevice::ReadOnly)) {
  358. QByteArray data = file.readAll();
  359. for (int j = 0; j < 16; j++) {
  360. uint16_t word = data[j*2] & 0xff;
  361. word += (data[j*2 + 1] & 0xff) << 8;
  362. int red = word & 0x1f;
  363. int green = (word >> 5) & 0x1f;
  364. int blue = (word >> 10) & 0x1f;
  365. QRgb color = qRgb(red * 8, green * 8, blue * 8);
  366. palette.prepend(color);
  367. }
  368. } else {
  369. for (int j = 0; j < 16; j++) {
  370. palette.append(qRgb(j * 16, j * 16, j * 16));
  371. }
  372. qDebug() << QString("Could not open '%1'").arg(path);
  373. }
  374. //qDebug() << path;
  375. palettes->append(palette);
  376. }
  377. tileset->palettes = palettes;
  378. }
  379. Blockdata* Project::readBlockdata(QString path) {
  380. Blockdata *blockdata = new Blockdata;
  381. //qDebug() << path;
  382. QFile file(path);
  383. if (file.open(QIODevice::ReadOnly)) {
  384. QByteArray data = file.readAll();
  385. for (int i = 0; (i + 1) < data.length(); i += 2) {
  386. uint16_t word = (data[i] & 0xff) + ((data[i + 1] & 0xff) << 8);
  387. blockdata->addBlock(word);
  388. }
  389. }
  390. return blockdata;
  391. }
  392. Map* Project::getMap(QString map_name) {
  393. if (map_cache->contains(map_name)) {
  394. return map_cache->value(map_name);
  395. } else {
  396. Map *map = loadMap(map_name);
  397. return map;
  398. }
  399. }
  400. Tileset* Project::getTileset(QString label) {
  401. if (tileset_cache->contains(label)) {
  402. return tileset_cache->value(label);
  403. } else {
  404. Tileset *tileset = loadTileset(label);
  405. return tileset;
  406. }
  407. }
  408. QString Project::readTextFile(QString path) {
  409. QFile file(path);
  410. if (!file.open(QIODevice::ReadOnly)) {
  411. //QMessageBox::information(0, "Error", QString("Could not open '%1': ").arg(path) + file.errorString());
  412. qDebug() << QString("Could not open '%1': ").arg(path) + file.errorString();
  413. return QString();
  414. }
  415. QTextStream in(&file);
  416. QString text = "";
  417. while (!in.atEnd()) {
  418. text += in.readLine() + "\n";
  419. }
  420. return text;
  421. }
  422. void Project::saveTextFile(QString path, QString text) {
  423. QFile file(path);
  424. if (file.open(QIODevice::WriteOnly)) {
  425. file.write(text.toUtf8());
  426. } else {
  427. qDebug() << QString("Could not open '%1' for writing: ").arg(path) + file.errorString();
  428. }
  429. }
  430. void Project::readMapGroups() {
  431. QString text = readTextFile(root + "/data/maps/_groups.inc");
  432. if (text.isNull()) {
  433. return;
  434. }
  435. Asm *parser = new Asm;
  436. QList<QStringList> *commands = parser->parse(text);
  437. bool in_group_pointers = false;
  438. QStringList *groups = new QStringList;
  439. for (int i = 0; i < commands->length(); i++) {
  440. QStringList params = commands->value(i);
  441. QString macro = params.value(0);
  442. if (macro == ".label") {
  443. if (in_group_pointers) {
  444. break;
  445. }
  446. if (params.value(1) == "gMapGroups") {
  447. in_group_pointers = true;
  448. }
  449. } else if (macro == ".4byte") {
  450. if (in_group_pointers) {
  451. for (int j = 1; j < params.length(); j++) {
  452. groups->append(params.value(j));
  453. }
  454. }
  455. }
  456. }
  457. QList<QStringList*> *groupedMaps = new QList<QStringList*>;
  458. for (int i = 0; i < groups->length(); i++) {
  459. QStringList *list = new QStringList;
  460. groupedMaps->append(list);
  461. }
  462. QStringList *maps = new QStringList;
  463. int group = -1;
  464. for (int i = 0; i < commands->length(); i++) {
  465. QStringList params = commands->value(i);
  466. QString macro = params.value(0);
  467. if (macro == ".label") {
  468. group = groups->indexOf(params.value(1));
  469. } else if (macro == ".4byte") {
  470. if (group != -1) {
  471. for (int j = 1; j < params.length(); j++) {
  472. QString mapName = params.value(j);
  473. QStringList *list = groupedMaps->value(group);
  474. list->append(mapName);
  475. maps->append(mapName);
  476. // Build the mapping and reverse mapping between map constants and map names.
  477. QString mapConstant = Map::mapConstantFromName(mapName);
  478. mapConstantsToMapNames.insert(mapConstant, mapName);
  479. mapNamesToMapConstants.insert(mapName, mapConstant);
  480. }
  481. }
  482. }
  483. }
  484. groupNames = groups;
  485. groupedMapNames = groupedMaps;
  486. mapNames = maps;
  487. }
  488. QList<QStringList>* Project::parse(QString text) {
  489. Asm *parser = new Asm;
  490. return parser->parse(text);
  491. }
  492. QStringList Project::getLocations() {
  493. // TODO
  494. QStringList names;
  495. for (int i = 0; i < 88; i++) {
  496. names.append(QString("%1").arg(i));
  497. }
  498. return names;
  499. }
  500. QStringList Project::getVisibilities() {
  501. // TODO
  502. QStringList names;
  503. for (int i = 0; i < 16; i++) {
  504. names.append(QString("%1").arg(i));
  505. }
  506. return names;
  507. }
  508. QStringList Project::getWeathers() {
  509. // TODO
  510. QStringList names;
  511. for (int i = 0; i < 16; i++) {
  512. names.append(QString("%1").arg(i));
  513. }
  514. return names;
  515. }
  516. QStringList Project::getMapTypes() {
  517. // TODO
  518. QStringList names;
  519. for (int i = 0; i < 16; i++) {
  520. names.append(QString("%1").arg(i));
  521. }
  522. return names;
  523. }
  524. QStringList Project::getBattleScenes() {
  525. // TODO
  526. QStringList names;
  527. for (int i = 0; i < 16; i++) {
  528. names.append(QString("%1").arg(i));
  529. }
  530. return names;
  531. }
  532. QStringList Project::getSongNames() {
  533. QStringList names;
  534. QString text = readTextFile(root + "/include/constants/songs.h");
  535. if (!text.isNull()) {
  536. QStringList songDefinePrefixes;
  537. songDefinePrefixes << "SE_" << "BGM_";
  538. QMap<QString, int> songDefines = readCDefines(text, songDefinePrefixes);
  539. names = songDefines.keys();
  540. }
  541. return names;
  542. }
  543. QString Project::getSongName(int songNumber) {
  544. QStringList names;
  545. QString text = readTextFile(root + "/include/constants/songs.h");
  546. if (!text.isNull()) {
  547. QStringList songDefinePrefixes;
  548. songDefinePrefixes << "SE_" << "BGM_";
  549. QMap<QString, int> songDefines = readCDefines(text, songDefinePrefixes);
  550. // Loop through song defines, and fine the one with the matching song number.
  551. QMap<QString, int>::iterator iter = songDefines.begin();
  552. while (iter != songDefines.end()) {
  553. if (iter.value() == songNumber) {
  554. return iter.key();
  555. }
  556. iter++;
  557. }
  558. }
  559. return "";
  560. }
  561. QMap<QString, int> Project::getMapObjGfxConstants() {
  562. QMap<QString, int> constants;
  563. QString text = readTextFile(root + "/include/constants/map_objects.h");
  564. if (!text.isNull()) {
  565. QStringList mapObjGfxPrefixes;
  566. mapObjGfxPrefixes << "MAP_OBJ_GFX_";
  567. constants = readCDefines(text, mapObjGfxPrefixes);
  568. }
  569. return constants;
  570. }
  571. QString Project::fixGraphicPath(QString path) {
  572. path = path.replace(QRegExp("\\.lz$"), "");
  573. path = path.replace(QRegExp("\\.[1248]bpp$"), ".png");
  574. return path;
  575. }
  576. void Project::loadObjectPixmaps(QList<Event*> objects) {
  577. bool needs_update = false;
  578. for (Event *object : objects) {
  579. if (object->pixmap.isNull()) {
  580. needs_update = true;
  581. break;
  582. }
  583. }
  584. if (!needs_update) {
  585. return;
  586. }
  587. QMap<QString, int> constants = getMapObjGfxConstants();
  588. QString pointers_text = readTextFile(root + "/src/data/field_map_obj/map_object_graphics_info_pointers.h");
  589. QString info_text = readTextFile(root + "/src/data/field_map_obj/map_object_graphics_info.h");
  590. QString pic_text = readTextFile(root + "/src/data/field_map_obj/map_object_pic_tables.h");
  591. QString assets_text = readTextFile(root + "/src/field/field_map_obj.c");
  592. QStringList pointers = readCArray(pointers_text, "gMapObjectGraphicsInfoPointers");
  593. for (Event *object : objects) {
  594. if (!object->pixmap.isNull()) {
  595. continue;
  596. }
  597. QString event_type = object->get("event_type");
  598. if (event_type == "object") {
  599. object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(0, 0, 16, 16);
  600. } else if (event_type == "warp") {
  601. object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(16, 0, 16, 16);
  602. } else if (event_type == "trap") {
  603. object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(32, 0, 16, 16);
  604. } else if (event_type == "sign" || event_type == "hidden item") {
  605. object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(48, 0, 16, 16);
  606. }
  607. if (event_type == "object") {
  608. int sprite_id = constants.value(object->get("sprite"));
  609. QString info_label = pointers.value(sprite_id).replace("&", "");
  610. QString pic_label = readCArray(info_text, info_label).value(14);
  611. QString gfx_label = readCArray(pic_text, pic_label).value(0);
  612. gfx_label = gfx_label.section(QRegExp("[\\(\\)]"), 1, 1);
  613. QString path = readCIncbin(assets_text, gfx_label);
  614. if (!path.isNull()) {
  615. path = fixGraphicPath(path);
  616. QPixmap pixmap(root + "/" + path);
  617. if (!pixmap.isNull()) {
  618. object->pixmap = pixmap;
  619. }
  620. }
  621. }
  622. }
  623. }
  624. void Project::saveMapEvents(Map *map) {
  625. QString path = root + QString("/data/maps/events/%1.inc").arg(map->name);
  626. QString text = "";
  627. text += QString("%1::\n").arg(map->object_events_label);
  628. for (int i = 0; i < map->events["object"].length(); i++) {
  629. Event *object_event = map->events["object"].value(i);
  630. int radius_x = object_event->getInt("radius_x");
  631. int radius_y = object_event->getInt("radius_y");
  632. QString radius = QString("%1").arg((radius_x & 0xf) + ((radius_y & 0xf) << 4));
  633. uint16_t x = object_event->getInt("x");
  634. uint16_t y = object_event->getInt("y");
  635. text += QString("\tobject_event %1").arg(i + 1);
  636. text += QString(", %1").arg(object_event->get("sprite"));
  637. text += QString(", %1").arg(object_event->get("replacement"));
  638. text += QString(", %1").arg(x & 0xff);
  639. text += QString(", %1").arg((x >> 8) & 0xff);
  640. text += QString(", %1").arg(y & 0xff);
  641. text += QString(", %1").arg((y >> 8) & 0xff);
  642. text += QString(", %1").arg(object_event->get("elevation"));
  643. text += QString(", %1").arg(object_event->get("behavior"));
  644. text += QString(", %1").arg(radius);
  645. text += QString(", 0");
  646. text += QString(", %1").arg(object_event->get("property"));
  647. text += QString(", 0");
  648. text += QString(", %1").arg(object_event->get("sight_radius"));
  649. text += QString(", 0");
  650. text += QString(", %1").arg(object_event->get("script_label"));
  651. text += QString(", %1").arg(object_event->get("event_flag"));
  652. text += QString(", 0");
  653. text += QString(", 0");
  654. text += "\n";
  655. }
  656. text += "\n";
  657. text += QString("%1::\n").arg(map->warps_label);
  658. for (Event *warp : map->events["warp"]) {
  659. text += QString("\twarp_def %1").arg(warp->get("x"));
  660. text += QString(", %1").arg(warp->get("y"));
  661. text += QString(", %1").arg(warp->get("elevation"));
  662. text += QString(", %1").arg(warp->get("destination_warp"));
  663. text += QString(", %1").arg(warp->get("destination_map"));
  664. text += "\n";
  665. }
  666. text += "\n";
  667. text += QString("%1::\n").arg(map->coord_events_label);
  668. for (Event *coords : map->events["trap"]) {
  669. text += QString("\tcoord_event %1").arg(coords->get("x"));
  670. text += QString(", %1").arg(coords->get("y"));
  671. text += QString(", %1").arg(coords->get("elevation"));
  672. text += QString(", 0");
  673. text += QString(", %1").arg(coords->get("coord_unknown1"));
  674. text += QString(", %1").arg(coords->get("coord_unknown2"));
  675. text += QString(", 0");
  676. text += QString(", %1").arg(coords->get("script_label"));
  677. text += "\n";
  678. }
  679. text += "\n";
  680. text += QString("%1::\n").arg(map->bg_events_label);
  681. for (Event *sign : map->events["sign"]) {
  682. text += QString("\tbg_event %1").arg(sign->get("x"));
  683. text += QString(", %1").arg(sign->get("y"));
  684. text += QString(", %1").arg(sign->get("elevation"));
  685. text += QString(", %1").arg(sign->get("type"));
  686. text += QString(", 0");
  687. text += QString(", %1").arg(sign->get("script_label"));
  688. text += "\n";
  689. }
  690. for (Event *item : map->events["hidden item"]) {
  691. text += QString("\tbg_event %1").arg(item->get("x"));
  692. text += QString(", %1").arg(item->get("y"));
  693. text += QString(", %1").arg(item->get("elevation"));
  694. text += QString(", %1").arg(item->get("type"));
  695. text += QString(", 0");
  696. text += QString(", %1").arg(item->get("item"));
  697. text += QString(", %1").arg(item->get("item_unknown5"));
  698. text += QString(", %1").arg(item->get("item_unknown6"));
  699. text += "\n";
  700. }
  701. text += "\n";
  702. text += QString("%1::\n").arg(map->events_label);
  703. text += QString("\tmap_events %1, %2, %3, %4\n")
  704. .arg(map->object_events_label)
  705. .arg(map->warps_label)
  706. .arg(map->coord_events_label)
  707. .arg(map->bg_events_label);
  708. saveTextFile(path, text);
  709. }
  710. void Project::readMapEvents(Map *map) {
  711. // lazy
  712. QString path = root + QString("/data/maps/events/%1.inc").arg(map->name);
  713. QString text = readTextFile(path);
  714. if (text.isNull()) {
  715. return;
  716. }
  717. QStringList *labels = getLabelValues(parse(text), map->events_label);
  718. map->object_events_label = labels->value(0);
  719. map->warps_label = labels->value(1);
  720. map->coord_events_label = labels->value(2);
  721. map->bg_events_label = labels->value(3);
  722. QList<QStringList> *object_events = getLabelMacros(parse(text), map->object_events_label);
  723. map->events["object"].clear();
  724. for (QStringList command : *object_events) {
  725. if (command.value(0) == "object_event") {
  726. Event *object = new Event;
  727. object->put("map_name", map->name);
  728. // This macro is not fixed as of writing, but it should take fewer args.
  729. bool old_macro = false;
  730. if (command.length() >= 20) {
  731. command.removeAt(19);
  732. command.removeAt(18);
  733. command.removeAt(15);
  734. command.removeAt(13);
  735. command.removeAt(11);
  736. command.removeAt(1); // id. not 0, but is just the index in the list of objects
  737. old_macro = true;
  738. }
  739. int i = 1;
  740. object->put("sprite", command.value(i++));
  741. object->put("replacement", command.value(i++));
  742. int16_t x = command.value(i++).toInt(nullptr, 0) | (command.value(i++).toInt(nullptr, 0) << 8);
  743. int16_t y = command.value(i++).toInt(nullptr, 0) | (command.value(i++).toInt(nullptr, 0) << 8);
  744. object->put("x", x);
  745. object->put("y", y);
  746. object->put("elevation", command.value(i++));
  747. object->put("behavior", command.value(i++));
  748. if (old_macro) {
  749. int radius = command.value(i++).toInt(nullptr, 0);
  750. object->put("radius_x", radius & 0xf);
  751. object->put("radius_y", (radius >> 4) & 0xf);
  752. } else {
  753. object->put("radius_x", command.value(i++));
  754. object->put("radius_y", command.value(i++));
  755. }
  756. object->put("property", command.value(i++));
  757. object->put("sight_radius", command.value(i++));
  758. object->put("script_label", command.value(i++));
  759. object->put("event_flag", command.value(i++));
  760. object->put("event_type", "object");
  761. map->events["object"].append(object);
  762. }
  763. }
  764. QList<QStringList> *warps = getLabelMacros(parse(text), map->warps_label);
  765. map->events["warp"].clear();
  766. for (QStringList command : *warps) {
  767. if (command.value(0) == "warp_def") {
  768. Event *warp = new Event;
  769. warp->put("map_name", map->name);
  770. int i = 1;
  771. warp->put("x", command.value(i++));
  772. warp->put("y", command.value(i++));
  773. warp->put("elevation", command.value(i++));
  774. warp->put("destination_warp", command.value(i++));
  775. // Ensure the warp destination map constant is valid before adding it to the warps.
  776. QString mapConstant = command.value(i++);
  777. if (mapConstantsToMapNames.contains(mapConstant)) {
  778. warp->put("destination_map_name", mapConstantsToMapNames[mapConstant]);
  779. warp->put("event_type", "warp");
  780. map->events["warp"].append(warp);
  781. } else {
  782. qDebug() << QString("Destination map constant '%1' is invalid for warp").arg(mapConstant);
  783. }
  784. }
  785. }
  786. QList<QStringList> *coords = getLabelMacros(parse(text), map->coord_events_label);
  787. map->events["trap"].clear();
  788. for (QStringList command : *coords) {
  789. if (command.value(0) == "coord_event") {
  790. Event *coord = new Event;
  791. coord->put("map_name", map->name);
  792. bool old_macro = false;
  793. if (command.length() >= 9) {
  794. command.removeAt(7);
  795. command.removeAt(4);
  796. old_macro = true;
  797. }
  798. int i = 1;
  799. coord->put("x", command.value(i++));
  800. coord->put("y", command.value(i++));
  801. coord->put("elevation", command.value(i++));
  802. coord->put("coord_unknown1", command.value(i++));
  803. coord->put("coord_unknown2", command.value(i++));
  804. coord->put("script_label", command.value(i++));
  805. //coord_unknown3
  806. //coord_unknown4
  807. coord->put("event_type", "trap");
  808. map->events["trap"].append(coord);
  809. }
  810. }
  811. QList<QStringList> *bgs = getLabelMacros(parse(text), map->bg_events_label);
  812. map->events["hidden item"].clear();
  813. map->events["sign"].clear();
  814. for (QStringList command : *bgs) {
  815. if (command.value(0) == "bg_event") {
  816. Event *bg = new Event;
  817. bg->put("map_name", map->name);
  818. int i = 1;
  819. bg->put("x", command.value(i++));
  820. bg->put("y", command.value(i++));
  821. bg->put("elevation", command.value(i++));
  822. bg->put("type", command.value(i++));
  823. i++;
  824. if (bg->is_hidden_item()) {
  825. bg->put("item", command.value(i++));
  826. bg->put("item_unknown5", command.value(i++));
  827. bg->put("item_unknown6", command.value(i++));
  828. bg->put("event_type", "hidden item");
  829. map->events["hidden item"].append(bg);
  830. } else {
  831. bg->put("script_label", command.value(i++));
  832. //sign_unknown7
  833. bg->put("event_type", "sign");
  834. map->events["sign"].append(bg);
  835. }
  836. }
  837. }
  838. }
  839. QStringList Project::readCArray(QString text, QString label) {
  840. QStringList list;
  841. if (label.isNull()) {
  842. return list;
  843. }
  844. QRegExp *re = new QRegExp(QString("\\b%1\\b\\s*\\[?\\s*\\]?\\s*=\\s*\\{([^\\}]*)\\}").arg(label));
  845. int pos = re->indexIn(text);
  846. if (pos != -1) {
  847. QString body = re->cap(1);
  848. body = body.replace(QRegExp("\\s*"), "");
  849. list = body.split(',');
  850. /*
  851. QRegExp *inner = new QRegExp("&?\\b([A-Za-z0-9_\\(\\)]*)\\b,");
  852. int pos = 0;
  853. while ((pos = inner->indexIn(body, pos)) != -1) {
  854. list << inner->cap(1);
  855. pos += inner->matchedLength();
  856. }
  857. */
  858. }
  859. return list;
  860. }
  861. QString Project::readCIncbin(QString text, QString label) {
  862. QString path;
  863. if (label.isNull()) {
  864. return path;
  865. }
  866. QRegExp *re = new QRegExp(QString(
  867. "\\b%1\\b"
  868. "\\s*\\[?\\s*\\]?\\s*=\\s*"
  869. "INCBIN_[US][0-9][0-9]?"
  870. "\\(\"([^\"]*)\"\\)").arg(label));
  871. int pos = re->indexIn(text);
  872. if (pos != -1) {
  873. path = re->cap(1);
  874. }
  875. return path;
  876. }
  877. QMap<QString, int> Project::readCDefines(QString text, QStringList prefixes) {
  878. QMap<QString, int> defines;
  879. QString combinedPrefixes = "[" + prefixes.join('|') + "]";
  880. QRegularExpression re(QString("#define\\s+(?<defineName>%1\\w+)\\s(?<defineValue>\\w+)").arg(combinedPrefixes));
  881. QRegularExpressionMatchIterator iter = re.globalMatch(text);
  882. while (iter.hasNext()) {
  883. QRegularExpressionMatch match = iter.next();
  884. QString name = match.captured("defineName");
  885. QString value = match.captured("defineValue");
  886. bool valid;
  887. int parsedValue = value.startsWith("0x") ? value.toInt(&valid, 16) : value.toInt(&valid, 10);
  888. if (valid) {
  889. if (!defines.contains(name)) {
  890. defines.insert(name, parsedValue);
  891. } else {
  892. qDebug() << QString("Define '%1' is defined multiple times'").arg(name);
  893. }
  894. } else {
  895. qDebug() << QString("Failed to parse define '%1' value '%2' as base 10 or hexadecimal value").arg(name, value);
  896. }
  897. }
  898. return defines;
  899. }