From 26c915c93fafffa559f3616589fa117e395a5b67 Mon Sep 17 00:00:00 2001 From: SkyratBot <59378654+SkyratBot@users.noreply.github.com> Date: Sat, 23 Oct 2021 16:14:32 +0200 Subject: [PATCH] [MIRROR] Binary and Decimal Conversion Components. [MDB IGNORE] (#8995) * 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. * Binary and Decimal Conversion Components. Co-authored-by: Ghom <42542238+Ghommie@users.noreply.github.com> --- code/__DEFINES/flags.dm | 3 + .../research/designs/wiremod_designs.dm | 10 +++ code/modules/research/techweb/all_nodes.dm | 2 + .../components/abstract/binary_decimal.dm | 72 +++++++++++++++++++ .../components/math/binary_conversion.dm | 26 +++++++ .../components/math/decimal_conversion.dm | 29 ++++++++ tgstation.dme | 3 + 7 files changed, 145 insertions(+) create mode 100644 code/modules/wiremod/components/abstract/binary_decimal.dm create mode 100644 code/modules/wiremod/components/math/binary_conversion.dm create mode 100644 code/modules/wiremod/components/math/decimal_conversion.dm diff --git a/code/__DEFINES/flags.dm b/code/__DEFINES/flags.dm index eb49762c85c..e8668200527 100644 --- a/code/__DEFINES/flags.dm +++ b/code/__DEFINES/flags.dm @@ -252,5 +252,8 @@ GLOBAL_LIST_INIT(bitflags, list(1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 204 ///Turns the dir by 180 degrees #define DIRFLIP(d) turn(d, 180) +#define MAX_BITFIELD_SIZE 24 + /// 33554431 (2^24 - 1) is the maximum value our bitflags can reach. #define MAX_BITFLAG_DIGITS 8 + diff --git a/code/modules/research/designs/wiremod_designs.dm b/code/modules/research/designs/wiremod_designs.dm index 346d9aaafe0..08fc5bfba68 100644 --- a/code/modules/research/designs/wiremod_designs.dm +++ b/code/modules/research/designs/wiremod_designs.dm @@ -103,6 +103,16 @@ id = "comp_random" build_path = /obj/item/circuit_component/random +/datum/design/component/binary_conversion + name = "Binary Conversion Component" + id = "comp_binary_convert" + build_path = /obj/item/circuit_component/binary_decimal/binary_conversion + +/datum/design/component/decimal_conversion + name = "Decimal Conversion Component" + id = "comp_decimal_convert" + build_path = /obj/item/circuit_component/binary_decimal/decimal_conversion + /datum/design/component/species name = "Get Species Component" id = "comp_species" diff --git a/code/modules/research/techweb/all_nodes.dm b/code/modules/research/techweb/all_nodes.dm index 44c3fa855e5..518431f9e6c 100644 --- a/code/modules/research/techweb/all_nodes.dm +++ b/code/modules/research/techweb/all_nodes.dm @@ -215,10 +215,12 @@ design_ids = list( "circuit_multitool", "comp_arithmetic", + "comp_binary_convert", "comp_clock", "comp_comparison", "comp_concat", "comp_concat_list", + "comp_decimal_convert", "comp_delay", "comp_direction", "comp_filter_list", diff --git a/code/modules/wiremod/components/abstract/binary_decimal.dm b/code/modules/wiremod/components/abstract/binary_decimal.dm new file mode 100644 index 00000000000..27610d22f36 --- /dev/null +++ b/code/modules/wiremod/components/abstract/binary_decimal.dm @@ -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)) diff --git a/code/modules/wiremod/components/math/binary_conversion.dm b/code/modules/wiremod/components/math/binary_conversion.dm new file mode 100644 index 00000000000..e991499df60 --- /dev/null +++ b/code/modules/wiremod/components/math/binary_conversion.dm @@ -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))) diff --git a/code/modules/wiremod/components/math/decimal_conversion.dm b/code/modules/wiremod/components/math/decimal_conversion.dm new file mode 100644 index 00000000000..6e35d66e105 --- /dev/null +++ b/code/modules/wiremod/components/math/decimal_conversion.dm @@ -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) diff --git a/tgstation.dme b/tgstation.dme index 2bc4ef9e6b4..93dcecf2f1a 100644 --- a/tgstation.dme +++ b/tgstation.dme @@ -3907,6 +3907,7 @@ #include "code\modules\vending\toys.dm" #include "code\modules\vending\wardrobes.dm" #include "code\modules\vending\youtool.dm" +#include "code\modules\wiremod\components\abstract\binary_decimal.dm" #include "code\modules\wiremod\components\abstract\compare.dm" #include "code\modules\wiremod\components\abstract\module.dm" #include "code\modules\wiremod\components\abstract\variable.dm" @@ -3952,7 +3953,9 @@ #include "code\modules\wiremod\components\list\select.dm" #include "code\modules\wiremod\components\list\split.dm" #include "code\modules\wiremod\components\math\arithmetic.dm" +#include "code\modules\wiremod\components\math\binary_conversion.dm" #include "code\modules\wiremod\components\math\comparison.dm" +#include "code\modules\wiremod\components\math\decimal_conversion.dm" #include "code\modules\wiremod\components\math\length.dm" #include "code\modules\wiremod\components\math\logic.dm" #include "code\modules\wiremod\components\math\not.dm"