Files
Bubberstation/code/modules/wiremod/components/math/arithmetic.dm
SkyratBot 3ab1e7ebe2 [MIRROR] Refactors port types completely and adds the option type. Refactors options to use this new type (#7386)
* Refactors port types completely and adds the option type. Refactors options to use this new type (#60571)

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

* Refactors port types completely and adds the option type. Refactors options to use this new type

Co-authored-by: Watermelon914 <37270891+Watermelon914@users.noreply.github.com>
Co-authored-by: Watermelon914 <3052169-Watermelon914@ users.noreply.gitlab.com>
2021-08-04 18:30:56 +01:00

76 lines
2.0 KiB
Plaintext

/**
* # Arithmetic Component
*
* General arithmetic unit with add/sub/mult/divide capabilities
* This one only works with numbers.
*/
/obj/item/circuit_component/arithmetic
display_name = "Arithmetic"
desc = "General arithmetic component with arithmetic capabilities."
/// The amount of input ports to have
var/input_port_amount = 4
var/datum/port/input/option/arithmetic_option
/// The result from the output
var/datum/port/output/output
var/list/arithmetic_ports
circuit_flags = CIRCUIT_FLAG_INPUT_SIGNAL|CIRCUIT_FLAG_OUTPUT_SIGNAL
/obj/item/circuit_component/arithmetic/populate_options()
var/static/component_options = list(
COMP_ARITHMETIC_ADD,
COMP_ARITHMETIC_SUBTRACT,
COMP_ARITHMETIC_MULTIPLY,
COMP_ARITHMETIC_DIVIDE,
COMP_ARITHMETIC_MIN,
COMP_ARITHMETIC_MAX,
)
arithmetic_option = add_option_port("Arithmetic Option", component_options)
/obj/item/circuit_component/arithmetic/Initialize()
. = ..()
arithmetic_ports = list()
for(var/port_id in 1 to input_port_amount)
var/letter = ascii2text(text2ascii("A") + (port_id-1))
arithmetic_ports += add_input_port(letter, PORT_TYPE_NUMBER)
output = add_output_port("Output", PORT_TYPE_NUMBER)
/obj/item/circuit_component/arithmetic/input_received(datum/port/input/port)
. = ..()
if(.)
return
var/list/ports = arithmetic_ports.Copy()
var/datum/port/input/first_port = popleft(ports)
var/result = first_port.input_value
for(var/datum/port/input/input_port as anything in ports)
var/value = input_port.input_value
if(isnull(value))
continue
switch(arithmetic_option.input_value)
if(COMP_ARITHMETIC_ADD)
result += value
if(COMP_ARITHMETIC_SUBTRACT)
result -= value
if(COMP_ARITHMETIC_MULTIPLY)
result *= value
if(COMP_ARITHMETIC_DIVIDE)
// Protect from div by zero errors.
if(value == 0)
result = null
break
result /= value
if(COMP_ARITHMETIC_MAX)
result = max(result, value)
if(COMP_ARITHMETIC_MIN)
result = min(result, value)
output.set_output(result)