CDefObjInfoHandler.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include "StdInc.h"
  2. #include "CDefObjInfoHandler.h"
  3. #include "filesystem/CResourceLoader.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 (int i=0; i<6; i++)
  19. {
  20. if (visitMap[i])
  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()->loadData(ResourceID(std::string("SPRITES/") + name, EResType::MASK));
  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. void CDefObjInfoHandler::load()
  42. {
  43. auto textFile = CResourceHandler::get()->loadData(ResourceID("DATA/ZOBJCTS.TXT"));
  44. std::istringstream inp(std::string((char*)textFile.first.get(), textFile.second));
  45. int objNumber;
  46. inp>>objNumber;
  47. std::string mapStr;
  48. for(int hh=0; hh<objNumber; ++hh)
  49. {
  50. CGDefInfo* nobj = new CGDefInfo();
  51. std::string dump;
  52. inp>>nobj->name;
  53. std::transform(nobj->name.begin(), nobj->name.end(), nobj->name.begin(), (int(*)(int))toupper);
  54. for(int o=0; o<6; ++o)
  55. {
  56. nobj->blockMap[o] = 0xff;
  57. nobj->visitMap[o] = 0x00;
  58. nobj->coverageMap[o] = 0x00;
  59. nobj->shadowCoverage[o] = 0x00;
  60. }
  61. inp>>mapStr;
  62. std::reverse(mapStr.begin(), mapStr.end());
  63. for(int v=0; v<mapStr.size(); ++v)
  64. {
  65. if(mapStr[v]=='0')
  66. {
  67. nobj->blockMap[v/8] &= 255 - (128 >> (v%8));
  68. }
  69. }
  70. inp>>mapStr;
  71. std::reverse(mapStr.begin(), mapStr.end());
  72. for(int v=0; v<mapStr.size(); ++v)
  73. {
  74. if(mapStr[v]=='1')
  75. {
  76. nobj->visitMap[v/8] |= (128 >> (v%8));
  77. }
  78. }
  79. for(int yy=0; yy<2; ++yy) //first - on which types of terrain object can be placed;
  80. inp>>dump; //second -in which terrains' menus object in the editor will be available (?)
  81. si32 id; inp >> id; nobj->id = Obj(id);
  82. inp>>nobj->subid;
  83. inp>>nobj->type;
  84. nobj->visitDir = (8|16|32|64|128); //disabled visiting from the top
  85. if(nobj->type == 2 || nobj->type == 3 || nobj->type == 4 || nobj->type == 5) //creature, hero, artifact, resource
  86. {
  87. nobj->visitDir = 0xff;
  88. }
  89. else
  90. {
  91. static const Obj visitableFromTop[] =
  92. {Obj::FLOTSAM,
  93. Obj::SEA_CHEST,
  94. Obj::SHIPWRECK_SURVIVOR,
  95. Obj::BUOY,
  96. Obj::OCEAN_BOTTLE,
  97. Obj::BOAT,
  98. Obj::WHIRLPOOL,
  99. Obj::GARRISON,
  100. Obj::SCHOLAR,
  101. Obj::CAMPFIRE,
  102. Obj::BORDERGUARD,
  103. Obj::BORDER_GATE,
  104. Obj::QUEST_GUARD,
  105. Obj::CORPSE};
  106. for(int i=0; i < ARRAY_COUNT(visitableFromTop); i++)
  107. {
  108. if(visitableFromTop[i] == nobj->id)
  109. {
  110. nobj->visitDir = 0xff;
  111. break;
  112. }
  113. }
  114. }
  115. inp >> nobj->printPriority;
  116. //coverageMap calculating
  117. nobj->fetchInfoFromMSK();
  118. nobj->id.toDefObjInfo()[nobj->subid] = nobj;
  119. }
  120. for (int i = 0; i < 8 ; i++)
  121. {
  122. static const char *holeDefs[] = {"AVLHOLD0.DEF", "AVLHLDS0.DEF", "AVLHOLG0.DEF", "AVLHLSN0.DEF",
  123. "AVLHOLS0.DEF", "AVLHOLR0.DEF", "AVLHOLX0.DEF", "AVLHOLL0.DEF"};
  124. if(i)
  125. {
  126. gobjs[Obj::HOLE][i] = new CGDefInfo(*gobjs[Obj::HOLE][0]);
  127. }
  128. gobjs[Obj::HOLE][i]->name = holeDefs[i];
  129. }
  130. }
  131. CDefObjInfoHandler::~CDefObjInfoHandler()
  132. {
  133. for(bmap<int,bmap<int, ConstTransitivePtr<CGDefInfo> > >::iterator i=gobjs.begin(); i!=gobjs.end(); i++)
  134. for(bmap<int, ConstTransitivePtr<CGDefInfo> >::iterator j=i->second.begin(); j!=i->second.end(); j++)
  135. j->second.dellNull();
  136. }