Community documentation

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
User avatar
ReFreezed
Posts: 8
Joined: Tue Nov 25, 2014 7:10 pm
Contact:

Re: Community documentation

Post by ReFreezed »

I added a new page listing the champion conditions and some info about them.
Batty
Posts: 509
Joined: Sun Apr 15, 2012 7:04 pm

Re: Community documentation

Post by Batty »

I made an entry for MersenneTwister:
  • MersenneTwister.create(seed)
    Returns an instance of MersenneTwister required to generate random numbers. Seed is a number.
    e.g. local random = MersenneTwister.create(Time.systemTime())
  • MersenneTwister.random()
    Generates a random real number between 0 and 1.
    e.g. local n = random:random()
  • MersenneTwister.randomInt(min, max)
    Generates a random integer between the given numbers min and max.
    e.g. local n = random:randomInt(1, 10) produces a random integer from 1 to 10
    e.g. MersenneTwister.randomInt(MersenneTwister.create(Time.systemTime()), 1, 10) will also return a random integer from 1 to 10
I searched the asset scripts and MersenneTwister doesn't appear. I looked in a few of the 115 script entities in the main game and saw only math.random() so I don't think it was ever used. But, if someone has made a dungeon.lua of the main game they could search for it because I'm curious as to when it may have been used.
Last edited by Batty on Sun Jan 11, 2015 2:34 am, edited 1 time in total.
minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Community documentation

Post by minmay »

MersenneTwister is used once in the original dungeon:

Code: Select all

function initMimics()
	local skipLevels = { 
		31,	-- shipwreck beach
		32, -- dead sailor's cave
		19,	-- twigroot forest
		20, -- twigroot tunnels
		21, -- twigroot basement
		22,	-- forgotten river
	}

	-- find all chests except in first three levels
	local chests = {}
	for i=1,Dungeon.getMaxLevels() do
		if not table.contains(skipLevels, i) then
			for e in Dungeon.getMap(i):allEntities() do
				if e.chest then
					chests[#chests+1] = e.chest
				end
			end
		end
	end
	--print(#chests.." found")
	
	-- place random mimics
	local count = 0
	local random = MersenneTwister.create(Time.systemTime())
	for i=1,8 do
		local n = random:randomInt(1, #chests)
		local chest = chests[n]
		if chest then
			--print(chest.go.x, chest.go.y, chest.go.level)
			chest:setMimic(true)
			table.remove(chests, n)
			count = count + 1
		end
	end
	--print(count.." mimics placed")
end
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
User avatar
Thorham
Posts: 80
Joined: Sat May 04, 2013 5:12 pm

Re: Community documentation

Post by Thorham »

minmay wrote:MersenneTwister is used once in the original dungeon:
Why did they even bother to put that in there?
Batty
Posts: 509
Joined: Sun Apr 15, 2012 7:04 pm

Re: Community documentation

Post by Batty »

Thorham wrote:Why did they even bother to put that in there?
AFAIK MersenneTwister should produce a more random random number than math.random() does, i.e. closer to being truly random.
minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Community documentation

Post by minmay »

From a modding perspective the main advantage of MersenneTwister over calling math.random() is that you can control the state. As far as I know, you cannot create your own instance of the generator used for math.random and its internal state can change in various ways you can't control because Grimrock calls it internally (just try saving/loading a game). An instance of MersenneTwister will only advance when you advance it. So a player saving/loading won't change the next numbers to be generated. This could be important if you're, say, generating a random level on the fly, and don't want the player to be able to savescum to get different levels.
The quality of the random numbers is probably better as well, but in most modding applications this is not noticeable (and Mersenne Twister is relatively easy for an end user to predict the state of, so if that's an issue you'll need a more complicated PRNG than either one anyway).
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
User avatar
Thorham
Posts: 80
Joined: Sat May 04, 2013 5:12 pm

Re: Community documentation

Post by Thorham »

minmay wrote:From a modding perspective the main advantage of MersenneTwister over calling math.random() is that you can control the state.
Good point.
Post Reply