# Theory

This implementation is based on two former articles published by Wizardry and Steamworks:

• Please see our original article state machines, for what we have defined as a Weighted Non-Deterministic Finite Automaton (wNFA).
• Also, please see randomness, entropy and statistics for an introduction to cumulative probabilities.
• Additionally, for extracting data from Second Life, for the cartography example, we use a modified version of permanent primitive url which was also previously used in population genetics and selection to pull and display allele frequencies on a pie chart. This time, we need a 3D plotter, that will allow us to feed a tuple vector, representing the current location of the Great Wanderer and obtain a 3D graph.

## Automata

Figure 1: The Great Wanderer uses an 8-state wNNA to determine the next traveling direction. The Second Life corresponding local axes are superposed on the automaton for clarity and shows the trajectory determined by the wNNA.

As you can observe from Figure 1., the Great Wanderer uses the following states to navigate an enclosed space:

default - this is the initial state.
forward - move the great wanderer forward.
backward - move the great wanderer backward.
left - move the great wanderer left.
right - move the great wanderer right.
up - move the great wanderer up.
down - move the great wanderer down.
compute - reserved for event protection.

The difference from our classic wNFA is that we do not consume states. Instead, they are re-used again, depending on the changes in probabilities for every movement: forward, backward, left, right, up and down. That makes it a Weighted Non-Deterministic Non-Finite Automaton (wNNA) rather, since the Great Wanderer never stops moving and hence never ends up in an end-state. This is on purpose because the Great Wanderer is not meant to stop moving at any point.

Furthermore, we ensure that we do not get stuck in some state by using a timer in each state:

    timer() {
llSetTimerEvent(0);
if(llFabs(
llVecMag(
llList2Vector(
llGetObjectDetails(llGetKey(), [OBJECT_VELOCITY]), 0)))
< ENGINE_SPEED/2) state compute;
llSetTimerEvent(1);
}

which ensures that in case the velocity of the Great Wanderer drops to less than half of the engine speed, the wNNA will immediately switch to the recompute state which will first decrement the probability of the current direction being chosen (since it got the Great Wanderer stuck in the first place) and then pick a new direction based on the current amended set of probabilities.

Formally, a wNNA is a reduction of a wNFA where the set of final accepting states is empty . An extra condition is that all the probabilities have to add up to the same value at every transition of the automaton:

In other words, must remain constant for all transitions of the wNNA (see Figure 1.). In this script, we have chosen probabilities in the range for convenience.

## Canceling Gravity

The default state is the main entry point, and used for initial set-up. In the default we set-up the counter-gravity by calling llSetBuoyancy with a parameter of 1. It might be (although it is unspecified), that in some cases llSetForce is inappropriate for countering gravity unless the distance between the Great Wanderer and the ground is taken into consideration. That is, when we tested llSetForce by calling:

llSetForce(<0,0,9.81>*llGetMass(), FALSE);

we noticed undefined behavior. Our first tests, consisted in a physical primitive containing the aforementioned code, placed within a physical and hollow box which resulted in the upward force being larger than -G, resulting in some unexplained upward movement. Conversely, when not in an enclosed environment, the physical box manifested expected behavior. Because of that, we switched to llSetBuoyancy which, for now, results in the expected result.

As you can see from Figure 1., the default state switches directly to the compute state in order to calculate the next direction of movement. That is where we use llSetBuoyancy to cancel out all the gravity.

## Simplified Algorithm

The movement of the Great Wanderer is given by the following algorithm:

1. initially pick some direction d to move on based on the set of probabilities P.
2. if there is a collision at some point on the trajectory given by the direction d
3. lower the probability of that direction being chosen again by decrementing it and redistributing the remainder to the rest of the probabilities P evenly.
4. random-cumulatively pick the next the direction d based on the amended set of probabilities P.
5. move on the trajectory given by the new direction d.
6. repeat at 2.