More randomness for slime chunks #4466

Merged
the-real-herowl merged 1 commits from kno10/VoxeLibre:slime-spawning into master 2024-09-09 19:58:56 +02:00
Member

The old code caused a diagonal pattern. If some x,z was a slime chunk, then so was x+1,z-1.
Use a classic pseudo-random hashing approach instead, by multiplication of chunk numbers
with large primes that should be more random.

  • make slime density (as 1 in N) and maximum light level (default: no limit) configurable
  • Allow using a 3d chunking system where y is also used for hashing

This pull does not modify spawn frequency, only the chunk logic.

Because this replaces the computation of slime chunks, do not expect slimes to spawn in the same places as before. For testing, you can increase the slime chunk density, e.g., to 2 (every other chunk). There is a commented log that will print, e.g., Chunk 1182545475 is a slime chunk: false and Chunk 2924583950 is a slime chunk: true

The old code caused a diagonal pattern. If some x,z was a slime chunk, then so was x+1,z-1. Use a classic pseudo-random hashing approach instead, by multiplication of chunk numbers with large primes that should be more random. - make slime density (as 1 in N) and maximum light level (default: no limit) configurable - Allow using a 3d chunking system where y is also used for hashing This pull does *not* modify spawn frequency, only the chunk logic. Because this replaces the computation of slime chunks, do not expect slimes to spawn in the same places as before. For testing, you can increase the slime chunk density, e.g., to 2 (every other chunk). There is a commented log that will print, e.g., `Chunk 1182545475 is a slime chunk: false` and `Chunk 2924583950 is a slime chunk: true`
the-real-herowl requested changes 2024-06-26 16:47:07 +02:00
the-real-herowl left a comment
Owner

Aside of the typo in a comment, lgtm... as long as slimes don't spawn on the surface more than they do without this patch.

Haven't tested yet.

Aside of the typo in a comment, lgtm... as long as slimes don't spawn on the surface more than they do without this patch. Haven't tested yet.
@ -9,0 +13,4 @@
local slime_max_light = (tonumber(minetest.settings:get_bool("mcl_mobs_slime_max_light", true)) or minetest.LIGHT_MAX) + 1
-- maximum light level for swamp spawning
local swamp_light_max = 7
-- maximum highzt to spawn in slime chunks

Typo: height not highzt?

Typo: `height` not `highzt`?
rudzik8 marked this conversation as resolved
the-real-herowl added the
mobs
code quality
labels 2024-06-26 16:59:03 +02:00
kno10 force-pushed slime-spawning from ee0db05876 to 0edcfce8d4 2024-06-26 22:03:23 +02:00 Compare
Author
Member

Fixed the typo, thanks.

Fixed the typo, thanks.
Member

Fixed the typo, thanks.

Next time you fix something, please mark the conversation as 'Resolved'.

> Fixed the typo, thanks. Next time you fix something, please mark the conversation as 'Resolved'.
rudzik8 requested changes 2024-06-27 05:26:36 +02:00
rudzik8 left a comment
Member

Here's your review.

I haven't been able to get any slimes to spawn. Perhaps it's because of all the settings being so broken?

Here's your review. I haven't been able to get any slimes to spawn. Perhaps it's because of all the settings being so broken?
@ -9,0 +8,4 @@
-- slime density, where default N=10 is every 10th chunk
local slime_density = math.floor(tonumber(minetest.settings:get("mcl_mobs_slime_density")) or 10)
-- use 3D chunking instead of 2d chunks
local slime_3d_chunks = minetest.settings:get("mcl_mobs_slime_3d_chunks") or false
Member
  1. Getting boolean values through get method is... bizarre.
  2. get_bool can handle default values as its second argument, see http://api.minetest.net/class-reference/#methods_16.
1. Getting boolean values through `get` method is... bizarre. 2. `get_bool` can handle default values as its second argument, see http://api.minetest.net/class-reference/#methods_16.
kno10 marked this conversation as resolved
@ -9,0 +10,4 @@
-- use 3D chunking instead of 2d chunks
local slime_3d_chunks = minetest.settings:get("mcl_mobs_slime_3d_chunks") or false
-- maximum light level, for slimes in caves only, not magma/swamps
local slime_max_light = (tonumber(minetest.settings:get_bool("mcl_mobs_slime_max_light", true)) or minetest.LIGHT_MAX) + 1
Member

