Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Componentizes rechargers & unwrenching #5107

Closed
wants to merge 15 commits into from
3 changes: 3 additions & 0 deletions code/__DEFINES/dcs/signals/atom/signals_atom.dm
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,6 @@

/// Called when an atom has emp_act called on it, from /atom/emp_act: (severity)
#define COMSIG_ATOM_EMP_ACT "atom_emp_act"

/// Called when an atom has attack_hand called on it, from /atom/attack_hand: (mob/user)
#define COMSIG_ATOM_ATTACK_HAND "atom_attack_hand"
22 changes: 22 additions & 0 deletions code/__DEFINES/dcs/signals/atom/signals_cell.dm
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,30 @@
#define COMSIG_CELL_CHECK_CHARGE "cell_check_charge"
#define COMPONENT_CELL_CHARGE_INSUFFICIENT (1<<0)

/// (check_percent)
#define COMSIG_CELL_CHECK_CHARGE_PERCENT "cell_check_charge_percent"
#define COMPONENT_CELL_CHARGE_PERCENT_INSUFFICIENT (1<<0)

#define COMSIG_CELL_CHECK_FULL_CHARGE "cell_check_full_charge"
#define COMPONENT_CELL_CHARGE_NOT_FULL (1<<0)

#define COMSIG_CELL_TRY_INSERT_CELL "cell_try_insert_cell"
#define COMPONENT_CANCEL_CELL_INSERT (1<<0)

/// (mob/living/user)
#define COMSIG_CELL_REMOVE_CELL "cell_remove_cell"
#define COMPONENT_CELL_NO_INSERTED_CELL (1<<0)

#define COMSIG_CELL_CHECK_INSERTED_CELL "cell_check_inserted_cell"
#define COMPONENT_CELL_NOT_INSERTED (1<<0)

/// (list/charge_pass) <- convert to a pointer once we're on 515
#define COMSIG_CELL_GET_CHARGE "cell_get_charge"

/// (list/charge_pass) <- convert to a pointer once we're on 515
#define COMSIG_CELL_GET_PERCENT "cell_get_percent"

/// (list/charge_pass) <- convert to a pointer once we're on 515
#define COMSIG_CELL_GET_MAX_CHARGE "cell_get_max_charge"

#define COMSIG_CELL_CHARGE_MODIFIED "cell_charge_modified"
7 changes: 7 additions & 0 deletions code/__DEFINES/dcs/signals/atom/signals_obj.dm
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,10 @@
#define COSMIG_OBJ_AFTER_BUCKLE "signal_obj_after_buckle"

#define COMSIG_STRUCTURE_CRATE_SQUAD_LAUNCHED "structure_crate_squad_launched"

/// from /obj/structure/machinery/power_change(): (new_power_state)
#define COMSIG_MACHINERY_POWER_CHANGE "machinery_power_change"

/// from /datum/element/simple_unwrench(): (obj/item/wrench, mob/living/user)
#define COMSIG_OBJ_TRY_UNWRENCH "obj_try_unwrench"
#define ELEMENT_OBJ_STOP_UNWRENCH (1<<0)
2 changes: 1 addition & 1 deletion code/__DEFINES/dcs/signals/signals_datum.dm
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#define COMSIG_TOPIC "handle_topic"
/// from datum ui_act (usr, action)
#define COMSIG_UI_ACT "COMSIG_UI_ACT"
///from base of atom/attackby(): (/obj/item, /mob/living, params)
///from base of atom/attackby(): (obj/item/weapon, mob/living/user, params)
#define COMSIG_PARENT_ATTACKBY "atom_attackby"
///Return this in response if you don't want afterattack to be called
#define COMPONENT_NO_AFTERATTACK (1<<0)
Expand Down
1 change: 1 addition & 0 deletions code/_onclick/human.dm
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
return HANDLE_CLICK_PASS_THRU

/atom/proc/attack_hand(mob/user)
SEND_SIGNAL(src, COMSIG_ATOM_ATTACK_HAND, user)
Zonespace27 marked this conversation as resolved.
Show resolved Hide resolved
return

/mob/living/carbon/human/MouseDrop_T(atom/dropping, mob/living/user)
Expand Down
3 changes: 2 additions & 1 deletion code/_onclick/item_attack.dm
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
return FALSE

