2010-11-29 19:13:04 +01:00
|
|
|
/*
|
2013-02-24 18:40:43 +01:00
|
|
|
Minetest
|
2013-02-24 19:38:45 +01:00
|
|
|
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
2010-11-29 19:13:04 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
2012-06-05 16:56:56 +02:00
|
|
|
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
|
2010-11-29 19:13:04 +01:00
|
|
|
(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
|
2012-06-05 16:56:56 +02:00
|
|
|
GNU Lesser General Public License for more details.
|
2010-11-29 19:13:04 +01:00
|
|
|
|
2012-06-05 16:56:56 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
2010-11-29 19:13:04 +01:00
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2015-04-01 15:01:28 +02:00
|
|
|
#include <fstream>
|
2010-11-27 00:02:21 +01:00
|
|
|
#include "environment.h"
|
2011-04-21 18:35:17 +02:00
|
|
|
#include "collision.h"
|
2016-07-23 21:11:20 +02:00
|
|
|
#include "raycast.h"
|
2017-04-25 19:38:08 +02:00
|
|
|
#include "scripting_server.h"
|
2015-07-16 17:37:46 +02:00
|
|
|
#include "server.h"
|
2012-03-16 15:34:30 +01:00
|
|
|
#include "daynightratio.h"
|
2013-08-11 04:09:45 +02:00
|
|
|
#include "emerge.h"
|
2016-04-27 17:58:09 +02:00
|
|
|
|
2017-04-06 16:03:29 +02:00
|
|
|
|
2017-03-17 07:48:29 +01:00
|
|
|
Environment::Environment(IGameDef *gamedef):
|
2017-06-16 11:25:52 +02:00
|
|
|
m_time_of_day_speed(0.0f),
|
2017-06-17 19:11:28 +02:00
|
|
|
m_day_count(0),
|
|
|
|
m_gamedef(gamedef)
|
2010-11-27 00:02:21 +01:00
|
|
|
{
|
2014-12-06 15:37:37 +01:00
|
|
|
m_cache_enable_shaders = g_settings->getBool("enable_shaders");
|
Allow NodeTimer, ABM and block mgmt interval changes.
ABM's are hardcoded to run every 1.0s, NodeTimers are hard coded to
run at every 1.0s. Block mgmt is running every 2.0sec.
However, these timers can be better tuned for both higher and lower
values by server owners. Some server owners want to, and have the
resources to send more packets per second to clients, and so they
may wish to send smaller updates sooner. Right now all ABM's are
coalesced into 1.0 second intervals, resulting in large send queues
to all clients. By reducing the amount of possible timers, one can
get a far better response rate and lower the perception of lag.
On the other side of the camp, some servers may want to increase
these values, which again isn't easily doable.
The global settings abm_interval and nodetimer_interval are set to
current values by default. I've tested with 0.2/0.5 type values
and noticed a greatly improved response and better scattering of
nodetimers, as well as enjoying not faceplanting into doors with
pressure plates anymore.
2016-03-02 08:14:26 +01:00
|
|
|
m_cache_active_block_mgmt_interval = g_settings->getFloat("active_block_mgmt_interval");
|
|
|
|
m_cache_abm_interval = g_settings->getFloat("abm_interval");
|
|
|
|
m_cache_nodetimer_interval = g_settings->getFloat("nodetimer_interval");
|
2020-08-13 01:02:07 +02:00
|
|
|
m_cache_abm_time_budget = g_settings->getFloat("abm_time_budget");
|
2017-09-02 04:18:42 +02:00
|
|
|
|
|
|
|
m_time_of_day = g_settings->getU32("world_start_time");
|
|
|
|
m_time_of_day_f = (float)m_time_of_day / 24000.0f;
|
2010-11-27 00:02:21 +01:00
|
|
|
}
|
|
|
|
|
2011-02-20 23:45:14 +01:00
|
|
|
u32 Environment::getDayNightRatio()
|
|
|
|
{
|
2022-11-03 17:35:31 +01:00
|
|
|
MutexAutoLock lock(m_time_lock);
|
2015-11-04 03:07:32 +01:00
|
|
|
if (m_enable_day_night_ratio_override)
|
|
|
|
return m_day_night_ratio_override;
|
2015-10-27 21:03:59 +01:00
|
|
|
return time_to_daynight_ratio(m_time_of_day_f * 24000, m_cache_enable_shaders);
|
2011-05-22 16:00:09 +02:00
|
|
|
}
|
|
|
|
|
2014-06-22 23:19:10 +02:00
|
|
|
void Environment::setTimeOfDaySpeed(float speed)
|
|
|
|
{
|
|
|
|
m_time_of_day_speed = speed;
|
|
|
|
}
|
|
|
|
|
2015-11-04 03:07:32 +01:00
|
|
|
void Environment::setDayNightRatioOverride(bool enable, u32 value)
|
|
|
|
{
|
2022-11-03 17:35:31 +01:00
|
|
|
MutexAutoLock lock(m_time_lock);
|
2015-11-04 03:07:32 +01:00
|
|
|
m_enable_day_night_ratio_override = enable;
|
|
|
|
m_day_night_ratio_override = value;
|
|
|
|
}
|
|
|
|
|
2015-03-04 11:46:31 +01:00
|
|
|
void Environment::setTimeOfDay(u32 time)
|
|
|
|
{
|
2022-11-03 17:35:31 +01:00
|
|
|
MutexAutoLock lock(m_time_lock);
|
2016-03-06 21:02:21 +01:00
|
|
|
if (m_time_of_day > time)
|
2017-04-19 23:02:07 +02:00
|
|
|
++m_day_count;
|
2015-03-04 11:46:31 +01:00
|
|
|
m_time_of_day = time;
|
|
|
|
m_time_of_day_f = (float)time / 24000.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 Environment::getTimeOfDay()
|
|
|
|
{
|
2022-11-03 17:35:31 +01:00
|
|
|
MutexAutoLock lock(m_time_lock);
|
2015-11-03 08:56:56 +01:00
|
|
|
return m_time_of_day;
|
2015-03-04 11:46:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
float Environment::getTimeOfDayF()
|
|
|
|
{
|
2022-11-03 17:35:31 +01:00
|
|
|
MutexAutoLock lock(m_time_lock);
|
2015-11-03 08:56:56 +01:00
|
|
|
return m_time_of_day_f;
|
2015-03-04 11:46:31 +01:00
|
|
|
}
|
|
|
|
|
2020-04-10 22:06:24 +02:00
|
|
|
bool Environment::line_of_sight(v3f pos1, v3f pos2, v3s16 *p)
|
|
|
|
{
|
|
|
|
// Iterate trough nodes on the line
|
|
|
|
voxalgo::VoxelLineIterator iterator(pos1 / BS, (pos2 - pos1) / BS);
|
|
|
|
do {
|
|
|
|
MapNode n = getMap().getNode(iterator.m_current_node_pos);
|
|
|
|
|
|
|
|
// Return non-air
|
|
|
|
if (n.param0 != CONTENT_AIR) {
|
|
|
|
if (p)
|
|
|
|
*p = iterator.m_current_node_pos;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
iterator.next();
|
|
|
|
} while (iterator.m_current_index <= iterator.m_last_index);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-23 21:11:20 +02:00
|
|
|
/*
|
|
|
|
Check if a node is pointable
|
|
|
|
*/
|
|
|
|
inline static bool isPointableNode(const MapNode &n,
|
2018-02-10 21:04:16 +01:00
|
|
|
const NodeDefManager *nodedef , bool liquids_pointable)
|
2016-07-23 21:11:20 +02:00
|
|
|
{
|
|
|
|
const ContentFeatures &features = nodedef->get(n);
|
|
|
|
return features.pointable ||
|
|
|
|
(liquids_pointable && features.isLiquid());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Environment::continueRaycast(RaycastState *state, PointedThing *result)
|
|
|
|
{
|
2018-02-10 21:04:16 +01:00
|
|
|
const NodeDefManager *nodedef = getMap().getNodeDefManager();
|
2016-07-23 21:11:20 +02:00
|
|
|
if (state->m_initialization_needed) {
|
|
|
|
// Add objects
|
|
|
|
if (state->m_objects_pointable) {
|
|
|
|
std::vector<PointedThing> found;
|
|
|
|
getSelectedActiveObjects(state->m_shootline, found);
|
2017-08-20 19:37:29 +02:00
|
|
|
for (const PointedThing &pointed : found) {
|
|
|
|
state->m_found.push(pointed);
|
2016-07-23 21:11:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Set search range
|
|
|
|
core::aabbox3d<s16> maximal_exceed = nodedef->getSelectionBoxIntUnion();
|
|
|
|
state->m_search_range.MinEdge = -maximal_exceed.MaxEdge;
|
|
|
|
state->m_search_range.MaxEdge = -maximal_exceed.MinEdge;
|
|
|
|
// Setting is done
|
|
|
|
state->m_initialization_needed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The index of the first pointed thing that was not returned
|
|
|
|
// before. The last index which needs to be tested.
|
|
|
|
s16 lastIndex = state->m_iterator.m_last_index;
|
|
|
|
if (!state->m_found.empty()) {
|
|
|
|
lastIndex = state->m_iterator.getIndex(
|
|
|
|
floatToInt(state->m_found.top().intersection_point, BS));
|
|
|
|
}
|
|
|
|
|
|
|
|
Map &map = getMap();
|
|
|
|
// If a node is found, this is the center of the
|
|
|
|
// first nodebox the shootline meets.
|
|
|
|
v3f found_boxcenter(0, 0, 0);
|
|
|
|
// The untested nodes are in this range.
|
|
|
|
core::aabbox3d<s16> new_nodes;
|
|
|
|
while (state->m_iterator.m_current_index <= lastIndex) {
|
|
|
|
// Test the nodes around the current node in search_range.
|
|
|
|
new_nodes = state->m_search_range;
|
|
|
|
new_nodes.MinEdge += state->m_iterator.m_current_node_pos;
|
|
|
|
new_nodes.MaxEdge += state->m_iterator.m_current_node_pos;
|
|
|
|
|
|
|
|
// Only check new nodes
|
|
|
|
v3s16 delta = state->m_iterator.m_current_node_pos
|
|
|
|
- state->m_previous_node;
|
|
|
|
if (delta.X > 0) {
|
|
|
|
new_nodes.MinEdge.X = new_nodes.MaxEdge.X;
|
|
|
|
} else if (delta.X < 0) {
|
|
|
|
new_nodes.MaxEdge.X = new_nodes.MinEdge.X;
|
|
|
|
} else if (delta.Y > 0) {
|
|
|
|
new_nodes.MinEdge.Y = new_nodes.MaxEdge.Y;
|
|
|
|
} else if (delta.Y < 0) {
|
|
|
|
new_nodes.MaxEdge.Y = new_nodes.MinEdge.Y;
|
|
|
|
} else if (delta.Z > 0) {
|
|
|
|
new_nodes.MinEdge.Z = new_nodes.MaxEdge.Z;
|
|
|
|
} else if (delta.Z < 0) {
|
|
|
|
new_nodes.MaxEdge.Z = new_nodes.MinEdge.Z;
|
|
|
|
}
|
|
|
|
|
2022-01-30 21:31:18 +01:00
|
|
|
if (new_nodes.MaxEdge.X == S16_MAX ||
|
|
|
|
new_nodes.MaxEdge.Y == S16_MAX ||
|
|
|
|
new_nodes.MaxEdge.Z == S16_MAX) {
|
|
|
|
break; // About to go out of bounds
|
|
|
|
}
|
|
|
|
|
2016-07-23 21:11:20 +02:00
|
|
|
// For each untested node
|
|
|
|
for (s16 x = new_nodes.MinEdge.X; x <= new_nodes.MaxEdge.X; x++)
|
|
|
|
for (s16 y = new_nodes.MinEdge.Y; y <= new_nodes.MaxEdge.Y; y++)
|
|
|
|
for (s16 z = new_nodes.MinEdge.Z; z <= new_nodes.MaxEdge.Z; z++) {
|
|
|
|
MapNode n;
|
|
|
|
v3s16 np(x, y, z);
|
|
|
|
bool is_valid_position;
|
|
|
|
|
2019-08-10 19:45:44 +02:00
|
|
|
n = map.getNode(np, &is_valid_position);
|
2016-07-23 21:11:20 +02:00
|
|
|
if (!(is_valid_position && isPointableNode(n, nodedef,
|
|
|
|
state->m_liquids_pointable))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
PointedThing result;
|
|
|
|
|
|
|
|
std::vector<aabb3f> boxes;
|
|
|
|
n.getSelectionBoxes(nodedef, &boxes,
|
|
|
|
n.getNeighbors(np, &map));
|
|
|
|
|
|
|
|
// Is there a collision with a selection box?
|
|
|
|
bool is_colliding = false;
|
|
|
|
// Minimal distance of all collisions
|
|
|
|
float min_distance_sq = 10000000;
|
2018-08-16 20:10:08 +02:00
|
|
|
// ID of the current box (loop counter)
|
|
|
|
u16 id = 0;
|
2016-07-23 21:11:20 +02:00
|
|
|
|
2022-10-30 16:53:14 +01:00
|
|
|
// Do calculations relative to the node center
|
|
|
|
// to translate the ray rather than the boxes
|
2016-07-23 21:11:20 +02:00
|
|
|
v3f npf = intToFloat(np, BS);
|
2022-10-30 16:53:14 +01:00
|
|
|
v3f rel_start = state->m_shootline.start - npf;
|
2018-08-16 20:10:08 +02:00
|
|
|
for (aabb3f &box : boxes) {
|
2016-07-23 21:11:20 +02:00
|
|
|
v3f intersection_point;
|
2022-10-30 16:53:14 +01:00
|
|
|
v3f intersection_normal;
|
|
|
|
if (!boxLineCollision(box, rel_start,
|
2016-07-23 21:11:20 +02:00
|
|
|
state->m_shootline.getVector(), &intersection_point,
|
2018-08-16 20:10:08 +02:00
|
|
|
&intersection_normal)) {
|
|
|
|
++id;
|
2016-07-23 21:11:20 +02:00
|
|
|
continue;
|
2018-08-16 20:10:08 +02:00
|
|
|
}
|
2016-07-23 21:11:20 +02:00
|
|
|
|
2022-10-30 16:53:14 +01:00
|
|
|
intersection_point += npf; // translate back to world coords
|
2016-07-23 21:11:20 +02:00
|
|
|
f32 distanceSq = (intersection_point
|
|
|
|
- state->m_shootline.start).getLengthSQ();
|
|
|
|
// If this is the nearest collision, save it
|
|
|
|
if (min_distance_sq > distanceSq) {
|
|
|
|
min_distance_sq = distanceSq;
|
|
|
|
result.intersection_point = intersection_point;
|
|
|
|
result.intersection_normal = intersection_normal;
|
2018-08-16 20:10:08 +02:00
|
|
|
result.box_id = id;
|
2016-07-23 21:11:20 +02:00
|
|
|
found_boxcenter = box.getCenter();
|
|
|
|
is_colliding = true;
|
|
|
|
}
|
2018-08-16 20:10:08 +02:00
|
|
|
++id;
|
2016-07-23 21:11:20 +02:00
|
|
|
}
|
|
|
|
// If there wasn't a collision, stop
|
|
|
|
if (!is_colliding) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
result.type = POINTEDTHING_NODE;
|
|
|
|
result.node_undersurface = np;
|
|
|
|
result.distanceSq = min_distance_sq;
|
|
|
|
// Set undersurface and abovesurface nodes
|
|
|
|
f32 d = 0.002 * BS;
|
|
|
|
v3f fake_intersection = result.intersection_point;
|
2022-10-31 09:31:21 +01:00
|
|
|
found_boxcenter += npf; // translate back to world coords
|
2016-07-23 21:11:20 +02:00
|
|
|
// Move intersection towards its source block.
|
|
|
|
if (fake_intersection.X < found_boxcenter.X) {
|
|
|
|
fake_intersection.X += d;
|
|
|
|
} else {
|
|
|
|
fake_intersection.X -= d;
|
|
|
|
}
|
|
|
|
if (fake_intersection.Y < found_boxcenter.Y) {
|
|
|
|
fake_intersection.Y += d;
|
|
|
|
} else {
|
|
|
|
fake_intersection.Y -= d;
|
|
|
|
}
|
|
|
|
if (fake_intersection.Z < found_boxcenter.Z) {
|
|
|
|
fake_intersection.Z += d;
|
|
|
|
} else {
|
|
|
|
fake_intersection.Z -= d;
|
|
|
|
}
|
|
|
|
result.node_real_undersurface = floatToInt(
|
|
|
|
fake_intersection, BS);
|
|
|
|
result.node_abovesurface = result.node_real_undersurface
|
2022-10-30 16:53:14 +01:00
|
|
|
+ floatToInt(result.intersection_normal, 1.0f);
|
2016-07-23 21:11:20 +02:00
|
|
|
// Push found PointedThing
|
|
|
|
state->m_found.push(result);
|
|
|
|
// If this is nearer than the old nearest object,
|
|
|
|
// the search can be shorter
|
|
|
|
s16 newIndex = state->m_iterator.getIndex(
|
|
|
|
result.node_real_undersurface);
|
|
|
|
if (newIndex < lastIndex) {
|
|
|
|
lastIndex = newIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Next node
|
|
|
|
state->m_previous_node = state->m_iterator.m_current_node_pos;
|
|
|
|
state->m_iterator.next();
|
|
|
|
}
|
|
|
|
// Return empty PointedThing if nothing left on the ray
|
|
|
|
if (state->m_found.empty()) {
|
|
|
|
result->type = POINTEDTHING_NOTHING;
|
|
|
|
} else {
|
|
|
|
*result = state->m_found.top();
|
|
|
|
state->m_found.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-16 15:34:30 +01:00
|
|
|
void Environment::stepTimeOfDay(float dtime)
|
|
|
|
{
|
2015-11-04 03:07:32 +01:00
|
|
|
MutexAutoLock lock(this->m_time_lock);
|
2015-11-04 06:44:55 +01:00
|
|
|
|
|
|
|
// Cached in order to prevent the two reads we do to give
|
|
|
|
// different results (can be written by code not under the lock)
|
|
|
|
f32 cached_time_of_day_speed = m_time_of_day_speed;
|
|
|
|
|
|
|
|
f32 speed = cached_time_of_day_speed * 24000. / (24. * 3600);
|
2015-11-04 03:07:32 +01:00
|
|
|
m_time_conversion_skew += dtime;
|
|
|
|
u32 units = (u32)(m_time_conversion_skew * speed);
|
2012-03-16 15:34:30 +01:00
|
|
|
bool sync_f = false;
|
2015-10-27 21:03:59 +01:00
|
|
|
if (units > 0) {
|
2012-03-16 15:34:30 +01:00
|
|
|
// Sync at overflow
|
2016-03-06 21:02:21 +01:00
|
|
|
if (m_time_of_day + units >= 24000) {
|
2012-03-16 15:34:30 +01:00
|
|
|
sync_f = true;
|
2017-04-19 23:02:07 +02:00
|
|
|
++m_day_count;
|
2016-03-06 21:02:21 +01:00
|
|
|
}
|
2012-03-16 15:34:30 +01:00
|
|
|
m_time_of_day = (m_time_of_day + units) % 24000;
|
2015-10-27 21:03:59 +01:00
|
|
|
if (sync_f)
|
2012-03-16 15:34:30 +01:00
|
|
|
m_time_of_day_f = (float)m_time_of_day / 24000.0;
|
|
|
|
}
|
2014-06-22 23:19:10 +02:00
|
|
|
if (speed > 0) {
|
2015-11-03 08:56:56 +01:00
|
|
|
m_time_conversion_skew -= (f32)units / speed;
|
2014-06-22 23:19:10 +02:00
|
|
|
}
|
2015-10-27 21:03:59 +01:00
|
|
|
if (!sync_f) {
|
2015-11-04 06:44:55 +01:00
|
|
|
m_time_of_day_f += cached_time_of_day_speed / 24 / 3600 * dtime;
|
2015-10-27 21:03:59 +01:00
|
|
|
if (m_time_of_day_f > 1.0)
|
2012-03-16 15:34:30 +01:00
|
|
|
m_time_of_day_f -= 1.0;
|
2015-10-27 21:03:59 +01:00
|
|
|
if (m_time_of_day_f < 0.0)
|
2012-03-16 15:34:30 +01:00
|
|
|
m_time_of_day_f += 1.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-06 21:02:21 +01:00
|
|
|
u32 Environment::getDayCount()
|
|
|
|
{
|
|
|
|
// Atomic<u32> counter
|
|
|
|
return m_day_count;
|
|
|
|
}
|