Skip to content

Commit

Permalink
[MIRROR] Minor code cleanup for PCM (#643)
Browse files Browse the repository at this point in the history
* Minor code cleanup for PCM (#81092)

## About The Pull Request
- Shortened the description for readability. Makes sense to display
further details only when examining.
- Removed deprecated `content` tag from button elements in the UI
- Added more sanity checks when dealing with numbers inside `ui_act()`
- Make sure we don't insert hologram items
- Use `as anything` to speed up some for loops
- Capitalized some text inside examine

## Changelog
:cl:
spellcheck: shortened description for the PCM and capitalized some text
for its examines.
code: adds sanity checks and removed deprecated `content` tag from PCM
UI. Stops hologram items from being inserted.
/:cl:

* Minor code cleanup for PCM

---------

Co-authored-by: SyncIt21 <[email protected]>
  • Loading branch information
2 people authored and FFMirrorBot committed Jan 27, 2024
1 parent b1a7972 commit 17a0036
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 80 deletions.
168 changes: 94 additions & 74 deletions code/modules/reagents/chemistry/machinery/portable_chem_mixer.dm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/obj/item/storage/portable_chem_mixer
name = "Portable Chemical Mixer"
desc = "A portable device that dispenses and mixes chemicals. All necessary reagents need to be supplied with beakers. A label indicates that the 'CTRL'-button on the device may be used to open it for refills. This device can be worn as a belt. The letters 'S&T' are imprinted on the side."
desc = "A portable device that dispenses and mixes chemicals using the beakers inserted inside."
icon = 'icons/obj/medical/chemical.dmi'
icon_state = "portablechemicalmixer_open"
worn_icon_state = "portable_chem_mixer"
Expand All @@ -11,7 +11,7 @@
custom_premium_price = PAYCHECK_CREW * 14

///Creating an empty slot for a beaker that can be added to dispense into
var/obj/item/reagent_containers/beaker = null
var/obj/item/reagent_containers/beaker
///The amount of reagent that is to be dispensed currently
var/amount = 30
///List in which all currently dispensable reagents go
Expand All @@ -31,19 +31,19 @@
register_context()

/obj/item/storage/portable_chem_mixer/Destroy()
dispensable_reagents.Cut()
QDEL_NULL(beaker)
return ..()

/obj/item/storage/portable_chem_mixer/add_context(atom/source, list/context, obj/item/held_item, mob/user)
. = ..()

context[SCREENTIP_CONTEXT_CTRL_LMB] = "[atom_storage.locked ? "Un" : ""]Lock storage"
if(atom_storage.locked && !QDELETED(beaker))
context[SCREENTIP_CONTEXT_ALT_LMB] = "Eject beaker"

if(!isnull(held_item))
if (!atom_storage.locked || \
(held_item.item_flags & ABSTRACT) || \
(held_item.flags_1 & HOLOGRAM_1) || \
!is_reagent_container(held_item) || \
!held_item.is_open_container() \
)
Expand All @@ -55,70 +55,34 @@
/obj/item/storage/portable_chem_mixer/examine(mob/user)
. = ..()
if(!atom_storage.locked)
. += span_notice("Use [EXAMINE_HINT("ctrl click")] to lock in order to use its interface.")
. += span_notice("Use [EXAMINE_HINT("Ctrl Click")] to lock in order to use its interface.")
else
. += span_notice("Its storage is locked, use [EXAMINE_HINT("ctrl click")] to unlock it.")
. += span_notice("Its storage is locked, use [EXAMINE_HINT("Ctrl Click")] to unlock it.")
if(QDELETED(beaker))
. += span_notice("A beaker can be inserted to dispense reagents after it is locked.")
else
. += span_notice("A beaker of [beaker.reagents.maximum_volume] units capacity is inserted.")
. += span_notice("It can be ejected with [EXAMINE_HINT("alt click")].")

/obj/item/storage/portable_chem_mixer/ex_act(severity, target)
return severity > EXPLODE_LIGHT ? ..() : FALSE

/obj/item/storage/portable_chem_mixer/attackby(obj/item/weapon, mob/user, params)
if (!atom_storage.locked || \
(weapon.item_flags & ABSTRACT) || \
!is_reagent_container(weapon) || \
!weapon.is_open_container() \
)
return ..()

replace_beaker(user, weapon)
update_appearance()
return TRUE
. += span_notice("A beaker of [beaker.reagents.maximum_volume]u capacity is inserted.")
. += span_notice("It can be ejected with [EXAMINE_HINT("Alt Click")].")

/obj/item/storage/portable_chem_mixer/update_icon_state()
if(!atom_storage.locked)
icon_state = "portablechemicalmixer_open"
return ..()
if(beaker)
if(!QDELETED(beaker))
icon_state = "portablechemicalmixer_full"
return ..()
icon_state = "portablechemicalmixer_empty"
return ..()

/obj/item/storage/portable_chem_mixer/AltClick(mob/living/user)
if(!atom_storage.locked)
balloon_alert(user, "lock first to use alt eject!")
return ..()
if(!can_interact(user) || !user.can_perform_action(src, FORBID_TELEKINESIS_REACH))
return

replace_beaker(user)
update_appearance()

/obj/item/storage/portable_chem_mixer/CtrlClick(mob/living/user)
if(atom_storage.locked == STORAGE_FULLY_LOCKED)
atom_storage.locked = STORAGE_NOT_LOCKED
replace_beaker(user)
SStgui.close_uis(src)
else
atom_storage.locked = STORAGE_FULLY_LOCKED
atom_storage.hide_contents(usr)

update_appearance()

/obj/item/storage/portable_chem_mixer/Exited(atom/movable/gone, direction)
/obj/item/storage/portable_chem_mixer/Entered(atom/movable/arrived, atom/old_loc, list/atom/old_locs)
. = ..()
if(gone == beaker)
beaker = null
else
if(!atom_storage.locked)
update_contents()

/// Reload dispensable reagents from new contents
/obj/item/storage/portable_chem_mixer/proc/update_contents()
PRIVATE_PROC(TRUE)

dispensable_reagents.Cut()
for (var/obj/item/reagent_containers/container in contents)
var/datum/reagent/key = container.reagents.get_master_reagent()
Expand All @@ -131,11 +95,29 @@
dispensable_reagents[key_type]["reagents"] = list()
dispensable_reagents[key_type]["reagents"] += container.reagents

/obj/item/storage/portable_chem_mixer/Entered(atom/movable/arrived, atom/old_loc, list/atom/old_locs)
/obj/item/storage/portable_chem_mixer/Exited(atom/movable/gone, direction)
. = ..()
if(!atom_storage.locked)
if(gone == beaker)
beaker = null
else
update_contents()

/obj/item/storage/portable_chem_mixer/ex_act(severity, target)
return severity > EXPLODE_LIGHT ? ..() : FALSE

/obj/item/storage/portable_chem_mixer/attackby(obj/item/weapon, mob/user, params)
if (!atom_storage.locked || \
(weapon.item_flags & ABSTRACT) || \
(weapon.flags_1 & HOLOGRAM_1) || \
!is_reagent_container(weapon) || \
!weapon.is_open_container() \
)
return ..()

replace_beaker(user, weapon)
update_appearance()
return TRUE

/**
* Replaces the beaker of the portable chemical mixer with another beaker, or simply adds the new beaker if none is in currently
*
Expand All @@ -145,21 +127,16 @@
* * obj/item/reagent_containers/new_beaker - The new beaker that the user wants to put into the device
*/
/obj/item/storage/portable_chem_mixer/proc/replace_beaker(mob/living/user, obj/item/reagent_containers/new_beaker)
if(beaker)
PRIVATE_PROC(TRUE)

if(!QDELETED(beaker))
user.put_in_hands(beaker)
if(new_beaker)

if(!QDELETED(new_beaker))
if(!user.transferItemToLoc(new_beaker, src))
return
beaker = new_beaker

/obj/item/storage/portable_chem_mixer/MouseDrop(obj/over_object)
. = ..()
if(ismob(loc))
var/mob/M = loc
if(!M.incapacitated() && istype(over_object, /atom/movable/screen/inventory/hand))
var/atom/movable/screen/inventory/hand/H = over_object
M.putItemFromInventoryInHandIfPossible(src, H.held_index)

/obj/item/storage/portable_chem_mixer/ui_interact(mob/user, datum/tgui/ui)
if(loc != user)
balloon_alert(user, "hold it in your hand!")
Expand All @@ -183,26 +160,24 @@
. = list()
.["amount"] = amount

var/list/chemicals = list()
var/is_hallucinating = FALSE
if(isliving(user))
var/mob/living/living_user = user
is_hallucinating = !!living_user.has_status_effect(/datum/status_effect/hallucination)

for(var/re in dispensable_reagents)
var/value = dispensable_reagents[re]
var/datum/reagent/temp = GLOB.chemical_reagents_list[re]
.["chemicals"] = list()
for(var/datum/reagent/reagent_type as anything in dispensable_reagents)
var/datum/reagent/temp = GLOB.chemical_reagents_list[reagent_type]
if(temp)
var/chemname = temp.name
var/total_volume = 0
var/total_ph = 0
for (var/datum/reagents/rs in value["reagents"])
for (var/datum/reagents/rs as anything in dispensable_reagents[reagent_type]["reagents"])
total_volume += rs.total_volume
total_ph = rs.ph
if(is_hallucinating && prob(5))
chemname = "[pick_list_replacements("hallucination.json", "chemicals")]"
chemicals += list(list("title" = chemname, "id" = temp.name, "volume" = total_volume, "pH" = total_ph))
.["chemicals"] = chemicals
.["chemicals"] += list(list("title" = chemname, "id" = temp.name, "volume" = total_volume, "pH" = total_ph))

var/list/beaker_data = null
if(!QDELETED(beaker))
Expand All @@ -212,7 +187,7 @@
beaker_data["pH"] = round(beaker.reagents.ph, 0.01)
beaker_data["currentVolume"] = round(beaker.reagents.total_volume, 0.01)
var/list/beakerContents = list()
if(length(beaker?.reagents.reagent_list))
if(length(beaker.reagents.reagent_list))
for(var/datum/reagent/reagent in beaker.reagents.reagent_list)
beakerContents += list(list("name" = reagent.name, "volume" = round(reagent.volume, 0.01))) // list in a list because Byond merges the first list...
beaker_data["contents"] = beakerContents
Expand All @@ -225,7 +200,15 @@

switch(action)
if("amount")
amount = text2num(params["target"])
var/target = params["target"]
if(isnull(target))
return

target = text2num(target)
if(isnull(target))
return

amount = target
return TRUE

if("dispense")
Expand All @@ -236,17 +219,54 @@
if(!QDELETED(beaker))
var/datum/reagents/container = beaker.reagents
var/actual = min(amount, container.maximum_volume - container.total_volume)
for (var/datum/reagents/source in dispensable_reagents[reagent]["reagents"])
for(var/datum/reagents/source as anything in dispensable_reagents[reagent]["reagents"])
actual -= source.trans_to(beaker, min(source.total_volume, actual), transferred_by = ui.user)
if (actual <= 0)
if(actual <= 0)
break
return TRUE
return TRUE

if("remove")
beaker.reagents.remove_all(text2num(params["amount"]))
var/target = params["amount"]
if(isnull(target))
return

target = text2num(target)
if(isnull(target))
return

beaker.reagents.remove_all(target)
return TRUE

if("eject")
replace_beaker(ui.user)
update_appearance()
return TRUE

/obj/item/storage/portable_chem_mixer/MouseDrop(obj/over_object)
. = ..()
if(ismob(loc))
var/mob/M = loc
if(!M.incapacitated() && istype(over_object, /atom/movable/screen/inventory/hand))
var/atom/movable/screen/inventory/hand/H = over_object
M.putItemFromInventoryInHandIfPossible(src, H.held_index)

/obj/item/storage/portable_chem_mixer/AltClick(mob/living/user)
if(!atom_storage.locked)
balloon_alert(user, "lock first to use alt eject!")
return ..()
if(!can_interact(user) || !user.can_perform_action(src, FORBID_TELEKINESIS_REACH))
return

replace_beaker(user)
update_appearance()

/obj/item/storage/portable_chem_mixer/CtrlClick(mob/living/user)
if(atom_storage.locked == STORAGE_FULLY_LOCKED)
atom_storage.locked = STORAGE_NOT_LOCKED
replace_beaker(user)
SStgui.close_uis(src)
else
atom_storage.locked = STORAGE_FULLY_LOCKED
atom_storage.hide_contents(usr)

update_appearance()
15 changes: 9 additions & 6 deletions tgui/packages/tgui/interfaces/PortableChemMixer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ export const PortableChemMixer = (props) => {
key={amount}
icon="plus"
selected={amount === data.amount}
content={amount}
onClick={() =>
act('amount', {
target: amount,
})
}
/>
>
{amount}
</Button>
))}
>
<Box>
Expand All @@ -55,14 +56,15 @@ export const PortableChemMixer = (props) => {
icon="tint"
fluid
lineHeight={1.75}
content={`(${chemical.volume}) ${chemical.title}`}
tooltip={'pH: ' + chemical.pH}
onClick={() =>
act('dispense', {
reagent: chemical.id,
})
}
/>
>
{`(${chemical.volume}) ${chemical.title}`}
</Button>
))}
</Box>
</Section>
Expand All @@ -72,9 +74,10 @@ export const PortableChemMixer = (props) => {
<Button
key={amount}
icon="minus"
content={amount}
onClick={() => act('remove', { amount })}
/>
>
{amount}
</Button>
))}
>
<BeakerDisplay beaker={beaker} showpH />
Expand Down

0 comments on commit 17a0036

Please sign in to comment.