Adds Circuit variables (#60590)

Co-authored-by: Watermelon914 <3052169-Watermelon914@users.noreply.gitlab.com>
This commit is contained in:
Watermelon914
2021-08-12 18:30:50 -07:00
committed by GitHub
co-authored by Watermelon914
parent 63e8466f39
commit 05ede8dc30
20 changed files with 494 additions and 106 deletions
@@ -211,7 +211,7 @@
/obj/item/circuit_component/module/ui_static_data(mob/user)
. = list()
.["global_port_types"] = GLOB.wiremod_types
.["global_port_types"] = GLOB.wiremod_basic_types
/obj/item/circuit_component/module/attackby(obj/item/I, mob/living/user, params)
if(istype(I, /obj/item/circuit_component))
@@ -280,7 +280,7 @@
if(action == "set_port_type")
var/type = params["port_type"]
if(!(type in GLOB.wiremod_types))
if(!(type in GLOB.wiremod_basic_types))
return
component_port.set_datatype(type)
internal_component_port.set_datatype(type)
@@ -45,10 +45,10 @@
/obj/item/circuit_component/light/input_received(datum/port/input/port)
. = ..()
brightness.set_input(clamp(brightness.value || 0, 0, max_power))
red.set_input(clamp(red.value, 0, 255))
blue.set_input(clamp(blue.value, 0, 255))
green.set_input(clamp(green.value, 0, 255))
brightness.set_value(clamp(brightness.value || 0, 0, max_power))
red.set_value(clamp(red.value, 0, 255))
blue.set_value(clamp(blue.value, 0, 255))
green.set_value(clamp(green.value, 0, 255))
var/list/hsl = rgb2hsl(red.value || 0, green.value || 0, blue.value || 0)
var/list/light_col = hsl2rgb(hsl[1], hsl[2], max(min_lightness, hsl[3]))
shell_light_color = rgb(light_col[1], light_col[2], light_col[3])
@@ -45,7 +45,7 @@
/obj/item/circuit_component/radio/input_received(datum/port/input/port)
. = ..()
freq.set_input(sanitize_frequency(freq.value, TRUE))
freq.set_value(sanitize_frequency(freq.value, TRUE))
if(.)
return
var/frequency = freq.value
@@ -49,8 +49,8 @@
/obj/item/circuit_component/soundemitter/input_received(datum/port/input/port)
. = ..()
volume.set_input(clamp(volume.value, 0, 100))
frequency.set_input(clamp(frequency.value, -100, 100))
volume.set_value(clamp(volume.value, 0, 100))
frequency.set_value(clamp(frequency.value, -100, 100))
if(.)
return
@@ -0,0 +1,70 @@
/**
* # Getter Component
*
* Gets the current value from a variable.
*/
/obj/item/circuit_component/getter
display_name = "Variable Getter"
desc = "A component that gets a variable globally on the circuit."
/// Variable name
var/datum/port/input/option/variable_name
/// The value of the variable
var/datum/port/output/value
var/datum/circuit_variable/current_variable
/obj/item/circuit_component/getter/populate_options()
variable_name = add_option_port("Variable", null)
/obj/item/circuit_component/getter/add_to(obj/item/integrated_circuit/added_to)
. = ..()
variable_name.possible_options = added_to.circuit_variables
/obj/item/circuit_component/getter/removed_from(obj/item/integrated_circuit/removed_from)
variable_name.possible_options = null
return ..()
/obj/item/circuit_component/getter/Initialize()
. = ..()
value = add_output_port("Value", PORT_TYPE_ANY)
/obj/item/circuit_component/getter/input_received(datum/port/input/port)
. = ..()
// We don't care much about the parent's return value. We only care if the parent exists
// since this should never really fail.
if(!parent)
return
var/variable_string = variable_name.value
if(!variable_string)
remove_current_variable()
value.set_output(null)
return
var/datum/circuit_variable/variable = parent.circuit_variables[variable_string]
if(!variable)
remove_current_variable()
value.set_output(null)
return
set_current_variable(variable)
value.set_output(variable.value)
/obj/item/circuit_component/getter/proc/remove_current_variable()
SIGNAL_HANDLER
if(current_variable)
current_variable.remove_listener(src)
UnregisterSignal(current_variable, COMSIG_PARENT_QDELETING)
current_variable = null
/obj/item/circuit_component/getter/proc/set_current_variable(datum/circuit_variable/variable)
if(variable == current_variable)
return
remove_current_variable()
current_variable = variable
current_variable.add_listener(src)
RegisterSignal(current_variable, COMSIG_PARENT_QDELETING, .proc/remove_current_variable)
value.set_datatype(variable.datatype)
@@ -1,72 +0,0 @@
/**
* # RAM Component
*
* Stores the current input when triggered.
* Players will need to think logically when using the RAM component
* as there can be race conditions due to the delays of transferring signals
*/
/obj/item/circuit_component/ram
display_name = "RAM"
desc = "A component that retains a variable."
circuit_flags = CIRCUIT_FLAG_OUTPUT_SIGNAL
var/datum/port/input/option/ram_options
/// The input to store
var/datum/port/input/input_port
/// The trigger to store the current value of the input
var/datum/port/input/trigger
/// Clears the current input
var/datum/port/input/clear
/// The current set value
var/datum/port/output/output
var/current_type
/obj/item/circuit_component/ram/populate_options()
var/static/component_options = list(
PORT_TYPE_ANY,
PORT_TYPE_STRING,
PORT_TYPE_NUMBER,
PORT_TYPE_LIST,
PORT_TYPE_ATOM,
PORT_TYPE_SIGNAL,
)
ram_options = add_option_port("RAM Options", component_options)
/obj/item/circuit_component/ram/Initialize()
. = ..()
input_port = add_input_port("Input", PORT_TYPE_ANY)
trigger = add_input_port("Store", PORT_TYPE_SIGNAL)
clear = add_input_port("Clear", PORT_TYPE_SIGNAL)
output = add_output_port("Stored Value", PORT_TYPE_ANY)
/obj/item/circuit_component/ram/Destroy()
input_port = null
trigger = null
clear = null
output = null
return ..()
/obj/item/circuit_component/ram/input_received(datum/port/input/port)
. = ..()
if(current_type != ram_options.value)
current_type = ram_options.value
input_port.set_datatype(current_type)
output.set_datatype(current_type)
if(.)
return
if(COMPONENT_TRIGGERED_BY(clear, port))
output.set_output(null)
return
if(!COMPONENT_TRIGGERED_BY(trigger, port))
return TRUE
var/input_val = input_port.value
output.set_output(input_val)
@@ -0,0 +1,58 @@
/**
* # Setter Component
*
* Stores the current input when triggered into a variable.
*/
/obj/item/circuit_component/setter
display_name = "Variable Setter"
desc = "A component that sets a variable globally on the circuit."
circuit_flags = CIRCUIT_FLAG_OUTPUT_SIGNAL
/// Variable name
var/datum/port/input/option/variable_name
/// The input to store
var/datum/port/input/input_port
/// The trigger to store the current value of the input
var/datum/port/input/trigger
var/current_type
/obj/item/circuit_component/setter/populate_options()
variable_name = add_option_port("Variable", null)
/obj/item/circuit_component/setter/add_to(obj/item/integrated_circuit/added_to)
. = ..()
variable_name.possible_options = added_to.circuit_variables
/obj/item/circuit_component/setter/removed_from(obj/item/integrated_circuit/removed_from)
variable_name.possible_options = null
return ..()
/obj/item/circuit_component/setter/Initialize()
. = ..()
input_port = add_input_port("Input", PORT_TYPE_ANY)
trigger = add_input_port("Store", PORT_TYPE_SIGNAL)
/obj/item/circuit_component/setter/input_received(datum/port/input/port)
. = ..()
var/variable_string = variable_name.value
if(!variable_string)
return
var/datum/circuit_variable/variable = parent.circuit_variables[variable_string]
if(!variable)
return
if(variable.datatype != current_type)
current_type = variable.datatype
input_port.set_datatype(current_type)
if(.)
return
if(!COMPONENT_TRIGGERED_BY(trigger, port))
return TRUE
variable.set_value(input_port.value)