tile_map_layer.cpp 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886
  1. /**************************************************************************/
  2. /* tile_map_layer.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "tile_map_layer.h"
  31. #include "core/core_string_names.h"
  32. #include "core/io/marshalls.h"
  33. #include "scene/gui/control.h"
  34. #include "scene/resources/world_2d.h"
  35. #include "servers/navigation_server_2d.h"
  36. #ifdef DEBUG_ENABLED
  37. #include "servers/navigation_server_3d.h"
  38. #endif // DEBUG_ENABLED
  39. #ifdef DEBUG_ENABLED
  40. /////////////////////////////// Debug //////////////////////////////////////////
  41. constexpr int TILE_MAP_DEBUG_QUADRANT_SIZE = 16;
  42. Vector2i TileMapLayer::_coords_to_debug_quadrant_coords(const Vector2i &p_coords) const {
  43. return Vector2i(
  44. p_coords.x > 0 ? p_coords.x / TILE_MAP_DEBUG_QUADRANT_SIZE : (p_coords.x - (TILE_MAP_DEBUG_QUADRANT_SIZE - 1)) / TILE_MAP_DEBUG_QUADRANT_SIZE,
  45. p_coords.y > 0 ? p_coords.y / TILE_MAP_DEBUG_QUADRANT_SIZE : (p_coords.y - (TILE_MAP_DEBUG_QUADRANT_SIZE - 1)) / TILE_MAP_DEBUG_QUADRANT_SIZE);
  46. }
  47. void TileMapLayer::_debug_update() {
  48. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  49. RenderingServer *rs = RenderingServer::get_singleton();
  50. // Check if we should cleanup everything.
  51. bool forced_cleanup = in_destructor || !enabled || !tile_map_node->is_inside_tree() || !tile_set.is_valid() || !tile_map_node->is_visible_in_tree();
  52. if (forced_cleanup) {
  53. for (KeyValue<Vector2i, Ref<DebugQuadrant>> &kv : debug_quadrant_map) {
  54. // Free the quadrant.
  55. Ref<DebugQuadrant> &debug_quadrant = kv.value;
  56. if (debug_quadrant->canvas_item.is_valid()) {
  57. rs->free(debug_quadrant->canvas_item);
  58. }
  59. }
  60. debug_quadrant_map.clear();
  61. _debug_was_cleaned_up = true;
  62. return;
  63. }
  64. // Check if anything is dirty, in such a case, redraw debug.
  65. bool anything_changed = false;
  66. for (int i = 0; i < DIRTY_FLAGS_MAX; i++) {
  67. if (dirty.flags[i]) {
  68. anything_changed = true;
  69. break;
  70. }
  71. }
  72. // List all debug quadrants to update, creating new ones if needed.
  73. SelfList<DebugQuadrant>::List dirty_debug_quadrant_list;
  74. if (_debug_was_cleaned_up || anything_changed) {
  75. // Update all cells.
  76. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  77. CellData &cell_data = kv.value;
  78. _debug_quadrants_update_cell(cell_data, dirty_debug_quadrant_list);
  79. }
  80. } else {
  81. // Update dirty cells.
  82. for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  83. CellData &cell_data = *cell_data_list_element->self();
  84. _debug_quadrants_update_cell(cell_data, dirty_debug_quadrant_list);
  85. }
  86. }
  87. // Update those quadrants.
  88. for (SelfList<DebugQuadrant> *quadrant_list_element = dirty_debug_quadrant_list.first(); quadrant_list_element;) {
  89. SelfList<DebugQuadrant> *next_quadrant_list_element = quadrant_list_element->next(); // "Hack" to clear the list while iterating.
  90. DebugQuadrant &debug_quadrant = *quadrant_list_element->self();
  91. // Check if the quadrant has a tile.
  92. bool has_a_tile = false;
  93. RID &ci = debug_quadrant.canvas_item;
  94. for (SelfList<CellData> *cell_data_list_element = debug_quadrant.cells.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  95. CellData &cell_data = *cell_data_list_element->self();
  96. if (cell_data.cell.source_id != TileSet::INVALID_SOURCE) {
  97. has_a_tile = true;
  98. break;
  99. }
  100. }
  101. if (has_a_tile) {
  102. // Update the quadrant.
  103. if (ci.is_valid()) {
  104. rs->canvas_item_clear(ci);
  105. } else {
  106. ci = rs->canvas_item_create();
  107. rs->canvas_item_set_z_index(ci, RS::CANVAS_ITEM_Z_MAX - 1);
  108. rs->canvas_item_set_parent(ci, tile_map_node->get_canvas_item());
  109. }
  110. const Vector2 quadrant_pos = tile_map_node->map_to_local(debug_quadrant.quadrant_coords * TILE_MAP_DEBUG_QUADRANT_SIZE);
  111. Transform2D xform(0, quadrant_pos);
  112. rs->canvas_item_set_transform(ci, xform);
  113. for (SelfList<CellData> *cell_data_list_element = debug_quadrant.cells.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  114. CellData &cell_data = *cell_data_list_element->self();
  115. if (cell_data.cell.source_id != TileSet::INVALID_SOURCE) {
  116. _rendering_draw_cell_debug(ci, quadrant_pos, cell_data);
  117. _physics_draw_cell_debug(ci, quadrant_pos, cell_data);
  118. _navigation_draw_cell_debug(ci, quadrant_pos, cell_data);
  119. _scenes_draw_cell_debug(ci, quadrant_pos, cell_data);
  120. }
  121. }
  122. } else {
  123. // Free the quadrant.
  124. if (ci.is_valid()) {
  125. rs->free(ci);
  126. }
  127. quadrant_list_element->remove_from_list();
  128. debug_quadrant_map.erase(debug_quadrant.quadrant_coords);
  129. }
  130. quadrant_list_element = next_quadrant_list_element;
  131. }
  132. dirty_debug_quadrant_list.clear();
  133. _debug_was_cleaned_up = false;
  134. }
  135. void TileMapLayer::_debug_quadrants_update_cell(CellData &r_cell_data, SelfList<DebugQuadrant>::List &r_dirty_debug_quadrant_list) {
  136. Vector2i quadrant_coords = _coords_to_debug_quadrant_coords(r_cell_data.coords);
  137. if (!debug_quadrant_map.has(quadrant_coords)) {
  138. // Create a new quadrant and add it to the quadrant map.
  139. Ref<DebugQuadrant> new_quadrant;
  140. new_quadrant.instantiate();
  141. new_quadrant->quadrant_coords = quadrant_coords;
  142. debug_quadrant_map[quadrant_coords] = new_quadrant;
  143. }
  144. // Add the cell to its quadrant, if it is not already in there.
  145. Ref<DebugQuadrant> &debug_quadrant = debug_quadrant_map[quadrant_coords];
  146. if (!r_cell_data.debug_quadrant_list_element.in_list()) {
  147. debug_quadrant->cells.add(&r_cell_data.debug_quadrant_list_element);
  148. }
  149. // Mark the quadrant as dirty.
  150. if (!debug_quadrant->dirty_quadrant_list_element.in_list()) {
  151. r_dirty_debug_quadrant_list.add(&debug_quadrant->dirty_quadrant_list_element);
  152. }
  153. }
  154. #endif // DEBUG_ENABLED
  155. /////////////////////////////// Rendering //////////////////////////////////////
  156. void TileMapLayer::_rendering_update() {
  157. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  158. RenderingServer *rs = RenderingServer::get_singleton();
  159. // Check if we should cleanup everything.
  160. bool forced_cleanup = in_destructor || !enabled || !tile_map_node->is_inside_tree() || !tile_set.is_valid() || !tile_map_node->is_visible_in_tree();
  161. // ----------- Layer level processing -----------
  162. if (forced_cleanup) {
  163. // Cleanup.
  164. if (canvas_item.is_valid()) {
  165. rs->free(canvas_item);
  166. canvas_item = RID();
  167. }
  168. } else {
  169. // Create/Update the layer's CanvasItem.
  170. if (!canvas_item.is_valid()) {
  171. RID ci = rs->canvas_item_create();
  172. rs->canvas_item_set_parent(ci, tile_map_node->get_canvas_item());
  173. canvas_item = ci;
  174. }
  175. RID &ci = canvas_item;
  176. rs->canvas_item_set_draw_index(ci, layer_index_in_tile_map_node - (int64_t)0x80000000);
  177. rs->canvas_item_set_sort_children_by_y(ci, y_sort_enabled);
  178. rs->canvas_item_set_use_parent_material(ci, tile_map_node->get_use_parent_material() || tile_map_node->get_material().is_valid());
  179. rs->canvas_item_set_z_index(ci, z_index);
  180. rs->canvas_item_set_default_texture_filter(ci, RS::CanvasItemTextureFilter(tile_map_node->get_texture_filter_in_tree()));
  181. rs->canvas_item_set_default_texture_repeat(ci, RS::CanvasItemTextureRepeat(tile_map_node->get_texture_repeat_in_tree()));
  182. rs->canvas_item_set_light_mask(ci, tile_map_node->get_light_mask());
  183. // Modulate the layer.
  184. Color layer_modulate = modulate;
  185. int selected_layer = tile_map_node->get_selected_layer();
  186. if (selected_layer >= 0 && layer_index_in_tile_map_node != selected_layer) {
  187. int z_selected = tile_map_node->get_layer_z_index(selected_layer);
  188. if (z_index < z_selected || (z_index == z_selected && layer_index_in_tile_map_node < selected_layer)) {
  189. layer_modulate = layer_modulate.darkened(0.5);
  190. } else if (z_index > z_selected || (z_index == z_selected && layer_index_in_tile_map_node > selected_layer)) {
  191. layer_modulate = layer_modulate.darkened(0.5);
  192. layer_modulate.a *= 0.3;
  193. }
  194. }
  195. rs->canvas_item_set_modulate(ci, layer_modulate);
  196. }
  197. // ----------- Quadrants processing -----------
  198. // List all rendering quadrants to update, creating new ones if needed.
  199. SelfList<RenderingQuadrant>::List dirty_rendering_quadrant_list;
  200. // Check if anything changed that might change the quadrant shape.
  201. // If so, recreate everything.
  202. bool quandrant_shape_changed = dirty.flags[DIRTY_FLAGS_TILE_MAP_QUADRANT_SIZE] ||
  203. (tile_map_node->is_y_sort_enabled() && y_sort_enabled && (dirty.flags[DIRTY_FLAGS_LAYER_Y_SORT_ENABLED] || dirty.flags[DIRTY_FLAGS_LAYER_Y_SORT_ORIGIN] || dirty.flags[DIRTY_FLAGS_TILE_MAP_Y_SORT_ENABLED] || dirty.flags[DIRTY_FLAGS_TILE_MAP_LOCAL_XFORM] || dirty.flags[DIRTY_FLAGS_TILE_MAP_TILE_SET]));
  204. // Free all quadrants.
  205. if (forced_cleanup || quandrant_shape_changed) {
  206. for (const KeyValue<Vector2i, Ref<RenderingQuadrant>> &kv : rendering_quadrant_map) {
  207. for (int i = 0; i < kv.value->canvas_items.size(); i++) {
  208. const RID &ci = kv.value->canvas_items[i];
  209. if (ci.is_valid()) {
  210. rs->free(ci);
  211. }
  212. }
  213. kv.value->cells.clear();
  214. }
  215. rendering_quadrant_map.clear();
  216. _rendering_was_cleaned_up = true;
  217. }
  218. if (!forced_cleanup) {
  219. // List all quadrants to update, recreating them if needed.
  220. if (dirty.flags[DIRTY_FLAGS_TILE_MAP_TILE_SET] || _rendering_was_cleaned_up) {
  221. // Update all cells.
  222. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  223. CellData &cell_data = kv.value;
  224. _rendering_quadrants_update_cell(cell_data, dirty_rendering_quadrant_list);
  225. }
  226. } else {
  227. // Update dirty cells.
  228. for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  229. CellData &cell_data = *cell_data_list_element->self();
  230. _rendering_quadrants_update_cell(cell_data, dirty_rendering_quadrant_list);
  231. }
  232. }
  233. // Update all dirty quadrants.
  234. for (SelfList<RenderingQuadrant> *quadrant_list_element = dirty_rendering_quadrant_list.first(); quadrant_list_element;) {
  235. SelfList<RenderingQuadrant> *next_quadrant_list_element = quadrant_list_element->next(); // "Hack" to clear the list while iterating.
  236. const Ref<RenderingQuadrant> &rendering_quadrant = quadrant_list_element->self();
  237. // Check if the quadrant has a tile.
  238. bool has_a_tile = false;
  239. for (SelfList<CellData> *cell_data_list_element = rendering_quadrant->cells.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  240. CellData &cell_data = *cell_data_list_element->self();
  241. if (cell_data.cell.source_id != TileSet::INVALID_SOURCE) {
  242. has_a_tile = true;
  243. break;
  244. }
  245. }
  246. if (has_a_tile) {
  247. // Process the quadrant.
  248. // First, clear the quadrant's canvas items.
  249. for (RID &ci : rendering_quadrant->canvas_items) {
  250. rs->free(ci);
  251. }
  252. rendering_quadrant->canvas_items.clear();
  253. // Sort the quadrant cells.
  254. if (tile_map_node->is_y_sort_enabled() && is_y_sort_enabled()) {
  255. // For compatibility reasons, we use another comparator for Y-sorted layers.
  256. rendering_quadrant->cells.sort_custom<CellDataYSortedComparator>();
  257. } else {
  258. rendering_quadrant->cells.sort();
  259. }
  260. // Those allow to group cell per material or z-index.
  261. Ref<Material> prev_material;
  262. int prev_z_index = 0;
  263. RID prev_ci;
  264. for (SelfList<CellData> *cell_data_quadrant_list_element = rendering_quadrant->cells.first(); cell_data_quadrant_list_element; cell_data_quadrant_list_element = cell_data_quadrant_list_element->next()) {
  265. CellData &cell_data = *cell_data_quadrant_list_element->self();
  266. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(*tile_set->get_source(cell_data.cell.source_id));
  267. // Get the tile data.
  268. const TileData *tile_data;
  269. if (cell_data.runtime_tile_data_cache) {
  270. tile_data = cell_data.runtime_tile_data_cache;
  271. } else {
  272. tile_data = atlas_source->get_tile_data(cell_data.cell.get_atlas_coords(), cell_data.cell.alternative_tile);
  273. }
  274. Ref<Material> mat = tile_data->get_material();
  275. int tile_z_index = tile_data->get_z_index();
  276. // Quandrant pos.
  277. // --- CanvasItems ---
  278. RID ci;
  279. // Check if the material or the z_index changed.
  280. if (prev_ci == RID() || prev_material != mat || prev_z_index != tile_z_index) {
  281. // If so, create a new CanvasItem.
  282. ci = rs->canvas_item_create();
  283. if (mat.is_valid()) {
  284. rs->canvas_item_set_material(ci, mat->get_rid());
  285. }
  286. rs->canvas_item_set_parent(ci, canvas_item);
  287. rs->canvas_item_set_use_parent_material(ci, tile_map_node->get_use_parent_material() || tile_map_node->get_material().is_valid());
  288. Transform2D xform(0, rendering_quadrant->canvas_items_position);
  289. rs->canvas_item_set_transform(ci, xform);
  290. rs->canvas_item_set_light_mask(ci, tile_map_node->get_light_mask());
  291. rs->canvas_item_set_z_as_relative_to_parent(ci, true);
  292. rs->canvas_item_set_z_index(ci, tile_z_index);
  293. rs->canvas_item_set_default_texture_filter(ci, RS::CanvasItemTextureFilter(tile_map_node->get_texture_filter_in_tree()));
  294. rs->canvas_item_set_default_texture_repeat(ci, RS::CanvasItemTextureRepeat(tile_map_node->get_texture_repeat_in_tree()));
  295. rendering_quadrant->canvas_items.push_back(ci);
  296. prev_ci = ci;
  297. prev_material = mat;
  298. prev_z_index = tile_z_index;
  299. } else {
  300. // Keep the same canvas_item to draw on.
  301. ci = prev_ci;
  302. }
  303. const Vector2 local_tile_pos = tile_map_node->map_to_local(cell_data.coords);
  304. // Random animation offset.
  305. real_t random_animation_offset = 0.0;
  306. if (atlas_source->get_tile_animation_mode(cell_data.cell.get_atlas_coords()) != TileSetAtlasSource::TILE_ANIMATION_MODE_DEFAULT) {
  307. Array to_hash;
  308. to_hash.push_back(local_tile_pos);
  309. to_hash.push_back(get_instance_id()); // Use instance id as a random hash
  310. random_animation_offset = RandomPCG(to_hash.hash()).randf();
  311. }
  312. // Drawing the tile in the canvas item.
  313. tile_map_node->draw_tile(ci, local_tile_pos - rendering_quadrant->canvas_items_position, tile_set, cell_data.cell.source_id, cell_data.cell.get_atlas_coords(), cell_data.cell.alternative_tile, -1, tile_map_node->get_self_modulate(), tile_data, random_animation_offset);
  314. }
  315. } else {
  316. // Free the quadrant.
  317. for (int i = 0; i < rendering_quadrant->canvas_items.size(); i++) {
  318. const RID &ci = rendering_quadrant->canvas_items[i];
  319. if (ci.is_valid()) {
  320. rs->free(ci);
  321. }
  322. }
  323. rendering_quadrant->cells.clear();
  324. rendering_quadrant_map.erase(rendering_quadrant->quadrant_coords);
  325. }
  326. quadrant_list_element = next_quadrant_list_element;
  327. }
  328. dirty_rendering_quadrant_list.clear();
  329. // Reset the drawing indices.
  330. {
  331. int index = -(int64_t)0x80000000; // Always must be drawn below children.
  332. // Sort the quadrants coords per local coordinates.
  333. RBMap<Vector2, Ref<RenderingQuadrant>, RenderingQuadrant::CoordsWorldComparator> local_to_map;
  334. for (KeyValue<Vector2i, Ref<RenderingQuadrant>> &kv : rendering_quadrant_map) {
  335. Ref<RenderingQuadrant> &rendering_quadrant = kv.value;
  336. local_to_map[tile_map_node->map_to_local(rendering_quadrant->quadrant_coords)] = rendering_quadrant;
  337. }
  338. // Sort the quadrants.
  339. for (const KeyValue<Vector2, Ref<RenderingQuadrant>> &E : local_to_map) {
  340. for (const RID &ci : E.value->canvas_items) {
  341. RS::get_singleton()->canvas_item_set_draw_index(ci, index++);
  342. }
  343. }
  344. }
  345. // Updates on TileMap changes.
  346. if (dirty.flags[DIRTY_FLAGS_TILE_MAP_LIGHT_MASK] ||
  347. dirty.flags[DIRTY_FLAGS_TILE_MAP_USE_PARENT_MATERIAL] ||
  348. dirty.flags[DIRTY_FLAGS_TILE_MAP_MATERIAL] ||
  349. dirty.flags[DIRTY_FLAGS_TILE_MAP_TEXTURE_FILTER] ||
  350. dirty.flags[DIRTY_FLAGS_TILE_MAP_TEXTURE_REPEAT]) {
  351. for (KeyValue<Vector2i, Ref<RenderingQuadrant>> &kv : rendering_quadrant_map) {
  352. Ref<RenderingQuadrant> &rendering_quadrant = kv.value;
  353. for (const RID &ci : rendering_quadrant->canvas_items) {
  354. rs->canvas_item_set_light_mask(ci, tile_map_node->get_light_mask());
  355. rs->canvas_item_set_use_parent_material(ci, tile_map_node->get_use_parent_material() || tile_map_node->get_material().is_valid());
  356. rs->canvas_item_set_default_texture_filter(ci, RS::CanvasItemTextureFilter(tile_map_node->get_texture_filter_in_tree()));
  357. rs->canvas_item_set_default_texture_repeat(ci, RS::CanvasItemTextureRepeat(tile_map_node->get_texture_repeat_in_tree()));
  358. }
  359. }
  360. }
  361. }
  362. // ----------- Occluders processing -----------
  363. if (forced_cleanup) {
  364. // Clean everything.
  365. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  366. _rendering_occluders_clear_cell(kv.value);
  367. }
  368. } else {
  369. if (_rendering_was_cleaned_up || dirty.flags[DIRTY_FLAGS_TILE_MAP_TILE_SET]) {
  370. // Update all cells.
  371. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  372. _rendering_occluders_update_cell(kv.value);
  373. }
  374. } else {
  375. // Update dirty cells.
  376. for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  377. CellData &cell_data = *cell_data_list_element->self();
  378. _rendering_occluders_update_cell(cell_data);
  379. }
  380. }
  381. // Updates on TileMap changes.
  382. if (dirty.flags[DIRTY_FLAGS_TILE_MAP_IN_CANVAS] || dirty.flags[DIRTY_FLAGS_TILE_MAP_VISIBILITY]) {
  383. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  384. CellData &cell_data = kv.value;
  385. for (const RID &occluder : cell_data.occluders) {
  386. if (occluder.is_null()) {
  387. continue;
  388. }
  389. Transform2D xform(0, tile_map_node->map_to_local(kv.key));
  390. rs->canvas_light_occluder_attach_to_canvas(occluder, tile_map_node->get_canvas());
  391. rs->canvas_light_occluder_set_transform(occluder, tile_map_node->get_global_transform() * xform);
  392. }
  393. }
  394. }
  395. }
  396. // -----------
  397. // Mark the rendering state as up to date.
  398. _rendering_was_cleaned_up = forced_cleanup;
  399. }
  400. void TileMapLayer::_rendering_quadrants_update_cell(CellData &r_cell_data, SelfList<RenderingQuadrant>::List &r_dirty_rendering_quadrant_list) {
  401. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  402. // Check if the cell is valid and retrieve its y_sort_origin.
  403. bool is_valid = false;
  404. int tile_y_sort_origin = 0;
  405. TileSetSource *source;
  406. if (tile_set->has_source(r_cell_data.cell.source_id)) {
  407. source = *tile_set->get_source(r_cell_data.cell.source_id);
  408. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  409. if (atlas_source && atlas_source->has_tile(r_cell_data.cell.get_atlas_coords()) && atlas_source->has_alternative_tile(r_cell_data.cell.get_atlas_coords(), r_cell_data.cell.alternative_tile)) {
  410. is_valid = true;
  411. const TileData *tile_data;
  412. if (r_cell_data.runtime_tile_data_cache) {
  413. tile_data = r_cell_data.runtime_tile_data_cache;
  414. } else {
  415. tile_data = atlas_source->get_tile_data(r_cell_data.cell.get_atlas_coords(), r_cell_data.cell.alternative_tile);
  416. }
  417. tile_y_sort_origin = tile_data->get_y_sort_origin();
  418. }
  419. }
  420. if (is_valid) {
  421. // Get the quadrant coords.
  422. Vector2 canvas_items_position;
  423. Vector2i quadrant_coords;
  424. if (tile_map_node->is_y_sort_enabled() && is_y_sort_enabled()) {
  425. canvas_items_position = Vector2(0, tile_map_node->map_to_local(r_cell_data.coords).y + tile_y_sort_origin + y_sort_origin);
  426. quadrant_coords = canvas_items_position * 100;
  427. } else {
  428. int quad_size = tile_map_node->get_rendering_quadrant_size();
  429. const Vector2i &coords = r_cell_data.coords;
  430. // Rounding down, instead of simply rounding towards zero (truncating).
  431. quadrant_coords = Vector2i(
  432. coords.x > 0 ? coords.x / quad_size : (coords.x - (quad_size - 1)) / quad_size,
  433. coords.y > 0 ? coords.y / quad_size : (coords.y - (quad_size - 1)) / quad_size);
  434. canvas_items_position = quad_size * quadrant_coords;
  435. }
  436. Ref<RenderingQuadrant> rendering_quadrant;
  437. if (rendering_quadrant_map.has(quadrant_coords)) {
  438. // Reuse existing rendering quadrant.
  439. rendering_quadrant = rendering_quadrant_map[quadrant_coords];
  440. } else {
  441. // Create a new rendering quadrant.
  442. rendering_quadrant.instantiate();
  443. rendering_quadrant->quadrant_coords = quadrant_coords;
  444. rendering_quadrant->canvas_items_position = canvas_items_position;
  445. rendering_quadrant_map[quadrant_coords] = rendering_quadrant;
  446. }
  447. // Mark the old quadrant as dirty (if it exists).
  448. if (r_cell_data.rendering_quadrant.is_valid()) {
  449. if (!r_cell_data.rendering_quadrant->dirty_quadrant_list_element.in_list()) {
  450. r_dirty_rendering_quadrant_list.add(&r_cell_data.rendering_quadrant->dirty_quadrant_list_element);
  451. }
  452. }
  453. // Remove the cell from that quadrant.
  454. if (r_cell_data.rendering_quadrant_list_element.in_list()) {
  455. r_cell_data.rendering_quadrant_list_element.remove_from_list();
  456. }
  457. // Add the cell to its new quadrant.
  458. r_cell_data.rendering_quadrant = rendering_quadrant;
  459. r_cell_data.rendering_quadrant->cells.add(&r_cell_data.rendering_quadrant_list_element);
  460. // Add the new quadrant to the dirty quadrant list.
  461. if (!rendering_quadrant->dirty_quadrant_list_element.in_list()) {
  462. r_dirty_rendering_quadrant_list.add(&rendering_quadrant->dirty_quadrant_list_element);
  463. }
  464. } else {
  465. Ref<RenderingQuadrant> rendering_quadrant = r_cell_data.rendering_quadrant;
  466. // Remove the cell from its quadrant.
  467. r_cell_data.rendering_quadrant = Ref<RenderingQuadrant>();
  468. if (r_cell_data.rendering_quadrant_list_element.in_list()) {
  469. rendering_quadrant->cells.remove(&r_cell_data.rendering_quadrant_list_element);
  470. }
  471. if (rendering_quadrant.is_valid()) {
  472. // Add the quadrant to the dirty quadrant list.
  473. if (!rendering_quadrant->dirty_quadrant_list_element.in_list()) {
  474. r_dirty_rendering_quadrant_list.add(&rendering_quadrant->dirty_quadrant_list_element);
  475. }
  476. }
  477. }
  478. }
  479. void TileMapLayer::_rendering_occluders_clear_cell(CellData &r_cell_data) {
  480. RenderingServer *rs = RenderingServer::get_singleton();
  481. // Free the occluders.
  482. for (const RID &rid : r_cell_data.occluders) {
  483. rs->free(rid);
  484. }
  485. r_cell_data.occluders.clear();
  486. }
  487. void TileMapLayer::_rendering_occluders_update_cell(CellData &r_cell_data) {
  488. bool node_visible = tile_map_node->is_visible_in_tree();
  489. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  490. RenderingServer *rs = RenderingServer::get_singleton();
  491. // Free unused occluders then resize the occluders array.
  492. for (uint32_t i = tile_set->get_occlusion_layers_count(); i < r_cell_data.occluders.size(); i++) {
  493. RID occluder_id = r_cell_data.occluders[i];
  494. if (occluder_id.is_valid()) {
  495. rs->free(occluder_id);
  496. }
  497. }
  498. r_cell_data.occluders.resize(tile_set->get_occlusion_layers_count());
  499. TileSetSource *source;
  500. if (tile_set->has_source(r_cell_data.cell.source_id)) {
  501. source = *tile_set->get_source(r_cell_data.cell.source_id);
  502. if (source->has_tile(r_cell_data.cell.get_atlas_coords()) && source->has_alternative_tile(r_cell_data.cell.get_atlas_coords(), r_cell_data.cell.alternative_tile)) {
  503. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  504. if (atlas_source) {
  505. // Get the tile data.
  506. const TileData *tile_data;
  507. if (r_cell_data.runtime_tile_data_cache) {
  508. tile_data = r_cell_data.runtime_tile_data_cache;
  509. } else {
  510. tile_data = atlas_source->get_tile_data(r_cell_data.cell.get_atlas_coords(), r_cell_data.cell.alternative_tile);
  511. }
  512. // Transform flags.
  513. bool flip_h = (r_cell_data.cell.alternative_tile & TileSetAtlasSource::TRANSFORM_FLIP_H);
  514. bool flip_v = (r_cell_data.cell.alternative_tile & TileSetAtlasSource::TRANSFORM_FLIP_V);
  515. bool transpose = (r_cell_data.cell.alternative_tile & TileSetAtlasSource::TRANSFORM_TRANSPOSE);
  516. // Create, update or clear occluders.
  517. for (uint32_t occlusion_layer_index = 0; occlusion_layer_index < r_cell_data.occluders.size(); occlusion_layer_index++) {
  518. Ref<OccluderPolygon2D> occluder_polygon = tile_data->get_occluder(occlusion_layer_index);
  519. RID &occluder = r_cell_data.occluders[occlusion_layer_index];
  520. if (occluder_polygon.is_valid()) {
  521. // Create or update occluder.
  522. Transform2D xform;
  523. xform.set_origin(tile_map_node->map_to_local(r_cell_data.coords));
  524. if (!occluder.is_valid()) {
  525. occluder = rs->canvas_light_occluder_create();
  526. }
  527. rs->canvas_light_occluder_set_enabled(occluder, node_visible);
  528. rs->canvas_light_occluder_set_transform(occluder, tile_map_node->get_global_transform() * xform);
  529. rs->canvas_light_occluder_set_polygon(occluder, tile_data->get_occluder(occlusion_layer_index, flip_h, flip_v, transpose)->get_rid());
  530. rs->canvas_light_occluder_attach_to_canvas(occluder, tile_map_node->get_canvas());
  531. rs->canvas_light_occluder_set_light_mask(occluder, tile_set->get_occlusion_layer_light_mask(occlusion_layer_index));
  532. } else {
  533. // Clear occluder.
  534. if (occluder.is_valid()) {
  535. rs->free(occluder);
  536. occluder = RID();
  537. }
  538. }
  539. }
  540. return;
  541. }
  542. }
  543. }
  544. // If we did not return earlier, clear the cell.
  545. _rendering_occluders_clear_cell(r_cell_data);
  546. }
  547. #ifdef DEBUG_ENABLED
  548. void TileMapLayer::_rendering_draw_cell_debug(const RID &p_canvas_item, const Vector2i &p_quadrant_pos, const CellData &r_cell_data) {
  549. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  550. ERR_FAIL_COND(!tile_set.is_valid());
  551. if (!Engine::get_singleton()->is_editor_hint()) {
  552. return;
  553. }
  554. // Draw a placeholder for tiles needing one.
  555. RenderingServer *rs = RenderingServer::get_singleton();
  556. const TileMapCell &c = r_cell_data.cell;
  557. TileSetSource *source;
  558. if (tile_set->has_source(c.source_id)) {
  559. source = *tile_set->get_source(c.source_id);
  560. if (source->has_tile(c.get_atlas_coords()) && source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  561. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  562. if (atlas_source) {
  563. Vector2i grid_size = atlas_source->get_atlas_grid_size();
  564. if (!atlas_source->get_runtime_texture().is_valid() || c.get_atlas_coords().x >= grid_size.x || c.get_atlas_coords().y >= grid_size.y) {
  565. // Generate a random color from the hashed values of the tiles.
  566. Array to_hash;
  567. to_hash.push_back(c.source_id);
  568. to_hash.push_back(c.get_atlas_coords());
  569. to_hash.push_back(c.alternative_tile);
  570. uint32_t hash = RandomPCG(to_hash.hash()).rand();
  571. Color color;
  572. color = color.from_hsv(
  573. (float)((hash >> 24) & 0xFF) / 256.0,
  574. Math::lerp(0.5, 1.0, (float)((hash >> 16) & 0xFF) / 256.0),
  575. Math::lerp(0.5, 1.0, (float)((hash >> 8) & 0xFF) / 256.0),
  576. 0.8);
  577. // Draw a placeholder tile.
  578. Transform2D cell_to_quadrant;
  579. cell_to_quadrant.set_origin(tile_map_node->map_to_local(r_cell_data.coords) - p_quadrant_pos);
  580. rs->canvas_item_add_set_transform(p_canvas_item, cell_to_quadrant);
  581. rs->canvas_item_add_circle(p_canvas_item, Vector2(), MIN(tile_set->get_tile_size().x, tile_set->get_tile_size().y) / 4.0, color);
  582. }
  583. }
  584. }
  585. }
  586. }
  587. #endif // DEBUG_ENABLED
  588. /////////////////////////////// Physics //////////////////////////////////////
  589. void TileMapLayer::_physics_update() {
  590. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  591. // Check if we should cleanup everything.
  592. bool forced_cleanup = in_destructor || !enabled || !tile_map_node->is_inside_tree() || !tile_set.is_valid();
  593. if (forced_cleanup) {
  594. // Clean everything.
  595. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  596. _physics_clear_cell(kv.value);
  597. }
  598. } else {
  599. if (_physics_was_cleaned_up || dirty.flags[DIRTY_FLAGS_TILE_MAP_TILE_SET] || dirty.flags[DIRTY_FLAGS_TILE_MAP_COLLISION_ANIMATABLE]) {
  600. // Update all cells.
  601. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  602. _physics_update_cell(kv.value);
  603. }
  604. } else {
  605. // Update dirty cells.
  606. for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  607. CellData &cell_data = *cell_data_list_element->self();
  608. _physics_update_cell(cell_data);
  609. }
  610. }
  611. }
  612. // -----------
  613. // Mark the physics state as up to date.
  614. _physics_was_cleaned_up = forced_cleanup;
  615. }
  616. void TileMapLayer::_physics_notify_tilemap_change(TileMapLayer::DirtyFlags p_what) {
  617. Transform2D gl_transform = tile_map_node->get_global_transform();
  618. PhysicsServer2D *ps = PhysicsServer2D::get_singleton();
  619. bool in_editor = false;
  620. #ifdef TOOLS_ENABLED
  621. in_editor = Engine::get_singleton()->is_editor_hint();
  622. #endif
  623. if (p_what == DIRTY_FLAGS_TILE_MAP_XFORM) {
  624. if (tile_map_node->is_inside_tree() && (!tile_map_node->is_collision_animatable() || in_editor)) {
  625. // Move the collisison shapes along with the TileMap.
  626. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  627. const CellData &cell_data = kv.value;
  628. for (RID body : cell_data.bodies) {
  629. if (body.is_valid()) {
  630. Transform2D xform(0, tile_map_node->map_to_local(bodies_coords[body]));
  631. xform = gl_transform * xform;
  632. ps->body_set_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM, xform);
  633. }
  634. }
  635. }
  636. }
  637. } else if (p_what == DIRTY_FLAGS_TILE_MAP_LOCAL_XFORM) {
  638. // With collisions animatable, move the collisison shapes along with the TileMap only on local xform change (they are synchornized on physics tick instead).
  639. if (tile_map_node->is_inside_tree() && tile_map_node->is_collision_animatable() && !in_editor) {
  640. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  641. const CellData &cell_data = kv.value;
  642. for (RID body : cell_data.bodies) {
  643. if (body.is_valid()) {
  644. Transform2D xform(0, tile_map_node->map_to_local(bodies_coords[body]));
  645. xform = gl_transform * xform;
  646. ps->body_set_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM, xform);
  647. }
  648. }
  649. }
  650. }
  651. } else if (p_what == DIRTY_FLAGS_TILE_MAP_IN_TREE) {
  652. // Changes in the tree may cause the space to change (e.g. when reparenting to a SubViewport).
  653. if (tile_map_node->is_inside_tree()) {
  654. RID space = tile_map_node->get_world_2d()->get_space();
  655. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  656. const CellData &cell_data = kv.value;
  657. for (RID body : cell_data.bodies) {
  658. if (body.is_valid()) {
  659. ps->body_set_space(body, space);
  660. }
  661. }
  662. }
  663. }
  664. }
  665. }
  666. void TileMapLayer::_physics_clear_cell(CellData &r_cell_data) {
  667. PhysicsServer2D *ps = PhysicsServer2D::get_singleton();
  668. // Clear bodies.
  669. for (RID body : r_cell_data.bodies) {
  670. if (body.is_valid()) {
  671. bodies_coords.erase(body);
  672. ps->free(body);
  673. }
  674. }
  675. r_cell_data.bodies.clear();
  676. }
  677. void TileMapLayer::_physics_update_cell(CellData &r_cell_data) {
  678. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  679. Transform2D gl_transform = tile_map_node->get_global_transform();
  680. RID space = tile_map_node->get_world_2d()->get_space();
  681. PhysicsServer2D *ps = PhysicsServer2D::get_singleton();
  682. // Recreate bodies and shapes.
  683. TileMapCell &c = r_cell_data.cell;
  684. TileSetSource *source;
  685. if (tile_set->has_source(c.source_id)) {
  686. source = *tile_set->get_source(c.source_id);
  687. if (source->has_tile(c.get_atlas_coords()) && source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  688. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  689. if (atlas_source) {
  690. const TileData *tile_data;
  691. if (r_cell_data.runtime_tile_data_cache) {
  692. tile_data = r_cell_data.runtime_tile_data_cache;
  693. } else {
  694. tile_data = atlas_source->get_tile_data(c.get_atlas_coords(), c.alternative_tile);
  695. }
  696. // Transform flags.
  697. bool flip_h = (c.alternative_tile & TileSetAtlasSource::TRANSFORM_FLIP_H);
  698. bool flip_v = (c.alternative_tile & TileSetAtlasSource::TRANSFORM_FLIP_V);
  699. bool transpose = (c.alternative_tile & TileSetAtlasSource::TRANSFORM_TRANSPOSE);
  700. // Free unused bodies then resize the bodies array.
  701. for (uint32_t i = tile_set->get_physics_layers_count(); i < r_cell_data.bodies.size(); i++) {
  702. RID body = r_cell_data.bodies[i];
  703. if (body.is_valid()) {
  704. bodies_coords.erase(body);
  705. ps->free(body);
  706. }
  707. }
  708. r_cell_data.bodies.resize(tile_set->get_physics_layers_count());
  709. for (uint32_t tile_set_physics_layer = 0; tile_set_physics_layer < (uint32_t)tile_set->get_physics_layers_count(); tile_set_physics_layer++) {
  710. Ref<PhysicsMaterial> physics_material = tile_set->get_physics_layer_physics_material(tile_set_physics_layer);
  711. uint32_t physics_layer = tile_set->get_physics_layer_collision_layer(tile_set_physics_layer);
  712. uint32_t physics_mask = tile_set->get_physics_layer_collision_mask(tile_set_physics_layer);
  713. RID body = r_cell_data.bodies[tile_set_physics_layer];
  714. if (tile_data->get_collision_polygons_count(tile_set_physics_layer) == 0) {
  715. // No body needed, free it if it exists.
  716. if (body.is_valid()) {
  717. bodies_coords.erase(body);
  718. ps->free(body);
  719. }
  720. body = RID();
  721. } else {
  722. // Create or update the body.
  723. if (!body.is_valid()) {
  724. body = ps->body_create();
  725. }
  726. bodies_coords[body] = r_cell_data.coords;
  727. ps->body_set_mode(body, tile_map_node->is_collision_animatable() ? PhysicsServer2D::BODY_MODE_KINEMATIC : PhysicsServer2D::BODY_MODE_STATIC);
  728. ps->body_set_space(body, space);
  729. Transform2D xform;
  730. xform.set_origin(tile_map_node->map_to_local(r_cell_data.coords));
  731. xform = gl_transform * xform;
  732. ps->body_set_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM, xform);
  733. ps->body_attach_object_instance_id(body, tile_map_node->get_instance_id());
  734. ps->body_set_collision_layer(body, physics_layer);
  735. ps->body_set_collision_mask(body, physics_mask);
  736. ps->body_set_pickable(body, false);
  737. ps->body_set_state(body, PhysicsServer2D::BODY_STATE_LINEAR_VELOCITY, tile_data->get_constant_linear_velocity(tile_set_physics_layer));
  738. ps->body_set_state(body, PhysicsServer2D::BODY_STATE_ANGULAR_VELOCITY, tile_data->get_constant_angular_velocity(tile_set_physics_layer));
  739. if (!physics_material.is_valid()) {
  740. ps->body_set_param(body, PhysicsServer2D::BODY_PARAM_BOUNCE, 0);
  741. ps->body_set_param(body, PhysicsServer2D::BODY_PARAM_FRICTION, 1);
  742. } else {
  743. ps->body_set_param(body, PhysicsServer2D::BODY_PARAM_BOUNCE, physics_material->computed_bounce());
  744. ps->body_set_param(body, PhysicsServer2D::BODY_PARAM_FRICTION, physics_material->computed_friction());
  745. }
  746. // Clear body's shape if needed.
  747. ps->body_clear_shapes(body);
  748. // Add the shapes to the body.
  749. int body_shape_index = 0;
  750. for (int polygon_index = 0; polygon_index < tile_data->get_collision_polygons_count(tile_set_physics_layer); polygon_index++) {
  751. // Iterate over the polygons.
  752. bool one_way_collision = tile_data->is_collision_polygon_one_way(tile_set_physics_layer, polygon_index);
  753. float one_way_collision_margin = tile_data->get_collision_polygon_one_way_margin(tile_set_physics_layer, polygon_index);
  754. int shapes_count = tile_data->get_collision_polygon_shapes_count(tile_set_physics_layer, polygon_index);
  755. for (int shape_index = 0; shape_index < shapes_count; shape_index++) {
  756. // Add decomposed convex shapes.
  757. Ref<ConvexPolygonShape2D> shape = tile_data->get_collision_polygon_shape(tile_set_physics_layer, polygon_index, shape_index, flip_h, flip_v, transpose);
  758. ps->body_add_shape(body, shape->get_rid());
  759. ps->body_set_shape_as_one_way_collision(body, body_shape_index, one_way_collision, one_way_collision_margin);
  760. body_shape_index++;
  761. }
  762. }
  763. }
  764. // Set the body again.
  765. r_cell_data.bodies[tile_set_physics_layer] = body;
  766. }
  767. return;
  768. }
  769. }
  770. }
  771. // If we did not return earlier, clear the cell.
  772. _physics_clear_cell(r_cell_data);
  773. }
  774. #ifdef DEBUG_ENABLED
  775. void TileMapLayer::_physics_draw_cell_debug(const RID &p_canvas_item, const Vector2i &p_quadrant_pos, const CellData &r_cell_data) {
  776. // Draw the debug collision shapes.
  777. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  778. ERR_FAIL_COND(!tile_set.is_valid());
  779. if (!tile_map_node->get_tree()) {
  780. return;
  781. }
  782. bool show_collision = false;
  783. switch (tile_map_node->get_collision_visibility_mode()) {
  784. case TileMap::VISIBILITY_MODE_DEFAULT:
  785. show_collision = !Engine::get_singleton()->is_editor_hint() && tile_map_node->get_tree()->is_debugging_collisions_hint();
  786. break;
  787. case TileMap::VISIBILITY_MODE_FORCE_HIDE:
  788. show_collision = false;
  789. break;
  790. case TileMap::VISIBILITY_MODE_FORCE_SHOW:
  791. show_collision = true;
  792. break;
  793. }
  794. if (!show_collision) {
  795. return;
  796. }
  797. RenderingServer *rs = RenderingServer::get_singleton();
  798. PhysicsServer2D *ps = PhysicsServer2D::get_singleton();
  799. Color debug_collision_color = tile_map_node->get_tree()->get_debug_collisions_color();
  800. Vector<Color> color;
  801. color.push_back(debug_collision_color);
  802. Transform2D quadrant_to_local(0, p_quadrant_pos);
  803. Transform2D global_to_quadrant = (tile_map_node->get_global_transform() * quadrant_to_local).affine_inverse();
  804. for (RID body : r_cell_data.bodies) {
  805. if (body.is_valid()) {
  806. Transform2D body_to_quadrant = global_to_quadrant * Transform2D(ps->body_get_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM));
  807. rs->canvas_item_add_set_transform(p_canvas_item, body_to_quadrant);
  808. for (int shape_index = 0; shape_index < ps->body_get_shape_count(body); shape_index++) {
  809. const RID &shape = ps->body_get_shape(body, shape_index);
  810. const PhysicsServer2D::ShapeType &type = ps->shape_get_type(shape);
  811. if (type == PhysicsServer2D::SHAPE_CONVEX_POLYGON) {
  812. rs->canvas_item_add_polygon(p_canvas_item, ps->shape_get_data(shape), color);
  813. } else {
  814. WARN_PRINT("Wrong shape type for a tile, should be SHAPE_CONVEX_POLYGON.");
  815. }
  816. }
  817. rs->canvas_item_add_set_transform(p_canvas_item, Transform2D());
  818. }
  819. }
  820. };
  821. #endif // DEBUG_ENABLED
  822. /////////////////////////////// Navigation //////////////////////////////////////
  823. void TileMapLayer::_navigation_update() {
  824. ERR_FAIL_NULL(NavigationServer2D::get_singleton());
  825. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  826. NavigationServer2D *ns = NavigationServer2D::get_singleton();
  827. // Check if we should cleanup everything.
  828. bool forced_cleanup = in_destructor || !enabled || !navigation_enabled || !tile_map_node->is_inside_tree() || !tile_set.is_valid();
  829. // ----------- Layer level processing -----------
  830. if (forced_cleanup) {
  831. if (navigation_map.is_valid() && !uses_world_navigation_map) {
  832. ns->free(navigation_map);
  833. navigation_map = RID();
  834. }
  835. } else {
  836. // Update navigation maps.
  837. if (!navigation_map.is_valid()) {
  838. if (layer_index_in_tile_map_node == 0) {
  839. // Use the default World2D navigation map for the first layer when empty.
  840. navigation_map = tile_map_node->get_world_2d()->get_navigation_map();
  841. uses_world_navigation_map = true;
  842. } else {
  843. RID new_layer_map = ns->map_create();
  844. // Set the default NavigationPolygon cell_size on the new map as a mismatch causes an error.
  845. ns->map_set_cell_size(new_layer_map, 1.0);
  846. ns->map_set_active(new_layer_map, true);
  847. navigation_map = new_layer_map;
  848. uses_world_navigation_map = false;
  849. }
  850. }
  851. }
  852. // ----------- Navigation regions processing -----------
  853. if (forced_cleanup) {
  854. // Clean everything.
  855. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  856. _navigation_clear_cell(kv.value);
  857. }
  858. } else {
  859. if (_navigation_was_cleaned_up || dirty.flags[DIRTY_FLAGS_TILE_MAP_TILE_SET]) {
  860. // Update all cells.
  861. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  862. _navigation_update_cell(kv.value);
  863. }
  864. } else {
  865. // Update dirty cells.
  866. for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  867. CellData &cell_data = *cell_data_list_element->self();
  868. _navigation_update_cell(cell_data);
  869. }
  870. }
  871. if (dirty.flags[DIRTY_FLAGS_TILE_MAP_XFORM]) {
  872. Transform2D tilemap_xform = tile_map_node->get_global_transform();
  873. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  874. const CellData &cell_data = kv.value;
  875. // Update navigation regions transform.
  876. for (const RID &region : cell_data.navigation_regions) {
  877. if (!region.is_valid()) {
  878. continue;
  879. }
  880. Transform2D tile_transform;
  881. tile_transform.set_origin(tile_map_node->map_to_local(kv.key));
  882. NavigationServer2D::get_singleton()->region_set_transform(region, tilemap_xform * tile_transform);
  883. }
  884. }
  885. }
  886. }
  887. // -----------
  888. // Mark the navigation state as up to date.
  889. _navigation_was_cleaned_up = forced_cleanup;
  890. }
  891. void TileMapLayer::_navigation_clear_cell(CellData &r_cell_data) {
  892. NavigationServer2D *ns = NavigationServer2D::get_singleton();
  893. // Clear navigation shapes.
  894. for (uint32_t i = 0; i < r_cell_data.navigation_regions.size(); i++) {
  895. const RID &region = r_cell_data.navigation_regions[i];
  896. if (region.is_valid()) {
  897. ns->region_set_map(region, RID());
  898. ns->free(region);
  899. }
  900. }
  901. r_cell_data.navigation_regions.clear();
  902. }
  903. void TileMapLayer::_navigation_update_cell(CellData &r_cell_data) {
  904. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  905. NavigationServer2D *ns = NavigationServer2D::get_singleton();
  906. Transform2D tilemap_xform = tile_map_node->get_global_transform();
  907. // Get the navigation polygons and create regions.
  908. TileMapCell &c = r_cell_data.cell;
  909. TileSetSource *source;
  910. if (tile_set->has_source(c.source_id)) {
  911. source = *tile_set->get_source(c.source_id);
  912. if (source->has_tile(c.get_atlas_coords()) && source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  913. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  914. if (atlas_source) {
  915. const TileData *tile_data;
  916. if (r_cell_data.runtime_tile_data_cache) {
  917. tile_data = r_cell_data.runtime_tile_data_cache;
  918. } else {
  919. tile_data = atlas_source->get_tile_data(c.get_atlas_coords(), c.alternative_tile);
  920. }
  921. // Transform flags.
  922. bool flip_h = (c.alternative_tile & TileSetAtlasSource::TRANSFORM_FLIP_H);
  923. bool flip_v = (c.alternative_tile & TileSetAtlasSource::TRANSFORM_FLIP_V);
  924. bool transpose = (c.alternative_tile & TileSetAtlasSource::TRANSFORM_TRANSPOSE);
  925. // Free unused regions then resize the regions array.
  926. for (uint32_t i = tile_set->get_navigation_layers_count(); i < r_cell_data.navigation_regions.size(); i++) {
  927. RID &region = r_cell_data.navigation_regions[i];
  928. if (region.is_valid()) {
  929. ns->region_set_map(region, RID());
  930. ns->free(region);
  931. region = RID();
  932. }
  933. }
  934. r_cell_data.navigation_regions.resize(tile_set->get_navigation_layers_count());
  935. // Create, update or clear regions.
  936. for (uint32_t navigation_layer_index = 0; navigation_layer_index < r_cell_data.navigation_regions.size(); navigation_layer_index++) {
  937. Ref<NavigationPolygon> navigation_polygon = tile_data->get_navigation_polygon(navigation_layer_index, flip_h, flip_v, transpose);
  938. RID &region = r_cell_data.navigation_regions[navigation_layer_index];
  939. if (navigation_polygon.is_valid() && (navigation_polygon->get_polygon_count() > 0 || navigation_polygon->get_outline_count() > 0)) {
  940. // Create or update regions.
  941. Transform2D tile_transform;
  942. tile_transform.set_origin(tile_map_node->map_to_local(r_cell_data.coords));
  943. if (!region.is_valid()) {
  944. region = ns->region_create();
  945. }
  946. ns->region_set_owner_id(region, tile_map_node->get_instance_id());
  947. ns->region_set_map(region, navigation_map);
  948. ns->region_set_transform(region, tilemap_xform * tile_transform);
  949. ns->region_set_navigation_layers(region, tile_set->get_navigation_layer_layers(navigation_layer_index));
  950. ns->region_set_navigation_polygon(region, navigation_polygon);
  951. } else {
  952. // Clear region.
  953. if (region.is_valid()) {
  954. ns->region_set_map(region, RID());
  955. ns->free(region);
  956. region = RID();
  957. }
  958. }
  959. }
  960. return;
  961. }
  962. }
  963. }
  964. // If we did not return earlier, clear the cell.
  965. _navigation_clear_cell(r_cell_data);
  966. }
  967. #ifdef DEBUG_ENABLED
  968. void TileMapLayer::_navigation_draw_cell_debug(const RID &p_canvas_item, const Vector2i &p_quadrant_pos, const CellData &r_cell_data) {
  969. // Draw the debug collision shapes.
  970. bool show_navigation = false;
  971. switch (tile_map_node->get_navigation_visibility_mode()) {
  972. case TileMap::VISIBILITY_MODE_DEFAULT:
  973. show_navigation = !Engine::get_singleton()->is_editor_hint() && tile_map_node->get_tree()->is_debugging_navigation_hint();
  974. break;
  975. case TileMap::VISIBILITY_MODE_FORCE_HIDE:
  976. show_navigation = false;
  977. break;
  978. case TileMap::VISIBILITY_MODE_FORCE_SHOW:
  979. show_navigation = true;
  980. break;
  981. }
  982. if (!show_navigation) {
  983. return;
  984. }
  985. // Check if the navigation is used.
  986. if (r_cell_data.navigation_regions.is_empty()) {
  987. return;
  988. }
  989. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  990. RenderingServer *rs = RenderingServer::get_singleton();
  991. const NavigationServer2D *ns2d = NavigationServer2D::get_singleton();
  992. bool enabled_geometry_face_random_color = ns2d->get_debug_navigation_enable_geometry_face_random_color();
  993. bool enabled_edge_lines = ns2d->get_debug_navigation_enable_edge_lines();
  994. Color debug_face_color = ns2d->get_debug_navigation_geometry_face_color();
  995. Color debug_edge_color = ns2d->get_debug_navigation_geometry_edge_color();
  996. RandomPCG rand;
  997. const TileMapCell &c = r_cell_data.cell;
  998. TileSetSource *source;
  999. if (tile_set->has_source(c.source_id)) {
  1000. source = *tile_set->get_source(c.source_id);
  1001. if (source->has_tile(c.get_atlas_coords()) && source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  1002. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  1003. if (atlas_source) {
  1004. const TileData *tile_data;
  1005. if (r_cell_data.runtime_tile_data_cache) {
  1006. tile_data = r_cell_data.runtime_tile_data_cache;
  1007. } else {
  1008. tile_data = atlas_source->get_tile_data(c.get_atlas_coords(), c.alternative_tile);
  1009. }
  1010. Transform2D cell_to_quadrant;
  1011. cell_to_quadrant.set_origin(tile_map_node->map_to_local(r_cell_data.coords) - p_quadrant_pos);
  1012. rs->canvas_item_add_set_transform(p_canvas_item, cell_to_quadrant);
  1013. for (int layer_index = 0; layer_index < tile_set->get_navigation_layers_count(); layer_index++) {
  1014. bool flip_h = (c.alternative_tile & TileSetAtlasSource::TRANSFORM_FLIP_H);
  1015. bool flip_v = (c.alternative_tile & TileSetAtlasSource::TRANSFORM_FLIP_V);
  1016. bool transpose = (c.alternative_tile & TileSetAtlasSource::TRANSFORM_TRANSPOSE);
  1017. Ref<NavigationPolygon> navigation_polygon = tile_data->get_navigation_polygon(layer_index, flip_h, flip_v, transpose);
  1018. if (navigation_polygon.is_valid()) {
  1019. Vector<Vector2> navigation_polygon_vertices = navigation_polygon->get_vertices();
  1020. if (navigation_polygon_vertices.size() < 3) {
  1021. continue;
  1022. }
  1023. for (int i = 0; i < navigation_polygon->get_polygon_count(); i++) {
  1024. // An array of vertices for this polygon.
  1025. Vector<int> polygon = navigation_polygon->get_polygon(i);
  1026. Vector<Vector2> debug_polygon_vertices;
  1027. debug_polygon_vertices.resize(polygon.size());
  1028. for (int j = 0; j < polygon.size(); j++) {
  1029. ERR_FAIL_INDEX(polygon[j], navigation_polygon_vertices.size());
  1030. debug_polygon_vertices.write[j] = navigation_polygon_vertices[polygon[j]];
  1031. }
  1032. // Generate the polygon color, slightly randomly modified from the settings one.
  1033. Color random_variation_color = debug_face_color;
  1034. if (enabled_geometry_face_random_color) {
  1035. random_variation_color.set_hsv(
  1036. debug_face_color.get_h() + rand.random(-1.0, 1.0) * 0.1,
  1037. debug_face_color.get_s(),
  1038. debug_face_color.get_v() + rand.random(-1.0, 1.0) * 0.2);
  1039. }
  1040. random_variation_color.a = debug_face_color.a;
  1041. Vector<Color> debug_face_colors;
  1042. debug_face_colors.push_back(random_variation_color);
  1043. rs->canvas_item_add_polygon(p_canvas_item, debug_polygon_vertices, debug_face_colors);
  1044. if (enabled_edge_lines) {
  1045. Vector<Color> debug_edge_colors;
  1046. debug_edge_colors.push_back(debug_edge_color);
  1047. debug_polygon_vertices.push_back(debug_polygon_vertices[0]); // Add first again for closing polyline.
  1048. rs->canvas_item_add_polyline(p_canvas_item, debug_polygon_vertices, debug_edge_colors);
  1049. }
  1050. }
  1051. }
  1052. }
  1053. }
  1054. }
  1055. }
  1056. }
  1057. #endif // DEBUG_ENABLED
  1058. /////////////////////////////// Scenes //////////////////////////////////////
  1059. void TileMapLayer::_scenes_update() {
  1060. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1061. // Check if we should cleanup everything.
  1062. bool forced_cleanup = in_destructor || !enabled || !tile_map_node->is_inside_tree() || !tile_set.is_valid();
  1063. if (forced_cleanup) {
  1064. // Clean everything.
  1065. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  1066. _scenes_clear_cell(kv.value);
  1067. }
  1068. } else {
  1069. if (_scenes_was_cleaned_up || dirty.flags[DIRTY_FLAGS_TILE_MAP_TILE_SET]) {
  1070. // Update all cells.
  1071. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  1072. _scenes_update_cell(kv.value);
  1073. }
  1074. } else {
  1075. // Update dirty cells.
  1076. for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  1077. CellData &cell_data = *cell_data_list_element->self();
  1078. _scenes_update_cell(cell_data);
  1079. }
  1080. }
  1081. }
  1082. // -----------
  1083. // Mark the scenes state as up to date.
  1084. _scenes_was_cleaned_up = forced_cleanup;
  1085. }
  1086. void TileMapLayer::_scenes_clear_cell(CellData &r_cell_data) {
  1087. // Cleanup existing scene.
  1088. Node *node = tile_map_node->get_node_or_null(r_cell_data.scene);
  1089. if (node) {
  1090. node->queue_free();
  1091. }
  1092. r_cell_data.scene = "";
  1093. }
  1094. void TileMapLayer::_scenes_update_cell(CellData &r_cell_data) {
  1095. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1096. // Clear the scene in any case.
  1097. _scenes_clear_cell(r_cell_data);
  1098. // Create the scene.
  1099. const TileMapCell &c = r_cell_data.cell;
  1100. TileSetSource *source;
  1101. if (tile_set->has_source(c.source_id)) {
  1102. source = *tile_set->get_source(c.source_id);
  1103. if (source->has_tile(c.get_atlas_coords()) && source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  1104. TileSetScenesCollectionSource *scenes_collection_source = Object::cast_to<TileSetScenesCollectionSource>(source);
  1105. if (scenes_collection_source) {
  1106. Ref<PackedScene> packed_scene = scenes_collection_source->get_scene_tile_scene(c.alternative_tile);
  1107. if (packed_scene.is_valid()) {
  1108. Node *scene = packed_scene->instantiate();
  1109. Control *scene_as_control = Object::cast_to<Control>(scene);
  1110. Node2D *scene_as_node2d = Object::cast_to<Node2D>(scene);
  1111. if (scene_as_control) {
  1112. scene_as_control->set_position(tile_map_node->map_to_local(r_cell_data.coords) + scene_as_control->get_position());
  1113. } else if (scene_as_node2d) {
  1114. Transform2D xform;
  1115. xform.set_origin(tile_map_node->map_to_local(r_cell_data.coords));
  1116. scene_as_node2d->set_transform(xform * scene_as_node2d->get_transform());
  1117. }
  1118. tile_map_node->add_child(scene);
  1119. r_cell_data.scene = scene->get_name();
  1120. }
  1121. }
  1122. }
  1123. }
  1124. }
  1125. #ifdef DEBUG_ENABLED
  1126. void TileMapLayer::_scenes_draw_cell_debug(const RID &p_canvas_item, const Vector2i &p_quadrant_pos, const CellData &r_cell_data) {
  1127. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1128. ERR_FAIL_COND(!tile_set.is_valid());
  1129. if (!Engine::get_singleton()->is_editor_hint()) {
  1130. return;
  1131. }
  1132. // Draw a placeholder for scenes needing one.
  1133. RenderingServer *rs = RenderingServer::get_singleton();
  1134. const TileMapCell &c = r_cell_data.cell;
  1135. TileSetSource *source;
  1136. if (tile_set->has_source(c.source_id)) {
  1137. source = *tile_set->get_source(c.source_id);
  1138. if (!source->has_tile(c.get_atlas_coords()) || !source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  1139. return;
  1140. }
  1141. TileSetScenesCollectionSource *scenes_collection_source = Object::cast_to<TileSetScenesCollectionSource>(source);
  1142. if (scenes_collection_source) {
  1143. if (!scenes_collection_source->get_scene_tile_scene(c.alternative_tile).is_valid() || scenes_collection_source->get_scene_tile_display_placeholder(c.alternative_tile)) {
  1144. // Generate a random color from the hashed values of the tiles.
  1145. Array to_hash;
  1146. to_hash.push_back(c.source_id);
  1147. to_hash.push_back(c.alternative_tile);
  1148. uint32_t hash = RandomPCG(to_hash.hash()).rand();
  1149. Color color;
  1150. color = color.from_hsv(
  1151. (float)((hash >> 24) & 0xFF) / 256.0,
  1152. Math::lerp(0.5, 1.0, (float)((hash >> 16) & 0xFF) / 256.0),
  1153. Math::lerp(0.5, 1.0, (float)((hash >> 8) & 0xFF) / 256.0),
  1154. 0.8);
  1155. // Draw a placeholder tile.
  1156. Transform2D cell_to_quadrant;
  1157. cell_to_quadrant.set_origin(tile_map_node->map_to_local(r_cell_data.coords) - p_quadrant_pos);
  1158. rs->canvas_item_add_set_transform(p_canvas_item, cell_to_quadrant);
  1159. rs->canvas_item_add_circle(p_canvas_item, Vector2(), MIN(tile_set->get_tile_size().x, tile_set->get_tile_size().y) / 4.0, color);
  1160. }
  1161. }
  1162. }
  1163. }
  1164. #endif // DEBUG_ENABLED
  1165. /////////////////////////////////////////////////////////////////////
  1166. void TileMapLayer::_build_runtime_update_tile_data() {
  1167. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1168. // Check if we should cleanup everything.
  1169. bool forced_cleanup = in_destructor || !enabled || !tile_map_node->is_inside_tree() || !tile_set.is_valid() || !tile_map_node->is_visible_in_tree();
  1170. if (!forced_cleanup) {
  1171. if (tile_map_node->GDVIRTUAL_IS_OVERRIDDEN(_use_tile_data_runtime_update) && tile_map_node->GDVIRTUAL_IS_OVERRIDDEN(_tile_data_runtime_update)) {
  1172. if (_runtime_update_tile_data_was_cleaned_up || dirty.flags[DIRTY_FLAGS_TILE_MAP_TILE_SET]) {
  1173. for (KeyValue<Vector2i, CellData> &E : tile_map) {
  1174. _build_runtime_update_tile_data_for_cell(E.value);
  1175. }
  1176. } else if (dirty.flags[DIRTY_FLAGS_TILE_MAP_RUNTIME_UPDATE]) {
  1177. for (KeyValue<Vector2i, CellData> &E : tile_map) {
  1178. _build_runtime_update_tile_data_for_cell(E.value, true);
  1179. }
  1180. } else {
  1181. for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  1182. CellData &cell_data = *cell_data_list_element->self();
  1183. _build_runtime_update_tile_data_for_cell(cell_data);
  1184. }
  1185. }
  1186. }
  1187. }
  1188. // -----------
  1189. // Mark the navigation state as up to date.
  1190. _runtime_update_tile_data_was_cleaned_up = forced_cleanup;
  1191. }
  1192. void TileMapLayer::_build_runtime_update_tile_data_for_cell(CellData &r_cell_data, bool p_auto_add_to_dirty_list) {
  1193. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1194. TileMapCell &c = r_cell_data.cell;
  1195. TileSetSource *source;
  1196. if (tile_set->has_source(c.source_id)) {
  1197. source = *tile_set->get_source(c.source_id);
  1198. if (source->has_tile(c.get_atlas_coords()) && source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  1199. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  1200. if (atlas_source) {
  1201. bool ret = false;
  1202. if (tile_map_node->GDVIRTUAL_CALL(_use_tile_data_runtime_update, layer_index_in_tile_map_node, r_cell_data.coords, ret) && ret) {
  1203. TileData *tile_data = atlas_source->get_tile_data(c.get_atlas_coords(), c.alternative_tile);
  1204. // Create the runtime TileData.
  1205. TileData *tile_data_runtime_use = tile_data->duplicate();
  1206. tile_data_runtime_use->set_allow_transform(true);
  1207. r_cell_data.runtime_tile_data_cache = tile_data_runtime_use;
  1208. tile_map_node->GDVIRTUAL_CALL(_tile_data_runtime_update, layer_index_in_tile_map_node, r_cell_data.coords, tile_data_runtime_use);
  1209. if (p_auto_add_to_dirty_list) {
  1210. dirty.cell_list.add(&r_cell_data.dirty_list_element);
  1211. }
  1212. }
  1213. }
  1214. }
  1215. }
  1216. }
  1217. void TileMapLayer::_clear_runtime_update_tile_data() {
  1218. for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  1219. CellData &cell_data = *cell_data_list_element->self();
  1220. // Clear the runtime tile data.
  1221. if (cell_data.runtime_tile_data_cache) {
  1222. memdelete(cell_data.runtime_tile_data_cache);
  1223. cell_data.runtime_tile_data_cache = nullptr;
  1224. }
  1225. }
  1226. }
  1227. TileSet::TerrainsPattern TileMapLayer::_get_best_terrain_pattern_for_constraints(int p_terrain_set, const Vector2i &p_position, const RBSet<TerrainConstraint> &p_constraints, TileSet::TerrainsPattern p_current_pattern) {
  1228. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1229. if (!tile_set.is_valid()) {
  1230. return TileSet::TerrainsPattern();
  1231. }
  1232. // Returns all tiles compatible with the given constraints.
  1233. RBMap<TileSet::TerrainsPattern, int> terrain_pattern_score;
  1234. RBSet<TileSet::TerrainsPattern> pattern_set = tile_set->get_terrains_pattern_set(p_terrain_set);
  1235. ERR_FAIL_COND_V(pattern_set.is_empty(), TileSet::TerrainsPattern());
  1236. for (TileSet::TerrainsPattern &terrain_pattern : pattern_set) {
  1237. int score = 0;
  1238. // Check the center bit constraint.
  1239. TerrainConstraint terrain_constraint = TerrainConstraint(tile_map_node, p_position, terrain_pattern.get_terrain());
  1240. const RBSet<TerrainConstraint>::Element *in_set_constraint_element = p_constraints.find(terrain_constraint);
  1241. if (in_set_constraint_element) {
  1242. if (in_set_constraint_element->get().get_terrain() != terrain_constraint.get_terrain()) {
  1243. score += in_set_constraint_element->get().get_priority();
  1244. }
  1245. } else if (p_current_pattern.get_terrain() != terrain_pattern.get_terrain()) {
  1246. continue; // Ignore a pattern that cannot keep bits without constraints unmodified.
  1247. }
  1248. // Check the surrounding bits
  1249. bool invalid_pattern = false;
  1250. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  1251. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  1252. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, bit)) {
  1253. // Check if the bit is compatible with the constraints.
  1254. TerrainConstraint terrain_bit_constraint = TerrainConstraint(tile_map_node, p_position, bit, terrain_pattern.get_terrain_peering_bit(bit));
  1255. in_set_constraint_element = p_constraints.find(terrain_bit_constraint);
  1256. if (in_set_constraint_element) {
  1257. if (in_set_constraint_element->get().get_terrain() != terrain_bit_constraint.get_terrain()) {
  1258. score += in_set_constraint_element->get().get_priority();
  1259. }
  1260. } else if (p_current_pattern.get_terrain_peering_bit(bit) != terrain_pattern.get_terrain_peering_bit(bit)) {
  1261. invalid_pattern = true; // Ignore a pattern that cannot keep bits without constraints unmodified.
  1262. break;
  1263. }
  1264. }
  1265. }
  1266. if (invalid_pattern) {
  1267. continue;
  1268. }
  1269. terrain_pattern_score[terrain_pattern] = score;
  1270. }
  1271. // Compute the minimum score.
  1272. TileSet::TerrainsPattern min_score_pattern = p_current_pattern;
  1273. int min_score = INT32_MAX;
  1274. for (KeyValue<TileSet::TerrainsPattern, int> E : terrain_pattern_score) {
  1275. if (E.value < min_score) {
  1276. min_score_pattern = E.key;
  1277. min_score = E.value;
  1278. }
  1279. }
  1280. return min_score_pattern;
  1281. }
  1282. RBSet<TerrainConstraint> TileMapLayer::_get_terrain_constraints_from_added_pattern(const Vector2i &p_position, int p_terrain_set, TileSet::TerrainsPattern p_terrains_pattern) const {
  1283. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1284. if (!tile_set.is_valid()) {
  1285. return RBSet<TerrainConstraint>();
  1286. }
  1287. // Compute the constraints needed from the surrounding tiles.
  1288. RBSet<TerrainConstraint> output;
  1289. output.insert(TerrainConstraint(tile_map_node, p_position, p_terrains_pattern.get_terrain()));
  1290. for (uint32_t i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  1291. TileSet::CellNeighbor side = TileSet::CellNeighbor(i);
  1292. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, side)) {
  1293. TerrainConstraint c = TerrainConstraint(tile_map_node, p_position, side, p_terrains_pattern.get_terrain_peering_bit(side));
  1294. output.insert(c);
  1295. }
  1296. }
  1297. return output;
  1298. }
  1299. RBSet<TerrainConstraint> TileMapLayer::_get_terrain_constraints_from_painted_cells_list(const RBSet<Vector2i> &p_painted, int p_terrain_set, bool p_ignore_empty_terrains) const {
  1300. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1301. if (!tile_set.is_valid()) {
  1302. return RBSet<TerrainConstraint>();
  1303. }
  1304. ERR_FAIL_INDEX_V(p_terrain_set, tile_set->get_terrain_sets_count(), RBSet<TerrainConstraint>());
  1305. // Build a set of dummy constraints to get the constrained points.
  1306. RBSet<TerrainConstraint> dummy_constraints;
  1307. for (const Vector2i &E : p_painted) {
  1308. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) { // Iterates over neighbor bits.
  1309. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  1310. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, bit)) {
  1311. dummy_constraints.insert(TerrainConstraint(tile_map_node, E, bit, -1));
  1312. }
  1313. }
  1314. }
  1315. // For each constrained point, we get all overlapping tiles, and select the most adequate terrain for it.
  1316. RBSet<TerrainConstraint> constraints;
  1317. for (const TerrainConstraint &E_constraint : dummy_constraints) {
  1318. HashMap<int, int> terrain_count;
  1319. // Count the number of occurrences per terrain.
  1320. HashMap<Vector2i, TileSet::CellNeighbor> overlapping_terrain_bits = E_constraint.get_overlapping_coords_and_peering_bits();
  1321. for (const KeyValue<Vector2i, TileSet::CellNeighbor> &E_overlapping : overlapping_terrain_bits) {
  1322. TileData *neighbor_tile_data = nullptr;
  1323. TileMapCell neighbor_cell = get_cell(E_overlapping.key);
  1324. if (neighbor_cell.source_id != TileSet::INVALID_SOURCE) {
  1325. Ref<TileSetSource> source = tile_set->get_source(neighbor_cell.source_id);
  1326. Ref<TileSetAtlasSource> atlas_source = source;
  1327. if (atlas_source.is_valid()) {
  1328. TileData *tile_data = atlas_source->get_tile_data(neighbor_cell.get_atlas_coords(), neighbor_cell.alternative_tile);
  1329. if (tile_data && tile_data->get_terrain_set() == p_terrain_set) {
  1330. neighbor_tile_data = tile_data;
  1331. }
  1332. }
  1333. }
  1334. int terrain = neighbor_tile_data ? neighbor_tile_data->get_terrain_peering_bit(TileSet::CellNeighbor(E_overlapping.value)) : -1;
  1335. if (!p_ignore_empty_terrains || terrain >= 0) {
  1336. if (!terrain_count.has(terrain)) {
  1337. terrain_count[terrain] = 0;
  1338. }
  1339. terrain_count[terrain] += 1;
  1340. }
  1341. }
  1342. // Get the terrain with the max number of occurrences.
  1343. int max = 0;
  1344. int max_terrain = -1;
  1345. for (const KeyValue<int, int> &E_terrain_count : terrain_count) {
  1346. if (E_terrain_count.value > max) {
  1347. max = E_terrain_count.value;
  1348. max_terrain = E_terrain_count.key;
  1349. }
  1350. }
  1351. // Set the adequate terrain.
  1352. if (max > 0) {
  1353. TerrainConstraint c = E_constraint;
  1354. c.set_terrain(max_terrain);
  1355. constraints.insert(c);
  1356. }
  1357. }
  1358. // Add the centers as constraints.
  1359. for (Vector2i E_coords : p_painted) {
  1360. TileData *tile_data = nullptr;
  1361. TileMapCell cell = get_cell(E_coords);
  1362. if (cell.source_id != TileSet::INVALID_SOURCE) {
  1363. Ref<TileSetSource> source = tile_set->get_source(cell.source_id);
  1364. Ref<TileSetAtlasSource> atlas_source = source;
  1365. if (atlas_source.is_valid()) {
  1366. tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  1367. }
  1368. }
  1369. int terrain = (tile_data && tile_data->get_terrain_set() == p_terrain_set) ? tile_data->get_terrain() : -1;
  1370. if (!p_ignore_empty_terrains || terrain >= 0) {
  1371. constraints.insert(TerrainConstraint(tile_map_node, E_coords, terrain));
  1372. }
  1373. }
  1374. return constraints;
  1375. }
  1376. void TileMapLayer::set_tile_map(TileMap *p_tile_map) {
  1377. tile_map_node = p_tile_map;
  1378. }
  1379. void TileMapLayer::set_layer_index_in_tile_map_node(int p_index) {
  1380. if (p_index == layer_index_in_tile_map_node) {
  1381. return;
  1382. }
  1383. layer_index_in_tile_map_node = p_index;
  1384. dirty.flags[DIRTY_FLAGS_LAYER_INDEX_IN_TILE_MAP_NODE] = true;
  1385. tile_map_node->queue_internal_update();
  1386. }
  1387. Rect2 TileMapLayer::get_rect(bool &r_changed) const {
  1388. // Compute the displayed area of the tilemap.
  1389. r_changed = false;
  1390. #ifdef DEBUG_ENABLED
  1391. if (rect_cache_dirty) {
  1392. Rect2 r_total;
  1393. bool first = true;
  1394. for (const KeyValue<Vector2i, CellData> &E : tile_map) {
  1395. Rect2 r;
  1396. r.position = tile_map_node->map_to_local(E.key);
  1397. r.size = Size2();
  1398. if (first) {
  1399. r_total = r;
  1400. first = false;
  1401. } else {
  1402. r_total = r_total.merge(r);
  1403. }
  1404. }
  1405. r_changed = rect_cache != r_total;
  1406. rect_cache = r_total;
  1407. rect_cache_dirty = false;
  1408. }
  1409. #endif
  1410. return rect_cache;
  1411. }
  1412. HashMap<Vector2i, TileSet::TerrainsPattern> TileMapLayer::terrain_fill_constraints(const Vector<Vector2i> &p_to_replace, int p_terrain_set, const RBSet<TerrainConstraint> &p_constraints) {
  1413. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1414. if (!tile_set.is_valid()) {
  1415. return HashMap<Vector2i, TileSet::TerrainsPattern>();
  1416. }
  1417. // Copy the constraints set.
  1418. RBSet<TerrainConstraint> constraints = p_constraints;
  1419. // Output map.
  1420. HashMap<Vector2i, TileSet::TerrainsPattern> output;
  1421. // Add all positions to a set.
  1422. for (int i = 0; i < p_to_replace.size(); i++) {
  1423. const Vector2i &coords = p_to_replace[i];
  1424. // Select the best pattern for the given constraints.
  1425. TileSet::TerrainsPattern current_pattern = TileSet::TerrainsPattern(*tile_set, p_terrain_set);
  1426. TileMapCell cell = get_cell(coords);
  1427. if (cell.source_id != TileSet::INVALID_SOURCE) {
  1428. TileSetSource *source = *tile_set->get_source(cell.source_id);
  1429. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  1430. if (atlas_source) {
  1431. // Get tile data.
  1432. TileData *tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  1433. if (tile_data && tile_data->get_terrain_set() == p_terrain_set) {
  1434. current_pattern = tile_data->get_terrains_pattern();
  1435. }
  1436. }
  1437. }
  1438. TileSet::TerrainsPattern pattern = _get_best_terrain_pattern_for_constraints(p_terrain_set, coords, constraints, current_pattern);
  1439. // Update the constraint set with the new ones.
  1440. RBSet<TerrainConstraint> new_constraints = _get_terrain_constraints_from_added_pattern(coords, p_terrain_set, pattern);
  1441. for (const TerrainConstraint &E_constraint : new_constraints) {
  1442. if (constraints.has(E_constraint)) {
  1443. constraints.erase(E_constraint);
  1444. }
  1445. TerrainConstraint c = E_constraint;
  1446. c.set_priority(5);
  1447. constraints.insert(c);
  1448. }
  1449. output[coords] = pattern;
  1450. }
  1451. return output;
  1452. }
  1453. HashMap<Vector2i, TileSet::TerrainsPattern> TileMapLayer::terrain_fill_connect(const Vector<Vector2i> &p_coords_array, int p_terrain_set, int p_terrain, bool p_ignore_empty_terrains) {
  1454. HashMap<Vector2i, TileSet::TerrainsPattern> output;
  1455. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1456. ERR_FAIL_COND_V(!tile_set.is_valid(), output);
  1457. ERR_FAIL_INDEX_V(p_terrain_set, tile_set->get_terrain_sets_count(), output);
  1458. // Build list and set of tiles that can be modified (painted and their surroundings).
  1459. Vector<Vector2i> can_modify_list;
  1460. RBSet<Vector2i> can_modify_set;
  1461. RBSet<Vector2i> painted_set;
  1462. for (int i = p_coords_array.size() - 1; i >= 0; i--) {
  1463. const Vector2i &coords = p_coords_array[i];
  1464. can_modify_list.push_back(coords);
  1465. can_modify_set.insert(coords);
  1466. painted_set.insert(coords);
  1467. }
  1468. for (Vector2i coords : p_coords_array) {
  1469. // Find the adequate neighbor.
  1470. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  1471. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  1472. if (tile_map_node->is_existing_neighbor(bit)) {
  1473. Vector2i neighbor = tile_map_node->get_neighbor_cell(coords, bit);
  1474. if (!can_modify_set.has(neighbor)) {
  1475. can_modify_list.push_back(neighbor);
  1476. can_modify_set.insert(neighbor);
  1477. }
  1478. }
  1479. }
  1480. }
  1481. // Build a set, out of the possibly modified tiles, of the one with a center bit that is set (or will be) to the painted terrain.
  1482. RBSet<Vector2i> cells_with_terrain_center_bit;
  1483. for (Vector2i coords : can_modify_set) {
  1484. bool connect = false;
  1485. if (painted_set.has(coords)) {
  1486. connect = true;
  1487. } else {
  1488. // Get the center bit of the cell.
  1489. TileData *tile_data = nullptr;
  1490. TileMapCell cell = get_cell(coords);
  1491. if (cell.source_id != TileSet::INVALID_SOURCE) {
  1492. Ref<TileSetSource> source = tile_set->get_source(cell.source_id);
  1493. Ref<TileSetAtlasSource> atlas_source = source;
  1494. if (atlas_source.is_valid()) {
  1495. tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  1496. }
  1497. }
  1498. if (tile_data && tile_data->get_terrain_set() == p_terrain_set && tile_data->get_terrain() == p_terrain) {
  1499. connect = true;
  1500. }
  1501. }
  1502. if (connect) {
  1503. cells_with_terrain_center_bit.insert(coords);
  1504. }
  1505. }
  1506. RBSet<TerrainConstraint> constraints;
  1507. // Add new constraints from the path drawn.
  1508. for (Vector2i coords : p_coords_array) {
  1509. // Constraints on the center bit.
  1510. TerrainConstraint c = TerrainConstraint(tile_map_node, coords, p_terrain);
  1511. c.set_priority(10);
  1512. constraints.insert(c);
  1513. // Constraints on the connecting bits.
  1514. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  1515. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  1516. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, bit)) {
  1517. c = TerrainConstraint(tile_map_node, coords, bit, p_terrain);
  1518. c.set_priority(10);
  1519. if ((int(bit) % 2) == 0) {
  1520. // Side peering bits: add the constraint if the center is of the same terrain.
  1521. Vector2i neighbor = tile_map_node->get_neighbor_cell(coords, bit);
  1522. if (cells_with_terrain_center_bit.has(neighbor)) {
  1523. constraints.insert(c);
  1524. }
  1525. } else {
  1526. // Corner peering bits: add the constraint if all tiles on the constraint has the same center bit.
  1527. HashMap<Vector2i, TileSet::CellNeighbor> overlapping_terrain_bits = c.get_overlapping_coords_and_peering_bits();
  1528. bool valid = true;
  1529. for (KeyValue<Vector2i, TileSet::CellNeighbor> kv : overlapping_terrain_bits) {
  1530. if (!cells_with_terrain_center_bit.has(kv.key)) {
  1531. valid = false;
  1532. break;
  1533. }
  1534. }
  1535. if (valid) {
  1536. constraints.insert(c);
  1537. }
  1538. }
  1539. }
  1540. }
  1541. }
  1542. // Fills in the constraint list from existing tiles.
  1543. for (TerrainConstraint c : _get_terrain_constraints_from_painted_cells_list(painted_set, p_terrain_set, p_ignore_empty_terrains)) {
  1544. constraints.insert(c);
  1545. }
  1546. // Fill the terrains.
  1547. output = terrain_fill_constraints(can_modify_list, p_terrain_set, constraints);
  1548. return output;
  1549. }
  1550. HashMap<Vector2i, TileSet::TerrainsPattern> TileMapLayer::terrain_fill_path(const Vector<Vector2i> &p_coords_array, int p_terrain_set, int p_terrain, bool p_ignore_empty_terrains) {
  1551. HashMap<Vector2i, TileSet::TerrainsPattern> output;
  1552. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1553. ERR_FAIL_COND_V(!tile_set.is_valid(), output);
  1554. ERR_FAIL_INDEX_V(p_terrain_set, tile_set->get_terrain_sets_count(), output);
  1555. // Make sure the path is correct and build the peering bit list while doing it.
  1556. Vector<TileSet::CellNeighbor> neighbor_list;
  1557. for (int i = 0; i < p_coords_array.size() - 1; i++) {
  1558. // Find the adequate neighbor.
  1559. TileSet::CellNeighbor found_bit = TileSet::CELL_NEIGHBOR_MAX;
  1560. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  1561. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  1562. if (tile_map_node->is_existing_neighbor(bit)) {
  1563. if (tile_map_node->get_neighbor_cell(p_coords_array[i], bit) == p_coords_array[i + 1]) {
  1564. found_bit = bit;
  1565. break;
  1566. }
  1567. }
  1568. }
  1569. ERR_FAIL_COND_V_MSG(found_bit == TileSet::CELL_NEIGHBOR_MAX, output, vformat("Invalid terrain path, %s is not a neighboring tile of %s", p_coords_array[i + 1], p_coords_array[i]));
  1570. neighbor_list.push_back(found_bit);
  1571. }
  1572. // Build list and set of tiles that can be modified (painted and their surroundings).
  1573. Vector<Vector2i> can_modify_list;
  1574. RBSet<Vector2i> can_modify_set;
  1575. RBSet<Vector2i> painted_set;
  1576. for (int i = p_coords_array.size() - 1; i >= 0; i--) {
  1577. const Vector2i &coords = p_coords_array[i];
  1578. can_modify_list.push_back(coords);
  1579. can_modify_set.insert(coords);
  1580. painted_set.insert(coords);
  1581. }
  1582. for (Vector2i coords : p_coords_array) {
  1583. // Find the adequate neighbor.
  1584. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  1585. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  1586. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, bit)) {
  1587. Vector2i neighbor = tile_map_node->get_neighbor_cell(coords, bit);
  1588. if (!can_modify_set.has(neighbor)) {
  1589. can_modify_list.push_back(neighbor);
  1590. can_modify_set.insert(neighbor);
  1591. }
  1592. }
  1593. }
  1594. }
  1595. RBSet<TerrainConstraint> constraints;
  1596. // Add new constraints from the path drawn.
  1597. for (Vector2i coords : p_coords_array) {
  1598. // Constraints on the center bit.
  1599. TerrainConstraint c = TerrainConstraint(tile_map_node, coords, p_terrain);
  1600. c.set_priority(10);
  1601. constraints.insert(c);
  1602. }
  1603. for (int i = 0; i < p_coords_array.size() - 1; i++) {
  1604. // Constraints on the peering bits.
  1605. TerrainConstraint c = TerrainConstraint(tile_map_node, p_coords_array[i], neighbor_list[i], p_terrain);
  1606. c.set_priority(10);
  1607. constraints.insert(c);
  1608. }
  1609. // Fills in the constraint list from existing tiles.
  1610. for (TerrainConstraint c : _get_terrain_constraints_from_painted_cells_list(painted_set, p_terrain_set, p_ignore_empty_terrains)) {
  1611. constraints.insert(c);
  1612. }
  1613. // Fill the terrains.
  1614. output = terrain_fill_constraints(can_modify_list, p_terrain_set, constraints);
  1615. return output;
  1616. }
  1617. HashMap<Vector2i, TileSet::TerrainsPattern> TileMapLayer::terrain_fill_pattern(const Vector<Vector2i> &p_coords_array, int p_terrain_set, TileSet::TerrainsPattern p_terrains_pattern, bool p_ignore_empty_terrains) {
  1618. HashMap<Vector2i, TileSet::TerrainsPattern> output;
  1619. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1620. ERR_FAIL_COND_V(!tile_set.is_valid(), output);
  1621. ERR_FAIL_INDEX_V(p_terrain_set, tile_set->get_terrain_sets_count(), output);
  1622. // Build list and set of tiles that can be modified (painted and their surroundings).
  1623. Vector<Vector2i> can_modify_list;
  1624. RBSet<Vector2i> can_modify_set;
  1625. RBSet<Vector2i> painted_set;
  1626. for (int i = p_coords_array.size() - 1; i >= 0; i--) {
  1627. const Vector2i &coords = p_coords_array[i];
  1628. can_modify_list.push_back(coords);
  1629. can_modify_set.insert(coords);
  1630. painted_set.insert(coords);
  1631. }
  1632. for (Vector2i coords : p_coords_array) {
  1633. // Find the adequate neighbor.
  1634. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  1635. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  1636. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, bit)) {
  1637. Vector2i neighbor = tile_map_node->get_neighbor_cell(coords, bit);
  1638. if (!can_modify_set.has(neighbor)) {
  1639. can_modify_list.push_back(neighbor);
  1640. can_modify_set.insert(neighbor);
  1641. }
  1642. }
  1643. }
  1644. }
  1645. // Add constraint by the new ones.
  1646. RBSet<TerrainConstraint> constraints;
  1647. // Add new constraints from the path drawn.
  1648. for (Vector2i coords : p_coords_array) {
  1649. // Constraints on the center bit.
  1650. RBSet<TerrainConstraint> added_constraints = _get_terrain_constraints_from_added_pattern(coords, p_terrain_set, p_terrains_pattern);
  1651. for (TerrainConstraint c : added_constraints) {
  1652. c.set_priority(10);
  1653. constraints.insert(c);
  1654. }
  1655. }
  1656. // Fills in the constraint list from modified tiles border.
  1657. for (TerrainConstraint c : _get_terrain_constraints_from_painted_cells_list(painted_set, p_terrain_set, p_ignore_empty_terrains)) {
  1658. constraints.insert(c);
  1659. }
  1660. // Fill the terrains.
  1661. output = terrain_fill_constraints(can_modify_list, p_terrain_set, constraints);
  1662. return output;
  1663. }
  1664. TileMapCell TileMapLayer::get_cell(const Vector2i &p_coords, bool p_use_proxies) const {
  1665. if (!tile_map.has(p_coords)) {
  1666. return TileMapCell();
  1667. } else {
  1668. TileMapCell c = tile_map.find(p_coords)->value.cell;
  1669. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1670. if (p_use_proxies && tile_set.is_valid()) {
  1671. Array proxyed = tile_set->map_tile_proxy(c.source_id, c.get_atlas_coords(), c.alternative_tile);
  1672. c.source_id = proxyed[0];
  1673. c.set_atlas_coords(proxyed[1]);
  1674. c.alternative_tile = proxyed[2];
  1675. }
  1676. return c;
  1677. }
  1678. }
  1679. void TileMapLayer::set_tile_data(TileMapDataFormat p_format, const Vector<int> &p_data) {
  1680. ERR_FAIL_COND(p_format > TileMapDataFormat::FORMAT_3);
  1681. // Set data for a given tile from raw data.
  1682. int c = p_data.size();
  1683. const int *r = p_data.ptr();
  1684. int offset = (p_format >= TileMapDataFormat::FORMAT_2) ? 3 : 2;
  1685. ERR_FAIL_COND_MSG(c % offset != 0, vformat("Corrupted tile data. Got size: %s. Expected modulo: %s", offset));
  1686. clear();
  1687. #ifdef DISABLE_DEPRECATED
  1688. ERR_FAIL_COND_MSG(p_format != TileMapDataFormat::FORMAT_3, vformat("Cannot handle deprecated TileMap data format version %d. This Godot version was compiled with no support for deprecated data.", p_format));
  1689. #endif
  1690. for (int i = 0; i < c; i += offset) {
  1691. const uint8_t *ptr = (const uint8_t *)&r[i];
  1692. uint8_t local[12];
  1693. for (int j = 0; j < ((p_format >= TileMapDataFormat::FORMAT_2) ? 12 : 8); j++) {
  1694. local[j] = ptr[j];
  1695. }
  1696. #ifdef BIG_ENDIAN_ENABLED
  1697. SWAP(local[0], local[3]);
  1698. SWAP(local[1], local[2]);
  1699. SWAP(local[4], local[7]);
  1700. SWAP(local[5], local[6]);
  1701. //TODO: ask someone to check this...
  1702. if (FORMAT >= FORMAT_2) {
  1703. SWAP(local[8], local[11]);
  1704. SWAP(local[9], local[10]);
  1705. }
  1706. #endif
  1707. // Extracts position in TileMap.
  1708. int16_t x = decode_uint16(&local[0]);
  1709. int16_t y = decode_uint16(&local[2]);
  1710. if (p_format == TileMapDataFormat::FORMAT_3) {
  1711. uint16_t source_id = decode_uint16(&local[4]);
  1712. uint16_t atlas_coords_x = decode_uint16(&local[6]);
  1713. uint16_t atlas_coords_y = decode_uint16(&local[8]);
  1714. uint16_t alternative_tile = decode_uint16(&local[10]);
  1715. set_cell(Vector2i(x, y), source_id, Vector2i(atlas_coords_x, atlas_coords_y), alternative_tile);
  1716. } else {
  1717. #ifndef DISABLE_DEPRECATED
  1718. // Previous decated format.
  1719. uint32_t v = decode_uint32(&local[4]);
  1720. // Extract the transform flags that used to be in the tilemap.
  1721. bool flip_h = v & (1UL << 29);
  1722. bool flip_v = v & (1UL << 30);
  1723. bool transpose = v & (1UL << 31);
  1724. v &= (1UL << 29) - 1;
  1725. // Extract autotile/atlas coords.
  1726. int16_t coord_x = 0;
  1727. int16_t coord_y = 0;
  1728. if (p_format == TileMapDataFormat::FORMAT_2) {
  1729. coord_x = decode_uint16(&local[8]);
  1730. coord_y = decode_uint16(&local[10]);
  1731. }
  1732. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1733. if (tile_set.is_valid()) {
  1734. Array a = tile_set->compatibility_tilemap_map(v, Vector2i(coord_x, coord_y), flip_h, flip_v, transpose);
  1735. if (a.size() == 3) {
  1736. set_cell(Vector2i(x, y), a[0], a[1], a[2]);
  1737. } else {
  1738. ERR_PRINT(vformat("No valid tile in Tileset for: tile:%s coords:%s flip_h:%s flip_v:%s transpose:%s", v, Vector2i(coord_x, coord_y), flip_h, flip_v, transpose));
  1739. }
  1740. } else {
  1741. int compatibility_alternative_tile = ((int)flip_h) + ((int)flip_v << 1) + ((int)transpose << 2);
  1742. set_cell(Vector2i(x, y), v, Vector2i(coord_x, coord_y), compatibility_alternative_tile);
  1743. }
  1744. #endif
  1745. }
  1746. }
  1747. }
  1748. Vector<int> TileMapLayer::get_tile_data() const {
  1749. // Export tile data to raw format.
  1750. Vector<int> tile_data;
  1751. tile_data.resize(tile_map.size() * 3);
  1752. int *w = tile_data.ptrw();
  1753. // Save in highest format.
  1754. int idx = 0;
  1755. for (const KeyValue<Vector2i, CellData> &E : tile_map) {
  1756. uint8_t *ptr = (uint8_t *)&w[idx];
  1757. encode_uint16((int16_t)(E.key.x), &ptr[0]);
  1758. encode_uint16((int16_t)(E.key.y), &ptr[2]);
  1759. encode_uint16(E.value.cell.source_id, &ptr[4]);
  1760. encode_uint16(E.value.cell.coord_x, &ptr[6]);
  1761. encode_uint16(E.value.cell.coord_y, &ptr[8]);
  1762. encode_uint16(E.value.cell.alternative_tile, &ptr[10]);
  1763. idx += 3;
  1764. }
  1765. return tile_data;
  1766. }
  1767. void TileMapLayer::notify_tile_map_change(DirtyFlags p_what) {
  1768. dirty.flags[p_what] = true;
  1769. tile_map_node->queue_internal_update();
  1770. _physics_notify_tilemap_change(p_what);
  1771. }
  1772. void TileMapLayer::internal_update() {
  1773. // Find TileData that need a runtime modification.
  1774. // This may add cells to the dirty list is a runtime modification has been notified.
  1775. _build_runtime_update_tile_data();
  1776. // Update all subsystems.
  1777. _rendering_update();
  1778. _physics_update();
  1779. _navigation_update();
  1780. _scenes_update();
  1781. #ifdef DEBUG_ENABLED
  1782. _debug_update();
  1783. #endif // DEBUG_ENABLED
  1784. _clear_runtime_update_tile_data();
  1785. // Clear the "what is dirty" flags.
  1786. for (int i = 0; i < DIRTY_FLAGS_MAX; i++) {
  1787. dirty.flags[i] = false;
  1788. }
  1789. // List the cells to delete definitely.
  1790. Vector<Vector2i> to_delete;
  1791. for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  1792. CellData &cell_data = *cell_data_list_element->self();
  1793. // Select the the cell from tile_map if it is invalid.
  1794. if (cell_data.cell.source_id == TileSet::INVALID_SOURCE) {
  1795. to_delete.push_back(cell_data.coords);
  1796. }
  1797. }
  1798. // Remove cells that are empty after the cleanup.
  1799. for (const Vector2i &coords : to_delete) {
  1800. tile_map.erase(coords);
  1801. }
  1802. // Clear the dirty cells list.
  1803. dirty.cell_list.clear();
  1804. }
  1805. void TileMapLayer::set_cell(const Vector2i &p_coords, int p_source_id, const Vector2i p_atlas_coords, int p_alternative_tile) {
  1806. // Set the current cell tile (using integer position).
  1807. Vector2i pk(p_coords);
  1808. HashMap<Vector2i, CellData>::Iterator E = tile_map.find(pk);
  1809. int source_id = p_source_id;
  1810. Vector2i atlas_coords = p_atlas_coords;
  1811. int alternative_tile = p_alternative_tile;
  1812. if ((source_id == TileSet::INVALID_SOURCE || atlas_coords == TileSetSource::INVALID_ATLAS_COORDS || alternative_tile == TileSetSource::INVALID_TILE_ALTERNATIVE) &&
  1813. (source_id != TileSet::INVALID_SOURCE || atlas_coords != TileSetSource::INVALID_ATLAS_COORDS || alternative_tile != TileSetSource::INVALID_TILE_ALTERNATIVE)) {
  1814. source_id = TileSet::INVALID_SOURCE;
  1815. atlas_coords = TileSetSource::INVALID_ATLAS_COORDS;
  1816. alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE;
  1817. }
  1818. if (!E) {
  1819. if (source_id == TileSet::INVALID_SOURCE) {
  1820. return; // Nothing to do, the tile is already empty.
  1821. }
  1822. // Insert a new cell in the tile map.
  1823. CellData new_cell_data;
  1824. new_cell_data.coords = pk;
  1825. E = tile_map.insert(pk, new_cell_data);
  1826. } else {
  1827. if (E->value.cell.source_id == source_id && E->value.cell.get_atlas_coords() == atlas_coords && E->value.cell.alternative_tile == alternative_tile) {
  1828. return; // Nothing changed.
  1829. }
  1830. }
  1831. TileMapCell &c = E->value.cell;
  1832. c.source_id = source_id;
  1833. c.set_atlas_coords(atlas_coords);
  1834. c.alternative_tile = alternative_tile;
  1835. // Make the given cell dirty.
  1836. if (!E->value.dirty_list_element.in_list()) {
  1837. dirty.cell_list.add(&(E->value.dirty_list_element));
  1838. }
  1839. tile_map_node->queue_internal_update();
  1840. used_rect_cache_dirty = true;
  1841. }
  1842. void TileMapLayer::erase_cell(const Vector2i &p_coords) {
  1843. set_cell(p_coords, TileSet::INVALID_SOURCE, TileSetSource::INVALID_ATLAS_COORDS, TileSetSource::INVALID_TILE_ALTERNATIVE);
  1844. }
  1845. int TileMapLayer::get_cell_source_id(const Vector2i &p_coords, bool p_use_proxies) const {
  1846. // Get a cell source id from position.
  1847. HashMap<Vector2i, CellData>::ConstIterator E = tile_map.find(p_coords);
  1848. if (!E) {
  1849. return TileSet::INVALID_SOURCE;
  1850. }
  1851. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1852. if (p_use_proxies && tile_set.is_valid()) {
  1853. Array proxyed = tile_set->map_tile_proxy(E->value.cell.source_id, E->value.cell.get_atlas_coords(), E->value.cell.alternative_tile);
  1854. return proxyed[0];
  1855. }
  1856. return E->value.cell.source_id;
  1857. }
  1858. Vector2i TileMapLayer::get_cell_atlas_coords(const Vector2i &p_coords, bool p_use_proxies) const {
  1859. // Get a cell source id from position.
  1860. HashMap<Vector2i, CellData>::ConstIterator E = tile_map.find(p_coords);
  1861. if (!E) {
  1862. return TileSetSource::INVALID_ATLAS_COORDS;
  1863. }
  1864. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1865. if (p_use_proxies && tile_set.is_valid()) {
  1866. Array proxyed = tile_set->map_tile_proxy(E->value.cell.source_id, E->value.cell.get_atlas_coords(), E->value.cell.alternative_tile);
  1867. return proxyed[1];
  1868. }
  1869. return E->value.cell.get_atlas_coords();
  1870. }
  1871. int TileMapLayer::get_cell_alternative_tile(const Vector2i &p_coords, bool p_use_proxies) const {
  1872. // Get a cell source id from position.
  1873. HashMap<Vector2i, CellData>::ConstIterator E = tile_map.find(p_coords);
  1874. if (!E) {
  1875. return TileSetSource::INVALID_TILE_ALTERNATIVE;
  1876. }
  1877. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1878. if (p_use_proxies && tile_set.is_valid()) {
  1879. Array proxyed = tile_set->map_tile_proxy(E->value.cell.source_id, E->value.cell.get_atlas_coords(), E->value.cell.alternative_tile);
  1880. return proxyed[2];
  1881. }
  1882. return E->value.cell.alternative_tile;
  1883. }
  1884. TileData *TileMapLayer::get_cell_tile_data(const Vector2i &p_coords, bool p_use_proxies) const {
  1885. int source_id = get_cell_source_id(p_coords, p_use_proxies);
  1886. if (source_id == TileSet::INVALID_SOURCE) {
  1887. return nullptr;
  1888. }
  1889. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1890. Ref<TileSetAtlasSource> source = tile_set->get_source(source_id);
  1891. if (source.is_valid()) {
  1892. return source->get_tile_data(get_cell_atlas_coords(p_coords, p_use_proxies), get_cell_alternative_tile(p_coords, p_use_proxies));
  1893. }
  1894. return nullptr;
  1895. }
  1896. void TileMapLayer::clear() {
  1897. // Remove all tiles.
  1898. for (KeyValue<Vector2i, CellData> &kv : tile_map) {
  1899. erase_cell(kv.key);
  1900. }
  1901. used_rect_cache_dirty = true;
  1902. }
  1903. Ref<TileMapPattern> TileMapLayer::get_pattern(TypedArray<Vector2i> p_coords_array) {
  1904. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1905. ERR_FAIL_COND_V(!tile_set.is_valid(), nullptr);
  1906. Ref<TileMapPattern> output;
  1907. output.instantiate();
  1908. if (p_coords_array.is_empty()) {
  1909. return output;
  1910. }
  1911. Vector2i min = Vector2i(p_coords_array[0]);
  1912. for (int i = 1; i < p_coords_array.size(); i++) {
  1913. min = min.min(p_coords_array[i]);
  1914. }
  1915. Vector<Vector2i> coords_in_pattern_array;
  1916. coords_in_pattern_array.resize(p_coords_array.size());
  1917. Vector2i ensure_positive_offset;
  1918. for (int i = 0; i < p_coords_array.size(); i++) {
  1919. Vector2i coords = p_coords_array[i];
  1920. Vector2i coords_in_pattern = coords - min;
  1921. if (tile_set->get_tile_shape() != TileSet::TILE_SHAPE_SQUARE) {
  1922. if (tile_set->get_tile_layout() == TileSet::TILE_LAYOUT_STACKED) {
  1923. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL && bool(min.y % 2) && bool(coords_in_pattern.y % 2)) {
  1924. coords_in_pattern.x -= 1;
  1925. if (coords_in_pattern.x < 0) {
  1926. ensure_positive_offset.x = 1;
  1927. }
  1928. } else if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_VERTICAL && bool(min.x % 2) && bool(coords_in_pattern.x % 2)) {
  1929. coords_in_pattern.y -= 1;
  1930. if (coords_in_pattern.y < 0) {
  1931. ensure_positive_offset.y = 1;
  1932. }
  1933. }
  1934. } else if (tile_set->get_tile_layout() == TileSet::TILE_LAYOUT_STACKED_OFFSET) {
  1935. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL && bool(min.y % 2) && bool(coords_in_pattern.y % 2)) {
  1936. coords_in_pattern.x += 1;
  1937. } else if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_VERTICAL && bool(min.x % 2) && bool(coords_in_pattern.x % 2)) {
  1938. coords_in_pattern.y += 1;
  1939. }
  1940. }
  1941. }
  1942. coords_in_pattern_array.write[i] = coords_in_pattern;
  1943. }
  1944. for (int i = 0; i < coords_in_pattern_array.size(); i++) {
  1945. Vector2i coords = p_coords_array[i];
  1946. Vector2i coords_in_pattern = coords_in_pattern_array[i];
  1947. output->set_cell(coords_in_pattern + ensure_positive_offset, get_cell_source_id(coords), get_cell_atlas_coords(coords), get_cell_alternative_tile(coords));
  1948. }
  1949. return output;
  1950. }
  1951. void TileMapLayer::set_pattern(const Vector2i &p_position, const Ref<TileMapPattern> p_pattern) {
  1952. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1953. ERR_FAIL_COND(tile_set.is_null());
  1954. ERR_FAIL_COND(p_pattern.is_null());
  1955. TypedArray<Vector2i> used_cells = p_pattern->get_used_cells();
  1956. for (int i = 0; i < used_cells.size(); i++) {
  1957. Vector2i coords = tile_map_node->map_pattern(p_position, used_cells[i], p_pattern);
  1958. set_cell(coords, p_pattern->get_cell_source_id(used_cells[i]), p_pattern->get_cell_atlas_coords(used_cells[i]), p_pattern->get_cell_alternative_tile(used_cells[i]));
  1959. }
  1960. }
  1961. void TileMapLayer::set_cells_terrain_connect(TypedArray<Vector2i> p_cells, int p_terrain_set, int p_terrain, bool p_ignore_empty_terrains) {
  1962. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  1963. ERR_FAIL_COND(!tile_set.is_valid());
  1964. ERR_FAIL_INDEX(p_terrain_set, tile_set->get_terrain_sets_count());
  1965. Vector<Vector2i> cells_vector;
  1966. HashSet<Vector2i> painted_set;
  1967. for (int i = 0; i < p_cells.size(); i++) {
  1968. cells_vector.push_back(p_cells[i]);
  1969. painted_set.insert(p_cells[i]);
  1970. }
  1971. HashMap<Vector2i, TileSet::TerrainsPattern> terrain_fill_output = terrain_fill_connect(cells_vector, p_terrain_set, p_terrain, p_ignore_empty_terrains);
  1972. for (const KeyValue<Vector2i, TileSet::TerrainsPattern> &kv : terrain_fill_output) {
  1973. if (painted_set.has(kv.key)) {
  1974. // Paint a random tile with the correct terrain for the painted path.
  1975. TileMapCell c = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value);
  1976. set_cell(kv.key, c.source_id, c.get_atlas_coords(), c.alternative_tile);
  1977. } else {
  1978. // Avoids updating the painted path from the output if the new pattern is the same as before.
  1979. TileSet::TerrainsPattern in_map_terrain_pattern = TileSet::TerrainsPattern(*tile_set, p_terrain_set);
  1980. TileMapCell cell = get_cell(kv.key);
  1981. if (cell.source_id != TileSet::INVALID_SOURCE) {
  1982. TileSetSource *source = *tile_set->get_source(cell.source_id);
  1983. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  1984. if (atlas_source) {
  1985. // Get tile data.
  1986. TileData *tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  1987. if (tile_data && tile_data->get_terrain_set() == p_terrain_set) {
  1988. in_map_terrain_pattern = tile_data->get_terrains_pattern();
  1989. }
  1990. }
  1991. }
  1992. if (in_map_terrain_pattern != kv.value) {
  1993. TileMapCell c = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value);
  1994. set_cell(kv.key, c.source_id, c.get_atlas_coords(), c.alternative_tile);
  1995. }
  1996. }
  1997. }
  1998. }
  1999. void TileMapLayer::set_cells_terrain_path(TypedArray<Vector2i> p_path, int p_terrain_set, int p_terrain, bool p_ignore_empty_terrains) {
  2000. const Ref<TileSet> &tile_set = tile_map_node->get_tileset();
  2001. ERR_FAIL_COND(!tile_set.is_valid());
  2002. ERR_FAIL_INDEX(p_terrain_set, tile_set->get_terrain_sets_count());
  2003. Vector<Vector2i> vector_path;
  2004. HashSet<Vector2i> painted_set;
  2005. for (int i = 0; i < p_path.size(); i++) {
  2006. vector_path.push_back(p_path[i]);
  2007. painted_set.insert(p_path[i]);
  2008. }
  2009. HashMap<Vector2i, TileSet::TerrainsPattern> terrain_fill_output = terrain_fill_path(vector_path, p_terrain_set, p_terrain, p_ignore_empty_terrains);
  2010. for (const KeyValue<Vector2i, TileSet::TerrainsPattern> &kv : terrain_fill_output) {
  2011. if (painted_set.has(kv.key)) {
  2012. // Paint a random tile with the correct terrain for the painted path.
  2013. TileMapCell c = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value);
  2014. set_cell(kv.key, c.source_id, c.get_atlas_coords(), c.alternative_tile);
  2015. } else {
  2016. // Avoids updating the painted path from the output if the new pattern is the same as before.
  2017. TileSet::TerrainsPattern in_map_terrain_pattern = TileSet::TerrainsPattern(*tile_set, p_terrain_set);
  2018. TileMapCell cell = get_cell(kv.key);
  2019. if (cell.source_id != TileSet::INVALID_SOURCE) {
  2020. TileSetSource *source = *tile_set->get_source(cell.source_id);
  2021. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  2022. if (atlas_source) {
  2023. // Get tile data.
  2024. TileData *tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  2025. if (tile_data && tile_data->get_terrain_set() == p_terrain_set) {
  2026. in_map_terrain_pattern = tile_data->get_terrains_pattern();
  2027. }
  2028. }
  2029. }
  2030. if (in_map_terrain_pattern != kv.value) {
  2031. TileMapCell c = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value);
  2032. set_cell(kv.key, c.source_id, c.get_atlas_coords(), c.alternative_tile);
  2033. }
  2034. }
  2035. }
  2036. }
  2037. TypedArray<Vector2i> TileMapLayer::get_used_cells() const {
  2038. // Returns the cells used in the tilemap.
  2039. TypedArray<Vector2i> a;
  2040. for (const KeyValue<Vector2i, CellData> &E : tile_map) {
  2041. const TileMapCell &c = E.value.cell;
  2042. if (c.source_id == TileSet::INVALID_SOURCE) {
  2043. continue;
  2044. }
  2045. a.push_back(E.key);
  2046. }
  2047. return a;
  2048. }
  2049. TypedArray<Vector2i> TileMapLayer::get_used_cells_by_id(int p_source_id, const Vector2i p_atlas_coords, int p_alternative_tile) const {
  2050. // Returns the cells used in the tilemap.
  2051. TypedArray<Vector2i> a;
  2052. for (const KeyValue<Vector2i, CellData> &E : tile_map) {
  2053. const TileMapCell &c = E.value.cell;
  2054. if (c.source_id == TileSet::INVALID_SOURCE) {
  2055. continue;
  2056. }
  2057. if ((p_source_id == TileSet::INVALID_SOURCE || p_source_id == c.source_id) &&
  2058. (p_atlas_coords == TileSetSource::INVALID_ATLAS_COORDS || p_atlas_coords == c.get_atlas_coords()) &&
  2059. (p_alternative_tile == TileSetSource::INVALID_TILE_ALTERNATIVE || p_alternative_tile == c.alternative_tile)) {
  2060. a.push_back(E.key);
  2061. }
  2062. }
  2063. return a;
  2064. }
  2065. Rect2i TileMapLayer::get_used_rect() const {
  2066. // Return the rect of the currently used area.
  2067. if (used_rect_cache_dirty) {
  2068. used_rect_cache = Rect2i();
  2069. bool first = true;
  2070. for (const KeyValue<Vector2i, CellData> &E : tile_map) {
  2071. const TileMapCell &c = E.value.cell;
  2072. if (c.source_id == TileSet::INVALID_SOURCE) {
  2073. continue;
  2074. }
  2075. if (first) {
  2076. used_rect_cache = Rect2i(E.key.x, E.key.y, 0, 0);
  2077. first = false;
  2078. } else {
  2079. used_rect_cache.expand_to(E.key);
  2080. }
  2081. }
  2082. if (!first) {
  2083. // Only if we have at least one cell.
  2084. // The cache expands to top-left coordinate, so we add one full tile.
  2085. used_rect_cache.size += Vector2i(1, 1);
  2086. }
  2087. used_rect_cache_dirty = false;
  2088. }
  2089. return used_rect_cache;
  2090. }
  2091. void TileMapLayer::set_name(String p_name) {
  2092. if (name == p_name) {
  2093. return;
  2094. }
  2095. name = p_name;
  2096. tile_map_node->emit_signal(CoreStringNames::get_singleton()->changed);
  2097. }
  2098. String TileMapLayer::get_name() const {
  2099. return name;
  2100. }
  2101. void TileMapLayer::set_enabled(bool p_enabled) {
  2102. if (enabled == p_enabled) {
  2103. return;
  2104. }
  2105. enabled = p_enabled;
  2106. dirty.flags[DIRTY_FLAGS_LAYER_ENABLED] = true;
  2107. tile_map_node->queue_internal_update();
  2108. tile_map_node->emit_signal(CoreStringNames::get_singleton()->changed);
  2109. tile_map_node->update_configuration_warnings();
  2110. }
  2111. bool TileMapLayer::is_enabled() const {
  2112. return enabled;
  2113. }
  2114. void TileMapLayer::set_modulate(Color p_modulate) {
  2115. if (modulate == p_modulate) {
  2116. return;
  2117. }
  2118. modulate = p_modulate;
  2119. dirty.flags[DIRTY_FLAGS_LAYER_MODULATE] = true;
  2120. tile_map_node->queue_internal_update();
  2121. tile_map_node->emit_signal(CoreStringNames::get_singleton()->changed);
  2122. }
  2123. Color TileMapLayer::get_modulate() const {
  2124. return modulate;
  2125. }
  2126. void TileMapLayer::set_y_sort_enabled(bool p_y_sort_enabled) {
  2127. if (y_sort_enabled == p_y_sort_enabled) {
  2128. return;
  2129. }
  2130. y_sort_enabled = p_y_sort_enabled;
  2131. dirty.flags[DIRTY_FLAGS_LAYER_Y_SORT_ENABLED] = true;
  2132. tile_map_node->queue_internal_update();
  2133. tile_map_node->emit_signal(CoreStringNames::get_singleton()->changed);
  2134. tile_map_node->update_configuration_warnings();
  2135. }
  2136. bool TileMapLayer::is_y_sort_enabled() const {
  2137. return y_sort_enabled;
  2138. }
  2139. void TileMapLayer::set_y_sort_origin(int p_y_sort_origin) {
  2140. if (y_sort_origin == p_y_sort_origin) {
  2141. return;
  2142. }
  2143. y_sort_origin = p_y_sort_origin;
  2144. dirty.flags[DIRTY_FLAGS_LAYER_Y_SORT_ORIGIN] = true;
  2145. tile_map_node->queue_internal_update();
  2146. tile_map_node->emit_signal(CoreStringNames::get_singleton()->changed);
  2147. }
  2148. int TileMapLayer::get_y_sort_origin() const {
  2149. return y_sort_origin;
  2150. }
  2151. void TileMapLayer::set_z_index(int p_z_index) {
  2152. if (z_index == p_z_index) {
  2153. return;
  2154. }
  2155. z_index = p_z_index;
  2156. dirty.flags[DIRTY_FLAGS_LAYER_Z_INDEX] = true;
  2157. tile_map_node->queue_internal_update();
  2158. tile_map_node->emit_signal(CoreStringNames::get_singleton()->changed);
  2159. tile_map_node->update_configuration_warnings();
  2160. }
  2161. int TileMapLayer::get_z_index() const {
  2162. return z_index;
  2163. }
  2164. void TileMapLayer::set_navigation_enabled(bool p_enabled) {
  2165. if (navigation_enabled == p_enabled) {
  2166. return;
  2167. }
  2168. navigation_enabled = p_enabled;
  2169. dirty.flags[DIRTY_FLAGS_LAYER_NAVIGATION_ENABLED] = true;
  2170. tile_map_node->queue_internal_update();
  2171. tile_map_node->emit_signal(CoreStringNames::get_singleton()->changed);
  2172. }
  2173. bool TileMapLayer::is_navigation_enabled() const {
  2174. return navigation_enabled;
  2175. }
  2176. void TileMapLayer::set_navigation_map(RID p_map) {
  2177. ERR_FAIL_COND_MSG(!tile_map_node->is_inside_tree(), "A TileMap navigation map can only be changed while inside the SceneTree.");
  2178. navigation_map = p_map;
  2179. uses_world_navigation_map = p_map == tile_map_node->get_world_2d()->get_navigation_map();
  2180. }
  2181. RID TileMapLayer::get_navigation_map() const {
  2182. if (navigation_map.is_valid()) {
  2183. return navigation_map;
  2184. }
  2185. return RID();
  2186. }
  2187. void TileMapLayer::fix_invalid_tiles() {
  2188. Ref<TileSet> tileset = tile_map_node->get_tileset();
  2189. ERR_FAIL_COND_MSG(tileset.is_null(), "Cannot call fix_invalid_tiles() on a TileMap without a valid TileSet.");
  2190. RBSet<Vector2i> coords;
  2191. for (const KeyValue<Vector2i, CellData> &E : tile_map) {
  2192. TileSetSource *source = *tileset->get_source(E.value.cell.source_id);
  2193. if (!source || !source->has_tile(E.value.cell.get_atlas_coords()) || !source->has_alternative_tile(E.value.cell.get_atlas_coords(), E.value.cell.alternative_tile)) {
  2194. coords.insert(E.key);
  2195. }
  2196. }
  2197. for (const Vector2i &E : coords) {
  2198. set_cell(E, TileSet::INVALID_SOURCE, TileSetSource::INVALID_ATLAS_COORDS, TileSetSource::INVALID_TILE_ALTERNATIVE);
  2199. }
  2200. }
  2201. bool TileMapLayer::has_body_rid(RID p_physics_body) const {
  2202. return bodies_coords.has(p_physics_body);
  2203. }
  2204. Vector2i TileMapLayer::get_coords_for_body_rid(RID p_physics_body) const {
  2205. return bodies_coords[p_physics_body];
  2206. }
  2207. TileMapLayer::~TileMapLayer() {
  2208. if (!tile_map_node) {
  2209. // Temporary layer.
  2210. return;
  2211. }
  2212. in_destructor = true;
  2213. clear();
  2214. internal_update();
  2215. }
  2216. HashMap<Vector2i, TileSet::CellNeighbor> TerrainConstraint::get_overlapping_coords_and_peering_bits() const {
  2217. HashMap<Vector2i, TileSet::CellNeighbor> output;
  2218. ERR_FAIL_COND_V(is_center_bit(), output);
  2219. Ref<TileSet> ts = tile_map->get_tileset();
  2220. ERR_FAIL_COND_V(!ts.is_valid(), output);
  2221. TileSet::TileShape shape = ts->get_tile_shape();
  2222. if (shape == TileSet::TILE_SHAPE_SQUARE) {
  2223. switch (bit) {
  2224. case 1:
  2225. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_RIGHT_SIDE;
  2226. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_LEFT_SIDE;
  2227. break;
  2228. case 2:
  2229. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER;
  2230. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER;
  2231. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER;
  2232. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER;
  2233. break;
  2234. case 3:
  2235. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_SIDE;
  2236. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_SIDE;
  2237. break;
  2238. default:
  2239. ERR_FAIL_V(output);
  2240. }
  2241. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  2242. switch (bit) {
  2243. case 1:
  2244. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE;
  2245. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE;
  2246. break;
  2247. case 2:
  2248. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_CORNER;
  2249. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_LEFT_CORNER;
  2250. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_CORNER)] = TileSet::CELL_NEIGHBOR_TOP_CORNER;
  2251. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE)] = TileSet::CELL_NEIGHBOR_RIGHT_CORNER;
  2252. break;
  2253. case 3:
  2254. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE;
  2255. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE;
  2256. break;
  2257. default:
  2258. ERR_FAIL_V(output);
  2259. }
  2260. } else {
  2261. // Half offset shapes.
  2262. TileSet::TileOffsetAxis offset_axis = ts->get_tile_offset_axis();
  2263. if (offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  2264. switch (bit) {
  2265. case 1:
  2266. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_RIGHT_SIDE;
  2267. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_LEFT_SIDE;
  2268. break;
  2269. case 2:
  2270. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER;
  2271. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER;
  2272. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_CORNER;
  2273. break;
  2274. case 3:
  2275. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE;
  2276. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE;
  2277. break;
  2278. case 4:
  2279. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_CORNER;
  2280. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER;
  2281. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER;
  2282. break;
  2283. case 5:
  2284. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE;
  2285. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE;
  2286. break;
  2287. default:
  2288. ERR_FAIL_V(output);
  2289. }
  2290. } else {
  2291. switch (bit) {
  2292. case 1:
  2293. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_RIGHT_CORNER;
  2294. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER;
  2295. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER;
  2296. break;
  2297. case 2:
  2298. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE;
  2299. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE;
  2300. break;
  2301. case 3:
  2302. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER;
  2303. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_LEFT_CORNER;
  2304. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER;
  2305. break;
  2306. case 4:
  2307. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_SIDE;
  2308. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_SIDE;
  2309. break;
  2310. case 5:
  2311. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE;
  2312. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE;
  2313. break;
  2314. default:
  2315. ERR_FAIL_V(output);
  2316. }
  2317. }
  2318. }
  2319. return output;
  2320. }
  2321. TerrainConstraint::TerrainConstraint(const TileMap *p_tile_map, const Vector2i &p_position, int p_terrain) {
  2322. tile_map = p_tile_map;
  2323. Ref<TileSet> ts = tile_map->get_tileset();
  2324. ERR_FAIL_COND(!ts.is_valid());
  2325. bit = 0;
  2326. base_cell_coords = p_position;
  2327. terrain = p_terrain;
  2328. }
  2329. TerrainConstraint::TerrainConstraint(const TileMap *p_tile_map, const Vector2i &p_position, const TileSet::CellNeighbor &p_bit, int p_terrain) {
  2330. // The way we build the constraint make it easy to detect conflicting constraints.
  2331. tile_map = p_tile_map;
  2332. Ref<TileSet> ts = tile_map->get_tileset();
  2333. ERR_FAIL_COND(!ts.is_valid());
  2334. TileSet::TileShape shape = ts->get_tile_shape();
  2335. if (shape == TileSet::TILE_SHAPE_SQUARE) {
  2336. switch (p_bit) {
  2337. case TileSet::CELL_NEIGHBOR_RIGHT_SIDE:
  2338. bit = 1;
  2339. base_cell_coords = p_position;
  2340. break;
  2341. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  2342. bit = 2;
  2343. base_cell_coords = p_position;
  2344. break;
  2345. case TileSet::CELL_NEIGHBOR_BOTTOM_SIDE:
  2346. bit = 3;
  2347. base_cell_coords = p_position;
  2348. break;
  2349. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  2350. bit = 2;
  2351. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_LEFT_SIDE);
  2352. break;
  2353. case TileSet::CELL_NEIGHBOR_LEFT_SIDE:
  2354. bit = 1;
  2355. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_LEFT_SIDE);
  2356. break;
  2357. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  2358. bit = 2;
  2359. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER);
  2360. break;
  2361. case TileSet::CELL_NEIGHBOR_TOP_SIDE:
  2362. bit = 3;
  2363. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_SIDE);
  2364. break;
  2365. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  2366. bit = 2;
  2367. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_SIDE);
  2368. break;
  2369. default:
  2370. ERR_FAIL();
  2371. break;
  2372. }
  2373. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  2374. switch (p_bit) {
  2375. case TileSet::CELL_NEIGHBOR_RIGHT_CORNER:
  2376. bit = 2;
  2377. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE);
  2378. break;
  2379. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  2380. bit = 1;
  2381. base_cell_coords = p_position;
  2382. break;
  2383. case TileSet::CELL_NEIGHBOR_BOTTOM_CORNER:
  2384. bit = 2;
  2385. base_cell_coords = p_position;
  2386. break;
  2387. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  2388. bit = 3;
  2389. base_cell_coords = p_position;
  2390. break;
  2391. case TileSet::CELL_NEIGHBOR_LEFT_CORNER:
  2392. bit = 2;
  2393. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  2394. break;
  2395. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  2396. bit = 1;
  2397. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  2398. break;
  2399. case TileSet::CELL_NEIGHBOR_TOP_CORNER:
  2400. bit = 2;
  2401. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_CORNER);
  2402. break;
  2403. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  2404. bit = 3;
  2405. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE);
  2406. break;
  2407. default:
  2408. ERR_FAIL();
  2409. break;
  2410. }
  2411. } else {
  2412. // Half-offset shapes.
  2413. TileSet::TileOffsetAxis offset_axis = ts->get_tile_offset_axis();
  2414. if (offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  2415. switch (p_bit) {
  2416. case TileSet::CELL_NEIGHBOR_RIGHT_SIDE:
  2417. bit = 1;
  2418. base_cell_coords = p_position;
  2419. break;
  2420. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  2421. bit = 2;
  2422. base_cell_coords = p_position;
  2423. break;
  2424. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  2425. bit = 3;
  2426. base_cell_coords = p_position;
  2427. break;
  2428. case TileSet::CELL_NEIGHBOR_BOTTOM_CORNER:
  2429. bit = 4;
  2430. base_cell_coords = p_position;
  2431. break;
  2432. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  2433. bit = 5;
  2434. base_cell_coords = p_position;
  2435. break;
  2436. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  2437. bit = 2;
  2438. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_LEFT_SIDE);
  2439. break;
  2440. case TileSet::CELL_NEIGHBOR_LEFT_SIDE:
  2441. bit = 1;
  2442. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_LEFT_SIDE);
  2443. break;
  2444. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  2445. bit = 4;
  2446. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  2447. break;
  2448. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  2449. bit = 3;
  2450. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  2451. break;
  2452. case TileSet::CELL_NEIGHBOR_TOP_CORNER:
  2453. bit = 2;
  2454. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  2455. break;
  2456. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  2457. bit = 5;
  2458. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE);
  2459. break;
  2460. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  2461. bit = 4;
  2462. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE);
  2463. break;
  2464. default:
  2465. ERR_FAIL();
  2466. break;
  2467. }
  2468. } else {
  2469. switch (p_bit) {
  2470. case TileSet::CELL_NEIGHBOR_RIGHT_CORNER:
  2471. bit = 1;
  2472. base_cell_coords = p_position;
  2473. break;
  2474. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  2475. bit = 2;
  2476. base_cell_coords = p_position;
  2477. break;
  2478. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  2479. bit = 3;
  2480. base_cell_coords = p_position;
  2481. break;
  2482. case TileSet::CELL_NEIGHBOR_BOTTOM_SIDE:
  2483. bit = 4;
  2484. base_cell_coords = p_position;
  2485. break;
  2486. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  2487. bit = 1;
  2488. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE);
  2489. break;
  2490. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  2491. bit = 5;
  2492. base_cell_coords = p_position;
  2493. break;
  2494. case TileSet::CELL_NEIGHBOR_LEFT_CORNER:
  2495. bit = 3;
  2496. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  2497. break;
  2498. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  2499. bit = 2;
  2500. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  2501. break;
  2502. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  2503. bit = 1;
  2504. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  2505. break;
  2506. case TileSet::CELL_NEIGHBOR_TOP_SIDE:
  2507. bit = 4;
  2508. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_SIDE);
  2509. break;
  2510. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  2511. bit = 3;
  2512. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_SIDE);
  2513. break;
  2514. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  2515. bit = 5;
  2516. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE);
  2517. break;
  2518. default:
  2519. ERR_FAIL();
  2520. break;
  2521. }
  2522. }
  2523. }
  2524. terrain = p_terrain;
  2525. }