Merge pull request #8590 from Spookerton/spkrtn/fix/lingering-overlay-odds

fix overlay odds
This commit is contained in:
Atermonera
2022-04-30 15:30:46 -08:00
committed by GitHub
3 changed files with 381 additions and 385 deletions

View File

@@ -1,242 +1,239 @@
SUBSYSTEM_DEF(overlays)
name = "Overlay"
flags = SS_TICKER
wait = 1 // SS_TICKER - Ticks
priority = FIRE_PRIORITY_OVERLAYS
init_order = INIT_ORDER_OVERLAY
/// The queue of atoms that need overlay updates.
var/static/tmp/list/queue = list()
/// An image used to create appearances to be cached.
var/static/tmp/image/renderer = new
/// A list([icon] = list([state] = [appearance], ...), ...) cache of appearances.
var/static/tmp/list/state_cache = list()
/// A list([icon] = [appearance], ...) cache of appearances.
var/static/tmp/list/icon_cache = list()
/// The number of appearances currently cached.
var/static/tmp/cache_size = 0
/datum/controller/subsystem/overlays/Recover()
queue.Cut()
renderer = new
state_cache.Cut()
icon_cache.Cut()
cache_size = 0
/datum/controller/subsystem/overlays/Initialize(timeofday)
fire(FALSE, TRUE)
/datum/controller/subsystem/overlays/stat_entry()
..("Queued Atoms: [queue.len], Cache Size: [cache_size]")
/datum/controller/subsystem/overlays/fire(resumed, no_mc_tick)
var/count = 1
for (var/atom/atom as anything in queue)
++count
atom?.UpdateOverlays()
if (no_mc_tick)
CHECK_TICK
else if (MC_TICK_CHECK)
queue.Cut(count)
return
queue.Cut()
/datum/controller/subsystem/overlays/proc/GetStateAppearance(icon, state)
var/list/subcache = state_cache[icon]
if (!subcache)
subcache = list()
state_cache[icon] = subcache
if (!subcache[state])
renderer.icon = icon
renderer.icon_state = state
subcache[state] = renderer.appearance
++cache_size
return subcache[state]
/datum/controller/subsystem/overlays/proc/GetIconAppearance(icon)
if (!icon_cache[icon])
renderer.icon = icon
icon_cache[icon] = renderer.appearance
++cache_size
return icon_cache[icon]
/datum/controller/subsystem/overlays/proc/GetAppearanceList(atom/subject, list/sources)
if (!sources)
return list()
if (!islist(sources))
sources = list(sources)
var/image/image
var/list/result = list()
var/icon/icon = subject.icon
for (var/atom/entry as anything in sources)
if (!entry)
continue
else if (istext(entry))
result += GetStateAppearance(icon, entry)
else if (isicon(entry))
result += GetIconAppearance(entry)
else
if (isloc(entry))
if (entry.flags & OVERLAY_QUEUED)
entry.ImmediateOverlayUpdate()
renderer.appearance = entry
if (!ispath(entry))
image = entry
renderer.dir = image.dir
result += renderer.appearance
return result
/// Enqueues the atom for an overlay update if not already queued
/atom/proc/QueueOverlayUpdate()
if (flags & OVERLAY_QUEUED)
return
SSoverlays.queue += src
flags |= OVERLAY_QUEUED
/// Builds the atom's overlay state from caches
/atom/proc/UpdateOverlays()
if (gc_destroyed)
if (length(overlays))
overlays.Cut()
return
flags &= ~OVERLAY_QUEUED
if (length(priority_overlays))
if (length(our_overlays))
overlays = priority_overlays + our_overlays
else
overlays = priority_overlays
else if (length(our_overlays))
overlays = our_overlays
else
overlays.Cut()
/// Immediately runs an overlay update and dequeues the atom
/atom/proc/ImmediateOverlayUpdate()
SSoverlays.queue -= src
UpdateOverlays()
/// Clears the atom's overlay cache(s) and queues an update if needed
/atom/proc/cut_overlays(priority)
if (priority)
if (!length(priority_overlays))
return
priority_overlays.Cut()
QueueOverlayUpdate()
else if (length(our_overlays))
our_overlays.Cut()
QueueOverlayUpdate()
/**
* Adds specific overlay(s) to the atom.
* It is designed so any of the types allowed to be added to /atom/overlays can be added here too. More details below.
*
* @param add The overlay(s) to add. These may be
* - A string: In which case it is treated as an icon_state of the atom's icon.
* - An icon: It is treated as an icon.
* - An atom: Its own overlays are compiled and then it's appearance is added. (Meaning its current apperance is frozen).
* - An image: Image's apperance is added (i.e. subsequently editing the image will not edit the overlay)
* - A type path: Added to overlays as is. Does whatever it is BYOND does when you add paths to overlays.
* - Or a list containing any of the above.
* @param priority The overlays are added to the "priority" list istead of the normal one.
*/
/atom/proc/add_overlay(list/add, priority)
if (!add)
return
add = SSoverlays.GetAppearanceList(src, add)
if (!length(add))
return
if (priority)
if (priority_overlays)
priority_overlays += add
else
priority_overlays = add
else if (our_overlays)
our_overlays += add
else
our_overlays = add
QueueOverlayUpdate()
/**
* Removes specific overlay(s) from the atom. Usually does not remove them from "priority" overlays.
*
* @param overlays The overlays to removed, type can be anything that is allowed for add_overlay().
* @param priority If true, also will remove them from the "priority" overlays.
*/
/atom/proc/cut_overlay(list/cut, priority)
if (!cut)
return
cut = SSoverlays.GetAppearanceList(src, cut)
if (!length(cut))
return
var/update
if (priority && length(priority_overlays))
priority_overlays -= cut
update = TRUE
if (length(our_overlays))
our_overlays -= cut
update = TRUE
if (update)
QueueOverlayUpdate()
/**
* Copy the overlays from another atom, either replacing all of ours or appending to our existing overlays.
* Note: This copies only the normal overlays, not the "priority" overlays.
*
* @param other The atom to copy overlays from.
* @param cut_old If true, all of our overlays will be *replaced* by the other's. If other is null, that means cutting all ours.
*/
/atom/proc/copy_overlays(atom/other, cut)
if (!other)
if (cut)
cut_overlays()
return
if (!length(other.our_overlays))
if (cut)
cut_overlays()
return
if (cut || !length(our_overlays))
our_overlays = other.our_overlays.Copy()
else
our_overlays |= other.our_overlays
QueueOverlayUpdate()
/**
* Returns a list of overlays that the target atom has
*
* @param priority If true, returns priority overlays as well
* @param special If true, returns special overlays like emissives and em_blockers
*/
/proc/get_overlays(atom/other, priority, special)
var/list/including = list()
if (!other)
return including
for (var/image/I as anything in other.our_overlays)
if (!special && I.plane > 0)
continue
including += I
if (!priority)
return including
for (var/image/I as anything in other.priority_overlays)
if (!special && I.plane > 0)
continue
including += I
return including
SUBSYSTEM_DEF(overlays)
name = "Overlay"
flags = SS_TICKER
wait = 1 // SS_TICKER - Ticks
priority = FIRE_PRIORITY_OVERLAYS
init_order = INIT_ORDER_OVERLAY
/// The queue of atoms that need overlay updates.
var/static/tmp/list/queue = list()
/// A list([icon] = list([state] = [appearance], ...), ...) cache of appearances.
var/static/tmp/list/state_cache = list()
/// A list([icon] = [appearance], ...) cache of appearances.
var/static/tmp/list/icon_cache = list()
/// The number of appearances currently cached.
var/static/tmp/cache_size = 0
/datum/controller/subsystem/overlays/Recover()
queue.Cut()
state_cache.Cut()
icon_cache.Cut()
cache_size = 0
for (var/atom/atom)
atom.flags &= ~OVERLAY_QUEUED
CHECK_TICK
/datum/controller/subsystem/overlays/Initialize(timeofday)
fire(FALSE, TRUE)
/datum/controller/subsystem/overlays/stat_entry()
..("Queued Atoms: [queue.len], Cache Size: [cache_size]")
/datum/controller/subsystem/overlays/fire(resumed, no_mc_tick)
var/count = 1
for (var/atom/atom as anything in queue)
++count
atom?.UpdateOverlays()
if (no_mc_tick)
CHECK_TICK
else if (MC_TICK_CHECK)
queue.Cut(count)
return
queue.Cut()
/datum/controller/subsystem/overlays/proc/GetStateAppearance(icon, state)
var/list/subcache = state_cache[icon]
if (!subcache)
subcache = list()
state_cache[icon] = subcache
if (!subcache[state])
var/image/image = new (icon, null, state)
subcache[state] = image.appearance
++cache_size
return subcache[state]
/datum/controller/subsystem/overlays/proc/GetIconAppearance(icon)
if (!icon_cache[icon])
var/image/image = new (icon)
icon_cache[icon] = image.appearance
++cache_size
return icon_cache[icon]
/datum/controller/subsystem/overlays/proc/GetAppearanceList(atom/subject, list/sources)
if (!sources)
return list()
if (!islist(sources))
sources = list(sources)
var/list/result = list()
var/icon/icon = subject.icon
for (var/atom/entry as anything in sources)
if (!entry)
continue
else if (istext(entry))
result += GetStateAppearance(icon, entry)
else if (isicon(entry))
result += GetIconAppearance(entry)
else
if (isloc(entry))
if (entry.flags & OVERLAY_QUEUED)
entry.ImmediateOverlayUpdate()
if (!ispath(entry))
result += entry.appearance
else
var/image/image = entry
result += image.appearance
return result
/// Enqueues the atom for an overlay update if not already queued
/atom/proc/QueueOverlayUpdate()
if (flags & OVERLAY_QUEUED)
return
SSoverlays.queue += src
flags |= OVERLAY_QUEUED
/// Builds the atom's overlay state from caches
/atom/proc/UpdateOverlays()
if (gc_destroyed)
if (length(overlays))
overlays.Cut()
return
flags &= ~OVERLAY_QUEUED
if (length(priority_overlays))
if (length(our_overlays))
overlays = priority_overlays + our_overlays
else
overlays = priority_overlays
else if (length(our_overlays))
overlays = our_overlays
else
overlays.Cut()
/// Immediately runs an overlay update and dequeues the atom
/atom/proc/ImmediateOverlayUpdate()
SSoverlays.queue -= src
UpdateOverlays()
/// Clears the atom's overlay cache(s) and queues an update if needed
/atom/proc/cut_overlays(priority)
if (priority)
if (!length(priority_overlays))
return
priority_overlays.Cut()
QueueOverlayUpdate()
else if (length(our_overlays))
our_overlays.Cut()
QueueOverlayUpdate()
/**
* Adds specific overlay(s) to the atom.
* It is designed so any of the types allowed to be added to /atom/overlays can be added here too. More details below.
*
* @param add The overlay(s) to add. These may be
* - A string: In which case it is treated as an icon_state of the atom's icon.
* - An icon: It is treated as an icon.
* - An atom: Its own overlays are compiled and then it's appearance is added. (Meaning its current apperance is frozen).
* - An image: Image's apperance is added (i.e. subsequently editing the image will not edit the overlay)
* - A type path: Added to overlays as is. Does whatever it is BYOND does when you add paths to overlays.
* - Or a list containing any of the above.
* @param priority The overlays are added to the "priority" list istead of the normal one.
*/
/atom/proc/add_overlay(list/add, priority)
if (!add)
return
add = SSoverlays.GetAppearanceList(src, add)
if (!length(add))
return
if (priority)
if (priority_overlays)
priority_overlays += add
else
priority_overlays = add
else if (our_overlays)
our_overlays += add
else
our_overlays = add
QueueOverlayUpdate()
/**
* Removes specific overlay(s) from the atom. Usually does not remove them from "priority" overlays.
*
* @param overlays The overlays to removed, type can be anything that is allowed for add_overlay().
* @param priority If true, also will remove them from the "priority" overlays.
*/
/atom/proc/cut_overlay(list/cut, priority)
if (!cut)
return
cut = SSoverlays.GetAppearanceList(src, cut)
if (!length(cut))
return
var/update
if (priority && length(priority_overlays))
priority_overlays -= cut
update = TRUE
if (length(our_overlays))
our_overlays -= cut
update = TRUE
if (update)
QueueOverlayUpdate()
/**
* Copy the overlays from another atom, either replacing all of ours or appending to our existing overlays.
* Note: This copies only the normal overlays, not the "priority" overlays.
*
* @param other The atom to copy overlays from.
* @param cut_old If true, all of our overlays will be *replaced* by the other's. If other is null, that means cutting all ours.
*/
/atom/proc/copy_overlays(atom/other, cut)
if (!other)
if (cut)
cut_overlays()
return
if (!length(other.our_overlays))
if (cut)
cut_overlays()
return
if (cut || !length(our_overlays))
our_overlays = other.our_overlays.Copy()
else
our_overlays |= other.our_overlays
QueueOverlayUpdate()
/**
* Returns a list of overlays that the target atom has
*
* @param priority If true, returns priority overlays as well
* @param special If true, returns special overlays like emissives and em_blockers
*/
/proc/get_overlays(atom/other, priority, special)
var/list/including = list()
if (!other)
return including
for (var/image/I as anything in other.our_overlays)
if (!special && I.plane > 0)
continue
including += I
if (!priority)
return including
for (var/image/I as anything in other.priority_overlays)
if (!special && I.plane > 0)
continue
including += I
return including

