A third try on terrain generation. No trees yet.

This commit is contained in:
Perttu Ahola 2011-02-28 02:01:40 +02:00
parent 5707c309c9
commit c8be58a65c
13 changed files with 998 additions and 250 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 763 B

@ -510,6 +510,10 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
assert(player != NULL);
player->setPosition(playerpos_f);
}
// Get map seed
m_map_seed = readU64(&data[2+1+6]);
dstream<<"Client: received map seed: "<<m_map_seed<<std::endl;
// Reply to server
u32 replysize = 2;

@ -252,6 +252,8 @@ public:
(std::wstring)L"<"+name+L"> "+message);
}
u64 getMapSeed(){ return m_map_seed; }
private:
// Virtual methods from con::PeerHandler
@ -311,6 +313,9 @@ private:
//u32 m_daynight_ratio;
Queue<std::wstring> m_chat_queue;
// The seed returned by the server in TOCLIENT_INIT is stored here
u64 m_map_seed;
};
#endif // !SERVER

@ -34,6 +34,7 @@ enum ToClientCommand
[0] u16 TOSERVER_INIT
[2] u8 deployed version
[3] v3s16 player's position + v3f(0,BS/2,0) floatToInt'd
[4] u64 map seed (new as of 2011-02-27)
*/
TOCLIENT_BLOCKDATA = 0x20, //TODO: Multiple blocks

