Plant: Extracted a function for plant reproduction.

The existing Seed() places plants with a random chance. This makes testing the placement tiresome, so the actual placing part was moved to the new function DoSeed().
liquid_container
Mark 2016-04-11 21:07:13 +02:00
parent 66c71b4ab8
commit 43d6fcd231
1 changed files with 34 additions and 25 deletions

View File

@ -127,31 +127,40 @@ private func Seed()
var plant; var plant;
if (CheckSeedChance()) if (CheckSeedChance())
{ {
// Apply confinement for plant placement plant = DoSeed();
var size = SeedArea(); }
var area = Shape->Rectangle(GetX() - size / 2, GetY() - size / 2, size, size); return plant;
var confined_area = nil; }
if (this.Confinement)
{ /** Forcefully places a seed of the plant, without random chance
confined_area = Shape->Intersect(this.Confinement, area); or other sanity checks. This is useful for testing.
// Quick-check if intersection to confinement yields an empty area */
// to avoid unnecessery search by PlaceVegetation private func DoSeed()
area = confined_area->GetBoundingRectangle(); {
if (area.w <= 0 || area.h <= 0) return; // Apply confinement for plant placement
} var size = SeedArea();
// Place the plant... var area = Shape->Rectangle(GetX() - size / 2, GetY() - size / 2, size, size);
plant = PlaceVegetation(GetID(), area.x, area.y, area.w, area.h, 3, confined_area); var confined_area = nil;
if (plant) if (this.Confinement)
{ {
// ...but check if it is not close to another one. confined_area = Shape->Intersect(this.Confinement, area);
var neighbour = FindObject(Find_ID(GetID()), Find_Exclude(plant), Sort_Distance(plant->GetX() - GetX(), plant->GetY() - GetY())); // Quick-check if intersection to confinement yields an empty area
var distance = ObjectDistance(plant, neighbour); // to avoid unnecessery search by PlaceVegetation
// Closeness check area = confined_area->GetBoundingRectangle();
if (distance < SeedOffset()) if (area.w <= 0 || area.h <= 0) return;
plant->RemoveObject(); }
else if (this.Confinement) // Place the plant...
plant->KeepArea(this.Confinement); var plant = PlaceVegetation(GetID(), area.x, area.y, area.w, area.h, 3, confined_area);
} if (plant)
{
// ...but check if it is not close to another one.
var neighbour = FindObject(Find_ID(GetID()), Find_Exclude(plant), Sort_Distance(plant->GetX() - GetX(), plant->GetY() - GetY()));
var distance = ObjectDistance(plant, neighbour);
// Closeness check
if (distance < SeedOffset())
plant->RemoveObject();
else if (this.Confinement)
plant->KeepArea(this.Confinement);
} }
return plant; return plant;
} }