Working as Designed Probably something wrong with pyramid placement source code

Unftf🌴

Terrarian
Steam or GOG
Steam
Single Player/Multiplayer
Both
Operating System
Windows 10
Terraria Version
1.4.2
Controls Used
Keyboard/Mouse
The placement of pyramids during world generation changed with 1.4. I had some look at the current (1.4.1.2) code. (edit: checked 1.4.2 as well, still the case)
It might be some translation error but I think something wrong is ongoing there.

(not sure if I'm allowed to post code here. I will describe it without)

1.) In "Dunes" world generation step the Y-coordinate (random point Y from 0 to MaxTilesY) of a new potential desert is compared with the right snow origin which is a X-coordinate. This makes deserts at the left of snow biomes, in jungle or in snow biome more likely.
(The left snow origin is compared with the X-coordinate)

2.) In "Ocean Sand" the value 1 in the first for loop don't seem to have any impact instead of mixing up random numbers. The loop continues with value 2 before placing any sand or pyramid location.
For other values (at ocean location) the pyramid location can only be located at half beach size which should somewhere between (maxTilesX-) 160 and 190.
At the "Pyramid" world generation step all positions with less than 300 range to world border get ignored.
So this part should never be able to generate pyramids.

3.) At "Pyramid" world generation step each stored pyramid location is compared with locations stored at a smaller index. If one is closer than 220 blocks it gets ignored. It does not check if a pyramid actually generated there. E.g. If a world has 3 locations which differ by 200 blocks each only one pyramid can be generated despite the first and the third having a distance of 400 blocks.


Also it is a little sad world generation is limited to 2(small), 3(medium), 4(large) pyramids. ;(
Was quite fun finding those one in a million 7 pyramid seeds. More than 2/3/4 was very rare but possible.
 
Last edited:
I made a post in the Q&A thread about this.

... Is it a bug that even if a dunes biome does not generate a pyramid, it always prevents nearby pyramids from spawning? Overlapping dunes biomes currently lower the chance of getting a pyramid instead of raising it. I think pyramids might be rarer than they were intended to be. ...

I had two other bugs I asked questions about in this Q&A that were fixed in 1.4.2. This is the only bug I mentioned in the Q&A that wasn't fixed. Developer replies suggested there might have been a discussion for this particular bug, but the statement is somewhat ambiguous.

... Well, your other questions are being discussed in dev chat as we speak...

The fix for this bug is straightforward if you understand the code:

In the GenerateWorld function the game records of the places it will try to generate pyramids using local variables PyrX, PyrY, and numPyr. These variables are populated during the "Dunes" and "Ocean Sand" generation passes.

In the "Pyramids" generation pass, the game checks PyrX of all previously generated pyramids and will not generate one if it is too close.

The problem is that when a pyramid is not generated for any reason, it does NOT have it's position removed from PyrX and PyrY.

When a pyramid is not generated, before moving on the the next pyramid, it either needs it's position removed from PyrX and PyrY or have it's coordinates moved outside of the map so it won't block other pyramids.

edit: fixed typo
 
Last edited:
... Is it a bug that even if a dunes biome does not generate a pyramid, it always prevents nearby pyramids from spawning? Overlapping dunes biomes currently lower the chance of getting a pyramid instead of raising it. I think pyramids might be rarer than they were intended to be. ...
I'm not sure if that's always the case. It should only prevent if an earlier position was at an invalid pyramid position. However due to the strange, not symmetric dune placement that should often be the case. They will nearly always be at the left of the snow biome (unless no spot found after many trials). If the snow biome is at the left side the dungeon will also be there. Pyramids left of the dungeon(+0.15 MaxTilesX) are not allowed. This makes pyramids very rare in left snow worlds

In the GenerateWorld function the game records of the places it will try to generate pyramids using local variables PyrX, PyrY, and numPyr. These variables are populated during the "Dunes" and "Ocean Sand" generation passes.
Currently "Ocean Sand" generates only invalid positions inside the ocean. Which lowers the chances for Pyramids near the left Ocean.
In the "Pyramids" generation pass, the game checks PyrY of all previously generated pyramids and will not generate one if it is too close.
I guess its typo but it is checking PyrX not PyrY.

The problem is that when a pyramid is not generated for any reason, it does NOT have it's position removed from PyrX and PyrY.

When a pyramid is not generated, before moving on the the next pyramid, it either needs it's position removed from PyrX and PyrY or have it's coordinates moved outside of the map so it won't block other pyramids.
yep
 
Given the number of issues here, the testing time required to review it all, and that it would result in a potentially large increase to Pyramids per world (on top of the 1.4.1 pyramid buff), we are not going to be making changes here due to time constraints.
 
Given the number of issues here, the testing time required to review it all, and that it would result in a potentially large increase to Pyramids per world (on top of the 1.4.1 pyramid buff), we are not going to be making changes here due to time constraints.
I understand the time constraints, which is why I suggest that only a single change is made and tested. (#3) That is to remove or invalidate a pyramid in the pyramid list if it isn't generated.

That specific problem is the reason that pyramids aren't showing up on special seeds where they should even with a 100% spawn rate set in worldgen settings.

In other word, my assessment of the issues stated in the original post:
  1. Skip fixing issue 1. Asymmetric placement causing rare pyramids in left snow worlds probably should be left alone to avoid changing pyramid spawn rate too much.
  2. Skip fixing issue 2, or just remove the code instead of fixing it. Fixing issue 3 makes this bug no longer harmful.
  3. This is the bug that should be fixed.
 
Given the number of issues here, the testing time required to review it all, and that it would result in a potentially large increase to Pyramids per world (on top of the 1.4.1 pyramid buff), we are not going to be making changes here due to time constraints.
1) and 3) can quickly be fixed. 2.) not so important, a fix of 3.) also partly fixes 2.) (pyramids in ocean no impact in placement). Not generating another Pyramid was probably intentional there (this will limit max Pyramid count to 2 (small world),3 (medium world),4 (large world).)

