mirror of
https://github.com/Bubberstation/Bubberstation.git
synced 2026-01-02 04:52:10 +00:00
* More circuit components. Restructures the circuit components folder to be more organised. * Mirror! Co-authored-by: Watermelon914 <37270891+Watermelon914@users.noreply.github.com> Co-authored-by: Funce <funce.973@gmail.com>
76 lines
1.8 KiB
Plaintext
76 lines
1.8 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"
|
|
|
|
/// The amount of input ports to have
|
|
var/input_port_amount = 4
|
|
|
|
/// The result from the output
|
|
var/datum/port/output/output
|
|
|
|
circuit_flags = CIRCUIT_FLAG_INPUT_SIGNAL|CIRCUIT_FLAG_OUTPUT_SIGNAL
|
|
|
|
GLOBAL_LIST_INIT(comp_arithmetic_options, list(
|
|
COMP_ARITHMETIC_ADD,
|
|
COMP_ARITHMETIC_SUBTRACT,
|
|
COMP_ARITHMETIC_MULTIPLY,
|
|
COMP_ARITHMETIC_DIVIDE,
|
|
COMP_ARITHMETIC_MIN,
|
|
COMP_ARITHMETIC_MAX,
|
|
))
|
|
|
|
/obj/item/circuit_component/arithmetic/Initialize()
|
|
options = GLOB.comp_arithmetic_options
|
|
. = ..()
|
|
for(var/port_id in 1 to input_port_amount)
|
|
var/letter = ascii2text(text2ascii("A") + (port_id-1))
|
|
add_input_port(letter, PORT_TYPE_NUMBER)
|
|
|
|
output = add_output_port("Output", PORT_TYPE_NUMBER)
|
|
|
|
/obj/item/circuit_component/arithmetic/Destroy()
|
|
output = null
|
|
return ..()
|
|
|
|
/obj/item/circuit_component/arithmetic/input_received(datum/port/input/port)
|
|
. = ..()
|
|
if(.)
|
|
return
|
|
|
|
var/list/ports = input_ports.Copy()
|
|
var/datum/port/input/first_port = ports[1]
|
|
ports -= first_port
|
|
ports -= trigger_input
|
|
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(current_option)
|
|
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)
|
|
|