void CEnvironment::Timestep(uint8_t n)
{
uint32_t i, x, y;
vector<CIndividual *>::iterator j;
string query;
for (i = 0; i < n; i++) // Loop allows multiple timesteps to be run in one call.
{
// Here's where we do all the gruntwork.
// Individuals must choose whether to mate or disperse; choose a mate; and
// generally faff about
cout << "Start timestep: " << IntToStr(this->CurrentTime) << endl;
DB.RunQuery("BEGIN TRANSACTION");
// Work across the grid, stepwise, from top left to bottom right
for (x = 0; x < GRID_X; x++)
for (y = 0; y < GRID_Y; y++)
for(j = this->Grid[x][y].Individuals.begin(); j != this->Grid[x][y].Individuals.end(); j++)
if(!(*j)->Moved) // Only one move/individual
{
CGridLoc *here = &this->Grid[x][y];
// p(Dispersal) = (d/q)*g, or disperse if 1 individual in the square.
if (here->Individuals.size() == 1 || (rand()/RAND_MAX <= (here->Individuals.size() / here->Quality) * (*j)->DispersalP))
{ // Disperse
// http://dx.doi.org/10.1016/j.ecolmodel.2005.04.017
} else {// Mate
// How do I found mate?
};
(*j)->Moved = true;
// p(Death) = age / quality
if (rand()/RAND_MAX <= (this->CurrentTime -(*j)->BirthTime) / here->Quality)
{ // Die
(*j)->DeathTime = this->CurrentTime;
query = "UPDATE Individuals SET DeathTime = '" +
IntToStr(this->CurrentTime) + "' WHERE IndividualID = '"+
IntToStr((*j)->IndividualID)+"'";
DB.RunQuery(query.c_str());CEnvironment::
here->Individuals.erase(j);
};
};
// Clean up this->Individuals
vector <CIndividual>::iterator l;
for (l = this->Individuals.begin(); l != this->Individuals.end(); l++)
if ((*l).DeathTime == this->CurrentTime) // Died this turn?
{
delete &(*l);
this->Individuals.erase(l);
} else (*l).Moved = false;
query = "INSERT INTO Misc (Timestamp) VALUES ('" +
IntToStr(this->CurrentTime) + "')";
DB.RunQuery(query.c_str());
DB.RunQuery("COMMIT TRANSACTION");
cout << "End timestep: " << IntToStr(this->CurrentTime) << endl;
this->CurrentTime++; // Next timestep, please
};
}