Jump to content
Sign in to follow this  
Time

[Paying] Simple Character Generator Needed

Recommended Posts

Hey guys, I need a character name and class generator for a project I'm working on. The program I need is separate from the project, so it doesn't need to be implemented into any of my code. All I need is for the program to be easy to run, and easy to edit. All of the names, locations, etc. can be done on my own, but I do need help just making the random generator. It shouldn't be hard, and I'm willing to compensate you for your work.



I need a basic generator that rolls a random name, class, location, and skill level.

For the name generating, I'll have a few different columns of names, A - D.
The name generator should work like this:
[code]
Rolls random number, 1 - 4. 1 = A, 2 = B, 3 = C, 4 = D
Number rolled was 2
Re-roll, selecting a random number from all of the first names in column B
Re-roll, selecting a random number from all of the last names in column B
print name
[/code]
Next, it should roll a random class, 1 - 21, with a name assigned to each class.
Then a random location, 1 - 51, with a name assigned to each location.
Last it should roll a skill level, 80 - 95.

Share this post


Link to post
Share on other sites
I wrote something simple in C:

[code]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUF_LEN 32
#define LINE_SIZE 128
#define ATTRIBUTES_FILE "attrs.txt"
#define SKILLS_FILE "skills.txt"

static int betterRand(int lim);
static int loadAttributes();
static void loadAttribute(char *attrfile, char *value, size_t len);
static void loadSkills();

struct attribute
{
char name[BUF_LEN];
char value[BUF_LEN];
};

struct skill
{
char name[BUF_LEN];
unsigned char level;
};

struct attribute *attributes;
struct skill *skills;

static int betterRand(int lim)
{
int div = RAND_MAX / (lim + 1);
int ret;

do
{
ret = rand() / div;
} while(ret > lim);

return ret;
}

static int loadAttributes()
{
FILE *file = fopen(ATTRIBUTES_FILE, "r");
char line[LINE_SIZE];
if (fgets(line, LINE_SIZE, file))
{
int attrscount = atoi(line);
int i = 0;

attributes = malloc(sizeof(struct attribute) * attrscount);

while (i < attrscount && fgets(line, LINE_SIZE, file))
{
char *attrname = strtok(line, " ");
char *attrfile = strtok(NULL, " \n");
memcpy(attributes[i].name, attrname, strlen(attrname));

loadAttribute(attrfile, attributes[i].value, BUF_LEN);

printf("%s %s\n", attrname, attributes[i].value);

i++;
}
}
}

static void loadAttribute(char *attrfile, char *value, size_t len)
{
FILE *file = fopen(attrfile, "r");

if (file)
{
char line[LINE_SIZE];

if (fgets(line, LINE_SIZE, file))
{
int count = atoi(line);
int index = betterRand(count - 1);
int i = 0;

for (; i < index + 1; i++)
{
fgets(line, LINE_SIZE, file);
}

line[strlen(line) - 1] = '\0';

memcpy(value, line, len);
}
}
else
{
printf("File %s doesn't exist.\n", attrfile);
}
}

static void loadSkills()
{
FILE *file = fopen(SKILLS_FILE, "r");
char line[LINE_SIZE];

if (file)
{
if (fgets(line, LINE_SIZE, file))
{
int skillscount = atoi(strtok(line, " "));
int skillmax = atoi(strtok(NULL, " \n"));
int i = 0;

skills = malloc(sizeof(struct skill) * skillscount);

while (i < skillscount && fgets(line, LINE_SIZE, file))
{
line[strlen(line) - 1] = '\0';
char *skillname = line;

memcpy(skills[i].name, skillname, BUF_LEN);
skills[i].level = betterRand(skillmax - 1) + 1;
printf("%s %d\n", skills[i].name, skills[i].level);
i++;
}
}
}
}

int main(void)
{
srand((unsigned int) time(NULL));
loadAttributes();
loadSkills();
return EXIT_SUCCESS;
}
[/code]

Store your data in these files:

