Page 2 of 2

Re: Community documentation

Posted: Mon Dec 22, 2014 2:01 pm
by ReFreezed
I added a new page listing the champion conditions and some info about them.

Re: Community documentation

Posted: Sat Jan 10, 2015 5:08 am
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.

Re: Community documentation

Posted: Sat Jan 10, 2015 6:56 am
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

Re: Community documentation

Posted: Sat Jan 10, 2015 6:08 pm
by Thorham
minmay wrote:MersenneTwister is used once in the original dungeon:
Why did they even bother to put that in there?

Re: Community documentation

Posted: Sat Jan 10, 2015 7:32 pm
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.

Re: Community documentation

Posted: Sat Jan 10, 2015 8:20 pm
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).

Re: Community documentation

Posted: Sat Jan 10, 2015 9:08 pm
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.