/atom/movable/attackby(obj/item/W, mob/living/user)
if(W)
. = ..()
if(!. && W)
if(!(W.flags_item & NOBLUDGEON))
visible_message(SPAN_DANGER("[src] has been hit by [user] with [W]."), null, null, 5, CHAT_TYPE_MELEE_HIT)
user.animation_attack_on(src)
Expand Down
97 changes: 91 additions & 6 deletions code/datums/components/cell.dm
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,65 @@
/// If draining charge on process(), how much to drain per process call
var/charge_drain = 10
/// If the parent should show cell charge on examine
var/display_charge = TRUE
var/display_charge = FALSE
/// From how many tiles at the highest someone can examine the parent to see the charge
var/charge_examine_range = 1
/// If the component requires a cell to be inserted to work instead of having an integrated one
var/cell_insert = FALSE
/// Ref to an inserted cell. Should only be null if cell_insert is false
var/obj/item/cell/inserted_cell
/// What should be displayed as the examine string if display_charge is TRUE. %CHARGE% and %MAXCHARGE% will be replaced with the remaining charge in the cell and its maximum, respectively
var/examine_string = "A small gauge in the corner reads \"Power: %CHARGE%\"."


/datum/component/cell/Initialize(
max_charge = 10000,
hit_charge = FALSE,
max_recharge_tick = 400,
charge_drain = 10,
display_charge = TRUE,
display_charge = FALSE,
charge_examine_range = 1,
cell_insert = FALSE,
cell_insert_default_cell = /obj/item/cell,
examine_string = "",
initial_charge = -1,
)

. = ..()
if(!isatom(parent))
return COMPONENT_INCOMPATIBLE

src.max_charge = max_charge
charge = max_charge
if(initial_charge == -1)
charge = max_charge
else
charge = min(initial_charge, max_charge)
src.hit_charge = hit_charge
src.max_recharge_tick = max_recharge_tick
src.charge_drain = charge_drain
src.display_charge = display_charge
src.charge_examine_range = charge_examine_range
src.cell_insert = cell_insert
if(cell_insert)
inserted_cell = new cell_insert_default_cell(parent)
src.examine_string = examine_string || src.examine_string

/datum/component/cell/Destroy(force, silent)
QDEL_NULL(inserted_cell)
return ..()


/datum/component/cell/RegisterWithParent()
..()
RegisterSignal(parent, list(COMSIG_PARENT_ATTACKBY, COMSIG_ITEM_ATTACKED), PROC_REF(on_object_hit))
RegisterSignal(parent, COMSIG_CELL_ADD_CHARGE, PROC_REF(add_charge))
RegisterSignal(parent, COMSIG_CELL_USE_CHARGE, PROC_REF(use_charge))
RegisterSignal(parent, COMSIG_CELL_CHECK_CHARGE, PROC_REF(has_charge))
RegisterSignal(parent, COMSIG_CELL_GET_CHARGE, PROC_REF(get_charge))
RegisterSignal(parent, COMSIG_CELL_GET_MAX_CHARGE, PROC_REF(get_max_charge))
RegisterSignal(parent, COMSIG_CELL_GET_PERCENT, PROC_REF(get_percent))
RegisterSignal(parent, COMSIG_CELL_CHECK_CHARGE_PERCENT, PROC_REF(has_charge_percent))
RegisterSignal(parent, COMSIG_CELL_CHECK_FULL_CHARGE, PROC_REF(has_full_charge))
RegisterSignal(parent, COMSIG_CELL_CHECK_INSERTED_CELL, PROC_REF(has_inserted_cell))
RegisterSignal(parent, COMSIG_CELL_START_TICK_DRAIN, PROC_REF(start_drain))
RegisterSignal(parent, COMSIG_CELL_STOP_TICK_DRAIN, PROC_REF(stop_drain))
RegisterSignal(parent, COMSIG_CELL_REMOVE_CELL, PROC_REF(remove_cell))
Expand Down Expand Up @@ -92,7 +108,7 @@
if((charge_examine_range != UNLIMITED_DISTANCE) && get_dist(examiner, parent) > charge_examine_range)
return

