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.
29 #define sleep_s(x) Sleep((x*1000))
32 #define sleep_s(x) sleep(x)
35 #include "common_irrlicht.h"
38 #include "mapsector.h"
39 #include "constants.h"
42 #include "nodemetadata.h"
44 #define MAPTYPE_BASE 0
45 #define MAPTYPE_SERVER 1
46 #define MAPTYPE_CLIENT 2
48 enum MapEditEventType{
56 MapEditEventType type;
59 core::map<v3s16, bool> modified_blocks;
60 u16 already_known_by_peer;
64 already_known_by_peer(0)
68 MapEditEvent * clone()
70 MapEditEvent *event = new MapEditEvent();
74 for(core::map<v3s16, bool>::Iterator
75 i = modified_blocks.getIterator();
76 i.atEnd()==false; i++)
78 v3s16 p = i.getNode()->getKey();
79 bool v = i.getNode()->getValue();
80 event->modified_blocks.insert(p, v);
86 class MapEventReceiver
89 // event shall be deleted by caller after the call.
90 virtual void onMapEditEvent(MapEditEvent *event) = 0;
93 class Map : public NodeContainer
97 Map(std::ostream &dout);
100 virtual u16 nodeContainerId() const
102 return NODECONTAINER_ID_MAP;
105 virtual s32 mapType() const
111 Drop (client) or delete (server) the map.
118 void addEventReceiver(MapEventReceiver *event_receiver);
119 void removeEventReceiver(MapEventReceiver *event_receiver);
120 // event shall be deleted by caller after the call.
121 void dispatchEvent(MapEditEvent *event);
123 // On failure returns NULL
124 MapSector * getSectorNoGenerateNoExNoLock(v2s16 p2d);
125 // On failure returns NULL
126 MapSector * getSectorNoGenerateNoEx(v2s16 p2d);
127 // On failure throws InvalidPositionException
128 MapSector * getSectorNoGenerate(v2s16 p2d);
129 // Gets an existing sector or creates an empty one
130 //MapSector * getSectorCreate(v2s16 p2d);
133 This is overloaded by ClientMap and ServerMap to allow
134 their differing fetch methods.
136 virtual MapSector * emergeSector(v2s16 p){ return NULL; }
137 virtual MapSector * emergeSector(v2s16 p,
138 core::map<v3s16, MapBlock*> &changed_blocks){ return NULL; }
140 // Returns InvalidPositionException if not found
141 MapBlock * getBlockNoCreate(v3s16 p);
142 // Returns NULL if not found
143 MapBlock * getBlockNoCreateNoEx(v3s16 p);
144 // Gets an existing block or creates an empty one
145 //MapBlock * getBlockCreate(v3s16 p);
147 // Returns InvalidPositionException if not found
148 bool isNodeUnderground(v3s16 p);
150 // virtual from NodeContainer
151 bool isValidPosition(v3s16 p)
153 v3s16 blockpos = getNodeBlockPos(p);
156 blockref = getBlockNoCreate(blockpos);
158 catch(InvalidPositionException &e)
163 /*v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
164 bool is_valid = blockref->isValidPosition(relpos);
168 // virtual from NodeContainer
169 // throws InvalidPositionException if not found
170 MapNode getNode(v3s16 p)
172 v3s16 blockpos = getNodeBlockPos(p);
173 MapBlock * blockref = getBlockNoCreate(blockpos);
174 v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
176 return blockref->getNodeNoCheck(relpos);
179 // virtual from NodeContainer
180 // throws InvalidPositionException if not found
181 void setNode(v3s16 p, MapNode & n)
183 v3s16 blockpos = getNodeBlockPos(p);
184 MapBlock * blockref = getBlockNoCreate(blockpos);
185 v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
186 blockref->setNodeNoCheck(relpos, n);
189 // Returns a CONTENT_IGNORE node if not found
190 MapNode getNodeNoEx(v3s16 p)
193 v3s16 blockpos = getNodeBlockPos(p);
194 MapBlock * blockref = getBlockNoCreate(blockpos);
195 v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
197 return blockref->getNodeNoCheck(relpos);
199 catch(InvalidPositionException &e)
201 return MapNode(CONTENT_IGNORE);
205 void unspreadLight(enum LightBank bank,
206 core::map<v3s16, u8> & from_nodes,
207 core::map<v3s16, bool> & light_sources,
208 core::map<v3s16, MapBlock*> & modified_blocks);
210 void unLightNeighbors(enum LightBank bank,
211 v3s16 pos, u8 lightwas,
212 core::map<v3s16, bool> & light_sources,
213 core::map<v3s16, MapBlock*> & modified_blocks);
215 void spreadLight(enum LightBank bank,
216 core::map<v3s16, bool> & from_nodes,
217 core::map<v3s16, MapBlock*> & modified_blocks);
219 void lightNeighbors(enum LightBank bank,
221 core::map<v3s16, MapBlock*> & modified_blocks);
223 v3s16 getBrightestNeighbour(enum LightBank bank, v3s16 p);
225 s16 propagateSunlight(v3s16 start,
226 core::map<v3s16, MapBlock*> & modified_blocks);
228 void updateLighting(enum LightBank bank,
229 core::map<v3s16, MapBlock*> & a_blocks,
230 core::map<v3s16, MapBlock*> & modified_blocks);
232 void updateLighting(core::map<v3s16, MapBlock*> & a_blocks,
233 core::map<v3s16, MapBlock*> & modified_blocks);
236 These handle lighting but not faces.
238 void addNodeAndUpdate(v3s16 p, MapNode n,
239 core::map<v3s16, MapBlock*> &modified_blocks);
240 void removeNodeAndUpdate(v3s16 p,
241 core::map<v3s16, MapBlock*> &modified_blocks);
244 Wrappers for the latter ones.
246 Return true if succeeded, false if not.
248 bool addNodeWithEvent(v3s16 p, MapNode n);
249 bool removeNodeWithEvent(v3s16 p);
252 Takes the blocks at the edges into account
254 bool dayNightDiffed(v3s16 blockpos);
256 //core::aabbox3d<s16> getDisplayedBlockArea();
258 //bool updateChangedVisibleArea();
260 virtual void save(bool only_changed){assert(0);};
265 void timerUpdate(float dtime);
267 // Takes cache into account
268 // sector mutex should be locked when calling
269 void deleteSectors(core::list<v2s16> &list, bool only_blocks);
271 // Returns count of deleted sectors
272 u32 deleteUnusedSectors(float timeout, bool only_blocks=false,
273 core::list<v3s16> *deleted_blocks=NULL);
275 // For debug printing
276 virtual void PrintInfo(std::ostream &out);
278 void transformLiquids(core::map<v3s16, MapBlock*> & modified_blocks);
282 These are basically coordinate wrappers to MapBlock
285 NodeMetadata* getNodeMetadata(v3s16 p);
286 void setNodeMetadata(v3s16 p, NodeMetadata *meta);
287 void removeNodeMetadata(v3s16 p);
288 void nodeMetadataStep(float dtime,
289 core::map<v3s16, MapBlock*> &changed_blocks);
294 core::map<v2s16, MapSector*> *getSectorsPtr(){return &m_sectors;}
302 std::ostream &m_dout;
304 core::map<MapEventReceiver*, bool> m_event_receivers;
306 core::map<v2s16, MapSector*> m_sectors;
307 //JMutex m_sector_mutex;
309 // Be sure to set this to NULL when the cached sector is deleted
310 MapSector *m_sector_cache;
311 v2s16 m_sector_cache_p;
313 // Queued transforming water nodes
314 UniqueQueue<v3s16> m_transforming_liquid;
320 This is the only map class that is able to generate map.
323 struct ChunkMakeData;
325 class ServerMap : public Map
329 savedir: directory to which map data should be saved
331 ServerMap(std::string savedir);
336 return MAPTYPE_SERVER;
343 // Returns the position of the chunk where the sector is in
344 v2s16 sector_to_chunk(v2s16 sectorpos)
348 sectorpos.X += m_chunksize / 2;
349 sectorpos.Y += m_chunksize / 2;
350 v2s16 chunkpos = getContainerPos(sectorpos, m_chunksize);
354 // Returns the position of the (0,0) sector of the chunk
355 v2s16 chunk_to_sector(v2s16 chunkpos)
360 chunkpos.X * m_chunksize,
361 chunkpos.Y * m_chunksize
363 sectorpos.X -= m_chunksize / 2;
364 sectorpos.Y -= m_chunksize / 2;
371 MapChunk *getChunk(v2s16 chunkpos)
373 core::map<v2s16, MapChunk*>::Node *n;
374 n = m_chunks.find(chunkpos);
377 return n->getValue();
381 True if the chunk and its neighbors are fully generated.
382 It means the chunk will not be touched in the future by the
383 generator. If false, generateChunk will make it true.
385 bool chunkNonVolatile(v2s16 chunkpos)
390 /*for(s16 x=-1; x<=1; x++)
391 for(s16 y=-1; y<=1; y++)*/
395 v2s16 chunkpos0 = chunkpos + v2s16(x,y);
396 MapChunk *chunk = getChunk(chunkpos);
399 if(chunk->getGenLevel() != GENERATED_FULLY)
406 Chunks are generated by using these and makeChunk().
408 void initChunkMake(ChunkMakeData &data, v2s16 chunkpos);
409 MapChunk* finishChunkMake(ChunkMakeData &data,
410 core::map<v3s16, MapBlock*> &changed_blocks);
415 All chunks touching this one can be altered also.
417 /*MapChunk* generateChunkRaw(v2s16 chunkpos,
418 core::map<v3s16, MapBlock*> &changed_blocks,
422 Generate a chunk and its neighbors so that it won't be touched
425 /*MapChunk* generateChunk(v2s16 chunkpos,
426 core::map<v3s16, MapBlock*> &changed_blocks);*/
431 This is mainly called by generateChunkRaw.
433 //ServerMapSector * generateSector(v2s16 p);
436 Get a sector from somewhere.
438 - Check disk (loads blocks also)
441 ServerMapSector * createSector(v2s16 p);
444 Get a sector from somewhere.
446 - Check disk (loads blocks also)
449 /*MapSector * emergeSector(v2s16 p,
450 core::map<v3s16, MapBlock*> &changed_blocks);*/
452 /*MapSector * emergeSector(v2s16 p)
454 core::map<v3s16, MapBlock*> changed_blocks;
455 return emergeSector(p, changed_blocks);
458 MapBlock * generateBlock(
460 MapBlock *original_dummy,
461 ServerMapSector *sector,
462 core::map<v3s16, MapBlock*> &changed_blocks,
463 core::map<v3s16, MapBlock*> &lighting_invalidated_blocks
467 Get a block from somewhere.
471 MapBlock * createBlock(v3s16 p);
474 only_from_disk, changed_blocks and lighting_invalidated_blocks
475 are not properly used by the new map generator.
477 MapBlock * emergeBlock(
480 core::map<v3s16, MapBlock*> &changed_blocks,
481 core::map<v3s16, MapBlock*> &lighting_invalidated_blocks
486 Forcefully get a block from somewhere.
489 - InvalidPositionException: possible if only_from_disk==true
492 - All already existing blocks that were modified are added.
493 - If found on disk, nothing will be added.
494 - If generated, the new block will not be included.
496 lighting_invalidated_blocks:
497 - All blocks that have heavy-to-calculate lighting changes
499 - updateLighting() should be called for these.
501 - A block that is in changed_blocks may not be in
502 lighting_invalidated_blocks.
504 MapBlock * emergeBlock(
507 core::map<v3s16, MapBlock*> &changed_blocks,
508 core::map<v3s16, MapBlock*> &lighting_invalidated_blocks
512 // Helper for placing objects on ground level
513 s16 findGroundLevel(v2s16 p2d);
516 Misc. helper functions for fiddling with directory and file
519 void createDir(std::string path);
520 void createSaveDir();
521 // returns something like "xxxxxxxx"
522 std::string getSectorSubDir(v2s16 pos);
523 // returns something like "map/sectors/xxxxxxxx"
524 std::string getSectorDir(v2s16 pos);
525 std::string createSectorDir(v2s16 pos);
526 // dirname: final directory name
527 v2s16 getSectorPos(std::string dirname);
528 v3s16 getBlockPos(std::string sectordir, std::string blockfile);
530 void save(bool only_changed);
533 // Saves map seed and possibly other stuff
537 void saveChunkMeta();
538 void loadChunkMeta();
540 // The sector mutex should be locked when calling most of these
542 // This only saves sector-specific data such as the heightmap
544 // DEPRECATED? Sectors have no metadata anymore.
545 void saveSectorMeta(ServerMapSector *sector);
546 MapSector* loadSectorMeta(std::string dirname);
548 // Full load of a sector including all blocks.
549 // returns true on success, false on failure.
550 bool loadSectorFull(v2s16 p2d);
551 // If sector is not found in memory, try to load it from disk.
552 // Returns true if sector now resides in memory
553 //bool deFlushSector(v2s16 p2d);
555 void saveBlock(MapBlock *block);
556 // This will generate a sector with getSector if not found.
557 void loadBlock(std::string sectordir, std::string blockfile, MapSector *sector);
559 // For debug printing
560 virtual void PrintInfo(std::ostream &out);
562 bool isSavingEnabled(){ return m_map_saving_enabled; }
565 // Seed used for all kinds of randomness
568 std::string m_savedir;
569 bool m_map_saving_enabled;
571 // Chunk size in MapSectors
572 // If 0, chunks are disabled.
575 core::map<v2s16, MapChunk*> m_chunks;
584 struct MapDrawControl
589 wanted_max_blocks(0),
592 blocks_would_have_drawn(0)
595 // Overrides limits by drawing everything
597 // Wanted drawing range
599 // Maximum number of blocks to draw
600 u32 wanted_max_blocks;
601 // Blocks in this range are drawn regardless of number of blocks drawn
602 float wanted_min_range;
603 // Number of blocks rendered is written here by the renderer
605 // Number of blocks that would have been drawn in wanted_range
606 u32 blocks_would_have_drawn;
614 This is the only map class that is able to render itself on screen.
617 class ClientMap : public Map, public scene::ISceneNode
622 MapDrawControl &control,
623 scene::ISceneNode* parent,
624 scene::ISceneManager* mgr,
632 return MAPTYPE_CLIENT;
640 void updateCamera(v3f pos, v3f dir)
642 JMutexAutoLock lock(m_camera_mutex);
643 m_camera_position = pos;
644 m_camera_direction = dir;
648 Forcefully get a sector from somewhere
650 MapSector * emergeSector(v2s16 p);
652 void deSerializeSector(v2s16 p2d, std::istream &is);
658 virtual void OnRegisterSceneNode();
660 virtual void render()
662 video::IVideoDriver* driver = SceneManager->getVideoDriver();
663 driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
664 renderMap(driver, SceneManager->getSceneNodeRenderPass());
667 virtual const core::aabbox3d<f32>& getBoundingBox() const
672 void renderMap(video::IVideoDriver* driver, s32 pass);
675 Methods for setting temporary modifications to nodes for
678 Returns true if something changed.
680 All blocks whose mesh could have been changed are inserted
683 bool setTempMod(v3s16 p, NodeMod mod,
684 core::map<v3s16, MapBlock*> *affected_blocks=NULL);
685 bool clearTempMod(v3s16 p,
686 core::map<v3s16, MapBlock*> *affected_blocks=NULL);
687 // Efficient implementation needs a cache of TempMods
688 //void clearTempMods();
690 void expireMeshes(bool only_daynight_diffed);
693 Update the faces of the given block and blocks on the
696 void updateMeshes(v3s16 blockpos, u32 daynight_ratio);
698 // Update meshes that touch the node
699 //void updateNodeMeshes(v3s16 nodepos, u32 daynight_ratio);
701 // For debug printing
702 virtual void PrintInfo(std::ostream &out);
707 core::aabbox3d<f32> m_box;
709 // This is the master heightmap mesh
710 //scene::SMesh *mesh;
713 MapDrawControl &m_control;
715 v3f m_camera_position;
716 v3f m_camera_direction;
717 JMutex m_camera_mutex;
723 class MapVoxelManipulator : public VoxelManipulator
726 MapVoxelManipulator(Map *map);
727 virtual ~MapVoxelManipulator();
731 VoxelManipulator::clear();
732 m_loaded_blocks.clear();
735 virtual void emerge(VoxelArea a, s32 caller_id=-1);
737 void blitBack(core::map<v3s16, MapBlock*> & modified_blocks);
743 value = block existed when loaded
745 core::map<v3s16, bool> m_loaded_blocks;
748 class ManualMapVoxelManipulator : public MapVoxelManipulator
751 ManualMapVoxelManipulator(Map *map);
752 virtual ~ManualMapVoxelManipulator();
754 void setMap(Map *map)
757 virtual void emerge(VoxelArea a, s32 caller_id=-1);
759 void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max);
761 // This is much faster with big chunks of generated data
762 void blitBackAll(core::map<v3s16, MapBlock*> * modified_blocks);
771 ManualMapVoxelManipulator vmanip;
776 v2s16 sectorpos_base;
777 s16 sectorpos_base_size;
778 v2s16 sectorpos_bigbase;
779 s16 sectorpos_bigbase_size;
780 s16 max_spread_amount;
781 UniqueQueue<v3s16> transforming_liquid;
790 void makeChunk(ChunkMakeData *data);