2024-10-28 15:57:39 +01:00
|
|
|
// Luanti
|
|
|
|
// SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
// Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
2012-03-16 15:34:30 +01:00
|
|
|
|
2017-08-17 22:19:39 +02:00
|
|
|
#pragma once
|
2012-03-16 15:34:30 +01:00
|
|
|
|
2012-12-02 17:02:04 +01:00
|
|
|
inline u32 time_to_daynight_ratio(float time_of_day, bool smooth)
|
2012-03-16 15:34:30 +01:00
|
|
|
{
|
2012-12-02 17:02:04 +01:00
|
|
|
float t = time_of_day;
|
2019-04-08 01:37:52 +02:00
|
|
|
if (t < 0.0f)
|
|
|
|
t += ((int)(-t) / 24000) * 24000.0f;
|
|
|
|
if (t >= 24000.0f)
|
|
|
|
t -= ((int)(t) / 24000) * 24000.0f;
|
|
|
|
if (t > 12000.0f)
|
|
|
|
t = 24000.0f - t;
|
|
|
|
|
|
|
|
const float values[9][2] = {
|
2019-10-18 22:54:44 +02:00
|
|
|
{4250.0f + 125.0f, 175.0f},
|
|
|
|
{4500.0f + 125.0f, 175.0f},
|
2019-04-08 01:37:52 +02:00
|
|
|
{4750.0f + 125.0f, 250.0f},
|
|
|
|
{5000.0f + 125.0f, 350.0f},
|
|
|
|
{5250.0f + 125.0f, 500.0f},
|
|
|
|
{5500.0f + 125.0f, 675.0f},
|
|
|
|
{5750.0f + 125.0f, 875.0f},
|
|
|
|
{6000.0f + 125.0f, 1000.0f},
|
|
|
|
{6250.0f + 125.0f, 1000.0f},
|
2012-12-02 17:02:04 +01:00
|
|
|
};
|
2019-04-08 01:37:52 +02:00
|
|
|
|
|
|
|
if (!smooth) {
|
2012-12-02 17:02:04 +01:00
|
|
|
float lastt = values[0][0];
|
2019-04-08 01:37:52 +02:00
|
|
|
for (u32 i = 1; i < 9; i++) {
|
2012-12-02 17:02:04 +01:00
|
|
|
float t0 = values[i][0];
|
2019-04-08 01:37:52 +02:00
|
|
|
float switch_t = (t0 + lastt) / 2.0f;
|
2012-12-02 17:02:04 +01:00
|
|
|
lastt = t0;
|
2019-04-08 01:37:52 +02:00
|
|
|
if (switch_t <= t)
|
2012-12-02 17:02:04 +01:00
|
|
|
continue;
|
2019-04-08 01:37:52 +02:00
|
|
|
|
2012-12-02 17:02:04 +01:00
|
|
|
return values[i][1];
|
|
|
|
}
|
2012-03-16 15:34:30 +01:00
|
|
|
return 1000;
|
2017-08-17 23:02:50 +02:00
|
|
|
}
|
|
|
|
|
2019-04-08 01:37:52 +02:00
|
|
|
if (t <= 4625.0f) // 4500 + 125
|
2019-04-05 00:30:10 +02:00
|
|
|
return values[0][1];
|
2019-04-08 01:37:52 +02:00
|
|
|
else if (t >= 6125.0f) // 6000 + 125
|
2019-04-05 00:30:10 +02:00
|
|
|
return 1000;
|
|
|
|
|
2019-04-08 01:37:52 +02:00
|
|
|
for (u32 i = 0; i < 9; i++) {
|
|
|
|
if (values[i][0] <= t)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
float td0 = values[i][0] - values[i - 1][0];
|
|
|
|
float f = (t - values[i - 1][0]) / td0;
|
|
|
|
return f * values[i][1] + (1.0f - f) * values[i - 1][1];
|
|
|
|
}
|
|
|
|
return 1000;
|
2012-03-16 15:34:30 +01:00
|
|
|
}
|