Nessuna descrizione

map.cpp 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. #include "map.h"
  2. #include <QTime>
  3. #include <QDebug>
  4. #include <QPainter>
  5. #include <QImage>
  6. #include <QRegularExpression>
  7. Map::Map(QObject *parent) : QObject(parent)
  8. {
  9. blockdata = new Blockdata;
  10. cached_blockdata = new Blockdata;
  11. cached_collision = new Blockdata;
  12. cached_border = new Blockdata;
  13. paint_tile = 1;
  14. paint_collision = 0;
  15. paint_elevation = 3;
  16. }
  17. void Map::setName(QString mapName) {
  18. name = mapName;
  19. constantName = mapConstantFromName(mapName);
  20. }
  21. QString Map::mapConstantFromName(QString mapName) {
  22. // Transform map names of the form 'GraniteCave_B1F` into map constants like 'MAP_GRANITE_CAVE_B1F'.
  23. QString nameWithUnderscores = mapName.replace(QRegularExpression("([a-z])([A-Z])"), "\\1_\\2");
  24. QString withMapAndUppercase = "MAP_" + nameWithUnderscores.toUpper();
  25. QString constantName = withMapAndUppercase.replace(QRegularExpression("_+"), "_");
  26. // Handle special cases.
  27. // SSTidal needs to be SS_TIDAL, rather than SSTIDAL
  28. constantName = constantName.replace("SSTIDAL", "SS_TIDAL");
  29. return constantName;
  30. }
  31. int Map::getWidth() {
  32. return width.toInt(nullptr, 0);
  33. }
  34. int Map::getHeight() {
  35. return height.toInt(nullptr, 0);
  36. }
  37. Tileset* Map::getBlockTileset(int metatile_index) {
  38. int primary_size = 0x200;//tileset_primary->metatiles->length();
  39. if (metatile_index < primary_size) {
  40. return tileset_primary;
  41. } else {
  42. return tileset_secondary;
  43. }
  44. }
  45. QList<QList<QRgb>> Map::getBlockPalettes(int metatile_index) {
  46. QList<QList<QRgb>> palettes;
  47. for (int i = 0; i < 6; i++) {
  48. palettes.append(tileset_primary->palettes->at(i));
  49. }
  50. for (int i = 6; i < tileset_secondary->palettes->length(); i++) {
  51. palettes.append(tileset_secondary->palettes->at(i));
  52. }
  53. return palettes;
  54. }
  55. int Map::getBlockIndex(int index) {
  56. int primary_size = 0x200;
  57. if (index < primary_size) {
  58. return index;
  59. } else {
  60. return index - primary_size;
  61. }
  62. }
  63. QImage Map::getMetatileTile(int tile) {
  64. Tileset *tileset = getBlockTileset(tile);
  65. int local_index = getBlockIndex(tile);
  66. if (!tileset || !tileset->tiles) {
  67. return QImage();
  68. }
  69. return tileset->tiles->value(local_index, QImage());
  70. }
  71. Metatile* Map::getMetatile(int index) {
  72. Tileset *tileset = getBlockTileset(index);
  73. int local_index = getBlockIndex(index);
  74. if (!tileset || !tileset->metatiles) {
  75. return NULL;
  76. }
  77. Metatile *metatile = tileset->metatiles->value(local_index, NULL);
  78. return metatile;
  79. }
  80. QImage Map::getCollisionMetatileImage(Block block) {
  81. return getCollisionMetatileImage(block.collision);
  82. }
  83. QImage Map::getCollisionMetatileImage(int collision) {
  84. QImage metatile_image(16, 16, QImage::Format_RGBA8888);
  85. QColor color;
  86. if (collision == 0) {
  87. color.setGreen(0xff);
  88. } else if (collision == 1) {
  89. color.setRed(0xff);
  90. } else if (collision == 2) {
  91. color.setBlue(0xff);
  92. } else if (collision == 3) {
  93. // black
  94. }
  95. metatile_image.fill(color);
  96. return metatile_image;
  97. }
  98. QImage Map::getElevationMetatileImage(Block block) {
  99. return getElevationMetatileImage(block.elevation);
  100. }
  101. QImage Map::getElevationMetatileImage(int elevation) {
  102. QImage metatile_image(16, 16, QImage::Format_RGBA8888);
  103. QColor color;
  104. if (elevation < 15) {
  105. uint saturation = (elevation + 1) * 16 + 15;
  106. color.setGreen(saturation);
  107. color.setRed(saturation);
  108. color.setBlue(saturation);
  109. } else {
  110. color.setGreen(0xd0);
  111. color.setBlue(0xd0);
  112. color.setRed(0);
  113. }
  114. metatile_image.fill(color);
  115. //QPainter painter(&metatile_image);
  116. //painter.end();
  117. return metatile_image;
  118. }
  119. QImage Map::getMetatileImage(int tile) {
  120. QImage metatile_image(16, 16, QImage::Format_RGBA8888);
  121. Metatile* metatile = getMetatile(tile);
  122. if (!metatile || !metatile->tiles) {
  123. metatile_image.fill(0xffffffff);
  124. return metatile_image;
  125. }
  126. Tileset* blockTileset = getBlockTileset(tile);
  127. if (!blockTileset) {
  128. metatile_image.fill(0xffffffff);
  129. return metatile_image;
  130. }
  131. QList<QList<QRgb>> palettes = getBlockPalettes(tile);
  132. QPainter metatile_painter(&metatile_image);
  133. for (int layer = 0; layer < 2; layer++)
  134. for (int y = 0; y < 2; y++)
  135. for (int x = 0; x < 2; x++) {
  136. Tile tile_ = metatile->tiles->value((y * 2) + x + (layer * 4));
  137. QImage tile_image = getMetatileTile(tile_.tile);
  138. if (tile_image.isNull()) {
  139. // Some metatiles specify tiles that are outside the valid range.
  140. // These are treated as completely transparent, so they can be skipped without
  141. // being drawn.
  142. continue;
  143. }
  144. // Colorize the metatile tiles with its palette.
  145. QList<QRgb> palette = palettes.value(tile_.palette);
  146. for (int j = 0; j < palette.length(); j++) {
  147. tile_image.setColor(j, palette.value(j));
  148. }
  149. // The top layer of the metatile has its last color displayed at transparent.
  150. if (layer > 0) {
  151. QColor color(tile_image.color(15));
  152. color.setAlpha(0);
  153. tile_image.setColor(15, color.rgba());
  154. }
  155. QPoint origin = QPoint(x*8, y*8);
  156. metatile_painter.drawImage(origin, tile_image.mirrored(tile_.xflip == 1, tile_.yflip == 1));
  157. }
  158. metatile_painter.end();
  159. return metatile_image;
  160. }
  161. bool Map::blockChanged(int i, Blockdata *cache) {
  162. if (cache == NULL || cache == nullptr) {
  163. return true;
  164. }
  165. if (blockdata == NULL || blockdata == nullptr) {
  166. return true;
  167. }
  168. if (cache->blocks == NULL || cache->blocks == nullptr) {
  169. return true;
  170. }
  171. if (blockdata->blocks == NULL || blockdata->blocks == nullptr) {
  172. return true;
  173. }
  174. if (cache->blocks->length() <= i) {
  175. return true;
  176. }
  177. if (blockdata->blocks->length() <= i) {
  178. return true;
  179. }
  180. return blockdata->blocks->value(i) != cache->blocks->value(i);
  181. }
  182. void Map::cacheBorder() {
  183. if (cached_border) delete cached_border;
  184. cached_border = new Blockdata;
  185. if (border && border->blocks) {
  186. for (int i = 0; i < border->blocks->length(); i++) {
  187. Block block = border->blocks->value(i);
  188. cached_border->blocks->append(block);
  189. }
  190. }
  191. }
  192. void Map::cacheBlockdata() {
  193. if (cached_blockdata) delete cached_blockdata;
  194. cached_blockdata = new Blockdata;
  195. if (blockdata && blockdata->blocks) {
  196. for (int i = 0; i < blockdata->blocks->length(); i++) {
  197. Block block = blockdata->blocks->value(i);
  198. cached_blockdata->blocks->append(block);
  199. }
  200. }
  201. }
  202. void Map::cacheCollision() {
  203. if (cached_collision) delete cached_collision;
  204. cached_collision = new Blockdata;
  205. if (blockdata && blockdata->blocks) {
  206. for (int i = 0; i < blockdata->blocks->length(); i++) {
  207. Block block = blockdata->blocks->value(i);
  208. cached_collision->blocks->append(block);
  209. }
  210. }
  211. }
  212. QPixmap Map::renderCollision() {
  213. bool changed_any = false;
  214. int width_ = getWidth();
  215. int height_ = getHeight();
  216. if (
  217. collision_image.isNull()
  218. || collision_image.width() != width_ * 16
  219. || collision_image.height() != height_ * 16
  220. ) {
  221. collision_image = QImage(width_ * 16, height_ * 16, QImage::Format_RGBA8888);
  222. changed_any = true;
  223. }
  224. if (!(blockdata && blockdata->blocks && width_ && height_)) {
  225. collision_pixmap = collision_pixmap.fromImage(collision_image);
  226. return collision_pixmap;
  227. }
  228. QPainter painter(&collision_image);
  229. for (int i = 0; i < blockdata->blocks->length(); i++) {
  230. if (cached_collision && !blockChanged(i, cached_collision)) {
  231. continue;
  232. }
  233. changed_any = true;
  234. Block block = blockdata->blocks->value(i);
  235. QImage metatile_image = getMetatileImage(block.tile);
  236. QImage collision_metatile_image = getCollisionMetatileImage(block);
  237. QImage elevation_metatile_image = getElevationMetatileImage(block);
  238. int map_y = width_ ? i / width_ : 0;
  239. int map_x = width_ ? i % width_ : 0;
  240. QPoint metatile_origin = QPoint(map_x * 16, map_y * 16);
  241. painter.setOpacity(1);
  242. painter.drawImage(metatile_origin, metatile_image);
  243. painter.save();
  244. if (block.elevation == 15) {
  245. painter.setOpacity(0.5);
  246. } else if (block.elevation == 0) {
  247. painter.setOpacity(0);
  248. } else {
  249. painter.setOpacity(1);//(block.elevation / 16.0) * 0.8);
  250. painter.setCompositionMode(QPainter::CompositionMode_Overlay);
  251. }
  252. painter.drawImage(metatile_origin, elevation_metatile_image);
  253. painter.restore();
  254. painter.save();
  255. if (block.collision == 0) {
  256. painter.setOpacity(0.1);
  257. } else {
  258. painter.setOpacity(0.4);
  259. }
  260. painter.drawImage(metatile_origin, collision_metatile_image);
  261. painter.restore();
  262. painter.save();
  263. painter.setOpacity(0.6);
  264. painter.setPen(QColor(255, 255, 255, 192));
  265. painter.setFont(QFont("Helvetica", 8));
  266. painter.drawText(QPoint(metatile_origin.x(), metatile_origin.y() + 8), QString("%1").arg(block.elevation));
  267. painter.restore();
  268. }
  269. painter.end();
  270. cacheCollision();
  271. if (changed_any) {
  272. collision_pixmap = collision_pixmap.fromImage(collision_image);
  273. }
  274. return collision_pixmap;
  275. }
  276. QPixmap Map::render() {
  277. bool changed_any = false;
  278. int width_ = getWidth();
  279. int height_ = getHeight();
  280. if (
  281. image.isNull()
  282. || image.width() != width_ * 16
  283. || image.height() != height_ * 16
  284. ) {
  285. image = QImage(width_ * 16, height_ * 16, QImage::Format_RGBA8888);
  286. changed_any = true;
  287. }
  288. if (!(blockdata && blockdata->blocks && width_ && height_)) {
  289. pixmap = pixmap.fromImage(image);
  290. return pixmap;
  291. }
  292. QPainter painter(&image);
  293. for (int i = 0; i < blockdata->blocks->length(); i++) {
  294. if (!blockChanged(i, cached_blockdata)) {
  295. continue;
  296. }
  297. changed_any = true;
  298. Block block = blockdata->blocks->value(i);
  299. QImage metatile_image = getMetatileImage(block.tile);
  300. int map_y = width_ ? i / width_ : 0;
  301. int map_x = width_ ? i % width_ : 0;
  302. QPoint metatile_origin = QPoint(map_x * 16, map_y * 16);
  303. painter.drawImage(metatile_origin, metatile_image);
  304. }
  305. painter.end();
  306. if (changed_any) {
  307. cacheBlockdata();
  308. pixmap = pixmap.fromImage(image);
  309. }
  310. return pixmap;
  311. }
  312. QPixmap Map::renderBorder() {
  313. bool changed_any = false;
  314. int width_ = 2;
  315. int height_ = 2;
  316. if (border_image.isNull()) {
  317. border_image = QImage(width_ * 16, height_ * 16, QImage::Format_RGBA8888);
  318. changed_any = true;
  319. }
  320. if (!(border && border->blocks)) {
  321. border_pixmap = border_pixmap.fromImage(border_image);
  322. return border_pixmap;
  323. }
  324. QPainter painter(&border_image);
  325. for (int i = 0; i < border->blocks->length(); i++) {
  326. if (!blockChanged(i, cached_border)) {
  327. continue;
  328. }
  329. changed_any = true;
  330. Block block = border->blocks->value(i);
  331. QImage metatile_image = getMetatileImage(block.tile);
  332. int map_y = i / width_;
  333. int map_x = i % width_;
  334. painter.drawImage(QPoint(map_x * 16, map_y * 16), metatile_image);
  335. }
  336. painter.end();
  337. if (changed_any) {
  338. cacheBorder();
  339. border_pixmap = border_pixmap.fromImage(border_image);
  340. }
  341. return border_pixmap;
  342. }
  343. QPixmap Map::renderConnection(Connection connection) {
  344. render();
  345. int x, y, w, h;
  346. if (connection.direction == "up") {
  347. x = 0;
  348. y = getHeight() - 6;
  349. w = getWidth();
  350. h = 6;
  351. } else if (connection.direction == "down") {
  352. x = 0;
  353. y = 0;
  354. w = getWidth();
  355. h = 6;
  356. } else if (connection.direction == "left") {
  357. x = getWidth() - 6;
  358. y = 0;
  359. w = 6;
  360. h = getHeight();
  361. } else if (connection.direction == "right") {
  362. x = 0;
  363. y = 0;
  364. w = 6;
  365. h = getHeight();
  366. } else {
  367. // this should not happen
  368. x = 0;
  369. y = 0;
  370. w = getWidth();
  371. h = getHeight();
  372. }
  373. QImage connection_image = image.copy(x * 16, y * 16, w * 16, h * 16);
  374. //connection_image = connection_image.convertToFormat(QImage::Format_Grayscale8);
  375. return QPixmap::fromImage(connection_image);
  376. }
  377. QPixmap Map::renderCollisionMetatiles() {
  378. int length_ = 4;
  379. int height_ = 1;
  380. int width_ = length_ / height_;
  381. QImage image(width_ * 16, height_ * 16, QImage::Format_RGBA8888);
  382. QPainter painter(&image);
  383. for (int i = 0; i < length_; i++) {
  384. int y = i / width_;
  385. int x = i % width_;
  386. QPoint origin(x * 16, y * 16);
  387. QImage metatile_image = getCollisionMetatileImage(i);
  388. painter.drawImage(origin, metatile_image);
  389. }
  390. drawSelection(paint_collision, width_, &painter);
  391. painter.end();
  392. return QPixmap::fromImage(image);
  393. }
  394. QPixmap Map::renderElevationMetatiles() {
  395. int length_ = 16;
  396. int height_ = 2;
  397. int width_ = length_ / height_;
  398. QImage image(width_ * 16, height_ * 16, QImage::Format_RGBA8888);
  399. QPainter painter(&image);
  400. for (int i = 0; i < length_; i++) {
  401. int y = i / width_;
  402. int x = i % width_;
  403. QPoint origin(x * 16, y * 16);
  404. QImage metatile_image = getElevationMetatileImage(i);
  405. painter.drawImage(origin, metatile_image);
  406. }
  407. drawSelection(paint_elevation, width_, &painter);
  408. painter.end();
  409. return QPixmap::fromImage(image);
  410. }
  411. void Map::drawSelection(int i, int w, QPainter *painter) {
  412. int x = i % w;
  413. int y = i / w;
  414. painter->save();
  415. QColor penColor = smart_paths_enabled ? QColor(0xff, 0x0, 0xff) : QColor(0xff, 0xff, 0xff);
  416. painter->setPen(penColor);
  417. int rectWidth = paint_tile_width * 16;
  418. int rectHeight = paint_tile_height * 16;
  419. painter->drawRect(x * 16, y * 16, rectWidth - 1, rectHeight -1);
  420. painter->setPen(QColor(0, 0, 0));
  421. painter->drawRect(x * 16 - 1, y * 16 - 1, rectWidth + 1, rectHeight + 1);
  422. painter->drawRect(x * 16 + 1, y * 16 + 1, rectWidth - 3, rectHeight - 3);
  423. painter->restore();
  424. }
  425. QPixmap Map::renderMetatiles() {
  426. if (!tileset_primary || !tileset_primary->metatiles || !tileset_secondary || !tileset_secondary->metatiles) {
  427. return QPixmap();
  428. }
  429. int primary_length = tileset_primary->metatiles->length();
  430. int length_ = primary_length + tileset_secondary->metatiles->length();
  431. int width_ = 8;
  432. int height_ = length_ / width_;
  433. QImage image(width_ * 16, height_ * 16, QImage::Format_RGBA8888);
  434. QPainter painter(&image);
  435. for (int i = 0; i < length_; i++) {
  436. int tile = i;
  437. if (i >= primary_length) {
  438. tile += 0x200 - primary_length;
  439. }
  440. QImage metatile_image = getMetatileImage(tile);
  441. int map_y = i / width_;
  442. int map_x = i % width_;
  443. QPoint metatile_origin = QPoint(map_x * 16, map_y * 16);
  444. painter.drawImage(metatile_origin, metatile_image);
  445. }
  446. drawSelection(paint_tile, width_, &painter);
  447. painter.end();
  448. return QPixmap::fromImage(image);
  449. }
  450. Block* Map::getBlock(int x, int y) {
  451. if (blockdata && blockdata->blocks) {
  452. if (x >= 0 && x < getWidth())
  453. if (y >= 0 && y < getHeight()) {
  454. int i = y * getWidth() + x;
  455. return new Block(blockdata->blocks->value(i));
  456. }
  457. }
  458. return NULL;
  459. }
  460. void Map::_setBlock(int x, int y, Block block) {
  461. int i = y * getWidth() + x;
  462. if (blockdata && blockdata->blocks) {
  463. blockdata->blocks->replace(i, block);
  464. }
  465. }
  466. void Map::_floodFill(int x, int y, uint tile) {
  467. QList<QPoint> todo;
  468. todo.append(QPoint(x, y));
  469. while (todo.length()) {
  470. QPoint point = todo.takeAt(0);
  471. x = point.x();
  472. y = point.y();
  473. Block *block = getBlock(x, y);
  474. if (block == NULL) {
  475. continue;
  476. }
  477. uint old_tile = block->tile;
  478. if (old_tile == tile) {
  479. continue;
  480. }
  481. block->tile = tile;
  482. _setBlock(x, y, *block);
  483. if ((block = getBlock(x + 1, y)) && block->tile == old_tile) {
  484. todo.append(QPoint(x + 1, y));
  485. }
  486. if ((block = getBlock(x - 1, y)) && block->tile == old_tile) {
  487. todo.append(QPoint(x - 1, y));
  488. }
  489. if ((block = getBlock(x, y + 1)) && block->tile == old_tile) {
  490. todo.append(QPoint(x, y + 1));
  491. }
  492. if ((block = getBlock(x, y - 1)) && block->tile == old_tile) {
  493. todo.append(QPoint(x, y - 1));
  494. }
  495. }
  496. }
  497. void Map::_floodFillCollision(int x, int y, uint collision) {
  498. QList<QPoint> todo;
  499. todo.append(QPoint(x, y));
  500. while (todo.length()) {
  501. QPoint point = todo.takeAt(0);
  502. x = point.x();
  503. y = point.y();
  504. Block *block = getBlock(x, y);
  505. if (block == NULL) {
  506. continue;
  507. }
  508. uint old_coll = block->collision;
  509. if (old_coll == collision) {
  510. continue;
  511. }
  512. block->collision = collision;
  513. _setBlock(x, y, *block);
  514. if ((block = getBlock(x + 1, y)) && block->collision == old_coll) {
  515. todo.append(QPoint(x + 1, y));
  516. }
  517. if ((block = getBlock(x - 1, y)) && block->collision == old_coll) {
  518. todo.append(QPoint(x - 1, y));
  519. }
  520. if ((block = getBlock(x, y + 1)) && block->collision == old_coll) {
  521. todo.append(QPoint(x, y + 1));
  522. }
  523. if ((block = getBlock(x, y - 1)) && block->collision == old_coll) {
  524. todo.append(QPoint(x, y - 1));
  525. }
  526. }
  527. }
  528. void Map::_floodFillElevation(int x, int y, uint elevation) {
  529. QList<QPoint> todo;
  530. todo.append(QPoint(x, y));
  531. while (todo.length()) {
  532. QPoint point = todo.takeAt(0);
  533. x = point.x();
  534. y = point.y();
  535. Block *block = getBlock(x, y);
  536. if (block == NULL) {
  537. continue;
  538. }
  539. uint old_z = block->elevation;
  540. if (old_z == elevation) {
  541. continue;
  542. }
  543. Block block_(*block);
  544. block_.elevation = elevation;
  545. _setBlock(x, y, block_);
  546. if ((block = getBlock(x + 1, y)) && block->elevation == old_z) {
  547. todo.append(QPoint(x + 1, y));
  548. }
  549. if ((block = getBlock(x - 1, y)) && block->elevation == old_z) {
  550. todo.append(QPoint(x - 1, y));
  551. }
  552. if ((block = getBlock(x, y + 1)) && block->elevation == old_z) {
  553. todo.append(QPoint(x, y + 1));
  554. }
  555. if ((block = getBlock(x, y - 1)) && block->elevation == old_z) {
  556. todo.append(QPoint(x, y - 1));
  557. }
  558. }
  559. }
  560. void Map::_floodFillCollisionElevation(int x, int y, uint collision, uint elevation) {
  561. QList<QPoint> todo;
  562. todo.append(QPoint(x, y));
  563. while (todo.length()) {
  564. QPoint point = todo.takeAt(0);
  565. x = point.x();
  566. y = point.y();
  567. Block *block = getBlock(x, y);
  568. if (block == NULL) {
  569. continue;
  570. }
  571. uint old_coll = block->collision;
  572. uint old_elev = block->elevation;
  573. if (old_coll == collision && old_elev == elevation) {
  574. continue;
  575. }
  576. block->collision = collision;
  577. block->elevation = elevation;
  578. _setBlock(x, y, *block);
  579. if ((block = getBlock(x + 1, y)) && block->collision == old_coll && block->elevation == old_elev) {
  580. todo.append(QPoint(x + 1, y));
  581. }
  582. if ((block = getBlock(x - 1, y)) && block->collision == old_coll && block->elevation == old_elev) {
  583. todo.append(QPoint(x - 1, y));
  584. }
  585. if ((block = getBlock(x, y + 1)) && block->collision == old_coll && block->elevation == old_elev) {
  586. todo.append(QPoint(x, y + 1));
  587. }
  588. if ((block = getBlock(x, y - 1)) && block->collision == old_coll && block->elevation == old_elev) {
  589. todo.append(QPoint(x, y - 1));
  590. }
  591. }
  592. }
  593. void Map::undo() {
  594. if (blockdata) {
  595. Blockdata *commit = history.back();
  596. if (commit != NULL) {
  597. blockdata->copyFrom(commit);
  598. emit mapChanged(this);
  599. }
  600. }
  601. }
  602. void Map::redo() {
  603. if (blockdata) {
  604. Blockdata *commit = history.next();
  605. if (commit != NULL) {
  606. blockdata->copyFrom(commit);
  607. emit mapChanged(this);
  608. }
  609. }
  610. }
  611. void Map::commit() {
  612. if (blockdata) {
  613. if (!blockdata->equals(history.current())) {
  614. Blockdata* commit = blockdata->copy();
  615. history.push(commit);
  616. emit mapChanged(this);
  617. }
  618. }
  619. }
  620. void Map::setBlock(int x, int y, Block block) {
  621. Block *old_block = getBlock(x, y);
  622. if (old_block && (*old_block) != block) {
  623. _setBlock(x, y, block);
  624. commit();
  625. }
  626. }
  627. void Map::floodFill(int x, int y, uint tile) {
  628. Block *block = getBlock(x, y);
  629. if (block && block->tile != tile) {
  630. _floodFill(x, y, tile);
  631. commit();
  632. }
  633. }
  634. void Map::floodFillCollision(int x, int y, uint collision) {
  635. Block *block = getBlock(x, y);
  636. if (block && block->collision != collision) {
  637. _floodFillCollision(x, y, collision);
  638. commit();
  639. }
  640. }
  641. void Map::floodFillElevation(int x, int y, uint elevation) {
  642. Block *block = getBlock(x, y);
  643. if (block && block->elevation != elevation) {
  644. _floodFillElevation(x, y, elevation);
  645. commit();
  646. }
  647. }
  648. void Map::floodFillCollisionElevation(int x, int y, uint collision, uint elevation) {
  649. Block *block = getBlock(x, y);
  650. if (block && (block->collision != collision || block->elevation != elevation)) {
  651. _floodFillCollisionElevation(x, y, collision, elevation);
  652. commit();
  653. }
  654. }
  655. QList<Event *> Map::getAllEvents() {
  656. QList<Event*> all;
  657. for (QList<Event*> list : events.values()) {
  658. all += list;
  659. }
  660. return all;
  661. }
  662. QList<Event *> Map::getEventsByType(QString type)
  663. {
  664. return events.value(type);
  665. }
  666. void Map::removeEvent(Event *event) {
  667. for (QString key : events.keys()) {
  668. events[key].removeAll(event);
  669. }
  670. }
  671. void Map::addEvent(Event *event) {
  672. events[event->get("event_type")].append(event);
  673. }
  674. bool Map::hasUnsavedChanges() {
  675. return !history.isSaved() || !isPersistedToFile;
  676. }
  677. void Map::hoveredTileChanged(int x, int y, int block) {
  678. emit statusBarMessage(QString("X: %1, Y: %2, Block: 0x%3")
  679. .arg(x)
  680. .arg(y)
  681. .arg(QString("%1").arg(block, 3, 16, QChar('0')).toUpper()));
  682. }
  683. void Map::clearHoveredTile() {
  684. emit statusBarMessage(QString(""));
  685. }
  686. void Map::hoveredMetatileChanged(int block) {
  687. emit statusBarMessage(QString("Block: 0x%1")
  688. .arg(QString("%1").arg(block, 3, 16, QChar('0')).toUpper()));
  689. }
  690. void Map::clearHoveredMetatile() {
  691. emit statusBarMessage(QString(""));
  692. }
  693. void Map::hoveredCollisionTileChanged(int collision) {
  694. emit statusBarMessage(QString("Collision: %1").arg(collision));
  695. }
  696. void Map::clearHoveredCollisionTile() {
  697. emit statusBarMessage(QString(""));
  698. }
  699. void Map::hoveredElevationTileChanged(int elevation) {
  700. emit statusBarMessage(QString("Elevation: %1").arg(elevation));
  701. }
  702. void Map::clearHoveredElevationTile() {
  703. emit statusBarMessage(QString(""));
  704. }