mirror of
https://github.com/Bubberstation/Bubberstation.git
synced 2026-01-18 21:15:21 +00:00
Currently, every stock part is created inside every machine on
initialize. This is costing us about 300ms. This was likely done because
a lot of similar code that I've been ripping out recently has been
related to the singularity being a common source of destruction, so
everything created its contents to be able to move later to reduce lag.
Unfortunately, machines are a lot more complicated than paper bins or
decks of cards. They need to be able to exchange parts, but also the
following pattern is very common:
```dm
// component_parts is sometimes, incorrectly, contents
for (var/obj/item/stock_parts/capacitor/C in component_parts)
```
That means the parts have to exist *somewhere*. I played with creating a
single one and passing it immutably around component_parts, but it was a
nightmare.
---
Instead, this PR creates `/datum/stock_part`, a singleton instance that
maps to a specific part and tier. Machines specify their requirements
using these stock part datums, which are not initialized, but should
function exactly like normal stock parts.
Interface looks like this:
```dm
/datum/stock_part/scanning_module
tier = 1
physical_object_type = /obj/item/stock_parts/scanning_module
physical_object_base_type = /obj/item/stock_parts/scanning_module
/datum/stock_part/scanning_module/tier2
tier = 2
physical_object_type = /obj/item/stock_parts/scanning_module/adv
/datum/stock_part/scanning_module/tier3
tier = 3
physical_object_type = /obj/item/stock_parts/scanning_module/phasic
/datum/stock_part/scanning_module/tier4
tier = 4
physical_object_type = /obj/item/stock_parts/scanning_module/triphasic
```
And to use:
```patch
- for (var/obj/item/stock_parts/capacitor/C in component_parts)
+ for (var/datum/stock_part/capacitor/C in component_parts)
```
```patch
req_components = list(
/obj/item/stack/glass = 2,
- /obj/item/stock_parts/capacitor = 1,
+ /datum/stock_part/capacitor = 1,
)
```
This PR currently only updates scanning modules. There are a very small
handful of machines that actually use these, which makes them a good
starting point for focusing on the system itself. After this PR is
confirmed to be working and merged, I'll make PRs for every other core
type.
Also, unlike paper bins, we do not hold onto the part that was put into
the machine, it is completely deleted. This means that if the stock part
has any unique qualities for whatever reason, they won't be preserved.
Only thing I can think of is RPG stats but the code is very much made
more complex with that support.
---
To maintainers: Hide whitespace mode recommended

🆑
fix: Fixed gulag teleporters not containing the correct parts.
/🆑
Co-authored-by: John Willard <53777086+JohnFulpWillard@users.noreply.github.com>