No Description

project.cpp 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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. QStringList *list = groupedMaps->value(group);
  473. list->append(params.value(j));
  474. maps->append(params.value(j));
  475. // Build the mapping between map constants and map names.
  476. QString mapConstant = Map::mapConstantFromName(params.value(j));
  477. mapConstantsToMapNames.insert(mapConstant, params.value(j));
  478. }
  479. }
  480. }
  481. }
  482. groupNames = groups;
  483. groupedMapNames = groupedMaps;
  484. mapNames = maps;
  485. }
  486. QList<QStringList>* Project::parse(QString text) {
  487. Asm *parser = new Asm;
  488. return parser->parse(text);
  489. }
  490. QStringList Project::getLocations() {
  491. // TODO
  492. QStringList names;
  493. for (int i = 0; i < 88; i++) {
  494. names.append(QString("%1").arg(i));
  495. }
  496. return names;
  497. }
  498. QStringList Project::getVisibilities() {
  499. // TODO
  500. QStringList names;
  501. for (int i = 0; i < 16; i++) {
  502. names.append(QString("%1").arg(i));
  503. }
  504. return names;
  505. }
  506. QStringList Project::getWeathers() {
  507. // TODO
  508. QStringList names;
  509. for (int i = 0; i < 16; i++) {
  510. names.append(QString("%1").arg(i));
  511. }
  512. return names;
  513. }
  514. QStringList Project::getMapTypes() {
  515. // TODO
  516. QStringList names;
  517. for (int i = 0; i < 16; i++) {
  518. names.append(QString("%1").arg(i));
  519. }
  520. return names;
  521. }
  522. QStringList Project::getBattleScenes() {
  523. // TODO
  524. QStringList names;
  525. for (int i = 0; i < 16; i++) {
  526. names.append(QString("%1").arg(i));
  527. }
  528. return names;
  529. }
  530. QStringList Project::getSongNames() {
  531. QStringList names;
  532. QString text = readTextFile(root + "/include/constants/songs.h");
  533. if (!text.isNull()) {
  534. QStringList songDefinePrefixes;
  535. songDefinePrefixes << "SE_" << "BGM_";
  536. QMap<QString, int> songDefines = readCDefines(text, songDefinePrefixes);
  537. names = songDefines.keys();
  538. }
  539. return names;
  540. }
  541. QString Project::getSongName(int songNumber) {
  542. QStringList names;
  543. QString text = readTextFile(root + "/include/constants/songs.h");
  544. if (!text.isNull()) {
  545. QStringList songDefinePrefixes;
  546. songDefinePrefixes << "SE_" << "BGM_";
  547. QMap<QString, int> songDefines = readCDefines(text, songDefinePrefixes);
  548. // Loop through song defines, and fine the one with the matching song number.
  549. QMap<QString, int>::iterator iter = songDefines.begin();
  550. while (iter != songDefines.end()) {
  551. if (iter.value() == songNumber) {
  552. return iter.key();
  553. }
  554. iter++;
  555. }
  556. }
  557. return "";
  558. }
  559. QMap<QString, int> Project::getMapObjGfxConstants() {
  560. QMap<QString, int> constants;
  561. QString text = readTextFile(root + "/include/constants/map_objects.h");
  562. if (!text.isNull()) {
  563. QStringList mapObjGfxPrefixes;
  564. mapObjGfxPrefixes << "MAP_OBJ_GFX_";
  565. constants = readCDefines(text, mapObjGfxPrefixes);
  566. }
  567. return constants;
  568. }
  569. QString Project::fixGraphicPath(QString path) {
  570. path = path.replace(QRegExp("\\.lz$"), "");
  571. path = path.replace(QRegExp("\\.[1248]bpp$"), ".png");
  572. return path;
  573. }
  574. void Project::loadObjectPixmaps(QList<Event*> objects) {
  575. bool needs_update = false;
  576. for (Event *object : objects) {
  577. if (object->pixmap.isNull()) {
  578. needs_update = true;
  579. break;
  580. }
  581. }
  582. if (!needs_update) {
  583. return;
  584. }
  585. QMap<QString, int> constants = getMapObjGfxConstants();
  586. QString pointers_text = readTextFile(root + "/src/data/field_map_obj/map_object_graphics_info_pointers.h");
  587. QString info_text = readTextFile(root + "/src/data/field_map_obj/map_object_graphics_info.h");
  588. QString pic_text = readTextFile(root + "/src/data/field_map_obj/map_object_pic_tables.h");
  589. QString assets_text = readTextFile(root + "/src/field/field_map_obj.c");
  590. QStringList pointers = readCArray(pointers_text, "gMapObjectGraphicsInfoPointers");
  591. for (Event *object : objects) {
  592. if (!object->pixmap.isNull()) {
  593. continue;
  594. }
  595. QString event_type = object->get("event_type");
  596. if (event_type == "object") {
  597. object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(0, 0, 16, 16);
  598. } else if (event_type == "warp") {
  599. object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(16, 0, 16, 16);
  600. } else if (event_type == "trap") {
  601. object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(32, 0, 16, 16);
  602. } else if (event_type == "sign" || event_type == "hidden item") {
  603. object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(48, 0, 16, 16);
  604. }
  605. if (event_type == "object") {
  606. int sprite_id = constants.value(object->get("sprite"));
  607. QString info_label = pointers.value(sprite_id).replace("&", "");
  608. QString pic_label = readCArray(info_text, info_label).value(14);
  609. QString gfx_label = readCArray(pic_text, pic_label).value(0);
  610. gfx_label = gfx_label.section(QRegExp("[\\(\\)]"), 1, 1);
  611. QString path = readCIncbin(assets_text, gfx_label);
  612. if (!path.isNull()) {
  613. path = fixGraphicPath(path);
  614. QPixmap pixmap(root + "/" + path);
  615. if (!pixmap.isNull()) {
  616. object->pixmap = pixmap;
  617. }
  618. }
  619. }
  620. }
  621. }
  622. void Project::saveMapEvents(Map *map) {
  623. QString path = root + QString("/data/maps/events/%1.inc").arg(map->name);
  624. QString text = "";
  625. text += QString("%1::\n").arg(map->object_events_label);
  626. for (int i = 0; i < map->events["object"].length(); i++) {
  627. Event *object_event = map->events["object"].value(i);
  628. int radius_x = object_event->getInt("radius_x");
  629. int radius_y = object_event->getInt("radius_y");
  630. QString radius = QString("%1").arg((radius_x & 0xf) + ((radius_y & 0xf) << 4));
  631. uint16_t x = object_event->getInt("x");
  632. uint16_t y = object_event->getInt("y");
  633. text += QString("\tobject_event %1").arg(i + 1);
  634. text += QString(", %1").arg(object_event->get("sprite"));
  635. text += QString(", %1").arg(object_event->get("replacement"));
  636. text += QString(", %1").arg(x & 0xff);
  637. text += QString(", %1").arg((x >> 8) & 0xff);
  638. text += QString(", %1").arg(y & 0xff);
  639. text += QString(", %1").arg((y >> 8) & 0xff);
  640. text += QString(", %1").arg(object_event->get("elevation"));
  641. text += QString(", %1").arg(object_event->get("behavior"));
  642. text += QString(", %1").arg(radius);
  643. text += QString(", 0");
  644. text += QString(", %1").arg(object_event->get("property"));
  645. text += QString(", 0");
  646. text += QString(", %1").arg(object_event->get("sight_radius"));
  647. text += QString(", 0");
  648. text += QString(", %1").arg(object_event->get("script_label"));
  649. text += QString(", %1").arg(object_event->get("event_flag"));
  650. text += QString(", 0");
  651. text += QString(", 0");
  652. text += "\n";
  653. }
  654. text += "\n";
  655. text += QString("%1::\n").arg(map->warps_label);
  656. for (Event *warp : map->events["warp"]) {
  657. text += QString("\twarp_def %1").arg(warp->get("x"));
  658. text += QString(", %1").arg(warp->get("y"));
  659. text += QString(", %1").arg(warp->get("elevation"));
  660. text += QString(", %1").arg(warp->get("destination_warp"));
  661. text += QString(", %1").arg(warp->get("destination_map"));
  662. text += "\n";
  663. }
  664. text += "\n";
  665. text += QString("%1::\n").arg(map->coord_events_label);
  666. for (Event *coords : map->events["trap"]) {
  667. text += QString("\tcoord_event %1").arg(coords->get("x"));
  668. text += QString(", %1").arg(coords->get("y"));
  669. text += QString(", %1").arg(coords->get("elevation"));
  670. text += QString(", 0");
  671. text += QString(", %1").arg(coords->get("coord_unknown1"));
  672. text += QString(", %1").arg(coords->get("coord_unknown2"));
  673. text += QString(", 0");
  674. text += QString(", %1").arg(coords->get("script_label"));
  675. text += "\n";
  676. }
  677. text += "\n";
  678. text += QString("%1::\n").arg(map->bg_events_label);
  679. for (Event *sign : map->events["sign"]) {
  680. text += QString("\tbg_event %1").arg(sign->get("x"));
  681. text += QString(", %1").arg(sign->get("y"));
  682. text += QString(", %1").arg(sign->get("elevation"));
  683. text += QString(", %1").arg(sign->get("type"));
  684. text += QString(", 0");
  685. text += QString(", %1").arg(sign->get("script_label"));
  686. text += "\n";
  687. }
  688. for (Event *item : map->events["hidden item"]) {
  689. text += QString("\tbg_event %1").arg(item->get("x"));
  690. text += QString(", %1").arg(item->get("y"));
  691. text += QString(", %1").arg(item->get("elevation"));
  692. text += QString(", %1").arg(item->get("type"));
  693. text += QString(", 0");
  694. text += QString(", %1").arg(item->get("item"));
  695. text += QString(", %1").arg(item->get("item_unknown5"));
  696. text += QString(", %1").arg(item->get("item_unknown6"));
  697. text += "\n";
  698. }
  699. text += "\n";
  700. text += QString("%1::\n").arg(map->events_label);
  701. text += QString("\tmap_events %1, %2, %3, %4\n")
  702. .arg(map->object_events_label)
  703. .arg(map->warps_label)
  704. .arg(map->coord_events_label)
  705. .arg(map->bg_events_label);
  706. saveTextFile(path, text);
  707. }
  708. void Project::readMapEvents(Map *map) {
  709. // lazy
  710. QString path = root + QString("/data/maps/events/%1.inc").arg(map->name);
  711. QString text = readTextFile(path);
  712. if (text.isNull()) {
  713. return;
  714. }
  715. QStringList *labels = getLabelValues(parse(text), map->events_label);
  716. map->object_events_label = labels->value(0);
  717. map->warps_label = labels->value(1);
  718. map->coord_events_label = labels->value(2);
  719. map->bg_events_label = labels->value(3);
  720. QList<QStringList> *object_events = getLabelMacros(parse(text), map->object_events_label);
  721. map->events["object"].clear();
  722. for (QStringList command : *object_events) {
  723. if (command.value(0) == "object_event") {
  724. Event *object = new Event;
  725. object->put("map_name", map->name);
  726. // This macro is not fixed as of writing, but it should take fewer args.
  727. bool old_macro = false;
  728. if (command.length() >= 20) {
  729. command.removeAt(19);
  730. command.removeAt(18);
  731. command.removeAt(15);
  732. command.removeAt(13);
  733. command.removeAt(11);
  734. command.removeAt(1); // id. not 0, but is just the index in the list of objects
  735. old_macro = true;
  736. }
  737. int i = 1;
  738. object->put("sprite", command.value(i++));
  739. object->put("replacement", command.value(i++));
  740. int16_t x = command.value(i++).toInt(nullptr, 0) | (command.value(i++).toInt(nullptr, 0) << 8);
  741. int16_t y = command.value(i++).toInt(nullptr, 0) | (command.value(i++).toInt(nullptr, 0) << 8);
  742. object->put("x", x);
  743. object->put("y", y);
  744. object->put("elevation", command.value(i++));
  745. object->put("behavior", command.value(i++));
  746. if (old_macro) {
  747. int radius = command.value(i++).toInt(nullptr, 0);
  748. object->put("radius_x", radius & 0xf);
  749. object->put("radius_y", (radius >> 4) & 0xf);
  750. } else {
  751. object->put("radius_x", command.value(i++));
  752. object->put("radius_y", command.value(i++));
  753. }
  754. object->put("property", command.value(i++));
  755. object->put("sight_radius", command.value(i++));
  756. object->put("script_label", command.value(i++));
  757. object->put("event_flag", command.value(i++));
  758. object->put("event_type", "object");
  759. map->events["object"].append(object);
  760. }
  761. }
  762. QList<QStringList> *warps = getLabelMacros(parse(text), map->warps_label);
  763. map->events["warp"].clear();
  764. for (QStringList command : *warps) {
  765. if (command.value(0) == "warp_def") {
  766. Event *warp = new Event;
  767. warp->put("map_name", map->name);
  768. int i = 1;
  769. warp->put("x", command.value(i++));
  770. warp->put("y", command.value(i++));
  771. warp->put("elevation", command.value(i++));
  772. warp->put("destination_warp", command.value(i++));
  773. warp->put("destination_map", command.value(i++));
  774. warp->put("event_type", "warp");
  775. map->events["warp"].append(warp);
  776. }
  777. }
  778. QList<QStringList> *coords = getLabelMacros(parse(text), map->coord_events_label);
  779. map->events["trap"].clear();
  780. for (QStringList command : *coords) {
  781. if (command.value(0) == "coord_event") {
  782. Event *coord = new Event;
  783. coord->put("map_name", map->name);
  784. bool old_macro = false;
  785. if (command.length() >= 9) {
  786. command.removeAt(7);
  787. command.removeAt(4);
  788. old_macro = true;
  789. }
  790. int i = 1;
  791. coord->put("x", command.value(i++));
  792. coord->put("y", command.value(i++));
  793. coord->put("elevation", command.value(i++));
  794. coord->put("coord_unknown1", command.value(i++));
  795. coord->put("coord_unknown2", command.value(i++));
  796. coord->put("script_label", command.value(i++));
  797. //coord_unknown3
  798. //coord_unknown4
  799. coord->put("event_type", "trap");
  800. map->events["trap"].append(coord);
  801. }
  802. }
  803. QList<QStringList> *bgs = getLabelMacros(parse(text), map->bg_events_label);
  804. map->events["hidden item"].clear();
  805. map->events["sign"].clear();
  806. for (QStringList command : *bgs) {
  807. if (command.value(0) == "bg_event") {
  808. Event *bg = new Event;
  809. bg->put("map_name", map->name);
  810. int i = 1;
  811. bg->put("x", command.value(i++));
  812. bg->put("y", command.value(i++));
  813. bg->put("elevation", command.value(i++));
  814. bg->put("type", command.value(i++));
  815. i++;
  816. if (bg->is_hidden_item()) {
  817. bg->put("item", command.value(i++));
  818. bg->put("item_unknown5", command.value(i++));
  819. bg->put("item_unknown6", command.value(i++));
  820. bg->put("event_type", "hidden item");
  821. map->events["hidden item"].append(bg);
  822. } else {
  823. bg->put("script_label", command.value(i++));
  824. //sign_unknown7
  825. bg->put("event_type", "sign");
  826. map->events["sign"].append(bg);
  827. }
  828. }
  829. }
  830. }
  831. QStringList Project::readCArray(QString text, QString label) {
  832. QStringList list;
  833. if (label.isNull()) {
  834. return list;
  835. }
  836. QRegExp *re = new QRegExp(QString("\\b%1\\b\\s*\\[?\\s*\\]?\\s*=\\s*\\{([^\\}]*)\\}").arg(label));
  837. int pos = re->indexIn(text);
  838. if (pos != -1) {
  839. QString body = re->cap(1);
  840. body = body.replace(QRegExp("\\s*"), "");
  841. list = body.split(',');
  842. /*
  843. QRegExp *inner = new QRegExp("&?\\b([A-Za-z0-9_\\(\\)]*)\\b,");
  844. int pos = 0;
  845. while ((pos = inner->indexIn(body, pos)) != -1) {
  846. list << inner->cap(1);
  847. pos += inner->matchedLength();
  848. }
  849. */
  850. }
  851. return list;
  852. }
  853. QString Project::readCIncbin(QString text, QString label) {
  854. QString path;
  855. if (label.isNull()) {
  856. return path;
  857. }
  858. QRegExp *re = new QRegExp(QString(
  859. "\\b%1\\b"
  860. "\\s*\\[?\\s*\\]?\\s*=\\s*"
  861. "INCBIN_[US][0-9][0-9]?"
  862. "\\(\"([^\"]*)\"\\)").arg(label));
  863. int pos = re->indexIn(text);
  864. if (pos != -1) {
  865. path = re->cap(1);
  866. }
  867. return path;
  868. }
  869. QMap<QString, int> Project::readCDefines(QString text, QStringList prefixes) {
  870. QMap<QString, int> defines;
  871. QString combinedPrefixes = "[" + prefixes.join('|') + "]";
  872. QRegularExpression re(QString("#define\\s+(?<defineName>%1\\w+)\\s(?<defineValue>\\w+)").arg(combinedPrefixes));
  873. QRegularExpressionMatchIterator iter = re.globalMatch(text);
  874. while (iter.hasNext()) {
  875. QRegularExpressionMatch match = iter.next();
  876. QString name = match.captured("defineName");
  877. QString value = match.captured("defineValue");
  878. bool valid;
  879. int parsedValue = value.startsWith("0x") ? value.toInt(&valid, 16) : value.toInt(&valid, 10);
  880. if (valid) {
  881. if (!defines.contains(name)) {
  882. defines.insert(name, parsedValue);
  883. } else {
  884. qDebug() << QString("Define '%1' is defined multiple times'").arg(name);
  885. }
  886. } else {
  887. qDebug() << QString("Failed to parse define '%1' value '%2' as base 10 or hexadecimal value").arg(name, value);
  888. }
  889. }
  890. return defines;
  891. }