Cabal Common Library Doc PhraseGen

From Elite Wiki
Revision as of 11:52, 13 April 2013 by Svengali (talk | contribs) (Started documentation)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Overview

This is the main class for the generator with its members and part of the Cabal_Common_Library.

PhraseGen is a generator for phrases, names, sentences or other messages and works similiar to other random mechanisms on the net. It uses 3 scripts:

  • a data script holds a few objects only (words, sets, irregular nouns, irregualar verbs and superlatives).
  • a build script which prefills some lists, processes OXP input and handles the rules.
  • a helper script which does the communication between HTML and build script. This is not used in Oolite.


The words object is the heart of the whole thing. It contains arrays which are used in the sets. Multiple arrays can be concatenated easily and content can be reused - if I recall it right it's a similiar approach to CMcLs RandomShipNames.oxp. The concept is basically using the 'soft' references in JS.

Every set contains 18 arrays with words (or phrases) and a array with sentence patterns.

The helper script + HTML will be available as separate download, but probably only works properly in Firefox/SeaMonkey. If someone wants to create a crossbrowser version, give me a call.


How it works

The script simply replaces the numbers (1-9) in the pattern with content from the arrays.

Switching between 1-9 and 10-18 is done via special char. Some other special chars are giving patterns some more oompf, e.g. for displaying player.name or setting flags for the word processing. And when a word is picked from the arrays additional rules are applied before it replaces the number in the pattern. The rules are designed to work with english words (some exceptions are handled).

Main goal is performance while handling a wide range of possibilities and it really looks promising.

The script also precreates some specials and populates the corresponding arrays to be combined via pattern when a OXP requests it.

Additionally fields can be related to each other and even a conditional markup is processed (recursion depth is limited).


Methods

makePhrase()

var a = worldScripts.Cabal_Common_PhraseGen.makePhrase(setname [,pattern]);

Is simple and straightforward. When called the method returns the created string.

Parameters:

setname
String/Object.
String:PhraseGen looks up if the pool contains a set with this name.
Object:PhraseGen uses the passed object.
pattern
String/Array. Optional. If not specified PhraseGen will use a randomly choosen pattern from the current set.
String:
Array:PhraseGen will randomly choose a element.

Returns:

phrase
String. The generated string or false.


addSet()

var a = worldScripts.Cabal_Common_PhraseGen.addSet(setname, obj [,clone]);

For adding data to the pool there are two possible ways.

Parameters:

setname
String. Unique identifier for the set. Probably the easist way is to use OXPName_SetID.
obj
Object. Holds the arrays with words and patterns.
clone
Boolean. Optional. If specified PhraseGen creates a deepcopy of the object. Otherwise it will store a object holding references.

Returns:

success
Boolean. True if set was placed, otherwise false.