In "Dunes" the 2nd condition is nearly always true.
Code:
bool flag62 = origin5.X > snowOriginLeft - 300 && origin5.Y < snowOriginRight + 300;
This makes pyramid placement very asymmetric.
A fix would just be:
Code:
bool flag62 = origin5.X > snowOriginLeft - 300 && origin5.X < snowOriginRight + 300;

3) could be fixed as Mail wrote by just set invalid PyrX to 0
-------
The 1.4.1 pyramid buff mainly need to happen bc of those odd dune positions.
-----
I would voluntarily generate worlds for both case (if I somehow get access to the modified version) and do some statistics about it to find a new 'ChanceOfPyramid' with same mean Pyramid count.
Currently ChanceOfPyramid is 0.8. Which does not represent the real distribution.

---------------------------
@Mail just fixing 3 would probably not enough. Left snow worlds still get much less pyramids than right snow worlds.
But that all theoretical so far. I think I will generate some (large and maybe small) worlds during night and do some statistics about it. (snow: left/right, Pyramid count, Pyr left/right, Pyr left/right of snow). @Mail if you like you can do some stats about medium or some additional for large/small to compare our results.
I predict a significant impact of issue 1) at the pyramid distribution for at least small worlds.
 
Last edited:
As explained by @Leinfors, the problem here isn't that the code changes are difficult, it is that that the changes need to be validated via testing. They won't be able to make the fix if there is a massive increase in pyramids.

In "Dunes" the 2nd condition is nearly always true.
Code:
bool flag62 = origin5.X > snowOriginLeft - 300 && origin5.Y < snowOriginRight + 300;
This makes pyramid placement very asymmetric.
A fix would just be:
Code:
bool flag62 = origin5.X > snowOriginLeft - 300 && origin5.X < snowOriginRight + 300;
Very clearly an error due to a typo and not intentional. I would want this fixed if possible.

I would voluntarily generate worlds for both case (if I somehow get access to the modified version) and do some statistics about it to find a new 'ChanceOfPyramid' with same mean Pyramid count.
I might be able to make a Terraria Tweaker 2 plugin for this. Should be possible to make a plugin that automates generating many worlds and report stats at the end, but this might all be wasted effort if the devs aren't willing to fix this even if we provide detailed statistics.


EDIT: Added a Terraria Tweaker 2 Plugin that fixes issues #1 and #3. Additional code patch hooks are included to allow easily fixing issue #2. Try using TT2_2.3.1405.1 if the latest version causes crashes.
 

Attachments

  • PyramidFixv1000.cs
    8.3 KB · Views: 102
Last edited:
I did some test (by hand) about the current 1.4.2. I generated 100 Large worlds and did some stats about it.
1.) I got not a single world with a dune to the right of the snow biome (some I may not spot bc located directly at the Main Desert or in right ocean). The best I got was a tiny one which probably wasn't a real dune:
1617178764097.png
2.) From 100 worlds, 57 had snow to the left, 43 to the right.
Out of those 57 left-snow-worlds I got 4 with 1 pyramid.
Out of those 43 right-snow-worlds I got 20 with 1, 8 with 2 and 3 with 3.
This will result in a mean pyramid count:
In mean 0.07 pyramids for left-snow-worlds, 7% chance to have any pyramid
In mean 1.05 pyramids for right-snow-wolds, 72% chance to have any pyramid
(In mean 0.49 pyramids in any large world, 35% chance to have any pyramid)
=> about 15 times more in right-snow-worlds
As eye catcher here some diagram about it:
1617180056789.png

