tgui backend
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
#define COMP_ARITHMETIC_ADD "Add"
|
||||
#define COMP_ARITHMETIC_SUBTRACT "Subtract"
|
||||
#define COMP_ARITHMETIC_MULTIPLY "Multiply"
|
||||
#define COMP_ARITHMETIC_DIVIDE "Divide"
|
||||
#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."
|
||||
|
||||
/// 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.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_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_MIN
|
||||
#undef COMP_ARITHMETIC_MAX
|
||||
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* # Comparison Component
|
||||
*
|
||||
* Compares two objects
|
||||
*/
|
||||
/obj/item/circuit_component/compare/comparison
|
||||
display_name = "Comparison"
|
||||
desc = "A component that compares two objects."
|
||||
|
||||
var/datum/port/input/option/comparison_option
|
||||
|
||||
input_port_amount = 2
|
||||
var/current_type = PORT_TYPE_ANY
|
||||
|
||||
/obj/item/circuit_component/compare/comparison/populate_options()
|
||||
var/static/component_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,
|
||||
)
|
||||
comparison_option = add_option_port("Comparison Option", component_options)
|
||||
|
||||
/obj/item/circuit_component/compare/comparison/input_received(datum/port/input/port)
|
||||
switch(comparison_option.value)
|
||||
if(COMP_COMPARISON_EQUAL, COMP_COMPARISON_NOT_EQUAL)
|
||||
if(current_type != PORT_TYPE_ANY)
|
||||
current_type = PORT_TYPE_ANY
|
||||
compare_ports[1].set_datatype(PORT_TYPE_ANY)
|
||||
compare_ports[2].set_datatype(PORT_TYPE_ANY)
|
||||
else
|
||||
if(current_type != PORT_TYPE_NUMBER)
|
||||
current_type = PORT_TYPE_NUMBER
|
||||
compare_ports[1].set_datatype(PORT_TYPE_NUMBER)
|
||||
compare_ports[2].set_datatype(PORT_TYPE_NUMBER)
|
||||
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 = compare_ports[1].value
|
||||
var/input2 = compare_ports[2].value
|
||||
var/current_option = comparison_option.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,29 @@
|
||||
/**
|
||||
* # Length Component
|
||||
*
|
||||
* Return the length of an input
|
||||
*/
|
||||
/obj/item/circuit_component/length
|
||||
display_name = "Length"
|
||||
desc = "A component that returns the length of its input."
|
||||
|
||||
/// 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/input_received(datum/port/input/port)
|
||||
. = ..()
|
||||
if(.)
|
||||
return
|
||||
|
||||
output.set_output(length(input_port.value))
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
#define COMP_LOGIC_AND "AND"
|
||||
#define COMP_LOGIC_OR "OR"
|
||||
#define COMP_LOGIC_XOR "XOR"
|
||||
|
||||
/**
|
||||
* # Logic Component
|
||||
*
|
||||
* General logic unit with AND OR capabilities
|
||||
*/
|
||||
/obj/item/circuit_component/compare/logic
|
||||
display_name = "Logic"
|
||||
desc = "A component with 'and' and 'or' capabilities."
|
||||
|
||||
var/datum/port/input/option/logic_options
|
||||
|
||||
/obj/item/circuit_component/compare/logic/populate_options()
|
||||
var/static/component_options = list(
|
||||
COMP_LOGIC_AND,
|
||||
COMP_LOGIC_OR,
|
||||
COMP_LOGIC_XOR,
|
||||
)
|
||||
logic_options = add_option_port("Logic Options", component_options)
|
||||
|
||||
/obj/item/circuit_component/compare/logic/do_comparisons(list/ports)
|
||||
. = FALSE
|
||||
var/current_option = logic_options.value
|
||||
|
||||
// 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.value) && length(port.connected_ports) == 0)
|
||||
continue
|
||||
|
||||
total_ports += 1
|
||||
switch(current_option)
|
||||
if(COMP_LOGIC_AND)
|
||||
if(!port.value)
|
||||
return FALSE
|
||||
. = TRUE
|
||||
if(COMP_LOGIC_OR)
|
||||
if(port.value)
|
||||
return TRUE
|
||||
if(COMP_LOGIC_XOR)
|
||||
if(port.value)
|
||||
. = TRUE
|
||||
total_true_ports += 1
|
||||
|
||||
if(current_option == COMP_LOGIC_XOR)
|
||||
if(total_ports == total_true_ports)
|
||||
return FALSE
|
||||
if(.)
|
||||
return TRUE
|
||||
|
||||
#undef COMP_LOGIC_AND
|
||||
#undef COMP_LOGIC_OR
|
||||
#undef COMP_LOGIC_XOR
|
||||
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* # Logic Component
|
||||
*
|
||||
* General logic unit with AND OR capabilities
|
||||
*/
|
||||
/obj/item/circuit_component/not
|
||||
display_name = "Not"
|
||||
desc = "A component that inverts its input."
|
||||
|
||||
/// 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/input_received(datum/port/input/port)
|
||||
. = ..()
|
||||
if(.)
|
||||
return
|
||||
|
||||
result.set_output(!input_port.value)
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* # Random Component
|
||||
*
|
||||
* Generates a random number between specific values
|
||||
*/
|
||||
/obj/item/circuit_component/random
|
||||
display_name = "Random"
|
||||
desc = "A component that returns random values."
|
||||
|
||||
/// 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/input_received(datum/port/input/port)
|
||||
. = ..()
|
||||
if(.)
|
||||
return
|
||||
|
||||
var/min_val = minimum.value || 0
|
||||
var/max_val = maximum.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