3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #define sleep_s(x) Sleep((x*1000))
33 #define sleep_s(x) sleep(x)
36 #include "common_irrlicht.h"
37 #include "heightmap.h"
38 #include "loadstatus.h"
41 #include "mapsector.h"
42 #include "constants.h"
48 A cache for short-term fast access to map data
50 NOTE: This doesn't really make anything more efficient
51 NOTE: Use VoxelManipulator, if possible
52 TODO: Get rid of this?
53 NOTE: CONFIRMED: THIS CACHE DOESN'T MAKE ANYTHING ANY FASTER
55 class MapBlockPointerCache : public NodeContainer
58 MapBlockPointerCache(Map *map);
59 ~MapBlockPointerCache();
61 virtual u16 nodeContainerId() const
63 return NODECONTAINER_ID_MAPBLOCKCACHE;
66 MapBlock * getBlockNoCreate(v3s16 p);
68 // virtual from NodeContainer
69 bool isValidPosition(v3s16 p)
71 v3s16 blockpos = getNodeBlockPos(p);
74 blockref = getBlockNoCreate(blockpos);
76 catch(InvalidPositionException &e)
83 // virtual from NodeContainer
84 MapNode getNode(v3s16 p)
86 v3s16 blockpos = getNodeBlockPos(p);
87 MapBlock * blockref = getBlockNoCreate(blockpos);
88 v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
90 return blockref->getNodeNoCheck(relpos);
93 // virtual from NodeContainer
94 void setNode(v3s16 p, MapNode & n)
96 v3s16 blockpos = getNodeBlockPos(p);
97 MapBlock * block = getBlockNoCreate(blockpos);
98 v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
99 block->setNodeNoCheck(relpos, n);
100 m_modified_blocks[blockpos] = block;
103 core::map<v3s16, MapBlock*> m_modified_blocks;
107 core::map<v3s16, MapBlock*> m_blocks;
109 u32 m_from_cache_count;
110 u32 m_from_map_count;
119 m_count_mutex.Init();
120 m_cache_mutex.Init();
121 m_waitcache_mutex.Init();
126 //dstream<<"cacheCreated() begin"<<std::endl;
127 JMutexAutoLock waitcachelock(m_waitcache_mutex);
128 JMutexAutoLock countlock(m_count_mutex);
130 // If this is the first cache, grab the cache lock
132 m_cache_mutex.Lock();
136 //dstream<<"cacheCreated() end"<<std::endl;
141 //dstream<<"cacheRemoved() begin"<<std::endl;
142 JMutexAutoLock countlock(m_count_mutex);
148 // If this is the last one, release the cache lock
150 m_cache_mutex.Unlock();
152 //dstream<<"cacheRemoved() end"<<std::endl;
156 This lock should be taken when removing stuff that can be
157 pointed by the cache.
159 You'll want to grab this in a SharedPtr.
161 JMutexAutoLock * waitCaches()
163 //dstream<<"waitCaches() begin"<<std::endl;
164 JMutexAutoLock waitcachelock(m_waitcache_mutex);
165 JMutexAutoLock *lock = new JMutexAutoLock(m_cache_mutex);
166 //dstream<<"waitCaches() end"<<std::endl;
171 // Count of existing caches
173 JMutex m_count_mutex;
174 // This is locked always when there are some caches
175 JMutex m_cache_mutex;
176 // Locked so that when waitCaches() is called, no more caches are created
177 JMutex m_waitcache_mutex;
180 #define MAPTYPE_BASE 0
181 #define MAPTYPE_SERVER 1
182 #define MAPTYPE_CLIENT 2
184 class Map : public NodeContainer, public Heightmappish
188 std::ostream &m_dout;
190 core::map<v2s16, MapSector*> m_sectors;
191 JMutex m_sector_mutex;
193 v3f m_camera_position;
194 v3f m_camera_direction;
195 JMutex m_camera_mutex;
197 // Be sure to set this to NULL when the cached sector is deleted
198 MapSector *m_sector_cache;
199 v2s16 m_sector_cache_p;
201 WrapperHeightmap m_hwrapper;
205 v3s16 drawoffset; // for drawbox()
208 Used by MapBlockPointerCache.
210 waitCaches() can be called to remove all caches before continuing
212 CacheLock m_blockcachelock;
214 Map(std::ostream &dout);
217 virtual u16 nodeContainerId() const
219 return NODECONTAINER_ID_MAP;
222 virtual s32 mapType() const
227 void updateCamera(v3f pos, v3f dir)
229 JMutexAutoLock lock(m_camera_mutex);
230 m_camera_position = pos;
231 m_camera_direction = dir;
234 /*void StartUpdater()
241 updater.setRun(false);
242 while(updater.IsRunning())
246 bool UpdaterIsRunning()
248 return updater.IsRunning();
251 static core::aabbox3d<f32> getNodeBox(v3s16 p)
253 return core::aabbox3d<f32>(
254 (float)p.X * BS - 0.5*BS,
255 (float)p.Y * BS - 0.5*BS,
256 (float)p.Z * BS - 0.5*BS,
257 (float)p.X * BS + 0.5*BS,
258 (float)p.Y * BS + 0.5*BS,
259 (float)p.Z * BS + 0.5*BS
263 //bool sectorExists(v2s16 p);
264 MapSector * getSectorNoGenerate(v2s16 p2d);
266 This is overloaded by ClientMap and ServerMap to allow
267 their differing fetch methods.
269 virtual MapSector * emergeSector(v2s16 p) = 0;
271 // Returns InvalidPositionException if not found
272 MapBlock * getBlockNoCreate(v3s16 p);
273 //virtual MapBlock * getBlock(v3s16 p, bool generate=true);
275 // Returns InvalidPositionException if not found
276 f32 getGroundHeight(v2s16 p, bool generate=false);
277 void setGroundHeight(v2s16 p, f32 y, bool generate=false);
279 // Returns InvalidPositionException if not found
280 bool isNodeUnderground(v3s16 p);
282 // virtual from NodeContainer
283 bool isValidPosition(v3s16 p)
285 v3s16 blockpos = getNodeBlockPos(p);
288 blockref = getBlockNoCreate(blockpos);
290 catch(InvalidPositionException &e)
295 /*v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
296 bool is_valid = blockref->isValidPosition(relpos);
300 // virtual from NodeContainer
301 MapNode getNode(v3s16 p)
303 v3s16 blockpos = getNodeBlockPos(p);
304 MapBlock * blockref = getBlockNoCreate(blockpos);
305 v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
307 return blockref->getNodeNoCheck(relpos);
310 // virtual from NodeContainer
311 void setNode(v3s16 p, MapNode & n)
313 v3s16 blockpos = getNodeBlockPos(p);
314 MapBlock * blockref = getBlockNoCreate(blockpos);
315 v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
316 blockref->setNodeNoCheck(relpos, n);
319 /*MapNode getNodeGenerate(v3s16 p)
321 v3s16 blockpos = getNodeBlockPos(p);
322 MapBlock * blockref = getBlock(blockpos);
323 v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
325 return blockref->getNode(relpos);
328 /*void setNodeGenerate(v3s16 p, MapNode & n)
330 v3s16 blockpos = getNodeBlockPos(p);
331 MapBlock * blockref = getBlock(blockpos);
332 v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
333 blockref->setNode(relpos, n);
336 void unspreadLight(core::map<v3s16, u8> & from_nodes,
337 core::map<v3s16, bool> & light_sources,
338 core::map<v3s16, MapBlock*> & modified_blocks);
340 void unLightNeighbors(v3s16 pos, u8 lightwas,
341 core::map<v3s16, bool> & light_sources,
342 core::map<v3s16, MapBlock*> & modified_blocks);
344 void spreadLight(core::map<v3s16, bool> & from_nodes,
345 core::map<v3s16, MapBlock*> & modified_blocks);
347 void lightNeighbors(v3s16 pos,
348 core::map<v3s16, MapBlock*> & modified_blocks);
350 v3s16 getBrightestNeighbour(v3s16 p);
352 s16 propagateSunlight(v3s16 start,
353 core::map<v3s16, MapBlock*> & modified_blocks);
355 void updateLighting(core::map<v3s16, MapBlock*> & a_blocks,
356 core::map<v3s16, MapBlock*> & modified_blocks);
359 These handle lighting but not faces.
361 void addNodeAndUpdate(v3s16 p, MapNode n,
362 core::map<v3s16, MapBlock*> &modified_blocks);
363 void removeNodeAndUpdate(v3s16 p,
364 core::map<v3s16, MapBlock*> &modified_blocks);
367 Updates the faces of the given block and blocks on the
370 void updateMeshes(v3s16 blockpos);
372 //core::aabbox3d<s16> getDisplayedBlockArea();
374 //bool updateChangedVisibleArea();
376 virtual void save(bool only_changed){assert(0);};
381 void timerUpdate(float dtime);
383 // Takes cache into account
384 // sector mutex should be locked when calling
385 void deleteSectors(core::list<v2s16> &list, bool only_blocks);
387 // Returns count of deleted sectors
388 u32 deleteUnusedSectors(float timeout, bool only_blocks=false,
389 core::list<v3s16> *deleted_blocks=NULL);
391 // For debug printing
392 virtual void PrintInfo(std::ostream &out);
395 // Master heightmap parameters
401 randmax = "constant 70.0";
402 randfactor = "constant 0.6";
403 base = "linear 0 80 0";
407 std::string randfactor;
417 ravines_amount = 1.0;
418 //max_objects_in_block = 30;
421 float ravines_amount;
422 //u16 max_objects_in_block;
425 class ServerMap : public Map
429 savedir: directory to which map data should be saved
431 ServerMap(std::string savedir, HMParams hmp, MapParams mp);
436 return MAPTYPE_SERVER;
440 Forcefully get a sector from somewhere
442 MapSector * emergeSector(v2s16 p);
444 Forcefully get a block from somewhere.
447 - InvalidPositionException: possible if only_from_disk==true
450 - All already existing blocks that were modified are added.
451 - If found on disk, nothing will be added.
452 - If generated, the new block will not be included.
454 lighting_invalidated_blocks:
455 - All blocks that have heavy-to-calculate lighting changes
457 - updateLighting() should be called for these.
459 - A block that is in changed_blocks may not be in
460 lighting_invalidated_blocks.
462 MapBlock * emergeBlock(
465 core::map<v3s16, MapBlock*> &changed_blocks,
466 core::map<v3s16, MapBlock*> &lighting_invalidated_blocks
469 void createDir(std::string path);
470 void createSaveDir();
471 // returns something like "xxxxxxxx"
472 std::string getSectorSubDir(v2s16 pos);
473 // returns something like "map/sectors/xxxxxxxx"
474 std::string getSectorDir(v2s16 pos);
475 std::string createSectorDir(v2s16 pos);
476 // dirname: final directory name
477 v2s16 getSectorPos(std::string dirname);
478 v3s16 getBlockPos(std::string sectordir, std::string blockfile);
480 void save(bool only_changed);
483 void saveMasterHeightmap();
484 void loadMasterHeightmap();
486 // The sector mutex should be locked when calling most of these
488 // This only saves sector-specific data such as the heightmap
490 void saveSectorMeta(ServerMapSector *sector);
491 MapSector* loadSectorMeta(std::string dirname);
493 // Full load of a sector including all blocks.
494 // returns true on success, false on failure.
495 bool loadSectorFull(v2s16 p2d);
496 // If sector is not found in memory, try to load it from disk.
497 // Returns true if sector now resides in memory
498 //bool deFlushSector(v2s16 p2d);
500 void saveBlock(MapBlock *block);
501 // This will generate a sector with getSector if not found.
502 void loadBlock(std::string sectordir, std::string blockfile, MapSector *sector);
504 // Gets from master heightmap
505 void getSectorCorners(v2s16 p2d, s16 *corners);
507 // For debug printing
508 virtual void PrintInfo(std::ostream &out);
511 UnlimitedHeightmap *m_heightmap;
514 std::string m_savedir;
515 bool m_map_saving_enabled;
520 class ClientMap : public Map, public scene::ISceneNode
525 video::SMaterial *materials,
526 scene::ISceneNode* parent,
527 scene::ISceneManager* mgr,
535 return MAPTYPE_CLIENT;
539 Forcefully get a sector from somewhere
541 MapSector * emergeSector(v2s16 p);
543 void deSerializeSector(v2s16 p2d, std::istream &is);
549 virtual void OnRegisterSceneNode()
553 //SceneManager->registerNodeForRendering(this, scene::ESNRP_SKY_BOX);
554 SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID);
555 SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT);
558 ISceneNode::OnRegisterSceneNode();
561 virtual void render()
563 video::IVideoDriver* driver = SceneManager->getVideoDriver();
564 driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
565 renderMap(driver, m_materials, SceneManager->getSceneNodeRenderPass());
568 virtual const core::aabbox3d<f32>& getBoundingBox() const
573 void renderMap(video::IVideoDriver* driver,
574 video::SMaterial *materials, s32 pass);
576 // Update master heightmap mesh
579 // For debug printing
580 virtual void PrintInfo(std::ostream &out);
585 video::SMaterial *m_materials;
587 core::aabbox3d<f32> m_box;
589 // This is the master heightmap mesh
594 class MapVoxelManipulator : public VoxelManipulator
597 MapVoxelManipulator(Map *map);
598 virtual ~MapVoxelManipulator();
602 VoxelManipulator::clear();
603 m_loaded_blocks.clear();
606 virtual void emerge(VoxelArea a, s32 caller_id=-1);
608 void blitBack(core::map<v3s16, MapBlock*> & modified_blocks);
613 NOTE: This might be used or not
615 SUGG: How 'bout an another VoxelManipulator for storing the
616 information about which block is loaded?
618 core::map<v3s16, bool> m_loaded_blocks;