forked from gardenappl/Miscellania
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MiscWorldGen.cs
139 lines (129 loc) · 3.6 KB
/
MiscWorldGen.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.GameContent.Generation;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.WorldBuilding;
using GoldensMisc.Tiles;
using Terraria.Localization;
using Terraria.IO;
namespace GoldensMisc
{
public class MiscWorldGen : ModSystem
{
public override void ModifyWorldGenTasks(List<GenPass> tasks, ref float totalWeight)
{
if(ModContent.GetInstance<ServerConfig>().AncientForges)
{
int index = tasks.FindIndex(genpass => genpass.Name.Equals("Hellforge"));
if(index != -1)
{
tasks.Insert(index + 1, new PassLegacy("[Miscellania] Ancient Hellforge", AddHellforges));
}
index = tasks.FindIndex(genpass => genpass.Name.Equals("Buried Chests"));
if(index != -1)
{
tasks.Insert(index + 1, new PassLegacy("[Miscellania] Ancient Forge", AddForges));
}
}
}
public static void AddHellforges(GenerationProgress progress = null, GameConfiguration configuration = null)
{
AddFurniture(progress, Language.GetTextValue("Mods.GoldensMisc.WorldGen.AncientHellforge"), ModContent.TileType<AncientHellforge>(), 1500, Main.maxTilesY - 250, Main.maxTilesY - 5, WallID.ObsidianBrickUnsafe, WallID.HellstoneBrickUnsafe);
}
public static void AddForges(GenerationProgress progress = null, GameConfiguration configuration = null)
{
AddFurniture(progress, Language.GetTextValue("Mods.GoldensMisc.WorldGen.AncientForge"), ModContent.TileType<AncientForge>(), 300, (int)Main.worldSurface, Main.maxTilesY - 300, WallID.Planked, WallID.BorealWood);
}
static void AddFurniture(GenerationProgress progress, string name, int type, int rarity, int minY, int maxY, params int[] wallIDs)
{
try
{
if (progress != null)
{
progress.Message = name;
}
else
{
Main.NewText(name);
}
int generated = 0;
int toGenerate = Main.maxTilesX / rarity;
for (int i = 0; i < toGenerate; i++)
{
if(progress != null)
progress.Set(i / toGenerate);
bool success = false;
byte attempts = 0;
ushort successcount = 0;
while (!success)
{
if (successcount < 65533)
{
successcount++;
}
else
{
successcount = ushort.MaxValue;
break;
}
int x = WorldGen.genRand.Next(1, Main.maxTilesX);
int y = WorldGen.genRand.Next(minY, maxY);
foreach(int wallID in wallIDs)
{
if(Main.tile[x, y].WallType == wallID)
{
byte hasTileCount = 0;
while (!Main.tile[x, y].HasTile)
{
y++;
if (hasTileCount < 80)
{
hasTileCount++;
}
else
{
hasTileCount = byte.MaxValue;
break;
}
}
y--;
WorldGen.PlaceObject(x, y, (ushort)type, true);
if (Main.tile[x, y].TileType == (ushort)type)
{
generated++;
success = true;
}
else
{
attempts++;
if (attempts > 254)
{
success = true;
}
}
}
}
}
}
if(progress != null)
{
GoldensMisc.Log(name + ' ' + Language.GetTextValue("Mods.GoldensMisc.WorldGen.FurnitureGeneratorResults"), generated, toGenerate);
}
else
{
Main.NewText(Language.GetTextValue("Mods.GoldensMisc.WorldGen.FurnitureGeneratorResults", generated, toGenerate));
}
}
//Vanilla Terraria does this, so I guess this is OK...?
catch(Exception e)
{
GoldensMisc.Error("Error during worldgen", e);
}
}
}
}