Optimization/Rewrite of Radiation Controller

* The performance of the radiation controller as-is was not fast enough for inclusion in production servers, but it has some nice featuers, so rewrote it to be more performant.
* Instead of storing the radiation strength for every turf, we only store the sources of radiation, and calculate the strength only for mobs who might be in range.
   * Old method was ray-tracing to every turf in range whether anything was there to be irradiated or not.  Could be hundreds of turfs.  New method only lazily calcualtes strength at a turf if we actually need to know it.   Often times this is zero turfs if nobody is standing in engineering.
  * Removed the automatic processing of objects with "rad_power" set.  Objects are responsible for calling the repository to create/update their radiation sources.   Saves some extra overhead that in practice was redundant with other process controllers.
  * Also tweaked to be more respectful of qdel'd objects and added some comments.
This commit is contained in:
Leshana
2017-05-24 16:37:06 -04:00
parent b1250a4391
commit 34c73dab69
16 changed files with 172 additions and 150 deletions

View File

@@ -1,114 +1,132 @@
//#define RADDBG
var/repository/radiation/radiation_repository = new()
var/list/to_process = list()
var/global/repository/radiation/radiation_repository = new()
/repository/radiation
var/list/sources = list() //All the radiation sources we know about
var/list/irradiated_turfs = list()
var/list/irradiated_mobs = list()
var/list/resistance_cache = list()
var/list/sources = list() // all radiation source datums
var/list/sources_assoc = list() // Sources indexed by turf for de-duplication.
var/list/resistance_cache = list() // Cache of turf's radiation resistance.
/repository/radiation/proc/report_rads(var/turf/T as turf)
return irradiated_turfs[T]
// Describes a point source of radiation. Created either in response to a pulse of radiation, or over an irradiated atom.
// Sources will decay over time, unless something is renewing their power!
/datum/radiation_source
var/turf/source_turf // Location of the radiation source.
var/rad_power // Strength of the radiation being emitted.
var/decay = TRUE // True for automatic decay. False if owner promises to handle it (i.e. supermatter)
var/respect_maint = FALSE // True for not affecting RAD_SHIELDED areas.
var/flat = FALSE // True for power falloff with distance.
var/range // Cached maximum range, used for quick checks against mobs.
/datum/radiation_source/Destroy()
radiation_repository.sources -= src
if(radiation_repository.sources_assoc[src.source_turf] == src)
radiation_repository.sources -= src.source_turf
. = ..()
/datum/radiation_source/proc/update_rad_power(var/new_power = null)
if(new_power != null && new_power != rad_power)
rad_power = new_power
. = 1
if(. && !flat)
range = min(round(sqrt(rad_power / config.radiation_lower_limit)), 31)
// Ray trace from all active radiation sources to T and return the strongest effect.
/repository/radiation/proc/get_rads_at_turf(var/turf/T)
if(!istype(T)) return 0
. = 0
for(var/value in sources)
var/datum/radiation_source/source = value
if(source.rad_power < .)
continue // Already being affected by a stronger source
var/dist = get_dist(source.source_turf, T)
if(dist > source.range)
continue // Too far to possibly affect
if(source.respect_maint)
var/atom/A = T.loc
if(A.flags & RAD_SHIELDED)
continue // In shielded area
if(source.flat)
. = max(., source.rad_power)
continue // No need to ray trace for flat field
// Okay, now ray trace to find resistence!
var/turf/origin = source.source_turf
var/working = source.rad_power
while(origin != T)
origin = get_step_towards(origin, T) //Raytracing
if(!(origin in resistance_cache)) //Only get the resistance if we don't already know it.
origin.calc_rad_resistance()
working = max((working - (origin.cached_rad_resistance * config.radiation_resistance_multiplier)), 0)
if(working <= .)
break // Already affected by a stronger source (or its zero...)
. = max((working * (1 / (dist ** 2))), .) //Butchered version of the inverse square law. Works for this purpose
// Add a radiation source instance to the repository. It will override any existing source on the same turf.
/repository/radiation/proc/add_source(var/datum/radiation_source/S)
var/datum/radiation_source/existing = sources_assoc[S.source_turf]
if(existing)
qdel(existing)
sources += S
sources_assoc[S.source_turf] = S
// Creates a temporary radiation source that will decay
/repository/radiation/proc/radiate(source, power) //Sends out a radiation pulse, taking walls into account
if(!(source && power)) //Sanity checking
return
var/datum/radiation_source/S = new()
S.source_turf = get_turf(source)
S.update_rad_power(power)
add_source(S)
return S
var/range = min(round(sqrt(power / config.radiation_lower_limit)), 31)
var/turf/epicentre = get_turf(source)
to_process = list()
range = min(epicentre.x, world.maxx - epicentre.x, epicentre.y, world.maxy - epicentre.y, range)
to_process = trange(range, epicentre)
to_process[epicentre] = power
for(var/turf/spot in to_process)
var/turf/origin = get_turf(epicentre)
var/working = power
while(origin != spot)
origin = get_step_towards(origin, spot) //Raytracing
if(!(origin in resistance_cache)) //Only get the resistance if we don't already know it.
origin.calc_rad_resistance()
working = max((working - (origin.rad_resistance * config.radiation_resistance_multiplier)), 0)
if(!working)
break
if(!to_process[origin])
to_process[origin] = working
else
to_process[origin] = max(to_process[origin], working)
for(var/turf/spot in to_process)
irradiated_turfs[spot] = max(((to_process[spot]) * (1 / (get_dist(epicentre, spot) ** 2))), irradiated_turfs[spot]) //Butchered version of the inverse square law. Works for this purpose
#ifdef RADDBG
var/x = Clamp( irradiated_turfs[spot], 0, 255)
spot.color = rgb(5,x,5)
#endif
/repository/radiation/proc/flat_radiate(source, power, range, var/respect_maint=0) //Sets the radiation in a range to a constant value.
// Sets the radiation in a range to a constant value.
/repository/radiation/proc/flat_radiate(source, power, range, var/respect_maint = FALSE)
if(!(source && power && range))
return
var/turf/epicentre = get_turf(source)
range = min(epicentre.x, world.maxx - epicentre.x, epicentre.y, world.maxy - epicentre.y, range)
if(!respect_maint)
for(var/turf/T in trange(range, epicentre))
irradiated_turfs[T] = max(power, irradiated_turfs[T])
else
for(var/turf/T in trange(range, epicentre))
var/area/A = T.loc
if(A.flags & RAD_SHIELDED)
continue
irradiated_turfs[T] = max(power, irradiated_turfs[T])
var/datum/radiation_source/S = new()
S.flat = TRUE
S.range = range
S.respect_maint = respect_maint
S.source_turf = get_turf(source)
S.update_rad_power(power)
add_source(S)
return S
/repository/radiation/proc/z_radiate(var/atom/source, power, var/respect_maint=0) //Irradiates a full Z-level. Hacky way of doing it, but not too expensive.
// Irradiates a full Z-level. Hacky way of doing it, but not too expensive.
/repository/radiation/proc/z_radiate(var/atom/source, power, var/respect_maint = FALSE)
if(!(power && source))
return
var/turf/epicentre = locate(round(world.maxx / 2), round(world.maxy / 2), source.z)
flat_radiate(epicentre, power, world.maxx, respect_maint)
/turf
var/cached_rad_resistance = 0
/turf/proc/calc_rad_resistance()
rad_resistance = 0
cached_rad_resistance = 0
for(var/obj/O in src.contents)
if(O.rad_resistance) //Override
rad_resistance += O.rad_resistance
cached_rad_resistance += O.rad_resistance
else if(O.density) //So doors don't get counted
else if(O.density) //So open doors don't get counted
var/material/M = O.get_material()
if(!M) continue
rad_resistance += M.weight
cached_rad_resistance += M.weight
// Looks like storing the contents length is meant to be a basic check if the cache is stale due to items enter/exiting. Better than nothing so I'm leaving it as is. ~Leshana
radiation_repository.resistance_cache[src] = (length(contents) + 1)
/turf/simulated/wall/calc_rad_resistance()
radiation_repository.resistance_cache[src] = (length(contents) + 1)
rad_resistance = (density ? material.weight : 0)
cached_rad_resistance = (density ? material.weight : 0)
/atom
var/rad_power = 0
var/rad_resistance = 0
/obj
var/rad_resistance = 0 // Allow overriding rad resistance
/atom/Destroy()
if(rad_power)
radiation_repository.sources.Remove(src)
. = ..()
/atom/proc/update_radiation() //For VV'ing something to make it radioactive at runtime
if((rad_power) && (!(src in radiation_repository.sources)))
radiation_repository.sources.Add(src)
else if((!rad_power) && (src in radiation_repository.sources))
radiation_repository.sources.Remove(src)
/atom/proc/rad_act(var/severity) //If people expand the system, this may be useful. Here as a placeholder until then
// If people expand the system, this may be useful. Here as a placeholder until then
/atom/proc/rad_act(var/severity)
return 1
/mob/living/rad_act(var/severity)
if(severity)
src.apply_effect(severity, IRRADIATE, src.getarmor(null, "rad"))
for(var/obj/I in src)
for(var/atom/I in src)
I.rad_act(severity)
//#undef RADDBG