Page 1 of 1

ABMs and mob spawning

PostPosted: Wed Feb 01, 2017 10:19
by taikedz
Hello all

I have been trying to figure out some mob spawning logic that takes into account the rate of fire of ABMs as determined by the Active Block Range... this post is a bit of a request for comment really, on whether these calculations seem right, and whether a better calculation, built-in to mobs libraries, would seem desirable/necessary...

Reasoning

A "map block" consists of 16 x 16 x 16 nodes.

If the active block range is set to `1` then only the immediate block in which the player is will run ABMs.

If the *abr* is set to `2`, everything within a radius of 2 blocks from the player will run their ABMs -- that is, a `(2 * 16) ^3 = 3276` number of blocks.

If the *abr* is set to `3` (the default), then that is raised to `(3 * 16) ^ 3 = 110592`

These numbers rack up pretty fast - a geometric progression copmounded by an exponential.

For mobs, if you consider spawning a bird, in the air, and the player is standing on the ground, then it is likely only half the space is filled with air. In the case of an *abr* set to 3, we have 110592 / 2 = 55296. This means that the chance of spawning a bird {when the ABM's main node is "air" and the neighbours is "air"} should be set to 55296 (or, (abr * 16)^3 / 2), if you want a bird to spawn anywhere a player is (1 in 1 chance); but if the ABR is changed to 1, the chance needs to be reduced to 4096.

Proposed heuristic calculation

In the code below, I define a function that can scale the chance, depending on the actual active_block_range, and an example of how it could be used ...

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all

local mt_abr = tonumber(minetest.setting_get("active_block_range") ) or 3 -- 3 is the default as described in minetest.conf.example


-- abr_chance() : calculate a chance based on the actual ABR
-- a raw chance of 4096 supplied to the ABM means an air mobs is likely to spawn once in range of player at ABR=1, but 2 times if ABR=2
-- So, we derive from that chance a new chance based on the currently set active_block_range
-- abr1chance is a chance based on ABR = 1 - if all mobs using this library specify on a basis of ABR=1

local function abr_chance(abr1chance)
        local normalizer = 4096 -- kludge heuristic value for a 1/1 chance at ABR=1
        local effective_abr_chance = math.pow( (mt_abr*16) , 3 )
        return math.floor( (abr1chance / normalizer) * effective_abr_chance )
end

-- start defining ABMs for mob spawns ... :

local chanceperblock = 1
local rangestyle = {
    air = 4096,
    land = 4096/2,
    constrainedarea = 4096/4
}

minetest.register_abm({
    ....
    chance = abr_chance(chanceperblock * rangestyle.land)
    ....
})


Any thoughts ?

Re: ABMs and mob spawning

PostPosted: Sat Feb 25, 2017 19:14
by burli
taikedz wrote:Any thoughts ?

I want to replace mob spawning with something different, because some ABM's go crazy like in this mod

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
 instrumentation                                         |    min µs |    max µs |    avg µs | min % | max % | avg %
-------------------------------------------------------- | --------- | --------- | --------- | ----- | ----- | ------
 zombies:                                                |         0 |   2838019 |      3640 |   0.0 |  99.1 |  43.3
  - ABM 'zombies:crawler spawning' ....................  |         4 |      7396 |       297 |   0.0 |   1.1 |   0.1
  - on_punch[1] .......................................  |       143 |       810 |       389 |  12.9 |  45.8 |  27.7
  - get_staticdata[1] .................................  |        68 |      2588 |       288 |   0.0 |  61.8 |   1.1
  - on_activate[4] ....................................  |        17 |       184 |        74 |   0.0 |   0.3 |   0.0
  - ABM 'zombies:normal spawning' .....................  |         3 |      2155 |       186 |   0.0 |   3.5 |   0.1
  - on_activate[2] ....................................  |        26 |       350 |       130 |   0.0 |   0.2 |   0.1
  - get_staticdata[3] .................................  |        68 |      4761 |       574 |   0.0 |  24.7 |   2.3
  - on_activate[1] ....................................  |        27 |       367 |       100 |   0.0 |   0.3 |   0.0
  - on_step[1] ........................................  |         6 |     35072 |       122 |   0.0 |  99.1 |  16.9
  - on_step[2] ........................................  |         4 |      1805 |       136 |   0.0 |  73.1 |  17.5
  - get_staticdata[4] .................................  |        62 |       657 |       283 |   0.0 |  23.7 |   3.1
  - on_activate[3] ....................................  |        60 |       432 |       170 |   0.0 |   0.1 |   0.0
  - get_staticdata[2] .................................  |        62 |       905 |       364 |   0.0 |  14.1 |   1.6
  - on_step[4] ........................................  |         4 |      3217 |       159 |   0.0 |  75.6 |  18.9
  - ABM 'zombies:zombie spawning' .....................  |        53 |   2836658 |    150375 |   0.0 |  96.0 |  26.5
  - ABM 'zombies:1arm spawning' .......................  |         3 |      2898 |       196 |   0.0 |   1.1 |   0.1

Re: ABMs and mob spawning

PostPosted: Sun Feb 26, 2017 10:41
by taikedz
Hmm.... table is missing headers, what mod is this, what ABR are you using...?

Re: ABMs and mob spawning

PostPosted: Sun Feb 26, 2017 11:01
by burli
Sorry, I add the header to the table. And it is the zombie mod with the crawling and one armed zombies. Don't remember the link