[Solved] use map checksum to seed some random spawning

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
Post Reply
User avatar
Nash
 
 
Posts: 17434
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

[Solved] use map checksum to seed some random spawning

Post by Nash »

I have no idea how to do this. Can someone teach me how to use the map's checksum to seed some predictable random spawning? Say I want to scatter a random bunch of trees all over the map but it uses the map checksum as a seed so that the randomness is, uh, consistent (until the map is altered, I understand).
Last edited by Nash on Thu Feb 22, 2018 1:09 am, edited 1 time in total.
_mental_
 
 
Posts: 3812
Joined: Sun Aug 07, 2011 4:32 am

Re: [Request] use map checksum to seed some random spawning

Post by _mental_ »

If the problem is only in setting the seed then it can be done like this

Code: Select all

string checksum = level.GetChecksum();
int length = checksum.Length(); // should be always 32 for MD5 checksum
int seed = 0;

for (int i = 0; i < length; ++i)
{
	seed ^= checksum.CharCodeAt(i) << 8 * (i % 4);
}

SetRandomSeed[my_random_sequence](seed);

for (int i = 0; i < 10; ++i)
{
	double x = frandom[my_random_sequence](0, 1000.);
	double y = frandom[my_random_sequence](0, 1000.);
	console.printf("%f, %f", x, y);
}
Of course you can code more sophisticated string to 32-bit number conversion, for example CRC32.
User avatar
Nash
 
 
Posts: 17434
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Re: [Request] use map checksum to seed some random spawning

Post by Nash »

Thank you, this solution works perfectly for the purposes of randomly spawning things based on map checksum.

Now another question: will subsequent random/frandom calls after SetRandomSeed be seeded? I only want SetRandomSeed to affect this one function, and then I want every random/frandom call outside of that to revert to GZDoom's usual, unrpredictable randomness. Do I have to do anything special to achieve this?
_mental_
 
 
Posts: 3812
Joined: Sun Aug 07, 2011 4:32 am

Re: [Request] use map checksum to seed some random spawning

Post by _mental_ »

Only numbers generated with my_random_sequence identifier are affected by seed value. So you need to make sure that name of random sequence identifier is unique.
User avatar
Nash
 
 
Posts: 17434
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Re: [Solved] use map checksum to seed some random spawning

Post by Nash »

Alright. Thanks _mental_ for simple and fast solution, :D
Post Reply

Return to “Scripting”