How can I spawn a monster randomly?

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
User avatar
Daght
Posts: 146
Joined: Sun Apr 15, 2012 12:28 pm
Location: Italy

How can I spawn a monster randomly?

Post by Daght »

I need to spawn the same monster type randomly, can some one help me with the script/solution?
If we do not end the war, the war will end us.
User avatar
cromcrom
Posts: 549
Joined: Tue Sep 11, 2012 7:16 am
Location: Chateauroux in a socialist s#!$*&% formerly known as "France"

Re: How can I spawn a monster randomly?

Post by cromcrom »

What kind of random, time or location ?
A trip of a thousand leagues starts with a step.
User avatar
Daght
Posts: 146
Joined: Sun Apr 15, 2012 12:28 pm
Location: Italy

Re: How can I spawn a monster randomly?

Post by Daght »

cromcrom wrote:What kind of random, time or location ?
Time and location if possible
If we do not end the war, the war will end us.
User avatar
cromcrom
Posts: 549
Joined: Tue Sep 11, 2012 7:16 am
Location: Chateauroux in a socialist s#!$*&% formerly known as "France"

Re: How can I spawn a monster randomly?

Post 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,
A trip of a thousand leagues starts with a step.
User avatar
Daght
Posts: 146
Joined: Sun Apr 15, 2012 12:28 pm
Location: Italy

Re: How can I spawn a monster randomly?

Post 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?
If we do not end the war, the war will end us.
User avatar
cromcrom
Posts: 549
Joined: Tue Sep 11, 2012 7:16 am
Location: Chateauroux in a socialist s#!$*&% formerly known as "France"

Re: How can I spawn a monster randomly?

Post by cromcrom »

Ask your questions, I might or might not have the answer, and it might benefit others :-)
A trip of a thousand leagues starts with a step.
User avatar
antti
Posts: 688
Joined: Thu Feb 23, 2012 1:43 pm
Location: Espoo, Finland
Contact:

Re: How can I spawn a monster randomly?

Post 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.
Steven Seagal of gaming industry
User avatar
cromcrom
Posts: 549
Joined: Tue Sep 11, 2012 7:16 am
Location: Chateauroux in a socialist s#!$*&% formerly known as "France"

Re: How can I spawn a monster randomly?

Post 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"
A trip of a thousand leagues starts with a step.
User avatar
Komag
Posts: 3659
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: How can I spawn a monster randomly?

Post by Komag »

how to avoid random spawning inside the walls?
Finished Dungeons - complete mods to play
User avatar
Darklord
Posts: 2001
Joined: Sat Mar 03, 2012 12:44 pm
Location: England

Re: How can I spawn a monster randomly?

Post 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.
A gently fried snail slice is absolutely delicious with a pat of butter...
Post Reply