@ -253,11 +253,9 @@ Doing now (most important at the top):
* not done
=== Next
* Continue making the scripting system:
* Make updateNodeMesh for a less verbose mesh update on add/removenode
* Switch to using a safe way for the self and env pointers
* Make some global environment hooks, like node placed and general
on_step()
* Make a system for pregenerating quick information for mapblocks, so
that the client can show them as cubes before they are actually sent
or even generated.
=== Fixmes
* Check the fixmes in the list above
@ -274,6 +272,11 @@ Doing now (most important at the top):
with the ones in utility.h
=== Features
* Continue making the scripting system:
* Make updateNodeMesh for a less verbose mesh update on add/removenode
* Switch to using a safe way for the self and env pointers
* Make some global environment hooks, like node placed and general
on_step()
* Map should make the appropriate MapEditEvents
* Add a global Lua spawn handler and such
* Get rid of MapBlockObjects
@ -490,6 +493,9 @@ Inventory local_inventory;
u16 g_selected_item = 0;
bool g_show_map_plot = false;
bool g_refresh_map_plot = true;
/*
Debug streams
*/
@ -606,6 +612,15 @@ public:
if(event.KeyInput.PressedDown)
{
//dstream<<"Pressed key: "<<(char)event.KeyInput.Key<<std::endl;
if(g_show_map_plot)
{
if(event.KeyInput.Key == irr::KEY_ESCAPE
|| event.KeyInput.Key == irr::KEY_KEY_M)
{
g_show_map_plot = false;
}
return true;
}
/*
Launch menus
@ -679,6 +694,16 @@ public:
<<std::endl;
debug_stacks_print();
}
// Map plot
if(event.KeyInput.Key == irr::KEY_KEY_M)
{
dstream<<"Map plot requested"<<std::endl;
g_show_map_plot = !g_show_map_plot;
if(g_show_map_plot)
g_refresh_map_plot = true;
}
}
}
@ -1110,8 +1135,8 @@ void updateViewingRange(f32 frametime_in, Client *client)
f32 wanted_frametime_change = wanted_frametime - frametime;
//dstream<<"wanted_frametime_change="<<wanted_frametime_change<<std::endl;
// If needed frametime change is very small, just return
if(fabs(wanted_frametime_change) < wanted_frametime*0.2)
// If needed frametime change is small, just return
if(fabs(wanted_frametime_change) < wanted_frametime*0.4)
{
//dstream<<"ignoring small wanted_frametime_change"<<std::endl;
return;
@ -1239,6 +1264,93 @@ void draw_hotbar(video::IVideoDriver *driver, gui::IGUIFont *font,
}
}
video::ITexture *g_map_plot_texture = NULL;
float g_map_plot_texture_scale = 2;
void updateMapPlotTexture(v2f centerpos, video::IVideoDriver* driver,
Client *client)
{
assert(driver);
assert(client);
core::dimension2d<u32> dim(640,480);
video::IImage *img = driver->createImage(video::ECF_A8R8G8B8, dim);
assert(img);
for(u32 y=0; y<dim.Height; y++)
for(u32 x=0; x<dim.Width; x++)
{
v2f pf = v2f(x, dim.Height-y) - v2f(dim.Width, dim.Height)/2;
pf *= g_map_plot_texture_scale;
pf += centerpos;
double h = base_rock_level_2d(client->getMapSeed(), pf);
video::SColor c;
//double d1 = 50;
/*s32 ux = x - centerpos.X / g_map_plot_texture_scale;
s32 uy = y - centerpos.Y / g_map_plot_texture_scale;*/
// Screen coordinates that are based on multiples of
// 1000/g_map_plot_texture_scale and never negative
u32 ux = x + (u32)(1000/g_map_plot_texture_scale) * 10;
u32 uy = y + (u32)(1000/g_map_plot_texture_scale) * 10;
// Offset to center of image
ux -= dim.Width/2;
uy -= dim.Height/2;
if(uy % (u32)(1000/g_map_plot_texture_scale) == 0
|| ux % (u32)(1000/g_map_plot_texture_scale) == 0)
c.set(255, 255, 255, 255);
else if(uy % (u32)(100/g_map_plot_texture_scale) == 0
|| ux % (u32)(100/g_map_plot_texture_scale) == 0)
c.set(255, 160, 160, 160);
else if(h < WATER_LEVEL - 0.5) // Water
c.set(255, 50, 50, 255);
else if(h < WATER_LEVEL + 2) // Sand
c.set(255, 237, 201, 175);
#if 1
else if(h < WATER_LEVEL + 10) // Green
c.set(255, 50, 150, 50);
else if(h < WATER_LEVEL + 20) // Greenish yellow
c.set(255, 110, 185, 50);
else if(h < WATER_LEVEL + 50) // Yellow
c.set(255, 220, 220, 50);
else if(h < WATER_LEVEL + 100) // White
c.set(255, 180, 180, 180);
else
c.set(255, 255, 255, 255);
#endif
/*else if(h < WATER_LEVEL + d1)
{
h -= WATER_LEVEL;
u32 a = (u32)(h / d1 * 255);
if(a > 255)
a = 255;
c.set(255, 0, a, 0);
}*/
#if 0
else
{
h -= WATER_LEVEL;
h /= 20.0;
h = 1.0 - exp(-h);
video::SColor c1(255,200,200,50);
video::SColor c2(255,0,150,0);
c = c1.getInterpolated(c2, h);
/*u32 a = (u32)(h*255);
if(a > 255)
a = 255;
a = 255-a;
c.set(255, a, a, a);*/
}
#endif
img->setPixel(x, y, c);
}
g_map_plot_texture = driver->addTexture("map_plot", img);
img->drop();
assert(g_map_plot_texture);
}
// Chat data
struct ChatLine
{
@ -2252,6 +2364,10 @@ int main(int argc, char *argv[])
*/
g_input->step(dtime);
/*
Misc. stuff
*/
/*
Player speed control
*/
@ -3020,16 +3136,6 @@ int main(int argc, char *argv[])
driver->draw3DBox(*i, video::SColor(255,0,0,0));
}
/*
Draw crosshair
*/
driver->draw2DLine(displaycenter - core::vector2d<s32>(10,0),
displaycenter + core::vector2d<s32>(10,0),
video::SColor(255,255,255,255));
driver->draw2DLine(displaycenter - core::vector2d<s32>(0,10),
displaycenter + core::vector2d<s32>(0,10),
video::SColor(255,255,255,255));
/*
Frametime log
*/
@ -3048,6 +3154,31 @@ int main(int argc, char *argv[])
}
}
/*
Draw map plot
*/
if(g_show_map_plot && g_map_plot_texture)
{
core::dimension2d<u32> drawdim(640,480);
core::rect<s32> dest(v2s32(0,0), drawdim);
dest += v2s32(
(screensize.X-drawdim.Width)/2,
(screensize.Y-drawdim.Height)/2
);
core::rect<s32> source(v2s32(0,0), g_map_plot_texture->getSize());
driver->draw2DImage(g_map_plot_texture, dest, source);
}
/*
Draw crosshair
*/
driver->draw2DLine(displaycenter - core::vector2d<s32>(10,0),
displaycenter + core::vector2d<s32>(10,0),
video::SColor(255,255,255,255));
driver->draw2DLine(displaycenter - core::vector2d<s32>(0,10),
displaycenter + core::vector2d<s32>(0,10),
video::SColor(255,255,255,255));
} // timer
//timer10.stop();
@ -3077,8 +3208,23 @@ int main(int argc, char *argv[])
drawtime = drawtimer.stop(true);
/*
Drawing ends
End of drawing
*/
/*
Refresh map plot if player has moved considerably
*/
if(g_refresh_map_plot)
{
static v3f old_player_pos = v3f(1,1,1) * 10000000;
v3f p = client.getPlayerPosition() / BS;
if(old_player_pos.getDistanceFrom(p) > 4 * g_map_plot_texture_scale)
{
updateMapPlotTexture(v2f(p.X,p.Z), driver, &client);
old_player_pos = p;
}
g_refresh_map_plot = false;
}
static s16 lastFPS = 0;
//u16 fps = driver->getFPS();

