How It Works

Initial Design

Like I said, the idea for this literally came to me in a dream. I dreamed that I used Outlaws of the Marsh as the basis for creating badass titles for my boss monsters in my game. It's a good source of inspiration, with such titles as: Sagacious Lu the Tattooed Monk, Fan Rui the Demon King who Roiled the World, and Li Kui the Black Whirlwind. If I could make my code capable of generating titles in the general style of the book, then there would be a clear indicator which of the monsters were the badasses. So, I used the following three sites, containing differently translated versions of their titles to create title formats, and a title word list:

Oh, I also happened to be reading another Chinese novel while working on this, and used a couple titles from it as well. There were only about 8 or so though.

XML for Title Formats

Originally my plan was to use XML for everything, but I soon discovered that wasn't going to work out. XML did work out rather well for one thing, that being creating a sort of grammar for the titles. Let's take a look at the first one in the list:

<!--   Shi Jin the 9 Dragons -->
<title>
  <name />
  <the />
  <number>
    <creature />
  </number>
</title>

We can see it's based on "Shi Jin the 9 Dragons", and the format is readily apparent in the XML. The creature element is contained within the number element so that depending on the procedurally generated number, it will turn the creature element plural where appropriate. Using formats from the three above sources, I've got 169 (13 squared!) different formats supported at the moment. Adding additional formats can be done entirely through XML, without ever touching the actual code (so long as the new formats don't require new, unsupported types of terms).

Overall Program Flow

It quickly became apparent that the terms weren't going to work out in XML format. I was going to need to be able to search for the same term multiple different ways, which would have necessitated doubling (at least) the entries for each term. That's pretty unacceptable. So I was going to have to re-learn databases. Now Actionscript is a client side scripting language, and thus, even if it can be made to access a database directly, it's kind of a bad idea, due to security concerns and so forth. Which meant I was going to need a server side scripting language, and most of what I was seeing online was recommending PHP for this particular task. So I was going to have to learn that too (starting to see why this became more than a weekend project, aren't you?). So, here's how this bit of code actually operates:

  1. Actionscript code accesses XML file.
  2. Actionscript code processes the XML, chooses a title format
  3. Actionscript code sends a request to php code for a type of term
  4. php code translates that request into a sql query to a database for the appropriate type of term
  5. The database searches through its tables to find a term, and sends it back to the php code
  6. The php code unpacks the database result and sends it back to the actionscript code
  7. The actionscript potentially does some post-processing on the term, and then inserts it into the title
  8. Steps 3-7 repeat as necessary for the given title format

Whew!

Database Structure

I remember when I was in college, I kind of hated the idea of ever working with databases again. It was the wobbly plate that semester, while I focused on "more important" classes. Considering it's been a few years, and I was hardly a master to begin with, the database design for this little project was pretty rough. In fact, I completely scrapped it and started over twice. Big thanks to my brother in law, Jason, because without his help, it would not have ended up in as good a place as it is now.

I have one main table, that contains every word in my database. This table contains only the id of the word, and the definition (i.e., the actual word). I then have a bunch (currently 46) of other tables, with a "wordID" column, as a foreign key to the id in the wordstable. So, if I want to get a random "weapon", let's say. I do a SQL query something like this:

SELECT def FROM words
INNER JOIN weapons ON words.P_Id=weapons.wordID
ORDER BY RAND() Limit 1

It gets a bit more complex than that for some types of words where I need to get multiple tables joined up, or only retrieve gender appropriate words (made much easier using SQL VIEWs), but a good chunk of the querying is done nice and simply like that. Since each of the tables outside of the words table contains only a listing of ids (mostly), it's not super expensive to have say... Tigers listed as "nouns", "animals", and "creatures" all at once.

A couple tables get a bit complex, such as the verb table. It contains alternate forms of the verb word. The verb "Attacks" has entries for "Attack", "Attacked", "Attacking", and "Attacker". This means that different forms of the same root word are ultimately all treated as the same root word, which will be important for some of the future plans for this prototype.

Some Facts

  • There are currently 431 root words in the database
  • Not all of those root words are actually in use, at the moment
  • There are currently 169 legal title formats
  • A little program I wrote estimates that I can currently generate 98,412,864 different titles!