The first step in this process is the most abstract: defining what I want the mod to be. I want to build an AI mod.
What does that mean? Well, I want an AI that will acquire resources and processes them. I want the acquisition to happen concurrently with the processing. I'll formalize this:
Code: Select all
//In thread 0:
while(true)
{
AcquireResources();
}
//In thread 1:
while(true)
{
ProcessResources();
}
How does a player acquire resources? First he must decide that he needs them; next he must find them; then he must mine or pump them; and finally he must bring them home.
Code: Select all
void AcquireResources()
{
Resource[] neededResources = AnalyzeResourceNeeds();
Vector2[] positions = FindResources(neededResources);
Mine(positions);
Transport(positions, homePosition);
}
Code: Select all
Resource[] AnalyzeResourceNeeds()
{
Resource[] requiredResources;
foreach(Resource res in primaryResources)
{
if(CalculateConsumers(res) >= CalculateProduction(res))
{
requiredResources.Add(res);
}
}
return requiredResources;
}
Code: Select all
double CalculateConsumers(Resource res)
{
double consumption;
foreach(Processor proc in processors)
{
if(proc.recipe.ingredients.ContainsKey(res))
{
consumption += proc.recipe.ingredients[res] / proc.recipe.time;
}
}
return consumption;
}
Code: Select all
double CalculateProducers(Resource res)
{
double production
foreach(Processor proc in processors)
{
if(proc.recipe.products.ContainsKey(res))
{
production += proc.recipe.products[res] / proc.recipe.time;
}
}
return production;
}
How might a player find resources? He might first check the map to search for promising ore patches above a certain size and, should he find none, expand his radar range.
Code: Select all
Vector2[] FindResources(Resource[] neededResources)
{
Vector2[] positions;
foreach(Resource res in neededResources)
{
if(BestOrePatch(res).amount > minimumExploitationSetpoint)
{
positions.Add(BestOrePatch(res).position);
}else
{
ExpandRadarCoverage();
}
}
return positions;
}
I still have an awful lot to do, and the actual placement of blueprint ghosts will be a daunting task, but it's a good start. I'll update the spoiler below with the pseudocode developed in future updates.
The Code So Far