Aucune description

project.cpp 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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 + "/constants/songs.inc");
  525. if (!text.isNull()) {
  526. QList<QStringList> *commands = parse(text);
  527. for (int i = 0; i < commands->length(); i++) {
  528. QStringList params = commands->value(i);
  529. QString macro = params.value(0);
  530. if (macro == ".equiv") {
  531. names.append(params.value(1));
  532. }
  533. }
  534. }
  535. return names;
  536. }
  537. QString Project::getSongName(int value) {
  538. QStringList names;
  539. QString text = readTextFile(root + "/constants/songs.inc");
  540. if (!text.isNull()) {
  541. QList<QStringList> *commands = parse(text);
  542. for (int i = 0; i < commands->length(); i++) {
  543. QStringList params = commands->value(i);
  544. QString macro = params.value(0);
  545. if (macro == ".equiv") {
  546. if (value == ((QString)(params.value(2))).toInt(nullptr, 0)) {
  547. return params.value(1);
  548. }
  549. }
  550. }
  551. }
  552. return "";
  553. }
  554. QMap<QString, int> Project::getMapObjGfxConstants() {
  555. QMap<QString, int> constants;
  556. QString text = readTextFile(root + "/constants/map_object_constants.inc");
  557. if (!text.isNull()) {
  558. QList<QStringList> *commands = parse(text);
  559. for (int i = 0; i < commands->length(); i++) {
  560. QStringList params = commands->value(i);
  561. QString macro = params.value(0);
  562. if (macro == ".set") {
  563. QString constant = params.value(1);
  564. if (constant.startsWith("MAP_OBJ_GFX_")) {
  565. int value = params.value(2).toInt(nullptr, 0);
  566. constants.insert(constant, value);
  567. }
  568. }
  569. }
  570. }
  571. return constants;
  572. }
  573. QString Project::fixGraphicPath(QString path) {
  574. path = path.replace(QRegExp("\\.lz$"), "");
  575. path = path.replace(QRegExp("\\.[1248]bpp$"), ".png");
  576. return path;
  577. }
  578. void Project::loadObjectPixmaps(QList<Event*> objects) {
  579. bool needs_update = false;
  580. for (Event *object : objects) {
  581. if (object->pixmap.isNull()) {
  582. needs_update = true;
  583. break;
  584. }
  585. }
  586. if (!needs_update) {
  587. return;
  588. }
  589. QMap<QString, int> constants = getMapObjGfxConstants();
  590. QString pointers_text = readTextFile(root + "/include/data/field_map_obj/map_object_graphics_info_pointers.h");
  591. QString info_text = readTextFile(root + "/include/data/field_map_obj/map_object_graphics_info.h");
  592. QString pic_text = readTextFile(root + "/include/data/field_map_obj/map_object_pic_tables.h");
  593. QString assets_text = readTextFile(root + "/src/field_map_obj.c");
  594. QStringList pointers = readCArray(pointers_text, "gMapObjectGraphicsInfoPointers");
  595. for (Event *object : objects) {
  596. if (!object->pixmap.isNull()) {
  597. continue;
  598. }
  599. QString event_type = object->get("event_type");
  600. if (event_type == "object") {
  601. object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(0, 0, 16, 16);
  602. } else if (event_type == "warp") {
  603. object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(16, 0, 16, 16);
  604. } else if (event_type == "trap") {
  605. object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(32, 0, 16, 16);
  606. } else if (event_type == "sign" || event_type == "hidden item") {
  607. object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(48, 0, 16, 16);
  608. }
  609. if (event_type == "object") {
  610. int sprite_id = constants.value(object->get("sprite"));
  611. QString info_label = pointers.value(sprite_id).replace("&", "");
  612. QString pic_label = readCArray(info_text, info_label).value(14);
  613. QString gfx_label = readCArray(pic_text, pic_label).value(0);
  614. gfx_label = gfx_label.section(QRegExp("[\\(\\)]"), 1, 1);
  615. QString path = readCIncbin(assets_text, gfx_label);
  616. if (!path.isNull()) {
  617. path = fixGraphicPath(path);
  618. QPixmap pixmap(root + "/" + path);
  619. if (!pixmap.isNull()) {
  620. object->pixmap = pixmap;
  621. }
  622. }
  623. }
  624. }
  625. }
  626. void Project::saveMapEvents(Map *map) {
  627. QString path = root + QString("/data/maps/events/%1.inc").arg(map->name);
  628. QString text = "";
  629. text += QString("%1::\n").arg(map->object_events_label);
  630. for (int i = 0; i < map->events["object"].length(); i++) {
  631. Event *object_event = map->events["object"].value(i);
  632. int radius_x = object_event->getInt("radius_x");
  633. int radius_y = object_event->getInt("radius_y");
  634. QString radius = QString("%1").arg((radius_x & 0xf) + ((radius_y & 0xf) << 4));
  635. uint16_t x = object_event->getInt("x");
  636. uint16_t y = object_event->getInt("y");
  637. text += QString("\tobject_event %1").arg(i + 1);
  638. text += QString(", %1").arg(object_event->get("sprite"));
  639. text += QString(", %1").arg(object_event->get("replacement"));
  640. text += QString(", %1").arg(x & 0xff);
  641. text += QString(", %1").arg((x >> 8) & 0xff);
  642. text += QString(", %1").arg(y & 0xff);
  643. text += QString(", %1").arg((y >> 8) & 0xff);
  644. text += QString(", %1").arg(object_event->get("elevation"));
  645. text += QString(", %1").arg(object_event->get("behavior"));
  646. text += QString(", %1").arg(radius);
  647. text += QString(", 0");
  648. text += QString(", %1").arg(object_event->get("property"));
  649. text += QString(", 0");
  650. text += QString(", %1").arg(object_event->get("sight_radius"));
  651. text += QString(", 0");
  652. text += QString(", %1").arg(object_event->get("script_label"));
  653. text += QString(", %1").arg(object_event->get("event_flag"));
  654. text += QString(", 0");
  655. text += QString(", 0");
  656. text += "\n";
  657. }
  658. text += "\n";
  659. text += QString("%1::\n").arg(map->warps_label);
  660. for (Event *warp : map->events["warp"]) {
  661. text += QString("\twarp_def %1").arg(warp->get("x"));
  662. text += QString(", %1").arg(warp->get("y"));
  663. text += QString(", %1").arg(warp->get("elevation"));
  664. text += QString(", %1").arg(warp->get("destination_warp"));
  665. text += QString(", %1").arg(warp->get("destination_map"));
  666. text += "\n";
  667. }
  668. text += "\n";
  669. text += QString("%1::\n").arg(map->coord_events_label);
  670. for (Event *coords : map->events["trap"]) {
  671. text += QString("\tcoord_event %1").arg(coords->get("x"));
  672. text += QString(", %1").arg(coords->get("y"));
  673. text += QString(", %1").arg(coords->get("elevation"));
  674. text += QString(", 0");
  675. text += QString(", %1").arg(coords->get("coord_unknown1"));
  676. text += QString(", %1").arg(coords->get("coord_unknown2"));
  677. text += QString(", 0");
  678. text += QString(", %1").arg(coords->get("script_label"));
  679. text += "\n";
  680. }
  681. text += "\n";
  682. text += QString("%1::\n").arg(map->bg_events_label);
  683. for (Event *sign : map->events["sign"]) {
  684. text += QString("\tbg_event %1").arg(sign->get("x"));
  685. text += QString(", %1").arg(sign->get("y"));
  686. text += QString(", %1").arg(sign->get("elevation"));
  687. text += QString(", %1").arg(sign->get("type"));
  688. text += QString(", 0");
  689. text += QString(", %1").arg(sign->get("script_label"));
  690. text += "\n";
  691. }
  692. for (Event *item : map->events["hidden item"]) {
  693. text += QString("\tbg_event %1").arg(item->get("x"));
  694. text += QString(", %1").arg(item->get("y"));
  695. text += QString(", %1").arg(item->get("elevation"));
  696. text += QString(", %1").arg(item->get("type"));
  697. text += QString(", 0");
  698. text += QString(", %1").arg(item->get("item"));
  699. text += QString(", %1").arg(item->get("item_unknown5"));
  700. text += QString(", %1").arg(item->get("item_unknown6"));
  701. text += "\n";
  702. }
  703. text += "\n";
  704. text += QString("%1::\n").arg(map->events_label);
  705. text += QString("\tmap_events %1, %2, %3, %4\n")
  706. .arg(map->object_events_label)
  707. .arg(map->warps_label)
  708. .arg(map->coord_events_label)
  709. .arg(map->bg_events_label);
  710. saveTextFile(path, text);
  711. }
  712. void Project::readMapEvents(Map *map) {
  713. // lazy
  714. QString path = root + QString("/data/maps/events/%1.inc").arg(map->name);
  715. QString text = readTextFile(path);
  716. if (text.isNull()) {
  717. return;
  718. }
  719. QStringList *labels = getLabelValues(parse(text), map->events_label);
  720. map->object_events_label = labels->value(0);
  721. map->warps_label = labels->value(1);
  722. map->coord_events_label = labels->value(2);
  723. map->bg_events_label = labels->value(3);
  724. QList<QStringList> *object_events = getLabelMacros(parse(text), map->object_events_label);
  725. map->events["object"].clear();
  726. for (QStringList command : *object_events) {
  727. if (command.value(0) == "object_event") {
  728. Event *object = new Event;
  729. object->put("map_name", map->name);
  730. // This macro is not fixed as of writing, but it should take fewer args.
  731. bool old_macro = false;
  732. if (command.length() >= 20) {
  733. command.removeAt(19);
  734. command.removeAt(18);
  735. command.removeAt(15);
  736. command.removeAt(13);
  737. command.removeAt(11);
  738. command.removeAt(1); // id. not 0, but is just the index in the list of objects
  739. old_macro = true;
  740. }
  741. int i = 1;
  742. object->put("sprite", command.value(i++));
  743. object->put("replacement", command.value(i++));
  744. int16_t x = command.value(i++).toInt(nullptr, 0) | (command.value(i++).toInt(nullptr, 0) << 8);
  745. int16_t y = command.value(i++).toInt(nullptr, 0) | (command.value(i++).toInt(nullptr, 0) << 8);
  746. object->put("x", x);
  747. object->put("y", y);
  748. object->put("elevation", command.value(i++));
  749. object->put("behavior", command.value(i++));
  750. if (old_macro) {
  751. int radius = command.value(i++).toInt(nullptr, 0);
  752. object->put("radius_x", radius & 0xf);
  753. object->put("radius_y", (radius >> 4) & 0xf);
  754. } else {
  755. object->put("radius_x", command.value(i++));
  756. object->put("radius_y", command.value(i++));
  757. }
  758. object->put("property", command.value(i++));
  759. object->put("sight_radius", command.value(i++));
  760. object->put("script_label", command.value(i++));
  761. object->put("event_flag", command.value(i++));
  762. object->put("event_type", "object");
  763. map->events["object"].append(object);
  764. }
  765. }
  766. QList<QStringList> *warps = getLabelMacros(parse(text), map->warps_label);
  767. map->events["warp"].clear();
  768. for (QStringList command : *warps) {
  769. if (command.value(0) == "warp_def") {
  770. Event *warp = new Event;
  771. warp->put("map_name", map->name);
  772. int i = 1;
  773. warp->put("x", command.value(i++));
  774. warp->put("y", command.value(i++));
  775. warp->put("elevation", command.value(i++));
  776. warp->put("destination_warp", command.value(i++));
  777. warp->put("destination_map", command.value(i++));
  778. warp->put("event_type", "warp");
  779. map->events["warp"].append(warp);
  780. }
  781. }
  782. QList<QStringList> *coords = getLabelMacros(parse(text), map->coord_events_label);
  783. map->events["trap"].clear();
  784. for (QStringList command : *coords) {
  785. if (command.value(0) == "coord_event") {
  786. Event *coord = new Event;
  787. coord->put("map_name", map->name);
  788. bool old_macro = false;
  789. if (command.length() >= 9) {
  790. command.removeAt(7);
  791. command.removeAt(4);
  792. old_macro = true;
  793. }
  794. int i = 1;
  795. coord->put("x", command.value(i++));
  796. coord->put("y", command.value(i++));
  797. coord->put("elevation", command.value(i++));
  798. coord->put("coord_unknown1", command.value(i++));
  799. coord->put("coord_unknown2", command.value(i++));
  800. coord->put("script_label", command.value(i++));
  801. //coord_unknown3
  802. //coord_unknown4
  803. coord->put("event_type", "trap");
  804. map->events["trap"].append(coord);
  805. }
  806. }
  807. QList<QStringList> *bgs = getLabelMacros(parse(text), map->bg_events_label);
  808. map->events["hidden item"].clear();
  809. map->events["sign"].clear();
  810. for (QStringList command : *bgs) {
  811. if (command.value(0) == "bg_event") {
  812. Event *bg = new Event;
  813. bg->put("map_name", map->name);
  814. int i = 1;
  815. bg->put("x", command.value(i++));
  816. bg->put("y", command.value(i++));
  817. bg->put("elevation", command.value(i++));
  818. bg->put("type", command.value(i++));
  819. i++;
  820. if (bg->is_hidden_item()) {
  821. bg->put("item", command.value(i++));
  822. bg->put("item_unknown5", command.value(i++));
  823. bg->put("item_unknown6", command.value(i++));
  824. bg->put("event_type", "hidden item");
  825. map->events["hidden item"].append(bg);
  826. } else {
  827. bg->put("script_label", command.value(i++));
  828. //sign_unknown7
  829. bg->put("event_type", "sign");
  830. map->events["sign"].append(bg);
  831. }
  832. }
  833. }
  834. }
  835. QStringList Project::readCArray(QString text, QString label) {
  836. QStringList list;
  837. if (label.isNull()) {
  838. return list;
  839. }
  840. QRegExp *re = new QRegExp(QString("\\b%1\\b\\s*\\[?\\s*\\]?\\s*=\\s*\\{([^\\}]*)\\}").arg(label));
  841. int pos = re->indexIn(text);
  842. if (pos != -1) {
  843. QString body = re->cap(1);
  844. body = body.replace(QRegExp("\\s*"), "");
  845. list = body.split(',');
  846. /*
  847. QRegExp *inner = new QRegExp("&?\\b([A-Za-z0-9_\\(\\)]*)\\b,");
  848. int pos = 0;
  849. while ((pos = inner->indexIn(body, pos)) != -1) {
  850. list << inner->cap(1);
  851. pos += inner->matchedLength();
  852. }
  853. */
  854. }
  855. return list;
  856. }
  857. QString Project::readCIncbin(QString text, QString label) {
  858. QString path;
  859. if (label.isNull()) {
  860. return path;
  861. }
  862. QRegExp *re = new QRegExp(QString(
  863. "\\b%1\\b"
  864. "\\s*\\[?\\s*\\]?\\s*=\\s*"
  865. "INCBIN_[US][0-9][0-9]?"
  866. "\\(\"([^\"]*)\"\\)").arg(label));
  867. int pos = re->indexIn(text);
  868. if (pos != -1) {
  869. path = re->cap(1);
  870. }
  871. return path;
  872. }