-
Notifications
You must be signed in to change notification settings - Fork 8
/
CrystalWorld.cs
300 lines (275 loc) · 10.1 KB
/
CrystalWorld.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using CrystiliumMod.Content.Items;
using CrystiliumMod.Content.Items.Weapons;
using CrystiliumMod.Content.Tiles;
using Terraria;
using Terraria.GameContent.Generation;
using Terraria.ID;
using Terraria.IO;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
using Terraria.WorldBuilding;
namespace CrystiliumMod
{
public class CrystalWorld : ModSystem
{
public static int CrystalTiles = 0;
private static List<Point> BiomeCenters;
// TODO, auto SendData using property?
public static bool downedCrystalKing = false;
public override void OnWorldLoad()/* tModPorter Suggestion: Also override OnWorldUnload, and mirror your worldgen-sensitive data initialization in PreWorldGen */
{
downedCrystalKing = false;
}
public override void SaveWorldData(TagCompound tag)/* tModPorter Suggestion: Edit tag parameter instead of returning new TagCompound */
{
List<string> downed = new();
if (downedCrystalKing)
downed.Add("crystalKing");
tag.Add(nameof(downed), downed);
}
public override void LoadWorldData(TagCompound tag)
{
if (tag.ContainsKey("downed"))
{
var downed = tag.GetList<string>("downed");
downedCrystalKing = downed.Contains("crystalKing");
}
}
public override void NetSend(BinaryWriter writer)
{
BitsByte flags = new()
{
[0] = downedCrystalKing
};
writer.Write(flags);
}
public override void NetReceive(BinaryReader reader)
{
BitsByte flags = reader.ReadByte();
downedCrystalKing = flags[0];
}
public override void ModifyWorldGenTasks(List<GenPass> tasks, ref float totalWeight)
{
BiomeCenters = new List<Point>();
int ShiniesIndex = tasks.FindIndex(genpass => genpass.Name.Equals("Shinies"));
if (ShiniesIndex == -1)
{
// Shinies pass removed by some other mod.
return;
}
tasks.Insert(ShiniesIndex + 1, new PassLegacy("CrystalBiomeGen", delegate(GenerationProgress progress, GameConfiguration config)
{
progress.Message = "Polishing crystals";
// 4200 1200
// 8400 2400
// 3 in small
// 6 large
for (int i = 0; i < (int)Main.maxTilesX / 1400; i++)
{
int Xvalue = WorldGen.genRand.Next(50, Main.maxTilesX - 700);
int Yvalue = WorldGen.genRand.Next((int)WorldGen.rockLayer - 200, Main.maxTilesY - 700);
int XvalueHigh = Xvalue + 800;
int YvalueHigh = Yvalue + 800;
int XvalueMid = Xvalue + 400;
int YvalueMid = Yvalue + 400;
WorldGen.TileRunner(XvalueMid, YvalueMid, (double)WorldGen.genRand.Next(300, 300), 1, ModContent.TileType<CrystalBlock>(), false, 0f, 0f, true, true); //c = x, d = y
/*
for (int A = Xvalue; A < XvalueHigh; A++)
{
for (int B = Yvalue; B < YvalueHigh; B++)
{
if (Main.tile[A,B] != null)
{
if (Main.tile[A,B].type == TileType<Tiles.CrystalBlock>()) // A = x, B = y.
{
WorldGen.KillWall(A, B);
WorldGen.PlaceWall(A, B, mod.WallType("CrystalWall"));
}
}
}
}
*/
/*bool placeSuccessful = false;
Tile tileResult;
int x;
int y;
int maxTries = 10000;
int tries = 0;
int successes = 0;
WorldGen.digTunnel(Xvalue + 400, Yvalue + 400, 0, 0, WorldGen.genRand.Next(15, 18), WorldGen.genRand.Next(14, 17), false);
while(tries < maxTries && successes < 5)
{
x = Xvalue + WorldGen.genRand.Next(350, 450);
y = Yvalue + WorldGen.genRand.Next(350, 450);
WorldGen.PlaceChest(x, y, (ushort)TileType<Tiles.CrystalChest>(), false, 2);
tileResult = Main.tile[x, y];
placeSuccessful = tileResult.active() && tileResult.type == TileType<Tiles.CrystalChest>();
if (placeSuccessful) successes++;
tries++;
}*/
int x;
int y;
int maxTries = 20000;
int tries = 0;
int successes = 0;
WorldGen.digTunnel(Xvalue + 400, Yvalue + 400, 0, 0, WorldGen.genRand.Next(15, 18), WorldGen.genRand.Next(14, 17), false);
while (tries < maxTries && successes < 5)
{
x = Xvalue + WorldGen.genRand.Next(350, 450);
y = Yvalue + WorldGen.genRand.Next(350, 450);
if (WorldGen.PlaceChest(x, y, (ushort) ModContent.TileType<CrystalChest>(), false, 2) != -1)
{
successes++;
}
tries++;
}
for (int C = 0; C < 40; C++)
{
int E = Xvalue + WorldGen.genRand.Next(340, 460);
int F = Yvalue + WorldGen.genRand.Next(340, 460);
WorldGen.PlaceTile(E, F, ModContent.TileType<GlowingCrystal2>());
}
for (int C = 0; C < 35; C++)
{
int E = Xvalue + WorldGen.genRand.Next(340, 460);
int F = Yvalue + WorldGen.genRand.Next(340, 460);
if (Main.tile[E, F] != null)
{
WorldGen.PlaceTile(E, F, ModContent.TileType<GlowingCrystal>());
}
}
for (int trees = 0; trees < 50000; trees++)
{
int E = Xvalue + WorldGen.genRand.Next(340, 460);
int F = Yvalue + WorldGen.genRand.Next(340, 460);
Tile tile = Framing.GetTileSafely(E, F);
if (tile.TileType == ModContent.TileType<CrystalBlock>() || tile.TileType == ModContent.TileType<FountainBlock>())
{
WorldGen.GrowTree(E, F);
}
}
for (int Ore = 0; Ore < 650; Ore++)
{
int Xore = XvalueMid + Main.rand.Next(-300, 300);
int Yore = YvalueMid + Main.rand.Next(-300, 300);
if (Main.tile[Xore, Yore].TileType == ModContent.TileType<CrystalBlock>()) // A = x, B = y.
{
WorldGen.TileRunner(Xore, Yore, (double)WorldGen.genRand.Next(3, 6), WorldGen.genRand.Next(3, 6), ModContent.TileType<RadiantOre>(), false, 0f, 0f, false, true);
}
}
BiomeCenters.Add(new Point(XvalueMid, YvalueMid));
}
}));
int LihzahrdAltarsIndex = tasks.FindIndex(genpass => genpass.Name.Equals("Lihzahrd Altars"));
if (LihzahrdAltarsIndex == -1)
{
// Lihzahrd Altars pass removed by some other mod.
return;
}
tasks.Insert(LihzahrdAltarsIndex, new PassLegacy("CrystalBiomeGenFountain", delegate (GenerationProgress progress, GameConfiguration config)
{
progress.Message = "Polishing more crystals";
if (BiomeCenters != null)
{
foreach (var center in BiomeCenters)
{
int XvalueMid = center.X;
int YvalueMid = center.Y;
for (int X1 = -4; X1 < 10; X1++)
{
for (int Y1 = 0; Y1 < 7; Y1++)
{
WorldGen.KillTile(XvalueMid + X1, YvalueMid - Y1);
}
WorldGen.PlaceTile(XvalueMid + X1, YvalueMid, ModContent.TileType<FountainBlock>(), forced: true);
}
for (int X1 = -2; X1 < 8; X1++)
{
WorldGen.PlaceTile(XvalueMid + X1, YvalueMid + 1, ModContent.TileType<CrystalBlock>());
}
for (int X1 = -1; X1 < 7; X1++)
{
WorldGen.PlaceTile(XvalueMid + X1, YvalueMid + 2, ModContent.TileType<CrystalBlock>());
}
WorldGen.PlaceObject(XvalueMid, YvalueMid - 6, ModContent.TileType<Fountain>());
WorldGen.PlaceObject(XvalueMid - 1, YvalueMid - 1, TileID.Lamps, false, 5); // Style 5 Lamp is FrozenLamp
WorldGen.PlaceObject(XvalueMid - 4, YvalueMid - 1, ModContent.TileType<Crystal>());
WorldGen.PlaceObject(XvalueMid + 7, YvalueMid - 1, ModContent.TileType<Crystal>());
WorldGen.PlaceObject(XvalueMid + 6, YvalueMid - 1, TileID.Lamps, false, 5);
}
}
}));
}
// TODO, this is ugly code, overwriting the first choice.
public override void PostWorldGen()
{
// place 3 or 4 items in each crystal chest
int upperLimit = Main.rand.Next(3, 5);
for (int i = 0; i < upperLimit; i++)
{
int[] itemsToPlaceInGlassChestsSecondary = new int[] {
ModContent.ItemType<CrystalBottle>(),
ModContent.ItemType<ShinyGemstone>(),
ModContent.ItemType<RadiantPrism>(),
ModContent.ItemType<GeodeItem>(), ItemID.GoldCoin };
int itemsToPlaceInGlassChestsSecondaryChoice = 0;
for (int chestIndex = 0; chestIndex < 1000; chestIndex++)
{
Chest chest = Main.chest[chestIndex];
if (chest != null && Main.tile[chest.x, chest.y].TileType == ModContent.TileType<CrystalChest>())
{
for (int inventoryIndex = 0; inventoryIndex < 40; inventoryIndex++)
{
if (chest.item[inventoryIndex].type == 0)
{
itemsToPlaceInGlassChestsSecondaryChoice = Main.rand.Next(itemsToPlaceInGlassChestsSecondary.Length);
chest.item[inventoryIndex].SetDefaults(itemsToPlaceInGlassChestsSecondary[itemsToPlaceInGlassChestsSecondaryChoice]); //the error is at this line
chest.item[inventoryIndex].stack = Main.rand.Next(1, 7);
//itemsToPlaceInGlassChestsSecondaryChoice = (itemsToPlaceInGlassChestsSecondaryChoice + 1) % itemsToPlaceInGlassChestsSecondary.Length;
break;
}
}
}
}
}
// is this suppose to be an additional item?
int[] itemsToPlaceInGlassChests = new int[] {
ModContent.ItemType<CrystalStaff>(),
ModContent.ItemType<CrystalEdge>(),
ModContent.ItemType<GemFury>(),
ModContent.ItemType<Geode>(),
ModContent.ItemType<PrismCast>(),
ModContent.ItemType<Glowstrike>(),
ModContent.ItemType<Sharpoon>() };
int itemsToPlaceInGlassChestsChoice = 0;
for (int chestIndex = 0; chestIndex < 1000; chestIndex++)
{
Chest chest = Main.chest[chestIndex];
if (chest != null && Main.tile[chest.x, chest.y].TileType/*.frameX == 47 * 36*/ == ModContent.TileType<CrystalChest>()) // if glass chest
{
for (int inventoryIndex = 0; inventoryIndex < 40; inventoryIndex++)
{
itemsToPlaceInGlassChestsChoice = Main.rand.Next(itemsToPlaceInGlassChests.Length);
chest.item[0].SetDefaults(itemsToPlaceInGlassChests[itemsToPlaceInGlassChestsChoice]);
//itemsToPlaceInGlassChestsChoice = (itemsToPlaceInGlassChestsChoice + 1) % itemsToPlaceInGlassChests.Length;
break;
}
}
}
}
public override void ResetNearbyTileEffects()
{
CrystalPlayer modPlayer = Main.LocalPlayer.GetModPlayer<CrystalPlayer>();
modPlayer.crystalFountain = false;
CrystalTiles = 0;
}
public override void TileCountsAvailable(ReadOnlySpan<int> tileCounts)
{
CrystalTiles = tileCounts[ModContent.TileType<CrystalBlock>()];
}
}
}