Shadowrun: Awakened 29 September 2011 - Build 871
Functions
Update_state.cpp File Reference
#include "Evolution.h"
Include dependency graph for Update_state.cpp:

Go to the source code of this file.

Functions

char CheckCell (Matrix *m_matrix, int cellNumber)
char GetAdjacentCellState (char *source, int x, int y, int cellNumber, int cp)
void UpdateState (Matrix *m_matrix, char *dest, int begin, int end)

Function Documentation

char CheckCell ( Matrix m_matrix,
int  cellNumber 
)

Definition at line 366 of file Update_state.cpp.

References Matrix::data, GetAdjacentCellState(), Matrix::height, and Matrix::width.

Referenced by UpdateState().

{
    char total = 0;
    char* source = m_matrix->data;
    //look around to find cell's with status "alive"
    for(int i=1; i<9; i++)
    {
        total += GetAdjacentCellState(source, m_matrix->width, m_matrix->height, cellNumber, i);
    }
    // if the number of adjacent live cells is < 2 or > 3, the result is a dead 
    // cell regardless of its current state. (A live cell dies of loneliness if it
    // has less than 2 neighbors, and of overcrowding if it has more than 3; a new
    // cell is born in an empty spot only if it has exactly 3 neighbors.
    if (total < 2 || total > 3)
    {
        return 0;
    }

    // if we get here and the cell position holds a living cell, it stays alive
    if (*(source+cellNumber))
    {
        return 1;
    }

    // we have an empty position. If there are only 2 neighbors, the position stays
    // empty.
    if (total == 2)
    {
        return 0;
    }

    // we have an empty position and exactly 3 neighbors. A cell is born.
    return 1;
}
char GetAdjacentCellState ( char *  source,
int  x,
int  y,
int  cellNumber,
int  cp 
)

Definition at line 230 of file Update_state.cpp.

Referenced by CheckCell().

{
/* 
cp 
*-- cp=1 ... --- cp=8 (summary: -1-2-3-
-x-          -x-                -4-x-5-
---          --*                -6-7-8- )
*/
    char cellState = 0;        // return value

    // set up boundary flags to trigger field-wrap logic
    bool onTopRow = false;
    bool onBottomRow = false;
    bool onLeftColumn = false;
    bool onRightColumn = false;

    // check to see if cell is on top row
    if (cellNumber < x)
    {
        onTopRow = true;
    }
    // check to see if cell is on bottom row
    if ((x*y)-cellNumber <= x)
    {
        onBottomRow = true;
    }
    // check to see if cell is on left column
    if (cellNumber%x == 0)
    {
        onLeftColumn = true;
    }
    // check to see if cell is on right column
    if ((cellNumber+1)%x == 0)
    {
        onRightColumn = true;
    }

    switch (cp)
    {
        case 1:
            if (onTopRow && onLeftColumn)
            {
                return *(source+((x*y)-1));
            }
            if (onTopRow && !onLeftColumn)
            {
                return *(source+(((x*y)-x)+(cellNumber-1)));
            }
            if (onLeftColumn && !onTopRow)
            {
                return *(source+(cellNumber-1));
            }
            return *((source+cellNumber)-(x+1));

        case 2:
            if (onTopRow)
            {
                return *(source+(((x*y)-x)+cellNumber));
            }
            return *((source+cellNumber)-x);

        case 3:
            if (onTopRow && onRightColumn)
            {
                return *(source+((x*y)-x));
            }
            if (onTopRow && !onRightColumn)
            {
                return *(source+(((x*y)-x)+(cellNumber+1)));
            }
            if (onRightColumn && !onTopRow)
            {
                return *(source+((cellNumber-(x*2))+1));
            }
            return *(source+(cellNumber-(x-1)));

        case 4:
            if (onRightColumn)
            {
                return *(source+(cellNumber-(x-1)));
            }
            return *(source+(cellNumber+1));

        case 5:
            if (onBottomRow && onRightColumn)
            {
                return *source;
            }
            if (onBottomRow && !onRightColumn)
            {
                return *(source+((cellNumber-((x*y)-x))+1));
            }
            if (onRightColumn && !onBottomRow)
            {
                return *(source+(cellNumber+1));
            }
            return *(source+(((cellNumber+x))+1));

        case 6:
            if (onBottomRow)
            {
                return *(source+(cellNumber-((x*y)-x)));
            }
            return *(source+(cellNumber+x));

        case 7:
            if (onBottomRow && onLeftColumn)
            {
                return *(source+(x-1));
            }
            if (onBottomRow && !onLeftColumn)
            {
                return *(source+(cellNumber-((x*y)-x)-1));
            }
            if (onLeftColumn && !onBottomRow)
            {
                return *(source+(cellNumber+((x*2)-1)));
            }
            return *(source+(cellNumber+(x-1)));

        case 8:
            if (onLeftColumn)
            {
                return *(source+(cellNumber+(x-1)));
            }
            return *(source+(cellNumber-1));
    }
    return cellState;
}
void UpdateState ( Matrix m_matrix,
char *  dest,
int  begin,
int  end 
)

Definition at line 401 of file Update_state.cpp.

References CheckCell().

{
        for (int i=begin; i<=end; i++)
        {
            *(dest+i) = CheckCell(m_matrix, i);
        }
}

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net