CDefObjInfoHandler.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "StdInc.h"
  2. #include "CDefObjInfoHandler.h"
  3. #include "filesystem/Filesystem.h"
  4. #include "../client/CGameInfo.h"
  5. #include "../lib/VCMI_Lib.h"
  6. #include "GameConstants.h"
  7. /*
  8. * CDefObjInfoHandler.cpp, part of VCMI engine
  9. *
  10. * Authors: listed in file AUTHORS in main folder
  11. *
  12. * License: GNU General Public License v2.0 or later
  13. * Full text of license available in license.txt file, in main folder
  14. *
  15. */
  16. bool CGDefInfo::isVisitable() const
  17. {
  18. for (auto & elem : visitMap)
  19. {
  20. if (elem)
  21. return true;
  22. }
  23. return false;
  24. }
  25. CGDefInfo::CGDefInfo()
  26. {
  27. visitDir = (8|16|32|64|128); //4,5,6,7,8 - any not-from-up direction
  28. width = height = -1;
  29. }
  30. void CGDefInfo::fetchInfoFromMSK()
  31. {
  32. ResourceID resID("SPRITES/" + name, EResType::MASK);
  33. if (CResourceHandler::get()->existsResource(resID))
  34. {
  35. auto msk = CResourceHandler::get()->load(resID)->readAll();
  36. width = msk.first.get()[0];
  37. height = msk.first.get()[1];
  38. for(int i=0; i<6; ++i)
  39. {
  40. coverageMap[i] = msk.first.get()[i+2];
  41. shadowCoverage[i] = msk.first.get()[i+8];
  42. }
  43. }
  44. else
  45. {
  46. //maximum possible size of H3 object
  47. //TODO: remove hardcode and move this data into modding system
  48. width = 8;
  49. height = 6;
  50. }
  51. }
  52. CDefObjInfoHandler::CDefObjInfoHandler()
  53. {
  54. VLC->dobjinfo = this;
  55. auto textFile = CResourceHandler::get()->load(ResourceID("DATA/OBJECTS.TXT"))->readAll();
  56. std::istringstream inp(std::string((char*)textFile.first.get(), textFile.second));
  57. int objNumber;
  58. inp>>objNumber;
  59. std::string mapStr;
  60. for(int hh=0; hh<objNumber; ++hh)
  61. {
  62. auto nobj = new CGDefInfo();
  63. std::string dump;
  64. inp>>nobj->name;
  65. std::transform(nobj->name.begin(), nobj->name.end(), nobj->name.begin(), (int(*)(int))toupper);
  66. for(int o=0; o<6; ++o)
  67. {
  68. nobj->blockMap[o] = 0xff;
  69. nobj->visitMap[o] = 0x00;
  70. nobj->coverageMap[o] = 0x00;
  71. nobj->shadowCoverage[o] = 0x00;
  72. }
  73. inp>>mapStr;
  74. std::reverse(mapStr.begin(), mapStr.end());
  75. for(int v=0; v<mapStr.size(); ++v)
  76. {
  77. if(mapStr[v]=='0')
  78. {
  79. nobj->blockMap[v/8] &= 255 - (128 >> (v%8));
  80. }
  81. }
  82. inp>>mapStr;
  83. std::reverse(mapStr.begin(), mapStr.end());
  84. for(int v=0; v<mapStr.size(); ++v)
  85. {
  86. if(mapStr[v]=='1')
  87. {
  88. nobj->visitMap[v/8] |= (128 >> (v%8));
  89. }
  90. }
  91. for(int yy=0; yy<2; ++yy) //first - on which types of terrain object can be placed;
  92. inp>>dump; //second -in which terrains' menus object in the editor will be available (?)
  93. si32 id; inp >> id; nobj->id = Obj(id);
  94. inp>>nobj->subid;
  95. inp>>nobj->type;
  96. nobj->visitDir = (8|16|32|64|128); //disabled visiting from the top
  97. if(nobj->type == 2 || nobj->type == 3 || nobj->type == 4 || nobj->type == 5) //creature, hero, artifact, resource
  98. {
  99. nobj->visitDir = 0xff;
  100. }
  101. else
  102. {
  103. static const Obj visitableFromTop[] =
  104. {Obj::FLOTSAM,
  105. Obj::SEA_CHEST,
  106. Obj::SHIPWRECK_SURVIVOR,
  107. Obj::BUOY,
  108. Obj::OCEAN_BOTTLE,
  109. Obj::BOAT,
  110. Obj::WHIRLPOOL,
  111. Obj::GARRISON,
  112. Obj::SCHOLAR,
  113. Obj::CAMPFIRE,
  114. Obj::BORDERGUARD,
  115. Obj::BORDER_GATE,
  116. Obj::QUEST_GUARD,
  117. Obj::CORPSE};
  118. for(auto & elem : visitableFromTop)
  119. {
  120. if(elem == nobj->id)
  121. {
  122. nobj->visitDir = 0xff;
  123. break;
  124. }
  125. }
  126. }
  127. inp >> nobj->printPriority;
  128. //coverageMap calculating
  129. nobj->fetchInfoFromMSK();
  130. auto dest = nobj->id.toDefObjInfo();
  131. if (dest.find(nobj->subid) != dest.end() && dest[nobj->subid] != nullptr)
  132. {
  133. // there is just too many of these. Note that this data is almost unused
  134. // exceptions are: town(village-capitol) and creation of new objects (holes, creatures, heroes, etc)
  135. //logGlobal->warnStream() << "Warning: overwriting def info for " << dest[nobj->subid]->name << " with " << nobj->name;
  136. dest[nobj->subid].dellNull(); // do not leak
  137. }
  138. nobj->id.toDefObjInfo()[nobj->subid] = nobj;
  139. }
  140. for (int i = 0; i < 8 ; i++)
  141. {
  142. static const char *holeDefs[] = {"AVLHOLD0.DEF", "AVLHLDS0.DEF", "AVLHOLG0.DEF", "AVLHLSN0.DEF",
  143. "AVLHOLS0.DEF", "AVLHOLR0.DEF", "AVLHOLX0.DEF", "AVLHOLL0.DEF"};
  144. if(i)
  145. {
  146. gobjs[Obj::HOLE][i] = new CGDefInfo(*gobjs[Obj::HOLE][0]);
  147. }
  148. gobjs[Obj::HOLE][i]->name = holeDefs[i];
  149. }
  150. }
  151. CDefObjInfoHandler::~CDefObjInfoHandler()
  152. {
  153. for(auto & elem : gobjs)
  154. for(auto j=elem.second.begin(); j!=elem.second.end(); j++)
  155. j->second.dellNull();
  156. }