2
0

tile_map.cpp 163 KB

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