Last active
February 14, 2018 14:54
-
-
Save Jofairden/5c5b2e58975b8670d16e8bf56c29a215 to your computer and use it in GitHub Desktop.
Terraria ItemValue struct that simplifies item values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct ItemValue | |
{ | |
public int RawValue { get; private set; } | |
public int Copper { get; private set; } | |
public int Silver { get; private set; } | |
public int Gold { get; private set; } | |
public int Platinum { get; private set; } | |
public ItemValue(int c = 0, int s = 0, int g = 0, int p = 0) | |
{ | |
// Need to assign first | |
RawValue = c + (s * 100) + (g * 100 * 100) + (p * 100 * 100 * 100); | |
Copper = c; | |
Silver = s; | |
Gold = g; | |
Platinum = p; | |
SetValues(c, s, g, p); | |
} | |
public override string ToString() | |
{ | |
StringBuilder stringBuilder = new StringBuilder(); | |
if (Platinum > 0) | |
stringBuilder.Append($"{Platinum}p"); | |
if (Gold > 0) | |
stringBuilder.Append($"{Gold}g"); | |
if (Silver > 0) | |
stringBuilder.Append($"{Silver}s"); | |
if (Copper > 0) | |
stringBuilder.Append($"{Copper}c"); | |
if (stringBuilder.Length <= 0) | |
return "No value"; | |
return string.Concat(stringBuilder.ToString().Select(c => $"{c}" + (char.IsLetter(c) ? " " : ""))).TrimEnd(' '); | |
} | |
public string ToTagString() | |
{ | |
StringBuilder stringBuilder = new StringBuilder(); | |
if (Platinum > 0) | |
stringBuilder.Append($"[i/s1:{ItemID.PlatinumCoin}]{Platinum}"); | |
if (Gold > 0) | |
stringBuilder.Append($"[i/s1:{ItemID.GoldCoin}]{Gold}"); | |
if (Silver > 0) | |
stringBuilder.Append($"[i/s1:{ItemID.SilverCoin}]{Silver}"); | |
if (Copper > 0) | |
stringBuilder.Append($"[i/s1:{ItemID.CopperCoin}]{Copper}"); | |
if (stringBuilder.Length <= 0) | |
return "No value"; | |
return $"{stringBuilder}"; | |
} | |
public ItemValue SetValues(float copper, float silver = 0, float gold = 0, float platinum = 0) | |
{ | |
SetFromCopperValue(GetRawCopperValue(copper, silver, gold, platinum)); | |
return this; | |
} | |
public ItemValue AddValues(float copper, float silver = 0, float gold = 0, float platinum = 0) | |
{ | |
SetFromCopperValue(RawValue + GetRawCopperValue(copper, silver, gold, platinum)); | |
return this; | |
} | |
private int GetRawCopperValue(float copper, float silver = 0, float gold = 0, float platinum = 0) | |
{ | |
return (int)(copper + silver * 100 + gold * Math.Pow(100, 2) + platinum * Math.Pow(100, 3)); | |
} | |
public ItemValue ToSellValue() | |
{ | |
RawValue /= 5; | |
SetFromCopperValue(RawValue); | |
return this; | |
} | |
public ItemValue ApplyDiscount(Player player) | |
{ | |
if (player.discount) | |
{ | |
RawValue = (int)(RawValue * 0.8f); | |
SetFromCopperValue(RawValue); | |
} | |
return this; | |
} | |
public ItemValue SetFromCopperValue(float value) | |
{ | |
RawValue = (int)value; | |
int copper = (int)value; | |
int silver = 0; | |
int gold = 0; | |
int platinum = 0; | |
if (copper >= 100) | |
{ | |
silver = copper / 100; | |
copper %= 100; | |
if (silver >= 100) | |
{ | |
gold = silver / 100; | |
silver %= 100; | |
if (gold >= 100) | |
{ | |
platinum = gold / 100; | |
gold %= 100; | |
} | |
} | |
} | |
this.Copper = copper; | |
this.Silver = silver; | |
this.Gold = gold; | |
this.Platinum = platinum; | |
return this; | |
} | |
public static implicit operator ItemValue(float rawValue) | |
{ | |
return new ItemValue().SetFromCopperValue((int)rawValue); | |
} | |
public static ItemValue operator +(ItemValue first, ItemValue second) | |
{ | |
return new ItemValue().SetFromCopperValue(first.RawValue + second.RawValue); | |
} | |
public static ItemValue operator +(ItemValue first, float second) | |
{ | |
return new ItemValue().SetFromCopperValue((int)(first.RawValue + second)); | |
} | |
public static ItemValue operator +(ItemValue first, double second) | |
{ | |
return new ItemValue().SetFromCopperValue((int)(first.RawValue + second)); | |
} | |
public static ItemValue operator +(ItemValue first, decimal second) | |
{ | |
return new ItemValue().SetFromCopperValue((int)(first.RawValue + second)); | |
} | |
public static ItemValue operator -(ItemValue first, ItemValue second) | |
{ | |
return new ItemValue().SetFromCopperValue(first.RawValue - second.RawValue); | |
} | |
public static ItemValue operator -(ItemValue first, float second) | |
{ | |
return new ItemValue().SetFromCopperValue((int)(first.RawValue - second)); | |
} | |
public static ItemValue operator -(ItemValue first, double second) | |
{ | |
return new ItemValue().SetFromCopperValue((int)(first.RawValue - second)); | |
} | |
public static ItemValue operator -(ItemValue first, decimal second) | |
{ | |
return new ItemValue().SetFromCopperValue((int)(first.RawValue - second)); | |
} | |
public static ItemValue operator *(ItemValue first, ItemValue second) | |
{ | |
return new ItemValue().SetFromCopperValue(first.RawValue * second.RawValue); | |
} | |
public static ItemValue operator *(ItemValue first, float second) | |
{ | |
return new ItemValue().SetFromCopperValue((int)(first.RawValue * second)); | |
} | |
public static ItemValue operator *(ItemValue first, double second) | |
{ | |
return new ItemValue().SetFromCopperValue((int)(first.RawValue * second)); | |
} | |
public static ItemValue operator *(ItemValue first, decimal second) | |
{ | |
return new ItemValue().SetFromCopperValue((int)(first.RawValue * second)); | |
} | |
public static ItemValue operator /(ItemValue first, ItemValue second) | |
{ | |
return new ItemValue().SetFromCopperValue(first.RawValue / second.RawValue); | |
} | |
public static ItemValue operator /(ItemValue first, float second) | |
{ | |
return new ItemValue().SetFromCopperValue((int)(first.RawValue / second)); | |
} | |
public static ItemValue operator /(ItemValue first, double second) | |
{ | |
return new ItemValue().SetFromCopperValue((int)(first.RawValue / second)); | |
} | |
public static ItemValue operator /(ItemValue first, decimal second) | |
{ | |
return new ItemValue().SetFromCopperValue((int)(first.RawValue / second)); | |
} | |
public static ItemValue operator %(ItemValue first, ItemValue second) | |
{ | |
return new ItemValue().SetFromCopperValue(first.RawValue % second.RawValue); | |
} | |
public static ItemValue operator %(ItemValue first, float second) | |
{ | |
return new ItemValue().SetFromCopperValue((int)(first.RawValue % second)); | |
} | |
public static ItemValue operator %(ItemValue first, double second) | |
{ | |
return new ItemValue().SetFromCopperValue((int)(first.RawValue % second)); | |
} | |
public static ItemValue operator %(ItemValue first, decimal second) | |
{ | |
return new ItemValue().SetFromCopperValue((int)(first.RawValue % second)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment