No Description

map.cpp 25KB

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