Skip to content

Commit

Permalink
Refactoring and improvements
Browse files Browse the repository at this point in the history
* Improved generation, now on SetupStartInventory, Worldgen and first load. No longer on pickup
* Added and improved documentation
* Refactored CanRoll methods to a single one
* Previous CanApply is renamed to CanRoll (ModifierPool)
* Modifier's Description is renamed to TooltipLines
* Removed the OnPickup context (SetupStartInventory is now in its place)
* Modifier.UniqueRoll now works
* Modifiers are unique: KnockbackImmunity, CursedDamage
* Added Modifier.PostRoll to control if the modifier is actually added, after Modifier.Roll is called. See RandomDebuff for specific usage
  • Loading branch information
Jofairden committed Apr 15, 2018
1 parent e3f4b80 commit 56625d2
Show file tree
Hide file tree
Showing 51 changed files with 288 additions and 649 deletions.
30 changes: 14 additions & 16 deletions EMMItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public override void Load(Item item, TagCompound tag)
{
if (tag.ContainsKey("Type"))
ModifierPool = ModifierPool._Load(item, tag);

HasRolled = tag.GetBool("HasRolled");

ModifierPool?.ApplyModifiers(item);
Expand Down Expand Up @@ -108,21 +109,13 @@ public override void NetSend(Item item, BinaryWriter writer)

public override void OnCraft(Item item, Recipe recipe)
{
ModifierContext ctx = new ModifierContext { Method = ModifierContextMethod.OnCraft, Item = item, Player = Main.LocalPlayer, Recipe = recipe };

ModifierPool pool = GetItemInfo(item).ModifierPool;
if (!HasRolled && pool == null)
ModifierContext ctx = new ModifierContext
{
pool = RollNewPool(ctx);
pool?.ApplyModifiers(item);
}

base.OnCraft(item, recipe);
}

public override bool OnPickup(Item item, Player player)
{
ModifierContext ctx = new ModifierContext { Method = ModifierContextMethod.OnPickup, Item = item, Player = player };
Method = ModifierContextMethod.OnCraft,
Item = item,
Player = Main.LocalPlayer,
Recipe = recipe
};

ModifierPool pool = GetItemInfo(item).ModifierPool;
if (!HasRolled && pool == null)
Expand All @@ -131,12 +124,17 @@ public override bool OnPickup(Item item, Player player)
pool?.ApplyModifiers(item);
}

return base.OnPickup(item, player);
base.OnCraft(item, recipe);
}

public override void PostReforge(Item item)
{
ModifierContext ctx = new ModifierContext { Method = ModifierContextMethod.OnReforge, Item = item, Player = Main.LocalPlayer };
ModifierContext ctx = new ModifierContext
{
Method = ModifierContextMethod.OnReforge,
Item = item,
Player = Main.LocalPlayer
};

ModifierPool pool = RollNewPool(ctx);
pool?.ApplyModifiers(item);
Expand Down
2 changes: 1 addition & 1 deletion EMMLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ internal static uint ReserveGlobalModifierID()
internal static ModifierPool GetWeightedPool(ModifierContext ctx)
{
var wr = new WeightedRandom<ModifierPool>();
foreach (var m in Pools.Where(x => x.Value._CanApply(ctx)))
foreach (var m in Pools.Where(x => x.Value._CanRoll(ctx)))
wr.Add(m.Value, m.Value.RollChance);
var mod = wr.Get();
return (ModifierPool)mod?.Clone();
Expand Down
97 changes: 73 additions & 24 deletions EMMWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,58 +6,107 @@
using Loot.System;
using Terraria;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
using Terraria.World.Generation;

namespace Loot
{
public class EMMWorld : ModWorld
{
// The world has not initialized yet, when it is first updated
public static bool Initialized { get; internal set; }

public override void Initialize()
{
Initialized = false;
}

public override TagCompound Save()
{
return new TagCompound
{
{"initialized", Initialized}
};
}

public override void Load(TagCompound tag)
{
try
{
Initialized = tag.GetBool("initialized");
}
catch (Exception e)
{
ErrorLogger.Log($"Error on EMMWorld:Load: {e}");
}
}

public override void PostUpdate()
{
if (!Initialized)
{
Initialized = true;
foreach (var chest in Main.chest.Where(chest => chest != null && chest.x > 0 && chest.y > 0))
WorldGenModifiersPass.GenerateModifiers(null, ModifierContextMethod.FirstLoad, chest.item.Where(x => !x.IsAir), chest);
}
}

// TODO hardmode task, generate better modifiers in new biomes etc.

public override void ModifyWorldGenTasks(List<GenPass> tasks, ref float totalWeight)
{
tasks.Add(new WorldGenModifiersPass("EvenMoreModifiers:WorldGenModifiersPass", 1));
}

private sealed class WorldGenModifiersPass : GenPass
internal sealed class WorldGenModifiersPass : GenPass
{
public WorldGenModifiersPass(string name, float loadWeight) : base(name, loadWeight)
{
}

public override void Apply(GenerationProgress progress)
// Attempt rolling modifiers on items
internal static void GenerateModifiers(GenerationProgress progress, ModifierContextMethod method, IEnumerable<Item> items, object obj = null)
{
progress.Message = "Generating modifiers on generated items...";
if (progress != null)
progress.Message = "Generating modifiers on generated items...";

foreach (Chest chest in Main.chest)
foreach (var item in items)
{
if (chest == null || chest.x <= 0 || chest.y <= 0)
continue;

foreach (Item item in chest.item)
EMMItem itemInfo = EMMItem.GetItemInfo(item);
ModifierPool pool = itemInfo.ModifierPool;
if (pool == null && WorldGen._genRand.NextBool())
{
if (!item.IsAir)
ModifierContext ctx = new ModifierContext
{
Method = method,
Item = item
};

if (obj is Chest)
{
EMMItem ItemInfo = EMMItem.GetItemInfo(item);
ModifierPool pool = ItemInfo.ModifierPool;
if (!ItemInfo.HasRolled && pool == null && WorldGen._genRand.NextBool())
ctx.CustomData = new Dictionary<string, object>
{
ModifierContext ctx = new ModifierContext
{
Method = ModifierContextMethod.WorldGeneration,
Item = item,
CustomData = new Dictionary<string, object>
{
{"chestData", new Tuple<int, int>(chest.x, chest.y) }
}
};
pool = ItemInfo.RollNewPool(ctx);
pool?.ApplyModifiers(item);
}
{"chestData", new Tuple<int, int>(((Chest)obj).x, ((Chest)obj).y)}
};
}
else if (obj is Player)
{
ctx.Player = (Player)obj;
}

itemInfo.HasRolled = true;
pool = itemInfo.RollNewPool(ctx);
pool?.ApplyModifiers(item);
}
}
}

public override void Apply(GenerationProgress progress)
{
Initialized = true;
foreach (var chest in Main.chest.Where(chest => chest != null && chest.x > 0 && chest.y > 0))
GenerateModifiers(progress, ModifierContextMethod.WorldGeneration, chest.item.Where(x => !x.IsAir), chest);
}
}
}
}
Loading

0 comments on commit 56625d2

Please sign in to comment.