Science.cpp 11 KB

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