So now you're getting an integer value through get_bool, with true as the default value. O_o

So now you're getting an integer value through `get_bool`, with `true` as the default value. O_o
Author
Member

LOL, put that into the wrong line...

LOL, put that into the wrong line...
kno10 marked this conversation as resolved
@ -88,2 +27,2 @@
assert(calculate_chunk_value(vector.new(16,0,64)) == (3), "calculate_chunk_value failed")
assert(calculate_chunk_value(vector.new(-160,0,-160)) == 0, "calculate_chunk_value failed")
local math_floor = math.floor
local math_max = math.max
Member

Why localize these functions if you're already using them unlocalized a few lines above (when handling settings)?

Why localize these functions if you're already using them unlocalized a few lines above (when handling settings)?
Author
Member

Following this pattern from other modules, although the minetest "vector" class does not seem to bother.
My guess was that this can help with performance in functions that are frequently called (as it is not necessary to check if someone modified math.floor in the meantime).
For the settings, which supposedly is only executed once and hence not performance critical, I would prefer the standard version.
But I have to rely on your experience whether localizing is beneficial or not.

Following this pattern from other modules, although the minetest "vector" class does not seem to bother. My guess was that this can help with performance in functions that are frequently called (as it is not necessary to check if someone modified math.floor in the meantime). For the settings, which supposedly is only executed once and hence not performance critical, I would prefer the standard version. But I have to rely on your experience whether localizing is beneficial or not.
Member

