I'm working on a video game written in C++, and I'm trying to create monster spawns based on a given radius, let's say I define a position like this:
Position ( X, Y, Z ) Amount ( Value )
What that code does is the start point is Position, and I want to place (Amount) monsters around the start point (X = X-Amount, Y = Y-Amount, X = X+Amount, Y = Y+Amount), there's a maximum of 9 monsters per spawn.
The code I'm using right now is pretty noobish:
// 1000 tries because I'm using random numbers
for (int i = 0; i < 1000; i++)
{
toPlace = centerPos;
toPlace.x = uniform_random(centerPos.x-monsterAmount, centerPos.x+monsterAmount);
toPlace.y = uniform_random(centerPos.y-monsterAmount, centerPos.y+monsterAmount);
if (Monster->CanSpawnAt(toPlace))
{
Monster->Spawn();
break;
}
}
Position calculation is using monsterAmount
(amount of monsters defined by user), so if there are 3 monsters, then it's -3 behind and +3 positions in front.
It's bad because I'm using a random value, hence why I'm inside a for loop of 1000, because sometimes the monster can't spawn on the given position.
Also sometimes monsters spawn next to each other, and I really don't want that. I'm wondering if you guys could help me by telling me what is the mathematical algorithm I should use for this type of task?