-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaycycle.py
133 lines (118 loc) · 5.12 KB
/
daycycle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""
Gives Ace of Spades a daycycle (using the fog).
Maintainer: hompy
"""
from twisted.internet.task import LoopingCall
from commands import name, admin, add
S_NO_RIGHTS = 'No administrator rights!'
S_TIME_OF_DAY = 'Time of day: {hours:02d}:{minutes:02d}'
S_SPEED = 'Day cycle speed is {multiplier}'
S_SPEED_SET = 'Day cycle speed changed to {multiplier}'
S_STOPPED = 'Day cycle stopped'
@name('dayspeed')
@admin
def day_speed(connection, value = None):
if value is None:
return S_SPEED.format(multiplier = connection.protocol.time_multiplier)
value = float(value)
protocol = connection.protocol
protocol.time_multiplier = value
if value == 0.0:
if protocol.daycycle_loop.running:
protocol.daycycle_loop.stop()
return S_STOPPED
else:
if not protocol.daycycle_loop.running:
protocol.daycycle_loop.start(protocol.day_update_frequency)
return S_SPEED_SET.format(multiplier = value)
from math import modf
@name('daytime')
def day_time(connection, value = None):
if value is not None:
if not connection.admin:
return S_NO_RIGHTS
value = float(value)
if value < 0.0:
raise ValueError()
connection.protocol.current_time = value
connection.protocol.update_day_color()
f, i = modf(connection.protocol.current_time)
return S_TIME_OF_DAY.format(hours = int(i), minutes = int(f * 60))
add(day_speed)
add(day_time)
from pyspades.color import *
def apply_script(protocol, connection, config):
class DayCycleProtocol(protocol):
current_color = None
current_time = None
daycycle_loop = None
day_duration = None
day_update_frequency = None
time_multiplier = None
def __init__(self, *arg, **kw):
protocol.__init__(self, *arg, **kw)
self.daycycle_loop = LoopingCall(self.update_day_color)
self.reset_daycycle()
def reset_daycycle(self):
if not self.daycycle_loop:
return
self.current_color = None
self.current_time = 7.00
self.day_duration = 24 * 60 * 60.00
self.day_update_frequency = 0.1
self.time_multiplier = 48.0
self.day_colors = [
( 0.00, (0.05, 0.05, 0.05), False),
( 4.00, (0.05, 0.77, 0.05), False),
( 5.00, (0.0694, 0.77, 0.78), True),
( 5.30, (0.0361, 0.25, 0.95), False),
( 6.00, (0.56, 0.18, 0.94), False),
( 9.00, (0.5527, 0.24, 0.94), False),
(12.00, (0.5527, 0.41, 0.95), False),
(19.50, (0.56, 0.28, 0.96), False),
(20.00, (0.15, 0.33, 0.87), False),
(20.25, (0.11, 0.49, 0.94), False),
(20.50, (0.1056, 0.69, 1.00), False),
(22.50, (0.1, 0.69, 0.1 ), True),
(23.00, (0.05, 0.05, 0.05), False)]
self.time_step = 24.00 / (self.day_duration /
self.day_update_frequency)
self.target_color_index = 0
self.next_color()
if not self.daycycle_loop.running:
self.daycycle_loop.start(self.day_update_frequency)
def update_day_color(self):
if self.current_time >= 24.00:
self.current_time = wrap(0.00, 24.00, self.current_time)
while (self.current_time < self.start_time or
self.current_time >= self.target_time):
self.next_color()
self.target_time = self.target_time or 24.00
t = ((self.current_time - self.start_time) /
(self.target_time - self.start_time))
if self.hsv_transition:
new_color = interpolate_hsb(self.start_color,
self.target_color, t)
new_color = hsb_to_rgb(*new_color)
else:
new_color = interpolate_rgb(self.start_color,
self.target_color, t)
if (self.current_color is None or
rgb_distance(self.current_color, new_color) > 3):
self.current_color = new_color
self.set_fog_color(self.current_color)
self.current_time += self.time_step * self.time_multiplier
def next_color(self):
self.start_time, self.start_color, _ = (
self.day_colors[self.target_color_index])
self.target_color_index = ((self.target_color_index + 1) %
len(self.day_colors))
self.target_time, self.target_color, self.hsv_transition = (
self.day_colors[self.target_color_index])
if not self.hsv_transition:
self.start_color = hsb_to_rgb(*self.start_color)
self.target_color = hsb_to_rgb(*self.target_color)
def on_map_change(self, map):
self.reset_daycycle()
protocol.on_map_change(self, map)
return DayCycleProtocol, connection