This script discribes a system to distribute all food sources to all animal populations in a single level. It is meant to be attech to a script holder in each level.
This distrubution function UpdateFoodNeeds will be given an food source type and do that following:
-
Realistic method
- Locate all food sources with the given type,
foodSources. - For each food source,
foodSource, get all the animal that has access to it,animalPopulations. - For all the animal populations,
animalPopulations, remove the populations that does not consumefoodSource, to get a new listanimalsPopulationsThatCanConsumeFoodSource. - Compute the competition rating (standard deviation) for each
foodSource. - Sort the food sources from
foodSourcebased on the competition rating in increading order. - Distribute each food source from
foodSourcesstarting with food source with low competition rating to high.
- Locate all food sources with the given type,
-
Naive method Step 1,2,3 is the same as realistic method. Jump to step 6 and distribute in any order.
- Population dominance = species dominance • population size
- Group food = (population dominance / total dominance) • food source production
- Individual food = group food / population size
class FoodDistributionSystem
{
// Plot/reserve system
List<FoodSource> getFoodSourceByType()
List<AnimalPopulation> getPopulationsCanAccess(FoodSource);
// AnimalPopulation class
List<AnimalPopulation> getPopulationsThatConsumeFoodSource(List<AnimalPopulation>, NeedType)
float getPoplulationDomiance(AnimalPopulation);
float getPopulationTotalDominace(AnimalPopulation);
float getPopulationSize(AnimalPopulation);
// NeedType
float getFoodSourceOutput(FoodSource);
void distributeFoodSource(FoodSource);
float getCompetionRating(List<AnimalPopulation>);
void updateFoodType(FoodSource.type)
{
List<FoodSource> foodSources = getFoodSourceByType();
SortedList ratingAndPopulationPair = new SortedList();
forEach(fs in foodSources)
{
List<AnimalPopulation> animalPopulations = getPopulationsCanAccess(fs);
List<AnimalPopulation> populationsThatCanConsumeFoodSource(animalPopulations, fs);
float competionRating = getCompetionRating(populationsThatCanConsumeFoodSource);
ratingAndPopulationPair.Add(competionRating, fs);
}
foreach(DictionaryEntry pair in ratingAndPopulationPair)
{
distributeFoodSource(pair.Value);
}
}
}- break functionality into different functions, each function handles exactlly one thing
- try to reduce time complexity
- The systems behavior outsome should be easy for player to notice the effect of a change to the system.