map.cpp 142 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394
  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