-#ifndef SERVER
-
-/*
- ClientMap
-*/
-
-ClientMap::ClientMap(
- Client *client,
- MapDrawControl &control,
- scene::ISceneNode* parent,
- scene::ISceneManager* mgr,
- s32 id
-):
- Map(dout_client),
- scene::ISceneNode(parent, mgr, id),
- m_client(client),
- m_control(control),
- m_camera_position(0,0,0),
- m_camera_direction(0,0,1)
-{
- m_camera_mutex.Init();
- assert(m_camera_mutex.IsInitialized());
-
- m_box = core::aabbox3d<f32>(-BS*1000000,-BS*1000000,-BS*1000000,
- BS*1000000,BS*1000000,BS*1000000);
-}
-
-ClientMap::~ClientMap()
-{
- /*JMutexAutoLock lock(mesh_mutex);
-
- if(mesh != NULL)
- {
- mesh->drop();
- mesh = NULL;
- }*/
-}
-
-MapSector * ClientMap::emergeSector(v2s16 p2d)
-{
- DSTACK(__FUNCTION_NAME);
- // Check that it doesn't exist already
- try{
- return getSectorNoGenerate(p2d);
- }
- catch(InvalidPositionException &e)
- {
- }
-
- // Create a sector
- ClientMapSector *sector = new ClientMapSector(this, p2d);
-
- {
- //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
- m_sectors.insert(p2d, sector);
- }
-
- return sector;
-}
-
-#if 0
-void ClientMap::deSerializeSector(v2s16 p2d, std::istream &is)
-{
- DSTACK(__FUNCTION_NAME);
- ClientMapSector *sector = NULL;
-
- //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
-
- core::map<v2s16, MapSector*>::Node *n = m_sectors.find(p2d);
-
- if(n != NULL)
- {
- sector = (ClientMapSector*)n->getValue();
- assert(sector->getId() == MAPSECTOR_CLIENT);
- }
- else
- {
- sector = new ClientMapSector(this, p2d);
- {
- //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
- m_sectors.insert(p2d, sector);
- }
- }
-
- sector->deSerialize(is);
-}
-#endif
-
-void ClientMap::OnRegisterSceneNode()
-{
- if(IsVisible)
- {
- SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID);
- SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT);
- }
-
- ISceneNode::OnRegisterSceneNode();
-}
-
-void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
-{
- //m_dout<<DTIME<<"Rendering map..."<<std::endl;
- DSTACK(__FUNCTION_NAME);
-
- bool is_transparent_pass = pass == scene::ESNRP_TRANSPARENT;
-
- /*
- This is called two times per frame, reset on the non-transparent one
- */
- if(pass == scene::ESNRP_SOLID)
- {
- m_last_drawn_sectors.clear();
- }
-
- /*
- Get time for measuring timeout.
-
- Measuring time is very useful for long delays when the
- machine is swapping a lot.
- */
- int time1 = time(0);
-
- //u32 daynight_ratio = m_client->getDayNightRatio();
-
- m_camera_mutex.Lock();
- v3f camera_position = m_camera_position;
- v3f camera_direction = m_camera_direction;
- m_camera_mutex.Unlock();
-
- /*
- Get all blocks and draw all visible ones
- */
-
- v3s16 cam_pos_nodes(
- camera_position.X / BS,
- camera_position.Y / BS,
- camera_position.Z / BS);
-
- v3s16 box_nodes_d = m_control.wanted_range * v3s16(1,1,1);
-
- v3s16 p_nodes_min = cam_pos_nodes - box_nodes_d;
- v3s16 p_nodes_max = cam_pos_nodes + box_nodes_d;
-
- // Take a fair amount as we will be dropping more out later
- v3s16 p_blocks_min(
- p_nodes_min.X / MAP_BLOCKSIZE - 2,
- p_nodes_min.Y / MAP_BLOCKSIZE - 2,
- p_nodes_min.Z / MAP_BLOCKSIZE - 2);
- v3s16 p_blocks_max(
- p_nodes_max.X / MAP_BLOCKSIZE + 1,
- p_nodes_max.Y / MAP_BLOCKSIZE + 1,
- p_nodes_max.Z / MAP_BLOCKSIZE + 1);
-
- u32 vertex_count = 0;
-
- // For limiting number of mesh updates per frame
- u32 mesh_update_count = 0;
-
- u32 blocks_would_have_drawn = 0;
- u32 blocks_drawn = 0;
-
- int timecheck_counter = 0;
- core::map<v2s16, MapSector*>::Iterator si;
- si = m_sectors.getIterator();
- for(; si.atEnd() == false; si++)
- {
- {
- timecheck_counter++;
- if(timecheck_counter > 50)
- {
- timecheck_counter = 0;
- int time2 = time(0);
- if(time2 > time1 + 4)
- {
- dstream<<"ClientMap::renderMap(): "
- "Rendering takes ages, returning."
- <<std::endl;
- return;
- }
- }
- }
-
- MapSector *sector = si.getNode()->getValue();
- v2s16 sp = sector->getPos();
-
- if(m_control.range_all == false)
- {
- if(sp.X < p_blocks_min.X
- || sp.X > p_blocks_max.X
- || sp.Y < p_blocks_min.Z
- || sp.Y > p_blocks_max.Z)
- continue;
- }
-
- core::list< MapBlock * > sectorblocks;
- sector->getBlocks(sectorblocks);
-
- /*
- Draw blocks
- */
-
- u32 sector_blocks_drawn = 0;
-
- core::list< MapBlock * >::Iterator i;
- for(i=sectorblocks.begin(); i!=sectorblocks.end(); i++)
- {
- MapBlock *block = *i;
-
- /*
- Compare block position to camera position, skip
- if not seen on display
- */
-
- float range = 100000 * BS;
- if(m_control.range_all == false)
- range = m_control.wanted_range * BS;
-
- float d = 0.0;
- if(isBlockInSight(block->getPos(), camera_position,
- camera_direction, range, &d) == false)
- {
- continue;
- }
-
- // Okay, this block will be drawn. Reset usage timer.
- block->resetUsageTimer();
-
- // This is ugly (spherical distance limit?)
- /*if(m_control.range_all == false &&
- d - 0.5*BS*MAP_BLOCKSIZE > range)
- continue;*/
-
-#if 1
- /*
- Update expired mesh (used for day/night change)
-
- It doesn't work exactly like it should now with the
- tasked mesh update but whatever.
- */
-
- bool mesh_expired = false;
-
- {
- JMutexAutoLock lock(block->mesh_mutex);
-
- mesh_expired = block->getMeshExpired();
-
- // Mesh has not been expired and there is no mesh:
- // block has no content
- if(block->mesh == NULL && mesh_expired == false)
- continue;
- }
-
- f32 faraway = BS*50;
- //f32 faraway = m_control.wanted_range * BS;
-
- /*
- This has to be done with the mesh_mutex unlocked
- */
- // Pretty random but this should work somewhat nicely
- if(mesh_expired && (
- (mesh_update_count < 3
- && (d < faraway || mesh_update_count < 2)
- )
- ||
- (m_control.range_all && mesh_update_count < 20)
- )
- )
- /*if(mesh_expired && mesh_update_count < 6
- && (d < faraway || mesh_update_count < 3))*/
- {
- mesh_update_count++;
-
- // Mesh has been expired: generate new mesh
- //block->updateMesh(daynight_ratio);
- m_client->addUpdateMeshTask(block->getPos());
-
- mesh_expired = false;
- }
-
-#endif
- /*
- Draw the faces of the block
- */
- {
- JMutexAutoLock lock(block->mesh_mutex);
-
- scene::SMesh *mesh = block->mesh;
-
- if(mesh == NULL)
- continue;
-
- blocks_would_have_drawn++;
- if(blocks_drawn >= m_control.wanted_max_blocks
- && m_control.range_all == false
- && d > m_control.wanted_min_range * BS)
- continue;
-
- blocks_drawn++;
- sector_blocks_drawn++;
-
- u32 c = mesh->getMeshBufferCount();
-
- for(u32 i=0; i<c; i++)
- {
- scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
- const video::SMaterial& material = buf->getMaterial();
- video::IMaterialRenderer* rnd =
- driver->getMaterialRenderer(material.MaterialType);
- bool transparent = (rnd && rnd->isTransparent());
- // Render transparent on transparent pass and likewise.
- if(transparent == is_transparent_pass)
- {
- /*
- This *shouldn't* hurt too much because Irrlicht
- doesn't change opengl textures if the old
- material is set again.
- */
- driver->setMaterial(buf->getMaterial());
- driver->drawMeshBuffer(buf);
- vertex_count += buf->getVertexCount();
- }
- }
- }
- } // foreach sectorblocks
-
- if(sector_blocks_drawn != 0)
- {
- m_last_drawn_sectors[sp] = true;
- }
- }
-
- m_control.blocks_drawn = blocks_drawn;
- m_control.blocks_would_have_drawn = blocks_would_have_drawn;
-
- /*dstream<<"renderMap(): is_transparent_pass="<<is_transparent_pass
- <<", rendered "<<vertex_count<<" vertices."<<std::endl;*/
-}
-
-bool ClientMap::setTempMod(v3s16 p, NodeMod mod,
- core::map<v3s16, MapBlock*> *affected_blocks)
-{
- bool changed = false;
- /*
- Add it to all blocks touching it
- */
- v3s16 dirs[7] = {
- v3s16(0,0,0), // this
- v3s16(0,0,1), // back
- v3s16(0,1,0), // top
- v3s16(1,0,0), // right
- v3s16(0,0,-1), // front
- v3s16(0,-1,0), // bottom
- v3s16(-1,0,0), // left
- };
- for(u16 i=0; i<7; i++)
- {
- v3s16 p2 = p + dirs[i];
- // Block position of neighbor (or requested) node
- v3s16 blockpos = getNodeBlockPos(p2);
- MapBlock * blockref = getBlockNoCreateNoEx(blockpos);
- if(blockref == NULL)
- continue;
- // Relative position of requested node
- v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
- if(blockref->setTempMod(relpos, mod))
- {
- changed = true;
- }
- }
- if(changed && affected_blocks!=NULL)
- {
- for(u16 i=0; i<7; i++)
- {
- v3s16 p2 = p + dirs[i];
- // Block position of neighbor (or requested) node
- v3s16 blockpos = getNodeBlockPos(p2);
- MapBlock * blockref = getBlockNoCreateNoEx(blockpos);
- if(blockref == NULL)
- continue;
- affected_blocks->insert(blockpos, blockref);
- }
- }
- return changed;
-}
-
-bool ClientMap::clearTempMod(v3s16 p,
- core::map<v3s16, MapBlock*> *affected_blocks)
-{
- bool changed = false;
- v3s16 dirs[7] = {
- v3s16(0,0,0), // this
- v3s16(0,0,1), // back
- v3s16(0,1,0), // top
- v3s16(1,0,0), // right
- v3s16(0,0,-1), // front
- v3s16(0,-1,0), // bottom
- v3s16(-1,0,0), // left
- };
- for(u16 i=0; i<7; i++)
- {
- v3s16 p2 = p + dirs[i];
- // Block position of neighbor (or requested) node
- v3s16 blockpos = getNodeBlockPos(p2);
- MapBlock * blockref = getBlockNoCreateNoEx(blockpos);
- if(blockref == NULL)
- continue;
- // Relative position of requested node
- v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
- if(blockref->clearTempMod(relpos))
- {
- changed = true;
- }
- }
- if(changed && affected_blocks!=NULL)
- {
- for(u16 i=0; i<7; i++)
- {
- v3s16 p2 = p + dirs[i];
- // Block position of neighbor (or requested) node
- v3s16 blockpos = getNodeBlockPos(p2);
- MapBlock * blockref = getBlockNoCreateNoEx(blockpos);
- if(blockref == NULL)
- continue;
- affected_blocks->insert(blockpos, blockref);
- }
- }
- return changed;
-}
-
-void ClientMap::expireMeshes(bool only_daynight_diffed)
-{
- TimeTaker timer("expireMeshes()");
-
- core::map<v2s16, MapSector*>::Iterator si;
- si = m_sectors.getIterator();
- for(; si.atEnd() == false; si++)
- {
- MapSector *sector = si.getNode()->getValue();
-
- core::list< MapBlock * > sectorblocks;
- sector->getBlocks(sectorblocks);
-
- core::list< MapBlock * >::Iterator i;
- for(i=sectorblocks.begin(); i!=sectorblocks.end(); i++)
- {
- MapBlock *block = *i;
-
- if(only_daynight_diffed && dayNightDiffed(block->getPos()) == false)
- {
- continue;
- }
-
- {
- JMutexAutoLock lock(block->mesh_mutex);
- if(block->mesh != NULL)
- {
- /*block->mesh->drop();
- block->mesh = NULL;*/
- block->setMeshExpired(true);
- }
- }
- }
- }
-}
-
-void ClientMap::updateMeshes(v3s16 blockpos, u32 daynight_ratio)
-{
- assert(mapType() == MAPTYPE_CLIENT);
-
- try{
- v3s16 p = blockpos + v3s16(0,0,0);
- MapBlock *b = getBlockNoCreate(p);
- b->updateMesh(daynight_ratio);
- //b->setMeshExpired(true);
- }
- catch(InvalidPositionException &e){}
- // Leading edge
- try{
- v3s16 p = blockpos + v3s16(-1,0,0);
- MapBlock *b = getBlockNoCreate(p);
- b->updateMesh(daynight_ratio);
- //b->setMeshExpired(true);
- }
- catch(InvalidPositionException &e){}
- try{
- v3s16 p = blockpos + v3s16(0,-1,0);
- MapBlock *b = getBlockNoCreate(p);
- b->updateMesh(daynight_ratio);
- //b->setMeshExpired(true);
- }
- catch(InvalidPositionException &e){}
- try{
- v3s16 p = blockpos + v3s16(0,0,-1);
- MapBlock *b = getBlockNoCreate(p);
- b->updateMesh(daynight_ratio);
- //b->setMeshExpired(true);
- }
- catch(InvalidPositionException &e){}
-}
-
-#if 0
-/*
- Update mesh of block in which the node is, and if the node is at the
- leading edge, update the appropriate leading blocks too.
-*/
-void ClientMap::updateNodeMeshes(v3s16 nodepos, u32 daynight_ratio)
-{
- v3s16 dirs[4] = {
- v3s16(0,0,0),
- v3s16(-1,0,0),
- v3s16(0,-1,0),
- v3s16(0,0,-1),
- };
- v3s16 blockposes[4];
- for(u32 i=0; i<4; i++)
- {
- v3s16 np = nodepos + dirs[i];
- blockposes[i] = getNodeBlockPos(np);
- // Don't update mesh of block if it has been done already
- bool already_updated = false;
- for(u32 j=0; j<i; j++)
- {
- if(blockposes[j] == blockposes[i])
- {
- already_updated = true;
- break;
- }
- }
- if(already_updated)
- continue;
- // Update mesh
- MapBlock *b = getBlockNoCreate(blockposes[i]);
- b->updateMesh(daynight_ratio);
- }
-}
-#endif
-
-void ClientMap::PrintInfo(std::ostream &out)
-{
- out<<"ClientMap: ";
-}
-
-#endif // !SERVER
-