From 20099b254f45410596ae1c6b35285247d201099a Mon Sep 17 00:00:00 2001 From: Fox-McCloud Date: Wed, 22 Jul 2015 18:20:21 -0400 Subject: [PATCH] More Lighting Tweaks and Optimizations --- code/__DEFINES/lighting.dm | 4 -- code/__DEFINES/misc.dm | 7 ++- code/__HELPERS/unsorted.dm | 20 +++++---- code/modules/lighting/light_source.dm | 53 +++++++++++++---------- code/modules/lighting/lighting_overlay.dm | 46 ++++++++++---------- code/modules/lighting/~lighting_undefs.dm | 1 - 6 files changed, 69 insertions(+), 62 deletions(-) diff --git a/code/__DEFINES/lighting.dm b/code/__DEFINES/lighting.dm index 0765980afc7..7fbec83455a 100644 --- a/code/__DEFINES/lighting.dm +++ b/code/__DEFINES/lighting.dm @@ -3,12 +3,8 @@ #define LIGHTING_FALLOFF 1 // type of falloff to use for lighting; 1 for circular, 2 for square #define LIGHTING_LAMBERTIAN 0 // use lambertian shading for light sources #define LIGHTING_HEIGHT 1 // height off the ground of light sources on the pseudo-z-axis, you should probably leave this alone -#define LIGHTING_TRANSITIONS 0 // smooth, animated transitions, similar to /tg/station #define LIGHTING_LAYER 10 // drawing layer for lighting overlays #define LIGHTING_ICON 'icons/effects/lighting_overlay.dmi' // icon used for lighting shading effects -#ifdef LIGHTING_TRANSITIONS -#define LIGHTING_TRANSITION_SPEED (LIGHTING_INTERVAL - 1) -#endif #define LIGHTING_ROUND_VALUE (1 / 128) //Value used to round lumcounts, values smaller than 1/255 don't matter (if they do, thanks sinking points), greater values will make lighting less precise, but in turn increase performance, VERY SLIGHTLY. diff --git a/code/__DEFINES/misc.dm b/code/__DEFINES/misc.dm index 19b3f99029a..6d24ef3099b 100644 --- a/code/__DEFINES/misc.dm +++ b/code/__DEFINES/misc.dm @@ -137,4 +137,9 @@ #define STAGE_THREE 5 #define STAGE_FOUR 7 #define STAGE_FIVE 9 -#define STAGE_SIX 11 //From supermatter shard \ No newline at end of file +#define STAGE_SIX 11 //From supermatter shard + +#define FOR_DVIEW(type, range, center, invis_flags) \ + dview_mob.loc = center; \ + dview_mob.see_invisible = invis_flags; \ + for(type in view(range, dview_mob)) \ No newline at end of file diff --git a/code/__HELPERS/unsorted.dm b/code/__HELPERS/unsorted.dm index 29838e443ba..f6894dc7c70 100644 --- a/code/__HELPERS/unsorted.dm +++ b/code/__HELPERS/unsorted.dm @@ -1685,23 +1685,25 @@ atom/proc/GetTypeInAllContents(typepath) return locate(dest_x,dest_y,dest_z) +var/mob/dview/dview_mob = new + //Version of view() which ignores darkness, because BYOND doesn't have it. /proc/dview(var/range = world.view, var/center, var/invis_flags = 0) if(!center) return - var/global/mob/dview/DV - if(!DV) - DV = new + dview_mob.loc = center - DV.loc = center + dview_mob.see_invisible = invis_flags - DV.see_in_dark = range - DV.see_invisible = invis_flags - - . = view(range, DV) - DV.loc = null + . = view(range, dview_mob) + dview_mob.loc = null /mob/dview invisibility = 101 density = 0 + + anchored = 1 + simulated = 0 + + see_in_dark = 1e6 diff --git a/code/modules/lighting/light_source.dm b/code/modules/lighting/light_source.dm index 2d637b3a749..0b6324a14a4 100644 --- a/code/modules/lighting/light_source.dm +++ b/code/modules/lighting/light_source.dm @@ -127,33 +127,34 @@ lum_g = 1 lum_b = 1 -/datum/light_source/proc/falloff(atom/movable/lighting_overlay/O) - #if LIGHTING_FALLOFF == 1 // circular - . = (O.x - source_turf.x)**2 + (O.y - source_turf.y)**2 + LIGHTING_HEIGHT - - #if LIGHTING_LAMBERTIAN == 1 - . = CLAMP01((1 - CLAMP01(sqrt(.) / max(1,light_range))) * (1 / (sqrt(. + 1)))) - #else - . = 1 - CLAMP01(sqrt(.) / max(1,light_range)) - #endif - - #elif LIGHTING_FALLOFF == 2 // square - . = abs(O.x - source_turf.x) + abs(O.y - source_turf.y) + LIGHTING_HEIGHT - - #if LIGHTING_LAMBERTIAN == 1 - . = CLAMP01((1 - CLAMP01(. / max(1,light_range))) * (1 / (sqrt(.)**2 + ))) - #else - . = 1 - CLAMP01(. / max(1,light_range)) - #endif +#if LIGHTING_FALLOFF == 1 //circular + #define LUM_DISTANCE(swapvar, O, T) swapvar = (O.x - T.x)**2 + (O.y - T.y)**2 + LIGHTING_HEIGHT + #if LIGHTING_LAMBERTIAN == 1 + #define LUM_ATTENUATION(swapvar) swapvar = CLAMP01((1 - CLAMP01(sqrt(swapvar) / light_range)) * (1 / sqrt(swapvar + 1))) + #else + #define LUM_ATTENUATION(swapvar) swapvar = 1 - CLAMP01(sqrt(swapvar) / light_range) #endif +#elif LIGHTING_FALLOFF == 2 //square + #define LUM_DISTANCE(swapvar, O, T) swapvar = abs(O.x - T.x) + abs(O.y - T.y) + LIGHTING_HEIGHT + #if LIGHTING_LAMBERTIAN == 1 + #define LUM_ATTENUATION(swapvar) swapvar = CLAMP01((1 - CLAMP01(swapvar / light_range)) * (1 / sqrt(swapvar**2 + 1))) + #else + #define LUM_ATTENUATION(swapvar) swapvar = CLAMP01(swapvar / light_range) + #endif +#endif + +#define LUM_FALLOFF(swapvar, O, T) \ + LUM_DISTANCE(swapvar, O, T); \ + LUM_ATTENUATION(swapvar); /datum/light_source/proc/apply_lum() applied = 1 if(istype(source_turf)) - for(var/turf/T in dview(light_range, source_turf, INVISIBILITY_LIGHTING)) + FOR_DVIEW(var/turf/T, light_range, source_turf, INVISIBILITY_LIGHTING) if(T.lighting_overlay) - - var/strength = light_power * falloff(T.lighting_overlay) + var/strength + LUM_FALLOFF(strength, T, source_turf) + strength *= light_power if(!strength) //Don't add turfs that aren't affected to the affected turfs. continue @@ -198,7 +199,7 @@ //Stupid dumb copy pasta because BYOND and speed. /datum/light_source/proc/smart_vis_update() var/list/view[0] - for(var/turf/T in dview(light_range, source_turf, INVISIBILITY_LIGHTING)) + FOR_DVIEW(var/turf/T, light_range, source_turf, INVISIBILITY_LIGHTING) view += T //Filter out turfs. //This is the part where we calculate new turfs (if any) @@ -206,7 +207,9 @@ for(var/turf/T in new_turfs) //Big huge copy paste from apply_lum() incoming because screw unreadable defines and screw proc call overhead. if(T.lighting_overlay) - . = light_power * falloff(T.lighting_overlay) + LUM_FALLOFF(., T, source_turf) + . *= light_power + if(!.) //Don't add turfs that aren't affected to the affected turfs. continue @@ -242,3 +245,7 @@ effect_turf.Cut(idx, idx + 1) effect_str.Cut(idx, idx + 1) + +#undef LUM_FALLOFF +#undef LUM_DISTANCE +#undef LUM_ATTENUATION \ No newline at end of file diff --git a/code/modules/lighting/lighting_overlay.dm b/code/modules/lighting/lighting_overlay.dm index 1c726cf3b0d..fc80f9ec9a1 100644 --- a/code/modules/lighting/lighting_overlay.dm +++ b/code/modules/lighting/lighting_overlay.dm @@ -7,7 +7,6 @@ icon = LIGHTING_ICON layer = LIGHTING_LAYER invisibility = INVISIBILITY_LIGHTING - blend_mode = BLEND_MULTIPLY color = "#000000" icon_state = "light1" @@ -62,34 +61,33 @@ lighting_update_overlays += src /atom/movable/lighting_overlay/proc/update_overlay() - var/mx = max(lum_r, lum_g, lum_b) - . = 1 // factor - if(mx > 1) - . = 1/mx - #if LIGHTING_TRANSITIONS == 1 - animate(src, - color = rgb(lum_r * 255 * ., lum_g * 255 * ., lum_b * 255 * .), - LIGHTING_TRANSITION_SPEED - ) - #else - color = rgb(lum_r * 255 * ., lum_g * 255 * ., lum_b * 255 * .) - #endif - var/turf/T = loc if(istype(T)) //Incase we're not on a turf, pool ourselves, something happened. - if(color != "#000000") - T.luminosity = 1 - else //No light, set the turf's luminosity to 0 to remove it from view() - #if LIGHTING_TRANSITIONS == 1 - spawn(LIGHTING_TRANSITION_SPEED) + if(lum_r == lum_g && lum_r == lum_b) //greyscale + blend_mode = BLEND_OVERLAY + if(lum_r <= 0) + T.luminosity = 0 + color = "#000000" + alpha = 255 + else + T.luminosity = 1 + color = "#000000" + alpha = (1 - min(lum_r, 1)) * 255 + else + alpha = 255 + var/mx = max(lum_r, lum_g, lum_b) + . = 1 // factor + if(mx > 1) + . = 1/mx + blend_mode = BLEND_MULTIPLY + color = rgb(lum_r * 255 * ., lum_g * 255 * ., lum_b * 255 * .) + if(color != "#000000") + T.luminosity = 1 + else //No light, set the turf's luminosity to 0 to remove it from view() T.luminosity = 0 - #else - T.luminosity = 0 - #endif - else - warning("A lighting overlay realised it's loc was NOT a turf (actual loc: [loc], [loc.type]) in update_overlay() and got qdel'd!") + warning("A lighting overlay realised its loc was NOT a turf (actual loc: [loc][loc ? ", " + loc.type : ""]) in update_overlay() and got qdel'ed!") qdel(src) /atom/movable/lighting_overlay/singularity_act() diff --git a/code/modules/lighting/~lighting_undefs.dm b/code/modules/lighting/~lighting_undefs.dm index 0f3651e15e5..09100322b8a 100644 --- a/code/modules/lighting/~lighting_undefs.dm +++ b/code/modules/lighting/~lighting_undefs.dm @@ -3,7 +3,6 @@ #undef LIGHTING_FALLOFF #undef LIGHTING_LAMBERTIAN #undef LIGHTING_HEIGHT -#undef LIGHTING_TRANSITIONS #undef LIGHTING_RESOLUTION #undef LIGHTING_LAYER