VoxelSim/simulation.cpp
2025-01-26 07:38:56 +01:00

51 lines
1.6 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* 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;
}
}
}
}
}
}