Skip to content

Commit

Permalink
General code refactoring/improvements in server, treegen and connection
Browse files Browse the repository at this point in the history
  • Loading branch information
sfan5 committed Mar 12, 2024
1 parent 50ba5f2 commit 413726d
Show file tree
Hide file tree
Showing 22 changed files with 274 additions and 380 deletions.
10 changes: 3 additions & 7 deletions src/client/clientmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,11 @@ class ClientMap : public Map, public scene::ISceneNode
s32 id
);

virtual ~ClientMap();

bool maySaveBlocks() override
{
return false;
}

void drop() override
{
ISceneNode::drop(); // calls destructor
}

void updateCamera(v3f pos, v3f dir, f32 fov, v3s16 offset, video::SColor light_color);

/*
Expand Down Expand Up @@ -122,6 +115,9 @@ class ClientMap : public Map, public scene::ISceneNode
void onSettingChanged(const std::string &name);

protected:
// use drop() instead
virtual ~ClientMap();

void reportMetrics(u64 save_time_us, u32 saved_blocks, u32 all_blocks) override;
private:
bool isMeshOccluded(MapBlock *mesh_block, u16 mesh_size, v3s16 cam_pos_nodes);
Expand Down
2 changes: 1 addition & 1 deletion src/emerge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ void *EmergeThread::run()
std::map<v3s16, MapBlock *> modified_blocks;

m_map = &m_server->m_env->getServerMap();
m_emerge = m_server->m_emerge;
m_emerge = m_server->getEmergeManager();
m_mapgen = m_emerge->m_mapgens[id];
enable_mapgen_debug_info = m_emerge->enable_mapgen_debug_info;

Expand Down
18 changes: 6 additions & 12 deletions src/httpfetch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#include "httpfetch.h"
#include "porting.h" // for sleep_ms(), get_sysinfo(), secure_rand_fill_buf()
#include <iostream>
#include <sstream>
#include <list>
#include <unordered_map>
#include <cerrno>
Expand Down Expand Up @@ -151,9 +149,9 @@ bool httpfetch_async_get(u64 caller, HTTPFetchResult &fetch_result)
static size_t httpfetch_writefunction(
char *ptr, size_t size, size_t nmemb, void *userdata)
{
std::ostringstream *stream = (std::ostringstream*)userdata;
auto *dest = reinterpret_cast<std::string*>(userdata);
size_t count = size * nmemb;
stream->write(ptr, count);
dest->append(ptr, count);
return count;
}

Expand Down Expand Up @@ -214,7 +212,6 @@ class HTTPFetchOngoing
CURLM *multi = nullptr;
HTTPFetchRequest request;
HTTPFetchResult result;
std::ostringstream oss;
struct curl_slist *http_header = nullptr;
curl_mime *multipart_mime = nullptr;
};
Expand All @@ -224,8 +221,7 @@ HTTPFetchOngoing::HTTPFetchOngoing(const HTTPFetchRequest &request_,
CurlHandlePool *pool_):
pool(pool_),
request(request_),
result(request_),
oss(std::ios::binary)
result(request_)
{
curl = pool->alloc();
if (!curl)
Expand Down Expand Up @@ -276,16 +272,15 @@ HTTPFetchOngoing::HTTPFetchOngoing(const HTTPFetchRequest &request_,
curl_easy_setopt(curl, CURLOPT_USERAGENT, request.useragent.c_str());

// Set up a write callback that writes to the
// ostringstream ongoing->oss, unless the data
// is to be discarded
// result struct, unless the data is to be discarded
if (request.caller == HTTPFETCH_DISCARD) {
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
httpfetch_discardfunction);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, nullptr);
} else {
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
httpfetch_writefunction);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &oss);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result.data);
}

// Set data from fields or raw_data
Expand Down Expand Up @@ -371,7 +366,6 @@ const HTTPFetchResult * HTTPFetchOngoing::complete(CURLcode res)
{
result.succeeded = (res == CURLE_OK);
result.timeout = (res == CURLE_OPERATION_TIMEDOUT);
result.data = oss.str();

// Get HTTP/FTP response code
result.response_code = 0;
Expand Down
8 changes: 0 additions & 8 deletions src/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,6 @@ class Map /*: public NodeContainer*/
virtual ~Map();
DISABLE_CLASS_COPY(Map);

/*
Drop (client) or delete (server) the map.
*/
virtual void drop()
{
delete this;
}

void addEventReceiver(MapEventReceiver *event_receiver);
void removeEventReceiver(MapEventReceiver *event_receiver);
// event shall be deleted by caller after the call.
Expand Down
101 changes: 56 additions & 45 deletions src/mapgen/treegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,37 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "irr_v3d.h"
#include <stack>
#include "treegen.h"
#include "irr_v3d.h"
#include "util/pointer.h"
#include "util/numeric.h"
#include "servermap.h"
#include "mapblock.h"
#include "noise.h"
#include "nodedef.h"
#include "treegen.h"
#include "voxelalgorithms.h"