View File

@@ -1,51 +1,50 @@
/obj/structure/closet/secure_closet/guncabinet
name = "gun cabinet"
icon = 'icons/obj/guncabinet.dmi'
icon_state = "base"
req_one_access = list(access_armory)
closet_appearance = null
/obj/structure/closet/secure_closet/guncabinet/Initialize()
. = ..()
update_icon()
/obj/structure/closet/secure_closet/guncabinet/toggle()
..()
update_icon()
/obj/structure/closet/secure_closet/guncabinet/update_icon()
cut_overlays()
if(opened)
add_overlay("door_open")
else
var/lazors = 0
var/shottas = 0
for (var/obj/item/gun/G in contents)
if (istype(G, /obj/item/gun/energy))
lazors++
if (istype(G, /obj/item/gun/projectile))
shottas++
for (var/i = 0 to 2)
if(lazors || shottas) // only make icons if we have one of the two types.
var/image/gun = image(icon(src.icon))
if (lazors > shottas)
lazors--
gun.icon_state = "laser"
else if (shottas)
shottas--
gun.icon_state = "projectile"
gun.pixel_x = i*4
add_overlay(gun)
add_overlay("door")
if(sealed)
add_overlay("sealed")
if(broken)
add_overlay("broken")
else if (locked)
add_overlay("locked")
else
add_overlay("open")
/obj/structure/closet/secure_closet/guncabinet
name = "gun cabinet"
icon = 'icons/obj/guncabinet.dmi'
icon_state = "base"
req_one_access = list(access_armory)
closet_appearance = null
/obj/structure/closet/secure_closet/guncabinet/Initialize()
. = ..()
update_icon()
/obj/structure/closet/secure_closet/guncabinet/toggle()
..()
update_icon()
/obj/structure/closet/secure_closet/guncabinet/update_icon()
cut_overlays()
var/list/add = list()
if (!opened)
var/energy_count = 0
var/projectile_count = 0
for (var/obj/item/gun/gun in contents)
if (istype(gun, /obj/item/gun/energy))
++energy_count
else if(istype(gun, /obj/item/gun/projectile))
++projectile_count
for (var/i = 0 to 2)
if (!energy_count && !projectile_count)
break
var/image/image = new (icon)
image.pixel_x = i * 4
if (energy_count > projectile_count)
image.icon_state = "laser"
--energy_count
else if (projectile_count)
image.icon_state = "projectile"
--projectile_count
add += image
add += "door"
if (sealed)
add += "sealed"
if (broken)
add += "broken"
else if (locked)
add += "locked"
else
add += "open"
else
add += "door_open"
add_overlay(add)

