Skip to content

Commit

Permalink
qol: optimize trim, inheritance for animal_temp + components (#6209)
Browse files Browse the repository at this point in the history
* meow

* Update paradise.dme

* Destroy()

* comments

* Update death_linked.dm

* Update code/__HELPERS/text.dm

Co-authored-by: BeebBeebBoob <[email protected]>

---------

Co-authored-by: BeebBeebBoob <[email protected]>
  • Loading branch information
Antoonij and BeebBeebBoob authored Jan 4, 2025
1 parent fef0557 commit 1bc45a9
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 36 deletions.
27 changes: 6 additions & 21 deletions code/__HELPERS/text.dm
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,12 @@

return ""

//Returns a string with reserved characters and spaces before the first word and after the last word removed.
/proc/trim(text)
return trim_reduced(text)
/// Returns a string with reserved characters and spaces before the first word and after the last word removed.
/proc/trim(text, max_length)
if(max_length)
text = copytext_char(text, 1, max_length)

return trimtext(text) || ""

/// Returns a string that does not exceed max_length characters in size
/proc/trim_length(text, max_length)
Expand Down Expand Up @@ -803,21 +806,3 @@
if(ofthree == 0)
return "[num]"
return "[num / (10 ** (ofthree * 3))][GLOB.si_suffixes[round(length(GLOB.si_suffixes) / 2) + ofthree + 1]]"

//Returns a string with reserved characters and spaces after the first and last letters removed
//Like trim(), but very slightly faster. worth it for niche usecases
/proc/trim_reduced(text)
var/starting_coord = 1
var/text_len = length(text)
for (var/i in 1 to text_len)
if (text2ascii(text, i) > 32)
starting_coord = i
break

for (var/i = text_len, i >= starting_coord, i--)
if (text2ascii(text, i) > 32)
return copytext(text, starting_coord, i + 1)

if(starting_coord > 1)
return copytext(text, starting_coord)
return ""
66 changes: 51 additions & 15 deletions code/datums/components/animal_temperature.dm
Original file line number Diff line number Diff line change
@@ -1,31 +1,67 @@
/datum/component/animal_temperature
dupe_mode = COMPONENT_DUPE_UNIQUE_PASSARGS
/// Min body temp
var/minbodytemp
var/minbodytemp = 250
/// Max body temp
var/maxbodytemp
var/maxbodytemp = 350
/// Damage when below min temp
var/cold_damage
var/cold_damage = 2
/// Damage when above max temp
var/heat_damage
var/heat_damage = 2
/// If true - alert will be shown
var/show_alert
var/show_alert = FALSE

/datum/component/animal_temperature/Initialize(
minbodytemp = 250,
maxbodytemp = 350,
cold_damage = 2,
heat_damage = 2,
show_alert = FALSE
minbodytemp,
maxbodytemp,
cold_damage,
heat_damage,
show_alert
)
if(!isanimal(parent))
return COMPONENT_INCOMPATIBLE

src.minbodytemp = minbodytemp
src.maxbodytemp = maxbodytemp
src.cold_damage = cold_damage
src.heat_damage = heat_damage
src.show_alert = show_alert
if(!isnull(minbodytemp))
src.minbodytemp = minbodytemp

if(!isnull(maxbodytemp))
src.maxbodytemp = maxbodytemp

if(!isnull(cold_damage))
src.cold_damage = cold_damage

if(!isnull(heat_damage))
src.heat_damage = heat_damage

if(!isnull(show_alert))
src.show_alert = show_alert

/datum/component/animal_temperature/InheritComponent(
datum/component/animal_temperature/new_comp,
i_am_original,
minbodytemp,
maxbodytemp,
cold_damage,
heat_damage,
show_alert
)
if(!i_am_original)
return

if(!isnull(minbodytemp))
src.minbodytemp = minbodytemp

if(!isnull(maxbodytemp))
src.maxbodytemp = maxbodytemp

if(!isnull(cold_damage))
src.cold_damage = cold_damage

if(!isnull(heat_damage))
src.heat_damage = heat_damage

if(!isnull(show_alert))
src.show_alert = show_alert

/datum/component/animal_temperature/RegisterWithParent()
RegisterSignal(parent, COMSIG_ANIMAL_HANDLE_ENVIRONMENT, PROC_REF(handle_environment))
Expand Down
51 changes: 51 additions & 0 deletions code/datums/components/death_linked.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* ## Death link component
*
* When the owner of this component dies it also gibs a linked mobs
*/
/datum/component/death_linked
dupe_mode = COMPONENT_DUPE_UNIQUE_PASSARGS
/// Mobs in that list will die when the user dies. Contains weakrefs
var/list/linked_mobs

/datum/component/death_linked/Initialize(list/mobs)
. = ..()

if(!isliving(parent))
return COMPONENT_INCOMPATIBLE

for(var/mob/mob as anything in mobs)
LAZYADD(linked_mobs, WEAKREF(mob))

/datum/component/death_linked/Destroy(force)
LAZYNULL(linked_mobs)

return ..()

/datum/component/death_linked/InheritComponent(datum/component/death_linked/new_comp, i_am_original, list/mobs)
if(!i_am_original)
return

if(!LAZYLEN(mobs))
return

for(var/mob/mob as anything in mobs)
LAZYADD(linked_mobs, WEAKREF(mob))

/datum/component/death_linked/RegisterWithParent()
. = ..()
RegisterSignal(parent, COMSIG_LIVING_DEATH, PROC_REF(on_death))

/datum/component/death_linked/UnregisterFromParent()
. = ..()
UnregisterSignal(parent, COMSIG_LIVING_DEATH)

/datum/component/death_linked/proc/on_death(mob/living/target, gibbed)
SIGNAL_HANDLER

if(!LAZYLEN(linked_mobs))
return

for(var/datum/weakref/weakref as anything in linked_mobs)
var/mob/living/linked_mob_resolved = weakref.resolve()
linked_mob_resolved?.gib()
69 changes: 69 additions & 0 deletions code/datums/components/holderloving.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/** Holder Loving Component
*
* When you drop an object onto a turf it gets moved back into its parent holder
*
* Prevents you from force moving the object into any other location that isn't its parent holder
*/
/datum/component/holderloving
/** Item that parent is bound to.
* We try to keep parent either directly in holder, or in holder's loc if loc is a mob,
* and warp parent into holder if they go anywhere else.
*/
var/atom/holder

/datum/component/holderloving/Initialize(holder)
if(!isitem(parent) || !holder)
return COMPONENT_INCOMPATIBLE

src.holder = holder

/datum/component/holderloving/Destroy(force)
holder = null

return ..()

/datum/component/holderloving/RegisterWithParent()
RegisterSignal(holder, COMSIG_QDELETING, PROC_REF(holder_deleting))
RegisterSignal(parent, COMSIG_ITEM_DROPPED, PROC_REF(check_my_loc))
RegisterSignal(parent, COMSIG_ITEM_PRE_UNEQUIP, PROC_REF(can_be_moved))

/datum/component/holderloving/UnregisterFromParent()
UnregisterSignal(holder, list(COMSIG_QDELETING))
UnregisterSignal(parent, list(COMSIG_ITEM_DROPPED, COMSIG_ITEM_PRE_UNEQUIP))

/datum/component/holderloving/proc/holder_deleting(datum/source, force)
SIGNAL_HANDLER

qdel(parent)

/datum/component/holderloving/proc/is_valid_location(atom/location)
SHOULD_BE_PURE(TRUE)

if(location == holder || (location == holder.loc && ismob(holder.loc)))
return TRUE

return FALSE

/datum/component/holderloving/proc/check_my_loc(datum/source, mob/user, slot)
SIGNAL_HANDLER

var/obj/item/item_parent = parent
if(!is_valid_location(item_parent.loc))
item_parent.forceMove(holder)

/datum/component/holderloving/proc/can_be_moved(
obj/item/I,
force,
atom/newloc,
no_move,
invdrop,
silent
)
SIGNAL_HANDLER

// allow the item to be dropped on the turf so it can be later moved back into the holder as a convinience tool
if(isturf(newloc) || is_valid_location(newloc))
return NONE

// prevent this item from being moved anywhere else
return COMPONENT_ITEM_BLOCK_UNEQUIP
2 changes: 2 additions & 0 deletions paradise.dme
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@
#include "code\datums\components\conveyor_movement.dm"
#include "code\datums\components\cross_shock.dm"
#include "code\datums\components\decal.dm"
#include "code\datums\components\death_linked.dm"
#include "code\datums\components\defibrillator.dm"
#include "code\datums\components\drift.dm"
#include "code\datums\components\ducttape.dm"
Expand All @@ -460,6 +461,7 @@
#include "code\datums\components\force_move.dm"
#include "code\datums\components\fullauto.dm"
#include "code\datums\components\hide_highest_offset.dm"
#include "code\datums\components\holderloving.dm"
#include "code\datums\components\jackboots.dm"
#include "code\datums\components\jetpack.dm"
#include "code\datums\components\label.dm"
Expand Down

0 comments on commit 1bc45a9

Please sign in to comment.