/*
The square step gets the hieght of each corner of the mesh, calculates the average of those heights
and uses that average to add an offset to the random number generated. The random value is used to lift the
centre point of each side of the square

int x : the left corner of the square to process
int z: the 'top' - far corner of the square to process
int size: The size of the square to process
double roughness: scale the amount of random change
*/
void DiamondSquare::SquareStep(int x, int z, int size, double roughness)
{
	int midpoint = size / 2;
	GLfloat averageHeight = 0.0;

	// Get the average height of the endpoints of the left side of the square
	averageHeight = mesh->GetHeightAt((GLfloat)x, (GLfloat)z);
	averageHeight += mesh->GetHeightAt((GLfloat)x + size, (GLfloat)z);
	averageHeight /= 2;
	// Set a new random height to the midpoint of the square side based on the average and roughness parameter
	mesh->SetHeightAt((GLfloat)x, (GLfloat)midpoint, (GLfloat)Displace(roughness) + averageHeight);

	// The same for the top (far) side of the square
	averageHeight = mesh->GetHeightAt((GLfloat)x + size, (GLfloat)z);
	averageHeight += mesh->GetHeightAt((GLfloat)x + size, (GLfloat)z + size);
	averageHeight /= 2;
	mesh->SetHeightAt((GLfloat)x + size, (GLfloat)z + midpoint, (GLfloat)Displace(roughness) + averageHeight);

	// The same for the right side of the square
	averageHeight = mesh->GetHeightAt((GLfloat)x + size, (GLfloat)z + size);
	averageHeight += mesh->GetHeightAt((GLfloat)x, (GLfloat)z + size);
	averageHeight /= 2;
	mesh->SetHeightAt((GLfloat)x + midpoint, (GLfloat)z + size, (GLfloat)Displace(roughness) + averageHeight);

	// The same for the bottom (near) side of the square
	averageHeight = mesh->GetHeightAt((GLfloat)x, (GLfloat)z + size);
	averageHeight += mesh->GetHeightAt((GLfloat)x, (GLfloat)z);
	averageHeight /= 2;
	mesh->SetHeightAt((GLfloat)x, (GLfloat)z + midpoint, (GLfloat)Displace(roughness) + averageHeight);
}
/*
Initialise the algorithm by creating the mesh at baseheight and raising the four corners of the mesh by a random amount 
*/
void DiamondSquare::Init()
{
	// Generate the terrain mesh at the set size and at the base height
	for(int i = 0; i < terrainSize; i++)
	{
		for(int j = 0; j < terrainSize; j++)
			mesh->AddQuad((GLfloat)j, params->baseHeight, (GLfloat)i, (GLfloat)j + 1, params->baseHeight, (GLfloat)i, (GLfloat)j + 1, params->baseHeight, (GLfloat)i + 1, (GLfloat)j, params->baseHeight, (GLfloat)i + 1);
	}

	// Lift each corner by a random amount
	// As this is the first step we don't modify the random number generated, hence 1.0 being passed to the Displace method
	mesh->SetHeightAt((GLfloat)0, (GLfloat)0, (GLfloat)fabs(Displace(1.0)));
	mesh->SetHeightAt((GLfloat)terrainSize, (GLfloat)0, (GLfloat)fabs(Displace(1.0)));
	mesh->SetHeightAt((GLfloat)terrainSize, (GLfloat)terrainSize, (GLfloat)fabs(Displace(1.0)));
	mesh->SetHeightAt((GLfloat)0, (GLfloat)terrainSize, (GLfloat)fabs(Displace(1.0)));
}
Beispiel #3
0
bool CUserMgr::_HandlePacket_UserDisplace(Packet* pack)
{
	if( !pack )
		return false;

	Message::UserDisplace msg;
	PROTOBUF_CMD_PARSER( pack, msg );

	CUser* user = GetUserByUID(msg.uid());
	if( user )
		Displace( user );

	return true;
}
/*
The diamond step takes the midpoint of each side of the square and uses the average height of the points to calculate
a new random height for the centre of the square

int x : the left corner of the square to process
int z: the 'top' - far corner of the square to process
int size: The size of the square to process
double roughness: scale the amount of random change
*/
void DiamondSquare::DiamondStep(int x, int z, int size, double roughness)
{
	int midpoint = size / 2;
	GLfloat averageHeight = 0.0;

	// Get the average hieght of the midpoints of each side of the square
	averageHeight = mesh->GetHeightAt((GLfloat)x, (GLfloat)z);
	averageHeight += mesh->GetHeightAt((GLfloat)x + size, (GLfloat)z);
	averageHeight += mesh->GetHeightAt((GLfloat)x + size, (GLfloat)z + size);
	averageHeight += mesh->GetHeightAt((GLfloat)x, (GLfloat)z + size);
	averageHeight /= 4;

	// Lift the centre point of the square by a random amount based on the average height and the roughness parameter
	mesh->SetHeightAt((GLfloat)midpoint, (GLfloat)midpoint, (GLfloat)Displace(roughness) + averageHeight);
}
Beispiel #5
0
bool CUserMgr::_HandlePacket_UserLogin(Packet* pack)
{
	Message::ClientLogin msg;
	PROTOBUF_CMD_PARSER(pack, msg);

	CLinker* server = GateServer.getServerByType(CBaseServer::Linker_Server_Game);
	if (!server) {
		LOGGER_ERROR("User login failed. Cannot find gameserver: user %lld", msg.uid());
		GETCLIENTNET(&GateServer)->shutdown(pack->GetNetID());
		return false;
	}

	CUser* user = UserMgr.Create(pack->GetNetID());
	if (!user) {
		LOGGER_ERROR("User login failed. Cannot create user: %lld", msg.uid());
		GETCLIENTNET(&GateServer)->shutdown(pack->GetNetID());
		return false;
	}

	CUser* oldUser = GetUserByUID(msg.uid());
	if (oldUser) {
		Displace(oldUser);
		LOGGER_WARN("user %lld relogin, displace the older.", msg.uid());
	}

	TMV t = time(NULL);
	user->m_id = msg.uid();
	user->m_worldID = server->m_worldID;
	user->m_svrID = server->m_nID;
	user->m_ClientSock = pack->GetNetID();
	user->m_GameSock = server->m_Socket;
	user->m_HeartTime = t;
	user->m_CanCreate = false;
	user->m_PackCount = 0;
	user->m_PackTime = t;
	user->m_AccessToken = msg.accesstoken();

	ThreadLib::CreateByPool(httpCheckUserThread, user);

	return true;
}