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"
|
2011-06-26 14:48:56 +02:00
|
|
|
#include "serverobject.h"
|
2017-01-21 15:02:08 +01:00
|
|
|
#include "serverscripting.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):
|
2015-11-04 03:07:32 +01:00
|
|
|
m_time_of_day_speed(0),
|
2012-03-16 15:34:30 +01:00
|
|
|
m_time_of_day(9000),
|
|
|
|
m_time_of_day_f(9000./24000),
|
2015-11-04 03:07:32 +01:00
|
|
|
m_time_conversion_skew(0.0f),
|
|
|
|
m_enable_day_night_ratio_override(false),
|
2017-03-17 07:48:29 +01:00
|
|
|
m_day_night_ratio_override(0.0f),
|
|
|
|
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");
|
2010-11-27 00:02:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Environment::~Environment()
|
|
|
|
{
|
2011-01-15 02:28:19 +01:00
|
|
|
}
|
|
|
|
|
2011-02-20 23:45:14 +01:00
|
|
|
u32 Environment::getDayNightRatio()
|
|
|
|
{
|
2015-11-04 03:07:32 +01:00
|
|
|
MutexAutoLock lock(this->m_time_lock);
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(this->m_time_lock);
|
|
|
|
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)
|
|
|
|
{
|
2015-11-04 03:07:32 +01:00
|
|
|
MutexAutoLock lock(this->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()
|
|
|
|
{
|
2015-11-04 03:07:32 +01:00
|
|
|
MutexAutoLock lock(this->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()
|
|
|
|
{
|
2015-11-04 03:07:32 +01:00
|
|
|
MutexAutoLock lock(this->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
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|