RankInfo.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: RankInfo.cpp /////////////////////////////////////////////////////////
  24. // Created: Steven Johnson, Sep 2002
  25. // Desc:
  26. //-----------------------------------------------------------------------------
  27. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  28. #include "Common/INI.h"
  29. #include "Common/Player.h"
  30. #include "GameLogic/RankInfo.h"
  31. RankInfoStore* TheRankInfoStore = NULL;
  32. #ifdef _INTERNAL
  33. // for occasional debugging...
  34. //#pragma optimize("", off)
  35. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  36. #endif
  37. //-----------------------------------------------------------------------------
  38. RankInfo::~RankInfo()
  39. {
  40. }
  41. //-----------------------------------------------------------------------------
  42. RankInfoStore::~RankInfoStore()
  43. {
  44. Int level;
  45. for (level =0; level < getRankLevelCount(); level++)
  46. {
  47. RankInfo* ri = m_rankInfos[level];
  48. if (ri)
  49. {
  50. ri->deleteInstance();
  51. }
  52. }
  53. m_rankInfos.clear();
  54. }
  55. //-----------------------------------------------------------------------------
  56. void RankInfoStore::init()
  57. {
  58. DEBUG_ASSERTCRASH(m_rankInfos.empty(), ("Hmm"));
  59. m_rankInfos.clear();
  60. }
  61. //-----------------------------------------------------------------------------
  62. void RankInfoStore::reset()
  63. {
  64. // nope.
  65. //m_rankInfos.clear();
  66. for (RankInfoVec::iterator it = m_rankInfos.begin(); it != m_rankInfos.end(); /*++it*/)
  67. {
  68. RankInfo* ri = *it;
  69. if (ri)
  70. {
  71. Overridable* temp = ri->deleteOverrides();
  72. if (!temp)
  73. {
  74. DEBUG_CRASH(("hmm, should not be possible for RankInfo"));
  75. it = m_rankInfos.erase(it);
  76. }
  77. else
  78. {
  79. ++it;
  80. }
  81. }
  82. }
  83. }
  84. //-----------------------------------------------------------------------------
  85. Int RankInfoStore::getRankLevelCount() const
  86. {
  87. return m_rankInfos.size();
  88. }
  89. //-----------------------------------------------------------------------------
  90. // note that level is 1...n, NOT 0...n-1
  91. const RankInfo* RankInfoStore::getRankInfo(Int level) const
  92. {
  93. if (level >= 1 && level <= getRankLevelCount())
  94. {
  95. const RankInfo* ri = m_rankInfos[level-1];
  96. if (ri)
  97. {
  98. return (const RankInfo*)ri->getFinalOverride();
  99. }
  100. }
  101. return NULL;
  102. }
  103. //-----------------------------------------------------------------------------
  104. void RankInfoStore::friend_parseRankDefinition( INI* ini )
  105. {
  106. if (TheRankInfoStore)
  107. {
  108. Int rank = INI::scanInt(ini->getNextToken());
  109. static const FieldParse myFieldParse[] =
  110. {
  111. { "RankName", INI::parseAndTranslateLabel, NULL, offsetof( RankInfo, m_rankName ) },
  112. { "SkillPointsNeeded", INI::parseInt, NULL, offsetof( RankInfo, m_skillPointsNeeded ) },
  113. { "SciencesGranted", INI::parseScienceVector, NULL, offsetof( RankInfo, m_sciencesGranted ) },
  114. { "SciencePurchasePointsGranted", INI::parseUnsignedInt, NULL, offsetof( RankInfo, m_sciencePurchasePointsGranted ) },
  115. { 0, 0, 0, 0 }
  116. };
  117. if (ini->getLoadType() == INI_LOAD_CREATE_OVERRIDES)
  118. {
  119. // we aren't allowed to add ranks in overrides, only to override existing ones.
  120. if (rank < 1 || rank > TheRankInfoStore->m_rankInfos.size())
  121. {
  122. DEBUG_CRASH(("Rank not found in map.ini"));
  123. throw INI_INVALID_DATA;
  124. }
  125. RankInfo* info = TheRankInfoStore->m_rankInfos[rank-1];
  126. if (!info)
  127. {
  128. DEBUG_CRASH(("Rank not found in map.ini"));
  129. throw INI_INVALID_DATA;
  130. }
  131. RankInfo* newInfo = newInstance(RankInfo);
  132. // copy data from final override to 'newInfo' as a set of initial default values
  133. info = (RankInfo*)(info->friend_getFinalOverride());
  134. *newInfo = *info;
  135. info->setNextOverride(newInfo);
  136. newInfo->markAsOverride(); // must do AFTER the copy
  137. ini->initFromINI(newInfo, myFieldParse);
  138. //TheRankInfoStore->m_rankInfos.push_back(newInfo); // NO, BAD, WRONG -- don't add in this case.
  139. }
  140. else
  141. {
  142. if (rank != TheRankInfoStore->m_rankInfos.size() + 1)
  143. {
  144. DEBUG_CRASH(("Ranks must increase monotonically"));
  145. throw INI_INVALID_DATA;
  146. }
  147. RankInfo* info = newInstance(RankInfo);
  148. ini->initFromINI(info, myFieldParse);
  149. TheRankInfoStore->m_rankInfos.push_back(info);
  150. }
  151. }
  152. }
  153. //-----------------------------------------------------------------------------
  154. void INI::parseRankDefinition( INI* ini )
  155. {
  156. RankInfoStore::friend_parseRankDefinition(ini);
  157. }