Нема описа

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