Advertisement
Guest User

Untitled

a guest
Jun 16th, 2015
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.93 KB | None | 0 0
  1.  
  2. var
  3. sd_dark_icon = 'sd_darkstates.dmi' // icon used for darkness
  4. sd_dark_shades = 4 // number of icon state in sd_dark_icon
  5. sd_light_layer = 50 // graphics layer for light effects
  6. sd_light_outside = 0 // how bright it is outside
  7. sd_top_luminosity = 0
  8. list
  9. sd_outside_areas = list() // list of outside areas
  10. sd_light_spill_turfs = list() // list of turfs to calculate light spill from
  11.  
  12. proc
  13. sd_OutsideLight(n as num)
  14. // set the brightness of the outside sunlight
  15. if(sd_light_outside == n) return // same level, no update
  16. if(sd_light_outside)
  17. for(var/turf/T in sd_light_spill_turfs)
  18. T.sd_StripSpill()
  19. sd_light_outside = n
  20.  
  21. // make all the outside areas update themselves
  22. for(var/area/A in sd_outside_areas)
  23. A.sd_LightLevel(sd_light_outside + A.sd_light_level,0)
  24. if(n)
  25. for(var/turf/T in sd_light_spill_turfs)
  26. T.sd_ApplySpill()
  27.  
  28. sd_SetDarkIcon(icon, shades)
  29. // reset the darkness icon and number of shades of darkness
  30. sd_dark_icon = icon
  31. sd_dark_shades = shades
  32. // change existing areas
  33. for(var/area/A)
  34. if(A.sd_darkimage) A.sd_LightLevel(A.sd_light_level,0)
  35.  
  36.  
  37. atom
  38. New()
  39. ..()
  40. // if this is not an area and is luminous
  41. if(!isarea(src)&&(luminosity>0))
  42. sd_ApplyLum()
  43.  
  44. Del()
  45. // if this is not an area and is luminous
  46. if(!isarea(src)&&(luminosity>0))
  47. sd_StripLum()
  48. ..()
  49.  
  50. proc
  51. sd_ApplyLum(list/V = view(luminosity,src), center = src)
  52. if(src.luminosity>sd_top_luminosity)
  53. sd_top_luminosity = src.luminosity
  54. // loop through all the turfs in V
  55. for(var/turf/T in V)
  56. /* increase the turf's brightness depending on the
  57. brightness and distance of the lightsource */
  58. T.sd_lumcount += (luminosity-get_dist(center,T))
  59. // update the turf's area
  60. T.sd_LumUpdate()
  61.  
  62. sd_StripLum(list/V = view(luminosity,src), center = src)
  63. // loop through all the turfs in V
  64. for(var/turf/T in V)
  65. /* increase the turf's brightness depending on the
  66. brightness and distance of the lightsource */
  67. T.sd_lumcount -= (luminosity-get_dist(center,T))
  68. // update the turf's area
  69. T.sd_LumUpdate()
  70.  
  71. sd_ApplyLocalLum(list/affected = view(sd_top_luminosity,src))
  72. // Reapplies the lighting effects of all atoms in affected.
  73. for(var/atom/A in affected)
  74. if(A.luminosity) A.sd_ApplyLum()
  75. if(sd_light_outside && (A in sd_light_spill_turfs))
  76. A:sd_ApplySpill()
  77.  
  78. sd_StripLocalLum()
  79. /* strips all local luminosity
  80.  
  81. RETURNS: list of all the luminous atoms stripped
  82.  
  83. IMPORTANT! Each sd_StripLocalLum() call should have a matching
  84. sd_ApplyLocalLum() to restore the local effects. */
  85. var/list/affected = list()
  86. for(var/atom/A in view(sd_top_luminosity,src))
  87. var/turfflag = (isturf(src)?1:0)
  88. if(A.luminosity && (get_dist(src,A) <= A.luminosity + turfflag))
  89. A.sd_StripLum()
  90. affected += A
  91.  
  92. if(sd_light_outside && (A in sd_light_spill_turfs))
  93. A:sd_StripSpill()
  94. affected += A
  95.  
  96. return affected
  97.  
  98. sd_SetLuminosity(new_luminosity as num)
  99. /* This proc should be called everytime you want to change the
  100. luminosity of an atom instead of setting it directly.
  101.  
  102. new_luminosity is the new value for luminosity. */
  103. // var/obj/a_light/a = new()
  104. // animate(a,transform = matrix()*10)
  105. // a.loc = locate(x,y,z)
  106. // a.alpha = new_luminosity * 50
  107. if(luminosity>0)
  108. sd_StripLum()
  109. luminosity = new_luminosity
  110. if(luminosity>0)
  111. sd_ApplyLum()
  112.  
  113. sd_SetOpacity(new_opacity as num)
  114. /* if(opacity != new_opacity)
  115. var/list/affected = sd_StripLocalLum()
  116. opacity = new_opacity
  117. sd_ApplyLocalLum(affected) */
  118. if(opacity == (new_opacity ? 1 : 0)) return
  119. var
  120. list
  121. affected = new
  122. spill
  123. atom/A
  124. turf
  125. T
  126. ATurf
  127. affected = new
  128. for(A in range(sd_top_luminosity,src))
  129. T = A
  130. while(T && !istype(T)) T = T.loc
  131. if(T)
  132. var/list/V = view(A.luminosity,T)
  133. if(!(src in V)) continue
  134. var/turfflag = 0
  135. if(A == T) turfflag = 1
  136. if(A.luminosity && get_dist(A,src)<=A.luminosity+turfflag)
  137. affected[A] = V
  138. if(sd_light_outside && (A in sd_light_spill_turfs))
  139. if(!spill) spill=new
  140. spill[A] = view(sd_light_outside, T)
  141. opacity = new_opacity
  142. if(opacity)
  143. for(A in affected)
  144. ATurf = A
  145. while(ATurf && !istype(ATurf)) ATurf = ATurf.loc
  146. if(ATurf)
  147. for(T in affected[A]-view(A.luminosity, ATurf))
  148. T.sd_lumcount -= (A.luminosity-get_dist(A,T))
  149. T.sd_LumUpdate()
  150. for(A in spill)
  151. if(A.opacity && A!=src) continue
  152. ATurf = A
  153. while(ATurf && !istype(ATurf)) ATurf = ATurf.loc
  154. if(ATurf)
  155. //spill[A] -= view(sd_light_outside, A)
  156. for(T in (A==src)?spill[A]:(spill[A]-view(sd_light_outside,ATurf)))
  157. if(T.loc:sd_outside) continue
  158. T.sd_lumcount -= (sd_light_outside-get_dist(A,T))
  159. T.sd_LumUpdate()
  160. // end new_opacity = 1 block
  161. else
  162. for(A in affected)
  163. ATurf = A
  164. while(ATurf && !istype(ATurf)) ATurf = ATurf.loc
  165. if(ATurf)
  166. for(T in view(A.luminosity, ATurf) - affected[A])
  167. T.sd_lumcount += (A.luminosity-get_dist(A,T))
  168. T.sd_LumUpdate()
  169. for(A in spill)
  170. if(A.opacity) continue
  171. ATurf = A
  172. while(ATurf && !istype(ATurf)) ATurf = ATurf.loc
  173. if(ATurf)
  174. for(T in (A==src)?spill[A]:(view(sd_light_outside, ATurf)-spill[A]))
  175. if(T.loc:sd_outside) continue
  176. T.sd_lumcount += (sd_light_outside-get_dist(A,T))
  177. T.sd_LumUpdate()
  178. // end new_opacity = 0 block
  179.  
  180. turf
  181. var
  182. // set to 1 to have outside light spill indoors from this turf
  183. sd_light_spill = 0
  184. tmp
  185. sd_lumcount = 0 // the brightness of the turf
  186.  
  187. proc
  188. sd_LumReset()
  189. /* Clear local lum, reset this turf's sd_lumcount, and
  190. re-apply local lum*/
  191. var/list/affected = sd_StripLocalLum()
  192. sd_lumcount = 0
  193. sd_ApplyLocalLum(affected)
  194.  
  195. sd_LumUpdate()
  196. var/area/Loc = loc
  197. if(!istype(Loc) || !Loc.sd_lighting) return
  198.  
  199. // change the turf's area depending on its brightness
  200. // restrict light to valid levels
  201. var/light = min(max(sd_lumcount,0),sd_dark_shades)
  202. var/ltag = copytext(Loc.tag,1,findtext(Loc.tag,"sd_L")) + "sd_L[light]"
  203.  
  204. if(Loc.tag!=ltag) //skip if already in this area
  205. var/area/A = locate(ltag) // find an appropriate area
  206. if(!A)
  207. A = new Loc.type() // create area if it wasn't found
  208. // replicate vars
  209. for(var/V in Loc.vars-"contents")
  210. if(issaved(Loc.vars[V])) A.vars[V] = Loc.vars[V]
  211. A.tag = ltag
  212. if(A.sd_outside)
  213. if(!(A in sd_outside_areas))
  214. sd_outside_areas += A
  215. A.sd_light_level = light
  216. A.sd_LightLevel(light + sd_light_outside,0)
  217. else
  218. A.sd_LightLevel(light)
  219. A.contents += src // move the turf into the area
  220.  
  221. sd_ApplySpill()
  222. if(opacity) return
  223. var/oldlum = luminosity
  224. luminosity = sd_light_outside
  225. // loop through all the turfs in V
  226. for(var/turf/T in view(sd_light_outside,src))
  227. var/area/A = T.loc
  228. if(!istype(A) || A.sd_outside) continue
  229. /* increase the turf's brightness depending on the
  230. brightness and distance of the lightsource */
  231. T.sd_lumcount += (sd_light_outside-get_dist(src,T))
  232. // update the turf's area
  233. T.sd_LumUpdate()
  234. luminosity = oldlum
  235.  
  236. sd_StripSpill()
  237. if(opacity) return
  238. var/oldlum = luminosity
  239. luminosity = sd_light_outside
  240. // loop through all the turfs in V
  241. for(var/turf/T in view(sd_light_outside,src))
  242. var/area/A = T.loc
  243. if(!istype(A) || A.sd_outside) continue
  244. /* increase the turf's brightness depending on the
  245. brightness and distance of the lightsource */
  246. T.sd_lumcount -= (sd_light_outside-get_dist(src,T))
  247. // update the turf's area
  248. T.sd_LumUpdate()
  249. luminosity = oldlum
  250.  
  251. New()
  252. ..()
  253. if(sd_light_spill)
  254. sd_light_spill_turfs += src
  255.  
  256. atom/movable/Move() // when something moves
  257. var/turf/oldloc = loc // remember for range calculations
  258. // list turfs in view and luminosity range of old loc
  259. var/list/oldview
  260. if(isturf(loc))
  261. oldview = view(luminosity,loc)
  262. else
  263. oldview = list()
  264.  
  265. . = ..()
  266.  
  267. if(.&&(luminosity>0)) // if the atom moved and is luminous
  268. if(istype(oldloc))
  269. sd_StripLum(oldview,oldloc)
  270. oldloc.sd_lumcount++ // correct "off by 1" error in oldloc
  271. sd_ApplyLum()
  272.  
  273. area
  274. var
  275. /* Turn this flag off to prevent sd_DynamicAreaLighting from affecting
  276. this area */
  277. sd_lighting = 1
  278.  
  279. /* This var determines if an area is outside (affected by sunlight) or
  280. not. */
  281. sd_outside = 0
  282.  
  283. sd_light_level = 0 // the current light level of the area
  284.  
  285. sd_darkimage // tracks the darkness image of the area for easy removal
  286.  
  287. proc
  288. sd_LightLevel(level = sd_light_level as num, keep = 1)
  289. if(!src) return
  290. overlays -= sd_darkimage
  291.  
  292. if(keep) sd_light_level = level
  293.  
  294. level = min(max(level,0),sd_dark_shades) // restrict range
  295.  
  296. if(level > 0)
  297. luminosity = 1
  298. else
  299. luminosity = 0
  300.  
  301. sd_darkimage = image(sd_dark_icon,,num2text(level),sd_light_layer)
  302. overlays += sd_darkimage
  303.  
  304. New()
  305. ..()
  306. if(!tag) tag = "[type]"
  307. spawn(1) // wait a tick
  308. if(sd_lighting)
  309. // see if this area was created by the library
  310. if(!findtext(tag,"sd_L"))
  311. /* show the dark overlay so areas outside of luminous regions
  312. won't be bright as day when they should be dark. */
  313. sd_LightLevel()
  314. if(sd_outside)
  315. sd_outside_areas += src
  316.  
  317. mob
  318. /* extend the mob procs to compensate for sight settings. */
  319. sd_ApplyLum(list/V, center = src)
  320. if(!V)
  321. if(isturf(loc))
  322. V = view(luminosity,loc)
  323. else
  324. V = view(luminosity,src)
  325. . = ..(V, center)
  326.  
  327. sd_StripLum(list/V, center = src)
  328. if(!V)
  329. if(isturf(loc))
  330. V = view(luminosity,loc)
  331. else
  332. V = view(luminosity,src)
  333. . = ..(V, center)
  334.  
  335. sd_ApplyLocalLum(list/affected)
  336. if(!affected)
  337. if(isturf(loc))
  338. affected = view(sd_top_luminosity,loc)
  339. else
  340. affected = view(sd_top_luminosity,src)
  341. . = ..(affected)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement