diff --git a/code/modules/library/skill_learning/skillchip.dm b/code/modules/library/skill_learning/skillchip.dm index f534e391bcb..e4fce399a76 100644 --- a/code/modules/library/skill_learning/skillchip.dm +++ b/code/modules/library/skill_learning/skillchip.dm @@ -97,25 +97,28 @@ * Arguments: * * silent - Boolean. Whether or not an activation message should be shown to the user. * * force - Boolean. Whether or not to just force de-activation if it would be prevented for any reason. + * * brain_owner - the owner var of the brain is set to null on organ/on_mob_remove(), so we need this if owner is null. */ -/obj/item/skillchip/proc/try_deactivate_skillchip(silent = FALSE, force = FALSE) +/obj/item/skillchip/proc/try_deactivate_skillchip(silent = FALSE, force = FALSE, mob/living/brain_owner) if(!active) return "Skillchip is not active." // Should not happen. Holding brain is destroyed and the chip hasn't had its state set appropriately. if(!holding_brain) - stack_trace("Skillchip's owner is null or qdeleted brain.") + stack_trace("Skillchip doesn't have a holding brain.") return "Skillchip cannot detect viable brain." + if(!brain_owner) + brain_owner = holding_brain.owner // Also should not happen. We're somehow deactivating skillchips in a bodyless brain. - if(QDELETED(holding_brain.owner)) + if(QDELETED(brain_owner)) active = FALSE stack_trace("Skillchip's brain has no owner, owner is null or owner qdeleted.") return "Skillchip cannot detect viable body." // We have a holding brain, the holding brain has an owner. If we're forcing this, do it hard and fast. if(force) - on_deactivate(holding_brain.owner, silent) + on_deactivate(brain_owner, silent) return // Is the chip still experiencing a cooldown period? @@ -123,7 +126,7 @@ return "Skillchip is still recharging for [COOLDOWN_TIMELEFT(src, chip_cooldown) * 0.1]s" // We're good to go. Deactive this chip. - on_deactivate(holding_brain.owner, silent) + on_deactivate(brain_owner, silent) /** * Called when a skillchip is inserted in a user's brain. diff --git a/code/modules/mob/living/brain/brain_item.dm b/code/modules/mob/living/brain/brain_item.dm index 4e1c22f5afe..73974cf248d 100644 --- a/code/modules/mob/living/brain/brain_item.dm +++ b/code/modules/mob/living/brain/brain_item.dm @@ -105,6 +105,7 @@ brain_owner.update_body_parts() /obj/item/organ/brain/on_mob_remove(mob/living/carbon/organ_owner, special, movement_flags) + . = ..() // Delete skillchips first as parent proc sets owner to null, and skillchips need to know the brain's owner. if(!QDELETED(organ_owner) && length(skillchips)) if(!special) @@ -112,9 +113,7 @@ for(var/chip in skillchips) var/obj/item/skillchip/skillchip = chip // Run the try_ proc with force = TRUE. - skillchip.try_deactivate_skillchip(silent = special, force = TRUE) - - ..() + skillchip.try_deactivate_skillchip(silent = special, force = TRUE, brain_owner = organ_owner) for(var/X in traumas) var/datum/brain_trauma/BT = X diff --git a/code/modules/unit_tests/organs.dm b/code/modules/unit_tests/organs.dm index cf01436796d..6162bd43e5b 100644 --- a/code/modules/unit_tests/organs.dm +++ b/code/modules/unit_tests/organs.dm @@ -98,3 +98,23 @@ TEST_ASSERT_EQUAL(dummy.get_organ_loss(slot_to_use), test_organ.maxHealth, \ "Mob level \"apply organ damage\" can exceed the [slot_to_use] organ's damage cap with a large maximum supplied.") dummy.fully_heal(HEAL_ORGANS) + +///Allocate a human mob, give 'em a skillchip and a generic trauma, then see if it throws any error when the brain is removed. +/datum/unit_test/chipped_traumatized_brain_removal + +/datum/unit_test/chipped_traumatized_brain_removal/Run() + var/mob/living/carbon/human/dummy/dummy = allocate(__IMPLIED_TYPE__) + + //add the chip and activate it + var/obj/item/skillchip/basketweaving/chip = new(dummy.loc) + dummy.implant_skillchip(chip, force = TRUE) + TEST_ASSERT(chip.holding_brain, "Skillchip couldn't be implanted successfully, 'holding_brain' is null") + chip.try_activate_skillchip(force = TRUE) + TEST_ASSERT(chip.active, "Skillchip couldn't be activated") + + //add a trauma + dummy.gain_trauma_type(BRAIN_TRAUMA_MILD) + + var/obj/item/organ/brain = locate() in dummy.organs + brain.forceMove(dummy.loc) + allocated += brain