treasuresetigc.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. ** Copyright (C) 1996, 1997 Microsoft Corporation. All Rights Reserved.
  3. **
  4. ** File: treasureSetIGC.h
  5. **
  6. ** Author:
  7. **
  8. ** Description:
  9. ** Header for the CtreasureSetIGC class. This file was initially created by
  10. ** the ATL wizard.
  11. **
  12. ** History:
  13. */
  14. // treasureSetIGC.h : Declaration of the CtreasureSetIGC
  15. #ifndef __TREASURESETIGC_H_
  16. #define __TREASURESETIGC_H_
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CtreasureSetIGC
  19. class CtreasureSetIGC : public ItreasureSetIGC
  20. {
  21. public:
  22. CtreasureSetIGC(void)
  23. :
  24. m_data(NULL)
  25. {
  26. }
  27. ~CtreasureSetIGC(void)
  28. {
  29. delete [] (char*)m_data;
  30. }
  31. // IbaseIGC
  32. virtual HRESULT Initialize(ImissionIGC* pMission, Time now, const void* data, int dataSize);
  33. virtual void Terminate(void)
  34. {
  35. m_pMission->DeleteTreasureSet(this);
  36. }
  37. virtual int Export(void* data) const;
  38. virtual ObjectType GetObjectType(void) const
  39. {
  40. return OT_treasureSet;
  41. }
  42. virtual ObjectID GetObjectID(void) const
  43. {
  44. return m_data->treasureSetID;
  45. }
  46. // ItreasureSetIGC
  47. virtual const char* GetName(void) const
  48. {
  49. return m_data->name;
  50. }
  51. virtual bool GetZoneOnly(void) const
  52. {
  53. return m_data->bZoneOnly;
  54. }
  55. virtual void AddTreasureData(TreasureCode tc, ObjectID oid, unsigned char chance)
  56. {
  57. if (m_data->nTreasureData == m_maxTreasureData)
  58. {
  59. //Not enough space to add one ... reallocate a larger array
  60. int oldSize = sizeof(DataTreasureSetIGC) + sizeof(TreasureData) *
  61. m_maxTreasureData;
  62. const int c_increment = 10;
  63. int newSize = oldSize + sizeof(TreasureData) * c_increment;
  64. m_maxTreasureData += c_increment;
  65. DataTreasureSetIGC* newData = (DataTreasureSetIGC*)(new char [newSize]);
  66. memcpy(newData, m_data, oldSize);
  67. delete [] (char*)m_data;
  68. m_data = newData;
  69. }
  70. TreasureData* ptd = m_data->treasureData0() + m_data->nTreasureData;
  71. ptd->treasureCode = tc;
  72. ptd->treasureID = oid;
  73. ptd->chance = chance;
  74. m_totalTreasureChance += chance;
  75. m_data->nTreasureData++;
  76. }
  77. virtual const TreasureData& GetRandomTreasureData(void) const
  78. {
  79. assert (m_totalTreasureChance >= 1);
  80. int r = randomInt(1, m_totalTreasureChance);
  81. TreasureData* ptd = m_data->treasureData0();
  82. while (true)
  83. {
  84. r -= ptd->chance;
  85. if (r <= 0)
  86. return *ptd;
  87. ptd++;
  88. assert (ptd < (m_data->treasureData0() + m_data->nTreasureData));
  89. }
  90. }
  91. private:
  92. ImissionIGC* m_pMission;
  93. DataTreasureSetIGC* m_data;
  94. short m_totalTreasureChance;
  95. short m_maxTreasureData;
  96. };
  97. #endif //__TREASURESETIGC_H_