]> git.lizzy.rs Git - dragonblocks_alpha.git/blobdiff - src/day.c
Fix NULL pointer bug in meshgen
[dragonblocks_alpha.git] / src / day.c
index 659de19cb4a6e424663cd77f1f834af8d9355dc0..24b6b4b4117d3995d759ae13f6686b712b78afd6 100644 (file)
--- a/src/day.c
+++ b/src/day.c
@@ -1,50 +1,41 @@
 #include <math.h>
+#include <time.h>
 #include "day.h"
 #include "util.h"
 
-static time_t time_of_day_offset;
+bool timelapse = false;
+static f64 time_of_day_offset;
 
-f64 get_time_of_day()
+f64 get_unix_time()
 {
        struct timespec ts;
        clock_gettime(CLOCK_REALTIME, &ts);
-
-       return fmod((f64) ts.tv_sec - time_of_day_offset + ts.tv_nsec / 1.0e9, MINUTES_PER_DAY);
+       return ((f64) ts.tv_sec + ts.tv_nsec / 1.0e9) * (timelapse ? 100.0 : 1.0);
 }
-void set_time_of_day(time_t new_time)
+
+f64 get_time_of_day()
 {
-       time_of_day_offset = time(NULL) - new_time;
+       return fmod(get_unix_time() - time_of_day_offset, MINUTES_PER_DAY);
 }
 
-f64 get_sun_angle()
+void set_time_of_day(f64 new_time)
 {
-       return 2.0 * M_PI * (f64) get_time_of_day() / MINUTES_PER_DAY + M_PI;
+       time_of_day_offset = get_unix_time() - new_time;
 }
 
-// workaround for calculating the nth root of a number x, where x may be negative if n is an odd number
-double root(double x, int n)
+f64 get_sun_angle()
 {
-       double sign = 1.0;
-
-       if (x < 0.0) {
-               if (n % 2 == 0)
-                       return 0.0 / 0.0;
-
-               x = -x;
-               sign = -sign;
-       }
-
-       return pow(x, 1.0 / (double) n) * sign;
+       return 2.0 * M_PI * (f64) get_time_of_day() / MINUTES_PER_DAY + M_PI;
 }
 
 f64 get_daylight()
 {
-       return root(cos(get_sun_angle()), 3) / 2.0 + 0.5;
+       return f64_clamp(cos(get_sun_angle()) * 2.0 + 0.5, 0.0, 1.0);
 }
 
 void split_time_of_day(int *hours, int *minutes)
 {
-       time_t time_of_day = get_time_of_day();
+       int time_of_day = get_time_of_day();
 
        *minutes = time_of_day % MINUTES_PER_HOUR;
        *hours = time_of_day / MINUTES_PER_HOUR;