Nessuna descrizione

map.cpp 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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. // continue;
  140. //}
  141. //if (blockTileset->palettes) {
  142. QList<QRgb> palette = palettes.value(tile_.palette);
  143. for (int j = 0; j < palette.length(); j++) {
  144. tile_image.setColor(j, palette.value(j));
  145. }
  146. //}
  147. //QVector<QRgb> vector = palette.toVector();
  148. //tile_image.setColorTable(vector);
  149. if (layer > 0) {
  150. QColor color(tile_image.color(15));
  151. color.setAlpha(0);
  152. tile_image.setColor(15, color.rgba());
  153. }
  154. QPoint origin = QPoint(x*8, y*8);
  155. metatile_painter.drawImage(origin, tile_image.mirrored(tile_.xflip == 1, tile_.yflip == 1));
  156. }
  157. metatile_painter.end();
  158. return metatile_image;
  159. }
  160. bool Map::blockChanged(int i, Blockdata *cache) {
  161. if (cache == NULL || cache == nullptr) {
  162. return true;
  163. }
  164. if (blockdata == NULL || blockdata == nullptr) {
  165. return true;
  166. }
  167. if (cache->blocks == NULL || cache->blocks == nullptr) {
  168. return true;
  169. }
  170. if (blockdata->blocks == NULL || blockdata->blocks == nullptr) {
  171. return true;
  172. }
  173. if (cache->blocks->length() <= i) {
  174. return true;
  175. }
  176. if (blockdata->blocks->length() <= i) {
  177. return true;
  178. }
  179. return blockdata->blocks->value(i) != cache->blocks->value(i);
  180. }
  181. void Map::cacheBorder() {
  182. if (cached_border) delete cached_border;
  183. cached_border = new Blockdata;
  184. if (border && border->blocks) {
  185. for (int i = 0; i < border->blocks->length(); i++) {
  186. Block block = border->blocks->value(i);
  187. cached_border->blocks->append(block);
  188. }
  189. }
  190. }
  191. void Map::cacheBlockdata() {
  192. if (cached_blockdata) delete cached_blockdata;
  193. cached_blockdata = new Blockdata;
  194. if (blockdata && blockdata->blocks) {
  195. for (int i = 0; i < blockdata->blocks->length(); i++) {
  196. Block block = blockdata->blocks->value(i);
  197. cached_blockdata->blocks->append(block);
  198. }
  199. }
  200. }
  201. void Map::cacheCollision() {
  202. if (cached_collision) delete cached_collision;
  203. cached_collision = new Blockdata;
  204. if (blockdata && blockdata->blocks) {
  205. for (int i = 0; i < blockdata->blocks->length(); i++) {
  206. Block block = blockdata->blocks->value(i);
  207. cached_collision->blocks->append(block);
  208. }
  209. }
  210. }
  211. QPixmap Map::renderCollision() {
  212. bool changed_any = false;
  213. int width_ = getWidth();
  214. int height_ = getHeight();
  215. if (
  216. collision_image.isNull()
  217. || collision_image.width() != width_ * 16
  218. || collision_image.height() != height_ * 16
  219. ) {
  220. collision_image = QImage(width_ * 16, height_ * 16, QImage::Format_RGBA8888);
  221. changed_any = true;
  222. }
  223. if (!(blockdata && blockdata->blocks && width_ && height_)) {
  224. collision_pixmap = collision_pixmap.fromImage(collision_image);
  225. return collision_pixmap;
  226. }
  227. QPainter painter(&collision_image);
  228. for (int i = 0; i < blockdata->blocks->length(); i++) {
  229. if (cached_collision && !blockChanged(i, cached_collision)) {
  230. continue;
  231. }
  232. changed_any = true;
  233. Block block = blockdata->blocks->value(i);
  234. QImage metatile_image = getMetatileImage(block.tile);
  235. QImage collision_metatile_image = getCollisionMetatileImage(block);
  236. QImage elevation_metatile_image = getElevationMetatileImage(block);
  237. int map_y = width_ ? i / width_ : 0;
  238. int map_x = width_ ? i % width_ : 0;
  239. QPoint metatile_origin = QPoint(map_x * 16, map_y * 16);
  240. painter.setOpacity(1);
  241. painter.drawImage(metatile_origin, metatile_image);
  242. painter.save();
  243. if (block.elevation == 15) {
  244. painter.setOpacity(0.5);
  245. } else if (block.elevation == 0) {
  246. painter.setOpacity(0);
  247. } else {
  248. painter.setOpacity(1);//(block.elevation / 16.0) * 0.8);
  249. painter.setCompositionMode(QPainter::CompositionMode_Overlay);
  250. }
  251. painter.drawImage(metatile_origin, elevation_metatile_image);
  252. painter.restore();
  253. painter.save();
  254. if (block.collision == 0) {
  255. painter.setOpacity(0.1);
  256. } else {
  257. painter.setOpacity(0.4);
  258. }
  259. painter.drawImage(metatile_origin, collision_metatile_image);
  260. painter.restore();
  261. painter.save();
  262. painter.setOpacity(0.6);
  263. painter.setPen(QColor(255, 255, 255, 192));
  264. painter.setFont(QFont("Helvetica", 8));
  265. painter.drawText(QPoint(metatile_origin.x(), metatile_origin.y() + 8), QString("%1").arg(block.elevation));
  266. painter.restore();
  267. }
  268. painter.end();
  269. cacheCollision();
  270. if (changed_any) {
  271. collision_pixmap = collision_pixmap.fromImage(collision_image);
  272. }
  273. return collision_pixmap;
  274. }
  275. QPixmap Map::render() {
  276. bool changed_any = false;
  277. int width_ = getWidth();
  278. int height_ = getHeight();
  279. if (
  280. image.isNull()
  281. || image.width() != width_ * 16
  282. || image.height() != height_ * 16
  283. ) {
  284. image = QImage(width_ * 16, height_ * 16, QImage::Format_RGBA8888);
  285. changed_any = true;
  286. }
  287. if (!(blockdata && blockdata->blocks && width_ && height_)) {
  288. pixmap = pixmap.fromImage(image);
  289. return pixmap;
  290. }
  291. QPainter painter(&image);
  292. for (int i = 0; i < blockdata->blocks->length(); i++) {
  293. if (!blockChanged(i, cached_blockdata)) {
  294. continue;
  295. }
  296. changed_any = true;
  297. Block block = blockdata->blocks->value(i);
  298. QImage metatile_image = getMetatileImage(block.tile);
  299. int map_y = width_ ? i / width_ : 0;
  300. int map_x = width_ ? i % width_ : 0;
  301. QPoint metatile_origin = QPoint(map_x * 16, map_y * 16);
  302. painter.drawImage(metatile_origin, metatile_image);
  303. }
  304. painter.end();
  305. if (changed_any) {
  306. cacheBlockdata();
  307. pixmap = pixmap.fromImage(image);
  308. }
  309. return pixmap;
  310. }
  311. QPixmap Map::renderBorder() {
  312. bool changed_any = false;
  313. int width_ = 2;
  314. int height_ = 2;
  315. if (border_image.isNull()) {
  316. border_image = QImage(width_ * 16, height_ * 16, QImage::Format_RGBA8888);
  317. changed_any = true;
  318. }
  319. if (!(border && border->blocks)) {
  320. border_pixmap = border_pixmap.fromImage(border_image);
  321. return border_pixmap;
  322. }
  323. QPainter painter(&border_image);
  324. for (int i = 0; i < border->blocks->length(); i++) {
  325. if (!blockChanged(i, cached_border)) {
  326. continue;
  327. }
  328. changed_any = true;
  329. Block block = border->blocks->value(i);
  330. QImage metatile_image = getMetatileImage(block.tile);
  331. int map_y = i / width_;
  332. int map_x = i % width_;
  333. painter.drawImage(QPoint(map_x * 16, map_y * 16), metatile_image);
  334. }
  335. painter.end();
  336. if (changed_any) {
  337. cacheBorder();
  338. border_pixmap = border_pixmap.fromImage(border_image);
  339. }
  340. return border_pixmap;
  341. }
  342. QPixmap Map::renderConnection(Connection connection) {
  343. render();
  344. int x, y, w, h;
  345. if (connection.direction == "up") {
  346. x = 0;
  347. y = getHeight() - 6;
  348. w = getWidth();
  349. h = 6;
  350. } else if (connection.direction == "down") {
  351. x = 0;
  352. y = 0;
  353. w = getWidth();
  354. h = 6;
  355. } else if (connection.direction == "left") {
  356. x = getWidth() - 6;
  357. y = 0;
  358. w = 6;
  359. h = getHeight();
  360. } else if (connection.direction == "right") {
  361. x = 0;
  362. y = 0;
  363. w = 6;
  364. h = getHeight();
  365. } else {
  366. // this should not happen
  367. x = 0;
  368. y = 0;
  369. w = getWidth();
  370. h = getHeight();
  371. }
  372. QImage connection_image = image.copy(x * 16, y * 16, w * 16, h * 16);
  373. //connection_image = connection_image.convertToFormat(QImage::Format_Grayscale8);
  374. return QPixmap::fromImage(connection_image);
  375. }
  376. QPixmap Map::renderCollisionMetatiles() {
  377. int length_ = 4;
  378. int height_ = 1;
  379. int width_ = length_ / height_;
  380. QImage image(width_ * 16, height_ * 16, QImage::Format_RGBA8888);
  381. QPainter painter(&image);
  382. for (int i = 0; i < length_; i++) {
  383. int y = i / width_;
  384. int x = i % width_;
  385. QPoint origin(x * 16, y * 16);
  386. QImage metatile_image = getCollisionMetatileImage(i);
  387. painter.drawImage(origin, metatile_image);
  388. }
  389. drawSelection(paint_collision, width_, &painter);
  390. painter.end();
  391. return QPixmap::fromImage(image);
  392. }
  393. QPixmap Map::renderElevationMetatiles() {
  394. int length_ = 16;
  395. int height_ = 2;
  396. int width_ = length_ / height_;
  397. QImage image(width_ * 16, height_ * 16, QImage::Format_RGBA8888);
  398. QPainter painter(&image);
  399. for (int i = 0; i < length_; i++) {
  400. int y = i / width_;
  401. int x = i % width_;
  402. QPoint origin(x * 16, y * 16);
  403. QImage metatile_image = getElevationMetatileImage(i);
  404. painter.drawImage(origin, metatile_image);
  405. }
  406. drawSelection(paint_elevation, width_, &painter);
  407. painter.end();
  408. return QPixmap::fromImage(image);
  409. }
  410. void Map::drawSelection(int i, int w, QPainter *painter) {
  411. int x = i % w;
  412. int y = i / w;
  413. painter->save();
  414. painter->setPen(QColor(0xff, 0xff, 0xff));
  415. painter->drawRect(x * 16, y * 16, 15, 15);
  416. painter->setPen(QColor(0, 0, 0));
  417. painter->drawRect(x * 16 - 1, y * 16 - 1, 17, 17);
  418. painter->drawRect(x * 16 + 1, y * 16 + 1, 13, 13);
  419. painter->restore();
  420. }
  421. QPixmap Map::renderMetatiles() {
  422. if (!tileset_primary || !tileset_primary->metatiles || !tileset_secondary || !tileset_secondary->metatiles) {
  423. return QPixmap();
  424. }
  425. int primary_length = tileset_primary->metatiles->length();
  426. int length_ = primary_length + tileset_secondary->metatiles->length();
  427. int width_ = 8;
  428. int height_ = length_ / width_;
  429. QImage image(width_ * 16, height_ * 16, QImage::Format_RGBA8888);
  430. QPainter painter(&image);
  431. for (int i = 0; i < length_; i++) {
  432. int tile = i;
  433. if (i >= primary_length) {
  434. tile += 0x200 - primary_length;
  435. }
  436. QImage metatile_image = getMetatileImage(tile);
  437. int map_y = i / width_;
  438. int map_x = i % width_;
  439. QPoint metatile_origin = QPoint(map_x * 16, map_y * 16);
  440. painter.drawImage(metatile_origin, metatile_image);
  441. }
  442. drawSelection(paint_tile, width_, &painter);
  443. painter.end();
  444. return QPixmap::fromImage(image);
  445. }
  446. Block* Map::getBlock(int x, int y) {
  447. if (blockdata && blockdata->blocks) {
  448. if (x >= 0 && x < getWidth())
  449. if (y >= 0 && y < getHeight()) {
  450. int i = y * getWidth() + x;
  451. return new Block(blockdata->blocks->value(i));
  452. }
  453. }
  454. return NULL;
  455. }
  456. void Map::_setBlock(int x, int y, Block block) {
  457. int i = y * getWidth() + x;
  458. if (blockdata && blockdata->blocks) {
  459. blockdata->blocks->replace(i, block);
  460. }
  461. }
  462. void Map::_floodFill(int x, int y, uint tile) {
  463. QList<QPoint> todo;
  464. todo.append(QPoint(x, y));
  465. while (todo.length()) {
  466. QPoint point = todo.takeAt(0);
  467. x = point.x();
  468. y = point.y();
  469. Block *block = getBlock(x, y);
  470. if (block == NULL) {
  471. continue;
  472. }
  473. uint old_tile = block->tile;
  474. if (old_tile == tile) {
  475. continue;
  476. }
  477. block->tile = tile;
  478. _setBlock(x, y, *block);
  479. if ((block = getBlock(x + 1, y)) && block->tile == old_tile) {
  480. todo.append(QPoint(x + 1, y));
  481. }
  482. if ((block = getBlock(x - 1, y)) && block->tile == old_tile) {
  483. todo.append(QPoint(x - 1, y));
  484. }
  485. if ((block = getBlock(x, y + 1)) && block->tile == old_tile) {
  486. todo.append(QPoint(x, y + 1));
  487. }
  488. if ((block = getBlock(x, y - 1)) && block->tile == old_tile) {
  489. todo.append(QPoint(x, y - 1));
  490. }
  491. }
  492. }
  493. void Map::_floodFillCollision(int x, int y, uint collision) {
  494. QList<QPoint> todo;
  495. todo.append(QPoint(x, y));
  496. while (todo.length()) {
  497. QPoint point = todo.takeAt(0);
  498. x = point.x();
  499. y = point.y();
  500. Block *block = getBlock(x, y);
  501. if (block == NULL) {
  502. continue;
  503. }
  504. uint old_coll = block->collision;
  505. if (old_coll == collision) {
  506. continue;
  507. }
  508. block->collision = collision;
  509. _setBlock(x, y, *block);
  510. if ((block = getBlock(x + 1, y)) && block->collision == old_coll) {
  511. todo.append(QPoint(x + 1, y));
  512. }
  513. if ((block = getBlock(x - 1, y)) && block->collision == old_coll) {
  514. todo.append(QPoint(x - 1, y));
  515. }
  516. if ((block = getBlock(x, y + 1)) && block->collision == old_coll) {
  517. todo.append(QPoint(x, y + 1));
  518. }
  519. if ((block = getBlock(x, y - 1)) && block->collision == old_coll) {
  520. todo.append(QPoint(x, y - 1));
  521. }
  522. }
  523. }
  524. void Map::_floodFillElevation(int x, int y, uint elevation) {
  525. QList<QPoint> todo;
  526. todo.append(QPoint(x, y));
  527. while (todo.length()) {
  528. QPoint point = todo.takeAt(0);
  529. x = point.x();
  530. y = point.y();
  531. Block *block = getBlock(x, y);
  532. if (block == NULL) {
  533. continue;
  534. }
  535. uint old_z = block->elevation;
  536. if (old_z == elevation) {
  537. continue;
  538. }
  539. Block block_(*block);
  540. block_.elevation = elevation;
  541. _setBlock(x, y, block_);
  542. if ((block = getBlock(x + 1, y)) && block->elevation == old_z) {
  543. todo.append(QPoint(x + 1, y));
  544. }
  545. if ((block = getBlock(x - 1, y)) && block->elevation == old_z) {
  546. todo.append(QPoint(x - 1, y));
  547. }
  548. if ((block = getBlock(x, y + 1)) && block->elevation == old_z) {
  549. todo.append(QPoint(x, y + 1));
  550. }
  551. if ((block = getBlock(x, y - 1)) && block->elevation == old_z) {
  552. todo.append(QPoint(x, y - 1));
  553. }
  554. }
  555. }
  556. void Map::_floodFillCollisionElevation(int x, int y, uint collision, uint elevation) {
  557. QList<QPoint> todo;
  558. todo.append(QPoint(x, y));
  559. while (todo.length()) {
  560. QPoint point = todo.takeAt(0);
  561. x = point.x();
  562. y = point.y();
  563. Block *block = getBlock(x, y);
  564. if (block == NULL) {
  565. continue;
  566. }
  567. uint old_coll = block->collision;
  568. uint old_elev = block->elevation;
  569. if (old_coll == collision && old_elev == elevation) {
  570. continue;
  571. }
  572. block->collision = collision;
  573. block->elevation = elevation;
  574. _setBlock(x, y, *block);
  575. if ((block = getBlock(x + 1, y)) && block->collision == old_coll && block->elevation == old_elev) {
  576. todo.append(QPoint(x + 1, y));
  577. }
  578. if ((block = getBlock(x - 1, y)) && block->collision == old_coll && block->elevation == old_elev) {
  579. todo.append(QPoint(x - 1, y));
  580. }
  581. if ((block = getBlock(x, y + 1)) && block->collision == old_coll && block->elevation == old_elev) {
  582. todo.append(QPoint(x, y + 1));
  583. }
  584. if ((block = getBlock(x, y - 1)) && block->collision == old_coll && block->elevation == old_elev) {
  585. todo.append(QPoint(x, y - 1));
  586. }
  587. }
  588. }
  589. void Map::undo() {
  590. if (blockdata) {
  591. Blockdata *commit = history.back();
  592. if (commit != NULL) {
  593. blockdata->copyFrom(commit);
  594. emit mapChanged(this);
  595. }
  596. }
  597. }
  598. void Map::redo() {
  599. if (blockdata) {
  600. Blockdata *commit = history.next();
  601. if (commit != NULL) {
  602. blockdata->copyFrom(commit);
  603. emit mapChanged(this);
  604. }
  605. }
  606. }
  607. void Map::commit() {
  608. if (blockdata) {
  609. if (!blockdata->equals(history.current())) {
  610. Blockdata* commit = blockdata->copy();
  611. history.push(commit);
  612. emit mapChanged(this);
  613. }
  614. }
  615. }
  616. void Map::setBlock(int x, int y, Block block) {
  617. Block *old_block = getBlock(x, y);
  618. if (old_block && (*old_block) != block) {
  619. _setBlock(x, y, block);
  620. commit();
  621. }
  622. }
  623. void Map::floodFill(int x, int y, uint tile) {
  624. Block *block = getBlock(x, y);
  625. if (block && block->tile != tile) {
  626. _floodFill(x, y, tile);
  627. commit();
  628. }
  629. }
  630. void Map::floodFillCollision(int x, int y, uint collision) {
  631. Block *block = getBlock(x, y);
  632. if (block && block->collision != collision) {
  633. _floodFillCollision(x, y, collision);
  634. commit();
  635. }
  636. }
  637. void Map::floodFillElevation(int x, int y, uint elevation) {
  638. Block *block = getBlock(x, y);
  639. if (block && block->elevation != elevation) {
  640. _floodFillElevation(x, y, elevation);
  641. commit();
  642. }
  643. }
  644. void Map::floodFillCollisionElevation(int x, int y, uint collision, uint elevation) {
  645. Block *block = getBlock(x, y);
  646. if (block && (block->collision != collision || block->elevation != elevation)) {
  647. _floodFillCollisionElevation(x, y, collision, elevation);
  648. commit();
  649. }
  650. }
  651. QList<Event *> Map::getAllEvents() {
  652. QList<Event*> all;
  653. for (QList<Event*> list : events.values()) {
  654. all += list;
  655. }
  656. return all;
  657. }
  658. QList<Event *> Map::getEventsByType(QString type)
  659. {
  660. return events.value(type);
  661. }
  662. void Map::removeEvent(Event *event) {
  663. for (QString key : events.keys()) {
  664. events[key].removeAll(event);
  665. }
  666. }
  667. void Map::addEvent(Event *event) {
  668. events[event->get("event_type")].append(event);
  669. }
  670. bool Map::hasUnsavedChanges() {
  671. return !history.isSaved();
  672. }