aboutsummaryrefslogtreecommitdiff
path: root/src/client/clientenvironment.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/clientenvironment.cpp')
-rw-r--r--src/client/clientenvironment.cpp198
1 files changed, 105 insertions, 93 deletions
diff --git a/src/client/clientenvironment.cpp b/src/client/clientenvironment.cpp
index 2f95177f3..3f82bd316 100644
--- a/src/client/clientenvironment.cpp
+++ b/src/client/clientenvironment.cpp
@@ -45,7 +45,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
class CAOShaderConstantSetter : public IShaderConstantSetter
{
public:
- CAOShaderConstantSetter() : m_emissive_color_setting("emissiveColor") {}
+ CAOShaderConstantSetter():
+ m_emissive_color_setting("emissiveColor")
+ {}
~CAOShaderConstantSetter() override = default;
@@ -59,15 +61,15 @@ public:
video::SColorf emissive_color(m_emissive_color);
float as_array[4] = {
- emissive_color.r,
- emissive_color.g,
- emissive_color.b,
- emissive_color.a,
+ emissive_color.r,
+ emissive_color.g,
+ emissive_color.b,
+ emissive_color.a,
};
m_emissive_color_setting.set(as_array, services);
}
- void onSetMaterial(const video::SMaterial &material) override
+ void onSetMaterial(const video::SMaterial& material) override
{
m_emissive_color = material.EmissiveColor;
}
@@ -80,19 +82,25 @@ private:
class CAOShaderConstantSetterFactory : public IShaderConstantSetterFactory
{
public:
- CAOShaderConstantSetterFactory() {}
+ CAOShaderConstantSetterFactory()
+ {}
- virtual IShaderConstantSetter *create() { return new CAOShaderConstantSetter(); }
+ virtual IShaderConstantSetter* create()
+ {
+ return new CAOShaderConstantSetter();
+ }
};
/*
ClientEnvironment
*/
-ClientEnvironment::ClientEnvironment(
- ClientMap *map, ITextureSource *texturesource, Client *client) :
- Environment(client),
- m_map(map), m_texturesource(texturesource), m_client(client)
+ClientEnvironment::ClientEnvironment(ClientMap *map,
+ ITextureSource *texturesource, Client *client):
+ Environment(client),
+ m_map(map),
+ m_texturesource(texturesource),
+ m_client(client)
{
auto *shdrsrc = m_client->getShaderSource();
shdrsrc->addShaderConstantSetterFactory(new CAOShaderConstantSetterFactory());
@@ -112,12 +120,12 @@ ClientEnvironment::~ClientEnvironment()
delete m_local_player;
}
-Map &ClientEnvironment::getMap()
+Map & ClientEnvironment::getMap()
{
return *m_map;
}
-ClientMap &ClientEnvironment::getClientMap()
+ClientMap & ClientEnvironment::getClientMap()
{
return *m_map;
}
@@ -127,7 +135,8 @@ void ClientEnvironment::setLocalPlayer(LocalPlayer *player)
/*
It is a failure if already is a local player
*/
- FATAL_ERROR_IF(m_local_player != NULL, "Local player already allocated");
+ FATAL_ERROR_IF(m_local_player != NULL,
+ "Local player already allocated");
m_local_player = player;
}
@@ -157,21 +166,21 @@ void ClientEnvironment::step(float dtime)
/*
Maximum position increment
*/
- // f32 position_max_increment = 0.05*BS;
- f32 position_max_increment = 0.1 * BS;
+ //f32 position_max_increment = 0.05*BS;
+ f32 position_max_increment = 0.1*BS;
// Maximum time increment (for collision detection etc)
// time = distance / speed
f32 dtime_max_increment = 1;
- if (player_speed > 0.001)
+ if(player_speed > 0.001)
dtime_max_increment = position_max_increment / player_speed;
// Maximum time increment is 10ms or lower
- if (dtime_max_increment > 0.01)
+ if(dtime_max_increment > 0.01)
dtime_max_increment = 0.01;
// Don't allow overly huge dtime
- if (dtime > 0.5)
+ if(dtime > 0.5)
dtime = 0.5;
f32 dtime_downcount = dtime;
@@ -181,19 +190,23 @@ void ClientEnvironment::step(float dtime)
*/
u32 loopcount = 0;
- do {
+ do
+ {
loopcount++;
f32 dtime_part;
- if (dtime_downcount > dtime_max_increment) {
+ if(dtime_downcount > dtime_max_increment)
+ {
dtime_part = dtime_max_increment;
dtime_downcount -= dtime_part;
- } else {
+ }
+ else
+ {
dtime_part = dtime_downcount;
/*
- Setting this to 0 (no -=dtime_part) disables an infinite
- loop when dtime_part is so small that dtime_downcount -=
- dtime_part does nothing
+ Setting this to 0 (no -=dtime_part) disables an infinite loop
+ when dtime_part is so small that dtime_downcount -= dtime_part
+ does nothing
*/
dtime_downcount = 0;
}
@@ -207,40 +220,33 @@ void ClientEnvironment::step(float dtime)
lplayer->applyControl(dtime_part, this);
// Apply physics
- if (!free_move && !is_climbing &&
- !g_settings->getBool("freecam")) {
+ if (!free_move && !is_climbing && ! g_settings->getBool("freecam")) {
// Gravity
v3f speed = lplayer->getSpeed();
if (!lplayer->in_liquid)
speed.Y -= lplayer->movement_gravity *
- lplayer->physics_override_gravity *
- dtime_part * 2.0f;
+ lplayer->physics_override_gravity * dtime_part * 2.0f;
// Liquid floating / sinking
if (lplayer->in_liquid && !lplayer->swimming_vertical &&
!lplayer->swimming_pitch)
- speed.Y -= lplayer->movement_liquid_sink *
- dtime_part * 2.0f;
+ speed.Y -= lplayer->movement_liquid_sink * dtime_part * 2.0f;
// Liquid resistance
if (lplayer->in_liquid_stable || lplayer->in_liquid) {
- // How much the node's viscosity blocks movement,
- // ranges between 0 and 1. Should match the scale
- // at which viscosity increase affects other
- // liquid attributes.
+ // How much the node's viscosity blocks movement, ranges
+ // between 0 and 1. Should match the scale at which viscosity
+ // increase affects other liquid attributes.
static const f32 viscosity_factor = 0.3f;
- v3f d_wanted = -speed /
- lplayer->movement_liquid_fluidity;
+ v3f d_wanted = -speed / lplayer->movement_liquid_fluidity;
f32 dl = d_wanted.getLength();
if (dl > lplayer->movement_liquid_fluidity_smooth)
dl = lplayer->movement_liquid_fluidity_smooth;
- dl *= (lplayer->liquid_viscosity *
- viscosity_factor) +
- (1 - viscosity_factor);
- v3f d = d_wanted.normalize() *
- (dl * dtime_part * 100.0f);
+ dl *= (lplayer->liquid_viscosity * viscosity_factor) +
+ (1 - viscosity_factor);
+ v3f d = d_wanted.normalize() * (dl * dtime_part * 100.0f);
speed += d;
}
@@ -252,15 +258,14 @@ void ClientEnvironment::step(float dtime)
This also does collision detection.
*/
lplayer->move(dtime_part, this, position_max_increment,
- &player_collisions);
+ &player_collisions);
}
} while (dtime_downcount > 0.001);
bool player_immortal = lplayer->getCAO() && lplayer->getCAO()->isImmortal();
for (const CollisionInfo &info : player_collisions) {
- v3f speed_diff = info.new_speed - info.old_speed;
- ;
+ v3f speed_diff = info.new_speed - info.old_speed;;
// Handle only fall damage
// (because otherwise walking against something in fast_move kills you)
if (speed_diff.Y < 0 || info.old_speed.Y >= 0)
@@ -268,12 +273,12 @@ void ClientEnvironment::step(float dtime)
// Get rid of other components
speed_diff.X = 0;
speed_diff.Z = 0;
- f32 pre_factor = 1; // 1 hp per node/s
- f32 tolerance = BS * 14; // 5 without damage
- f32 post_factor = 1; // 1 hp per node/s
+ f32 pre_factor = 1; // 1 hp per node/s
+ f32 tolerance = BS*14; // 5 without damage
+ f32 post_factor = 1; // 1 hp per node/s
if (info.type == COLLISION_NODE) {
- const ContentFeatures &f = m_client->ndef()->get(
- m_map->getNode(info.node_p));
+ const ContentFeatures &f = m_client->ndef()->
+ get(m_map->getNode(info.node_p));
// Determine fall damage multiplier
int addp = itemgroup_get(f.groups, "fall_damage_add_percent");
pre_factor = 1.0f + (float)addp / 100.0f;
@@ -284,8 +289,8 @@ void ClientEnvironment::step(float dtime)
u16 damage = (u16)MYMIN(damage_f + 0.5, U16_MAX);
if (damage != 0) {
damageLocalPlayer(damage, true);
- m_client->getEventManager()->put(new SimpleTriggerEvent(
- MtEvent::PLAYER_FALLING_DAMAGE));
+ m_client->getEventManager()->put(
+ new SimpleTriggerEvent(MtEvent::PLAYER_FALLING_DAMAGE));
}
}
}
@@ -314,8 +319,7 @@ void ClientEnvironment::step(float dtime)
*/
bool update_lighting = m_active_object_light_update_interval.step(dtime, 0.21);
- auto cb_state = [this, dtime, update_lighting, day_night_ratio](
- ClientActiveObject *cao) {
+ auto cb_state = [this, dtime, update_lighting, day_night_ratio] (ClientActiveObject *cao) {
// Step object
cao->step(dtime, this);
@@ -333,10 +337,11 @@ void ClientEnvironment::step(float dtime)
ClientSimpleObject *simple = *i;
simple->step(dtime);
- if (simple->m_to_be_removed) {
+ if(simple->m_to_be_removed) {
delete simple;
i = m_simple_objects.erase(i);
- } else {
+ }
+ else {
++i;
}
}
@@ -347,18 +352,20 @@ void ClientEnvironment::addSimpleObject(ClientSimpleObject *simple)
m_simple_objects.push_back(simple);
}
-GenericCAO *ClientEnvironment::getGenericCAO(u16 id)
+GenericCAO* ClientEnvironment::getGenericCAO(u16 id)
{
ClientActiveObject *obj = getActiveObject(id);
if (obj && obj->getType() == ACTIVEOBJECT_TYPE_GENERIC)
- return (GenericCAO *)obj;
+ return (GenericCAO*) obj;
return NULL;
}
-bool isFreeClientActiveObjectId(const u16 id, ClientActiveObjectMap &objects)
+bool isFreeClientActiveObjectId(const u16 id,
+ ClientActiveObjectMap &objects)
{
return id != 0 && objects.find(id) == objects.end();
+
}
u16 getFreeClientActiveObjectId(ClientActiveObjectMap &objects)
@@ -366,8 +373,8 @@ u16 getFreeClientActiveObjectId(ClientActiveObjectMap &objects)
// try to reuse id's as late as possible
static u16 last_used_id = 0;
u16 startid = last_used_id;
- for (;;) {
- last_used_id++;
+ for(;;) {
+ last_used_id ++;
if (isFreeClientActiveObjectId(last_used_id, objects))
return last_used_id;
@@ -389,27 +396,33 @@ u16 ClientEnvironment::addActiveObject(ClientActiveObject *object)
return object->getId();
}
-void ClientEnvironment::addActiveObject(u16 id, u8 type, const std::string &init_data)
+void ClientEnvironment::addActiveObject(u16 id, u8 type,
+ const std::string &init_data)
{
- ClientActiveObject *obj = ClientActiveObject::create(
- (ActiveObjectType)type, m_client, this);
- if (obj == NULL) {
- infostream << "ClientEnvironment::addActiveObject(): "
- << "id=" << id << " type=" << type
- << ": Couldn't create object" << std::endl;
+ ClientActiveObject* obj =
+ ClientActiveObject::create((ActiveObjectType) type, m_client, this);
+ if(obj == NULL)
+ {
+ infostream<<"ClientEnvironment::addActiveObject(): "
+ <<"id="<<id<<" type="<<type<<": Couldn't create object"
+ <<std::endl;
return;
}
obj->setId(id);
- try {
+ try
+ {
obj->initialize(init_data);
- } catch (SerializationError &e) {
- errorstream << "ClientEnvironment::addActiveObject():"
- << " id=" << id << " type=" << type
- << ": SerializationError in initialize(): " << e.what()
- << ": init_data=" << serializeJsonString(init_data)
- << std::endl;
+ }
+ catch(SerializationError &e)
+ {
+ errorstream<<"ClientEnvironment::addActiveObject():"
+ <<" id="<<id<<" type="<<type
+ <<": SerializationError in initialize(): "
+ <<e.what()
+ <<": init_data="<<serializeJsonString(init_data)
+ <<std::endl;
}
u16 new_id = addActiveObject(obj);
@@ -425,6 +438,7 @@ void ClientEnvironment::addActiveObject(u16 id, u8 type, const std::string &init
}
}
+
void ClientEnvironment::removeActiveObject(u16 id)
{
// Get current attachment childs to detach them visually
@@ -446,18 +460,18 @@ void ClientEnvironment::processActiveObjectMessage(u16 id, const std::string &da
ClientActiveObject *obj = getActiveObject(id);
if (obj == NULL) {
infostream << "ClientEnvironment::processActiveObjectMessage():"
- << " got message for id=" << id << ", which doesn't exist."
- << std::endl;
+ << " got message for id=" << id << ", which doesn't exist."
+ << std::endl;
return;
}
try {
obj->processMessage(data);
} catch (SerializationError &e) {
- errorstream << "ClientEnvironment::processActiveObjectMessage():"
- << " id=" << id << " type=" << obj->getType()
- << " SerializationError in processMessage(): " << e.what()
- << std::endl;
+ errorstream<<"ClientEnvironment::processActiveObjectMessage():"
+ << " id=" << id << " type=" << obj->getType()
+ << " SerializationError in processMessage(): " << e.what()
+ << std::endl;
}
}
@@ -499,12 +513,12 @@ ClientEnvEvent ClientEnvironment::getClientEnvEvent()
}
void ClientEnvironment::getSelectedActiveObjects(
- const core::line3d<f32> &shootline_on_map,
- std::vector<PointedThing> &objects)
+ const core::line3d<f32> &shootline_on_map,
+ std::vector<PointedThing> &objects)
{
std::vector<DistanceSortedActiveObject> allObjects;
- getActiveObjects(shootline_on_map.start, shootline_on_map.getLength() + 10.0f,
- allObjects);
+ getActiveObjects(shootline_on_map.start,
+ shootline_on_map.getLength() + 10.0f, allObjects);
const v3f line_vector = shootline_on_map.getVector();
for (const auto &allObject : allObjects) {
@@ -514,17 +528,15 @@ void ClientEnvironment::getSelectedActiveObjects(
continue;
const v3f &pos = obj->getPosition();
- aabb3f offsetted_box(
- selection_box.MinEdge + pos, selection_box.MaxEdge + pos);
+ aabb3f offsetted_box(selection_box.MinEdge + pos,
+ selection_box.MaxEdge + pos);
v3f current_intersection;
v3s16 current_normal;
if (boxLineCollision(offsetted_box, shootline_on_map.start, line_vector,
- &current_intersection, &current_normal)) {
- objects.emplace_back((s16)obj->getId(), current_intersection,
- current_normal,
- (current_intersection - shootline_on_map.start)
- .getLengthSQ());
+ &current_intersection, &current_normal)) {
+ objects.emplace_back((s16) obj->getId(), current_intersection, current_normal,
+ (current_intersection - shootline_on_map.start).getLengthSQ());
}
}
}