File diff suppressed because it is too large Load Diff

@ -40,6 +40,15 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "voxel.h"
#include "mapchunk.h"
/*
Some exposed functions
*/
double base_rock_level_2d(u64 seed, v2f p);
/*
*/
#define MAPTYPE_BASE 0
#define MAPTYPE_SERVER 1
#define MAPTYPE_CLIENT 2
@ -531,6 +540,8 @@ public:
bool isSavingEnabled(){ return m_map_saving_enabled; }
u64 getSeed(){ return m_seed; }
private:
// Seed used for all kinds of randomness
u64 m_seed;

@ -34,6 +34,7 @@ MapBlock::MapBlock(NodeContainer *parent, v3s16 pos, bool dummy):
m_pos(pos),
changed(true),
is_underground(false),
m_lighting_expired(true),
m_day_night_differs(false),
m_objects(this)
{

@ -92,8 +92,7 @@ double noise3d(int x, int y, int z, int seed)
return 1.0 - (double)n/1073741824;
}
#if 0
// This is too slow
#if 1
double noise2d_gradient(double x, double y, int seed)
{
// Calculate the integer coordinates
@ -118,7 +117,7 @@ double noise2d_gradient(double x, double y, int seed)
}
#endif
#if 1
#if 0
double noise2d_gradient(double x, double y, int seed)
{
// Calculate the integer coordinates
@ -175,6 +174,21 @@ double noise2d_perlin(double x, double y, int seed,
return a;
}
double noise2d_perlin_abs(double x, double y, int seed,
int octaves, double persistence)
{
double a = 0;
double f = 1.0;
double g = 1.0;
for(int i=0; i<octaves; i++)
{
a += g * fabs(noise2d_gradient(x*f, y*f, seed+i));
f *= 2.0;
g *= persistence;
}
return a;
}
double noise3d_perlin(double x, double y, double z, int seed,
int octaves, double persistence)
{
@ -190,3 +204,18 @@ double noise3d_perlin(double x, double y, double z, int seed,
return a;
}
double noise3d_perlin_abs(double x, double y, double z, int seed,
int octaves, double persistence)
{
double a = 0;
double f = 1.0;
double g = 1.0;
for(int i=0; i<octaves; i++)
{
a += g * fabs(noise3d_gradient(x*f, y*f, z*f, seed+i));
f *= 2.0;
g *= persistence;
}
return a;
}

@ -32,8 +32,14 @@ double noise3d_gradient(double x, double y, double z, int seed);
double noise2d_perlin(double x, double y, int seed,
int octaves, double persistence);
double noise2d_perlin_abs(double x, double y, int seed,
int octaves, double persistence);
double noise3d_perlin(double x, double y, double z, int seed,
int octaves, double persistence);
double noise3d_perlin_abs(double x, double y, double z, int seed,
int octaves, double persistence);
#endif

@ -167,6 +167,26 @@ void * EmergeThread::Thread()
only_from_disk,
changed_blocks,
lighting_invalidated_blocks);
/*
While we're at it, generate some other blocks too
*/
try
{
map.emergeBlock(
p+v3s16(0,1,0),
only_from_disk,
changed_blocks,
lighting_invalidated_blocks);
map.emergeBlock(
p+v3s16(0,-1,0),
only_from_disk,
changed_blocks,
lighting_invalidated_blocks);
}
catch(InvalidPositionException &e)
{
}
}
// If it is a dummy, block was not found on disk
@ -208,23 +228,25 @@ void * EmergeThread::Thread()
Collect a list of blocks that have been modified in
addition to the fetched one.
*/
// Add all the "changed blocks" to modified_blocks
if(lighting_invalidated_blocks.size() > 0)
dstream<<"lighting "<<lighting_invalidated_blocks.size()
<<" blocks"<<std::endl;
// 50-100ms for single block generation
//TimeTaker timer("** EmergeThread updateLighting");
// Update lighting without locking the environment mutex,
// add modified blocks to changed blocks
map.updateLighting(lighting_invalidated_blocks, modified_blocks);
// Add all from changed_blocks to modified_blocks
for(core::map<v3s16, MapBlock*>::Iterator i = changed_blocks.getIterator();
i.atEnd() == false; i++)
{
MapBlock *block = i.getNode()->getValue();
modified_blocks.insert(block->getPos(), block);
}
/*dstream<<"lighting "<<lighting_invalidated_blocks.size()
<<" blocks"<<std::endl;*/
//TimeTaker timer("** updateLighting");
// Update lighting without locking the environment mutex,
// add modified blocks to changed blocks
map.updateLighting(lighting_invalidated_blocks, modified_blocks);
}
// If we got no block, there should be no invalidated blocks
else
@ -551,7 +573,8 @@ void RemoteClient::GetNextBlocks(Server *server, float dtime,
{
//TODO: Get value from somewhere
// Allow only one block in emerge queue
if(server->m_emerge_queue.peerItemCount(peer_id) < 1)
//if(server->m_emerge_queue.peerItemCount(peer_id) < 1)
if(server->m_emerge_queue.peerItemCount(peer_id) < 2)
{
//dstream<<"Adding block to emerge queue"<<std::endl;
@ -1681,10 +1704,11 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
// Now answer with a TOCLIENT_INIT
SharedBuffer<u8> reply(2+1+6);
SharedBuffer<u8> reply(2+1+6+8);
writeU16(&reply[0], TOCLIENT_INIT);
writeU8(&reply[2], deployed);
writeV3S16(&reply[3], floatToInt(player->getPosition()+v3f(0,BS/2,0), BS));
writeV3S16(&reply[2+1], floatToInt(player->getPosition()+v3f(0,BS/2,0), BS));
writeU64(&reply[2+1+6], m_env.getServerMap().getSeed());
// Send as reliable
m_con.Send(peer_id, 0, reply, true);

@ -486,7 +486,7 @@ void TextureSource::buildMainAtlas()
sourcelist.push_back("sand.png^mineral_iron.png");
// Padding to disallow texture bleeding
s32 padding = 8;
s32 padding = 16;
/*
First pass: generate almost everything

@ -39,6 +39,18 @@ extern const v3s16 g_26dirs[26];
// 26th is (0,0,0)
extern const v3s16 g_27dirs[27];
inline void writeU64(u8 *data, u64 i)
{
data[0] = ((i>>56)&0xff);
data[1] = ((i>>48)&0xff);
data[2] = ((i>>40)&0xff);
data[3] = ((i>>32)&0xff);
data[4] = ((i>>24)&0xff);
data[5] = ((i>>16)&0xff);
data[6] = ((i>> 8)&0xff);
data[7] = ((i>> 0)&0xff);
}
inline void writeU32(u8 *data, u32 i)
{
data[0] = ((i>>24)&0xff);
@ -58,6 +70,14 @@ inline void writeU8(u8 *data, u8 i)
data[0] = ((i>> 0)&0xff);
}
inline u64 readU64(u8 *data)
{
return ((u64)data[0]<<56) | ((u64)data[1]<<48)
| ((u64)data[2]<<40) | ((u64)data[3]<<32)
| ((u64)data[4]<<24) | ((u64)data[5]<<16)
| ((u64)data[6]<<8) | ((u64)data[7]<<0);
}
inline u32 readU32(u8 *data)
{
return (data[0]<<24) | (data[1]<<16) | (data[2]<<8) | (data[3]<<0);