RankInfo.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. ** Command & Conquer Generals(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. void RankInfoStore::init()
  39. {
  40. DEBUG_ASSERTCRASH(m_rankInfos.empty(), ("Hmm"));
  41. m_rankInfos.clear();
  42. }
  43. //-----------------------------------------------------------------------------
  44. void RankInfoStore::reset()
  45. {
  46. // nope.
  47. //m_rankInfos.clear();
  48. for (RankInfoVec::iterator it = m_rankInfos.begin(); it != m_rankInfos.end(); /*++it*/)
  49. {
  50. RankInfo* ri = *it;
  51. if (ri)
  52. {
  53. Overridable* temp = ri->deleteOverrides();
  54. if (!temp)
  55. {
  56. DEBUG_CRASH(("hmm, should not be possible for RankInfo"));
  57. it = m_rankInfos.erase(it);
  58. }
  59. else
  60. {
  61. ++it;
  62. }
  63. }
  64. }
  65. }
  66. //-----------------------------------------------------------------------------
  67. Int RankInfoStore::getRankLevelCount() const
  68. {
  69. return m_rankInfos.size();
  70. }
  71. //-----------------------------------------------------------------------------
  72. // note that level is 1...n, NOT 0...n-1
  73. const RankInfo* RankInfoStore::getRankInfo(Int level) const
  74. {
  75. if (level >= 1 && level <= getRankLevelCount())
  76. {
  77. const RankInfo* ri = m_rankInfos[level-1];
  78. if (ri)
  79. {
  80. return (const RankInfo*)ri->getFinalOverride();
  81. }
  82. }
  83. return NULL;
  84. }
  85. //-----------------------------------------------------------------------------
  86. void RankInfoStore::friend_parseRankDefinition( INI* ini )
  87. {
  88. if (TheRankInfoStore)
  89. {
  90. Int rank = INI::scanInt(ini->getNextToken());
  91. static const FieldParse myFieldParse[] =
  92. {
  93. { "RankName", INI::parseAndTranslateLabel, NULL, offsetof( RankInfo, m_rankName ) },
  94. { "SkillPointsNeeded", INI::parseInt, NULL, offsetof( RankInfo, m_skillPointsNeeded ) },
  95. { "SciencesGranted", INI::parseScienceVector, NULL, offsetof( RankInfo, m_sciencesGranted ) },
  96. { "SciencePurchasePointsGranted", INI::parseUnsignedInt, NULL, offsetof( RankInfo, m_sciencePurchasePointsGranted ) },
  97. { 0, 0, 0, 0 }
  98. };
  99. if (ini->getLoadType() == INI_LOAD_CREATE_OVERRIDES)
  100. {
  101. // we aren't allowed to add ranks in overrides, only to override existing ones.
  102. if (rank < 1 || rank > TheRankInfoStore->m_rankInfos.size())
  103. {
  104. DEBUG_CRASH(("Rank not found in map.ini"));
  105. throw INI_INVALID_DATA;
  106. }
  107. RankInfo* info = TheRankInfoStore->m_rankInfos[rank-1];
  108. if (!info)
  109. {
  110. DEBUG_CRASH(("Rank not found in map.ini"));
  111. throw INI_INVALID_DATA;
  112. }
  113. RankInfo* newInfo = newInstance(RankInfo);
  114. // copy data from final override to 'newInfo' as a set of initial default values
  115. info = (RankInfo*)(info->friend_getFinalOverride());
  116. *newInfo = *info;
  117. info->setNextOverride(newInfo);
  118. newInfo->markAsOverride(); // must do AFTER the copy
  119. ini->initFromINI(newInfo, myFieldParse);
  120. //TheRankInfoStore->m_rankInfos.push_back(newInfo); // NO, BAD, WRONG -- don't add in this case.
  121. }
  122. else
  123. {
  124. if (rank != TheRankInfoStore->m_rankInfos.size() + 1)
  125. {
  126. DEBUG_CRASH(("Ranks must increase monotonically"));
  127. throw INI_INVALID_DATA;
  128. }
  129. RankInfo* info = newInstance(RankInfo);
  130. ini->initFromINI(info, myFieldParse);
  131. TheRankInfoStore->m_rankInfos.push_back(info);
  132. }
  133. }
  134. }
  135. //-----------------------------------------------------------------------------
  136. void INI::parseRankDefinition( INI* ini )
  137. {
  138. RankInfoStore::friend_parseRankDefinition(ini);
  139. }