Wednesday, May 8, 2013

Pulse Function Overview

The game consists of a series of Nodes composed of a Mesh Renderer, a Collider, 2 LineRenderers, and a Script specific to the cell type.

When a Node is created, it "Checks-Into" a "Game Manager" Script present in the scene.  The game manager controls the flow of time by calling my own Update() method called "Pulse()". The "Pulse" originates in the Stem at Depth 0 and gets called  in order of construction on each subsequent depth of nodes.
       

       Roots<--Stem ---> Branches
   4 - 3 - 2 - 1 - 0 - 1 - 2 - 3 - 4
           /                               \
    4 - 3                                 4 - 5 - 6

Here is an Example "Pulse" Function from a branch


public void Pulse ()                                                                                                                                                                                                 
{                                                                                                                                                                                                                               
       DeterminePublicFillLevel (); // Determines fill Percentage of nutrients before Pulse                                                                                
if (Parent)                                                                                                                                                                                                      
CellMaintinance ();      // Uses 1 ATP and/or breaks a sugar into 31 more ATP                                                                                
if (CellIsPowered) {         // If a cell has ATP                                                                                                                                      
if (myLevel < desiredLevel) {                                                                                                                                                               
RequestUpgrade ();   // Cells Manage their queued upgrading                                                                                            
}                                                                                                                                                                                                             
ExternalWork ();         // If cell has power, Perform Diffusion and Active Transport of Nutrients                                                                                
} else {                                                                                                                                                                                                                       
CallForHelp ();          // If Cell is not powered, Request supplies from neighbors and Downgrade to stay alive                                                                                
if (myLevel > desiredLevel) {                                                                                                                                                                   
RequestDowngrade ();                                                                                                                                                                
}                                                                                                                                                                                                                                                
DiffuseNutrientFront (0); // Even if the cell is Unpowered, water will still diffuse Forwards(Up)             }                                                                                                                                                                                                                                                
DeterminePublicFillLevel ();  //Determine Fill Percentages after turn for other cells to View                                                                                
UpdateVisuals ();             // Update all visual aspects of cells activities during this turn                                                                                
}                                                                                                                                                                                                                                                

A cell will attempt to "Average" its nutrients with its neighbors during DiffuseNutrientsFront/Back() while constrained by certain Maximum outgoing and incoming bandwidths determined by each node individually.


public void DiffuseNutrientBack (int index)                                                                              
{                                                                                                                                                              
if (Parent) { // Parent is the Node that created this node                                                                              
GeneralCell f = Parent; // cache the Parent node(also standardizes Front/Back code as "f")                                       
if (PublicFillLevel [index] > f.PublicFillLevel [index]) {  // Averageing is done on wighted averages as percentages of maximum capacities                                                                                                                     
float Average = (PublicFillLevel [index] + f.PublicFillLevel [index]) / 2.0f;                                       
float NewTarget = Nutrients [index] - ((float)Capacities [index] * Average); // the Delta(difference) is calculated                                                                                                                                                            
float delta = Mathf.Abs (NewTarget);                                                                              
amountsRequested [index] += delta;                                                                              
delta = Mathf.Min (Mathf.Min (delta, OutGoingBandwidths [index]),  f.IncomingBandwidths [index]);//Delta is adjusted to be no larger than the outgoing or incoming bandwidths of the sender and reciever
sendResources (index, f, delta*.8f); //80 percent of the calculated exchange is sent.(helps stabilize simulation not to always send everything)                                                                                                                     
}                                                                                                                                                            
}                                                                                                                                                            
}                                                                                                                                                            

As always a link to the Freshest Version of Simulation:
GAME

No comments:

Post a Comment