Files
Bubberstation/code/game/objects/structures/aliens.dm
SkyratBot 2b9eba0fe0 [MIRROR] Smoothing groups optimization, save 265ms with configs, more on production & w/ space ruins [MDB IGNORE] (#18189)
* Smoothing groups optimization, save 265ms with configs, more on production & w/ space ruins (#71989)

This one is fun.

On every /turf/Initialize and /atom/Initialize, we try to set
`smoothing_groups` and `canSmoothWith` to a cached list of bitfields. At
the type level, these are specified as lists of IDs, which are then
`Join`ed in Initialize, and retrieved from the cache (or built from
there).

The problem is that the cache only misses about 60 times, but the cache
hits more than a hundred thousand times. This means we eat the cost of
`Join` (which is very very slow, because strings + BYOND), as well as
the preliminary `length` checks, for every single atom.

Furthermore, as you might remember, if you have any list variable set on
a type, it'll create a hidden `(init)` proc to create the list. On
turfs, that costs us about 60ms.

This PR does a cool trick where we can completely eliminate the `Join`
*and* the lists at the cost of a little more work when building the
cache.

The trick is that we replace the current type definitions with this:

```patch
- smoothing_groups = list(SMOOTH_GROUP_TURF_OPEN, SMOOTH_GROUP_FLOOR_ASH)
- canSmoothWith = list(SMOOTH_GROUP_FLOOR_ASH, SMOOTH_GROUP_CLOSED_TURFS)
+ smoothing_groups = SMOOTH_GROUP_TURF_OPEN + SMOOTH_GROUP_FLOOR_ASH
+ canSmoothWith = SMOOTH_GROUP_FLOOR_ASH + SMOOTH_GROUP_CLOSED_TURFS
```

These defines, instead of being numbers, are now segments of a string,
delimited by commas.

For instance, if ASH used to be 13, and CLOSED_TURFS used to be 37, this
used to equal `list(13, 37)`. Now, it equals `"13,37,"`.

Then, when the cache misses, we take that string, and treat it as part
of a JSON list, and decode it from there. Meaning:

```java
// Starting value
"13,37,"

// We have a trailing comma, so add a dummy value
"13,37,0"

// Make it an array
"[13,37,0]"

// Decode
list(13, 37, 0)

// Chop off the dummy value
list(13, 37) // Done!
```

This on its own eliminates 265ms *without space ruins*, with the
combined savings of turf/Initialize, atom/Initialize, and the hidden
(init) procs that no longer exist.

Furthermore, there's some other fun stuff we gain from this approach
emergently.

We previously had a difference between `S_TURF` and `S_OBJ`. The idea is
that if you have any smoothing groups with `S_OBJ`, then you will gain
the `SMOOTH_OBJ` bitflag (though note to self, I need to check that the
cost of adding this is actually worth it). This is achieved by the fact
that `S_OBJ` simply takes the last turf, and adds onto that, meaning
that if the biggest value in the sorting groups is greater than that,
then we know we're going to be smoothing to objects.

This new method provides a limitation here. BYOND has no way of
converting a number to a string at compile time, meaning that we can't
evaluate `MAX_S_TURF + offset` into a string. Instead, in order to
preserve the nice UX, `S_OBJ` now instead opts to make the numbers
negative. This means that what used to be something like:

```dm
smoothing_groups = list(SMOOTH_GROUP_ALIEN_RESIN, SMOOTH_GROUP_ALIEN_WEEDS)
```

...which may have been represented as

```dm
smoothing_groups = list(15, MAX_S_TURF + 3)
```

...will now become, at compile time:

```dm
smoothing_groups = "15,-3,"
```

Except! Because we guarantee smoothing groups are sorted through unit
testing, this is actually going to look like:

```dm
smoothing_groups = "-3,15,"
```

Meaning that we can now check if we're smoothing with objects just by
checking if `smoothing_groups[1] == "-"`, as that's the only way that is
possible. Neat!

Furthermore, though much simpler, what used to be `if
(length(smoothing_groups))` (and canSmoothWith) on every single
atom/Initialize and turf/Initialize can now be `if (smoothing_groups)`,
since empty strings are falsy. `length` is about 15% slower than doing
nothing, so in procs as hot as this, this gives some nice gains just on
its own.

For developers, very little changes. Instead of using `list`, you now
use `+`. The order might change, as `S_OBJ` now needs to come first, but
unit tests will catch you if you mess up. Also, you will notice that all
`S_OBJ` have been increased by one. This is because we used to have
`S_TURF(0)` and `S_OBJ(0)`, but with this new trick, -0 == 0, and so
they conflicted and needed to be changed.

* Sorting how did I miss it

Co-authored-by: Mothblocks <35135081+Mothblocks@users.noreply.github.com>
Co-authored-by: Funce <funce.973@gmail.com>
2023-01-07 23:51:36 +13:00

465 lines
13 KiB
Plaintext

/* Alien shit!
* Contains:
* structure/alien
* Resin
* Weeds
* Egg
*/
/obj/structure/alien
icon = 'icons/mob/nonhuman-player/alien.dmi'
max_integrity = 100
/obj/structure/alien/run_atom_armor(damage_amount, damage_type, damage_flag = 0, attack_dir)
if(damage_flag == MELEE)
switch(damage_type)
if(BRUTE)
damage_amount *= 0.25
if(BURN)
damage_amount *= 2
. = ..()
/obj/structure/alien/play_attack_sound(damage_amount, damage_type = BRUTE, damage_flag = 0)
switch(damage_type)
if(BRUTE)
if(damage_amount)
playsound(loc, 'sound/effects/attackblob.ogg', 100, TRUE)
else
playsound(src, 'sound/weapons/tap.ogg', 50, TRUE)
if(BURN)
if(damage_amount)
playsound(loc, 'sound/items/welder.ogg', 100, TRUE)
/*
* Generic alien stuff, not related to the purple lizards but still alien-like
*/
/obj/structure/alien/gelpod
name = "gelatinous mound"
desc = "A mound of jelly-like substance encasing something inside."
icon = 'icons/obj/fluff.dmi'
icon_state = "gelmound"
/obj/structure/alien/gelpod/deconstruct(disassembled = TRUE)
if(!(flags_1 & NODECONSTRUCT_1))
new /obj/effect/mob_spawn/corpse/human/damaged(get_turf(src))
qdel(src)
/*
* Resin
*/
/obj/structure/alien/resin
name = "resin"
desc = "Looks like some kind of thick resin."
icon = 'icons/obj/smooth_structures/alien/resin_wall.dmi'
icon_state = "resin_wall-0"
base_icon_state = "resin_wall"
density = TRUE
opacity = TRUE
anchored = TRUE
smoothing_flags = SMOOTH_BITMASK
smoothing_groups = SMOOTH_GROUP_ALIEN_RESIN
canSmoothWith = SMOOTH_GROUP_ALIEN_RESIN
max_integrity = 200
var/resintype = null
can_atmos_pass = ATMOS_PASS_DENSITY
/obj/structure/alien/resin/Initialize(mapload)
. = ..()
air_update_turf(TRUE, TRUE)
/obj/structure/alien/resin/Destroy()
air_update_turf(TRUE, FALSE)
. = ..()
/obj/structure/alien/resin/Move()
var/turf/T = loc
. = ..()
move_update_air(T)
/obj/structure/alien/resin/wall
name = "resin wall"
desc = "Thick resin solidified into a wall."
icon = 'icons/obj/smooth_structures/alien/resin_wall.dmi'
icon_state = "resin_wall-0"
base_icon_state = "resin_wall"
resintype = "wall"
smoothing_groups = SMOOTH_GROUP_ALIEN_WALLS + SMOOTH_GROUP_ALIEN_RESIN
canSmoothWith = SMOOTH_GROUP_ALIEN_WALLS
/obj/structure/alien/resin/wall/block_superconductivity()
return 1
/// meant for one lavaland ruin or anywhere that has simplemobs who can push aside structures
/obj/structure/alien/resin/wall/immovable
desc = "Dense resin solidified into a wall."
move_resist = MOVE_FORCE_VERY_STRONG
/obj/structure/alien/resin/wall/creature
name = "gelatinous wall"
desc = "Thick material shaped into a wall. Yuck."
color = "#8EC127"
/obj/structure/alien/resin/membrane
name = "resin membrane"
desc = "Resin just thin enough to let light pass through."
icon = 'icons/obj/smooth_structures/alien/resin_membrane.dmi'
icon_state = "resin_membrane-0"
base_icon_state = "resin_membrane"
opacity = FALSE
max_integrity = 160
resintype = "membrane"
smoothing_groups = SMOOTH_GROUP_ALIEN_WALLS + SMOOTH_GROUP_ALIEN_RESIN
canSmoothWith = SMOOTH_GROUP_ALIEN_WALLS
/obj/structure/alien/resin/attack_paw(mob/user, list/modifiers)
return attack_hand(user, modifiers)
///Used in the big derelict ruin exclusively.
/obj/structure/alien/resin/membrane/creature
name = "gelatinous membrane"
desc = "A strange combination of thin, gelatinous material."
color = "#4BAE56"
/*
* Weeds
*/
#define NODERANGE 3
/obj/structure/alien/weeds
gender = PLURAL
name = "resin floor"
desc = "A thick resin surface covers the floor."
anchored = TRUE
density = FALSE
layer = TURF_LAYER
plane = FLOOR_PLANE
icon = 'icons/obj/smooth_structures/alien/weeds1.dmi'
icon_state = "weeds1-0"
base_icon_state = "weeds1"
max_integrity = 15
smoothing_flags = SMOOTH_BITMASK
smoothing_groups = SMOOTH_GROUP_ALIEN_WEEDS + SMOOTH_GROUP_ALIEN_RESIN
canSmoothWith = SMOOTH_GROUP_ALIEN_WEEDS + SMOOTH_GROUP_WALLS
///the range of the weeds going to be affected by the node
var/node_range = NODERANGE
///the parent node that will determine if we grow or die
var/obj/structure/alien/weeds/node/parent_node
///the list of turfs that the weeds will not be able to grow over
var/static/list/blacklisted_turfs = list(
/turf/open/space,
/turf/open/chasm,
/turf/open/lava,
/turf/open/water,
/turf/open/openspace,
)
/obj/structure/alien/weeds/Initialize(mapload)
//so the sprites line up right in the map editor
pixel_x = -4
pixel_y = -4
. = ..()
set_base_icon()
AddElement(/datum/element/atmos_sensitive, mapload)
/obj/structure/alien/weeds/Destroy()
if(parent_node)
UnregisterSignal(parent_node, COMSIG_PARENT_QDELETING)
parent_node = null
return ..()
///Randomizes the weeds' starting icon, gets redefined by children for them not to share the behavior.
/obj/structure/alien/weeds/proc/set_base_icon()
. = base_icon_state
switch(rand(1,3))
if(1)
icon = 'icons/obj/smooth_structures/alien/weeds1.dmi'
base_icon_state = "weeds1"
if(2)
icon = 'icons/obj/smooth_structures/alien/weeds2.dmi'
base_icon_state = "weeds2"
if(3)
icon = 'icons/obj/smooth_structures/alien/weeds3.dmi'
base_icon_state = "weeds3"
set_smoothed_icon_state(smoothing_junction)
/**
* Called when the node is trying to grow/expand
*/
/obj/structure/alien/weeds/proc/try_expand()
//we cant grow without a parent node
if(!parent_node)
return
//lets make sure we are still on a valid location
var/turf/src_turf = get_turf(src)
if(is_type_in_list(src_turf, blacklisted_turfs))
qdel(src)
return
//lets try to grow in a direction
for(var/turf/check_turf in src_turf.get_atmos_adjacent_turfs())
//we cannot grow on blacklisted turfs
if(is_type_in_list(check_turf, blacklisted_turfs))
continue
var/obj/structure/alien/weeds/check_weed = locate() in check_turf
//we cannot grow onto other weeds
if(check_weed)
continue
//spawn a new one in the turf
check_weed = new(check_turf)
//set the new one's parent node to our parent node
check_weed.parent_node = parent_node
check_weed.RegisterSignal(parent_node, COMSIG_PARENT_QDELETING, PROC_REF(after_parent_destroyed))
/**
* Called when the parent node is destroyed
*/
/obj/structure/alien/weeds/proc/after_parent_destroyed()
if(!find_new_parent())
var/random_time = rand(2 SECONDS, 8 SECONDS)
addtimer(CALLBACK(src, PROC_REF(do_qdel)), random_time)
/**
* Called when trying to find a new parent after our previous parent died
* Will return false if it can't find a new_parent
* Will return the new parent if it can find one
*/
/obj/structure/alien/weeds/proc/find_new_parent()
var/previous_node = parent_node
parent_node = null
for(var/obj/structure/alien/weeds/node/new_parent in range(node_range, src))
if(new_parent == previous_node)
continue
parent_node = new_parent
RegisterSignal(parent_node, COMSIG_PARENT_QDELETING, PROC_REF(after_parent_destroyed))
return parent_node
return FALSE
/**
* Called to delete the weed
*/
/obj/structure/alien/weeds/proc/do_qdel()
qdel(src)
/obj/structure/alien/weeds/should_atmos_process(datum/gas_mixture/air, exposed_temperature)
return exposed_temperature > 300
/obj/structure/alien/weeds/atmos_expose(datum/gas_mixture/air, exposed_temperature)
take_damage(5, BURN, 0, 0)
/obj/structure/alien/weeds/node
name = "glowing resin"
desc = "Blue bioluminescence shines from beneath the surface."
icon = 'icons/obj/smooth_structures/alien/weednode.dmi'
icon_state = "weednode-0"
base_icon_state = "weednode"
light_color = LIGHT_COLOR_BLUE
light_power = 0.5
///the range of the light for the node
var/lon_range = 4
///the minimum time it takes for another weed to spread from this one
var/minimum_growtime = 5 SECONDS
///the maximum time it takes for another weed to spread from this one
var/maximum_growtime = 10 SECONDS
//the cooldown between each growth
COOLDOWN_DECLARE(growtime)
/obj/structure/alien/weeds/node/Initialize(mapload)
. = ..()
//give it light
set_light(lon_range)
//we are the parent node
parent_node = src
//destroy any non-node weeds on turf
var/obj/structure/alien/weeds/check_weed = locate(/obj/structure/alien/weeds) in loc
if(check_weed && check_weed != src)
qdel(check_weed)
//start the cooldown
COOLDOWN_START(src, growtime, rand(minimum_growtime, maximum_growtime))
//start processing
START_PROCESSING(SSobj, src)
/obj/structure/alien/weeds/node/Destroy()
STOP_PROCESSING(SSobj, src)
return ..()
/obj/structure/alien/weeds/node/process()
//we need to have a cooldown, so check and then add
if(!COOLDOWN_FINISHED(src, growtime))
return
COOLDOWN_START(src, growtime, rand(minimum_growtime, maximum_growtime))
//attempt to grow all weeds in range
for(var/obj/structure/alien/weeds/growing_weed in range(node_range, src))
growing_weed.try_expand()
/obj/structure/alien/weeds/node/set_base_icon()
return //No icon randomization at init. The node's icon is already well defined.
/obj/structure/alien/weeds/creature
name = "gelatinous floor"
desc = "A thick gelatinous surface covers the floor. Someone get the galoshes."
color = "#4BAE56"
#undef NODERANGE
/*
* Egg
*/
//for the status var
#define BURSTING "bursting"
#define BURST "burst"
#define GROWING "growing"
#define GROWN "grown"
#define MIN_GROWTH_TIME 900 //time it takes to grow a hugger
#define MAX_GROWTH_TIME 1500
/obj/structure/alien/egg
name = "egg"
desc = "A large mottled egg."
icon_state = "egg_growing"
base_icon_state = "egg"
density = FALSE
anchored = TRUE
max_integrity = 100
integrity_failure = 0.05
var/status = GROWING //can be GROWING, GROWN or BURST; all mutually exclusive
layer = MOB_LAYER
plane = GAME_PLANE_FOV_HIDDEN
var/obj/item/clothing/mask/facehugger/child
///Proximity monitor associated with this atom, needed for proximity checks.
var/datum/proximity_monitor/proximity_monitor
/obj/structure/alien/egg/Initialize(mapload)
. = ..()
update_appearance()
if(status == GROWING || status == GROWN)
child = new(src)
if(status == GROWING)
addtimer(CALLBACK(src, PROC_REF(Grow)), rand(MIN_GROWTH_TIME, MAX_GROWTH_TIME))
proximity_monitor = new(src, status == GROWN ? 1 : 0)
if(status == BURST)
atom_integrity = integrity_failure * max_integrity
AddElement(/datum/element/atmos_sensitive, mapload)
/obj/structure/alien/egg/update_icon_state()
switch(status)
if(GROWING)
icon_state = "[base_icon_state]_growing"
if(GROWN)
icon_state = "[base_icon_state]"
if(BURST)
icon_state = "[base_icon_state]_hatched"
return ..()
/obj/structure/alien/egg/attack_paw(mob/living/user, list/modifiers)
return attack_hand(user, modifiers)
/obj/structure/alien/egg/attack_alien(mob/living/carbon/alien/user, list/modifiers)
return attack_hand(user, modifiers)
/obj/structure/alien/egg/attack_hand(mob/living/user, list/modifiers)
. = ..()
if(.)
return
if(user.getorgan(/obj/item/organ/internal/alien/plasmavessel))
switch(status)
if(BURSTING)
to_chat(user, span_notice("The child is hatching out."))
return
if(BURST)
to_chat(user, span_notice("You clear the hatched egg."))
playsound(loc, 'sound/effects/attackblob.ogg', 100, TRUE)
qdel(src)
return
if(GROWING)
to_chat(user, span_notice("The child is not developed yet."))
return
if(GROWN)
to_chat(user, span_notice("You retrieve the child."))
Burst(kill=FALSE)
return
else
to_chat(user, span_notice("It feels slimy."))
user.changeNext_move(CLICK_CD_MELEE)
/obj/structure/alien/egg/proc/Grow()
status = GROWN
update_appearance()
proximity_monitor.set_range(1)
//drops and kills the hugger if any is remaining
/obj/structure/alien/egg/proc/Burst(kill = TRUE)
if(status == GROWN || status == GROWING)
status = BURSTING
proximity_monitor.set_range(0)
flick("egg_opening", src)
addtimer(CALLBACK(src, PROC_REF(finish_bursting), kill), 15)
/obj/structure/alien/egg/proc/finish_bursting(kill = TRUE)
status = BURST
update_appearance()
if(child)
child.forceMove(get_turf(src))
// TECHNICALLY you could put non-facehuggers in the child var
if(istype(child))
if(kill)
child.Die()
else
for(var/mob/M in range(1,src))
if(CanHug(M))
child.Leap(M)
break
/obj/structure/alien/egg/should_atmos_process(datum/gas_mixture/air, exposed_temperature)
return exposed_temperature > 500
/obj/structure/alien/egg/atmos_expose(datum/gas_mixture/air, exposed_temperature)
take_damage(5, BURN, 0, 0)
/obj/structure/alien/egg/atom_break(damage_flag)
. = ..()
if(!(flags_1 & NODECONSTRUCT_1))
if(status != BURST)
Burst(kill=TRUE)
/obj/structure/alien/egg/HasProximity(atom/movable/AM)
if(status == GROWN)
if(!CanHug(AM))
return
var/mob/living/carbon/C = AM
if(C.stat == CONSCIOUS && C.getorgan(/obj/item/organ/internal/body_egg/alien_embryo))
return
Burst(kill=FALSE)
/obj/structure/alien/egg/grown
status = GROWN
icon_state = "egg"
/obj/structure/alien/egg/burst
status = BURST
icon_state = "egg_hatched"
#undef BURSTING
#undef BURST
#undef GROWING
#undef GROWN
#undef MIN_GROWTH_TIME
#undef MAX_GROWTH_TIME