Spawn mob in Radius

Discussion in 'Spigot Plugin Development' started by joblo.2213, May 29, 2018.

  1. So I'm looking for the best way to spawn multiple mobs randomly inside a radius.

    I wan't it to spawn the mobs on ground and prevent mobs spawning inside blocks.
    Also it has to be lag efficient even for large radiuses and inside caves where most blocks are sollid.

    I'd like to know how you would aproach it as I have different ideas which all have some floors.
     
  2. The way I would do this is get a random location within the radius and check if the block below is solid, if not get a new location, if so spawn a random mob. There are definitely was to make this method more efficient.
     
    • Agree Agree x 1
    • Useful Useful x 1
  3. x/y/z = randomFloat(-radius, radius)
    I guess that you can just use a while until you find a location in ground
    or if you're in cave area, find random x/z, and decrease y until finding a good pos ?
     
    • Useful Useful x 1
  4. That is also what I already was thinking about:
    1. Get a random location in 3D (random x,y and z). Check if it is a vallid spawn space. If not retry.
    If more than x retrys have been done spawn the mob at original location to prevent endless loop.

    2.Get a random location in 2D (random x and z, y is the same as the original location).
    Check if it is valid spawn space.
    If not try one block higher and one block lower.
    If y exceeds max hight / 0 or radius spawn at original location.

    I could also imagine a mix of both.
    Wonder which works better.
    Or are there any other ideas on this?
     
    • Agree Agree x 1
  5. Strahan

    Benefactor

    I read that "original location" as meaning the original random location. If that is what you meant, that isn't required to prevent an endless loop as you can just break the loop any time you wish by checking iterations. The original location which was discarded as being unsuitable would still be unsuitable no matter how many other places you check.
     
    • Agree Agree x 1
  6. I meant the center which was originaly given, around which the mobs should be spawned.

    To clearify this:
    A instruction could be "location:x,y,z,world radius:10" if no radius is given it spawns the mobs all at the same place (which I call original location), if it is given it spreads them randomly arround this location.

    Hope it's now clearer.
     
  7. Strahan

    Benefactor

    Yea, that would make more sense.
     
    • Agree Agree x 1