Armor.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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: ArmorTemplate.cpp ///////////////////////////////////////////////////////////////////////////////
  24. // Author: Steven Johnson, November 2001
  25. // Desc: ArmorTemplate descriptions
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  29. #define DEFINE_DAMAGE_NAMES // for DamageNames[]
  30. #include "Common/INI.h"
  31. #include "Common/ThingFactory.h"
  32. #include "GameLogic/Armor.h"
  33. #include "GameLogic/Damage.h"
  34. ///////////////////////////////////////////////////////////////////////////////////////////////////
  35. // PUBLIC DATA ////////////////////////////////////////////////////////////////////////////////////
  36. ///////////////////////////////////////////////////////////////////////////////////////////////////
  37. ArmorStore* TheArmorStore = NULL; ///< the ArmorTemplate store definition
  38. ///////////////////////////////////////////////////////////////////////////////////////////////////
  39. // PRIVATE DATA ///////////////////////////////////////////////////////////////////////////////////
  40. ///////////////////////////////////////////////////////////////////////////////////////////////////
  41. ///////////////////////////////////////////////////////////////////////////////////////////////////
  42. // PUBLIC FUNCTIONS ///////////////////////////////////////////////////////////////////////////////
  43. ///////////////////////////////////////////////////////////////////////////////////////////////////
  44. //-------------------------------------------------------------------------------------------------
  45. ArmorTemplate::ArmorTemplate()
  46. {
  47. clear();
  48. }
  49. //-------------------------------------------------------------------------------------------------
  50. void ArmorTemplate::clear()
  51. {
  52. for (int i = 0; i < DAMAGE_NUM_TYPES; i++)
  53. {
  54. m_damageCoefficient[i] = 1.0f;
  55. }
  56. }
  57. //-------------------------------------------------------------------------------------------------
  58. Real ArmorTemplate::adjustDamage(DamageType t, Real damage) const
  59. {
  60. if (t == DAMAGE_UNRESISTABLE)
  61. return damage;
  62. damage *= m_damageCoefficient[t];
  63. if (damage < 0.0f)
  64. damage = 0.0f;
  65. return damage;
  66. }
  67. //-------------------------------------------------------------------------------------------Static
  68. /*static*/ void ArmorTemplate::parseArmorCoefficients( INI* ini, void *instance, void* /* store */, const void* userData )
  69. {
  70. ArmorTemplate* self = (ArmorTemplate*) instance;
  71. const char* damageName = ini->getNextToken();
  72. Real pct = INI::scanPercentToReal(ini->getNextToken());
  73. if (stricmp(damageName, "Default") == 0)
  74. {
  75. for (Int i = 0; i < DAMAGE_NUM_TYPES; i++)
  76. {
  77. self->m_damageCoefficient[i] = pct;
  78. }
  79. return;
  80. }
  81. DamageType dt = (DamageType)INI::scanIndexList(damageName, TheDamageNames);
  82. self->m_damageCoefficient[dt] = pct;
  83. }
  84. //-------------------------------------------------------------------------------------------------
  85. //-------------------------------------------------------------------------------------------------
  86. //-------------------------------------------------------------------------------------------------
  87. //-------------------------------------------------------------------------------------------------
  88. ArmorStore::ArmorStore()
  89. {
  90. m_armorTemplates.clear();
  91. }
  92. //-------------------------------------------------------------------------------------------------
  93. ArmorStore::~ArmorStore()
  94. {
  95. m_armorTemplates.clear();
  96. }
  97. //-------------------------------------------------------------------------------------------------
  98. const ArmorTemplate* ArmorStore::findArmorTemplate(AsciiString name) const
  99. {
  100. NameKeyType namekey = TheNameKeyGenerator->nameToKey(name);
  101. ArmorTemplateMap::const_iterator it = m_armorTemplates.find(namekey);
  102. if (it == m_armorTemplates.end())
  103. {
  104. return NULL;
  105. }
  106. else
  107. {
  108. return &(*it).second;
  109. }
  110. }
  111. //-------------------------------------------------------------------------------------------------
  112. /*static */ void ArmorStore::parseArmorDefinition(INI *ini)
  113. {
  114. static const FieldParse myFieldParse[] =
  115. {
  116. { "Armor", ArmorTemplate::parseArmorCoefficients, NULL, 0 }
  117. };
  118. const char *c = ini->getNextToken();
  119. NameKeyType key = TheNameKeyGenerator->nameToKey(c);
  120. ArmorTemplate& armorTmpl = TheArmorStore->m_armorTemplates[key];
  121. armorTmpl.clear();
  122. ini->initFromINI(&armorTmpl, myFieldParse);
  123. }
  124. //-------------------------------------------------------------------------------------------------
  125. /*static*/ void INI::parseArmorDefinition(INI *ini)
  126. {
  127. ArmorStore::parseArmorDefinition(ini);
  128. }