Files
OldThink/Content.Shared/Preferences/HumanoidCharacterProfile.cs

446 lines
16 KiB
C#
Raw Normal View History

using System.Linq;
using System.Text.RegularExpressions;
using Content.Shared.CCVar;
2021-06-09 22:19:39 +02:00
using Content.Shared.CharacterAppearance;
using Content.Shared.GameTicking;
2021-06-09 22:19:39 +02:00
using Content.Shared.Random.Helpers;
using Content.Shared.Roles;
using Content.Shared.Species;
2022-09-10 17:40:06 +02:00
using Content.Shared.Traits;
using Robust.Shared.Configuration;
using Robust.Shared.Enums;
using Robust.Shared.Prototypes;
2020-10-11 13:15:09 +02:00
using Robust.Shared.Random;
using Robust.Shared.Serialization;
2022-05-14 08:58:45 +10:00
using Robust.Shared.Utility;
namespace Content.Shared.Preferences
{
/// <summary>
/// Character profile. Looks immutable, but uses non-immutable semantics internally for serialization/code sanity purposes.
/// </summary>
[Serializable, NetSerializable]
public sealed class HumanoidCharacterProfile : ICharacterProfile
{
public const int MinimumAge = 18;
public const int MaximumAge = 120;
public const int MaxNameLength = 32;
2022-05-14 08:58:45 +10:00
public const int MaxDescLength = 512;
private readonly Dictionary<string, JobPriority> _jobPriorities;
private readonly List<string> _antagPreferences;
2022-09-10 17:40:06 +02:00
private readonly List<string> _traitPreferences;
private HumanoidCharacterProfile(
string name,
2022-05-14 08:58:45 +10:00
string flavortext,
string species,
int age,
Sex sex,
Gender gender,
HumanoidCharacterAppearance appearance,
ClothingPreference clothing,
BackpackPreference backpack,
Dictionary<string, JobPriority> jobPriorities,
PreferenceUnavailableMode preferenceUnavailable,
2022-09-10 17:40:06 +02:00
List<string> antagPreferences,
List<string> traitPreferences)
{
Name = name;
2022-05-14 08:58:45 +10:00
FlavorText = flavortext;
Species = species;
Age = age;
Sex = sex;
Gender = gender;
Appearance = appearance;
Clothing = clothing;
Backpack = backpack;
_jobPriorities = jobPriorities;
PreferenceUnavailable = preferenceUnavailable;
_antagPreferences = antagPreferences;
2022-09-10 17:40:06 +02:00
_traitPreferences = traitPreferences;
}
/// <summary>Copy constructor but with overridable references (to prevent useless copies)</summary>
private HumanoidCharacterProfile(
HumanoidCharacterProfile other,
Dictionary<string, JobPriority> jobPriorities,
2022-09-10 17:40:06 +02:00
List<string> antagPreferences,
List<string> traitPreferences)
2022-05-14 08:58:45 +10:00
: this(other.Name, other.FlavorText, other.Species, other.Age, other.Sex, other.Gender, other.Appearance, other.Clothing, other.Backpack,
2022-09-10 17:40:06 +02:00
jobPriorities, other.PreferenceUnavailable, antagPreferences, traitPreferences)
{
}
/// <summary>Copy constructor</summary>
private HumanoidCharacterProfile(HumanoidCharacterProfile other)
2022-09-10 17:40:06 +02:00
: this(other, new Dictionary<string, JobPriority>(other.JobPriorities), new List<string>(other.AntagPreferences), new List<string>(other.TraitPreferences))
{
}
public HumanoidCharacterProfile(
string name,
2022-05-14 08:58:45 +10:00
string flavortext,
string species,
int age,
Sex sex,
Gender gender,
HumanoidCharacterAppearance appearance,
ClothingPreference clothing,
BackpackPreference backpack,
IReadOnlyDictionary<string, JobPriority> jobPriorities,
PreferenceUnavailableMode preferenceUnavailable,
2022-09-10 17:40:06 +02:00
IReadOnlyList<string> antagPreferences,
IReadOnlyList<string> traitPreferences)
2022-05-14 08:58:45 +10:00
: this(name, flavortext, species, age, sex, gender, appearance, clothing, backpack, new Dictionary<string, JobPriority>(jobPriorities),
2022-09-10 17:40:06 +02:00
preferenceUnavailable, new List<string>(antagPreferences), new List<string>(traitPreferences))
{
}
public static HumanoidCharacterProfile Default()
{
return new(
"John Doe",
2022-05-14 08:58:45 +10:00
"",
SpeciesManager.DefaultSpecies,
MinimumAge,
Sex.Male,
Gender.Male,
HumanoidCharacterAppearance.Default(),
ClothingPreference.Jumpsuit,
BackpackPreference.Backpack,
new Dictionary<string, JobPriority>
{
Refactor how jobs are handed out (#5422) * Completely refactor how job spawning works * Remove remains of old system. * Squash the final bug, cleanup. * Attempt to fix tests * Adjusts packed's round-start crew roster, re-enables a bunch of old roles. Also adds the Central Command Official as a proper role. * pretty up ui * refactor StationSystem into the correct folder & namespace. * remove a log, make sure the lobby gets updated if a new map is spontaneously added. * re-add accidentally removed log * We do a little logging * we do a little resolving * we do a little documenting * Renamed OverflowJob to FallbackOverflowJob Allows stations to configure their own roundstart overflow job list. * narrator: it did not compile * oops * support having no overflow jobs * filescope for consistency * small fixes * Bumps a few role counts for Packed, namely engis * log moment * E * Update Resources/Prototypes/Entities/Objects/Misc/identification_cards.yml Co-authored-by: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com> * Update Content.Server/Maps/GameMapPrototype.cs Co-authored-by: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com> * factored job logic, cleanup. * e * Address reviews * Remove the concept of a "default" grid. It has no future in our new multi-station world * why was clickable using that in the first place * fix bad evil bug that almost slipped through also adds chemist * rms obsolete things from chemist * Adds a sanity fallback * address reviews * adds ability to set name * fuck * cleanup joingame
2021-11-26 03:02:46 -06:00
{SharedGameTicker.FallbackOverflowJob, JobPriority.High}
},
PreferenceUnavailableMode.SpawnAsOverflow,
2022-09-10 17:40:06 +02:00
new List<string>(),
new List<string>());
2020-10-11 13:15:09 +02:00
}
public static HumanoidCharacterProfile Random()
{
var prototypeManager = IoCManager.Resolve<IPrototypeManager>();
2020-10-11 13:15:09 +02:00
var random = IoCManager.Resolve<IRobustRandom>();
var species = random.Pick(prototypeManager
.EnumeratePrototypes<SpeciesPrototype>().Where(x => x.RoundStart).ToArray()).ID;
2020-10-11 13:15:09 +02:00
var sex = random.Prob(0.5f) ? Sex.Male : Sex.Female;
var gender = sex == Sex.Male ? Gender.Male : Gender.Female;
2020-10-11 13:15:09 +02:00
var name = sex.GetName(species, prototypeManager, random);
2020-10-11 13:15:09 +02:00
var age = random.Next(MinimumAge, MaximumAge);
2022-05-14 08:58:45 +10:00
return new HumanoidCharacterProfile(name, "", species, age, sex, gender, HumanoidCharacterAppearance.Random(sex), ClothingPreference.Jumpsuit, BackpackPreference.Backpack,
new Dictionary<string, JobPriority>
{
2022-09-10 17:40:06 +02:00
{SharedGameTicker.FallbackOverflowJob, JobPriority.High},
}, PreferenceUnavailableMode.StayInLobby, new List<string>(), new List<string>());
}
public string Name { get; private set; }
2022-05-14 08:58:45 +10:00
public string FlavorText { get; private set; }
public string Species { get; private set; }
public int Age { get; private set; }
public Sex Sex { get; private set; }
public Gender Gender { get; private set; }
public ICharacterAppearance CharacterAppearance => Appearance;
public HumanoidCharacterAppearance Appearance { get; private set; }
public ClothingPreference Clothing { get; private set; }
public BackpackPreference Backpack { get; private set; }
public IReadOnlyDictionary<string, JobPriority> JobPriorities => _jobPriorities;
public IReadOnlyList<string> AntagPreferences => _antagPreferences;
2022-09-10 17:40:06 +02:00
public IReadOnlyList<string> TraitPreferences => _traitPreferences;
public PreferenceUnavailableMode PreferenceUnavailable { get; private set; }
public HumanoidCharacterProfile WithName(string name)
{
return new(this) { Name = name };
}
2022-05-14 08:58:45 +10:00
public HumanoidCharacterProfile WithFlavorText(string flavorText)
{
return new(this) { FlavorText = flavorText };
}
public HumanoidCharacterProfile WithAge(int age)
{
return new(this) { Age = age };
}
public HumanoidCharacterProfile WithSex(Sex sex)
{
return new(this) { Sex = sex };
}
public HumanoidCharacterProfile WithGender(Gender gender)
{
return new(this) { Gender = gender };
}
public HumanoidCharacterProfile WithSpecies(string species)
{
return new(this) { Species = species };
}
public HumanoidCharacterProfile WithCharacterAppearance(HumanoidCharacterAppearance appearance)
{
return new(this) { Appearance = appearance };
}
public HumanoidCharacterProfile WithClothingPreference(ClothingPreference clothing)
{
return new(this) { Clothing = clothing };
}
public HumanoidCharacterProfile WithBackpackPreference(BackpackPreference backpack)
{
return new(this) { Backpack = backpack };
}
public HumanoidCharacterProfile WithJobPriorities(IEnumerable<KeyValuePair<string, JobPriority>> jobPriorities)
{
2022-09-10 17:40:06 +02:00
return new(this, new Dictionary<string, JobPriority>(jobPriorities), _antagPreferences, _traitPreferences);
}
public HumanoidCharacterProfile WithJobPriority(string jobId, JobPriority priority)
{
var dictionary = new Dictionary<string, JobPriority>(_jobPriorities);
if (priority == JobPriority.Never)
{
dictionary.Remove(jobId);
}
else
{
dictionary[jobId] = priority;
}
2022-09-10 17:40:06 +02:00
return new(this, dictionary, _antagPreferences, _traitPreferences);
}
public HumanoidCharacterProfile WithPreferenceUnavailable(PreferenceUnavailableMode mode)
{
return new(this) { PreferenceUnavailable = mode };
}
public HumanoidCharacterProfile WithAntagPreferences(IEnumerable<string> antagPreferences)
{
2022-09-10 17:40:06 +02:00
return new(this, _jobPriorities, new List<string>(antagPreferences), _traitPreferences);
}
public HumanoidCharacterProfile WithAntagPreference(string antagId, bool pref)
{
var list = new List<string>(_antagPreferences);
if(pref)
{
if(!list.Contains(antagId))
{
list.Add(antagId);
}
}
else
{
if(list.Contains(antagId))
{
list.Remove(antagId);
}
}
2022-09-10 17:40:06 +02:00
return new(this, _jobPriorities, list, _traitPreferences);
}
public HumanoidCharacterProfile WithTraitPreference(string traitId, bool pref)
{
var list = new List<string>(_traitPreferences);
// TODO: Maybe just refactor this to HashSet? Same with _antagPreferences
if(pref)
{
if(!list.Contains(traitId))
{
list.Add(traitId);
}
}
else
{
if(list.Contains(traitId))
{
list.Remove(traitId);
}
}
return new(this, _jobPriorities, _antagPreferences, list);
}
public string Summary =>
Loc.GetString(
"humanoid-character-profile-summary",
("name", Name),
("gender", Gender.ToString().ToLowerInvariant()),
("age", Age)
);
public bool MemberwiseEquals(ICharacterProfile maybeOther)
{
if (maybeOther is not HumanoidCharacterProfile other) return false;
if (Name != other.Name) return false;
if (Age != other.Age) return false;
if (Sex != other.Sex) return false;
if (Gender != other.Gender) return false;
if (PreferenceUnavailable != other.PreferenceUnavailable) return false;
if (Clothing != other.Clothing) return false;
if (Backpack != other.Backpack) return false;
if (!_jobPriorities.SequenceEqual(other._jobPriorities)) return false;
if (!_antagPreferences.SequenceEqual(other._antagPreferences)) return false;
2022-09-10 17:40:06 +02:00
if (!_traitPreferences.SequenceEqual(other._traitPreferences)) return false;
return Appearance.MemberwiseEquals(other.Appearance);
}
public void EnsureValid()
{
var age = Math.Clamp(Age, MinimumAge, MaximumAge);
var sex = Sex switch
{
Sex.Male => Sex.Male,
Sex.Female => Sex.Female,
_ => Sex.Male // Invalid enum values.
};
var gender = Gender switch
{
Gender.Epicene => Gender.Epicene,
Gender.Female => Gender.Female,
Gender.Male => Gender.Male,
Gender.Neuter => Gender.Neuter,
_ => Gender.Epicene // Invalid enum values.
};
string name;
if (string.IsNullOrEmpty(Name))
{
name = Sex.GetName(Species);
}
else if (Name.Length > MaxNameLength)
{
name = Name[..MaxNameLength];
}
else
{
name = Name;
}
name = name.Trim();
if (IoCManager.Resolve<IConfigurationManager>().GetCVar(CCVars.RestrictedNames))
{
name = Regex.Replace(name, @"[^A-Z,a-z,0-9, -]", string.Empty);
}
if (string.IsNullOrEmpty(name))
{
name = Sex.GetName(Species);
}
2022-05-14 08:58:45 +10:00
string flavortext;
if (FlavorText.Length > MaxDescLength)
{
flavortext = FormattedMessage.RemoveMarkup(FlavorText)[..MaxDescLength];
}
else
{
flavortext = FormattedMessage.RemoveMarkup(FlavorText);
}
var appearance = HumanoidCharacterAppearance.EnsureValid(Appearance, Species);
var prefsUnavailableMode = PreferenceUnavailable switch
{
PreferenceUnavailableMode.StayInLobby => PreferenceUnavailableMode.StayInLobby,
PreferenceUnavailableMode.SpawnAsOverflow => PreferenceUnavailableMode.SpawnAsOverflow,
_ => PreferenceUnavailableMode.StayInLobby // Invalid enum values.
};
var clothing = Clothing switch
{
ClothingPreference.Jumpsuit => ClothingPreference.Jumpsuit,
ClothingPreference.Jumpskirt => ClothingPreference.Jumpskirt,
_ => ClothingPreference.Jumpsuit // Invalid enum values.
};
var backpack = Backpack switch
{
BackpackPreference.Backpack => BackpackPreference.Backpack,
BackpackPreference.Satchel => BackpackPreference.Satchel,
BackpackPreference.Duffelbag => BackpackPreference.Duffelbag,
_ => BackpackPreference.Backpack // Invalid enum values.
};
var prototypeManager = IoCManager.Resolve<IPrototypeManager>();
var priorities = new Dictionary<string, JobPriority>(JobPriorities
.Where(p => prototypeManager.HasIndex<JobPrototype>(p.Key) && p.Value switch
{
JobPriority.Never => false, // Drop never since that's assumed default.
JobPriority.Low => true,
JobPriority.Medium => true,
JobPriority.High => true,
_ => false
}));
var antags = AntagPreferences
.Where(prototypeManager.HasIndex<AntagPrototype>)
.ToList();
2022-09-10 17:40:06 +02:00
var traits = TraitPreferences
.Where(prototypeManager.HasIndex<TraitPrototype>)
.ToList();
Name = name;
2022-05-14 08:58:45 +10:00
FlavorText = flavortext;
Age = age;
Sex = sex;
Gender = gender;
Appearance = appearance;
Clothing = clothing;
Backpack = backpack;
_jobPriorities.Clear();
foreach (var (job, priority) in priorities)
{
_jobPriorities.Add(job, priority);
}
PreferenceUnavailable = prefsUnavailableMode;
_antagPreferences.Clear();
_antagPreferences.AddRange(antags);
2022-09-10 17:40:06 +02:00
_traitPreferences.Clear();
_traitPreferences.AddRange(traits);
}
public override bool Equals(object? obj)
{
return obj is HumanoidCharacterProfile other && MemberwiseEquals(other);
}
public override int GetHashCode()
{
return HashCode.Combine(
HashCode.Combine(
Name,
Species,
Age,
Sex,
Gender,
Appearance,
Clothing,
Backpack
),
PreferenceUnavailable,
_jobPriorities,
2022-09-10 17:40:06 +02:00
_antagPreferences,
_traitPreferences
);
}
}
}