Skip to content

Commit

Permalink
Merge pull request #3726 from out-of-phaze/optimization/lighthouse-opts
Browse files Browse the repository at this point in the history
Port some init optimizations from Lighthouse
  • Loading branch information
MistakeNot4892 authored Feb 25, 2024
2 parents 50c179e + 9a2a1ac commit 1ba0d09
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 147 deletions.
4 changes: 4 additions & 0 deletions code/__defines/misc.dm
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@
#define ICON_STATE_INV "inventory"

#define hex2num(X) text2num(X, 16)
/// Returns the hex value of a number given a value assumed to be a base-ten value, padded to a minimum length of 2.
#define num2hex(num) num2text(num, 2, 16)
/// Returns the hex value of a number given a value assumed to be a base-ten value, padded to a supplied minimum length.
#define num2hex_padded(num, len) num2text(num, len, 16)

#define Z_ALL_TURFS(Z) block(locate(1, 1, Z), locate(world.maxx, world.maxy, Z))

Expand Down
27 changes: 0 additions & 27 deletions code/_helpers/type2type.dm
Original file line number Diff line number Diff line change
@@ -1,40 +1,13 @@
/*
* Holds procs designed to change one type of value, into another.
* Contains:
* hex2num & num2hex
* text2list & list2text
* file2list
* angle2dir
* angle2text
* worldtime2text
*/

// Returns the hex value of a number given a value assumed to be a base-ten value
/proc/num2hex(num, len=2)
if(!isnum(num))
num = 0
num = round(abs(num))
. = ""
var/i=0
while(1)
if(len<=0)
if(!num)
break
else
if(i>=len)
break
var/remainder = num/16
num = round(remainder)
remainder = (remainder - num) * 16
switch(remainder)
if(9,8,7,6,5,4,3,2,1)
. = "[remainder]" + .
if(10,11,12,13,14,15)
. = ascii2text(remainder+87) + .
else
. = "0" + .
i++

