Page 1 of 2

How can I spawn a monster randomly?

Posted: Thu Sep 20, 2012 9:39 am
by Daght
I need to spawn the same monster type randomly, can some one help me with the script/solution?

Re: How can I spawn a monster randomly?

Posted: Thu Sep 20, 2012 9:41 am
by cromcrom
What kind of random, time or location ?

Re: How can I spawn a monster randomly?

Posted: Thu Sep 20, 2012 9:43 am
by Daght
cromcrom wrote:What kind of random, time or location ?
Time and location if possible

Re: How can I spawn a monster randomly?

Posted: Thu Sep 20, 2012 9:46 am
by cromcrom
try this for location:

Code: Select all

function spawnInRange(objectToSpawn,Xrange,Yrange)
local coordinateX = self.x
local coordinateY = self.y

local basecoordinateX = coordinateX-Xrange
local basecoordinateY = coordinateY-Yrange

local maxSpawnX = Xrange*2
local maxSpawnY = Yrange*2


local randomCoordinateX = math.random(0,maxSpawnX)
local randomCoordinateY = math.random(0,maxSpawnY)

local finalCoordinateX = basecoordinateX + randomCoordinateX
local finalCoordinateY = basecoordinateY + randomCoordinateY

spawn(objectToSpawn,1,finalCoordinateX,finalCoordinateY,0)
print(finalCoordinateX,finalCoordinateY)
end

function check()
spawnInRange("snail",3,3)
end
Tie it to a timer, choose the check() function
What it does is that it spawns a monster (objectToSpawn) on a random tile that is within a + or - range (Xrange, Yrange) of the tile this script_entity is located at. Beware of the spawn level.
I am sure it is easy to improve it.

As for time, I have not checked this issue yet, but out of my mind, you could set a percentage chance that this script fires every "timer second", thus making the trick (example, timer seconds = 10, if percentage = 50%, the monster might or might not spawn every 10 seconds. You could reduce the percentage for less spawning chance. Well, you got the figure...). I must go to work now, I will check this stuff in a while,

Re: How can I spawn a monster randomly?

Posted: Thu Sep 20, 2012 9:53 am
by Daght
cromcrom wrote:try this for location:

Code: Select all

function spawnInRange(objectToSpawn,Xrange,Yrange)
local coordinateX = self.x
local coordinateY = self.y

local basecoordinateX = coordinateX-Xrange
local basecoordinateY = coordinateY-Yrange

local maxSpawnX = Xrange*2
local maxSpawnY = Yrange*2


local randomCoordinateX = math.random(0,maxSpawnX)
local randomCoordinateY = math.random(0,maxSpawnY)

local finalCoordinateX = basecoordinateX + randomCoordinateX
local finalCoordinateY = basecoordinateY + randomCoordinateY

spawn(objectToSpawn,1,finalCoordinateX,finalCoordinateY,0)
print(finalCoordinateX,finalCoordinateY)
end

function check()
spawnInRange("snail",3,3)
end
Tie it to a timer, choose the check() function
What it does is that it spawns a monster (objectToSpawn) on a random tile that is within a + or - range (Xrange, Yrange) of the tile this script_entity is located at. Beware of the spawn level.
I am sure it is easy to improve it.

As for time, I have not checked this issue yet, but out of my mind, you could set a percentage chance that this script fires every "timer second", thus making the trick (example, timer seconds = 10, if percentage = 50%, the monster might or might not spawn every 10 seconds. You could reduce the percentage for less spawning chance. Well, you got the figure...). I must go to work now, I will check this stuff in a while,
This seems hard to me... seems simple, but i cannot figure out it. Can i send you a PM with the specifics? And maybe mailing you the dungeon with the description of the level?

Re: How can I spawn a monster randomly?

Posted: Thu Sep 20, 2012 10:20 am
by cromcrom
Ask your questions, I might or might not have the answer, and it might benefit others :-)

Re: How can I spawn a monster randomly?

Posted: Thu Sep 20, 2012 10:26 am
by antti
I had some random spawning in my mod. You can load the source files here: http://www.grimrock.net/extras/rotten_place_source.zip

I basically went through all the spawner-entities (with allEntities-iterator) in the level and used a random chance to determine with each of them if they are activated.

Re: How can I spawn a monster randomly?

Posted: Thu Sep 20, 2012 10:48 am
by cromcrom
It seems a very galant way to do this random spawn indeed :-)

I post the modified function anyways, if it might help:

Code: Select all

function spawnInRandomRangeChanceTime(objectToSpawn,Xrange,Yrange,ChanceTime)
local coordinateX = self.x
local coordinateY = self.y

local basecoordinateX = coordinateX-Xrange
local basecoordinateY = coordinateY-Yrange

local maxSpawnX = Xrange*2
local maxSpawnY = Yrange*2


local randomCoordinateX = math.random(0,maxSpawnX)
local randomCoordinateY = math.random(0,maxSpawnY)

local finalCoordinateX = basecoordinateX + randomCoordinateX
local finalCoordinateY = basecoordinateY + randomCoordinateY

local chancetospawn = math.random(1,100)

	if chancetospawn < ChanceTime then
	spawn(objectToSpawn,1,finalCoordinateX,finalCoordinateY,0)
	--print(finalCoordinateX,finalCoordinateY)
	end
end

function check()
spawnInRandomRangeChanceTime("snail",1,1,15)
end
How to use:
1- copy paste this code in a script_entity on the level to check.
2- in the check function, change the spawnInRandomRangeChanceTime variables to whatever you want.
3- add a timer, set it to "XX_seconds", and link it to the script_entity, choose the "check" function

In the example above, it will have 15% chance to spawn a snail within 1 tile of the script_entity every "XX_seconds"

Re: How can I spawn a monster randomly?

Posted: Thu Sep 20, 2012 12:38 pm
by Komag
how to avoid random spawning inside the walls?

Re: How can I spawn a monster randomly?

Posted: Thu Sep 20, 2012 12:43 pm
by Darklord
Komag wrote:how to avoid random spawning inside the walls?
Oooooh Ooooooh!!! Setup!!! :D

They're coming outta the goddamn walls! :lol:

Daniel.