説明なし

project.cpp 38KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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" || event_type == "trap_weather") {
  603. object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(32, 0, 16, 16);
  604. } else if (event_type == "sign" || event_type == "event_hidden_item" || event_type == "event_secret_base") {
  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. if (map->events["object"].length() > 0) {
  628. text += QString("%1::\n").arg(map->object_events_label);
  629. for (int i = 0; i < map->events["object"].length(); i++) {
  630. Event *object_event = map->events["object"].value(i);
  631. int radius_x = object_event->getInt("radius_x");
  632. int radius_y = object_event->getInt("radius_y");
  633. QString radius = QString("%1").arg((radius_x & 0xf) + ((radius_y & 0xf) << 4));
  634. uint16_t x = object_event->getInt("x");
  635. uint16_t y = object_event->getInt("y");
  636. text += QString("\tobject_event %1").arg(i + 1);
  637. text += QString(", %1").arg(object_event->get("sprite"));
  638. text += QString(", %1").arg(object_event->get("replacement"));
  639. text += QString(", %1").arg(x & 0xff);
  640. text += QString(", %1").arg((x >> 8) & 0xff);
  641. text += QString(", %1").arg(y & 0xff);
  642. text += QString(", %1").arg((y >> 8) & 0xff);
  643. text += QString(", %1").arg(object_event->get("elevation"));
  644. text += QString(", %1").arg(object_event->get("behavior"));
  645. text += QString(", %1").arg(radius);
  646. text += QString(", 0");
  647. text += QString(", %1").arg(object_event->get("property"));
  648. text += QString(", 0");
  649. text += QString(", %1").arg(object_event->get("sight_radius"));
  650. text += QString(", 0");
  651. text += QString(", %1").arg(object_event->get("script_label"));
  652. text += QString(", %1").arg(object_event->get("event_flag"));
  653. text += QString(", 0");
  654. text += QString(", 0");
  655. text += "\n";
  656. }
  657. text += "\n";
  658. }
  659. if (map->events["warp"].length() > 0) {
  660. text += QString("%1::\n").arg(map->warps_label);
  661. for (Event *warp : map->events["warp"]) {
  662. text += QString("\twarp_def %1").arg(warp->get("x"));
  663. text += QString(", %1").arg(warp->get("y"));
  664. text += QString(", %1").arg(warp->get("elevation"));
  665. text += QString(", %1").arg(warp->get("destination_warp"));
  666. text += QString(", %1").arg(mapNamesToMapConstants[warp->get("destination_map_name")]);
  667. text += "\n";
  668. }
  669. text += "\n";
  670. }
  671. if (map->events["trap"].length() + map->events["trap_weather"].length() > 0) {
  672. text += QString("%1::\n").arg(map->coord_events_label);
  673. for (Event *coords : map->events["trap"]) {
  674. text += QString("\tcoord_event %1").arg(coords->get("x"));
  675. text += QString(", %1").arg(coords->get("y"));
  676. text += QString(", %1").arg(coords->get("elevation"));
  677. text += QString(", 0");
  678. text += QString(", %1").arg(coords->get("coord_unknown1"));
  679. text += QString(", %1").arg(coords->get("coord_unknown2"));
  680. text += QString(", 0");
  681. text += QString(", %1").arg(coords->get("script_label"));
  682. text += "\n";
  683. }
  684. for (Event *coords : map->events["trap_weather"]) {
  685. text += QString("\tcoord_weather_event %1").arg(coords->get("x"));
  686. text += QString(", %1").arg(coords->get("y"));
  687. text += QString(", %1").arg(coords->get("elevation"));
  688. text += QString(", %1").arg(coords->get("weather"));
  689. text += "\n";
  690. }
  691. text += "\n";
  692. }
  693. if (map->events["sign"].length() +
  694. map->events["event_hidden_item"].length() +
  695. map->events["event_secret_base"].length() > 0)
  696. {
  697. text += QString("%1::\n").arg(map->bg_events_label);
  698. for (Event *sign : map->events["sign"]) {
  699. text += QString("\tbg_event %1").arg(sign->get("x"));
  700. text += QString(", %1").arg(sign->get("y"));
  701. text += QString(", %1").arg(sign->get("elevation"));
  702. text += QString(", %1").arg(sign->get("type"));
  703. text += QString(", 0");
  704. text += QString(", %1").arg(sign->get("script_label"));
  705. text += "\n";
  706. }
  707. for (Event *item : map->events["event_hidden_item"]) {
  708. text += QString("\tbg_hidden_item_event %1").arg(item->get("x"));
  709. text += QString(", %1").arg(item->get("y"));
  710. text += QString(", %1").arg(item->get("elevation"));
  711. text += QString(", %1").arg(item->get("item"));
  712. text += QString(", %1").arg(item->get("flag"));
  713. text += "\n";
  714. }
  715. for (Event *item : map->events["event_secret_base"]) {
  716. text += QString("\tbg_secret_base_event %1").arg(item->get("x"));
  717. text += QString(", %1").arg(item->get("y"));
  718. text += QString(", %1").arg(item->get("elevation"));
  719. text += QString(", %1").arg(item->get("secret_base_map"));
  720. text += "\n";
  721. }
  722. text += "\n";
  723. }
  724. text += QString("%1::\n").arg(map->events_label);
  725. text += QString("\tmap_events %1, %2, %3, %4\n")
  726. .arg(map->object_events_label)
  727. .arg(map->warps_label)
  728. .arg(map->coord_events_label)
  729. .arg(map->bg_events_label);
  730. saveTextFile(path, text);
  731. }
  732. void Project::readMapEvents(Map *map) {
  733. // lazy
  734. QString path = root + QString("/data/maps/events/%1.inc").arg(map->name);
  735. QString text = readTextFile(path);
  736. if (text.isNull()) {
  737. return;
  738. }
  739. QStringList *labels = getLabelValues(parse(text), map->events_label);
  740. map->object_events_label = labels->value(0);
  741. map->warps_label = labels->value(1);
  742. map->coord_events_label = labels->value(2);
  743. map->bg_events_label = labels->value(3);
  744. QList<QStringList> *object_events = getLabelMacros(parse(text), map->object_events_label);
  745. map->events["object"].clear();
  746. for (QStringList command : *object_events) {
  747. if (command.value(0) == "object_event") {
  748. Event *object = new Event;
  749. object->put("map_name", map->name);
  750. // This macro is not fixed as of writing, but it should take fewer args.
  751. bool old_macro = false;
  752. if (command.length() >= 20) {
  753. command.removeAt(19);
  754. command.removeAt(18);
  755. command.removeAt(15);
  756. command.removeAt(13);
  757. command.removeAt(11);
  758. command.removeAt(1); // id. not 0, but is just the index in the list of objects
  759. old_macro = true;
  760. }
  761. int i = 1;
  762. object->put("sprite", command.value(i++));
  763. object->put("replacement", command.value(i++));
  764. int16_t x = command.value(i++).toInt(nullptr, 0) | (command.value(i++).toInt(nullptr, 0) << 8);
  765. int16_t y = command.value(i++).toInt(nullptr, 0) | (command.value(i++).toInt(nullptr, 0) << 8);
  766. object->put("x", x);
  767. object->put("y", y);
  768. object->put("elevation", command.value(i++));
  769. object->put("behavior", command.value(i++));
  770. if (old_macro) {
  771. int radius = command.value(i++).toInt(nullptr, 0);
  772. object->put("radius_x", radius & 0xf);
  773. object->put("radius_y", (radius >> 4) & 0xf);
  774. } else {
  775. object->put("radius_x", command.value(i++));
  776. object->put("radius_y", command.value(i++));
  777. }
  778. object->put("property", command.value(i++));
  779. object->put("sight_radius", command.value(i++));
  780. object->put("script_label", command.value(i++));
  781. object->put("event_flag", command.value(i++));
  782. object->put("event_type", "object");
  783. map->events["object"].append(object);
  784. }
  785. }
  786. QList<QStringList> *warps = getLabelMacros(parse(text), map->warps_label);
  787. map->events["warp"].clear();
  788. for (QStringList command : *warps) {
  789. if (command.value(0) == "warp_def") {
  790. Event *warp = new Event;
  791. warp->put("map_name", map->name);
  792. int i = 1;
  793. warp->put("x", command.value(i++));
  794. warp->put("y", command.value(i++));
  795. warp->put("elevation", command.value(i++));
  796. warp->put("destination_warp", command.value(i++));
  797. // Ensure the warp destination map constant is valid before adding it to the warps.
  798. QString mapConstant = command.value(i++);
  799. if (mapConstantsToMapNames.contains(mapConstant)) {
  800. warp->put("destination_map_name", mapConstantsToMapNames[mapConstant]);
  801. warp->put("event_type", "warp");
  802. map->events["warp"].append(warp);
  803. } else {
  804. qDebug() << QString("Destination map constant '%1' is invalid for warp").arg(mapConstant);
  805. }
  806. }
  807. }
  808. QList<QStringList> *coords = getLabelMacros(parse(text), map->coord_events_label);
  809. map->events["trap"].clear();
  810. map->events["trap_weather"].clear();
  811. for (QStringList command : *coords) {
  812. if (command.value(0) == "coord_event") {
  813. Event *coord = new Event;
  814. coord->put("map_name", map->name);
  815. bool old_macro = false;
  816. if (command.length() >= 9) {
  817. command.removeAt(7);
  818. command.removeAt(4);
  819. old_macro = true;
  820. }
  821. int i = 1;
  822. coord->put("x", command.value(i++));
  823. coord->put("y", command.value(i++));
  824. coord->put("elevation", command.value(i++));
  825. coord->put("coord_unknown1", command.value(i++));
  826. coord->put("coord_unknown2", command.value(i++));
  827. coord->put("script_label", command.value(i++));
  828. //coord_unknown3
  829. //coord_unknown4
  830. coord->put("event_type", "trap");
  831. map->events["trap"].append(coord);
  832. } else if (command.value(0) == "coord_weather_event") {
  833. Event *coord = new Event;
  834. coord->put("map_name", map->name);
  835. int i = 1;
  836. coord->put("x", command.value(i++));
  837. coord->put("y", command.value(i++));
  838. coord->put("elevation", command.value(i++));
  839. coord->put("weather", command.value(i++));
  840. coord->put("event_type", "trap_weather");
  841. map->events["trap_weather"].append(coord);
  842. }
  843. }
  844. QList<QStringList> *bgs = getLabelMacros(parse(text), map->bg_events_label);
  845. map->events["sign"].clear();
  846. map->events["event_hidden_item"].clear();
  847. map->events["event_secret_base"].clear();
  848. for (QStringList command : *bgs) {
  849. if (command.value(0) == "bg_event") {
  850. Event *bg = new Event;
  851. bg->put("map_name", map->name);
  852. int i = 1;
  853. bg->put("x", command.value(i++));
  854. bg->put("y", command.value(i++));
  855. bg->put("elevation", command.value(i++));
  856. bg->put("type", command.value(i++));
  857. i++;
  858. bg->put("script_label", command.value(i++));
  859. //sign_unknown7
  860. bg->put("event_type", "sign");
  861. map->events["sign"].append(bg);
  862. } else if (command.value(0) == "bg_hidden_item_event") {
  863. Event *bg = new Event;
  864. bg->put("map_name", map->name);
  865. int i = 1;
  866. bg->put("x", command.value(i++));
  867. bg->put("y", command.value(i++));
  868. bg->put("elevation", command.value(i++));
  869. bg->put("item", command.value(i++));
  870. bg->put("flag", command.value(i++));
  871. bg->put("event_type", "event_hidden_item");
  872. map->events["event_hidden_item"].append(bg);
  873. } else if (command.value(0) == "bg_secret_base_event") {
  874. Event *bg = new Event;
  875. bg->put("map_name", map->name);
  876. int i = 1;
  877. bg->put("x", command.value(i++));
  878. bg->put("y", command.value(i++));
  879. bg->put("elevation", command.value(i++));
  880. bg->put("secret_base_map", command.value(i++));
  881. bg->put("event_type", "event_secret_base");
  882. map->events["event_secret_base"].append(bg);
  883. }
  884. }
  885. }
  886. QStringList Project::readCArray(QString text, QString label) {
  887. QStringList list;
  888. if (label.isNull()) {
  889. return list;
  890. }
  891. QRegExp *re = new QRegExp(QString("\\b%1\\b\\s*\\[?\\s*\\]?\\s*=\\s*\\{([^\\}]*)\\}").arg(label));
  892. int pos = re->indexIn(text);
  893. if (pos != -1) {
  894. QString body = re->cap(1);
  895. body = body.replace(QRegExp("\\s*"), "");
  896. list = body.split(',');
  897. /*
  898. QRegExp *inner = new QRegExp("&?\\b([A-Za-z0-9_\\(\\)]*)\\b,");
  899. int pos = 0;
  900. while ((pos = inner->indexIn(body, pos)) != -1) {
  901. list << inner->cap(1);
  902. pos += inner->matchedLength();
  903. }
  904. */
  905. }
  906. return list;
  907. }
  908. QString Project::readCIncbin(QString text, QString label) {
  909. QString path;
  910. if (label.isNull()) {
  911. return path;
  912. }
  913. QRegExp *re = new QRegExp(QString(
  914. "\\b%1\\b"
  915. "\\s*\\[?\\s*\\]?\\s*=\\s*"
  916. "INCBIN_[US][0-9][0-9]?"
  917. "\\(\"([^\"]*)\"\\)").arg(label));
  918. int pos = re->indexIn(text);
  919. if (pos != -1) {
  920. path = re->cap(1);
  921. }
  922. return path;
  923. }
  924. QMap<QString, int> Project::readCDefines(QString text, QStringList prefixes) {
  925. QMap<QString, int> defines;
  926. QString combinedPrefixes = "[" + prefixes.join('|') + "]";
  927. QRegularExpression re(QString("#define\\s+(?<defineName>%1\\w+)\\s(?<defineValue>\\w+)").arg(combinedPrefixes));
  928. QRegularExpressionMatchIterator iter = re.globalMatch(text);
  929. while (iter.hasNext()) {
  930. QRegularExpressionMatch match = iter.next();
  931. QString name = match.captured("defineName");
  932. QString value = match.captured("defineValue");
  933. bool valid;
  934. int parsedValue = value.startsWith("0x") ? value.toInt(&valid, 16) : value.toInt(&valid, 10);
  935. if (valid) {
  936. if (!defines.contains(name)) {
  937. defines.insert(name, parsedValue);
  938. } else {
  939. qDebug() << QString("Define '%1' is defined multiple times'").arg(name);
  940. }
  941. } else {
  942. qDebug() << QString("Failed to parse define '%1' value '%2' as base 10 or hexadecimal value").arg(name, value);
  943. }
  944. }
  945. return defines;
  946. }