Science.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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: Science.cpp /////////////////////////////////////////////////////////
  24. // Created: Steven Johnson, October 2001
  25. // Desc: @todo
  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 "Common/Science.h"
  31. ScienceStore* TheScienceStore = 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 ScienceStore::init()
  39. {
  40. DEBUG_ASSERTCRASH(m_sciences.empty(), ("Hmm"));
  41. m_sciences.clear();
  42. }
  43. //-----------------------------------------------------------------------------
  44. ScienceStore::~ScienceStore()
  45. {
  46. // nope.
  47. //m_sciences.clear();
  48. // go through all sciences and delete any overrides
  49. for (ScienceInfoVec::iterator it = m_sciences.begin(); it != m_sciences.end(); /*++it*/)
  50. {
  51. ScienceInfo* si = *it;
  52. ++it;
  53. if (si) {
  54. si->deleteInstance();
  55. }
  56. }
  57. }
  58. //-----------------------------------------------------------------------------
  59. void ScienceStore::reset()
  60. {
  61. // nope.
  62. //m_sciences.clear();
  63. // go through all sciences and delete any overrides
  64. for (ScienceInfoVec::iterator it = m_sciences.begin(); it != m_sciences.end(); /*++it*/)
  65. {
  66. ScienceInfo* si = *it;
  67. Overridable* temp = si->deleteOverrides();
  68. if (!temp)
  69. {
  70. it = m_sciences.erase(it);
  71. }
  72. else
  73. {
  74. ++it;
  75. }
  76. }
  77. }
  78. //-----------------------------------------------------------------------------
  79. ScienceType ScienceStore::getScienceFromInternalName(const AsciiString& name) const
  80. {
  81. if (name.isEmpty())
  82. return SCIENCE_INVALID;
  83. NameKeyType nkt = TheNameKeyGenerator->nameToKey(name);
  84. ScienceType st = (ScienceType)nkt;
  85. return st;
  86. }
  87. //-----------------------------------------------------------------------------
  88. AsciiString ScienceStore::getInternalNameForScience(ScienceType science) const
  89. {
  90. if (science == SCIENCE_INVALID)
  91. return AsciiString::TheEmptyString;
  92. NameKeyType nk = (NameKeyType)(science);
  93. return TheNameKeyGenerator->keyToName(nk);
  94. }
  95. //-----------------------------------------------------------------------------
  96. // return a vector of all the currently-known science names
  97. // NOTE: this is really only for use by WorldBuilder! Please
  98. // do not use it in RTS!
  99. std::vector<AsciiString> ScienceStore::friend_getScienceNames() const
  100. {
  101. std::vector<AsciiString> v;
  102. for (ScienceInfoVec::const_iterator it = m_sciences.begin(); it != m_sciences.end(); ++it)
  103. {
  104. const ScienceInfo* si = (const ScienceInfo*)(*it)->getFinalOverride();
  105. NameKeyType nk = (NameKeyType)(si->m_science);
  106. v.push_back(TheNameKeyGenerator->keyToName(nk));
  107. }
  108. return v;
  109. }
  110. //-----------------------------------------------------------------------------
  111. void ScienceInfo::addRootSciences(ScienceVec& v) const
  112. {
  113. if (m_prereqSciences.empty())
  114. {
  115. // we're a root. add ourselves.
  116. if (std::find(v.begin(), v.end(), m_science) == v.end())
  117. v.push_back(m_science);
  118. }
  119. else
  120. {
  121. // we're not a root. add the roots of all our prereqs.
  122. for (ScienceVec::const_iterator it = m_prereqSciences.begin(); it != m_prereqSciences.end(); ++it)
  123. {
  124. const ScienceInfo* si = TheScienceStore->findScienceInfo(*it);
  125. if (si)
  126. si->addRootSciences(v);
  127. }
  128. }
  129. }
  130. //-------------------------------------------------------------------------------------------------
  131. //-------------------------------------------------------------------------------------------------
  132. const ScienceInfo* ScienceStore::findScienceInfo(ScienceType st) const
  133. {
  134. for (ScienceInfoVec::const_iterator it = m_sciences.begin(); it != m_sciences.end(); ++it)
  135. {
  136. const ScienceInfo* si = (const ScienceInfo*)(*it)->getFinalOverride();
  137. if (si->m_science == st)
  138. {
  139. return si;
  140. }
  141. }
  142. return NULL;
  143. }
  144. //-----------------------------------------------------------------------------
  145. /*static*/ void ScienceStore::friend_parseScienceDefinition( INI* ini )
  146. {
  147. const char* c = ini->getNextToken();
  148. NameKeyType nkt = NAMEKEY(c);
  149. ScienceType st = (ScienceType)nkt;
  150. if (TheScienceStore)
  151. {
  152. static const FieldParse myFieldParse[] =
  153. {
  154. { "PrerequisiteSciences", INI::parseScienceVector, NULL, offsetof( ScienceInfo, m_prereqSciences ) },
  155. { "SciencePurchasePointCost", INI::parseInt, NULL, offsetof( ScienceInfo, m_sciencePurchasePointCost ) },
  156. { "IsGrantable", INI::parseBool, NULL, offsetof( ScienceInfo, m_grantable ) },
  157. { "DisplayName", INI::parseAndTranslateLabel, NULL, offsetof( ScienceInfo, m_name) },
  158. { "Description", INI::parseAndTranslateLabel, NULL, offsetof( ScienceInfo, m_description) },
  159. { 0, 0, 0, 0 }
  160. };
  161. ScienceInfo* info = NULL;
  162. // see if the science already exists. (can't use findScienceInfo() since it is const and should remain so.)
  163. for (ScienceInfoVec::iterator it = TheScienceStore->m_sciences.begin(); it != TheScienceStore->m_sciences.end(); ++it)
  164. {
  165. // note that we don't use getFinalOverride here. this is correct and as-desired.
  166. if ((*it)->m_science == st)
  167. {
  168. info = *it;
  169. break;
  170. }
  171. }
  172. if (ini->getLoadType() == INI_LOAD_CREATE_OVERRIDES)
  173. {
  174. ScienceInfo* newInfo = newInstance(ScienceInfo);
  175. if (info == NULL)
  176. {
  177. // only add if it's not overriding an existing one.
  178. info = newInfo;
  179. info->markAsOverride(); // yep, so we will get cleared on reset()
  180. TheScienceStore->m_sciences.push_back(info);
  181. }
  182. else
  183. {
  184. // copy data from final override to 'newInfo' as a set of initial default values
  185. info = (ScienceInfo*)(info->friend_getFinalOverride());
  186. *newInfo = *info;
  187. info->setNextOverride(newInfo);
  188. newInfo->markAsOverride(); // must do AFTER the copy
  189. // use the newly created override for us to set values with etc
  190. info = newInfo;
  191. //TheScienceStore->m_sciences.push_back(info); // NO, BAD, WRONG -- don't add in this case.
  192. }
  193. }
  194. else
  195. {
  196. if (info != NULL)
  197. {
  198. DEBUG_CRASH(("duplicate science %s!\n",c));
  199. throw INI_INVALID_DATA;
  200. }
  201. info = newInstance(ScienceInfo);
  202. TheScienceStore->m_sciences.push_back(info);
  203. }
  204. ini->initFromINI(info, myFieldParse);
  205. info->m_science = st;
  206. info->addRootSciences(info->m_rootSciences);
  207. }
  208. }
  209. //-----------------------------------------------------------------------------
  210. Int ScienceStore::getSciencePurchaseCost(ScienceType st) const
  211. {
  212. const ScienceInfo* si = findScienceInfo(st);
  213. if (si)
  214. {
  215. return si->m_sciencePurchasePointCost;
  216. }
  217. else
  218. {
  219. return 0;
  220. }
  221. }
  222. //-----------------------------------------------------------------------------
  223. Bool ScienceStore::isScienceGrantable(ScienceType st) const
  224. {
  225. const ScienceInfo* si = findScienceInfo(st);
  226. if (si)
  227. {
  228. return si->m_grantable;
  229. }
  230. else
  231. {
  232. return false;
  233. }
  234. }
  235. //-----------------------------------------------------------------------------
  236. Bool ScienceStore::getNameAndDescription(ScienceType st, UnicodeString& name, UnicodeString& description) const
  237. {
  238. const ScienceInfo* si = findScienceInfo(st);
  239. if (si)
  240. {
  241. name = si->m_name;
  242. description = si->m_description;
  243. return true;
  244. }
  245. else
  246. {
  247. return false;
  248. }
  249. }
  250. //-----------------------------------------------------------------------------
  251. Bool ScienceStore::playerHasPrereqsForScience(const Player* player, ScienceType st) const
  252. {
  253. const ScienceInfo* si = findScienceInfo(st);
  254. if (si)
  255. {
  256. for (ScienceVec::const_iterator it2 = si->m_prereqSciences.begin(); it2 != si->m_prereqSciences.end(); ++it2)
  257. {
  258. if (!player->hasScience(*it2))
  259. {
  260. return false;
  261. }
  262. }
  263. return true;
  264. }
  265. else
  266. {
  267. return false;
  268. }
  269. }
  270. //-----------------------------------------------------------------------------
  271. Bool ScienceStore::playerHasRootPrereqsForScience(const Player* player, ScienceType st) const
  272. {
  273. const ScienceInfo* si = findScienceInfo(st);
  274. if (si)
  275. {
  276. for (ScienceVec::const_iterator it2 = si->m_rootSciences.begin(); it2 != si->m_rootSciences.end(); ++it2)
  277. {
  278. if (!player->hasScience(*it2))
  279. {
  280. return false;
  281. }
  282. }
  283. return true;
  284. }
  285. else
  286. {
  287. return false;
  288. }
  289. }
  290. //-----------------------------------------------------------------------------
  291. /** return a list of the sciences the given player can purchase now, and a list he might be able to purchase in the future,
  292. but currently lacks prereqs or points for. (either might be an empty list) */
  293. void ScienceStore::getPurchasableSciences(const Player* player, ScienceVec& purchasable, ScienceVec& potentiallyPurchasable) const
  294. {
  295. purchasable.clear();
  296. potentiallyPurchasable.clear();
  297. for (ScienceInfoVec::const_iterator it = m_sciences.begin(); it != m_sciences.end(); ++it)
  298. {
  299. const ScienceInfo* si = (const ScienceInfo*)(*it)->getFinalOverride();
  300. if (si->m_sciencePurchasePointCost == 0)
  301. {
  302. // 0 means "cannot be purchased"
  303. continue;
  304. }
  305. if (player->hasScience(si->m_science))
  306. {
  307. continue;
  308. }
  309. if (playerHasPrereqsForScience(player, si->m_science))
  310. {
  311. purchasable.push_back(si->m_science);
  312. }
  313. else if (playerHasRootPrereqsForScience(player, si->m_science))
  314. {
  315. potentiallyPurchasable.push_back(si->m_science);
  316. }
  317. }
  318. }
  319. //-----------------------------------------------------------------------------
  320. // this is intended ONLY for use by INI::scanScience.
  321. // Don't use it anywhere else. In particular, never, ever, ever
  322. // call this with a hardcoded science name. (srj)
  323. ScienceType ScienceStore::friend_lookupScience(const char* scienceName) const
  324. {
  325. NameKeyType nkt = NAMEKEY(scienceName);
  326. ScienceType st = (ScienceType)nkt;
  327. if (!isValidScience(st))
  328. {
  329. DEBUG_CRASH(("Science name %s not known! (Did you define it in Science.ini?)",scienceName));
  330. throw INI_INVALID_DATA;
  331. }
  332. return st;
  333. }
  334. //-----------------------------------------------------------------------------
  335. Bool ScienceStore::isValidScience(ScienceType st) const
  336. {
  337. const ScienceInfo* si = findScienceInfo(st);
  338. return si != NULL;
  339. }
  340. //-----------------------------------------------------------------------------
  341. void INI::parseScienceDefinition( INI* ini )
  342. {
  343. ScienceStore::friend_parseScienceDefinition(ini);
  344. }