Cubitect / cubiomes

C library that mimics the Minecraft biome generation.
MIT License
579 stars 104 forks source link

Line 897 in layers.c makes no sense #41

Closed JetMouseWasTaken closed 3 years ago

JetMouseWasTaken commented 4 years ago

I'm trying to translate it into python just for fun. I should probably be using java so I could make an app about it, but I can do that later. The only reason I like python is that it is easy to understand while you are reading it, which would be good for this for when I try to bring it into other languages. Anyway, line 897 is this: out[i + j*w] = mcFirstInt(cs, 299999)+2; it's only run for biomes that aren't the ocean, which doesn't make much sense because this is after the ocean has already been divided into different types of ocean (regular and deep), but that's not the problem. It looks like it's taking everything that isn't land and replacing it with a pseudo-random number between 0 and 299999, which is way more numbers than biomes that Minecraft has. Is that what it's supposed to be doing?

Also, earlier I noticed something similar happening on line 655 of the same file, which is: v |= (1 + mcFirstInt(cs, 15)) << 8 & 0xf00; I had fixed it by changing it to this: v |= (1 + mcFirstInt(cs, 15)) << (8 & 0xf00); but that completely changes the meaning of the line because now it will never be bit-shifted. Just now, I noticed that it fixes itself without my edit after going through the mapBiome function, but it doesn't make sense that the numbers should be bit-shifted to a very large number.

As you can probably tell, I'm not very good at programming in C. Anyway, thanks if you can help!

Cubitect commented 4 years ago

The first line you mentioned acts in a way as a pseudo random number source for the river branch. (It's a little outdated, but you can have a look at docs/layers.pdfto visualise the concept of branching layers.) This branch goes through some other layers to determine where the river biome should generate, and does not actually contribute biomes by it self. River biomes only exist on lands that's why it can skip locations where there is ocean. However, the mapHills layer also uses this random source to generate small islands inside deep ocean, so only normal ocean can be skipped. (Despite what the mapDeepOcean layer might suggest ocean variant don't accutally occur at this point, but the code is there to reflect the implementation in Minecraft).

Regards the second line you pointed out: bit shifts have lower precedence than bitwise AND and will be computed first, so the line is actually equivalent to v |= ((1 + mcFirstInt(cs, 15)) << 8) & 0xf00;.