tile_map.cpp 163 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199
  1. /**************************************************************************/
  2. /* tile_map.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.h"
  31. #include "core/io/marshalls.h"
  32. #include "scene/resources/world_2d.h"
  33. #include "servers/navigation_server_2d.h"
  34. #ifdef DEBUG_ENABLED
  35. #include "servers/navigation_server_3d.h"
  36. #endif // DEBUG_ENABLED
  37. HashMap<Vector2i, TileSet::CellNeighbor> TileMap::TerrainConstraint::get_overlapping_coords_and_peering_bits() const {
  38. HashMap<Vector2i, TileSet::CellNeighbor> output;
  39. ERR_FAIL_COND_V(is_center_bit(), output);
  40. Ref<TileSet> ts = tile_map->get_tileset();
  41. ERR_FAIL_COND_V(!ts.is_valid(), output);
  42. TileSet::TileShape shape = ts->get_tile_shape();
  43. if (shape == TileSet::TILE_SHAPE_SQUARE) {
  44. switch (bit) {
  45. case 1:
  46. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_RIGHT_SIDE;
  47. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_LEFT_SIDE;
  48. break;
  49. case 2:
  50. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER;
  51. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER;
  52. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER;
  53. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER;
  54. break;
  55. case 3:
  56. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_SIDE;
  57. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_SIDE;
  58. break;
  59. default:
  60. ERR_FAIL_V(output);
  61. }
  62. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  63. switch (bit) {
  64. case 1:
  65. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE;
  66. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE;
  67. break;
  68. case 2:
  69. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_CORNER;
  70. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_LEFT_CORNER;
  71. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_CORNER)] = TileSet::CELL_NEIGHBOR_TOP_CORNER;
  72. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE)] = TileSet::CELL_NEIGHBOR_RIGHT_CORNER;
  73. break;
  74. case 3:
  75. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE;
  76. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE;
  77. break;
  78. default:
  79. ERR_FAIL_V(output);
  80. }
  81. } else {
  82. // Half offset shapes.
  83. TileSet::TileOffsetAxis offset_axis = ts->get_tile_offset_axis();
  84. if (offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  85. switch (bit) {
  86. case 1:
  87. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_RIGHT_SIDE;
  88. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_LEFT_SIDE;
  89. break;
  90. case 2:
  91. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER;
  92. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER;
  93. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_CORNER;
  94. break;
  95. case 3:
  96. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE;
  97. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE;
  98. break;
  99. case 4:
  100. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_CORNER;
  101. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER;
  102. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER;
  103. break;
  104. case 5:
  105. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE;
  106. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE;
  107. break;
  108. default:
  109. ERR_FAIL_V(output);
  110. }
  111. } else {
  112. switch (bit) {
  113. case 1:
  114. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_RIGHT_CORNER;
  115. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER;
  116. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER;
  117. break;
  118. case 2:
  119. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE;
  120. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE;
  121. break;
  122. case 3:
  123. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER;
  124. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE)] = TileSet::CELL_NEIGHBOR_LEFT_CORNER;
  125. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER;
  126. break;
  127. case 4:
  128. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_SIDE;
  129. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_SIDE;
  130. break;
  131. case 5:
  132. output[base_cell_coords] = TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE;
  133. output[tile_map->get_neighbor_cell(base_cell_coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE)] = TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE;
  134. break;
  135. default:
  136. ERR_FAIL_V(output);
  137. }
  138. }
  139. }
  140. return output;
  141. }
  142. TileMap::TerrainConstraint::TerrainConstraint(const TileMap *p_tile_map, const Vector2i &p_position, int p_terrain) {
  143. tile_map = p_tile_map;
  144. Ref<TileSet> ts = tile_map->get_tileset();
  145. ERR_FAIL_COND(!ts.is_valid());
  146. bit = 0;
  147. base_cell_coords = p_position;
  148. terrain = p_terrain;
  149. }
  150. TileMap::TerrainConstraint::TerrainConstraint(const TileMap *p_tile_map, const Vector2i &p_position, const TileSet::CellNeighbor &p_bit, int p_terrain) {
  151. // The way we build the constraint make it easy to detect conflicting constraints.
  152. tile_map = p_tile_map;
  153. Ref<TileSet> ts = tile_map->get_tileset();
  154. ERR_FAIL_COND(!ts.is_valid());
  155. TileSet::TileShape shape = ts->get_tile_shape();
  156. if (shape == TileSet::TILE_SHAPE_SQUARE) {
  157. switch (p_bit) {
  158. case TileSet::CELL_NEIGHBOR_RIGHT_SIDE:
  159. bit = 1;
  160. base_cell_coords = p_position;
  161. break;
  162. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  163. bit = 2;
  164. base_cell_coords = p_position;
  165. break;
  166. case TileSet::CELL_NEIGHBOR_BOTTOM_SIDE:
  167. bit = 3;
  168. base_cell_coords = p_position;
  169. break;
  170. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  171. bit = 2;
  172. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_LEFT_SIDE);
  173. break;
  174. case TileSet::CELL_NEIGHBOR_LEFT_SIDE:
  175. bit = 1;
  176. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_LEFT_SIDE);
  177. break;
  178. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  179. bit = 2;
  180. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER);
  181. break;
  182. case TileSet::CELL_NEIGHBOR_TOP_SIDE:
  183. bit = 3;
  184. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_SIDE);
  185. break;
  186. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  187. bit = 2;
  188. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_SIDE);
  189. break;
  190. default:
  191. ERR_FAIL();
  192. break;
  193. }
  194. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  195. switch (p_bit) {
  196. case TileSet::CELL_NEIGHBOR_RIGHT_CORNER:
  197. bit = 2;
  198. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE);
  199. break;
  200. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  201. bit = 1;
  202. base_cell_coords = p_position;
  203. break;
  204. case TileSet::CELL_NEIGHBOR_BOTTOM_CORNER:
  205. bit = 2;
  206. base_cell_coords = p_position;
  207. break;
  208. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  209. bit = 3;
  210. base_cell_coords = p_position;
  211. break;
  212. case TileSet::CELL_NEIGHBOR_LEFT_CORNER:
  213. bit = 2;
  214. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  215. break;
  216. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  217. bit = 1;
  218. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  219. break;
  220. case TileSet::CELL_NEIGHBOR_TOP_CORNER:
  221. bit = 2;
  222. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_CORNER);
  223. break;
  224. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  225. bit = 3;
  226. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE);
  227. break;
  228. default:
  229. ERR_FAIL();
  230. break;
  231. }
  232. } else {
  233. // Half-offset shapes
  234. TileSet::TileOffsetAxis offset_axis = ts->get_tile_offset_axis();
  235. if (offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  236. switch (p_bit) {
  237. case TileSet::CELL_NEIGHBOR_RIGHT_SIDE:
  238. bit = 1;
  239. base_cell_coords = p_position;
  240. break;
  241. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  242. bit = 2;
  243. base_cell_coords = p_position;
  244. break;
  245. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  246. bit = 3;
  247. base_cell_coords = p_position;
  248. break;
  249. case TileSet::CELL_NEIGHBOR_BOTTOM_CORNER:
  250. bit = 4;
  251. base_cell_coords = p_position;
  252. break;
  253. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  254. bit = 5;
  255. base_cell_coords = p_position;
  256. break;
  257. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  258. bit = 2;
  259. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_LEFT_SIDE);
  260. break;
  261. case TileSet::CELL_NEIGHBOR_LEFT_SIDE:
  262. bit = 1;
  263. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_LEFT_SIDE);
  264. break;
  265. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  266. bit = 4;
  267. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  268. break;
  269. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  270. bit = 3;
  271. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  272. break;
  273. case TileSet::CELL_NEIGHBOR_TOP_CORNER:
  274. bit = 2;
  275. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  276. break;
  277. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  278. bit = 5;
  279. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE);
  280. break;
  281. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  282. bit = 4;
  283. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE);
  284. break;
  285. default:
  286. ERR_FAIL();
  287. break;
  288. }
  289. } else {
  290. switch (p_bit) {
  291. case TileSet::CELL_NEIGHBOR_RIGHT_CORNER:
  292. bit = 1;
  293. base_cell_coords = p_position;
  294. break;
  295. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  296. bit = 2;
  297. base_cell_coords = p_position;
  298. break;
  299. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  300. bit = 3;
  301. base_cell_coords = p_position;
  302. break;
  303. case TileSet::CELL_NEIGHBOR_BOTTOM_SIDE:
  304. bit = 4;
  305. base_cell_coords = p_position;
  306. break;
  307. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  308. bit = 1;
  309. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE);
  310. break;
  311. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  312. bit = 5;
  313. base_cell_coords = p_position;
  314. break;
  315. case TileSet::CELL_NEIGHBOR_LEFT_CORNER:
  316. bit = 3;
  317. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  318. break;
  319. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  320. bit = 2;
  321. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  322. break;
  323. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  324. bit = 1;
  325. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE);
  326. break;
  327. case TileSet::CELL_NEIGHBOR_TOP_SIDE:
  328. bit = 4;
  329. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_SIDE);
  330. break;
  331. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  332. bit = 3;
  333. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_SIDE);
  334. break;
  335. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  336. bit = 5;
  337. base_cell_coords = p_tile_map->get_neighbor_cell(p_position, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE);
  338. break;
  339. default:
  340. ERR_FAIL();
  341. break;
  342. }
  343. }
  344. }
  345. terrain = p_terrain;
  346. }
  347. Vector2i TileMap::transform_coords_layout(const Vector2i &p_coords, TileSet::TileOffsetAxis p_offset_axis, TileSet::TileLayout p_from_layout, TileSet::TileLayout p_to_layout) {
  348. // Transform to stacked layout.
  349. Vector2i output = p_coords;
  350. if (p_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL) {
  351. SWAP(output.x, output.y);
  352. }
  353. switch (p_from_layout) {
  354. case TileSet::TILE_LAYOUT_STACKED:
  355. break;
  356. case TileSet::TILE_LAYOUT_STACKED_OFFSET:
  357. if (output.y % 2) {
  358. output.x -= 1;
  359. }
  360. break;
  361. case TileSet::TILE_LAYOUT_STAIRS_RIGHT:
  362. case TileSet::TILE_LAYOUT_STAIRS_DOWN:
  363. if ((p_from_layout == TileSet::TILE_LAYOUT_STAIRS_RIGHT) ^ (p_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL)) {
  364. if (output.y < 0 && bool(output.y % 2)) {
  365. output = Vector2i(output.x + output.y / 2 - 1, output.y);
  366. } else {
  367. output = Vector2i(output.x + output.y / 2, output.y);
  368. }
  369. } else {
  370. if (output.x < 0 && bool(output.x % 2)) {
  371. output = Vector2i(output.x / 2 - 1, output.x + output.y * 2);
  372. } else {
  373. output = Vector2i(output.x / 2, output.x + output.y * 2);
  374. }
  375. }
  376. break;
  377. case TileSet::TILE_LAYOUT_DIAMOND_RIGHT:
  378. case TileSet::TILE_LAYOUT_DIAMOND_DOWN:
  379. if ((p_from_layout == TileSet::TILE_LAYOUT_DIAMOND_RIGHT) ^ (p_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL)) {
  380. if ((output.x + output.y) < 0 && (output.x - output.y) % 2) {
  381. output = Vector2i((output.x + output.y) / 2 - 1, output.y - output.x);
  382. } else {
  383. output = Vector2i((output.x + output.y) / 2, -output.x + output.y);
  384. }
  385. } else {
  386. if ((output.x - output.y) < 0 && (output.x + output.y) % 2) {
  387. output = Vector2i((output.x - output.y) / 2 - 1, output.x + output.y);
  388. } else {
  389. output = Vector2i((output.x - output.y) / 2, output.x + output.y);
  390. }
  391. }
  392. break;
  393. }
  394. switch (p_to_layout) {
  395. case TileSet::TILE_LAYOUT_STACKED:
  396. break;
  397. case TileSet::TILE_LAYOUT_STACKED_OFFSET:
  398. if (output.y % 2) {
  399. output.x += 1;
  400. }
  401. break;
  402. case TileSet::TILE_LAYOUT_STAIRS_RIGHT:
  403. case TileSet::TILE_LAYOUT_STAIRS_DOWN:
  404. if ((p_to_layout == TileSet::TILE_LAYOUT_STAIRS_RIGHT) ^ (p_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL)) {
  405. if (output.y < 0 && (output.y % 2)) {
  406. output = Vector2i(output.x - output.y / 2 + 1, output.y);
  407. } else {
  408. output = Vector2i(output.x - output.y / 2, output.y);
  409. }
  410. } else {
  411. if (output.y % 2) {
  412. if (output.y < 0) {
  413. output = Vector2i(2 * output.x + 1, -output.x + output.y / 2 - 1);
  414. } else {
  415. output = Vector2i(2 * output.x + 1, -output.x + output.y / 2);
  416. }
  417. } else {
  418. output = Vector2i(2 * output.x, -output.x + output.y / 2);
  419. }
  420. }
  421. break;
  422. case TileSet::TILE_LAYOUT_DIAMOND_RIGHT:
  423. case TileSet::TILE_LAYOUT_DIAMOND_DOWN:
  424. if ((p_to_layout == TileSet::TILE_LAYOUT_DIAMOND_RIGHT) ^ (p_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL)) {
  425. if (output.y % 2) {
  426. if (output.y > 0) {
  427. output = Vector2i(output.x - output.y / 2, output.x + output.y / 2 + 1);
  428. } else {
  429. output = Vector2i(output.x - output.y / 2 + 1, output.x + output.y / 2);
  430. }
  431. } else {
  432. output = Vector2i(output.x - output.y / 2, output.x + output.y / 2);
  433. }
  434. } else {
  435. if (output.y % 2) {
  436. if (output.y < 0) {
  437. output = Vector2i(output.x + output.y / 2, -output.x + output.y / 2 - 1);
  438. } else {
  439. output = Vector2i(output.x + output.y / 2 + 1, -output.x + output.y / 2);
  440. }
  441. } else {
  442. output = Vector2i(output.x + output.y / 2, -output.x + output.y / 2);
  443. }
  444. }
  445. break;
  446. }
  447. if (p_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL) {
  448. SWAP(output.x, output.y);
  449. }
  450. return output;
  451. }
  452. int TileMap::get_effective_quadrant_size(int p_layer) const {
  453. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), 1);
  454. // When using YSort, the quadrant size is reduced to 1 to have one CanvasItem per quadrant
  455. if (is_y_sort_enabled() && layers[p_layer].y_sort_enabled) {
  456. return 1;
  457. } else {
  458. return quadrant_size;
  459. }
  460. }
  461. void TileMap::set_selected_layer(int p_layer_id) {
  462. ERR_FAIL_COND(p_layer_id < -1 || p_layer_id >= (int)layers.size());
  463. selected_layer = p_layer_id;
  464. emit_signal(SNAME("changed"));
  465. // Update the layers modulation.
  466. for (unsigned int layer = 0; layer < layers.size(); layer++) {
  467. _rendering_update_layer(layer);
  468. }
  469. }
  470. int TileMap::get_selected_layer() const {
  471. return selected_layer;
  472. }
  473. void TileMap::_notification(int p_what) {
  474. switch (p_what) {
  475. case NOTIFICATION_ENTER_TREE: {
  476. _clear_internals();
  477. _recreate_internals();
  478. } break;
  479. case NOTIFICATION_EXIT_TREE: {
  480. _clear_internals();
  481. } break;
  482. }
  483. // Transfers the notification to tileset plugins.
  484. if (tile_set.is_valid()) {
  485. _rendering_notification(p_what);
  486. _physics_notification(p_what);
  487. _navigation_notification(p_what);
  488. }
  489. }
  490. Ref<TileSet> TileMap::get_tileset() const {
  491. return tile_set;
  492. }
  493. void TileMap::set_tileset(const Ref<TileSet> &p_tileset) {
  494. if (p_tileset == tile_set) {
  495. return;
  496. }
  497. // Set the tileset, registering to its changes.
  498. if (tile_set.is_valid()) {
  499. tile_set->disconnect("changed", callable_mp(this, &TileMap::_tile_set_changed));
  500. }
  501. if (!p_tileset.is_valid()) {
  502. _clear_internals();
  503. }
  504. tile_set = p_tileset;
  505. if (tile_set.is_valid()) {
  506. tile_set->connect("changed", callable_mp(this, &TileMap::_tile_set_changed));
  507. _clear_internals();
  508. _recreate_internals();
  509. }
  510. emit_signal(SNAME("changed"));
  511. }
  512. void TileMap::set_quadrant_size(int p_size) {
  513. ERR_FAIL_COND_MSG(p_size < 1, "TileMapQuadrant size cannot be smaller than 1.");
  514. quadrant_size = p_size;
  515. _clear_internals();
  516. _recreate_internals();
  517. emit_signal(SNAME("changed"));
  518. }
  519. int TileMap::get_quadrant_size() const {
  520. return quadrant_size;
  521. }
  522. int TileMap::get_layers_count() const {
  523. return layers.size();
  524. }
  525. void TileMap::add_layer(int p_to_pos) {
  526. if (p_to_pos < 0) {
  527. p_to_pos = layers.size() + p_to_pos + 1;
  528. }
  529. ERR_FAIL_INDEX(p_to_pos, (int)layers.size() + 1);
  530. // Must clear before adding the layer.
  531. _clear_internals();
  532. layers.insert(p_to_pos, TileMapLayer());
  533. _recreate_internals();
  534. notify_property_list_changed();
  535. emit_signal(SNAME("changed"));
  536. update_configuration_warnings();
  537. }
  538. void TileMap::move_layer(int p_layer, int p_to_pos) {
  539. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  540. ERR_FAIL_INDEX(p_to_pos, (int)layers.size() + 1);
  541. // Clear before shuffling layers.
  542. _clear_internals();
  543. TileMapLayer tl = layers[p_layer];
  544. layers.insert(p_to_pos, tl);
  545. layers.remove_at(p_to_pos < p_layer ? p_layer + 1 : p_layer);
  546. _recreate_internals();
  547. notify_property_list_changed();
  548. if (selected_layer == p_layer) {
  549. selected_layer = p_to_pos < p_layer ? p_to_pos - 1 : p_to_pos;
  550. }
  551. emit_signal(SNAME("changed"));
  552. update_configuration_warnings();
  553. }
  554. void TileMap::remove_layer(int p_layer) {
  555. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  556. // Clear before removing the layer.
  557. _clear_internals();
  558. layers.remove_at(p_layer);
  559. _recreate_internals();
  560. notify_property_list_changed();
  561. if (selected_layer >= p_layer) {
  562. selected_layer -= 1;
  563. }
  564. emit_signal(SNAME("changed"));
  565. update_configuration_warnings();
  566. }
  567. void TileMap::set_layer_name(int p_layer, String p_name) {
  568. if (p_layer < 0) {
  569. p_layer = layers.size() + p_layer;
  570. }
  571. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  572. layers[p_layer].name = p_name;
  573. emit_signal(SNAME("changed"));
  574. }
  575. String TileMap::get_layer_name(int p_layer) const {
  576. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), String());
  577. return layers[p_layer].name;
  578. }
  579. void TileMap::set_layer_enabled(int p_layer, bool p_enabled) {
  580. if (p_layer < 0) {
  581. p_layer = layers.size() + p_layer;
  582. }
  583. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  584. layers[p_layer].enabled = p_enabled;
  585. _clear_layer_internals(p_layer);
  586. _recreate_layer_internals(p_layer);
  587. emit_signal(SNAME("changed"));
  588. update_configuration_warnings();
  589. }
  590. bool TileMap::is_layer_enabled(int p_layer) const {
  591. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), false);
  592. return layers[p_layer].enabled;
  593. }
  594. void TileMap::set_layer_modulate(int p_layer, Color p_modulate) {
  595. if (p_layer < 0) {
  596. p_layer = layers.size() + p_layer;
  597. }
  598. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  599. layers[p_layer].modulate = p_modulate;
  600. _rendering_update_layer(p_layer);
  601. emit_signal(SNAME("changed"));
  602. }
  603. Color TileMap::get_layer_modulate(int p_layer) const {
  604. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), Color());
  605. return layers[p_layer].modulate;
  606. }
  607. void TileMap::set_layer_y_sort_enabled(int p_layer, bool p_y_sort_enabled) {
  608. if (p_layer < 0) {
  609. p_layer = layers.size() + p_layer;
  610. }
  611. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  612. layers[p_layer].y_sort_enabled = p_y_sort_enabled;
  613. _clear_layer_internals(p_layer);
  614. _recreate_layer_internals(p_layer);
  615. emit_signal(SNAME("changed"));
  616. update_configuration_warnings();
  617. }
  618. bool TileMap::is_layer_y_sort_enabled(int p_layer) const {
  619. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), false);
  620. return layers[p_layer].y_sort_enabled;
  621. }
  622. void TileMap::set_layer_y_sort_origin(int p_layer, int p_y_sort_origin) {
  623. if (p_layer < 0) {
  624. p_layer = layers.size() + p_layer;
  625. }
  626. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  627. layers[p_layer].y_sort_origin = p_y_sort_origin;
  628. _clear_layer_internals(p_layer);
  629. _recreate_layer_internals(p_layer);
  630. emit_signal(SNAME("changed"));
  631. }
  632. int TileMap::get_layer_y_sort_origin(int p_layer) const {
  633. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), false);
  634. return layers[p_layer].y_sort_origin;
  635. }
  636. void TileMap::set_layer_z_index(int p_layer, int p_z_index) {
  637. if (p_layer < 0) {
  638. p_layer = layers.size() + p_layer;
  639. }
  640. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  641. layers[p_layer].z_index = p_z_index;
  642. _rendering_update_layer(p_layer);
  643. emit_signal(SNAME("changed"));
  644. update_configuration_warnings();
  645. }
  646. int TileMap::get_layer_z_index(int p_layer) const {
  647. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), false);
  648. return layers[p_layer].z_index;
  649. }
  650. void TileMap::set_collision_animatable(bool p_enabled) {
  651. collision_animatable = p_enabled;
  652. _clear_internals();
  653. set_notify_local_transform(p_enabled);
  654. set_physics_process_internal(p_enabled);
  655. _recreate_internals();
  656. emit_signal(SNAME("changed"));
  657. }
  658. bool TileMap::is_collision_animatable() const {
  659. return collision_animatable;
  660. }
  661. void TileMap::set_collision_visibility_mode(TileMap::VisibilityMode p_show_collision) {
  662. collision_visibility_mode = p_show_collision;
  663. _clear_internals();
  664. _recreate_internals();
  665. emit_signal(SNAME("changed"));
  666. }
  667. TileMap::VisibilityMode TileMap::get_collision_visibility_mode() {
  668. return collision_visibility_mode;
  669. }
  670. void TileMap::set_navigation_visibility_mode(TileMap::VisibilityMode p_show_navigation) {
  671. navigation_visibility_mode = p_show_navigation;
  672. _clear_internals();
  673. _recreate_internals();
  674. emit_signal(SNAME("changed"));
  675. }
  676. TileMap::VisibilityMode TileMap::get_navigation_visibility_mode() {
  677. return navigation_visibility_mode;
  678. }
  679. void TileMap::set_navigation_map(int p_layer, RID p_map) {
  680. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  681. ERR_FAIL_COND_MSG(!is_inside_tree(), "A TileMap navigation map can only be changed while inside the SceneTree.");
  682. layers[p_layer].navigation_map = p_map;
  683. layers[p_layer].uses_world_navigation_map = p_map == get_world_2d()->get_navigation_map();
  684. }
  685. RID TileMap::get_navigation_map(int p_layer) const {
  686. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), RID());
  687. if (layers[p_layer].navigation_map.is_valid()) {
  688. return layers[p_layer].navigation_map;
  689. }
  690. return RID();
  691. }
  692. void TileMap::set_y_sort_enabled(bool p_enable) {
  693. Node2D::set_y_sort_enabled(p_enable);
  694. _clear_internals();
  695. _recreate_internals();
  696. emit_signal(SNAME("changed"));
  697. update_configuration_warnings();
  698. }
  699. Vector2i TileMap::_coords_to_quadrant_coords(int p_layer, const Vector2i &p_coords) const {
  700. int quad_size = get_effective_quadrant_size(p_layer);
  701. // Rounding down, instead of simply rounding towards zero (truncating)
  702. return Vector2i(
  703. p_coords.x > 0 ? p_coords.x / quad_size : (p_coords.x - (quad_size - 1)) / quad_size,
  704. p_coords.y > 0 ? p_coords.y / quad_size : (p_coords.y - (quad_size - 1)) / quad_size);
  705. }
  706. HashMap<Vector2i, TileMapQuadrant>::Iterator TileMap::_create_quadrant(int p_layer, const Vector2i &p_qk) {
  707. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), nullptr);
  708. TileMapQuadrant q;
  709. q.layer = p_layer;
  710. q.coords = p_qk;
  711. rect_cache_dirty = true;
  712. // Create the debug canvas item.
  713. RenderingServer *rs = RenderingServer::get_singleton();
  714. q.debug_canvas_item = rs->canvas_item_create();
  715. rs->canvas_item_set_z_index(q.debug_canvas_item, RS::CANVAS_ITEM_Z_MAX - 1);
  716. rs->canvas_item_set_parent(q.debug_canvas_item, get_canvas_item());
  717. // Call the create_quadrant method on plugins
  718. if (tile_set.is_valid()) {
  719. _rendering_create_quadrant(&q);
  720. }
  721. return layers[p_layer].quadrant_map.insert(p_qk, q);
  722. }
  723. void TileMap::_make_quadrant_dirty(HashMap<Vector2i, TileMapQuadrant>::Iterator Q) {
  724. // Make the given quadrant dirty, then trigger an update later.
  725. TileMapQuadrant &q = Q->value;
  726. if (!q.dirty_list_element.in_list()) {
  727. layers[q.layer].dirty_quadrant_list.add(&q.dirty_list_element);
  728. }
  729. _queue_update_dirty_quadrants();
  730. }
  731. void TileMap::_make_all_quadrants_dirty() {
  732. // Make all quandrants dirty, then trigger an update later.
  733. for (TileMapLayer &layer : layers) {
  734. for (KeyValue<Vector2i, TileMapQuadrant> &E : layer.quadrant_map) {
  735. if (!E.value.dirty_list_element.in_list()) {
  736. layer.dirty_quadrant_list.add(&E.value.dirty_list_element);
  737. }
  738. }
  739. }
  740. _queue_update_dirty_quadrants();
  741. }
  742. void TileMap::_queue_update_dirty_quadrants() {
  743. if (pending_update || !is_inside_tree()) {
  744. return;
  745. }
  746. pending_update = true;
  747. call_deferred(SNAME("_update_dirty_quadrants"));
  748. }
  749. void TileMap::_update_dirty_quadrants() {
  750. if (!pending_update) {
  751. return;
  752. }
  753. if (!is_inside_tree() || !tile_set.is_valid()) {
  754. pending_update = false;
  755. return;
  756. }
  757. for (unsigned int layer = 0; layer < layers.size(); layer++) {
  758. SelfList<TileMapQuadrant>::List &dirty_quadrant_list = layers[layer].dirty_quadrant_list;
  759. // Update the coords cache.
  760. for (SelfList<TileMapQuadrant> *q = dirty_quadrant_list.first(); q; q = q->next()) {
  761. q->self()->map_to_local.clear();
  762. q->self()->local_to_map.clear();
  763. for (const Vector2i &E : q->self()->cells) {
  764. Vector2i pk = E;
  765. Vector2i pk_local_coords = map_to_local(pk);
  766. q->self()->map_to_local[pk] = pk_local_coords;
  767. q->self()->local_to_map[pk_local_coords] = pk;
  768. }
  769. }
  770. // Find TileData that need a runtime modification.
  771. _build_runtime_update_tile_data(dirty_quadrant_list);
  772. // Call the update_dirty_quadrant method on plugins.
  773. _rendering_update_dirty_quadrants(dirty_quadrant_list);
  774. _physics_update_dirty_quadrants(dirty_quadrant_list);
  775. _navigation_update_dirty_quadrants(dirty_quadrant_list);
  776. _scenes_update_dirty_quadrants(dirty_quadrant_list);
  777. // Redraw the debug canvas_items.
  778. RenderingServer *rs = RenderingServer::get_singleton();
  779. for (SelfList<TileMapQuadrant> *q = dirty_quadrant_list.first(); q; q = q->next()) {
  780. rs->canvas_item_clear(q->self()->debug_canvas_item);
  781. Transform2D xform;
  782. xform.set_origin(map_to_local(q->self()->coords * get_effective_quadrant_size(layer)));
  783. rs->canvas_item_set_transform(q->self()->debug_canvas_item, xform);
  784. _rendering_draw_quadrant_debug(q->self());
  785. _physics_draw_quadrant_debug(q->self());
  786. _navigation_draw_quadrant_debug(q->self());
  787. _scenes_draw_quadrant_debug(q->self());
  788. }
  789. // Clear the list
  790. while (dirty_quadrant_list.first()) {
  791. // Clear the runtime tile data.
  792. for (const KeyValue<Vector2i, TileData *> &kv : dirty_quadrant_list.first()->self()->runtime_tile_data_cache) {
  793. memdelete(kv.value);
  794. }
  795. dirty_quadrant_list.remove(dirty_quadrant_list.first());
  796. }
  797. }
  798. pending_update = false;
  799. _recompute_rect_cache();
  800. }
  801. void TileMap::_recreate_layer_internals(int p_layer) {
  802. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  803. // Make sure that _clear_internals() was called prior.
  804. ERR_FAIL_COND_MSG(layers[p_layer].quadrant_map.size() > 0, "TileMap layer " + itos(p_layer) + " had a non-empty quadrant map.");
  805. if (!layers[p_layer].enabled) {
  806. return;
  807. }
  808. // Update the layer internals.
  809. _rendering_update_layer(p_layer);
  810. // Update the layer internal navigation maps.
  811. _navigation_update_layer(p_layer);
  812. // Recreate the quadrants.
  813. const HashMap<Vector2i, TileMapCell> &tile_map = layers[p_layer].tile_map;
  814. for (const KeyValue<Vector2i, TileMapCell> &E : tile_map) {
  815. Vector2i qk = _coords_to_quadrant_coords(p_layer, Vector2i(E.key.x, E.key.y));
  816. HashMap<Vector2i, TileMapQuadrant>::Iterator Q = layers[p_layer].quadrant_map.find(qk);
  817. if (!Q) {
  818. Q = _create_quadrant(p_layer, qk);
  819. layers[p_layer].dirty_quadrant_list.add(&Q->value.dirty_list_element);
  820. }
  821. Vector2i pk = E.key;
  822. Q->value.cells.insert(pk);
  823. _make_quadrant_dirty(Q);
  824. }
  825. _queue_update_dirty_quadrants();
  826. }
  827. void TileMap::_recreate_internals() {
  828. for (unsigned int layer = 0; layer < layers.size(); layer++) {
  829. _recreate_layer_internals(layer);
  830. }
  831. }
  832. void TileMap::_erase_quadrant(HashMap<Vector2i, TileMapQuadrant>::Iterator Q) {
  833. // Remove a quadrant.
  834. TileMapQuadrant *q = &(Q->value);
  835. // Call the cleanup_quadrant method on plugins.
  836. if (tile_set.is_valid()) {
  837. _rendering_cleanup_quadrant(q);
  838. _physics_cleanup_quadrant(q);
  839. _navigation_cleanup_quadrant(q);
  840. _scenes_cleanup_quadrant(q);
  841. }
  842. // Remove the quadrant from the dirty_list if it is there.
  843. if (q->dirty_list_element.in_list()) {
  844. layers[q->layer].dirty_quadrant_list.remove(&(q->dirty_list_element));
  845. }
  846. // Free the debug canvas item.
  847. RenderingServer *rs = RenderingServer::get_singleton();
  848. rs->free(q->debug_canvas_item);
  849. layers[q->layer].quadrant_map.remove(Q);
  850. rect_cache_dirty = true;
  851. }
  852. void TileMap::_clear_layer_internals(int p_layer) {
  853. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  854. // Clear quadrants.
  855. while (layers[p_layer].quadrant_map.size()) {
  856. _erase_quadrant(layers[p_layer].quadrant_map.begin());
  857. }
  858. // Clear the layers internals.
  859. _rendering_cleanup_layer(p_layer);
  860. // Clear the layers internal navigation maps.
  861. _navigation_cleanup_layer(p_layer);
  862. // Clear the dirty quadrants list.
  863. while (layers[p_layer].dirty_quadrant_list.first()) {
  864. layers[p_layer].dirty_quadrant_list.remove(layers[p_layer].dirty_quadrant_list.first());
  865. }
  866. }
  867. void TileMap::_clear_internals() {
  868. // Clear quadrants.
  869. for (unsigned int layer = 0; layer < layers.size(); layer++) {
  870. _clear_layer_internals(layer);
  871. }
  872. }
  873. void TileMap::_recompute_rect_cache() {
  874. // Compute the displayed area of the tilemap.
  875. #ifdef DEBUG_ENABLED
  876. if (!rect_cache_dirty) {
  877. return;
  878. }
  879. Rect2 r_total;
  880. bool first = true;
  881. for (unsigned int layer = 0; layer < layers.size(); layer++) {
  882. for (const KeyValue<Vector2i, TileMapQuadrant> &E : layers[layer].quadrant_map) {
  883. Rect2 r;
  884. r.position = map_to_local(E.key * get_effective_quadrant_size(layer));
  885. r.expand_to(map_to_local((E.key + Vector2i(1, 0)) * get_effective_quadrant_size(layer)));
  886. r.expand_to(map_to_local((E.key + Vector2i(1, 1)) * get_effective_quadrant_size(layer)));
  887. r.expand_to(map_to_local((E.key + Vector2i(0, 1)) * get_effective_quadrant_size(layer)));
  888. if (first) {
  889. r_total = r;
  890. first = false;
  891. } else {
  892. r_total = r_total.merge(r);
  893. }
  894. }
  895. }
  896. bool changed = rect_cache != r_total;
  897. rect_cache = r_total;
  898. item_rect_changed(changed);
  899. rect_cache_dirty = false;
  900. #endif
  901. }
  902. /////////////////////////////// Rendering //////////////////////////////////////
  903. void TileMap::_rendering_notification(int p_what) {
  904. switch (p_what) {
  905. case NOTIFICATION_ENTER_CANVAS: {
  906. bool node_visible = is_visible_in_tree();
  907. for (TileMapLayer &layer : layers) {
  908. for (KeyValue<Vector2i, TileMapQuadrant> &E_quadrant : layer.quadrant_map) {
  909. TileMapQuadrant &q = E_quadrant.value;
  910. for (const KeyValue<Vector2i, RID> &kv : q.occluders) {
  911. Transform2D xform;
  912. xform.set_origin(map_to_local(kv.key));
  913. RS::get_singleton()->canvas_light_occluder_attach_to_canvas(kv.value, get_canvas());
  914. RS::get_singleton()->canvas_light_occluder_set_transform(kv.value, get_global_transform() * xform);
  915. RS::get_singleton()->canvas_light_occluder_set_enabled(kv.value, node_visible);
  916. }
  917. }
  918. }
  919. } break;
  920. case NOTIFICATION_VISIBILITY_CHANGED: {
  921. bool node_visible = is_visible_in_tree();
  922. for (TileMapLayer &layer : layers) {
  923. for (KeyValue<Vector2i, TileMapQuadrant> &E_quadrant : layer.quadrant_map) {
  924. TileMapQuadrant &q = E_quadrant.value;
  925. // Update occluders transform.
  926. for (const KeyValue<Vector2i, Vector2i> &E_cell : q.local_to_map) {
  927. Transform2D xform;
  928. xform.set_origin(E_cell.key);
  929. for (const KeyValue<Vector2i, RID> &kv : q.occluders) {
  930. RS::get_singleton()->canvas_light_occluder_set_enabled(kv.value, node_visible);
  931. }
  932. }
  933. }
  934. }
  935. } break;
  936. case NOTIFICATION_TRANSFORM_CHANGED: {
  937. if (!is_inside_tree()) {
  938. return;
  939. }
  940. for (TileMapLayer &layer : layers) {
  941. for (KeyValue<Vector2i, TileMapQuadrant> &E_quadrant : layer.quadrant_map) {
  942. TileMapQuadrant &q = E_quadrant.value;
  943. // Update occluders transform.
  944. for (const KeyValue<Vector2i, RID> &kv : q.occluders) {
  945. Transform2D xform;
  946. xform.set_origin(map_to_local(kv.key));
  947. RenderingServer::get_singleton()->canvas_light_occluder_set_transform(kv.value, get_global_transform() * xform);
  948. }
  949. }
  950. }
  951. } break;
  952. case NOTIFICATION_DRAW: {
  953. if (tile_set.is_valid()) {
  954. RenderingServer::get_singleton()->canvas_item_set_sort_children_by_y(get_canvas_item(), is_y_sort_enabled());
  955. }
  956. } break;
  957. case NOTIFICATION_EXIT_CANVAS: {
  958. for (TileMapLayer &layer : layers) {
  959. for (KeyValue<Vector2i, TileMapQuadrant> &E_quadrant : layer.quadrant_map) {
  960. TileMapQuadrant &q = E_quadrant.value;
  961. for (const KeyValue<Vector2i, RID> &kv : q.occluders) {
  962. RS::get_singleton()->canvas_light_occluder_attach_to_canvas(kv.value, RID());
  963. }
  964. }
  965. }
  966. } break;
  967. }
  968. }
  969. void TileMap::_navigation_update_layer(int p_layer) {
  970. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  971. ERR_FAIL_NULL(NavigationServer2D::get_singleton());
  972. if (!layers[p_layer].navigation_map.is_valid()) {
  973. if (p_layer == 0 && is_inside_tree()) {
  974. // Use the default World2D navigation map for the first layer when empty.
  975. layers[p_layer].navigation_map = get_world_2d()->get_navigation_map();
  976. layers[p_layer].uses_world_navigation_map = true;
  977. } else {
  978. RID new_layer_map = NavigationServer2D::get_singleton()->map_create();
  979. NavigationServer2D::get_singleton()->map_set_active(new_layer_map, true);
  980. layers[p_layer].navigation_map = new_layer_map;
  981. layers[p_layer].uses_world_navigation_map = false;
  982. }
  983. }
  984. }
  985. void TileMap::_navigation_cleanup_layer(int p_layer) {
  986. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  987. ERR_FAIL_NULL(NavigationServer2D::get_singleton());
  988. if (layers[p_layer].navigation_map.is_valid()) {
  989. if (layers[p_layer].uses_world_navigation_map) {
  990. // Do not delete the World2D default navigation map.
  991. return;
  992. }
  993. NavigationServer2D::get_singleton()->free(layers[p_layer].navigation_map);
  994. layers[p_layer].navigation_map = RID();
  995. }
  996. }
  997. void TileMap::_rendering_update_layer(int p_layer) {
  998. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  999. RenderingServer *rs = RenderingServer::get_singleton();
  1000. if (!layers[p_layer].canvas_item.is_valid()) {
  1001. RID ci = rs->canvas_item_create();
  1002. rs->canvas_item_set_parent(ci, get_canvas_item());
  1003. /*Transform2D xform;
  1004. xform.set_origin(Vector2(0, p_layer));
  1005. rs->canvas_item_set_transform(ci, xform);*/
  1006. rs->canvas_item_set_draw_index(ci, p_layer - (int64_t)0x80000000);
  1007. layers[p_layer].canvas_item = ci;
  1008. }
  1009. RID &ci = layers[p_layer].canvas_item;
  1010. rs->canvas_item_set_sort_children_by_y(ci, layers[p_layer].y_sort_enabled);
  1011. rs->canvas_item_set_use_parent_material(ci, get_use_parent_material() || get_material().is_valid());
  1012. rs->canvas_item_set_z_index(ci, layers[p_layer].z_index);
  1013. rs->canvas_item_set_default_texture_filter(ci, RS::CanvasItemTextureFilter(get_texture_filter_in_tree()));
  1014. rs->canvas_item_set_default_texture_repeat(ci, RS::CanvasItemTextureRepeat(get_texture_repeat_in_tree()));
  1015. rs->canvas_item_set_light_mask(ci, get_light_mask());
  1016. Color layer_modulate = get_layer_modulate(p_layer);
  1017. if (selected_layer >= 0 && p_layer != selected_layer) {
  1018. int z1 = get_layer_z_index(p_layer);
  1019. int z2 = get_layer_z_index(selected_layer);
  1020. if (z1 < z2 || (z1 == z2 && p_layer < selected_layer)) {
  1021. layer_modulate = layer_modulate.darkened(0.5);
  1022. } else if (z1 > z2 || (z1 == z2 && p_layer > selected_layer)) {
  1023. layer_modulate = layer_modulate.darkened(0.5);
  1024. layer_modulate.a *= 0.3;
  1025. }
  1026. }
  1027. rs->canvas_item_set_modulate(ci, layer_modulate);
  1028. }
  1029. void TileMap::_rendering_cleanup_layer(int p_layer) {
  1030. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  1031. ERR_FAIL_NULL(RenderingServer::get_singleton());
  1032. RenderingServer *rs = RenderingServer::get_singleton();
  1033. if (layers[p_layer].canvas_item.is_valid()) {
  1034. rs->free(layers[p_layer].canvas_item);
  1035. layers[p_layer].canvas_item = RID();
  1036. }
  1037. }
  1038. void TileMap::_rendering_update_dirty_quadrants(SelfList<TileMapQuadrant>::List &r_dirty_quadrant_list) {
  1039. ERR_FAIL_COND(!is_inside_tree());
  1040. ERR_FAIL_COND(!tile_set.is_valid());
  1041. bool node_visible = is_visible_in_tree();
  1042. SelfList<TileMapQuadrant> *q_list_element = r_dirty_quadrant_list.first();
  1043. while (q_list_element) {
  1044. TileMapQuadrant &q = *q_list_element->self();
  1045. RenderingServer *rs = RenderingServer::get_singleton();
  1046. // Free the canvas items.
  1047. for (const RID &ci : q.canvas_items) {
  1048. rs->free(ci);
  1049. }
  1050. q.canvas_items.clear();
  1051. // Free the occluders.
  1052. for (const KeyValue<Vector2i, RID> &kv : q.occluders) {
  1053. rs->free(kv.value);
  1054. }
  1055. q.occluders.clear();
  1056. // Those allow to group cell per material or z-index.
  1057. Ref<Material> prev_material;
  1058. int prev_z_index = 0;
  1059. RID prev_ci;
  1060. // Iterate over the cells of the quadrant.
  1061. for (const KeyValue<Vector2i, Vector2i> &E_cell : q.local_to_map) {
  1062. TileMapCell c = get_cell(q.layer, E_cell.value, true);
  1063. TileSetSource *source;
  1064. if (tile_set->has_source(c.source_id)) {
  1065. source = *tile_set->get_source(c.source_id);
  1066. if (!source->has_tile(c.get_atlas_coords()) || !source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  1067. continue;
  1068. }
  1069. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  1070. if (atlas_source) {
  1071. // Get the tile data.
  1072. const TileData *tile_data;
  1073. if (q.runtime_tile_data_cache.has(E_cell.value)) {
  1074. tile_data = q.runtime_tile_data_cache[E_cell.value];
  1075. } else {
  1076. tile_data = atlas_source->get_tile_data(c.get_atlas_coords(), c.alternative_tile);
  1077. }
  1078. Ref<Material> mat = tile_data->get_material();
  1079. int tile_z_index = tile_data->get_z_index();
  1080. // Quandrant pos.
  1081. Vector2 tile_position = map_to_local(q.coords * get_effective_quadrant_size(q.layer));
  1082. if (is_y_sort_enabled() && layers[q.layer].y_sort_enabled) {
  1083. // When Y-sorting, the quandrant size is sure to be 1, we can thus offset the CanvasItem.
  1084. tile_position.y += layers[q.layer].y_sort_origin + tile_data->get_y_sort_origin();
  1085. }
  1086. // --- CanvasItems ---
  1087. // Create two canvas items, for rendering and debug.
  1088. RID ci;
  1089. // Check if the material or the z_index changed.
  1090. if (prev_ci == RID() || prev_material != mat || prev_z_index != tile_z_index) {
  1091. // If so, create a new CanvasItem.
  1092. ci = rs->canvas_item_create();
  1093. if (mat.is_valid()) {
  1094. rs->canvas_item_set_material(ci, mat->get_rid());
  1095. }
  1096. rs->canvas_item_set_parent(ci, layers[q.layer].canvas_item);
  1097. rs->canvas_item_set_use_parent_material(ci, get_use_parent_material() || get_material().is_valid());
  1098. Transform2D xform;
  1099. xform.set_origin(tile_position);
  1100. rs->canvas_item_set_transform(ci, xform);
  1101. rs->canvas_item_set_light_mask(ci, get_light_mask());
  1102. rs->canvas_item_set_z_as_relative_to_parent(ci, true);
  1103. rs->canvas_item_set_z_index(ci, tile_z_index);
  1104. rs->canvas_item_set_default_texture_filter(ci, RS::CanvasItemTextureFilter(get_texture_filter_in_tree()));
  1105. rs->canvas_item_set_default_texture_repeat(ci, RS::CanvasItemTextureRepeat(get_texture_repeat_in_tree()));
  1106. q.canvas_items.push_back(ci);
  1107. prev_ci = ci;
  1108. prev_material = mat;
  1109. prev_z_index = tile_z_index;
  1110. } else {
  1111. // Keep the same canvas_item to draw on.
  1112. ci = prev_ci;
  1113. }
  1114. // Drawing the tile in the canvas item.
  1115. draw_tile(ci, E_cell.key - tile_position, tile_set, c.source_id, c.get_atlas_coords(), c.alternative_tile, -1, get_self_modulate(), tile_data);
  1116. // --- Occluders ---
  1117. for (int i = 0; i < tile_set->get_occlusion_layers_count(); i++) {
  1118. Transform2D xform;
  1119. xform.set_origin(E_cell.key);
  1120. if (tile_data->get_occluder(i).is_valid()) {
  1121. RID occluder_id = rs->canvas_light_occluder_create();
  1122. rs->canvas_light_occluder_set_enabled(occluder_id, node_visible);
  1123. rs->canvas_light_occluder_set_transform(occluder_id, get_global_transform() * xform);
  1124. rs->canvas_light_occluder_set_polygon(occluder_id, tile_data->get_occluder(i)->get_rid());
  1125. rs->canvas_light_occluder_attach_to_canvas(occluder_id, get_canvas());
  1126. rs->canvas_light_occluder_set_light_mask(occluder_id, tile_set->get_occlusion_layer_light_mask(i));
  1127. q.occluders[E_cell.value] = occluder_id;
  1128. }
  1129. }
  1130. }
  1131. }
  1132. }
  1133. _rendering_quadrant_order_dirty = true;
  1134. q_list_element = q_list_element->next();
  1135. }
  1136. // Reset the drawing indices
  1137. if (_rendering_quadrant_order_dirty) {
  1138. int index = -(int64_t)0x80000000; //always must be drawn below children.
  1139. for (TileMapLayer &layer : layers) {
  1140. // Sort the quadrants coords per local coordinates.
  1141. RBMap<Vector2i, Vector2i, TileMapQuadrant::CoordsWorldComparator> local_to_map;
  1142. for (const KeyValue<Vector2i, TileMapQuadrant> &E : layer.quadrant_map) {
  1143. local_to_map[map_to_local(E.key)] = E.key;
  1144. }
  1145. // Sort the quadrants.
  1146. for (const KeyValue<Vector2i, Vector2i> &E : local_to_map) {
  1147. TileMapQuadrant &q = layer.quadrant_map[E.value];
  1148. for (const RID &ci : q.canvas_items) {
  1149. RS::get_singleton()->canvas_item_set_draw_index(ci, index++);
  1150. }
  1151. }
  1152. }
  1153. _rendering_quadrant_order_dirty = false;
  1154. }
  1155. }
  1156. void TileMap::_rendering_create_quadrant(TileMapQuadrant *p_quadrant) {
  1157. ERR_FAIL_COND(!tile_set.is_valid());
  1158. _rendering_quadrant_order_dirty = true;
  1159. }
  1160. void TileMap::_rendering_cleanup_quadrant(TileMapQuadrant *p_quadrant) {
  1161. ERR_FAIL_NULL(RenderingServer::get_singleton());
  1162. // Free the canvas items.
  1163. for (const RID &ci : p_quadrant->canvas_items) {
  1164. RenderingServer::get_singleton()->free(ci);
  1165. }
  1166. p_quadrant->canvas_items.clear();
  1167. // Free the occluders.
  1168. for (const KeyValue<Vector2i, RID> &kv : p_quadrant->occluders) {
  1169. RenderingServer::get_singleton()->free(kv.value);
  1170. }
  1171. p_quadrant->occluders.clear();
  1172. }
  1173. void TileMap::_rendering_draw_quadrant_debug(TileMapQuadrant *p_quadrant) {
  1174. ERR_FAIL_COND(!tile_set.is_valid());
  1175. if (!Engine::get_singleton()->is_editor_hint()) {
  1176. return;
  1177. }
  1178. // Draw a placeholder for tiles needing one.
  1179. RenderingServer *rs = RenderingServer::get_singleton();
  1180. Vector2 quadrant_pos = map_to_local(p_quadrant->coords * get_effective_quadrant_size(p_quadrant->layer));
  1181. for (const Vector2i &E_cell : p_quadrant->cells) {
  1182. const TileMapCell &c = get_cell(p_quadrant->layer, E_cell, true);
  1183. TileSetSource *source;
  1184. if (tile_set->has_source(c.source_id)) {
  1185. source = *tile_set->get_source(c.source_id);
  1186. if (!source->has_tile(c.get_atlas_coords()) || !source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  1187. continue;
  1188. }
  1189. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  1190. if (atlas_source) {
  1191. Vector2i grid_size = atlas_source->get_atlas_grid_size();
  1192. if (!atlas_source->get_runtime_texture().is_valid() || c.get_atlas_coords().x >= grid_size.x || c.get_atlas_coords().y >= grid_size.y) {
  1193. // Generate a random color from the hashed values of the tiles.
  1194. Array to_hash;
  1195. to_hash.push_back(c.source_id);
  1196. to_hash.push_back(c.get_atlas_coords());
  1197. to_hash.push_back(c.alternative_tile);
  1198. uint32_t hash = RandomPCG(to_hash.hash()).rand();
  1199. Color color;
  1200. color = color.from_hsv(
  1201. (float)((hash >> 24) & 0xFF) / 256.0,
  1202. Math::lerp(0.5, 1.0, (float)((hash >> 16) & 0xFF) / 256.0),
  1203. Math::lerp(0.5, 1.0, (float)((hash >> 8) & 0xFF) / 256.0),
  1204. 0.8);
  1205. // Draw a placeholder tile.
  1206. Transform2D cell_to_quadrant;
  1207. cell_to_quadrant.set_origin(map_to_local(E_cell) - quadrant_pos);
  1208. rs->canvas_item_add_set_transform(p_quadrant->debug_canvas_item, cell_to_quadrant);
  1209. rs->canvas_item_add_circle(p_quadrant->debug_canvas_item, Vector2(), MIN(tile_set->get_tile_size().x, tile_set->get_tile_size().y) / 4.0, color);
  1210. }
  1211. }
  1212. }
  1213. }
  1214. }
  1215. void TileMap::draw_tile(RID p_canvas_item, const Vector2i &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) {
  1216. ERR_FAIL_COND(!p_tile_set.is_valid());
  1217. ERR_FAIL_COND(!p_tile_set->has_source(p_atlas_source_id));
  1218. ERR_FAIL_COND(!p_tile_set->get_source(p_atlas_source_id)->has_tile(p_atlas_coords));
  1219. ERR_FAIL_COND(!p_tile_set->get_source(p_atlas_source_id)->has_alternative_tile(p_atlas_coords, p_alternative_tile));
  1220. TileSetSource *source = *p_tile_set->get_source(p_atlas_source_id);
  1221. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  1222. if (atlas_source) {
  1223. // Check for the frame.
  1224. if (p_frame >= 0) {
  1225. ERR_FAIL_INDEX(p_frame, atlas_source->get_tile_animation_frames_count(p_atlas_coords));
  1226. }
  1227. // Get the texture.
  1228. Ref<Texture2D> tex = atlas_source->get_runtime_texture();
  1229. if (!tex.is_valid()) {
  1230. return;
  1231. }
  1232. // Check if we are in the texture, return otherwise.
  1233. Vector2i grid_size = atlas_source->get_atlas_grid_size();
  1234. if (p_atlas_coords.x >= grid_size.x || p_atlas_coords.y >= grid_size.y) {
  1235. return;
  1236. }
  1237. // Get tile data.
  1238. const TileData *tile_data = p_tile_data_override ? p_tile_data_override : atlas_source->get_tile_data(p_atlas_coords, p_alternative_tile);
  1239. // Get the tile modulation.
  1240. Color modulate = tile_data->get_modulate() * p_modulation;
  1241. // Compute the offset.
  1242. Vector2i tile_offset = tile_data->get_texture_origin();
  1243. // Get destination rect.
  1244. Rect2 dest_rect;
  1245. dest_rect.size = atlas_source->get_runtime_tile_texture_region(p_atlas_coords).size;
  1246. dest_rect.size.x += FP_ADJUST;
  1247. dest_rect.size.y += FP_ADJUST;
  1248. bool transpose = tile_data->get_transpose();
  1249. if (transpose) {
  1250. dest_rect.position = (p_position - Vector2(dest_rect.size.y, dest_rect.size.x) / 2 - tile_offset);
  1251. } else {
  1252. dest_rect.position = (p_position - dest_rect.size / 2 - tile_offset);
  1253. }
  1254. if (tile_data->get_flip_h()) {
  1255. dest_rect.size.x = -dest_rect.size.x;
  1256. }
  1257. if (tile_data->get_flip_v()) {
  1258. dest_rect.size.y = -dest_rect.size.y;
  1259. }
  1260. // Draw the tile.
  1261. if (p_frame >= 0) {
  1262. Rect2i source_rect = atlas_source->get_runtime_tile_texture_region(p_atlas_coords, p_frame);
  1263. tex->draw_rect_region(p_canvas_item, dest_rect, source_rect, modulate, transpose, p_tile_set->is_uv_clipping());
  1264. } else if (atlas_source->get_tile_animation_frames_count(p_atlas_coords) == 1) {
  1265. Rect2i source_rect = atlas_source->get_runtime_tile_texture_region(p_atlas_coords, 0);
  1266. tex->draw_rect_region(p_canvas_item, dest_rect, source_rect, modulate, transpose, p_tile_set->is_uv_clipping());
  1267. } else {
  1268. real_t speed = atlas_source->get_tile_animation_speed(p_atlas_coords);
  1269. real_t animation_duration = atlas_source->get_tile_animation_total_duration(p_atlas_coords) / speed;
  1270. real_t time = 0.0;
  1271. for (int frame = 0; frame < atlas_source->get_tile_animation_frames_count(p_atlas_coords); frame++) {
  1272. real_t frame_duration = atlas_source->get_tile_animation_frame_duration(p_atlas_coords, frame) / speed;
  1273. RenderingServer::get_singleton()->canvas_item_add_animation_slice(p_canvas_item, animation_duration, time, time + frame_duration, 0.0);
  1274. Rect2i source_rect = atlas_source->get_runtime_tile_texture_region(p_atlas_coords, frame);
  1275. tex->draw_rect_region(p_canvas_item, dest_rect, source_rect, modulate, transpose, p_tile_set->is_uv_clipping());
  1276. time += frame_duration;
  1277. }
  1278. RenderingServer::get_singleton()->canvas_item_add_animation_slice(p_canvas_item, 1.0, 0.0, 1.0, 0.0);
  1279. }
  1280. }
  1281. }
  1282. /////////////////////////////// Physics //////////////////////////////////////
  1283. void TileMap::_physics_notification(int p_what) {
  1284. switch (p_what) {
  1285. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  1286. bool in_editor = false;
  1287. #ifdef TOOLS_ENABLED
  1288. in_editor = Engine::get_singleton()->is_editor_hint();
  1289. #endif
  1290. if (is_inside_tree() && collision_animatable && !in_editor) {
  1291. // Update transform on the physics tick when in animatable mode.
  1292. last_valid_transform = new_transform;
  1293. set_notify_local_transform(false);
  1294. set_global_transform(new_transform);
  1295. set_notify_local_transform(true);
  1296. }
  1297. } break;
  1298. case NOTIFICATION_TRANSFORM_CHANGED: {
  1299. bool in_editor = false;
  1300. #ifdef TOOLS_ENABLED
  1301. in_editor = Engine::get_singleton()->is_editor_hint();
  1302. #endif
  1303. if (is_inside_tree() && (!collision_animatable || in_editor)) {
  1304. // Update the new transform directly if we are not in animatable mode.
  1305. Transform2D gl_transform = get_global_transform();
  1306. for (TileMapLayer &layer : layers) {
  1307. for (KeyValue<Vector2i, TileMapQuadrant> &E : layer.quadrant_map) {
  1308. TileMapQuadrant &q = E.value;
  1309. for (RID body : q.bodies) {
  1310. Transform2D xform;
  1311. xform.set_origin(map_to_local(bodies_coords[body]));
  1312. xform = gl_transform * xform;
  1313. PhysicsServer2D::get_singleton()->body_set_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM, xform);
  1314. }
  1315. }
  1316. }
  1317. }
  1318. } break;
  1319. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  1320. bool in_editor = false;
  1321. #ifdef TOOLS_ENABLED
  1322. in_editor = Engine::get_singleton()->is_editor_hint();
  1323. #endif
  1324. if (is_inside_tree() && !in_editor && collision_animatable) {
  1325. // Only active when animatable. Send the new transform to the physics...
  1326. new_transform = get_global_transform();
  1327. for (TileMapLayer &layer : layers) {
  1328. for (KeyValue<Vector2i, TileMapQuadrant> &E : layer.quadrant_map) {
  1329. TileMapQuadrant &q = E.value;
  1330. for (RID body : q.bodies) {
  1331. Transform2D xform;
  1332. xform.set_origin(map_to_local(bodies_coords[body]));
  1333. xform = new_transform * xform;
  1334. PhysicsServer2D::get_singleton()->body_set_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM, xform);
  1335. }
  1336. }
  1337. }
  1338. // ... but then revert changes.
  1339. set_notify_local_transform(false);
  1340. set_global_transform(last_valid_transform);
  1341. set_notify_local_transform(true);
  1342. }
  1343. } break;
  1344. }
  1345. }
  1346. void TileMap::_physics_update_dirty_quadrants(SelfList<TileMapQuadrant>::List &r_dirty_quadrant_list) {
  1347. ERR_FAIL_COND(!is_inside_tree());
  1348. ERR_FAIL_COND(!tile_set.is_valid());
  1349. Transform2D gl_transform = get_global_transform();
  1350. last_valid_transform = gl_transform;
  1351. new_transform = gl_transform;
  1352. PhysicsServer2D *ps = PhysicsServer2D::get_singleton();
  1353. RID space = get_world_2d()->get_space();
  1354. SelfList<TileMapQuadrant> *q_list_element = r_dirty_quadrant_list.first();
  1355. while (q_list_element) {
  1356. TileMapQuadrant &q = *q_list_element->self();
  1357. // Clear bodies.
  1358. for (RID body : q.bodies) {
  1359. bodies_coords.erase(body);
  1360. ps->free(body);
  1361. }
  1362. q.bodies.clear();
  1363. // Recreate bodies and shapes.
  1364. for (const Vector2i &E_cell : q.cells) {
  1365. TileMapCell c = get_cell(q.layer, E_cell, true);
  1366. TileSetSource *source;
  1367. if (tile_set->has_source(c.source_id)) {
  1368. source = *tile_set->get_source(c.source_id);
  1369. if (!source->has_tile(c.get_atlas_coords()) || !source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  1370. continue;
  1371. }
  1372. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  1373. if (atlas_source) {
  1374. const TileData *tile_data;
  1375. if (q.runtime_tile_data_cache.has(E_cell)) {
  1376. tile_data = q.runtime_tile_data_cache[E_cell];
  1377. } else {
  1378. tile_data = atlas_source->get_tile_data(c.get_atlas_coords(), c.alternative_tile);
  1379. }
  1380. for (int tile_set_physics_layer = 0; tile_set_physics_layer < tile_set->get_physics_layers_count(); tile_set_physics_layer++) {
  1381. Ref<PhysicsMaterial> physics_material = tile_set->get_physics_layer_physics_material(tile_set_physics_layer);
  1382. uint32_t physics_layer = tile_set->get_physics_layer_collision_layer(tile_set_physics_layer);
  1383. uint32_t physics_mask = tile_set->get_physics_layer_collision_mask(tile_set_physics_layer);
  1384. // Create the body.
  1385. RID body = ps->body_create();
  1386. bodies_coords[body] = E_cell;
  1387. ps->body_set_mode(body, collision_animatable ? PhysicsServer2D::BODY_MODE_KINEMATIC : PhysicsServer2D::BODY_MODE_STATIC);
  1388. ps->body_set_space(body, space);
  1389. Transform2D xform;
  1390. xform.set_origin(map_to_local(E_cell));
  1391. xform = gl_transform * xform;
  1392. ps->body_set_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM, xform);
  1393. ps->body_attach_object_instance_id(body, get_instance_id());
  1394. ps->body_set_collision_layer(body, physics_layer);
  1395. ps->body_set_collision_mask(body, physics_mask);
  1396. ps->body_set_pickable(body, false);
  1397. ps->body_set_state(body, PhysicsServer2D::BODY_STATE_LINEAR_VELOCITY, tile_data->get_constant_linear_velocity(tile_set_physics_layer));
  1398. ps->body_set_state(body, PhysicsServer2D::BODY_STATE_ANGULAR_VELOCITY, tile_data->get_constant_angular_velocity(tile_set_physics_layer));
  1399. if (!physics_material.is_valid()) {
  1400. ps->body_set_param(body, PhysicsServer2D::BODY_PARAM_BOUNCE, 0);
  1401. ps->body_set_param(body, PhysicsServer2D::BODY_PARAM_FRICTION, 1);
  1402. } else {
  1403. ps->body_set_param(body, PhysicsServer2D::BODY_PARAM_BOUNCE, physics_material->computed_bounce());
  1404. ps->body_set_param(body, PhysicsServer2D::BODY_PARAM_FRICTION, physics_material->computed_friction());
  1405. }
  1406. q.bodies.push_back(body);
  1407. // Add the shapes to the body.
  1408. int body_shape_index = 0;
  1409. for (int polygon_index = 0; polygon_index < tile_data->get_collision_polygons_count(tile_set_physics_layer); polygon_index++) {
  1410. // Iterate over the polygons.
  1411. bool one_way_collision = tile_data->is_collision_polygon_one_way(tile_set_physics_layer, polygon_index);
  1412. float one_way_collision_margin = tile_data->get_collision_polygon_one_way_margin(tile_set_physics_layer, polygon_index);
  1413. int shapes_count = tile_data->get_collision_polygon_shapes_count(tile_set_physics_layer, polygon_index);
  1414. for (int shape_index = 0; shape_index < shapes_count; shape_index++) {
  1415. // Add decomposed convex shapes.
  1416. Ref<ConvexPolygonShape2D> shape = tile_data->get_collision_polygon_shape(tile_set_physics_layer, polygon_index, shape_index);
  1417. ps->body_add_shape(body, shape->get_rid());
  1418. ps->body_set_shape_as_one_way_collision(body, body_shape_index, one_way_collision, one_way_collision_margin);
  1419. body_shape_index++;
  1420. }
  1421. }
  1422. }
  1423. }
  1424. }
  1425. }
  1426. q_list_element = q_list_element->next();
  1427. }
  1428. }
  1429. void TileMap::_physics_cleanup_quadrant(TileMapQuadrant *p_quadrant) {
  1430. // Remove a quadrant.
  1431. ERR_FAIL_NULL(PhysicsServer2D::get_singleton());
  1432. for (RID body : p_quadrant->bodies) {
  1433. bodies_coords.erase(body);
  1434. PhysicsServer2D::get_singleton()->free(body);
  1435. }
  1436. p_quadrant->bodies.clear();
  1437. }
  1438. void TileMap::_physics_draw_quadrant_debug(TileMapQuadrant *p_quadrant) {
  1439. // Draw the debug collision shapes.
  1440. ERR_FAIL_COND(!tile_set.is_valid());
  1441. if (!get_tree()) {
  1442. return;
  1443. }
  1444. bool show_collision = false;
  1445. switch (collision_visibility_mode) {
  1446. case TileMap::VISIBILITY_MODE_DEFAULT:
  1447. show_collision = !Engine::get_singleton()->is_editor_hint() && (get_tree() && get_tree()->is_debugging_collisions_hint());
  1448. break;
  1449. case TileMap::VISIBILITY_MODE_FORCE_HIDE:
  1450. show_collision = false;
  1451. break;
  1452. case TileMap::VISIBILITY_MODE_FORCE_SHOW:
  1453. show_collision = true;
  1454. break;
  1455. }
  1456. if (!show_collision) {
  1457. return;
  1458. }
  1459. RenderingServer *rs = RenderingServer::get_singleton();
  1460. PhysicsServer2D *ps = PhysicsServer2D::get_singleton();
  1461. Color debug_collision_color = get_tree()->get_debug_collisions_color();
  1462. Vector<Color> color;
  1463. color.push_back(debug_collision_color);
  1464. Vector2 quadrant_pos = map_to_local(p_quadrant->coords * get_effective_quadrant_size(p_quadrant->layer));
  1465. Transform2D quadrant_to_local;
  1466. quadrant_to_local.set_origin(quadrant_pos);
  1467. Transform2D global_to_quadrant = (get_global_transform() * quadrant_to_local).affine_inverse();
  1468. for (RID body : p_quadrant->bodies) {
  1469. Transform2D body_to_quadrant = global_to_quadrant * Transform2D(ps->body_get_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM));
  1470. rs->canvas_item_add_set_transform(p_quadrant->debug_canvas_item, body_to_quadrant);
  1471. for (int shape_index = 0; shape_index < ps->body_get_shape_count(body); shape_index++) {
  1472. const RID &shape = ps->body_get_shape(body, shape_index);
  1473. PhysicsServer2D::ShapeType type = ps->shape_get_type(shape);
  1474. if (type == PhysicsServer2D::SHAPE_CONVEX_POLYGON) {
  1475. Vector<Vector2> polygon = ps->shape_get_data(shape);
  1476. rs->canvas_item_add_polygon(p_quadrant->debug_canvas_item, polygon, color);
  1477. } else {
  1478. WARN_PRINT("Wrong shape type for a tile, should be SHAPE_CONVEX_POLYGON.");
  1479. }
  1480. }
  1481. rs->canvas_item_add_set_transform(p_quadrant->debug_canvas_item, Transform2D());
  1482. }
  1483. };
  1484. /////////////////////////////// Navigation //////////////////////////////////////
  1485. void TileMap::_navigation_notification(int p_what) {
  1486. switch (p_what) {
  1487. case NOTIFICATION_TRANSFORM_CHANGED: {
  1488. if (is_inside_tree()) {
  1489. for (TileMapLayer &layer : layers) {
  1490. Transform2D tilemap_xform = get_global_transform();
  1491. for (KeyValue<Vector2i, TileMapQuadrant> &E_quadrant : layer.quadrant_map) {
  1492. TileMapQuadrant &q = E_quadrant.value;
  1493. for (const KeyValue<Vector2i, Vector<RID>> &E_region : q.navigation_regions) {
  1494. for (const RID &region : E_region.value) {
  1495. if (!region.is_valid()) {
  1496. continue;
  1497. }
  1498. Transform2D tile_transform;
  1499. tile_transform.set_origin(map_to_local(E_region.key));
  1500. NavigationServer2D::get_singleton()->region_set_transform(region, tilemap_xform * tile_transform);
  1501. }
  1502. }
  1503. }
  1504. }
  1505. }
  1506. } break;
  1507. }
  1508. }
  1509. void TileMap::_navigation_update_dirty_quadrants(SelfList<TileMapQuadrant>::List &r_dirty_quadrant_list) {
  1510. ERR_FAIL_COND(!is_inside_tree());
  1511. ERR_FAIL_COND(!tile_set.is_valid());
  1512. Transform2D tilemap_xform = get_global_transform();
  1513. SelfList<TileMapQuadrant> *q_list_element = r_dirty_quadrant_list.first();
  1514. while (q_list_element) {
  1515. TileMapQuadrant &q = *q_list_element->self();
  1516. // Clear navigation shapes in the quadrant.
  1517. for (const KeyValue<Vector2i, Vector<RID>> &E : q.navigation_regions) {
  1518. for (int i = 0; i < E.value.size(); i++) {
  1519. RID region = E.value[i];
  1520. if (!region.is_valid()) {
  1521. continue;
  1522. }
  1523. NavigationServer2D::get_singleton()->region_set_map(region, RID());
  1524. }
  1525. }
  1526. q.navigation_regions.clear();
  1527. // Get the navigation polygons and create regions.
  1528. for (const Vector2i &E_cell : q.cells) {
  1529. TileMapCell c = get_cell(q.layer, E_cell, true);
  1530. TileSetSource *source;
  1531. if (tile_set->has_source(c.source_id)) {
  1532. source = *tile_set->get_source(c.source_id);
  1533. if (!source->has_tile(c.get_atlas_coords()) || !source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  1534. continue;
  1535. }
  1536. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  1537. if (atlas_source) {
  1538. const TileData *tile_data;
  1539. if (q.runtime_tile_data_cache.has(E_cell)) {
  1540. tile_data = q.runtime_tile_data_cache[E_cell];
  1541. } else {
  1542. tile_data = atlas_source->get_tile_data(c.get_atlas_coords(), c.alternative_tile);
  1543. }
  1544. q.navigation_regions[E_cell].resize(tile_set->get_navigation_layers_count());
  1545. for (int layer_index = 0; layer_index < tile_set->get_navigation_layers_count(); layer_index++) {
  1546. if (layer_index >= (int)layers.size() || !layers[layer_index].navigation_map.is_valid()) {
  1547. continue;
  1548. }
  1549. Ref<NavigationPolygon> navigation_polygon;
  1550. navigation_polygon = tile_data->get_navigation_polygon(layer_index);
  1551. if (navigation_polygon.is_valid()) {
  1552. Transform2D tile_transform;
  1553. tile_transform.set_origin(map_to_local(E_cell));
  1554. RID region = NavigationServer2D::get_singleton()->region_create();
  1555. NavigationServer2D::get_singleton()->region_set_owner_id(region, get_instance_id());
  1556. NavigationServer2D::get_singleton()->region_set_map(region, layers[layer_index].navigation_map);
  1557. NavigationServer2D::get_singleton()->region_set_transform(region, tilemap_xform * tile_transform);
  1558. NavigationServer2D::get_singleton()->region_set_navigation_layers(region, tile_set->get_navigation_layer_layers(layer_index));
  1559. NavigationServer2D::get_singleton()->region_set_navigation_polygon(region, navigation_polygon);
  1560. q.navigation_regions[E_cell].write[layer_index] = region;
  1561. }
  1562. }
  1563. }
  1564. }
  1565. }
  1566. q_list_element = q_list_element->next();
  1567. }
  1568. }
  1569. void TileMap::_navigation_cleanup_quadrant(TileMapQuadrant *p_quadrant) {
  1570. // Clear navigation shapes in the quadrant.
  1571. ERR_FAIL_NULL(NavigationServer2D::get_singleton());
  1572. for (const KeyValue<Vector2i, Vector<RID>> &E : p_quadrant->navigation_regions) {
  1573. for (int i = 0; i < E.value.size(); i++) {
  1574. RID region = E.value[i];
  1575. if (!region.is_valid()) {
  1576. continue;
  1577. }
  1578. NavigationServer2D::get_singleton()->free(region);
  1579. }
  1580. }
  1581. p_quadrant->navigation_regions.clear();
  1582. }
  1583. void TileMap::_navigation_draw_quadrant_debug(TileMapQuadrant *p_quadrant) {
  1584. // Draw the debug collision shapes.
  1585. ERR_FAIL_COND(!tile_set.is_valid());
  1586. if (!get_tree()) {
  1587. return;
  1588. }
  1589. bool show_navigation = false;
  1590. switch (navigation_visibility_mode) {
  1591. case TileMap::VISIBILITY_MODE_DEFAULT:
  1592. show_navigation = !Engine::get_singleton()->is_editor_hint() && (get_tree() && get_tree()->is_debugging_navigation_hint());
  1593. break;
  1594. case TileMap::VISIBILITY_MODE_FORCE_HIDE:
  1595. show_navigation = false;
  1596. break;
  1597. case TileMap::VISIBILITY_MODE_FORCE_SHOW:
  1598. show_navigation = true;
  1599. break;
  1600. }
  1601. if (!show_navigation) {
  1602. return;
  1603. }
  1604. #ifdef DEBUG_ENABLED
  1605. RenderingServer *rs = RenderingServer::get_singleton();
  1606. const NavigationServer2D *ns2d = NavigationServer2D::get_singleton();
  1607. bool enabled_geometry_face_random_color = ns2d->get_debug_navigation_enable_geometry_face_random_color();
  1608. bool enabled_edge_lines = ns2d->get_debug_navigation_enable_edge_lines();
  1609. Color debug_face_color = ns2d->get_debug_navigation_geometry_face_color();
  1610. Color debug_edge_color = ns2d->get_debug_navigation_geometry_edge_color();
  1611. RandomPCG rand;
  1612. Vector2 quadrant_pos = map_to_local(p_quadrant->coords * get_effective_quadrant_size(p_quadrant->layer));
  1613. for (const Vector2i &E_cell : p_quadrant->cells) {
  1614. TileMapCell c = get_cell(p_quadrant->layer, E_cell, true);
  1615. TileSetSource *source;
  1616. if (tile_set->has_source(c.source_id)) {
  1617. source = *tile_set->get_source(c.source_id);
  1618. if (!source->has_tile(c.get_atlas_coords()) || !source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  1619. continue;
  1620. }
  1621. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  1622. if (atlas_source) {
  1623. const TileData *tile_data;
  1624. if (p_quadrant->runtime_tile_data_cache.has(E_cell)) {
  1625. tile_data = p_quadrant->runtime_tile_data_cache[E_cell];
  1626. } else {
  1627. tile_data = atlas_source->get_tile_data(c.get_atlas_coords(), c.alternative_tile);
  1628. }
  1629. Transform2D cell_to_quadrant;
  1630. cell_to_quadrant.set_origin(map_to_local(E_cell) - quadrant_pos);
  1631. rs->canvas_item_add_set_transform(p_quadrant->debug_canvas_item, cell_to_quadrant);
  1632. for (int layer_index = 0; layer_index < tile_set->get_navigation_layers_count(); layer_index++) {
  1633. Ref<NavigationPolygon> navigation_polygon = tile_data->get_navigation_polygon(layer_index);
  1634. if (navigation_polygon.is_valid()) {
  1635. Vector<Vector2> navigation_polygon_vertices = navigation_polygon->get_vertices();
  1636. if (navigation_polygon_vertices.size() < 3) {
  1637. continue;
  1638. }
  1639. for (int i = 0; i < navigation_polygon->get_polygon_count(); i++) {
  1640. // An array of vertices for this polygon.
  1641. Vector<int> polygon = navigation_polygon->get_polygon(i);
  1642. Vector<Vector2> debug_polygon_vertices;
  1643. debug_polygon_vertices.resize(polygon.size());
  1644. for (int j = 0; j < polygon.size(); j++) {
  1645. ERR_FAIL_INDEX(polygon[j], navigation_polygon_vertices.size());
  1646. debug_polygon_vertices.write[j] = navigation_polygon_vertices[polygon[j]];
  1647. }
  1648. // Generate the polygon color, slightly randomly modified from the settings one.
  1649. Color random_variation_color = debug_face_color;
  1650. if (enabled_geometry_face_random_color) {
  1651. random_variation_color.set_hsv(
  1652. debug_face_color.get_h() + rand.random(-1.0, 1.0) * 0.1,
  1653. debug_face_color.get_s(),
  1654. debug_face_color.get_v() + rand.random(-1.0, 1.0) * 0.2);
  1655. }
  1656. random_variation_color.a = debug_face_color.a;
  1657. Vector<Color> debug_face_colors;
  1658. debug_face_colors.push_back(random_variation_color);
  1659. rs->canvas_item_add_polygon(p_quadrant->debug_canvas_item, debug_polygon_vertices, debug_face_colors);
  1660. if (enabled_edge_lines) {
  1661. Vector<Color> debug_edge_colors;
  1662. debug_edge_colors.push_back(debug_edge_color);
  1663. debug_polygon_vertices.push_back(debug_polygon_vertices[0]); // Add first again for closing polyline.
  1664. rs->canvas_item_add_polyline(p_quadrant->debug_canvas_item, debug_polygon_vertices, debug_edge_colors);
  1665. }
  1666. }
  1667. }
  1668. }
  1669. }
  1670. }
  1671. }
  1672. #endif // DEBUG_ENABLED
  1673. }
  1674. /////////////////////////////// Scenes //////////////////////////////////////
  1675. void TileMap::_scenes_update_dirty_quadrants(SelfList<TileMapQuadrant>::List &r_dirty_quadrant_list) {
  1676. ERR_FAIL_COND(!tile_set.is_valid());
  1677. SelfList<TileMapQuadrant> *q_list_element = r_dirty_quadrant_list.first();
  1678. while (q_list_element) {
  1679. TileMapQuadrant &q = *q_list_element->self();
  1680. // Clear the scenes if instance cache was cleared.
  1681. if (instantiated_scenes.is_empty()) {
  1682. for (const KeyValue<Vector2i, String> &E : q.scenes) {
  1683. Node *node = get_node_or_null(E.value);
  1684. if (node) {
  1685. node->queue_free();
  1686. }
  1687. }
  1688. }
  1689. q.scenes.clear();
  1690. // Recreate the scenes.
  1691. for (const Vector2i &E_cell : q.cells) {
  1692. Vector3i cell_coords = Vector3i(q.layer, E_cell.x, E_cell.y);
  1693. if (instantiated_scenes.has(cell_coords)) {
  1694. // Skip scene if the instance was cached (to avoid recreating scenes unnecessarily).
  1695. continue;
  1696. }
  1697. if (!Engine::get_singleton()->is_editor_hint()) {
  1698. instantiated_scenes.insert(cell_coords);
  1699. }
  1700. const TileMapCell &c = get_cell(q.layer, E_cell, true);
  1701. TileSetSource *source;
  1702. if (tile_set->has_source(c.source_id)) {
  1703. source = *tile_set->get_source(c.source_id);
  1704. if (!source->has_tile(c.get_atlas_coords()) || !source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  1705. continue;
  1706. }
  1707. TileSetScenesCollectionSource *scenes_collection_source = Object::cast_to<TileSetScenesCollectionSource>(source);
  1708. if (scenes_collection_source) {
  1709. Ref<PackedScene> packed_scene = scenes_collection_source->get_scene_tile_scene(c.alternative_tile);
  1710. if (packed_scene.is_valid()) {
  1711. Node *scene = packed_scene->instantiate();
  1712. Control *scene_as_control = Object::cast_to<Control>(scene);
  1713. Node2D *scene_as_node2d = Object::cast_to<Node2D>(scene);
  1714. if (scene_as_control) {
  1715. scene_as_control->set_position(map_to_local(E_cell) + scene_as_control->get_position());
  1716. } else if (scene_as_node2d) {
  1717. Transform2D xform;
  1718. xform.set_origin(map_to_local(E_cell));
  1719. scene_as_node2d->set_transform(xform * scene_as_node2d->get_transform());
  1720. }
  1721. add_child(scene);
  1722. q.scenes[E_cell] = scene->get_name();
  1723. }
  1724. }
  1725. }
  1726. }
  1727. q_list_element = q_list_element->next();
  1728. }
  1729. }
  1730. void TileMap::_scenes_cleanup_quadrant(TileMapQuadrant *p_quadrant) {
  1731. // Clear the scenes if instance cache was cleared.
  1732. if (instantiated_scenes.is_empty()) {
  1733. for (const KeyValue<Vector2i, String> &E : p_quadrant->scenes) {
  1734. Node *node = get_node_or_null(E.value);
  1735. if (node) {
  1736. node->queue_free();
  1737. }
  1738. }
  1739. p_quadrant->scenes.clear();
  1740. }
  1741. }
  1742. void TileMap::_scenes_draw_quadrant_debug(TileMapQuadrant *p_quadrant) {
  1743. ERR_FAIL_COND(!tile_set.is_valid());
  1744. if (!Engine::get_singleton()->is_editor_hint()) {
  1745. return;
  1746. }
  1747. // Draw a placeholder for scenes needing one.
  1748. RenderingServer *rs = RenderingServer::get_singleton();
  1749. Vector2 quadrant_pos = map_to_local(p_quadrant->coords * get_effective_quadrant_size(p_quadrant->layer));
  1750. for (const Vector2i &E_cell : p_quadrant->cells) {
  1751. const TileMapCell &c = get_cell(p_quadrant->layer, E_cell, true);
  1752. TileSetSource *source;
  1753. if (tile_set->has_source(c.source_id)) {
  1754. source = *tile_set->get_source(c.source_id);
  1755. if (!source->has_tile(c.get_atlas_coords()) || !source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  1756. continue;
  1757. }
  1758. TileSetScenesCollectionSource *scenes_collection_source = Object::cast_to<TileSetScenesCollectionSource>(source);
  1759. if (scenes_collection_source) {
  1760. if (!scenes_collection_source->get_scene_tile_scene(c.alternative_tile).is_valid() || scenes_collection_source->get_scene_tile_display_placeholder(c.alternative_tile)) {
  1761. // Generate a random color from the hashed values of the tiles.
  1762. Array to_hash;
  1763. to_hash.push_back(c.source_id);
  1764. to_hash.push_back(c.alternative_tile);
  1765. uint32_t hash = RandomPCG(to_hash.hash()).rand();
  1766. Color color;
  1767. color = color.from_hsv(
  1768. (float)((hash >> 24) & 0xFF) / 256.0,
  1769. Math::lerp(0.5, 1.0, (float)((hash >> 16) & 0xFF) / 256.0),
  1770. Math::lerp(0.5, 1.0, (float)((hash >> 8) & 0xFF) / 256.0),
  1771. 0.8);
  1772. // Draw a placeholder tile.
  1773. Transform2D cell_to_quadrant;
  1774. cell_to_quadrant.set_origin(map_to_local(E_cell) - quadrant_pos);
  1775. rs->canvas_item_add_set_transform(p_quadrant->debug_canvas_item, cell_to_quadrant);
  1776. rs->canvas_item_add_circle(p_quadrant->debug_canvas_item, Vector2(), MIN(tile_set->get_tile_size().x, tile_set->get_tile_size().y) / 4.0, color);
  1777. }
  1778. }
  1779. }
  1780. }
  1781. }
  1782. void TileMap::set_cell(int p_layer, const Vector2i &p_coords, int p_source_id, const Vector2i p_atlas_coords, int p_alternative_tile) {
  1783. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  1784. // Set the current cell tile (using integer position).
  1785. HashMap<Vector2i, TileMapCell> &tile_map = layers[p_layer].tile_map;
  1786. Vector2i pk(p_coords);
  1787. HashMap<Vector2i, TileMapCell>::Iterator E = tile_map.find(pk);
  1788. int source_id = p_source_id;
  1789. Vector2i atlas_coords = p_atlas_coords;
  1790. int alternative_tile = p_alternative_tile;
  1791. if ((source_id == TileSet::INVALID_SOURCE || atlas_coords == TileSetSource::INVALID_ATLAS_COORDS || alternative_tile == TileSetSource::INVALID_TILE_ALTERNATIVE) &&
  1792. (source_id != TileSet::INVALID_SOURCE || atlas_coords != TileSetSource::INVALID_ATLAS_COORDS || alternative_tile != TileSetSource::INVALID_TILE_ALTERNATIVE)) {
  1793. source_id = TileSet::INVALID_SOURCE;
  1794. atlas_coords = TileSetSource::INVALID_ATLAS_COORDS;
  1795. alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE;
  1796. }
  1797. if (!E && source_id == TileSet::INVALID_SOURCE) {
  1798. return; // Nothing to do, the tile is already empty.
  1799. }
  1800. // Get the quadrant
  1801. Vector2i qk = _coords_to_quadrant_coords(p_layer, pk);
  1802. HashMap<Vector2i, TileMapQuadrant>::Iterator Q = layers[p_layer].quadrant_map.find(qk);
  1803. if (source_id == TileSet::INVALID_SOURCE) {
  1804. // Erase existing cell in the tile map.
  1805. tile_map.erase(pk);
  1806. // Erase existing cell in the quadrant.
  1807. ERR_FAIL_COND(!Q);
  1808. TileMapQuadrant &q = Q->value;
  1809. q.cells.erase(pk);
  1810. // Remove or make the quadrant dirty.
  1811. if (q.cells.size() == 0) {
  1812. _erase_quadrant(Q);
  1813. } else {
  1814. _make_quadrant_dirty(Q);
  1815. }
  1816. used_rect_cache_dirty = true;
  1817. } else {
  1818. if (!E) {
  1819. // Insert a new cell in the tile map.
  1820. E = tile_map.insert(pk, TileMapCell());
  1821. // Create a new quadrant if needed, then insert the cell if needed.
  1822. if (!Q) {
  1823. Q = _create_quadrant(p_layer, qk);
  1824. }
  1825. TileMapQuadrant &q = Q->value;
  1826. q.cells.insert(pk);
  1827. } else {
  1828. ERR_FAIL_COND(!Q); // TileMapQuadrant should exist...
  1829. if (E->value.source_id == source_id && E->value.get_atlas_coords() == atlas_coords && E->value.alternative_tile == alternative_tile) {
  1830. return; // Nothing changed.
  1831. }
  1832. }
  1833. TileMapCell &c = E->value;
  1834. c.source_id = source_id;
  1835. c.set_atlas_coords(atlas_coords);
  1836. c.alternative_tile = alternative_tile;
  1837. _make_quadrant_dirty(Q);
  1838. used_rect_cache_dirty = true;
  1839. }
  1840. }
  1841. void TileMap::erase_cell(int p_layer, const Vector2i &p_coords) {
  1842. set_cell(p_layer, p_coords, TileSet::INVALID_SOURCE, TileSetSource::INVALID_ATLAS_COORDS, TileSetSource::INVALID_TILE_ALTERNATIVE);
  1843. }
  1844. int TileMap::get_cell_source_id(int p_layer, const Vector2i &p_coords, bool p_use_proxies) const {
  1845. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), TileSet::INVALID_SOURCE);
  1846. // Get a cell source id from position.
  1847. const HashMap<Vector2i, TileMapCell> &tile_map = layers[p_layer].tile_map;
  1848. HashMap<Vector2i, TileMapCell>::ConstIterator E = tile_map.find(p_coords);
  1849. if (!E) {
  1850. return TileSet::INVALID_SOURCE;
  1851. }
  1852. if (p_use_proxies && tile_set.is_valid()) {
  1853. Array proxyed = tile_set->map_tile_proxy(E->value.source_id, E->value.get_atlas_coords(), E->value.alternative_tile);
  1854. return proxyed[0];
  1855. }
  1856. return E->value.source_id;
  1857. }
  1858. Vector2i TileMap::get_cell_atlas_coords(int p_layer, const Vector2i &p_coords, bool p_use_proxies) const {
  1859. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), TileSetSource::INVALID_ATLAS_COORDS);
  1860. // Get a cell source id from position
  1861. const HashMap<Vector2i, TileMapCell> &tile_map = layers[p_layer].tile_map;
  1862. HashMap<Vector2i, TileMapCell>::ConstIterator E = tile_map.find(p_coords);
  1863. if (!E) {
  1864. return TileSetSource::INVALID_ATLAS_COORDS;
  1865. }
  1866. if (p_use_proxies && tile_set.is_valid()) {
  1867. Array proxyed = tile_set->map_tile_proxy(E->value.source_id, E->value.get_atlas_coords(), E->value.alternative_tile);
  1868. return proxyed[1];
  1869. }
  1870. return E->value.get_atlas_coords();
  1871. }
  1872. int TileMap::get_cell_alternative_tile(int p_layer, const Vector2i &p_coords, bool p_use_proxies) const {
  1873. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), TileSetSource::INVALID_TILE_ALTERNATIVE);
  1874. // Get a cell source id from position
  1875. const HashMap<Vector2i, TileMapCell> &tile_map = layers[p_layer].tile_map;
  1876. HashMap<Vector2i, TileMapCell>::ConstIterator E = tile_map.find(p_coords);
  1877. if (!E) {
  1878. return TileSetSource::INVALID_TILE_ALTERNATIVE;
  1879. }
  1880. if (p_use_proxies && tile_set.is_valid()) {
  1881. Array proxyed = tile_set->map_tile_proxy(E->value.source_id, E->value.get_atlas_coords(), E->value.alternative_tile);
  1882. return proxyed[2];
  1883. }
  1884. return E->value.alternative_tile;
  1885. }
  1886. TileData *TileMap::get_cell_tile_data(int p_layer, const Vector2i &p_coords, bool p_use_proxies) const {
  1887. int source_id = get_cell_source_id(p_layer, p_coords, p_use_proxies);
  1888. if (source_id == TileSet::INVALID_SOURCE) {
  1889. return nullptr;
  1890. }
  1891. Ref<TileSetAtlasSource> source = tile_set->get_source(source_id);
  1892. if (source.is_valid()) {
  1893. return source->get_tile_data(get_cell_atlas_coords(p_layer, p_coords, p_use_proxies), get_cell_alternative_tile(p_layer, p_coords, p_use_proxies));
  1894. }
  1895. return nullptr;
  1896. }
  1897. Ref<TileMapPattern> TileMap::get_pattern(int p_layer, TypedArray<Vector2i> p_coords_array) {
  1898. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), nullptr);
  1899. ERR_FAIL_COND_V(!tile_set.is_valid(), nullptr);
  1900. Ref<TileMapPattern> output;
  1901. output.instantiate();
  1902. if (p_coords_array.is_empty()) {
  1903. return output;
  1904. }
  1905. Vector2i min = Vector2i(p_coords_array[0]);
  1906. for (int i = 1; i < p_coords_array.size(); i++) {
  1907. min = min.min(p_coords_array[i]);
  1908. }
  1909. Vector<Vector2i> coords_in_pattern_array;
  1910. coords_in_pattern_array.resize(p_coords_array.size());
  1911. Vector2i ensure_positive_offset;
  1912. for (int i = 0; i < p_coords_array.size(); i++) {
  1913. Vector2i coords = p_coords_array[i];
  1914. Vector2i coords_in_pattern = coords - min;
  1915. if (tile_set->get_tile_shape() != TileSet::TILE_SHAPE_SQUARE) {
  1916. if (tile_set->get_tile_layout() == TileSet::TILE_LAYOUT_STACKED) {
  1917. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL && bool(min.y % 2) && bool(coords_in_pattern.y % 2)) {
  1918. coords_in_pattern.x -= 1;
  1919. if (coords_in_pattern.x < 0) {
  1920. ensure_positive_offset.x = 1;
  1921. }
  1922. } else if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_VERTICAL && bool(min.x % 2) && bool(coords_in_pattern.x % 2)) {
  1923. coords_in_pattern.y -= 1;
  1924. if (coords_in_pattern.y < 0) {
  1925. ensure_positive_offset.y = 1;
  1926. }
  1927. }
  1928. } else if (tile_set->get_tile_layout() == TileSet::TILE_LAYOUT_STACKED_OFFSET) {
  1929. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL && bool(min.y % 2) && bool(coords_in_pattern.y % 2)) {
  1930. coords_in_pattern.x += 1;
  1931. } else if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_VERTICAL && bool(min.x % 2) && bool(coords_in_pattern.x % 2)) {
  1932. coords_in_pattern.y += 1;
  1933. }
  1934. }
  1935. }
  1936. coords_in_pattern_array.write[i] = coords_in_pattern;
  1937. }
  1938. for (int i = 0; i < coords_in_pattern_array.size(); i++) {
  1939. Vector2i coords = p_coords_array[i];
  1940. Vector2i coords_in_pattern = coords_in_pattern_array[i];
  1941. output->set_cell(coords_in_pattern + ensure_positive_offset, get_cell_source_id(p_layer, coords), get_cell_atlas_coords(p_layer, coords), get_cell_alternative_tile(p_layer, coords));
  1942. }
  1943. return output;
  1944. }
  1945. Vector2i TileMap::map_pattern(const Vector2i &p_position_in_tilemap, const Vector2i &p_coords_in_pattern, Ref<TileMapPattern> p_pattern) {
  1946. ERR_FAIL_COND_V(p_pattern.is_null(), Vector2i());
  1947. ERR_FAIL_COND_V(!p_pattern->has_cell(p_coords_in_pattern), Vector2i());
  1948. Vector2i output = p_position_in_tilemap + p_coords_in_pattern;
  1949. if (tile_set->get_tile_shape() != TileSet::TILE_SHAPE_SQUARE) {
  1950. if (tile_set->get_tile_layout() == TileSet::TILE_LAYOUT_STACKED) {
  1951. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL && bool(p_position_in_tilemap.y % 2) && bool(p_coords_in_pattern.y % 2)) {
  1952. output.x += 1;
  1953. } else if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_VERTICAL && bool(p_position_in_tilemap.x % 2) && bool(p_coords_in_pattern.x % 2)) {
  1954. output.y += 1;
  1955. }
  1956. } else if (tile_set->get_tile_layout() == TileSet::TILE_LAYOUT_STACKED_OFFSET) {
  1957. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL && bool(p_position_in_tilemap.y % 2) && bool(p_coords_in_pattern.y % 2)) {
  1958. output.x -= 1;
  1959. } else if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_VERTICAL && bool(p_position_in_tilemap.x % 2) && bool(p_coords_in_pattern.x % 2)) {
  1960. output.y -= 1;
  1961. }
  1962. }
  1963. }
  1964. return output;
  1965. }
  1966. void TileMap::set_pattern(int p_layer, const Vector2i &p_position, const Ref<TileMapPattern> p_pattern) {
  1967. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  1968. ERR_FAIL_COND(!tile_set.is_valid());
  1969. TypedArray<Vector2i> used_cells = p_pattern->get_used_cells();
  1970. for (int i = 0; i < used_cells.size(); i++) {
  1971. Vector2i coords = map_pattern(p_position, used_cells[i], p_pattern);
  1972. set_cell(p_layer, 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]));
  1973. }
  1974. }
  1975. TileSet::TerrainsPattern TileMap::_get_best_terrain_pattern_for_constraints(int p_terrain_set, const Vector2i &p_position, const RBSet<TerrainConstraint> &p_constraints, TileSet::TerrainsPattern p_current_pattern) {
  1976. if (!tile_set.is_valid()) {
  1977. return TileSet::TerrainsPattern();
  1978. }
  1979. // Returns all tiles compatible with the given constraints.
  1980. RBMap<TileSet::TerrainsPattern, int> terrain_pattern_score;
  1981. RBSet<TileSet::TerrainsPattern> pattern_set = tile_set->get_terrains_pattern_set(p_terrain_set);
  1982. ERR_FAIL_COND_V(pattern_set.is_empty(), TileSet::TerrainsPattern());
  1983. for (TileSet::TerrainsPattern &terrain_pattern : pattern_set) {
  1984. int score = 0;
  1985. // Check the center bit constraint
  1986. TerrainConstraint terrain_constraint = TerrainConstraint(this, p_position, terrain_pattern.get_terrain());
  1987. const RBSet<TerrainConstraint>::Element *in_set_constraint_element = p_constraints.find(terrain_constraint);
  1988. if (in_set_constraint_element) {
  1989. if (in_set_constraint_element->get().get_terrain() != terrain_constraint.get_terrain()) {
  1990. score += in_set_constraint_element->get().get_priority();
  1991. }
  1992. } else if (p_current_pattern.get_terrain() != terrain_pattern.get_terrain()) {
  1993. continue; // Ignore a pattern that cannot keep bits without constraints unmodified.
  1994. }
  1995. // Check the surrounding bits
  1996. bool invalid_pattern = false;
  1997. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  1998. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  1999. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, bit)) {
  2000. // Check if the bit is compatible with the constraints.
  2001. TerrainConstraint terrain_bit_constraint = TerrainConstraint(this, p_position, bit, terrain_pattern.get_terrain_peering_bit(bit));
  2002. in_set_constraint_element = p_constraints.find(terrain_bit_constraint);
  2003. if (in_set_constraint_element) {
  2004. if (in_set_constraint_element->get().get_terrain() != terrain_bit_constraint.get_terrain()) {
  2005. score += in_set_constraint_element->get().get_priority();
  2006. }
  2007. } else if (p_current_pattern.get_terrain_peering_bit(bit) != terrain_pattern.get_terrain_peering_bit(bit)) {
  2008. invalid_pattern = true; // Ignore a pattern that cannot keep bits without constraints unmodified.
  2009. break;
  2010. }
  2011. }
  2012. }
  2013. if (invalid_pattern) {
  2014. continue;
  2015. }
  2016. terrain_pattern_score[terrain_pattern] = score;
  2017. }
  2018. // Compute the minimum score
  2019. TileSet::TerrainsPattern min_score_pattern = p_current_pattern;
  2020. int min_score = INT32_MAX;
  2021. for (KeyValue<TileSet::TerrainsPattern, int> E : terrain_pattern_score) {
  2022. if (E.value < min_score) {
  2023. min_score_pattern = E.key;
  2024. min_score = E.value;
  2025. }
  2026. }
  2027. return min_score_pattern;
  2028. }
  2029. RBSet<TileMap::TerrainConstraint> TileMap::_get_terrain_constraints_from_added_pattern(const Vector2i &p_position, int p_terrain_set, TileSet::TerrainsPattern p_terrains_pattern) const {
  2030. if (!tile_set.is_valid()) {
  2031. return RBSet<TerrainConstraint>();
  2032. }
  2033. // Compute the constraints needed from the surrounding tiles.
  2034. RBSet<TerrainConstraint> output;
  2035. output.insert(TerrainConstraint(this, p_position, p_terrains_pattern.get_terrain()));
  2036. for (uint32_t i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  2037. TileSet::CellNeighbor side = TileSet::CellNeighbor(i);
  2038. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, side)) {
  2039. TerrainConstraint c = TerrainConstraint(this, p_position, side, p_terrains_pattern.get_terrain_peering_bit(side));
  2040. output.insert(c);
  2041. }
  2042. }
  2043. return output;
  2044. }
  2045. RBSet<TileMap::TerrainConstraint> TileMap::_get_terrain_constraints_from_painted_cells_list(int p_layer, const RBSet<Vector2i> &p_painted, int p_terrain_set, bool p_ignore_empty_terrains) const {
  2046. if (!tile_set.is_valid()) {
  2047. return RBSet<TerrainConstraint>();
  2048. }
  2049. ERR_FAIL_INDEX_V(p_terrain_set, tile_set->get_terrain_sets_count(), RBSet<TerrainConstraint>());
  2050. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), RBSet<TerrainConstraint>());
  2051. // Build a set of dummy constraints to get the constrained points.
  2052. RBSet<TerrainConstraint> dummy_constraints;
  2053. for (const Vector2i &E : p_painted) {
  2054. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) { // Iterates over neighbor bits.
  2055. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  2056. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, bit)) {
  2057. dummy_constraints.insert(TerrainConstraint(this, E, bit, -1));
  2058. }
  2059. }
  2060. }
  2061. // For each constrained point, we get all overlapping tiles, and select the most adequate terrain for it.
  2062. RBSet<TerrainConstraint> constraints;
  2063. for (const TerrainConstraint &E_constraint : dummy_constraints) {
  2064. HashMap<int, int> terrain_count;
  2065. // Count the number of occurrences per terrain.
  2066. HashMap<Vector2i, TileSet::CellNeighbor> overlapping_terrain_bits = E_constraint.get_overlapping_coords_and_peering_bits();
  2067. for (const KeyValue<Vector2i, TileSet::CellNeighbor> &E_overlapping : overlapping_terrain_bits) {
  2068. TileData *neighbor_tile_data = nullptr;
  2069. TileMapCell neighbor_cell = get_cell(p_layer, E_overlapping.key);
  2070. if (neighbor_cell.source_id != TileSet::INVALID_SOURCE) {
  2071. Ref<TileSetSource> source = tile_set->get_source(neighbor_cell.source_id);
  2072. Ref<TileSetAtlasSource> atlas_source = source;
  2073. if (atlas_source.is_valid()) {
  2074. TileData *tile_data = atlas_source->get_tile_data(neighbor_cell.get_atlas_coords(), neighbor_cell.alternative_tile);
  2075. if (tile_data && tile_data->get_terrain_set() == p_terrain_set) {
  2076. neighbor_tile_data = tile_data;
  2077. }
  2078. }
  2079. }
  2080. int terrain = neighbor_tile_data ? neighbor_tile_data->get_terrain_peering_bit(TileSet::CellNeighbor(E_overlapping.value)) : -1;
  2081. if (!p_ignore_empty_terrains || terrain >= 0) {
  2082. if (!terrain_count.has(terrain)) {
  2083. terrain_count[terrain] = 0;
  2084. }
  2085. terrain_count[terrain] += 1;
  2086. }
  2087. }
  2088. // Get the terrain with the max number of occurrences.
  2089. int max = 0;
  2090. int max_terrain = -1;
  2091. for (const KeyValue<int, int> &E_terrain_count : terrain_count) {
  2092. if (E_terrain_count.value > max) {
  2093. max = E_terrain_count.value;
  2094. max_terrain = E_terrain_count.key;
  2095. }
  2096. }
  2097. // Set the adequate terrain.
  2098. if (max > 0) {
  2099. TerrainConstraint c = E_constraint;
  2100. c.set_terrain(max_terrain);
  2101. constraints.insert(c);
  2102. }
  2103. }
  2104. // Add the centers as constraints
  2105. for (Vector2i E_coords : p_painted) {
  2106. TileData *tile_data = nullptr;
  2107. TileMapCell cell = get_cell(p_layer, E_coords);
  2108. if (cell.source_id != TileSet::INVALID_SOURCE) {
  2109. Ref<TileSetSource> source = tile_set->get_source(cell.source_id);
  2110. Ref<TileSetAtlasSource> atlas_source = source;
  2111. if (atlas_source.is_valid()) {
  2112. tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  2113. }
  2114. }
  2115. int terrain = (tile_data && tile_data->get_terrain_set() == p_terrain_set) ? tile_data->get_terrain() : -1;
  2116. if (!p_ignore_empty_terrains || terrain >= 0) {
  2117. constraints.insert(TerrainConstraint(this, E_coords, terrain));
  2118. }
  2119. }
  2120. return constraints;
  2121. }
  2122. HashMap<Vector2i, TileSet::TerrainsPattern> TileMap::terrain_fill_constraints(int p_layer, const Vector<Vector2i> &p_to_replace, int p_terrain_set, const RBSet<TerrainConstraint> &p_constraints) {
  2123. if (!tile_set.is_valid()) {
  2124. return HashMap<Vector2i, TileSet::TerrainsPattern>();
  2125. }
  2126. // Copy the constraints set.
  2127. RBSet<TerrainConstraint> constraints = p_constraints;
  2128. // Output map.
  2129. HashMap<Vector2i, TileSet::TerrainsPattern> output;
  2130. // Add all positions to a set.
  2131. for (int i = 0; i < p_to_replace.size(); i++) {
  2132. const Vector2i &coords = p_to_replace[i];
  2133. // Select the best pattern for the given constraints
  2134. TileSet::TerrainsPattern current_pattern = TileSet::TerrainsPattern(*tile_set, p_terrain_set);
  2135. TileMapCell cell = get_cell(p_layer, coords);
  2136. if (cell.source_id != TileSet::INVALID_SOURCE) {
  2137. TileSetSource *source = *tile_set->get_source(cell.source_id);
  2138. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  2139. if (atlas_source) {
  2140. // Get tile data.
  2141. TileData *tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  2142. if (tile_data && tile_data->get_terrain_set() == p_terrain_set) {
  2143. current_pattern = tile_data->get_terrains_pattern();
  2144. }
  2145. }
  2146. }
  2147. TileSet::TerrainsPattern pattern = _get_best_terrain_pattern_for_constraints(p_terrain_set, coords, constraints, current_pattern);
  2148. // Update the constraint set with the new ones
  2149. RBSet<TerrainConstraint> new_constraints = _get_terrain_constraints_from_added_pattern(coords, p_terrain_set, pattern);
  2150. for (const TerrainConstraint &E_constraint : new_constraints) {
  2151. if (constraints.has(E_constraint)) {
  2152. constraints.erase(E_constraint);
  2153. }
  2154. TerrainConstraint c = E_constraint;
  2155. c.set_priority(5);
  2156. constraints.insert(c);
  2157. }
  2158. output[coords] = pattern;
  2159. }
  2160. return output;
  2161. }
  2162. HashMap<Vector2i, TileSet::TerrainsPattern> TileMap::terrain_fill_connect(int p_layer, const Vector<Vector2i> &p_coords_array, int p_terrain_set, int p_terrain, bool p_ignore_empty_terrains) {
  2163. HashMap<Vector2i, TileSet::TerrainsPattern> output;
  2164. ERR_FAIL_COND_V(!tile_set.is_valid(), output);
  2165. ERR_FAIL_INDEX_V(p_terrain_set, tile_set->get_terrain_sets_count(), output);
  2166. // Build list and set of tiles that can be modified (painted and their surroundings)
  2167. Vector<Vector2i> can_modify_list;
  2168. RBSet<Vector2i> can_modify_set;
  2169. RBSet<Vector2i> painted_set;
  2170. for (int i = p_coords_array.size() - 1; i >= 0; i--) {
  2171. const Vector2i &coords = p_coords_array[i];
  2172. can_modify_list.push_back(coords);
  2173. can_modify_set.insert(coords);
  2174. painted_set.insert(coords);
  2175. }
  2176. for (Vector2i coords : p_coords_array) {
  2177. // Find the adequate neighbor
  2178. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  2179. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  2180. if (is_existing_neighbor(bit)) {
  2181. Vector2i neighbor = get_neighbor_cell(coords, bit);
  2182. if (!can_modify_set.has(neighbor)) {
  2183. can_modify_list.push_back(neighbor);
  2184. can_modify_set.insert(neighbor);
  2185. }
  2186. }
  2187. }
  2188. }
  2189. // 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
  2190. RBSet<Vector2i> cells_with_terrain_center_bit;
  2191. for (Vector2i coords : can_modify_set) {
  2192. bool connect = false;
  2193. if (painted_set.has(coords)) {
  2194. connect = true;
  2195. } else {
  2196. // Get the center bit of the cell
  2197. TileData *tile_data = nullptr;
  2198. TileMapCell cell = get_cell(p_layer, coords);
  2199. if (cell.source_id != TileSet::INVALID_SOURCE) {
  2200. Ref<TileSetSource> source = tile_set->get_source(cell.source_id);
  2201. Ref<TileSetAtlasSource> atlas_source = source;
  2202. if (atlas_source.is_valid()) {
  2203. tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  2204. }
  2205. }
  2206. if (tile_data && tile_data->get_terrain_set() == p_terrain_set && tile_data->get_terrain() == p_terrain) {
  2207. connect = true;
  2208. }
  2209. }
  2210. if (connect) {
  2211. cells_with_terrain_center_bit.insert(coords);
  2212. }
  2213. }
  2214. RBSet<TerrainConstraint> constraints;
  2215. // Add new constraints from the path drawn.
  2216. for (Vector2i coords : p_coords_array) {
  2217. // Constraints on the center bit.
  2218. TerrainConstraint c = TerrainConstraint(this, coords, p_terrain);
  2219. c.set_priority(10);
  2220. constraints.insert(c);
  2221. // Constraints on the connecting bits.
  2222. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  2223. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  2224. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, bit)) {
  2225. c = TerrainConstraint(this, coords, bit, p_terrain);
  2226. c.set_priority(10);
  2227. if ((int(bit) % 2) == 0) {
  2228. // Side peering bits: add the constraint if the center is of the same terrain
  2229. Vector2i neighbor = get_neighbor_cell(coords, bit);
  2230. if (cells_with_terrain_center_bit.has(neighbor)) {
  2231. constraints.insert(c);
  2232. }
  2233. } else {
  2234. // Corner peering bits: add the constraint if all tiles on the constraint has the same center bit
  2235. HashMap<Vector2i, TileSet::CellNeighbor> overlapping_terrain_bits = c.get_overlapping_coords_and_peering_bits();
  2236. bool valid = true;
  2237. for (KeyValue<Vector2i, TileSet::CellNeighbor> kv : overlapping_terrain_bits) {
  2238. if (!cells_with_terrain_center_bit.has(kv.key)) {
  2239. valid = false;
  2240. break;
  2241. }
  2242. }
  2243. if (valid) {
  2244. constraints.insert(c);
  2245. }
  2246. }
  2247. }
  2248. }
  2249. }
  2250. // Fills in the constraint list from existing tiles.
  2251. for (TerrainConstraint c : _get_terrain_constraints_from_painted_cells_list(p_layer, painted_set, p_terrain_set, p_ignore_empty_terrains)) {
  2252. constraints.insert(c);
  2253. }
  2254. // Fill the terrains.
  2255. output = terrain_fill_constraints(p_layer, can_modify_list, p_terrain_set, constraints);
  2256. return output;
  2257. }
  2258. HashMap<Vector2i, TileSet::TerrainsPattern> TileMap::terrain_fill_path(int p_layer, const Vector<Vector2i> &p_path, int p_terrain_set, int p_terrain, bool p_ignore_empty_terrains) {
  2259. HashMap<Vector2i, TileSet::TerrainsPattern> output;
  2260. ERR_FAIL_COND_V(!tile_set.is_valid(), output);
  2261. ERR_FAIL_INDEX_V(p_terrain_set, tile_set->get_terrain_sets_count(), output);
  2262. // Make sure the path is correct and build the peering bit list while doing it.
  2263. Vector<TileSet::CellNeighbor> neighbor_list;
  2264. for (int i = 0; i < p_path.size() - 1; i++) {
  2265. // Find the adequate neighbor
  2266. TileSet::CellNeighbor found_bit = TileSet::CELL_NEIGHBOR_MAX;
  2267. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  2268. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  2269. if (is_existing_neighbor(bit)) {
  2270. if (get_neighbor_cell(p_path[i], bit) == p_path[i + 1]) {
  2271. found_bit = bit;
  2272. break;
  2273. }
  2274. }
  2275. }
  2276. 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_path[i + 1], p_path[i]));
  2277. neighbor_list.push_back(found_bit);
  2278. }
  2279. // Build list and set of tiles that can be modified (painted and their surroundings)
  2280. Vector<Vector2i> can_modify_list;
  2281. RBSet<Vector2i> can_modify_set;
  2282. RBSet<Vector2i> painted_set;
  2283. for (int i = p_path.size() - 1; i >= 0; i--) {
  2284. const Vector2i &coords = p_path[i];
  2285. can_modify_list.push_back(coords);
  2286. can_modify_set.insert(coords);
  2287. painted_set.insert(coords);
  2288. }
  2289. for (Vector2i coords : p_path) {
  2290. // Find the adequate neighbor
  2291. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  2292. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  2293. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, bit)) {
  2294. Vector2i neighbor = get_neighbor_cell(coords, bit);
  2295. if (!can_modify_set.has(neighbor)) {
  2296. can_modify_list.push_back(neighbor);
  2297. can_modify_set.insert(neighbor);
  2298. }
  2299. }
  2300. }
  2301. }
  2302. RBSet<TerrainConstraint> constraints;
  2303. // Add new constraints from the path drawn.
  2304. for (Vector2i coords : p_path) {
  2305. // Constraints on the center bit
  2306. TerrainConstraint c = TerrainConstraint(this, coords, p_terrain);
  2307. c.set_priority(10);
  2308. constraints.insert(c);
  2309. }
  2310. for (int i = 0; i < p_path.size() - 1; i++) {
  2311. // Constraints on the peering bits.
  2312. TerrainConstraint c = TerrainConstraint(this, p_path[i], neighbor_list[i], p_terrain);
  2313. c.set_priority(10);
  2314. constraints.insert(c);
  2315. }
  2316. // Fills in the constraint list from existing tiles.
  2317. for (TerrainConstraint c : _get_terrain_constraints_from_painted_cells_list(p_layer, painted_set, p_terrain_set, p_ignore_empty_terrains)) {
  2318. constraints.insert(c);
  2319. }
  2320. // Fill the terrains.
  2321. output = terrain_fill_constraints(p_layer, can_modify_list, p_terrain_set, constraints);
  2322. return output;
  2323. }
  2324. HashMap<Vector2i, TileSet::TerrainsPattern> TileMap::terrain_fill_pattern(int p_layer, const Vector<Vector2i> &p_coords_array, int p_terrain_set, TileSet::TerrainsPattern p_terrains_pattern, bool p_ignore_empty_terrains) {
  2325. HashMap<Vector2i, TileSet::TerrainsPattern> output;
  2326. ERR_FAIL_COND_V(!tile_set.is_valid(), output);
  2327. ERR_FAIL_INDEX_V(p_terrain_set, tile_set->get_terrain_sets_count(), output);
  2328. // Build list and set of tiles that can be modified (painted and their surroundings).
  2329. Vector<Vector2i> can_modify_list;
  2330. RBSet<Vector2i> can_modify_set;
  2331. RBSet<Vector2i> painted_set;
  2332. for (int i = p_coords_array.size() - 1; i >= 0; i--) {
  2333. const Vector2i &coords = p_coords_array[i];
  2334. can_modify_list.push_back(coords);
  2335. can_modify_set.insert(coords);
  2336. painted_set.insert(coords);
  2337. }
  2338. for (Vector2i coords : p_coords_array) {
  2339. // Find the adequate neighbor
  2340. for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) {
  2341. TileSet::CellNeighbor bit = TileSet::CellNeighbor(j);
  2342. if (tile_set->is_valid_terrain_peering_bit(p_terrain_set, bit)) {
  2343. Vector2i neighbor = get_neighbor_cell(coords, bit);
  2344. if (!can_modify_set.has(neighbor)) {
  2345. can_modify_list.push_back(neighbor);
  2346. can_modify_set.insert(neighbor);
  2347. }
  2348. }
  2349. }
  2350. }
  2351. // Add constraint by the new ones.
  2352. RBSet<TerrainConstraint> constraints;
  2353. // Add new constraints from the path drawn.
  2354. for (Vector2i coords : p_coords_array) {
  2355. // Constraints on the center bit
  2356. RBSet<TerrainConstraint> added_constraints = _get_terrain_constraints_from_added_pattern(coords, p_terrain_set, p_terrains_pattern);
  2357. for (TerrainConstraint c : added_constraints) {
  2358. c.set_priority(10);
  2359. constraints.insert(c);
  2360. }
  2361. }
  2362. // Fills in the constraint list from modified tiles border.
  2363. for (TerrainConstraint c : _get_terrain_constraints_from_painted_cells_list(p_layer, painted_set, p_terrain_set, p_ignore_empty_terrains)) {
  2364. constraints.insert(c);
  2365. }
  2366. // Fill the terrains.
  2367. output = terrain_fill_constraints(p_layer, can_modify_list, p_terrain_set, constraints);
  2368. return output;
  2369. }
  2370. void TileMap::set_cells_terrain_connect(int p_layer, TypedArray<Vector2i> p_cells, int p_terrain_set, int p_terrain, bool p_ignore_empty_terrains) {
  2371. ERR_FAIL_COND(!tile_set.is_valid());
  2372. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  2373. ERR_FAIL_INDEX(p_terrain_set, tile_set->get_terrain_sets_count());
  2374. Vector<Vector2i> cells_vector;
  2375. HashSet<Vector2i> painted_set;
  2376. for (int i = 0; i < p_cells.size(); i++) {
  2377. cells_vector.push_back(p_cells[i]);
  2378. painted_set.insert(p_cells[i]);
  2379. }
  2380. HashMap<Vector2i, TileSet::TerrainsPattern> terrain_fill_output = terrain_fill_connect(p_layer, cells_vector, p_terrain_set, p_terrain, p_ignore_empty_terrains);
  2381. for (const KeyValue<Vector2i, TileSet::TerrainsPattern> &kv : terrain_fill_output) {
  2382. if (painted_set.has(kv.key)) {
  2383. // Paint a random tile with the correct terrain for the painted path.
  2384. TileMapCell c = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value);
  2385. set_cell(p_layer, kv.key, c.source_id, c.get_atlas_coords(), c.alternative_tile);
  2386. } else {
  2387. // Avoids updating the painted path from the output if the new pattern is the same as before.
  2388. TileSet::TerrainsPattern in_map_terrain_pattern = TileSet::TerrainsPattern(*tile_set, p_terrain_set);
  2389. TileMapCell cell = get_cell(p_layer, kv.key);
  2390. if (cell.source_id != TileSet::INVALID_SOURCE) {
  2391. TileSetSource *source = *tile_set->get_source(cell.source_id);
  2392. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  2393. if (atlas_source) {
  2394. // Get tile data.
  2395. TileData *tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  2396. if (tile_data && tile_data->get_terrain_set() == p_terrain_set) {
  2397. in_map_terrain_pattern = tile_data->get_terrains_pattern();
  2398. }
  2399. }
  2400. }
  2401. if (in_map_terrain_pattern != kv.value) {
  2402. TileMapCell c = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value);
  2403. set_cell(p_layer, kv.key, c.source_id, c.get_atlas_coords(), c.alternative_tile);
  2404. }
  2405. }
  2406. }
  2407. }
  2408. void TileMap::set_cells_terrain_path(int p_layer, TypedArray<Vector2i> p_path, int p_terrain_set, int p_terrain, bool p_ignore_empty_terrains) {
  2409. ERR_FAIL_COND(!tile_set.is_valid());
  2410. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  2411. ERR_FAIL_INDEX(p_terrain_set, tile_set->get_terrain_sets_count());
  2412. Vector<Vector2i> vector_path;
  2413. HashSet<Vector2i> painted_set;
  2414. for (int i = 0; i < p_path.size(); i++) {
  2415. vector_path.push_back(p_path[i]);
  2416. painted_set.insert(p_path[i]);
  2417. }
  2418. HashMap<Vector2i, TileSet::TerrainsPattern> terrain_fill_output = terrain_fill_path(p_layer, vector_path, p_terrain_set, p_terrain, p_ignore_empty_terrains);
  2419. for (const KeyValue<Vector2i, TileSet::TerrainsPattern> &kv : terrain_fill_output) {
  2420. if (painted_set.has(kv.key)) {
  2421. // Paint a random tile with the correct terrain for the painted path.
  2422. TileMapCell c = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value);
  2423. set_cell(p_layer, kv.key, c.source_id, c.get_atlas_coords(), c.alternative_tile);
  2424. } else {
  2425. // Avoids updating the painted path from the output if the new pattern is the same as before.
  2426. TileSet::TerrainsPattern in_map_terrain_pattern = TileSet::TerrainsPattern(*tile_set, p_terrain_set);
  2427. TileMapCell cell = get_cell(p_layer, kv.key);
  2428. if (cell.source_id != TileSet::INVALID_SOURCE) {
  2429. TileSetSource *source = *tile_set->get_source(cell.source_id);
  2430. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  2431. if (atlas_source) {
  2432. // Get tile data.
  2433. TileData *tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  2434. if (tile_data && tile_data->get_terrain_set() == p_terrain_set) {
  2435. in_map_terrain_pattern = tile_data->get_terrains_pattern();
  2436. }
  2437. }
  2438. }
  2439. if (in_map_terrain_pattern != kv.value) {
  2440. TileMapCell c = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value);
  2441. set_cell(p_layer, kv.key, c.source_id, c.get_atlas_coords(), c.alternative_tile);
  2442. }
  2443. }
  2444. }
  2445. }
  2446. TileMapCell TileMap::get_cell(int p_layer, const Vector2i &p_coords, bool p_use_proxies) const {
  2447. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), TileMapCell());
  2448. const HashMap<Vector2i, TileMapCell> &tile_map = layers[p_layer].tile_map;
  2449. if (!tile_map.has(p_coords)) {
  2450. return TileMapCell();
  2451. } else {
  2452. TileMapCell c = tile_map.find(p_coords)->value;
  2453. if (p_use_proxies && tile_set.is_valid()) {
  2454. Array proxyed = tile_set->map_tile_proxy(c.source_id, c.get_atlas_coords(), c.alternative_tile);
  2455. c.source_id = proxyed[0];
  2456. c.set_atlas_coords(proxyed[1]);
  2457. c.alternative_tile = proxyed[2];
  2458. }
  2459. return c;
  2460. }
  2461. }
  2462. HashMap<Vector2i, TileMapQuadrant> *TileMap::get_quadrant_map(int p_layer) {
  2463. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), nullptr);
  2464. return &layers[p_layer].quadrant_map;
  2465. }
  2466. Vector2i TileMap::get_coords_for_body_rid(RID p_physics_body) {
  2467. ERR_FAIL_COND_V_MSG(!bodies_coords.has(p_physics_body), Vector2i(), vformat("No tiles for the given body RID %d.", p_physics_body));
  2468. return bodies_coords[p_physics_body];
  2469. }
  2470. void TileMap::fix_invalid_tiles() {
  2471. ERR_FAIL_COND_MSG(tile_set.is_null(), "Cannot fix invalid tiles if Tileset is not open.");
  2472. for (unsigned int i = 0; i < layers.size(); i++) {
  2473. const HashMap<Vector2i, TileMapCell> &tile_map = layers[i].tile_map;
  2474. RBSet<Vector2i> coords;
  2475. for (const KeyValue<Vector2i, TileMapCell> &E : tile_map) {
  2476. TileSetSource *source = *tile_set->get_source(E.value.source_id);
  2477. if (!source || !source->has_tile(E.value.get_atlas_coords()) || !source->has_alternative_tile(E.value.get_atlas_coords(), E.value.alternative_tile)) {
  2478. coords.insert(E.key);
  2479. }
  2480. }
  2481. for (const Vector2i &E : coords) {
  2482. set_cell(i, E, TileSet::INVALID_SOURCE, TileSetSource::INVALID_ATLAS_COORDS, TileSetSource::INVALID_TILE_ALTERNATIVE);
  2483. }
  2484. }
  2485. }
  2486. void TileMap::clear_layer(int p_layer) {
  2487. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  2488. // Remove all tiles.
  2489. _clear_layer_internals(p_layer);
  2490. layers[p_layer].tile_map.clear();
  2491. _recreate_layer_internals(p_layer);
  2492. used_rect_cache_dirty = true;
  2493. }
  2494. void TileMap::clear() {
  2495. // Remove all tiles.
  2496. _clear_internals();
  2497. for (TileMapLayer &layer : layers) {
  2498. layer.tile_map.clear();
  2499. }
  2500. _recreate_internals();
  2501. used_rect_cache_dirty = true;
  2502. }
  2503. void TileMap::force_update(int p_layer) {
  2504. if (p_layer >= 0) {
  2505. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  2506. _clear_layer_internals(p_layer);
  2507. _recreate_layer_internals(p_layer);
  2508. } else {
  2509. _clear_internals();
  2510. _recreate_internals();
  2511. }
  2512. }
  2513. void TileMap::_set_tile_data(int p_layer, const Vector<int> &p_data) {
  2514. ERR_FAIL_INDEX(p_layer, (int)layers.size());
  2515. ERR_FAIL_COND(format > FORMAT_3);
  2516. // Set data for a given tile from raw data.
  2517. int c = p_data.size();
  2518. const int *r = p_data.ptr();
  2519. int offset = (format >= FORMAT_2) ? 3 : 2;
  2520. ERR_FAIL_COND_MSG(c % offset != 0, vformat("Corrupted tile data. Got size: %s. Expected modulo: %s", offset));
  2521. clear_layer(p_layer);
  2522. #ifdef DISABLE_DEPRECATED
  2523. ERR_FAIL_COND_MSG(format != FORMAT_3, vformat("Cannot handle deprecated TileMap data format version %d. This Godot version was compiled with no support for deprecated data.", format));
  2524. #endif
  2525. for (int i = 0; i < c; i += offset) {
  2526. const uint8_t *ptr = (const uint8_t *)&r[i];
  2527. uint8_t local[12];
  2528. for (int j = 0; j < ((format >= FORMAT_2) ? 12 : 8); j++) {
  2529. local[j] = ptr[j];
  2530. }
  2531. #ifdef BIG_ENDIAN_ENABLED
  2532. SWAP(local[0], local[3]);
  2533. SWAP(local[1], local[2]);
  2534. SWAP(local[4], local[7]);
  2535. SWAP(local[5], local[6]);
  2536. //TODO: ask someone to check this...
  2537. if (FORMAT >= FORMAT_2) {
  2538. SWAP(local[8], local[11]);
  2539. SWAP(local[9], local[10]);
  2540. }
  2541. #endif
  2542. // Extracts position in TileMap.
  2543. int16_t x = decode_uint16(&local[0]);
  2544. int16_t y = decode_uint16(&local[2]);
  2545. if (format == FORMAT_3) {
  2546. uint16_t source_id = decode_uint16(&local[4]);
  2547. uint16_t atlas_coords_x = decode_uint16(&local[6]);
  2548. uint16_t atlas_coords_y = decode_uint16(&local[8]);
  2549. uint16_t alternative_tile = decode_uint16(&local[10]);
  2550. set_cell(p_layer, Vector2i(x, y), source_id, Vector2i(atlas_coords_x, atlas_coords_y), alternative_tile);
  2551. } else {
  2552. #ifndef DISABLE_DEPRECATED
  2553. // Previous decated format.
  2554. uint32_t v = decode_uint32(&local[4]);
  2555. // Extract the transform flags that used to be in the tilemap.
  2556. bool flip_h = v & (1UL << 29);
  2557. bool flip_v = v & (1UL << 30);
  2558. bool transpose = v & (1UL << 31);
  2559. v &= (1UL << 29) - 1;
  2560. // Extract autotile/atlas coords.
  2561. int16_t coord_x = 0;
  2562. int16_t coord_y = 0;
  2563. if (format == FORMAT_2) {
  2564. coord_x = decode_uint16(&local[8]);
  2565. coord_y = decode_uint16(&local[10]);
  2566. }
  2567. if (tile_set.is_valid()) {
  2568. Array a = tile_set->compatibility_tilemap_map(v, Vector2i(coord_x, coord_y), flip_h, flip_v, transpose);
  2569. if (a.size() == 3) {
  2570. set_cell(p_layer, Vector2i(x, y), a[0], a[1], a[2]);
  2571. } else {
  2572. ERR_PRINT(vformat("No valid tile in Tileset for: tile:%s coords:%s flip_h:%s flip_v:%s transpose:%s", v, Vector2i(coord_x, coord_y), flip_h, flip_v, transpose));
  2573. }
  2574. } else {
  2575. int compatibility_alternative_tile = ((int)flip_h) + ((int)flip_v << 1) + ((int)transpose << 2);
  2576. set_cell(p_layer, Vector2i(x, y), v, Vector2i(coord_x, coord_y), compatibility_alternative_tile);
  2577. }
  2578. #endif
  2579. }
  2580. }
  2581. emit_signal(SNAME("changed"));
  2582. }
  2583. Vector<int> TileMap::_get_tile_data(int p_layer) const {
  2584. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), Vector<int>());
  2585. // Export tile data to raw format
  2586. const HashMap<Vector2i, TileMapCell> &tile_map = layers[p_layer].tile_map;
  2587. Vector<int> tile_data;
  2588. tile_data.resize(tile_map.size() * 3);
  2589. int *w = tile_data.ptrw();
  2590. // Save in highest format
  2591. int idx = 0;
  2592. for (const KeyValue<Vector2i, TileMapCell> &E : tile_map) {
  2593. uint8_t *ptr = (uint8_t *)&w[idx];
  2594. encode_uint16((int16_t)(E.key.x), &ptr[0]);
  2595. encode_uint16((int16_t)(E.key.y), &ptr[2]);
  2596. encode_uint16(E.value.source_id, &ptr[4]);
  2597. encode_uint16(E.value.coord_x, &ptr[6]);
  2598. encode_uint16(E.value.coord_y, &ptr[8]);
  2599. encode_uint16(E.value.alternative_tile, &ptr[10]);
  2600. idx += 3;
  2601. }
  2602. return tile_data;
  2603. }
  2604. void TileMap::_build_runtime_update_tile_data(SelfList<TileMapQuadrant>::List &r_dirty_quadrant_list) {
  2605. if (GDVIRTUAL_IS_OVERRIDDEN(_use_tile_data_runtime_update) && GDVIRTUAL_IS_OVERRIDDEN(_tile_data_runtime_update)) {
  2606. SelfList<TileMapQuadrant> *q_list_element = r_dirty_quadrant_list.first();
  2607. while (q_list_element) {
  2608. TileMapQuadrant &q = *q_list_element->self();
  2609. // Iterate over the cells of the quadrant.
  2610. for (const KeyValue<Vector2i, Vector2i> &E_cell : q.local_to_map) {
  2611. TileMapCell c = get_cell(q.layer, E_cell.value, true);
  2612. TileSetSource *source;
  2613. if (tile_set->has_source(c.source_id)) {
  2614. source = *tile_set->get_source(c.source_id);
  2615. if (!source->has_tile(c.get_atlas_coords()) || !source->has_alternative_tile(c.get_atlas_coords(), c.alternative_tile)) {
  2616. continue;
  2617. }
  2618. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  2619. if (atlas_source) {
  2620. bool ret = false;
  2621. if (GDVIRTUAL_CALL(_use_tile_data_runtime_update, q.layer, E_cell.value, ret) && ret) {
  2622. TileData *tile_data = atlas_source->get_tile_data(c.get_atlas_coords(), c.alternative_tile);
  2623. // Create the runtime TileData.
  2624. TileData *tile_data_runtime_use = tile_data->duplicate();
  2625. tile_data->set_allow_transform(true);
  2626. q.runtime_tile_data_cache[E_cell.value] = tile_data_runtime_use;
  2627. GDVIRTUAL_CALL(_tile_data_runtime_update, q.layer, E_cell.value, tile_data_runtime_use);
  2628. }
  2629. }
  2630. }
  2631. }
  2632. q_list_element = q_list_element->next();
  2633. }
  2634. }
  2635. }
  2636. #ifdef TOOLS_ENABLED
  2637. Rect2 TileMap::_edit_get_rect() const {
  2638. // Return the visible rect of the tilemap
  2639. const_cast<TileMap *>(this)->_recompute_rect_cache();
  2640. return rect_cache;
  2641. }
  2642. #endif
  2643. bool TileMap::_set(const StringName &p_name, const Variant &p_value) {
  2644. Vector<String> components = String(p_name).split("/", true, 2);
  2645. if (p_name == "format") {
  2646. if (p_value.get_type() == Variant::INT) {
  2647. format = (DataFormat)(p_value.operator int64_t()); // Set format used for loading
  2648. return true;
  2649. }
  2650. } else if (p_name == "tile_data") { // Kept for compatibility reasons.
  2651. if (p_value.is_array()) {
  2652. if (layers.size() < 1) {
  2653. layers.resize(1);
  2654. }
  2655. _set_tile_data(0, p_value);
  2656. return true;
  2657. }
  2658. return false;
  2659. } else if (components.size() == 2 && components[0].begins_with("layer_") && components[0].trim_prefix("layer_").is_valid_int()) {
  2660. int index = components[0].trim_prefix("layer_").to_int();
  2661. if (index < 0) {
  2662. return false;
  2663. }
  2664. if (index >= (int)layers.size()) {
  2665. _clear_internals();
  2666. while (index >= (int)layers.size()) {
  2667. layers.push_back(TileMapLayer());
  2668. }
  2669. _recreate_internals();
  2670. notify_property_list_changed();
  2671. emit_signal(SNAME("changed"));
  2672. update_configuration_warnings();
  2673. }
  2674. if (components[1] == "name") {
  2675. set_layer_name(index, p_value);
  2676. return true;
  2677. } else if (components[1] == "enabled") {
  2678. set_layer_enabled(index, p_value);
  2679. return true;
  2680. } else if (components[1] == "modulate") {
  2681. set_layer_modulate(index, p_value);
  2682. return true;
  2683. } else if (components[1] == "y_sort_enabled") {
  2684. set_layer_y_sort_enabled(index, p_value);
  2685. return true;
  2686. } else if (components[1] == "y_sort_origin") {
  2687. set_layer_y_sort_origin(index, p_value);
  2688. return true;
  2689. } else if (components[1] == "z_index") {
  2690. set_layer_z_index(index, p_value);
  2691. return true;
  2692. } else if (components[1] == "tile_data") {
  2693. _set_tile_data(index, p_value);
  2694. return true;
  2695. } else {
  2696. return false;
  2697. }
  2698. }
  2699. return false;
  2700. }
  2701. bool TileMap::_get(const StringName &p_name, Variant &r_ret) const {
  2702. Vector<String> components = String(p_name).split("/", true, 2);
  2703. if (p_name == "format") {
  2704. r_ret = FORMAT_3; // When saving, always save highest format
  2705. return true;
  2706. } else if (components.size() == 2 && components[0].begins_with("layer_") && components[0].trim_prefix("layer_").is_valid_int()) {
  2707. int index = components[0].trim_prefix("layer_").to_int();
  2708. if (index < 0 || index >= (int)layers.size()) {
  2709. return false;
  2710. }
  2711. if (components[1] == "name") {
  2712. r_ret = get_layer_name(index);
  2713. return true;
  2714. } else if (components[1] == "enabled") {
  2715. r_ret = is_layer_enabled(index);
  2716. return true;
  2717. } else if (components[1] == "modulate") {
  2718. r_ret = get_layer_modulate(index);
  2719. return true;
  2720. } else if (components[1] == "y_sort_enabled") {
  2721. r_ret = is_layer_y_sort_enabled(index);
  2722. return true;
  2723. } else if (components[1] == "y_sort_origin") {
  2724. r_ret = get_layer_y_sort_origin(index);
  2725. return true;
  2726. } else if (components[1] == "z_index") {
  2727. r_ret = get_layer_z_index(index);
  2728. return true;
  2729. } else if (components[1] == "tile_data") {
  2730. r_ret = _get_tile_data(index);
  2731. return true;
  2732. } else {
  2733. return false;
  2734. }
  2735. }
  2736. return false;
  2737. }
  2738. void TileMap::_get_property_list(List<PropertyInfo> *p_list) const {
  2739. p_list->push_back(PropertyInfo(Variant::INT, "format", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  2740. p_list->push_back(PropertyInfo(Variant::NIL, "Layers", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  2741. for (unsigned int i = 0; i < layers.size(); i++) {
  2742. p_list->push_back(PropertyInfo(Variant::STRING, vformat("layer_%d/name", i), PROPERTY_HINT_NONE));
  2743. p_list->push_back(PropertyInfo(Variant::BOOL, vformat("layer_%d/enabled", i), PROPERTY_HINT_NONE));
  2744. p_list->push_back(PropertyInfo(Variant::COLOR, vformat("layer_%d/modulate", i), PROPERTY_HINT_NONE));
  2745. p_list->push_back(PropertyInfo(Variant::BOOL, vformat("layer_%d/y_sort_enabled", i), PROPERTY_HINT_NONE));
  2746. p_list->push_back(PropertyInfo(Variant::INT, vformat("layer_%d/y_sort_origin", i), PROPERTY_HINT_NONE, "suffix:px"));
  2747. p_list->push_back(PropertyInfo(Variant::INT, vformat("layer_%d/z_index", i), PROPERTY_HINT_NONE));
  2748. p_list->push_back(PropertyInfo(Variant::OBJECT, vformat("layer_%d/tile_data", i), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  2749. }
  2750. }
  2751. Vector2 TileMap::map_to_local(const Vector2i &p_pos) const {
  2752. // SHOULD RETURN THE CENTER OF THE CELL
  2753. ERR_FAIL_COND_V(!tile_set.is_valid(), Vector2());
  2754. Vector2 ret = p_pos;
  2755. TileSet::TileShape tile_shape = tile_set->get_tile_shape();
  2756. TileSet::TileOffsetAxis tile_offset_axis = tile_set->get_tile_offset_axis();
  2757. if (tile_shape == TileSet::TILE_SHAPE_HALF_OFFSET_SQUARE || tile_shape == TileSet::TILE_SHAPE_HEXAGON || tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  2758. // Technically, those 3 shapes are equivalent, as they are basically half-offset, but with different levels or overlap.
  2759. // square = no overlap, hexagon = 0.25 overlap, isometric = 0.5 overlap
  2760. if (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  2761. switch (tile_set->get_tile_layout()) {
  2762. case TileSet::TILE_LAYOUT_STACKED:
  2763. ret = Vector2(ret.x + (Math::posmod(ret.y, 2) == 0 ? 0.0 : 0.5), ret.y);
  2764. break;
  2765. case TileSet::TILE_LAYOUT_STACKED_OFFSET:
  2766. ret = Vector2(ret.x + (Math::posmod(ret.y, 2) == 1 ? 0.0 : 0.5), ret.y);
  2767. break;
  2768. case TileSet::TILE_LAYOUT_STAIRS_RIGHT:
  2769. ret = Vector2(ret.x + ret.y / 2, ret.y);
  2770. break;
  2771. case TileSet::TILE_LAYOUT_STAIRS_DOWN:
  2772. ret = Vector2(ret.x / 2, ret.y * 2 + ret.x);
  2773. break;
  2774. case TileSet::TILE_LAYOUT_DIAMOND_RIGHT:
  2775. ret = Vector2((ret.x + ret.y) / 2, ret.y - ret.x);
  2776. break;
  2777. case TileSet::TILE_LAYOUT_DIAMOND_DOWN:
  2778. ret = Vector2((ret.x - ret.y) / 2, ret.y + ret.x);
  2779. break;
  2780. }
  2781. } else { // TILE_OFFSET_AXIS_VERTICAL
  2782. switch (tile_set->get_tile_layout()) {
  2783. case TileSet::TILE_LAYOUT_STACKED:
  2784. ret = Vector2(ret.x, ret.y + (Math::posmod(ret.x, 2) == 0 ? 0.0 : 0.5));
  2785. break;
  2786. case TileSet::TILE_LAYOUT_STACKED_OFFSET:
  2787. ret = Vector2(ret.x, ret.y + (Math::posmod(ret.x, 2) == 1 ? 0.0 : 0.5));
  2788. break;
  2789. case TileSet::TILE_LAYOUT_STAIRS_RIGHT:
  2790. ret = Vector2(ret.x * 2 + ret.y, ret.y / 2);
  2791. break;
  2792. case TileSet::TILE_LAYOUT_STAIRS_DOWN:
  2793. ret = Vector2(ret.x, ret.y + ret.x / 2);
  2794. break;
  2795. case TileSet::TILE_LAYOUT_DIAMOND_RIGHT:
  2796. ret = Vector2(ret.x + ret.y, (ret.y - ret.x) / 2);
  2797. break;
  2798. case TileSet::TILE_LAYOUT_DIAMOND_DOWN:
  2799. ret = Vector2(ret.x - ret.y, (ret.y + ret.x) / 2);
  2800. break;
  2801. }
  2802. }
  2803. }
  2804. // Multiply by the overlapping ratio
  2805. double overlapping_ratio = 1.0;
  2806. if (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  2807. if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  2808. overlapping_ratio = 0.5;
  2809. } else if (tile_shape == TileSet::TILE_SHAPE_HEXAGON) {
  2810. overlapping_ratio = 0.75;
  2811. }
  2812. ret.y *= overlapping_ratio;
  2813. } else { // TILE_OFFSET_AXIS_VERTICAL
  2814. if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  2815. overlapping_ratio = 0.5;
  2816. } else if (tile_shape == TileSet::TILE_SHAPE_HEXAGON) {
  2817. overlapping_ratio = 0.75;
  2818. }
  2819. ret.x *= overlapping_ratio;
  2820. }
  2821. return (ret + Vector2(0.5, 0.5)) * tile_set->get_tile_size();
  2822. }
  2823. Vector2i TileMap::local_to_map(const Vector2 &p_local_position) const {
  2824. ERR_FAIL_COND_V(!tile_set.is_valid(), Vector2i());
  2825. Vector2 ret = p_local_position;
  2826. ret /= tile_set->get_tile_size();
  2827. TileSet::TileShape tile_shape = tile_set->get_tile_shape();
  2828. TileSet::TileOffsetAxis tile_offset_axis = tile_set->get_tile_offset_axis();
  2829. TileSet::TileLayout tile_layout = tile_set->get_tile_layout();
  2830. // Divide by the overlapping ratio
  2831. double overlapping_ratio = 1.0;
  2832. if (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  2833. if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  2834. overlapping_ratio = 0.5;
  2835. } else if (tile_shape == TileSet::TILE_SHAPE_HEXAGON) {
  2836. overlapping_ratio = 0.75;
  2837. }
  2838. ret.y /= overlapping_ratio;
  2839. } else { // TILE_OFFSET_AXIS_VERTICAL
  2840. if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  2841. overlapping_ratio = 0.5;
  2842. } else if (tile_shape == TileSet::TILE_SHAPE_HEXAGON) {
  2843. overlapping_ratio = 0.75;
  2844. }
  2845. ret.x /= overlapping_ratio;
  2846. }
  2847. // For each half-offset shape, we check if we are in the corner of the tile, and thus should correct the local position accordingly.
  2848. if (tile_shape == TileSet::TILE_SHAPE_HALF_OFFSET_SQUARE || tile_shape == TileSet::TILE_SHAPE_HEXAGON || tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  2849. // Technically, those 3 shapes are equivalent, as they are basically half-offset, but with different levels or overlap.
  2850. // square = no overlap, hexagon = 0.25 overlap, isometric = 0.5 overlap
  2851. if (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  2852. // Smart floor of the position
  2853. Vector2 raw_pos = ret;
  2854. if (Math::posmod(Math::floor(ret.y), 2) ^ (tile_layout == TileSet::TILE_LAYOUT_STACKED_OFFSET)) {
  2855. ret = Vector2(Math::floor(ret.x + 0.5) - 0.5, Math::floor(ret.y));
  2856. } else {
  2857. ret = ret.floor();
  2858. }
  2859. // Compute the tile offset, and if we might the output for a neighbor top tile
  2860. Vector2 in_tile_pos = raw_pos - ret;
  2861. bool in_top_left_triangle = (in_tile_pos - Vector2(0.5, 0.0)).cross(Vector2(-0.5, 1.0 / overlapping_ratio - 1)) <= 0;
  2862. bool in_top_right_triangle = (in_tile_pos - Vector2(0.5, 0.0)).cross(Vector2(0.5, 1.0 / overlapping_ratio - 1)) > 0;
  2863. switch (tile_layout) {
  2864. case TileSet::TILE_LAYOUT_STACKED:
  2865. ret = ret.floor();
  2866. if (in_top_left_triangle) {
  2867. ret += Vector2i(Math::posmod(Math::floor(ret.y), 2) ? 0 : -1, -1);
  2868. } else if (in_top_right_triangle) {
  2869. ret += Vector2i(Math::posmod(Math::floor(ret.y), 2) ? 1 : 0, -1);
  2870. }
  2871. break;
  2872. case TileSet::TILE_LAYOUT_STACKED_OFFSET:
  2873. ret = ret.floor();
  2874. if (in_top_left_triangle) {
  2875. ret += Vector2i(Math::posmod(Math::floor(ret.y), 2) ? -1 : 0, -1);
  2876. } else if (in_top_right_triangle) {
  2877. ret += Vector2i(Math::posmod(Math::floor(ret.y), 2) ? 0 : 1, -1);
  2878. }
  2879. break;
  2880. case TileSet::TILE_LAYOUT_STAIRS_RIGHT:
  2881. ret = Vector2(ret.x - ret.y / 2, ret.y).floor();
  2882. if (in_top_left_triangle) {
  2883. ret += Vector2i(0, -1);
  2884. } else if (in_top_right_triangle) {
  2885. ret += Vector2i(1, -1);
  2886. }
  2887. break;
  2888. case TileSet::TILE_LAYOUT_STAIRS_DOWN:
  2889. ret = Vector2(ret.x * 2, ret.y / 2 - ret.x).floor();
  2890. if (in_top_left_triangle) {
  2891. ret += Vector2i(-1, 0);
  2892. } else if (in_top_right_triangle) {
  2893. ret += Vector2i(1, -1);
  2894. }
  2895. break;
  2896. case TileSet::TILE_LAYOUT_DIAMOND_RIGHT:
  2897. ret = Vector2(ret.x - ret.y / 2, ret.y / 2 + ret.x).floor();
  2898. if (in_top_left_triangle) {
  2899. ret += Vector2i(0, -1);
  2900. } else if (in_top_right_triangle) {
  2901. ret += Vector2i(1, 0);
  2902. }
  2903. break;
  2904. case TileSet::TILE_LAYOUT_DIAMOND_DOWN:
  2905. ret = Vector2(ret.x + ret.y / 2, ret.y / 2 - ret.x).floor();
  2906. if (in_top_left_triangle) {
  2907. ret += Vector2i(-1, 0);
  2908. } else if (in_top_right_triangle) {
  2909. ret += Vector2i(0, -1);
  2910. }
  2911. break;
  2912. }
  2913. } else { // TILE_OFFSET_AXIS_VERTICAL
  2914. // Smart floor of the position
  2915. Vector2 raw_pos = ret;
  2916. if (Math::posmod(Math::floor(ret.x), 2) ^ (tile_layout == TileSet::TILE_LAYOUT_STACKED_OFFSET)) {
  2917. ret = Vector2(Math::floor(ret.x), Math::floor(ret.y + 0.5) - 0.5);
  2918. } else {
  2919. ret = ret.floor();
  2920. }
  2921. // Compute the tile offset, and if we might the output for a neighbor top tile
  2922. Vector2 in_tile_pos = raw_pos - ret;
  2923. bool in_top_left_triangle = (in_tile_pos - Vector2(0.0, 0.5)).cross(Vector2(1.0 / overlapping_ratio - 1, -0.5)) > 0;
  2924. bool in_bottom_left_triangle = (in_tile_pos - Vector2(0.0, 0.5)).cross(Vector2(1.0 / overlapping_ratio - 1, 0.5)) <= 0;
  2925. switch (tile_layout) {
  2926. case TileSet::TILE_LAYOUT_STACKED:
  2927. ret = ret.floor();
  2928. if (in_top_left_triangle) {
  2929. ret += Vector2i(-1, Math::posmod(Math::floor(ret.x), 2) ? 0 : -1);
  2930. } else if (in_bottom_left_triangle) {
  2931. ret += Vector2i(-1, Math::posmod(Math::floor(ret.x), 2) ? 1 : 0);
  2932. }
  2933. break;
  2934. case TileSet::TILE_LAYOUT_STACKED_OFFSET:
  2935. ret = ret.floor();
  2936. if (in_top_left_triangle) {
  2937. ret += Vector2i(-1, Math::posmod(Math::floor(ret.x), 2) ? -1 : 0);
  2938. } else if (in_bottom_left_triangle) {
  2939. ret += Vector2i(-1, Math::posmod(Math::floor(ret.x), 2) ? 0 : 1);
  2940. }
  2941. break;
  2942. case TileSet::TILE_LAYOUT_STAIRS_RIGHT:
  2943. ret = Vector2(ret.x / 2 - ret.y, ret.y * 2).floor();
  2944. if (in_top_left_triangle) {
  2945. ret += Vector2i(0, -1);
  2946. } else if (in_bottom_left_triangle) {
  2947. ret += Vector2i(-1, 1);
  2948. }
  2949. break;
  2950. case TileSet::TILE_LAYOUT_STAIRS_DOWN:
  2951. ret = Vector2(ret.x, ret.y - ret.x / 2).floor();
  2952. if (in_top_left_triangle) {
  2953. ret += Vector2i(-1, 0);
  2954. } else if (in_bottom_left_triangle) {
  2955. ret += Vector2i(-1, 1);
  2956. }
  2957. break;
  2958. case TileSet::TILE_LAYOUT_DIAMOND_RIGHT:
  2959. ret = Vector2(ret.x / 2 - ret.y, ret.y + ret.x / 2).floor();
  2960. if (in_top_left_triangle) {
  2961. ret += Vector2i(0, -1);
  2962. } else if (in_bottom_left_triangle) {
  2963. ret += Vector2i(-1, 0);
  2964. }
  2965. break;
  2966. case TileSet::TILE_LAYOUT_DIAMOND_DOWN:
  2967. ret = Vector2(ret.x / 2 + ret.y, ret.y - ret.x / 2).floor();
  2968. if (in_top_left_triangle) {
  2969. ret += Vector2i(-1, 0);
  2970. } else if (in_bottom_left_triangle) {
  2971. ret += Vector2i(0, 1);
  2972. }
  2973. break;
  2974. }
  2975. }
  2976. } else {
  2977. ret = (ret + Vector2(0.00005, 0.00005)).floor();
  2978. }
  2979. return Vector2i(ret);
  2980. }
  2981. bool TileMap::is_existing_neighbor(TileSet::CellNeighbor p_cell_neighbor) const {
  2982. ERR_FAIL_COND_V(!tile_set.is_valid(), false);
  2983. TileSet::TileShape shape = tile_set->get_tile_shape();
  2984. if (shape == TileSet::TILE_SHAPE_SQUARE) {
  2985. return p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE ||
  2986. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER ||
  2987. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE ||
  2988. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER ||
  2989. p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE ||
  2990. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER ||
  2991. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE ||
  2992. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER;
  2993. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  2994. return p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER ||
  2995. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE ||
  2996. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER ||
  2997. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE ||
  2998. p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER ||
  2999. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE ||
  3000. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER ||
  3001. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE;
  3002. } else {
  3003. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  3004. return p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE ||
  3005. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE ||
  3006. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE ||
  3007. p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE ||
  3008. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE ||
  3009. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE;
  3010. } else {
  3011. return p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE ||
  3012. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE ||
  3013. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE ||
  3014. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE ||
  3015. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE ||
  3016. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE;
  3017. }
  3018. }
  3019. }
  3020. Vector2i TileMap::get_neighbor_cell(const Vector2i &p_coords, TileSet::CellNeighbor p_cell_neighbor) const {
  3021. ERR_FAIL_COND_V(!tile_set.is_valid(), p_coords);
  3022. TileSet::TileShape shape = tile_set->get_tile_shape();
  3023. if (shape == TileSet::TILE_SHAPE_SQUARE) {
  3024. switch (p_cell_neighbor) {
  3025. case TileSet::CELL_NEIGHBOR_RIGHT_SIDE:
  3026. return p_coords + Vector2i(1, 0);
  3027. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  3028. return p_coords + Vector2i(1, 1);
  3029. case TileSet::CELL_NEIGHBOR_BOTTOM_SIDE:
  3030. return p_coords + Vector2i(0, 1);
  3031. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  3032. return p_coords + Vector2i(-1, 1);
  3033. case TileSet::CELL_NEIGHBOR_LEFT_SIDE:
  3034. return p_coords + Vector2i(-1, 0);
  3035. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  3036. return p_coords + Vector2i(-1, -1);
  3037. case TileSet::CELL_NEIGHBOR_TOP_SIDE:
  3038. return p_coords + Vector2i(0, -1);
  3039. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  3040. return p_coords + Vector2i(1, -1);
  3041. default:
  3042. ERR_FAIL_V(p_coords);
  3043. }
  3044. } else { // Half-offset shapes (square and hexagon)
  3045. switch (tile_set->get_tile_layout()) {
  3046. case TileSet::TILE_LAYOUT_STACKED: {
  3047. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  3048. bool is_offset = p_coords.y % 2;
  3049. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) ||
  3050. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE)) {
  3051. return p_coords + Vector2i(1, 0);
  3052. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3053. return p_coords + Vector2i(is_offset ? 1 : 0, 1);
  3054. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) {
  3055. return p_coords + Vector2i(0, 2);
  3056. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3057. return p_coords + Vector2i(is_offset ? 0 : -1, 1);
  3058. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) ||
  3059. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE)) {
  3060. return p_coords + Vector2i(-1, 0);
  3061. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3062. return p_coords + Vector2i(is_offset ? 0 : -1, -1);
  3063. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) {
  3064. return p_coords + Vector2i(0, -2);
  3065. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3066. return p_coords + Vector2i(is_offset ? 1 : 0, -1);
  3067. } else {
  3068. ERR_FAIL_V(p_coords);
  3069. }
  3070. } else {
  3071. bool is_offset = p_coords.x % 2;
  3072. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) ||
  3073. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)) {
  3074. return p_coords + Vector2i(0, 1);
  3075. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3076. return p_coords + Vector2i(1, is_offset ? 1 : 0);
  3077. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) {
  3078. return p_coords + Vector2i(2, 0);
  3079. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3080. return p_coords + Vector2i(1, is_offset ? 0 : -1);
  3081. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) ||
  3082. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE)) {
  3083. return p_coords + Vector2i(0, -1);
  3084. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3085. return p_coords + Vector2i(-1, is_offset ? 0 : -1);
  3086. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) {
  3087. return p_coords + Vector2i(-2, 0);
  3088. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3089. return p_coords + Vector2i(-1, is_offset ? 1 : 0);
  3090. } else {
  3091. ERR_FAIL_V(p_coords);
  3092. }
  3093. }
  3094. } break;
  3095. case TileSet::TILE_LAYOUT_STACKED_OFFSET: {
  3096. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  3097. bool is_offset = p_coords.y % 2;
  3098. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) ||
  3099. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE)) {
  3100. return p_coords + Vector2i(1, 0);
  3101. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3102. return p_coords + Vector2i(is_offset ? 0 : 1, 1);
  3103. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) {
  3104. return p_coords + Vector2i(0, 2);
  3105. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3106. return p_coords + Vector2i(is_offset ? -1 : 0, 1);
  3107. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) ||
  3108. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE)) {
  3109. return p_coords + Vector2i(-1, 0);
  3110. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3111. return p_coords + Vector2i(is_offset ? -1 : 0, -1);
  3112. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) {
  3113. return p_coords + Vector2i(0, -2);
  3114. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3115. return p_coords + Vector2i(is_offset ? 0 : 1, -1);
  3116. } else {
  3117. ERR_FAIL_V(p_coords);
  3118. }
  3119. } else {
  3120. bool is_offset = p_coords.x % 2;
  3121. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) ||
  3122. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)) {
  3123. return p_coords + Vector2i(0, 1);
  3124. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3125. return p_coords + Vector2i(1, is_offset ? 0 : 1);
  3126. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) {
  3127. return p_coords + Vector2i(2, 0);
  3128. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3129. return p_coords + Vector2i(1, is_offset ? -1 : 0);
  3130. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) ||
  3131. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE)) {
  3132. return p_coords + Vector2i(0, -1);
  3133. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3134. return p_coords + Vector2i(-1, is_offset ? -1 : 0);
  3135. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) {
  3136. return p_coords + Vector2i(-2, 0);
  3137. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3138. return p_coords + Vector2i(-1, is_offset ? 0 : 1);
  3139. } else {
  3140. ERR_FAIL_V(p_coords);
  3141. }
  3142. }
  3143. } break;
  3144. case TileSet::TILE_LAYOUT_STAIRS_RIGHT:
  3145. case TileSet::TILE_LAYOUT_STAIRS_DOWN: {
  3146. if ((tile_set->get_tile_layout() == TileSet::TILE_LAYOUT_STAIRS_RIGHT) ^ (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_VERTICAL)) {
  3147. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  3148. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) ||
  3149. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE)) {
  3150. return p_coords + Vector2i(1, 0);
  3151. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3152. return p_coords + Vector2i(0, 1);
  3153. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) {
  3154. return p_coords + Vector2i(-1, 2);
  3155. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3156. return p_coords + Vector2i(-1, 1);
  3157. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) ||
  3158. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE)) {
  3159. return p_coords + Vector2i(-1, 0);
  3160. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3161. return p_coords + Vector2i(0, -1);
  3162. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) {
  3163. return p_coords + Vector2i(1, -2);
  3164. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3165. return p_coords + Vector2i(1, -1);
  3166. } else {
  3167. ERR_FAIL_V(p_coords);
  3168. }
  3169. } else {
  3170. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) ||
  3171. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)) {
  3172. return p_coords + Vector2i(0, 1);
  3173. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3174. return p_coords + Vector2i(1, 0);
  3175. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) {
  3176. return p_coords + Vector2i(2, -1);
  3177. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3178. return p_coords + Vector2i(1, -1);
  3179. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) ||
  3180. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE)) {
  3181. return p_coords + Vector2i(0, -1);
  3182. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3183. return p_coords + Vector2i(-1, 0);
  3184. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) {
  3185. return p_coords + Vector2i(-2, 1);
  3186. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3187. return p_coords + Vector2i(-1, 1);
  3188. } else {
  3189. ERR_FAIL_V(p_coords);
  3190. }
  3191. }
  3192. } else {
  3193. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  3194. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) ||
  3195. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE)) {
  3196. return p_coords + Vector2i(2, -1);
  3197. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3198. return p_coords + Vector2i(1, 0);
  3199. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) {
  3200. return p_coords + Vector2i(0, 1);
  3201. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3202. return p_coords + Vector2i(-1, 1);
  3203. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) ||
  3204. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE)) {
  3205. return p_coords + Vector2i(-2, 1);
  3206. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3207. return p_coords + Vector2i(-1, 0);
  3208. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) {
  3209. return p_coords + Vector2i(0, -1);
  3210. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3211. return p_coords + Vector2i(1, -1);
  3212. } else {
  3213. ERR_FAIL_V(p_coords);
  3214. }
  3215. } else {
  3216. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) ||
  3217. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)) {
  3218. return p_coords + Vector2i(-1, 2);
  3219. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3220. return p_coords + Vector2i(0, 1);
  3221. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) {
  3222. return p_coords + Vector2i(1, 0);
  3223. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3224. return p_coords + Vector2i(1, -1);
  3225. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) ||
  3226. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE)) {
  3227. return p_coords + Vector2i(1, -2);
  3228. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3229. return p_coords + Vector2i(0, -1);
  3230. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) {
  3231. return p_coords + Vector2i(-1, 0);
  3232. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3233. return p_coords + Vector2i(-1, 1);
  3234. } else {
  3235. ERR_FAIL_V(p_coords);
  3236. }
  3237. }
  3238. }
  3239. } break;
  3240. case TileSet::TILE_LAYOUT_DIAMOND_RIGHT:
  3241. case TileSet::TILE_LAYOUT_DIAMOND_DOWN: {
  3242. if ((tile_set->get_tile_layout() == TileSet::TILE_LAYOUT_DIAMOND_RIGHT) ^ (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_VERTICAL)) {
  3243. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  3244. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) ||
  3245. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE)) {
  3246. return p_coords + Vector2i(1, 1);
  3247. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3248. return p_coords + Vector2i(0, 1);
  3249. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) {
  3250. return p_coords + Vector2i(-1, 1);
  3251. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3252. return p_coords + Vector2i(-1, 0);
  3253. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) ||
  3254. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE)) {
  3255. return p_coords + Vector2i(-1, -1);
  3256. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3257. return p_coords + Vector2i(0, -1);
  3258. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) {
  3259. return p_coords + Vector2i(1, -1);
  3260. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3261. return p_coords + Vector2i(1, 0);
  3262. } else {
  3263. ERR_FAIL_V(p_coords);
  3264. }
  3265. } else {
  3266. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) ||
  3267. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)) {
  3268. return p_coords + Vector2i(1, 1);
  3269. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3270. return p_coords + Vector2i(1, 0);
  3271. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) {
  3272. return p_coords + Vector2i(1, -1);
  3273. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3274. return p_coords + Vector2i(0, -1);
  3275. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) ||
  3276. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE)) {
  3277. return p_coords + Vector2i(-1, -1);
  3278. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3279. return p_coords + Vector2i(-1, 0);
  3280. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) {
  3281. return p_coords + Vector2i(-1, 1);
  3282. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3283. return p_coords + Vector2i(0, 1);
  3284. } else {
  3285. ERR_FAIL_V(p_coords);
  3286. }
  3287. }
  3288. } else {
  3289. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  3290. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) ||
  3291. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE)) {
  3292. return p_coords + Vector2i(1, -1);
  3293. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3294. return p_coords + Vector2i(1, 0);
  3295. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) {
  3296. return p_coords + Vector2i(1, 1);
  3297. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3298. return p_coords + Vector2i(0, 1);
  3299. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) ||
  3300. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE)) {
  3301. return p_coords + Vector2i(-1, 1);
  3302. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3303. return p_coords + Vector2i(-1, 0);
  3304. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) {
  3305. return p_coords + Vector2i(-1, -1);
  3306. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3307. return p_coords + Vector2i(0, -1);
  3308. } else {
  3309. ERR_FAIL_V(p_coords);
  3310. }
  3311. } else {
  3312. if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) ||
  3313. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)) {
  3314. return p_coords + Vector2i(-1, 1);
  3315. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  3316. return p_coords + Vector2i(0, 1);
  3317. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) {
  3318. return p_coords + Vector2i(1, 1);
  3319. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  3320. return p_coords + Vector2i(1, 0);
  3321. } else if ((shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) ||
  3322. (shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE)) {
  3323. return p_coords + Vector2i(1, -1);
  3324. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  3325. return p_coords + Vector2i(0, -1);
  3326. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) {
  3327. return p_coords + Vector2i(-1, -1);
  3328. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  3329. return p_coords + Vector2i(-1, 0);
  3330. } else {
  3331. ERR_FAIL_V(p_coords);
  3332. }
  3333. }
  3334. }
  3335. } break;
  3336. default:
  3337. ERR_FAIL_V(p_coords);
  3338. }
  3339. }
  3340. }
  3341. TypedArray<Vector2i> TileMap::get_used_cells(int p_layer) const {
  3342. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), TypedArray<Vector2i>());
  3343. // Returns the cells used in the tilemap.
  3344. TypedArray<Vector2i> a;
  3345. a.resize(layers[p_layer].tile_map.size());
  3346. int i = 0;
  3347. for (const KeyValue<Vector2i, TileMapCell> &E : layers[p_layer].tile_map) {
  3348. Vector2i p(E.key.x, E.key.y);
  3349. a[i++] = p;
  3350. }
  3351. return a;
  3352. }
  3353. TypedArray<Vector2i> TileMap::get_used_cells_by_id(int p_layer, int p_source_id, const Vector2i p_atlas_coords, int p_alternative_tile) const {
  3354. ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), TypedArray<Vector2i>());
  3355. // Returns the cells used in the tilemap.
  3356. TypedArray<Vector2i> a;
  3357. for (const KeyValue<Vector2i, TileMapCell> &E : layers[p_layer].tile_map) {
  3358. if ((p_source_id == TileSet::INVALID_SOURCE || p_source_id == E.value.source_id) &&
  3359. (p_atlas_coords == TileSetSource::INVALID_ATLAS_COORDS || p_atlas_coords == E.value.get_atlas_coords()) &&
  3360. (p_alternative_tile == TileSetSource::INVALID_TILE_ALTERNATIVE || p_alternative_tile == E.value.alternative_tile)) {
  3361. a.push_back(E.key);
  3362. }
  3363. }
  3364. return a;
  3365. }
  3366. Rect2i TileMap::get_used_rect() { // Not const because of cache
  3367. // Return the rect of the currently used area
  3368. if (used_rect_cache_dirty) {
  3369. bool first = true;
  3370. used_rect_cache = Rect2i();
  3371. for (unsigned int i = 0; i < layers.size(); i++) {
  3372. const HashMap<Vector2i, TileMapCell> &tile_map = layers[i].tile_map;
  3373. if (tile_map.size() > 0) {
  3374. if (first) {
  3375. used_rect_cache = Rect2i(tile_map.begin()->key.x, tile_map.begin()->key.y, 0, 0);
  3376. first = false;
  3377. }
  3378. for (const KeyValue<Vector2i, TileMapCell> &E : tile_map) {
  3379. used_rect_cache.expand_to(Vector2i(E.key.x, E.key.y));
  3380. }
  3381. }
  3382. }
  3383. if (!first) { // first is true if every layer is empty.
  3384. used_rect_cache.size += Vector2i(1, 1); // The cache expands to top-left coordinate, so we add one full tile.
  3385. }
  3386. used_rect_cache_dirty = false;
  3387. }
  3388. return used_rect_cache;
  3389. }
  3390. // --- Override some methods of the CanvasItem class to pass the changes to the quadrants CanvasItems ---
  3391. void TileMap::set_light_mask(int p_light_mask) {
  3392. // Occlusion: set light mask.
  3393. CanvasItem::set_light_mask(p_light_mask);
  3394. for (unsigned int layer = 0; layer < layers.size(); layer++) {
  3395. for (const KeyValue<Vector2i, TileMapQuadrant> &E : layers[layer].quadrant_map) {
  3396. for (const RID &ci : E.value.canvas_items) {
  3397. RenderingServer::get_singleton()->canvas_item_set_light_mask(ci, get_light_mask());
  3398. }
  3399. }
  3400. _rendering_update_layer(layer);
  3401. }
  3402. }
  3403. void TileMap::set_material(const Ref<Material> &p_material) {
  3404. // Set material for the whole tilemap.
  3405. CanvasItem::set_material(p_material);
  3406. // Update material for the whole tilemap.
  3407. for (unsigned int layer = 0; layer < layers.size(); layer++) {
  3408. for (KeyValue<Vector2i, TileMapQuadrant> &E : layers[layer].quadrant_map) {
  3409. TileMapQuadrant &q = E.value;
  3410. for (const RID &ci : q.canvas_items) {
  3411. RS::get_singleton()->canvas_item_set_use_parent_material(ci, get_use_parent_material() || get_material().is_valid());
  3412. }
  3413. }
  3414. _rendering_update_layer(layer);
  3415. }
  3416. }
  3417. void TileMap::set_use_parent_material(bool p_use_parent_material) {
  3418. // Set use_parent_material for the whole tilemap.
  3419. CanvasItem::set_use_parent_material(p_use_parent_material);
  3420. // Update use_parent_material for the whole tilemap.
  3421. for (unsigned int layer = 0; layer < layers.size(); layer++) {
  3422. for (KeyValue<Vector2i, TileMapQuadrant> &E : layers[layer].quadrant_map) {
  3423. TileMapQuadrant &q = E.value;
  3424. for (const RID &ci : q.canvas_items) {
  3425. RS::get_singleton()->canvas_item_set_use_parent_material(ci, get_use_parent_material() || get_material().is_valid());
  3426. }
  3427. }
  3428. _rendering_update_layer(layer);
  3429. }
  3430. }
  3431. void TileMap::set_texture_filter(TextureFilter p_texture_filter) {
  3432. // Set a default texture filter for the whole tilemap.
  3433. CanvasItem::set_texture_filter(p_texture_filter);
  3434. TextureFilter target_filter = get_texture_filter_in_tree();
  3435. for (unsigned int layer = 0; layer < layers.size(); layer++) {
  3436. for (HashMap<Vector2i, TileMapQuadrant>::Iterator F = layers[layer].quadrant_map.begin(); F; ++F) {
  3437. TileMapQuadrant &q = F->value;
  3438. for (const RID &ci : q.canvas_items) {
  3439. RenderingServer::get_singleton()->canvas_item_set_default_texture_filter(ci, RS::CanvasItemTextureFilter(target_filter));
  3440. _make_quadrant_dirty(F);
  3441. }
  3442. }
  3443. _rendering_update_layer(layer);
  3444. }
  3445. }
  3446. void TileMap::set_texture_repeat(CanvasItem::TextureRepeat p_texture_repeat) {
  3447. // Set a default texture repeat for the whole tilemap.
  3448. CanvasItem::set_texture_repeat(p_texture_repeat);
  3449. TextureRepeat target_repeat = get_texture_repeat_in_tree();
  3450. for (unsigned int layer = 0; layer < layers.size(); layer++) {
  3451. for (HashMap<Vector2i, TileMapQuadrant>::Iterator F = layers[layer].quadrant_map.begin(); F; ++F) {
  3452. TileMapQuadrant &q = F->value;
  3453. for (const RID &ci : q.canvas_items) {
  3454. RenderingServer::get_singleton()->canvas_item_set_default_texture_repeat(ci, RS::CanvasItemTextureRepeat(target_repeat));
  3455. _make_quadrant_dirty(F);
  3456. }
  3457. }
  3458. _rendering_update_layer(layer);
  3459. }
  3460. }
  3461. TypedArray<Vector2i> TileMap::get_surrounding_cells(const Vector2i &coords) {
  3462. if (!tile_set.is_valid()) {
  3463. return TypedArray<Vector2i>();
  3464. }
  3465. TypedArray<Vector2i> around;
  3466. TileSet::TileShape shape = tile_set->get_tile_shape();
  3467. if (shape == TileSet::TILE_SHAPE_SQUARE) {
  3468. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_RIGHT_SIDE));
  3469. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_BOTTOM_SIDE));
  3470. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_LEFT_SIDE));
  3471. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_TOP_SIDE));
  3472. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  3473. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE));
  3474. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE));
  3475. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE));
  3476. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE));
  3477. } else {
  3478. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  3479. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_RIGHT_SIDE));
  3480. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE));
  3481. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE));
  3482. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_LEFT_SIDE));
  3483. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE));
  3484. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE));
  3485. } else {
  3486. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE));
  3487. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_BOTTOM_SIDE));
  3488. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE));
  3489. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE));
  3490. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_TOP_SIDE));
  3491. around.push_back(get_neighbor_cell(coords, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE));
  3492. }
  3493. }
  3494. return around;
  3495. }
  3496. void TileMap::draw_cells_outline(Control *p_control, const RBSet<Vector2i> &p_cells, Color p_color, Transform2D p_transform) {
  3497. if (!tile_set.is_valid()) {
  3498. return;
  3499. }
  3500. // Create a set.
  3501. Vector2i tile_size = tile_set->get_tile_size();
  3502. Vector<Vector2> polygon = tile_set->get_tile_shape_polygon();
  3503. TileSet::TileShape shape = tile_set->get_tile_shape();
  3504. for (const Vector2i &E : p_cells) {
  3505. Vector2 center = map_to_local(E);
  3506. #define DRAW_SIDE_IF_NEEDED(side, polygon_index_from, polygon_index_to) \
  3507. if (!p_cells.has(get_neighbor_cell(E, side))) { \
  3508. Vector2 from = p_transform.xform(center + polygon[polygon_index_from] * tile_size); \
  3509. Vector2 to = p_transform.xform(center + polygon[polygon_index_to] * tile_size); \
  3510. p_control->draw_line(from, to, p_color); \
  3511. }
  3512. if (shape == TileSet::TILE_SHAPE_SQUARE) {
  3513. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_RIGHT_SIDE, 1, 2);
  3514. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_SIDE, 2, 3);
  3515. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_LEFT_SIDE, 3, 0);
  3516. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_SIDE, 0, 1);
  3517. } else if (shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  3518. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE, 2, 3);
  3519. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE, 1, 2);
  3520. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE, 0, 1);
  3521. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE, 3, 0);
  3522. } else {
  3523. if (tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  3524. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE, 3, 4);
  3525. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE, 2, 3);
  3526. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_LEFT_SIDE, 1, 2);
  3527. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE, 0, 1);
  3528. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE, 5, 0);
  3529. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_RIGHT_SIDE, 4, 5);
  3530. } else {
  3531. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE, 3, 4);
  3532. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_SIDE, 4, 5);
  3533. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE, 5, 0);
  3534. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE, 0, 1);
  3535. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_SIDE, 1, 2);
  3536. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE, 2, 3);
  3537. }
  3538. }
  3539. }
  3540. #undef DRAW_SIDE_IF_NEEDED
  3541. }
  3542. PackedStringArray TileMap::get_configuration_warnings() const {
  3543. PackedStringArray warnings = Node::get_configuration_warnings();
  3544. // Retrieve the set of Z index values with a Y-sorted layer.
  3545. RBSet<int> y_sorted_z_index;
  3546. for (const TileMapLayer &layer : layers) {
  3547. if (layer.y_sort_enabled) {
  3548. y_sorted_z_index.insert(layer.z_index);
  3549. }
  3550. }
  3551. // Check if we have a non-sorted layer in a Z-index with a Y-sorted layer.
  3552. for (const TileMapLayer &layer : layers) {
  3553. if (!layer.y_sort_enabled && y_sorted_z_index.has(layer.z_index)) {
  3554. warnings.push_back(RTR("A Y-sorted layer has the same Z-index value as a not Y-sorted layer.\nThis may lead to unwanted behaviors, as a layer that is not Y-sorted will be Y-sorted as a whole with tiles from Y-sorted layers."));
  3555. break;
  3556. }
  3557. }
  3558. // Check if Y-sort is enabled on a layer but not on the node.
  3559. if (!is_y_sort_enabled()) {
  3560. for (const TileMapLayer &layer : layers) {
  3561. if (layer.y_sort_enabled) {
  3562. warnings.push_back(RTR("A TileMap layer is set as Y-sorted, but Y-sort is not enabled on the TileMap node itself."));
  3563. break;
  3564. }
  3565. }
  3566. }
  3567. // Check if we are in isometric mode without Y-sort enabled.
  3568. if (tile_set.is_valid() && tile_set->get_tile_shape() == TileSet::TILE_SHAPE_ISOMETRIC) {
  3569. bool warn = !is_y_sort_enabled();
  3570. if (!warn) {
  3571. for (const TileMapLayer &layer : layers) {
  3572. if (!layer.y_sort_enabled) {
  3573. warn = true;
  3574. break;
  3575. }
  3576. }
  3577. }
  3578. if (warn) {
  3579. warnings.push_back(RTR("Isometric TileSet will likely not look as intended without Y-sort enabled for the TileMap and all of its layers."));
  3580. }
  3581. }
  3582. return warnings;
  3583. }
  3584. void TileMap::_bind_methods() {
  3585. ClassDB::bind_method(D_METHOD("set_tileset", "tileset"), &TileMap::set_tileset);
  3586. ClassDB::bind_method(D_METHOD("get_tileset"), &TileMap::get_tileset);
  3587. ClassDB::bind_method(D_METHOD("set_quadrant_size", "size"), &TileMap::set_quadrant_size);
  3588. ClassDB::bind_method(D_METHOD("get_quadrant_size"), &TileMap::get_quadrant_size);
  3589. ClassDB::bind_method(D_METHOD("get_layers_count"), &TileMap::get_layers_count);
  3590. ClassDB::bind_method(D_METHOD("add_layer", "to_position"), &TileMap::add_layer);
  3591. ClassDB::bind_method(D_METHOD("move_layer", "layer", "to_position"), &TileMap::move_layer);
  3592. ClassDB::bind_method(D_METHOD("remove_layer", "layer"), &TileMap::remove_layer);
  3593. ClassDB::bind_method(D_METHOD("set_layer_name", "layer", "name"), &TileMap::set_layer_name);
  3594. ClassDB::bind_method(D_METHOD("get_layer_name", "layer"), &TileMap::get_layer_name);
  3595. ClassDB::bind_method(D_METHOD("set_layer_enabled", "layer", "enabled"), &TileMap::set_layer_enabled);
  3596. ClassDB::bind_method(D_METHOD("is_layer_enabled", "layer"), &TileMap::is_layer_enabled);
  3597. ClassDB::bind_method(D_METHOD("set_layer_modulate", "layer", "modulate"), &TileMap::set_layer_modulate);
  3598. ClassDB::bind_method(D_METHOD("get_layer_modulate", "layer"), &TileMap::get_layer_modulate);
  3599. ClassDB::bind_method(D_METHOD("set_layer_y_sort_enabled", "layer", "y_sort_enabled"), &TileMap::set_layer_y_sort_enabled);
  3600. ClassDB::bind_method(D_METHOD("is_layer_y_sort_enabled", "layer"), &TileMap::is_layer_y_sort_enabled);
  3601. ClassDB::bind_method(D_METHOD("set_layer_y_sort_origin", "layer", "y_sort_origin"), &TileMap::set_layer_y_sort_origin);
  3602. ClassDB::bind_method(D_METHOD("get_layer_y_sort_origin", "layer"), &TileMap::get_layer_y_sort_origin);
  3603. ClassDB::bind_method(D_METHOD("set_layer_z_index", "layer", "z_index"), &TileMap::set_layer_z_index);
  3604. ClassDB::bind_method(D_METHOD("get_layer_z_index", "layer"), &TileMap::get_layer_z_index);
  3605. ClassDB::bind_method(D_METHOD("set_collision_animatable", "enabled"), &TileMap::set_collision_animatable);
  3606. ClassDB::bind_method(D_METHOD("is_collision_animatable"), &TileMap::is_collision_animatable);
  3607. ClassDB::bind_method(D_METHOD("set_collision_visibility_mode", "collision_visibility_mode"), &TileMap::set_collision_visibility_mode);
  3608. ClassDB::bind_method(D_METHOD("get_collision_visibility_mode"), &TileMap::get_collision_visibility_mode);
  3609. ClassDB::bind_method(D_METHOD("set_navigation_visibility_mode", "navigation_visibility_mode"), &TileMap::set_navigation_visibility_mode);
  3610. ClassDB::bind_method(D_METHOD("get_navigation_visibility_mode"), &TileMap::get_navigation_visibility_mode);
  3611. ClassDB::bind_method(D_METHOD("set_navigation_map", "layer", "map"), &TileMap::set_navigation_map);
  3612. ClassDB::bind_method(D_METHOD("get_navigation_map", "layer"), &TileMap::get_navigation_map);
  3613. ClassDB::bind_method(D_METHOD("set_cell", "layer", "coords", "source_id", "atlas_coords", "alternative_tile"), &TileMap::set_cell, DEFVAL(TileSet::INVALID_SOURCE), DEFVAL(TileSetSource::INVALID_ATLAS_COORDS), DEFVAL(0));
  3614. ClassDB::bind_method(D_METHOD("erase_cell", "layer", "coords"), &TileMap::erase_cell);
  3615. ClassDB::bind_method(D_METHOD("get_cell_source_id", "layer", "coords", "use_proxies"), &TileMap::get_cell_source_id, DEFVAL(false));
  3616. ClassDB::bind_method(D_METHOD("get_cell_atlas_coords", "layer", "coords", "use_proxies"), &TileMap::get_cell_atlas_coords, DEFVAL(false));
  3617. ClassDB::bind_method(D_METHOD("get_cell_alternative_tile", "layer", "coords", "use_proxies"), &TileMap::get_cell_alternative_tile, DEFVAL(false));
  3618. ClassDB::bind_method(D_METHOD("get_cell_tile_data", "layer", "coords", "use_proxies"), &TileMap::get_cell_tile_data, DEFVAL(false));
  3619. ClassDB::bind_method(D_METHOD("get_coords_for_body_rid", "body"), &TileMap::get_coords_for_body_rid);
  3620. ClassDB::bind_method(D_METHOD("get_pattern", "layer", "coords_array"), &TileMap::get_pattern);
  3621. ClassDB::bind_method(D_METHOD("map_pattern", "position_in_tilemap", "coords_in_pattern", "pattern"), &TileMap::map_pattern);
  3622. ClassDB::bind_method(D_METHOD("set_pattern", "layer", "position", "pattern"), &TileMap::set_pattern);
  3623. ClassDB::bind_method(D_METHOD("set_cells_terrain_connect", "layer", "cells", "terrain_set", "terrain", "ignore_empty_terrains"), &TileMap::set_cells_terrain_connect, DEFVAL(true));
  3624. ClassDB::bind_method(D_METHOD("set_cells_terrain_path", "layer", "path", "terrain_set", "terrain", "ignore_empty_terrains"), &TileMap::set_cells_terrain_path, DEFVAL(true));
  3625. ClassDB::bind_method(D_METHOD("fix_invalid_tiles"), &TileMap::fix_invalid_tiles);
  3626. ClassDB::bind_method(D_METHOD("clear_layer", "layer"), &TileMap::clear_layer);
  3627. ClassDB::bind_method(D_METHOD("clear"), &TileMap::clear);
  3628. ClassDB::bind_method(D_METHOD("force_update", "layer"), &TileMap::force_update, DEFVAL(-1));
  3629. ClassDB::bind_method(D_METHOD("get_surrounding_cells", "coords"), &TileMap::get_surrounding_cells);
  3630. ClassDB::bind_method(D_METHOD("get_used_cells", "layer"), &TileMap::get_used_cells);
  3631. ClassDB::bind_method(D_METHOD("get_used_cells_by_id", "layer", "source_id", "atlas_coords", "alternative_tile"), &TileMap::get_used_cells_by_id, DEFVAL(TileSet::INVALID_SOURCE), DEFVAL(TileSetSource::INVALID_ATLAS_COORDS), DEFVAL(TileSetSource::INVALID_TILE_ALTERNATIVE));
  3632. ClassDB::bind_method(D_METHOD("get_used_rect"), &TileMap::get_used_rect);
  3633. ClassDB::bind_method(D_METHOD("map_to_local", "map_position"), &TileMap::map_to_local);
  3634. ClassDB::bind_method(D_METHOD("local_to_map", "local_position"), &TileMap::local_to_map);
  3635. ClassDB::bind_method(D_METHOD("get_neighbor_cell", "coords", "neighbor"), &TileMap::get_neighbor_cell);
  3636. ClassDB::bind_method(D_METHOD("_update_dirty_quadrants"), &TileMap::_update_dirty_quadrants);
  3637. ClassDB::bind_method(D_METHOD("_tile_set_changed_deferred_update"), &TileMap::_tile_set_changed_deferred_update);
  3638. GDVIRTUAL_BIND(_use_tile_data_runtime_update, "layer", "coords");
  3639. GDVIRTUAL_BIND(_tile_data_runtime_update, "layer", "coords", "tile_data");
  3640. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "tile_set", PROPERTY_HINT_RESOURCE_TYPE, "TileSet"), "set_tileset", "get_tileset");
  3641. ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_quadrant_size", PROPERTY_HINT_RANGE, "1,128,1"), "set_quadrant_size", "get_quadrant_size");
  3642. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collision_animatable"), "set_collision_animatable", "is_collision_animatable");
  3643. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_visibility_mode", PROPERTY_HINT_ENUM, "Default,Force Show,Force Hide"), "set_collision_visibility_mode", "get_collision_visibility_mode");
  3644. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_visibility_mode", PROPERTY_HINT_ENUM, "Default,Force Show,Force Hide"), "set_navigation_visibility_mode", "get_navigation_visibility_mode");
  3645. ADD_ARRAY("layers", "layer_");
  3646. ADD_PROPERTY_DEFAULT("format", FORMAT_1);
  3647. ADD_SIGNAL(MethodInfo("changed"));
  3648. BIND_ENUM_CONSTANT(VISIBILITY_MODE_DEFAULT);
  3649. BIND_ENUM_CONSTANT(VISIBILITY_MODE_FORCE_HIDE);
  3650. BIND_ENUM_CONSTANT(VISIBILITY_MODE_FORCE_SHOW);
  3651. }
  3652. void TileMap::_tile_set_changed() {
  3653. emit_signal(SNAME("changed"));
  3654. _tile_set_changed_deferred_update_needed = true;
  3655. instantiated_scenes.clear();
  3656. call_deferred(SNAME("_tile_set_changed_deferred_update"));
  3657. update_configuration_warnings();
  3658. }
  3659. void TileMap::_tile_set_changed_deferred_update() {
  3660. if (_tile_set_changed_deferred_update_needed) {
  3661. _clear_internals();
  3662. _recreate_internals();
  3663. _tile_set_changed_deferred_update_needed = false;
  3664. }
  3665. }
  3666. TileMap::TileMap() {
  3667. set_notify_transform(true);
  3668. set_notify_local_transform(false);
  3669. layers.resize(1);
  3670. }
  3671. TileMap::~TileMap() {
  3672. if (tile_set.is_valid()) {
  3673. tile_set->disconnect("changed", callable_mp(this, &TileMap::_tile_set_changed));
  3674. }
  3675. _clear_internals();
  3676. }