/proc/text2numlist(text, delimiter="\n")
var/list/num_list = list()
for(var/x in splittext(text, delimiter))
Expand Down
10 changes: 2 additions & 8 deletions code/_helpers/unsorted.dm
Original file line number Diff line number Diff line change
Expand Up @@ -773,16 +773,10 @@ var/global/list/WALLITEMS = list(
return 0

/proc/get_random_colour(var/simple = FALSE, var/lower = 0, var/upper = 255)
var/colour
if(simple)
colour = pick(list("FF0000","FF7F00","FFFF00","00FF00","0000FF","4B0082","8F00FF"))
return pick(list("#ff0000","#ff7f00","#ffff00","#00ff00","#0000ff","#4b0082","#8f00ff"))
else
for(var/i=1;i<=3;i++)
var/temp_col = "[num2hex(rand(lower,upper))]"
if(length(temp_col )<2)
temp_col = "0[temp_col]"
colour += temp_col
return "#[colour]"
return rgb(rand(lower, upper), rand(lower, upper), rand(lower, upper))

// call to generate a stack trace and print to runtime logs
/proc/get_stack_trace(msg, file, line)
Expand Down
13 changes: 8 additions & 5 deletions code/datums/datum.dm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
var/tmp/is_processing = FALSE
/// Used by the SStimer subsystem
var/list/active_timers
/// Used to avoid unnecessary refstring creation in Destroy().
var/tmp/has_state_machine = FALSE

#ifdef TESTING
var/tmp/running_find_references
Expand Down Expand Up @@ -49,11 +51,12 @@
if (!isturf(src)) // Not great, but the 'correct' way to do it would add overhead for little benefit.
cleanup_events(src)

var/list/machines = global.state_machines["\ref[src]"]
if(length(machines))
for(var/base_type in machines)
qdel(machines[base_type])
global.state_machines -= "\ref[src]"
if(has_state_machine)
var/list/machines = global.state_machines["\ref[src]"]
if(length(machines))
for(var/base_type in machines)
qdel(machines[base_type])
global.state_machines -= "\ref[src]"

return QDEL_HINT_QUEUE

Expand Down
6 changes: 4 additions & 2 deletions code/datums/extensions/state_machine.dm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
var/global/list/state_machines = list()

/proc/get_state_machine(var/datum/holder, var/base_type)
if(istype(holder) && base_type)
if(istype(holder) && base_type && holder.has_state_machine)
var/list/machines = global.state_machines["\ref[holder]"]
return islist(machines) && machines[base_type]

Expand All @@ -18,16 +18,18 @@ var/global/list/state_machines = list()
fsm_type = base_type
var/datum/state_machine/machine = new fsm_type(holder)
machines[base_type] = machine
holder.has_state_machine = TRUE
return machine

/proc/remove_state_machine(var/datum/holder, var/base_type)
if(istype(holder) && base_type)
if(istype(holder) && base_type && holder.has_state_machine)
var/holder_ref = "\ref[holder]"
var/list/machines = global.state_machines[holder_ref]
if(length(machines))
machines -= base_type
if(!length(machines))
global.state_machines -= holder_ref
holder.has_state_machine = FALSE
return TRUE
return FALSE

Expand Down
2 changes: 1 addition & 1 deletion code/game/dna/dna2.dm
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ var/global/list/assigned_blocks[DNA_SE_LENGTH]
SetSEBlock(block,newBlock,defer)

/proc/EncodeDNABlock(var/value)
return add_zero2(num2hex(value,1), 3)
return num2hex_padded(value, 3)

/datum/dna/proc/UpdateUI()
src.uni_identity=""
Expand Down
10 changes: 0 additions & 10 deletions code/game/dna/dna2_helpers.dm
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,6 @@
// Helpers for DNA2
/////////////////////////////

// Pads 0s to t until length == u
/proc/add_zero2(t, u)
var/temp1
while (length(t) < u)
t = "0[t]"
temp1 = t
if (length(t) > u)
temp1 = copytext(t,2,u+1)
return temp1

// DNA Gene activation boundaries, see dna2.dm.
// Returns a list object with 4 numbers.
/proc/GetDNABounds(var/block)
Expand Down
39 changes: 16 additions & 23 deletions code/game/machinery/alarm.dm
Original file line number Diff line number Diff line change
Expand Up @@ -843,38 +843,31 @@ FIRE ALARM
var/decl/security_state/security_state = GET_DECL(global.using_map.security_state)
to_chat(user, "The current alert level is [security_state.current_security_level.name].")

/obj/machinery/firealarm/proc/get_cached_overlay(key)
if(!LAZYACCESS(overlays_cache, key))
var/state
switch(key)
if(/decl/machine_construction/wall_frame/panel_open)
state = "b2"
if(/decl/machine_construction/wall_frame/no_wires)
state = "b1"
if(/decl/machine_construction/wall_frame/no_circuit)
state = "b0"
else
state = key
LAZYSET(overlays_cache, key, image(icon, state))
return overlays_cache[key]

/obj/machinery/firealarm/on_update_icon()
overlays.Cut()
cut_overlays()
icon_state = "casing"
if(construct_state && !istype(construct_state, /decl/machine_construction/wall_frame/panel_closed))
overlays += get_cached_overlay(construct_state.type)
var/construct_icon_state
switch(construct_state.type)
if(/decl/machine_construction/wall_frame/panel_open)
construct_icon_state = "b2"
if(/decl/machine_construction/wall_frame/no_wires)
construct_icon_state = "b1"
if(/decl/machine_construction/wall_frame/no_circuit)
construct_icon_state = "b0"
add_overlay(construct_icon_state)
set_light(0)
return

if(stat & BROKEN)
overlays += get_cached_overlay("broken")
add_overlay("broken")
set_light(0)
else if(stat & NOPOWER)
overlays += get_cached_overlay("unpowered")
add_overlay("unpowered")
set_light(0)
else
if(!detecting)
overlays += get_cached_overlay("fire1")
add_overlay("fire1")
set_light(2, 0.25, COLOR_RED)
else if(isContactLevel(z))
var/decl/security_state/security_state = GET_DECL(global.using_map.security_state)
Expand All @@ -885,14 +878,14 @@ FIRE ALARM
if(sl.alarm_appearance.alarm_icon)
var/image/alert1 = image(sl.icon, sl.alarm_appearance.alarm_icon)
alert1.color = sl.alarm_appearance.alarm_icon_color
overlays |= alert1
add_overlay(alert1)

if(sl.alarm_appearance.alarm_icon_twotone)
var/image/alert2 = image(sl.icon, sl.alarm_appearance.alarm_icon_twotone)
alert2.color = sl.alarm_appearance.alarm_icon_twotone_color
overlays |= alert2
add_overlay(alert2)
else
overlays += get_cached_overlay("fire0")
add_overlay("fire0")

/obj/machinery/firealarm/fire_act(datum/gas_mixture/air, exposed_temperature, exposed_volume)
if(detecting && exposed_temperature > T0C+200)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
addtimer(CALLBACK(src, PROC_REF(finish)), rand(20 SECONDS, 29 SECONDS))

/obj/item/grenade/anti_photon/proc/finish()
set_light(10, 10, "#[num2hex(rand(64,255))][num2hex(rand(64,255))][num2hex(rand(64,255))]")
set_light(10, 10, rgb(rand(64,255), rand(64,255), rand(64,255)))
playsound(loc, 'sound/effects/bang.ogg', 50, 1, 5)
sleep(1 SECOND)
qdel(src)
11 changes: 9 additions & 2 deletions code/game/turfs/flooring/flooring.dm
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@
//How we smooth with other flooring
var/decal_layer = DECAL_LAYER
var/floor_smooth = SMOOTH_ALL
var/list/flooring_whitelist = list() //Smooth with nothing except the contents of this list
var/list/flooring_blacklist = list() //Smooth with everything except the contents of this list
/// Smooth with nothing except the types in this list. Turned into a typecache for performance reasons.
var/list/flooring_whitelist = list()
/// Smooth with everything except the types in this list. Turned into a typecache for performance reasons.
var/list/flooring_blacklist = list()

//How we smooth with walls
var/wall_smooth = SMOOTH_ALL
Expand All @@ -51,6 +53,11 @@

var/height = 0

/decl/flooring/Initialize()
. = ..()
flooring_whitelist = typecacheof(flooring_whitelist)
flooring_blacklist = typecacheof(flooring_blacklist)

/decl/flooring/proc/on_remove()
return

Expand Down
2 changes: 1 addition & 1 deletion code/game/turfs/simulated/floor.dm
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
else
disable_zmimic()

update_icon(1)
queue_icon_update(SSatoms.initialized) // only update neighbors if we're setting flooring after SSatoms has finished
levelupdate()

//This proc will set floor_type to null and the update_icon() proc will then change the icon_state of the turf
Expand Down
40 changes: 18 additions & 22 deletions code/game/turfs/simulated/floor_icon.dm
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ var/global/list/flooring_cache = list()
if(update_neighbors)
for(var/turf/simulated/floor/F in orange(src, 1))
F.queue_ao(FALSE)
F.update_icon()
F.queue_icon_update()

/turf/simulated/floor/proc/get_flooring_overlay(var/cache_key, var/icon_base, var/icon_dir = 0, var/external = FALSE)
if(!flooring_cache[cache_key])
Expand Down Expand Up @@ -107,45 +107,41 @@ var/global/list/flooring_cache = list()
flooring_cache[cache_key] = I
return flooring_cache[cache_key]

/decl/flooring/proc/test_link(var/turf/origin, var/turf/T)
/decl/flooring/proc/test_link(var/turf/origin, var/turf/opponent)
var/is_linked = FALSE
if(istype(origin) && istype(T))
if(istype(origin) && istype(opponent))
//is_wall is true for wall turfs and for floors containing a low wall
if(T.is_wall())
if(opponent.is_wall())
if(wall_smooth == SMOOTH_ALL)
is_linked = TRUE
//If is_hole is true, then it's space or openspace
else if(T.is_open())
else if(opponent.is_open())
if(space_smooth == SMOOTH_ALL)
is_linked = TRUE

//If we get here then its a normal floor
else if (istype(T, /turf/simulated/floor))
var/turf/simulated/floor/t = T
//Check for window frames.
if(wall_smooth == SMOOTH_ALL)
for(var/obj/structure/wall_frame/WF in T.contents)
is_linked = TRUE
else if (istype(opponent, /turf/simulated/floor))
var/turf/simulated/floor/floor_opponent = opponent
//If the floor is the same as us,then we're linked,
if (istype(src, t.flooring))
if (istype(src, floor_opponent.flooring))
is_linked = TRUE
else if (floor_smooth == SMOOTH_ALL)
is_linked = TRUE
else if (floor_smooth != SMOOTH_NONE)
//If we get here it must be using a whitelist or blacklist
if (floor_smooth == SMOOTH_WHITELIST)
for (var/v in flooring_whitelist)
if (istype(t.flooring, v))
//Found a match on the list
is_linked = TRUE
break
if (flooring_whitelist[floor_opponent.flooring.type])
//Found a match on the typecache
is_linked = TRUE
else if(floor_smooth == SMOOTH_BLACKLIST)
is_linked = TRUE //Default to true for the blacklist, then make it false if a match comes up
for (var/v in flooring_blacklist)
if (istype(t.flooring, v))
//Found a match on the list
is_linked = FALSE
break
if (flooring_blacklist[floor_opponent.flooring.type])
//Found a match on the typecache
is_linked = FALSE
//Check for window frames.
if (!is_linked && wall_smooth == SMOOTH_ALL)
if(locate(/obj/structure/wall_frame) in opponent)
is_linked = TRUE
return is_linked

/decl/flooring/proc/symmetric_test_link(var/turf/A, var/turf/B)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,7 @@
/datum/integrated_io/color/scramble()
if(!is_valid())
return
var/new_data
for(var/i=1;i<=3;i++)
var/temp_col = "[num2hex(rand(0,255))]"
if(length(temp_col )<2)
temp_col = "0[temp_col]"
new_data += temp_col
data = new_data
data = rgb(rand(0, 255), rand(0, 255), rand(0, 255))
push_data()

/datum/integrated_io/color/display_pin_type()
Expand Down
7 changes: 4 additions & 3 deletions code/modules/lighting/lighting_source.dm
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@
// Decompile the hexadecimal colour into lumcounts of each perspective.
/datum/light_source/proc/parse_light_color()
if (light_color)
lum_r = HEX_RED(light_color) / 255
lum_g = HEX_GREEN(light_color) / 255
lum_b = HEX_BLUE(light_color) / 255
var/list/color_list = rgb2num(light_color)
lum_r = color_list[1] / 255
lum_g = color_list[2] / 255
lum_b = color_list[3] / 255
else
lum_r = 1
lum_g = 1
Expand Down
2 changes: 1 addition & 1 deletion code/modules/lighting/lighting_turf.dm
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
lum_g = CLAMP01(lum_g / 4) * 255
lum_b = CLAMP01(lum_b / 4) * 255

return "#[num2hex(lum_r, 2)][num2hex(lum_g, 2)][num2hex(lum_g, 2)]"
return rgb(lum_r, lum_b, lum_g)

#define SCALE(targ,min,max) (targ - min) / (max - min)

Expand Down
Loading

0 comments on commit 1ba0d09

Please sign in to comment.