No Description

project.cpp 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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. map_cache = new QMap<QString, Map*>;
  17. tileset_cache = new QMap<QString, Tileset*>;
  18. }
  19. QString Project::getProjectTitle() {
  20. return root.section('/', -1);
  21. }
  22. Map* Project::loadMap(QString map_name) {
  23. Map *map = new Map;
  24. map->name = map_name;
  25. readMapHeader(map);
  26. readMapAttributes(map);
  27. getTilesets(map);
  28. loadBlockdata(map);
  29. loadMapBorder(map);
  30. readMapEvents(map);
  31. loadMapConnections(map);
  32. map->commit();
  33. map_cache->insert(map_name, map);
  34. return map;
  35. }
  36. void Project::loadMapConnections(Map *map) {
  37. map->connections.clear();
  38. if (!map->connections_label.isNull()) {
  39. QString path = root + QString("/data/maps/%1/connections.s").arg(map->name);
  40. QString text = readTextFile(path);
  41. if (text != NULL) {
  42. QList<QStringList> *commands = parse(text);
  43. QStringList *list = getLabelValues(commands, map->connections_label);
  44. //// Avoid using this value. It ought to be generated instead.
  45. //int num_connections = list->value(0).toInt(nullptr, 0);
  46. QString connections_list_label = list->value(1);
  47. QList<QStringList> *connections = getLabelMacros(commands, connections_list_label);
  48. for (QStringList command : *connections) {
  49. QString macro = command.value(0);
  50. if (macro == "connection") {
  51. Connection *connection = new Connection;
  52. connection->direction = command.value(1);
  53. connection->offset = command.value(2);
  54. connection->map_name = command.value(3);
  55. map->connections.append(connection);
  56. }
  57. }
  58. }
  59. }
  60. }
  61. QList<QStringList>* Project::getLabelMacros(QList<QStringList> *list, QString label) {
  62. bool in_label = false;
  63. QList<QStringList> *new_list = new QList<QStringList>;
  64. for (int i = 0; i < list->length(); i++) {
  65. QStringList params = list->value(i);
  66. QString macro = params.value(0);
  67. if (macro == ".label") {
  68. if (params.value(1) == label) {
  69. in_label = true;
  70. } else if (in_label) {
  71. // If nothing has been read yet, assume the label
  72. // we're looking for is in a stack of labels.
  73. if (new_list->length() > 0) {
  74. break;
  75. }
  76. }
  77. } else if (in_label) {
  78. new_list->append(params);
  79. }
  80. }
  81. return new_list;
  82. }
  83. // For if you don't care about filtering by macro,
  84. // and just want all values associated with some label.
  85. QStringList* Project::getLabelValues(QList<QStringList> *list, QString label) {
  86. list = getLabelMacros(list, label);
  87. QStringList *values = new QStringList;
  88. for (int i = 0; i < list->length(); i++) {
  89. QStringList params = list->value(i);
  90. QString macro = params.value(0);
  91. // Ignore .align
  92. if (macro == ".align") {
  93. continue;
  94. }
  95. for (int j = 1; j < params.length(); j++) {
  96. values->append(params.value(j));
  97. }
  98. }
  99. return values;
  100. }
  101. void Project::readMapHeader(Map* map) {
  102. QString label = map->name;
  103. Asm *parser = new Asm;
  104. QString header_text = readTextFile(root + "/data/maps/" + label + "/header.s");
  105. QStringList *header = getLabelValues(parser->parse(header_text), label);
  106. map->attributes_label = header->value(0);
  107. map->events_label = header->value(1);
  108. map->scripts_label = header->value(2);
  109. map->connections_label = header->value(3);
  110. map->song = header->value(4);
  111. map->index = header->value(5);
  112. map->location = header->value(6);
  113. map->visibility = header->value(7);
  114. map->weather = header->value(8);
  115. map->type = header->value(9);
  116. map->unknown = header->value(10);
  117. map->show_location = header->value(11);
  118. map->battle_scene = header->value(12);
  119. }
  120. void Project::saveMapHeader(Map *map) {
  121. QString label = map->name;
  122. QString header_path = root + "/data/maps/" + label + "/header.s";
  123. QString text = "";
  124. text += QString("%1::\n").arg(label);
  125. text += QString("\t.4byte %1\n").arg(map->attributes_label);
  126. text += QString("\t.4byte %1\n").arg(map->events_label);
  127. text += QString("\t.4byte %1\n").arg(map->scripts_label);
  128. text += QString("\t.4byte %1\n").arg(map->connections_label);
  129. text += QString("\t.2byte %1\n").arg(map->song);
  130. text += QString("\t.2byte %1\n").arg(map->index);
  131. text += QString("\t.byte %1\n").arg(map->location);
  132. text += QString("\t.byte %1\n").arg(map->visibility);
  133. text += QString("\t.byte %1\n").arg(map->weather);
  134. text += QString("\t.byte %1\n").arg(map->type);
  135. text += QString("\t.2byte %1\n").arg(map->unknown);
  136. text += QString("\t.byte %1\n").arg(map->show_location);
  137. text += QString("\t.byte %1\n").arg(map->battle_scene);
  138. saveTextFile(header_path, text);
  139. }
  140. void Project::readMapAttributes(Map* map) {
  141. Asm *parser = new Asm;
  142. QString assets_text = readTextFile(root + "/data/maps/_assets.s");
  143. QStringList *attributes = getLabelValues(parser->parse(assets_text), map->attributes_label);
  144. map->width = attributes->value(0);
  145. map->height = attributes->value(1);
  146. map->border_label = attributes->value(2);
  147. map->blockdata_label = attributes->value(3);
  148. map->tileset_primary_label = attributes->value(4);
  149. map->tileset_secondary_label = attributes->value(5);
  150. }
  151. void Project::getTilesets(Map* map) {
  152. map->tileset_primary = getTileset(map->tileset_primary_label);
  153. map->tileset_secondary = getTileset(map->tileset_secondary_label);
  154. }
  155. Tileset* Project::loadTileset(QString label) {
  156. Asm *parser = new Asm;
  157. QString headers_text = readTextFile(root + "/data/tilesets/headers.s");
  158. QStringList *values = getLabelValues(parser->parse(headers_text), label);
  159. Tileset *tileset = new Tileset;
  160. tileset->name = label;
  161. tileset->is_compressed = values->value(0);
  162. tileset->is_secondary = values->value(1);
  163. tileset->padding = values->value(2);
  164. tileset->tiles_label = values->value(3);
  165. tileset->palettes_label = values->value(4);
  166. tileset->metatiles_label = values->value(5);
  167. tileset->metatile_attrs_label = values->value(6);
  168. tileset->callback_label = values->value(7);
  169. loadTilesetAssets(tileset);
  170. tileset_cache->insert(label, tileset);
  171. return tileset;
  172. }
  173. QString Project::getBlockdataPath(Map* map) {
  174. QString text = readTextFile(root + "/data/maps/_assets.s");
  175. QStringList *values = getLabelValues(parse(text), map->blockdata_label);
  176. QString path;
  177. if (!values->isEmpty()) {
  178. path = root + "/" + values->value(0).section('"', 1, 1);
  179. } else {
  180. path = root + "/data/maps/" + map->name + "/map.bin";
  181. }
  182. return path;
  183. }
  184. QString Project::getMapBorderPath(Map *map) {
  185. QString text = readTextFile(root + "/data/maps/_assets.s");
  186. QStringList *values = getLabelValues(parse(text), map->border_label);
  187. QString path;
  188. if (!values->isEmpty()) {
  189. path = root + "/" + values->value(0).section('"', 1, 1);
  190. } else {
  191. path = root + "/data/maps/" + map->name + "/border.bin";
  192. }
  193. return path;
  194. }
  195. void Project::loadBlockdata(Map* map) {
  196. QString path = getBlockdataPath(map);
  197. map->blockdata = readBlockdata(path);
  198. }
  199. void Project::loadMapBorder(Map *map) {
  200. QString path = getMapBorderPath(map);
  201. map->border = readBlockdata(path);
  202. }
  203. void Project::saveBlockdata(Map* map) {
  204. QString path = getBlockdataPath(map);
  205. writeBlockdata(path, map->blockdata);
  206. }
  207. void Project::writeBlockdata(QString path, Blockdata *blockdata) {
  208. QFile file(path);
  209. if (file.open(QIODevice::WriteOnly)) {
  210. QByteArray data = blockdata->serialize();
  211. file.write(data);
  212. }
  213. }
  214. void Project::saveAllMaps() {
  215. QList<QString> keys = map_cache->keys();
  216. for (int i = 0; i < keys.length(); i++) {
  217. QString key = keys.value(i);
  218. Map* map = map_cache->value(key);
  219. saveMap(map);
  220. }
  221. }
  222. void Project::saveMap(Map *map) {
  223. saveBlockdata(map);
  224. saveMapHeader(map);
  225. }
  226. void Project::loadTilesetAssets(Tileset* tileset) {
  227. Asm* parser = new Asm;
  228. QString category = (tileset->is_secondary == "TRUE") ? "secondary" : "primary";
  229. QString dir_path = root + "/data/tilesets/" + category + "/" + tileset->name.replace("gTileset_", "").toLower();
  230. QString graphics_text = readTextFile(root + "/data/tilesets/graphics.s");
  231. QList<QStringList> *graphics = parser->parse(graphics_text);
  232. QStringList *tiles_values = getLabelValues(graphics, tileset->tiles_label);
  233. QStringList *palettes_values = getLabelValues(graphics, tileset->palettes_label);
  234. QString tiles_path;
  235. if (!tiles_values->isEmpty()) {
  236. tiles_path = root + "/" + tiles_values->value(0).section('"', 1, 1);
  237. } else {
  238. tiles_path = dir_path + "/tiles.4bpp";
  239. if (tileset->is_compressed == "TRUE") {
  240. tiles_path += ".lz";
  241. }
  242. }
  243. QStringList *palette_paths = new QStringList;
  244. if (!palettes_values->isEmpty()) {
  245. for (int i = 0; i < palettes_values->length(); i++) {
  246. QString value = palettes_values->value(i);
  247. palette_paths->append(root + "/" + value.section('"', 1, 1));
  248. }
  249. } else {
  250. QString palettes_dir_path = dir_path + "/palettes";
  251. for (int i = 0; i < 16; i++) {
  252. palette_paths->append(palettes_dir_path + "/" + QString("%1").arg(i, 2, 10, QLatin1Char('0')) + ".gbapal");
  253. }
  254. }
  255. QString metatiles_path;
  256. QString metatile_attrs_path;
  257. QString metatiles_text = readTextFile(root + "/data/tilesets/metatiles.s");
  258. QList<QStringList> *metatiles_macros = parser->parse(metatiles_text);
  259. QStringList *metatiles_values = getLabelValues(metatiles_macros, tileset->metatiles_label);
  260. if (!metatiles_values->isEmpty()) {
  261. metatiles_path = root + "/" + metatiles_values->value(0).section('"', 1, 1);
  262. } else {
  263. metatiles_path = dir_path + "/metatiles.bin";
  264. }
  265. QStringList *metatile_attrs_values = getLabelValues(metatiles_macros, tileset->metatile_attrs_label);
  266. if (!metatile_attrs_values->isEmpty()) {
  267. metatile_attrs_path = root + "/" + metatile_attrs_values->value(0).section('"', 1, 1);
  268. } else {
  269. metatile_attrs_path = dir_path + "/metatile_attributes.bin";
  270. }
  271. // tiles
  272. tiles_path = fixGraphicPath(tiles_path);
  273. QImage *image = new QImage(tiles_path);
  274. //image->setColor(0, qRgb(0xff, 0, 0)); // debug
  275. QList<QImage> *tiles = new QList<QImage>;
  276. int w = 8;
  277. int h = 8;
  278. for (int y = 0; y < image->height(); y += h)
  279. for (int x = 0; x < image->width(); x += w) {
  280. QImage tile = image->copy(x, y, w, h);
  281. tiles->append(tile);
  282. }
  283. tileset->tiles = tiles;
  284. // metatiles
  285. //qDebug() << metatiles_path;
  286. QFile metatiles_file(metatiles_path);
  287. if (metatiles_file.open(QIODevice::ReadOnly)) {
  288. QByteArray data = metatiles_file.readAll();
  289. int num_metatiles = data.length() / 16;
  290. int num_layers = 2;
  291. QList<Metatile*> *metatiles = new QList<Metatile*>;
  292. for (int i = 0; i < num_metatiles; i++) {
  293. Metatile *metatile = new Metatile;
  294. int index = i * 16;
  295. for (int j = 0; j < 4 * num_layers; j++) {
  296. uint16_t word = data[index++] & 0xff;
  297. word += (data[index++] & 0xff) << 8;
  298. Tile tile;
  299. tile.tile = word & 0x3ff;
  300. tile.xflip = (word >> 10) & 1;
  301. tile.yflip = (word >> 11) & 1;
  302. tile.palette = (word >> 12) & 0xf;
  303. metatile->tiles->append(tile);
  304. }
  305. metatiles->append(metatile);
  306. }
  307. tileset->metatiles = metatiles;
  308. }
  309. QFile attrs_file(metatile_attrs_path);
  310. //qDebug() << metatile_attrs_path;
  311. if (attrs_file.open(QIODevice::ReadOnly)) {
  312. QByteArray data = attrs_file.readAll();
  313. int num_metatiles = data.length() / 2;
  314. for (int i = 0; i < num_metatiles; i++) {
  315. uint16_t word = data[i*2] & 0xff;
  316. word += (data[i*2 + 1] & 0xff) << 8;
  317. tileset->metatiles->value(i)->attr = word;
  318. }
  319. }
  320. // palettes
  321. QList<QList<QRgb>> *palettes = new QList<QList<QRgb>>;
  322. for (int i = 0; i < palette_paths->length(); i++) {
  323. QString path = palette_paths->value(i);
  324. // the palettes are not compressed. this should never happen. it's only a precaution.
  325. path = path.replace(QRegExp("\\.lz$"), "");
  326. // TODO default to .pal (JASC-PAL)
  327. // just use .gbapal for now
  328. QFile file(path);
  329. QList<QRgb> palette;
  330. if (file.open(QIODevice::ReadOnly)) {
  331. QByteArray data = file.readAll();
  332. for (int j = 0; j < 16; j++) {
  333. uint16_t word = data[j*2] & 0xff;
  334. word += (data[j*2 + 1] & 0xff) << 8;
  335. int red = word & 0x1f;
  336. int green = (word >> 5) & 0x1f;
  337. int blue = (word >> 10) & 0x1f;
  338. QRgb color = qRgb(red * 8, green * 8, blue * 8);
  339. palette.prepend(color);
  340. }
  341. }
  342. //qDebug() << path;
  343. palettes->append(palette);
  344. }
  345. tileset->palettes = palettes;
  346. }
  347. Blockdata* Project::readBlockdata(QString path) {
  348. Blockdata *blockdata = new Blockdata;
  349. //qDebug() << path;
  350. QFile file(path);
  351. if (file.open(QIODevice::ReadOnly)) {
  352. QByteArray data = file.readAll();
  353. for (int i = 0; (i + 1) < data.length(); i += 2) {
  354. uint16_t word = (data[i] & 0xff) + ((data[i + 1] & 0xff) << 8);
  355. blockdata->addBlock(word);
  356. }
  357. }
  358. return blockdata;
  359. }
  360. Map* Project::getMap(QString map_name) {
  361. if (map_cache->contains(map_name)) {
  362. return map_cache->value(map_name);
  363. } else {
  364. Map *map = loadMap(map_name);
  365. return map;
  366. }
  367. }
  368. Tileset* Project::getTileset(QString label) {
  369. if (tileset_cache->contains(label)) {
  370. return tileset_cache->value(label);
  371. } else {
  372. Tileset *tileset = loadTileset(label);
  373. return tileset;
  374. }
  375. }
  376. QString Project::readTextFile(QString path) {
  377. QFile file(path);
  378. if (!file.open(QIODevice::ReadOnly)) {
  379. //QMessageBox::information(0, "Error", QString("Could not open '%1': ").arg(path) + file.errorString());
  380. return NULL;
  381. }
  382. QTextStream in(&file);
  383. QString text = "";
  384. while (!in.atEnd()) {
  385. text += in.readLine() + "\n";
  386. }
  387. return text;
  388. }
  389. void Project::saveTextFile(QString path, QString text) {
  390. QFile file(path);
  391. if (file.open(QIODevice::WriteOnly)) {
  392. file.write(text.toUtf8());
  393. }
  394. }
  395. void Project::readMapGroups() {
  396. QString text = readTextFile(root + "/data/maps/_groups.s");
  397. if (text == NULL) {
  398. return;
  399. }
  400. Asm *parser = new Asm;
  401. QList<QStringList> *commands = parser->parse(text);
  402. bool in_group_pointers = false;
  403. QStringList *groups = new QStringList;
  404. for (int i = 0; i < commands->length(); i++) {
  405. QStringList params = commands->value(i);
  406. QString macro = params.value(0);
  407. if (macro == ".label") {
  408. if (in_group_pointers) {
  409. break;
  410. }
  411. if (params.value(1) == "gMapGroups") {
  412. in_group_pointers = true;
  413. }
  414. } else if (macro == ".4byte") {
  415. if (in_group_pointers) {
  416. for (int j = 1; j < params.length(); j++) {
  417. groups->append(params.value(j));
  418. }
  419. }
  420. }
  421. }
  422. QList<QStringList*> *maps = new QList<QStringList*>;
  423. for (int i = 0; i < groups->length(); i++) {
  424. QStringList *list = new QStringList;
  425. maps->append(list);
  426. }
  427. int group = -1;
  428. for (int i = 0; i < commands->length(); i++) {
  429. QStringList params = commands->value(i);
  430. QString macro = params.value(0);
  431. if (macro == ".label") {
  432. group = groups->indexOf(params.value(1));
  433. } else if (macro == ".4byte") {
  434. if (group != -1) {
  435. for (int j = 1; j < params.length(); j++) {
  436. QStringList *list = maps->value(group);
  437. list->append(params.value(j));
  438. }
  439. }
  440. }
  441. }
  442. groupNames = groups;
  443. groupedMapNames = maps;
  444. }
  445. QList<QStringList>* Project::parse(QString text) {
  446. Asm *parser = new Asm;
  447. return parser->parse(text);
  448. }
  449. QStringList Project::getLocations() {
  450. // TODO
  451. QStringList names;
  452. for (int i = 0; i < 88; i++) {
  453. names.append(QString("%1").arg(i));
  454. }
  455. return names;
  456. }
  457. QStringList Project::getVisibilities() {
  458. // TODO
  459. QStringList names;
  460. for (int i = 0; i < 16; i++) {
  461. names.append(QString("%1").arg(i));
  462. }
  463. return names;
  464. }
  465. QStringList Project::getWeathers() {
  466. // TODO
  467. QStringList names;
  468. for (int i = 0; i < 16; i++) {
  469. names.append(QString("%1").arg(i));
  470. }
  471. return names;
  472. }
  473. QStringList Project::getMapTypes() {
  474. // TODO
  475. QStringList names;
  476. for (int i = 0; i < 16; i++) {
  477. names.append(QString("%1").arg(i));
  478. }
  479. return names;
  480. }
  481. QStringList Project::getBattleScenes() {
  482. // TODO
  483. QStringList names;
  484. for (int i = 0; i < 16; i++) {
  485. names.append(QString("%1").arg(i));
  486. }
  487. return names;
  488. }
  489. QStringList Project::getSongNames() {
  490. QStringList names;
  491. QString text = readTextFile(root + "/constants/songs.s");
  492. if (text != NULL) {
  493. QList<QStringList> *commands = parse(text);
  494. for (int i = 0; i < commands->length(); i++) {
  495. QStringList params = commands->value(i);
  496. QString macro = params.value(0);
  497. if (macro == ".equiv") {
  498. names.append(params.value(1));
  499. }
  500. }
  501. }
  502. return names;
  503. }
  504. QString Project::getSongName(int value) {
  505. QStringList names;
  506. QString text = readTextFile(root + "/constants/songs.s");
  507. if (text != NULL) {
  508. QList<QStringList> *commands = parse(text);
  509. for (int i = 0; i < commands->length(); i++) {
  510. QStringList params = commands->value(i);
  511. QString macro = params.value(0);
  512. if (macro == ".equiv") {
  513. if (value == ((QString)(params.value(2))).toInt(nullptr, 0)) {
  514. return params.value(1);
  515. }
  516. }
  517. }
  518. }
  519. return "";
  520. }
  521. QMap<QString, int> Project::getMapObjGfxConstants() {
  522. QMap<QString, int> constants;
  523. QString text = readTextFile(root + "/constants/map_object_constants.s");
  524. if (text != NULL) {
  525. QList<QStringList> *commands = parse(text);
  526. for (int i = 0; i < commands->length(); i++) {
  527. QStringList params = commands->value(i);
  528. QString macro = params.value(0);
  529. if (macro == ".set") {
  530. QString constant = params.value(1);
  531. if (constant.startsWith("MAP_OBJ_GFX_")) {
  532. int value = params.value(2).toInt(nullptr, 0);
  533. constants.insert(constant, value);
  534. }
  535. }
  536. }
  537. }
  538. return constants;
  539. }
  540. QString Project::fixGraphicPath(QString path) {
  541. path = path.replace(QRegExp("\\.lz$"), "");
  542. path = path.replace(QRegExp("\\.[1248]bpp$"), ".png");
  543. return path;
  544. }
  545. void Project::loadObjectPixmaps(QList<ObjectEvent*> objects) {
  546. bool needs_update = false;
  547. for (ObjectEvent *object : objects) {
  548. if (object->pixmap.isNull()) {
  549. needs_update = true;
  550. break;
  551. }
  552. }
  553. if (!needs_update) {
  554. return;
  555. }
  556. QMap<QString, int> constants = getMapObjGfxConstants();
  557. QString pointers_path = root + "/data/graphics/field_objects/map_object_graphics_info_pointers.s";
  558. QString pointers_text = readTextFile(pointers_path);
  559. if (pointers_text == NULL) {
  560. return;
  561. }
  562. QString info_path = root + "/data/graphics/field_objects/map_object_graphics_info.s";
  563. QString info_text = readTextFile(info_path);
  564. if (info_text == NULL) {
  565. return;
  566. }
  567. QString pic_path = root + "/data/graphics/field_objects/map_object_pic_tables.s";
  568. QString pic_text = readTextFile(pic_path);
  569. if (pic_text == NULL) {
  570. return;
  571. }
  572. QString assets_path = root + "/data/graphics/field_objects/map_object_graphics.s";
  573. QString assets_text = readTextFile(assets_path);
  574. if (assets_text == NULL) {
  575. return;
  576. }
  577. QStringList *pointers = getLabelValues(parse(pointers_text), "gMapObjectGraphicsInfoPointers");
  578. QList<QStringList> *info_commands = parse(info_text);
  579. QList<QStringList> *asset_commands = parse(assets_text);
  580. QList<QStringList> *pic_commands = parse(pic_text);
  581. for (ObjectEvent *object : objects) {
  582. if (!object->pixmap.isNull()) {
  583. continue;
  584. }
  585. int id = constants.value(object->sprite);
  586. QString info_label = pointers->value(id);
  587. QStringList *info = getLabelValues(info_commands, info_label);
  588. QString pic_label = info->value(12);
  589. QList<QStringList> *pic = getLabelMacros(pic_commands, pic_label);
  590. for (int i = 0; i < pic->length(); i++) {
  591. QStringList command = pic->value(i);
  592. QString macro = command.value(0);
  593. if (macro == "obj_frame_tiles") {
  594. QString label = command.value(1);
  595. QStringList *incbins = getLabelValues(asset_commands, label);
  596. QString path = incbins->value(0).section('"', 1, 1);
  597. path = fixGraphicPath(path);
  598. QPixmap pixmap(root + "/" + path);
  599. object->pixmap = pixmap;
  600. break;
  601. }
  602. }
  603. }
  604. }
  605. void Project::readMapEvents(Map *map) {
  606. // lazy
  607. QString path = root + QString("/data/maps/events/%1.s").arg(map->name);
  608. QString text = readTextFile(path);
  609. QStringList *labels = getLabelValues(parse(text), map->events_label);
  610. map->object_events_label = labels->value(0);
  611. map->warps_label = labels->value(1);
  612. map->coord_events_label = labels->value(2);
  613. map->bg_events_label = labels->value(3);
  614. QList<QStringList> *object_events = getLabelMacros(parse(text), map->object_events_label);
  615. map->object_events.clear();
  616. for (QStringList command : *object_events) {
  617. if (command.value(0) == "object_event") {
  618. ObjectEvent *object = new ObjectEvent;
  619. // This macro is not fixed as of writing, but it should take fewer args.
  620. bool old_macro = false;
  621. if (command.length() >= 20) {
  622. command.removeAt(19);
  623. command.removeAt(18);
  624. command.removeAt(15);
  625. command.removeAt(13);
  626. command.removeAt(11);
  627. command.removeAt(7);
  628. command.removeAt(5);
  629. command.removeAt(1); // id. not 0, but is just the index in the list of objects
  630. old_macro = true;
  631. }
  632. int i = 1;
  633. object->sprite = command.value(i++);
  634. object->replacement = command.value(i++);
  635. object->x_ = command.value(i++);
  636. object->y_ = command.value(i++);
  637. object->elevation_ = command.value(i++);
  638. object->behavior = command.value(i++);
  639. if (old_macro) {
  640. int radius = command.value(i++).toInt(nullptr, 0);
  641. object->radius_x = QString("%1").arg(radius & 0xf);
  642. object->radius_y = QString("%1").arg((radius >> 4) & 0xf);
  643. } else {
  644. object->radius_x = command.value(i++);
  645. object->radius_y = command.value(i++);
  646. }
  647. object->property = command.value(i++);
  648. object->sight_radius = command.value(i++);
  649. object->script_label = command.value(i++);
  650. object->event_flag = command.value(i++);
  651. map->object_events.append(object);
  652. }
  653. }
  654. QList<QStringList> *warps = getLabelMacros(parse(text), map->warps_label);
  655. map->warps.clear();
  656. for (QStringList command : *warps) {
  657. if (command.value(0) == "warp_def") {
  658. Warp *warp = new Warp;
  659. int i = 1;
  660. warp->x_ = command.value(i++);
  661. warp->y_ = command.value(i++);
  662. warp->elevation_ = command.value(i++);
  663. warp->destination_warp = command.value(i++);
  664. warp->destination_map = command.value(i++);
  665. map->warps.append(warp);
  666. }
  667. }
  668. QList<QStringList> *coords = getLabelMacros(parse(text), map->coord_events_label);
  669. map->coord_events.clear();
  670. for (QStringList command : *coords) {
  671. if (command.value(0) == "coord_event") {
  672. CoordEvent *coord = new CoordEvent;
  673. bool old_macro = false;
  674. if (command.length() >= 9) {
  675. command.removeAt(7);
  676. command.removeAt(6);
  677. command.removeAt(4);
  678. old_macro = true;
  679. }
  680. int i = 1;
  681. coord->x_ = command.value(i++);
  682. coord->y_ = command.value(i++);
  683. coord->elevation_ = command.value(i++);
  684. coord->unknown1 = command.value(i++);
  685. coord->script_label = command.value(i++);
  686. map->coord_events.append(coord);
  687. }
  688. }
  689. QList<QStringList> *bgs = getLabelMacros(parse(text), map->warps_label);
  690. map->hidden_items.clear();
  691. map->signs.clear();
  692. for (QStringList command : *bgs) {
  693. if (command.value(0) == "bg_event") {
  694. BGEvent *bg = new BGEvent;
  695. int i = 1;
  696. bg->x_ = command.value(i++);
  697. bg->y_ = command.value(i++);
  698. bg->elevation_ = command.value(i++);
  699. bg->type = command.value(i++);
  700. i++;
  701. if (bg->is_item()) {
  702. HiddenItem *item = new HiddenItem(*bg);
  703. item->item = command.value(i++);
  704. item->unknown5 = command.value(i++);
  705. item->unknown6 = command.value(i++);
  706. map->hidden_items.append(item);
  707. } else {
  708. Sign *sign = new Sign(*bg);
  709. sign->script_label = command.value(i++);
  710. map->signs.append(sign);
  711. }
  712. }
  713. }
  714. }