I did a profile of this recently (local access compared to accessing a global) and accessing a local was about 3 times faster on my hardware than a simple global access (see #4338 (comment)). I haven't profiled it this specific thing, but I would expect accessing a field thru a global would be a bit less than an additional 2x. But even then, we are talking about 3.1 ns for a local access and an estimated 16.3 ns (2 * (9.7 - 3.1) + 3.1) for the non-local access so unless this is in an inner loop it probably doesn't matter that much. Defaulting to the faster method when they behave (or should behave) the same is a good habit to get into though.

Probably easy enough just to move the local variable assignment to near the top of the file and use it everywhere.

I did a profile of this recently (local access compared to accessing a global) and accessing a local was about 3 times faster on my hardware than a simple global access (see https://git.minetest.land/VoxeLibre/VoxeLibre/pulls/4338#issuecomment-80333). I haven't profiled it this specific thing, but I would expect accessing a field thru a global would be a bit less than an additional 2x. But even then, we are talking about 3.1 ns for a local access and an estimated 16.3 ns (2 * (9.7 - 3.1) + 3.1) for the non-local access so unless this is in an inner loop it probably doesn't matter that much. Defaulting to the faster method when they behave (or should behave) the same is a good habit to get into though. Probably easy enough just to move the local variable assignment to near the top of the file and use it everywhere.
rudzik8 marked this conversation as resolved
settingtypes.txt Outdated
@ -224,0 +224,4 @@
# Slime chunk density
mcl_mobs_slime_density (Slime chunk density, default is one in ten, 0 to disable cave slime) int 10 0
# Use 3d chunking instead of 2d chunking
mcl_mobs_slime_3d_chunks (Use 3d chunking instead of 2d chunking) bool false
Member

Reduce all of these settings to from mcl_mobs_slime_... to slime_... (and don't forget to update the .lua file for that). Explanation:

  1. Slimes have nothing to do with mcl_mobs. They are defined in mobs_mc.
  2. Wither-related settings (see L183-199) do the same thing with their namespace and it's good to be consistent.

Also, consider making the visible names of these settings a lot shorter and prepending all of them with "Slime". If the 3D/2D chunking is only relevant for slimes, it should be apparent for the player that it's only relevant for slimes.

image

The names get cut off, and it's even worse when you're using a custom font.

UPD: I think I know what the issue was. You've mixed up text in comment (which is the description for a setting) and in brackets (which is the visible name of a setting). Still, you don't have to repeat the same thing for slime_3d_chunks name and description if there's no more info to add.

Reduce all of these settings to from `mcl_mobs_slime_...` to `slime_...` (and don't forget to update the .lua file for that). Explanation: 1. Slimes have nothing to do with `mcl_mobs`. They are defined in `mobs_mc`. 2. Wither-related settings (see L183-199) do the same thing with their namespace and it's good to be consistent. Also, consider making the visible names of these settings a lot shorter and prepending all of them with "Slime". If the 3D/2D chunking is only relevant for slimes, it should be apparent for the player that it's only relevant for slimes. ![image](/attachments/c4bbb5b2-8a4f-461b-ad35-96ec3056dae5) The names get cut off, and it's even worse when you're using a custom font. UPD: I think I know what the issue was. You've mixed up text in comment (which is the description for a setting) and in brackets (which is the visible name of a setting). Still, you don't have to repeat the same thing for `slime_3d_chunks` name and description if there's no more info to add.
kno10 marked this conversation as resolved
kno10 force-pushed slime-spawning from 0edcfce8d4 to 9c759d4f6b 2024-06-27 12:54:06 +02:00 Compare
kno10 force-pushed slime-spawning from 9c759d4f6b to 2adf3a9904 2024-06-27 13:01:12 +02:00 Compare
Author
Member

@rudzik8, updated. Please let me know whether I should de-localize these functions.

@rudzik8, updated. Please let me know whether I should de-localize these functions.
Member

@rudzik8, updated. Please let me know whether I should de-localize these functions.

First:

image

Second, after reading through teknomunk's comment, I don't think you should.

> @rudzik8, updated. Please let me know whether I should de-localize these functions. First: ![image](/attachments/3c629fa2-73bc-4f9c-8cee-fc9f5e941961) Second, after reading through teknomunk's comment, I don't think you should.
4.4 KiB
rudzik8 added the
Testing / Retest
label 2024-06-27 16:10:30 +02:00

@rudzik8 you can use worldedit to rip out large areas underground and see if slimes are spawning there.

@rudzik8 you can use worldedit to rip out large areas underground and see if slimes are spawning there.
kno10 requested review from rudzik8 2024-07-01 22:57:31 +02:00
kno10 requested review from the-real-herowl 2024-07-01 22:57:54 +02:00
kno10 closed this pull request 2024-08-08 12:20:59 +02:00
Author
Member

@rudzik8 @the-real-herowl I updated the patch to use PcgRandom. This makes the code simpler.
Chunk density can now also be float, so a value of 12.34 is one in 12.34 blocks on average has slime.

@rudzik8 @the-real-herowl I updated the patch to use `PcgRandom`. This makes the code simpler. Chunk density can now also be float, so a value of 12.34 is one in 12.34 blocks on average has slime.
kno10 reopened this pull request 2024-08-08 12:46:01 +02:00
rudzik8 requested changes 2024-08-09 08:16:46 +02:00
@ -9,0 +5,4 @@
local SEED_OFFSET = 362 -- module specific seed
local world_seed = (minetest.get_mapgen_setting("seed") + SEED_OFFSET) % 4294967296
-- slime density, where default N=10 is every 10th chunk
local slime_density = tonumber(minetest.settings:get("slime_density")) or 10
Member

If increasing this value means decreasing slime density, then it should be called Slime chunk rarity.

If increasing this value means *de*creasing slime density, then it should be called **Slime chunk rarity**.
Author
Member

As this is a new control, what type of control would you prefer? I'm a probability guy, I would prefer 0.10 for 10% chance, but I figure that many people would prefer 10 for 10%, or 10 for "1 in 10" as used in other parts (note that these are opposite, 5% is not 1 in 5).

As this is a new control, what type of control would you prefer? I'm a probability guy, I would prefer 0.10 for 10% chance, but I figure that many people would prefer 10 for 10%, or 10 for "1 in 10" as used in other parts (note that these are opposite, 5% is not 1 in 5).
Member

Looking at the rest of the code, chance and rarity seems to be the name used to describe 1 in N probability. The minetest API uses chance, rarity or scarcity, each in different contexts, for the same thing.

Looking at the rest of the code, `chance` and `rarity` seems to be the name used to describe 1 in N probability. The minetest API uses `chance`, `rarity` or `scarcity`, each in different contexts, for the same thing.
kno10 marked this conversation as resolved
teknomunk requested changes 2024-08-09 13:52:44 +02:00
teknomunk left a comment
Member

Overall, it looks quite good to me, with just a couple of items. Testing shows that slime chunks are no longer along diagonals.

Overall, it looks quite good to me, with just a couple of items. Testing shows that slime chunks are no longer along diagonals.
@ -103,0 +24,4 @@
if slime_density == 1 then return true end -- slime everywhere
local bpos = vector.new(math_floor(pos.x / MAPBLOCK_SIZE), slime_3d_chunks and math.floor(pos.y / MAPBLOCK_SIZE) or 0, math_floor(pos.z / MAPBLOCK_SIZE))
local p = PcgRandom(minetest.hash_node_position(bpos) + world_seed):next(0,1e9)/1e9
return p * slime_density < 1
Member

Is there a difference between what you have here and return PcgRandom(...):next(0, slime_density - 1) == 0?

Is there a difference between what you have here and `return PcgRandom(...):next(0, slime_density - 1) == 0`?
Author
Member

support for floating point values.

support for floating point values.
teknomunk marked this conversation as resolved
@ -164,3 +80,2 @@
local swamp_light_max = 7
-- FIXME: redundant check?
Member

Looks like there are two separate checks in the mob spawning code, one in the spawning definition and one in the mob definition. This function overrides the default light level checks. I don't think we can remove this right now.

Looks like there are two separate checks in the mob spawning code, one in the spawning definition and one in the mob definition. This function overrides the default light level checks. I don't think we can remove this right now.
kno10 marked this conversation as resolved
kno10 force-pushed slime-spawning from c24039be71 to 4916c59e1d 2024-08-10 22:27:43 +02:00 Compare
kno10 requested review from teknomunk 2024-08-10 22:27:56 +02:00
kno10 requested review from rudzik8 2024-08-10 22:28:03 +02:00
the-real-herowl reviewed 2024-08-14 15:08:40 +02:00
the-real-herowl left a comment
Owner

Code LGTM. Haven't tested personally. Count this as half an approval.

Code LGTM. Haven't tested personally. Count this as half an approval.
rudzik8 requested changes 2024-08-15 04:30:33 +02:00
settingtypes.txt Outdated
@ -222,2 +222,4 @@
mcl_mobs_overworld_passive_threshold (Combined light threshold to stop animal and npc spawns in the Overworld) int 7 0 14
# Slime chunk density, default is one in ten, 0 to disable cave slime
slime_chance (Slime chunk chance) float 10.0 0.0
Member

Still not satisfied.

Oh, look at this fancy setting! I want a bigger chance of getting slimes!

sets from 10.0 to 100.0

Where are all the slimes? :(

Let's just settle on rarity. A control that is 1 in N is okay, it's just that it should be named appropriately.

Still not satisfied. > Oh, look at this fancy setting! I want a bigger chance of getting slimes! > *sets from 10.0 to 100.0* > Where are all the slimes? :( Let's just settle on *rarity*. A control that is 1 in N is okay, it's just that it should be named appropriately.
Author
Member

Maybe we should then really go for slime_chunk_percentage, and not 1:N, that name is likely least confusing?
MCLA has mcl_villages_village_chance which is also 1:N.
A possible name for the 1:N value would be slime_chunk_ratio?

Maybe we should then really go for slime_chunk_percentage, and not 1:N, that name is likely least confusing? MCLA has `mcl_villages_village_chance` which is also 1:N. A possible name for the 1:N value would be `slime_chunk_ratio`?
Member

Maybe, but ratio is not a common word and may not be understood as well as rarity.

Percentage is fine, even though we don't seem to be ever using that. That's just switching from 1:N to N:100.

Maybe, but *ratio* is not a common word and may not be understood as well as *rarity*. Percentage is fine, even though we don't seem to be ever using that. That's just switching from 1:N to N:100.
Author
Member

I am not sure if rarity is that common, and if people will have a good understanding of what semantics a "rarity" number gives.
In google books, ratio is much more common https://books.google.com/ngrams/graph?content=rarity%2C+ratio and ratios are widely used in sport bets, aren't they?

I am not sure if rarity is that common, and if people will have a good understanding of what semantics a "rarity" number gives. In google books, ratio is much more common <https://books.google.com/ngrams/graph?content=rarity%2C+ratio> and ratios are widely used in sport bets, aren't they?
rudzik8 marked this conversation as resolved
kno10 force-pushed slime-spawning from 4916c59e1d to 142ef3b41b 2024-08-15 12:54:46 +02:00 Compare
kno10 force-pushed slime-spawning from 142ef3b41b to f448495d53 2024-08-15 13:19:43 +02:00 Compare
teknomunk requested changes 2024-08-30 13:11:10 +02:00
teknomunk left a comment
Member

Two minor issues and a nitpick. I think we should be good to merge this after the two minor issues are addressed.

Two minor issues and a nitpick. I think we should be good to merge this after the two minor issues are addressed.
@ -102,1 +22,3 @@
return slime_chunk
if not pos then return end -- no position given
if slime_ratio == 0 then return end -- no slime chunks
if slime_ratio == 1 then return true end -- slime everywhere
Member

I think this should be if slime_ratio <= 1. The conditional below will always be true when slime_ratio between 0 and 1.

I think this should be `if slime_ratio <= 1`. The conditional below will always be true when `slime_ratio` between `0` and `1`.
kno10 marked this conversation as resolved
@ -103,0 +22,4 @@
if not pos then return end -- no position given
if slime_ratio == 0 then return end -- no slime chunks
if slime_ratio == 1 then return true end -- slime everywhere
local bpos = vector.new(math_floor(pos.x / MAPBLOCK_SIZE), slime_3d_chunks and math.floor(pos.y / MAPBLOCK_SIZE) or 0, math_floor(pos.z / MAPBLOCK_SIZE))
Member

Both math_floor and math.floor in this line.

Both `math_floor` and `math.floor` in this line.
kno10 marked this conversation as resolved
@ -559,3 +470,4 @@ mcl_mobs:non_spawn_specific("mobs_mc:magma_cube_big","overworld",0, minetest.LIG
mcl_mobs.register_egg("mobs_mc:slime_big", S("Slime"), "#52a03e", "#7ebf6d")
-- FIXME: add spawn eggs for small and tiny slimes and magma cubes
Member

Nitpick: I believe that for this project, we don't add an empty line at the end of the file, though, this does not appear in the code guidelines. @the-real-herowl, perhaps it should?

Nitpick: I believe that for this project, we don't add an empty line at the end of the file, though, this does not appear in [the code guidelines](https://git.minetest.land/VoxeLibre/VoxeLibre/src/branch/master/CONTRIBUTING.md#code-guidelines). @the-real-herowl, perhaps it should?

Maybe. Should deal with file ending problems. I think most files do have that. This is not an issue here though.

Maybe. Should deal with file ending problems. I think most files do have that. This is not an issue here though.
the-real-herowl marked this conversation as resolved
kno10 force-pushed slime-spawning from f448495d53 to 16f0d85be8 2024-08-30 19:55:56 +02:00 Compare
kno10 requested review from teknomunk 2024-08-30 19:56:04 +02:00
the-real-herowl refused to review 2024-09-09 13:21:20 +02:00
the-real-herowl requested review from the-real-herowl 2024-09-09 13:21:33 +02:00
the-real-herowl force-pushed slime-spawning from 16f0d85be8 to 8eb0433659 2024-09-09 19:39:48 +02:00 Compare
the-real-herowl approved these changes 2024-09-09 19:57:18 +02:00
the-real-herowl removed the
Testing / Retest
label 2024-09-09 19:57:25 +02:00
the-real-herowl added this to the 0.88.0 – Back on Track milestone 2024-09-09 19:57:30 +02:00
the-real-herowl merged commit 6c38823606 into master 2024-09-09 19:58:56 +02:00
kno10 deleted branch slime-spawning 2024-09-10 09:04:42 +02:00
Sign in to join this conversation.
No project
No Assignees
4 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: VoxeLibre/VoxeLibre#4466
No description provided.