tAPI [Tutorial] The many aspects of World Generation

I think the code for it wont

public override void WorldGenPostGen()
{
for(int amount = 0; amount < 120; amount++) //70 is the amount of veins
{
int x = Main.rand.Next(0,(int)Main.worldSurface);
int y = Main.rand.Next((int)Main.worldSurface, (int)Main.hellLayer);
int strength = Main.rand.Next(40, 80); //put it back to 4, 8
int steps = Main.rand.Next(40, 80);
WorldGen.OreRunner(x, y, strength, steps, TileDef.byName["MBMod:ErcOre"]);
}
}
here it is

Your maximum x value should be Main.maxTilesX, not (int)Main.worldSurface, when I said about the world surface I meant the y value :p
 
Great tutorial! Really helped a lot. But I got a problem...
My building generates just fine and so does the chest. My chest should has seven items in it but it just has two. It's the same with every chest in every building. Here is the code that I am using.
Code:
            int chestX = Main.rand.Next(1, len - 1);
            int chest1 = WorldGen.PlaceChest(x + chestX, y - floor - 1, 21, false, 1); // 0 for wood chest

                #region Place 1
                int rand2 = WorldGen.genRand.Next(2);
                if(rand2 == 0)
                {
                Main.chest[chest1].item[0].SetDefaults("Dragonborn:Spiritsever");
                Main.chest[chest1].item[0].stack = 1;
                Main.chest[chest1].item[0].Prefix(-1);
                }
                else
                {
                Main.chest[chest1].item[0].SetDefaults("Dragonborn:BlackHoleBlaster");
                Main.chest[chest1].item[0].stack = 1;
                Main.chest[chest1].item[0].Prefix(-1);
                }
                #endregion
                #region Place 2
                int rand = WorldGen.genRand.Next(7);
                if (rand == 0)
                {
                Main.chest[chest1].item[1].SetDefaults(280, false);
                Main.chest[chest1].item[1].Prefix(-1);
                }
                if (rand == 1)
                {
                Main.chest[chest1].item[1].SetDefaults(281, false);
                Main.chest[chest1].item[1].Prefix(-1);
                }
                if (rand == 2)
                {
                Main.chest[chest1].item[1].SetDefaults(284, false);
                Main.chest[chest1].item[1].Prefix(-1);
                }
                if (rand == 3)
                {
                Main.chest[chest1].item[1].SetDefaults(282, false);
                Main.chest[chest1].item[1].stack = WorldGen.genRand.Next(50, 75);
                }
                if (rand == 4)
                {
                Main.chest[chest1].item[1].SetDefaults(279, false);
                Main.chest[chest1].item[1].stack = WorldGen.genRand.Next(25, 50);
                }
                if (rand == 5)
                {
                Main.chest[chest1].item[1].SetDefaults(285, false);
                Main.chest[chest1].item[1].Prefix(-1);
                }
                if (rand == 6)
                {
                Main.chest[chest1].item[1].SetDefaults(953, false);
                Main.chest[chest1].item[1].Prefix(-1);
                }
                if (rand == 7)
                {
                Main.chest[chest1].item[1].SetDefaults(946, false);
                Main.chest[chest1].item[1].Prefix(-1);
                #endregion
                #region Place 3
                int rand3 = WorldGen.genRand.Next(2);
                if (rand3 == 0)
                {
                    Main.chest[chest1].item[2].SetDefaults(168, false);
                    Main.chest[chest1].item[2].stack = WorldGen.genRand.Next(3, 6);
                }
                else
                {
                    Main.chest[chest1].item[2].SetDefaults(166, false);
                    Main.chest[chest1].item[2].stack = WorldGen.genRand.Next(3, 6);
                }
                #endregion
                #region Place 4
                int rand4 = WorldGen.genRand.Next(2);
                if (rand4 == 1)
                {
                    Main.chest[chest1].item[3].SetDefaults(40, false);
                    Main.chest[chest1].item[3].stack = WorldGen.genRand.Next(26) + 25;
                }
                else
                {
                    Main.chest[chest1].item[3].SetDefaults(42, false);
                    Main.chest[chest1].item[3].stack = WorldGen.genRand.Next(26) + 25;
                }
                #endregion
                #region Place 5
                    Main.chest[chest1].item[4].SetDefaults(28, false);
                    Main.chest[chest1].item[4].stack = WorldGen.genRand.Next(3) + 3;
                #endregion
                #region Place 6
                int num10 = WorldGen.genRand.Next(6);
                int stack5 = WorldGen.genRand.Next(1, 3);
                if (num10 == 0)
                {
                    Main.chest[chest1].item[5].SetDefaults(292, false);
                }
                if (num10 == 1)
                {
                    Main.chest[chest1].item[5].SetDefaults(298, false);
                }
                if (num10 == 2)
                {
                    Main.chest[chest1].item[5].SetDefaults(299, false);
                }
                if (num10 == 3)
                {
                    Main.chest[chest1].item[5].SetDefaults(290, false);
                }
                if (num10 == 4)
                {
                    Main.chest[chest1].item[5].SetDefaults(2322, false);
                }
                if (num10 == 5)
                {
                    Main.chest[chest1].item[5].SetDefaults(2325, false);
                }
                if (num10 == 6)
                {
                    Main.chest[chest1].item[5].SetDefaults(2350, false);
                }
                Main.chest[chest1].item[5].stack = stack5;
                #endregion
                #region Place 7
                int num11 = WorldGen.genRand.Next(3);
                    if (num11 == 1)
                    {
                        int stack6 = WorldGen.genRand.Next(1, 3);
                        Main.chest[chest1].item[6].SetDefaults(8, false);
                        Main.chest[chest1].item[6].stack = stack6;
                    }
                    if (num11 == 2)
                    {
                        int stack6 = WorldGen.genRand.Next(15, 30);
                        Main.chest[chest1].item[6].SetDefaults(31, false);
                        Main.chest[chest1].item[6].stack = stack6;
                    }
                    if (num11 == 3)
                    {
                        int stack6 = WorldGen.genRand.Next(1, 7);
                        Main.chest[chest1].item[6].SetDefaults(72, false);
                        Main.chest[chest1].item[6].stack = stack6;
                    }
                #endregion
Any ideas?
 
Great tutorial! Really helped a lot. But I got a problem...
My building generates just fine and so does the chest. My chest should has seven items in it but it just has two. It's the same with every chest in every building. Here is the code that I am using.
Code:
            int chestX = Main.rand.Next(1, len - 1);
            int chest1 = WorldGen.PlaceChest(x + chestX, y - floor - 1, 21, false, 1); // 0 for wood chest

                #region Place 1
                int rand2 = WorldGen.genRand.Next(2);
                if(rand2 == 0)
                {
                Main.chest[chest1].item[0].SetDefaults("Dragonborn:Spiritsever");
                Main.chest[chest1].item[0].stack = 1;
                Main.chest[chest1].item[0].Prefix(-1);
                }
                else
                {
                Main.chest[chest1].item[0].SetDefaults("Dragonborn:BlackHoleBlaster");
                Main.chest[chest1].item[0].stack = 1;
                Main.chest[chest1].item[0].Prefix(-1);
                }
                #endregion
                #region Place 2
                int rand = WorldGen.genRand.Next(7);
                if (rand == 0)
                {
                Main.chest[chest1].item[1].SetDefaults(280, false);
                Main.chest[chest1].item[1].Prefix(-1);
                }
                if (rand == 1)
                {
                Main.chest[chest1].item[1].SetDefaults(281, false);
                Main.chest[chest1].item[1].Prefix(-1);
                }
                if (rand == 2)
                {
                Main.chest[chest1].item[1].SetDefaults(284, false);
                Main.chest[chest1].item[1].Prefix(-1);
                }
                if (rand == 3)
                {
                Main.chest[chest1].item[1].SetDefaults(282, false);
                Main.chest[chest1].item[1].stack = WorldGen.genRand.Next(50, 75);
                }
                if (rand == 4)
                {
                Main.chest[chest1].item[1].SetDefaults(279, false);
                Main.chest[chest1].item[1].stack = WorldGen.genRand.Next(25, 50);
                }
                if (rand == 5)
                {
                Main.chest[chest1].item[1].SetDefaults(285, false);
                Main.chest[chest1].item[1].Prefix(-1);
                }
                if (rand == 6)
                {
                Main.chest[chest1].item[1].SetDefaults(953, false);
                Main.chest[chest1].item[1].Prefix(-1);
                }
                if (rand == 7)
                {
                Main.chest[chest1].item[1].SetDefaults(946, false);
                Main.chest[chest1].item[1].Prefix(-1);
                #endregion
                #region Place 3
                int rand3 = WorldGen.genRand.Next(2);
                if (rand3 == 0)
                {
                    Main.chest[chest1].item[2].SetDefaults(168, false);
                    Main.chest[chest1].item[2].stack = WorldGen.genRand.Next(3, 6);
                }
                else
                {
                    Main.chest[chest1].item[2].SetDefaults(166, false);
                    Main.chest[chest1].item[2].stack = WorldGen.genRand.Next(3, 6);
                }
                #endregion
                #region Place 4
                int rand4 = WorldGen.genRand.Next(2);
                if (rand4 == 1)
                {
                    Main.chest[chest1].item[3].SetDefaults(40, false);
                    Main.chest[chest1].item[3].stack = WorldGen.genRand.Next(26) + 25;
                }
                else
                {
                    Main.chest[chest1].item[3].SetDefaults(42, false);
                    Main.chest[chest1].item[3].stack = WorldGen.genRand.Next(26) + 25;
                }
                #endregion
                #region Place 5
                    Main.chest[chest1].item[4].SetDefaults(28, false);
                    Main.chest[chest1].item[4].stack = WorldGen.genRand.Next(3) + 3;
                #endregion
                #region Place 6
                int num10 = WorldGen.genRand.Next(6);
                int stack5 = WorldGen.genRand.Next(1, 3);
                if (num10 == 0)
                {
                    Main.chest[chest1].item[5].SetDefaults(292, false);
                }
                if (num10 == 1)
                {
                    Main.chest[chest1].item[5].SetDefaults(298, false);
                }
                if (num10 == 2)
                {
                    Main.chest[chest1].item[5].SetDefaults(299, false);
                }
                if (num10 == 3)
                {
                    Main.chest[chest1].item[5].SetDefaults(290, false);
                }
                if (num10 == 4)
                {
                    Main.chest[chest1].item[5].SetDefaults(2322, false);
                }
                if (num10 == 5)
                {
                    Main.chest[chest1].item[5].SetDefaults(2325, false);
                }
                if (num10 == 6)
                {
                    Main.chest[chest1].item[5].SetDefaults(2350, false);
                }
                Main.chest[chest1].item[5].stack = stack5;
                #endregion
                #region Place 7
                int num11 = WorldGen.genRand.Next(3);
                    if (num11 == 1)
                    {
                        int stack6 = WorldGen.genRand.Next(1, 3);
                        Main.chest[chest1].item[6].SetDefaults(8, false);
                        Main.chest[chest1].item[6].stack = stack6;
                    }
                    if (num11 == 2)
                    {
                        int stack6 = WorldGen.genRand.Next(15, 30);
                        Main.chest[chest1].item[6].SetDefaults(31, false);
                        Main.chest[chest1].item[6].stack = stack6;
                    }
                    if (num11 == 3)
                    {
                        int stack6 = WorldGen.genRand.Next(1, 7);
                        Main.chest[chest1].item[6].SetDefaults(72, false);
                        Main.chest[chest1].item[6].stack = stack6;
                    }
                #endregion
Any ideas?
Code:
int chestX = Main.rand.Next(1, len - 1);
int chest1 = WorldGen.PlaceChest(x + chestX, y - floor - 1, 21, false, 1); // 0 for wood chest

#region Place 1
int rand2 = WorldGen.genRand.Next(2);
if (rand2 == 0) {
    Main.chest[chest1].item[0].SetDefaults("Dragonborn:Spiritsever");
    Main.chest[chest1].item[0].stack = 1;
    Main.chest[chest1].item[0].Prefix(-1);
} else {
    Main.chest[chest1].item[0].SetDefaults("Dragonborn:BlackHoleBlaster");
    Main.chest[chest1].item[0].stack = 1;
    Main.chest[chest1].item[0].Prefix(-1);
}#endregion#region Place 2
int rand = WorldGen.genRand.Next(7);
if (rand == 0) {
    Main.chest[chest1].item[1].SetDefaults(280, false);
    Main.chest[chest1].item[1].Prefix(-1);
}
if (rand == 1) {
    Main.chest[chest1].item[1].SetDefaults(281, false);
    Main.chest[chest1].item[1].Prefix(-1);
}
if (rand == 2) {
    Main.chest[chest1].item[1].SetDefaults(284, false);
    Main.chest[chest1].item[1].Prefix(-1);
}
if (rand == 3) {
    Main.chest[chest1].item[1].SetDefaults(282, false);
    Main.chest[chest1].item[1].stack = WorldGen.genRand.Next(50, 75);
}
if (rand == 4) {
    Main.chest[chest1].item[1].SetDefaults(279, false);
    Main.chest[chest1].item[1].stack = WorldGen.genRand.Next(25, 50);
}
if (rand == 5) {
    Main.chest[chest1].item[1].SetDefaults(285, false);
    Main.chest[chest1].item[1].Prefix(-1);
}
if (rand == 6) {
    Main.chest[chest1].item[1].SetDefaults(953, false);
    Main.chest[chest1].item[1].Prefix(-1);
}
if (rand == 7) {
    Main.chest[chest1].item[1].SetDefaults(946, false);
    Main.chest[chest1].item[1].Prefix(-1);
}#endregion#region Place 3
int rand3 = WorldGen.genRand.Next(2);
if (rand3 == 0) {
    Main.chest[chest1].item[2].SetDefaults(168, false);
    Main.chest[chest1].item[2].stack = WorldGen.genRand.Next(3, 6);
} else {
    Main.chest[chest1].item[2].SetDefaults(166, false);
    Main.chest[chest1].item[2].stack = WorldGen.genRand.Next(3, 6);
}#endregion#region Place 4
int rand4 = WorldGen.genRand.Next(2);
if (rand4 == 1) {
    Main.chest[chest1].item[3].SetDefaults(40, false);
    Main.chest[chest1].item[3].stack = WorldGen.genRand.Next(26) + 25;
} else {
    Main.chest[chest1].item[3].SetDefaults(42, false);
    Main.chest[chest1].item[3].stack = WorldGen.genRand.Next(26) + 25;
}#endregion#region Place 5
Main.chest[chest1].item[4].SetDefaults(28, false);
Main.chest[chest1].item[4].stack = WorldGen.genRand.Next(3) + 3;#endregion#region Place 6
int num10 = WorldGen.genRand.Next(6);
int stack5 = WorldGen.genRand.Next(1, 3);
if (num10 == 0) {
    Main.chest[chest1].item[5].SetDefaults(292, false);
}
if (num10 == 1) {
    Main.chest[chest1].item[5].SetDefaults(298, false);
}
if (num10 == 2) {
    Main.chest[chest1].item[5].SetDefaults(299, false);
}
if (num10 == 3) {
    Main.chest[chest1].item[5].SetDefaults(290, false);
}
if (num10 == 4) {
    Main.chest[chest1].item[5].SetDefaults(2322, false);
}
if (num10 == 5) {
    Main.chest[chest1].item[5].SetDefaults(2325, false);
}
if (num10 == 6) {
    Main.chest[chest1].item[5].SetDefaults(2350, false);
}
Main.chest[chest1].item[5].stack = stack5;#endregion#region Place 7
int num11 = WorldGen.genRand.Next(3);
if (num11 == 1) {
    int stack6 = WorldGen.genRand.Next(1, 3);
    Main.chest[chest1].item[6].SetDefaults(8, false);
    Main.chest[chest1].item[6].stack = stack6;
}
if (num11 == 2) {
    int stack6 = WorldGen.genRand.Next(15, 30);
    Main.chest[chest1].item[6].SetDefaults(31, false);
    Main.chest[chest1].item[6].stack = stack6;
}
if (num11 == 3) {
    int stack6 = WorldGen.genRand.Next(1, 7);
    Main.chest[chest1].item[6].SetDefaults(72, false);
    Main.chest[chest1].item[6].stack = stack6;
}#endregion
}

I'm gonna have to carefully explain where it is, but just before you end region 2, and start region 3, you forgot to add a }. So there was only a 1 / 7 chance for your chest to contain seven items. This should fix it (I think, i've not got time to test :p)

