Nav apraksta

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