Procedurally generating objects... Ideas?

Started by
0 comments, last by alvaro 14 years, 10 months ago
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

Advertisement
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);  }};

This topic is closed to new replies.

Advertisement