tile_map_layer.cpp 131 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388
  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/io/marshalls.h"
  32. #include "scene/2d/tile_map.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. /////////////////////////////// Debug //////////////////////////////////////////
  38. constexpr int TILE_MAP_DEBUG_QUADRANT_SIZE = 16;
  39. Vector2i TileMapLayer::_coords_to_debug_quadrant_coords(const Vector2i &p_coords) const {
  40. return Vector2i(
  41. 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,
  42. 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);
  43. }
  44. void TileMapLayer::_debug_update(bool p_force_cleanup) {
  45. RenderingServer *rs = RenderingServer::get_singleton();
  46. // Check if we should cleanup everything.
  47. bool forced_cleanup = p_force_cleanup || !enabled || tile_set.is_null() || !is_visible_in_tree();
  48. if (forced_cleanup) {
  49. for (KeyValue<Vector2i, Ref<DebugQuadrant>> &kv : debug_quadrant_map) {
  50. // Free the quadrant.
  51. Ref<DebugQuadrant> &debug_quadrant = kv.value;
  52. if (debug_quadrant->canvas_item.is_valid()) {
  53. rs->free(debug_quadrant->canvas_item);
  54. }
  55. }
  56. debug_quadrant_map.clear();
  57. _debug_was_cleaned_up = true;
  58. return;
  59. }
  60. // Check if anything is dirty, in such a case, redraw debug.
  61. bool anything_changed = false;
  62. for (int i = 0; i < DIRTY_FLAGS_MAX; i++) {
  63. if (dirty.flags[i]) {
  64. anything_changed = true;
  65. break;
  66. }
  67. }
  68. // List all debug quadrants to update, creating new ones if needed.
  69. SelfList<DebugQuadrant>::List dirty_debug_quadrant_list;
  70. if (_debug_was_cleaned_up || anything_changed) {
  71. // Update all cells.
  72. for (KeyValue<Vector2i, CellData> &kv : tile_map_layer_data) {
  73. CellData &cell_data = kv.value;
  74. _debug_quadrants_update_cell(cell_data, dirty_debug_quadrant_list);
  75. }
  76. } else {
  77. // Update dirty cells.
  78. for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  79. CellData &cell_data = *cell_data_list_element->self();
  80. _debug_quadrants_update_cell(cell_data, dirty_debug_quadrant_list);
  81. }
  82. }
  83. // Update those quadrants.
  84. bool needs_set_not_interpolated = is_inside_tree() && get_tree()->is_physics_interpolation_enabled() && !is_physics_interpolated();
  85. for (SelfList<DebugQuadrant> *quadrant_list_element = dirty_debug_quadrant_list.first(); quadrant_list_element;) {
  86. SelfList<DebugQuadrant> *next_quadrant_list_element = quadrant_list_element->next(); // "Hack" to clear the list while iterating.
  87. DebugQuadrant &debug_quadrant = *quadrant_list_element->self();
  88. // Check if the quadrant has a tile.
  89. bool has_a_tile = false;
  90. RID &ci = debug_quadrant.canvas_item;
  91. for (SelfList<CellData> *cell_data_list_element = debug_quadrant.cells.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  92. CellData &cell_data = *cell_data_list_element->self();
  93. if (cell_data.cell.source_id != TileSet::INVALID_SOURCE) {
  94. has_a_tile = true;
  95. break;
  96. }
  97. }
  98. if (has_a_tile) {
  99. // Update the quadrant.
  100. if (ci.is_valid()) {
  101. rs->canvas_item_clear(ci);
  102. } else {
  103. ci = rs->canvas_item_create();
  104. if (needs_set_not_interpolated) {
  105. rs->canvas_item_set_interpolated(ci, false);
  106. }
  107. rs->canvas_item_set_z_index(ci, RS::CANVAS_ITEM_Z_MAX - 1);
  108. rs->canvas_item_set_parent(ci, get_canvas_item());
  109. }
  110. const Vector2 quadrant_pos = tile_set->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(bool p_force_cleanup) {
  157. RenderingServer *rs = RenderingServer::get_singleton();
  158. // Check if we should cleanup everything.
  159. bool forced_cleanup = p_force_cleanup || !enabled || tile_set.is_null() || !is_visible_in_tree();
  160. // ----------- Layer level processing -----------
  161. if (!forced_cleanup) {
  162. // Modulate the layer.
  163. Color layer_modulate = get_modulate();
  164. #ifdef TOOLS_ENABLED
  165. if (highlight_mode == HIGHLIGHT_MODE_BELOW) {
  166. layer_modulate = layer_modulate.darkened(0.5);
  167. } else if (highlight_mode == HIGHLIGHT_MODE_ABOVE) {
  168. layer_modulate = layer_modulate.darkened(0.5);
  169. layer_modulate.a *= 0.3;
  170. }
  171. #endif // TOOLS_ENABLED
  172. rs->canvas_item_set_modulate(get_canvas_item(), layer_modulate);
  173. }
  174. // ----------- Quadrants processing -----------
  175. // List all rendering quadrants to update, creating new ones if needed.
  176. SelfList<RenderingQuadrant>::List dirty_rendering_quadrant_list;
  177. // Check if anything changed that might change the quadrant shape.
  178. // If so, recreate everything.
  179. bool quadrant_shape_changed = dirty.flags[DIRTY_FLAGS_LAYER_Y_SORT_ENABLED] || dirty.flags[DIRTY_FLAGS_TILE_SET] ||
  180. (is_y_sort_enabled() && (dirty.flags[DIRTY_FLAGS_LAYER_Y_SORT_ORIGIN] || dirty.flags[DIRTY_FLAGS_LAYER_X_DRAW_ORDER_REVERSED] || dirty.flags[DIRTY_FLAGS_LAYER_LOCAL_TRANSFORM])) ||
  181. (!is_y_sort_enabled() && dirty.flags[DIRTY_FLAGS_LAYER_RENDERING_QUADRANT_SIZE]);
  182. // Free all quadrants.
  183. if (forced_cleanup || quadrant_shape_changed) {
  184. for (const KeyValue<Vector2i, Ref<RenderingQuadrant>> &kv : rendering_quadrant_map) {
  185. for (const RID &ci : kv.value->canvas_items) {
  186. if (ci.is_valid()) {
  187. rs->free(ci);
  188. }
  189. }
  190. kv.value->cells.clear();
  191. }
  192. rendering_quadrant_map.clear();
  193. _rendering_was_cleaned_up = true;
  194. }
  195. if (!forced_cleanup) {
  196. // List all quadrants to update, recreating them if needed.
  197. if (dirty.flags[DIRTY_FLAGS_TILE_SET] || dirty.flags[DIRTY_FLAGS_LAYER_IN_TREE] || _rendering_was_cleaned_up) {
  198. // Update all cells.
  199. for (KeyValue<Vector2i, CellData> &kv : tile_map_layer_data) {
  200. CellData &cell_data = kv.value;
  201. _rendering_quadrants_update_cell(cell_data, dirty_rendering_quadrant_list);
  202. }
  203. } else {
  204. // Update dirty cells.
  205. for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  206. CellData &cell_data = *cell_data_list_element->self();
  207. _rendering_quadrants_update_cell(cell_data, dirty_rendering_quadrant_list);
  208. }
  209. }
  210. // Update all dirty quadrants.
  211. bool needs_set_not_interpolated = is_inside_tree() && get_tree()->is_physics_interpolation_enabled() && !is_physics_interpolated();
  212. for (SelfList<RenderingQuadrant> *quadrant_list_element = dirty_rendering_quadrant_list.first(); quadrant_list_element;) {
  213. SelfList<RenderingQuadrant> *next_quadrant_list_element = quadrant_list_element->next(); // "Hack" to clear the list while iterating.
  214. const Ref<RenderingQuadrant> &rendering_quadrant = quadrant_list_element->self();
  215. // Check if the quadrant has a tile.
  216. bool has_a_tile = false;
  217. for (SelfList<CellData> *cell_data_list_element = rendering_quadrant->cells.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  218. CellData &cell_data = *cell_data_list_element->self();
  219. if (cell_data.cell.source_id != TileSet::INVALID_SOURCE) {
  220. has_a_tile = true;
  221. break;
  222. }
  223. }
  224. if (has_a_tile) {
  225. // Process the quadrant.
  226. // First, clear the quadrant's canvas items.
  227. for (RID &ci : rendering_quadrant->canvas_items) {
  228. rs->free(ci);
  229. }
  230. rendering_quadrant->canvas_items.clear();
  231. // Sort the quadrant cells.
  232. if (is_y_sort_enabled() && x_draw_order_reversed) {
  233. rendering_quadrant->cells.sort_custom<CellDataYSortedXReversedComparator>();
  234. } else {
  235. rendering_quadrant->cells.sort();
  236. }
  237. // Those allow to group cell per material or z-index.
  238. Ref<Material> prev_material;
  239. int prev_z_index = 0;
  240. RID prev_ci;
  241. 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()) {
  242. CellData &cell_data = *cell_data_quadrant_list_element->self();
  243. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(*tile_set->get_source(cell_data.cell.source_id));
  244. // Get the tile data.
  245. const TileData *tile_data;
  246. if (cell_data.runtime_tile_data_cache) {
  247. tile_data = cell_data.runtime_tile_data_cache;
  248. } else {
  249. tile_data = atlas_source->get_tile_data(cell_data.cell.get_atlas_coords(), cell_data.cell.alternative_tile);
  250. }
  251. Ref<Material> mat = tile_data->get_material();
  252. int tile_z_index = tile_data->get_z_index();
  253. // Quandrant pos.
  254. // --- CanvasItems ---
  255. RID ci;
  256. // Check if the material or the z_index changed.
  257. if (prev_ci == RID() || prev_material != mat || prev_z_index != tile_z_index) {
  258. // If so, create a new CanvasItem.
  259. ci = rs->canvas_item_create();
  260. if (needs_set_not_interpolated) {
  261. rs->canvas_item_set_interpolated(ci, false);
  262. }
  263. if (mat.is_valid()) {
  264. rs->canvas_item_set_material(ci, mat->get_rid());
  265. }
  266. rs->canvas_item_set_parent(ci, get_canvas_item());
  267. rs->canvas_item_set_use_parent_material(ci, !mat.is_valid());
  268. Transform2D xform(0, rendering_quadrant->canvas_items_position);
  269. rs->canvas_item_set_transform(ci, xform);
  270. rs->canvas_item_set_light_mask(ci, get_light_mask());
  271. rs->canvas_item_set_z_as_relative_to_parent(ci, true);
  272. rs->canvas_item_set_z_index(ci, tile_z_index);
  273. rs->canvas_item_set_default_texture_filter(ci, RS::CanvasItemTextureFilter(get_texture_filter_in_tree()));
  274. rs->canvas_item_set_default_texture_repeat(ci, RS::CanvasItemTextureRepeat(get_texture_repeat_in_tree()));
  275. rendering_quadrant->canvas_items.push_back(ci);
  276. prev_ci = ci;
  277. prev_material = mat;
  278. prev_z_index = tile_z_index;
  279. } else {
  280. // Keep the same canvas_item to draw on.
  281. ci = prev_ci;
  282. }
  283. const Vector2 local_tile_pos = tile_set->map_to_local(cell_data.coords);
  284. // Random animation offset.
  285. real_t random_animation_offset = 0.0;
  286. if (atlas_source->get_tile_animation_mode(cell_data.cell.get_atlas_coords()) != TileSetAtlasSource::TILE_ANIMATION_MODE_DEFAULT) {
  287. Array to_hash;
  288. to_hash.push_back(local_tile_pos);
  289. to_hash.push_back(get_instance_id()); // Use instance id as a random hash
  290. random_animation_offset = RandomPCG(to_hash.hash()).randf();
  291. }
  292. // Drawing the tile in the canvas item.
  293. 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, get_self_modulate(), tile_data, random_animation_offset);
  294. }
  295. // Reset physics interpolation for any recreated canvas items.
  296. if (is_physics_interpolated_and_enabled() && is_visible_in_tree()) {
  297. for (const RID &ci : rendering_quadrant->canvas_items) {
  298. rs->canvas_item_reset_physics_interpolation(ci);
  299. }
  300. }
  301. } else {
  302. // Free the quadrant.
  303. for (const RID &ci : rendering_quadrant->canvas_items) {
  304. if (ci.is_valid()) {
  305. rs->free(ci);
  306. }
  307. }
  308. rendering_quadrant->cells.clear();
  309. rendering_quadrant_map.erase(rendering_quadrant->quadrant_coords);
  310. }
  311. quadrant_list_element = next_quadrant_list_element;
  312. }
  313. dirty_rendering_quadrant_list.clear();
  314. // Reset the drawing indices.
  315. {
  316. int index = -(int64_t)0x80000000; // Always must be drawn below children.
  317. // Sort the quadrants coords per local coordinates.
  318. RBMap<Vector2, Ref<RenderingQuadrant>, RenderingQuadrant::CoordsWorldComparator> local_to_map;
  319. for (KeyValue<Vector2i, Ref<RenderingQuadrant>> &kv : rendering_quadrant_map) {
  320. Ref<RenderingQuadrant> &rendering_quadrant = kv.value;
  321. local_to_map[tile_set->map_to_local(rendering_quadrant->quadrant_coords)] = rendering_quadrant;
  322. }
  323. // Sort the quadrants.
  324. for (const KeyValue<Vector2, Ref<RenderingQuadrant>> &E : local_to_map) {
  325. for (const RID &ci : E.value->canvas_items) {
  326. RS::get_singleton()->canvas_item_set_draw_index(ci, index++);
  327. }
  328. }
  329. }
  330. // Updates on rendering changes.
  331. if (dirty.flags[DIRTY_FLAGS_LAYER_LIGHT_MASK] ||
  332. dirty.flags[DIRTY_FLAGS_LAYER_TEXTURE_FILTER] ||
  333. dirty.flags[DIRTY_FLAGS_LAYER_TEXTURE_REPEAT] ||
  334. dirty.flags[DIRTY_FLAGS_LAYER_SELF_MODULATE]) {
  335. for (KeyValue<Vector2i, Ref<RenderingQuadrant>> &kv : rendering_quadrant_map) {
  336. Ref<RenderingQuadrant> &rendering_quadrant = kv.value;
  337. for (const RID &ci : rendering_quadrant->canvas_items) {
  338. rs->canvas_item_set_light_mask(ci, get_light_mask());
  339. rs->canvas_item_set_default_texture_filter(ci, RS::CanvasItemTextureFilter(get_texture_filter_in_tree()));
  340. rs->canvas_item_set_default_texture_repeat(ci, RS::CanvasItemTextureRepeat(get_texture_repeat_in_tree()));
  341. rs->canvas_item_set_self_modulate(ci, get_self_modulate());
  342. }
  343. }
  344. }
  345. }
  346. // ----------- Occluders processing -----------
  347. if (forced_cleanup || !occlusion_enabled) {
  348. // Clean everything.
  349. for (KeyValue<Vector2i, CellData> &kv : tile_map_layer_data) {
  350. _rendering_occluders_clear_cell(kv.value);
  351. }
  352. } else {
  353. if (_rendering_was_cleaned_up || dirty.flags[DIRTY_FLAGS_TILE_SET]) {
  354. // Update all cells.
  355. for (KeyValue<Vector2i, CellData> &kv : tile_map_layer_data) {
  356. _rendering_occluders_update_cell(kv.value);
  357. }
  358. } else {
  359. // Update dirty cells.
  360. for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  361. CellData &cell_data = *cell_data_list_element->self();
  362. _rendering_occluders_update_cell(cell_data);
  363. }
  364. }
  365. }
  366. // -----------
  367. // Mark the rendering state as up to date.
  368. _rendering_was_cleaned_up = forced_cleanup || !occlusion_enabled;
  369. }
  370. void TileMapLayer::_rendering_notification(int p_what) {
  371. RenderingServer *rs = RenderingServer::get_singleton();
  372. if (p_what == NOTIFICATION_TRANSFORM_CHANGED || p_what == NOTIFICATION_ENTER_CANVAS || p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  373. if (tile_set.is_valid()) {
  374. Transform2D tilemap_xform = get_global_transform();
  375. for (KeyValue<Vector2i, CellData> &kv : tile_map_layer_data) {
  376. const CellData &cell_data = kv.value;
  377. for (const LocalVector<RID> &polygons : cell_data.occluders) {
  378. for (const RID &rid : polygons) {
  379. if (rid.is_null()) {
  380. continue;
  381. }
  382. Transform2D xform(0, tile_set->map_to_local(kv.key));
  383. rs->canvas_light_occluder_attach_to_canvas(rid, get_canvas());
  384. rs->canvas_light_occluder_set_transform(rid, tilemap_xform * xform);
  385. }
  386. }
  387. }
  388. }
  389. } else if (p_what == NOTIFICATION_RESET_PHYSICS_INTERPOLATION) {
  390. if (is_physics_interpolated_and_enabled() && is_visible_in_tree()) {
  391. for (const KeyValue<Vector2i, Ref<RenderingQuadrant>> &kv : rendering_quadrant_map) {
  392. for (const RID &ci : kv.value->canvas_items) {
  393. if (ci.is_valid()) {
  394. rs->canvas_item_reset_physics_interpolation(ci);
  395. }
  396. }
  397. }
  398. }
  399. }
  400. }
  401. void TileMapLayer::_rendering_quadrants_update_cell(CellData &r_cell_data, SelfList<RenderingQuadrant>::List &r_dirty_rendering_quadrant_list) {
  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 (is_y_sort_enabled()) {
  425. canvas_items_position = Vector2(0, tile_set->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. const Vector2i &coords = r_cell_data.coords;
  429. // Rounding down, instead of simply rounding towards zero (truncating).
  430. quadrant_coords = Vector2i(
  431. coords.x > 0 ? coords.x / rendering_quadrant_size : (coords.x - (rendering_quadrant_size - 1)) / rendering_quadrant_size,
  432. coords.y > 0 ? coords.y / rendering_quadrant_size : (coords.y - (rendering_quadrant_size - 1)) / rendering_quadrant_size);
  433. canvas_items_position = tile_set->map_to_local(rendering_quadrant_size * quadrant_coords);
  434. }
  435. Ref<RenderingQuadrant> rendering_quadrant;
  436. if (rendering_quadrant_map.has(quadrant_coords)) {
  437. // Reuse existing rendering quadrant.
  438. rendering_quadrant = rendering_quadrant_map[quadrant_coords];
  439. } else {
  440. // Create a new rendering quadrant.
  441. rendering_quadrant.instantiate();
  442. rendering_quadrant->quadrant_coords = quadrant_coords;
  443. rendering_quadrant->canvas_items_position = canvas_items_position;
  444. rendering_quadrant_map[quadrant_coords] = rendering_quadrant;
  445. }
  446. // Mark the old quadrant as dirty (if it exists).
  447. if (r_cell_data.rendering_quadrant.is_valid()) {
  448. if (!r_cell_data.rendering_quadrant->dirty_quadrant_list_element.in_list()) {
  449. r_dirty_rendering_quadrant_list.add(&r_cell_data.rendering_quadrant->dirty_quadrant_list_element);
  450. }
  451. }
  452. // Remove the cell from that quadrant.
  453. if (r_cell_data.rendering_quadrant_list_element.in_list()) {
  454. r_cell_data.rendering_quadrant_list_element.remove_from_list();
  455. }
  456. // Add the cell to its new quadrant.
  457. r_cell_data.rendering_quadrant = rendering_quadrant;
  458. r_cell_data.rendering_quadrant->cells.add(&r_cell_data.rendering_quadrant_list_element);
  459. // Add the new quadrant to the dirty quadrant list.
  460. if (!rendering_quadrant->dirty_quadrant_list_element.in_list()) {
  461. r_dirty_rendering_quadrant_list.add(&rendering_quadrant->dirty_quadrant_list_element);
  462. }
  463. } else {
  464. Ref<RenderingQuadrant> rendering_quadrant = r_cell_data.rendering_quadrant;
  465. // Remove the cell from its quadrant.
  466. r_cell_data.rendering_quadrant = Ref<RenderingQuadrant>();
  467. if (r_cell_data.rendering_quadrant_list_element.in_list()) {
  468. rendering_quadrant->cells.remove(&r_cell_data.rendering_quadrant_list_element);
  469. }
  470. if (rendering_quadrant.is_valid()) {
  471. // Add the quadrant to the dirty quadrant list.
  472. if (!rendering_quadrant->dirty_quadrant_list_element.in_list()) {
  473. r_dirty_rendering_quadrant_list.add(&rendering_quadrant->dirty_quadrant_list_element);
  474. }
  475. }
  476. }
  477. }
  478. void TileMapLayer::_rendering_occluders_clear_cell(CellData &r_cell_data) {
  479. RenderingServer *rs = RenderingServer::get_singleton();
  480. // Free the occluders.
  481. for (const LocalVector<RID> &polygons : r_cell_data.occluders) {
  482. for (const RID &rid : polygons) {
  483. rs->free(rid);
  484. }
  485. }
  486. r_cell_data.occluders.clear();
  487. }
  488. void TileMapLayer::_rendering_occluders_update_cell(CellData &r_cell_data) {
  489. RenderingServer *rs = RenderingServer::get_singleton();
  490. // Free unused occluders then resize the occluder array.
  491. for (uint32_t i = tile_set->get_occlusion_layers_count(); i < r_cell_data.occluders.size(); i++) {
  492. for (const RID &occluder_id : r_cell_data.occluders[i]) {
  493. if (occluder_id.is_valid()) {
  494. rs->free(occluder_id);
  495. }
  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. bool needs_set_not_interpolated = is_inside_tree() && get_tree()->is_physics_interpolation_enabled() && !is_physics_interpolated();
  518. for (uint32_t occlusion_layer_index = 0; occlusion_layer_index < r_cell_data.occluders.size(); occlusion_layer_index++) {
  519. LocalVector<RID> &occluders = r_cell_data.occluders[occlusion_layer_index];
  520. // Free unused occluders then resize the occluders array.
  521. for (uint32_t i = tile_data->get_occluder_polygons_count(occlusion_layer_index); i < r_cell_data.occluders[occlusion_layer_index].size(); i++) {
  522. RID occluder_id = occluders[i];
  523. if (occluder_id.is_valid()) {
  524. rs->free(occluder_id);
  525. }
  526. }
  527. occluders.resize(tile_data->get_occluder_polygons_count(occlusion_layer_index));
  528. for (uint32_t occlusion_polygon_index = 0; occlusion_polygon_index < occluders.size(); occlusion_polygon_index++) {
  529. RID &occluder = occluders[occlusion_polygon_index];
  530. Ref<OccluderPolygon2D> occluder_polygon = tile_data->get_occluder_polygon(occlusion_layer_index, occlusion_polygon_index);
  531. if (occluder_polygon.is_valid()) {
  532. // Create or update occluder.
  533. Transform2D xform;
  534. xform.set_origin(tile_set->map_to_local(r_cell_data.coords));
  535. if (!occluder.is_valid()) {
  536. occluder = rs->canvas_light_occluder_create();
  537. if (needs_set_not_interpolated) {
  538. rs->canvas_light_occluder_set_interpolated(occluder, false);
  539. }
  540. }
  541. rs->canvas_light_occluder_set_transform(occluder, get_global_transform() * xform);
  542. rs->canvas_light_occluder_set_polygon(occluder, tile_data->get_occluder_polygon(occlusion_layer_index, occlusion_polygon_index, flip_h, flip_v, transpose)->get_rid());
  543. rs->canvas_light_occluder_attach_to_canvas(occluder, get_canvas());
  544. rs->canvas_light_occluder_set_light_mask(occluder, tile_set->get_occlusion_layer_light_mask(occlusion_layer_index));
  545. rs->canvas_light_occluder_set_as_sdf_collision(occluder, tile_set->get_occlusion_layer_sdf_collision(occlusion_layer_index));
  546. } else {
  547. // Clear occluder.
  548. if (occluder.is_valid()) {
  549. rs->free(occluder);
  550. occluder = RID();
  551. }
  552. }
  553. }
  554. }
  555. return;
  556. }
  557. }
  558. }
  559. // If we did not return earlier, clear the cell.
  560. _rendering_occluders_clear_cell(r_cell_data);
  561. }
  562. #ifdef DEBUG_ENABLED
  563. void TileMapLayer::_rendering_draw_cell_debug(const RID &p_canvas_item, const Vector2 &p_quadrant_pos, const CellData &r_cell_data) {
  564. ERR_FAIL_COND(tile_set.is_null());
  565. if (!Engine::get_singleton()->is_editor_hint()) {
  566. return;
  567. }
  568. // Draw a placeholder for tiles needing one.
  569. RenderingServer *rs = RenderingServer::get_singleton();
  570. const TileMapCell &c = r_cell_data.cell;
  571. TileSetSource *source;
  572. if (tile_set->has_source(c.source_id)) {
  573. source = *tile_set->get_source(c.source_id);
  574. if (source->has_tile(c.get_atlas_coords()) && source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  575. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  576. if (atlas_source) {
  577. Vector2i grid_size = atlas_source->get_atlas_grid_size();
  578. if (!atlas_source->get_runtime_texture().is_valid() || c.get_atlas_coords().x >= grid_size.x || c.get_atlas_coords().y >= grid_size.y) {
  579. // Generate a random color from the hashed values of the tiles.
  580. Array to_hash;
  581. to_hash.push_back(c.source_id);
  582. to_hash.push_back(c.get_atlas_coords());
  583. to_hash.push_back(c.alternative_tile);
  584. uint32_t hash = RandomPCG(to_hash.hash()).rand();
  585. Color color;
  586. color = color.from_hsv(
  587. (float)((hash >> 24) & 0xFF) / 256.0,
  588. Math::lerp(0.5, 1.0, (float)((hash >> 16) & 0xFF) / 256.0),
  589. Math::lerp(0.5, 1.0, (float)((hash >> 8) & 0xFF) / 256.0),
  590. 0.8);
  591. // Draw a placeholder tile.
  592. Transform2D cell_to_quadrant;
  593. cell_to_quadrant.set_origin(tile_set->map_to_local(r_cell_data.coords) - p_quadrant_pos);
  594. rs->canvas_item_add_set_transform(p_canvas_item, cell_to_quadrant);
  595. 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);
  596. }
  597. }
  598. }
  599. }
  600. }
  601. #endif // DEBUG_ENABLED
  602. /////////////////////////////// Physics //////////////////////////////////////
  603. void TileMapLayer::_physics_update(bool p_force_cleanup) {
  604. // Check if we should cleanup everything.
  605. bool forced_cleanup = p_force_cleanup || !enabled || !collision_enabled || !is_inside_tree() || tile_set.is_null();
  606. if (forced_cleanup) {
  607. // Clean everything.
  608. for (KeyValue<Vector2i, CellData> &kv : tile_map_layer_data) {
  609. _physics_clear_cell(kv.value);
  610. }
  611. } else {
  612. if (_physics_was_cleaned_up || dirty.flags[DIRTY_FLAGS_TILE_SET] || dirty.flags[DIRTY_FLAGS_LAYER_USE_KINEMATIC_BODIES] || dirty.flags[DIRTY_FLAGS_LAYER_IN_TREE]) {
  613. // Update all cells.
  614. for (KeyValue<Vector2i, CellData> &kv : tile_map_layer_data) {
  615. _physics_update_cell(kv.value);
  616. }
  617. } else {
  618. // Update dirty cells.
  619. for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  620. CellData &cell_data = *cell_data_list_element->self();
  621. _physics_update_cell(cell_data);
  622. }
  623. }
  624. }
  625. // -----------
  626. // Mark the physics state as up to date.
  627. _physics_was_cleaned_up = forced_cleanup;
  628. }
  629. void TileMapLayer::_physics_notification(int p_what) {
  630. Transform2D gl_transform = get_global_transform();
  631. PhysicsServer2D *ps = PhysicsServer2D::get_singleton();
  632. switch (p_what) {
  633. case NOTIFICATION_TRANSFORM_CHANGED:
  634. // Move the collisison shapes along with the TileMap.
  635. if (is_inside_tree() && tile_set.is_valid()) {
  636. for (KeyValue<Vector2i, CellData> &kv : tile_map_layer_data) {
  637. const CellData &cell_data = kv.value;
  638. for (RID body : cell_data.bodies) {
  639. if (body.is_valid()) {
  640. Transform2D xform(0, tile_set->map_to_local(kv.key));
  641. xform = gl_transform * xform;
  642. ps->body_set_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM, xform);
  643. }
  644. }
  645. }
  646. }
  647. break;
  648. case NOTIFICATION_ENTER_TREE:
  649. // Changes in the tree may cause the space to change (e.g. when reparenting to a SubViewport).
  650. if (is_inside_tree()) {
  651. RID space = get_world_2d()->get_space();
  652. for (KeyValue<Vector2i, CellData> &kv : tile_map_layer_data) {
  653. const CellData &cell_data = kv.value;
  654. for (RID body : cell_data.bodies) {
  655. if (body.is_valid()) {
  656. ps->body_set_space(body, space);
  657. }
  658. }
  659. }
  660. }
  661. }
  662. }
  663. void TileMapLayer::_physics_clear_cell(CellData &r_cell_data) {
  664. PhysicsServer2D *ps = PhysicsServer2D::get_singleton();
  665. // Clear bodies.
  666. for (RID body : r_cell_data.bodies) {
  667. if (body.is_valid()) {
  668. bodies_coords.erase(body);
  669. ps->free(body);
  670. }
  671. }
  672. r_cell_data.bodies.clear();
  673. }
  674. void TileMapLayer::_physics_update_cell(CellData &r_cell_data) {
  675. Transform2D gl_transform = get_global_transform();
  676. RID space = get_world_2d()->get_space();
  677. PhysicsServer2D *ps = PhysicsServer2D::get_singleton();
  678. // Recreate bodies and shapes.
  679. TileMapCell &c = r_cell_data.cell;
  680. TileSetSource *source;
  681. if (tile_set->has_source(c.source_id)) {
  682. source = *tile_set->get_source(c.source_id);
  683. if (source->has_tile(c.get_atlas_coords()) && source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  684. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  685. if (atlas_source) {
  686. const TileData *tile_data;
  687. if (r_cell_data.runtime_tile_data_cache) {
  688. tile_data = r_cell_data.runtime_tile_data_cache;
  689. } else {
  690. tile_data = atlas_source->get_tile_data(c.get_atlas_coords(), c.alternative_tile);
  691. }
  692. // Transform flags.
  693. bool flip_h = (c.alternative_tile & TileSetAtlasSource::TRANSFORM_FLIP_H);
  694. bool flip_v = (c.alternative_tile & TileSetAtlasSource::TRANSFORM_FLIP_V);
  695. bool transpose = (c.alternative_tile & TileSetAtlasSource::TRANSFORM_TRANSPOSE);
  696. // Free unused bodies then resize the bodies array.
  697. for (uint32_t i = tile_set->get_physics_layers_count(); i < r_cell_data.bodies.size(); i++) {
  698. RID &body = r_cell_data.bodies[i];
  699. if (body.is_valid()) {
  700. bodies_coords.erase(body);
  701. ps->free(body);
  702. body = RID();
  703. }
  704. }
  705. r_cell_data.bodies.resize(tile_set->get_physics_layers_count());
  706. for (uint32_t tile_set_physics_layer = 0; tile_set_physics_layer < (uint32_t)tile_set->get_physics_layers_count(); tile_set_physics_layer++) {
  707. Ref<PhysicsMaterial> physics_material = tile_set->get_physics_layer_physics_material(tile_set_physics_layer);
  708. uint32_t physics_layer = tile_set->get_physics_layer_collision_layer(tile_set_physics_layer);
  709. uint32_t physics_mask = tile_set->get_physics_layer_collision_mask(tile_set_physics_layer);
  710. real_t physics_priority = tile_set->get_physics_layer_collision_priority(tile_set_physics_layer);
  711. RID body = r_cell_data.bodies[tile_set_physics_layer];
  712. if (tile_data->get_collision_polygons_count(tile_set_physics_layer) == 0) {
  713. // No body needed, free it if it exists.
  714. if (body.is_valid()) {
  715. bodies_coords.erase(body);
  716. ps->free(body);
  717. }
  718. body = RID();
  719. } else {
  720. // Create or update the body.
  721. if (!body.is_valid()) {
  722. body = ps->body_create();
  723. }
  724. bodies_coords[body] = r_cell_data.coords;
  725. ps->body_set_mode(body, use_kinematic_bodies ? PhysicsServer2D::BODY_MODE_KINEMATIC : PhysicsServer2D::BODY_MODE_STATIC);
  726. ps->body_set_space(body, space);
  727. Transform2D xform;
  728. xform.set_origin(tile_set->map_to_local(r_cell_data.coords));
  729. xform = gl_transform * xform;
  730. ps->body_set_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM, xform);
  731. ps->body_attach_object_instance_id(body, tile_map_node ? tile_map_node->get_instance_id() : get_instance_id());
  732. ps->body_set_collision_layer(body, physics_layer);
  733. ps->body_set_collision_mask(body, physics_mask);
  734. ps->body_set_collision_priority(body, physics_priority);
  735. ps->body_set_pickable(body, false);
  736. ps->body_set_state(body, PhysicsServer2D::BODY_STATE_LINEAR_VELOCITY, tile_data->get_constant_linear_velocity(tile_set_physics_layer));
  737. ps->body_set_state(body, PhysicsServer2D::BODY_STATE_ANGULAR_VELOCITY, tile_data->get_constant_angular_velocity(tile_set_physics_layer));
  738. if (!physics_material.is_valid()) {
  739. ps->body_set_param(body, PhysicsServer2D::BODY_PARAM_BOUNCE, 0);
  740. ps->body_set_param(body, PhysicsServer2D::BODY_PARAM_FRICTION, 1);
  741. } else {
  742. ps->body_set_param(body, PhysicsServer2D::BODY_PARAM_BOUNCE, physics_material->computed_bounce());
  743. ps->body_set_param(body, PhysicsServer2D::BODY_PARAM_FRICTION, physics_material->computed_friction());
  744. }
  745. // Clear body's shape if needed.
  746. ps->body_clear_shapes(body);
  747. // Add the shapes to the body.
  748. int body_shape_index = 0;
  749. for (int polygon_index = 0; polygon_index < tile_data->get_collision_polygons_count(tile_set_physics_layer); polygon_index++) {
  750. // Iterate over the polygons.
  751. bool one_way_collision = tile_data->is_collision_polygon_one_way(tile_set_physics_layer, polygon_index);
  752. float one_way_collision_margin = tile_data->get_collision_polygon_one_way_margin(tile_set_physics_layer, polygon_index);
  753. int shapes_count = tile_data->get_collision_polygon_shapes_count(tile_set_physics_layer, polygon_index);
  754. for (int shape_index = 0; shape_index < shapes_count; shape_index++) {
  755. // Add decomposed convex shapes.
  756. Ref<ConvexPolygonShape2D> shape = tile_data->get_collision_polygon_shape(tile_set_physics_layer, polygon_index, shape_index, flip_h, flip_v, transpose);
  757. ps->body_add_shape(body, shape->get_rid());
  758. ps->body_set_shape_as_one_way_collision(body, body_shape_index, one_way_collision, one_way_collision_margin);
  759. body_shape_index++;
  760. }
  761. }
  762. }
  763. // Set the body again.
  764. r_cell_data.bodies[tile_set_physics_layer] = body;
  765. }
  766. return;
  767. }
  768. }
  769. }
  770. // If we did not return earlier, clear the cell.
  771. _physics_clear_cell(r_cell_data);
  772. }
  773. #ifdef DEBUG_ENABLED
  774. void TileMapLayer::_physics_draw_cell_debug(const RID &p_canvas_item, const Vector2 &p_quadrant_pos, const CellData &r_cell_data) {
  775. // Draw the debug collision shapes.
  776. ERR_FAIL_COND(tile_set.is_null());
  777. if (!get_tree()) {
  778. return;
  779. }
  780. bool show_collision = false;
  781. switch (collision_visibility_mode) {
  782. case TileMapLayer::DEBUG_VISIBILITY_MODE_DEFAULT:
  783. show_collision = !Engine::get_singleton()->is_editor_hint() && get_tree()->is_debugging_collisions_hint();
  784. break;
  785. case TileMapLayer::DEBUG_VISIBILITY_MODE_FORCE_HIDE:
  786. show_collision = false;
  787. break;
  788. case TileMapLayer::DEBUG_VISIBILITY_MODE_FORCE_SHOW:
  789. show_collision = true;
  790. break;
  791. }
  792. if (!show_collision) {
  793. return;
  794. }
  795. RenderingServer *rs = RenderingServer::get_singleton();
  796. PhysicsServer2D *ps = PhysicsServer2D::get_singleton();
  797. Color debug_collision_color = get_tree()->get_debug_collisions_color();
  798. Vector<Color> color;
  799. color.push_back(debug_collision_color);
  800. Transform2D quadrant_to_local(0, p_quadrant_pos);
  801. Transform2D global_to_quadrant = (get_global_transform() * quadrant_to_local).affine_inverse();
  802. for (RID body : r_cell_data.bodies) {
  803. if (body.is_valid()) {
  804. Transform2D body_to_quadrant = global_to_quadrant * Transform2D(ps->body_get_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM));
  805. rs->canvas_item_add_set_transform(p_canvas_item, body_to_quadrant);
  806. for (int shape_index = 0; shape_index < ps->body_get_shape_count(body); shape_index++) {
  807. const RID &shape = ps->body_get_shape(body, shape_index);
  808. const PhysicsServer2D::ShapeType &type = ps->shape_get_type(shape);
  809. if (type == PhysicsServer2D::SHAPE_CONVEX_POLYGON) {
  810. rs->canvas_item_add_polygon(p_canvas_item, ps->shape_get_data(shape), color);
  811. } else {
  812. WARN_PRINT("Wrong shape type for a tile, should be SHAPE_CONVEX_POLYGON.");
  813. }
  814. }
  815. rs->canvas_item_add_set_transform(p_canvas_item, Transform2D());
  816. }
  817. }
  818. }
  819. #endif // DEBUG_ENABLED
  820. /////////////////////////////// Navigation //////////////////////////////////////
  821. void TileMapLayer::_navigation_update(bool p_force_cleanup) {
  822. ERR_FAIL_NULL(NavigationServer2D::get_singleton());
  823. NavigationServer2D *ns = NavigationServer2D::get_singleton();
  824. // Check if we should cleanup everything.
  825. bool forced_cleanup = p_force_cleanup || !enabled || !navigation_enabled || !is_inside_tree() || tile_set.is_null();
  826. // ----------- Layer level processing -----------
  827. // All this processing is kept for compatibility with the TileMap node.
  828. // Otherwise, layers shall use the World2D navigation map or define a custom one with set_navigation_map(...).
  829. if (tile_map_node) {
  830. if (forced_cleanup) {
  831. if (navigation_map_override.is_valid()) {
  832. ns->free(navigation_map_override);
  833. navigation_map_override = RID();
  834. }
  835. } else {
  836. // Update navigation maps.
  837. if (!navigation_map_override.is_valid()) {
  838. if (layer_index_in_tile_map_node > 0) {
  839. // Create a dedicated map for each layer.
  840. RID new_layer_map = ns->map_create();
  841. // Set the default NavigationPolygon cell_size on the new map as a mismatch causes an error.
  842. ns->map_set_cell_size(new_layer_map, NavigationDefaults2D::navmesh_cell_size);
  843. ns->map_set_active(new_layer_map, true);
  844. navigation_map_override = new_layer_map;
  845. }
  846. }
  847. }
  848. }
  849. // ----------- Navigation regions processing -----------
  850. if (forced_cleanup) {
  851. // Clean everything.
  852. for (KeyValue<Vector2i, CellData> &kv : tile_map_layer_data) {
  853. _navigation_clear_cell(kv.value);
  854. }
  855. } else {
  856. if (_navigation_was_cleaned_up || dirty.flags[DIRTY_FLAGS_TILE_SET] || dirty.flags[DIRTY_FLAGS_LAYER_IN_TREE] || dirty.flags[DIRTY_FLAGS_LAYER_NAVIGATION_MAP]) {
  857. // Update all cells.
  858. for (KeyValue<Vector2i, CellData> &kv : tile_map_layer_data) {
  859. _navigation_update_cell(kv.value);
  860. }
  861. } else {
  862. // Update dirty cells.
  863. for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  864. CellData &cell_data = *cell_data_list_element->self();
  865. _navigation_update_cell(cell_data);
  866. }
  867. }
  868. }
  869. // -----------
  870. // Mark the navigation state as up to date.
  871. _navigation_was_cleaned_up = forced_cleanup;
  872. }
  873. void TileMapLayer::_navigation_notification(int p_what) {
  874. if (p_what == NOTIFICATION_TRANSFORM_CHANGED) {
  875. if (tile_set.is_valid()) {
  876. Transform2D tilemap_xform = get_global_transform();
  877. for (KeyValue<Vector2i, CellData> &kv : tile_map_layer_data) {
  878. const CellData &cell_data = kv.value;
  879. // Update navigation regions transform.
  880. for (const RID &region : cell_data.navigation_regions) {
  881. if (!region.is_valid()) {
  882. continue;
  883. }
  884. Transform2D tile_transform;
  885. tile_transform.set_origin(tile_set->map_to_local(kv.key));
  886. NavigationServer2D::get_singleton()->region_set_transform(region, tilemap_xform * tile_transform);
  887. }
  888. }
  889. }
  890. }
  891. }
  892. void TileMapLayer::_navigation_clear_cell(CellData &r_cell_data) {
  893. NavigationServer2D *ns = NavigationServer2D::get_singleton();
  894. // Clear navigation shapes.
  895. for (uint32_t i = 0; i < r_cell_data.navigation_regions.size(); i++) {
  896. const RID &region = r_cell_data.navigation_regions[i];
  897. if (region.is_valid()) {
  898. ns->region_set_map(region, RID());
  899. ns->free(region);
  900. }
  901. }
  902. r_cell_data.navigation_regions.clear();
  903. }
  904. void TileMapLayer::_navigation_update_cell(CellData &r_cell_data) {
  905. NavigationServer2D *ns = NavigationServer2D::get_singleton();
  906. Transform2D gl_xform = get_global_transform();
  907. RID navigation_map = navigation_map_override.is_valid() ? navigation_map_override : get_world_2d()->get_navigation_map();
  908. ERR_FAIL_COND(navigation_map.is_null());
  909. // Get the navigation polygons and create regions.
  910. TileMapCell &c = r_cell_data.cell;
  911. TileSetSource *source;
  912. if (tile_set->has_source(c.source_id)) {
  913. source = *tile_set->get_source(c.source_id);
  914. if (source->has_tile(c.get_atlas_coords()) && source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  915. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  916. if (atlas_source) {
  917. const TileData *tile_data;
  918. if (r_cell_data.runtime_tile_data_cache) {
  919. tile_data = r_cell_data.runtime_tile_data_cache;
  920. } else {
  921. tile_data = atlas_source->get_tile_data(c.get_atlas_coords(), c.alternative_tile);
  922. }
  923. // Transform flags.
  924. bool flip_h = (c.alternative_tile & TileSetAtlasSource::TRANSFORM_FLIP_H);
  925. bool flip_v = (c.alternative_tile & TileSetAtlasSource::TRANSFORM_FLIP_V);
  926. bool transpose = (c.alternative_tile & TileSetAtlasSource::TRANSFORM_TRANSPOSE);
  927. // Free unused regions then resize the regions array.
  928. for (uint32_t i = tile_set->get_navigation_layers_count(); i < r_cell_data.navigation_regions.size(); i++) {
  929. RID &region = r_cell_data.navigation_regions[i];
  930. if (region.is_valid()) {
  931. ns->region_set_map(region, RID());
  932. ns->free(region);
  933. region = RID();
  934. }
  935. }
  936. r_cell_data.navigation_regions.resize(tile_set->get_navigation_layers_count());
  937. // Create, update or clear regions.
  938. for (uint32_t navigation_layer_index = 0; navigation_layer_index < r_cell_data.navigation_regions.size(); navigation_layer_index++) {
  939. Ref<NavigationPolygon> navigation_polygon = tile_data->get_navigation_polygon(navigation_layer_index, flip_h, flip_v, transpose);
  940. RID &region = r_cell_data.navigation_regions[navigation_layer_index];
  941. if (navigation_polygon.is_valid() && (navigation_polygon->get_polygon_count() > 0 || navigation_polygon->get_outline_count() > 0)) {
  942. // Create or update regions.
  943. Transform2D tile_transform;
  944. tile_transform.set_origin(tile_set->map_to_local(r_cell_data.coords));
  945. if (!region.is_valid()) {
  946. region = ns->region_create();
  947. }
  948. ns->region_set_owner_id(region, tile_map_node ? tile_map_node->get_instance_id() : get_instance_id());
  949. ns->region_set_map(region, navigation_map);
  950. ns->region_set_transform(region, gl_xform * tile_transform);
  951. ns->region_set_navigation_layers(region, tile_set->get_navigation_layer_layers(navigation_layer_index));
  952. ns->region_set_navigation_polygon(region, navigation_polygon);
  953. } else {
  954. // Clear region.
  955. if (region.is_valid()) {
  956. ns->region_set_map(region, RID());
  957. ns->free(region);
  958. region = RID();
  959. }
  960. }
  961. }
  962. return;
  963. }
  964. }
  965. }
  966. // If we did not return earlier, clear the cell.
  967. _navigation_clear_cell(r_cell_data);
  968. }
  969. #ifdef DEBUG_ENABLED
  970. void TileMapLayer::_navigation_draw_cell_debug(const RID &p_canvas_item, const Vector2 &p_quadrant_pos, const CellData &r_cell_data) {
  971. // Draw the debug collision shapes.
  972. bool show_navigation = false;
  973. switch (navigation_visibility_mode) {
  974. case TileMapLayer::DEBUG_VISIBILITY_MODE_DEFAULT:
  975. show_navigation = !Engine::get_singleton()->is_editor_hint() && get_tree()->is_debugging_navigation_hint();
  976. break;
  977. case TileMapLayer::DEBUG_VISIBILITY_MODE_FORCE_HIDE:
  978. show_navigation = false;
  979. break;
  980. case TileMapLayer::DEBUG_VISIBILITY_MODE_FORCE_SHOW:
  981. show_navigation = true;
  982. break;
  983. }
  984. if (!show_navigation) {
  985. return;
  986. }
  987. // Check if the navigation is used.
  988. if (r_cell_data.navigation_regions.is_empty()) {
  989. return;
  990. }
  991. RenderingServer *rs = RenderingServer::get_singleton();
  992. const NavigationServer2D *ns2d = NavigationServer2D::get_singleton();
  993. bool enabled_geometry_face_random_color = ns2d->get_debug_navigation_enable_geometry_face_random_color();
  994. bool enabled_edge_lines = ns2d->get_debug_navigation_enable_edge_lines();
  995. Color debug_face_color = ns2d->get_debug_navigation_geometry_face_color();
  996. Color debug_edge_color = ns2d->get_debug_navigation_geometry_edge_color();
  997. RandomPCG rand;
  998. const TileMapCell &c = r_cell_data.cell;
  999. TileSetSource *source;
  1000. if (tile_set->has_source(c.source_id)) {
  1001. source = *tile_set->get_source(c.source_id);
  1002. if (source->has_tile(c.get_atlas_coords()) && source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  1003. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  1004. if (atlas_source) {
  1005. const TileData *tile_data;
  1006. if (r_cell_data.runtime_tile_data_cache) {
  1007. tile_data = r_cell_data.runtime_tile_data_cache;
  1008. } else {
  1009. tile_data = atlas_source->get_tile_data(c.get_atlas_coords(), c.alternative_tile);
  1010. }
  1011. Transform2D cell_to_quadrant;
  1012. cell_to_quadrant.set_origin(tile_set->map_to_local(r_cell_data.coords) - p_quadrant_pos);
  1013. rs->canvas_item_add_set_transform(p_canvas_item, cell_to_quadrant);
  1014. for (int layer_index = 0; layer_index < tile_set->get_navigation_layers_count(); layer_index++) {
  1015. bool flip_h = (c.alternative_tile & TileSetAtlasSource::TRANSFORM_FLIP_H);
  1016. bool flip_v = (c.alternative_tile & TileSetAtlasSource::TRANSFORM_FLIP_V);
  1017. bool transpose = (c.alternative_tile & TileSetAtlasSource::TRANSFORM_TRANSPOSE);
  1018. Ref<NavigationPolygon> navigation_polygon = tile_data->get_navigation_polygon(layer_index, flip_h, flip_v, transpose);
  1019. if (navigation_polygon.is_valid()) {
  1020. Vector<Vector2> navigation_polygon_vertices = navigation_polygon->get_vertices();
  1021. if (navigation_polygon_vertices.size() < 3) {
  1022. continue;
  1023. }
  1024. for (int i = 0; i < navigation_polygon->get_polygon_count(); i++) {
  1025. // An array of vertices for this polygon.
  1026. Vector<int> polygon = navigation_polygon->get_polygon(i);
  1027. Vector<Vector2> debug_polygon_vertices;
  1028. debug_polygon_vertices.resize(polygon.size());
  1029. for (int j = 0; j < polygon.size(); j++) {
  1030. ERR_FAIL_INDEX(polygon[j], navigation_polygon_vertices.size());
  1031. debug_polygon_vertices.write[j] = navigation_polygon_vertices[polygon[j]];
  1032. }
  1033. // Generate the polygon color, slightly randomly modified from the settings one.
  1034. Color random_variation_color = debug_face_color;
  1035. if (enabled_geometry_face_random_color) {
  1036. random_variation_color.set_hsv(
  1037. debug_face_color.get_h() + rand.random(-1.0, 1.0) * 0.1,
  1038. debug_face_color.get_s(),
  1039. debug_face_color.get_v() + rand.random(-1.0, 1.0) * 0.2);
  1040. }
  1041. random_variation_color.a = debug_face_color.a;
  1042. Vector<Color> debug_face_colors;
  1043. debug_face_colors.push_back(random_variation_color);
  1044. rs->canvas_item_add_polygon(p_canvas_item, debug_polygon_vertices, debug_face_colors);
  1045. if (enabled_edge_lines) {
  1046. Vector<Color> debug_edge_colors;
  1047. debug_edge_colors.push_back(debug_edge_color);
  1048. debug_polygon_vertices.push_back(debug_polygon_vertices[0]); // Add first again for closing polyline.
  1049. rs->canvas_item_add_polyline(p_canvas_item, debug_polygon_vertices, debug_edge_colors);
  1050. }
  1051. }
  1052. }
  1053. }
  1054. }
  1055. }
  1056. }
  1057. }
  1058. #endif // DEBUG_ENABLED
  1059. /////////////////////////////// Scenes //////////////////////////////////////
  1060. void TileMapLayer::_scenes_update(bool p_force_cleanup) {
  1061. // Check if we should cleanup everything.
  1062. bool forced_cleanup = p_force_cleanup || !enabled || !is_inside_tree() || tile_set.is_null();
  1063. if (forced_cleanup) {
  1064. // Clean everything.
  1065. for (KeyValue<Vector2i, CellData> &kv : tile_map_layer_data) {
  1066. _scenes_clear_cell(kv.value);
  1067. }
  1068. } else {
  1069. if (_scenes_was_cleaned_up || dirty.flags[DIRTY_FLAGS_TILE_SET] || dirty.flags[DIRTY_FLAGS_LAYER_IN_TREE]) {
  1070. // Update all cells.
  1071. for (KeyValue<Vector2i, CellData> &kv : tile_map_layer_data) {
  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 = nullptr;
  1089. if (tile_map_node) {
  1090. // Compatibility with TileMap.
  1091. node = tile_map_node->get_node_or_null(r_cell_data.scene);
  1092. } else {
  1093. node = get_node_or_null(r_cell_data.scene);
  1094. }
  1095. if (node) {
  1096. node->queue_free();
  1097. }
  1098. r_cell_data.scene = "";
  1099. }
  1100. void TileMapLayer::_scenes_update_cell(CellData &r_cell_data) {
  1101. // Clear the scene in any case.
  1102. _scenes_clear_cell(r_cell_data);
  1103. // Create the scene.
  1104. const TileMapCell &c = r_cell_data.cell;
  1105. TileSetSource *source;
  1106. if (tile_set->has_source(c.source_id)) {
  1107. source = *tile_set->get_source(c.source_id);
  1108. if (source->has_tile(c.get_atlas_coords()) && source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  1109. TileSetScenesCollectionSource *scenes_collection_source = Object::cast_to<TileSetScenesCollectionSource>(source);
  1110. if (scenes_collection_source) {
  1111. Ref<PackedScene> packed_scene = scenes_collection_source->get_scene_tile_scene(c.alternative_tile);
  1112. if (packed_scene.is_valid()) {
  1113. Node *scene = packed_scene->instantiate();
  1114. Control *scene_as_control = Object::cast_to<Control>(scene);
  1115. Node2D *scene_as_node2d = Object::cast_to<Node2D>(scene);
  1116. if (scene_as_control) {
  1117. scene_as_control->set_position(tile_set->map_to_local(r_cell_data.coords) + scene_as_control->get_position());
  1118. } else if (scene_as_node2d) {
  1119. Transform2D xform;
  1120. xform.set_origin(tile_set->map_to_local(r_cell_data.coords));
  1121. scene_as_node2d->set_transform(xform * scene_as_node2d->get_transform());
  1122. }
  1123. if (tile_map_node) {
  1124. // Compatibility with TileMap.
  1125. tile_map_node->add_child(scene);
  1126. } else {
  1127. add_child(scene);
  1128. }
  1129. r_cell_data.scene = scene->get_name();
  1130. }
  1131. }
  1132. }
  1133. }
  1134. }
  1135. #ifdef DEBUG_ENABLED
  1136. void TileMapLayer::_scenes_draw_cell_debug(const RID &p_canvas_item, const Vector2 &p_quadrant_pos, const CellData &r_cell_data) {
  1137. ERR_FAIL_COND(tile_set.is_null());
  1138. if (!Engine::get_singleton()->is_editor_hint()) {
  1139. return;
  1140. }
  1141. // Draw a placeholder for scenes needing one.
  1142. RenderingServer *rs = RenderingServer::get_singleton();
  1143. const TileMapCell &c = r_cell_data.cell;
  1144. TileSetSource *source;
  1145. if (tile_set->has_source(c.source_id)) {
  1146. source = *tile_set->get_source(c.source_id);
  1147. if (!source->has_tile(c.get_atlas_coords()) || !source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  1148. return;
  1149. }
  1150. TileSetScenesCollectionSource *scenes_collection_source = Object::cast_to<TileSetScenesCollectionSource>(source);
  1151. if (scenes_collection_source) {
  1152. if (!scenes_collection_source->get_scene_tile_scene(c.alternative_tile).is_valid() || scenes_collection_source->get_scene_tile_display_placeholder(c.alternative_tile)) {
  1153. // Generate a random color from the hashed values of the tiles.
  1154. Array to_hash;
  1155. to_hash.push_back(c.source_id);
  1156. to_hash.push_back(c.alternative_tile);
  1157. uint32_t hash = RandomPCG(to_hash.hash()).rand();
  1158. Color color;
  1159. color = color.from_hsv(
  1160. (float)((hash >> 24) & 0xFF) / 256.0,
  1161. Math::lerp(0.5, 1.0, (float)((hash >> 16) & 0xFF) / 256.0),
  1162. Math::lerp(0.5, 1.0, (float)((hash >> 8) & 0xFF) / 256.0),
  1163. 0.8);
  1164. // Draw a placeholder tile.
  1165. Transform2D cell_to_quadrant;
  1166. cell_to_quadrant.set_origin(tile_set->map_to_local(r_cell_data.coords) - p_quadrant_pos);
  1167. rs->canvas_item_add_set_transform(p_canvas_item, cell_to_quadrant);
  1168. 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);
  1169. }
  1170. }
  1171. }
  1172. }
  1173. #endif // DEBUG_ENABLED
  1174. /////////////////////////////////////////////////////////////////////
  1175. void TileMapLayer::_build_runtime_update_tile_data(bool p_force_cleanup) {
  1176. // Check if we should cleanup everything.
  1177. bool forced_cleanup = p_force_cleanup || !enabled || tile_set.is_null() || !is_visible_in_tree();
  1178. if (!forced_cleanup) {
  1179. bool valid_runtime_update = GDVIRTUAL_IS_OVERRIDDEN(_use_tile_data_runtime_update) && GDVIRTUAL_IS_OVERRIDDEN(_tile_data_runtime_update);
  1180. bool valid_runtime_update_for_tilemap = tile_map_node && tile_map_node->GDVIRTUAL_IS_OVERRIDDEN(_use_tile_data_runtime_update) && tile_map_node->GDVIRTUAL_IS_OVERRIDDEN(_tile_data_runtime_update); // For keeping compatibility.
  1181. if (valid_runtime_update || valid_runtime_update_for_tilemap) {
  1182. bool use_tilemap_for_runtime = valid_runtime_update_for_tilemap && !valid_runtime_update;
  1183. if (_runtime_update_tile_data_was_cleaned_up || dirty.flags[DIRTY_FLAGS_TILE_SET]) {
  1184. _runtime_update_needs_all_cells_cleaned_up = true;
  1185. for (KeyValue<Vector2i, CellData> &E : tile_map_layer_data) {
  1186. _build_runtime_update_tile_data_for_cell(E.value, use_tilemap_for_runtime);
  1187. }
  1188. } else if (dirty.flags[DIRTY_FLAGS_LAYER_RUNTIME_UPDATE]) {
  1189. for (KeyValue<Vector2i, CellData> &E : tile_map_layer_data) {
  1190. _build_runtime_update_tile_data_for_cell(E.value, use_tilemap_for_runtime, true);
  1191. }
  1192. } else {
  1193. for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  1194. CellData &cell_data = *cell_data_list_element->self();
  1195. _build_runtime_update_tile_data_for_cell(cell_data, use_tilemap_for_runtime);
  1196. }
  1197. }
  1198. }
  1199. }
  1200. // -----------
  1201. // Mark the navigation state as up to date.
  1202. _runtime_update_tile_data_was_cleaned_up = forced_cleanup;
  1203. }
  1204. void TileMapLayer::_build_runtime_update_tile_data_for_cell(CellData &r_cell_data, bool p_use_tilemap_for_runtime, bool p_auto_add_to_dirty_list) {
  1205. TileMapCell &c = r_cell_data.cell;
  1206. TileSetSource *source;
  1207. if (tile_set->has_source(c.source_id)) {
  1208. source = *tile_set->get_source(c.source_id);
  1209. if (source->has_tile(c.get_atlas_coords()) && source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  1210. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  1211. if (atlas_source) {
  1212. bool ret = false;
  1213. if (p_use_tilemap_for_runtime) {
  1214. // Compatibility with TileMap.
  1215. if (tile_map_node->GDVIRTUAL_CALL(_use_tile_data_runtime_update, layer_index_in_tile_map_node, r_cell_data.coords, ret) && ret) {
  1216. TileData *tile_data = atlas_source->get_tile_data(c.get_atlas_coords(), c.alternative_tile);
  1217. // Create the runtime TileData.
  1218. TileData *tile_data_runtime_use = tile_data->duplicate();
  1219. tile_data_runtime_use->set_allow_transform(true);
  1220. r_cell_data.runtime_tile_data_cache = tile_data_runtime_use;
  1221. tile_map_node->GDVIRTUAL_CALL(_tile_data_runtime_update, layer_index_in_tile_map_node, r_cell_data.coords, tile_data_runtime_use);
  1222. if (p_auto_add_to_dirty_list) {
  1223. dirty.cell_list.add(&r_cell_data.dirty_list_element);
  1224. }
  1225. }
  1226. } else {
  1227. if (GDVIRTUAL_CALL(_use_tile_data_runtime_update, r_cell_data.coords, ret) && ret) {
  1228. TileData *tile_data = atlas_source->get_tile_data(c.get_atlas_coords(), c.alternative_tile);
  1229. // Create the runtime TileData.
  1230. TileData *tile_data_runtime_use = tile_data->duplicate();
  1231. tile_data_runtime_use->set_allow_transform(true);
  1232. r_cell_data.runtime_tile_data_cache = tile_data_runtime_use;
  1233. GDVIRTUAL_CALL(_tile_data_runtime_update, r_cell_data.coords, tile_data_runtime_use);
  1234. if (p_auto_add_to_dirty_list) {
  1235. dirty.cell_list.add(&r_cell_data.dirty_list_element);
  1236. }
  1237. }
  1238. }
  1239. }
  1240. }
  1241. }
  1242. }
  1243. void TileMapLayer::_clear_runtime_update_tile_data() {
  1244. if (_runtime_update_needs_all_cells_cleaned_up) {
  1245. for (KeyValue<Vector2i, CellData> &E : tile_map_layer_data) {
  1246. _clear_runtime_update_tile_data_for_cell(E.value);
  1247. }
  1248. _runtime_update_needs_all_cells_cleaned_up = false;
  1249. } else {
  1250. for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  1251. CellData &r_cell_data = *cell_data_list_element->self();
  1252. _clear_runtime_update_tile_data_for_cell(r_cell_data);
  1253. }
  1254. }
  1255. }
  1256. void TileMapLayer::_clear_runtime_update_tile_data_for_cell(CellData &r_cell_data) {
  1257. // Clear the runtime tile data.
  1258. if (r_cell_data.runtime_tile_data_cache) {
  1259. memdelete(r_cell_data.runtime_tile_data_cache);
  1260. r_cell_data.runtime_tile_data_cache = nullptr;
  1261. }
  1262. }
  1263. void TileMapLayer::_update_cells_callback(bool p_force_cleanup) {
  1264. if (!GDVIRTUAL_IS_OVERRIDDEN(_update_cells)) {
  1265. return;
  1266. }
  1267. // Check if we should cleanup everything.
  1268. bool forced_cleanup = p_force_cleanup || !enabled || tile_set.is_null() || !is_visible_in_tree();
  1269. // List all the dirty cell's positions to notify script of cell updates.
  1270. TypedArray<Vector2i> dirty_cell_positions;
  1271. for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  1272. CellData &cell_data = *cell_data_list_element->self();
  1273. dirty_cell_positions.push_back(cell_data.coords);
  1274. }
  1275. GDVIRTUAL_CALL(_update_cells, dirty_cell_positions, forced_cleanup);
  1276. }
  1277. 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) const {
  1278. if (tile_set.is_null()) {
  1279. return TileSet::TerrainsPattern();
  1280. }
  1281. // Returns all tiles compatible with the given constraints.
  1282. RBMap<TileSet::TerrainsPattern, int> terrain_pattern_score;
  1283. RBSet<TileSet::TerrainsPattern> pattern_set = tile_set->get_terrains_pattern_set(p_terrain_set);
  1284. ERR_FAIL_COND_V(pattern_set.is_empty(), TileSet::TerrainsPattern());
  1285. for (TileSet::TerrainsPattern &terrain_pattern : pattern_set) {
  1286. int score = 0;
  1287. // Check the center bit constraint.
  1288. TerrainConstraint terrain_constraint = TerrainConstraint(tile_set, p_position, terrain_pattern.get_terrain());
  1289. const RBSet<TerrainConstraint>::Element *in_set_constraint_element = p_constraints.find(terrain_constraint);
  1290. if (in_set_constraint_element) {
  1291. if (in_set_constraint_element->get().get_terrain() != terrain_constraint.get_terrain()) {
  1292. score += in_set_constraint_element->get().get_priority();
  1293. }
  1294. } else if (p_current_pattern.get_terrain() != terrain_pattern.get_terrain()) {
  1295. continue; // Ignore a pattern that cannot keep bits without constraints unmodified.
  1296. }
  1297. // Check the surrounding bits
  1298. bool invalid_pattern = false;
  1299. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  1300. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  1301. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, bit)) {
  1302. // Check if the bit is compatible with the constraints.
  1303. TerrainConstraint terrain_bit_constraint = TerrainConstraint(tile_set, p_position, bit, terrain_pattern.get_terrain_peering_bit(bit));
  1304. in_set_constraint_element = p_constraints.find(terrain_bit_constraint);
  1305. if (in_set_constraint_element) {
  1306. if (in_set_constraint_element->get().get_terrain() != terrain_bit_constraint.get_terrain()) {
  1307. score += in_set_constraint_element->get().get_priority();
  1308. }
  1309. } else if (p_current_pattern.get_terrain_peering_bit(bit) != terrain_pattern.get_terrain_peering_bit(bit)) {
  1310. invalid_pattern = true; // Ignore a pattern that cannot keep bits without constraints unmodified.
  1311. break;
  1312. }
  1313. }
  1314. }
  1315. if (invalid_pattern) {
  1316. continue;
  1317. }
  1318. terrain_pattern_score[terrain_pattern] = score;
  1319. }
  1320. // Compute the minimum score.
  1321. TileSet::TerrainsPattern min_score_pattern = p_current_pattern;
  1322. int min_score = INT32_MAX;
  1323. for (KeyValue<TileSet::TerrainsPattern, int> E : terrain_pattern_score) {
  1324. if (E.value < min_score) {
  1325. min_score_pattern = E.key;
  1326. min_score = E.value;
  1327. }
  1328. }
  1329. return min_score_pattern;
  1330. }
  1331. RBSet<TerrainConstraint> TileMapLayer::_get_terrain_constraints_from_added_pattern(const Vector2i &p_position, int p_terrain_set, TileSet::TerrainsPattern p_terrains_pattern) const {
  1332. if (tile_set.is_null()) {
  1333. return RBSet<TerrainConstraint>();
  1334. }
  1335. // Compute the constraints needed from the surrounding tiles.
  1336. RBSet<TerrainConstraint> output;
  1337. output.insert(TerrainConstraint(tile_set, p_position, p_terrains_pattern.get_terrain()));
  1338. for (uint32_t i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  1339. TileSet::CellNeighbor side = TileSet::CellNeighbor(i);
  1340. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, side)) {
  1341. TerrainConstraint c = TerrainConstraint(tile_set, p_position, side, p_terrains_pattern.get_terrain_peering_bit(side));
  1342. output.insert(c);
  1343. }
  1344. }
  1345. return output;
  1346. }
  1347. 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 {
  1348. if (tile_set.is_null()) {
  1349. return RBSet<TerrainConstraint>();
  1350. }
  1351. ERR_FAIL_INDEX_V(p_terrain_set, tile_set->get_terrain_sets_count(), RBSet<TerrainConstraint>());
  1352. // Build a set of dummy constraints to get the constrained points.
  1353. RBSet<TerrainConstraint> dummy_constraints;
  1354. for (const Vector2i &E : p_painted) {
  1355. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) { // Iterates over neighbor bits.
  1356. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  1357. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, bit)) {
  1358. dummy_constraints.insert(TerrainConstraint(tile_set, E, bit, -1));
  1359. }
  1360. }
  1361. }
  1362. // For each constrained point, we get all overlapping tiles, and select the most adequate terrain for it.
  1363. RBSet<TerrainConstraint> constraints;
  1364. for (const TerrainConstraint &E_constraint : dummy_constraints) {
  1365. HashMap<int, int> terrain_count;
  1366. // Count the number of occurrences per terrain.
  1367. HashMap<Vector2i, TileSet::CellNeighbor> overlapping_terrain_bits = E_constraint.get_overlapping_coords_and_peering_bits();
  1368. for (const KeyValue<Vector2i, TileSet::CellNeighbor> &E_overlapping : overlapping_terrain_bits) {
  1369. TileData *neighbor_tile_data = nullptr;
  1370. TileMapCell neighbor_cell = get_cell(E_overlapping.key);
  1371. if (neighbor_cell.source_id != TileSet::INVALID_SOURCE) {
  1372. Ref<TileSetSource> source = tile_set->get_source(neighbor_cell.source_id);
  1373. Ref<TileSetAtlasSource> atlas_source = source;
  1374. if (atlas_source.is_valid()) {
  1375. TileData *tile_data = atlas_source->get_tile_data(neighbor_cell.get_atlas_coords(), neighbor_cell.alternative_tile);
  1376. if (tile_data && tile_data->get_terrain_set() == p_terrain_set) {
  1377. neighbor_tile_data = tile_data;
  1378. }
  1379. }
  1380. }
  1381. int terrain = neighbor_tile_data ? neighbor_tile_data->get_terrain_peering_bit(TileSet::CellNeighbor(E_overlapping.value)) : -1;
  1382. if (!p_ignore_empty_terrains || terrain >= 0) {
  1383. if (!terrain_count.has(terrain)) {
  1384. terrain_count[terrain] = 0;
  1385. }
  1386. terrain_count[terrain] += 1;
  1387. }
  1388. }
  1389. // Get the terrain with the max number of occurrences.
  1390. int max = 0;
  1391. int max_terrain = -1;
  1392. for (const KeyValue<int, int> &E_terrain_count : terrain_count) {
  1393. if (E_terrain_count.value > max) {
  1394. max = E_terrain_count.value;
  1395. max_terrain = E_terrain_count.key;
  1396. }
  1397. }
  1398. // Set the adequate terrain.
  1399. if (max > 0) {
  1400. TerrainConstraint c = E_constraint;
  1401. c.set_terrain(max_terrain);
  1402. constraints.insert(c);
  1403. }
  1404. }
  1405. // Add the centers as constraints.
  1406. for (Vector2i E_coords : p_painted) {
  1407. TileData *tile_data = nullptr;
  1408. TileMapCell cell = get_cell(E_coords);
  1409. if (cell.source_id != TileSet::INVALID_SOURCE) {
  1410. Ref<TileSetSource> source = tile_set->get_source(cell.source_id);
  1411. Ref<TileSetAtlasSource> atlas_source = source;
  1412. if (atlas_source.is_valid()) {
  1413. tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  1414. }
  1415. }
  1416. int terrain = (tile_data && tile_data->get_terrain_set() == p_terrain_set) ? tile_data->get_terrain() : -1;
  1417. if (!p_ignore_empty_terrains || terrain >= 0) {
  1418. constraints.insert(TerrainConstraint(tile_set, E_coords, terrain));
  1419. }
  1420. }
  1421. return constraints;
  1422. }
  1423. void TileMapLayer::_tile_set_changed() {
  1424. dirty.flags[DIRTY_FLAGS_TILE_SET] = true;
  1425. _queue_internal_update();
  1426. emit_signal(CoreStringName(changed));
  1427. }
  1428. void TileMapLayer::_renamed() {
  1429. emit_signal(CoreStringName(changed));
  1430. }
  1431. void TileMapLayer::_update_notify_local_transform() {
  1432. bool notify = is_using_kinematic_bodies() || is_y_sort_enabled();
  1433. if (!notify) {
  1434. if (is_y_sort_enabled()) {
  1435. notify = true;
  1436. }
  1437. }
  1438. set_notify_local_transform(notify);
  1439. }
  1440. void TileMapLayer::_queue_internal_update() {
  1441. if (pending_update) {
  1442. return;
  1443. }
  1444. // Don't update when outside the tree, it doesn't do anything useful, and causes threading problems.
  1445. if (is_inside_tree()) {
  1446. pending_update = true;
  1447. callable_mp(this, &TileMapLayer::_deferred_internal_update).call_deferred();
  1448. }
  1449. }
  1450. void TileMapLayer::_deferred_internal_update() {
  1451. // Other updates.
  1452. if (!pending_update) {
  1453. return;
  1454. }
  1455. // Update dirty quadrants on layers.
  1456. _internal_update(false);
  1457. }
  1458. void TileMapLayer::_internal_update(bool p_force_cleanup) {
  1459. // Find TileData that need a runtime modification.
  1460. // This may add cells to the dirty list if a runtime modification has been notified.
  1461. _build_runtime_update_tile_data(p_force_cleanup);
  1462. // Callback for implementing custom subsystems.
  1463. // This may add to the dirty list if some cells are changed inside _update_cells.
  1464. _update_cells_callback(p_force_cleanup);
  1465. // Update all subsystems.
  1466. _rendering_update(p_force_cleanup);
  1467. _physics_update(p_force_cleanup);
  1468. _navigation_update(p_force_cleanup);
  1469. _scenes_update(p_force_cleanup);
  1470. #ifdef DEBUG_ENABLED
  1471. _debug_update(p_force_cleanup);
  1472. #endif // DEBUG_ENABLED
  1473. _clear_runtime_update_tile_data();
  1474. // Clear the "what is dirty" flags.
  1475. for (int i = 0; i < DIRTY_FLAGS_MAX; i++) {
  1476. dirty.flags[i] = false;
  1477. }
  1478. // List the cells to delete definitely.
  1479. Vector<Vector2i> to_delete;
  1480. for (SelfList<CellData> *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) {
  1481. CellData &cell_data = *cell_data_list_element->self();
  1482. // Select the cell from tile_map if it is invalid.
  1483. if (cell_data.cell.source_id == TileSet::INVALID_SOURCE) {
  1484. to_delete.push_back(cell_data.coords);
  1485. }
  1486. }
  1487. // Remove cells that are empty after the cleanup.
  1488. for (const Vector2i &coords : to_delete) {
  1489. tile_map_layer_data.erase(coords);
  1490. }
  1491. // Clear the dirty cells list.
  1492. dirty.cell_list.clear();
  1493. pending_update = false;
  1494. }
  1495. void TileMapLayer::_physics_interpolated_changed() {
  1496. RenderingServer *rs = RenderingServer::get_singleton();
  1497. bool interpolated = is_physics_interpolated();
  1498. bool needs_reset = interpolated && is_visible_in_tree();
  1499. for (const KeyValue<Vector2i, Ref<RenderingQuadrant>> &kv : rendering_quadrant_map) {
  1500. for (const RID &ci : kv.value->canvas_items) {
  1501. if (ci.is_valid()) {
  1502. rs->canvas_item_set_interpolated(ci, interpolated);
  1503. if (needs_reset) {
  1504. rs->canvas_item_reset_physics_interpolation(ci);
  1505. }
  1506. }
  1507. }
  1508. }
  1509. for (const KeyValue<Vector2i, CellData> &E : tile_map_layer_data) {
  1510. for (const LocalVector<RID> &polygons : E.value.occluders) {
  1511. for (const RID &occluder_id : polygons) {
  1512. if (occluder_id.is_valid()) {
  1513. rs->canvas_light_occluder_set_interpolated(occluder_id, interpolated);
  1514. if (needs_reset) {
  1515. rs->canvas_light_occluder_reset_physics_interpolation(occluder_id);
  1516. }
  1517. }
  1518. }
  1519. }
  1520. }
  1521. }
  1522. void TileMapLayer::_notification(int p_what) {
  1523. switch (p_what) {
  1524. case NOTIFICATION_POSTINITIALIZE: {
  1525. connect(SNAME("renamed"), callable_mp(this, &TileMapLayer::_renamed));
  1526. break;
  1527. }
  1528. case NOTIFICATION_ENTER_TREE: {
  1529. _update_notify_local_transform();
  1530. dirty.flags[DIRTY_FLAGS_LAYER_IN_TREE] = true;
  1531. _queue_internal_update();
  1532. } break;
  1533. case NOTIFICATION_EXIT_TREE: {
  1534. dirty.flags[DIRTY_FLAGS_LAYER_IN_TREE] = true;
  1535. // Update immediately on exiting, and force cleanup.
  1536. _internal_update(true);
  1537. } break;
  1538. case NOTIFICATION_ENTER_CANVAS: {
  1539. dirty.flags[DIRTY_FLAGS_LAYER_IN_CANVAS] = true;
  1540. _queue_internal_update();
  1541. } break;
  1542. case NOTIFICATION_EXIT_CANVAS: {
  1543. dirty.flags[DIRTY_FLAGS_LAYER_IN_CANVAS] = true;
  1544. // Update immediately on exiting, and force cleanup.
  1545. _internal_update(true);
  1546. } break;
  1547. case NOTIFICATION_VISIBILITY_CHANGED: {
  1548. dirty.flags[DIRTY_FLAGS_LAYER_VISIBILITY] = true;
  1549. _queue_internal_update();
  1550. } break;
  1551. }
  1552. _rendering_notification(p_what);
  1553. _physics_notification(p_what);
  1554. _navigation_notification(p_what);
  1555. }
  1556. void TileMapLayer::_bind_methods() {
  1557. // --- Cells manipulation ---
  1558. // Generic cells manipulations and access.
  1559. ClassDB::bind_method(D_METHOD("set_cell", "coords", "source_id", "atlas_coords", "alternative_tile"), &TileMapLayer::set_cell, DEFVAL(TileSet::INVALID_SOURCE), DEFVAL(TileSetSource::INVALID_ATLAS_COORDS), DEFVAL(0));
  1560. ClassDB::bind_method(D_METHOD("erase_cell", "coords"), &TileMapLayer::erase_cell);
  1561. ClassDB::bind_method(D_METHOD("fix_invalid_tiles"), &TileMapLayer::fix_invalid_tiles);
  1562. ClassDB::bind_method(D_METHOD("clear"), &TileMapLayer::clear);
  1563. ClassDB::bind_method(D_METHOD("get_cell_source_id", "coords"), &TileMapLayer::get_cell_source_id);
  1564. ClassDB::bind_method(D_METHOD("get_cell_atlas_coords", "coords"), &TileMapLayer::get_cell_atlas_coords);
  1565. ClassDB::bind_method(D_METHOD("get_cell_alternative_tile", "coords"), &TileMapLayer::get_cell_alternative_tile);
  1566. ClassDB::bind_method(D_METHOD("get_cell_tile_data", "coords"), &TileMapLayer::get_cell_tile_data);
  1567. ClassDB::bind_method(D_METHOD("is_cell_flipped_h", "coords"), &TileMapLayer::is_cell_flipped_h);
  1568. ClassDB::bind_method(D_METHOD("is_cell_flipped_v", "coords"), &TileMapLayer::is_cell_flipped_v);
  1569. ClassDB::bind_method(D_METHOD("is_cell_transposed", "coords"), &TileMapLayer::is_cell_transposed);
  1570. ClassDB::bind_method(D_METHOD("get_used_cells"), &TileMapLayer::get_used_cells);
  1571. ClassDB::bind_method(D_METHOD("get_used_cells_by_id", "source_id", "atlas_coords", "alternative_tile"), &TileMapLayer::get_used_cells_by_id, DEFVAL(TileSet::INVALID_SOURCE), DEFVAL(TileSetSource::INVALID_ATLAS_COORDS), DEFVAL(TileSetSource::INVALID_TILE_ALTERNATIVE));
  1572. ClassDB::bind_method(D_METHOD("get_used_rect"), &TileMapLayer::get_used_rect);
  1573. // Patterns.
  1574. ClassDB::bind_method(D_METHOD("get_pattern", "coords_array"), &TileMapLayer::get_pattern);
  1575. ClassDB::bind_method(D_METHOD("set_pattern", "position", "pattern"), &TileMapLayer::set_pattern);
  1576. // Terrains.
  1577. ClassDB::bind_method(D_METHOD("set_cells_terrain_connect", "cells", "terrain_set", "terrain", "ignore_empty_terrains"), &TileMapLayer::set_cells_terrain_connect, DEFVAL(true));
  1578. ClassDB::bind_method(D_METHOD("set_cells_terrain_path", "path", "terrain_set", "terrain", "ignore_empty_terrains"), &TileMapLayer::set_cells_terrain_path, DEFVAL(true));
  1579. // --- Physics helpers ---
  1580. ClassDB::bind_method(D_METHOD("has_body_rid", "body"), &TileMapLayer::has_body_rid);
  1581. ClassDB::bind_method(D_METHOD("get_coords_for_body_rid", "body"), &TileMapLayer::get_coords_for_body_rid);
  1582. // --- Runtime ---
  1583. ClassDB::bind_method(D_METHOD("update_internals"), &TileMapLayer::update_internals);
  1584. ClassDB::bind_method(D_METHOD("notify_runtime_tile_data_update"), &TileMapLayer::notify_runtime_tile_data_update, DEFVAL(-1));
  1585. // --- Shortcuts to methods defined in TileSet ---
  1586. ClassDB::bind_method(D_METHOD("map_pattern", "position_in_tilemap", "coords_in_pattern", "pattern"), &TileMapLayer::map_pattern);
  1587. ClassDB::bind_method(D_METHOD("get_surrounding_cells", "coords"), &TileMapLayer::get_surrounding_cells);
  1588. ClassDB::bind_method(D_METHOD("get_neighbor_cell", "coords", "neighbor"), &TileMapLayer::get_neighbor_cell);
  1589. ClassDB::bind_method(D_METHOD("map_to_local", "map_position"), &TileMapLayer::map_to_local);
  1590. ClassDB::bind_method(D_METHOD("local_to_map", "local_position"), &TileMapLayer::local_to_map);
  1591. // --- Accessors ---
  1592. ClassDB::bind_method(D_METHOD("set_tile_map_data_from_array", "tile_map_layer_data"), &TileMapLayer::set_tile_map_data_from_array);
  1593. ClassDB::bind_method(D_METHOD("get_tile_map_data_as_array"), &TileMapLayer::get_tile_map_data_as_array);
  1594. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &TileMapLayer::set_enabled);
  1595. ClassDB::bind_method(D_METHOD("is_enabled"), &TileMapLayer::is_enabled);
  1596. ClassDB::bind_method(D_METHOD("set_tile_set", "tile_set"), &TileMapLayer::set_tile_set);
  1597. ClassDB::bind_method(D_METHOD("get_tile_set"), &TileMapLayer::get_tile_set);
  1598. ClassDB::bind_method(D_METHOD("set_y_sort_origin", "y_sort_origin"), &TileMapLayer::set_y_sort_origin);
  1599. ClassDB::bind_method(D_METHOD("get_y_sort_origin"), &TileMapLayer::get_y_sort_origin);
  1600. ClassDB::bind_method(D_METHOD("set_x_draw_order_reversed", "x_draw_order_reversed"), &TileMapLayer::set_x_draw_order_reversed);
  1601. ClassDB::bind_method(D_METHOD("is_x_draw_order_reversed"), &TileMapLayer::is_x_draw_order_reversed);
  1602. ClassDB::bind_method(D_METHOD("set_rendering_quadrant_size", "size"), &TileMapLayer::set_rendering_quadrant_size);
  1603. ClassDB::bind_method(D_METHOD("get_rendering_quadrant_size"), &TileMapLayer::get_rendering_quadrant_size);
  1604. ClassDB::bind_method(D_METHOD("set_collision_enabled", "enabled"), &TileMapLayer::set_collision_enabled);
  1605. ClassDB::bind_method(D_METHOD("is_collision_enabled"), &TileMapLayer::is_collision_enabled);
  1606. ClassDB::bind_method(D_METHOD("set_use_kinematic_bodies", "use_kinematic_bodies"), &TileMapLayer::set_use_kinematic_bodies);
  1607. ClassDB::bind_method(D_METHOD("is_using_kinematic_bodies"), &TileMapLayer::is_using_kinematic_bodies);
  1608. ClassDB::bind_method(D_METHOD("set_collision_visibility_mode", "visibility_mode"), &TileMapLayer::set_collision_visibility_mode);
  1609. ClassDB::bind_method(D_METHOD("get_collision_visibility_mode"), &TileMapLayer::get_collision_visibility_mode);
  1610. ClassDB::bind_method(D_METHOD("set_occlusion_enabled", "enabled"), &TileMapLayer::set_occlusion_enabled);
  1611. ClassDB::bind_method(D_METHOD("is_occlusion_enabled"), &TileMapLayer::is_occlusion_enabled);
  1612. ClassDB::bind_method(D_METHOD("set_navigation_enabled", "enabled"), &TileMapLayer::set_navigation_enabled);
  1613. ClassDB::bind_method(D_METHOD("is_navigation_enabled"), &TileMapLayer::is_navigation_enabled);
  1614. ClassDB::bind_method(D_METHOD("set_navigation_map", "map"), &TileMapLayer::set_navigation_map);
  1615. ClassDB::bind_method(D_METHOD("get_navigation_map"), &TileMapLayer::get_navigation_map);
  1616. ClassDB::bind_method(D_METHOD("set_navigation_visibility_mode", "show_navigation"), &TileMapLayer::set_navigation_visibility_mode);
  1617. ClassDB::bind_method(D_METHOD("get_navigation_visibility_mode"), &TileMapLayer::get_navigation_visibility_mode);
  1618. GDVIRTUAL_BIND(_use_tile_data_runtime_update, "coords");
  1619. GDVIRTUAL_BIND(_tile_data_runtime_update, "coords", "tile_data");
  1620. GDVIRTUAL_BIND(_update_cells, "coords", "forced_cleanup");
  1621. ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "tile_map_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_tile_map_data_from_array", "get_tile_map_data_as_array");
  1622. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
  1623. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "tile_set", PROPERTY_HINT_RESOURCE_TYPE, "TileSet"), "set_tile_set", "get_tile_set");
  1624. ADD_GROUP("Rendering", "");
  1625. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "occlusion_enabled"), "set_occlusion_enabled", "is_occlusion_enabled");
  1626. ADD_PROPERTY(PropertyInfo(Variant::INT, "y_sort_origin"), "set_y_sort_origin", "get_y_sort_origin");
  1627. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "x_draw_order_reversed"), "set_x_draw_order_reversed", "is_x_draw_order_reversed");
  1628. ADD_PROPERTY(PropertyInfo(Variant::INT, "rendering_quadrant_size"), "set_rendering_quadrant_size", "get_rendering_quadrant_size");
  1629. ADD_GROUP("Physics", "");
  1630. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collision_enabled"), "set_collision_enabled", "is_collision_enabled");
  1631. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_kinematic_bodies"), "set_use_kinematic_bodies", "is_using_kinematic_bodies");
  1632. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_visibility_mode", PROPERTY_HINT_ENUM, "Default,Force Show,Force Hide"), "set_collision_visibility_mode", "get_collision_visibility_mode");
  1633. ADD_GROUP("Navigation", "");
  1634. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "navigation_enabled"), "set_navigation_enabled", "is_navigation_enabled");
  1635. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_visibility_mode", PROPERTY_HINT_ENUM, "Default,Force Show,Force Hide"), "set_navigation_visibility_mode", "get_navigation_visibility_mode");
  1636. ADD_SIGNAL(MethodInfo(CoreStringName(changed)));
  1637. ADD_PROPERTY_DEFAULT("tile_map_data_format", TileMapDataFormat::TILE_MAP_DATA_FORMAT_1);
  1638. BIND_ENUM_CONSTANT(DEBUG_VISIBILITY_MODE_DEFAULT);
  1639. BIND_ENUM_CONSTANT(DEBUG_VISIBILITY_MODE_FORCE_HIDE);
  1640. BIND_ENUM_CONSTANT(DEBUG_VISIBILITY_MODE_FORCE_SHOW);
  1641. }
  1642. void TileMapLayer::_validate_property(PropertyInfo &p_property) const {
  1643. if (is_y_sort_enabled()) {
  1644. if (p_property.name == "rendering_quadrant_size") {
  1645. p_property.usage |= PROPERTY_USAGE_READ_ONLY;
  1646. }
  1647. } else {
  1648. if (p_property.name == "x_draw_order_reversed") {
  1649. p_property.usage |= PROPERTY_USAGE_READ_ONLY;
  1650. }
  1651. }
  1652. }
  1653. void TileMapLayer::_update_self_texture_filter(RS::CanvasItemTextureFilter p_texture_filter) {
  1654. // Set a default texture filter for the whole tilemap.
  1655. CanvasItem::_update_self_texture_filter(p_texture_filter);
  1656. dirty.flags[DIRTY_FLAGS_LAYER_TEXTURE_FILTER] = true;
  1657. _queue_internal_update();
  1658. emit_signal(CoreStringName(changed));
  1659. }
  1660. void TileMapLayer::_update_self_texture_repeat(RS::CanvasItemTextureRepeat p_texture_repeat) {
  1661. // Set a default texture repeat for the whole tilemap.
  1662. CanvasItem::_update_self_texture_repeat(p_texture_repeat);
  1663. dirty.flags[DIRTY_FLAGS_LAYER_TEXTURE_REPEAT] = true;
  1664. _queue_internal_update();
  1665. emit_signal(CoreStringName(changed));
  1666. }
  1667. #ifdef TOOLS_ENABLED
  1668. bool TileMapLayer::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
  1669. return tile_set.is_valid() && get_cell_source_id(local_to_map(p_point)) != TileSet::INVALID_SOURCE;
  1670. }
  1671. #endif
  1672. void TileMapLayer::set_as_tile_map_internal_node(int p_index) {
  1673. // Compatibility with TileMap.
  1674. ERR_FAIL_NULL(get_parent());
  1675. tile_map_node = Object::cast_to<TileMap>(get_parent());
  1676. set_use_parent_material(true);
  1677. force_parent_owned();
  1678. if (layer_index_in_tile_map_node != p_index) {
  1679. layer_index_in_tile_map_node = p_index;
  1680. dirty.flags[DIRTY_FLAGS_LAYER_INDEX_IN_TILE_MAP_NODE] = true;
  1681. _queue_internal_update();
  1682. }
  1683. }
  1684. Rect2 TileMapLayer::get_rect(bool &r_changed) const {
  1685. if (tile_set.is_null()) {
  1686. r_changed = rect_cache != Rect2();
  1687. return Rect2();
  1688. }
  1689. // Compute the displayed area of the tilemap.
  1690. r_changed = false;
  1691. #ifdef DEBUG_ENABLED
  1692. if (rect_cache_dirty) {
  1693. Rect2 r_total;
  1694. bool first = true;
  1695. for (const KeyValue<Vector2i, CellData> &E : tile_map_layer_data) {
  1696. Rect2 r;
  1697. r.position = tile_set->map_to_local(E.key);
  1698. r.size = Size2();
  1699. if (first) {
  1700. r_total = r;
  1701. first = false;
  1702. } else {
  1703. r_total = r_total.merge(r);
  1704. }
  1705. }
  1706. r_changed = rect_cache != r_total;
  1707. rect_cache = r_total;
  1708. rect_cache_dirty = false;
  1709. }
  1710. #endif
  1711. return rect_cache;
  1712. }
  1713. HashMap<Vector2i, TileSet::TerrainsPattern> TileMapLayer::terrain_fill_constraints(const Vector<Vector2i> &p_to_replace, int p_terrain_set, const RBSet<TerrainConstraint> &p_constraints) const {
  1714. if (tile_set.is_null()) {
  1715. return HashMap<Vector2i, TileSet::TerrainsPattern>();
  1716. }
  1717. // Copy the constraints set.
  1718. RBSet<TerrainConstraint> constraints = p_constraints;
  1719. // Output map.
  1720. HashMap<Vector2i, TileSet::TerrainsPattern> output;
  1721. // Add all positions to a set.
  1722. for (int i = 0; i < p_to_replace.size(); i++) {
  1723. const Vector2i &coords = p_to_replace[i];
  1724. // Select the best pattern for the given constraints.
  1725. TileSet::TerrainsPattern current_pattern = TileSet::TerrainsPattern(*tile_set, p_terrain_set);
  1726. TileMapCell cell = get_cell(coords);
  1727. if (cell.source_id != TileSet::INVALID_SOURCE) {
  1728. TileSetSource *source = *tile_set->get_source(cell.source_id);
  1729. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  1730. if (atlas_source) {
  1731. // Get tile data.
  1732. TileData *tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  1733. if (tile_data && tile_data->get_terrain_set() == p_terrain_set) {
  1734. current_pattern = tile_data->get_terrains_pattern();
  1735. }
  1736. }
  1737. }
  1738. TileSet::TerrainsPattern pattern = _get_best_terrain_pattern_for_constraints(p_terrain_set, coords, constraints, current_pattern);
  1739. // Update the constraint set with the new ones.
  1740. RBSet<TerrainConstraint> new_constraints = _get_terrain_constraints_from_added_pattern(coords, p_terrain_set, pattern);
  1741. for (const TerrainConstraint &E_constraint : new_constraints) {
  1742. if (constraints.has(E_constraint)) {
  1743. constraints.erase(E_constraint);
  1744. }
  1745. TerrainConstraint c = E_constraint;
  1746. c.set_priority(5);
  1747. constraints.insert(c);
  1748. }
  1749. output[coords] = pattern;
  1750. }
  1751. return output;
  1752. }
  1753. 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) const {
  1754. HashMap<Vector2i, TileSet::TerrainsPattern> output;
  1755. ERR_FAIL_COND_V(tile_set.is_null(), output);
  1756. ERR_FAIL_INDEX_V(p_terrain_set, tile_set->get_terrain_sets_count(), output);
  1757. // Build list and set of tiles that can be modified (painted and their surroundings).
  1758. Vector<Vector2i> can_modify_list;
  1759. RBSet<Vector2i> can_modify_set;
  1760. RBSet<Vector2i> painted_set;
  1761. for (int i = p_coords_array.size() - 1; i >= 0; i--) {
  1762. const Vector2i &coords = p_coords_array[i];
  1763. can_modify_list.push_back(coords);
  1764. can_modify_set.insert(coords);
  1765. painted_set.insert(coords);
  1766. }
  1767. for (Vector2i coords : p_coords_array) {
  1768. // Find the adequate neighbor.
  1769. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  1770. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  1771. if (tile_set->is_existing_neighbor(bit)) {
  1772. Vector2i neighbor = tile_set->get_neighbor_cell(coords, bit);
  1773. if (!can_modify_set.has(neighbor)) {
  1774. can_modify_list.push_back(neighbor);
  1775. can_modify_set.insert(neighbor);
  1776. }
  1777. }
  1778. }
  1779. }
  1780. // 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.
  1781. RBSet<Vector2i> cells_with_terrain_center_bit;
  1782. for (Vector2i coords : can_modify_set) {
  1783. bool connect = false;
  1784. if (painted_set.has(coords)) {
  1785. connect = true;
  1786. } else {
  1787. // Get the center bit of the cell.
  1788. TileData *tile_data = nullptr;
  1789. TileMapCell cell = get_cell(coords);
  1790. if (cell.source_id != TileSet::INVALID_SOURCE) {
  1791. Ref<TileSetSource> source = tile_set->get_source(cell.source_id);
  1792. Ref<TileSetAtlasSource> atlas_source = source;
  1793. if (atlas_source.is_valid()) {
  1794. tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  1795. }
  1796. }
  1797. if (tile_data && tile_data->get_terrain_set() == p_terrain_set && tile_data->get_terrain() == p_terrain) {
  1798. connect = true;
  1799. }
  1800. }
  1801. if (connect) {
  1802. cells_with_terrain_center_bit.insert(coords);
  1803. }
  1804. }
  1805. RBSet<TerrainConstraint> constraints;
  1806. // Add new constraints from the path drawn.
  1807. for (Vector2i coords : p_coords_array) {
  1808. // Constraints on the center bit.
  1809. TerrainConstraint c = TerrainConstraint(tile_set, coords, p_terrain);
  1810. c.set_priority(10);
  1811. constraints.insert(c);
  1812. // Constraints on the connecting bits.
  1813. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  1814. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  1815. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, bit)) {
  1816. c = TerrainConstraint(tile_set, coords, bit, p_terrain);
  1817. c.set_priority(10);
  1818. if ((int(bit) % 2) == 0) {
  1819. // Side peering bits: add the constraint if the center is of the same terrain.
  1820. Vector2i neighbor = tile_set->get_neighbor_cell(coords, bit);
  1821. if (cells_with_terrain_center_bit.has(neighbor)) {
  1822. constraints.insert(c);
  1823. }
  1824. } else {
  1825. // Corner peering bits: add the constraint if all tiles on the constraint has the same center bit.
  1826. HashMap<Vector2i, TileSet::CellNeighbor> overlapping_terrain_bits = c.get_overlapping_coords_and_peering_bits();
  1827. bool valid = true;
  1828. for (KeyValue<Vector2i, TileSet::CellNeighbor> kv : overlapping_terrain_bits) {
  1829. if (!cells_with_terrain_center_bit.has(kv.key)) {
  1830. valid = false;
  1831. break;
  1832. }
  1833. }
  1834. if (valid) {
  1835. constraints.insert(c);
  1836. }
  1837. }
  1838. }
  1839. }
  1840. }
  1841. // Fills in the constraint list from existing tiles.
  1842. for (TerrainConstraint c : _get_terrain_constraints_from_painted_cells_list(painted_set, p_terrain_set, p_ignore_empty_terrains)) {
  1843. constraints.insert(c);
  1844. }
  1845. // Fill the terrains.
  1846. output = terrain_fill_constraints(can_modify_list, p_terrain_set, constraints);
  1847. return output;
  1848. }
  1849. 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) const {
  1850. HashMap<Vector2i, TileSet::TerrainsPattern> output;
  1851. ERR_FAIL_COND_V(tile_set.is_null(), output);
  1852. ERR_FAIL_INDEX_V(p_terrain_set, tile_set->get_terrain_sets_count(), output);
  1853. // Make sure the path is correct and build the peering bit list while doing it.
  1854. Vector<TileSet::CellNeighbor> neighbor_list;
  1855. for (int i = 0; i < p_coords_array.size() - 1; i++) {
  1856. // Find the adequate neighbor.
  1857. TileSet::CellNeighbor found_bit = TileSet::CELL_NEIGHBOR_MAX;
  1858. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  1859. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  1860. if (tile_set->is_existing_neighbor(bit)) {
  1861. if (tile_set->get_neighbor_cell(p_coords_array[i], bit) == p_coords_array[i + 1]) {
  1862. found_bit = bit;
  1863. break;
  1864. }
  1865. }
  1866. }
  1867. 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]));
  1868. neighbor_list.push_back(found_bit);
  1869. }
  1870. // Build list and set of tiles that can be modified (painted and their surroundings).
  1871. Vector<Vector2i> can_modify_list;
  1872. RBSet<Vector2i> can_modify_set;
  1873. RBSet<Vector2i> painted_set;
  1874. for (int i = p_coords_array.size() - 1; i >= 0; i--) {
  1875. const Vector2i &coords = p_coords_array[i];
  1876. can_modify_list.push_back(coords);
  1877. can_modify_set.insert(coords);
  1878. painted_set.insert(coords);
  1879. }
  1880. for (Vector2i coords : p_coords_array) {
  1881. // Find the adequate neighbor.
  1882. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  1883. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  1884. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, bit)) {
  1885. Vector2i neighbor = tile_set->get_neighbor_cell(coords, bit);
  1886. if (!can_modify_set.has(neighbor)) {
  1887. can_modify_list.push_back(neighbor);
  1888. can_modify_set.insert(neighbor);
  1889. }
  1890. }
  1891. }
  1892. }
  1893. RBSet<TerrainConstraint> constraints;
  1894. // Add new constraints from the path drawn.
  1895. for (Vector2i coords : p_coords_array) {
  1896. // Constraints on the center bit.
  1897. TerrainConstraint c = TerrainConstraint(tile_set, coords, p_terrain);
  1898. c.set_priority(10);
  1899. constraints.insert(c);
  1900. }
  1901. for (int i = 0; i < p_coords_array.size() - 1; i++) {
  1902. // Constraints on the peering bits.
  1903. TerrainConstraint c = TerrainConstraint(tile_set, p_coords_array[i], neighbor_list[i], p_terrain);
  1904. c.set_priority(10);
  1905. constraints.insert(c);
  1906. }
  1907. // Fills in the constraint list from existing tiles.
  1908. for (TerrainConstraint c : _get_terrain_constraints_from_painted_cells_list(painted_set, p_terrain_set, p_ignore_empty_terrains)) {
  1909. constraints.insert(c);
  1910. }
  1911. // Fill the terrains.
  1912. output = terrain_fill_constraints(can_modify_list, p_terrain_set, constraints);
  1913. return output;
  1914. }
  1915. 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) const {
  1916. HashMap<Vector2i, TileSet::TerrainsPattern> output;
  1917. ERR_FAIL_COND_V(tile_set.is_null(), output);
  1918. ERR_FAIL_INDEX_V(p_terrain_set, tile_set->get_terrain_sets_count(), output);
  1919. // Build list and set of tiles that can be modified (painted and their surroundings).
  1920. Vector<Vector2i> can_modify_list;
  1921. RBSet<Vector2i> can_modify_set;
  1922. RBSet<Vector2i> painted_set;
  1923. for (int i = p_coords_array.size() - 1; i >= 0; i--) {
  1924. const Vector2i &coords = p_coords_array[i];
  1925. can_modify_list.push_back(coords);
  1926. can_modify_set.insert(coords);
  1927. painted_set.insert(coords);
  1928. }
  1929. for (Vector2i coords : p_coords_array) {
  1930. // Find the adequate neighbor.
  1931. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  1932. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  1933. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, bit)) {
  1934. Vector2i neighbor = tile_set->get_neighbor_cell(coords, bit);
  1935. if (!can_modify_set.has(neighbor)) {
  1936. can_modify_list.push_back(neighbor);
  1937. can_modify_set.insert(neighbor);
  1938. }
  1939. }
  1940. }
  1941. }
  1942. // Add constraint by the new ones.
  1943. RBSet<TerrainConstraint> constraints;
  1944. // Add new constraints from the path drawn.
  1945. for (Vector2i coords : p_coords_array) {
  1946. // Constraints on the center bit.
  1947. RBSet<TerrainConstraint> added_constraints = _get_terrain_constraints_from_added_pattern(coords, p_terrain_set, p_terrains_pattern);
  1948. for (TerrainConstraint c : added_constraints) {
  1949. c.set_priority(10);
  1950. constraints.insert(c);
  1951. }
  1952. }
  1953. // Fills in the constraint list from modified tiles border.
  1954. for (TerrainConstraint c : _get_terrain_constraints_from_painted_cells_list(painted_set, p_terrain_set, p_ignore_empty_terrains)) {
  1955. constraints.insert(c);
  1956. }
  1957. // Fill the terrains.
  1958. output = terrain_fill_constraints(can_modify_list, p_terrain_set, constraints);
  1959. return output;
  1960. }
  1961. TileMapCell TileMapLayer::get_cell(const Vector2i &p_coords) const {
  1962. if (!tile_map_layer_data.has(p_coords)) {
  1963. return TileMapCell();
  1964. } else {
  1965. return tile_map_layer_data.find(p_coords)->value.cell;
  1966. }
  1967. }
  1968. void TileMapLayer::draw_tile(RID p_canvas_item, const Vector2 &p_position, const Ref<TileSet> p_tile_set, int p_atlas_source_id, const Vector2i &p_atlas_coords, int p_alternative_tile, int p_frame, Color p_modulation, const TileData *p_tile_data_override, real_t p_normalized_animation_offset) {
  1969. ERR_FAIL_COND(p_tile_set.is_null());
  1970. ERR_FAIL_COND(!p_tile_set->has_source(p_atlas_source_id));
  1971. ERR_FAIL_COND(!p_tile_set->get_source(p_atlas_source_id)->has_tile(p_atlas_coords));
  1972. ERR_FAIL_COND(!p_tile_set->get_source(p_atlas_source_id)->has_alternative_tile(p_atlas_coords, p_alternative_tile));
  1973. TileSetSource *source = *p_tile_set->get_source(p_atlas_source_id);
  1974. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  1975. if (atlas_source) {
  1976. // Check for the frame.
  1977. if (p_frame >= 0) {
  1978. ERR_FAIL_INDEX(p_frame, atlas_source->get_tile_animation_frames_count(p_atlas_coords));
  1979. }
  1980. // Get the texture.
  1981. Ref<Texture2D> tex = atlas_source->get_runtime_texture();
  1982. if (tex.is_null()) {
  1983. return;
  1984. }
  1985. // Check if we are in the texture, return otherwise.
  1986. Vector2i grid_size = atlas_source->get_atlas_grid_size();
  1987. if (p_atlas_coords.x >= grid_size.x || p_atlas_coords.y >= grid_size.y) {
  1988. return;
  1989. }
  1990. // Get tile data.
  1991. const TileData *tile_data = p_tile_data_override ? p_tile_data_override : atlas_source->get_tile_data(p_atlas_coords, p_alternative_tile);
  1992. // Get the tile modulation.
  1993. Color modulate = tile_data->get_modulate() * p_modulation;
  1994. // Compute the offset.
  1995. Vector2 tile_offset = tile_data->get_texture_origin();
  1996. // Get destination rect.
  1997. Rect2 dest_rect;
  1998. dest_rect.size = atlas_source->get_runtime_tile_texture_region(p_atlas_coords).size;
  1999. dest_rect.size.x += FP_ADJUST;
  2000. dest_rect.size.y += FP_ADJUST;
  2001. bool transpose = tile_data->get_transpose() ^ bool(p_alternative_tile & TileSetAtlasSource::TRANSFORM_TRANSPOSE);
  2002. if (transpose) {
  2003. dest_rect.position = (p_position - Vector2(dest_rect.size.y, dest_rect.size.x) / 2 - tile_offset);
  2004. } else {
  2005. dest_rect.position = (p_position - dest_rect.size / 2 - tile_offset);
  2006. }
  2007. if (tile_data->get_flip_h() ^ bool(p_alternative_tile & TileSetAtlasSource::TRANSFORM_FLIP_H)) {
  2008. dest_rect.size.x = -dest_rect.size.x;
  2009. }
  2010. if (tile_data->get_flip_v() ^ bool(p_alternative_tile & TileSetAtlasSource::TRANSFORM_FLIP_V)) {
  2011. dest_rect.size.y = -dest_rect.size.y;
  2012. }
  2013. // Draw the tile.
  2014. if (p_frame >= 0) {
  2015. Rect2i source_rect = atlas_source->get_runtime_tile_texture_region(p_atlas_coords, p_frame);
  2016. tex->draw_rect_region(p_canvas_item, dest_rect, source_rect, modulate, transpose, p_tile_set->is_uv_clipping());
  2017. } else if (atlas_source->get_tile_animation_frames_count(p_atlas_coords) == 1) {
  2018. Rect2i source_rect = atlas_source->get_runtime_tile_texture_region(p_atlas_coords, 0);
  2019. tex->draw_rect_region(p_canvas_item, dest_rect, source_rect, modulate, transpose, p_tile_set->is_uv_clipping());
  2020. } else {
  2021. real_t speed = atlas_source->get_tile_animation_speed(p_atlas_coords);
  2022. real_t animation_duration = atlas_source->get_tile_animation_total_duration(p_atlas_coords) / speed;
  2023. real_t animation_offset = p_normalized_animation_offset * animation_duration;
  2024. // Accumulate durations unaffected by the speed to avoid accumulating floating point division errors.
  2025. // Aka do `sum(duration[i]) / speed` instead of `sum(duration[i] / speed)`.
  2026. real_t time_unscaled = 0.0;
  2027. for (int frame = 0; frame < atlas_source->get_tile_animation_frames_count(p_atlas_coords); frame++) {
  2028. real_t frame_duration_unscaled = atlas_source->get_tile_animation_frame_duration(p_atlas_coords, frame);
  2029. real_t slice_start = time_unscaled / speed;
  2030. real_t slice_end = (time_unscaled + frame_duration_unscaled) / speed;
  2031. RenderingServer::get_singleton()->canvas_item_add_animation_slice(p_canvas_item, animation_duration, slice_start, slice_end, animation_offset);
  2032. Rect2i source_rect = atlas_source->get_runtime_tile_texture_region(p_atlas_coords, frame);
  2033. tex->draw_rect_region(p_canvas_item, dest_rect, source_rect, modulate, transpose, p_tile_set->is_uv_clipping());
  2034. time_unscaled += frame_duration_unscaled;
  2035. }
  2036. RenderingServer::get_singleton()->canvas_item_add_animation_slice(p_canvas_item, 1.0, 0.0, 1.0, 0.0);
  2037. }
  2038. }
  2039. }
  2040. void TileMapLayer::set_cell(const Vector2i &p_coords, int p_source_id, const Vector2i &p_atlas_coords, int p_alternative_tile) {
  2041. // Set the current cell tile (using integer position).
  2042. Vector2i pk(p_coords);
  2043. HashMap<Vector2i, CellData>::Iterator E = tile_map_layer_data.find(pk);
  2044. int source_id = p_source_id;
  2045. Vector2i atlas_coords = p_atlas_coords;
  2046. int alternative_tile = p_alternative_tile;
  2047. if ((source_id == TileSet::INVALID_SOURCE || atlas_coords == TileSetSource::INVALID_ATLAS_COORDS || alternative_tile == TileSetSource::INVALID_TILE_ALTERNATIVE) &&
  2048. (source_id != TileSet::INVALID_SOURCE || atlas_coords != TileSetSource::INVALID_ATLAS_COORDS || alternative_tile != TileSetSource::INVALID_TILE_ALTERNATIVE)) {
  2049. source_id = TileSet::INVALID_SOURCE;
  2050. atlas_coords = TileSetSource::INVALID_ATLAS_COORDS;
  2051. alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE;
  2052. }
  2053. if (!E) {
  2054. if (source_id == TileSet::INVALID_SOURCE) {
  2055. return; // Nothing to do, the tile is already empty.
  2056. }
  2057. // Insert a new cell in the tile map.
  2058. CellData new_cell_data;
  2059. new_cell_data.coords = pk;
  2060. E = tile_map_layer_data.insert(pk, new_cell_data);
  2061. } else {
  2062. if (E->value.cell.source_id == source_id && E->value.cell.get_atlas_coords() == atlas_coords && E->value.cell.alternative_tile == alternative_tile) {
  2063. return; // Nothing changed.
  2064. }
  2065. }
  2066. TileMapCell &c = E->value.cell;
  2067. c.source_id = source_id;
  2068. c.set_atlas_coords(atlas_coords);
  2069. c.alternative_tile = alternative_tile;
  2070. // Make the given cell dirty.
  2071. if (!E->value.dirty_list_element.in_list()) {
  2072. dirty.cell_list.add(&(E->value.dirty_list_element));
  2073. }
  2074. _queue_internal_update();
  2075. used_rect_cache_dirty = true;
  2076. }
  2077. void TileMapLayer::erase_cell(const Vector2i &p_coords) {
  2078. set_cell(p_coords, TileSet::INVALID_SOURCE, TileSetSource::INVALID_ATLAS_COORDS, TileSetSource::INVALID_TILE_ALTERNATIVE);
  2079. }
  2080. void TileMapLayer::fix_invalid_tiles() {
  2081. ERR_FAIL_COND_MSG(tile_set.is_null(), "Cannot call fix_invalid_tiles() on a TileMapLayer without a valid TileSet.");
  2082. RBSet<Vector2i> coords;
  2083. for (const KeyValue<Vector2i, CellData> &E : tile_map_layer_data) {
  2084. TileSetSource *source = *tile_set->get_source(E.value.cell.source_id);
  2085. 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)) {
  2086. coords.insert(E.key);
  2087. }
  2088. }
  2089. for (const Vector2i &E : coords) {
  2090. set_cell(E, TileSet::INVALID_SOURCE, TileSetSource::INVALID_ATLAS_COORDS, TileSetSource::INVALID_TILE_ALTERNATIVE);
  2091. }
  2092. }
  2093. void TileMapLayer::clear() {
  2094. // Remove all tiles.
  2095. for (KeyValue<Vector2i, CellData> &kv : tile_map_layer_data) {
  2096. erase_cell(kv.key);
  2097. }
  2098. used_rect_cache_dirty = true;
  2099. }
  2100. int TileMapLayer::get_cell_source_id(const Vector2i &p_coords) const {
  2101. // Get a cell source id from position.
  2102. HashMap<Vector2i, CellData>::ConstIterator E = tile_map_layer_data.find(p_coords);
  2103. if (!E) {
  2104. return TileSet::INVALID_SOURCE;
  2105. }
  2106. return E->value.cell.source_id;
  2107. }
  2108. Vector2i TileMapLayer::get_cell_atlas_coords(const Vector2i &p_coords) const {
  2109. // Get a cell source id from position.
  2110. HashMap<Vector2i, CellData>::ConstIterator E = tile_map_layer_data.find(p_coords);
  2111. if (!E) {
  2112. return TileSetSource::INVALID_ATLAS_COORDS;
  2113. }
  2114. return E->value.cell.get_atlas_coords();
  2115. }
  2116. int TileMapLayer::get_cell_alternative_tile(const Vector2i &p_coords) const {
  2117. // Get a cell source id from position.
  2118. HashMap<Vector2i, CellData>::ConstIterator E = tile_map_layer_data.find(p_coords);
  2119. if (!E) {
  2120. return TileSetSource::INVALID_TILE_ALTERNATIVE;
  2121. }
  2122. return E->value.cell.alternative_tile;
  2123. }
  2124. TileData *TileMapLayer::get_cell_tile_data(const Vector2i &p_coords) const {
  2125. int source_id = get_cell_source_id(p_coords);
  2126. if (source_id == TileSet::INVALID_SOURCE) {
  2127. return nullptr;
  2128. }
  2129. Ref<TileSetAtlasSource> source = tile_set->get_source(source_id);
  2130. if (source.is_valid()) {
  2131. return source->get_tile_data(get_cell_atlas_coords(p_coords), get_cell_alternative_tile(p_coords));
  2132. }
  2133. return nullptr;
  2134. }
  2135. TypedArray<Vector2i> TileMapLayer::get_used_cells() const {
  2136. // Returns the cells used in the tilemap.
  2137. TypedArray<Vector2i> a;
  2138. for (const KeyValue<Vector2i, CellData> &E : tile_map_layer_data) {
  2139. const TileMapCell &c = E.value.cell;
  2140. if (c.source_id == TileSet::INVALID_SOURCE) {
  2141. continue;
  2142. }
  2143. a.push_back(E.key);
  2144. }
  2145. return a;
  2146. }
  2147. TypedArray<Vector2i> TileMapLayer::get_used_cells_by_id(int p_source_id, const Vector2i &p_atlas_coords, int p_alternative_tile) const {
  2148. // Returns the cells used in the tilemap.
  2149. TypedArray<Vector2i> a;
  2150. for (const KeyValue<Vector2i, CellData> &E : tile_map_layer_data) {
  2151. const TileMapCell &c = E.value.cell;
  2152. if (c.source_id == TileSet::INVALID_SOURCE) {
  2153. continue;
  2154. }
  2155. if ((p_source_id == TileSet::INVALID_SOURCE || p_source_id == c.source_id) &&
  2156. (p_atlas_coords == TileSetSource::INVALID_ATLAS_COORDS || p_atlas_coords == c.get_atlas_coords()) &&
  2157. (p_alternative_tile == TileSetSource::INVALID_TILE_ALTERNATIVE || p_alternative_tile == c.alternative_tile)) {
  2158. a.push_back(E.key);
  2159. }
  2160. }
  2161. return a;
  2162. }
  2163. Rect2i TileMapLayer::get_used_rect() const {
  2164. // Return the rect of the currently used area.
  2165. if (used_rect_cache_dirty) {
  2166. used_rect_cache = Rect2i();
  2167. bool first = true;
  2168. for (const KeyValue<Vector2i, CellData> &E : tile_map_layer_data) {
  2169. const TileMapCell &c = E.value.cell;
  2170. if (c.source_id == TileSet::INVALID_SOURCE) {
  2171. continue;
  2172. }
  2173. if (first) {
  2174. used_rect_cache = Rect2i(E.key, Size2i());
  2175. first = false;
  2176. } else {
  2177. used_rect_cache.expand_to(E.key);
  2178. }
  2179. }
  2180. if (!first) {
  2181. // Only if we have at least one cell.
  2182. // The cache expands to top-left coordinate, so we add one full tile.
  2183. used_rect_cache.size += Vector2i(1, 1);
  2184. }
  2185. used_rect_cache_dirty = false;
  2186. }
  2187. return used_rect_cache;
  2188. }
  2189. bool TileMapLayer::is_cell_flipped_h(const Vector2i &p_coords) const {
  2190. return get_cell_alternative_tile(p_coords) & TileSetAtlasSource::TRANSFORM_FLIP_H;
  2191. }
  2192. bool TileMapLayer::is_cell_flipped_v(const Vector2i &p_coords) const {
  2193. return get_cell_alternative_tile(p_coords) & TileSetAtlasSource::TRANSFORM_FLIP_V;
  2194. }
  2195. bool TileMapLayer::is_cell_transposed(const Vector2i &p_coords) const {
  2196. return get_cell_alternative_tile(p_coords) & TileSetAtlasSource::TRANSFORM_TRANSPOSE;
  2197. }
  2198. Ref<TileMapPattern> TileMapLayer::get_pattern(TypedArray<Vector2i> p_coords_array) {
  2199. ERR_FAIL_COND_V(tile_set.is_null(), nullptr);
  2200. Ref<TileMapPattern> output;
  2201. output.instantiate();
  2202. if (p_coords_array.is_empty()) {
  2203. return output;
  2204. }
  2205. Vector2i min = Vector2i(p_coords_array[0]);
  2206. for (int i = 1; i < p_coords_array.size(); i++) {
  2207. min = min.min(p_coords_array[i]);
  2208. }
  2209. Vector<Vector2i> coords_in_pattern_array;
  2210. coords_in_pattern_array.resize(p_coords_array.size());
  2211. Vector2i ensure_positive_offset;
  2212. for (int i = 0; i < p_coords_array.size(); i++) {
  2213. Vector2i coords = p_coords_array[i];
  2214. Vector2i coords_in_pattern = coords - min;
  2215. if (tile_set->get_tile_shape() != TileSet::TILE_SHAPE_SQUARE) {
  2216. if (tile_set->get_tile_layout() == TileSet::TILE_LAYOUT_STACKED) {
  2217. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL && bool(min.y % 2) && bool(coords_in_pattern.y % 2)) {
  2218. coords_in_pattern.x -= 1;
  2219. if (coords_in_pattern.x < 0) {
  2220. ensure_positive_offset.x = 1;
  2221. }
  2222. } else if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_VERTICAL && bool(min.x % 2) && bool(coords_in_pattern.x % 2)) {
  2223. coords_in_pattern.y -= 1;
  2224. if (coords_in_pattern.y < 0) {
  2225. ensure_positive_offset.y = 1;
  2226. }
  2227. }
  2228. } else if (tile_set->get_tile_layout() == TileSet::TILE_LAYOUT_STACKED_OFFSET) {
  2229. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL && bool(min.y % 2) && bool(coords_in_pattern.y % 2)) {
  2230. coords_in_pattern.x += 1;
  2231. } else if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_VERTICAL && bool(min.x % 2) && bool(coords_in_pattern.x % 2)) {
  2232. coords_in_pattern.y += 1;
  2233. }
  2234. }
  2235. }
  2236. coords_in_pattern_array.write[i] = coords_in_pattern;
  2237. }
  2238. for (int i = 0; i < coords_in_pattern_array.size(); i++) {
  2239. Vector2i coords = p_coords_array[i];
  2240. Vector2i coords_in_pattern = coords_in_pattern_array[i];
  2241. output->set_cell(coords_in_pattern + ensure_positive_offset, get_cell_source_id(coords), get_cell_atlas_coords(coords), get_cell_alternative_tile(coords));
  2242. }
  2243. return output;
  2244. }
  2245. void TileMapLayer::set_pattern(const Vector2i &p_position, const Ref<TileMapPattern> p_pattern) {
  2246. ERR_FAIL_COND(tile_set.is_null());
  2247. ERR_FAIL_COND(p_pattern.is_null());
  2248. TypedArray<Vector2i> used_cells = p_pattern->get_used_cells();
  2249. for (int i = 0; i < used_cells.size(); i++) {
  2250. Vector2i coords = tile_set->map_pattern(p_position, used_cells[i], p_pattern);
  2251. 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]));
  2252. }
  2253. }
  2254. void TileMapLayer::set_cells_terrain_connect(TypedArray<Vector2i> p_cells, int p_terrain_set, int p_terrain, bool p_ignore_empty_terrains) {
  2255. ERR_FAIL_COND(tile_set.is_null());
  2256. ERR_FAIL_INDEX(p_terrain_set, tile_set->get_terrain_sets_count());
  2257. Vector<Vector2i> cells_vector;
  2258. HashSet<Vector2i> painted_set;
  2259. for (int i = 0; i < p_cells.size(); i++) {
  2260. cells_vector.push_back(p_cells[i]);
  2261. painted_set.insert(p_cells[i]);
  2262. }
  2263. HashMap<Vector2i, TileSet::TerrainsPattern> terrain_fill_output = terrain_fill_connect(cells_vector, p_terrain_set, p_terrain, p_ignore_empty_terrains);
  2264. for (const KeyValue<Vector2i, TileSet::TerrainsPattern> &kv : terrain_fill_output) {
  2265. if (painted_set.has(kv.key)) {
  2266. // Paint a random tile with the correct terrain for the painted path.
  2267. TileMapCell c = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value);
  2268. set_cell(kv.key, c.source_id, c.get_atlas_coords(), c.alternative_tile);
  2269. } else {
  2270. // Avoids updating the painted path from the output if the new pattern is the same as before.
  2271. TileSet::TerrainsPattern in_map_terrain_pattern = TileSet::TerrainsPattern(*tile_set, p_terrain_set);
  2272. TileMapCell cell = get_cell(kv.key);
  2273. if (cell.source_id != TileSet::INVALID_SOURCE) {
  2274. TileSetSource *source = *tile_set->get_source(cell.source_id);
  2275. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  2276. if (atlas_source) {
  2277. // Get tile data.
  2278. TileData *tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  2279. if (tile_data && tile_data->get_terrain_set() == p_terrain_set) {
  2280. in_map_terrain_pattern = tile_data->get_terrains_pattern();
  2281. }
  2282. }
  2283. }
  2284. if (in_map_terrain_pattern != kv.value) {
  2285. TileMapCell c = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value);
  2286. set_cell(kv.key, c.source_id, c.get_atlas_coords(), c.alternative_tile);
  2287. }
  2288. }
  2289. }
  2290. }
  2291. void TileMapLayer::set_cells_terrain_path(TypedArray<Vector2i> p_path, int p_terrain_set, int p_terrain, bool p_ignore_empty_terrains) {
  2292. ERR_FAIL_COND(tile_set.is_null());
  2293. ERR_FAIL_INDEX(p_terrain_set, tile_set->get_terrain_sets_count());
  2294. Vector<Vector2i> vector_path;
  2295. HashSet<Vector2i> painted_set;
  2296. for (int i = 0; i < p_path.size(); i++) {
  2297. vector_path.push_back(p_path[i]);
  2298. painted_set.insert(p_path[i]);
  2299. }
  2300. HashMap<Vector2i, TileSet::TerrainsPattern> terrain_fill_output = terrain_fill_path(vector_path, p_terrain_set, p_terrain, p_ignore_empty_terrains);
  2301. for (const KeyValue<Vector2i, TileSet::TerrainsPattern> &kv : terrain_fill_output) {
  2302. if (painted_set.has(kv.key)) {
  2303. // Paint a random tile with the correct terrain for the painted path.
  2304. TileMapCell c = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value);
  2305. set_cell(kv.key, c.source_id, c.get_atlas_coords(), c.alternative_tile);
  2306. } else {
  2307. // Avoids updating the painted path from the output if the new pattern is the same as before.
  2308. TileSet::TerrainsPattern in_map_terrain_pattern = TileSet::TerrainsPattern(*tile_set, p_terrain_set);
  2309. TileMapCell cell = get_cell(kv.key);
  2310. if (cell.source_id != TileSet::INVALID_SOURCE) {
  2311. TileSetSource *source = *tile_set->get_source(cell.source_id);
  2312. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  2313. if (atlas_source) {
  2314. // Get tile data.
  2315. TileData *tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  2316. if (tile_data && tile_data->get_terrain_set() == p_terrain_set) {
  2317. in_map_terrain_pattern = tile_data->get_terrains_pattern();
  2318. }
  2319. }
  2320. }
  2321. if (in_map_terrain_pattern != kv.value) {
  2322. TileMapCell c = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value);
  2323. set_cell(kv.key, c.source_id, c.get_atlas_coords(), c.alternative_tile);
  2324. }
  2325. }
  2326. }
  2327. }
  2328. bool TileMapLayer::has_body_rid(RID p_physics_body) const {
  2329. return bodies_coords.has(p_physics_body);
  2330. }
  2331. Vector2i TileMapLayer::get_coords_for_body_rid(RID p_physics_body) const {
  2332. const Vector2i *found = bodies_coords.getptr(p_physics_body);
  2333. ERR_FAIL_NULL_V(found, Vector2i());
  2334. return *found;
  2335. }
  2336. void TileMapLayer::update_internals() {
  2337. _internal_update(false);
  2338. }
  2339. void TileMapLayer::notify_runtime_tile_data_update() {
  2340. dirty.flags[TileMapLayer::DIRTY_FLAGS_LAYER_RUNTIME_UPDATE] = true;
  2341. _queue_internal_update();
  2342. emit_signal(CoreStringName(changed));
  2343. }
  2344. Vector2i TileMapLayer::map_pattern(const Vector2i &p_position_in_tilemap, const Vector2i &p_coords_in_pattern, Ref<TileMapPattern> p_pattern) {
  2345. ERR_FAIL_COND_V(tile_set.is_null(), Vector2i());
  2346. return tile_set->map_pattern(p_position_in_tilemap, p_coords_in_pattern, p_pattern);
  2347. }
  2348. TypedArray<Vector2i> TileMapLayer::get_surrounding_cells(const Vector2i &p_coords) {
  2349. ERR_FAIL_COND_V(tile_set.is_null(), TypedArray<Vector2i>());
  2350. return tile_set->get_surrounding_cells(p_coords);
  2351. }
  2352. Vector2i TileMapLayer::get_neighbor_cell(const Vector2i &p_coords, TileSet::CellNeighbor p_cell_neighbor) const {
  2353. ERR_FAIL_COND_V(tile_set.is_null(), Vector2i());
  2354. return tile_set->get_neighbor_cell(p_coords, p_cell_neighbor);
  2355. }
  2356. Vector2 TileMapLayer::map_to_local(const Vector2i &p_pos) const {
  2357. ERR_FAIL_COND_V(tile_set.is_null(), Vector2());
  2358. return tile_set->map_to_local(p_pos);
  2359. }
  2360. Vector2i TileMapLayer::local_to_map(const Vector2 &p_pos) const {
  2361. ERR_FAIL_COND_V(tile_set.is_null(), Vector2i());
  2362. return tile_set->local_to_map(p_pos);
  2363. }
  2364. void TileMapLayer::set_enabled(bool p_enabled) {
  2365. if (enabled == p_enabled) {
  2366. return;
  2367. }
  2368. enabled = p_enabled;
  2369. dirty.flags[DIRTY_FLAGS_LAYER_ENABLED] = true;
  2370. _queue_internal_update();
  2371. emit_signal(CoreStringName(changed));
  2372. }
  2373. bool TileMapLayer::is_enabled() const {
  2374. return enabled;
  2375. }
  2376. void TileMapLayer::set_tile_set(const Ref<TileSet> &p_tile_set) {
  2377. if (p_tile_set == tile_set) {
  2378. return;
  2379. }
  2380. dirty.flags[DIRTY_FLAGS_TILE_SET] = true;
  2381. _queue_internal_update();
  2382. // Set the TileSet, registering to its changes.
  2383. if (tile_set.is_valid()) {
  2384. tile_set->disconnect_changed(callable_mp(this, &TileMapLayer::_tile_set_changed));
  2385. }
  2386. tile_set = p_tile_set;
  2387. if (tile_set.is_valid()) {
  2388. tile_set->connect_changed(callable_mp(this, &TileMapLayer::_tile_set_changed));
  2389. }
  2390. emit_signal(CoreStringName(changed));
  2391. // Trigger updates for TileSet's read-only status.
  2392. notify_property_list_changed();
  2393. }
  2394. Ref<TileSet> TileMapLayer::get_tile_set() const {
  2395. return tile_set;
  2396. }
  2397. void TileMapLayer::set_highlight_mode(HighlightMode p_highlight_mode) {
  2398. if (p_highlight_mode == highlight_mode) {
  2399. return;
  2400. }
  2401. highlight_mode = p_highlight_mode;
  2402. _queue_internal_update();
  2403. }
  2404. TileMapLayer::HighlightMode TileMapLayer::get_highlight_mode() const {
  2405. return highlight_mode;
  2406. }
  2407. void TileMapLayer::set_tile_map_data_from_array(const Vector<uint8_t> &p_data) {
  2408. if (p_data.is_empty()) {
  2409. clear();
  2410. return;
  2411. }
  2412. const int cell_data_struct_size = 12;
  2413. int size = p_data.size();
  2414. const uint8_t *ptr = p_data.ptr();
  2415. // Index in the array.
  2416. int index = 0;
  2417. // First extract the data version.
  2418. ERR_FAIL_COND_MSG(size < 2, "Corrupted tile map data: not enough bytes.");
  2419. uint16_t format = decode_uint16(&ptr[index]);
  2420. index += 2;
  2421. ERR_FAIL_COND_MSG(format >= TileMapLayerDataFormat::TILE_MAP_LAYER_DATA_FORMAT_MAX, vformat("Unsupported tile map data format: %s. Expected format ID lower or equal to: %s", format, TileMapLayerDataFormat::TILE_MAP_LAYER_DATA_FORMAT_MAX - 1));
  2422. // Clear the TileMap.
  2423. clear();
  2424. while (index < size) {
  2425. ERR_FAIL_COND_MSG(index + cell_data_struct_size > size, vformat("Corrupted tile map data: tiles might be missing."));
  2426. // Get a pointer at the start of the cell data.
  2427. const uint8_t *cell_data_ptr = &ptr[index];
  2428. // Extracts position in TileMap.
  2429. int16_t x = decode_uint16(&cell_data_ptr[0]);
  2430. int16_t y = decode_uint16(&cell_data_ptr[2]);
  2431. // Extracts the tile identifiers.
  2432. uint16_t source_id = decode_uint16(&cell_data_ptr[4]);
  2433. uint16_t atlas_coords_x = decode_uint16(&cell_data_ptr[6]);
  2434. uint16_t atlas_coords_y = decode_uint16(&cell_data_ptr[8]);
  2435. uint16_t alternative_tile = decode_uint16(&cell_data_ptr[10]);
  2436. set_cell(Vector2i(x, y), source_id, Vector2i(atlas_coords_x, atlas_coords_y), alternative_tile);
  2437. index += cell_data_struct_size;
  2438. }
  2439. }
  2440. Vector<uint8_t> TileMapLayer::get_tile_map_data_as_array() const {
  2441. const int cell_data_struct_size = 12;
  2442. Vector<uint8_t> tile_map_data_array;
  2443. if (tile_map_layer_data.is_empty()) {
  2444. return tile_map_data_array;
  2445. }
  2446. tile_map_data_array.resize(2 + tile_map_layer_data.size() * cell_data_struct_size);
  2447. uint8_t *ptr = tile_map_data_array.ptrw();
  2448. // Index in the array.
  2449. int index = 0;
  2450. // Save the version.
  2451. encode_uint16(TileMapLayerDataFormat::TILE_MAP_LAYER_DATA_FORMAT_MAX - 1, &ptr[index]);
  2452. index += 2;
  2453. // Save in highest format.
  2454. for (const KeyValue<Vector2i, CellData> &E : tile_map_layer_data) {
  2455. // Get a pointer at the start of the cell data.
  2456. uint8_t *cell_data_ptr = (uint8_t *)&ptr[index];
  2457. // Store position in TileMap.
  2458. encode_uint16((int16_t)(E.key.x), &cell_data_ptr[0]);
  2459. encode_uint16((int16_t)(E.key.y), &cell_data_ptr[2]);
  2460. // Store the tile identifiers.
  2461. encode_uint16(E.value.cell.source_id, &cell_data_ptr[4]);
  2462. encode_uint16(E.value.cell.coord_x, &cell_data_ptr[6]);
  2463. encode_uint16(E.value.cell.coord_y, &cell_data_ptr[8]);
  2464. encode_uint16(E.value.cell.alternative_tile, &cell_data_ptr[10]);
  2465. index += cell_data_struct_size;
  2466. }
  2467. return tile_map_data_array;
  2468. }
  2469. void TileMapLayer::set_self_modulate(const Color &p_self_modulate) {
  2470. if (get_self_modulate() == p_self_modulate) {
  2471. return;
  2472. }
  2473. CanvasItem::set_self_modulate(p_self_modulate);
  2474. dirty.flags[DIRTY_FLAGS_LAYER_SELF_MODULATE] = true;
  2475. _queue_internal_update();
  2476. emit_signal(CoreStringName(changed));
  2477. }
  2478. void TileMapLayer::set_y_sort_enabled(bool p_y_sort_enabled) {
  2479. if (is_y_sort_enabled() == p_y_sort_enabled) {
  2480. return;
  2481. }
  2482. CanvasItem::set_y_sort_enabled(p_y_sort_enabled);
  2483. dirty.flags[DIRTY_FLAGS_LAYER_Y_SORT_ENABLED] = true;
  2484. _queue_internal_update();
  2485. emit_signal(CoreStringName(changed));
  2486. notify_property_list_changed();
  2487. _update_notify_local_transform();
  2488. }
  2489. void TileMapLayer::set_y_sort_origin(int p_y_sort_origin) {
  2490. if (y_sort_origin == p_y_sort_origin) {
  2491. return;
  2492. }
  2493. y_sort_origin = p_y_sort_origin;
  2494. dirty.flags[DIRTY_FLAGS_LAYER_Y_SORT_ORIGIN] = true;
  2495. _queue_internal_update();
  2496. emit_signal(CoreStringName(changed));
  2497. }
  2498. int TileMapLayer::get_y_sort_origin() const {
  2499. return y_sort_origin;
  2500. }
  2501. void TileMapLayer::set_x_draw_order_reversed(bool p_x_draw_order_reversed) {
  2502. if (x_draw_order_reversed == p_x_draw_order_reversed) {
  2503. return;
  2504. }
  2505. x_draw_order_reversed = p_x_draw_order_reversed;
  2506. dirty.flags[DIRTY_FLAGS_LAYER_X_DRAW_ORDER_REVERSED] = true;
  2507. _queue_internal_update();
  2508. emit_signal(CoreStringName(changed));
  2509. }
  2510. bool TileMapLayer::is_x_draw_order_reversed() const {
  2511. return x_draw_order_reversed;
  2512. }
  2513. void TileMapLayer::set_z_index(int p_z_index) {
  2514. if (get_z_index() == p_z_index) {
  2515. return;
  2516. }
  2517. CanvasItem::set_z_index(p_z_index);
  2518. dirty.flags[DIRTY_FLAGS_LAYER_Z_INDEX] = true;
  2519. _queue_internal_update();
  2520. emit_signal(CoreStringName(changed));
  2521. }
  2522. void TileMapLayer::set_light_mask(int p_light_mask) {
  2523. if (get_light_mask() == p_light_mask) {
  2524. return;
  2525. }
  2526. CanvasItem::set_light_mask(p_light_mask);
  2527. dirty.flags[DIRTY_FLAGS_LAYER_LIGHT_MASK] = true;
  2528. _queue_internal_update();
  2529. emit_signal(CoreStringName(changed));
  2530. }
  2531. void TileMapLayer::set_rendering_quadrant_size(int p_size) {
  2532. if (rendering_quadrant_size == p_size) {
  2533. return;
  2534. }
  2535. ERR_FAIL_COND_MSG(p_size < 1, "TileMapQuadrant size cannot be smaller than 1.");
  2536. rendering_quadrant_size = p_size;
  2537. dirty.flags[DIRTY_FLAGS_LAYER_RENDERING_QUADRANT_SIZE] = true;
  2538. _queue_internal_update();
  2539. emit_signal(CoreStringName(changed));
  2540. }
  2541. int TileMapLayer::get_rendering_quadrant_size() const {
  2542. return rendering_quadrant_size;
  2543. }
  2544. void TileMapLayer::set_collision_enabled(bool p_enabled) {
  2545. if (collision_enabled == p_enabled) {
  2546. return;
  2547. }
  2548. collision_enabled = p_enabled;
  2549. dirty.flags[DIRTY_FLAGS_LAYER_COLLISION_ENABLED] = true;
  2550. _queue_internal_update();
  2551. emit_signal(CoreStringName(changed));
  2552. }
  2553. bool TileMapLayer::is_collision_enabled() const {
  2554. return collision_enabled;
  2555. }
  2556. void TileMapLayer::set_use_kinematic_bodies(bool p_use_kinematic_bodies) {
  2557. if (use_kinematic_bodies == p_use_kinematic_bodies) {
  2558. return;
  2559. }
  2560. use_kinematic_bodies = p_use_kinematic_bodies;
  2561. dirty.flags[DIRTY_FLAGS_LAYER_USE_KINEMATIC_BODIES] = p_use_kinematic_bodies;
  2562. _queue_internal_update();
  2563. emit_signal(CoreStringName(changed));
  2564. }
  2565. bool TileMapLayer::is_using_kinematic_bodies() const {
  2566. return use_kinematic_bodies;
  2567. }
  2568. void TileMapLayer::set_collision_visibility_mode(TileMapLayer::DebugVisibilityMode p_show_collision) {
  2569. if (collision_visibility_mode == p_show_collision) {
  2570. return;
  2571. }
  2572. collision_visibility_mode = p_show_collision;
  2573. dirty.flags[DIRTY_FLAGS_LAYER_COLLISION_VISIBILITY_MODE] = true;
  2574. _queue_internal_update();
  2575. emit_signal(CoreStringName(changed));
  2576. }
  2577. TileMapLayer::DebugVisibilityMode TileMapLayer::get_collision_visibility_mode() const {
  2578. return collision_visibility_mode;
  2579. }
  2580. void TileMapLayer::set_occlusion_enabled(bool p_enabled) {
  2581. if (occlusion_enabled == p_enabled) {
  2582. return;
  2583. }
  2584. occlusion_enabled = p_enabled;
  2585. dirty.flags[DIRTY_FLAGS_LAYER_OCCLUSION_ENABLED] = true;
  2586. _queue_internal_update();
  2587. emit_signal(CoreStringName(changed));
  2588. }
  2589. bool TileMapLayer::is_occlusion_enabled() const {
  2590. return occlusion_enabled;
  2591. }
  2592. void TileMapLayer::set_navigation_enabled(bool p_enabled) {
  2593. if (navigation_enabled == p_enabled) {
  2594. return;
  2595. }
  2596. navigation_enabled = p_enabled;
  2597. dirty.flags[DIRTY_FLAGS_LAYER_NAVIGATION_ENABLED] = true;
  2598. _queue_internal_update();
  2599. emit_signal(CoreStringName(changed));
  2600. }
  2601. bool TileMapLayer::is_navigation_enabled() const {
  2602. return navigation_enabled;
  2603. }
  2604. void TileMapLayer::set_navigation_map(RID p_map) {
  2605. if (navigation_map_override == p_map) {
  2606. return;
  2607. }
  2608. navigation_map_override = p_map;
  2609. dirty.flags[DIRTY_FLAGS_LAYER_NAVIGATION_MAP] = true;
  2610. _queue_internal_update();
  2611. emit_signal(CoreStringName(changed));
  2612. }
  2613. RID TileMapLayer::get_navigation_map() const {
  2614. if (navigation_map_override.is_valid()) {
  2615. return navigation_map_override;
  2616. } else if (is_inside_tree()) {
  2617. return get_world_2d()->get_navigation_map();
  2618. }
  2619. return RID();
  2620. }
  2621. void TileMapLayer::set_navigation_visibility_mode(TileMapLayer::DebugVisibilityMode p_show_navigation) {
  2622. if (navigation_visibility_mode == p_show_navigation) {
  2623. return;
  2624. }
  2625. navigation_visibility_mode = p_show_navigation;
  2626. dirty.flags[DIRTY_FLAGS_LAYER_NAVIGATION_VISIBILITY_MODE] = true;
  2627. _queue_internal_update();
  2628. emit_signal(CoreStringName(changed));
  2629. }
  2630. TileMapLayer::DebugVisibilityMode TileMapLayer::get_navigation_visibility_mode() const {
  2631. return navigation_visibility_mode;
  2632. }
  2633. TileMapLayer::TileMapLayer() {
  2634. set_notify_transform(true);
  2635. }
  2636. TileMapLayer::~TileMapLayer() {
  2637. clear();
  2638. _internal_update(true);
  2639. }
  2640. HashMap<Vector2i, TileSet::CellNeighbor> TerrainConstraint::get_overlapping_coords_and_peering_bits() const {
  2641. HashMap<Vector2i, TileSet::CellNeighbor> output;
  2642. ERR_FAIL_COND_V(is_center_bit(), output);
  2643. ERR_FAIL_COND_V(!tile_set.is_valid(), output);
  2644. TileSet::TileShape shape = tile_set->get_tile_shape();
  2645. if (shape == TileSet::TILE_SHAPE_SQUARE) {
  2646. switch (bit) {
  2647. case 1:
  2648. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_RIGHT_SIDE;
  2649. output[tile_set->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_LEFT_SIDE;
  2650. break;
  2651. case 2:
  2652. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER;
  2653. output[tile_set->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER;
  2654. output[tile_set->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER;
  2655. output[tile_set->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER;
  2656. break;
  2657. case 3:
  2658. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_SIDE;
  2659. output[tile_set->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_SIDE;
  2660. break;
  2661. default:
  2662. ERR_FAIL_V(output);
  2663. }
  2664. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  2665. switch (bit) {
  2666. case 1:
  2667. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE;
  2668. output[tile_set->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE;
  2669. break;
  2670. case 2:
  2671. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_CORNER;
  2672. output[tile_set->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_LEFT_CORNER;
  2673. output[tile_set->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_CORNER)] = TileSet::CELL_NEIGHBOR_TOP_CORNER;
  2674. output[tile_set->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE)] = TileSet::CELL_NEIGHBOR_RIGHT_CORNER;
  2675. break;
  2676. case 3:
  2677. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE;
  2678. output[tile_set->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE;
  2679. break;
  2680. default:
  2681. ERR_FAIL_V(output);
  2682. }
  2683. } else {
  2684. // Half offset shapes.
  2685. TileSet::TileOffsetAxis offset_axis = tile_set->get_tile_offset_axis();
  2686. if (offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  2687. switch (bit) {
  2688. case 1:
  2689. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_RIGHT_SIDE;
  2690. output[tile_set->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_LEFT_SIDE;
  2691. break;
  2692. case 2:
  2693. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER;
  2694. output[tile_set->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER;
  2695. output[tile_set->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_CORNER;
  2696. break;
  2697. case 3:
  2698. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE;
  2699. output[tile_set->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE;
  2700. break;
  2701. case 4:
  2702. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_CORNER;
  2703. output[tile_set->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER;
  2704. output[tile_set->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER;
  2705. break;
  2706. case 5:
  2707. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE;
  2708. output[tile_set->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE;
  2709. break;
  2710. default:
  2711. ERR_FAIL_V(output);
  2712. }
  2713. } else {
  2714. switch (bit) {
  2715. case 1:
  2716. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_RIGHT_CORNER;
  2717. output[tile_set->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER;
  2718. output[tile_set->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER;
  2719. break;
  2720. case 2:
  2721. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE;
  2722. output[tile_set->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE;
  2723. break;
  2724. case 3:
  2725. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER;
  2726. output[tile_set->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_LEFT_CORNER;
  2727. output[tile_set->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER;
  2728. break;
  2729. case 4:
  2730. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_SIDE;
  2731. output[tile_set->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_SIDE;
  2732. break;
  2733. case 5:
  2734. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE;
  2735. output[tile_set->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE;
  2736. break;
  2737. default:
  2738. ERR_FAIL_V(output);
  2739. }
  2740. }
  2741. }
  2742. return output;
  2743. }
  2744. TerrainConstraint::TerrainConstraint(Ref<TileSet> p_tile_set, const Vector2i &p_position, int p_terrain) {
  2745. ERR_FAIL_COND(!p_tile_set.is_valid());
  2746. tile_set = p_tile_set;
  2747. bit = 0;
  2748. base_cell_coords = p_position;
  2749. terrain = p_terrain;
  2750. }
  2751. TerrainConstraint::TerrainConstraint(Ref<TileSet> p_tile_set, const Vector2i &p_position, const TileSet::CellNeighbor &p_bit, int p_terrain) {
  2752. // The way we build the constraint make it easy to detect conflicting constraints.
  2753. ERR_FAIL_COND(!p_tile_set.is_valid());
  2754. tile_set = p_tile_set;
  2755. TileSet::TileShape shape = tile_set->get_tile_shape();
  2756. if (shape == TileSet::TILE_SHAPE_SQUARE) {
  2757. switch (p_bit) {
  2758. case TileSet::CELL_NEIGHBOR_RIGHT_SIDE:
  2759. bit = 1;
  2760. base_cell_coords = p_position;
  2761. break;
  2762. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  2763. bit = 2;
  2764. base_cell_coords = p_position;
  2765. break;
  2766. case TileSet::CELL_NEIGHBOR_BOTTOM_SIDE:
  2767. bit = 3;
  2768. base_cell_coords = p_position;
  2769. break;
  2770. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  2771. bit = 2;
  2772. base_cell_coords = tile_set->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_LEFT_SIDE);
  2773. break;
  2774. case TileSet::CELL_NEIGHBOR_LEFT_SIDE:
  2775. bit = 1;
  2776. base_cell_coords = tile_set->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_LEFT_SIDE);
  2777. break;
  2778. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  2779. bit = 2;
  2780. base_cell_coords = tile_set->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER);
  2781. break;
  2782. case TileSet::CELL_NEIGHBOR_TOP_SIDE:
  2783. bit = 3;
  2784. base_cell_coords = tile_set->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_SIDE);
  2785. break;
  2786. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  2787. bit = 2;
  2788. base_cell_coords = tile_set->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_SIDE);
  2789. break;
  2790. default:
  2791. ERR_FAIL();
  2792. break;
  2793. }
  2794. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  2795. switch (p_bit) {
  2796. case TileSet::CELL_NEIGHBOR_RIGHT_CORNER:
  2797. bit = 2;
  2798. base_cell_coords = tile_set->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE);
  2799. break;
  2800. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  2801. bit = 1;
  2802. base_cell_coords = p_position;
  2803. break;
  2804. case TileSet::CELL_NEIGHBOR_BOTTOM_CORNER:
  2805. bit = 2;
  2806. base_cell_coords = p_position;
  2807. break;
  2808. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  2809. bit = 3;
  2810. base_cell_coords = p_position;
  2811. break;
  2812. case TileSet::CELL_NEIGHBOR_LEFT_CORNER:
  2813. bit = 2;
  2814. base_cell_coords = tile_set->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  2815. break;
  2816. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  2817. bit = 1;
  2818. base_cell_coords = tile_set->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  2819. break;
  2820. case TileSet::CELL_NEIGHBOR_TOP_CORNER:
  2821. bit = 2;
  2822. base_cell_coords = tile_set->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_CORNER);
  2823. break;
  2824. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  2825. bit = 3;
  2826. base_cell_coords = tile_set->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE);
  2827. break;
  2828. default:
  2829. ERR_FAIL();
  2830. break;
  2831. }
  2832. } else {
  2833. // Half-offset shapes.
  2834. TileSet::TileOffsetAxis offset_axis = tile_set->get_tile_offset_axis();
  2835. if (offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  2836. switch (p_bit) {
  2837. case TileSet::CELL_NEIGHBOR_RIGHT_SIDE:
  2838. bit = 1;
  2839. base_cell_coords = p_position;
  2840. break;
  2841. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  2842. bit = 2;
  2843. base_cell_coords = p_position;
  2844. break;
  2845. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  2846. bit = 3;
  2847. base_cell_coords = p_position;
  2848. break;
  2849. case TileSet::CELL_NEIGHBOR_BOTTOM_CORNER:
  2850. bit = 4;
  2851. base_cell_coords = p_position;
  2852. break;
  2853. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  2854. bit = 5;
  2855. base_cell_coords = p_position;
  2856. break;
  2857. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  2858. bit = 2;
  2859. base_cell_coords = tile_set->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_LEFT_SIDE);
  2860. break;
  2861. case TileSet::CELL_NEIGHBOR_LEFT_SIDE:
  2862. bit = 1;
  2863. base_cell_coords = tile_set->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_LEFT_SIDE);
  2864. break;
  2865. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  2866. bit = 4;
  2867. base_cell_coords = tile_set->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  2868. break;
  2869. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  2870. bit = 3;
  2871. base_cell_coords = tile_set->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  2872. break;
  2873. case TileSet::CELL_NEIGHBOR_TOP_CORNER:
  2874. bit = 2;
  2875. base_cell_coords = tile_set->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  2876. break;
  2877. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  2878. bit = 5;
  2879. base_cell_coords = tile_set->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE);
  2880. break;
  2881. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  2882. bit = 4;
  2883. base_cell_coords = tile_set->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE);
  2884. break;
  2885. default:
  2886. ERR_FAIL();
  2887. break;
  2888. }
  2889. } else {
  2890. switch (p_bit) {
  2891. case TileSet::CELL_NEIGHBOR_RIGHT_CORNER:
  2892. bit = 1;
  2893. base_cell_coords = p_position;
  2894. break;
  2895. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  2896. bit = 2;
  2897. base_cell_coords = p_position;
  2898. break;
  2899. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  2900. bit = 3;
  2901. base_cell_coords = p_position;
  2902. break;
  2903. case TileSet::CELL_NEIGHBOR_BOTTOM_SIDE:
  2904. bit = 4;
  2905. base_cell_coords = p_position;
  2906. break;
  2907. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  2908. bit = 1;
  2909. base_cell_coords = tile_set->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE);
  2910. break;
  2911. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  2912. bit = 5;
  2913. base_cell_coords = p_position;
  2914. break;
  2915. case TileSet::CELL_NEIGHBOR_LEFT_CORNER:
  2916. bit = 3;
  2917. base_cell_coords = tile_set->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  2918. break;
  2919. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  2920. bit = 2;
  2921. base_cell_coords = tile_set->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  2922. break;
  2923. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  2924. bit = 1;
  2925. base_cell_coords = tile_set->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  2926. break;
  2927. case TileSet::CELL_NEIGHBOR_TOP_SIDE:
  2928. bit = 4;
  2929. base_cell_coords = tile_set->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_SIDE);
  2930. break;
  2931. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  2932. bit = 3;
  2933. base_cell_coords = tile_set->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_SIDE);
  2934. break;
  2935. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  2936. bit = 5;
  2937. base_cell_coords = tile_set->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE);
  2938. break;
  2939. default:
  2940. ERR_FAIL();
  2941. break;
  2942. }
  2943. }
  2944. }
  2945. terrain = p_terrain;
  2946. }