Client LuaCsForBarotrauma
BarotraumaShared/SharedSource/Items/Components/Quality.cs
1 using Microsoft.Xna.Framework;
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Xml.Linq;
6 
8 {
9  partial class Quality : ItemComponent
10  {
11  public const int MaxQuality = 3;
12 
13  public enum StatType
14  {
15  Condition,
16  ExplosionRadius,
17  ExplosionDamage,
22  FirepowerMultiplier,
23  StrikingPowerMultiplier,
24  StrikingSpeedMultiplier,
25  FiringRateMultiplier
26  }
27 
28  private readonly Dictionary<StatType, float> statValues = new Dictionary<StatType, float>();
29 
30  private int qualityLevel;
31 
32  [Editable(MinValueInt = 0, MaxValueInt = MaxQuality), Serialize(0, IsPropertySaveable.Yes)]
33  public int QualityLevel
34  {
35  get { return qualityLevel; }
36  set
37  {
38  if (value == qualityLevel) { return; }
39 
40  bool wasInFullCondition = item.IsFullCondition;
41  qualityLevel = MathHelper.Clamp(value, 0, MaxQuality);
43  //set the condition to the new max condition
44  if (wasInFullCondition && statValues.ContainsKey(StatType.Condition))
45  {
47  }
48  }
49  }
50 
51  public Quality(Item item, ContentXElement element) : base(item, element)
52  {
53  foreach (XElement subElement in element.Elements())
54  {
55  switch (subElement.Name.ToString().ToLower())
56  {
57  case "stattype":
58  case "statvalue":
59  case "qualitystat":
60  string statTypeString = subElement.GetAttributeString("stattype", "");
61  if (!Enum.TryParse(statTypeString, true, out StatType statType))
62  {
63  DebugConsole.ThrowError("Invalid stat type type \"" + statTypeString + "\" in item (" + ((MapEntity)item).Prefab.Identifier + ")",
64  contentPackage: element.ContentPackage);
65  }
66  float statValue = subElement.GetAttributeFloat("value", 0f);
67  statValues.TryAdd(statType, statValue);
68  break;
69  }
70  }
71  }
72 
73  public float GetValue(StatType statType)
74  {
75  if (!statValues.ContainsKey(statType)) { return 0.0f; }
76  return statValues[statType] * qualityLevel;
77  }
78 
83  public static int GetSpawnedItemQuality(Submarine submarine, Level level, Rand.RandSync randSync = Rand.RandSync.ServerAndClient)
84  {
85  if (submarine?.Info == null || level == null || submarine.Info.Type == SubmarineType.Player) { return 0; }
86 
87  float difficultyFactor = MathHelper.Clamp(level.Difficulty, 0.0f, level.LevelData.Biome.ActualMaxDifficulty / 100.0f);
88 
89  if (level.Type == LevelData.LevelType.Outpost &&
90  level.StartLocation?.Type?.OutpostTeam == CharacterTeamType.FriendlyNPC)
91  {
92  //no high-quality spawns in friendly outposts
93  difficultyFactor = 0.0f;
94  }
95 
96  return ToolBox.SelectWeightedRandom(Enumerable.Range(0, MaxQuality + 1), q => GetCommonness(q, difficultyFactor), randSync);
97 
98  static float GetCommonness(int quality, float difficultyFactor)
99  {
100  return quality switch
101  {
102  0 => 1,
103  1 => MathHelper.Lerp(0.0f, 1f, difficultyFactor),
104  2 => MathHelper.Lerp(0.0f, 1f, Math.Max(difficultyFactor-0.15f, 0f)), //15 difficulty transition to next biome - unlock Excellent loot
105  3 => MathHelper.Lerp(0.0f, 1f, Math.Max(difficultyFactor-0.35f, 0f)), //35 difficulty transition to next biome - unlock Masterwork loot
106  _ => 0.0f,
107  };
108  }
109  }
110  }
111 }
float ActualMaxDifficulty
Definition: Biome.cs:21
void RecalculateConditionValues()
Recalculates the item's maximum condition, condition percentage and whether it's in full condition....
The base class for components holding the different functionalities of the item
static int GetSpawnedItemQuality(Submarine submarine, Level level, Rand.RandSync randSync=Rand.RandSync.ServerAndClient)
Get a random quality for an item spawning in some sub, taking into account the type of the submarine ...
readonly Biome Biome
Definition: LevelData.cs:25
LocationType Type
Definition: Location.cs:91
readonly CharacterTeamType OutpostTeam
Definition: LocationType.cs:34
readonly Identifier Identifier
Definition: Prefab.cs:34
@ RepairToolStructureRepairMultiplier
Increases the repair speed of repair tools that fix submarine walls by a percentage.
@ RepairToolDeattachTimeMultiplier
Increase the detach speed of items like minerals that require a tool to detach from the wall by a per...
@ RepairSpeed
Increases the repair speed of the character by a percentage.
@ RepairToolStructureDamageMultiplier
Increases the wall damage of tools that destroy submarine walls like plasma cutter by a percentage.