Files
Bubberstation/code/modules/wiremod/components/math/arithmetic.dm
CRITAWAKETS 36212e59e1 Adds in the modulo operator to the circuit arithmetic component. (#88253)
## About The Pull Request


![image](https://github.com/user-attachments/assets/17a5d230-c10f-4fda-b782-0d59c2797bff)
This PR just adds in modulo to the circuit arithmetic component.

## Why It's Good For The Game

You _can_ calculate it yourself, but then it ends up extremely unoptimal
and you're usually better off not doing it. It also ends up really hard
to read.
I want to see more clever circuits and computing things.

## Changelog
🆑
add: Added in the modulo operator to the circuit arithmetic component.
/🆑
2024-11-29 04:28:18 +01:00

96 lines
2.6 KiB
Plaintext

#define COMP_ARITHMETIC_ADD "Add"
#define COMP_ARITHMETIC_SUBTRACT "Subtract"
#define COMP_ARITHMETIC_MULTIPLY "Multiply"
#define COMP_ARITHMETIC_DIVIDE "Divide"
#define COMP_ARITHMETIC_MODULO "Modulo"
#define COMP_ARITHMETIC_MIN "Minimum"
#define COMP_ARITHMETIC_MAX "Maximum"
/**
* # 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."
category = "Math"
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
ui_buttons = list(
"plus" = "add",
"minus" = "remove"
)
/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_MODULO,
COMP_ARITHMETIC_MIN,
COMP_ARITHMETIC_MAX,
)
arithmetic_option = add_option_port("Arithmetic Option", component_options)
/obj/item/circuit_component/arithmetic/populate_ports()
arithmetic_ports = list()
AddComponent(/datum/component/circuit_component_add_port, \
port_list = arithmetic_ports, \
add_action = "add", \
remove_action = "remove", \
port_type = PORT_TYPE_NUMBER, \
prefix = "Port", \
minimum_amount = 2 \
)
output = add_output_port("Output", PORT_TYPE_NUMBER, order = 1.1)
/obj/item/circuit_component/arithmetic/input_received(datum/port/input/port)
var/list/ports = arithmetic_ports.Copy()
var/datum/port/input/first_port = popleft(ports)
var/result = first_port.value
for(var/datum/port/input/input_port as anything in ports)
var/value = input_port.value
if(isnull(value))
continue
switch(arithmetic_option.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_MODULO)
result %= value
if(COMP_ARITHMETIC_MAX)
result = max(result, value)
if(COMP_ARITHMETIC_MIN)
result = min(result, value)
output.set_output(result)
#undef COMP_ARITHMETIC_ADD
#undef COMP_ARITHMETIC_SUBTRACT
#undef COMP_ARITHMETIC_MULTIPLY
#undef COMP_ARITHMETIC_DIVIDE
#undef COMP_ARITHMETIC_MODULO
#undef COMP_ARITHMETIC_MIN
#undef COMP_ARITHMETIC_MAX