No Description

map.cpp 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. #include "map.h"
  2. #include <QTime>
  3. #include <QDebug>
  4. #include <QPainter>
  5. Map::Map(QObject *parent) : QObject(parent)
  6. {
  7. cached_blockdata = new Blockdata;
  8. cached_collision = new Blockdata;
  9. cached_border = new Blockdata;
  10. paint_tile = 1;
  11. paint_collision = 0;
  12. paint_elevation = 3;
  13. }
  14. int Map::getWidth() {
  15. return width.toInt(nullptr, 0);
  16. }
  17. int Map::getHeight() {
  18. return height.toInt(nullptr, 0);
  19. }
  20. Tileset* Map::getBlockTileset(uint metatile_index) {
  21. uint primary_size = 0x200;//tileset_primary->metatiles->length();
  22. if (metatile_index < primary_size) {
  23. return tileset_primary;
  24. } else {
  25. return tileset_secondary;
  26. }
  27. }
  28. QImage Map::getMetatileTile(uint tile) {
  29. uint primary_size = 0x200;//tileset_primary->metatiles->length();
  30. if (tile < primary_size) {
  31. return tileset_primary->tiles->value(tile);
  32. } else {
  33. return tileset_secondary->tiles->value(tile - primary_size);
  34. }
  35. }
  36. Metatile* Map::getMetatile(uint index) {
  37. uint primary_size = 0x200;//tileset_primary->metatiles->length();
  38. if (index < primary_size) {
  39. return tileset_primary->metatiles->value(index);
  40. } else {
  41. //qDebug() << QString("secondary tileset: %1").arg(index - primary_size, 0, 16);
  42. return tileset_secondary->metatiles->value(index - primary_size);
  43. }
  44. }
  45. QImage Map::getCollisionMetatileImage(Block block) {
  46. return getCollisionMetatileImage(block.collision);
  47. }
  48. QImage Map::getCollisionMetatileImage(int collision) {
  49. QImage metatile_image(16, 16, QImage::Format_RGBA8888);
  50. QColor color;
  51. if (collision == 0) {
  52. color.setGreen(0xff);
  53. } else if (collision == 1) {
  54. color.setRed(0xff);
  55. } else if (collision == 2) {
  56. color.setBlue(0xff);
  57. } else if (collision == 3) {
  58. // black
  59. }
  60. metatile_image.fill(color);
  61. return metatile_image;
  62. }
  63. QImage Map::getElevationMetatileImage(Block block) {
  64. return getElevationMetatileImage(block.elevation);
  65. }
  66. QImage Map::getElevationMetatileImage(int elevation) {
  67. QImage metatile_image(16, 16, QImage::Format_RGBA8888);
  68. QColor color;
  69. if (elevation < 15) {
  70. uint saturation = (elevation + 1) * 16 + 15;
  71. color.setGreen(saturation);
  72. color.setRed(saturation);
  73. color.setBlue(saturation);
  74. } else {
  75. color.setGreen(0xd0);
  76. color.setBlue(0xd0);
  77. color.setRed(0);
  78. }
  79. metatile_image.fill(color);
  80. //QPainter painter(&metatile_image);
  81. //painter.end();
  82. return metatile_image;
  83. }
  84. QImage Map::getMetatileImage(uint tile) {
  85. QImage metatile_image(16, 16, QImage::Format_RGBA8888);
  86. Metatile* metatile = getMetatile(tile);
  87. if (metatile == NULL) {
  88. metatile_image.fill(0xffffffff);
  89. return metatile_image;
  90. }
  91. Tileset* blockTileset = getBlockTileset(tile);
  92. for (int layer = 0; layer < 2; layer++)
  93. for (int y = 0; y < 2; y++)
  94. for (int x = 0; x < 2; x++) {
  95. //qDebug() << QString("x=%1 y=%2 layer=%3").arg(x).arg(y).arg(layer);
  96. Tile tile = metatile->tiles->value((y * 2) + x + (layer * 4));
  97. QImage tile_image = getMetatileTile(tile.tile);
  98. QList<QRgb> palette = blockTileset->palettes->value(tile.palette);
  99. for (int j = 0; j < palette.length(); j++) {
  100. tile_image.setColor(j, palette.value(j));
  101. }
  102. //QVector<QRgb> vector = palette.toVector();
  103. //tile_image.setColorTable(vector);
  104. if (layer > 0) {
  105. QColor color(tile_image.color(15));
  106. color.setAlpha(0);
  107. tile_image.setColor(15, color.rgba());
  108. }
  109. QPainter metatile_painter(&metatile_image);
  110. QPoint origin = QPoint(x*8, y*8);
  111. metatile_painter.drawImage(origin, tile_image.mirrored(tile.xflip == 1, tile.yflip == 1));
  112. metatile_painter.end();
  113. }
  114. return metatile_image;
  115. }
  116. bool Map::blockChanged(int i, Blockdata *cache) {
  117. if (cache->blocks->length() <= i) {
  118. return true;
  119. }
  120. return blockdata->blocks->value(i) != cache->blocks->value(i);
  121. }
  122. void Map::cacheBorder() {
  123. cached_border = new Blockdata;
  124. for (int i = 0; i < border->blocks->length(); i++) {
  125. Block block = border->blocks->value(i);
  126. cached_border->blocks->append(block);
  127. }
  128. }
  129. void Map::cacheBlockdata() {
  130. cached_blockdata = new Blockdata;
  131. for (int i = 0; i < blockdata->blocks->length(); i++) {
  132. Block block = blockdata->blocks->value(i);
  133. cached_blockdata->blocks->append(block);
  134. }
  135. }
  136. void Map::cacheCollision() {
  137. cached_collision = new Blockdata;
  138. for (int i = 0; i < blockdata->blocks->length(); i++) {
  139. Block block = blockdata->blocks->value(i);
  140. cached_collision->blocks->append(block);
  141. }
  142. }
  143. QPixmap Map::renderCollision() {
  144. bool changed_any = false;
  145. int width_ = getWidth();
  146. int height_ = getHeight();
  147. if (
  148. collision_image.isNull()
  149. || collision_image.width() != width_ * 16
  150. || collision_image.height() != height_ * 16
  151. ) {
  152. collision_image = QImage(width_ * 16, height_ * 16, QImage::Format_RGBA8888);
  153. changed_any = true;
  154. }
  155. QPainter painter(&collision_image);
  156. for (int i = 0; i < blockdata->blocks->length(); i++) {
  157. if (!blockChanged(i, cached_collision)) {
  158. continue;
  159. }
  160. changed_any = true;
  161. Block block = blockdata->blocks->value(i);
  162. QImage metatile_image = getMetatileImage(block.tile);
  163. QImage collision_metatile_image = getCollisionMetatileImage(block);
  164. QImage elevation_metatile_image = getElevationMetatileImage(block);
  165. int map_y = i / width_;
  166. int map_x = i % width_;
  167. QPoint metatile_origin = QPoint(map_x * 16, map_y * 16);
  168. painter.setOpacity(1);
  169. painter.drawImage(metatile_origin, metatile_image);
  170. painter.save();
  171. if (block.elevation == 15) {
  172. painter.setOpacity(0.5);
  173. } else if (block.elevation == 0) {
  174. painter.setOpacity(0);
  175. } else {
  176. painter.setOpacity(1);//(block.elevation / 16.0) * 0.8);
  177. painter.setCompositionMode(QPainter::CompositionMode_Overlay);
  178. }
  179. painter.drawImage(metatile_origin, elevation_metatile_image);
  180. painter.restore();
  181. painter.save();
  182. if (block.collision == 0) {
  183. painter.setOpacity(0.1);
  184. } else {
  185. painter.setOpacity(0.4);
  186. }
  187. painter.drawImage(metatile_origin, collision_metatile_image);
  188. painter.restore();
  189. painter.save();
  190. painter.setOpacity(0.6);
  191. painter.setPen(QColor(255, 255, 255, 192));
  192. painter.setFont(QFont("Helvetica", 8));
  193. painter.drawText(QPoint(metatile_origin.x(), metatile_origin.y() + 8), QString("%1").arg(block.elevation));
  194. painter.restore();
  195. }
  196. painter.end();
  197. cacheCollision();
  198. if (changed_any) {
  199. collision_pixmap = collision_pixmap.fromImage(collision_image);
  200. }
  201. return collision_pixmap;
  202. }
  203. QPixmap Map::render() {
  204. bool changed_any = false;
  205. int width_ = getWidth();
  206. int height_ = getHeight();
  207. if (
  208. image.isNull()
  209. || image.width() != width_ * 16
  210. || image.height() != height_ * 16
  211. ) {
  212. image = QImage(width_ * 16, height_ * 16, QImage::Format_RGBA8888);
  213. changed_any = true;
  214. }
  215. QPainter painter(&image);
  216. for (int i = 0; i < blockdata->blocks->length(); i++) {
  217. if (!blockChanged(i, cached_blockdata)) {
  218. continue;
  219. }
  220. changed_any = true;
  221. Block block = blockdata->blocks->value(i);
  222. QImage metatile_image = getMetatileImage(block.tile);
  223. int map_y = i / width_;
  224. int map_x = i % width_;
  225. QPoint metatile_origin = QPoint(map_x * 16, map_y * 16);
  226. painter.drawImage(metatile_origin, metatile_image);
  227. }
  228. painter.end();
  229. if (changed_any) {
  230. cacheBlockdata();
  231. pixmap = pixmap.fromImage(image);
  232. }
  233. return pixmap;
  234. }
  235. QPixmap Map::renderBorder() {
  236. bool changed_any = false;
  237. int width_ = 2;
  238. int height_ = 2;
  239. if (border_image.isNull()) {
  240. border_image = QImage(width_ * 16, height_ * 16, QImage::Format_RGBA8888);
  241. changed_any = true;
  242. }
  243. QPainter painter(&border_image);
  244. for (int i = 0; i < border->blocks->length(); i++) {
  245. if (!blockChanged(i, cached_border)) {
  246. continue;
  247. }
  248. changed_any = true;
  249. Block block = border->blocks->value(i);
  250. QImage metatile_image = getMetatileImage(block.tile);
  251. int map_y = i / width_;
  252. int map_x = i % width_;
  253. painter.drawImage(QPoint(map_x * 16, map_y * 16), metatile_image);
  254. }
  255. painter.end();
  256. if (changed_any) {
  257. cacheBorder();
  258. border_pixmap = border_pixmap.fromImage(border_image);
  259. }
  260. return border_pixmap;
  261. }
  262. QPixmap Map::renderConnection(Connection connection) {
  263. render();
  264. int x, y, w, h;
  265. if (connection.direction == "up") {
  266. x = 0;
  267. y = getHeight() - 6;
  268. w = getWidth();
  269. h = 6;
  270. } else if (connection.direction == "down") {
  271. x = 0;
  272. y = 0;
  273. w = getWidth();
  274. h = 6;
  275. } else if (connection.direction == "left") {
  276. x = getWidth() - 6;
  277. y = 0;
  278. w = 6;
  279. h = getHeight();
  280. } else if (connection.direction == "right") {
  281. x = 0;
  282. y = 0;
  283. w = 6;
  284. h = getHeight();
  285. } else {
  286. // this should not happen
  287. x = 0;
  288. y = 0;
  289. w = getWidth();
  290. h = getHeight();
  291. }
  292. QImage connection_image = image.copy(x * 16, y * 16, w * 16, h * 16);
  293. //connection_image = connection_image.convertToFormat(QImage::Format_Grayscale8);
  294. return QPixmap::fromImage(connection_image);
  295. }
  296. QPixmap Map::renderCollisionMetatiles() {
  297. int length_ = 4;
  298. int height_ = 1;
  299. int width_ = length_ / height_;
  300. QImage image(width_ * 16, height_ * 16, QImage::Format_RGBA8888);
  301. QPainter painter(&image);
  302. for (int i = 0; i < length_; i++) {
  303. int y = i / width_;
  304. int x = i % width_;
  305. QPoint origin(x * 16, y * 16);
  306. QImage metatile_image = getCollisionMetatileImage(i);
  307. painter.drawImage(origin, metatile_image);
  308. }
  309. drawSelection(paint_collision, width_, &painter);
  310. painter.end();
  311. return QPixmap::fromImage(image);
  312. }
  313. QPixmap Map::renderElevationMetatiles() {
  314. int length_ = 16;
  315. int height_ = 2;
  316. int width_ = length_ / height_;
  317. QImage image(width_ * 16, height_ * 16, QImage::Format_RGBA8888);
  318. QPainter painter(&image);
  319. for (int i = 0; i < length_; i++) {
  320. int y = i / width_;
  321. int x = i % width_;
  322. QPoint origin(x * 16, y * 16);
  323. QImage metatile_image = getElevationMetatileImage(i);
  324. painter.drawImage(origin, metatile_image);
  325. }
  326. drawSelection(paint_elevation, width_, &painter);
  327. painter.end();
  328. return QPixmap::fromImage(image);
  329. }
  330. void Map::drawSelection(int i, int w, QPainter *painter) {
  331. int x = i % w;
  332. int y = i / w;
  333. painter->save();
  334. painter->setPen(QColor(0xff, 0xff, 0xff));
  335. painter->drawRect(x * 16, y * 16, 15, 15);
  336. painter->setPen(QColor(0, 0, 0));
  337. painter->drawRect(x * 16 - 1, y * 16 - 1, 17, 17);
  338. painter->drawRect(x * 16 + 1, y * 16 + 1, 13, 13);
  339. painter->restore();
  340. }
  341. QPixmap Map::renderMetatiles() {
  342. int primary_length = tileset_primary->metatiles->length();
  343. int length_ = primary_length + tileset_secondary->metatiles->length();
  344. int width_ = 8;
  345. int height_ = length_ / width_;
  346. QImage image(width_ * 16, height_ * 16, QImage::Format_RGBA8888);
  347. QPainter painter(&image);
  348. for (int i = 0; i < length_; i++) {
  349. uint tile = i;
  350. if (i >= primary_length) {
  351. tile += 0x200 - primary_length;
  352. }
  353. QImage metatile_image = getMetatileImage(tile);
  354. int map_y = i / width_;
  355. int map_x = i % width_;
  356. QPoint metatile_origin = QPoint(map_x * 16, map_y * 16);
  357. painter.drawImage(metatile_origin, metatile_image);
  358. }
  359. drawSelection(paint_tile, width_, &painter);
  360. painter.end();
  361. return QPixmap::fromImage(image);
  362. }
  363. Block* Map::getBlock(int x, int y) {
  364. if (x >= 0 && x < getWidth())
  365. if (y >= 0 && y < getHeight()) {
  366. int i = y * getWidth() + x;
  367. return new Block(blockdata->blocks->value(i));
  368. }
  369. return NULL;
  370. }
  371. void Map::_setBlock(int x, int y, Block block) {
  372. int i = y * getWidth() + x;
  373. blockdata->blocks->replace(i, block);
  374. }
  375. void Map::_floodFill(int x, int y, uint tile) {
  376. QList<QPoint> todo;
  377. todo.append(QPoint(x, y));
  378. while (todo.length()) {
  379. QPoint point = todo.takeAt(0);
  380. x = point.x();
  381. y = point.y();
  382. Block *block = getBlock(x, y);
  383. if (block == NULL) {
  384. continue;
  385. }
  386. uint old_tile = block->tile;
  387. if (old_tile == tile) {
  388. continue;
  389. }
  390. block->tile = tile;
  391. _setBlock(x, y, *block);
  392. if ((block = getBlock(x + 1, y)) && block->tile == old_tile) {
  393. todo.append(QPoint(x + 1, y));
  394. }
  395. if ((block = getBlock(x - 1, y)) && block->tile == old_tile) {
  396. todo.append(QPoint(x - 1, y));
  397. }
  398. if ((block = getBlock(x, y + 1)) && block->tile == old_tile) {
  399. todo.append(QPoint(x, y + 1));
  400. }
  401. if ((block = getBlock(x, y - 1)) && block->tile == old_tile) {
  402. todo.append(QPoint(x, y - 1));
  403. }
  404. }
  405. }
  406. void Map::_floodFillCollision(int x, int y, uint collision) {
  407. QList<QPoint> todo;
  408. todo.append(QPoint(x, y));
  409. while (todo.length()) {
  410. QPoint point = todo.takeAt(0);
  411. x = point.x();
  412. y = point.y();
  413. Block *block = getBlock(x, y);
  414. if (block == NULL) {
  415. continue;
  416. }
  417. uint old_coll = block->collision;
  418. if (old_coll == collision) {
  419. continue;
  420. }
  421. block->collision = collision;
  422. _setBlock(x, y, *block);
  423. if ((block = getBlock(x + 1, y)) && block->collision == old_coll) {
  424. todo.append(QPoint(x + 1, y));
  425. }
  426. if ((block = getBlock(x - 1, y)) && block->collision == old_coll) {
  427. todo.append(QPoint(x - 1, y));
  428. }
  429. if ((block = getBlock(x, y + 1)) && block->collision == old_coll) {
  430. todo.append(QPoint(x, y + 1));
  431. }
  432. if ((block = getBlock(x, y - 1)) && block->collision == old_coll) {
  433. todo.append(QPoint(x, y - 1));
  434. }
  435. }
  436. }
  437. void Map::_floodFillElevation(int x, int y, uint elevation) {
  438. QList<QPoint> todo;
  439. todo.append(QPoint(x, y));
  440. while (todo.length()) {
  441. QPoint point = todo.takeAt(0);
  442. x = point.x();
  443. y = point.y();
  444. Block *block = getBlock(x, y);
  445. if (block == NULL) {
  446. continue;
  447. }
  448. uint old_z = block->elevation;
  449. if (old_z == elevation) {
  450. continue;
  451. }
  452. Block block_(*block);
  453. block_.elevation = elevation;
  454. _setBlock(x, y, block_);
  455. if ((block = getBlock(x + 1, y)) && block->elevation == old_z) {
  456. todo.append(QPoint(x + 1, y));
  457. }
  458. if ((block = getBlock(x - 1, y)) && block->elevation == old_z) {
  459. todo.append(QPoint(x - 1, y));
  460. }
  461. if ((block = getBlock(x, y + 1)) && block->elevation == old_z) {
  462. todo.append(QPoint(x, y + 1));
  463. }
  464. if ((block = getBlock(x, y - 1)) && block->elevation == old_z) {
  465. todo.append(QPoint(x, y - 1));
  466. }
  467. }
  468. }
  469. void Map::_floodFillCollisionElevation(int x, int y, uint collision, uint elevation) {
  470. QList<QPoint> todo;
  471. todo.append(QPoint(x, y));
  472. while (todo.length()) {
  473. QPoint point = todo.takeAt(0);
  474. x = point.x();
  475. y = point.y();
  476. Block *block = getBlock(x, y);
  477. if (block == NULL) {
  478. continue;
  479. }
  480. uint old_coll = block->collision;
  481. uint old_elev = block->elevation;
  482. if (old_coll == collision && old_elev == elevation) {
  483. continue;
  484. }
  485. block->collision = collision;
  486. block->elevation = elevation;
  487. _setBlock(x, y, *block);
  488. if ((block = getBlock(x + 1, y)) && block->collision == old_coll && block->elevation == old_elev) {
  489. todo.append(QPoint(x + 1, y));
  490. }
  491. if ((block = getBlock(x - 1, y)) && block->collision == old_coll && block->elevation == old_elev) {
  492. todo.append(QPoint(x - 1, y));
  493. }
  494. if ((block = getBlock(x, y + 1)) && block->collision == old_coll && block->elevation == old_elev) {
  495. todo.append(QPoint(x, y + 1));
  496. }
  497. if ((block = getBlock(x, y - 1)) && block->collision == old_coll && block->elevation == old_elev) {
  498. todo.append(QPoint(x, y - 1));
  499. }
  500. }
  501. }
  502. void Map::undo() {
  503. Blockdata *commit = history.pop();
  504. if (commit != NULL) {
  505. blockdata->copyFrom(commit);
  506. }
  507. }
  508. void Map::redo() {
  509. Blockdata *commit = history.next();
  510. if (commit != NULL) {
  511. blockdata->copyFrom(commit);
  512. }
  513. }
  514. void Map::commit() {
  515. Blockdata* commit = blockdata->copy();
  516. history.push(commit);
  517. }
  518. void Map::setBlock(int x, int y, Block block) {
  519. Block *old_block = getBlock(x, y);
  520. if (old_block && (*old_block) != block) {
  521. _setBlock(x, y, block);
  522. commit();
  523. }
  524. }
  525. void Map::floodFill(int x, int y, uint tile) {
  526. Block *block = getBlock(x, y);
  527. if (block && block->tile != tile) {
  528. _floodFill(x, y, tile);
  529. commit();
  530. }
  531. }
  532. void Map::floodFillCollision(int x, int y, uint collision) {
  533. Block *block = getBlock(x, y);
  534. if (block && block->collision != collision) {
  535. _floodFillCollision(x, y, collision);
  536. commit();
  537. }
  538. }
  539. void Map::floodFillElevation(int x, int y, uint elevation) {
  540. Block *block = getBlock(x, y);
  541. if (block && block->elevation != elevation) {
  542. _floodFillElevation(x, y, elevation);
  543. commit();
  544. }
  545. }
  546. void Map::floodFillCollisionElevation(int x, int y, uint collision, uint elevation) {
  547. Block *block = getBlock(x, y);
  548. if (block && (block->collision != collision || block->elevation != elevation)) {
  549. _floodFillCollisionElevation(x, y, collision, elevation);
  550. commit();
  551. }
  552. }