CDefObjInfoHandler.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. auto msk = CResourceHandler::get()->load(ResourceID(std::string("SPRITES/") + name, EResType::MASK))->readAll();
  33. width = msk.first.get()[0];
  34. height = msk.first.get()[1];
  35. for(int i=0; i<6; ++i)
  36. {
  37. coverageMap[i] = msk.first.get()[i+2];
  38. shadowCoverage[i] = msk.first.get()[i+8];
  39. }
  40. }
  41. CDefObjInfoHandler::CDefObjInfoHandler()
  42. {
  43. VLC->dobjinfo = this;
  44. auto textFile = CResourceHandler::get()->load(ResourceID("DATA/OBJECTS.TXT"))->readAll();
  45. std::istringstream inp(std::string((char*)textFile.first.get(), textFile.second));
  46. int objNumber;
  47. inp>>objNumber;
  48. std::string mapStr;
  49. for(int hh=0; hh<objNumber; ++hh)
  50. {
  51. auto nobj = new CGDefInfo();
  52. std::string dump;
  53. inp>>nobj->name;
  54. std::transform(nobj->name.begin(), nobj->name.end(), nobj->name.begin(), (int(*)(int))toupper);
  55. for(int o=0; o<6; ++o)
  56. {
  57. nobj->blockMap[o] = 0xff;
  58. nobj->visitMap[o] = 0x00;
  59. nobj->coverageMap[o] = 0x00;
  60. nobj->shadowCoverage[o] = 0x00;
  61. }
  62. inp>>mapStr;
  63. std::reverse(mapStr.begin(), mapStr.end());
  64. for(int v=0; v<mapStr.size(); ++v)
  65. {
  66. if(mapStr[v]=='0')
  67. {
  68. nobj->blockMap[v/8] &= 255 - (128 >> (v%8));
  69. }
  70. }
  71. inp>>mapStr;
  72. std::reverse(mapStr.begin(), mapStr.end());
  73. for(int v=0; v<mapStr.size(); ++v)
  74. {
  75. if(mapStr[v]=='1')
  76. {
  77. nobj->visitMap[v/8] |= (128 >> (v%8));
  78. }
  79. }
  80. for(int yy=0; yy<2; ++yy) //first - on which types of terrain object can be placed;
  81. inp>>dump; //second -in which terrains' menus object in the editor will be available (?)
  82. si32 id; inp >> id; nobj->id = Obj(id);
  83. inp>>nobj->subid;
  84. inp>>nobj->type;
  85. nobj->visitDir = (8|16|32|64|128); //disabled visiting from the top
  86. if(nobj->type == 2 || nobj->type == 3 || nobj->type == 4 || nobj->type == 5) //creature, hero, artifact, resource
  87. {
  88. nobj->visitDir = 0xff;
  89. }
  90. else
  91. {
  92. static const Obj visitableFromTop[] =
  93. {Obj::FLOTSAM,
  94. Obj::SEA_CHEST,
  95. Obj::SHIPWRECK_SURVIVOR,
  96. Obj::BUOY,
  97. Obj::OCEAN_BOTTLE,
  98. Obj::BOAT,
  99. Obj::WHIRLPOOL,
  100. Obj::GARRISON,
  101. Obj::SCHOLAR,
  102. Obj::CAMPFIRE,
  103. Obj::BORDERGUARD,
  104. Obj::BORDER_GATE,
  105. Obj::QUEST_GUARD,
  106. Obj::CORPSE};
  107. for(auto & elem : visitableFromTop)
  108. {
  109. if(elem == nobj->id)
  110. {
  111. nobj->visitDir = 0xff;
  112. break;
  113. }
  114. }
  115. }
  116. inp >> nobj->printPriority;
  117. //coverageMap calculating
  118. nobj->fetchInfoFromMSK();
  119. auto dest = nobj->id.toDefObjInfo();
  120. if (dest.find(nobj->subid) != dest.end() && dest[nobj->subid] != nullptr)
  121. {
  122. // there is just too many of these. Note that this data is almost unused
  123. // exceptions are: town(village-capitol) and creation of new objects (holes, creatures, heroes, etc)
  124. //logGlobal->warnStream() << "Warning: overwriting def info for " << dest[nobj->subid]->name << " with " << nobj->name;
  125. dest[nobj->subid].dellNull(); // do not leak
  126. }
  127. nobj->id.toDefObjInfo()[nobj->subid] = nobj;
  128. }
  129. for (int i = 0; i < 8 ; i++)
  130. {
  131. static const char *holeDefs[] = {"AVLHOLD0.DEF", "AVLHLDS0.DEF", "AVLHOLG0.DEF", "AVLHLSN0.DEF",
  132. "AVLHOLS0.DEF", "AVLHOLR0.DEF", "AVLHOLX0.DEF", "AVLHOLL0.DEF"};
  133. if(i)
  134. {
  135. gobjs[Obj::HOLE][i] = new CGDefInfo(*gobjs[Obj::HOLE][0]);
  136. }
  137. gobjs[Obj::HOLE][i]->name = holeDefs[i];
  138. }
  139. }
  140. CDefObjInfoHandler::~CDefObjInfoHandler()
  141. {
  142. for(auto & elem : gobjs)
  143. for(auto j=elem.second.begin(); j!=elem.second.end(); j++)
  144. j->second.dellNull();
  145. }