void CLocationCriteriaTableEntry::InitSelector (SSelectorInitCtx &InitCtx, CCompositeImageSelector *retSelector) // InitSelector // // Initializes the selector { int i; // Generate the table based on criteria and location TProbabilityTable<int> ProbTable; for (i = 0; i < m_Table.GetCount(); i++) { // Compute the probability of this entry at the // given location. int iChance = m_Table[i].Criteria.CalcLocationWeight(InitCtx.pSystem, InitCtx.sLocAttribs, InitCtx.vObjPos); if (iChance > 0) ProbTable.Insert(i, iChance); } // If none match, then add the default element if (ProbTable.GetCount() == 0 && m_iDefault != -1) ProbTable.Insert(m_iDefault, 1000); // Roll if (ProbTable.GetCount() > 0) { int iRoll = ProbTable.RollPos(); int iIndex = ProbTable[iRoll]; retSelector->AddVariant(GetID(), iIndex); m_Table[iIndex].pImage->InitSelector(InitCtx, retSelector); } }
void CSystemCreateStats::AddFillLocationsTable (CSystem *pSystem, const TProbabilityTable<int> &LocationTable, const CString &sStationCriteria) // AddFillLocationsTable // // Adds stats about <FillLocations> { int i, j; if (LocationTable.GetCount() == 0) return; SFillLocationsTable *pEntry = m_FillLocationsTables.Insert(); pEntry->iLevel = pSystem->GetLevel(); pEntry->sSystemName = pSystem->GetName(); pEntry->pSystemType = pSystem->GetType(); pEntry->sStationCriteria = sStationCriteria; // Parse station criteria if we've got it. // NOTE: For now we only do enemies. CString sEnemyStationCriteria = strPatternSubst(CONSTLIT("%s,%s"), sStationCriteria, CONSTLIT("*enemy")); CAttributeCriteria StationCriteria; StationCriteria.Parse(sEnemyStationCriteria, 0); // Start by generating a base probability for all station encounters // relative to the system. TProbabilityTable<CStationType *> BaseProb; for (i = 0; i < g_pUniverse->GetStationTypeCount(); i++) { CStationType *pType = g_pUniverse->GetStationType(i); int iBaseChance = StationCriteria.AdjStationWeight(pType, ((1000 / ftCommon) * pType->GetFrequencyForSystem(pSystem))); if (iBaseChance > 0) { CAttributeCriteria LocationCriteria; LocationCriteria.Parse(pType->GetLocationCriteria(), 0); // Average out our chance of ending up at one of the given locations. int iTotal = 0; for (j = 0; j < LocationTable.GetCount(); j++) { int iLocID = LocationTable[j]; CLocationDef *pLoc = pSystem->GetLocation(iLocID); iTotal += LocationCriteria.AdjLocationWeight(pSystem, pLoc); } int iAverageChance = iTotal / LocationTable.GetCount(); // Now adjust the base chance int iChance = iBaseChance * iAverageChance / 1000; if (iChance <= 0) continue; // Add it to our table. pEntry->Table.Insert(pType, iChance); } } }
CSoundType *CSoundtrackManager::CalcGameTrackToPlay (const CString &sRequiredAttrib) // CalcGameTrackToPlay // // Calculates a game track to play. { int i; CSystem *pSystem = g_pUniverse->GetCurrentSystem(); // Create a probability table of tracks to play. TSortMap<int, TProbabilityTable<CSoundType *>> Table(DescendingSort); for (i = 0; i < g_pUniverse->GetSoundTypeCount(); i++) { CSoundType *pTrack = g_pUniverse->GetSoundType(i); // If this is not appropriate music then skip it if (!pTrack->HasAttribute(sRequiredAttrib)) continue; // If this is a system track and we've already played a // system track, then skip it. else if (m_bSystemTrackPlayed && pTrack->HasAttribute(ATTRIB_SYSTEM_SOUNDTRACK)) continue; // Calculate the chance for this track based on location // criteria. int iChance = (pSystem ? pSystem->CalcMatchStrength(pTrack->GetLocationCriteria()) : 1000); if (iChance == 0) continue; // Adjust probability based on when we last played this tack. switch (GetLastPlayedRank(pTrack->GetUNID())) { case 0: iChance = 0; break; case 1: iChance = iChance / 10; break; case 2: iChance = iChance / 5; break; case 3: case 4: iChance = iChance / 3; break; case 5: case 6: case 7: iChance = iChance / 2; break; case 8: case 9: case 10: iChance = 2 * iChance / 3; break; } if (iChance == 0) continue; // Add to the probability table TProbabilityTable<CSoundType *> *pEntry = Table.SetAt(pTrack->GetPriority()); pEntry->Insert(pTrack, iChance); } // If the table is empty, then there is nothing to play. if (Table.GetCount() == 0) return NULL; // Otherwise, roll out of the first table. TProbabilityTable<CSoundType *> &Entry = Table[0]; return Entry.GetAt(Entry.RollPos()); }