forked from tgstation/tgstation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ultralight.dm
341 lines (232 loc) · 7.87 KB
/
Ultralight.dm
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//This file was auto-corrected by findeclaration.exe on 25.5.2012 20:42:33
//UltraLight system, by Sukasa
const/ar/UL_LUMINOSITY = 0
const/ar/UL_SQUARELIGHT = 0
const/ar/UL_RGB = 1
const/ar/UL_ROUNDLIGHT = 2
const/ar/UL_I_FALLOFF_SQUARE = 0
const/ar/UL_I_FALLOFF_ROUND = 1
const/ar/UL_I_LUMINOSITY = 0
const/ar/UL_I_RGB = 1
const/ar/UL_I_LIT = 0
const/ar/UL_I_EXTINGUISHED = 1
const/ar/UL_I_ONZERO = 2
ul_LightingEnabled = 1
ul_LightingResolution = 1
ul_Steps = 7
ul_LightingModel = UL_I_RGB
ul_FalloffStyle = UL_I_FALLOFF_ROUND
ul_TopLuminosity = 0
ul_Layer = 10
ul_SuppressLightLevelChanges = 0
list/ul_FastRoot = list(0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7)
proc
ul_Clamp(var/Value)
return min(max(Value, 0), ul_Steps)
atom
var/LuminosityRed = 0
var/LuminosityGreen = 0
var/LuminosityBlue = 0
var/ul_Extinguished = UL_I_ONZERO
proc
ul_SetLuminosity(var/Red, var/Green = Red, var/Blue = Red)
if(LuminosityRed == Red && LuminosityGreen == Green && LuminosityBlue == Blue)
return //No point doing all that work if it won't have any effect anyways...
if (ul_Extinguished == UL_I_EXTINGUISHED)
LuminosityRed = Red
LuminosityGreen = Green
LuminosityBlue = Blue
return
if (ul_IsLuminous())
ul_Extinguish()
LuminosityRed = Red
LuminosityGreen = Green
LuminosityBlue = Blue
ul_Extinguished = UL_I_ONZERO
if (ul_IsLuminous())
ul_Illuminate()
return
ul_Illuminate()
if (ul_Extinguished == UL_I_LIT)
return
ul_Extinguished = UL_I_LIT
ul_UpdateTopLuminosity()
luminosity = ul_Luminosity()
for(var/turf/Affected in view(ul_Luminosity(), src))
var/Falloff = src.ul_FalloffAmount(Affected)
var/DeltaRed = LuminosityRed - Falloff
var/DeltaGreen = LuminosityGreen - Falloff
var/DeltaBlue = LuminosityBlue - Falloff
if(ul_IsLuminous(DeltaRed, DeltaGreen, DeltaBlue))
Affected.LightLevelRed += max(DeltaRed, 0)
Affected.LightLevelGreen += max(DeltaGreen, 0)
Affected.LightLevelBlue += max(DeltaBlue, 0)
Affected.MaxRed += LuminosityRed
Affected.MaxGreen += LuminosityGreen
Affected.MaxBlue += LuminosityBlue
Affected.ul_UpdateLight()
if (ul_SuppressLightLevelChanges == 0)
Affected.ul_LightLevelChanged()
for(var/atom/AffectedAtom in Affected)
AffectedAtom.ul_LightLevelChanged()
return
ul_Extinguish()
if (ul_Extinguished != UL_I_LIT)
return
ul_Extinguished = UL_I_EXTINGUISHED
for(var/turf/Affected in view(ul_Luminosity(), src))
var/Falloff = ul_FalloffAmount(Affected)
var/DeltaRed = LuminosityRed - Falloff
var/DeltaGreen = LuminosityGreen - Falloff
var/DeltaBlue = LuminosityBlue - Falloff
if(ul_IsLuminous(DeltaRed, DeltaGreen, DeltaBlue))
Affected.LightLevelRed -= max(DeltaRed, 0)
Affected.LightLevelGreen -= max(DeltaGreen, 0)
Affected.LightLevelBlue -= max(DeltaBlue, 0)
Affected.MaxRed -= LuminosityRed
Affected.MaxGreen -= LuminosityGreen
Affected.MaxBlue -= LuminosityBlue
Affected.ul_UpdateLight()
if (ul_SuppressLightLevelChanges == 0)
Affected.ul_LightLevelChanged()
for(var/atom/AffectedAtom in Affected)
AffectedAtom.ul_LightLevelChanged()
luminosity = 0
return
ul_FalloffAmount(var/atom/ref)
if (ul_FalloffStyle == UL_I_FALLOFF_ROUND)
var/x = (ref.x - src.x)
var/y = (ref.y - src.y)
if ((x*x + y*y) > ul_FastRoot.len)
for(var/i = ul_FastRoot.len, i <= x*x+y*y, i++)
ul_FastRoot += round(sqrt(x*x+y*y))
return round(ul_LightingResolution * ul_FastRoot[x*x + y*y + 1], 1)
else if (ul_FalloffStyle == UL_I_FALLOFF_SQUARE)
return get_dist(src, ref)
return 0
ul_SetOpacity(var/NewOpacity)
if(opacity != NewOpacity)
var/list/Blanked = ul_BlankLocal()
var/atom/T = src
while(T && !isturf(T))
T = T.loc
opacity = NewOpacity
if(T)
T:LightLevelRed = 0
T:LightLevelGreen = 0
T:LightLevelBlue = 0
ul_UnblankLocal(Blanked)
return
ul_UnblankLocal(var/list/ReApply = view(ul_TopLuminosity, src))
for(var/atom/Light in ReApply)
if(Light.ul_IsLuminous())
Light.ul_Illuminate()
return
ul_BlankLocal()
var/list/Blanked = list( )
var/TurfAdjust = isturf(src) ? 1 : 0
for(var/atom/Affected in view(ul_TopLuminosity, src))
if(Affected.ul_IsLuminous() && Affected.ul_Extinguished == UL_I_LIT && (ul_FalloffAmount(Affected) <= Affected.luminosity + TurfAdjust))
Affected.ul_Extinguish()
Blanked += Affected
return Blanked
ul_UpdateTopLuminosity()
if (ul_TopLuminosity < LuminosityRed)
ul_TopLuminosity = LuminosityRed
if (ul_TopLuminosity < LuminosityGreen)
ul_TopLuminosity = LuminosityGreen
if (ul_TopLuminosity < LuminosityBlue)
ul_TopLuminosity = LuminosityBlue
return
ul_Luminosity()
return max(LuminosityRed, LuminosityGreen, LuminosityBlue)
ul_IsLuminous(var/Red = LuminosityRed, var/Green = LuminosityGreen, var/Blue = LuminosityBlue)
return (Red > 0 || Green > 0 || Blue > 0)
ul_LightLevelChanged()
//Designed for client projects to use. Called on items when the turf they are in has its light level changed
return
New()
..()
if(ul_IsLuminous())
spawn(1)
ul_Illuminate()
return
Del()
if(ul_IsLuminous())
ul_Extinguish()
..()
return
movable
Move()
ul_Extinguish()
..()
ul_Illuminate()
return
turf
var/LightLevelRed = 0
var/LightLevelGreen = 0
var/LightLevelBlue = 0
var/list/MaxRed = list( )
var/list/MaxGreen = list( )
var/list/MaxBlue = list( )
proc
ul_GetRed()
return ul_Clamp(min(LightLevelRed, max(MaxRed)))
ul_GetGreen()
return ul_Clamp(min(LightLevelGreen, max(MaxGreen)))
ul_GetBlue()
return ul_Clamp(min(LightLevelBlue, max(MaxBlue)))
ul_UpdateLight()
var/area/CurrentArea = loc
if(!isarea(CurrentArea) || !CurrentArea.ul_Lighting)
return
var/LightingTag = copytext(CurrentArea.tag, 1, findtext(CurrentArea.tag, ":UL")) + ":UL[ul_GetRed()]_[ul_GetGreen()]_[ul_GetBlue()]"
if(CurrentArea.tag != LightingTag)
var/area/NewArea = locate(LightingTag)
if(!NewArea)
NewArea = new CurrentArea.type()
NewArea.tag = LightingTag
for(var/V in CurrentArea.vars - "contents")
if(issaved(CurrentArea.vars[V]))
NewArea.vars[V] = CurrentArea.vars[V]
NewArea.tag = LightingTag
NewArea.ul_Light(ul_GetRed(), ul_GetGreen(), ul_GetBlue())
NewArea.contents += src
return
ul_Recalculate()
ul_SuppressLightLevelChanges++
var/list/Lights = ul_BlankLocal()
LightLevelRed = 0
LightLevelGreen = 0
LightLevelBlue = 0
ul_UnblankLocal(Lights)
ul_SuppressLightLevelChanges--
return
area
var/ul_Overlay = null
var/ul_Lighting = 1
var/LightLevelRed = 0
var/LightLevelGreen = 0
var/LightLevelBlue = 0
proc
ul_Light(var/Red = LightLevelRed, var/Green = LightLevelGreen, var/Blue = LightLevelBlue)
if(!src || !src.ul_Lighting)
return
overlays -= ul_Overlay
LightLevelRed = Red
LightLevelGreen = Green
LightLevelBlue = Blue
luminosity = ul_IsLuminous(LightLevelRed, LightLevelGreen, LightLevelBlue)
ul_Overlay = image('icons/effects/ULIcons.dmi', , num2text(LightLevelRed) + "-" + num2text(LightLevelGreen) + "-" + num2text(LightLevelBlue), ul_Layer)
overlays += ul_Overlay
return
ul_Prep()
if(!tag)
tag = "[type]"
if(ul_Lighting)
if(!findtext(tag,":UL"))
ul_Light()
//world.log << tag
return