Armor.cpp 5.6 KB

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