mirror of
https://github.com/minetest/minetest.git
synced 2024-12-02 20:43:45 +01:00
Environment: Time of day fixes and add serverside getter
-> Put access to time variables under the time lock. -> Merge both time locks, there is no point to have two locks. -> Fix the lock being released too early in Environment::setTimeOfDay -> Add serverside getter so that you don't have to get the environment if you only have the server
This commit is contained in:
parent
c406438b95
commit
ca8e56c15a
@ -180,6 +180,7 @@ std::vector<Player*> Environment::getPlayers(bool ignore_disconnected)
|
|||||||
|
|
||||||
u32 Environment::getDayNightRatio()
|
u32 Environment::getDayNightRatio()
|
||||||
{
|
{
|
||||||
|
MutexAutoLock(this->m_time_lock);
|
||||||
if (m_enable_day_night_ratio_override)
|
if (m_enable_day_night_ratio_override)
|
||||||
return m_day_night_ratio_override;
|
return m_day_night_ratio_override;
|
||||||
return time_to_daynight_ratio(m_time_of_day_f * 24000, m_cache_enable_shaders);
|
return time_to_daynight_ratio(m_time_of_day_f * 24000, m_cache_enable_shaders);
|
||||||
@ -187,13 +188,13 @@ u32 Environment::getDayNightRatio()
|
|||||||
|
|
||||||
void Environment::setTimeOfDaySpeed(float speed)
|
void Environment::setTimeOfDaySpeed(float speed)
|
||||||
{
|
{
|
||||||
MutexAutoLock(this->m_timeofday_lock);
|
MutexAutoLock(this->m_time_lock);
|
||||||
m_time_of_day_speed = speed;
|
m_time_of_day_speed = speed;
|
||||||
}
|
}
|
||||||
|
|
||||||
float Environment::getTimeOfDaySpeed()
|
float Environment::getTimeOfDaySpeed()
|
||||||
{
|
{
|
||||||
MutexAutoLock(this->m_timeofday_lock);
|
MutexAutoLock(this->m_time_lock);
|
||||||
float retval = m_time_of_day_speed;
|
float retval = m_time_of_day_speed;
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
@ -221,11 +222,10 @@ float Environment::getTimeOfDayF()
|
|||||||
|
|
||||||
void Environment::stepTimeOfDay(float dtime)
|
void Environment::stepTimeOfDay(float dtime)
|
||||||
{
|
{
|
||||||
// getTimeOfDaySpeed lock the value we need to prevent MT problems
|
MutexAutoLock(this->m_time_lock);
|
||||||
float day_speed = getTimeOfDaySpeed();
|
|
||||||
|
|
||||||
m_time_counter += dtime;
|
m_time_counter += dtime;
|
||||||
f32 speed = day_speed * 24000./(24.*3600);
|
f32 speed = m_time_of_day_speed * 24000. / (24. * 3600);
|
||||||
u32 units = (u32)(m_time_counter * speed);
|
u32 units = (u32)(m_time_counter * speed);
|
||||||
bool sync_f = false;
|
bool sync_f = false;
|
||||||
if (units > 0) {
|
if (units > 0) {
|
||||||
@ -240,7 +240,7 @@ void Environment::stepTimeOfDay(float dtime)
|
|||||||
m_time_counter -= (f32)units / speed;
|
m_time_counter -= (f32)units / speed;
|
||||||
}
|
}
|
||||||
if (!sync_f) {
|
if (!sync_f) {
|
||||||
m_time_of_day_f += day_speed/24/3600*dtime;
|
m_time_of_day_f += m_time_of_day_speed / 24 / 3600 * dtime;
|
||||||
if (m_time_of_day_f > 1.0)
|
if (m_time_of_day_f > 1.0)
|
||||||
m_time_of_day_f -= 1.0;
|
m_time_of_day_f -= 1.0;
|
||||||
if (m_time_of_day_f < 0.0)
|
if (m_time_of_day_f < 0.0)
|
||||||
|
@ -104,6 +104,11 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
// peer_ids in here should be unique, except that there may be many 0s
|
// peer_ids in here should be unique, except that there may be many 0s
|
||||||
std::vector<Player*> m_players;
|
std::vector<Player*> m_players;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Below: values under m_time_lock
|
||||||
|
*/
|
||||||
// Time of day in milli-hours (0-23999); determines day and night
|
// Time of day in milli-hours (0-23999); determines day and night
|
||||||
u32 m_time_of_day;
|
u32 m_time_of_day;
|
||||||
// Time of day in 0...1
|
// Time of day in 0...1
|
||||||
@ -114,6 +119,9 @@ protected:
|
|||||||
// Overriding the day-night ratio is useful for custom sky visuals
|
// Overriding the day-night ratio is useful for custom sky visuals
|
||||||
bool m_enable_day_night_ratio_override;
|
bool m_enable_day_night_ratio_override;
|
||||||
u32 m_day_night_ratio_override;
|
u32 m_day_night_ratio_override;
|
||||||
|
/*
|
||||||
|
* Above: values under m_time_lock
|
||||||
|
*/
|
||||||
|
|
||||||
/* TODO: Add a callback function so these can be updated when a setting
|
/* TODO: Add a callback function so these can be updated when a setting
|
||||||
* changes. At this point in time it doesn't matter (e.g. /set
|
* changes. At this point in time it doesn't matter (e.g. /set
|
||||||
@ -127,7 +135,6 @@ protected:
|
|||||||
bool m_cache_enable_shaders;
|
bool m_cache_enable_shaders;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Mutex m_timeofday_lock;
|
|
||||||
Mutex m_time_lock;
|
Mutex m_time_lock;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -1249,6 +1249,11 @@ void Server::setTimeOfDay(u32 time)
|
|||||||
m_time_of_day_send_timer = 0;
|
m_time_of_day_send_timer = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
u32 Server::getTimeOfDay()
|
||||||
|
{
|
||||||
|
return m_env->getTimeOfDay();
|
||||||
|
}
|
||||||
|
|
||||||
void Server::onMapEditEvent(MapEditEvent *event)
|
void Server::onMapEditEvent(MapEditEvent *event)
|
||||||
{
|
{
|
||||||
if(m_ignore_map_edit_events)
|
if(m_ignore_map_edit_events)
|
||||||
|
@ -220,8 +220,10 @@ public:
|
|||||||
|
|
||||||
void Send(NetworkPacket* pkt);
|
void Send(NetworkPacket* pkt);
|
||||||
|
|
||||||
// Environment must be locked when called
|
// Both setter and getter need no envlock,
|
||||||
|
// can be called freely from threads
|
||||||
void setTimeOfDay(u32 time);
|
void setTimeOfDay(u32 time);
|
||||||
|
inline u32 getTimeOfDay();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Shall be called with the environment locked.
|
Shall be called with the environment locked.
|
||||||
|
Loading…
Reference in New Issue
Block a user