NamingSystem.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using Content.Shared.Humanoid.Prototypes;
  2. using Content.Shared.Dataset;
  3. using Content.Shared.Random.Helpers;
  4. using Robust.Shared.Random;
  5. using Robust.Shared.Prototypes;
  6. using Robust.Shared.Enums;
  7. namespace Content.Shared.Humanoid
  8. {
  9. /// <summary>
  10. /// Figure out how to name a humanoid with these extensions.
  11. /// </summary>
  12. public sealed class NamingSystem : EntitySystem
  13. {
  14. [Dependency] private readonly IRobustRandom _random = default!;
  15. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  16. public string GetName(string species, Gender? gender = null)
  17. {
  18. // if they have an old species or whatever just fall back to human I guess?
  19. // Some downstream is probably gonna have this eventually but then they can deal with fallbacks.
  20. if (!_prototypeManager.TryIndex(species, out SpeciesPrototype? speciesProto))
  21. {
  22. speciesProto = _prototypeManager.Index<SpeciesPrototype>("Human");
  23. Log.Warning($"Unable to find species {species} for name, falling back to Human");
  24. }
  25. switch (speciesProto.Naming)
  26. {
  27. case SpeciesNaming.First:
  28. return Loc.GetString("namepreset-first",
  29. ("first", GetFirstName(speciesProto, gender)));
  30. case SpeciesNaming.TheFirstofLast:
  31. return Loc.GetString("namepreset-thefirstoflast",
  32. ("first", GetFirstName(speciesProto, gender)), ("last", GetLastName(speciesProto)));
  33. case SpeciesNaming.FirstDashFirst:
  34. return Loc.GetString("namepreset-firstdashfirst",
  35. ("first1", GetFirstName(speciesProto, gender)), ("first2", GetFirstName(speciesProto, gender)));
  36. case SpeciesNaming.FirstLast:
  37. default:
  38. return Loc.GetString("namepreset-firstlast",
  39. ("first", GetFirstName(speciesProto, gender)), ("last", GetLastName(speciesProto)));
  40. }
  41. }
  42. public string GetFirstName(SpeciesPrototype speciesProto, Gender? gender = null)
  43. {
  44. switch (gender)
  45. {
  46. case Gender.Male:
  47. return _random.Pick(_prototypeManager.Index<LocalizedDatasetPrototype>(speciesProto.MaleFirstNames));
  48. case Gender.Female:
  49. return _random.Pick(_prototypeManager.Index<LocalizedDatasetPrototype>(speciesProto.FemaleFirstNames));
  50. default:
  51. if (_random.Prob(0.5f))
  52. return _random.Pick(_prototypeManager.Index<LocalizedDatasetPrototype>(speciesProto.MaleFirstNames));
  53. else
  54. return _random.Pick(_prototypeManager.Index<LocalizedDatasetPrototype>(speciesProto.FemaleFirstNames));
  55. }
  56. }
  57. public string GetLastName(SpeciesPrototype speciesProto)
  58. {
  59. return _random.Pick(_prototypeManager.Index<LocalizedDatasetPrototype>(speciesProto.LastNames));
  60. }
  61. }
  62. }