So it's not just small difference. Basically it means to generate a new world if you got a left-snow-world.


-----------------------------------------------------------------------------------
@Mail I didn't knew about Terraria Tweaker 2. Nice work. Let me see if I'm able to run it. Will do some update about this.

-------------------------------

Update:
Did some run with the fix Mail proposed on top (fix 1. and fix 3.):
(out of 103 worlds)
51 left-snow-worlds (LSW)
52 right-snow-world (RSW)
1) This time I got Dunes to the left and right of the snow biome:
LSW: 79 to the left, 82 to the right of the snow biome
RSW: 83 to the left, 65 to the right of the snow biome
2) Pyramid distribution:
LSW: 16 with 1 Pyramid, 13 with 2 Pyramid
RSW: 27 with 1 Pyramid, 5 with 2 Pyramid
This will result in a mean pyramid count:
In mean 0.82 pyramids for left-snow-worlds, 57% chance to have any pyramid
In mean 0.71 pyramids for right-snow-wolds, 62% chance to have any pyramid
(In mean 0.77 pyramids in any large world, 59% chance to have any pyramid)
1617226743449.png

TODO: scaling down the chance of Pyramid to get similar mean count as in 1.4.2 (mean of all kind of worlds).
Dunes can now also be at the right of the snow biome but somehow they are still a little more often to the left of snow biome for LSW.
Maybe test with some bigger test size. Or if I remember correct there was at some point also a asymmetric generation of the snow biome (TODO check that as well, edit: at least they look different at world map).

-------------
Update I did a run with both fixes and a pyramid chance of 0.6 instead of 0.8
(sorry for the other format)
Left snow worlds:​
74​
Right snow worlds:​
95​
Total:​
169​
Mean N of P:​
0,5857988166​
Chance for P:​
0,4852071006​


Number of P at left S:​
Number of P at right S:​
43​
56​
at least one left S:​
at least one right S:​
35​
47​
One P at left S:​
One P at right S:​
27​
39​
Two P at left S:​
Two P at right S:​
8​
7​
3 P at left S:​
3 P at right S:​
0​
1​
4 P at left S:​
4 P at right S:​
0​
0​
Mean N of P left:​
Mean N of P right:​
0,5810810811​
0,5894736842​
Chance of P left:​
Chance of P right:​
0,472972973​
0,4947368421​
Total N of P:​
Total mean N of P:​
99​
0,5857988166​
Total at least one P:​
Total chance for P:​
82​
0,4852071006​



LSW D to the left of S:​
112​
LSW D to the right of S:​
118​
LSW D to the left of S mean count:​
1,5135135135​
LSW D to the right of S mean count:​
1,5945945946​
RSW D to the left of S:​
151​
RSW D to the right of S:​
128​
RSW D to the left of S mean count:​
1,5894736842​
RSW D to the right of S mean count:​
1,3473684211​
-> about equal mean P count and chance for worlds with snow to the left and right
-> In mean 0.59 pyramids in any large world, 49% chance to have any pyramid
-> still a little higher than in 1.4.2

However the Dune distribution still looks a little off. LSW get too many Dunes to the left of the Snow Biome (assuming there are less there because less space)
 
