Files
Bubberstation/code/datums/components/pinata.dm
Mothblocks c1d68698fb Micro-optimize qdel by only permitting one parameter (#80628)
Productionizes #80615.

The core optimization is this:

```patch
-	var/hint = to_delete.Destroy(arglist(args.Copy(2))) // Let our friend know they're about to get fucked up.
+	var/hint = to_delete.Destroy(force) // Let our friend know they're about to get fucked up.
```

We avoid a heap allocation in the form of copying the args over to a new
list. A/B testing shows this results in 33% better overtime, and in a
real round shaving off a full second of self time and 0.4 seconds of
overtime--both of these would be doubled in the event this is merged as
the new proc was only being run 50% of the time.
2023-12-28 13:52:44 -08:00

63 lines
2.2 KiB
Plaintext

///Objects or mobs with this componenet will drop items when taking damage.
/datum/component/pinata
///How much damage does an attack need to do to have a chance to drop "candy"
var/minimum_damage
///What is the likelyhood some "candy" should drop when attacked.
var/drop_chance
///A list of "candy" items that can be dropped when taking damage
var/candy
///Number of "candy" items dropped when the structure is destroyed/mob is killed, set to 0 if none. drop_chance and minimum damage are not applied.
var/death_drop
/datum/component/pinata/Initialize(
minimum_damage = 10,
drop_chance = 40,
candy = list(/obj/item/food/candy, /obj/item/food/lollipop/cyborg, /obj/item/food/gumball, /obj/item/food/bubblegum, /obj/item/food/chocolatebar),
death_drop = 5,
)
src.minimum_damage = minimum_damage
src.drop_chance = drop_chance
src.candy = candy
src.death_drop = death_drop
if(ismob(parent))
RegisterSignal(parent, COMSIG_MOB_APPLY_DAMAGE, PROC_REF(damage_inflicted))
RegisterSignal(parent, COMSIG_LIVING_DEATH, PROC_REF(pinata_broken))
return
if(isobj(parent))
var/obj/parent_integrity_test = parent
if(parent_integrity_test.uses_integrity)
RegisterSignal(parent, COMSIG_ATOM_TAKE_DAMAGE, PROC_REF(damage_inflicted))
RegisterSignal(parent, COMSIG_ATOM_DESTRUCTION, PROC_REF(pinata_broken))
return
return COMPONENT_INCOMPATIBLE
/datum/component/pinata/proc/damage_inflicted(obj/target, damage, damage_type, ...)
SIGNAL_HANDLER
if(damage < minimum_damage || damage_type == STAMINA || damage_type == OXY)
return
if(!prob(drop_chance + damage)) //Higher damage means less rolls but higher odds on the roll
return
var/list/turf_options = get_adjacent_open_turfs(parent)
turf_options += get_turf(parent)
if(length(turf_options))
var/dropped_item = pick(candy)
new dropped_item(pick(turf_options))
/datum/component/pinata/proc/pinata_broken()
SIGNAL_HANDLER
for(var/i in 1 to death_drop)
var/dropped_item = pick(candy)
new dropped_item(get_turf(parent))
qdel(src)
/datum/component/pinata/Destroy(force)
UnregisterSignal(parent, list(
COMSIG_MOB_APPLY_DAMAGE,
COMSIG_LIVING_DEATH,
COMSIG_ATOM_TAKE_DAMAGE,
COMSIG_ATOM_DESTRUCTION,
))
return ..()