Bez popisu

project.cpp 39KB

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