Files
crystall-punk-14/Content.Shared/Salvage/SharedSalvageSystem.cs

243 lines
7.9 KiB
C#
Raw Normal View History

2023-04-20 10:43:13 +10:00
using System.Linq;
2023-03-10 16:41:22 +11:00
using Content.Shared.Dataset;
using Content.Shared.Random;
using Content.Shared.Random.Helpers;
2023-04-20 10:43:13 +10:00
using Content.Shared.Salvage.Expeditions;
using Content.Shared.Salvage.Expeditions.Modifiers;
2023-03-10 16:41:22 +11:00
using Robust.Shared.Prototypes;
2023-04-20 10:43:13 +10:00
using Robust.Shared.Random;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
2023-03-10 16:41:22 +11:00
namespace Content.Shared.Salvage;
public abstract class SharedSalvageSystem : EntitySystem
{
2023-04-20 10:43:13 +10:00
[Dependency] private readonly ILocalizationManager _loc = default!;
[Dependency] private readonly IPrototypeManager _proto = default!;
#region Descriptions
2023-03-10 16:41:22 +11:00
2023-04-20 10:43:13 +10:00
public string GetMissionDescription(SalvageMission mission)
2023-03-10 16:41:22 +11:00
{
2023-04-20 10:43:13 +10:00
// Hardcoded in coooooz it's dynamic based on difficulty and I'm lazy.
switch (mission.Mission)
{
case SalvageMissionType.Mining:
// Taxation: , ("tax", $"{GetMiningTax(mission.Difficulty) * 100f:0}")
return Loc.GetString("salvage-expedition-desc-mining");
case SalvageMissionType.Destruction:
var proto = _proto.Index<SalvageFactionPrototype>(mission.Faction).Configs["DefenseStructure"];
return Loc.GetString("salvage-expedition-desc-structure",
("count", GetStructureCount(mission.Difficulty)),
("structure", _loc.GetEntityData(proto).Name));
case SalvageMissionType.Elimination:
return Loc.GetString("salvage-expedition-desc-elimination");
2023-04-20 10:43:13 +10:00
default:
throw new NotImplementedException();
}
}
public float GetMiningTax(DifficultyRating baseRating)
{
return 0.6f + (int) baseRating * 0.05f;
}
/// <summary>
/// Gets the amount of structures to destroy.
/// </summary>
public int GetStructureCount(DifficultyRating baseRating)
{
return 1 + (int) baseRating * 2;
}
#endregion
public int GetDifficulty(DifficultyRating rating)
{
switch (rating)
2023-03-10 16:41:22 +11:00
{
2023-04-21 15:05:50 +10:00
case DifficultyRating.Minimal:
2023-04-20 10:43:13 +10:00
return 1;
2023-03-10 16:41:22 +11:00
case DifficultyRating.Minor:
2023-04-20 10:43:13 +10:00
return 2;
2023-03-10 16:41:22 +11:00
case DifficultyRating.Moderate:
2023-04-20 10:43:13 +10:00
return 4;
2023-03-10 16:41:22 +11:00
case DifficultyRating.Hazardous:
2023-04-20 10:43:13 +10:00
return 8;
case DifficultyRating.Extreme:
return 16;
2023-03-10 16:41:22 +11:00
default:
2023-04-20 10:43:13 +10:00
throw new ArgumentOutOfRangeException(nameof(rating), rating, null);
2023-03-10 16:41:22 +11:00
}
}
2023-04-20 10:43:13 +10:00
/// <summary>
/// How many groups of mobs to spawn for a mission.
/// </summary>
public float GetSpawnCount(DifficultyRating difficulty)
{
return (int) difficulty * 2;
}
2023-03-10 16:41:22 +11:00
public static string GetFTLName(DatasetPrototype dataset, int seed)
{
var random = new System.Random(seed);
return $"{dataset.Values[random.Next(dataset.Values.Count)]}-{random.Next(10, 100)}-{(char) (65 + random.Next(26))}";
}
2023-04-20 10:43:13 +10:00
public SalvageMission GetMission(SalvageMissionType config, DifficultyRating difficulty, int seed)
2023-03-10 16:41:22 +11:00
{
2023-04-20 10:43:13 +10:00
// This is on shared to ensure the client display for missions and what the server generates are consistent
var rating = (float) GetDifficulty(difficulty);
// Don't want easy missions to have any negative modifiers but also want
// easy to be a 1 for difficulty.
rating -= 1f;
var rand = new System.Random(seed);
var faction = GetMod<SalvageFactionPrototype>(rand, ref rating);
var biome = GetMod<SalvageBiomeMod>(rand, ref rating);
var dungeon = GetBiomeMod<SalvageDungeonMod>(biome.ID, rand, ref rating);
2023-04-20 10:43:13 +10:00
var mods = new List<string>();
var air = GetBiomeMod<SalvageAirMod>(biome.ID, rand, ref rating);
if (air.Description != string.Empty)
{
mods.Add(air.Description);
}
2023-04-20 10:43:13 +10:00
// only show the description if there is an atmosphere since wont matter otherwise
var temp = GetBiomeMod<SalvageTemperatureMod>(biome.ID, rand, ref rating);
if (temp.Description != string.Empty && !air.Space)
{
mods.Add(temp.Description);
}
var light = GetBiomeMod<SalvageLightMod>(biome.ID, rand, ref rating);
if (light.Description != string.Empty)
2023-04-20 10:43:13 +10:00
{
mods.Add(light.Description);
}
var time = GetMod<SalvageTimeMod>(rand, ref rating);
// Round the duration to nearest 15 seconds.
var exactDuration = MathHelper.Lerp(time.MinDuration, time.MaxDuration, rand.NextFloat());
2023-04-20 10:43:13 +10:00
exactDuration = MathF.Round(exactDuration / 15f) * 15f;
var duration = TimeSpan.FromSeconds(exactDuration);
if (time.Description != string.Empty)
2023-04-20 10:43:13 +10:00
{
mods.Add(time.Description);
}
var rewards = GetRewards(difficulty, rand);
return new SalvageMission(seed, difficulty, dungeon.ID, faction.ID, config, biome.ID, air.ID, temp.Temperature, light.Color, duration, rewards, mods);
2023-03-10 16:41:22 +11:00
}
public T GetBiomeMod<T>(string biome, System.Random rand, ref float rating) where T : class, IPrototype, IBiomeSpecificMod
2023-03-10 16:41:22 +11:00
{
var mods = _proto.EnumeratePrototypes<T>().ToList();
2023-04-20 10:43:13 +10:00
mods.Sort((x, y) => string.Compare(x.ID, y.ID, StringComparison.Ordinal));
rand.Shuffle(mods);
foreach (var mod in mods)
{
if (mod.Cost > rating || (mod.Biomes != null && !mod.Biomes.Contains(biome)))
2023-04-20 10:43:13 +10:00
continue;
rating -= mod.Cost;
return mod;
}
throw new InvalidOperationException();
2023-03-10 16:41:22 +11:00
}
2023-04-20 10:43:13 +10:00
public T GetMod<T>(System.Random rand, ref float rating) where T : class, IPrototype, ISalvageMod
{
var mods = _proto.EnumeratePrototypes<T>().ToList();
mods.Sort((x, y) => string.Compare(x.ID, y.ID, StringComparison.Ordinal));
rand.Shuffle(mods);
foreach (var mod in mods)
{
if (mod.Cost > rating)
continue;
rating -= mod.Cost;
return mod;
}
throw new InvalidOperationException();
}
2023-03-10 16:41:22 +11:00
private List<string> GetRewards(DifficultyRating difficulty, System.Random rand)
2023-03-10 16:41:22 +11:00
{
var rewards = new List<string>(3);
var ids = RewardsForDifficulty(difficulty);
foreach (var id in ids)
2023-04-20 10:43:13 +10:00
{
// pick a random reward to give
var weights = _proto.Index<WeightedRandomEntityPrototype>(id);
rewards.Add(weights.Pick(rand));
}
2023-04-20 10:43:13 +10:00
return rewards;
}
2023-04-20 10:43:13 +10:00
/// <summary>
/// Get a list of WeightedRandomEntityPrototype IDs with the rewards for a certain difficulty.
/// </summary>
private string[] RewardsForDifficulty(DifficultyRating rating)
{
var common = "SalvageRewardCommon";
var rare = "SalvageRewardRare";
var epic = "SalvageRewardEpic";
switch (rating)
{
case DifficultyRating.Minimal:
return new string[] { common, common, common };
case DifficultyRating.Minor:
return new string[] { common, common, rare };
case DifficultyRating.Moderate:
return new string[] { common, rare, rare };
case DifficultyRating.Hazardous:
return new string[] { rare, rare, rare, epic };
case DifficultyRating.Extreme:
return new string[] { rare, rare, epic, epic, epic };
default:
throw new NotImplementedException();
2023-04-20 10:43:13 +10:00
}
2023-03-10 16:41:22 +11:00
}
2023-04-20 10:43:13 +10:00
}
2023-03-10 16:41:22 +11:00
2023-04-20 10:43:13 +10:00
[Serializable, NetSerializable]
public enum SalvageMissionType : byte
{
/// <summary>
/// No dungeon, just ore loot and random mob spawns.
/// </summary>
Mining,
/// <summary>
/// Destroy the specified structures in a dungeon.
/// </summary>
Destruction,
/// <summary>
/// Kill a large creature in a dungeon.
/// </summary>
Elimination,
2023-04-20 10:43:13 +10:00
}
[Serializable, NetSerializable]
public enum DifficultyRating : byte
{
2023-04-21 15:05:50 +10:00
Minimal,
2023-04-20 10:43:13 +10:00
Minor,
Moderate,
Hazardous,
Extreme,
2023-03-10 16:41:22 +11:00
}