2017-01-08 10:49:47 +01:00
|
|
|
/*
|
|
|
|
Minetest
|
|
|
|
Copyright (C) 2010-2017 celeron55, Perttu Ahola <celeron55@gmail.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "util/serialize.h"
|
|
|
|
#include "util/pointedthing.h"
|
2017-08-16 22:11:45 +02:00
|
|
|
#include "client.h"
|
2017-01-08 10:49:47 +01:00
|
|
|
#include "clientenvironment.h"
|
|
|
|
#include "clientsimpleobject.h"
|
|
|
|
#include "clientmap.h"
|
2017-04-25 19:38:08 +02:00
|
|
|
#include "scripting_client.h"
|
2017-01-08 10:49:47 +01:00
|
|
|
#include "mapblock_mesh.h"
|
|
|
|
#include "event.h"
|
|
|
|
#include "collision.h"
|
2017-08-18 19:25:07 +02:00
|
|
|
#include "nodedef.h"
|
2017-01-08 10:49:47 +01:00
|
|
|
#include "profiler.h"
|
|
|
|
#include "raycast.h"
|
|
|
|
#include "voxelalgorithms.h"
|
2017-01-11 09:03:07 +01:00
|
|
|
#include "settings.h"
|
2020-02-16 20:37:28 +01:00
|
|
|
#include "shader.h"
|
2018-01-23 19:28:21 +01:00
|
|
|
#include "content_cao.h"
|
2015-07-06 18:53:30 +02:00
|
|
|
#include <algorithm>
|
2017-06-26 20:11:17 +02:00
|
|
|
#include "client/renderingengine.h"
|
2017-01-08 10:49:47 +01:00
|
|
|
|
2020-02-16 20:37:28 +01:00
|
|
|
/*
|
|
|
|
CAOShaderConstantSetter
|
|
|
|
*/
|
|
|
|
|
|
|
|
//! Shader constant setter for passing material emissive color to the CAO object_shader
|
|
|
|
class CAOShaderConstantSetter : public IShaderConstantSetter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CAOShaderConstantSetter():
|
|
|
|
m_emissive_color_setting("emissiveColor")
|
|
|
|
{}
|
|
|
|
|
|
|
|
~CAOShaderConstantSetter() override = default;
|
|
|
|
|
|
|
|
void onSetConstants(video::IMaterialRendererServices *services,
|
|
|
|
bool is_highlevel) override
|
|
|
|
{
|
|
|
|
if (!is_highlevel)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Ambient color
|
|
|
|
video::SColorf emissive_color(m_emissive_color);
|
|
|
|
|
|
|
|
float as_array[4] = {
|
|
|
|
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
|
|
|
|
{
|
|
|
|
m_emissive_color = material.EmissiveColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
video::SColor m_emissive_color;
|
|
|
|
CachedPixelShaderSetting<float, 4> m_emissive_color_setting;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CAOShaderConstantSetterFactory : public IShaderConstantSetterFactory
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CAOShaderConstantSetterFactory()
|
|
|
|
{}
|
|
|
|
|
|
|
|
virtual IShaderConstantSetter* create()
|
|
|
|
{
|
|
|
|
return new CAOShaderConstantSetter();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-08 10:49:47 +01:00
|
|
|
/*
|
|
|
|
ClientEnvironment
|
|
|
|
*/
|
|
|
|
|
2017-06-26 20:11:17 +02:00
|
|
|
ClientEnvironment::ClientEnvironment(ClientMap *map,
|
2017-06-20 00:04:18 +02:00
|
|
|
ITextureSource *texturesource, Client *client):
|
2017-03-17 07:48:29 +01:00
|
|
|
Environment(client),
|
2017-01-08 10:49:47 +01:00
|
|
|
m_map(map),
|
|
|
|
m_texturesource(texturesource),
|
2017-06-20 00:04:18 +02:00
|
|
|
m_client(client)
|
2017-01-08 10:49:47 +01:00
|
|
|
{
|
2020-02-16 20:37:28 +01:00
|
|
|
auto *shdrsrc = m_client->getShaderSource();
|
|
|
|
shdrsrc->addShaderConstantSetterFactory(new CAOShaderConstantSetterFactory());
|
2017-01-08 10:49:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ClientEnvironment::~ClientEnvironment()
|
|
|
|
{
|
2018-12-13 20:18:54 +01:00
|
|
|
m_ao_manager.clear();
|
2017-01-08 10:49:47 +01:00
|
|
|
|
2017-08-17 08:11:39 +02:00
|
|
|
for (auto &simple_object : m_simple_objects) {
|
|
|
|
delete simple_object;
|
2017-01-08 10:49:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Drop/delete map
|
|
|
|
m_map->drop();
|
2017-04-05 12:30:52 +02:00
|
|
|
|
|
|
|
delete m_local_player;
|
2017-01-08 10:49:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Map & ClientEnvironment::getMap()
|
|
|
|
{
|
|
|
|
return *m_map;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClientMap & ClientEnvironment::getClientMap()
|
|
|
|
{
|
|
|
|
return *m_map;
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
m_local_player = player;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientEnvironment::step(float dtime)
|
|
|
|
{
|
|
|
|
/* Step time of day */
|
|
|
|
stepTimeOfDay(dtime);
|
|
|
|
|
|
|
|
// Get some settings
|
2017-01-09 20:39:22 +01:00
|
|
|
bool fly_allowed = m_client->checkLocalPrivilege("fly");
|
2017-01-08 10:49:47 +01:00
|
|
|
bool free_move = fly_allowed && g_settings->getBool("free_move");
|
|
|
|
|
|
|
|
// Get local player
|
|
|
|
LocalPlayer *lplayer = getLocalPlayer();
|
|
|
|
assert(lplayer);
|
|
|
|
// collision info queue
|
|
|
|
std::vector<CollisionInfo> player_collisions;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Get the speed the player is going
|
|
|
|
*/
|
|
|
|
bool is_climbing = lplayer->is_climbing;
|
|
|
|
|
|
|
|
f32 player_speed = lplayer->getSpeed().getLength();
|
|
|
|
|
|
|
|
/*
|
|
|
|
Maximum position increment
|
|
|
|
*/
|
|
|
|
//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)
|
|
|
|
dtime_max_increment = position_max_increment / player_speed;
|
|
|
|
|
|
|
|
// Maximum time increment is 10ms or lower
|
|
|
|
if(dtime_max_increment > 0.01)
|
|
|
|
dtime_max_increment = 0.01;
|
|
|
|
|
|
|
|
// Don't allow overly huge dtime
|
|
|
|
if(dtime > 0.5)
|
|
|
|
dtime = 0.5;
|
|
|
|
|
|
|
|
f32 dtime_downcount = dtime;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Stuff that has a maximum time increment
|
|
|
|
*/
|
|
|
|
|
|
|
|
u32 loopcount = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
loopcount++;
|
|
|
|
|
|
|
|
f32 dtime_part;
|
|
|
|
if(dtime_downcount > dtime_max_increment)
|
|
|
|
{
|
|
|
|
dtime_part = dtime_max_increment;
|
|
|
|
dtime_downcount -= dtime_part;
|
|
|
|
}
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
dtime_downcount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Handle local player
|
|
|
|
*/
|
|
|
|
|
|
|
|
{
|
|
|
|
// Apply physics
|
2018-07-14 19:41:26 +02:00
|
|
|
if (!free_move && !is_climbing) {
|
2017-01-08 10:49:47 +01:00
|
|
|
// Gravity
|
|
|
|
v3f speed = lplayer->getSpeed();
|
2018-07-14 19:41:26 +02:00
|
|
|
if (!lplayer->in_liquid)
|
|
|
|
speed.Y -= lplayer->movement_gravity *
|
|
|
|
lplayer->physics_override_gravity * dtime_part * 2.0f;
|
2017-01-08 10:49:47 +01:00
|
|
|
|
|
|
|
// Liquid floating / sinking
|
2018-12-31 01:07:30 +01:00
|
|
|
if (lplayer->in_liquid && !lplayer->swimming_vertical &&
|
|
|
|
!lplayer->swimming_pitch)
|
2018-07-14 19:41:26 +02:00
|
|
|
speed.Y -= lplayer->movement_liquid_sink * dtime_part * 2.0f;
|
2017-01-08 10:49:47 +01:00
|
|
|
|
|
|
|
// Liquid resistance
|
2018-07-14 19:41:26 +02:00
|
|
|
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.
|
|
|
|
static const f32 viscosity_factor = 0.3f;
|
2017-01-08 10:49:47 +01:00
|
|
|
|
|
|
|
v3f d_wanted = -speed / lplayer->movement_liquid_fluidity;
|
|
|
|
f32 dl = d_wanted.getLength();
|
2018-07-14 19:41:26 +02:00
|
|
|
if (dl > lplayer->movement_liquid_fluidity_smooth)
|
2017-01-08 10:49:47 +01:00
|
|
|
dl = lplayer->movement_liquid_fluidity_smooth;
|
|
|
|
|
2018-07-14 19:41:26 +02:00
|
|
|
dl *= (lplayer->liquid_viscosity * viscosity_factor) +
|
|
|
|
(1 - viscosity_factor);
|
|
|
|
v3f d = d_wanted.normalize() * (dl * dtime_part * 100.0f);
|
2017-01-08 10:49:47 +01:00
|
|
|
speed += d;
|
|
|
|
}
|
|
|
|
|
|
|
|
lplayer->setSpeed(speed);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Move the lplayer.
|
|
|
|
This also does collision detection.
|
|
|
|
*/
|
|
|
|
lplayer->move(dtime_part, this, position_max_increment,
|
|
|
|
&player_collisions);
|
|
|
|
}
|
2018-07-01 13:47:50 +02:00
|
|
|
} while (dtime_downcount > 0.001);
|
2017-01-08 10:49:47 +01:00
|
|
|
|
2018-01-23 19:28:21 +01:00
|
|
|
bool player_immortal = lplayer->getCAO() && lplayer->getCAO()->isImmortal();
|
|
|
|
|
2017-08-17 08:11:39 +02:00
|
|
|
for (const CollisionInfo &info : player_collisions) {
|
2017-01-08 10:49:47 +01:00
|
|
|
v3f speed_diff = info.new_speed - info.old_speed;;
|
|
|
|
// Handle only fall damage
|
|
|
|
// (because otherwise walking against something in fast_move kills you)
|
2017-08-17 08:11:39 +02:00
|
|
|
if (speed_diff.Y < 0 || info.old_speed.Y >= 0)
|
2017-01-08 10:49:47 +01:00
|
|
|
continue;
|
|
|
|
// 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
|
2017-08-17 08:11:39 +02:00
|
|
|
if (info.type == COLLISION_NODE) {
|
2017-01-09 20:39:22 +01:00
|
|
|
const ContentFeatures &f = m_client->ndef()->
|
2019-08-10 19:45:44 +02:00
|
|
|
get(m_map->getNode(info.node_p));
|
2017-01-08 10:49:47 +01:00
|
|
|
// Determine fall damage multiplier
|
|
|
|
int addp = itemgroup_get(f.groups, "fall_damage_add_percent");
|
2018-03-30 18:32:52 +02:00
|
|
|
pre_factor = 1.0f + (float)addp / 100.0f;
|
2017-01-08 10:49:47 +01:00
|
|
|
}
|
|
|
|
float speed = pre_factor * speed_diff.getLength();
|
2018-01-23 19:28:21 +01:00
|
|
|
if (speed > tolerance && !player_immortal) {
|
2017-05-21 11:18:38 +02:00
|
|
|
f32 damage_f = (speed - tolerance) / BS * post_factor;
|
2019-02-11 00:03:26 +01:00
|
|
|
u16 damage = (u16)MYMIN(damage_f + 0.5, U16_MAX);
|
2017-05-21 11:18:38 +02:00
|
|
|
if (damage != 0) {
|
2017-01-08 10:49:47 +01:00
|
|
|
damageLocalPlayer(damage, true);
|
2018-11-24 11:41:11 +01:00
|
|
|
m_client->getEventManager()->put(
|
|
|
|
new SimpleTriggerEvent(MtEvent::PLAYER_FALLING_DAMAGE));
|
2017-01-08 10:49:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-24 11:41:11 +01:00
|
|
|
if (m_client->modsLoaded())
|
2017-03-13 15:55:30 +01:00
|
|
|
m_script->environment_step(dtime);
|
2017-01-22 11:17:41 +01:00
|
|
|
|
2017-01-08 10:49:47 +01:00
|
|
|
// Update lighting on local player (used for wield item)
|
|
|
|
u32 day_night_ratio = getDayNightRatio();
|
|
|
|
{
|
|
|
|
// Get node at head
|
|
|
|
|
|
|
|
// On InvalidPositionException, use this as default
|
|
|
|
// (day: LIGHT_SUN, night: 0)
|
|
|
|
MapNode node_at_lplayer(CONTENT_AIR, 0x0f, 0);
|
|
|
|
|
|
|
|
v3s16 p = lplayer->getLightPosition();
|
2019-08-10 19:45:44 +02:00
|
|
|
node_at_lplayer = m_map->getNode(p);
|
2017-01-08 10:49:47 +01:00
|
|
|
|
2017-01-09 20:39:22 +01:00
|
|
|
u16 light = getInteriorLight(node_at_lplayer, 0, m_client->ndef());
|
2017-01-12 15:46:30 +01:00
|
|
|
final_color_blend(&lplayer->light_color, light, day_night_ratio);
|
2017-01-08 10:49:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Step active objects and update lighting of them
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool update_lighting = m_active_object_light_update_interval.step(dtime, 0.21);
|
2018-12-13 20:18:54 +01:00
|
|
|
auto cb_state = [this, dtime, update_lighting, day_night_ratio] (ClientActiveObject *cao) {
|
2017-01-08 10:49:47 +01:00
|
|
|
// Step object
|
2018-12-13 20:18:54 +01:00
|
|
|
cao->step(dtime, this);
|
2017-01-08 10:49:47 +01:00
|
|
|
|
2017-08-17 08:11:39 +02:00
|
|
|
if (update_lighting) {
|
2017-01-08 10:49:47 +01:00
|
|
|
// Update lighting
|
|
|
|
u8 light = 0;
|
|
|
|
bool pos_ok;
|
|
|
|
|
|
|
|
// Get node at head
|
2018-12-13 20:18:54 +01:00
|
|
|
v3s16 p = cao->getLightPosition();
|
2019-08-10 19:45:44 +02:00
|
|
|
MapNode n = this->m_map->getNode(p, &pos_ok);
|
2017-01-08 10:49:47 +01:00
|
|
|
if (pos_ok)
|
2017-01-09 20:39:22 +01:00
|
|
|
light = n.getLightBlend(day_night_ratio, m_client->ndef());
|
2017-01-08 10:49:47 +01:00
|
|
|
else
|
|
|
|
light = blend_light(day_night_ratio, LIGHT_SUN, 0);
|
|
|
|
|
2018-12-13 20:18:54 +01:00
|
|
|
cao->updateLight(light);
|
2017-01-08 10:49:47 +01:00
|
|
|
}
|
2018-12-13 20:18:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
m_ao_manager.step(dtime, cb_state);
|
2017-01-08 10:49:47 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
Step and handle simple objects
|
|
|
|
*/
|
2019-08-13 19:56:55 +02:00
|
|
|
g_profiler->avg("ClientEnv: CSO count [#]", m_simple_objects.size());
|
2017-08-17 08:11:39 +02:00
|
|
|
for (auto i = m_simple_objects.begin(); i != m_simple_objects.end();) {
|
2019-08-13 19:56:55 +02:00
|
|
|
ClientSimpleObject *simple = *i;
|
2017-01-08 10:49:47 +01:00
|
|
|
|
|
|
|
simple->step(dtime);
|
|
|
|
if(simple->m_to_be_removed) {
|
|
|
|
delete simple;
|
2019-08-13 19:56:55 +02:00
|
|
|
i = m_simple_objects.erase(i);
|
2017-01-08 10:49:47 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientEnvironment::addSimpleObject(ClientSimpleObject *simple)
|
|
|
|
{
|
|
|
|
m_simple_objects.push_back(simple);
|
|
|
|
}
|
|
|
|
|
|
|
|
GenericCAO* ClientEnvironment::getGenericCAO(u16 id)
|
|
|
|
{
|
|
|
|
ClientActiveObject *obj = getActiveObject(id);
|
|
|
|
if (obj && obj->getType() == ACTIVEOBJECT_TYPE_GENERIC)
|
|
|
|
return (GenericCAO*) obj;
|
2017-08-17 08:11:39 +02:00
|
|
|
|
|
|
|
return NULL;
|
2017-01-08 10:49:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isFreeClientActiveObjectId(const u16 id,
|
2017-06-04 21:00:04 +02:00
|
|
|
ClientActiveObjectMap &objects)
|
2017-01-08 10:49:47 +01:00
|
|
|
{
|
2017-08-17 08:11:39 +02:00
|
|
|
return id != 0 && objects.find(id) == objects.end();
|
2017-01-08 10:49:47 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-06-04 21:00:04 +02:00
|
|
|
u16 getFreeClientActiveObjectId(ClientActiveObjectMap &objects)
|
2017-01-08 10:49:47 +01:00
|
|
|
{
|
2018-12-13 20:18:54 +01:00
|
|
|
// try to reuse id's as late as possible
|
2017-01-08 10:49:47 +01:00
|
|
|
static u16 last_used_id = 0;
|
|
|
|
u16 startid = last_used_id;
|
|
|
|
for(;;) {
|
|
|
|
last_used_id ++;
|
|
|
|
if (isFreeClientActiveObjectId(last_used_id, objects))
|
|
|
|
return last_used_id;
|
|
|
|
|
|
|
|
if (last_used_id == startid)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u16 ClientEnvironment::addActiveObject(ClientActiveObject *object)
|
|
|
|
{
|
2018-12-13 20:18:54 +01:00
|
|
|
// Register object. If failed return zero id
|
|
|
|
if (!m_ao_manager.registerObject(object))
|
2017-01-08 10:49:47 +01:00
|
|
|
return 0;
|
2018-12-13 20:18:54 +01:00
|
|
|
|
2017-07-02 20:29:58 +02:00
|
|
|
object->addToScene(m_texturesource);
|
2017-01-08 10:49:47 +01:00
|
|
|
|
2018-12-13 20:18:54 +01:00
|
|
|
// Update lighting immediately
|
|
|
|
u8 light = 0;
|
|
|
|
bool pos_ok;
|
2017-01-08 10:49:47 +01:00
|
|
|
|
2018-12-13 20:18:54 +01:00
|
|
|
// Get node at head
|
|
|
|
v3s16 p = object->getLightPosition();
|
2019-08-10 19:45:44 +02:00
|
|
|
MapNode n = m_map->getNode(p, &pos_ok);
|
2018-12-13 20:18:54 +01:00
|
|
|
if (pos_ok)
|
|
|
|
light = n.getLightBlend(getDayNightRatio(), m_client->ndef());
|
|
|
|
else
|
|
|
|
light = blend_light(getDayNightRatio(), LIGHT_SUN, 0);
|
|
|
|
|
|
|
|
object->updateLight(light);
|
2017-01-08 10:49:47 +01:00
|
|
|
return object->getId();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientEnvironment::addActiveObject(u16 id, u8 type,
|
|
|
|
const std::string &init_data)
|
|
|
|
{
|
|
|
|
ClientActiveObject* obj =
|
2017-01-09 20:39:22 +01:00
|
|
|
ClientActiveObject::create((ActiveObjectType) type, m_client, this);
|
2017-01-08 10:49:47 +01:00
|
|
|
if(obj == NULL)
|
|
|
|
{
|
|
|
|
infostream<<"ClientEnvironment::addActiveObject(): "
|
|
|
|
<<"id="<<id<<" type="<<type<<": Couldn't create object"
|
|
|
|
<<std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
obj->setId(id);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-07-29 19:14:07 +02:00
|
|
|
u16 new_id = addActiveObject(obj);
|
|
|
|
// Object initialized:
|
|
|
|
if ((obj = getActiveObject(new_id))) {
|
|
|
|
// Final step is to update all children which are already known
|
|
|
|
// Data provided by GENERIC_CMD_SPAWN_INFANT
|
|
|
|
const auto &children = obj->getAttachmentChildIds();
|
|
|
|
for (auto c_id : children) {
|
|
|
|
if (auto *o = getActiveObject(c_id))
|
|
|
|
o->updateAttachments();
|
|
|
|
}
|
|
|
|
}
|
2017-01-08 10:49:47 +01:00
|
|
|
}
|
|
|
|
|
2019-10-02 19:11:27 +02:00
|
|
|
|
|
|
|
void ClientEnvironment::removeActiveObject(u16 id)
|
|
|
|
{
|
|
|
|
// Get current attachment childs to detach them visually
|
|
|
|
std::unordered_set<int> attachment_childs;
|
|
|
|
if (auto *obj = getActiveObject(id))
|
|
|
|
attachment_childs = obj->getAttachmentChildIds();
|
|
|
|
|
|
|
|
m_ao_manager.removeObject(id);
|
|
|
|
|
|
|
|
// Perform a proper detach in Irrlicht
|
|
|
|
for (auto c_id : attachment_childs) {
|
|
|
|
if (ClientActiveObject *child = getActiveObject(c_id))
|
|
|
|
child->updateAttachments();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-08 10:49:47 +01:00
|
|
|
void ClientEnvironment::processActiveObjectMessage(u16 id, const std::string &data)
|
|
|
|
{
|
|
|
|
ClientActiveObject *obj = getActiveObject(id);
|
|
|
|
if (obj == NULL) {
|
|
|
|
infostream << "ClientEnvironment::processActiveObjectMessage():"
|
|
|
|
<< " 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Callbacks for activeobjects
|
|
|
|
*/
|
|
|
|
|
2019-02-11 00:03:26 +01:00
|
|
|
void ClientEnvironment::damageLocalPlayer(u16 damage, bool handle_hp)
|
2017-01-08 10:49:47 +01:00
|
|
|
{
|
|
|
|
LocalPlayer *lplayer = getLocalPlayer();
|
|
|
|
assert(lplayer);
|
|
|
|
|
|
|
|
if (handle_hp) {
|
|
|
|
if (lplayer->hp > damage)
|
|
|
|
lplayer->hp -= damage;
|
|
|
|
else
|
|
|
|
lplayer->hp = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClientEnvEvent event;
|
|
|
|
event.type = CEE_PLAYER_DAMAGE;
|
|
|
|
event.player_damage.amount = damage;
|
|
|
|
event.player_damage.send_to_server = handle_hp;
|
|
|
|
m_client_event_queue.push(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Client likes to call these
|
|
|
|
*/
|
|
|
|
|
2017-04-29 17:25:25 +02:00
|
|
|
ClientEnvEvent ClientEnvironment::getClientEnvEvent()
|
2017-01-08 10:49:47 +01:00
|
|
|
{
|
2017-04-29 17:25:25 +02:00
|
|
|
FATAL_ERROR_IF(m_client_event_queue.empty(),
|
|
|
|
"ClientEnvironment::getClientEnvEvent(): queue is empty");
|
|
|
|
|
|
|
|
ClientEnvEvent event = m_client_event_queue.front();
|
|
|
|
m_client_event_queue.pop();
|
2017-01-08 10:49:47 +01:00
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
2016-07-23 21:11:20 +02:00
|
|
|
void ClientEnvironment::getSelectedActiveObjects(
|
|
|
|
const core::line3d<f32> &shootline_on_map,
|
|
|
|
std::vector<PointedThing> &objects)
|
2017-01-08 10:49:47 +01:00
|
|
|
{
|
2016-07-23 21:11:20 +02:00
|
|
|
std::vector<DistanceSortedActiveObject> allObjects;
|
2017-01-08 10:49:47 +01:00
|
|
|
getActiveObjects(shootline_on_map.start,
|
2016-07-23 21:11:20 +02:00
|
|
|
shootline_on_map.getLength() + 10.0f, allObjects);
|
2017-01-08 10:49:47 +01:00
|
|
|
const v3f line_vector = shootline_on_map.getVector();
|
|
|
|
|
2017-08-17 08:11:39 +02:00
|
|
|
for (const auto &allObject : allObjects) {
|
|
|
|
ClientActiveObject *obj = allObject.obj;
|
2016-07-23 21:11:20 +02:00
|
|
|
aabb3f selection_box;
|
|
|
|
if (!obj->getSelectionBox(&selection_box))
|
2017-01-08 10:49:47 +01:00
|
|
|
continue;
|
2017-08-17 08:11:39 +02:00
|
|
|
|
|
|
|
const v3f &pos = obj->getPosition();
|
2016-07-23 21:11:20 +02:00
|
|
|
aabb3f offsetted_box(selection_box.MinEdge + pos,
|
|
|
|
selection_box.MaxEdge + pos);
|
2017-01-08 10:49:47 +01:00
|
|
|
|
|
|
|
v3f current_intersection;
|
|
|
|
v3s16 current_normal;
|
|
|
|
if (boxLineCollision(offsetted_box, shootline_on_map.start, line_vector,
|
2016-07-23 21:11:20 +02:00
|
|
|
¤t_intersection, ¤t_normal)) {
|
2017-08-17 08:11:39 +02:00
|
|
|
objects.emplace_back((s16) obj->getId(), current_intersection, current_normal,
|
|
|
|
(current_intersection - shootline_on_map.start).getLengthSQ());
|
2017-01-08 10:49:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|