Files
Bubberstation/code/modules/wiremod/components/list/assoc_literal.dm
SkyratBot 2b5cfd484f [MIRROR] Adds a user type to integrated circuits, refactors the list pick component. [MDB IGNORE] (#24827)
* Adds a user type to integrated circuits, refactors the list pick component. (#79412)

## About The Pull Request
Added a user type to integrated circuits that can't be stored as a user
type but can be typecasted to entity. Useful for components that
directly ask for an input from the user, like the list pick component.

Refactored the list pick component to use this user port and to also
send failure signals whenever a success signal is not sent.
Removed the triggered port for the list pick component.

Also fixes a runtime that occurs with the list pick component if the
list passed in only contains null values.

## Why It's Good For The Game
Can't force a prompt onto people who haven't interacted with your
circuit.

## Changelog
🆑
add: Added a user type to integrated circuits
/🆑

---------

Co-authored-by: Watermelon914 <3052169-Watermelon914@ users.noreply.gitlab.com>

* Adds a user type to integrated circuits, refactors the list pick component.

---------

Co-authored-by: Watermelon914 <37270891+Watermelon914@users.noreply.github.com>
Co-authored-by: Watermelon914 <3052169-Watermelon914@ users.noreply.gitlab.com>
2023-11-06 21:21:54 -05:00

77 lines
2.7 KiB
Plaintext

/**
* # Assoc List Literal Component
*
* Return an associative list literal.
*/
/obj/item/circuit_component/assoc_literal
display_name = "Associative List Literal"
desc = "A component that returns an associative list consisting of the inputs."
category = "List"
circuit_flags = CIRCUIT_FLAG_INPUT_SIGNAL|CIRCUIT_FLAG_OUTPUT_SIGNAL
/// The list type
var/datum/port/input/option/list_options
/// The inputs used to create the list
var/list/datum/port/input/entry_ports = list()
/// The inputs used to create the list
var/list/datum/port/input/key_ports = list()
/// The result from the output
var/datum/port/output/list_output
ui_buttons = list(
"plus" = "add",
"minus" = "remove"
)
var/max_list_count = 100
/obj/item/circuit_component/assoc_literal/pre_input_received(datum/port/input/port)
if(port == list_options)
var/new_datatype = list_options.value
list_output.set_datatype(PORT_TYPE_ASSOC_LIST(PORT_TYPE_STRING, new_datatype))
for(var/datum/port/input/port_to_set as anything in entry_ports)
port_to_set.set_datatype(new_datatype)
/obj/item/circuit_component/assoc_literal/populate_options()
list_options = add_option_port("List Type", GLOB.wiremod_basic_types)
/obj/item/circuit_component/assoc_literal/populate_ports()
AddComponent(/datum/component/circuit_component_add_port, \
port_list = key_ports, \
add_action = "add", \
remove_action = "remove", \
port_type = PORT_TYPE_STRING, \
prefix = "Key", \
minimum_amount = 1, \
maximum_amount = 20 \
)
AddComponent(/datum/component/circuit_component_add_port, \
port_list = entry_ports, \
add_action = "add", \
remove_action = "remove", \
port_type = PORT_TYPE_ANY, \
prefix = "Value", \
minimum_amount = 1, \
maximum_amount = 20 \
)
list_output = add_output_port("Value", PORT_TYPE_ASSOC_LIST(PORT_TYPE_STRING, PORT_TYPE_ANY), order = 1.1)
/obj/item/circuit_component/assoc_literal/input_received(datum/port/input/port)
var/list/new_literal = list()
var/datum/circuit_datatype/value_handler = GLOB.circuit_datatypes[list_options.value]
var/datum/circuit_datatype/key_handler = GLOB.circuit_datatypes[PORT_TYPE_STRING]
for(var/index in 1 to length(entry_ports))
// To prevent people from infinitely making lists to crash the server
if(islist(entry_ports[index].value) && get_list_count(entry_ports[index].value, max_list_count) >= max_list_count)
visible_message("[src] begins to overheat!")
return
var/value_to_add = value_handler.convert_value(port, entry_ports[index].value)
if(isdatum(value_to_add))
value_to_add = WEAKREF(value_to_add)
new_literal[key_handler.convert_value(port, key_ports[index].value)] = value_to_add
list_output.set_output(new_literal)