hey.
im working on a sort of similar project to this, a fantasy-type rpg thing, and one of the things i did was create a program that let me quickly generate random npcs: random race, looks, class, stats, etc. as i was trying to debug it tonight and finally got it working right, i realized that it might be useful to you. the code isnt very clean, but the idea is. i have no illusions that this is anything but the work of a hack, but its effective, useful, and easily expanded. essentially, it works like this:
see attached - int generateVVV(int i)
loopCount, sizeCount, and copyArray are all fairly simple functions. loopCount adds all of the numbers in an array, generates a number from 1 to X (where X is the sum of all the elements in the array) and then goes back to the beginning of the array and starts subtracting each element from the random number. when it finds an element in the array that is greater than or equal to it's (depleting) random number, it returns the index of that element, giving a weighted random response. its a bit easier to understand when looking at the arrays.
//example loopCount arrays
//0 = warrior
//1 = wizard
//2 = thief
//3 = priest
int humanClasses[] = { 1, 1, 1, 1 };
int orcClasses[] = { 2, 0, 1, 1 };
int elfClasses[] = {0, 1, 1, 0 };
int classCount = 4;
int classUsesLoop = true;
so, loopcount would have a 1 in 4 chance to return a given index from the human classes, a 50% chance to return the 'warrior' index for orcs, and a 0% chance to return 'warrior' for elves.
sizeCount works a bit differently. it was made for generating ranges of numbers. so, for instance:
//example sizeCount arrays
int humanStrength[] = { 8, 12, 50, 4};
int orcStrength[] = { 10, 15, 50, 4 };
int elfStrength[] = { 8, 12, 25, 3 };
int strengthCount = 4;
int strengthUsesLoop = false;
first, it generates a random number between array[0] and array[1]. then, there is an array[2]% chance that it gets an extra array[3] bonus. so in this example, humans would have 8-12 strength, and a 50% chance for 4 extra, orcs would have 10-15 with a 50% chance for 4 bonus, and elves would have 8-12 with a 25% chance for 3 bonus. code for loopCount and sizeCount are at the end of the post.
i think there are four great things about doing things this way.
1. easily expanded
expanding the random element generation through this method is as easy as populating a data array, using find/replace to finish the template, and then copying everything into the code. it doesnt require a lot of hunting, which saves time.
2. easily altered
if you decide later on (for balance issues or otherwise) that you want to change something, lets say giving elves access to priests as a class, you dont have to hunt through ten spots in the character generation code to change things. its all stored in one location.
3. moddable
by allowing the data arrays to be read into memory from a file i/o that reads them from a text or xml file or the like, you allow players to effectively create their own custom files for the game, making modding easily accessible to the community. while this isnt necessary for these functions to work, it does make it much easier to work in later.
4. versatility
this isnt a set of functions that only works when creating npcs, it works with any random element. when a monster drops an item, you can use a weighted array to determine what class of item they drop (weapon, armor, supplies), then randomly define that item (sword, axe, mace, bow), and then randomly create properties to add to the item (rusty, mundane, superior, magical), even to fetch what types of random monsters will appear in a given area, which itself was also randomly generated with this method.
http://www.mediafire.com/?3iii1cmhg4v35or (code.txt - 8kb)
oh, right, and copyArray. copy array copies one array to another. i suck with c++ and didnt know how to pointers when i wrote all this, so i hacked together the hackiest hack solution i could and just made a function that copies array B into array A. i think the only thing ive ever programmed that was dumber than that was when i was first learning file i/o in java, and because of an endless loop, accidentally filled a flash drive with 64mb .txt file consisting of nothing but the number 0.