aboutsummaryrefslogtreecommitdiff
path: root/src/script/cpp_api
ModeNameSize
-rw-r--r--CMakeLists.txt563logplain
-rw-r--r--s_base.cpp6289logplain
-rw-r--r--s_base.h2453logplain
-rw-r--r--s_entity.cpp8065logplain
-rw-r--r--s_entity.h1542logplain
-rw-r--r--s_env.cpp4532logplain
-rw-r--r--s_env.h1312logplain
-rw-r--r--s_internal.h2282logplain
-rw-r--r--s_inventory.cpp7932logplain
-rw-r--r--s_inventory.h2342logplain
-rw-r--r--s_item.cpp6487logplain
-rw-r--r--s_item.h1887logplain
-rw-r--r--s_mainmenu.cpp2634logplain
-rw-r--r--s_mainmenu.h1442logplain
-rw-r--r--s_node.cpp7299logplain
-rw-r--r--s_node.h1949logplain
-rw-r--r--s_nodemeta.cpp8236logplain
-rw-r--r--s_nodemeta.h2220logplain
-rw-r--r--s_player.cpp3885logplain
-rw-r--r--s_player.h1463logplain
-rw-r--r--s_server.cpp4722logplain
-rw-r--r--s_server.h1559logplain
#n359'>359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
/*
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"
#include "client.h"
#include "clientenvironment.h"
#include "clientsimpleobject.h"
#include "clientmap.h"
#include "scripting_client.h"
#include "mapblock_mesh.h"
#include "mtevent.h"
#include "collision.h"
#include "nodedef.h"
#include "profiler.h"
#include "raycast.h"
#include "voxelalgorithms.h"
#include "settings.h"
#include "shader.h"
#include "content_cao.h"
#include <algorithm>
#include "client/renderingengine.h"

/*
	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) override
	{
		// 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();
	}
};

/*
	ClientEnvironment
*/

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());
}

ClientEnvironment::~ClientEnvironment()
{
	m_ao_manager.clear();

	for (auto &simple_object : m_simple_objects) {
		delete simple_object;
	}

	// Drop/delete map
	m_map->drop();

	delete m_local_player;
}

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
	bool fly_allowed = m_client->checkLocalPrivilege("fly");
	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;

	/*
		Stuff that has a maximum time increment
	*/

	u32 steps = ceil(dtime / dtime_max_increment);
	f32 dtime_part = dtime / steps;
	for (; steps > 0; --steps) {
		/*
			Local player handling
		*/

		// Control local player
		lplayer->applyControl(dtime_part, this);

		// Apply physics
		if (!free_move && !is_climbing) {
			// Gravity
			v3f speed = lplayer->getSpeed();
			if (!lplayer->in_liquid)
				speed.Y -= lplayer->movement_gravity *
					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;

			// 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.
				static const f32 viscosity_factor = 0.3f;

				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);
				speed += d;
			}

			lplayer->setSpeed(speed);
		}

		/*
			Move the lplayer.
			This also does collision detection.
		*/
		lplayer->move(dtime_part, this, position_max_increment,
			&player_collisions);
	}

	bool player_immortal = false;
	f32 player_fall_factor = 1.0f;
	GenericCAO *playercao = lplayer->getCAO();
	if (playercao) {
		player_immortal = playercao->isImmortal();
		int addp_p = itemgroup_get(playercao->getGroups(),
			"fall_damage_add_percent");
		// convert armor group into an usable fall damage factor
		player_fall_factor = 1.0f + (float)addp_p / 100.0f;
	}

	for (const CollisionInfo &info : player_collisions) {
		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)
			continue;
		// Get rid of other components
		speed_diff.X = 0;