Initial
This commit is contained in:
parent
591cbd03bb
commit
d886a1c2f2
5 changed files with 158 additions and 0 deletions
51
simulation.cpp
Normal file
51
simulation.cpp
Normal file
|
@ -0,0 +1,51 @@
|
|||
#include "simulation.h"
|
||||
|
||||
namespace simulation
|
||||
{
|
||||
unsigned int getCellOffset(
|
||||
unsigned char x,
|
||||
unsigned char y,
|
||||
unsigned char z,
|
||||
SimulationGrid* grid)
|
||||
{
|
||||
if (x < grid->x_max && y < grid->y_max && z < grid->z_max)
|
||||
{
|
||||
unsigned int offset = x + y * grid->x_max + z * (grid->y_max * grid->x_max);
|
||||
return offset;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void step(
|
||||
double millisecondsSinceLastStep,
|
||||
SimulationGrid* input,
|
||||
SimulationGrid* output)
|
||||
{
|
||||
for (unsigned char x = 0; x < input->x_max; x++)
|
||||
{
|
||||
for (unsigned char y = 0; y < input->y_max; y++)
|
||||
{
|
||||
for (unsigned char z = 0; z < input->z_max; z++)
|
||||
{
|
||||
unsigned int offset = getCellOffset(x, y, z, input);
|
||||
unsigned int material = input->cells[offset];
|
||||
switch (material)
|
||||
{
|
||||
case WATER:
|
||||
if (y > 0)
|
||||
{
|
||||
unsigned int downOffset = getCellOffset(x, y - 1, z, input);
|
||||
// Flow down if cell is empty
|
||||
if (output->cells[downOffset] == 0)
|
||||
{
|
||||
output->cells[downOffset] = material;
|
||||
}
|
||||
}
|
||||
default:
|
||||
output->cells[offset] = material;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue