Improve unit name SSN generation

Names shorter than the required 9 characters are now reused until we have 9 characters - making for fully unique generation.

To keep things distributed as pleasantly as possible they're itterated over with a step of two - alternating between odd and even indexes every full loop.

Similarly, the slice of string used for names longer than 9 characters is now offset based on the length of the name.
This commit is contained in:
SilentSpike 2016-06-21 20:35:23 +01:00
parent ebcf1687a7
commit 6d8121b544

View File

@ -6,18 +6,26 @@
* 0: Name of a unit <STRING> * 0: Name of a unit <STRING>
* *
* Return Value: * Return Value:
* A random social security number <STRING> * A random three/two/four format social security number <STRING>
* *
* Public: No * Public: No
*/ */
#include "script_component.hpp" #include "script_component.hpp"
params ["_name"]; params ["_name"];
private _length = count _name;
private _chars = toArray _name;
private _nums = ((toArray _name) select [0,9]) apply { _x % 10 }; // For short names, reuse characters
if (_length < 9) then {
while {count _nums < 9} do { // Iterates every second character, swapping odd/even with each loop
_nums pushBack (floor random 10); for [{_i = 0},{_i < 2*(9 - _length)},{_i = _i + 2}] do {
_chars pushBack (_chars select floor((_i + (_i/_length % 2)) % _length));
};
}; };
// Offset array slice for long names to make generation more unique
private _slice = [0, _length % 9] select (_length > 9);
private _nums = (_chars select [_slice, 9]) apply { _x % 10 };
([_nums select [0,3],_nums select [3,2], _nums select [5,4]] apply { _x joinString "" }) joinString "-" ([_nums select [0,3],_nums select [3,2], _nums select [5,4]] apply { _x joinString "" }) joinString "-"