73 lines
2.3 KiB
C++
73 lines
2.3 KiB
C++
#include "simulation.h"
|
|
#include <cstring>
|
|
|
|
namespace simulation
|
|
{
|
|
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)
|
|
{
|
|
int offset = x + y * grid->x_max + z * (grid->y_max * grid->x_max);
|
|
return offset;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
void step(
|
|
double millisecondsSinceLastStep,
|
|
SimulationGrid* grid)
|
|
{
|
|
for (unsigned char x = 0; x < grid->x_max; x++)
|
|
{
|
|
for (unsigned char y = 0; y < grid->y_max; y++)
|
|
{
|
|
for (unsigned char z = 0; z < grid->z_max; z++)
|
|
{
|
|
int offset = getCellOffset(x, y, z, grid);
|
|
unsigned int material = grid->in[offset];
|
|
switch (material)
|
|
{
|
|
case WATER:
|
|
{
|
|
int downOffset = getCellOffset(x, y - 1, z, grid);
|
|
// Flow down if cell is empty
|
|
if (downOffset >= 0 && grid->out[downOffset] == 0)
|
|
{
|
|
grid->out[downOffset] = material;
|
|
grid->out[offset] = VACUUM;
|
|
} else
|
|
{
|
|
// Stay in the same location
|
|
grid->out[offset] = material;
|
|
}
|
|
break;
|
|
}
|
|
case MERCURY:
|
|
{
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
grid->out[offset] = material;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void prepareForNextStep(SimulationGrid* grid)
|
|
{
|
|
// Flip the buffers
|
|
unsigned int* inCells = grid->in;
|
|
grid->in = grid->out;
|
|
grid->out = inCells;
|
|
// Clear out buffer
|
|
size_t bufferSizeInBytes = sizeof(unsigned int) * grid->x_max * grid->y_max * grid->z_max;
|
|
std::memset(grid->out, VACUUM, bufferSizeInBytes);
|
|
}
|
|
}
|