Skip to content

Commit

Permalink
New features, New wrappers for cell_slot system. (#6421)
Browse files Browse the repository at this point in the history
<!-- Write **BELOW** The Headers and **ABOVE** The comments else it may
not be viewable. -->
<!-- You can view Contributing.MD for a detailed description of the pull
request process. -->

## About The Pull Request
Adds wrappers for cells to the cell_slot system for cleaner calling.
Adds a function to remove a cell and hand it directly to a mob for saner
handling outside the file. Adds a bunch of comments from what I've
divined in the hopes of granting others the same wisdom.
Swaps the single inducer line that can use the new wrapper procs.
<!-- Describe The Pull Request. Please be sure every change is
documented or this can delay review and even discourage maintainers from
merging your PR! -->

## Why It's Good For The Game
Will hopefully allow others to look upon silicons work and understand it
easier. Allows me to write prettier and safer code for the vehicle
refactor.
<!-- Argue for the merits of your changes and how they benefit the game,
especially if they are controversial and/or far reaching. If you can't
actually explain WHY what you are doing will improve the game, then it
probably isn't good for the game in the first place. -->

## Changelog
-Comments a bunch of the functions because my brain is smooth
-Adds wrappers for cell.dm functions to allow for cleaner code lines
when dealing with this system
-Adds mob_remove_cell() for easier cell removal in vehicles update.
-"Updates" inducers to use the new wrappers (a single proc)
<!-- If your PR modifies aspects of the game that can be concretely
observed by players or admins you should add a changelog. If your change
does NOT meet this description, remove this section. Be sure to properly
mark your PRs to prevent unnecessary GBP loss. You can read up on GBP
and it's effects on PRs in the tgstation guides for contributors. Please
note that maintainers freely reserve the right to remove and add tags
should they deem it appropriate. You can attempt to finagle the system
all you want, but it's best to shoot for clear communication right off
the bat. -->

:cl: Shadowcat
code: Adds cell.dm function wrappers to cell_slot.dm
code: Adds a function to remove a cell and hand it directly to a move in
cell_slot.dm
refactor: Comments a lot of the functions in cell_slot.dm
/:cl:

<!-- Both :cl:'s are required for the changelog to work! You can put
your name to the right of the first :cl: if you want to overwrite your
GitHub username as author ingame. -->
<!-- You can use multiple of the same prefix (they're only used for the
icon ingame) and delete the unneeded ones. Despite some of the tags,
changelogs should generally represent how a player might be affected by
the changes rather than a summary of the PR's contents. -->

---------

Co-authored-by: LordME <58342752+TheLordME@users.noreply.github.com>
  • Loading branch information
Sorrowfulwinds and TheLordME authored Jul 20, 2024
1 parent edfb931 commit a4c3dcb
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 1 deletion.
2 changes: 1 addition & 1 deletion code/game/objects/items/inducer.dm
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
break
if(charged <= 0)
break
obj_cell_slot.cell.use(charged)
obj_cell_slot.use(charged)
used += charged

qdel(spark_system)
Expand Down
119 changes: 119 additions & 0 deletions code/game/objects/systems/cell_slot.dm
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@
// todo: kill this
var/legacy_use_device_cells = FALSE

/**
* returns TRUE if slot accepts this type of cell
*/
/datum/object_system/cell_slot/proc/accepts_cell(obj/item/cell/cell)
return legacy_use_device_cells? istype(cell, /obj/item/cell/device) : TRUE

/**
* removes cell from the system and drops it at new_loc
*/
/datum/object_system/cell_slot/proc/remove_cell(atom/new_loc)
if(isnull(cell))
return
Expand All @@ -48,6 +54,22 @@
cell = null
parent.object_cell_slot_removed(., src)

/**
* helper to have a mob yank a cell
*
* * this does not check adjacency!
* * puts the cell in their hand if possible, otherwise drops it on them
*/
/datum/object_system/cell_slot/proc/mob_yank_cell(mob/user)
var/obj/item/cell/removed = remove_cell(user)
if(!removed)
return
user.put_in_hands_or_drop(removed)
return removed

/**
* replaces the existing cell with the inserted cell, dropping the old cell
*/
/datum/object_system/cell_slot/proc/insert_cell(obj/item/cell/cell)
if(!isnull(cell))
. = remove_cell(parent.drop_location())
Expand All @@ -56,9 +78,18 @@
cell.forceMove(parent)
parent.object_cell_slot_inserted(cell, src)

/**
* returns TRUE if the cell slot is mutable
*/
/datum/object_system/cell_slot/proc/interaction_active(mob/user)
return parent.object_cell_slot_mutable(user, src)

/**
* returns TRUE if the slot has a cell
*/
/datum/object_system/cell_slot/proc/has_cell()
return !isnull(cell)

//? Hooks

/**
Expand Down Expand Up @@ -101,3 +132,91 @@
obj_cell_slot.remove_yank_time = 0
obj_cell_slot.legacy_use_device_cells = TRUE
return obj_cell_slot

//? Wrappers for cell.dm functions
//+ These exist to sanely perform cell functions through the obj_cell_slot system. These break safely if no cell exists.

/**
* cell function wrapper - returns the rating of the cell inside or 0 if null
*/
/datum/object_system/cell_slot/proc/get_rating()
return cell?.get_rating() || 0

/**
* cell function wrapper - returns the cell src or FALSE if null
*/
/datum/object_system/cell_slot/proc/get_cell(inducer)
return cell?.get_cell(inducer) || FALSE

/**
* cell function passthrough - consumes energy using *universal units*
*
* *Uses universal units.*
*
* @params
* - actor - thing draining, can be null
* - amount - amount to drain in kilojoules
* - flags
*
* @return Amount drained
*/
/datum/object_system/cell_slot/proc/drain_energy(datum/actor, amount, flags)
return cell?.drain_energy(actor, amount, flags) || 0

/**
* cell function wrapper - returns % charge of the cell or 0 if null
*/
/datum/object_system/cell_slot/proc/percent()
return cell?.percent() || 0

/**
* cell function wrapper - checks if cell is fully charged
* returns TRUE or FALSE. returns FALSE if cell is null
*/
/datum/object_system/cell_slot/proc/fully_charged()
return cell?.fully_charged() ? TRUE : FALSE

/**
* cell function wrapper - returns true if cell can provide specified amount
* returns 0 if null
*/
/datum/object_system/cell_slot/proc/check_charge(var/amount)
return cell?.check_charge(amount) || 0

/**
* cell function wrapper - returns how much charge is missing from the cell or 0 if null
*/
/datum/object_system/cell_slot/proc/amount_missing()
return cell?.amount_missing() || 0

/**
* cell function wrapper - attempts to use power from cell, returns the amount actually used or 0 if null
*/
/datum/object_system/cell_slot/proc/use(var/amount)
return cell?.use(amount) || 0

/**
* cell function wrapper - checks if the specified amount can be provided. If it can, it removes the amount from the cell and returns TRUE otherwise does nothing and returns FALSE
* returns FALSE if cell is null
*/
/datum/object_system/cell_slot/proc/checked_use(var/amount)
return cell?.checked_use(amount) ? TRUE : FALSE

/**
* cell function wrapper - use x cell units, affected by GLOB.cellefficiency, returns the amount actually used or 0 if null
*/
/datum/object_system/cell_slot/proc/use_scaled(var/amount)
return cell?.use_scaled(amount) || 0

/**
* cell function wrapper - checked_use() but scaled by GLOB.cellefficiency
*/
/datum/object_system/cell_slot/proc/checked_use_scaled(var/amount)
return cell?.checked_use_scaled(amount) ? TRUE : FALSE

/**
* cell function wrapper - recharge the cell by x amount returns the amount consumed or 0 if cell is null
*/
/datum/object_system/cell_slot/proc/give(var/amount)
return cell?.give(amount) || 0
//? End wrappers for cell.dm functions

0 comments on commit a4c3dcb

Please sign in to comment.