Last edited:
I double checked the decompiled code and fix the snow side fix (#1) is live in update 1.4.2.1

There don't appear to be fixes for #2 and #3.

My patch fixes #3 which which I would expect to be more relevant on medium to large drunk worlds. It probably has no effect on most worlds, but can increase pyramids slightly on worlds that were getting at least two less than the maximum possible.

I haven't studied the code well enough to have an opinion about bug #2. If I had a better idea of how the pyramid generation has changed the past few updates, this would probably be more obvious.

Worlds with the dungeon close to the center still tend to have fewer or no pyramids even with drunk mode enabled, but that seems like a design choice and not a bug. When the dungeon is near the center, that entire side of the map past the dungeon is blocked from getting any pyramids.
 
Phew, at least they did one fix. Very happy about that. And they also updated two more of my bug reports. Best update ever :). Only crucial left over is different world gen at different OS.
My patch fixes #3 which which I would expect to be more relevant on medium to large drunk worlds. It probably has no effect on most worlds, but can increase pyramids slightly on worlds that were getting at least two less than the maximum possible.
The problem 3 are too near together won't happen that often. In test only 1 out of 100 but all 3 locations too close to Dungeon anyways.
But I think two close to each other were the first gets not placed isn't too rare.
Two possible location too close to each other happened in 25 of 100 cases. However a location got rejected in 72%. Only in 2% it got rejected because it was too close to another location. In most cases rejected bc it was too near to the Dungeon or no sand there.
However I think now, with the new update (the fix of #1) Dunes get more spread which reduces the chance for #3
But some tests about it would be interesting.
With current update the chance for at least one pyramid should increase but chance for 3 or even 4 pyramids should reduce (bc they often get rejected bc of no sand, ~23% in test).


I haven't studied the code well enough to have an opinion about bug #2. If I had a better idea of how the pyramid generation has changed the past few updates, this would probably be more obvious.
In 1.3.5 there was no Dunes world gen step. The Ocean Sand world gen step just was named Sand and generated Ocean Sand and Sand-mini-biomes (named Dunes now) all over the map. It did also include the possible pyramid locations. Theoretically up to 20 sand-mini-biomes including a pyramid location was possible. (Best I found was 7 pyramids).
So now "Ocean Sand" basically dont need to generate any pyramid locations anymore unless they decide to allow more than 2|3|4 pyramids in one world.

Worlds with the dungeon close to the center still tend to have fewer or no pyramids even with drunk mode enabled, but that seems like a design choice and not a bug. When the dungeon is near the center, that entire side of the map past the dungeon is blocked from getting any pyramids.
Yep, but even bigger impact has the random amount of dunes located in the world. In small worlds in can be 1 or 2. Worlds with only 1 Dune-mini-biome have only about half the chance for a pyramid compared to a world with 2 dunes.
------------------------------------------------------------

Edit: In case someone is interested I did some run with the new update 1.4.2.1 which did fix issue #1 (but not #3!).
I checked 222 worlds, 105 had snow to the left, 117 to the right
Left snow worlds:​
105​
Right snow worlds:​
117​
Total:​
222​
Mean N of P:​
0,6261261261​
Chance for P:​
0,472972973​


Number of P at left S:​
Number of P at right S:​
74​
65​
at least one left S:​
at least one right S:​
55​
50​
One P at left S:​
One P at right S:​
38​
36​
Two P at left S:​
Two P at right S:​
15​
13​
3 P at left S:​
3 P at right S:​
2​
1​
4 P at left S:​
4 P at right S:​
0​
0​
Mean N of P left:​
Mean N of P right:​
0,7047619048​
0,5555555556​
Chance of P left:​
Chance of P right:​
0,5238095238​
0,4273504274​
Total N of P:​
Total mean N of P:​
139​
0,6261261261​
Total at least one P:​
Total chance for P:​
105​
0,472972973​


LSW D to the left of S:​
131​
LSW D to the right of S:​
177​
LSW D to the left of S mean count:​
1,2476190476​
LSW D to the right of S mean count:​
1,6857142857​
RSW D to the left of S:​
166​
RSW D to the right of S:​
188​
RSW D to the left of S mean count:​
1,4188034188​
RSW D to the right of S mean count:​
1,6068376068​
-> mean number of Pyramids is 0.63, chance to have any pyramid is 47%
(before this fix it was 0.49 pyramids and 35% chance to have any pyramid) ==> @devs maybe lower chance of pyramids a little ( 0.60 instead of 0.8?)

This time the results for left and right aren't that close:
left mean 0.7, chance 52%
right mean 0.56, chance 43%

This time Dunes also not crowded at the left of left snow worlds but they are at the right of right snow worlds.
Test size too small?

---------------------------
Edit 2: @Mail
To test the impact of fix #3 I did a run with current update 1.4.2.1 including fix #1, and add fix #3 with TT
(to compare results better I also used 222 worlds (but not equal seeds))
Left snow worlds:​
115​
Right snow worlds:​
107​
Total:​
222​
Mean N of P:​
0,6396396396​
Chance for P:​
0,4954954955​


Number of P at left S:​
Number of P at right S:​
77​
65​
at least one left S:​
at least one right S:​
58​
52​
One P at left S:​
One P at right S:​
39​
40​
Two P at left S:​
Two P at right S:​
19​
11​
3 P at left S:​
3 P at right S:​
0​
1​
4 P at left S:​
4 P at right S:​
0​
0​
Mean N of P left:​
Mean N of P right:​
0,6695652174​
0,6074766355​
Chance of P left:​
Chance of P right:​
0,5043478261​
0,4859813084​
Total N of P:​
Total mean N of P:​
142​
0,6396396396​
Total at least one P:​
Total chance for P:​
110​
0,4954954955​


LSW D to the left of S:​
175​
LSW D to the right of S:​
171​
LSW D to the left of S mean count:​
1,5217391304​
LSW D to the right of S mean count:​
1,4869565217​
RSW D to the left of S:​
162​
RSW D to the right of S:​
158​
RSW D to the left of S mean count:​
1,5140186916​
RSW D to the right of S mean count:​
1,476635514​
with the additional fix #3:
-> mean number of Pyramids is 0.64, chance to have any pyramid is 50%
(recent update 1.4.2.1 (with just fix#1) it was 0.63 pyramids and 47% chance to have any pyramid) ==> almost equal, fix #3 seems to have about no impact
(before the update 1.4.2.1 (with fix#1) it was 0.49 pyramids and 35% chance to have any pyramid)

The results for left and right were more close than just fix #1:
left mean 0.67, chance 50%
right mean 0.61, chance 49%

But again Dunes were crowded at the left of left snow worlds.
Without the fix #3 they are crowded to the right at right snow worlds. With the fix #3 they are crowded at the left of left snow worlds.
Strange, theoretically fix #3 should have no impact in Dune placement.
Test size too small?


------------------------------------------------------------
EDIT 3:
For larger test set I did run some more at current update 1.4.2.1 (just fix 1)
combined with the numbers on top
(222 worlds, 105 had snow to the left, 117 to the right)
In total I got:
409 worlds (198 snow at left, 211 snow to the right )

Left worlds had: 70 one Pyramid, 31 two, 2 three -> 52% chance, 0.7 mean
Right worlds had: 75 one Pyramid, 25 two, 2 three -> 48% chance, 0.62 mean

So in total they had:
mean number of Pyramids is 0.66, chance to have any pyramid is 50%
(before this fix it was 0.49 pyramids and 35% chance to have any pyramid) ==> @devs maybe lower chance of pyramids a little ( 0.60 instead of 0.8?)

---------------------------------------------------------------------
Edit 4:
I did some test with 1.4.2.1 (just fix #1) and 60% pyramid chance:
For 304 worlds, 132 snow to the left, 172 snow to the right I got:
In total a mean of 0.55 pyramids per world. About 47% of the world had at least one.

Left snow worlds had: 47 one Pyramid, 12 two, 1 three -> 45% chance, 0.56 mean
Right snow worlds had: 71 one Pyramid, 10 two, 1 three -> 48% chance, 0.55 mean

This is closer to the mean value before update 1.4.2.1 which fixed #1
(before fix it was 0.49 pyramids and 35% chance to have any pyramid)

It looks like with fix #1 a world with more than 1 pyramid is more rare now but the chance to get at least one is higher now.
 
Last edited:
Fix #3 might have a bigger impact on large drunk world seeds. It won't make any difference unless pyramids are crowded together, which is rarer the smaller the world is.

In theory, with fix#3 large worlds that had 0-2 pyramids can increase up to 3. If the unspawnable Ocean Sand pyramids can be close enough, it could increase to the maximum of 4, but I don't know if that is possible.

If you modify Dunes.Count.Min and Dunes.Count.Max in Worldbuilding.Configuration.json you can get the game to spawn much larger deserts. When you change it to a large value, pyramids tend to overlap in a way that creates a cascade of spawn failures. Bug #3 needs to be fixed to allow those configuration options to work properly at larger values. I know fixing this bug is a huge benefit to modders, but I'm not sure how relevant it is for large worlds.
 
In drunk worlds it may have higher impact but in normal world gen (with fix #1 as it is in 1.4.2.1) it has about no impact. At least in my test run:
mean Pyramid count from 0.63 (only fix #1) to 0.64 (with fix #1 and #3) and
chance to have a pyramid from 47% to 50%

I migh have done some wrong statements about the Ocean Sand pyramids. As long they do not add more pyramids in the ocean sand world gen step they have no impact in pyramid placement (with or without fix #3). In the pyramid location array they are placed after the dune pyramids. In pyramid gen it only checks array entries lower than current.
And pyramids in ocean get skipped bc to near to border.
So currently their only impact is changing the random number generation.

Ye, if changes done in the Configuration.json the impact of #3 will increase. Another way to increase the impact of #3 would be the increase of the beach sizes. This might result in some pyrmids below water.


-------------------------
some more stats about current world gen in 1.4.2.1 for small worlds (master mode)
Out of 1000 worlds (502 snow to the L, 498 snow to the r):
The chance for any pyramid is 27%. The mean count 0.28 pyramids.
259 had one pyramid, 11 had two pyramids (3 with snow to the right).
 
Last edited:
Back
Top Bottom