map.cpp 142 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393
  1. /*
  2. Minetest-c55
  3. Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "map.h"
  17. #include "main.h"
  18. #include "jmutexautolock.h"
  19. #include "client.h"
  20. #include "filesys.h"
  21. #include "utility.h"
  22. #include "voxel.h"
  23. #include "porting.h"
  24. #include "mineral.h"
  25. #include "noise.h"
  26. #include "serverobject.h"
  27. /*
  28. Map
  29. */
  30. Map::Map(std::ostream &dout):
  31. m_dout(dout),
  32. m_sector_cache(NULL)
  33. {
  34. /*m_sector_mutex.Init();
  35. assert(m_sector_mutex.IsInitialized());*/
  36. }
  37. Map::~Map()
  38. {
  39. /*
  40. Free all MapSectors
  41. */
  42. core::map<v2s16, MapSector*>::Iterator i = m_sectors.getIterator();
  43. for(; i.atEnd() == false; i++)
  44. {
  45. MapSector *sector = i.getNode()->getValue();
  46. delete sector;
  47. }
  48. }
  49. void Map::addEventReceiver(MapEventReceiver *event_receiver)
  50. {
  51. m_event_receivers.insert(event_receiver, false);
  52. }
  53. void Map::removeEventReceiver(MapEventReceiver *event_receiver)
  54. {
  55. if(m_event_receivers.find(event_receiver) == NULL)
  56. return;
  57. m_event_receivers.remove(event_receiver);
  58. }
  59. void Map::dispatchEvent(MapEditEvent *event)
  60. {
  61. for(core::map<MapEventReceiver*, bool>::Iterator
  62. i = m_event_receivers.getIterator();
  63. i.atEnd()==false; i++)
  64. {
  65. MapEventReceiver* event_receiver = i.getNode()->getKey();
  66. event_receiver->onMapEditEvent(event);
  67. }
  68. }
  69. MapSector * Map::getSectorNoGenerateNoExNoLock(v2s16 p)
  70. {
  71. if(m_sector_cache != NULL && p == m_sector_cache_p){
  72. MapSector * sector = m_sector_cache;
  73. // Reset inactivity timer
  74. sector->usage_timer = 0.0;
  75. return sector;
  76. }
  77. core::map<v2s16, MapSector*>::Node *n = m_sectors.find(p);
  78. if(n == NULL)
  79. return NULL;
  80. MapSector *sector = n->getValue();
  81. // Cache the last result
  82. m_sector_cache_p = p;
  83. m_sector_cache = sector;
  84. // Reset inactivity timer
  85. sector->usage_timer = 0.0;
  86. return sector;
  87. }
  88. MapSector * Map::getSectorNoGenerateNoEx(v2s16 p)
  89. {
  90. //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
  91. return getSectorNoGenerateNoExNoLock(p);
  92. }
  93. MapSector * Map::getSectorNoGenerate(v2s16 p)
  94. {
  95. MapSector *sector = getSectorNoGenerateNoEx(p);
  96. if(sector == NULL)
  97. throw InvalidPositionException();
  98. return sector;
  99. }
  100. MapBlock * Map::getBlockNoCreate(v3s16 p3d)
  101. {
  102. v2s16 p2d(p3d.X, p3d.Z);
  103. MapSector * sector = getSectorNoGenerate(p2d);
  104. MapBlock *block = sector->getBlockNoCreate(p3d.Y);
  105. return block;
  106. }
  107. MapBlock * Map::getBlockNoCreateNoEx(v3s16 p3d)
  108. {
  109. try
  110. {
  111. v2s16 p2d(p3d.X, p3d.Z);
  112. MapSector * sector = getSectorNoGenerate(p2d);
  113. MapBlock *block = sector->getBlockNoCreate(p3d.Y);
  114. return block;
  115. }
  116. catch(InvalidPositionException &e)
  117. {
  118. return NULL;
  119. }
  120. }
  121. /*MapBlock * Map::getBlockCreate(v3s16 p3d)
  122. {
  123. v2s16 p2d(p3d.X, p3d.Z);
  124. MapSector * sector = getSectorCreate(p2d);
  125. assert(sector);
  126. MapBlock *block = sector->getBlockNoCreate(p3d.Y);
  127. if(block)
  128. return block;
  129. block = sector->createBlankBlock(p3d.Y);
  130. return block;
  131. }*/
  132. bool Map::isNodeUnderground(v3s16 p)
  133. {
  134. v3s16 blockpos = getNodeBlockPos(p);
  135. try{
  136. MapBlock * block = getBlockNoCreate(blockpos);
  137. return block->getIsUnderground();
  138. }
  139. catch(InvalidPositionException &e)
  140. {
  141. return false;
  142. }
  143. }
  144. /*
  145. Goes recursively through the neighbours of the node.
  146. Alters only transparent nodes.
  147. If the lighting of the neighbour is lower than the lighting of
  148. the node was (before changing it to 0 at the step before), the
  149. lighting of the neighbour is set to 0 and then the same stuff
  150. repeats for the neighbour.
  151. The ending nodes of the routine are stored in light_sources.
  152. This is useful when a light is removed. In such case, this
  153. routine can be called for the light node and then again for
  154. light_sources to re-light the area without the removed light.
  155. values of from_nodes are lighting values.
  156. */
  157. void Map::unspreadLight(enum LightBank bank,
  158. core::map<v3s16, u8> & from_nodes,
  159. core::map<v3s16, bool> & light_sources,
  160. core::map<v3s16, MapBlock*> & modified_blocks)
  161. {
  162. v3s16 dirs[6] = {
  163. v3s16(0,0,1), // back
  164. v3s16(0,1,0), // top
  165. v3s16(1,0,0), // right
  166. v3s16(0,0,-1), // front
  167. v3s16(0,-1,0), // bottom
  168. v3s16(-1,0,0), // left
  169. };
  170. if(from_nodes.size() == 0)
  171. return;
  172. u32 blockchangecount = 0;
  173. core::map<v3s16, u8> unlighted_nodes;
  174. core::map<v3s16, u8>::Iterator j;
  175. j = from_nodes.getIterator();
  176. /*
  177. Initialize block cache
  178. */
  179. v3s16 blockpos_last;
  180. MapBlock *block = NULL;
  181. // Cache this a bit, too
  182. bool block_checked_in_modified = false;
  183. for(; j.atEnd() == false; j++)
  184. {
  185. v3s16 pos = j.getNode()->getKey();
  186. v3s16 blockpos = getNodeBlockPos(pos);
  187. // Only fetch a new block if the block position has changed
  188. try{
  189. if(block == NULL || blockpos != blockpos_last){
  190. block = getBlockNoCreate(blockpos);
  191. blockpos_last = blockpos;
  192. block_checked_in_modified = false;
  193. blockchangecount++;
  194. }
  195. }
  196. catch(InvalidPositionException &e)
  197. {
  198. continue;
  199. }
  200. if(block->isDummy())
  201. continue;
  202. // Calculate relative position in block
  203. v3s16 relpos = pos - blockpos_last * MAP_BLOCKSIZE;
  204. // Get node straight from the block
  205. MapNode n = block->getNode(relpos);
  206. u8 oldlight = j.getNode()->getValue();
  207. // Loop through 6 neighbors
  208. for(u16 i=0; i<6; i++)
  209. {
  210. // Get the position of the neighbor node
  211. v3s16 n2pos = pos + dirs[i];
  212. // Get the block where the node is located
  213. v3s16 blockpos = getNodeBlockPos(n2pos);
  214. try
  215. {
  216. // Only fetch a new block if the block position has changed
  217. try{
  218. if(block == NULL || blockpos != blockpos_last){
  219. block = getBlockNoCreate(blockpos);
  220. blockpos_last = blockpos;
  221. block_checked_in_modified = false;
  222. blockchangecount++;
  223. }
  224. }
  225. catch(InvalidPositionException &e)
  226. {
  227. continue;
  228. }
  229. // Calculate relative position in block
  230. v3s16 relpos = n2pos - blockpos * MAP_BLOCKSIZE;
  231. // Get node straight from the block
  232. MapNode n2 = block->getNode(relpos);
  233. bool changed = false;
  234. //TODO: Optimize output by optimizing light_sources?
  235. /*
  236. If the neighbor is dimmer than what was specified
  237. as oldlight (the light of the previous node)
  238. */
  239. if(n2.getLight(bank) < oldlight)
  240. {
  241. /*
  242. And the neighbor is transparent and it has some light
  243. */
  244. if(n2.light_propagates() && n2.getLight(bank) != 0)
  245. {
  246. /*
  247. Set light to 0 and add to queue
  248. */
  249. u8 current_light = n2.getLight(bank);
  250. n2.setLight(bank, 0);
  251. block->setNode(relpos, n2);
  252. unlighted_nodes.insert(n2pos, current_light);
  253. changed = true;
  254. /*
  255. Remove from light_sources if it is there
  256. NOTE: This doesn't happen nearly at all
  257. */
  258. /*if(light_sources.find(n2pos))
  259. {
  260. std::cout<<"Removed from light_sources"<<std::endl;
  261. light_sources.remove(n2pos);
  262. }*/
  263. }
  264. /*// DEBUG
  265. if(light_sources.find(n2pos) != NULL)
  266. light_sources.remove(n2pos);*/
  267. }
  268. else{
  269. light_sources.insert(n2pos, true);
  270. }
  271. // Add to modified_blocks
  272. if(changed == true && block_checked_in_modified == false)
  273. {
  274. // If the block is not found in modified_blocks, add.
  275. if(modified_blocks.find(blockpos) == NULL)
  276. {
  277. modified_blocks.insert(blockpos, block);
  278. }
  279. block_checked_in_modified = true;
  280. }
  281. }
  282. catch(InvalidPositionException &e)
  283. {
  284. continue;
  285. }
  286. }
  287. }
  288. /*dstream<<"unspreadLight(): Changed block "
  289. <<blockchangecount<<" times"
  290. <<" for "<<from_nodes.size()<<" nodes"
  291. <<std::endl;*/
  292. if(unlighted_nodes.size() > 0)
  293. unspreadLight(bank, unlighted_nodes, light_sources, modified_blocks);
  294. }
  295. /*
  296. A single-node wrapper of the above
  297. */
  298. void Map::unLightNeighbors(enum LightBank bank,
  299. v3s16 pos, u8 lightwas,
  300. core::map<v3s16, bool> & light_sources,
  301. core::map<v3s16, MapBlock*> & modified_blocks)
  302. {
  303. core::map<v3s16, u8> from_nodes;
  304. from_nodes.insert(pos, lightwas);
  305. unspreadLight(bank, from_nodes, light_sources, modified_blocks);
  306. }
  307. /*
  308. Lights neighbors of from_nodes, collects all them and then
  309. goes on recursively.
  310. */
  311. void Map::spreadLight(enum LightBank bank,
  312. core::map<v3s16, bool> & from_nodes,
  313. core::map<v3s16, MapBlock*> & modified_blocks)
  314. {
  315. const v3s16 dirs[6] = {
  316. v3s16(0,0,1), // back
  317. v3s16(0,1,0), // top
  318. v3s16(1,0,0), // right
  319. v3s16(0,0,-1), // front
  320. v3s16(0,-1,0), // bottom
  321. v3s16(-1,0,0), // left
  322. };
  323. if(from_nodes.size() == 0)
  324. return;
  325. u32 blockchangecount = 0;
  326. core::map<v3s16, bool> lighted_nodes;
  327. core::map<v3s16, bool>::Iterator j;
  328. j = from_nodes.getIterator();
  329. /*
  330. Initialize block cache
  331. */
  332. v3s16 blockpos_last;
  333. MapBlock *block = NULL;
  334. // Cache this a bit, too
  335. bool block_checked_in_modified = false;
  336. for(; j.atEnd() == false; j++)
  337. //for(; j != from_nodes.end(); j++)
  338. {
  339. v3s16 pos = j.getNode()->getKey();
  340. //v3s16 pos = *j;
  341. //dstream<<"pos=("<<pos.X<<","<<pos.Y<<","<<pos.Z<<")"<<std::endl;
  342. v3s16 blockpos = getNodeBlockPos(pos);
  343. // Only fetch a new block if the block position has changed
  344. try{
  345. if(block == NULL || blockpos != blockpos_last){
  346. block = getBlockNoCreate(blockpos);
  347. blockpos_last = blockpos;
  348. block_checked_in_modified = false;
  349. blockchangecount++;
  350. }
  351. }
  352. catch(InvalidPositionException &e)
  353. {
  354. continue;
  355. }
  356. if(block->isDummy())
  357. continue;
  358. // Calculate relative position in block
  359. v3s16 relpos = pos - blockpos_last * MAP_BLOCKSIZE;
  360. // Get node straight from the block
  361. MapNode n = block->getNode(relpos);
  362. u8 oldlight = n.getLight(bank);
  363. u8 newlight = diminish_light(oldlight);
  364. // Loop through 6 neighbors
  365. for(u16 i=0; i<6; i++){
  366. // Get the position of the neighbor node
  367. v3s16 n2pos = pos + dirs[i];
  368. // Get the block where the node is located
  369. v3s16 blockpos = getNodeBlockPos(n2pos);
  370. try
  371. {
  372. // Only fetch a new block if the block position has changed
  373. try{
  374. if(block == NULL || blockpos != blockpos_last){
  375. block = getBlockNoCreate(blockpos);
  376. blockpos_last = blockpos;
  377. block_checked_in_modified = false;
  378. blockchangecount++;
  379. }
  380. }
  381. catch(InvalidPositionException &e)
  382. {
  383. continue;
  384. }
  385. // Calculate relative position in block
  386. v3s16 relpos = n2pos - blockpos * MAP_BLOCKSIZE;
  387. // Get node straight from the block
  388. MapNode n2 = block->getNode(relpos);
  389. bool changed = false;
  390. /*
  391. If the neighbor is brighter than the current node,
  392. add to list (it will light up this node on its turn)
  393. */
  394. if(n2.getLight(bank) > undiminish_light(oldlight))
  395. {
  396. lighted_nodes.insert(n2pos, true);
  397. //lighted_nodes.push_back(n2pos);
  398. changed = true;
  399. }
  400. /*
  401. If the neighbor is dimmer than how much light this node
  402. would spread on it, add to list
  403. */
  404. if(n2.getLight(bank) < newlight)
  405. {
  406. if(n2.light_propagates())
  407. {
  408. n2.setLight(bank, newlight);
  409. block->setNode(relpos, n2);
  410. lighted_nodes.insert(n2pos, true);
  411. //lighted_nodes.push_back(n2pos);
  412. changed = true;
  413. }
  414. }
  415. // Add to modified_blocks
  416. if(changed == true && block_checked_in_modified == false)
  417. {
  418. // If the block is not found in modified_blocks, add.
  419. if(modified_blocks.find(blockpos) == NULL)
  420. {
  421. modified_blocks.insert(blockpos, block);
  422. }
  423. block_checked_in_modified = true;
  424. }
  425. }
  426. catch(InvalidPositionException &e)
  427. {
  428. continue;
  429. }
  430. }
  431. }
  432. /*dstream<<"spreadLight(): Changed block "
  433. <<blockchangecount<<" times"
  434. <<" for "<<from_nodes.size()<<" nodes"
  435. <<std::endl;*/
  436. if(lighted_nodes.size() > 0)
  437. spreadLight(bank, lighted_nodes, modified_blocks);
  438. }
  439. /*
  440. A single-node source variation of the above.
  441. */
  442. void Map::lightNeighbors(enum LightBank bank,
  443. v3s16 pos,
  444. core::map<v3s16, MapBlock*> & modified_blocks)
  445. {
  446. core::map<v3s16, bool> from_nodes;
  447. from_nodes.insert(pos, true);
  448. spreadLight(bank, from_nodes, modified_blocks);
  449. }
  450. v3s16 Map::getBrightestNeighbour(enum LightBank bank, v3s16 p)
  451. {
  452. v3s16 dirs[6] = {
  453. v3s16(0,0,1), // back
  454. v3s16(0,1,0), // top
  455. v3s16(1,0,0), // right
  456. v3s16(0,0,-1), // front
  457. v3s16(0,-1,0), // bottom
  458. v3s16(-1,0,0), // left
  459. };
  460. u8 brightest_light = 0;
  461. v3s16 brightest_pos(0,0,0);
  462. bool found_something = false;
  463. // Loop through 6 neighbors
  464. for(u16 i=0; i<6; i++){
  465. // Get the position of the neighbor node
  466. v3s16 n2pos = p + dirs[i];
  467. MapNode n2;
  468. try{
  469. n2 = getNode(n2pos);
  470. }
  471. catch(InvalidPositionException &e)
  472. {
  473. continue;
  474. }
  475. if(n2.getLight(bank) > brightest_light || found_something == false){
  476. brightest_light = n2.getLight(bank);
  477. brightest_pos = n2pos;
  478. found_something = true;
  479. }
  480. }
  481. if(found_something == false)
  482. throw InvalidPositionException();
  483. return brightest_pos;
  484. }
  485. /*
  486. Propagates sunlight down from a node.
  487. Starting point gets sunlight.
  488. Returns the lowest y value of where the sunlight went.
  489. Mud is turned into grass in where the sunlight stops.
  490. */
  491. s16 Map::propagateSunlight(v3s16 start,
  492. core::map<v3s16, MapBlock*> & modified_blocks)
  493. {
  494. s16 y = start.Y;
  495. for(; ; y--)
  496. {
  497. v3s16 pos(start.X, y, start.Z);
  498. v3s16 blockpos = getNodeBlockPos(pos);
  499. MapBlock *block;
  500. try{
  501. block = getBlockNoCreate(blockpos);
  502. }
  503. catch(InvalidPositionException &e)
  504. {
  505. break;
  506. }
  507. v3s16 relpos = pos - blockpos*MAP_BLOCKSIZE;
  508. MapNode n = block->getNode(relpos);
  509. if(n.sunlight_propagates())
  510. {
  511. n.setLight(LIGHTBANK_DAY, LIGHT_SUN);
  512. block->setNode(relpos, n);
  513. modified_blocks.insert(blockpos, block);
  514. }
  515. else
  516. {
  517. // Turn mud into grass
  518. if(n.d == CONTENT_MUD)
  519. {
  520. n.d = CONTENT_GRASS;
  521. block->setNode(relpos, n);
  522. modified_blocks.insert(blockpos, block);
  523. }
  524. // Sunlight goes no further
  525. break;
  526. }
  527. }
  528. return y + 1;
  529. }
  530. void Map::updateLighting(enum LightBank bank,
  531. core::map<v3s16, MapBlock*> & a_blocks,
  532. core::map<v3s16, MapBlock*> & modified_blocks)
  533. {
  534. /*m_dout<<DTIME<<"Map::updateLighting(): "
  535. <<a_blocks.size()<<" blocks."<<std::endl;*/
  536. //TimeTaker timer("updateLighting");
  537. // For debugging
  538. //bool debug=true;
  539. //u32 count_was = modified_blocks.size();
  540. core::map<v3s16, MapBlock*> blocks_to_update;
  541. core::map<v3s16, bool> light_sources;
  542. core::map<v3s16, u8> unlight_from;
  543. core::map<v3s16, MapBlock*>::Iterator i;
  544. i = a_blocks.getIterator();
  545. for(; i.atEnd() == false; i++)
  546. {
  547. MapBlock *block = i.getNode()->getValue();
  548. for(;;)
  549. {
  550. // Don't bother with dummy blocks.
  551. if(block->isDummy())
  552. break;
  553. v3s16 pos = block->getPos();
  554. modified_blocks.insert(pos, block);
  555. blocks_to_update.insert(pos, block);
  556. /*
  557. Clear all light from block
  558. */
  559. for(s16 z=0; z<MAP_BLOCKSIZE; z++)
  560. for(s16 x=0; x<MAP_BLOCKSIZE; x++)
  561. for(s16 y=0; y<MAP_BLOCKSIZE; y++)
  562. {
  563. try{
  564. v3s16 p(x,y,z);
  565. MapNode n = block->getNode(v3s16(x,y,z));
  566. u8 oldlight = n.getLight(bank);
  567. n.setLight(bank, 0);
  568. block->setNode(v3s16(x,y,z), n);
  569. // Collect borders for unlighting
  570. if(x==0 || x == MAP_BLOCKSIZE-1
  571. || y==0 || y == MAP_BLOCKSIZE-1
  572. || z==0 || z == MAP_BLOCKSIZE-1)
  573. {
  574. v3s16 p_map = p + v3s16(
  575. MAP_BLOCKSIZE*pos.X,
  576. MAP_BLOCKSIZE*pos.Y,
  577. MAP_BLOCKSIZE*pos.Z);
  578. unlight_from.insert(p_map, oldlight);
  579. }
  580. }
  581. catch(InvalidPositionException &e)
  582. {
  583. /*
  584. This would happen when dealing with a
  585. dummy block.
  586. */
  587. //assert(0);
  588. dstream<<"updateLighting(): InvalidPositionException"
  589. <<std::endl;
  590. }
  591. }
  592. if(bank == LIGHTBANK_DAY)
  593. {
  594. bool bottom_valid = block->propagateSunlight(light_sources);
  595. // If bottom is valid, we're done.
  596. if(bottom_valid)
  597. break;
  598. }
  599. else if(bank == LIGHTBANK_NIGHT)
  600. {
  601. // For night lighting, sunlight is not propagated
  602. break;
  603. }
  604. else
  605. {
  606. // Invalid lighting bank
  607. assert(0);
  608. }
  609. /*dstream<<"Bottom for sunlight-propagated block ("
  610. <<pos.X<<","<<pos.Y<<","<<pos.Z<<") not valid"
  611. <<std::endl;*/
  612. // Bottom sunlight is not valid; get the block and loop to it
  613. pos.Y--;
  614. try{
  615. block = getBlockNoCreate(pos);
  616. }
  617. catch(InvalidPositionException &e)
  618. {
  619. assert(0);
  620. }
  621. }
  622. }
  623. #if 0
  624. {
  625. TimeTaker timer("unspreadLight");
  626. unspreadLight(bank, unlight_from, light_sources, modified_blocks);
  627. }
  628. if(debug)
  629. {
  630. u32 diff = modified_blocks.size() - count_was;
  631. count_was = modified_blocks.size();
  632. dstream<<"unspreadLight modified "<<diff<<std::endl;
  633. }
  634. {
  635. TimeTaker timer("spreadLight");
  636. spreadLight(bank, light_sources, modified_blocks);
  637. }
  638. if(debug)
  639. {
  640. u32 diff = modified_blocks.size() - count_was;
  641. count_was = modified_blocks.size();
  642. dstream<<"spreadLight modified "<<diff<<std::endl;
  643. }
  644. #endif
  645. {
  646. //MapVoxelManipulator vmanip(this);
  647. // Make a manual voxel manipulator and load all the blocks
  648. // that touch the requested blocks
  649. ManualMapVoxelManipulator vmanip(this);
  650. core::map<v3s16, MapBlock*>::Iterator i;
  651. i = blocks_to_update.getIterator();
  652. for(; i.atEnd() == false; i++)
  653. {
  654. MapBlock *block = i.getNode()->getValue();
  655. v3s16 p = block->getPos();
  656. // Add all surrounding blocks
  657. vmanip.initialEmerge(p - v3s16(1,1,1), p + v3s16(1,1,1));
  658. /*
  659. Add all surrounding blocks that have up-to-date lighting
  660. NOTE: This doesn't quite do the job (not everything
  661. appropriate is lighted)
  662. */
  663. /*for(s16 z=-1; z<=1; z++)
  664. for(s16 y=-1; y<=1; y++)
  665. for(s16 x=-1; x<=1; x++)
  666. {
  667. v3s16 p(x,y,z);
  668. MapBlock *block = getBlockNoCreateNoEx(p);
  669. if(block == NULL)
  670. continue;
  671. if(block->isDummy())
  672. continue;
  673. if(block->getLightingExpired())
  674. continue;
  675. vmanip.initialEmerge(p, p);
  676. }*/
  677. // Lighting of block will be updated completely
  678. block->setLightingExpired(false);
  679. }
  680. {
  681. //TimeTaker timer("unSpreadLight");
  682. vmanip.unspreadLight(bank, unlight_from, light_sources);
  683. }
  684. {
  685. //TimeTaker timer("spreadLight");
  686. vmanip.spreadLight(bank, light_sources);
  687. }
  688. {
  689. //TimeTaker timer("blitBack");
  690. vmanip.blitBack(modified_blocks);
  691. }
  692. /*dstream<<"emerge_time="<<emerge_time<<std::endl;
  693. emerge_time = 0;*/
  694. }
  695. //m_dout<<"Done ("<<getTimestamp()<<")"<<std::endl;
  696. }
  697. void Map::updateLighting(core::map<v3s16, MapBlock*> & a_blocks,
  698. core::map<v3s16, MapBlock*> & modified_blocks)
  699. {
  700. updateLighting(LIGHTBANK_DAY, a_blocks, modified_blocks);
  701. updateLighting(LIGHTBANK_NIGHT, a_blocks, modified_blocks);
  702. /*
  703. Update information about whether day and night light differ
  704. */
  705. for(core::map<v3s16, MapBlock*>::Iterator
  706. i = modified_blocks.getIterator();
  707. i.atEnd() == false; i++)
  708. {
  709. MapBlock *block = i.getNode()->getValue();
  710. block->updateDayNightDiff();
  711. }
  712. }
  713. /*
  714. This is called after changing a node from transparent to opaque.
  715. The lighting value of the node should be left as-is after changing
  716. other values. This sets the lighting value to 0.
  717. */
  718. void Map::addNodeAndUpdate(v3s16 p, MapNode n,
  719. core::map<v3s16, MapBlock*> &modified_blocks)
  720. {
  721. /*PrintInfo(m_dout);
  722. m_dout<<DTIME<<"Map::addNodeAndUpdate(): p=("
  723. <<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;*/
  724. /*
  725. From this node to nodes underneath:
  726. If lighting is sunlight (1.0), unlight neighbours and
  727. set lighting to 0.
  728. Else discontinue.
  729. */
  730. v3s16 toppos = p + v3s16(0,1,0);
  731. v3s16 bottompos = p + v3s16(0,-1,0);
  732. bool node_under_sunlight = true;
  733. core::map<v3s16, bool> light_sources;
  734. /*
  735. If there is a node at top and it doesn't have sunlight,
  736. there has not been any sunlight going down.
  737. Otherwise there probably is.
  738. */
  739. try{
  740. MapNode topnode = getNode(toppos);
  741. if(topnode.getLight(LIGHTBANK_DAY) != LIGHT_SUN)
  742. node_under_sunlight = false;
  743. }
  744. catch(InvalidPositionException &e)
  745. {
  746. }
  747. /*
  748. If the new node doesn't propagate sunlight and there is
  749. grass below, change it to mud
  750. */
  751. if(content_features(n.d).sunlight_propagates == false)
  752. {
  753. try{
  754. MapNode bottomnode = getNode(bottompos);
  755. if(bottomnode.d == CONTENT_GRASS
  756. || bottomnode.d == CONTENT_GRASS_FOOTSTEPS)
  757. {
  758. bottomnode.d = CONTENT_MUD;
  759. setNode(bottompos, bottomnode);
  760. }
  761. }
  762. catch(InvalidPositionException &e)
  763. {
  764. }
  765. }
  766. /*
  767. If the new node is mud and it is under sunlight, change it
  768. to grass
  769. */
  770. if(n.d == CONTENT_MUD && node_under_sunlight)
  771. {
  772. n.d = CONTENT_GRASS;
  773. }
  774. /*
  775. Remove all light that has come out of this node
  776. */
  777. enum LightBank banks[] =
  778. {
  779. LIGHTBANK_DAY,
  780. LIGHTBANK_NIGHT
  781. };
  782. for(s32 i=0; i<2; i++)
  783. {
  784. enum LightBank bank = banks[i];
  785. u8 lightwas = getNode(p).getLight(bank);
  786. // Add the block of the added node to modified_blocks
  787. v3s16 blockpos = getNodeBlockPos(p);
  788. MapBlock * block = getBlockNoCreate(blockpos);
  789. assert(block != NULL);
  790. modified_blocks.insert(blockpos, block);
  791. assert(isValidPosition(p));
  792. // Unlight neighbours of node.
  793. // This means setting light of all consequent dimmer nodes
  794. // to 0.
  795. // This also collects the nodes at the border which will spread
  796. // light again into this.
  797. unLightNeighbors(bank, p, lightwas, light_sources, modified_blocks);
  798. n.setLight(bank, 0);
  799. }
  800. /*
  801. If node lets sunlight through and is under sunlight, it has
  802. sunlight too.
  803. */
  804. if(node_under_sunlight && content_features(n.d).sunlight_propagates)
  805. {
  806. n.setLight(LIGHTBANK_DAY, LIGHT_SUN);
  807. }
  808. /*
  809. Set the node on the map
  810. */
  811. setNode(p, n);
  812. /*
  813. Add intial metadata
  814. */
  815. NodeMetadata *meta_proto = content_features(n.d).initial_metadata;
  816. if(meta_proto)
  817. {
  818. NodeMetadata *meta = meta_proto->clone();
  819. setNodeMetadata(p, meta);
  820. }
  821. /*
  822. If node is under sunlight and doesn't let sunlight through,
  823. take all sunlighted nodes under it and clear light from them
  824. and from where the light has been spread.
  825. TODO: This could be optimized by mass-unlighting instead
  826. of looping
  827. */
  828. if(node_under_sunlight && !content_features(n.d).sunlight_propagates)
  829. {
  830. s16 y = p.Y - 1;
  831. for(;; y--){
  832. //m_dout<<DTIME<<"y="<<y<<std::endl;
  833. v3s16 n2pos(p.X, y, p.Z);
  834. MapNode n2;
  835. try{
  836. n2 = getNode(n2pos);
  837. }
  838. catch(InvalidPositionException &e)
  839. {
  840. break;
  841. }
  842. if(n2.getLight(LIGHTBANK_DAY) == LIGHT_SUN)
  843. {
  844. unLightNeighbors(LIGHTBANK_DAY,
  845. n2pos, n2.getLight(LIGHTBANK_DAY),
  846. light_sources, modified_blocks);
  847. n2.setLight(LIGHTBANK_DAY, 0);
  848. setNode(n2pos, n2);
  849. }
  850. else
  851. break;
  852. }
  853. }
  854. for(s32 i=0; i<2; i++)
  855. {
  856. enum LightBank bank = banks[i];
  857. /*
  858. Spread light from all nodes that might be capable of doing so
  859. */
  860. spreadLight(bank, light_sources, modified_blocks);
  861. }
  862. /*
  863. Update information about whether day and night light differ
  864. */
  865. for(core::map<v3s16, MapBlock*>::Iterator
  866. i = modified_blocks.getIterator();
  867. i.atEnd() == false; i++)
  868. {
  869. MapBlock *block = i.getNode()->getValue();
  870. block->updateDayNightDiff();
  871. }
  872. /*
  873. Add neighboring liquid nodes and the node itself if it is
  874. liquid (=water node was added) to transform queue.
  875. */
  876. v3s16 dirs[7] = {
  877. v3s16(0,0,0), // self
  878. v3s16(0,0,1), // back
  879. v3s16(0,1,0), // top
  880. v3s16(1,0,0), // right
  881. v3s16(0,0,-1), // front
  882. v3s16(0,-1,0), // bottom
  883. v3s16(-1,0,0), // left
  884. };
  885. for(u16 i=0; i<7; i++)
  886. {
  887. try
  888. {
  889. v3s16 p2 = p + dirs[i];
  890. MapNode n2 = getNode(p2);
  891. if(content_liquid(n2.d))
  892. {
  893. m_transforming_liquid.push_back(p2);
  894. }
  895. }catch(InvalidPositionException &e)
  896. {
  897. }
  898. }
  899. }
  900. /*
  901. */
  902. void Map::removeNodeAndUpdate(v3s16 p,
  903. core::map<v3s16, MapBlock*> &modified_blocks)
  904. {
  905. /*PrintInfo(m_dout);
  906. m_dout<<DTIME<<"Map::removeNodeAndUpdate(): p=("
  907. <<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;*/
  908. bool node_under_sunlight = true;
  909. v3s16 toppos = p + v3s16(0,1,0);
  910. // Node will be replaced with this
  911. u8 replace_material = CONTENT_AIR;
  912. /*
  913. If there is a node at top and it doesn't have sunlight,
  914. there will be no sunlight going down.
  915. */
  916. try{
  917. MapNode topnode = getNode(toppos);
  918. if(topnode.getLight(LIGHTBANK_DAY) != LIGHT_SUN)
  919. node_under_sunlight = false;
  920. }
  921. catch(InvalidPositionException &e)
  922. {
  923. }
  924. core::map<v3s16, bool> light_sources;
  925. enum LightBank banks[] =
  926. {
  927. LIGHTBANK_DAY,
  928. LIGHTBANK_NIGHT
  929. };
  930. for(s32 i=0; i<2; i++)
  931. {
  932. enum LightBank bank = banks[i];
  933. /*
  934. Unlight neighbors (in case the node is a light source)
  935. */
  936. unLightNeighbors(bank, p,
  937. getNode(p).getLight(bank),
  938. light_sources, modified_blocks);
  939. }
  940. /*
  941. Remove node metadata
  942. */
  943. removeNodeMetadata(p);
  944. /*
  945. Remove the node.
  946. This also clears the lighting.
  947. */
  948. MapNode n;
  949. n.d = replace_material;
  950. setNode(p, n);
  951. for(s32 i=0; i<2; i++)
  952. {
  953. enum LightBank bank = banks[i];
  954. /*
  955. Recalculate lighting
  956. */
  957. spreadLight(bank, light_sources, modified_blocks);
  958. }
  959. // Add the block of the removed node to modified_blocks
  960. v3s16 blockpos = getNodeBlockPos(p);
  961. MapBlock * block = getBlockNoCreate(blockpos);
  962. assert(block != NULL);
  963. modified_blocks.insert(blockpos, block);
  964. /*
  965. If the removed node was under sunlight, propagate the
  966. sunlight down from it and then light all neighbors
  967. of the propagated blocks.
  968. */
  969. if(node_under_sunlight)
  970. {
  971. s16 ybottom = propagateSunlight(p, modified_blocks);
  972. /*m_dout<<DTIME<<"Node was under sunlight. "
  973. "Propagating sunlight";
  974. m_dout<<DTIME<<" -> ybottom="<<ybottom<<std::endl;*/
  975. s16 y = p.Y;
  976. for(; y >= ybottom; y--)
  977. {
  978. v3s16 p2(p.X, y, p.Z);
  979. /*m_dout<<DTIME<<"lighting neighbors of node ("
  980. <<p2.X<<","<<p2.Y<<","<<p2.Z<<")"
  981. <<std::endl;*/
  982. lightNeighbors(LIGHTBANK_DAY, p2, modified_blocks);
  983. }
  984. }
  985. else
  986. {
  987. // Set the lighting of this node to 0
  988. // TODO: Is this needed? Lighting is cleared up there already.
  989. try{
  990. MapNode n = getNode(p);
  991. n.setLight(LIGHTBANK_DAY, 0);
  992. setNode(p, n);
  993. }
  994. catch(InvalidPositionException &e)
  995. {
  996. assert(0);
  997. }
  998. }
  999. for(s32 i=0; i<2; i++)
  1000. {
  1001. enum LightBank bank = banks[i];
  1002. // Get the brightest neighbour node and propagate light from it
  1003. v3s16 n2p = getBrightestNeighbour(bank, p);
  1004. try{
  1005. MapNode n2 = getNode(n2p);
  1006. lightNeighbors(bank, n2p, modified_blocks);
  1007. }
  1008. catch(InvalidPositionException &e)
  1009. {
  1010. }
  1011. }
  1012. /*
  1013. Update information about whether day and night light differ
  1014. */
  1015. for(core::map<v3s16, MapBlock*>::Iterator
  1016. i = modified_blocks.getIterator();
  1017. i.atEnd() == false; i++)
  1018. {
  1019. MapBlock *block = i.getNode()->getValue();
  1020. block->updateDayNightDiff();
  1021. }
  1022. /*
  1023. Add neighboring liquid nodes to transform queue.
  1024. */
  1025. v3s16 dirs[6] = {
  1026. v3s16(0,0,1), // back
  1027. v3s16(0,1,0), // top
  1028. v3s16(1,0,0), // right
  1029. v3s16(0,0,-1), // front
  1030. v3s16(0,-1,0), // bottom
  1031. v3s16(-1,0,0), // left
  1032. };
  1033. for(u16 i=0; i<6; i++)
  1034. {
  1035. try
  1036. {
  1037. v3s16 p2 = p + dirs[i];
  1038. MapNode n2 = getNode(p2);
  1039. if(content_liquid(n2.d))
  1040. {
  1041. m_transforming_liquid.push_back(p2);
  1042. }
  1043. }catch(InvalidPositionException &e)
  1044. {
  1045. }
  1046. }
  1047. }
  1048. bool Map::addNodeWithEvent(v3s16 p, MapNode n)
  1049. {
  1050. MapEditEvent event;
  1051. event.type = MEET_ADDNODE;
  1052. event.p = p;
  1053. event.n = n;
  1054. bool succeeded = true;
  1055. try{
  1056. core::map<v3s16, MapBlock*> modified_blocks;
  1057. addNodeAndUpdate(p, n, modified_blocks);
  1058. // Copy modified_blocks to event
  1059. for(core::map<v3s16, MapBlock*>::Iterator
  1060. i = modified_blocks.getIterator();
  1061. i.atEnd()==false; i++)
  1062. {
  1063. event.modified_blocks.insert(i.getNode()->getKey(), false);
  1064. }
  1065. }
  1066. catch(InvalidPositionException &e){
  1067. succeeded = false;
  1068. }
  1069. dispatchEvent(&event);
  1070. return succeeded;
  1071. }
  1072. bool Map::removeNodeWithEvent(v3s16 p)
  1073. {
  1074. MapEditEvent event;
  1075. event.type = MEET_REMOVENODE;
  1076. event.p = p;
  1077. bool succeeded = true;
  1078. try{
  1079. core::map<v3s16, MapBlock*> modified_blocks;
  1080. removeNodeAndUpdate(p, modified_blocks);
  1081. // Copy modified_blocks to event
  1082. for(core::map<v3s16, MapBlock*>::Iterator
  1083. i = modified_blocks.getIterator();
  1084. i.atEnd()==false; i++)
  1085. {
  1086. event.modified_blocks.insert(i.getNode()->getKey(), false);
  1087. }
  1088. }
  1089. catch(InvalidPositionException &e){
  1090. succeeded = false;
  1091. }
  1092. dispatchEvent(&event);
  1093. return succeeded;
  1094. }
  1095. bool Map::dayNightDiffed(v3s16 blockpos)
  1096. {
  1097. try{
  1098. v3s16 p = blockpos + v3s16(0,0,0);
  1099. MapBlock *b = getBlockNoCreate(p);
  1100. if(b->dayNightDiffed())
  1101. return true;
  1102. }
  1103. catch(InvalidPositionException &e){}
  1104. // Leading edges
  1105. try{
  1106. v3s16 p = blockpos + v3s16(-1,0,0);
  1107. MapBlock *b = getBlockNoCreate(p);
  1108. if(b->dayNightDiffed())
  1109. return true;
  1110. }
  1111. catch(InvalidPositionException &e){}
  1112. try{
  1113. v3s16 p = blockpos + v3s16(0,-1,0);
  1114. MapBlock *b = getBlockNoCreate(p);
  1115. if(b->dayNightDiffed())
  1116. return true;
  1117. }
  1118. catch(InvalidPositionException &e){}
  1119. try{
  1120. v3s16 p = blockpos + v3s16(0,0,-1);
  1121. MapBlock *b = getBlockNoCreate(p);
  1122. if(b->dayNightDiffed())
  1123. return true;
  1124. }
  1125. catch(InvalidPositionException &e){}
  1126. // Trailing edges
  1127. try{
  1128. v3s16 p = blockpos + v3s16(1,0,0);
  1129. MapBlock *b = getBlockNoCreate(p);
  1130. if(b->dayNightDiffed())
  1131. return true;
  1132. }
  1133. catch(InvalidPositionException &e){}
  1134. try{
  1135. v3s16 p = blockpos + v3s16(0,1,0);
  1136. MapBlock *b = getBlockNoCreate(p);
  1137. if(b->dayNightDiffed())
  1138. return true;
  1139. }
  1140. catch(InvalidPositionException &e){}
  1141. try{
  1142. v3s16 p = blockpos + v3s16(0,0,1);
  1143. MapBlock *b = getBlockNoCreate(p);
  1144. if(b->dayNightDiffed())
  1145. return true;
  1146. }
  1147. catch(InvalidPositionException &e){}
  1148. return false;
  1149. }
  1150. /*
  1151. Updates usage timers
  1152. */
  1153. void Map::timerUpdate(float dtime)
  1154. {
  1155. //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
  1156. core::map<v2s16, MapSector*>::Iterator si;
  1157. si = m_sectors.getIterator();
  1158. for(; si.atEnd() == false; si++)
  1159. {
  1160. MapSector *sector = si.getNode()->getValue();
  1161. sector->usage_timer += dtime;
  1162. }
  1163. }
  1164. void Map::deleteSectors(core::list<v2s16> &list, bool only_blocks)
  1165. {
  1166. /*
  1167. Wait for caches to be removed before continuing.
  1168. This disables the existence of caches while locked
  1169. */
  1170. //SharedPtr<JMutexAutoLock> cachelock(m_blockcachelock.waitCaches());
  1171. core::list<v2s16>::Iterator j;
  1172. for(j=list.begin(); j!=list.end(); j++)
  1173. {
  1174. MapSector *sector = m_sectors[*j];
  1175. if(only_blocks)
  1176. {
  1177. sector->deleteBlocks();
  1178. }
  1179. else
  1180. {
  1181. /*
  1182. If sector is in sector cache, remove it from there
  1183. */
  1184. if(m_sector_cache == sector)
  1185. {
  1186. m_sector_cache = NULL;
  1187. }
  1188. /*
  1189. Remove from map and delete
  1190. */
  1191. m_sectors.remove(*j);
  1192. delete sector;
  1193. }
  1194. }
  1195. }
  1196. u32 Map::deleteUnusedSectors(float timeout, bool only_blocks,
  1197. core::list<v3s16> *deleted_blocks)
  1198. {
  1199. //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
  1200. core::list<v2s16> sector_deletion_queue;
  1201. core::map<v2s16, MapSector*>::Iterator i = m_sectors.getIterator();
  1202. for(; i.atEnd() == false; i++)
  1203. {
  1204. MapSector *sector = i.getNode()->getValue();
  1205. /*
  1206. Delete sector from memory if it hasn't been used in a long time
  1207. */
  1208. if(sector->usage_timer > timeout)
  1209. {
  1210. sector_deletion_queue.push_back(i.getNode()->getKey());
  1211. if(deleted_blocks != NULL)
  1212. {
  1213. // Collect positions of blocks of sector
  1214. MapSector *sector = i.getNode()->getValue();
  1215. core::list<MapBlock*> blocks;
  1216. sector->getBlocks(blocks);
  1217. for(core::list<MapBlock*>::Iterator i = blocks.begin();
  1218. i != blocks.end(); i++)
  1219. {
  1220. deleted_blocks->push_back((*i)->getPos());
  1221. }
  1222. }
  1223. }
  1224. }
  1225. deleteSectors(sector_deletion_queue, only_blocks);
  1226. return sector_deletion_queue.getSize();
  1227. }
  1228. void Map::PrintInfo(std::ostream &out)
  1229. {
  1230. out<<"Map: ";
  1231. }
  1232. #define WATER_DROP_BOOST 4
  1233. void Map::transformLiquids(core::map<v3s16, MapBlock*> & modified_blocks)
  1234. {
  1235. DSTACK(__FUNCTION_NAME);
  1236. //TimeTaker timer("transformLiquids()");
  1237. u32 loopcount = 0;
  1238. u32 initial_size = m_transforming_liquid.size();
  1239. /*if(initial_size != 0)
  1240. dstream<<"transformLiquids(): initial_size="<<initial_size<<std::endl;*/
  1241. while(m_transforming_liquid.size() != 0)
  1242. {
  1243. /*
  1244. Get a queued transforming liquid node
  1245. */
  1246. v3s16 p0 = m_transforming_liquid.pop_front();
  1247. MapNode n0 = getNode(p0);
  1248. // Don't deal with non-liquids
  1249. if(content_liquid(n0.d) == false)
  1250. continue;
  1251. bool is_source = !content_flowing_liquid(n0.d);
  1252. u8 liquid_level = 8;
  1253. if(is_source == false)
  1254. liquid_level = n0.param2 & 0x0f;
  1255. // Turn possible source into non-source
  1256. u8 nonsource_c = make_liquid_flowing(n0.d);
  1257. /*
  1258. If not source, check that some node flows into this one
  1259. and what is the level of liquid in this one
  1260. */
  1261. if(is_source == false)
  1262. {
  1263. s8 new_liquid_level_max = -1;
  1264. v3s16 dirs_from[5] = {
  1265. v3s16(0,1,0), // top
  1266. v3s16(0,0,1), // back
  1267. v3s16(1,0,0), // right
  1268. v3s16(0,0,-1), // front
  1269. v3s16(-1,0,0), // left
  1270. };
  1271. for(u16 i=0; i<5; i++)
  1272. {
  1273. try
  1274. {
  1275. bool from_top = (i==0);
  1276. v3s16 p2 = p0 + dirs_from[i];
  1277. MapNode n2 = getNode(p2);
  1278. if(content_liquid(n2.d))
  1279. {
  1280. u8 n2_nonsource_c = make_liquid_flowing(n2.d);
  1281. // Check that the liquids are the same type
  1282. if(n2_nonsource_c != nonsource_c)
  1283. {
  1284. dstream<<"WARNING: Not handling: different liquids"
  1285. " collide"<<std::endl;
  1286. continue;
  1287. }
  1288. bool n2_is_source = !content_flowing_liquid(n2.d);
  1289. s8 n2_liquid_level = 8;
  1290. if(n2_is_source == false)
  1291. n2_liquid_level = n2.param2 & 0x07;
  1292. s8 new_liquid_level = -1;
  1293. if(from_top)
  1294. {
  1295. //new_liquid_level = 7;
  1296. if(n2_liquid_level >= 7 - WATER_DROP_BOOST)
  1297. new_liquid_level = 7;
  1298. else
  1299. new_liquid_level = n2_liquid_level + WATER_DROP_BOOST;
  1300. }
  1301. else if(n2_liquid_level > 0)
  1302. {
  1303. new_liquid_level = n2_liquid_level - 1;
  1304. }
  1305. if(new_liquid_level > new_liquid_level_max)
  1306. new_liquid_level_max = new_liquid_level;
  1307. }
  1308. }catch(InvalidPositionException &e)
  1309. {
  1310. }
  1311. } //for
  1312. /*
  1313. If liquid level should be something else, update it and
  1314. add all the neighboring water nodes to the transform queue.
  1315. */
  1316. if(new_liquid_level_max != liquid_level)
  1317. {
  1318. if(new_liquid_level_max == -1)
  1319. {
  1320. // Remove water alltoghether
  1321. n0.d = CONTENT_AIR;
  1322. n0.param2 = 0;
  1323. setNode(p0, n0);
  1324. }
  1325. else
  1326. {
  1327. n0.param2 = new_liquid_level_max;
  1328. setNode(p0, n0);
  1329. }
  1330. // Block has been modified
  1331. {
  1332. v3s16 blockpos = getNodeBlockPos(p0);
  1333. MapBlock *block = getBlockNoCreateNoEx(blockpos);
  1334. if(block != NULL)
  1335. modified_blocks.insert(blockpos, block);
  1336. }
  1337. /*
  1338. Add neighboring non-source liquid nodes to transform queue.
  1339. */
  1340. v3s16 dirs[6] = {
  1341. v3s16(0,0,1), // back
  1342. v3s16(0,1,0), // top
  1343. v3s16(1,0,0), // right
  1344. v3s16(0,0,-1), // front
  1345. v3s16(0,-1,0), // bottom
  1346. v3s16(-1,0,0), // left
  1347. };
  1348. for(u16 i=0; i<6; i++)
  1349. {
  1350. try
  1351. {
  1352. v3s16 p2 = p0 + dirs[i];
  1353. MapNode n2 = getNode(p2);
  1354. if(content_flowing_liquid(n2.d))
  1355. {
  1356. m_transforming_liquid.push_back(p2);
  1357. }
  1358. }catch(InvalidPositionException &e)
  1359. {
  1360. }
  1361. }
  1362. }
  1363. }
  1364. // Get a new one from queue if the node has turned into non-water
  1365. if(content_liquid(n0.d) == false)
  1366. continue;
  1367. /*
  1368. Flow water from this node
  1369. */
  1370. v3s16 dirs_to[5] = {
  1371. v3s16(0,-1,0), // bottom
  1372. v3s16(0,0,1), // back
  1373. v3s16(1,0,0), // right
  1374. v3s16(0,0,-1), // front
  1375. v3s16(-1,0,0), // left
  1376. };
  1377. for(u16 i=0; i<5; i++)
  1378. {
  1379. try
  1380. {
  1381. bool to_bottom = (i == 0);
  1382. // If liquid is at lowest possible height, it's not going
  1383. // anywhere except down
  1384. if(liquid_level == 0 && to_bottom == false)
  1385. continue;
  1386. u8 liquid_next_level = 0;
  1387. // If going to bottom
  1388. if(to_bottom)
  1389. {
  1390. //liquid_next_level = 7;
  1391. if(liquid_level >= 7 - WATER_DROP_BOOST)
  1392. liquid_next_level = 7;
  1393. else
  1394. liquid_next_level = liquid_level + WATER_DROP_BOOST;
  1395. }
  1396. else
  1397. liquid_next_level = liquid_level - 1;
  1398. bool n2_changed = false;
  1399. bool flowed = false;
  1400. v3s16 p2 = p0 + dirs_to[i];
  1401. MapNode n2 = getNode(p2);
  1402. //dstream<<"[1] n2.param="<<(int)n2.param<<std::endl;
  1403. if(content_liquid(n2.d))
  1404. {
  1405. u8 n2_nonsource_c = make_liquid_flowing(n2.d);
  1406. // Check that the liquids are the same type
  1407. if(n2_nonsource_c != nonsource_c)
  1408. {
  1409. dstream<<"WARNING: Not handling: different liquids"
  1410. " collide"<<std::endl;
  1411. continue;
  1412. }
  1413. bool n2_is_source = !content_flowing_liquid(n2.d);
  1414. u8 n2_liquid_level = 8;
  1415. if(n2_is_source == false)
  1416. n2_liquid_level = n2.param2 & 0x07;
  1417. if(to_bottom)
  1418. {
  1419. flowed = true;
  1420. }
  1421. if(n2_is_source)
  1422. {
  1423. // Just flow into the source, nothing changes.
  1424. // n2_changed is not set because destination didn't change
  1425. flowed = true;
  1426. }
  1427. else
  1428. {
  1429. if(liquid_next_level > liquid_level)
  1430. {
  1431. n2.param2 = liquid_next_level;
  1432. setNode(p2, n2);
  1433. n2_changed = true;
  1434. flowed = true;
  1435. }
  1436. }
  1437. }
  1438. else if(n2.d == CONTENT_AIR)
  1439. {
  1440. n2.d = nonsource_c;
  1441. n2.param2 = liquid_next_level;
  1442. setNode(p2, n2);
  1443. n2_changed = true;
  1444. flowed = true;
  1445. }
  1446. //dstream<<"[2] n2.param="<<(int)n2.param<<std::endl;
  1447. if(n2_changed)
  1448. {
  1449. m_transforming_liquid.push_back(p2);
  1450. v3s16 blockpos = getNodeBlockPos(p2);
  1451. MapBlock *block = getBlockNoCreateNoEx(blockpos);
  1452. if(block != NULL)
  1453. modified_blocks.insert(blockpos, block);
  1454. }
  1455. // If n2_changed to bottom, don't flow anywhere else
  1456. if(to_bottom && flowed && !is_source)
  1457. break;
  1458. }catch(InvalidPositionException &e)
  1459. {
  1460. }
  1461. }
  1462. loopcount++;
  1463. //if(loopcount >= 100000)
  1464. if(loopcount >= initial_size * 1)
  1465. break;
  1466. }
  1467. //dstream<<"Map::transformLiquids(): loopcount="<<loopcount<<std::endl;
  1468. }
  1469. NodeMetadata* Map::getNodeMetadata(v3s16 p)
  1470. {
  1471. v3s16 blockpos = getNodeBlockPos(p);
  1472. v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
  1473. MapBlock *block = getBlockNoCreateNoEx(blockpos);
  1474. if(block == NULL)
  1475. {
  1476. dstream<<"WARNING: Map::setNodeMetadata(): Block not found"
  1477. <<std::endl;
  1478. return NULL;
  1479. }
  1480. NodeMetadata *meta = block->m_node_metadata.get(p_rel);
  1481. return meta;
  1482. }
  1483. void Map::setNodeMetadata(v3s16 p, NodeMetadata *meta)
  1484. {
  1485. v3s16 blockpos = getNodeBlockPos(p);
  1486. v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
  1487. MapBlock *block = getBlockNoCreateNoEx(blockpos);
  1488. if(block == NULL)
  1489. {
  1490. dstream<<"WARNING: Map::setNodeMetadata(): Block not found"
  1491. <<std::endl;
  1492. return;
  1493. }
  1494. block->m_node_metadata.set(p_rel, meta);
  1495. }
  1496. void Map::removeNodeMetadata(v3s16 p)
  1497. {
  1498. v3s16 blockpos = getNodeBlockPos(p);
  1499. v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
  1500. MapBlock *block = getBlockNoCreateNoEx(blockpos);
  1501. if(block == NULL)
  1502. {
  1503. dstream<<"WARNING: Map::removeNodeMetadata(): Block not found"
  1504. <<std::endl;
  1505. return;
  1506. }
  1507. block->m_node_metadata.remove(p_rel);
  1508. }
  1509. void Map::nodeMetadataStep(float dtime,
  1510. core::map<v3s16, MapBlock*> &changed_blocks)
  1511. {
  1512. /*
  1513. NOTE:
  1514. Currently there is no way to ensure that all the necessary
  1515. blocks are loaded when this is run. (They might get unloaded)
  1516. NOTE: ^- Actually, that might not be so. In a quick test it
  1517. reloaded a block with a furnace when I walked back to it from
  1518. a distance.
  1519. */
  1520. core::map<v2s16, MapSector*>::Iterator si;
  1521. si = m_sectors.getIterator();
  1522. for(; si.atEnd() == false; si++)
  1523. {
  1524. MapSector *sector = si.getNode()->getValue();
  1525. core::list< MapBlock * > sectorblocks;
  1526. sector->getBlocks(sectorblocks);
  1527. core::list< MapBlock * >::Iterator i;
  1528. for(i=sectorblocks.begin(); i!=sectorblocks.end(); i++)
  1529. {
  1530. MapBlock *block = *i;
  1531. bool changed = block->m_node_metadata.step(dtime);
  1532. if(changed)
  1533. changed_blocks[block->getPos()] = block;
  1534. }
  1535. }
  1536. }
  1537. /*
  1538. ServerMap
  1539. */
  1540. ServerMap::ServerMap(std::string savedir):
  1541. Map(dout_server),
  1542. m_seed(0),
  1543. m_map_metadata_changed(true)
  1544. {
  1545. dstream<<__FUNCTION_NAME<<std::endl;
  1546. //m_chunksize = 64;
  1547. //m_chunksize = 16; // Too slow
  1548. m_chunksize = 8; // Takes a few seconds
  1549. //m_chunksize = 4;
  1550. //m_chunksize = 2;
  1551. m_seed = (((u64)(myrand()%0xffff)<<0)
  1552. + ((u64)(myrand()%0xffff)<<16)
  1553. + ((u64)(myrand()%0xffff)<<32)
  1554. + ((u64)(myrand()%0xffff)<<48));
  1555. /*
  1556. Experimental and debug stuff
  1557. */
  1558. {
  1559. }
  1560. /*
  1561. Try to load map; if not found, create a new one.
  1562. */
  1563. m_savedir = savedir;
  1564. m_map_saving_enabled = false;
  1565. try
  1566. {
  1567. // If directory exists, check contents and load if possible
  1568. if(fs::PathExists(m_savedir))
  1569. {
  1570. // If directory is empty, it is safe to save into it.
  1571. if(fs::GetDirListing(m_savedir).size() == 0)
  1572. {
  1573. dstream<<DTIME<<"Server: Empty save directory is valid."
  1574. <<std::endl;
  1575. m_map_saving_enabled = true;
  1576. }
  1577. else
  1578. {
  1579. try{
  1580. // Load map metadata (seed, chunksize)
  1581. loadMapMeta();
  1582. // Load chunk metadata
  1583. loadChunkMeta();
  1584. }
  1585. catch(FileNotGoodException &e){
  1586. dstream<<DTIME<<"WARNING: Server: Could not load "
  1587. <<"metafile(s). Disabling chunk-based "
  1588. <<"generation."<<std::endl;
  1589. m_chunksize = 0;
  1590. }
  1591. /*// Load sector (0,0) and throw and exception on fail
  1592. if(loadSectorFull(v2s16(0,0)) == false)
  1593. throw LoadError("Failed to load sector (0,0)");*/
  1594. /*dstream<<DTIME<<"Server: Successfully loaded chunk "
  1595. "metadata and sector (0,0) from "<<savedir<<
  1596. ", assuming valid save directory."
  1597. <<std::endl;*/
  1598. dstream<<DTIME<<"INFO: Server: Successfully loaded map "
  1599. <<"and chunk metadata from "<<savedir
  1600. <<", assuming valid save directory."
  1601. <<std::endl;
  1602. m_map_saving_enabled = true;
  1603. // Map loaded, not creating new one
  1604. return;
  1605. }
  1606. }
  1607. // If directory doesn't exist, it is safe to save to it
  1608. else{
  1609. m_map_saving_enabled = true;
  1610. }
  1611. }
  1612. catch(std::exception &e)
  1613. {
  1614. dstream<<DTIME<<"WARNING: Server: Failed to load map from "<<savedir
  1615. <<", exception: "<<e.what()<<std::endl;
  1616. dstream<<"Please remove the map or fix it."<<std::endl;
  1617. dstream<<"WARNING: Map saving will be disabled."<<std::endl;
  1618. }
  1619. dstream<<DTIME<<"INFO: Initializing new map."<<std::endl;
  1620. // Create zero sector
  1621. emergeSector(v2s16(0,0));
  1622. // Initially write whole map
  1623. save(false);
  1624. }
  1625. ServerMap::~ServerMap()
  1626. {
  1627. dstream<<__FUNCTION_NAME<<std::endl;
  1628. try
  1629. {
  1630. if(m_map_saving_enabled)
  1631. {
  1632. //save(false);
  1633. // Save only changed parts
  1634. save(true);
  1635. dstream<<DTIME<<"Server: saved map to "<<m_savedir<<std::endl;
  1636. }
  1637. else
  1638. {
  1639. dstream<<DTIME<<"Server: map not saved"<<std::endl;
  1640. }
  1641. }
  1642. catch(std::exception &e)
  1643. {
  1644. dstream<<DTIME<<"Server: Failed to save map to "<<m_savedir
  1645. <<", exception: "<<e.what()<<std::endl;
  1646. }
  1647. /*
  1648. Free all MapChunks
  1649. */
  1650. core::map<v2s16, MapChunk*>::Iterator i = m_chunks.getIterator();
  1651. for(; i.atEnd() == false; i++)
  1652. {
  1653. MapChunk *chunk = i.getNode()->getValue();
  1654. delete chunk;
  1655. }
  1656. }
  1657. /*
  1658. Some helper functions for the map generator
  1659. */
  1660. s16 find_ground_level(VoxelManipulator &vmanip, v2s16 p2d)
  1661. {
  1662. v3s16 em = vmanip.m_area.getExtent();
  1663. s16 y_nodes_max = vmanip.m_area.MaxEdge.Y;
  1664. s16 y_nodes_min = vmanip.m_area.MinEdge.Y;
  1665. u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
  1666. s16 y;
  1667. for(y=y_nodes_max; y>=y_nodes_min; y--)
  1668. {
  1669. MapNode &n = vmanip.m_data[i];
  1670. if(content_walkable(n.d))
  1671. break;
  1672. vmanip.m_area.add_y(em, i, -1);
  1673. }
  1674. if(y >= y_nodes_min)
  1675. return y;
  1676. else
  1677. return y_nodes_min;
  1678. }
  1679. s16 find_ground_level_clever(VoxelManipulator &vmanip, v2s16 p2d)
  1680. {
  1681. v3s16 em = vmanip.m_area.getExtent();
  1682. s16 y_nodes_max = vmanip.m_area.MaxEdge.Y;
  1683. s16 y_nodes_min = vmanip.m_area.MinEdge.Y;
  1684. u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
  1685. s16 y;
  1686. for(y=y_nodes_max; y>=y_nodes_min; y--)
  1687. {
  1688. MapNode &n = vmanip.m_data[i];
  1689. if(content_walkable(n.d)
  1690. && n.d != CONTENT_TREE
  1691. && n.d != CONTENT_LEAVES)
  1692. break;
  1693. vmanip.m_area.add_y(em, i, -1);
  1694. }
  1695. if(y >= y_nodes_min)
  1696. return y;
  1697. else
  1698. return y_nodes_min;
  1699. }
  1700. void make_tree(VoxelManipulator &vmanip, v3s16 p0)
  1701. {
  1702. MapNode treenode(CONTENT_TREE);
  1703. MapNode leavesnode(CONTENT_LEAVES);
  1704. s16 trunk_h = myrand_range(3, 6);
  1705. v3s16 p1 = p0;
  1706. for(s16 ii=0; ii<trunk_h; ii++)
  1707. {
  1708. if(vmanip.m_area.contains(p1))
  1709. vmanip.m_data[vmanip.m_area.index(p1)] = treenode;
  1710. p1.Y++;
  1711. }
  1712. // p1 is now the last piece of the trunk
  1713. p1.Y -= 1;
  1714. VoxelArea leaves_a(v3s16(-2,-2,-2), v3s16(2,2,2));
  1715. //SharedPtr<u8> leaves_d(new u8[leaves_a.getVolume()]);
  1716. Buffer<u8> leaves_d(leaves_a.getVolume());
  1717. for(s32 i=0; i<leaves_a.getVolume(); i++)
  1718. leaves_d[i] = 0;
  1719. // Force leaves at near the end of the trunk
  1720. {
  1721. s16 d = 1;
  1722. for(s16 z=-d; z<=d; z++)
  1723. for(s16 y=-d; y<=d; y++)
  1724. for(s16 x=-d; x<=d; x++)
  1725. {
  1726. leaves_d[leaves_a.index(v3s16(x,y,z))] = 1;
  1727. }
  1728. }
  1729. // Add leaves randomly
  1730. for(u32 iii=0; iii<7; iii++)
  1731. {
  1732. s16 d = 1;
  1733. v3s16 p(
  1734. myrand_range(leaves_a.MinEdge.X, leaves_a.MaxEdge.X-d),
  1735. myrand_range(leaves_a.MinEdge.Y, leaves_a.MaxEdge.Y-d),
  1736. myrand_range(leaves_a.MinEdge.Z, leaves_a.MaxEdge.Z-d)
  1737. );
  1738. for(s16 z=0; z<=d; z++)
  1739. for(s16 y=0; y<=d; y++)
  1740. for(s16 x=0; x<=d; x++)
  1741. {
  1742. leaves_d[leaves_a.index(p+v3s16(x,y,z))] = 1;
  1743. }
  1744. }
  1745. // Blit leaves to vmanip
  1746. for(s16 z=leaves_a.MinEdge.Z; z<=leaves_a.MaxEdge.Z; z++)
  1747. for(s16 y=leaves_a.MinEdge.Y; y<=leaves_a.MaxEdge.Y; y++)
  1748. for(s16 x=leaves_a.MinEdge.X; x<=leaves_a.MaxEdge.X; x++)
  1749. {
  1750. v3s16 p(x,y,z);
  1751. p += p1;
  1752. if(vmanip.m_area.contains(p) == false)
  1753. continue;
  1754. u32 vi = vmanip.m_area.index(p);
  1755. if(vmanip.m_data[vi].d != CONTENT_AIR)
  1756. continue;
  1757. u32 i = leaves_a.index(x,y,z);
  1758. if(leaves_d[i] == 1)
  1759. vmanip.m_data[vi] = leavesnode;
  1760. }
  1761. }
  1762. /*
  1763. Helpers for noise
  1764. */
  1765. /*// -1->0, 0->1, 1->0
  1766. double contour(double v)
  1767. {
  1768. v = fabs(v);
  1769. if(v >= 1.0)
  1770. return 0.0;
  1771. return (1.0-v);
  1772. }*/
  1773. /*
  1774. Noise functions. Make sure seed is mangled differently in each one.
  1775. */
  1776. // This affects the shape of the contour
  1777. #define CAVE_NOISE_SCALE 10.0
  1778. NoiseParams get_cave_noise1_params(u64 seed)
  1779. {
  1780. return NoiseParams(NOISE_PERLIN_CONTOUR, seed+52534, 5, 0.7,
  1781. 200, CAVE_NOISE_SCALE);
  1782. }
  1783. NoiseParams get_cave_noise2_params(u64 seed)
  1784. {
  1785. return NoiseParams(NOISE_PERLIN_CONTOUR_FLIP_YZ, seed+10325, 5, 0.7,
  1786. 200, CAVE_NOISE_SCALE);
  1787. }
  1788. #define CAVE_NOISE_THRESHOLD (2.5/CAVE_NOISE_SCALE)
  1789. bool is_cave(u64 seed, v3s16 p)
  1790. {
  1791. double d1 = noise3d_param(get_cave_noise1_params(seed), p.X,p.Y,p.Z);
  1792. double d2 = noise3d_param(get_cave_noise2_params(seed), p.X,p.Y,p.Z);
  1793. return d1*d2 > CAVE_NOISE_THRESHOLD;
  1794. }
  1795. // Amount of trees per area in nodes
  1796. double tree_amount_2d(u64 seed, v2s16 p)
  1797. {
  1798. double noise = noise2d_perlin(
  1799. 0.5+(float)p.X/250, 0.5+(float)p.Y/250,
  1800. seed+2, 5, 0.66);
  1801. double zeroval = -0.3;
  1802. if(noise < zeroval)
  1803. return 0;
  1804. else
  1805. return 0.04 * (noise-zeroval) / (1.0-zeroval);
  1806. }
  1807. #define AVERAGE_MUD_AMOUNT 4
  1808. double base_rock_level_2d(u64 seed, v2s16 p)
  1809. {
  1810. // The base ground level
  1811. double base = (double)WATER_LEVEL - (double)AVERAGE_MUD_AMOUNT
  1812. + 20. * noise2d_perlin(
  1813. 0.5+(float)p.X/500., 0.5+(float)p.Y/500.,
  1814. (seed>>32)+654879876, 6, 0.6);
  1815. /*// A bit hillier one
  1816. double base2 = WATER_LEVEL - 4.0 + 40. * noise2d_perlin(
  1817. 0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
  1818. (seed>>27)+90340, 6, 0.69);
  1819. if(base2 > base)
  1820. base = base2;*/
  1821. #if 1
  1822. // Higher ground level
  1823. double higher = (double)WATER_LEVEL + 25. + 35. * noise2d_perlin(
  1824. 0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
  1825. seed+85039, 5, 0.69);
  1826. //higher = 30; // For debugging
  1827. // Limit higher to at least base
  1828. if(higher < base)
  1829. higher = base;
  1830. // Steepness factor of cliffs
  1831. double b = 1.0 + 1.0 * noise2d_perlin(
  1832. 0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
  1833. seed-932, 7, 0.7);
  1834. b = rangelim(b, 0.0, 1000.0);
  1835. b = pow(b, 5);
  1836. b *= 7;
  1837. b = rangelim(b, 3.0, 1000.0);
  1838. //dstream<<"b="<<b<<std::endl;
  1839. //double b = 20;
  1840. // Offset to more low
  1841. double a_off = -0.2;
  1842. // High/low selector
  1843. /*double a = 0.5 + b * (a_off + noise2d_perlin(
  1844. 0.5+(float)p.X/500., 0.5+(float)p.Y/500.,
  1845. seed-359, 6, 0.7));*/
  1846. double a = (double)0.5 + b * (a_off + noise2d_perlin(
  1847. 0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
  1848. seed-359, 5, 0.60));
  1849. // Limit
  1850. a = rangelim(a, 0.0, 1.0);
  1851. //dstream<<"a="<<a<<std::endl;
  1852. double h = base*(1.0-a) + higher*a;
  1853. #else
  1854. double h = base;
  1855. #endif
  1856. return h;
  1857. }
  1858. double get_mud_add_amount(u64 seed, v2s16 p)
  1859. {
  1860. return ((float)AVERAGE_MUD_AMOUNT + 3.0 * noise2d_perlin(
  1861. 0.5+(float)p.X/200, 0.5+(float)p.Y/200,
  1862. seed+91013, 3, 0.55));
  1863. }
  1864. /*
  1865. Adds random objects to block, depending on the content of the block
  1866. */
  1867. void addRandomObjects(MapBlock *block)
  1868. {
  1869. for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
  1870. for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
  1871. {
  1872. bool last_node_walkable = false;
  1873. for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
  1874. {
  1875. v3s16 p(x0,y0,z0);
  1876. MapNode n = block->getNodeNoEx(p);
  1877. if(n.d == CONTENT_IGNORE)
  1878. continue;
  1879. if(content_features(n.d).liquid_type != LIQUID_NONE)
  1880. continue;
  1881. if(content_features(n.d).walkable)
  1882. {
  1883. last_node_walkable = true;
  1884. continue;
  1885. }
  1886. if(last_node_walkable)
  1887. {
  1888. // If block contains light information
  1889. if(content_features(n.d).param_type == CPT_LIGHT)
  1890. {
  1891. if(n.getLight(LIGHTBANK_DAY) <= 3)
  1892. {
  1893. if(myrand() % 300 == 0)
  1894. {
  1895. v3f pos_f = intToFloat(p+block->getPosRelative(), BS);
  1896. pos_f.Y -= BS*0.4;
  1897. ServerActiveObject *obj = new RatSAO(NULL, 0, pos_f);
  1898. std::string data = obj->getStaticData();
  1899. StaticObject s_obj(obj->getType(),
  1900. obj->getBasePosition(), data);
  1901. // Add some
  1902. block->m_static_objects.insert(0, s_obj);
  1903. block->m_static_objects.insert(0, s_obj);
  1904. block->m_static_objects.insert(0, s_obj);
  1905. block->m_static_objects.insert(0, s_obj);
  1906. block->m_static_objects.insert(0, s_obj);
  1907. block->m_static_objects.insert(0, s_obj);
  1908. delete obj;
  1909. }
  1910. if(myrand() % 300 == 0)
  1911. {
  1912. v3f pos_f = intToFloat(p+block->getPosRelative(), BS);
  1913. pos_f.Y -= BS*0.4;
  1914. ServerActiveObject *obj = new Oerkki1SAO(NULL,0,pos_f);
  1915. std::string data = obj->getStaticData();
  1916. StaticObject s_obj(obj->getType(),
  1917. obj->getBasePosition(), data);
  1918. // Add one
  1919. block->m_static_objects.insert(0, s_obj);
  1920. delete obj;
  1921. }
  1922. }
  1923. }
  1924. }
  1925. last_node_walkable = false;
  1926. }
  1927. }
  1928. block->setChangedFlag();
  1929. }
  1930. /*
  1931. This is the main map generation method
  1932. */
  1933. #define VMANIP_FLAG_DUNGEON VOXELFLAG_CHECKED1
  1934. void makeChunk(ChunkMakeData *data)
  1935. {
  1936. if(data->no_op)
  1937. return;
  1938. s16 y_nodes_min = data->y_blocks_min * MAP_BLOCKSIZE;
  1939. s16 y_nodes_max = data->y_blocks_max * MAP_BLOCKSIZE + MAP_BLOCKSIZE - 1;
  1940. s16 h_blocks = data->y_blocks_max - data->y_blocks_min + 1;
  1941. u32 relative_volume = (u32)data->sectorpos_base_size*MAP_BLOCKSIZE
  1942. *(u32)data->sectorpos_base_size*MAP_BLOCKSIZE
  1943. *(u32)h_blocks*MAP_BLOCKSIZE;
  1944. v3s16 bigarea_blocks_min(
  1945. data->sectorpos_bigbase.X,
  1946. data->y_blocks_min,
  1947. data->sectorpos_bigbase.Y
  1948. );
  1949. v3s16 bigarea_blocks_max(
  1950. data->sectorpos_bigbase.X + data->sectorpos_bigbase_size - 1,
  1951. data->y_blocks_max,
  1952. data->sectorpos_bigbase.Y + data->sectorpos_bigbase_size - 1
  1953. );
  1954. s16 lighting_min_d = 0-data->max_spread_amount;
  1955. s16 lighting_max_d = data->sectorpos_base_size*MAP_BLOCKSIZE
  1956. + data->max_spread_amount-1;
  1957. // Clear all flags
  1958. data->vmanip.clearFlag(0xff);
  1959. TimeTaker timer_generate("makeChunk() generate");
  1960. // Maximum height of the stone surface and obstacles.
  1961. // This is used to disable cave generation from going too high.
  1962. s16 stone_surface_max_y = 0;
  1963. /*
  1964. Generate general ground level to full area
  1965. */
  1966. {
  1967. // 22ms @cs=8
  1968. TimeTaker timer1("Generating ground level");
  1969. //NoiseBuffer noisebuf1;
  1970. //NoiseBuffer noisebuf2;
  1971. NoiseBuffer noisebuf_cave;
  1972. {
  1973. v3f minpos_f(
  1974. data->sectorpos_bigbase.X*MAP_BLOCKSIZE,
  1975. y_nodes_min,
  1976. data->sectorpos_bigbase.Y*MAP_BLOCKSIZE
  1977. );
  1978. v3f maxpos_f = minpos_f + v3f(
  1979. data->sectorpos_bigbase_size*MAP_BLOCKSIZE,
  1980. y_nodes_max-y_nodes_min,
  1981. data->sectorpos_bigbase_size*MAP_BLOCKSIZE
  1982. );
  1983. //v3f samplelength_f = v3f(4.0, 4.0, 4.0);
  1984. TimeTaker timer("noisebuf.create");
  1985. noisebuf_cave.create(get_cave_noise1_params(data->seed),
  1986. minpos_f.X, minpos_f.Y, minpos_f.Z,
  1987. maxpos_f.X, maxpos_f.Y, maxpos_f.Z,
  1988. 4, 4, 4);
  1989. noisebuf_cave.multiply(get_cave_noise2_params(data->seed));
  1990. /*noisebuf1.create(data->seed+25104, 6, 0.60, 200.0, false,
  1991. minpos_f.X, minpos_f.Y, minpos_f.Z,
  1992. maxpos_f.X, maxpos_f.Y, maxpos_f.Z,
  1993. samplelength_f.X, samplelength_f.Y, samplelength_f.Z);*/
  1994. /*noisebuf1.create(data->seed+25104, 3, 0.60, 25.0, false,
  1995. minpos_f.X, minpos_f.Y, minpos_f.Z,
  1996. maxpos_f.X, maxpos_f.Y, maxpos_f.Z,
  1997. samplelength_f.X, samplelength_f.Y, samplelength_f.Z);
  1998. noisebuf2.create(data->seed+25105, 4, 0.50, 200.0, false,
  1999. minpos_f.X, minpos_f.Y, minpos_f.Z,
  2000. maxpos_f.X, maxpos_f.Y, maxpos_f.Z,
  2001. samplelength_f.X, samplelength_f.Y, samplelength_f.Z);*/
  2002. }
  2003. #if 0
  2004. for(s16 x=0; x<data->sectorpos_bigbase_size*MAP_BLOCKSIZE; x++)
  2005. for(s16 z=0; z<data->sectorpos_bigbase_size*MAP_BLOCKSIZE; z++)
  2006. {
  2007. // Node position
  2008. v2s16 p2d = data->sectorpos_bigbase*MAP_BLOCKSIZE + v2s16(x,z);
  2009. // Ground height at this point
  2010. float surface_y_f = 0.0;
  2011. // Use perlin noise for ground height
  2012. surface_y_f = base_rock_level_2d(data->seed, p2d);
  2013. //surface_y_f = base_rock_level_2d(data->seed, p2d);
  2014. // Convert to integer
  2015. s16 surface_y = (s16)surface_y_f;
  2016. // Log it
  2017. if(surface_y > stone_surface_max_y)
  2018. stone_surface_max_y = surface_y;
  2019. /*
  2020. Fill ground with stone
  2021. */
  2022. {
  2023. // Use fast index incrementing
  2024. v3s16 em = data->vmanip.m_area.getExtent();
  2025. u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_nodes_min, p2d.Y));
  2026. for(s16 y=y_nodes_min; y<=y_nodes_max; y++)
  2027. {
  2028. // Skip if already generated.
  2029. // This is done here because there might be a cave at
  2030. // any point in ground, which could look like it
  2031. // wasn't generated.
  2032. if(data->vmanip.m_data[i].d != CONTENT_AIR)
  2033. break;
  2034. /*s16 noiseval = 50.0 * noise3d_perlin(
  2035. 0.5+(float)p2d.X/100.0,
  2036. 0.5+(float)y/100.0,
  2037. 0.5+(float)p2d.Y/100.0,
  2038. data->seed+123, 5, 0.5);*/
  2039. double noiseval = 64.0 * noisebuf1.get(p2d.X, y, p2d.Y);
  2040. /*double noiseval = 30.0 * noisebuf1.get(p2d.X, y, p2d.Y);
  2041. noiseval *= MYMAX(0, -0.2 + noisebuf2.get(p2d.X, y, p2d.Y));*/
  2042. //if(y < surface_y + noiseval)
  2043. if(noiseval > 0)
  2044. //if(noiseval > y)
  2045. data->vmanip.m_data[i].d = CONTENT_STONE;
  2046. data->vmanip.m_area.add_y(em, i, 1);
  2047. }
  2048. }
  2049. }
  2050. #endif
  2051. #if 1
  2052. for(s16 x=0; x<data->sectorpos_bigbase_size*MAP_BLOCKSIZE; x++)
  2053. for(s16 z=0; z<data->sectorpos_bigbase_size*MAP_BLOCKSIZE; z++)
  2054. {
  2055. // Node position
  2056. v2s16 p2d = data->sectorpos_bigbase*MAP_BLOCKSIZE + v2s16(x,z);
  2057. /*
  2058. Skip of already generated
  2059. */
  2060. /*{
  2061. v3s16 p(p2d.X, y_nodes_min, p2d.Y);
  2062. if(data->vmanip.m_data[data->vmanip.m_area.index(p)].d != CONTENT_AIR)
  2063. continue;
  2064. }*/
  2065. #define CAVE_TEST 0
  2066. #if CAVE_TEST
  2067. /*
  2068. Fill ground with stone
  2069. */
  2070. {
  2071. // Use fast index incrementing
  2072. v3s16 em = data->vmanip.m_area.getExtent();
  2073. u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_nodes_min, p2d.Y));
  2074. for(s16 y=y_nodes_min; y<=y_nodes_max; y++)
  2075. {
  2076. // Skip if already generated.
  2077. // This is done here because there might be a cave at
  2078. // any point in ground, which could look like it
  2079. // wasn't generated.
  2080. if(data->vmanip.m_data[i].d != CONTENT_AIR)
  2081. break;
  2082. //if(is_cave(data->seed, v3s16(p2d.X, y, p2d.Y)))
  2083. if(noisebuf_cave.get(p2d.X,y,p2d.Y) > CAVE_NOISE_THRESHOLD)
  2084. {
  2085. data->vmanip.m_data[i].d = CONTENT_STONE;
  2086. }
  2087. else
  2088. {
  2089. data->vmanip.m_data[i].d = CONTENT_AIR;
  2090. }
  2091. data->vmanip.m_area.add_y(em, i, 1);
  2092. }
  2093. }
  2094. #else
  2095. // Ground height at this point
  2096. float surface_y_f = 0.0;
  2097. // Use perlin noise for ground height
  2098. surface_y_f = base_rock_level_2d(data->seed, p2d);
  2099. // Convert to integer
  2100. s16 surface_y = (s16)surface_y_f;
  2101. // Log it
  2102. if(surface_y > stone_surface_max_y)
  2103. stone_surface_max_y = surface_y;
  2104. /*
  2105. Fill ground with stone
  2106. */
  2107. {
  2108. // Use fast index incrementing
  2109. v3s16 em = data->vmanip.m_area.getExtent();
  2110. u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_nodes_min, p2d.Y));
  2111. for(s16 y=y_nodes_min; y<surface_y && y<=y_nodes_max; y++)
  2112. {
  2113. // Skip if already generated.
  2114. // This is done here because there might be a cave at
  2115. // any point in ground, which could look like it
  2116. // wasn't generated.
  2117. if(data->vmanip.m_data[i].d != CONTENT_AIR)
  2118. break;
  2119. //if(is_cave(data->seed, v3s16(p2d.X, y, p2d.Y)))
  2120. if(noisebuf_cave.get(p2d.X,y,p2d.Y) > CAVE_NOISE_THRESHOLD)
  2121. {
  2122. // Set tunnel flag
  2123. data->vmanip.m_flags[i] |= VMANIP_FLAG_DUNGEON;
  2124. // Is now air
  2125. data->vmanip.m_data[i].d = CONTENT_AIR;
  2126. }
  2127. else
  2128. {
  2129. data->vmanip.m_data[i].d = CONTENT_STONE;
  2130. }
  2131. data->vmanip.m_area.add_y(em, i, 1);
  2132. }
  2133. }
  2134. #endif // !CAVE_TEST
  2135. }
  2136. #endif
  2137. }//timer1
  2138. /*
  2139. Randomize some parameters
  2140. */
  2141. //s32 stone_obstacle_count = 0;
  2142. /*s32 stone_obstacle_count =
  2143. rangelim((1.0+noise2d(data->seed+897,
  2144. data->sectorpos_base.X, data->sectorpos_base.Y))/2.0 * 30, 0, 100000);*/
  2145. //s16 stone_obstacle_max_height = 0;
  2146. /*s16 stone_obstacle_max_height =
  2147. rangelim((1.0+noise2d(data->seed+5902,
  2148. data->sectorpos_base.X, data->sectorpos_base.Y))/2.0 * 30, 0, 100000);*/
  2149. // Set to 0 to disable everything but lighting
  2150. #if 1
  2151. /*
  2152. Loop this part, it will make stuff look older and newer nicely
  2153. */
  2154. const u32 age_loops = 2;
  2155. for(u32 i_age=0; i_age<age_loops; i_age++)
  2156. { // Aging loop
  2157. /******************************
  2158. BEGINNING OF AGING LOOP
  2159. ******************************/
  2160. #if 1
  2161. {
  2162. // 24ms @cs=8
  2163. //TimeTaker timer1("caves");
  2164. /*
  2165. Make caves
  2166. */
  2167. //u32 caves_count = relative_volume / 400000;
  2168. u32 caves_count = 0;
  2169. u32 bruises_count = relative_volume * stone_surface_max_y / 40000000;
  2170. if(stone_surface_max_y < WATER_LEVEL)
  2171. bruises_count = 0;
  2172. /*u32 caves_count = 0;
  2173. u32 bruises_count = 0;*/
  2174. for(u32 jj=0; jj<caves_count+bruises_count; jj++)
  2175. {
  2176. s16 min_tunnel_diameter = 3;
  2177. s16 max_tunnel_diameter = 5;
  2178. u16 tunnel_routepoints = 20;
  2179. v3f main_direction(0,0,0);
  2180. bool bruise_surface = (jj > caves_count);
  2181. if(bruise_surface)
  2182. {
  2183. min_tunnel_diameter = 5;
  2184. max_tunnel_diameter = myrand_range(10, 20);
  2185. /*min_tunnel_diameter = MYMAX(0, stone_surface_max_y/6);
  2186. max_tunnel_diameter = myrand_range(MYMAX(0, stone_surface_max_y/6), MYMAX(0, stone_surface_max_y/2));*/
  2187. /*s16 tunnel_rou = rangelim(25*(0.5+1.0*noise2d(data->seed+42,
  2188. data->sectorpos_base.X, data->sectorpos_base.Y)), 0, 15);*/
  2189. tunnel_routepoints = 5;
  2190. }
  2191. else
  2192. {
  2193. }
  2194. // Allowed route area size in nodes
  2195. v3s16 ar(
  2196. data->sectorpos_base_size*MAP_BLOCKSIZE,
  2197. h_blocks*MAP_BLOCKSIZE,
  2198. data->sectorpos_base_size*MAP_BLOCKSIZE
  2199. );
  2200. // Area starting point in nodes
  2201. v3s16 of(
  2202. data->sectorpos_base.X*MAP_BLOCKSIZE,
  2203. data->y_blocks_min*MAP_BLOCKSIZE,
  2204. data->sectorpos_base.Y*MAP_BLOCKSIZE
  2205. );
  2206. // Allow a bit more
  2207. //(this should be more than the maximum radius of the tunnel)
  2208. //s16 insure = 5; // Didn't work with max_d = 20
  2209. s16 insure = 10;
  2210. s16 more = data->max_spread_amount - max_tunnel_diameter/2 - insure;
  2211. ar += v3s16(1,0,1) * more * 2;
  2212. of -= v3s16(1,0,1) * more;
  2213. s16 route_y_min = 0;
  2214. // Allow half a diameter + 7 over stone surface
  2215. s16 route_y_max = -of.Y + stone_surface_max_y + max_tunnel_diameter/2 + 7;
  2216. /*// If caves, don't go through surface too often
  2217. if(bruise_surface == false)
  2218. route_y_max -= myrand_range(0, max_tunnel_diameter*2);*/
  2219. // Limit maximum to area
  2220. route_y_max = rangelim(route_y_max, 0, ar.Y-1);
  2221. if(bruise_surface)
  2222. {
  2223. /*// Minimum is at y=0
  2224. route_y_min = -of.Y - 0;*/
  2225. // Minimum is at y=max_tunnel_diameter/4
  2226. //route_y_min = -of.Y + max_tunnel_diameter/4;
  2227. //s16 min = -of.Y + max_tunnel_diameter/4;
  2228. s16 min = -of.Y + 0;
  2229. route_y_min = myrand_range(min, min + max_tunnel_diameter);
  2230. route_y_min = rangelim(route_y_min, 0, route_y_max);
  2231. }
  2232. /*dstream<<"route_y_min = "<<route_y_min
  2233. <<", route_y_max = "<<route_y_max<<std::endl;*/
  2234. s16 route_start_y_min = route_y_min;
  2235. s16 route_start_y_max = route_y_max;
  2236. // Start every 2nd cave from surface
  2237. bool coming_from_surface = (jj % 2 == 0 && bruise_surface == false);
  2238. if(coming_from_surface)
  2239. {
  2240. route_start_y_min = -of.Y + stone_surface_max_y + 10;
  2241. }
  2242. route_start_y_min = rangelim(route_start_y_min, 0, ar.Y-1);
  2243. route_start_y_max = rangelim(route_start_y_max, route_start_y_min, ar.Y-1);
  2244. // Randomize starting position
  2245. v3f orp(
  2246. (float)(myrand()%ar.X)+0.5,
  2247. (float)(myrand_range(route_start_y_min, route_start_y_max))+0.5,
  2248. (float)(myrand()%ar.Z)+0.5
  2249. );
  2250. MapNode airnode(CONTENT_AIR);
  2251. /*
  2252. Generate some tunnel starting from orp
  2253. */
  2254. for(u16 j=0; j<tunnel_routepoints; j++)
  2255. {
  2256. if(j%7==0 && bruise_surface == false)
  2257. {
  2258. main_direction = v3f(
  2259. ((float)(myrand()%20)-(float)10)/10,
  2260. ((float)(myrand()%20)-(float)10)/30,
  2261. ((float)(myrand()%20)-(float)10)/10
  2262. );
  2263. main_direction *= (float)myrand_range(1, 3);
  2264. }
  2265. // Randomize size
  2266. s16 min_d = min_tunnel_diameter;
  2267. s16 max_d = max_tunnel_diameter;
  2268. s16 rs = myrand_range(min_d, max_d);
  2269. v3s16 maxlen;
  2270. if(bruise_surface)
  2271. {
  2272. maxlen = v3s16(rs*7,rs*7,rs*7);
  2273. }
  2274. else
  2275. {
  2276. maxlen = v3s16(rs*4, myrand_range(1, rs*3), rs*4);
  2277. }
  2278. v3f vec;
  2279. if(coming_from_surface && j < 3)
  2280. {
  2281. vec = v3f(
  2282. (float)(myrand()%(maxlen.X*2))-(float)maxlen.X,
  2283. (float)(myrand()%(maxlen.Y*1))-(float)maxlen.Y,
  2284. (float)(myrand()%(maxlen.Z*2))-(float)maxlen.Z
  2285. );
  2286. }
  2287. else
  2288. {
  2289. vec = v3f(
  2290. (float)(myrand()%(maxlen.X*2))-(float)maxlen.X,
  2291. (float)(myrand()%(maxlen.Y*2))-(float)maxlen.Y,
  2292. (float)(myrand()%(maxlen.Z*2))-(float)maxlen.Z
  2293. );
  2294. }
  2295. vec += main_direction;
  2296. v3f rp = orp + vec;
  2297. if(rp.X < 0)
  2298. rp.X = 0;
  2299. else if(rp.X >= ar.X)
  2300. rp.X = ar.X-1;
  2301. if(rp.Y < route_y_min)
  2302. rp.Y = route_y_min;
  2303. else if(rp.Y >= route_y_max)
  2304. rp.Y = route_y_max-1;
  2305. if(rp.Z < 0)
  2306. rp.Z = 0;
  2307. else if(rp.Z >= ar.Z)
  2308. rp.Z = ar.Z-1;
  2309. vec = rp - orp;
  2310. for(float f=0; f<1.0; f+=1.0/vec.getLength())
  2311. {
  2312. v3f fp = orp + vec * f;
  2313. v3s16 cp(fp.X, fp.Y, fp.Z);
  2314. s16 d0 = -rs/2;
  2315. s16 d1 = d0 + rs - 1;
  2316. for(s16 z0=d0; z0<=d1; z0++)
  2317. {
  2318. //s16 si = rs - MYMAX(0, abs(z0)-rs/4);
  2319. s16 si = rs - MYMAX(0, abs(z0)-rs/7);
  2320. for(s16 x0=-si; x0<=si-1; x0++)
  2321. {
  2322. s16 maxabsxz = MYMAX(abs(x0), abs(z0));
  2323. //s16 si2 = rs - MYMAX(0, maxabsxz-rs/4);
  2324. s16 si2 = rs - MYMAX(0, maxabsxz-rs/7);
  2325. //s16 si2 = rs - abs(x0);
  2326. for(s16 y0=-si2+1+2; y0<=si2-1; y0++)
  2327. {
  2328. s16 z = cp.Z + z0;
  2329. s16 y = cp.Y + y0;
  2330. s16 x = cp.X + x0;
  2331. v3s16 p(x,y,z);
  2332. /*if(isInArea(p, ar) == false)
  2333. continue;*/
  2334. // Check only height
  2335. if(y < 0 || y >= ar.Y)
  2336. continue;
  2337. p += of;
  2338. //assert(data->vmanip.m_area.contains(p));
  2339. if(data->vmanip.m_area.contains(p) == false)
  2340. {
  2341. dstream<<"WARNING: "<<__FUNCTION_NAME
  2342. <<":"<<__LINE__<<": "
  2343. <<"point not in area"
  2344. <<std::endl;
  2345. continue;
  2346. }
  2347. // Just set it to air, it will be changed to
  2348. // water afterwards
  2349. u32 i = data->vmanip.m_area.index(p);
  2350. data->vmanip.m_data[i] = airnode;
  2351. if(bruise_surface == false)
  2352. {
  2353. // Set tunnel flag
  2354. data->vmanip.m_flags[i] |= VMANIP_FLAG_DUNGEON;
  2355. }
  2356. }
  2357. }
  2358. }
  2359. }
  2360. orp = rp;
  2361. }
  2362. }
  2363. }//timer1
  2364. #endif
  2365. #if 1
  2366. {
  2367. // 46ms @cs=8
  2368. //TimeTaker timer1("ore veins");
  2369. /*
  2370. Make ore veins
  2371. */
  2372. for(u32 jj=0; jj<relative_volume/1000; jj++)
  2373. {
  2374. s16 max_vein_diameter = 3;
  2375. // Allowed route area size in nodes
  2376. v3s16 ar(
  2377. data->sectorpos_base_size*MAP_BLOCKSIZE,
  2378. h_blocks*MAP_BLOCKSIZE,
  2379. data->sectorpos_base_size*MAP_BLOCKSIZE
  2380. );
  2381. // Area starting point in nodes
  2382. v3s16 of(
  2383. data->sectorpos_base.X*MAP_BLOCKSIZE,
  2384. data->y_blocks_min*MAP_BLOCKSIZE,
  2385. data->sectorpos_base.Y*MAP_BLOCKSIZE
  2386. );
  2387. // Allow a bit more
  2388. //(this should be more than the maximum radius of the tunnel)
  2389. s16 insure = 3;
  2390. s16 more = data->max_spread_amount - max_vein_diameter/2 - insure;
  2391. ar += v3s16(1,0,1) * more * 2;
  2392. of -= v3s16(1,0,1) * more;
  2393. // Randomize starting position
  2394. v3f orp(
  2395. (float)(myrand()%ar.X)+0.5,
  2396. (float)(myrand()%ar.Y)+0.5,
  2397. (float)(myrand()%ar.Z)+0.5
  2398. );
  2399. // Randomize mineral
  2400. u8 mineral;
  2401. if(myrand()%3 != 0)
  2402. mineral = MINERAL_COAL;
  2403. else
  2404. mineral = MINERAL_IRON;
  2405. /*
  2406. Generate some vein starting from orp
  2407. */
  2408. for(u16 j=0; j<2; j++)
  2409. {
  2410. /*v3f rp(
  2411. (float)(myrand()%ar.X)+0.5,
  2412. (float)(myrand()%ar.Y)+0.5,
  2413. (float)(myrand()%ar.Z)+0.5
  2414. );
  2415. v3f vec = rp - orp;*/
  2416. v3s16 maxlen(5, 5, 5);
  2417. v3f vec(
  2418. (float)(myrand()%(maxlen.X*2))-(float)maxlen.X,
  2419. (float)(myrand()%(maxlen.Y*2))-(float)maxlen.Y,
  2420. (float)(myrand()%(maxlen.Z*2))-(float)maxlen.Z
  2421. );
  2422. v3f rp = orp + vec;
  2423. if(rp.X < 0)
  2424. rp.X = 0;
  2425. else if(rp.X >= ar.X)
  2426. rp.X = ar.X;
  2427. if(rp.Y < 0)
  2428. rp.Y = 0;
  2429. else if(rp.Y >= ar.Y)
  2430. rp.Y = ar.Y;
  2431. if(rp.Z < 0)
  2432. rp.Z = 0;
  2433. else if(rp.Z >= ar.Z)
  2434. rp.Z = ar.Z;
  2435. vec = rp - orp;
  2436. // Randomize size
  2437. s16 min_d = 0;
  2438. s16 max_d = max_vein_diameter;
  2439. s16 rs = myrand_range(min_d, max_d);
  2440. for(float f=0; f<1.0; f+=1.0/vec.getLength())
  2441. {
  2442. v3f fp = orp + vec * f;
  2443. v3s16 cp(fp.X, fp.Y, fp.Z);
  2444. s16 d0 = -rs/2;
  2445. s16 d1 = d0 + rs - 1;
  2446. for(s16 z0=d0; z0<=d1; z0++)
  2447. {
  2448. s16 si = rs - abs(z0);
  2449. for(s16 x0=-si; x0<=si-1; x0++)
  2450. {
  2451. s16 si2 = rs - abs(x0);
  2452. for(s16 y0=-si2+1; y0<=si2-1; y0++)
  2453. {
  2454. // Don't put mineral to every place
  2455. if(myrand()%5 != 0)
  2456. continue;
  2457. s16 z = cp.Z + z0;
  2458. s16 y = cp.Y + y0;
  2459. s16 x = cp.X + x0;
  2460. v3s16 p(x,y,z);
  2461. /*if(isInArea(p, ar) == false)
  2462. continue;*/
  2463. // Check only height
  2464. if(y < 0 || y >= ar.Y)
  2465. continue;
  2466. p += of;
  2467. assert(data->vmanip.m_area.contains(p));
  2468. // Just set it to air, it will be changed to
  2469. // water afterwards
  2470. u32 i = data->vmanip.m_area.index(p);
  2471. MapNode *n = &data->vmanip.m_data[i];
  2472. if(n->d == CONTENT_STONE)
  2473. n->param = mineral;
  2474. }
  2475. }
  2476. }
  2477. }
  2478. orp = rp;
  2479. }
  2480. }
  2481. }//timer1
  2482. #endif
  2483. #if 1
  2484. {
  2485. // 15ms @cs=8
  2486. TimeTaker timer1("add mud");
  2487. /*
  2488. Add mud to the central chunk
  2489. */
  2490. for(s16 x=0; x<data->sectorpos_base_size*MAP_BLOCKSIZE; x++)
  2491. for(s16 z=0; z<data->sectorpos_base_size*MAP_BLOCKSIZE; z++)
  2492. {
  2493. // Node position in 2d
  2494. v2s16 p2d = data->sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
  2495. // Randomize mud amount
  2496. s16 mud_add_amount = get_mud_add_amount(data->seed, p2d) / 2.0;
  2497. // Find ground level
  2498. s16 surface_y = find_ground_level_clever(data->vmanip, p2d);
  2499. /*
  2500. If topmost node is grass, change it to mud.
  2501. It might be if it was flown to there from a neighboring
  2502. chunk and then converted.
  2503. */
  2504. {
  2505. u32 i = data->vmanip.m_area.index(v3s16(p2d.X, surface_y, p2d.Y));
  2506. MapNode *n = &data->vmanip.m_data[i];
  2507. if(n->d == CONTENT_GRASS)
  2508. *n = MapNode(CONTENT_MUD);
  2509. //n->d = CONTENT_MUD;
  2510. }
  2511. /*
  2512. Add mud on ground
  2513. */
  2514. {
  2515. s16 mudcount = 0;
  2516. v3s16 em = data->vmanip.m_area.getExtent();
  2517. s16 y_start = surface_y+1;
  2518. u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
  2519. for(s16 y=y_start; y<=y_nodes_max; y++)
  2520. {
  2521. if(mudcount >= mud_add_amount)
  2522. break;
  2523. MapNode &n = data->vmanip.m_data[i];
  2524. n = MapNode(CONTENT_MUD);
  2525. //n.d = CONTENT_MUD;
  2526. mudcount++;
  2527. data->vmanip.m_area.add_y(em, i, 1);
  2528. }
  2529. }
  2530. }
  2531. }//timer1
  2532. #endif
  2533. #if 1
  2534. {
  2535. // 340ms @cs=8
  2536. TimeTaker timer1("flow mud");
  2537. /*
  2538. Flow mud away from steep edges
  2539. */
  2540. // Limit area by 1 because mud is flown into neighbors.
  2541. s16 mudflow_minpos = 0-data->max_spread_amount+1;
  2542. s16 mudflow_maxpos = data->sectorpos_base_size*MAP_BLOCKSIZE+data->max_spread_amount-2;
  2543. // Iterate a few times
  2544. for(s16 k=0; k<3; k++)
  2545. {
  2546. for(s16 x=mudflow_minpos;
  2547. x<=mudflow_maxpos;
  2548. x++)
  2549. for(s16 z=mudflow_minpos;
  2550. z<=mudflow_maxpos;
  2551. z++)
  2552. {
  2553. // Invert coordinates every 2nd iteration
  2554. if(k%2 == 0)
  2555. {
  2556. x = mudflow_maxpos - (x-mudflow_minpos);
  2557. z = mudflow_maxpos - (z-mudflow_minpos);
  2558. }
  2559. // Node position in 2d
  2560. v2s16 p2d = data->sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
  2561. v3s16 em = data->vmanip.m_area.getExtent();
  2562. u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
  2563. s16 y=y_nodes_max;
  2564. for(;; y--)
  2565. {
  2566. MapNode *n = NULL;
  2567. // Find mud
  2568. for(; y>=y_nodes_min; y--)
  2569. {
  2570. n = &data->vmanip.m_data[i];
  2571. //if(content_walkable(n->d))
  2572. // break;
  2573. if(n->d == CONTENT_MUD || n->d == CONTENT_GRASS)
  2574. break;
  2575. data->vmanip.m_area.add_y(em, i, -1);
  2576. }
  2577. // Stop if out of area
  2578. //if(data->vmanip.m_area.contains(i) == false)
  2579. if(y < y_nodes_min)
  2580. break;
  2581. /*// If not mud, do nothing to it
  2582. MapNode *n = &data->vmanip.m_data[i];
  2583. if(n->d != CONTENT_MUD && n->d != CONTENT_GRASS)
  2584. continue;*/
  2585. /*
  2586. Don't flow it if the stuff under it is not mud
  2587. */
  2588. {
  2589. u32 i2 = i;
  2590. data->vmanip.m_area.add_y(em, i2, -1);
  2591. // Cancel if out of area
  2592. if(data->vmanip.m_area.contains(i2) == false)
  2593. continue;
  2594. MapNode *n2 = &data->vmanip.m_data[i2];
  2595. if(n2->d != CONTENT_MUD && n2->d != CONTENT_GRASS)
  2596. continue;
  2597. }
  2598. // Make it exactly mud
  2599. n->d = CONTENT_MUD;
  2600. /*s16 recurse_count = 0;
  2601. mudflow_recurse:*/
  2602. v3s16 dirs4[4] = {
  2603. v3s16(0,0,1), // back
  2604. v3s16(1,0,0), // right
  2605. v3s16(0,0,-1), // front
  2606. v3s16(-1,0,0), // left
  2607. };
  2608. // Theck that upper is air or doesn't exist.
  2609. // Cancel dropping if upper keeps it in place
  2610. u32 i3 = i;
  2611. data->vmanip.m_area.add_y(em, i3, 1);
  2612. if(data->vmanip.m_area.contains(i3) == true
  2613. && content_walkable(data->vmanip.m_data[i3].d) == true)
  2614. {
  2615. continue;
  2616. }
  2617. // Drop mud on side
  2618. for(u32 di=0; di<4; di++)
  2619. {
  2620. v3s16 dirp = dirs4[di];
  2621. u32 i2 = i;
  2622. // Move to side
  2623. data->vmanip.m_area.add_p(em, i2, dirp);
  2624. // Fail if out of area
  2625. if(data->vmanip.m_area.contains(i2) == false)
  2626. continue;
  2627. // Check that side is air
  2628. MapNode *n2 = &data->vmanip.m_data[i2];
  2629. if(content_walkable(n2->d))
  2630. continue;
  2631. // Check that under side is air
  2632. data->vmanip.m_area.add_y(em, i2, -1);
  2633. if(data->vmanip.m_area.contains(i2) == false)
  2634. continue;
  2635. n2 = &data->vmanip.m_data[i2];
  2636. if(content_walkable(n2->d))
  2637. continue;
  2638. /*// Check that under that is air (need a drop of 2)
  2639. data->vmanip.m_area.add_y(em, i2, -1);
  2640. if(data->vmanip.m_area.contains(i2) == false)
  2641. continue;
  2642. n2 = &data->vmanip.m_data[i2];
  2643. if(content_walkable(n2->d))
  2644. continue;*/
  2645. // Loop further down until not air
  2646. do{
  2647. data->vmanip.m_area.add_y(em, i2, -1);
  2648. // Fail if out of area
  2649. if(data->vmanip.m_area.contains(i2) == false)
  2650. continue;
  2651. n2 = &data->vmanip.m_data[i2];
  2652. }while(content_walkable(n2->d) == false);
  2653. // Loop one up so that we're in air
  2654. data->vmanip.m_area.add_y(em, i2, 1);
  2655. n2 = &data->vmanip.m_data[i2];
  2656. // Move mud to new place
  2657. *n2 = *n;
  2658. // Set old place to be air
  2659. *n = MapNode(CONTENT_AIR);
  2660. // Done
  2661. break;
  2662. }
  2663. }
  2664. }
  2665. }
  2666. }//timer1
  2667. #endif
  2668. #if 1
  2669. {
  2670. // 50ms @cs=8
  2671. TimeTaker timer1("add water");
  2672. /*
  2673. Add water to the central chunk (and a bit more)
  2674. */
  2675. for(s16 x=0-data->max_spread_amount;
  2676. x<data->sectorpos_base_size*MAP_BLOCKSIZE+data->max_spread_amount;
  2677. x++)
  2678. for(s16 z=0-data->max_spread_amount;
  2679. z<data->sectorpos_base_size*MAP_BLOCKSIZE+data->max_spread_amount;
  2680. z++)
  2681. {
  2682. // Node position in 2d
  2683. v2s16 p2d = data->sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
  2684. // Find ground level
  2685. //s16 surface_y = find_ground_level(data->vmanip, p2d);
  2686. /*
  2687. If ground level is over water level, skip.
  2688. NOTE: This leaves caves near water without water,
  2689. which looks especially crappy when the nearby water
  2690. won't start flowing either for some reason
  2691. */
  2692. /*if(surface_y > WATER_LEVEL)
  2693. continue;*/
  2694. /*
  2695. Add water on ground
  2696. */
  2697. {
  2698. v3s16 em = data->vmanip.m_area.getExtent();
  2699. u8 light = LIGHT_MAX;
  2700. // Start at global water surface level
  2701. s16 y_start = WATER_LEVEL;
  2702. u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
  2703. MapNode *n = &data->vmanip.m_data[i];
  2704. for(s16 y=y_start; y>=y_nodes_min; y--)
  2705. {
  2706. n = &data->vmanip.m_data[i];
  2707. // Stop when there is no water and no air
  2708. if(n->d != CONTENT_AIR && n->d != CONTENT_WATERSOURCE
  2709. && n->d != CONTENT_WATER)
  2710. {
  2711. break;
  2712. }
  2713. // Make water only not in caves
  2714. if(!(data->vmanip.m_flags[i]&VMANIP_FLAG_DUNGEON))
  2715. {
  2716. n->d = CONTENT_WATERSOURCE;
  2717. //n->setLight(LIGHTBANK_DAY, light);
  2718. // Add to transforming liquid queue (in case it'd
  2719. // start flowing)
  2720. v3s16 p = v3s16(p2d.X, y, p2d.Y);
  2721. data->transforming_liquid.push_back(p);
  2722. }
  2723. // Next one
  2724. data->vmanip.m_area.add_y(em, i, -1);
  2725. if(light > 0)
  2726. light--;
  2727. }
  2728. }
  2729. }
  2730. }//timer1
  2731. #endif
  2732. } // Aging loop
  2733. /***********************
  2734. END OF AGING LOOP
  2735. ************************/
  2736. #if 1
  2737. {
  2738. //TimeTaker timer1("convert mud to sand");
  2739. /*
  2740. Convert mud to sand
  2741. */
  2742. //s16 mud_add_amount = myrand_range(2, 4);
  2743. //s16 mud_add_amount = 0;
  2744. /*for(s16 x=0; x<data->sectorpos_base_size*MAP_BLOCKSIZE; x++)
  2745. for(s16 z=0; z<data->sectorpos_base_size*MAP_BLOCKSIZE; z++)*/
  2746. for(s16 x=0-data->max_spread_amount+1;
  2747. x<data->sectorpos_base_size*MAP_BLOCKSIZE+data->max_spread_amount-1;
  2748. x++)
  2749. for(s16 z=0-data->max_spread_amount+1;
  2750. z<data->sectorpos_base_size*MAP_BLOCKSIZE+data->max_spread_amount-1;
  2751. z++)
  2752. {
  2753. // Node position in 2d
  2754. v2s16 p2d = data->sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
  2755. // Determine whether to have sand here
  2756. double sandnoise = noise2d_perlin(
  2757. 0.5+(float)p2d.X/500, 0.5+(float)p2d.Y/500,
  2758. data->seed+59420, 3, 0.50);
  2759. bool have_sand = (sandnoise > -0.15);
  2760. if(have_sand == false)
  2761. continue;
  2762. // Find ground level
  2763. s16 surface_y = find_ground_level_clever(data->vmanip, p2d);
  2764. if(surface_y > WATER_LEVEL + 2)
  2765. continue;
  2766. {
  2767. v3s16 em = data->vmanip.m_area.getExtent();
  2768. s16 y_start = surface_y;
  2769. u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
  2770. u32 not_sand_counter = 0;
  2771. for(s16 y=y_start; y>=y_nodes_min; y--)
  2772. {
  2773. MapNode *n = &data->vmanip.m_data[i];
  2774. if(n->d == CONTENT_MUD || n->d == CONTENT_GRASS)
  2775. {
  2776. n->d = CONTENT_SAND;
  2777. }
  2778. else
  2779. {
  2780. not_sand_counter++;
  2781. if(not_sand_counter > 3)
  2782. break;
  2783. }
  2784. data->vmanip.m_area.add_y(em, i, -1);
  2785. }
  2786. }
  2787. }
  2788. }//timer1
  2789. #endif
  2790. #if 1
  2791. {
  2792. // 1ms @cs=8
  2793. //TimeTaker timer1("generate trees");
  2794. /*
  2795. Generate some trees
  2796. */
  2797. {
  2798. // Divide area into parts
  2799. s16 div = 8;
  2800. s16 sidelen = data->sectorpos_base_size*MAP_BLOCKSIZE / div;
  2801. double area = sidelen * sidelen;
  2802. for(s16 x0=0; x0<div; x0++)
  2803. for(s16 z0=0; z0<div; z0++)
  2804. {
  2805. // Center position of part of division
  2806. v2s16 p2d_center(
  2807. data->sectorpos_base.X*MAP_BLOCKSIZE + sidelen/2 + sidelen*x0,
  2808. data->sectorpos_base.Y*MAP_BLOCKSIZE + sidelen/2 + sidelen*z0
  2809. );
  2810. // Minimum edge of part of division
  2811. v2s16 p2d_min(
  2812. data->sectorpos_base.X*MAP_BLOCKSIZE + sidelen*x0,
  2813. data->sectorpos_base.Y*MAP_BLOCKSIZE + sidelen*z0
  2814. );
  2815. // Maximum edge of part of division
  2816. v2s16 p2d_max(
  2817. data->sectorpos_base.X*MAP_BLOCKSIZE + sidelen + sidelen*x0 - 1,
  2818. data->sectorpos_base.Y*MAP_BLOCKSIZE + sidelen + sidelen*z0 - 1
  2819. );
  2820. // Amount of trees
  2821. u32 tree_count = area * tree_amount_2d(data->seed, p2d_center);
  2822. // Put trees in random places on part of division
  2823. for(u32 i=0; i<tree_count; i++)
  2824. {
  2825. s16 x = myrand_range(p2d_min.X, p2d_max.X);
  2826. s16 z = myrand_range(p2d_min.Y, p2d_max.Y);
  2827. s16 y = find_ground_level(data->vmanip, v2s16(x,z));
  2828. // Don't make a tree under water level
  2829. if(y < WATER_LEVEL)
  2830. continue;
  2831. // Don't make a tree so high that it doesn't fit
  2832. if(y > y_nodes_max - 6)
  2833. continue;
  2834. v3s16 p(x,y,z);
  2835. /*
  2836. Trees grow only on mud and grass
  2837. */
  2838. {
  2839. u32 i = data->vmanip.m_area.index(v3s16(p));
  2840. MapNode *n = &data->vmanip.m_data[i];
  2841. if(n->d != CONTENT_MUD && n->d != CONTENT_GRASS)
  2842. continue;
  2843. }
  2844. p.Y++;
  2845. // Make a tree
  2846. make_tree(data->vmanip, p);
  2847. }
  2848. }
  2849. /*u32 tree_max = relative_area / 60;
  2850. //u32 count = myrand_range(0, tree_max);
  2851. for(u32 i=0; i<count; i++)
  2852. {
  2853. s16 x = myrand_range(0, data->sectorpos_base_size*MAP_BLOCKSIZE-1);
  2854. s16 z = myrand_range(0, data->sectorpos_base_size*MAP_BLOCKSIZE-1);
  2855. x += data->sectorpos_base.X*MAP_BLOCKSIZE;
  2856. z += data->sectorpos_base.Y*MAP_BLOCKSIZE;
  2857. s16 y = find_ground_level(data->vmanip, v2s16(x,z));
  2858. // Don't make a tree under water level
  2859. if(y < WATER_LEVEL)
  2860. continue;
  2861. v3s16 p(x,y+1,z);
  2862. // Make a tree
  2863. make_tree(data->vmanip, p);
  2864. }*/
  2865. }
  2866. }//timer1
  2867. #endif
  2868. #if 1
  2869. {
  2870. // 19ms @cs=8
  2871. //TimeTaker timer1("grow grass");
  2872. /*
  2873. Grow grass
  2874. */
  2875. /*for(s16 x=0-4; x<data->sectorpos_base_size*MAP_BLOCKSIZE+4; x++)
  2876. for(s16 z=0-4; z<data->sectorpos_base_size*MAP_BLOCKSIZE+4; z++)*/
  2877. for(s16 x=0-data->max_spread_amount;
  2878. x<data->sectorpos_base_size*MAP_BLOCKSIZE+data->max_spread_amount;
  2879. x++)
  2880. for(s16 z=0-data->max_spread_amount;
  2881. z<data->sectorpos_base_size*MAP_BLOCKSIZE+data->max_spread_amount;
  2882. z++)
  2883. {
  2884. // Node position in 2d
  2885. v2s16 p2d = data->sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
  2886. /*
  2887. Find the lowest surface to which enough light ends up
  2888. to make grass grow.
  2889. Basically just wait until not air and not leaves.
  2890. */
  2891. s16 surface_y = 0;
  2892. {
  2893. v3s16 em = data->vmanip.m_area.getExtent();
  2894. u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
  2895. s16 y;
  2896. // Go to ground level
  2897. for(y=y_nodes_max; y>=y_nodes_min; y--)
  2898. {
  2899. MapNode &n = data->vmanip.m_data[i];
  2900. if(n.d != CONTENT_AIR
  2901. && n.d != CONTENT_LEAVES)
  2902. break;
  2903. data->vmanip.m_area.add_y(em, i, -1);
  2904. }
  2905. if(y >= y_nodes_min)
  2906. surface_y = y;
  2907. else
  2908. surface_y = y_nodes_min;
  2909. }
  2910. u32 i = data->vmanip.m_area.index(p2d.X, surface_y, p2d.Y);
  2911. MapNode *n = &data->vmanip.m_data[i];
  2912. if(n->d == CONTENT_MUD)
  2913. n->d = CONTENT_GRASS;
  2914. }
  2915. }//timer1
  2916. #endif
  2917. #endif // Disable all but lighting
  2918. /*
  2919. Initial lighting (sunlight)
  2920. */
  2921. core::map<v3s16, bool> light_sources;
  2922. {
  2923. // 750ms @cs=8, can't optimize more
  2924. TimeTaker timer1("initial lighting");
  2925. // NOTE: This is no used... umm... for some reason!
  2926. #if 0
  2927. /*
  2928. Go through the edges and add all nodes that have light to light_sources
  2929. */
  2930. // Four edges
  2931. for(s16 i=0; i<4; i++)
  2932. // Edge length
  2933. for(s16 j=lighting_min_d;
  2934. j<=lighting_max_d;
  2935. j++)
  2936. {
  2937. s16 x;
  2938. s16 z;
  2939. // +-X
  2940. if(i == 0 || i == 1)
  2941. {
  2942. x = (i==0) ? lighting_min_d : lighting_max_d;
  2943. if(i == 0)
  2944. z = lighting_min_d;
  2945. else
  2946. z = lighting_max_d;
  2947. }
  2948. // +-Z
  2949. else
  2950. {
  2951. z = (i==0) ? lighting_min_d : lighting_max_d;
  2952. if(i == 0)
  2953. x = lighting_min_d;
  2954. else
  2955. x = lighting_max_d;
  2956. }
  2957. // Node position in 2d
  2958. v2s16 p2d = data->sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
  2959. {
  2960. v3s16 em = data->vmanip.m_area.getExtent();
  2961. s16 y_start = y_nodes_max;
  2962. u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
  2963. for(s16 y=y_start; y>=y_nodes_min; y--)
  2964. {
  2965. MapNode *n = &data->vmanip.m_data[i];
  2966. if(n->getLight(LIGHTBANK_DAY) != 0)
  2967. {
  2968. light_sources.insert(v3s16(p2d.X, y, p2d.Y), true);
  2969. }
  2970. //NOTE: This is broken, at least the index has to
  2971. // be incremented
  2972. }
  2973. }
  2974. }
  2975. #endif
  2976. #if 1
  2977. /*
  2978. Go through the edges and apply sunlight to them, not caring
  2979. about neighbors
  2980. */
  2981. // Four edges
  2982. for(s16 i=0; i<4; i++)
  2983. // Edge length
  2984. for(s16 j=lighting_min_d;
  2985. j<=lighting_max_d;
  2986. j++)
  2987. {
  2988. s16 x;
  2989. s16 z;
  2990. // +-X
  2991. if(i == 0 || i == 1)
  2992. {
  2993. x = (i==0) ? lighting_min_d : lighting_max_d;
  2994. if(i == 0)
  2995. z = lighting_min_d;
  2996. else
  2997. z = lighting_max_d;
  2998. }
  2999. // +-Z
  3000. else
  3001. {
  3002. z = (i==0) ? lighting_min_d : lighting_max_d;
  3003. if(i == 0)
  3004. x = lighting_min_d;
  3005. else
  3006. x = lighting_max_d;
  3007. }
  3008. // Node position in 2d
  3009. v2s16 p2d = data->sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
  3010. // Loop from top to down
  3011. {
  3012. u8 light = LIGHT_SUN;
  3013. v3s16 em = data->vmanip.m_area.getExtent();
  3014. s16 y_start = y_nodes_max;
  3015. u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
  3016. for(s16 y=y_start; y>=y_nodes_min; y--)
  3017. {
  3018. MapNode *n = &data->vmanip.m_data[i];
  3019. if(light_propagates_content(n->d) == false)
  3020. {
  3021. light = 0;
  3022. }
  3023. else if(light != LIGHT_SUN
  3024. || sunlight_propagates_content(n->d) == false)
  3025. {
  3026. if(light > 0)
  3027. light--;
  3028. }
  3029. n->setLight(LIGHTBANK_DAY, light);
  3030. n->setLight(LIGHTBANK_NIGHT, 0);
  3031. if(light != 0)
  3032. {
  3033. // Insert light source
  3034. light_sources.insert(v3s16(p2d.X, y, p2d.Y), true);
  3035. }
  3036. // Increment index by y
  3037. data->vmanip.m_area.add_y(em, i, -1);
  3038. }
  3039. }
  3040. }
  3041. #endif
  3042. /*for(s16 x=0; x<data->sectorpos_base_size*MAP_BLOCKSIZE; x++)
  3043. for(s16 z=0; z<data->sectorpos_base_size*MAP_BLOCKSIZE; z++)*/
  3044. /*for(s16 x=0-data->max_spread_amount+1;
  3045. x<data->sectorpos_base_size*MAP_BLOCKSIZE+data->max_spread_amount-1;
  3046. x++)
  3047. for(s16 z=0-data->max_spread_amount+1;
  3048. z<data->sectorpos_base_size*MAP_BLOCKSIZE+data->max_spread_amount-1;
  3049. z++)*/
  3050. #if 1
  3051. /*
  3052. This has to be 1 smaller than the actual area, because
  3053. neighboring nodes are checked.
  3054. */
  3055. for(s16 x=lighting_min_d+1;
  3056. x<=lighting_max_d-1;
  3057. x++)
  3058. for(s16 z=lighting_min_d+1;
  3059. z<=lighting_max_d-1;
  3060. z++)
  3061. {
  3062. // Node position in 2d
  3063. v2s16 p2d = data->sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
  3064. /*
  3065. Apply initial sunlight
  3066. */
  3067. {
  3068. u8 light = LIGHT_SUN;
  3069. bool add_to_sources = false;
  3070. v3s16 em = data->vmanip.m_area.getExtent();
  3071. s16 y_start = y_nodes_max;
  3072. u32 i = data->vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
  3073. for(s16 y=y_start; y>=y_nodes_min; y--)
  3074. {
  3075. MapNode *n = &data->vmanip.m_data[i];
  3076. if(light_propagates_content(n->d) == false)
  3077. {
  3078. light = 0;
  3079. }
  3080. else if(light != LIGHT_SUN
  3081. || sunlight_propagates_content(n->d) == false)
  3082. {
  3083. if(light > 0)
  3084. light--;
  3085. }
  3086. // This doesn't take much time
  3087. if(add_to_sources == false)
  3088. {
  3089. /*
  3090. Check sides. If side is not air or water, start
  3091. adding to light_sources.
  3092. */
  3093. v3s16 dirs4[4] = {
  3094. v3s16(0,0,1), // back
  3095. v3s16(1,0,0), // right
  3096. v3s16(0,0,-1), // front
  3097. v3s16(-1,0,0), // left
  3098. };
  3099. for(u32 di=0; di<4; di++)
  3100. {
  3101. v3s16 dirp = dirs4[di];
  3102. u32 i2 = i;
  3103. data->vmanip.m_area.add_p(em, i2, dirp);
  3104. MapNode *n2 = &data->vmanip.m_data[i2];
  3105. if(
  3106. n2->d != CONTENT_AIR
  3107. && n2->d != CONTENT_WATERSOURCE
  3108. && n2->d != CONTENT_WATER
  3109. ){
  3110. add_to_sources = true;
  3111. break;
  3112. }
  3113. }
  3114. }
  3115. n->setLight(LIGHTBANK_DAY, light);
  3116. n->setLight(LIGHTBANK_NIGHT, 0);
  3117. // This doesn't take much time
  3118. if(light != 0 && add_to_sources)
  3119. {
  3120. // Insert light source
  3121. light_sources.insert(v3s16(p2d.X, y, p2d.Y), true);
  3122. }
  3123. // Increment index by y
  3124. data->vmanip.m_area.add_y(em, i, -1);
  3125. }
  3126. }
  3127. }
  3128. #endif
  3129. }//timer1
  3130. // Spread light around
  3131. {
  3132. TimeTaker timer("makeChunk() spreadLight");
  3133. data->vmanip.spreadLight(LIGHTBANK_DAY, light_sources);
  3134. }
  3135. /*
  3136. Generation ended
  3137. */
  3138. timer_generate.stop();
  3139. }
  3140. //###################################################################
  3141. //###################################################################
  3142. //###################################################################
  3143. //###################################################################
  3144. //###################################################################
  3145. //###################################################################
  3146. //###################################################################
  3147. //###################################################################
  3148. //###################################################################
  3149. //###################################################################
  3150. //###################################################################
  3151. //###################################################################
  3152. //###################################################################
  3153. //###################################################################
  3154. //###################################################################
  3155. void ServerMap::initChunkMake(ChunkMakeData &data, v2s16 chunkpos)
  3156. {
  3157. if(m_chunksize == 0)
  3158. {
  3159. data.no_op = true;
  3160. return;
  3161. }
  3162. data.no_op = false;
  3163. // The distance how far into the neighbors the generator is allowed to go.
  3164. s16 max_spread_amount_sectors = 2;
  3165. assert(max_spread_amount_sectors <= m_chunksize);
  3166. s16 max_spread_amount = max_spread_amount_sectors * MAP_BLOCKSIZE;
  3167. s16 y_blocks_min = -4;
  3168. s16 y_blocks_max = 3;
  3169. v2s16 sectorpos_base = chunk_to_sector(chunkpos);
  3170. s16 sectorpos_base_size = m_chunksize;
  3171. v2s16 sectorpos_bigbase =
  3172. sectorpos_base - v2s16(1,1) * max_spread_amount_sectors;
  3173. s16 sectorpos_bigbase_size =
  3174. sectorpos_base_size + 2 * max_spread_amount_sectors;
  3175. data.seed = m_seed;
  3176. data.chunkpos = chunkpos;
  3177. data.y_blocks_min = y_blocks_min;
  3178. data.y_blocks_max = y_blocks_max;
  3179. data.sectorpos_base = sectorpos_base;
  3180. data.sectorpos_base_size = sectorpos_base_size;
  3181. data.sectorpos_bigbase = sectorpos_bigbase;
  3182. data.sectorpos_bigbase_size = sectorpos_bigbase_size;
  3183. data.max_spread_amount = max_spread_amount;
  3184. /*
  3185. Create the whole area of this and the neighboring chunks
  3186. */
  3187. {
  3188. TimeTaker timer("initChunkMake() create area");
  3189. for(s16 x=0; x<sectorpos_bigbase_size; x++)
  3190. for(s16 z=0; z<sectorpos_bigbase_size; z++)
  3191. {
  3192. v2s16 sectorpos = sectorpos_bigbase + v2s16(x,z);
  3193. ServerMapSector *sector = createSector(sectorpos);
  3194. assert(sector);
  3195. for(s16 y=y_blocks_min; y<=y_blocks_max; y++)
  3196. {
  3197. v3s16 blockpos(sectorpos.X, y, sectorpos.Y);
  3198. MapBlock *block = createBlock(blockpos);
  3199. // Lighting won't be calculated
  3200. //block->setLightingExpired(true);
  3201. // Lighting will be calculated
  3202. block->setLightingExpired(false);
  3203. /*
  3204. Block gets sunlight if this is true.
  3205. This should be set to true when the top side of a block
  3206. is completely exposed to the sky.
  3207. Actually this doesn't matter now because the
  3208. initial lighting is done here.
  3209. */
  3210. block->setIsUnderground(y != y_blocks_max);
  3211. }
  3212. }
  3213. }
  3214. /*
  3215. Now we have a big empty area.
  3216. Make a ManualMapVoxelManipulator that contains this and the
  3217. neighboring chunks
  3218. */
  3219. v3s16 bigarea_blocks_min(
  3220. sectorpos_bigbase.X,
  3221. y_blocks_min,
  3222. sectorpos_bigbase.Y
  3223. );
  3224. v3s16 bigarea_blocks_max(
  3225. sectorpos_bigbase.X + sectorpos_bigbase_size - 1,
  3226. y_blocks_max,
  3227. sectorpos_bigbase.Y + sectorpos_bigbase_size - 1
  3228. );
  3229. data.vmanip.setMap(this);
  3230. // Add the area
  3231. {
  3232. TimeTaker timer("initChunkMake() initialEmerge");
  3233. data.vmanip.initialEmerge(bigarea_blocks_min, bigarea_blocks_max);
  3234. }
  3235. }
  3236. MapChunk* ServerMap::finishChunkMake(ChunkMakeData &data,
  3237. core::map<v3s16, MapBlock*> &changed_blocks)
  3238. {
  3239. if(data.no_op)
  3240. return NULL;
  3241. /*
  3242. Blit generated stuff to map
  3243. */
  3244. {
  3245. // 70ms @cs=8
  3246. //TimeTaker timer("generateChunkRaw() blitBackAll");
  3247. data.vmanip.blitBackAll(&changed_blocks);
  3248. }
  3249. /*
  3250. Update day/night difference cache of the MapBlocks
  3251. */
  3252. {
  3253. for(core::map<v3s16, MapBlock*>::Iterator i = changed_blocks.getIterator();
  3254. i.atEnd() == false; i++)
  3255. {
  3256. MapBlock *block = i.getNode()->getValue();
  3257. block->updateDayNightDiff();
  3258. }
  3259. }
  3260. /*
  3261. Copy transforming liquid information
  3262. */
  3263. while(data.transforming_liquid.size() > 0)
  3264. {
  3265. v3s16 p = data.transforming_liquid.pop_front();
  3266. m_transforming_liquid.push_back(p);
  3267. }
  3268. /*
  3269. Add random objects to blocks
  3270. */
  3271. {
  3272. for(s16 x=0; x<data.sectorpos_base_size; x++)
  3273. for(s16 z=0; z<data.sectorpos_base_size; z++)
  3274. {
  3275. v2s16 sectorpos = data.sectorpos_base + v2s16(x,z);
  3276. ServerMapSector *sector = createSector(sectorpos);
  3277. assert(sector);
  3278. for(s16 y=data.y_blocks_min; y<=data.y_blocks_max; y++)
  3279. {
  3280. v3s16 blockpos(sectorpos.X, y, sectorpos.Y);
  3281. MapBlock *block = createBlock(blockpos);
  3282. addRandomObjects(block);
  3283. }
  3284. }
  3285. }
  3286. /*
  3287. Create chunk metadata
  3288. */
  3289. for(s16 x=-1; x<=1; x++)
  3290. for(s16 y=-1; y<=1; y++)
  3291. {
  3292. v2s16 chunkpos0 = data.chunkpos + v2s16(x,y);
  3293. // Add chunk meta information
  3294. MapChunk *chunk = getChunk(chunkpos0);
  3295. if(chunk == NULL)
  3296. {
  3297. chunk = new MapChunk();
  3298. m_chunks.insert(chunkpos0, chunk);
  3299. }
  3300. //chunk->setIsVolatile(true);
  3301. if(chunk->getGenLevel() > GENERATED_PARTLY)
  3302. chunk->setGenLevel(GENERATED_PARTLY);
  3303. }
  3304. /*
  3305. Set central chunk non-volatile
  3306. */
  3307. MapChunk *chunk = getChunk(data.chunkpos);
  3308. assert(chunk);
  3309. // Set non-volatile
  3310. //chunk->setIsVolatile(false);
  3311. chunk->setGenLevel(GENERATED_FULLY);
  3312. /*
  3313. Save changed parts of map
  3314. */
  3315. save(true);
  3316. return chunk;
  3317. }
  3318. #if 0
  3319. // NOTE: Deprecated
  3320. MapChunk* ServerMap::generateChunkRaw(v2s16 chunkpos,
  3321. core::map<v3s16, MapBlock*> &changed_blocks,
  3322. bool force)
  3323. {
  3324. DSTACK(__FUNCTION_NAME);
  3325. /*
  3326. Don't generate if already fully generated
  3327. */
  3328. if(force == false)
  3329. {
  3330. MapChunk *chunk = getChunk(chunkpos);
  3331. if(chunk != NULL && chunk->getGenLevel() == GENERATED_FULLY)
  3332. {
  3333. dstream<<"generateChunkRaw(): Chunk "
  3334. <<"("<<chunkpos.X<<","<<chunkpos.Y<<")"
  3335. <<" already generated"<<std::endl;
  3336. return chunk;
  3337. }
  3338. }
  3339. dstream<<"generateChunkRaw(): Generating chunk "
  3340. <<"("<<chunkpos.X<<","<<chunkpos.Y<<")"
  3341. <<std::endl;
  3342. TimeTaker timer("generateChunkRaw()");
  3343. ChunkMakeData data;
  3344. // Initialize generation
  3345. initChunkMake(data, chunkpos);
  3346. // Generate stuff
  3347. makeChunk(&data);
  3348. // Finalize generation
  3349. MapChunk *chunk = finishChunkMake(data, changed_blocks);
  3350. /*
  3351. Return central chunk (which was requested)
  3352. */
  3353. return chunk;
  3354. }
  3355. // NOTE: Deprecated
  3356. MapChunk* ServerMap::generateChunk(v2s16 chunkpos1,
  3357. core::map<v3s16, MapBlock*> &changed_blocks)
  3358. {
  3359. dstream<<"generateChunk(): Generating chunk "
  3360. <<"("<<chunkpos1.X<<","<<chunkpos1.Y<<")"
  3361. <<std::endl;
  3362. /*for(s16 x=-1; x<=1; x++)
  3363. for(s16 y=-1; y<=1; y++)*/
  3364. for(s16 x=-0; x<=0; x++)
  3365. for(s16 y=-0; y<=0; y++)
  3366. {
  3367. v2s16 chunkpos0 = chunkpos1 + v2s16(x,y);
  3368. MapChunk *chunk = getChunk(chunkpos0);
  3369. // Skip if already generated
  3370. if(chunk != NULL && chunk->getGenLevel() == GENERATED_FULLY)
  3371. continue;
  3372. generateChunkRaw(chunkpos0, changed_blocks);
  3373. }
  3374. assert(chunkNonVolatile(chunkpos1));
  3375. MapChunk *chunk = getChunk(chunkpos1);
  3376. return chunk;
  3377. }
  3378. #endif
  3379. ServerMapSector * ServerMap::createSector(v2s16 p2d)
  3380. {
  3381. DSTACK("%s: p2d=(%d,%d)",
  3382. __FUNCTION_NAME,
  3383. p2d.X, p2d.Y);
  3384. /*
  3385. Check if it exists already in memory
  3386. */
  3387. ServerMapSector *sector = (ServerMapSector*)getSectorNoGenerateNoEx(p2d);
  3388. if(sector != NULL)
  3389. return sector;
  3390. /*
  3391. Try to load it from disk (with blocks)
  3392. */
  3393. if(loadSectorFull(p2d) == true)
  3394. {
  3395. ServerMapSector *sector = (ServerMapSector*)getSectorNoGenerateNoEx(p2d);
  3396. if(sector == NULL)
  3397. {
  3398. dstream<<"ServerMap::createSector(): loadSectorFull didn't make a sector"<<std::endl;
  3399. throw InvalidPositionException("");
  3400. }
  3401. return sector;
  3402. }
  3403. /*
  3404. Do not create over-limit
  3405. */
  3406. if(p2d.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
  3407. || p2d.X > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
  3408. || p2d.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
  3409. || p2d.Y > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE)
  3410. throw InvalidPositionException("createSector(): pos. over limit");
  3411. /*
  3412. Generate blank sector
  3413. */
  3414. sector = new ServerMapSector(this, p2d);
  3415. // Sector position on map in nodes
  3416. v2s16 nodepos2d = p2d * MAP_BLOCKSIZE;
  3417. /*
  3418. Insert to container
  3419. */
  3420. m_sectors.insert(p2d, sector);
  3421. return sector;
  3422. }
  3423. #if 0
  3424. MapSector * ServerMap::emergeSector(v2s16 p2d,
  3425. core::map<v3s16, MapBlock*> &changed_blocks)
  3426. {
  3427. DSTACK("%s: p2d=(%d,%d)",
  3428. __FUNCTION_NAME,
  3429. p2d.X, p2d.Y);
  3430. /*
  3431. Check chunk status
  3432. */
  3433. v2s16 chunkpos = sector_to_chunk(p2d);
  3434. /*bool chunk_nonvolatile = false;
  3435. MapChunk *chunk = getChunk(chunkpos);
  3436. if(chunk && chunk->getIsVolatile() == false)
  3437. chunk_nonvolatile = true;*/
  3438. bool chunk_nonvolatile = chunkNonVolatile(chunkpos);
  3439. /*
  3440. If chunk is not fully generated, generate chunk
  3441. */
  3442. if(chunk_nonvolatile == false)
  3443. {
  3444. // Generate chunk and neighbors
  3445. generateChunk(chunkpos, changed_blocks);
  3446. }
  3447. /*
  3448. Return sector if it exists now
  3449. */
  3450. MapSector *sector = getSectorNoGenerateNoEx(p2d);
  3451. if(sector != NULL)
  3452. return sector;
  3453. /*
  3454. Try to load it from disk
  3455. */
  3456. if(loadSectorFull(p2d) == true)
  3457. {
  3458. MapSector *sector = getSectorNoGenerateNoEx(p2d);
  3459. if(sector == NULL)
  3460. {
  3461. dstream<<"ServerMap::emergeSector(): loadSectorFull didn't make a sector"<<std::endl;
  3462. throw InvalidPositionException("");
  3463. }
  3464. return sector;
  3465. }
  3466. /*
  3467. generateChunk should have generated the sector
  3468. */
  3469. //assert(0);
  3470. dstream<<"WARNING: ServerMap::emergeSector: Cannot find sector ("
  3471. <<p2d.X<<","<<p2d.Y<<" and chunk is already generated. "
  3472. <<std::endl;
  3473. #if 0
  3474. dstream<<"WARNING: Creating an empty sector."<<std::endl;
  3475. return createSector(p2d);
  3476. #endif
  3477. #if 1
  3478. dstream<<"WARNING: Forcing regeneration of chunk."<<std::endl;
  3479. // Generate chunk
  3480. generateChunkRaw(chunkpos, changed_blocks, true);
  3481. /*
  3482. Return sector if it exists now
  3483. */
  3484. sector = getSectorNoGenerateNoEx(p2d);
  3485. if(sector != NULL)
  3486. return sector;
  3487. dstream<<"ERROR: Could not get sector from anywhere."<<std::endl;
  3488. assert(0);
  3489. #endif
  3490. /*
  3491. Generate directly
  3492. */
  3493. //return generateSector();
  3494. }
  3495. #endif
  3496. /*
  3497. NOTE: This is not used for main map generation, only for blocks
  3498. that are very high or low
  3499. */
  3500. MapBlock * ServerMap::generateBlock(
  3501. v3s16 p,
  3502. MapBlock *original_dummy,
  3503. ServerMapSector *sector,
  3504. core::map<v3s16, MapBlock*> &changed_blocks,
  3505. core::map<v3s16, MapBlock*> &lighting_invalidated_blocks
  3506. )
  3507. {
  3508. DSTACK("%s: p=(%d,%d,%d)",
  3509. __FUNCTION_NAME,
  3510. p.X, p.Y, p.Z);
  3511. // If chunks are disabled
  3512. /*if(m_chunksize == 0)
  3513. {
  3514. dstream<<"ServerMap::generateBlock(): Chunks disabled -> "
  3515. <<"not generating."<<std::endl;
  3516. return NULL;
  3517. }*/
  3518. /*dstream<<"generateBlock(): "
  3519. <<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"
  3520. <<std::endl;*/
  3521. MapBlock *block = original_dummy;
  3522. v2s16 p2d(p.X, p.Z);
  3523. s16 block_y = p.Y;
  3524. v2s16 p2d_nodes = p2d * MAP_BLOCKSIZE;
  3525. /*
  3526. Do not generate over-limit
  3527. */
  3528. if(blockpos_over_limit(p))
  3529. {
  3530. dstream<<__FUNCTION_NAME<<": Block position over limit"<<std::endl;
  3531. throw InvalidPositionException("generateBlock(): pos. over limit");
  3532. }
  3533. /*
  3534. If block doesn't exist, create one.
  3535. If it exists, it is a dummy. In that case unDummify() it.
  3536. NOTE: This already sets the map as the parent of the block
  3537. */
  3538. if(block == NULL)
  3539. {
  3540. block = sector->createBlankBlockNoInsert(block_y);
  3541. }
  3542. else
  3543. {
  3544. // Remove the block so that nobody can get a half-generated one.
  3545. sector->removeBlock(block);
  3546. // Allocate the block to contain the generated data
  3547. block->unDummify();
  3548. }
  3549. #if 0
  3550. /*
  3551. Generate a completely empty block
  3552. */
  3553. for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
  3554. for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
  3555. {
  3556. for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
  3557. {
  3558. MapNode n;
  3559. n.d = CONTENT_AIR;
  3560. block->setNode(v3s16(x0,y0,z0), n);
  3561. }
  3562. }
  3563. #else
  3564. /*
  3565. Generate a proper block
  3566. */
  3567. u8 water_material = CONTENT_WATERSOURCE;
  3568. s32 lowest_ground_y = 32767;
  3569. s32 highest_ground_y = -32768;
  3570. for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
  3571. for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
  3572. {
  3573. //dstream<<"generateBlock: x0="<<x0<<", z0="<<z0<<std::endl;
  3574. //s16 surface_y = 0;
  3575. s16 mud_add_amount = get_mud_add_amount(m_seed, p2d_nodes+v2s16(x0,z0));
  3576. s16 surface_y = base_rock_level_2d(m_seed, p2d_nodes+v2s16(x0,z0))
  3577. + mud_add_amount;
  3578. /*// Less mud if it is high
  3579. if(surface_y >= 64)
  3580. {
  3581. mud_add_amount--;
  3582. surface_y--;
  3583. }*/
  3584. // If chunks are disabled
  3585. if(m_chunksize == 0)
  3586. surface_y = WATER_LEVEL + 1;
  3587. if(surface_y < lowest_ground_y)
  3588. lowest_ground_y = surface_y;
  3589. if(surface_y > highest_ground_y)
  3590. highest_ground_y = surface_y;
  3591. s32 surface_depth = AVERAGE_MUD_AMOUNT;
  3592. for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
  3593. {
  3594. s16 real_y = block_y * MAP_BLOCKSIZE + y0;
  3595. MapNode n;
  3596. /*
  3597. Calculate lighting
  3598. NOTE: If there are some man-made structures above the
  3599. newly created block, they won't be taken into account.
  3600. */
  3601. if(real_y > surface_y)
  3602. n.setLight(LIGHTBANK_DAY, LIGHT_SUN);
  3603. /*
  3604. Calculate material
  3605. */
  3606. // If node is over heightmap y, it's air or water
  3607. if(real_y > surface_y)
  3608. {
  3609. // If under water level, it's water
  3610. if(real_y < WATER_LEVEL)
  3611. {
  3612. n.d = water_material;
  3613. n.setLight(LIGHTBANK_DAY,
  3614. diminish_light(LIGHT_SUN, WATER_LEVEL-real_y+1));
  3615. /*
  3616. Add to transforming liquid queue (in case it'd
  3617. start flowing)
  3618. */
  3619. v3s16 real_pos = v3s16(x0,y0,z0) + p*MAP_BLOCKSIZE;
  3620. m_transforming_liquid.push_back(real_pos);
  3621. }
  3622. // else air
  3623. else
  3624. n.d = CONTENT_AIR;
  3625. }
  3626. // Else it's ground or caves (air)
  3627. else
  3628. {
  3629. // If it's cave, it's cave
  3630. if(is_cave(m_seed, block->getPosRelative()+v3s16(x0,y0,z0)))
  3631. {
  3632. n.d = CONTENT_AIR;
  3633. }
  3634. // If it's surface_depth under ground, it's stone
  3635. else if(real_y <= surface_y - surface_depth)
  3636. {
  3637. n.d = CONTENT_STONE;
  3638. }
  3639. else
  3640. {
  3641. // It is mud if it is under the first ground
  3642. // level or under water
  3643. if(real_y < WATER_LEVEL || real_y <= surface_y - 1)
  3644. {
  3645. n.d = CONTENT_MUD;
  3646. }
  3647. else
  3648. {
  3649. n.d = CONTENT_GRASS;
  3650. }
  3651. //n.d = CONTENT_MUD;
  3652. /*// If under water level, it's mud
  3653. if(real_y < WATER_LEVEL)
  3654. n.d = CONTENT_MUD;
  3655. // Only the topmost node is grass
  3656. else if(real_y <= surface_y - 1)
  3657. n.d = CONTENT_MUD;
  3658. else
  3659. n.d = CONTENT_GRASS;*/
  3660. }
  3661. }
  3662. block->setNode(v3s16(x0,y0,z0), n);
  3663. }
  3664. }
  3665. /*
  3666. Calculate some helper variables
  3667. */
  3668. // Completely underground if the highest part of block is under lowest
  3669. // ground height.
  3670. // This has to be very sure; it's probably one too strict now but
  3671. // that's just better.
  3672. bool completely_underground =
  3673. block_y * MAP_BLOCKSIZE + MAP_BLOCKSIZE < lowest_ground_y;
  3674. bool some_part_underground = block_y * MAP_BLOCKSIZE <= highest_ground_y;
  3675. bool mostly_underwater_surface = false;
  3676. if(highest_ground_y < WATER_LEVEL
  3677. && some_part_underground && !completely_underground)
  3678. mostly_underwater_surface = true;
  3679. #if 0
  3680. /*
  3681. Generate caves
  3682. */
  3683. float caves_amount = 0.5;
  3684. // Initialize temporary table
  3685. const s32 ued = MAP_BLOCKSIZE;
  3686. bool underground_emptiness[ued*ued*ued];
  3687. for(s32 i=0; i<ued*ued*ued; i++)
  3688. {
  3689. underground_emptiness[i] = 0;
  3690. }
  3691. // Fill table
  3692. #if 1
  3693. {
  3694. /*
  3695. Initialize orp and ors. Try to find if some neighboring
  3696. MapBlock has a tunnel ended in its side
  3697. */
  3698. v3f orp(
  3699. (float)(myrand()%ued)+0.5,
  3700. (float)(myrand()%ued)+0.5,
  3701. (float)(myrand()%ued)+0.5
  3702. );
  3703. bool found_existing = false;
  3704. // Check z-
  3705. try
  3706. {
  3707. s16 z = -1;
  3708. for(s16 y=0; y<ued; y++)
  3709. for(s16 x=0; x<ued; x++)
  3710. {
  3711. v3s16 ap = v3s16(x,y,z) + block->getPosRelative();
  3712. if(getNode(ap).d == CONTENT_AIR)
  3713. {
  3714. orp = v3f(x+1,y+1,0);
  3715. found_existing = true;
  3716. goto continue_generating;
  3717. }
  3718. }
  3719. }
  3720. catch(InvalidPositionException &e){}
  3721. // Check z+
  3722. try
  3723. {
  3724. s16 z = ued;
  3725. for(s16 y=0; y<ued; y++)
  3726. for(s16 x=0; x<ued; x++)
  3727. {
  3728. v3s16 ap = v3s16(x,y,z) + block->getPosRelative();
  3729. if(getNode(ap).d == CONTENT_AIR)
  3730. {
  3731. orp = v3f(x+1,y+1,ued-1);
  3732. found_existing = true;
  3733. goto continue_generating;
  3734. }
  3735. }
  3736. }
  3737. catch(InvalidPositionException &e){}
  3738. // Check x-
  3739. try
  3740. {
  3741. s16 x = -1;
  3742. for(s16 y=0; y<ued; y++)
  3743. for(s16 z=0; z<ued; z++)
  3744. {
  3745. v3s16 ap = v3s16(x,y,z) + block->getPosRelative();
  3746. if(getNode(ap).d == CONTENT_AIR)
  3747. {
  3748. orp = v3f(0,y+1,z+1);
  3749. found_existing = true;
  3750. goto continue_generating;
  3751. }
  3752. }
  3753. }
  3754. catch(InvalidPositionException &e){}
  3755. // Check x+
  3756. try
  3757. {
  3758. s16 x = ued;
  3759. for(s16 y=0; y<ued; y++)
  3760. for(s16 z=0; z<ued; z++)
  3761. {
  3762. v3s16 ap = v3s16(x,y,z) + block->getPosRelative();
  3763. if(getNode(ap).d == CONTENT_AIR)
  3764. {
  3765. orp = v3f(ued-1,y+1,z+1);
  3766. found_existing = true;
  3767. goto continue_generating;
  3768. }
  3769. }
  3770. }
  3771. catch(InvalidPositionException &e){}
  3772. // Check y-
  3773. try
  3774. {
  3775. s16 y = -1;
  3776. for(s16 x=0; x<ued; x++)
  3777. for(s16 z=0; z<ued; z++)
  3778. {
  3779. v3s16 ap = v3s16(x,y,z) + block->getPosRelative();
  3780. if(getNode(ap).d == CONTENT_AIR)
  3781. {
  3782. orp = v3f(x+1,0,z+1);
  3783. found_existing = true;
  3784. goto continue_generating;
  3785. }
  3786. }
  3787. }
  3788. catch(InvalidPositionException &e){}
  3789. // Check y+
  3790. try
  3791. {
  3792. s16 y = ued;
  3793. for(s16 x=0; x<ued; x++)
  3794. for(s16 z=0; z<ued; z++)
  3795. {
  3796. v3s16 ap = v3s16(x,y,z) + block->getPosRelative();
  3797. if(getNode(ap).d == CONTENT_AIR)
  3798. {
  3799. orp = v3f(x+1,ued-1,z+1);
  3800. found_existing = true;
  3801. goto continue_generating;
  3802. }
  3803. }
  3804. }
  3805. catch(InvalidPositionException &e){}
  3806. continue_generating:
  3807. /*
  3808. Choose whether to actually generate cave
  3809. */
  3810. bool do_generate_caves = true;
  3811. // Don't generate if no part is underground
  3812. if(!some_part_underground)
  3813. {
  3814. do_generate_caves = false;
  3815. }
  3816. // Don't generate if mostly underwater surface
  3817. /*else if(mostly_underwater_surface)
  3818. {
  3819. do_generate_caves = false;
  3820. }*/
  3821. // Partly underground = cave
  3822. else if(!completely_underground)
  3823. {
  3824. do_generate_caves = (rand() % 100 <= (s32)(caves_amount*100));
  3825. }
  3826. // Found existing cave underground
  3827. else if(found_existing && completely_underground)
  3828. {
  3829. do_generate_caves = (rand() % 100 <= (s32)(caves_amount*100));
  3830. }
  3831. // Underground and no caves found
  3832. else
  3833. {
  3834. do_generate_caves = (rand() % 300 <= (s32)(caves_amount*100));
  3835. }
  3836. if(do_generate_caves)
  3837. {
  3838. /*
  3839. Generate some tunnel starting from orp and ors
  3840. */
  3841. for(u16 i=0; i<3; i++)
  3842. {
  3843. v3f rp(
  3844. (float)(myrand()%ued)+0.5,
  3845. (float)(myrand()%ued)+0.5,
  3846. (float)(myrand()%ued)+0.5
  3847. );
  3848. s16 min_d = 0;
  3849. s16 max_d = 4;
  3850. s16 rs = (myrand()%(max_d-min_d+1))+min_d;
  3851. v3f vec = rp - orp;
  3852. for(float f=0; f<1.0; f+=0.04)
  3853. {
  3854. v3f fp = orp + vec * f;
  3855. v3s16 cp(fp.X, fp.Y, fp.Z);
  3856. s16 d0 = -rs/2;
  3857. s16 d1 = d0 + rs - 1;
  3858. for(s16 z0=d0; z0<=d1; z0++)
  3859. {
  3860. s16 si = rs - abs(z0);
  3861. for(s16 x0=-si; x0<=si-1; x0++)
  3862. {
  3863. s16 si2 = rs - abs(x0);
  3864. for(s16 y0=-si2+1; y0<=si2-1; y0++)
  3865. {
  3866. s16 z = cp.Z + z0;
  3867. s16 y = cp.Y + y0;
  3868. s16 x = cp.X + x0;
  3869. v3s16 p(x,y,z);
  3870. if(isInArea(p, ued) == false)
  3871. continue;
  3872. underground_emptiness[ued*ued*z + ued*y + x] = 1;
  3873. }
  3874. }
  3875. }
  3876. }
  3877. orp = rp;
  3878. }
  3879. }
  3880. }
  3881. #endif
  3882. // Set to true if has caves.
  3883. // Set when some non-air is changed to air when making caves.
  3884. bool has_caves = false;
  3885. /*
  3886. Apply temporary cave data to block
  3887. */
  3888. for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
  3889. for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
  3890. {
  3891. for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
  3892. {
  3893. MapNode n = block->getNode(v3s16(x0,y0,z0));
  3894. // Create caves
  3895. if(underground_emptiness[
  3896. ued*ued*(z0*ued/MAP_BLOCKSIZE)
  3897. +ued*(y0*ued/MAP_BLOCKSIZE)
  3898. +(x0*ued/MAP_BLOCKSIZE)])
  3899. {
  3900. if(content_features(n.d).walkable/*is_ground_content(n.d)*/)
  3901. {
  3902. // Has now caves
  3903. has_caves = true;
  3904. // Set air to node
  3905. n.d = CONTENT_AIR;
  3906. }
  3907. }
  3908. block->setNode(v3s16(x0,y0,z0), n);
  3909. }
  3910. }
  3911. #endif
  3912. /*
  3913. This is used for guessing whether or not the block should
  3914. receive sunlight from the top if the block above doesn't exist
  3915. */
  3916. block->setIsUnderground(completely_underground);
  3917. /*
  3918. Force lighting update if some part of block is partly
  3919. underground and has caves.
  3920. */
  3921. /*if(some_part_underground && !completely_underground && has_caves)
  3922. {
  3923. //dstream<<"Half-ground caves"<<std::endl;
  3924. lighting_invalidated_blocks[block->getPos()] = block;
  3925. }*/
  3926. // DEBUG: Always update lighting
  3927. //lighting_invalidated_blocks[block->getPos()] = block;
  3928. /*
  3929. Add some minerals
  3930. */
  3931. if(some_part_underground)
  3932. {
  3933. s16 underground_level = (lowest_ground_y/MAP_BLOCKSIZE - block_y)+1;
  3934. /*
  3935. Add meseblocks
  3936. */
  3937. for(s16 i=0; i<underground_level/4 + 1; i++)
  3938. {
  3939. if(myrand()%50 == 0)
  3940. {
  3941. v3s16 cp(
  3942. (myrand()%(MAP_BLOCKSIZE-2))+1,
  3943. (myrand()%(MAP_BLOCKSIZE-2))+1,
  3944. (myrand()%(MAP_BLOCKSIZE-2))+1
  3945. );
  3946. MapNode n;
  3947. n.d = CONTENT_MESE;
  3948. for(u16 i=0; i<27; i++)
  3949. {
  3950. if(block->getNode(cp+g_27dirs[i]).d == CONTENT_STONE)
  3951. if(myrand()%8 == 0)
  3952. block->setNode(cp+g_27dirs[i], n);
  3953. }
  3954. }
  3955. }
  3956. /*
  3957. Add coal
  3958. */
  3959. u16 coal_amount = 30;
  3960. u16 coal_rareness = 60 / coal_amount;
  3961. if(coal_rareness == 0)
  3962. coal_rareness = 1;
  3963. if(myrand()%coal_rareness == 0)
  3964. {
  3965. u16 a = myrand() % 16;
  3966. u16 amount = coal_amount * a*a*a / 1000;
  3967. for(s16 i=0; i<amount; i++)
  3968. {
  3969. v3s16 cp(
  3970. (myrand()%(MAP_BLOCKSIZE-2))+1,
  3971. (myrand()%(MAP_BLOCKSIZE-2))+1,
  3972. (myrand()%(MAP_BLOCKSIZE-2))+1
  3973. );
  3974. MapNode n;
  3975. n.d = CONTENT_STONE;
  3976. n.param = MINERAL_COAL;
  3977. for(u16 i=0; i<27; i++)
  3978. {
  3979. if(block->getNode(cp+g_27dirs[i]).d == CONTENT_STONE)
  3980. if(myrand()%8 == 0)
  3981. block->setNode(cp+g_27dirs[i], n);
  3982. }
  3983. }
  3984. }
  3985. /*
  3986. Add iron
  3987. */
  3988. //TODO: change to iron_amount or whatever
  3989. u16 iron_amount = 15;
  3990. u16 iron_rareness = 60 / iron_amount;
  3991. if(iron_rareness == 0)
  3992. iron_rareness = 1;
  3993. if(myrand()%iron_rareness == 0)
  3994. {
  3995. u16 a = myrand() % 16;
  3996. u16 amount = iron_amount * a*a*a / 1000;
  3997. for(s16 i=0; i<amount; i++)
  3998. {
  3999. v3s16 cp(
  4000. (myrand()%(MAP_BLOCKSIZE-2))+1,
  4001. (myrand()%(MAP_BLOCKSIZE-2))+1,
  4002. (myrand()%(MAP_BLOCKSIZE-2))+1
  4003. );
  4004. MapNode n;
  4005. n.d = CONTENT_STONE;
  4006. n.param = MINERAL_IRON;
  4007. for(u16 i=0; i<27; i++)
  4008. {
  4009. if(block->getNode(cp+g_27dirs[i]).d == CONTENT_STONE)
  4010. if(myrand()%8 == 0)
  4011. block->setNode(cp+g_27dirs[i], n);
  4012. }
  4013. }
  4014. }
  4015. }
  4016. /*
  4017. Create a few rats in empty blocks underground
  4018. */
  4019. if(completely_underground)
  4020. {
  4021. //for(u16 i=0; i<2; i++)
  4022. {
  4023. v3s16 cp(
  4024. (myrand()%(MAP_BLOCKSIZE-2))+1,
  4025. (myrand()%(MAP_BLOCKSIZE-2))+1,
  4026. (myrand()%(MAP_BLOCKSIZE-2))+1
  4027. );
  4028. // Check that the place is empty
  4029. //if(!is_ground_content(block->getNode(cp).d))
  4030. if(1)
  4031. {
  4032. RatObject *obj = new RatObject(NULL, -1, intToFloat(cp, BS));
  4033. block->addObject(obj);
  4034. }
  4035. }
  4036. }
  4037. #endif // end of proper block generation
  4038. /*
  4039. Add block to sector.
  4040. */
  4041. sector->insertBlock(block);
  4042. // Lighting is invalid after generation.
  4043. block->setLightingExpired(true);
  4044. #if 0
  4045. /*
  4046. Debug information
  4047. */
  4048. dstream
  4049. <<"lighting_invalidated_blocks.size()"
  4050. <<", has_caves"
  4051. <<", completely_ug"
  4052. <<", some_part_ug"
  4053. <<" "<<lighting_invalidated_blocks.size()
  4054. <<", "<<has_caves
  4055. <<", "<<completely_underground
  4056. <<", "<<some_part_underground
  4057. <<std::endl;
  4058. #endif
  4059. return block;
  4060. }
  4061. MapBlock * ServerMap::createBlock(v3s16 p)
  4062. {
  4063. DSTACK("%s: p=(%d,%d,%d)",
  4064. __FUNCTION_NAME, p.X, p.Y, p.Z);
  4065. /*
  4066. Do not create over-limit
  4067. */
  4068. if(p.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
  4069. || p.X > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
  4070. || p.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
  4071. || p.Y > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
  4072. || p.Z < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
  4073. || p.Z > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE)
  4074. throw InvalidPositionException("createBlock(): pos. over limit");
  4075. v2s16 p2d(p.X, p.Z);
  4076. s16 block_y = p.Y;
  4077. /*
  4078. This will create or load a sector if not found in memory.
  4079. If block exists on disk, it will be loaded.
  4080. NOTE: On old save formats, this will be slow, as it generates
  4081. lighting on blocks for them.
  4082. */
  4083. ServerMapSector *sector;
  4084. try{
  4085. sector = (ServerMapSector*)createSector(p2d);
  4086. assert(sector->getId() == MAPSECTOR_SERVER);
  4087. }
  4088. catch(InvalidPositionException &e)
  4089. {
  4090. dstream<<"createBlock: createSector() failed"<<std::endl;
  4091. throw e;
  4092. }
  4093. /*
  4094. NOTE: This should not be done, or at least the exception
  4095. should not be passed on as std::exception, because it
  4096. won't be catched at all.
  4097. */
  4098. /*catch(std::exception &e)
  4099. {
  4100. dstream<<"createBlock: createSector() failed: "
  4101. <<e.what()<<std::endl;
  4102. throw e;
  4103. }*/
  4104. /*
  4105. Try to get a block from the sector
  4106. */
  4107. MapBlock *block = sector->getBlockNoCreateNoEx(block_y);
  4108. if(block)
  4109. return block;
  4110. // Create blank
  4111. block = sector->createBlankBlock(block_y);
  4112. return block;
  4113. }
  4114. MapBlock * ServerMap::emergeBlock(
  4115. v3s16 p,
  4116. bool only_from_disk,
  4117. core::map<v3s16, MapBlock*> &changed_blocks,
  4118. core::map<v3s16, MapBlock*> &lighting_invalidated_blocks
  4119. )
  4120. {
  4121. DSTACK("%s: p=(%d,%d,%d), only_from_disk=%d",
  4122. __FUNCTION_NAME,
  4123. p.X, p.Y, p.Z, only_from_disk);
  4124. /*
  4125. Do not generate over-limit
  4126. */
  4127. if(p.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
  4128. || p.X > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
  4129. || p.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
  4130. || p.Y > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
  4131. || p.Z < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
  4132. || p.Z > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE)
  4133. throw InvalidPositionException("emergeBlock(): pos. over limit");
  4134. v2s16 p2d(p.X, p.Z);
  4135. s16 block_y = p.Y;
  4136. /*
  4137. This will create or load a sector if not found in memory.
  4138. If block exists on disk, it will be loaded.
  4139. */
  4140. ServerMapSector *sector;
  4141. try{
  4142. sector = (ServerMapSector*)emergeSector(p2d, changed_blocks);
  4143. assert(sector->getId() == MAPSECTOR_SERVER);
  4144. }
  4145. catch(InvalidPositionException &e)
  4146. {
  4147. dstream<<"emergeBlock: emergeSector() failed: "
  4148. <<e.what()<<std::endl;
  4149. dstream<<"Path to failed sector: "<<getSectorDir(p2d)
  4150. <<std::endl
  4151. <<"You could try to delete it."<<std::endl;
  4152. throw e;
  4153. }
  4154. catch(VersionMismatchException &e)
  4155. {
  4156. dstream<<"emergeBlock: emergeSector() failed: "
  4157. <<e.what()<<std::endl;
  4158. dstream<<"Path to failed sector: "<<getSectorDir(p2d)
  4159. <<std::endl
  4160. <<"You could try to delete it."<<std::endl;
  4161. throw e;
  4162. }
  4163. /*
  4164. NOTE: This should not be done, or at least the exception
  4165. should not be passed on as std::exception, because it
  4166. won't be catched at all.
  4167. */
  4168. /*catch(std::exception &e)
  4169. {
  4170. dstream<<"emergeBlock: emergeSector() failed: "
  4171. <<e.what()<<std::endl;
  4172. dstream<<"Path to failed sector: "<<getSectorDir(p2d)
  4173. <<std::endl
  4174. <<"You could try to delete it."<<std::endl;
  4175. throw e;
  4176. }*/
  4177. /*
  4178. Try to get a block from the sector
  4179. */
  4180. bool does_not_exist = false;
  4181. bool lighting_expired = false;
  4182. MapBlock *block = sector->getBlockNoCreateNoEx(block_y);
  4183. if(block == NULL)
  4184. {
  4185. does_not_exist = true;
  4186. }
  4187. else if(block->isDummy() == true)
  4188. {
  4189. does_not_exist = true;
  4190. }
  4191. else if(block->getLightingExpired())
  4192. {
  4193. lighting_expired = true;
  4194. }
  4195. else
  4196. {
  4197. // Valid block
  4198. //dstream<<"emergeBlock(): Returning already valid block"<<std::endl;
  4199. return block;
  4200. }
  4201. /*
  4202. If block was not found on disk and not going to generate a
  4203. new one, make sure there is a dummy block in place.
  4204. */
  4205. if(only_from_disk && (does_not_exist || lighting_expired))
  4206. {
  4207. //dstream<<"emergeBlock(): Was not on disk but not generating"<<std::endl;
  4208. if(block == NULL)
  4209. {
  4210. // Create dummy block
  4211. block = new MapBlock(this, p, true);
  4212. // Add block to sector
  4213. sector->insertBlock(block);
  4214. }
  4215. // Done.
  4216. return block;
  4217. }
  4218. //dstream<<"Not found on disk, generating."<<std::endl;
  4219. // 0ms
  4220. //TimeTaker("emergeBlock() generate");
  4221. //dstream<<"emergeBlock(): Didn't find valid block -> making one"<<std::endl;
  4222. /*
  4223. If the block doesn't exist, generate the block.
  4224. */
  4225. if(does_not_exist)
  4226. {
  4227. block = generateBlock(p, block, sector, changed_blocks,
  4228. lighting_invalidated_blocks);
  4229. }
  4230. if(lighting_expired)
  4231. {
  4232. lighting_invalidated_blocks.insert(p, block);
  4233. }
  4234. /*
  4235. Initially update sunlight
  4236. */
  4237. {
  4238. core::map<v3s16, bool> light_sources;
  4239. bool black_air_left = false;
  4240. bool bottom_invalid =
  4241. block->propagateSunlight(light_sources, true,
  4242. &black_air_left, true);
  4243. // If sunlight didn't reach everywhere and part of block is
  4244. // above ground, lighting has to be properly updated
  4245. //if(black_air_left && some_part_underground)
  4246. if(black_air_left)
  4247. {
  4248. lighting_invalidated_blocks[block->getPos()] = block;
  4249. }
  4250. if(bottom_invalid)
  4251. {
  4252. lighting_invalidated_blocks[block->getPos()] = block;
  4253. }
  4254. }
  4255. return block;
  4256. }
  4257. s16 ServerMap::findGroundLevel(v2s16 p2d)
  4258. {
  4259. /*
  4260. Uh, just do something random...
  4261. */
  4262. // Find existing map from top to down
  4263. s16 max=63;
  4264. s16 min=-64;
  4265. v3s16 p(p2d.X, max, p2d.Y);
  4266. for(; p.Y>min; p.Y--)
  4267. {
  4268. MapNode n = getNodeNoEx(p);
  4269. if(n.d != CONTENT_IGNORE)
  4270. break;
  4271. }
  4272. if(p.Y == min)
  4273. goto plan_b;
  4274. // If this node is not air, go to plan b
  4275. if(getNodeNoEx(p).d != CONTENT_AIR)
  4276. goto plan_b;
  4277. // Search existing walkable and return it
  4278. for(; p.Y>min; p.Y--)
  4279. {
  4280. MapNode n = getNodeNoEx(p);
  4281. if(content_walkable(n.d) && n.d != CONTENT_IGNORE)
  4282. return p.Y;
  4283. }
  4284. // Move to plan b
  4285. plan_b:
  4286. /*
  4287. Plan B: Get from map generator perlin noise function
  4288. */
  4289. // This won't work if proper generation is disabled
  4290. if(m_chunksize == 0)
  4291. return WATER_LEVEL+2;
  4292. double level = base_rock_level_2d(m_seed, p2d) + AVERAGE_MUD_AMOUNT;
  4293. return (s16)level;
  4294. }
  4295. void ServerMap::createDir(std::string path)
  4296. {
  4297. if(fs::CreateDir(path) == false)
  4298. {
  4299. m_dout<<DTIME<<"ServerMap: Failed to create directory "
  4300. <<"\""<<path<<"\""<<std::endl;
  4301. throw BaseException("ServerMap failed to create directory");
  4302. }
  4303. }
  4304. std::string ServerMap::getSectorSubDir(v2s16 pos)
  4305. {
  4306. char cc[9];
  4307. snprintf(cc, 9, "%.4x%.4x",
  4308. (unsigned int)pos.X&0xffff,
  4309. (unsigned int)pos.Y&0xffff);
  4310. return std::string(cc);
  4311. }
  4312. std::string ServerMap::getSectorDir(v2s16 pos)
  4313. {
  4314. return m_savedir + "/sectors/" + getSectorSubDir(pos);
  4315. }
  4316. v2s16 ServerMap::getSectorPos(std::string dirname)
  4317. {
  4318. if(dirname.size() != 8)
  4319. throw InvalidFilenameException("Invalid sector directory name");
  4320. unsigned int x, y;
  4321. int r = sscanf(dirname.c_str(), "%4x%4x", &x, &y);
  4322. if(r != 2)
  4323. throw InvalidFilenameException("Invalid sector directory name");
  4324. v2s16 pos((s16)x, (s16)y);
  4325. return pos;
  4326. }
  4327. v3s16 ServerMap::getBlockPos(std::string sectordir, std::string blockfile)
  4328. {
  4329. v2s16 p2d = getSectorPos(sectordir);
  4330. if(blockfile.size() != 4){
  4331. throw InvalidFilenameException("Invalid block filename");
  4332. }
  4333. unsigned int y;
  4334. int r = sscanf(blockfile.c_str(), "%4x", &y);
  4335. if(r != 1)
  4336. throw InvalidFilenameException("Invalid block filename");
  4337. return v3s16(p2d.X, y, p2d.Y);
  4338. }
  4339. void ServerMap::save(bool only_changed)
  4340. {
  4341. DSTACK(__FUNCTION_NAME);
  4342. if(m_map_saving_enabled == false)
  4343. {
  4344. dstream<<DTIME<<"WARNING: Not saving map, saving disabled."<<std::endl;
  4345. return;
  4346. }
  4347. if(only_changed == false)
  4348. dstream<<DTIME<<"ServerMap: Saving whole map, this can take time."
  4349. <<std::endl;
  4350. if(only_changed == false || m_map_metadata_changed)
  4351. {
  4352. saveMapMeta();
  4353. }
  4354. // Disable saving chunk metadata if chunks are disabled
  4355. if(m_chunksize != 0)
  4356. {
  4357. if(only_changed == false || anyChunkModified())
  4358. saveChunkMeta();
  4359. }
  4360. u32 sector_meta_count = 0;
  4361. u32 block_count = 0;
  4362. { //sectorlock
  4363. //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
  4364. core::map<v2s16, MapSector*>::Iterator i = m_sectors.getIterator();
  4365. for(; i.atEnd() == false; i++)
  4366. {
  4367. ServerMapSector *sector = (ServerMapSector*)i.getNode()->getValue();
  4368. assert(sector->getId() == MAPSECTOR_SERVER);
  4369. if(sector->differs_from_disk || only_changed == false)
  4370. {
  4371. saveSectorMeta(sector);
  4372. sector_meta_count++;
  4373. }
  4374. core::list<MapBlock*> blocks;
  4375. sector->getBlocks(blocks);
  4376. core::list<MapBlock*>::Iterator j;
  4377. for(j=blocks.begin(); j!=blocks.end(); j++)
  4378. {
  4379. MapBlock *block = *j;
  4380. if(block->getChangedFlag() || only_changed == false)
  4381. {
  4382. saveBlock(block);
  4383. block_count++;
  4384. /*dstream<<"ServerMap: Written block ("
  4385. <<block->getPos().X<<","
  4386. <<block->getPos().Y<<","
  4387. <<block->getPos().Z<<")"
  4388. <<std::endl;*/
  4389. }
  4390. }
  4391. }
  4392. }//sectorlock
  4393. /*
  4394. Only print if something happened or saved whole map
  4395. */
  4396. if(only_changed == false || sector_meta_count != 0
  4397. || block_count != 0)
  4398. {
  4399. dstream<<DTIME<<"ServerMap: Written: "
  4400. <<sector_meta_count<<" sector metadata files, "
  4401. <<block_count<<" block files"
  4402. <<std::endl;
  4403. }
  4404. }
  4405. #if 0
  4406. // NOTE: Doing this is insane. Deprecated and probably broken.
  4407. void ServerMap::loadAll()
  4408. {
  4409. DSTACK(__FUNCTION_NAME);
  4410. dstream<<DTIME<<"ServerMap: Loading map..."<<std::endl;
  4411. loadMapMeta();
  4412. loadChunkMeta();
  4413. std::vector<fs::DirListNode> list = fs::GetDirListing(m_savedir+"/sectors/");
  4414. dstream<<DTIME<<"There are "<<list.size()<<" sectors."<<std::endl;
  4415. //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
  4416. s32 counter = 0;
  4417. s32 printed_counter = -100000;
  4418. s32 count = list.size();
  4419. std::vector<fs::DirListNode>::iterator i;
  4420. for(i=list.begin(); i!=list.end(); i++)
  4421. {
  4422. if(counter > printed_counter + 10)
  4423. {
  4424. dstream<<DTIME<<counter<<"/"<<count<<std::endl;
  4425. printed_counter = counter;
  4426. }
  4427. counter++;
  4428. MapSector *sector = NULL;
  4429. // We want directories
  4430. if(i->dir == false)
  4431. continue;
  4432. try{
  4433. sector = loadSectorMeta(i->name);
  4434. }
  4435. catch(InvalidFilenameException &e)
  4436. {
  4437. // This catches unknown crap in directory
  4438. }
  4439. std::vector<fs::DirListNode> list2 = fs::GetDirListing
  4440. (m_savedir+"/sectors/"+i->name);
  4441. std::vector<fs::DirListNode>::iterator i2;
  4442. for(i2=list2.begin(); i2!=list2.end(); i2++)
  4443. {
  4444. // We want files
  4445. if(i2->dir)
  4446. continue;
  4447. try{
  4448. loadBlock(i->name, i2->name, sector);
  4449. }
  4450. catch(InvalidFilenameException &e)
  4451. {
  4452. // This catches unknown crap in directory
  4453. }
  4454. }
  4455. }
  4456. dstream<<DTIME<<"ServerMap: Map loaded."<<std::endl;
  4457. }
  4458. #endif
  4459. #if 0
  4460. void ServerMap::saveMasterHeightmap()
  4461. {
  4462. DSTACK(__FUNCTION_NAME);
  4463. dstream<<"DEPRECATED: "<<__FUNCTION_NAME<<std::endl;
  4464. createDir(m_savedir);
  4465. /*std::string fullpath = m_savedir + "/master_heightmap";
  4466. std::ofstream o(fullpath.c_str(), std::ios_base::binary);
  4467. if(o.good() == false)
  4468. throw FileNotGoodException("Cannot open master heightmap");*/
  4469. // Format used for writing
  4470. //u8 version = SER_FMT_VER_HIGHEST;
  4471. }
  4472. void ServerMap::loadMasterHeightmap()
  4473. {
  4474. DSTACK(__FUNCTION_NAME);
  4475. dstream<<"DEPRECATED: "<<__FUNCTION_NAME<<std::endl;
  4476. /*std::string fullpath = m_savedir + "/master_heightmap";
  4477. std::ifstream is(fullpath.c_str(), std::ios_base::binary);
  4478. if(is.good() == false)
  4479. throw FileNotGoodException("Cannot open master heightmap");*/
  4480. }
  4481. #endif
  4482. void ServerMap::saveMapMeta()
  4483. {
  4484. DSTACK(__FUNCTION_NAME);
  4485. dstream<<"INFO: ServerMap::saveMapMeta(): "
  4486. <<"seed="<<m_seed<<", chunksize="<<m_chunksize
  4487. <<std::endl;
  4488. createDir(m_savedir);
  4489. std::string fullpath = m_savedir + "/map_meta.txt";
  4490. std::ofstream os(fullpath.c_str(), std::ios_base::binary);
  4491. if(os.good() == false)
  4492. {
  4493. dstream<<"ERROR: ServerMap::saveMapMeta(): "
  4494. <<"could not open"<<fullpath<<std::endl;
  4495. throw FileNotGoodException("Cannot open chunk metadata");
  4496. }
  4497. Settings params;
  4498. params.setU64("seed", m_seed);
  4499. params.setS32("chunksize", m_chunksize);
  4500. params.writeLines(os);
  4501. os<<"[end_of_params]\n";
  4502. m_map_metadata_changed = false;
  4503. }
  4504. void ServerMap::loadMapMeta()
  4505. {
  4506. DSTACK(__FUNCTION_NAME);
  4507. dstream<<"INFO: ServerMap::loadMapMeta(): Loading map metadata"
  4508. <<std::endl;
  4509. std::string fullpath = m_savedir + "/map_meta.txt";
  4510. std::ifstream is(fullpath.c_str(), std::ios_base::binary);
  4511. if(is.good() == false)
  4512. {
  4513. dstream<<"ERROR: ServerMap::loadMapMeta(): "
  4514. <<"could not open"<<fullpath<<std::endl;
  4515. throw FileNotGoodException("Cannot open map metadata");
  4516. }
  4517. Settings params;
  4518. for(;;)
  4519. {
  4520. if(is.eof())
  4521. throw SerializationError
  4522. ("ServerMap::loadMapMeta(): [end_of_params] not found");
  4523. std::string line;
  4524. std::getline(is, line);
  4525. std::string trimmedline = trim(line);
  4526. if(trimmedline == "[end_of_params]")
  4527. break;
  4528. params.parseConfigLine(line);
  4529. }
  4530. m_seed = params.getU64("seed");
  4531. m_chunksize = params.getS32("chunksize");
  4532. dstream<<"INFO: ServerMap::loadMapMeta(): "
  4533. <<"seed="<<m_seed<<", chunksize="<<m_chunksize
  4534. <<std::endl;
  4535. }
  4536. void ServerMap::saveChunkMeta()
  4537. {
  4538. DSTACK(__FUNCTION_NAME);
  4539. // This should not be called if chunks are disabled.
  4540. assert(m_chunksize != 0);
  4541. u32 count = m_chunks.size();
  4542. dstream<<"INFO: ServerMap::saveChunkMeta(): Saving metadata of "
  4543. <<count<<" chunks"<<std::endl;
  4544. createDir(m_savedir);
  4545. std::string fullpath = m_savedir + "/chunk_meta";
  4546. std::ofstream os(fullpath.c_str(), std::ios_base::binary);
  4547. if(os.good() == false)
  4548. {
  4549. dstream<<"ERROR: ServerMap::saveChunkMeta(): "
  4550. <<"could not open"<<fullpath<<std::endl;
  4551. throw FileNotGoodException("Cannot open chunk metadata");
  4552. }
  4553. u8 version = 0;
  4554. // Write version
  4555. os.write((char*)&version, 1);
  4556. u8 buf[4];
  4557. // Write count
  4558. writeU32(buf, count);
  4559. os.write((char*)buf, 4);
  4560. for(core::map<v2s16, MapChunk*>::Iterator
  4561. i = m_chunks.getIterator();
  4562. i.atEnd()==false; i++)
  4563. {
  4564. v2s16 p = i.getNode()->getKey();
  4565. MapChunk *chunk = i.getNode()->getValue();
  4566. // Write position
  4567. writeV2S16(buf, p);
  4568. os.write((char*)buf, 4);
  4569. // Write chunk data
  4570. chunk->serialize(os, version);
  4571. }
  4572. setChunksNonModified();
  4573. }
  4574. void ServerMap::loadChunkMeta()
  4575. {
  4576. DSTACK(__FUNCTION_NAME);
  4577. dstream<<"INFO: ServerMap::loadChunkMeta(): Loading chunk metadata"
  4578. <<std::endl;
  4579. std::string fullpath = m_savedir + "/chunk_meta";
  4580. std::ifstream is(fullpath.c_str(), std::ios_base::binary);
  4581. if(is.good() == false)
  4582. {
  4583. dstream<<"ERROR: ServerMap::loadChunkMeta(): "
  4584. <<"could not open"<<fullpath<<std::endl;
  4585. throw FileNotGoodException("Cannot open chunk metadata");
  4586. }
  4587. u8 version = 0;
  4588. // Read version
  4589. is.read((char*)&version, 1);
  4590. u8 buf[4];
  4591. // Read count
  4592. is.read((char*)buf, 4);
  4593. u32 count = readU32(buf);
  4594. dstream<<"INFO: ServerMap::loadChunkMeta(): Loading metadata of "
  4595. <<count<<" chunks"<<std::endl;
  4596. for(u32 i=0; i<count; i++)
  4597. {
  4598. v2s16 p;
  4599. MapChunk *chunk = new MapChunk();
  4600. // Read position
  4601. is.read((char*)buf, 4);
  4602. p = readV2S16(buf);
  4603. // Read chunk data
  4604. chunk->deSerialize(is, version);
  4605. m_chunks.insert(p, chunk);
  4606. }
  4607. }
  4608. void ServerMap::saveSectorMeta(ServerMapSector *sector)
  4609. {
  4610. DSTACK(__FUNCTION_NAME);
  4611. // Format used for writing
  4612. u8 version = SER_FMT_VER_HIGHEST;
  4613. // Get destination
  4614. v2s16 pos = sector->getPos();
  4615. createDir(m_savedir);
  4616. createDir(m_savedir+"/sectors");
  4617. std::string dir = getSectorDir(pos);
  4618. createDir(dir);
  4619. std::string fullpath = dir + "/meta";
  4620. std::ofstream o(fullpath.c_str(), std::ios_base::binary);
  4621. if(o.good() == false)
  4622. throw FileNotGoodException("Cannot open sector metafile");
  4623. sector->serialize(o, version);
  4624. sector->differs_from_disk = false;
  4625. }
  4626. MapSector* ServerMap::loadSectorMeta(std::string dirname)
  4627. {
  4628. DSTACK(__FUNCTION_NAME);
  4629. // Get destination
  4630. v2s16 p2d = getSectorPos(dirname);
  4631. std::string dir = m_savedir + "/sectors/" + dirname;
  4632. ServerMapSector *sector = NULL;
  4633. std::string fullpath = dir + "/meta";
  4634. std::ifstream is(fullpath.c_str(), std::ios_base::binary);
  4635. if(is.good() == false)
  4636. {
  4637. // If the directory exists anyway, it probably is in some old
  4638. // format. Just go ahead and create the sector.
  4639. if(fs::PathExists(dir))
  4640. {
  4641. dstream<<"ServerMap::loadSectorMeta(): Sector metafile "
  4642. <<fullpath<<" doesn't exist but directory does."
  4643. <<" Continuing with a sector with no metadata."
  4644. <<std::endl;
  4645. sector = new ServerMapSector(this, p2d);
  4646. m_sectors.insert(p2d, sector);
  4647. }
  4648. else
  4649. throw FileNotGoodException("Cannot open sector metafile");
  4650. }
  4651. else
  4652. {
  4653. sector = ServerMapSector::deSerialize
  4654. (is, this, p2d, m_sectors);
  4655. }
  4656. sector->differs_from_disk = false;
  4657. return sector;
  4658. }
  4659. bool ServerMap::loadSectorFull(v2s16 p2d)
  4660. {
  4661. DSTACK(__FUNCTION_NAME);
  4662. std::string sectorsubdir = getSectorSubDir(p2d);
  4663. MapSector *sector = NULL;
  4664. //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
  4665. try{
  4666. sector = loadSectorMeta(sectorsubdir);
  4667. }
  4668. catch(InvalidFilenameException &e)
  4669. {
  4670. return false;
  4671. }
  4672. catch(FileNotGoodException &e)
  4673. {
  4674. return false;
  4675. }
  4676. catch(std::exception &e)
  4677. {
  4678. return false;
  4679. }
  4680. /*
  4681. Load blocks
  4682. */
  4683. std::vector<fs::DirListNode> list2 = fs::GetDirListing
  4684. (m_savedir+"/sectors/"+sectorsubdir);
  4685. std::vector<fs::DirListNode>::iterator i2;
  4686. for(i2=list2.begin(); i2!=list2.end(); i2++)
  4687. {
  4688. // We want files
  4689. if(i2->dir)
  4690. continue;
  4691. try{
  4692. loadBlock(sectorsubdir, i2->name, sector);
  4693. }
  4694. catch(InvalidFilenameException &e)
  4695. {
  4696. // This catches unknown crap in directory
  4697. }
  4698. }
  4699. return true;
  4700. }
  4701. void ServerMap::saveBlock(MapBlock *block)
  4702. {
  4703. DSTACK(__FUNCTION_NAME);
  4704. /*
  4705. Dummy blocks are not written
  4706. */
  4707. if(block->isDummy())
  4708. {
  4709. /*v3s16 p = block->getPos();
  4710. dstream<<"ServerMap::saveBlock(): WARNING: Not writing dummy block "
  4711. <<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;*/
  4712. return;
  4713. }
  4714. // Format used for writing
  4715. u8 version = SER_FMT_VER_HIGHEST;
  4716. // Get destination
  4717. v3s16 p3d = block->getPos();
  4718. v2s16 p2d(p3d.X, p3d.Z);
  4719. createDir(m_savedir);
  4720. createDir(m_savedir+"/sectors");
  4721. std::string dir = getSectorDir(p2d);
  4722. createDir(dir);
  4723. // Block file is map/sectors/xxxxxxxx/xxxx
  4724. char cc[5];
  4725. snprintf(cc, 5, "%.4x", (unsigned int)p3d.Y&0xffff);
  4726. std::string fullpath = dir + "/" + cc;
  4727. std::ofstream o(fullpath.c_str(), std::ios_base::binary);
  4728. if(o.good() == false)
  4729. throw FileNotGoodException("Cannot open block data");
  4730. /*
  4731. [0] u8 serialization version
  4732. [1] data
  4733. */
  4734. o.write((char*)&version, 1);
  4735. block->serialize(o, version);
  4736. /*
  4737. Versions up from 9 have block objects.
  4738. */
  4739. if(version >= 9)
  4740. {
  4741. block->serializeObjects(o, version);
  4742. }
  4743. /*
  4744. Versions up from 15 have static objects.
  4745. */
  4746. if(version >= 15)
  4747. {
  4748. block->m_static_objects.serialize(o);
  4749. }
  4750. // We just wrote it to the disk
  4751. block->resetChangedFlag();
  4752. }
  4753. void ServerMap::loadBlock(std::string sectordir, std::string blockfile, MapSector *sector)
  4754. {
  4755. DSTACK(__FUNCTION_NAME);
  4756. // Block file is map/sectors/xxxxxxxx/xxxx
  4757. std::string fullpath = m_savedir+"/sectors/"+sectordir+"/"+blockfile;
  4758. try{
  4759. std::ifstream is(fullpath.c_str(), std::ios_base::binary);
  4760. if(is.good() == false)
  4761. throw FileNotGoodException("Cannot open block file");
  4762. v3s16 p3d = getBlockPos(sectordir, blockfile);
  4763. v2s16 p2d(p3d.X, p3d.Z);
  4764. assert(sector->getPos() == p2d);
  4765. u8 version = SER_FMT_VER_INVALID;
  4766. is.read((char*)&version, 1);
  4767. if(is.fail())
  4768. throw SerializationError("ServerMap::loadBlock(): Failed"
  4769. " to read MapBlock version");
  4770. /*u32 block_size = MapBlock::serializedLength(version);
  4771. SharedBuffer<u8> data(block_size);
  4772. is.read((char*)*data, block_size);*/
  4773. // This will always return a sector because we're the server
  4774. //MapSector *sector = emergeSector(p2d);
  4775. MapBlock *block = NULL;
  4776. bool created_new = false;
  4777. try{
  4778. block = sector->getBlockNoCreate(p3d.Y);
  4779. }
  4780. catch(InvalidPositionException &e)
  4781. {
  4782. block = sector->createBlankBlockNoInsert(p3d.Y);
  4783. created_new = true;
  4784. }
  4785. // deserialize block data
  4786. block->deSerialize(is, version);
  4787. /*
  4788. Versions up from 9 have block objects.
  4789. */
  4790. if(version >= 9)
  4791. {
  4792. block->updateObjects(is, version, NULL, 0);
  4793. }
  4794. /*
  4795. Versions up from 15 have static objects.
  4796. */
  4797. if(version >= 15)
  4798. {
  4799. block->m_static_objects.deSerialize(is);
  4800. }
  4801. if(created_new)
  4802. sector->insertBlock(block);
  4803. /*
  4804. Convert old formats to new and save
  4805. */
  4806. // Save old format blocks in new format
  4807. if(version < SER_FMT_VER_HIGHEST)
  4808. {
  4809. saveBlock(block);
  4810. }
  4811. // We just loaded it from the disk, so it's up-to-date.
  4812. block->resetChangedFlag();
  4813. }
  4814. catch(SerializationError &e)
  4815. {
  4816. dstream<<"WARNING: Invalid block data on disk "
  4817. "(SerializationError). Ignoring. "
  4818. "A new one will be generated."
  4819. <<std::endl;
  4820. // TODO: Backup file; name is in fullpath.
  4821. }
  4822. }
  4823. void ServerMap::PrintInfo(std::ostream &out)
  4824. {
  4825. out<<"ServerMap: ";
  4826. }
  4827. #ifndef SERVER
  4828. /*
  4829. ClientMap
  4830. */
  4831. ClientMap::ClientMap(
  4832. Client *client,
  4833. MapDrawControl &control,
  4834. scene::ISceneNode* parent,
  4835. scene::ISceneManager* mgr,
  4836. s32 id
  4837. ):
  4838. Map(dout_client),
  4839. scene::ISceneNode(parent, mgr, id),
  4840. m_client(client),
  4841. m_control(control),
  4842. m_camera_position(0,0,0),
  4843. m_camera_direction(0,0,1)
  4844. {
  4845. m_camera_mutex.Init();
  4846. assert(m_camera_mutex.IsInitialized());
  4847. m_box = core::aabbox3d<f32>(-BS*1000000,-BS*1000000,-BS*1000000,
  4848. BS*1000000,BS*1000000,BS*1000000);
  4849. }
  4850. ClientMap::~ClientMap()
  4851. {
  4852. /*JMutexAutoLock lock(mesh_mutex);
  4853. if(mesh != NULL)
  4854. {
  4855. mesh->drop();
  4856. mesh = NULL;
  4857. }*/
  4858. }
  4859. MapSector * ClientMap::emergeSector(v2s16 p2d)
  4860. {
  4861. DSTACK(__FUNCTION_NAME);
  4862. // Check that it doesn't exist already
  4863. try{
  4864. return getSectorNoGenerate(p2d);
  4865. }
  4866. catch(InvalidPositionException &e)
  4867. {
  4868. }
  4869. // Create a sector
  4870. ClientMapSector *sector = new ClientMapSector(this, p2d);
  4871. {
  4872. //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
  4873. m_sectors.insert(p2d, sector);
  4874. }
  4875. return sector;
  4876. }
  4877. void ClientMap::deSerializeSector(v2s16 p2d, std::istream &is)
  4878. {
  4879. DSTACK(__FUNCTION_NAME);
  4880. ClientMapSector *sector = NULL;
  4881. //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
  4882. core::map<v2s16, MapSector*>::Node *n = m_sectors.find(p2d);
  4883. if(n != NULL)
  4884. {
  4885. sector = (ClientMapSector*)n->getValue();
  4886. assert(sector->getId() == MAPSECTOR_CLIENT);
  4887. }
  4888. else
  4889. {
  4890. sector = new ClientMapSector(this, p2d);
  4891. {
  4892. //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
  4893. m_sectors.insert(p2d, sector);
  4894. }
  4895. }
  4896. sector->deSerialize(is);
  4897. }
  4898. void ClientMap::OnRegisterSceneNode()
  4899. {
  4900. if(IsVisible)
  4901. {
  4902. SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID);
  4903. SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT);
  4904. }
  4905. ISceneNode::OnRegisterSceneNode();
  4906. }
  4907. void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
  4908. {
  4909. //m_dout<<DTIME<<"Rendering map..."<<std::endl;
  4910. DSTACK(__FUNCTION_NAME);
  4911. bool is_transparent_pass = pass == scene::ESNRP_TRANSPARENT;
  4912. /*
  4913. Get time for measuring timeout.
  4914. Measuring time is very useful for long delays when the
  4915. machine is swapping a lot.
  4916. */
  4917. int time1 = time(0);
  4918. //u32 daynight_ratio = m_client->getDayNightRatio();
  4919. m_camera_mutex.Lock();
  4920. v3f camera_position = m_camera_position;
  4921. v3f camera_direction = m_camera_direction;
  4922. m_camera_mutex.Unlock();
  4923. /*
  4924. Get all blocks and draw all visible ones
  4925. */
  4926. v3s16 cam_pos_nodes(
  4927. camera_position.X / BS,
  4928. camera_position.Y / BS,
  4929. camera_position.Z / BS);
  4930. v3s16 box_nodes_d = m_control.wanted_range * v3s16(1,1,1);
  4931. v3s16 p_nodes_min = cam_pos_nodes - box_nodes_d;
  4932. v3s16 p_nodes_max = cam_pos_nodes + box_nodes_d;
  4933. // Take a fair amount as we will be dropping more out later
  4934. v3s16 p_blocks_min = getNodeBlockPos(p_nodes_min) - v3s16(1,1,1);
  4935. v3s16 p_blocks_max = getNodeBlockPos(p_nodes_max) + v3s16(1,1,1);
  4936. u32 vertex_count = 0;
  4937. // For limiting number of mesh updates per frame
  4938. u32 mesh_update_count = 0;
  4939. u32 blocks_would_have_drawn = 0;
  4940. u32 blocks_drawn = 0;
  4941. //NOTE: The sectors map should be locked but we're not doing it
  4942. // because it'd cause too much delays
  4943. int timecheck_counter = 0;
  4944. core::map<v2s16, MapSector*>::Iterator si;
  4945. si = m_sectors.getIterator();
  4946. for(; si.atEnd() == false; si++)
  4947. {
  4948. {
  4949. timecheck_counter++;
  4950. if(timecheck_counter > 50)
  4951. {
  4952. timecheck_counter = 0;
  4953. int time2 = time(0);
  4954. if(time2 > time1 + 4)
  4955. {
  4956. dstream<<"ClientMap::renderMap(): "
  4957. "Rendering takes ages, returning."
  4958. <<std::endl;
  4959. return;
  4960. }
  4961. }
  4962. }
  4963. MapSector *sector = si.getNode()->getValue();
  4964. v2s16 sp = sector->getPos();
  4965. if(m_control.range_all == false)
  4966. {
  4967. if(sp.X < p_blocks_min.X
  4968. || sp.X > p_blocks_max.X
  4969. || sp.Y < p_blocks_min.Z
  4970. || sp.Y > p_blocks_max.Z)
  4971. continue;
  4972. }
  4973. core::list< MapBlock * > sectorblocks;
  4974. sector->getBlocks(sectorblocks);
  4975. /*
  4976. Draw blocks
  4977. */
  4978. core::list< MapBlock * >::Iterator i;
  4979. for(i=sectorblocks.begin(); i!=sectorblocks.end(); i++)
  4980. {
  4981. MapBlock *block = *i;
  4982. /*
  4983. Compare block position to camera position, skip
  4984. if not seen on display
  4985. */
  4986. float range = 100000 * BS;
  4987. if(m_control.range_all == false)
  4988. range = m_control.wanted_range * BS;
  4989. float d = 0.0;
  4990. if(isBlockInSight(block->getPos(), camera_position,
  4991. camera_direction, range, &d) == false)
  4992. {
  4993. continue;
  4994. }
  4995. // This is ugly (spherical distance limit?)
  4996. /*if(m_control.range_all == false &&
  4997. d - 0.5*BS*MAP_BLOCKSIZE > range)
  4998. continue;*/
  4999. #if 1
  5000. /*
  5001. Update expired mesh (used for day/night change)
  5002. It doesn't work exactly like it should now with the
  5003. tasked mesh update but whatever.
  5004. */
  5005. bool mesh_expired = false;
  5006. {
  5007. JMutexAutoLock lock(block->mesh_mutex);
  5008. mesh_expired = block->getMeshExpired();
  5009. // Mesh has not been expired and there is no mesh:
  5010. // block has no content
  5011. if(block->mesh == NULL && mesh_expired == false)
  5012. continue;
  5013. }
  5014. f32 faraway = BS*50;
  5015. //f32 faraway = m_control.wanted_range * BS;
  5016. /*
  5017. This has to be done with the mesh_mutex unlocked
  5018. */
  5019. // Pretty random but this should work somewhat nicely
  5020. if(mesh_expired && (
  5021. (mesh_update_count < 3
  5022. && (d < faraway || mesh_update_count < 2)
  5023. )
  5024. ||
  5025. (m_control.range_all && mesh_update_count < 20)
  5026. )
  5027. )
  5028. /*if(mesh_expired && mesh_update_count < 6
  5029. && (d < faraway || mesh_update_count < 3))*/
  5030. {
  5031. mesh_update_count++;
  5032. // Mesh has been expired: generate new mesh
  5033. //block->updateMesh(daynight_ratio);
  5034. m_client->addUpdateMeshTask(block->getPos());
  5035. mesh_expired = false;
  5036. }
  5037. #endif
  5038. /*
  5039. Draw the faces of the block
  5040. */
  5041. {
  5042. JMutexAutoLock lock(block->mesh_mutex);
  5043. scene::SMesh *mesh = block->mesh;
  5044. if(mesh == NULL)
  5045. continue;
  5046. blocks_would_have_drawn++;
  5047. if(blocks_drawn >= m_control.wanted_max_blocks
  5048. && m_control.range_all == false
  5049. && d > m_control.wanted_min_range * BS)
  5050. continue;
  5051. blocks_drawn++;
  5052. u32 c = mesh->getMeshBufferCount();
  5053. for(u32 i=0; i<c; i++)
  5054. {
  5055. scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
  5056. const video::SMaterial& material = buf->getMaterial();
  5057. video::IMaterialRenderer* rnd =
  5058. driver->getMaterialRenderer(material.MaterialType);
  5059. bool transparent = (rnd && rnd->isTransparent());
  5060. // Render transparent on transparent pass and likewise.
  5061. if(transparent == is_transparent_pass)
  5062. {
  5063. /*
  5064. This *shouldn't* hurt too much because Irrlicht
  5065. doesn't change opengl textures if the old
  5066. material is set again.
  5067. */
  5068. driver->setMaterial(buf->getMaterial());
  5069. driver->drawMeshBuffer(buf);
  5070. vertex_count += buf->getVertexCount();
  5071. }
  5072. }
  5073. }
  5074. } // foreach sectorblocks
  5075. }
  5076. m_control.blocks_drawn = blocks_drawn;
  5077. m_control.blocks_would_have_drawn = blocks_would_have_drawn;
  5078. /*dstream<<"renderMap(): is_transparent_pass="<<is_transparent_pass
  5079. <<", rendered "<<vertex_count<<" vertices."<<std::endl;*/
  5080. }
  5081. bool ClientMap::setTempMod(v3s16 p, NodeMod mod,
  5082. core::map<v3s16, MapBlock*> *affected_blocks)
  5083. {
  5084. bool changed = false;
  5085. /*
  5086. Add it to all blocks touching it
  5087. */
  5088. v3s16 dirs[7] = {
  5089. v3s16(0,0,0), // this
  5090. v3s16(0,0,1), // back
  5091. v3s16(0,1,0), // top
  5092. v3s16(1,0,0), // right
  5093. v3s16(0,0,-1), // front
  5094. v3s16(0,-1,0), // bottom
  5095. v3s16(-1,0,0), // left
  5096. };
  5097. for(u16 i=0; i<7; i++)
  5098. {
  5099. v3s16 p2 = p + dirs[i];
  5100. // Block position of neighbor (or requested) node
  5101. v3s16 blockpos = getNodeBlockPos(p2);
  5102. MapBlock * blockref = getBlockNoCreateNoEx(blockpos);
  5103. if(blockref == NULL)
  5104. continue;
  5105. // Relative position of requested node
  5106. v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
  5107. if(blockref->setTempMod(relpos, mod))
  5108. {
  5109. changed = true;
  5110. }
  5111. }
  5112. if(changed && affected_blocks!=NULL)
  5113. {
  5114. for(u16 i=0; i<7; i++)
  5115. {
  5116. v3s16 p2 = p + dirs[i];
  5117. // Block position of neighbor (or requested) node
  5118. v3s16 blockpos = getNodeBlockPos(p2);
  5119. MapBlock * blockref = getBlockNoCreateNoEx(blockpos);
  5120. if(blockref == NULL)
  5121. continue;
  5122. affected_blocks->insert(blockpos, blockref);
  5123. }
  5124. }
  5125. return changed;
  5126. }
  5127. bool ClientMap::clearTempMod(v3s16 p,
  5128. core::map<v3s16, MapBlock*> *affected_blocks)
  5129. {
  5130. bool changed = false;
  5131. v3s16 dirs[7] = {
  5132. v3s16(0,0,0), // this
  5133. v3s16(0,0,1), // back
  5134. v3s16(0,1,0), // top
  5135. v3s16(1,0,0), // right
  5136. v3s16(0,0,-1), // front
  5137. v3s16(0,-1,0), // bottom
  5138. v3s16(-1,0,0), // left
  5139. };
  5140. for(u16 i=0; i<7; i++)
  5141. {
  5142. v3s16 p2 = p + dirs[i];
  5143. // Block position of neighbor (or requested) node
  5144. v3s16 blockpos = getNodeBlockPos(p2);
  5145. MapBlock * blockref = getBlockNoCreateNoEx(blockpos);
  5146. if(blockref == NULL)
  5147. continue;
  5148. // Relative position of requested node
  5149. v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
  5150. if(blockref->clearTempMod(relpos))
  5151. {
  5152. changed = true;
  5153. }
  5154. }
  5155. if(changed && affected_blocks!=NULL)
  5156. {
  5157. for(u16 i=0; i<7; i++)
  5158. {
  5159. v3s16 p2 = p + dirs[i];
  5160. // Block position of neighbor (or requested) node
  5161. v3s16 blockpos = getNodeBlockPos(p2);
  5162. MapBlock * blockref = getBlockNoCreateNoEx(blockpos);
  5163. if(blockref == NULL)
  5164. continue;
  5165. affected_blocks->insert(blockpos, blockref);
  5166. }
  5167. }
  5168. return changed;
  5169. }
  5170. void ClientMap::expireMeshes(bool only_daynight_diffed)
  5171. {
  5172. TimeTaker timer("expireMeshes()");
  5173. core::map<v2s16, MapSector*>::Iterator si;
  5174. si = m_sectors.getIterator();
  5175. for(; si.atEnd() == false; si++)
  5176. {
  5177. MapSector *sector = si.getNode()->getValue();
  5178. core::list< MapBlock * > sectorblocks;
  5179. sector->getBlocks(sectorblocks);
  5180. core::list< MapBlock * >::Iterator i;
  5181. for(i=sectorblocks.begin(); i!=sectorblocks.end(); i++)
  5182. {
  5183. MapBlock *block = *i;
  5184. if(only_daynight_diffed && dayNightDiffed(block->getPos()) == false)
  5185. {
  5186. continue;
  5187. }
  5188. {
  5189. JMutexAutoLock lock(block->mesh_mutex);
  5190. if(block->mesh != NULL)
  5191. {
  5192. /*block->mesh->drop();
  5193. block->mesh = NULL;*/
  5194. block->setMeshExpired(true);
  5195. }
  5196. }
  5197. }
  5198. }
  5199. }
  5200. void ClientMap::updateMeshes(v3s16 blockpos, u32 daynight_ratio)
  5201. {
  5202. assert(mapType() == MAPTYPE_CLIENT);
  5203. try{
  5204. v3s16 p = blockpos + v3s16(0,0,0);
  5205. MapBlock *b = getBlockNoCreate(p);
  5206. b->updateMesh(daynight_ratio);
  5207. //b->setMeshExpired(true);
  5208. }
  5209. catch(InvalidPositionException &e){}
  5210. // Leading edge
  5211. try{
  5212. v3s16 p = blockpos + v3s16(-1,0,0);
  5213. MapBlock *b = getBlockNoCreate(p);
  5214. b->updateMesh(daynight_ratio);
  5215. //b->setMeshExpired(true);
  5216. }
  5217. catch(InvalidPositionException &e){}
  5218. try{
  5219. v3s16 p = blockpos + v3s16(0,-1,0);
  5220. MapBlock *b = getBlockNoCreate(p);
  5221. b->updateMesh(daynight_ratio);
  5222. //b->setMeshExpired(true);
  5223. }
  5224. catch(InvalidPositionException &e){}
  5225. try{
  5226. v3s16 p = blockpos + v3s16(0,0,-1);
  5227. MapBlock *b = getBlockNoCreate(p);
  5228. b->updateMesh(daynight_ratio);
  5229. //b->setMeshExpired(true);
  5230. }
  5231. catch(InvalidPositionException &e){}
  5232. }
  5233. #if 0
  5234. /*
  5235. Update mesh of block in which the node is, and if the node is at the
  5236. leading edge, update the appropriate leading blocks too.
  5237. */
  5238. void ClientMap::updateNodeMeshes(v3s16 nodepos, u32 daynight_ratio)
  5239. {
  5240. v3s16 dirs[4] = {
  5241. v3s16(0,0,0),
  5242. v3s16(-1,0,0),
  5243. v3s16(0,-1,0),
  5244. v3s16(0,0,-1),
  5245. };
  5246. v3s16 blockposes[4];
  5247. for(u32 i=0; i<4; i++)
  5248. {
  5249. v3s16 np = nodepos + dirs[i];
  5250. blockposes[i] = getNodeBlockPos(np);
  5251. // Don't update mesh of block if it has been done already
  5252. bool already_updated = false;
  5253. for(u32 j=0; j<i; j++)
  5254. {
  5255. if(blockposes[j] == blockposes[i])
  5256. {
  5257. already_updated = true;
  5258. break;
  5259. }
  5260. }
  5261. if(already_updated)
  5262. continue;
  5263. // Update mesh
  5264. MapBlock *b = getBlockNoCreate(blockposes[i]);
  5265. b->updateMesh(daynight_ratio);
  5266. }
  5267. }
  5268. #endif
  5269. void ClientMap::PrintInfo(std::ostream &out)
  5270. {
  5271. out<<"ClientMap: ";
  5272. }
  5273. #endif // !SERVER
  5274. /*
  5275. MapVoxelManipulator
  5276. */
  5277. MapVoxelManipulator::MapVoxelManipulator(Map *map)
  5278. {
  5279. m_map = map;
  5280. }
  5281. MapVoxelManipulator::~MapVoxelManipulator()
  5282. {
  5283. /*dstream<<"MapVoxelManipulator: blocks: "<<m_loaded_blocks.size()
  5284. <<std::endl;*/
  5285. }
  5286. void MapVoxelManipulator::emerge(VoxelArea a, s32 caller_id)
  5287. {
  5288. TimeTaker timer1("emerge", &emerge_time);
  5289. // Units of these are MapBlocks
  5290. v3s16 p_min = getNodeBlockPos(a.MinEdge);
  5291. v3s16 p_max = getNodeBlockPos(a.MaxEdge);
  5292. VoxelArea block_area_nodes
  5293. (p_min*MAP_BLOCKSIZE, (p_max+1)*MAP_BLOCKSIZE-v3s16(1,1,1));
  5294. addArea(block_area_nodes);
  5295. for(s32 z=p_min.Z; z<=p_max.Z; z++)
  5296. for(s32 y=p_min.Y; y<=p_max.Y; y++)
  5297. for(s32 x=p_min.X; x<=p_max.X; x++)
  5298. {
  5299. v3s16 p(x,y,z);
  5300. core::map<v3s16, bool>::Node *n;
  5301. n = m_loaded_blocks.find(p);
  5302. if(n != NULL)
  5303. continue;
  5304. bool block_data_inexistent = false;
  5305. try
  5306. {
  5307. TimeTaker timer1("emerge load", &emerge_load_time);
  5308. /*dstream<<"Loading block (caller_id="<<caller_id<<")"
  5309. <<" ("<<p.X<<","<<p.Y<<","<<p.Z<<")"
  5310. <<" wanted area: ";
  5311. a.print(dstream);
  5312. dstream<<std::endl;*/
  5313. MapBlock *block = m_map->getBlockNoCreate(p);
  5314. if(block->isDummy())
  5315. block_data_inexistent = true;
  5316. else
  5317. block->copyTo(*this);
  5318. }
  5319. catch(InvalidPositionException &e)
  5320. {
  5321. block_data_inexistent = true;
  5322. }
  5323. if(block_data_inexistent)
  5324. {
  5325. VoxelArea a(p*MAP_BLOCKSIZE, (p+1)*MAP_BLOCKSIZE-v3s16(1,1,1));
  5326. // Fill with VOXELFLAG_INEXISTENT
  5327. for(s32 z=a.MinEdge.Z; z<=a.MaxEdge.Z; z++)
  5328. for(s32 y=a.MinEdge.Y; y<=a.MaxEdge.Y; y++)
  5329. {
  5330. s32 i = m_area.index(a.MinEdge.X,y,z);
  5331. memset(&m_flags[i], VOXELFLAG_INEXISTENT, MAP_BLOCKSIZE);
  5332. }
  5333. }
  5334. m_loaded_blocks.insert(p, !block_data_inexistent);
  5335. }
  5336. //dstream<<"emerge done"<<std::endl;
  5337. }
  5338. /*
  5339. SUGG: Add an option to only update eg. water and air nodes.
  5340. This will make it interfere less with important stuff if
  5341. run on background.
  5342. */
  5343. void MapVoxelManipulator::blitBack
  5344. (core::map<v3s16, MapBlock*> & modified_blocks)
  5345. {
  5346. if(m_area.getExtent() == v3s16(0,0,0))
  5347. return;
  5348. //TimeTaker timer1("blitBack");
  5349. /*dstream<<"blitBack(): m_loaded_blocks.size()="
  5350. <<m_loaded_blocks.size()<<std::endl;*/
  5351. /*
  5352. Initialize block cache
  5353. */
  5354. v3s16 blockpos_last;
  5355. MapBlock *block = NULL;
  5356. bool block_checked_in_modified = false;
  5357. for(s32 z=m_area.MinEdge.Z; z<=m_area.MaxEdge.Z; z++)
  5358. for(s32 y=m_area.MinEdge.Y; y<=m_area.MaxEdge.Y; y++)
  5359. for(s32 x=m_area.MinEdge.X; x<=m_area.MaxEdge.X; x++)
  5360. {
  5361. v3s16 p(x,y,z);
  5362. u8 f = m_flags[m_area.index(p)];
  5363. if(f & (VOXELFLAG_NOT_LOADED|VOXELFLAG_INEXISTENT))
  5364. continue;
  5365. MapNode &n = m_data[m_area.index(p)];
  5366. v3s16 blockpos = getNodeBlockPos(p);
  5367. try
  5368. {
  5369. // Get block
  5370. if(block == NULL || blockpos != blockpos_last){
  5371. block = m_map->getBlockNoCreate(blockpos);
  5372. blockpos_last = blockpos;
  5373. block_checked_in_modified = false;
  5374. }
  5375. // Calculate relative position in block
  5376. v3s16 relpos = p - blockpos * MAP_BLOCKSIZE;
  5377. // Don't continue if nothing has changed here
  5378. if(block->getNode(relpos) == n)
  5379. continue;
  5380. //m_map->setNode(m_area.MinEdge + p, n);
  5381. block->setNode(relpos, n);
  5382. /*
  5383. Make sure block is in modified_blocks
  5384. */
  5385. if(block_checked_in_modified == false)
  5386. {
  5387. modified_blocks[blockpos] = block;
  5388. block_checked_in_modified = true;
  5389. }
  5390. }
  5391. catch(InvalidPositionException &e)
  5392. {
  5393. }
  5394. }
  5395. }
  5396. ManualMapVoxelManipulator::ManualMapVoxelManipulator(Map *map):
  5397. MapVoxelManipulator(map),
  5398. m_create_area(false)
  5399. {
  5400. }
  5401. ManualMapVoxelManipulator::~ManualMapVoxelManipulator()
  5402. {
  5403. }
  5404. void ManualMapVoxelManipulator::emerge(VoxelArea a, s32 caller_id)
  5405. {
  5406. // Just create the area so that it can be pointed to
  5407. VoxelManipulator::emerge(a, caller_id);
  5408. }
  5409. void ManualMapVoxelManipulator::initialEmerge(
  5410. v3s16 blockpos_min, v3s16 blockpos_max)
  5411. {
  5412. TimeTaker timer1("initialEmerge", &emerge_time);
  5413. // Units of these are MapBlocks
  5414. v3s16 p_min = blockpos_min;
  5415. v3s16 p_max = blockpos_max;
  5416. VoxelArea block_area_nodes
  5417. (p_min*MAP_BLOCKSIZE, (p_max+1)*MAP_BLOCKSIZE-v3s16(1,1,1));
  5418. u32 size_MB = block_area_nodes.getVolume()*4/1000000;
  5419. if(size_MB >= 1)
  5420. {
  5421. dstream<<"initialEmerge: area: ";
  5422. block_area_nodes.print(dstream);
  5423. dstream<<" ("<<size_MB<<"MB)";
  5424. dstream<<std::endl;
  5425. }
  5426. addArea(block_area_nodes);
  5427. for(s32 z=p_min.Z; z<=p_max.Z; z++)
  5428. for(s32 y=p_min.Y; y<=p_max.Y; y++)
  5429. for(s32 x=p_min.X; x<=p_max.X; x++)
  5430. {
  5431. v3s16 p(x,y,z);
  5432. core::map<v3s16, bool>::Node *n;
  5433. n = m_loaded_blocks.find(p);
  5434. if(n != NULL)
  5435. continue;
  5436. bool block_data_inexistent = false;
  5437. try
  5438. {
  5439. TimeTaker timer1("emerge load", &emerge_load_time);
  5440. MapBlock *block = m_map->getBlockNoCreate(p);
  5441. if(block->isDummy())
  5442. block_data_inexistent = true;
  5443. else
  5444. block->copyTo(*this);
  5445. }
  5446. catch(InvalidPositionException &e)
  5447. {
  5448. block_data_inexistent = true;
  5449. }
  5450. if(block_data_inexistent)
  5451. {
  5452. /*
  5453. Mark area inexistent
  5454. */
  5455. VoxelArea a(p*MAP_BLOCKSIZE, (p+1)*MAP_BLOCKSIZE-v3s16(1,1,1));
  5456. // Fill with VOXELFLAG_INEXISTENT
  5457. for(s32 z=a.MinEdge.Z; z<=a.MaxEdge.Z; z++)
  5458. for(s32 y=a.MinEdge.Y; y<=a.MaxEdge.Y; y++)
  5459. {
  5460. s32 i = m_area.index(a.MinEdge.X,y,z);
  5461. memset(&m_flags[i], VOXELFLAG_INEXISTENT, MAP_BLOCKSIZE);
  5462. }
  5463. }
  5464. m_loaded_blocks.insert(p, !block_data_inexistent);
  5465. }
  5466. }
  5467. void ManualMapVoxelManipulator::blitBackAll(
  5468. core::map<v3s16, MapBlock*> * modified_blocks)
  5469. {
  5470. if(m_area.getExtent() == v3s16(0,0,0))
  5471. return;
  5472. /*
  5473. Copy data of all blocks
  5474. */
  5475. for(core::map<v3s16, bool>::Iterator
  5476. i = m_loaded_blocks.getIterator();
  5477. i.atEnd() == false; i++)
  5478. {
  5479. bool existed = i.getNode()->getValue();
  5480. if(existed == false)
  5481. continue;
  5482. v3s16 p = i.getNode()->getKey();
  5483. MapBlock *block = m_map->getBlockNoCreateNoEx(p);
  5484. if(block == NULL)
  5485. {
  5486. dstream<<"WARNING: "<<__FUNCTION_NAME
  5487. <<": got NULL block "
  5488. <<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"
  5489. <<std::endl;
  5490. continue;
  5491. }
  5492. block->copyFrom(*this);
  5493. if(modified_blocks)
  5494. modified_blocks->insert(p, block);
  5495. }
  5496. }
  5497. //END