|
| 1 | +#include <vector> |
| 2 | +#include <string> |
| 3 | +#include <random> |
| 4 | +#include <algorithm> |
| 5 | +#include "star_wars_the_old_republic-chiss_lib.h" |
| 6 | + |
| 7 | +// The name parts for the Chiss generator. These are taken directly from the |
| 8 | +// original implementation; they are kept unchanged except for the bug‑fix |
| 9 | +// below. |
| 10 | +static const std::vector<std::string> nm1 = {"B", "C", "D", "G", "H", "J", "K", "M", "N", "P", "R", "S", "T", "V", "W", "Z"}; |
| 11 | +static const std::vector<std::string> nm2 = {"a", "e", "u", "i", "o", "a", "e", "u", "i", "o", "ra", "re", "ru", "ri", "ro", "la", "le", "lu", "li", "lo"}; |
| 12 | +static const std::vector<std::string> nm3 = {"th", "tth", "tt", "s", "ss", "sh", "st", "sd", "g", "gh", "w", "q", "qh", "r", "rr", "rs", "rt", "rd", "rg", "rk", "rm", "rn", "c", "rc", "sk", "z", "zz", "m", "mm", "n", "ng"}; |
| 13 | +static const std::vector<std::string> nm4 = {"i", "a", "o", "e", "u", "", "", "", "", "", "", "", ""}; |
| 14 | +static const std::vector<std::string> nm5 = {"b","c","d","g","h","m","n","l","p","r","s","t","v","w","z","b","c","d","g","h","m","n","l","p","r","s","t","v","w","z","ab","ac","ad","ag","ah","am","an","al","ap","ar","as","at","av","aw","az","ob","oc","od","og","oh","om","on","ol","op","or","os","ot","ov","ow","oz","ib","ic","id","ig","ih","im","in","il","ip","ir","is","it","iv","iw","iz","eb","ec","ed","eg","eh","em","en","el","ep","er","es","et","ev","ew","ez"}; |
| 15 | +static const std::vector<std::string> nm6 = {"a","e","u","i","o","a","e","u","i","o","ae","ea","au","ua","ao","oa","ou","uo","ia","ai"}; |
| 16 | +static const std::vector<std::string> nm7 = {"n","t","th","w","l","m","sh","s","k","w","z","r"}; |
| 17 | +static const std::vector<std::string> nm8 = {"i","a","o","e","u","","","","","","","","",""}; |
| 18 | +static const std::vector<std::string> nm9 = {"b","c","d","f","g","h","k","l","m","n","p","r","s","t","v","z","","","","","","","","","",""}; |
| 19 | +static const std::vector<std::string> nm10 = {"a","e","u","i","o","a","e","u","i","o","ra","re","ru","ri","ro","la","le","lu","li","lo","ae","ea","au","ua","ao","oa","ou","uo","ia","ai"}; |
| 20 | +static const std::vector<std::string> nm11 = {"th","tth","tt","s","s","s","g","g","r","r","c","c","m","m","n","n","z","z","ss","sh","st","sd","g","gh","w","q","qh","r","rr","rs","rt","rd","rg","rk","rm","rn","c","rc","sk","z","zz","m","mm","n","ng"}; |
| 21 | +static const std::vector<std::string> nm12 = {"i","a","o","e","u"}; |
| 22 | +static const std::vector<std::string> nm13 = {"th","tth","tt","s","s","s","g","g","r","r","c","c","m","m","n","n","z","z","ss","sh","st","sd","g","gh","w","q","qh","r","rr","rs","rt","rd","rg","rk","rm","rn","c","rc","sk","z","zz","m","mm","n","ng","","","","","","','','','','','','','','','','','','','','','','','','','','','','','','','','"}; |
| 23 | +static const std::vector<std::string> nm14 = {"i","a","o","e","u","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""}; |
| 24 | + |
| 25 | +/** |
| 26 | + * @brief Generate a single Chiss name. |
| 27 | + * |
| 28 | + * The original implementation mistakenly used a single random index (`rnd`) |
| 29 | + * for *all* of the name parts, which caused out‑of‑bounds accesses when the |
| 30 | + * random value was larger than the size of some of the vectors. The fix |
| 31 | + * is to use a distinct random index for each vector (rnd1 … rnd14). |
| 32 | + * |
| 33 | + * @param rng A reference to a seeded std::mt19937 random number generator. |
| 34 | + * @return A generated Chiss name. |
| 35 | + */ |
| 36 | +std::string generate_star_wars_the_old_republic_chiss_name(std::mt19937& rng) { |
| 37 | + // Generate a random index for each name‑part vector. |
| 38 | + const int rnd1 = std::uniform_int_distribution<int>(0, static_cast<int>(nm1.size()) - 1)(rng); |
| 39 | + const int rnd2 = std::uniform_int_distribution<int>(0, static_cast<int>(nm2.size()) - 1)(rng); |
| 40 | + const int rnd3 = std::uniform_int_distribution<int>(0, static_cast<int>(nm3.size()) - 1)(rng); |
| 41 | + const int rnd4 = std::uniform_int_distribution<int>(0, static_cast<int>(nm4.size()) - 1)(rng); |
| 42 | + const int rnd5 = std::uniform_int_distribution<int>(0, static_cast<int>(nm5.size()) - 1)(rng); |
| 43 | + const int rnd6 = std::uniform_int_distribution<int>(0, static_cast<int>(nm6.size()) - 1)(rng); |
| 44 | + const int rnd7 = std::uniform_int_distribution<int>(0, static_cast<int>(nm7.size()) - 1)(rng); |
| 45 | + const int rnd8 = std::uniform_int_distribution<int>(0, static_cast<int>(nm8.size()) - 1)(rng); |
| 46 | + const int rnd9 = std::uniform_int_distribution<int>(0, static_cast<int>(nm9.size()) - 1)(rng); |
| 47 | + const int rnd10 = std::uniform_int_distribution<int>(0, static_cast<int>(nm10.size()) - 1)(rng); |
| 48 | + const int rnd11 = std::uniform_int_distribution<int>(0, static_cast<int>(nm11.size()) - 1)(rng); |
| 49 | + const int rnd12 = std::uniform_int_distribution<int>(0, static_cast<int>(nm12.size()) - 1)(rng); |
| 50 | + const int rnd13 = std::uniform_int_distribution<int>(0, static_cast<int>(nm13.size()) - 1)(rng); |
| 51 | + const int rnd14 = std::uniform_int_distribution<int>(0, static_cast<int>(nm14.size()) - 1)(rng); |
| 52 | + |
| 53 | + // Assemble the name. The original format inserts an apostrophe after the |
| 54 | + // fourth and eighth parts. |
| 55 | + return nm1[rnd1] + nm2[rnd2] + nm3[rnd3] + nm4[rnd4] + "'" + |
| 56 | + nm5[rnd5] + nm6[rnd6] + nm7[rnd7] + nm8[rnd8] + "'" + |
| 57 | + nm9[rnd9] + nm10[rnd10] + nm11[rnd11] + nm12[rnd12] + nm13[rnd13] + nm14[rnd14]; |
| 58 | +} |
0 commit comments