[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.

[Solved] use map checksum to seed some random spawning

Postby Nash » Sun Feb 18, 2018 5:27 pm

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.
User avatar
Nash
Twitter/Facebook/Youtube: nashmuhandes
 
 
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia
Twitch ID: nashmuhandes
Github ID: nashmuhandes

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

Postby _mental_ » Mon Feb 19, 2018 3:10 am

If the problem is only in setting the seed then it can be done like this
Code: Select allExpand view
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.
_mental_
 
 
 
Joined: 07 Aug 2011

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

Postby Nash » Wed Feb 21, 2018 3:47 pm

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?
User avatar
Nash
Twitter/Facebook/Youtube: nashmuhandes
 
 
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia
Twitch ID: nashmuhandes
Github ID: nashmuhandes

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

Postby _mental_ » Thu Feb 22, 2018 12:21 am

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.
_mental_
 
 
 
Joined: 07 Aug 2011

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

Postby Nash » Thu Feb 22, 2018 1:09 am

Alright. Thanks _mental_ for simple and fast solution, :D
User avatar
Nash
Twitter/Facebook/Youtube: nashmuhandes
 
 
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia
Twitch ID: nashmuhandes
Github ID: nashmuhandes


Return to Scripting

Who is online

Users browsing this forum: No registered users and 0 guests