forked from Jofairden/EvenMoreModifiers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LootModWorld.cs
134 lines (116 loc) · 3.22 KB
/
LootModWorld.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;
using System.Collections.Generic;
using System.Linq;
using Loot.Api.Core;
using Loot.Api.Ext;
using Terraria;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
using Terraria.Utilities;
using Terraria.World.Generation;
namespace Loot
{
/// <summary>
/// This class is responsible for generating modifiers when a world is being created.
/// </summary>
public class LootModWorld : 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)
{
Loot.Logger.Error("Error on EMMWorld.Load", e);
}
}
public override void PostUpdate()
{
if (Initialized)
{
return;
}
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));
}
internal sealed class WorldGenModifiersPass : GenPass
{
public WorldGenModifiersPass(string name, float loadWeight) : base(name, loadWeight)
{
}
// Attempt rolling modifiers on items
internal static void GenerateModifiers(GenerationProgress progress, ModifierContextMethod method, IEnumerable<Item> items, object obj = null)
{
if (progress != null)
{
progress.Message = "Generating modifiers on generated items...";
}
foreach (var item in items)
{
LootModItem itemInfo = LootModItem.GetInfo(item);
ModifierPool pool = itemInfo.ModifierPool;
UnifiedRandom rand = (Main.rand ?? WorldGen.genRand) ?? new UnifiedRandom();
if (itemInfo.HasRolled || pool != null)
continue;
itemInfo.HasRolled = true;
if (rand != null && rand.NextBool())
continue;
ModifierContext ctx = new ModifierContext
{
Method = method,
Item = item,
Strategy = RollingUtils.Strategies.Normal
};
switch (obj)
{
case Chest chest:
ctx.CustomData = new Dictionary<string, object>
{
{"chestData", new Tuple<int, int>(chest.x, chest.y)}
};
break;
case Player player:
ctx.Player = player;
break;
}
pool = itemInfo.RollNewPool(ctx, RollingUtils.Properties.WorldGen);
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 != null && !x.IsAir && x.IsModifierRollableItem()),
chest);
}
}
}
}
}