1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

mapgen stuff

This commit is contained in:
Perttu Ahola 2011-02-05 14:55:16 +02:00
parent 7f2aa30bf2
commit ea6740e900
17 changed files with 239 additions and 3396 deletions

View file

@ -176,168 +176,22 @@ void MapSector::getBlocks(core::list<MapBlock*> &dest)
ServerMapSector
*/
ServerMapSector::ServerMapSector(NodeContainer *parent, v2s16 pos, u16 hm_split):
MapSector(parent, pos),
m_hm_split(hm_split),
m_objects(NULL)
ServerMapSector::ServerMapSector(NodeContainer *parent, v2s16 pos):
MapSector(parent, pos)
{
// hm_split has to be 1 or 2^x
assert(hm_split == 0 || hm_split == 1 || (hm_split & (hm_split-1)) == 0);
assert(hm_split * hm_split <= MAPSECTOR_FIXEDHEIGHTMAPS_MAXCOUNT);
for(u16 i=0; i<hm_split*hm_split; i++)
m_heightmaps[i] = NULL;
}
ServerMapSector::~ServerMapSector()
{
u16 hm_count = m_hm_split * m_hm_split;
// Write heightmaps
for(u16 i=0; i<hm_count; i++)
{
if(m_heightmaps[i])
delete m_heightmaps[i];
}
if(m_objects)
delete m_objects;
}
void ServerMapSector::setHeightmap(v2s16 hm_p, FixedHeightmap *hm)
{
assert(isInArea(hm_p, m_hm_split));
s16 i = hm_p.Y * m_hm_split + hm_p.X;
// Don't allow setting already set heightmaps as of now
assert(m_heightmaps[i] == NULL);
/*std::cout<<"MapSector::setHeightmap for sector "
<<"("<<m_pos.X<<","<<m_pos.Y<<"): "
<<"Setting heightmap "
<<"("<<hm_p.X<<","<<hm_p.Y<<")"
<<" which is i="<<i
<<" to pointer "<<(long long)hm
<<std::endl;*/
m_heightmaps[i] = hm;
differs_from_disk = true;
}
FixedHeightmap * ServerMapSector::getHeightmap(v2s16 hm_p)
{
assert(isInArea(hm_p, m_hm_split));
s16 i = hm_p.Y * m_hm_split + hm_p.X;
return m_heightmaps[i];
}
f32 ServerMapSector::getGroundHeight(v2s16 p, bool generate)
{
// If no heightmaps
if(m_hm_split == 0)
{
/*std::cout<<"Sector has no heightmap"
<<" while trying to get height at ("<<p.X<<","<<p.Y<<")"
<<" for sector ("<<m_pos.X<<","<<m_pos.Y<<")"
<<std::endl;*/
return GROUNDHEIGHT_NOTFOUND_SETVALUE;
}
// Side length of heightmap
s16 hm_d = MAP_BLOCKSIZE / m_hm_split;
// Position of selected heightmap
v2s16 hm_p = getContainerPos(p, hm_d);
if(isInArea(hm_p, m_hm_split) == false)
{
/*std::cout<<"Sector has no heightmap ("<<hm_p.X<<","<<hm_p.Y<<")"
<<" while trying to get height at ("<<p.X<<","<<p.Y<<")"
<<" for sector ("<<m_pos.X<<","<<m_pos.Y<<")"
<<std::endl;*/
return GROUNDHEIGHT_NOTFOUND_SETVALUE;
}
// Selected heightmap
FixedHeightmap *hm = m_heightmaps[hm_p.Y * m_hm_split + hm_p.X];
if(hm == NULL)
{
/*std::cout<<"Sector heightmap ("<<hm_p.X<<","<<hm_p.Y<<")"
" is NULL"
<<" while trying to get height at ("<<p.X<<","<<p.Y<<")"
<<" for sector ("<<m_pos.X<<","<<m_pos.Y<<")"
<<std::endl;*/
return GROUNDHEIGHT_NOTFOUND_SETVALUE;
}
// Position in selected heighmap
v2s16 p_in_hm = p - hm_p * hm_d;
if(isInArea(p_in_hm, hm_d+1) == false)
{
/*std::cout<<"Position ("<<p_in_hm.X<<","<<p_in_hm.Y<<")"
" not in sector heightmap area"
<<" while trying to get height at ("<<p.X<<","<<p.Y<<")"
<<" for sector ("<<m_pos.X<<","<<m_pos.Y<<")"
<<std::endl;*/
return GROUNDHEIGHT_NOTFOUND_SETVALUE;
}
f32 h = hm->getGroundHeight(p_in_hm);
/*if(h < GROUNDHEIGHT_VALID_MINVALUE)
{
std::cout<<"Sector heightmap ("<<hm_p.X<<","<<hm_p.Y<<")"
" returned invalid value"
<<" while trying to get height at ("<<p.X<<","<<p.Y<<")"
<<" which is ("<<p_in_hm.X<<","<<p_in_hm.Y<<") in heightmap"
<<" for sector ("<<m_pos.X<<","<<m_pos.Y<<")"
<<std::endl;
}*/
return h;
return GROUNDHEIGHT_NOTFOUND_SETVALUE;
}
void ServerMapSector::setGroundHeight(v2s16 p, f32 y, bool generate)
{
/*
NOTE:
This causes glitches because the sector cannot be actually
modified according to heightmap changes.
This is useful when generating continued sub-heightmaps
inside the sector.
*/
// If no heightmaps
if(m_hm_split == 0)
return;
// Side length of heightmap
s16 hm_d = MAP_BLOCKSIZE / m_hm_split;
// Position of selected heightmap
v2s16 hm_p = getContainerPos(p, hm_d);
if(isInArea(hm_p, m_hm_split) == false)
return;
// Selected heightmap
FixedHeightmap *hm = m_heightmaps[hm_p.Y * m_hm_split + hm_p.X];
if(hm == NULL)
return;
// Position in selected heighmap
v2s16 p_in_hm = p - hm_p * hm_d;
if(isInArea(p_in_hm, hm_d) == false)
return;
hm->setGroundHeight(p_in_hm, y);
differs_from_disk = true;
}
void ServerMapSector::serialize(std::ostream &os, u8 version)
@ -351,118 +205,21 @@ void ServerMapSector::serialize(std::ostream &os, u8 version)
*/
// Server has both of these, no need to support not having them.
assert(m_objects != NULL);
//assert(m_objects != NULL);
// Write version
os.write((char*)&version, 1);
/*
Serialize heightmap(s)
Add stuff here, if needed
*/
// Version with single heightmap
if(version <= 7)
{
u32 heightmap_size =
FixedHeightmap::serializedLength(version, MAP_BLOCKSIZE);
SharedBuffer<u8> data(heightmap_size);
m_heightmaps[0]->serialize(*data, version);
os.write((const char*)*data, heightmap_size);
if(version >= 5)
{
/*
Write objects
*/
u16 object_count;
if(m_objects->size() > 65535)
object_count = 65535;
else
object_count = m_objects->size();
u8 b[2];
writeU16(b, object_count);
os.write((char*)b, 2);
core::map<v3s16, u8>::Iterator i;
i = m_objects->getIterator();
for(; i.atEnd() == false; i++)
{
v3s16 p = i.getNode()->getKey();
u8 d = i.getNode()->getValue();
u8 b[7];
writeV3S16(&b[0], p);
b[6] = d;
os.write((char*)b, 7);
}
}
}
// Version with multiple heightmaps
else
{
u8 buf[2];
if(m_hm_split > 255)
throw SerializationError("Sector has too many heightmaps");
// Write heightmap split ratio
writeU8(buf, m_hm_split);
os.write((char*)buf, 1);
// If there are heightmaps, write them
if(m_hm_split != 0)
{
u16 hm_d = MAP_BLOCKSIZE / m_hm_split;
u32 hm_size = FixedHeightmap::serializedLength(version, hm_d);
SharedBuffer<u8> data(hm_size);
u16 hm_count = m_hm_split * m_hm_split;
// Write heightmaps
for(u16 i=0; i<hm_count; i++)
{
m_heightmaps[i]->serialize(*data, version);
os.write((const char*)*data, hm_size);
}
}
/*
Write objects
*/
u16 object_count;
if(m_objects->size() > 65535)
object_count = 65535;
else
object_count = m_objects->size();
u8 b[2];
writeU16(b, object_count);
os.write((char*)b, 2);
core::map<v3s16, u8>::Iterator i;
i = m_objects->getIterator();
for(; i.atEnd() == false; i++)
{
v3s16 p = i.getNode()->getKey();
u8 d = i.getNode()->getValue();
u8 b[7];
writeV3S16(&b[0], p);
b[6] = d;
os.write((char*)b, 7);
}
}
}
ServerMapSector* ServerMapSector::deSerialize(
std::istream &is,
NodeContainer *parent,
v2s16 p2d,
Heightmap *master_hm,
core::map<v2s16, MapSector*> & sectors
)
{
@ -483,82 +240,8 @@ ServerMapSector* ServerMapSector::deSerialize(
throw VersionMismatchException("ERROR: MapSector format not supported");
/*
Read heightmap(s)
Add necessary reading stuff here
*/
FixedHeightmap *hms[MAPSECTOR_FIXEDHEIGHTMAPS_MAXCOUNT];
u16 hm_split = 0;
// Version with a single heightmap
if(version <= 7)
{
hm_split = 1;
u32 hm_size =
FixedHeightmap::serializedLength(version, MAP_BLOCKSIZE);
SharedBuffer<u8> data(hm_size);
is.read((char*)*data, hm_size);
hms[0] = new FixedHeightmap(master_hm, p2d, MAP_BLOCKSIZE);
hms[0]->deSerialize(*data, version);
}
// Version with multiple heightmaps
else
{
u8 buf[2];
// Read split ratio
is.read((char*)buf, 1);
hm_split = readU8(buf);
// If there are heightmaps, read them
if(hm_split != 0)
{
u16 hm_count = hm_split * hm_split;
if(hm_count > MAPSECTOR_FIXEDHEIGHTMAPS_MAXCOUNT)
throw SerializationError("Sector has too many heightmaps");
u16 hm_d = MAP_BLOCKSIZE / hm_split;
u32 hm_size = FixedHeightmap::serializedLength(version, hm_d);
u16 i=0;
for(s16 y=0; y<hm_split; y++)
for(s16 x=0; x<hm_split; x++)
{
SharedBuffer<u8> data(hm_size);
is.read((char*)*data, hm_size);
hms[i] = new FixedHeightmap(master_hm, p2d+v2s16(x,y), hm_d);
hms[i]->deSerialize(*data, version);
i++;
}
}
}
/*
Read objects
*/
core::map<v3s16, u8> *objects = new core::map<v3s16, u8>;
if(version >= 5)
{
u8 b[2];
is.read((char*)b, 2);
u16 object_count = readU16(b);
for(u16 i=0; i<object_count; i++)
{
u8 b[7];
is.read((char*)b, 7);
v3s16 p = readV3S16(&b[0]);
u8 d = b[6];
objects->insert(p, d);
}
}
/*
Get or create sector
@ -574,18 +257,13 @@ ServerMapSector* ServerMapSector::deSerialize(
"at the moment, because code hasn't been tested."
<<std::endl;
//assert(0);
MapSector *sector = n->getValue();
assert(sector->getId() == MAPSECTOR_SERVER);
return (ServerMapSector*)sector;
// NOTE: At least hm_split mismatch would have to be checked
//sector = n->getValue();
}
else
{
sector = new ServerMapSector(parent, p2d, hm_split);
sector = new ServerMapSector(parent, p2d);
sectors.insert(p2d, sector);
}
@ -593,26 +271,7 @@ ServerMapSector* ServerMapSector::deSerialize(
Set stuff in sector
*/
// Set heightmaps
sector->m_hm_split = hm_split;
u16 hm_count = hm_split * hm_split;
for(u16 i=0; i<hm_count; i++)
{
// Set (or change) heightmap
FixedHeightmap *oldhm = sector->m_heightmaps[i];
sector->m_heightmaps[i] = hms[i];
if(oldhm != NULL)
delete oldhm;
}
// Set (or change) objects
core::map<v3s16, u8> *oldfo = sector->m_objects;
sector->m_objects = objects;
if(oldfo)
delete oldfo;
// Nothing here
return sector;
}
@ -654,29 +313,21 @@ void ClientMapSector::deSerialize(std::istream &is)
if(!ser_ver_supported(version))
throw VersionMismatchException("ERROR: MapSector format not supported");
if(version <= 7)
throw VersionMismatchException("ERROR: MapSector format not supported");
u8 buf[2];
// Read corners
// Dummy read corners
is.read((char*)buf, 2);
s16 c0 = readU16(buf);
is.read((char*)buf, 2);
s16 c1 = readU16(buf);
is.read((char*)buf, 2);
s16 c2 = readU16(buf);
is.read((char*)buf, 2);
s16 c3 = readU16(buf);
/*
Set stuff in sector
*/
m_corners[0] = c0;
m_corners[1] = c1;
m_corners[2] = c2;
m_corners[3] = c3;
// Nothing here
}
#endif // !SERVER