Re: String to Integer

From: Erwin S. Andreasen (erwin@PIP.DKNET.DK)
Date: 09/30/97


On Tue, 30 Sep 1997, Kareem Glover wrote:

> I was reading somewhere about how this mud was having their character
> skills based off the name they enter. (in you name lies your true
> power). I wanted to add this feature to my mud.  I need some ideas on
> how to convert a string to get a numeric value.

Here's a function that will turn a string into a value in the range of
0..max-1:

unsigned int get_hash( const char *key, unsigned int max )
{
    int len = strlen(key);
    int i = UMIN(len,32);
    unsigned hash = 0;

    while(i--)
    {
        hash = hash * 33U + LOWER(*key);
        key++;
    }

    return hash % max;
}

I use as a hash function for a lot of hash tables that store strings. It
originates from Larry Wall's perl.

The above function will only calculate the hash value on the first 32
characters: you can increase that if you want - I found 32 to be a good
value for one of the applications of this function, in a string manager,
SSM, that would use this function to detect duplicate strings and merge
them. That version was case sensitive BTW - this function isn't: "Drylock"
and "drylock" will return the same value.

Anyway, what you could do is to call get_hash on the name, with a max
value of say, 2 ^ 16. Then you will get value which has 16 bits, which are
pretty well scrambled depending on name, e.g. the difference between the
hash value of "Drylock" and "Drytock" will be quite great.

You could then based on the values of those 16 bits modify the stats, e.g.

if bit 0 is set: +1 str, -1 wis
if bit 1 is set: +1 int, -1 con

etc. You can make it balanced (giving each stat just as many possible
plusses and minuses) or not.


PS: I'm not certain LOWER is a MERC compatibility macro; you can use
tolower if your system has it (most should have nowadays :)

PPS: The MUD that uses a technique like this is Smaug. I don't know how
exactly they do it, but the source is available. ftp.game.org.

 =============================================================================
Erwin Andreasen   Herlev, Denmark <erwin@pip.dknet.dk>  UNIX System Programmer
<URL:http://pip.dknet.dk/~erwin/>         <*>           (not speaking for) DDE
 =============================================================================


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/08/00 PST