attrs.txt:
[code]
number of attributes
attribute_name[space]attribute_options_file
...
[/code]

Example:
[code]
number of attributes
class classes.txt
name names.txt
...
[/code]

where each individual attribute file is in the format:
[code]
number of options
option1
option2
...
[/code]

And a skills.txt:
[code]
Number of skills[space]Max value of skills
Skillname1
skillname2
...
[/code]

Example output:
[code]
firstname bob
lastname brown
clas fat
strength 5
defense 1
magic 4
hitpoints 7
accuracy 10
[/code]

If you need anything changed or help, message me. Edited by Trey

Share this post


Link to post
Share on other sites
[MENTION=153]Trey[/MENTION]

Only 1 request and 1 question.

I've got a few different types of races, so I want to use a multitude of names. So I need it to randomly pick a race, then pick a name from that category. So it should roll for say Race A, then it should pick a name from Race A. I need it to work for both first and last names.

The only question is how to compile and run a C file. I've never worked with it before

Share this post


Link to post
Share on other sites
[quote name='Time'][MENTION=153]Trey[/MENTION]

Only 1 request and 1 question.

I've got a few different types of races, so I want to use a multitude of names. So I need it to randomly pick a race, then pick a name from that category. So it should roll for say Race A, then it should pick a name from Race A. I need it to work for both first and last names.

The only question is how to compile and run a C file. I've never worked with it before[/QUOTE]

I can try and add loading attributes dependent on the value of another attribute. If you're in *nix (unix/linux) you simply need to use gcc:
[code]
gcc chargen.c -o chargen
[/code]

The same is true of Windows, but you likely wont have GCC installed. If it would be more useful I can rewrite this in another language, just went with one that had easy file IO.

Share this post


Link to post
Share on other sites
[quote name='Trey']I can try and add loading attributes dependent on the value of another attribute.
The same is true of Windows, but you likely wont have GCC installed. If it would be more useful I can rewrite this in another language, just went with one that had easy file IO.[/QUOTE]
[MENTION=153]Trey[/MENTION]

If you could add loading attributes dependent on the value of another attribute, and maybe write it in another language, that'd be awesome. Thanks a ton man. Edited by Time

Share this post


Link to post
Share on other sites
I'm still to this day impressed at your ability to work with C Trey, lol.

Share this post


Link to post
Share on other sites
I can try making something in Python, you would have to have Python 2.7 installed though.

Share this post


Link to post
Share on other sites
[MENTION=165055]GreyMan[/MENTION], thank you sir, you're quite an impressive programmer yourself.

Sorry I haven't gotten back on this yet, I started something but realized that resolving dependencies would be a little more complicated than expected. You essentially have to randomize the attributes that aren't dependent first, then continously loop through and determine which attributes have all of their dependencies determined and then use the dependencies to randomize a value. However, there are situations where dependencies can't be resolved (such as A -> B, B -> C, C -> A, or even A -> B, B -> A).

The best way to represent this is as a graph/tree structure with each node as the attribute. Each node is then a subtree. If a graph contains a cycle, then there are dependencies that can't be met/resolved (user error). Otherwise, you simply recurse and for each leaf node you randomize a value for it (leaves have no children, i.e. no dependencies), and give this value to the parent node and remove the leaf. You do this for each attribute, until all attributes trees/graphs are leafs and have a value. If for a given attribute, there are no leaves, then there is a cycle and you inform the user that this attribute's dependencies are incorrect (as in can't be met). You can be sure there are no leaves for a subtree if you recurse through each nodes child and end up back at the root for the subtree (the initial attribute you starting recursing from): keep a set of nodes you've hit while recursing, and if the current node is in this list (check before you add, obviously), then you've hit a cycle. Clear the set for each attribute you use as the root node.


Hopefully this makes sense. I'm out of it right now, so I may have over complicated things. If I run into enough time soon I'll write something up. I'm sure this can be done is shorter, but probably uglier/less extensible, fashions, but this was the first algorithm that came to mind. Edited by Trey

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  

×