# A possible flaw in ocean terrain generation code

## Recommended Posts

I increased the number of waterlogs in a map by 30 times and allowed stacking to calculate the distribution pattern of waterlogs. Afterwards, I discovered a possible flaw in terrain generation.

You can see that the water wood is mainly concentrated in these three linear red box areas, and in the dozens of maps I generated, the water wood is basically generated in this form as a linear area.

This is because when the initial generation point of waterlog cannot meet the conditions for water wood generation, the water wood scene will search for the next judgment point in the positive direction of the x-axis. The initial generation point of water wood is random across the entire map, which means that when the initial generation point is randomly located on the mainland, the final generation position is often the seaside in the positive x-axis direction of the mainland. If the sea area in the positive x-axis direction does not have enough generating space, then water trees are more likely to generate on the map edge in the negative x-axis direction. (scripts/map/ocean_gen.lua: 766-813 function findLayoutPositions)

local function findLayoutPositions(size, edge_dist, populating_tile, count, min_dist_from_land)
local positions = {}
edge_dist = edge_dist or 0
min_dist_from_land = min_dist_from_land or 0

local width, height = world:GetWorldSize()
local adj_width, adj_height = width - 2 * edge_dist - size, height - 2 * edge_dist - size
local i, j = 0, 0

while j < adj_height and (count == nil or #positions < count) do
local y = ((start_y + j) % adj_height) + edge_dist
while i < adj_width and (count == nil or #positions < count) do
-- check the corners first
local x = ((start_x + i) % adj_width) + edge_dist
local x2, y2 = x + size - 1, y + size - 1
if checkTile(x2 + min_dist_from_land, y - min_dist_from_land, populating_tile) and checkTile(x2 + min_dist_from_land, y2 + min_dist_from_land, populating_tile) then
if checkTile(x - min_dist_from_land, y - min_dist_from_land, populating_tile) and checkTile(x - min_dist_from_land, y2 + min_dist_from_land, populating_tile) then
--print("Found 4 corners", x, y, x2, y2)
--check all tiles
local ok, last_x, last_y = checkAllTiles(populating_tile, x - min_dist_from_land, y - min_dist_from_land, x2 + min_dist_from_land, y2 + min_dist_from_land)
if ok == true then
--bottom-left
--print(string.format("Location found (%4.2f, %4.2f)", x, y))
--local adj = 0.5 * (size - actualsize)
--return {x + adj, y2 - adj} --{0.5 * (x + x2), 0.5 * (y + y2)}
table.insert(positions, {x = x, y = y, x2 = x2, y2 = y2, size = size})
i = i + size + 1
else
--print(string.format("Failed at (%4.2f, %4.2f) skip, (%4.2f, %4.2f)", last_x, last_y, x, y))
last_x = math.clamp(last_x, x, x2)
i = i + last_x - x + 1
end
else
i = i + 1
end
else
--print(string.format("Failed on x2, skip (%4.2f, %4.2f)", x, y))
i = i + size + 1
end
end
j = j + 1
i = 0
end

return positions
end

This code is used to generate water logs and salt mines. I guess the purpose of writing this is to allow most of the waterlogs and salt mines to be generated closer to the mainland, but the final result is that the number of water logs and salt mines in the positive x-axis direction is much higher than in other directions of the sea. My suggestion is to randomize the x-direction and y-direction of this algorithm, so that the number of waterlog and salt mines in the water distributed in the four directions of the sea area will be relatively average and reasonable.

This post was generated using translation software, and any issues can be pointed out. Thank you for your patient reading!

oh no

##### Share on other sites

It took me a couple of reads (I’m clueless when it comes to code) but I think I understand.

So the biomes aren’t evenly distributed across the map? That could explain why one side of the ocean always feels especially bare. I’d be very interested in seeing how world generation would change were this tweaked.

##### Share on other sites

9 hours ago, YouKnowWho said:

So the biomes aren’t evenly distributed across the map? That could explain why one side of the ocean always feels especially bare. I’d be very interested in seeing how world generation would change were this tweaked.

Yes, that's the reason. This resulted in most of the waterlog and salt mines being generated not far from the shore, but also made the sea on one side bare.

##### Share on other sites

Ah, that's why the ocean is so empty... it's really hard to tell whether something is intended or a bug until you start checking the scripts...

##### Share on other sites

On side note i rolled loopy "Atoll" shaped worlds so many times and never got Waterlogged or Lunar Island in the middle like i seen on some screenshots..
The middle ocean is always empty.

##### Share on other sites

Take this from someone who doesn't understand code, but could it be that they coded it this way so it doesn't collide with something else?

##### Share on other sites

Take this from someone who doesn't understand code, but could it be that they coded it this way so it doesn't collide with something else?

i mean not like there's much in the ocean to collide with...

##### Share on other sites

tbh I see this as less of a bug, more of a feature.  Kinda like how you can locate lunar island from the main land generation being "cut off" to fit the biome, this can probably help me locate these resources more easily.  I'd just need to note map orientation before exploring.

##### Share on other sites

10 minutes ago, Shosuko said:

I'd just need to note map orientation before exploring.

22 hours ago, Ardyn said:

in the positive x-axis direction is much higher than in other directions of the sea.

So what side of the map does this prefer?

##### Share on other sites

1 hour ago, Shosuko said:

tbh I see this as less of a bug, more of a feature.  Kinda like how you can locate lunar island from the main land generation being "cut off" to fit the biome, this can probably help me locate these resources more easily.  I'd just need to note map orientation before exploring.

Considering that this highly influential mechanism has existed in the game for at least 3 years before being discovered, I am skeptical about it

##### Share on other sites

On 5/25/2023 at 11:14 AM, Shosuko said:

Kinda like how you can locate lunar island from the main land generation being "cut off" to fit the biome

This isnt a bug so much as a “make sure the land masses dont touch” check. But it definitely wasn’t introduced so we could find the lunar island. Im pretty sure at least

##### Share on other sites

2 hours ago, Copyafriend said:

This isnt a bug so much as a “make sure the land masses dont touch” check. But it definitely wasn’t introduced so we could find the lunar island. Im pretty sure at least

I'd agree with you, but given how Klei also made it so that there can be only shallow waters between the mainland and lunar, it might very well have been intentional for such a purpose.