mirror of
https://github.com/Bubberstation/Bubberstation.git
synced 2026-07-20 04:26:03 +01:00
[MIRROR] More circuit components. Restructures the circuit components folder to be more organised. (#6142)
* 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>
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* # 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)
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* # Comparison Component
|
||||
*
|
||||
* Compares two objects
|
||||
*/
|
||||
/obj/item/circuit_component/compare/comparison
|
||||
display_name = "Comparison"
|
||||
|
||||
input_port_amount = 2
|
||||
|
||||
GLOBAL_LIST_INIT(comp_comparison_options, list(
|
||||
COMP_COMPARISON_EQUAL,
|
||||
COMP_COMPARISON_NOT_EQUAL,
|
||||
COMP_COMPARISON_GREATER_THAN,
|
||||
COMP_COMPARISON_LESS_THAN,
|
||||
COMP_COMPARISON_GREATER_THAN_OR_EQUAL,
|
||||
COMP_COMPARISON_LESS_THAN_OR_EQUAL
|
||||
))
|
||||
|
||||
/obj/item/circuit_component/compare/comparison/Initialize()
|
||||
options = GLOB.comp_comparison_options
|
||||
return ..()
|
||||
|
||||
/obj/item/circuit_component/compare/comparison/do_comparisons(list/ports)
|
||||
if(length(ports) < input_port_amount)
|
||||
return FALSE
|
||||
|
||||
// Comparison component only compares the first two ports
|
||||
var/input1 = input_ports[1].input_value
|
||||
var/input2 = input_ports[2].input_value
|
||||
|
||||
switch(current_option)
|
||||
if(COMP_COMPARISON_EQUAL)
|
||||
return input1 == input2
|
||||
if(COMP_COMPARISON_NOT_EQUAL)
|
||||
return input1 != input2
|
||||
if(COMP_COMPARISON_GREATER_THAN)
|
||||
return input1 > input2
|
||||
if(COMP_COMPARISON_GREATER_THAN_OR_EQUAL)
|
||||
return input1 >= input2
|
||||
if(COMP_COMPARISON_LESS_THAN)
|
||||
return input1 < input2
|
||||
if(COMP_COMPARISON_LESS_THAN_OR_EQUAL)
|
||||
return input1 <= input2
|
||||
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* # Index Component
|
||||
*
|
||||
* Return the index of a list
|
||||
*/
|
||||
/obj/item/circuit_component/index
|
||||
display_name = "Index List"
|
||||
|
||||
/// The input port
|
||||
var/datum/port/input/list_port
|
||||
var/datum/port/input/index_port
|
||||
|
||||
/// The result from the output
|
||||
var/datum/port/output/output
|
||||
circuit_flags = CIRCUIT_FLAG_INPUT_SIGNAL|CIRCUIT_FLAG_OUTPUT_SIGNAL
|
||||
|
||||
/obj/item/circuit_component/index/Initialize()
|
||||
. = ..()
|
||||
index_port = add_input_port("Index", PORT_TYPE_ANY)
|
||||
list_port = add_input_port("List", PORT_TYPE_LIST)
|
||||
|
||||
output = add_output_port("Value", PORT_TYPE_ANY)
|
||||
|
||||
/obj/item/circuit_component/index/Destroy()
|
||||
list_port = null
|
||||
index_port = null
|
||||
output = null
|
||||
return ..()
|
||||
|
||||
/obj/item/circuit_component/index/input_received(datum/port/input/port)
|
||||
. = ..()
|
||||
if(.)
|
||||
return
|
||||
|
||||
var/index = index_port.input_value
|
||||
var/list/list_input = list_port.input_value
|
||||
|
||||
if(!islist(list_input) || !index)
|
||||
output.set_output(null)
|
||||
return
|
||||
|
||||
if(isnum(index) && (index < 1 || index > length(list_input)))
|
||||
output.set_output(null)
|
||||
return
|
||||
|
||||
output.set_output(list_input[index])
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* # Length Component
|
||||
*
|
||||
* Return the length of an input
|
||||
*/
|
||||
/obj/item/circuit_component/length
|
||||
display_name = "Length"
|
||||
|
||||
/// The input port
|
||||
var/datum/port/input/input_port
|
||||
|
||||
/// The result from the output
|
||||
var/datum/port/output/output
|
||||
circuit_flags = CIRCUIT_FLAG_INPUT_SIGNAL|CIRCUIT_FLAG_OUTPUT_SIGNAL
|
||||
|
||||
/obj/item/circuit_component/length/Initialize()
|
||||
. = ..()
|
||||
input_port = add_input_port("Input", PORT_TYPE_ANY)
|
||||
|
||||
output = add_output_port("Length", PORT_TYPE_NUMBER)
|
||||
|
||||
/obj/item/circuit_component/length/Destroy()
|
||||
input_port = null
|
||||
output = null
|
||||
return ..()
|
||||
|
||||
/obj/item/circuit_component/length/input_received(datum/port/input/port)
|
||||
. = ..()
|
||||
if(.)
|
||||
return
|
||||
|
||||
output.set_output(length(input_port.input_value))
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* # Logic Component
|
||||
*
|
||||
* General logic unit with AND OR capabilities
|
||||
*/
|
||||
/obj/item/circuit_component/compare/logic
|
||||
display_name = "Logic"
|
||||
|
||||
|
||||
GLOBAL_LIST_INIT(comp_logic_options, list(
|
||||
COMP_LOGIC_AND,
|
||||
COMP_LOGIC_OR,
|
||||
COMP_LOGIC_XOR
|
||||
))
|
||||
|
||||
/obj/item/circuit_component/compare/logic/Initialize()
|
||||
options = GLOB.comp_logic_options
|
||||
return ..()
|
||||
|
||||
/obj/item/circuit_component/compare/logic/do_comparisons(list/ports)
|
||||
. = FALSE
|
||||
// Used by XOR
|
||||
var/total_ports = 0
|
||||
var/total_true_ports = 0
|
||||
for(var/datum/port/input/port as anything in ports)
|
||||
if(isnull(port.input_value) && isnull(port.connected_port))
|
||||
continue
|
||||
|
||||
total_ports += 1
|
||||
switch(current_option)
|
||||
if(COMP_LOGIC_AND)
|
||||
if(!port.input_value)
|
||||
return FALSE
|
||||
. = TRUE
|
||||
if(COMP_LOGIC_OR)
|
||||
if(port.input_value)
|
||||
return TRUE
|
||||
if(COMP_LOGIC_XOR)
|
||||
if(port.input_value)
|
||||
. = TRUE
|
||||
total_true_ports += 1
|
||||
|
||||
if(current_option == COMP_LOGIC_XOR)
|
||||
if(total_ports == total_true_ports)
|
||||
return FALSE
|
||||
if(.)
|
||||
return TRUE
|
||||
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* # Logic Component
|
||||
*
|
||||
* General logic unit with AND OR capabilities
|
||||
*/
|
||||
/obj/item/circuit_component/not
|
||||
display_name = "Not"
|
||||
|
||||
/// The input port
|
||||
var/datum/port/input/input_port
|
||||
|
||||
/// The result from the output
|
||||
var/datum/port/output/result
|
||||
circuit_flags = CIRCUIT_FLAG_INPUT_SIGNAL|CIRCUIT_FLAG_OUTPUT_SIGNAL
|
||||
|
||||
/obj/item/circuit_component/not/Initialize()
|
||||
. = ..()
|
||||
input_port = add_input_port("Input", PORT_TYPE_ANY)
|
||||
|
||||
result = add_output_port("Result", PORT_TYPE_NUMBER)
|
||||
|
||||
/obj/item/circuit_component/not/Destroy()
|
||||
input_port = null
|
||||
result = null
|
||||
return ..()
|
||||
|
||||
/obj/item/circuit_component/not/input_received(datum/port/input/port)
|
||||
. = ..()
|
||||
if(.)
|
||||
return
|
||||
|
||||
result.set_output(!input_port.input_value)
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* # Random Component
|
||||
*
|
||||
* Generates a random number between specific values
|
||||
*/
|
||||
/obj/item/circuit_component/random
|
||||
display_name = "Random"
|
||||
|
||||
/// The minimum value that the random number can be
|
||||
var/datum/port/input/minimum
|
||||
/// The maximum value that the random number can be
|
||||
var/datum/port/input/maximum
|
||||
|
||||
circuit_flags = CIRCUIT_FLAG_INPUT_SIGNAL|CIRCUIT_FLAG_OUTPUT_SIGNAL
|
||||
|
||||
/// The result from the output
|
||||
var/datum/port/output/output
|
||||
|
||||
/obj/item/circuit_component/random/Initialize()
|
||||
. = ..()
|
||||
minimum = add_input_port("Minimum", PORT_TYPE_NUMBER, FALSE)
|
||||
maximum = add_input_port("Maximum", PORT_TYPE_NUMBER, FALSE)
|
||||
|
||||
output = add_output_port("Output", PORT_TYPE_NUMBER)
|
||||
|
||||
/obj/item/circuit_component/random/Destroy()
|
||||
minimum = null
|
||||
maximum = null
|
||||
output = null
|
||||
return ..()
|
||||
|
||||
/obj/item/circuit_component/random/input_received(datum/port/input/port)
|
||||
. = ..()
|
||||
if(.)
|
||||
return
|
||||
|
||||
var/min_val = minimum.input_value || 0
|
||||
var/max_val = maximum.input_value || 0
|
||||
|
||||
if(max_val < min_val)
|
||||
output.set_output(0)
|
||||
return
|
||||
|
||||
output.set_output(rand(min_val, max_val))
|
||||
|
||||
Reference in New Issue
Block a user