Refactors reagent pump code (#8316)

* refactors reagent pump code

* Moves reagent definitions onto turfs

* Various tweaks

* qdel_null on destroy
This commit is contained in:
Atermonera
2021-10-31 12:08:50 -08:00
committed by atermonera
parent b868789cd4
commit a2ba682ac8
4 changed files with 206 additions and 271 deletions

View File

@@ -1,270 +0,0 @@
/obj/machinery/pump
name = "fluid pump"
desc = "A fluid pumping machine."
description_info = "A machine that can pump fluid from certain turfs.<br>\
Water can be pumped from any body of water. Certain locations or environmental\
conditions can cause different byproducts to be produced.<br>\
Magma or Lava can be pumped to produce mineralized fluid."
anchored = 0
density = 1
icon = 'icons/obj/machines/reagent.dmi'
icon_state = "pump"
circuit = /obj/item/weapon/circuitboard/fluidpump
var/on = 0
var/obj/item/weapon/cell/cell = null
var/use = 200
var/efficiency = 1
var/reagents_per_cycle = 40
var/unlocked = 0
var/open = 0
var/obj/item/hose_connector/output/Output
// Overlay cache vars.
var/icon/liquid
var/icon/tank
var/icon/powerlow
var/icon/glass
var/icon/open_overlay
var/icon/cell_overlay
/obj/machinery/pump/Initialize()
create_reagents(200)
. = ..()
default_apply_parts()
if(ispath(cell))
cell = new cell(src)
Output = new(src)
RefreshParts()
update_icon()
/obj/machinery/pump/update_icon()
..()
if(!tank)
tank = new/icon(icon, "[icon_state]-volume")
if(!powerlow)
powerlow = new/icon(icon, "[icon_state]-lowpower")
if(!liquid)
var/icon/cutter = new/icon(icon, "[icon_state]-volume")
cutter.Blend(rgb(0, 0, 0,), ICON_MULTIPLY)
liquid = new/icon(icon, "[icon_state]-cutting")
liquid.Blend(cutter,ICON_AND)
if(!glass)
glass = new/icon(icon, "[icon_state]-glass")
glass.Blend(rgb(1,1,1,0.5), ICON_MULTIPLY)
if(!open_overlay)
open_overlay = new/icon(icon, "[icon_state]-open")
if(!cell_overlay)
cell_overlay = new/icon(icon, "[icon_state]-cell")
cut_overlays()
add_overlay(tank)
if(cell && cell.charge < (use * CELLRATE / efficiency))
add_overlay(powerlow)
if(reagents.total_volume >= 1)
var/list/hextorgb = hex2rgb(reagents.get_color())
liquid.GrayScale()
liquid.Blend(rgb(hextorgb[1],hextorgb[2],hextorgb[3]),ICON_MULTIPLY)
add_overlay(liquid)
add_overlay(glass)
if(open)
add_overlay(open_overlay)
if(cell)
add_overlay(cell_overlay)
if(on)
icon_state = "[initial(icon_state)]-running"
else
icon_state = "[initial(icon_state)]"
/obj/machinery/pump/process()
if(Output.get_pairing())
reagents.trans_to_holder(Output.reagents, Output.reagents.maximum_volume)
if(prob(5))
visible_message("<span class='notice'>\The [src] gurgles as it exports fluid.</span>")
if(!on)
return
if(!cell || (cell.charge < (use * CELLRATE / efficiency)))
turn_off(TRUE)
return
handle_pumping()
update_icon()
/obj/machinery/pump/RefreshParts()
for(var/obj/item/weapon/stock_parts/manipulator/SM in component_parts)
efficiency = SM.rating
var/total_bin_rating = 0
for(var/obj/item/weapon/stock_parts/matter_bin/SB in component_parts)
total_bin_rating += SB.rating
reagents.maximum_volume = round(initial(reagents.maximum_volume) + 100 * total_bin_rating)
return
/obj/machinery/pump/power_change()
if(!cell || cell.charge < (use * CELLRATE) || !anchored)
return turn_off(TRUE)
return FALSE
// Returns 0 on failure and 1 on success
/obj/machinery/pump/proc/turn_on(var/loud = 0)
if(!cell)
return 0
if(cell.charge < (use * CELLRATE))
return 0
on = 1
update_icon()
if(loud)
visible_message("<span class='notice'>\The [src] turns on.</span>")
return 1
/obj/machinery/pump/proc/turn_off(var/loud = 0)
on = 0
set_light(0, 0)
update_icon()
if(loud)
visible_message("<span class='notice'>\The [src] shuts down.</span>")
if(!on)
return TRUE
return FALSE
/obj/machinery/pump/attack_ai(mob/user as mob)
if(istype(user, /mob/living/silicon/robot) && Adjacent(user))
return attack_hand(user)
if(on)
turn_off(TRUE)
else
if(!turn_on(1))
to_chat(user, "<span class='notice'>You try to turn on \the [src] but it does not work.</span>")
/obj/machinery/pump/attack_hand(mob/user as mob)
if(open && cell)
if(ishuman(user))
if(!user.get_active_hand())
user.put_in_hands(cell)
cell.loc = user.loc
else
cell.loc = src.loc
cell.add_fingerprint(user)
cell.update_icon()
cell = null
on = 0
set_light(0)
to_chat(user, "<span class='notice'>You remove the power cell.</span>")
update_icon()
return
if(on)
turn_off(TRUE)
else if(anchored)
if(!turn_on(1))
to_chat(user, "<span class='notice'>You try to turn on \the [src] but it does not work.</span>")
update_icon()
/obj/machinery/pump/attackby(obj/item/weapon/W as obj, mob/user as mob)
if(W.is_screwdriver())
if(!open)
if(unlocked)
unlocked = 0
to_chat(user, "<span class='notice'>You screw the battery panel in place.</span>")
else
unlocked = 1
to_chat(user, "<span class='notice'>You unscrew the battery panel.</span>")
else if(W.is_crowbar())
if(unlocked)
if(open)
open = 0
overlays = null
to_chat(user, "<span class='notice'>You crowbar the battery panel in place.</span>")
else
if(unlocked)
open = 1
to_chat(user, "<span class='notice'>You remove the battery panel.</span>")
else if(W.is_wrench())
if(on)
to_chat(user, "<span class='notice'>\The [src] is active. Turn it off before trying to move it!</span>")
return
default_unfasten_wrench(user, W, 2 SECONDS)
else if(istype(W, /obj/item/weapon/cell))
if(open)
if(cell)
to_chat(user, "<span class='notice'>There is a power cell already installed.</span>")
else
user.drop_item()
W.loc = src
cell = W
to_chat(user, "<span class='notice'>You insert the power cell.</span>")
else
..()
RefreshParts()
update_icon()
/obj/machinery/pump/proc/handle_pumping()
var/turf/T = get_turf(src)
if(istype(T, /turf/simulated/floor/water))
cell.use(use * CELLRATE / efficiency)
reagents.add_reagent("water", reagents_per_cycle)
if(T.temperature <= T0C)
reagents.add_reagent("ice", round(reagents_per_cycle / 2, 0.1))
if((istype(T,/turf/simulated/floor/water/pool) || istype(T,/turf/simulated/floor/water/deep/pool)))
reagents.add_reagent("chlorine", round(reagents_per_cycle / 10 * efficiency, 0.1))
else if(istype(T,/turf/simulated/floor/water/contaminated))
reagents.add_reagent("vatstabilizer", round(reagents_per_cycle / 2))
if(T.loc.name == "Sea") // Saltwater.
reagents.add_reagent("sodiumchloride", round(reagents_per_cycle / 10 * efficiency, 0.1))
for(var/turf/simulated/mineral/MT in range(5))
if(MT.mineral)
var/obj/effect/mineral/OR = MT.mineral
reagents.add_reagent(OR.ore_reagent, round(reagents_per_cycle / 20 * efficiency, 0.1))
else if(istype(T, /turf/simulated/floor/lava))
cell.use(use * CELLRATE / efficiency * 4)
reagents.add_reagent("mineralizedfluid", round(reagents_per_cycle * efficiency / 2, 0.1))

View File

@@ -0,0 +1,205 @@
/obj/machinery/pump
name = "fluid pump"
desc = "A fluid pumping machine."
description_info = "A machine that can pump fluid from certain turfs.<br>\
Water can be pumped from any body of water. Certain locations or environmental\
conditions can cause different byproducts to be produced.<br>\
Magma or Lava can be pumped to produce mineralized fluid."
anchored = 0
density = 1
icon = 'icons/obj/machines/reagent.dmi'
icon_state = "pump"
circuit = /obj/item/weapon/circuitboard/fluidpump
active_power_usage = 200 * CELLRATE
var/obj/item/weapon/cell/cell = null
var/obj/item/hose_connector/output/Output = null
var/reagents_per_cycle = 40
var/on = 0
var/unlocked = 0
var/open = 0
/obj/machinery/pump/Initialize()
create_reagents(200)
. = ..()
default_apply_parts()
cell = default_use_hicell()
Output = new(src)
RefreshParts()
update_icon()
/obj/machinery/pump/Destroy()
QDEL_NULL(cell)
QDEL_NULL(Output)
. = ..()
/obj/machinery/pump/RefreshParts()
var/obj/item/weapon/stock_parts/manipulator/SM = locate() in component_parts
active_power_usage = initial(active_power_usage) / SM.rating
var/motor_power = 0
for(var/obj/item/weapon/stock_parts/motor/M in component_parts)
motor_power += M.rating
reagents_per_cycle = initial(reagents_per_cycle) * motor_power / 2
var/bin_size = 0
for(var/obj/item/weapon/stock_parts/matter_bin/SB in component_parts)
bin_size += SB.rating
// New holder might have different volume. Transfer everything to a new holder to account for this.
var/datum/reagents/R = new(round(initial(reagents.maximum_volume) + 100 * bin_size), src)
src.reagents.trans_to_holder(R, src.reagents.total_volume)
qdel(src.reagents)
src.reagents = R
/obj/machinery/pump/update_icon()
..()
cut_overlays()
add_overlay("[icon_state]-tank")
if(!(cell?.check_charge(active_power_usage)))
add_overlay("[icon_state]-lowpower")
if(reagents.total_volume >= 1)
var/image/I = image(icon, "[icon_state]-volume")
I.color = reagents.get_color()
add_overlay(I)
add_overlay("[icon_state]-glass")
if(open)
add_overlay("[icon_state]-open")
if(istype(cell))
add_overlay("[icon_state]-cell")
icon_state = "[initial(icon_state)][on ? "-running" : ""]"
/obj/machinery/pump/process()
if(!on)
return
if(!anchored || !(cell?.use(active_power_usage)))
set_state(FALSE)
return
var/turf/T = get_turf(src)
if(!istype(T))
return
T.pump_reagents(reagents, reagents_per_cycle)
update_icon()
if(Output.get_pairing())
reagents.trans_to_holder(Output.reagents, Output.reagents.maximum_volume)
if(prob(5))
visible_message("<span class='notice'>\The [src] gurgles as it pumps fluid.</span>")
// Sets the power state, if possible.
// Returns TRUE/FALSE on power state changing
// var/target = target power state
// var/message = TRUE/FALSE whether to make a message about state change
/obj/machinery/pump/proc/set_state(var/target, var/message = TRUE)
if(target == on)
return FALSE
if(!on && (!(cell?.check_charge(active_power_usage)) || !anchored))
return FALSE
on = !on
update_icon()
if(message)
if(on)
message = SPAN_NOTICE("\The [src] turns on.")
else
message = SPAN_NOTICE("\The [src] shuts down.")
visible_message(message)
return TRUE
/obj/machinery/pump/attack_robot(mob/user)
return attack_hand(user)
/obj/machinery/pump/attack_ai(mob/user)
if(!set_state(!on))
to_chat(user, "<span class='notice'>You try to toggle \the [src] but it does not respond.</span>")
/obj/machinery/pump/attack_hand(mob/user)
if(open && istype(cell))
user.put_in_hands(cell)
cell.add_fingerprint(user)
cell.update_icon()
cell = null
set_state(FALSE)
to_chat(user, "<span class='notice'>You remove the power cell.</span>")
return
if(!set_state(!on))
to_chat(user, "<span class='notice'>You try to toggle \the [src] but it does not respond.</span>")
/obj/machinery/pump/attackby(obj/item/weapon/W, mob/user)
. = TRUE
if(W.is_screwdriver() && !open)
to_chat(user, SPAN_NOTICE("You [unlocked ? "screw" : "unscrew"] the battery panel."))
unlocked = !unlocked
else if(W.is_crowbar() && unlocked)
to_chat(user, open ? \
"<span class='notice'>You crowbar the battery panel in place.</span>" : \
"<span class='notice'>You remove the battery panel.</span>" \
)
open = !open
else if(W.is_wrench())
if(on)
to_chat(user, "<span class='notice'>\The [src] is active. Turn it off before trying to move it!</span>")
return FALSE
default_unfasten_wrench(user, W, 2 SECONDS)
else if(istype(W, /obj/item/weapon/cell) && open)
if(istype(cell))
to_chat(user, "<span class='notice'>There is a power cell already installed.</span>")
return FALSE
user.drop_from_inventory(W, src)
to_chat(user, "<span class='notice'>You insert the power cell.</span>")
else
. = ..()
RefreshParts()
update_icon()
/turf/proc/pump_reagents()
return
/turf/simulated/floor/lava/pump_reagents(var/datum/reagents/R, var/volume)
. = ..()
R.add_reagent("mineralizedfluid", round(volume / 2, 0.1))
/turf/simulated/floor/water/pump_reagents(var/datum/reagents/R, var/volume)
. = ..()
R.add_reagent("water", round(volume, 0.1))
if(temperature <= T0C)
R.add_reagent("ice", round(volume / 2, 0.1))
for(var/turf/simulated/mineral/M in orange(5))
if(istype(M.mineral, /obj/effect/mineral))
var/obj/effect/mineral/ore = M.mineral
reagents.add_reagent(ore.ore_reagent, round(volume / 2, 0.1))
/turf/simulated/floor/water/pool/pump_reagents(var/datum/reagents/R, var/volume)
. = ..()
R.add_reagent("chlorine", round(volume / 10, 0.1))
/turf/simulated/floor/water/deep/pool/pump_reagents(var/datum/reagents/R, var/volume)
. = ..()
R.add_reagent("chlorine", round(volume / 10, 0.1))
/turf/simulated/floor/water/contaminated/pump_reagents(var/datum/reagents/R, var/volume)
. = ..()
R.add_reagent("vatstabilizer", round(volume / 2, 0.1))