(Sorry the code is messy, I recommend adding it yourself. Damn code formatter)
 
Code:
int chestX = Main.rand.Next(1, len - 1);
int chest1 = WorldGen.PlaceChest(x + chestX, y - floor - 1, 21, false, 1); // 0 for wood chest
[SPOILER]
#region Place 1
int rand2 = WorldGen.genRand.Next(2);
if (rand2 == 0) {
    Main.chest[chest1].item[0].SetDefaults("Dragonborn:Spiritsever");
    Main.chest[chest1].item[0].stack = 1;
    Main.chest[chest1].item[0].Prefix(-1);
} else {
    Main.chest[chest1].item[0].SetDefaults("Dragonborn:BlackHoleBlaster");
    Main.chest[chest1].item[0].stack = 1;
    Main.chest[chest1].item[0].Prefix(-1);
}#endregion#region Place 2
int rand = WorldGen.genRand.Next(7);
if (rand == 0) {
    Main.chest[chest1].item[1].SetDefaults(280, false);
    Main.chest[chest1].item[1].Prefix(-1);
}
if (rand == 1) {
    Main.chest[chest1].item[1].SetDefaults(281, false);
    Main.chest[chest1].item[1].Prefix(-1);
}
if (rand == 2) {
    Main.chest[chest1].item[1].SetDefaults(284, false);
    Main.chest[chest1].item[1].Prefix(-1);
}
if (rand == 3) {
    Main.chest[chest1].item[1].SetDefaults(282, false);
    Main.chest[chest1].item[1].stack = WorldGen.genRand.Next(50, 75);
}
if (rand == 4) {
    Main.chest[chest1].item[1].SetDefaults(279, false);
    Main.chest[chest1].item[1].stack = WorldGen.genRand.Next(25, 50);
}
if (rand == 5) {
    Main.chest[chest1].item[1].SetDefaults(285, false);
    Main.chest[chest1].item[1].Prefix(-1);
}
if (rand == 6) {
    Main.chest[chest1].item[1].SetDefaults(953, false);
    Main.chest[chest1].item[1].Prefix(-1);
}
if (rand == 7) {
    Main.chest[chest1].item[1].SetDefaults(946, false);
    Main.chest[chest1].item[1].Prefix(-1);
}#endregion#region Place 3
int rand3 = WorldGen.genRand.Next(2);
if (rand3 == 0) {
    Main.chest[chest1].item[2].SetDefaults(168, false);
    Main.chest[chest1].item[2].stack = WorldGen.genRand.Next(3, 6);
} else {
    Main.chest[chest1].item[2].SetDefaults(166, false);
    Main.chest[chest1].item[2].stack = WorldGen.genRand.Next(3, 6);
}#endregion#region Place 4
int rand4 = WorldGen.genRand.Next(2);
if (rand4 == 1) {
    Main.chest[chest1].item[3].SetDefaults(40, false);
    Main.chest[chest1].item[3].stack = WorldGen.genRand.Next(26) + 25;
} else {
    Main.chest[chest1].item[3].SetDefaults(42, false);
    Main.chest[chest1].item[3].stack = WorldGen.genRand.Next(26) + 25;
}#endregion#region Place 5
Main.chest[chest1].item[4].SetDefaults(28, false);
Main.chest[chest1].item[4].stack = WorldGen.genRand.Next(3) + 3;#endregion#region Place 6
int num10 = WorldGen.genRand.Next(6);
int stack5 = WorldGen.genRand.Next(1, 3);
if (num10 == 0) {
    Main.chest[chest1].item[5].SetDefaults(292, false);
}
if (num10 == 1) {
    Main.chest[chest1].item[5].SetDefaults(298, false);
}
if (num10 == 2) {
    Main.chest[chest1].item[5].SetDefaults(299, false);
}
if (num10 == 3) {
    Main.chest[chest1].item[5].SetDefaults(290, false);
}
if (num10 == 4) {
    Main.chest[chest1].item[5].SetDefaults(2322, false);
}
if (num10 == 5) {
    Main.chest[chest1].item[5].SetDefaults(2325, false);
}
if (num10 == 6) {
    Main.chest[chest1].item[5].SetDefaults(2350, false);
}
Main.chest[chest1].item[5].stack = stack5;#endregion#region Place 7
int num11 = WorldGen.genRand.Next(3);
if (num11 == 1) {
    int stack6 = WorldGen.genRand.Next(1, 3);
    Main.chest[chest1].item[6].SetDefaults(8, false);
    Main.chest[chest1].item[6].stack = stack6;
}
if (num11 == 2) {
    int stack6 = WorldGen.genRand.Next(15, 30);
    Main.chest[chest1].item[6].SetDefaults(31, false);
    Main.chest[chest1].item[6].stack = stack6;
}
if (num11 == 3) {
    int stack6 = WorldGen.genRand.Next(1, 7);
    Main.chest[chest1].item[6].SetDefaults(72, false);
    Main.chest[chest1].item[6].stack = stack6;
}#endregion
}
[/SPOILER]


