Anq_CivFanatics

packed array?

Jun 8th, 2019
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Design:
  2.  Arr[0] [1] [2] [3] ... [N]  => len(Array)=N+1
  3. Count=N 1st 2nd 3rd ... Nth
  4. Get(-1) (0) (1) (2) ...(N-1) => for (i=0;i<p->Get(-1);i++)  p->Get(i) [...];
  5. Count=N 1st 2nd 3rd ... Nth     Appears like a normal array.
  6.  
  7. ======================================================
  8. CvBuildingInfo::getPrereqOrBonuses()
  9. ======================================================
  10. // returns array length when called with -1
  11. int CvBuildingInfo::getPrereqOrBonuses(int i) const    
  12. {
  13.     FAssertMsg(i < GC.getNUM_BUILDING_PREREQ_OR_BONUSES(), "Index out of bounds");
  14.     if (m_piPrereqOrBonuses)
  15.     {
  16.         int size = m_piPrereqOrBonuses[0];
  17.         if (i == -1)    return size;
  18.         if (i < size)   return m_piPrereqOrBonuses[i+1];
  19.         return -1;
  20.     }
  21.     return -1;
  22. }
  23.  
  24. ======================================================
  25. CvBuildingInfo::getCheckSum()
  26. ======================================================
  27.     if (m_piPrereqOrBonuses)    CheckSumI(iSum, m_piPrereqOrBonuses[0]+1, m_piPrereqOrBonuses);
  28.  
  29.  
  30. ======================================================
  31. CvBuildingInfo::read()
  32. ======================================================
  33.     if (pXML->TryMoveToXmlFirstChild(L"PrereqBonuses"))
  34.     {
  35.         iNumChildren = pXML->GetXmlChildrenNumber();
  36.         FAssertMsg((iNumChildren <= GC.getNUM_BUILDING_PREREQ_OR_BONUSES()),"Too many Prereq Or Bonuses for the building.");
  37.         int *tmp = new int[iNumChildren];
  38.         int iTotal = 0;
  39.         if (0 < iNumChildren)
  40.         {
  41.             if (pXML->GetChildXmlVal(szTextVal))
  42.             {
  43.                 for (j=0; j<iNumChildren; j++)
  44.                 {
  45.                     int eInfo = pXML->GetInfoClass(szTextVal);
  46.                     if (eInfo != -1)    tmp[iTotal++] = eInfo;
  47.                     if (!pXML->GetNextXmlVal(szTextVal))    break;
  48.                 }
  49.                 if (iTotal != 0)
  50.                 {
  51.                     m_piPrereqOrBonuses = new int[iTotal+1];
  52.                     m_piPrereqOrBonuses[0] = iTotal;
  53.                     for (j=0; j<iTotal; j++)    m_piPrereqOrBonuses[j+1] = tmp[j];
  54.                 }
  55.                 pXML->MoveToXmlParent();
  56.             }
  57.         }
  58.         else    SAFE_DELETE_ARRAY(m_piPrereqOrBonuses);
  59.         pXML->MoveToXmlParent();
  60.     }
  61.     else    SAFE_DELETE_ARRAY(m_piPrereqOrBonuses);
  62.  
  63. ======================================================
  64. CvBuildingInfo::CopyNonDefaults()
  65. ======================================================
  66.     int iTotalTheirs = pClassInfo->getPrereqOrBonuses(-1);
  67.     if (iTotalTheirs != -1)
  68.     {
  69.         int iTotalOurs = 0;
  70.         if (m_piPrereqOrBonuses)    iTotalOurs = m_piPrereqOrBonuses[0];
  71.         int* tmp = new int[iTotalTheirs+iTotalOurs+1];
  72.         tmp[0] = iTotalTheirs+iTotalOurs;
  73.         for (int j=0; j<iTotalTheirs; j++)
  74.             tmp[j+1] = pClassInfo->getPrereqOrBonuses(j);
  75.         for (int j=0; j<iTotalOurs; j++)
  76.             tmp[j+iTotalTheirs+1] = m_piPrereqOrBonuses[j+1];
  77.         m_piPrereqOrBonuses = tmp;
  78.     }
  79.  
  80. ======================================================
  81. Take Two
  82. ======================================================
  83. // Macro for concatenating two packed arrays in CopyNonDefaults()
  84. #define PACKED_ARRAY_COPYNONDEFAULTS(theirFn, ourArr) if (theirFn(-1) != -1) {      \
  85.     int theirSize = theirFn(-1), ourSize = 0; if (ourArr)   ourSize = ourArr[0];    \
  86.     int* tmp = new int[theirSize+ourSize+1]; tmp[0] = theirSize+ourSize;            \
  87.     for (int i=0; i<theirSize; i++) tmp[i+1] = theirFn(i);                          \
  88.     for (int i=0; i<ourSize; i++)   tmp[i+theirSize+1] = ourArr[i+1]; ourArr = tmp; }
  89.  
  90. void CvBuildingInfo::copyNonDefaults(CvBuildingInfo* pClassInfo, CvXMLLoadUtility* pXML)
  91. { [...]
  92.     PACKED_ARRAY_COPYNONDEFAULTS(pClassInfo->getPrereqAndTechs, m_piPrereqAndTechs);
  93.  
  94.     if (getPrereqAndBonus() == NO_BONUS) m_iPrereqAndBonus = pClassInfo->getPrereqAndBonus();
  95.     PACKED_ARRAY_COPYNONDEFAULTS(pClassInfo->getPrereqOrBonuses, m_piPrereqOrBonuses);
  96. [...] }
  97.  
  98.  
  99. ======================================================
  100. CvCity::CanConstructInternal()
  101. This modification applies in four other places:
  102.     CvGameTextMgr::buildBuildingRequiresString(),
  103.     CvPlayer::recalculateResourceConsumption(),
  104.     CvPlayerAI::AI_bonusTrade(), and
  105.     CvPlayerAI::AI_baseBonusVal().
  106. ======================================================
  107.             for (iI = 0; iI < kBuilding.getPrereqOrBonuses(-1); iI++) // was "iI < GC.getNUM_BUILDING_PREREQ_OR_BONUSES()"
  108.             {
  109.                 if (kBuilding.getPrereqOrBonuses(iI) != NO_BONUS)
  110.                 {
  111.                     bRequiresBonus = true;
  112.  
  113.                     if (hasBonus((BonusTypes)kBuilding.getPrereqOrBonuses(iI)))
  114.                     {
  115.                         bNeedsBonus = false;
  116.                     }
  117.                 }
  118.             }
Advertisement
Add Comment
Please, Sign In to add comment