namespace treegen
{

/*
L-System tree gen helper functions
NOTE: the PseudoRandom parameters here were probably accidentally used
as by-value instead of by-reference. But don't change this now to keep
the old behaviour.
*/
void tree_trunk_placement(MMVManip &vmanip, v3f p0, const TreeDef &def);
void tree_leaves_placement(MMVManip &vmanip, v3f p0,
PseudoRandom ps, const TreeDef &def);
void tree_single_leaves_placement(MMVManip &vmanip, v3f p0,
PseudoRandom ps, const TreeDef &def);
void tree_fruit_placement(MMVManip &vmanip, v3f p0, const TreeDef &def);

void setRotationAxisRadians(core::matrix4 &M, float angle, v3f axis);
v3f transposeMatrix(const core::matrix4 &M, v3f v);

void make_tree(MMVManip &vmanip, v3s16 p0, bool is_apple_tree,
const NodeDefManager *ndef, s32 seed)
{
Expand Down Expand Up @@ -118,20 +136,18 @@ void make_tree(MMVManip &vmanip, v3s16 p0, bool is_apple_tree,
}


// L-System tree LUA spawner
treegen::error spawn_ltree(ServerMap *map, v3s16 p0,
const NodeDefManager *ndef, const TreeDef &tree_definition)
const TreeDef &tree_definition)
{
std::map<v3s16, MapBlock*> modified_blocks;
MMVManip vmanip(map);
v3s16 tree_blockp = getNodeBlockPos(p0);
treegen::error e;

vmanip.initialEmerge(tree_blockp - v3s16(1, 1, 1), tree_blockp + v3s16(1, 3, 1));
e = make_ltree(vmanip, p0, ndef, tree_definition);
treegen::error e = make_ltree(vmanip, p0, tree_definition);
if (e != SUCCESS)
return e;

std::map<v3s16, MapBlock*> modified_blocks;
voxalgo::blit_back_with_light(map, &vmanip, &modified_blocks);

// Send a MEET_OTHER event
Expand All @@ -143,9 +159,8 @@ treegen::error spawn_ltree(ServerMap *map, v3s16 p0,
}


//L-System tree generator
treegen::error make_ltree(MMVManip &vmanip, v3s16 p0,
const NodeDefManager *ndef, TreeDef tree_definition)
const TreeDef &tree_definition)
{
s32 seed;
if (tree_definition.explicit_seed)
Expand All @@ -155,10 +170,10 @@ treegen::error make_ltree(MMVManip &vmanip, v3s16 p0,
PseudoRandom ps(seed);

// chance of inserting abcd rules
double prop_a = 9;
double prop_b = 8;
double prop_c = 7;
double prop_d = 6;
constexpr float prop_a = 9;
constexpr float prop_b = 8;
constexpr float prop_c = 7;
constexpr float prop_d = 6;

//randomize tree growth level, minimum=2
s16 iterations = tree_definition.iterations;
Expand All @@ -167,13 +182,13 @@ treegen::error make_ltree(MMVManip &vmanip, v3s16 p0,
if (iterations < 2)
iterations = 2;

s16 MAX_ANGLE_OFFSET = 5;
double angle_in_radians = (double)tree_definition.angle * M_PI / 180;
double angleOffset_in_radians = (s16)(ps.range(0, 1) % MAX_ANGLE_OFFSET) * M_PI / 180;
constexpr s16 MAX_ANGLE_OFFSET = 5;
float angle_in_radians = tree_definition.angle * M_PI / 180;
float angleOffset_in_radians = (s16)(ps.range(0, 1) % MAX_ANGLE_OFFSET) * M_PI / 180;

//initialize rotation matrix, position and stacks for branches
core::matrix4 rotation;
rotation = setRotationAxisRadians(rotation, M_PI / 2, v3f(0, 0, 1));
setRotationAxisRadians(rotation, M_PI / 2, v3f(0, 0, 1));
v3f position;
position.X = p0.X;
position.Y = p0.Y;
Expand Down Expand Up @@ -484,37 +499,37 @@ treegen::error make_ltree(MMVManip &vmanip, v3s16 p0,
break;
case '+':
temp_rotation.makeIdentity();
temp_rotation = setRotationAxisRadians(temp_rotation,
setRotationAxisRadians(temp_rotation,
angle_in_radians + angleOffset_in_radians, v3f(0, 0, 1));
rotation *= temp_rotation;
break;
case '-':
temp_rotation.makeIdentity();
temp_rotation = setRotationAxisRadians(temp_rotation,
setRotationAxisRadians(temp_rotation,
angle_in_radians + angleOffset_in_radians, v3f(0, 0, -1));
rotation *= temp_rotation;
break;
case '&':
temp_rotation.makeIdentity();
temp_rotation = setRotationAxisRadians(temp_rotation,
setRotationAxisRadians(temp_rotation,
angle_in_radians + angleOffset_in_radians, v3f(0, 1, 0));
rotation *= temp_rotation;
break;
case '^':
temp_rotation.makeIdentity();
temp_rotation = setRotationAxisRadians(temp_rotation,
setRotationAxisRadians(temp_rotation,
angle_in_radians + angleOffset_in_radians, v3f(0, -1, 0));
rotation *= temp_rotation;
break;
case '*':
temp_rotation.makeIdentity();
temp_rotation = setRotationAxisRadians(temp_rotation,
setRotationAxisRadians(temp_rotation,
angle_in_radians, v3f(1, 0, 0));
rotation *= temp_rotation;
break;
case '/':
temp_rotation.makeIdentity();
temp_rotation = setRotationAxisRadians(temp_rotation,
setRotationAxisRadians(temp_rotation,
angle_in_radians, v3f(-1, 0, 0));
rotation *= temp_rotation;
break;
Expand All @@ -527,7 +542,7 @@ treegen::error make_ltree(MMVManip &vmanip, v3s16 p0,
}


void tree_trunk_placement(MMVManip &vmanip, v3f p0, TreeDef &tree_definition)
void tree_trunk_placement(MMVManip &vmanip, v3f p0, const TreeDef &tree_definition)
{
v3s16 p1 = v3s16(myround(p0.X), myround(p0.Y), myround(p0.Z));
if (!vmanip.m_area.contains(p1))
Expand All @@ -544,7 +559,7 @@ void tree_trunk_placement(MMVManip &vmanip, v3f p0, TreeDef &tree_definition)


void tree_leaves_placement(MMVManip &vmanip, v3f p0,
PseudoRandom ps, TreeDef &tree_definition)
PseudoRandom ps, const TreeDef &tree_definition)
{
MapNode leavesnode = tree_definition.leavesnode;
if (ps.range(1, 100) > 100 - tree_definition.leaves2_chance)
Expand All @@ -568,7 +583,7 @@ void tree_leaves_placement(MMVManip &vmanip, v3f p0,


void tree_single_leaves_placement(MMVManip &vmanip, v3f p0,
PseudoRandom ps, TreeDef &tree_definition)
PseudoRandom ps, const TreeDef &tree_definition)
{
MapNode leavesnode = tree_definition.leavesnode;
if (ps.range(1, 100) > 100 - tree_definition.leaves2_chance)
Expand All @@ -584,7 +599,7 @@ void tree_single_leaves_placement(MMVManip &vmanip, v3f p0,
}


void tree_fruit_placement(MMVManip &vmanip, v3f p0, TreeDef &tree_definition)
void tree_fruit_placement(MMVManip &vmanip, v3f p0, const TreeDef &tree_definition)
{
v3s16 p1 = v3s16(myround(p0.X), myround(p0.Y), myround(p0.Z));
if (!vmanip.m_area.contains(p1))
Expand All @@ -597,18 +612,18 @@ void tree_fruit_placement(MMVManip &vmanip, v3f p0, TreeDef &tree_definition)
}


irr::core::matrix4 setRotationAxisRadians(irr::core::matrix4 M, double angle, v3f axis)
void setRotationAxisRadians(core::matrix4 &M, float angle, v3f axis)
{
double c = cos(angle);
double s = sin(angle);
double t = 1.0 - c;
float c = std::cos(angle);
float s = std::sin(angle);
float t = 1.0f - c;

double tx = t * axis.X;
double ty = t * axis.Y;
double tz = t * axis.Z;
double sx = s * axis.X;
double sy = s * axis.Y;
double sz = s * axis.Z;
float tx = t * axis.X;
float ty = t * axis.Y;
float tz = t * axis.Z;
float sx = s * axis.X;
float sy = s * axis.Y;
float sz = s * axis.Z;

M[0] = tx * axis.X + c;
M[1] = tx * axis.Y + sz;
Expand All @@ -621,19 +636,15 @@ irr::core::matrix4 setRotationAxisRadians(irr::core::matrix4 M, double angle, v3
M[8] = tz * axis.X + sy;
M[9] = tz * axis.Y - sx;
M[10] = tz * axis.Z + c;
return M;
}


v3f transposeMatrix(irr::core::matrix4 M, v3f v)
v3f transposeMatrix(const core::matrix4 &M, v3f v)
{
v3f translated;
double x = M[0] * v.X + M[4] * v.Y + M[8] * v.Z +M[12];
double y = M[1] * v.X + M[5] * v.Y + M[9] * v.Z +M[13];
double z = M[2] * v.X + M[6] * v.Y + M[10] * v.Z +M[14];
translated.X = x;
translated.Y = y;
translated.Z = z;
translated.X = M[0] * v.X + M[4] * v.Y + M[8] * v.Z + M[12];
translated.Y = M[1] * v.X + M[5] * v.Y + M[9] * v.Z + M[13];
translated.Z = M[2] * v.X + M[6] * v.Y + M[10] * v.Z + M[14];
return translated;
}

Expand Down

0 comments on commit 413726d

Please sign in to comment.