bool CNavigationPath::PathIsClear (CSystem *pSystem, CSovereign *pSovereign, const CVector &vFrom, const CVector &vTo, CSpaceObject **retpEnemy, CVector *retvAway) // PathIsClear // // Returns TRUE if the path from vFrom to vTo is free from enemy stations. // If FALSE, retpEnemy is initialized with the enemy that is blocking the // path and retvAway is a unit vector away from the enemy that avoids it { int i; // Loop over all objects in the system CSpaceObject *pNearestEnemy = NULL; Metric rNearestDist = MAX_SAFE_DIST; CVector vNearestAway; for (i = 0; i < pSystem->GetObjectCount(); i++) { CSpaceObject *pObj = pSystem->GetObject(i); CSovereign *pObjSovereign; if (pObj && (pObj->GetScale() == scaleStructure || ((pObj->GetScale() == scaleShip) && (pObj->GetVel().Length2() < MIN_SPEED2))) && (pObjSovereign = pObj->GetSovereign()) && (pObjSovereign->IsEnemy(pSovereign)) && pObj->CanAttack()) { CVector vAway; Metric rDist = CalcDistanceToPath(pObj->GetPos(), vFrom, vTo, NULL, &vAway); if (rDist < rNearestDist) { rNearestDist = rDist; pNearestEnemy = pObj; vNearestAway = vAway; } } } // If we found a threatening object, return it if (pNearestEnemy) { if (retpEnemy) *retpEnemy = pNearestEnemy; if (retvAway) *retvAway = vNearestAway; return false; } // Otherwise, the path is OK return true; }
bool CFerianShipAI::InRangeOfThreat (CSpaceObject **retpThreat) // InRangeOfThreat // // Returns the nearest threat { if (m_pShip->IsDestinyTime(19)) { CSystem *pSystem = m_pShip->GetSystem(); int iDestiny = m_pShip->GetDestiny(); // Get the list of objects that intersect the object SSpaceObjectGridEnumerator i; pSystem->EnumObjectsInBoxStart(i, m_pShip->GetPos(), MAX_THREAT_DIST); // Loop over all objects Metric rNearestDist2 = MAX_THREAT_DIST * MAX_THREAT_DIST2; CSpaceObject *pNearestObj = NULL; while (pSystem->EnumObjectsInBoxHasMore(i)) { CSpaceObject *pObj = pSystem->EnumObjectsInBoxGetNext(i); // If the object is in the bounding box then remember // it so that we can do a more accurate calculation. if (pObj->GetCategory() == CSpaceObject::catShip && (!m_pShip->IsFriend(pObj) || pObj->GetDestiny() < iDestiny) && pObj->CanAttack()) { Metric rDist2 = m_pShip->GetDistance2(pObj); if (rDist2 < rNearestDist2) { pNearestObj = pObj; rNearestDist2 = rDist2; } } } // Done if (pNearestObj) { *retpThreat = pNearestObj; return true; } } return false; }
void GenerateStationPlaceSim (CUniverse &Universe, CXMLElement *pCmdLine) { ALERROR error; int i, j, k; CSovereign *pPlayer = Universe.FindSovereign(g_PlayerSovereignUNID); int iSystemSample = pCmdLine->GetAttributeIntegerBounded(CONSTLIT("count"), 1, -1, 1); bool bLogo = !pCmdLine->GetAttributeBool(NO_LOGO_SWITCH); // Generate systems for multiple games CSymbolTable AllSystems(TRUE, TRUE); for (i = 0; i < iSystemSample; i++) { if (bLogo) printf("pass %d...\n", i+1); CTopologyNode *pNode = Universe.GetFirstTopologyNode(); while (true) { // Create the system CSystem *pSystem; if (error = Universe.CreateStarSystem(pNode, &pSystem)) { printf("ERROR: Unable to create star system.\n"); return; } // Find this system in the table. SPSimSystemInfo *pSystemEntry; if (error = AllSystems.Lookup(pNode->GetSystemName(), (CObject **)&pSystemEntry)) { pSystemEntry = new SPSimSystemInfo; pSystemEntry->sName = pNode->GetSystemName(); pSystemEntry->iLevel = pNode->GetLevel(); pSystemEntry->dwSystemType = pNode->GetSystemTypeUNID(); pSystemEntry->iCount = 1; for (j = 0; j < DIST_BUCKET_COUNT; j++) pSystemEntry->iEnemies[j] = 0; AllSystems.AddEntry(pSystemEntry->sName, pSystemEntry); } else pSystemEntry->iCount++; // For all active stations in the system, count the number of enemy stations // within certain distance buckets for (j = 0; j < pSystem->GetObjectCount(); j++) { CSpaceObject *pObj = pSystem->GetObject(j); // Find any objects that are lootable by the player if (pObj && pObj->GetCategory() == CSpaceObject::catStation && pObj->CanAttack()) { // Count to see how many enemy stations are in range for (k = 0; k < pSystem->GetObjectCount(); k++) { CSpaceObject *pEnemy = pSystem->GetObject(k); if (pEnemy && pEnemy->GetCategory() == CSpaceObject::catStation && pEnemy->CanAttack() && (pEnemy->IsEnemy(pObj) || pObj->IsEnemy(pEnemy))) { Metric rDist = pObj->GetDistance(pEnemy); int iDist = DistToBucketIndex(rDist); if (iDist != -1) { ASSERT(iDist < DIST_BUCKET_COUNT && iDist >= 0); pSystemEntry->iEnemies[iDist]++; int iLSDist = (int)((rDist / LIGHT_SECOND) + 0.5); if (iLSDist < 30) { printf("%s: %s (%x) and %s (%x) within %d ls\n", pSystem->GetName().GetASCIIZPointer(), pObj->GetNounPhrase().GetASCIIZPointer(), pObj->GetID(), pEnemy->GetNounPhrase().GetASCIIZPointer(), pEnemy->GetID(), iLSDist); } } } } } } // Get the next node CString sEntryPoint; pNode = pSystem->GetStargateDestination(CONSTLIT("Outbound"), &sEntryPoint); if (pNode == NULL || pNode->IsEndGame()) break; // Done with old system Universe.DestroySystem(pSystem); } Universe.Reinit(); } if (bLogo) printf("FINAL SYSTEM STATISTICS\n\n"); // Output total value stats printf("Level\tSystem\t<10 ls\t<25 ls\t<50ls\t<100 ls\n"); int iTotals[DIST_BUCKET_COUNT]; for (i = 0; i < DIST_BUCKET_COUNT; i++) iTotals[i] = 0; for (i = 0; i < AllSystems.GetCount(); i++) { SPSimSystemInfo *pSystemEntry = (SPSimSystemInfo *)AllSystems.GetValue(i); printf("%d\t%s", pSystemEntry->iLevel, pSystemEntry->sName.GetASCIIZPointer()); for (j = 0; j < DIST_BUCKET_COUNT; j++) { printf("\t%.2f", (double)pSystemEntry->iEnemies[j] / (double)pSystemEntry->iCount); iTotals[j] += pSystemEntry->iEnemies[j]; } printf("\n"); } // Totals printf("\n"); printf("Within 10 ls: %.2f\n", iTotals[0] / (double)iSystemSample); printf("Within 25 ls: %.2f\n", iTotals[1] / (double)iSystemSample); printf("Within 50 ls: %.2f\n", iTotals[2] / (double)iSystemSample); printf("Within 100 ls: %.2f\n", iTotals[3] / (double)iSystemSample); printf("\n"); }