Sign in to follow this  
Toolmaker

Procedurally generating objects... Ideas?

Recommended Posts

I'm currently in the process of coming up with a way to quickly create objects based on a seed. The idea is simple: I have various 'items', each of which have different properties, and of which the properties may or may not exist based on the type of item. I need to store these items in an XML file of the user who owns the items in a very quickly and most importantly simple fashion. Initially I thought about just marking certain fields in the item templates are serializable, but there is a simpler solution: Generate items based on a seed. What I want to do in the item template is define stats as 'ranges' like this: <stat name="Damage" min="10" max="15" /> <stat name="Defense" value="0" /> During item spawning, I could take a seed and template then calculate the range values and set their values, so that my scripts can easy find them by calling item.GetData("stats.damage", -1). However, I have no idea on how to implement such a procedural system. I'm specifically looking for a system that's easy to work out, as spawning items should be relatively fast. toolmaker

Share this post


Link to post
Share on other sites
You just need a pseudo-random number generator. When you want to generate your object, you initialize it with the seed and when you need a new number in some range, you get it from the pseudo-random number generator.

Here's a simple pseudo-random number generator that's probably good enough for what you want:
class PseudoRandomGenerator {
unsigned seed;

public:
PseudoRandomGenerator(unsigned seed) : seed(seed) {
}

int generate_in_range(int begin, int end) {
seed ^= (seed<<19) + (seed>>1) + 1033594897;
return begin + seed%(end-begin);
}
};

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this