View File

@@ -1,92 +1,92 @@
//Refreshes the icon and sets the luminosity
/obj/machinery/portable_atmospherics/hydroponics/update_icon()
// Update name.
if(seed)
if(mechanical)
name = "[base_name] ([seed.seed_name])"
else
name = "[seed.seed_name]"
else
name = initial(name)
if(labelled)
name += " ([labelled])"
overlays.Cut()
// Updates the plant overlay.
if(!isnull(seed))
if(mechanical && health <= (seed.get_trait(TRAIT_ENDURANCE) / 2))
add_overlay("over_lowhealth3")
if(dead)
var/ikey = "[seed.get_trait(TRAIT_PLANT_ICON)]-dead"
var/image/dead_overlay = plant_controller.plant_icon_cache["[ikey]"]
if(!dead_overlay)
dead_overlay = image('icons/obj/hydroponics_growing.dmi', "[ikey]")
dead_overlay.color = DEAD_PLANT_COLOUR
add_overlay(dead_overlay)
else
if(!seed.growth_stages)
seed.update_growth_stages()
if(!seed.growth_stages)
to_world("<span class='danger'>Seed type [seed.get_trait(TRAIT_PLANT_ICON)] cannot find a growth stage value.</span>")
return
var/overlay_stage = 1
if(age >= seed.get_trait(TRAIT_MATURATION))
overlay_stage = seed.growth_stages
else
var/maturation = seed.get_trait(TRAIT_MATURATION)/seed.growth_stages
if(maturation < 1)
maturation = 1
overlay_stage = maturation ? max(1,round(age/maturation)) : 1
var/ikey = "[seed.get_trait(TRAIT_PLANT_ICON)]-[overlay_stage]"
var/image/plant_overlay = plant_controller.plant_icon_cache["[ikey]-[seed.get_trait(TRAIT_PLANT_COLOUR)]"]
if(frozen == 1)
plant_overlay = image('icons/obj/hydroponics_growing.dmi', "[ikey]")
plant_overlay.color = FROZEN_PLANT_COLOUR
if(!plant_overlay)
plant_overlay = image('icons/obj/hydroponics_growing.dmi', "[ikey]")
plant_overlay.color = seed.get_trait(TRAIT_PLANT_COLOUR)
plant_controller.plant_icon_cache["[ikey]-[seed.get_trait(TRAIT_PLANT_COLOUR)]"] = plant_overlay
add_overlay(plant_overlay)
if(harvest && overlay_stage == seed.growth_stages)
ikey = "[seed.get_trait(TRAIT_PRODUCT_ICON)]"
var/image/harvest_overlay = plant_controller.plant_icon_cache["product-[ikey]-[seed.get_trait(TRAIT_PLANT_COLOUR)]"]
if(!harvest_overlay)
harvest_overlay = image('icons/obj/hydroponics_products.dmi', "[ikey]")
harvest_overlay.color = seed.get_trait(TRAIT_PRODUCT_COLOUR)
plant_controller.plant_icon_cache["product-[ikey]-[seed.get_trait(TRAIT_PRODUCT_COLOUR)]"] = harvest_overlay
add_overlay(harvest_overlay)
//Draw the cover.
if(closed_system)
add_overlay("hydrocover")
//Updated the various alert icons.
if(mechanical)
var/list/add = list()
if(waterlevel <= 10)
add += "over_lowwater3"
if(nutrilevel <= 2)
add += "over_lownutri3"
if(weedlevel >= 5 || pestlevel >= 5 || toxins >= 40)
add += "over_alert3"
if(harvest)
add += "over_harvest3"
if(frozen)
add += "over_frozen3"
add_overlay(add)
// Update bioluminescence.
if(seed)
if(seed.get_trait(TRAIT_BIOLUM))
var/clr
if(seed.get_trait(TRAIT_BIOLUM_COLOUR))
clr = seed.get_trait(TRAIT_BIOLUM_COLOUR)
set_light(round(seed.get_trait(TRAIT_POTENCY)/10), l_color = clr)
return
set_light(0)
return
//Refreshes the icon and sets the luminosity
/obj/machinery/portable_atmospherics/hydroponics/update_icon()
// Update name.
if(seed)
if(mechanical)
name = "[base_name] ([seed.seed_name])"
else
name = "[seed.seed_name]"
else
name = initial(name)
if(labelled)
name += " ([labelled])"
cut_overlays()
// Updates the plant overlay.
if(!isnull(seed))
if(mechanical && health <= (seed.get_trait(TRAIT_ENDURANCE) / 2))
add_overlay("over_lowhealth3")
if(dead)
var/ikey = "[seed.get_trait(TRAIT_PLANT_ICON)]-dead"
var/image/dead_overlay = plant_controller.plant_icon_cache["[ikey]"]
if(!dead_overlay)
dead_overlay = image('icons/obj/hydroponics_growing.dmi', "[ikey]")
dead_overlay.color = DEAD_PLANT_COLOUR
add_overlay(dead_overlay)
else
if(!seed.growth_stages)
seed.update_growth_stages()
if(!seed.growth_stages)
to_world("<span class='danger'>Seed type [seed.get_trait(TRAIT_PLANT_ICON)] cannot find a growth stage value.</span>")
return
var/overlay_stage = 1
if(age >= seed.get_trait(TRAIT_MATURATION))
overlay_stage = seed.growth_stages
else
var/maturation = seed.get_trait(TRAIT_MATURATION)/seed.growth_stages
if(maturation < 1)
maturation = 1
overlay_stage = maturation ? max(1,round(age/maturation)) : 1
var/ikey = "[seed.get_trait(TRAIT_PLANT_ICON)]-[overlay_stage]"
var/image/plant_overlay = plant_controller.plant_icon_cache["[ikey]-[seed.get_trait(TRAIT_PLANT_COLOUR)]"]
if(frozen == 1)
plant_overlay = image('icons/obj/hydroponics_growing.dmi', "[ikey]")
plant_overlay.color = FROZEN_PLANT_COLOUR
if(!plant_overlay)
plant_overlay = image('icons/obj/hydroponics_growing.dmi', "[ikey]")
plant_overlay.color = seed.get_trait(TRAIT_PLANT_COLOUR)
plant_controller.plant_icon_cache["[ikey]-[seed.get_trait(TRAIT_PLANT_COLOUR)]"] = plant_overlay
add_overlay(plant_overlay)
if(harvest && overlay_stage == seed.growth_stages)
ikey = "[seed.get_trait(TRAIT_PRODUCT_ICON)]"
var/image/harvest_overlay = plant_controller.plant_icon_cache["product-[ikey]-[seed.get_trait(TRAIT_PLANT_COLOUR)]"]
if(!harvest_overlay)
harvest_overlay = image('icons/obj/hydroponics_products.dmi', "[ikey]")
harvest_overlay.color = seed.get_trait(TRAIT_PRODUCT_COLOUR)
plant_controller.plant_icon_cache["product-[ikey]-[seed.get_trait(TRAIT_PRODUCT_COLOUR)]"] = harvest_overlay
add_overlay(harvest_overlay)
//Draw the cover.
if(closed_system)
add_overlay("hydrocover")
//Updated the various alert icons.
if(mechanical)
var/list/add = list()
if(waterlevel <= 10)
add += "over_lowwater3"
if(nutrilevel <= 2)
add += "over_lownutri3"
if(weedlevel >= 5 || pestlevel >= 5 || toxins >= 40)
add += "over_alert3"
if(harvest)
add += "over_harvest3"
if(frozen)
add += "over_frozen3"
add_overlay(add)
// Update bioluminescence.
if(seed)
if(seed.get_trait(TRAIT_BIOLUM))
var/clr
if(seed.get_trait(TRAIT_BIOLUM_COLOUR))
clr = seed.get_trait(TRAIT_BIOLUM_COLOUR)
set_light(round(seed.get_trait(TRAIT_POTENCY)/10), l_color = clr)
return
set_light(0)
return