Skip to content

Instantly share code, notes, and snippets.

@Jofairden
Last active February 14, 2018 14:46
Show Gist options
  • Save Jofairden/37c053b23ee096e31c8c93fec047218f to your computer and use it in GitHub Desktop.
Save Jofairden/37c053b23ee096e31c8c93fec047218f to your computer and use it in GitHub Desktop.
Poops out terraria armor sets recipes in wiki format
public override void OnEnterWorld(Player player)
{
// some random sheet to log armor recipes
// cuz im lazy, the following is just an example implementation
// poops out the wiki format for armor sets recipes
// does not support 3 hat variants, or assumes all hats use the same recipes
// (deviant hat recipes not allowed), put in 1 hat
// WorkBenches => Work Bench
// Anvils => Iron Anvil / Lead Anvil
// MythrilAnvil => Mythril Anvil / Orihalcum Anvil
var armors = new[]
{
new[]
{
mod.ItemType<InvarHat>(),
mod.ItemType<InvarBreastplate>(),
mod.ItemType<InvarGreaves>(),
},
new[]
{
mod.ItemType<HarpyHelmet>(),
mod.ItemType<HarpyChestplate>(),
mod.ItemType<HarpyLeggings>(),
},
new[]
{
mod.ItemType<ArgiteHelmet>(),
mod.ItemType<ArgiteBreastplate>(),
mod.ItemType<ArgiteGreaves>(),
},
new[]
{
mod.ItemType<NightingaleHood>(),
mod.ItemType<NightingaleChestplate>(),
mod.ItemType<NightingaleGreaves>(),
},
new[]
{
mod.ItemType<OrcishHelmet>(),
mod.ItemType<OrcishBreastplate>(),
mod.ItemType<OrcishGreaves>(),
},
new[]
{
mod.ItemType<FungusHelmet>(),
mod.ItemType<FungusBreastplate>(),
mod.ItemType<FungusGreaves>(),
},
new[]
{
mod.ItemType<FleshHelmet>(),
mod.ItemType<FleshBreastplate>(),
mod.ItemType<FleshGreaves>(),
},
new[]
{
mod.ItemType<CrystalHelmet>(),
mod.ItemType<CrystalChestplate>(),
mod.ItemType<CrystalGreaves>(),
},
new[]
{
mod.ItemType<LuxoriousHelmet>(),
mod.ItemType<LuxoriousBreastplate>(),
mod.ItemType<LuxoriousLeggings>(),
},
new[]
{
mod.ItemType<AfterlifeHood>(),
mod.ItemType<AfterlifeBreastplate>(),
mod.ItemType<AfterlifeLeggings>(),
},
new[]
{
mod.ItemType<NanoHelmet>(),
mod.ItemType<NanoBreastplate>(),
mod.ItemType<NanoGreaves>(),
},
new[]
{
mod.ItemType<MagmoniumHelmet>(),
mod.ItemType<MagmoniumBreastplate>(),
mod.ItemType<MagmoniumGreaves>(),
},
new[]
{
mod.ItemType<SandStoneHelmet>(),
mod.ItemType<SandStoneBreastplate>(),
mod.ItemType<SandStoneGreaves>(),
},
new[]
{
mod.ItemType<ChaosHelmet>(),
mod.ItemType<ChaosBreastplate>(),
mod.ItemType<ChaosGreaves>(),
},
new[]
{
mod.ItemType<BrassHelmet>(),
mod.ItemType<BrassHeadgear>(),
mod.ItemType<BrassMask>(),
},
new[]
{
mod.ItemType<BerserkerHelmet>(),
mod.ItemType<BerserkerChestplate>(),
mod.ItemType<BerserkerGreaves>(),
},
};
ErrorLogger.ClearLog();
foreach (var armor in armors)
{
Item item = new Item(); // item used for initializing
ErrorLogger.Log("==========");
ErrorLogger.Log("Combined recipe for: " + string.Join(", ", armor.Select(x =>
{
item.SetDefaults(x);
return item.Name;
})));
ErrorLogger.Log("==========");
var reqItems = new List<Item>();
var reqTiles = new List<int>();
var reqTileNames = new List<string>();
// loop pieces
foreach (int piece in armor)
{
var recipes = Main.recipe.Where(x => x.createItem.type == piece);
foreach (Recipe recipe in recipes)
{
reqItems = reqItems.Concat(recipe.requiredItem).ToList();
reqTiles = reqTiles.Concat(recipe.requiredTile).ToList();
}
}
// combine stacks
var duplicateItems = reqItems.GroupBy(x => x.type).Where(x => x.Count() > 1).Select(x => x);
var combItems = DistinctBy(reqItems, x => x.type).ToList();
foreach (var grouping in duplicateItems)
{
var reqItem = combItems.FirstOrDefault(x => x.type == grouping.Key);
if (reqItem != null)
{
reqItem.stack = reqItems.Where(x => x.type == grouping.Key).Sum(x => x.stack);
}
}
reqItems = new List<Item>(combItems);
reqTiles = reqTiles.Distinct().ToList();
// get non empty entries
reqItems = reqItems.Where(x => !x.IsAir).ToList();
reqTileNames = reqTiles.Where(x => x > 0).Select(x => FindNameByConstant(typeof(TileID), (ushort) x)).ToList();
int i = reqTileNames.IndexOf("Anvils");
if (i != -1)
{
reqTileNames.RemoveAt(i);
reqTileNames.Add("Iron Anvil");
reqTileNames.Add("Lead Anvil");
}
i = reqTileNames.IndexOf("WorkBenches");
if (i != -1)
{
reqTileNames.RemoveAt(i);
reqTileNames.Add("Work Bench");
}
i = reqTileNames.IndexOf("OrichalcumAnvil");
if (i != -1)
{
reqTileNames.RemoveAt(i);
reqTileNames.Add("Orichalcum Anvil");
}
i = reqTileNames.IndexOf("MythrilAnvil");
if (i != -1)
{
reqTileNames.RemoveAt(i);
reqTileNames.Add("Mythril Anvil");
reqTileNames.Add("Orichalcum Anvil");
}
// make strings, log
string items = "* " + string.Join(" + ", reqItems.Select(x => $"{x.stack} {{{{item link|{x.Name}}}}}"));
string tiles = "* Crafted at " + string.Join(" / ", reqTileNames.Select(x => $"{{{{item link|{x}}}}}"));
ErrorLogger.Log($"{items}\n{tiles}");
ErrorLogger.Log("==========\n");
}
}
private static readonly Dictionary<Type, Dictionary<int, string>> NameFromConstCache = new Dictionary<Type, Dictionary<int, string>>();
private static readonly Type[] IntTypes = new Type[] { typeof(byte), typeof(sbyte), typeof(ushort), typeof(short), typeof(uint), typeof(int), typeof(ulong), typeof(long) };
public static string FindNameByConstant(Type classType, int id)
{
Dictionary<int, string> cache;
if (!NameFromConstCache.ContainsKey(classType))
{
FieldInfo[] fields = classType.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy).Where(f => f.IsLiteral && !f.IsInitOnly && IntTypes.Contains(f.FieldType)).ToArray();
cache = new Dictionary<int, string>();
for (int i = 0; i < fields.Length; i++)
{
int val = Convert.ToInt32(fields[i].GetRawConstantValue());
string name = fields[i].Name;
if (name != "Count" && !cache.ContainsKey(val))
{
AddIfNeedTo(cache, val, name);
}
}
NameFromConstCache[classType] = cache;
}
else
{
cache = NameFromConstCache[classType];
}
string result;
if (cache.TryGetValue(id, out result))
{
return result;
}
return "UNDEFINED";
}
public static IEnumerable<TSource> DistinctBy<TSource, TKey>
(IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
HashSet<TKey> knownKeys = new HashSet<TKey>();
foreach (TSource element in source)
{
if (knownKeys.Add(keySelector(element)))
{
yield return element;
}
}
}
public static void AddIfNeedTo<T1, T2>(Dictionary<T1, T2> dict, T1 key, T2 value)
{
if (!dict.ContainsKey(key))
{
dict.Add(key, value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment