MCPcopy Create free account
hub / github.com/OpenLoco/OpenLoco / selectNewCompetitor

Function selectNewCompetitor

src/OpenLoco/src/World/CompanyManager.cpp:363–442  ·  view source on GitHub ↗

0x0042F9CB

Source from the content-addressed store, hash-verified

361
362 // 0x0042F9CB
363 static LoadedObjectId selectNewCompetitor()
364 {
365 sfl::static_vector<LoadedObjectId, ObjectManager::getMaxObjects(ObjectType::competitor)> loadedUnusedCompetitors;
366 for (LoadedObjectId id = 0U; id < ObjectManager::getMaxObjects(ObjectType::competitor); ++id)
367 {
368 auto* competitorObj = ObjectManager::get<CompetitorObject>(id);
369 if (competitorObj == nullptr)
370 {
371 continue;
372 }
373 const bool isFree = std::none_of(companies().begin(), companies().end(), [id](Company& company) { return company.competitorId == id; });
374 if (isFree)
375 {
376 loadedUnusedCompetitors.push_back(id);
377 }
378 }
379
380 if (!loadedUnusedCompetitors.empty())
381 {
382 const auto r = ((gPrng1().randNext() & 0xFFFFU) * loadedUnusedCompetitors.size()) / 65536;
383 return loadedUnusedCompetitors[r];
384 }
385
386 std::optional<std::vector<ObjectHeader>> bestInstalled = std::nullopt;
387 uint8_t bestInstalledValue = 0xFFU;
388 auto installedCompetitors = ObjectManager::getAvailableObjects(ObjectType::competitor);
389 for (auto& installed : installedCompetitors)
390 {
391 bool isInUse = false;
392 for (LoadedObjectId id = 0U; id < ObjectManager::getMaxObjects(ObjectType::competitor); ++id)
393 {
394 if (ObjectManager::get<CompetitorObject>(id) == nullptr)
395 {
396 continue;
397 }
398 auto& loadedHeader = ObjectManager::getHeader(LoadedObjectHandle{ ObjectType::competitor, id });
399 if (loadedHeader == installed.object._header)
400 {
401 isInUse = true;
402 break;
403 }
404 }
405 if (isInUse)
406 {
407 continue;
408 }
409
410 uint8_t metric = 0;
411 if (getGameState().preferredAIIntelligence != 0)
412 {
413 metric += std::abs(kAiToMetric[installed.object._displayData.intelligence] - getGameState().preferredAIIntelligence);
414 }
415 if (getGameState().preferredAIAggressiveness != 0)
416 {
417 metric += std::abs(kAiToMetric[installed.object._displayData.aggressiveness] - getGameState().preferredAIAggressiveness);
418 }
419 if (getGameState().preferredAICompetitiveness != 0)
420 {

Callers 2

createPlayerCompanyFunction · 0.85
createAiCompanyFunction · 0.85

Calls 10

getMaxObjectsFunction · 0.85
companiesFunction · 0.85
getAvailableObjectsFunction · 0.85
push_backMethod · 0.80
beginMethod · 0.45
endMethod · 0.45
emptyMethod · 0.45
randNextMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected