Class Index | File Index

Classes


Class com.jiffeegames.Encoding

An Encoding object very efficiently encodes the entire state of the game as a string, or decodes it from a string. The model used is very general, so there should be no need to change this code for any specific game. It basically takes the whole string and breaks it up into 4-char 'chunks', each of which is interpreted as a base-90 integer. Each of these integers in turn is interpreted as a mixed-base integer, where each 'place' stores the value of one variable. Note that "variable" here means a named value being stored in the encoding, which is not the same as a JavaScript "var".

The model:

The state is divided into 'chunks', each of which is a roughly 26-bit integer which holds the encoded value of one or more variables. Each chunk has both a 'chunkAsNum' and a 'chunkAsStr' which both hold the same number and are kept in sync.
The chunkAsStr is just a base-90 representation of the integer.
The chunkAsNum is the same value, but stored as a 64-bit JavaScript floating point number.
The integer represents the variable values in mixed-base format, e.g. if:

then: This scheme maximizes the amount of information you can represent in the string, which is restricted to printable characters and thus has only about 6.5 bits per byte available for real data. We stick to ASCII characters, because Unicode would have even worse efficiency once the UTF-8 encoding rules are taken into account. (This deliberately reflects the limitations of cookies, so that these strings may easily be stored inside a cookie.)

This means that the state of each variable in the game is stored in multiple places, all of which are kept synchronized: Of course the variable values are also stored on disk (in cookies) and in the jiffee state-management code, but that is outside the scope of this file.

The standard index names used are:
Defined in: jiffee-encoding.js.

Class Summary
Constructor Attributes Constructor Name and Description
 
Create an empty Encoding object.
Method Summary
Method Attributes Method Name and Description
 
addVar(noun, trait, maxVal)
Add another variable to the encoding.
 
Get the hash code (fingerprint) associated with this encoding.
 
Get the string that encodes the entire game state.
 
getVar(varNum)
Read the value of a single encoded variable.
 
init(locate, language)
 
Do an integer division.
 
setString(str)
Change the state of the whole game as needed to match an encoded string.
 
setVar(varNum, newVal)
Set the value of a single variable in the Encoding.
 
Allocate all the variables to "chunks".
Class Detail
com.jiffeegames.Encoding()
Create an empty Encoding object. The normal call is
var encoding = new com.jiffeegames.Encoding();
Requires:
com.jiffeegames.Checks
Method Detail
{integer} addVar(noun, trait, maxVal)
Add another variable to the encoding. Note that the noun and trait names are used only to compute a fingerprint for the encoding; they are not actually stored anywhere.
Parameters:
{String} noun
The name of the noun which owns the trait.
{String} trait
The name of the trait whose value is being encoded.
{integer} maxVal
The maximum allowed value of the trait.
Returns:
The index of the new variable within the Encoding. This index must be used to refer to the variable in get/set calls.

{String} getFingerprint()
Get the hash code (fingerprint) associated with this encoding.
Returns:
The fingerprint.

{String} getString()
Get the string that encodes the entire game state.
Returns:
The string that encodes the state.

{integer} getVar(varNum)
Read the value of a single encoded variable.
Parameters:
{integer} varNum
The index of the variable within an Encoding.
Returns:
The value of the variable.

init(locate, language)
Parameters:
locate
language

{integer} integerDivide_(a, b)
Do an integer division.
Parameters:
{integer} a
The dividend.
{integer} b
The divisor.
Returns:
The quotient, truncated to an integer.

{void} setString(str)
Change the state of the whole game as needed to match an encoded string.
Parameters:
{String} str
The string that encodes a state.

{void} setVar(varNum, newVal)
Set the value of a single variable in the Encoding.
Parameters:
{integer} varNum
The index of the variable in the encoding.
{integer} newVal
The new value of the variable.

{void} startManually()
Allocate all the variables to "chunks". Do this after adding all variables but before touching the encodingString. This is not a normal start() routine because it needs to be called *after* traits starts.

Documentation generated by JsDoc Toolkit 2.3.2 on Sun Jul 25 2010 13:49:30 GMT-0700 (PDT)