if (this->elem >= PFE_AI0 && this->elem <= PFE_AI14) {
uint active_ais = 0;
Company *c;
FOR_ALL_COMPANIES(c) {
if (Company::IsValidAiID(c->index) && Company::Get(c->index)->ai_instance != NULL && !Company::Get(c->index)->ai_instance->IsDead()) {
active_ais++;
}
}
if (active_ais != 0) {
uint opcodes = AI::GetMaxOpCodes((CompanyID)(this->elem - PFE_AI0));
uint value = opcodes;
double avg = min(9999.99, _pf_data[this->elem].GetAverageDurationMilliseconds(8));
if (avg * active_ais > GL_RATE) {
value = Clamp(opcodes - (avg * active_ais - GL_RATE) * (avg * active_ais - GL_RATE), 500, 250000);
} else if (avg * active_ais > 0 && avg < GL_RATE / 3) {
value = Clamp(opcodes + GL_RATE / 3 - avg * active_ais, 500, 250000);
}
if (value != opcodes) AI::SetMaxOpCodes((CompanyID)(this->elem - PFE_AI0), value);
}
}