CvInfoCache.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // InfoCache 10/2019 lfgr
  2. #ifndef CVINFOCACHE_H_
  3. #define CVINFOCACHE_H_
  4. #pragma once
  5. #include "CvInfos.h"
  6. // enumLength function, adapted from Nightinggale's EnumMap
  7. // enumLength(x) returns the number of entries for the (enum) type of x. Note that x is not used.
  8. // ENUM_LENGTH(EnumType) is a macro where only the type has to be supplied
  9. #define ENUM_LENGTH(EnumType) enumLength( (EnumType) 0)
  10. // More to be added if necessary
  11. inline int enumLength( TraitTypes x ) { return GC.getNumTraitInfos(); }
  12. inline int enumLength( UnitAITypes x ) { return NUM_UNITAI_TYPES; }
  13. inline int enumLength( UnitCombatTypes x ) { return GC.getNumUnitCombatInfos(); }
  14. inline int enumLength( UnitTypes x ) { return GC.getNumUnitInfos(); }
  15. // Utility cache classes
  16. // Data structure mapping each KeyEnumType to some ValType
  17. // Must call init() exactly once after construction, after that the data structure doesn't change
  18. // Warning: does not support keys NO_XXX, i.e. -1
  19. template<class KeyEnumType, class ValType>
  20. class FixedEnumMap {
  21. public :
  22. FixedEnumMap() :
  23. m_aMap( NULL )
  24. {}
  25. inline void init( ValType calcFunc(KeyEnumType) ) {
  26. FAssertMsg( m_aMap == NULL, "FixedEnumMap already initialized!" );
  27. if( m_aMap != NULL ) {
  28. return;
  29. }
  30. m_aMap = new ValType[ENUM_LENGTH(KeyEnumType)];
  31. for( int i = 0; i < ENUM_LENGTH(KeyEnumType); i++ ) {
  32. m_aMap[i] = calcFunc( (KeyEnumType) i );
  33. }
  34. }
  35. ~FixedEnumMap() {
  36. SAFE_DELETE_ARRAY( m_aMap );
  37. }
  38. ValType at( const KeyEnumType e ) const {
  39. FAssert( m_aMap != NULL );
  40. FAssert( 0 <= e );
  41. FAssert( e <= ENUM_LENGTH(KeyEnumType) );
  42. return m_aMap[e];
  43. }
  44. private :
  45. ValType* m_aMap;
  46. };
  47. // Data structure mapping each pair of KeyEnumType1, KeyEnumType2 to some ValType
  48. // Must call init() exactly once after construction, after that the data structure doesn't change
  49. // Warning: does not support keys NO_XXX, i.e. -1
  50. template<class KeyEnumType1, class KeyEnumType2, class ValType>
  51. class Fixed2DEnumMap {
  52. public :
  53. Fixed2DEnumMap() :
  54. m_aMap( NULL )
  55. {}
  56. inline void init( ValType (*calcFunc)(KeyEnumType1, KeyEnumType2) ) {
  57. FAssertMsg( m_aMap == NULL, "Fixed2DEnumMap already initialized!" );
  58. if( m_aMap != NULL ) {
  59. return;
  60. }
  61. m_aMap = new ValType[ENUM_LENGTH(KeyEnumType1) * ENUM_LENGTH(KeyEnumType2)];
  62. for( int i = 0; i < ENUM_LENGTH(KeyEnumType1); i++ ) {
  63. for( int j = 0; j < ENUM_LENGTH(KeyEnumType2); j++ ) {
  64. m_aMap[i * ENUM_LENGTH(KeyEnumType2) + j] = calcFunc( (KeyEnumType1) i, (KeyEnumType2) j );
  65. }
  66. }
  67. }
  68. ~Fixed2DEnumMap() {
  69. SAFE_DELETE_ARRAY( m_aMap );
  70. }
  71. ValType at( const KeyEnumType1 i, const KeyEnumType2 j ) const {
  72. FAssert( m_aMap != NULL );
  73. FAssert( 0 <= i );
  74. FAssert( i < ENUM_LENGTH(KeyEnumType1) );
  75. FAssert( 0 <= j );
  76. FAssert( j < ENUM_LENGTH(KeyEnumType2) );
  77. return m_aMap[i * ENUM_LENGTH(KeyEnumType2) + j];
  78. }
  79. private :
  80. ValType* m_aMap;
  81. };
  82. class CvInfoCache {
  83. public :
  84. CvInfoCache();
  85. virtual ~CvInfoCache();
  86. void init(); // TODO: Call!
  87. int AI_getUnitValueFromTrait( UnitCombatTypes eUnitCombat, TraitTypes eTrait ) const;
  88. int AI_getUnitValueFromFreePromotions( UnitTypes eUnit, UnitAITypes eUnitAI ) const;
  89. bool AI_isUnitAmphib( UnitTypes eUnit ) const;
  90. private :
  91. Fixed2DEnumMap<UnitCombatTypes, TraitTypes, int> m_aiUnitValueFromTraitCache;
  92. Fixed2DEnumMap<UnitTypes, UnitAITypes, int> m_aiUnitValueFromFreePromotionsCache;
  93. FixedEnumMap<UnitTypes, bool> m_aiUnitAmphibCache;
  94. };
  95. void initInfoCache();
  96. CvInfoCache& getInfoCache();
  97. // AI calculations that are based solely on InfoTypes and do not depend on the game state
  98. namespace info_ai {
  99. int AI_calcUnitValueFromTrait( UnitCombatTypes eUnitCombat, TraitTypes eTrait );
  100. int AI_calcUnitValueFromFreePromotions( UnitTypes eUnit, UnitAITypes eUnitAI );
  101. bool AI_checkUnitAmphib( UnitTypes eUnit );
  102. }
  103. #endif /* CVINFOCACHE_H_ */