VoxelSim/simulation.cpp
2025-01-26 20:07:57 +01:00

50 lines
1.5 KiB
C++

#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 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++)
{
unsigned int offset = getCellOffset(x, y, z, grid);
unsigned int material = grid.in[offset];
switch (material)
{
case WATER:
if (y > 0)
{
unsigned int downOffset = getCellOffset(x, y - 1, z, grid);
// Flow down if cell is empty
if (grid.out[downOffset] == 0)
{
grid.out[downOffset] = material;
}
}
default:
grid.out[offset] = material;
}
}
}
}
}
}