examine_text += "A small gauge in the corner reads \"Power: [round(100 * charge / max_charge)]%\"."
examine_text += replacetext(replacetext(examine_text, "%CHARGE%", "[round(100 * charge / max_charge)]"), "%MAXCHARGE%", "[max_charge]")

/datum/component/cell/proc/on_object_hit(datum/source, obj/item/cell/attack_obj, mob/living/attacker, params)
SIGNAL_HANDLER
Expand Down Expand Up @@ -122,9 +138,12 @@
charge = power_cell.charge
max_charge = power_cell.maxcharge

/datum/component/cell/proc/remove_cell(mob/living/user)
/datum/component/cell/proc/remove_cell(datum/source, mob/living/user)
SIGNAL_HANDLER

if(!inserted_cell)
return COMPONENT_CELL_NO_INSERTED_CELL

user.put_in_hands(inserted_cell, TRUE)
to_chat(user, SPAN_NOTICE("You remove [inserted_cell] from [parent]."))
inserted_cell = null
Expand Down Expand Up @@ -162,24 +181,35 @@
if(max_charge == UNLIMITED_CHARGE)
return

if(cell_insert && !inserted_cell)
return COMPONENT_CELL_NO_INSERTED_CELL

if(!charge_add)
return

charge = clamp(charge + charge_add, 0, max_charge)
on_charge_modify()

/datum/component/cell/proc/use_charge(datum/source, charge_use = 0)
SIGNAL_HANDLER

if(max_charge == UNLIMITED_CHARGE)
return

if(cell_insert && !inserted_cell)
return COMPONENT_CELL_NO_INSERTED_CELL

if(!charge_use)
return

if(!charge)
return COMPONENT_CELL_NO_USE_CHARGE

if(charge_use > charge)
return COMPONENT_CELL_NO_USE_CHARGE

charge = clamp(charge - charge_use, 0, max_charge)
on_charge_modify()

if(!charge)
on_charge_empty()
Expand All @@ -191,12 +221,67 @@
if(!charge)
return COMPONENT_CELL_CHARGE_INSUFFICIENT

if(cell_insert && !inserted_cell)
return COMPONENT_CELL_CHARGE_INSUFFICIENT

if(charge < charge_amount)
return COMPONENT_CELL_CHARGE_INSUFFICIENT

/datum/component/cell/proc/has_charge_percent(datum/source, check_percent = 0)
SIGNAL_HANDLER

if(!charge)
return COMPONENT_CELL_CHARGE_INSUFFICIENT

if(cell_insert && !inserted_cell)
return COMPONENT_CELL_CHARGE_PERCENT_INSUFFICIENT

if(check_percent > (100 * (charge / max_charge)))
return COMPONENT_CELL_CHARGE_PERCENT_INSUFFICIENT

/datum/component/cell/proc/has_full_charge(datum/source)
SIGNAL_HANDLER

if(cell_insert && !inserted_cell)
return COMPONENT_CELL_CHARGE_NOT_FULL

if(charge < max_charge)
return COMPONENT_CELL_CHARGE_NOT_FULL

/datum/component/cell/proc/on_charge_modify()
SEND_SIGNAL(parent, COMSIG_CELL_CHARGE_MODIFIED)

/datum/component/cell/proc/on_charge_empty()
stop_drain()
SEND_SIGNAL(parent, COMSIG_CELL_OUT_OF_CHARGE)

/datum/component/cell/proc/has_inserted_cell(datum/source)
SIGNAL_HANDLER

// When a cell isn't required to be inserted, we act like we always have an innate one
if(!cell_insert)
return

if(!inserted_cell)
return COMPONENT_CELL_NOT_INSERTED

/// When passed in a list, will add the cell's charge to that list
/datum/component/cell/proc/get_charge(datum/source, list/charge_pass)
SIGNAL_HANDLER

charge_pass += charge

/// When passed in a list, will add the cell's charge as a percentage (out of 100) to that list
/datum/component/cell/proc/get_percent(datum/source, list/charge_pass)
SIGNAL_HANDLER

charge_pass += (100 * (charge / max_charge))

/// When passed in a list, will add the cell's max charge to that list
/datum/component/cell/proc/get_max_charge(datum/source, list/charge_pass)
SIGNAL_HANDLER

charge_pass += max_charge

#undef UNLIMITED_CHARGE
#undef UNLIMITED_DISTANCE
Loading