I'm gonna have to carefully explain where it is, but just before you end region 2, and start region 3, you forgot to add a }. So there was only a 1 / 7 chance for your chest to contain seven items. This should fix it (I think, i've not got time to test :p)

(Sorry the code is messy, I recommend adding it yourself. Damn code formatter)
Oh, I should really have seen that. Anyway, thanks for a quick answear. :happy:
 
Oh, I should really have seen that. Anyway, thanks for a quick answear. :happy:

No problem!

SIDE NOTE:
Also, would anyone be interested in me adding a way to nicely add random loot to chests? Guess it wouldn't take too long! :D
 
I would. That is what I am trying to accomplish right now, but i think there is a easier way to do it. I would be happy if you did a tutorial on it :)
 
Can you show the id's for locked biome chests and the ice, moss, cobweb, and jungle chests? I could use those.
Also, does this tutorial cover making a specific building (like a precise building) randomly? And (if it's possible) make it generate only a few specific times?
 
Getting this error
========================================
Building mod Ingots Galore!

Validating Jsons...
Compiling code...
MWorld.cs (13,17)
The name 'list' does not exist in the current context
list.Insert(19, new WorldGenTask.Action("IngotsGalore:WhatYouAreDoing", delegate
^
MWorld.cs (1,21)
The name 'list' does not exist in the current context
list.Add(new WorldGenTask.Action("IngotsGalore:WhatYouAreDoing", delegate
^
MWorld.cs (1,25)
The name 'list' does not exist in the current context
list.RemoveAt(14);
^
Failed to build Ingots Galore!.

========================================
With a very similar code to what you made.
 
Can you show the id's for locked biome chests and the ice, moss, cobweb, and jungle chests? I could use those.
Also, does this tutorial cover making a specific building (like a precise building) randomly? And (if it's possible) make it generate only a few specific times?

I could, but if you know where to find the sprite sheet for chests, you can just read it off of that. From the left to right starting on style 0.

Getting this error
========================================
Building mod Ingots Galore!

Validating Jsons...
Compiling code...
MWorld.cs (13,17)
The name 'list' does not exist in the current context
list.Insert(19, new WorldGenTask.Action("IngotsGalore:WhatYouAreDoing", delegate
^
MWorld.cs (1,21)
The name 'list' does not exist in the current context
list.Add(new WorldGenTask.Action("IngotsGalore:WhatYouAreDoing", delegate
^
MWorld.cs (1,25)
The name 'list' does not exist in the current context
list.RemoveAt(14);
^
Failed to build Ingots Galore!.

========================================
With a very similar code to what you made.

Is that code in WorldGenModifyTaskList? Also, why are you removing whatever is at 14? I think it's CleanUpStrayTiles, which may not be the best thing to remove from the list :p
 
  • Like
Reactions: Azu
I could, but if you know where to find the sprite sheet for chests, you can just read it off of that. From the left to right starting on style 0.



Is that code in WorldGenModifyTaskList? Also, why are you removing whatever is at 14? I think it's CleanUpStrayTiles, which may not be the best thing to remove from the list :p
By the time you posted it the coder for my team already fixed it :p
 
Thank you! This is some good information. A few parts might be a bit easier to read if they were moved into Code blocks though.

I don't think it's just Terraria that has Y+ in the downward direction. I believe most 2D XNA games share that coordinate system.
CoordintateSystem2D.png

I added a link to this thread to the tAPI Community Resources OP. Thanks again!
 
Back
Top Bottom