Binary and Decimal Conversion Components. (#61889)

Added a couple components that convert a number into an array of binary digits and viceversa.
May help players mak more complex and advanced components without filtering those lacking knowledge in the binary field.
This commit is contained in:
Ghom
2021-10-23 08:03:26 +01:00
committed by GitHub
parent e596e24f53
commit 585f1119e3
7 changed files with 145 additions and 0 deletions
@@ -0,0 +1,72 @@
/**
* # Binary Decimal Component
*
* Has a bit array on one side and a decimal number on the other.
*/
/obj/item/circuit_component/binary_decimal
display_name = "Decimal - Bit Array"
desc = "Splits a decimal number into an array of binary digits and vicecersa."
circuit_flags = CIRCUIT_FLAG_INPUT_SIGNAL|CIRCUIT_FLAG_OUTPUT_SIGNAL
/// One number
var/datum/port/output/number
/// Many binary digits
var/list/datum/port/bit_array = list()
var/array_size = 0
var/default_array_size = 8
var/min_size = 1 //Who in their right mind would use a 1-bit array for circuits anyway?
var/max_size = MAX_BITFIELD_SIZE
ui_buttons = list(
"plus" = "increase",
"minus" = "decrease"
)
/obj/item/circuit_component/binary_decimal/save_data_to_list(list/component_data)
. = ..()
component_data["array_size"] = array_size
/obj/item/circuit_component/binary_decimal/load_data_from_list(list/component_data)
set_array_size(component_data["array_size"])
return ..()
/obj/item/circuit_component/binary_decimal/proc/set_array_size(new_size)
if(new_size <= 0)
for(var/datum/port/port in bit_array)
remove_bit_port(port)
bit_array = list()
array_size = 0
return
while(array_size > new_size)
var/index = length(bit_array)
var/datum/port/output = bit_array[index]
bit_array -= output
remove_bit_port(output)
array_size--
while(array_size < new_size)
array_size++
var/index = length(bit_array)
bit_array += add_bit_port(index)
/obj/item/circuit_component/binary_decimal/proc/add_bit_port(index)
return
/obj/item/circuit_component/binary_decimal/proc/remove_bit_port(datum/port/to_remove)
return
/obj/item/circuit_component/binary_decimal/populate_ports()
set_array_size(default_array_size)
// Increase or decrease the array size
/obj/item/circuit_component/binary_decimal/ui_perform_action(mob/user, action)
switch(action)
if("increase")
set_array_size(min(array_size + 1, max_size))
if("decrease")
set_array_size(max(array_size - 1, min_size))
@@ -0,0 +1,26 @@
/**
* # Binary Conversion Component
*
* Return an array of binary digits from a number input.
*/
/obj/item/circuit_component/binary_decimal/binary_conversion
display_name = "Binary Conversion"
desc = "Splits a decimal number into an array of binary digits, or bits, represented as 1 or 0 and often used in boolean or binary operations like AND, OR and XOR."
/obj/item/circuit_component/binary_decimal/binary_conversion/populate_ports()
. = ..()
number = add_input_port("Number", PORT_TYPE_NUMBER)
/obj/item/circuit_component/binary_decimal/binary_conversion/add_bit_port(index)
return add_output_port("Bit [index]", PORT_TYPE_NUMBER)
/obj/item/circuit_component/binary_decimal/binary_conversion/remove_bit_port(datum/port/to_remove)
return remove_output_port(to_remove)
/obj/item/circuit_component/binary_decimal/binary_conversion/input_received(datum/port/input/port)
if(!array_size)
return
for(var/iteration in 1 to array_size)
var/datum/port/output/bit = bit_array[iteration]
bit.set_output(number.value & (2 ** (iteration - 1)))
@@ -0,0 +1,29 @@
/**
* # Decimal Conversion Component
*
* Return a number from an array of binary inputs.
*/
/obj/item/circuit_component/binary_decimal/decimal_conversion
display_name = "Decimal Conversion"
desc = "Merges an array of binary digits, or bits, represented as 1 or 0 and often used in boolean or binary operations, into a decimal number."
/obj/item/circuit_component/binary_decimal/decimal_conversion/populate_ports()
. = ..()
number = add_output_port("Number", PORT_TYPE_NUMBER)
/obj/item/circuit_component/binary_decimal/decimal_conversion/add_bit_port(index)
return add_input_port("Bit [index]", PORT_TYPE_NUMBER)
/obj/item/circuit_component/binary_decimal/decimal_conversion/remove_bit_port(datum/port/to_remove)
return remove_input_port(to_remove)
/obj/item/circuit_component/binary_decimal/decimal_conversion/input_received(datum/port/input/port)
if(!array_size)
return
var/result = 0
for(var/iteration in 1 to array_size)
var/datum/port/input/bit = bit_array[iteration]
if(bit.value)
result += (2 ** (iteration-1))
number.set_output(result)