ObjectTemplate.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. #include "StdInc.h"
  2. #include "CObjectClassesHandler.h"
  3. #include "../filesystem/Filesystem.h"
  4. #include "../filesystem/CBinaryReader.h"
  5. #include "../VCMI_Lib.h"
  6. #include "../GameConstants.h"
  7. #include "../StringConstants.h"
  8. #include "../CGeneralTextHandler.h"
  9. #include "CObjectHandler.h"
  10. #include "../CModHandler.h"
  11. #include "../JsonNode.h"
  12. #include "CRewardableConstructor.h"
  13. /*
  14. * ObjectTemplate.cpp, part of VCMI engine
  15. *
  16. * Authors: listed in file AUTHORS in main folder
  17. *
  18. * License: GNU General Public License v2.0 or later
  19. * Full text of license available in license.txt file, in main folder
  20. *
  21. */
  22. static bool isOnVisitableFromTopList(int identifier, int type)
  23. {
  24. if(type == 2 || type == 3 || type == 4 || type == 5) //creature, hero, artifact, resource
  25. return true;
  26. static const Obj visitableFromTop[] =
  27. {Obj::FLOTSAM,
  28. Obj::SEA_CHEST,
  29. Obj::SHIPWRECK_SURVIVOR,
  30. Obj::BUOY,
  31. Obj::OCEAN_BOTTLE,
  32. Obj::BOAT,
  33. Obj::WHIRLPOOL,
  34. Obj::GARRISON,
  35. Obj::GARRISON2,
  36. Obj::SCHOLAR,
  37. Obj::CAMPFIRE,
  38. Obj::BORDERGUARD,
  39. Obj::BORDER_GATE,
  40. Obj::QUEST_GUARD,
  41. Obj::CORPSE
  42. };
  43. if (vstd::find_pos(visitableFromTop, identifier) != -1)
  44. return true;
  45. return false;
  46. }
  47. ObjectTemplate::ObjectTemplate():
  48. visitDir(8|16|32|64|128), // all but top
  49. id(Obj::NO_OBJ),
  50. subid(0),
  51. printPriority(0)
  52. {
  53. }
  54. void ObjectTemplate::readTxt(CLegacyConfigParser & parser)
  55. {
  56. std::string data = parser.readString();
  57. std::vector<std::string> strings;
  58. boost::split(strings, data, boost::is_any_of(" "));
  59. assert(strings.size() == 9);
  60. animationFile = strings[0];
  61. stringID = strings[0];
  62. std::string & blockStr = strings[1]; //block map, 0 = blocked, 1 = unblocked
  63. std::string & visitStr = strings[2]; //visit map, 1 = visitable, 0 = not visitable
  64. assert(blockStr.size() == 6*8);
  65. assert(visitStr.size() == 6*8);
  66. setSize(8, 6);
  67. for (size_t i=0; i<6; i++) // 6 rows
  68. {
  69. for (size_t j=0; j<8; j++) // 8 columns
  70. {
  71. auto & tile = usedTiles[i][j];
  72. tile |= VISIBLE; // assume that all tiles are visible
  73. if (blockStr[i*8 + j] == '0')
  74. tile |= BLOCKED;
  75. if (visitStr[i*8 + j] == '1')
  76. tile |= VISITABLE;
  77. }
  78. }
  79. // strings[3] most likely - terrains on which this object can be placed in editor.
  80. // e.g. Whirpool can be placed manually only on water while mines can be placed everywhere despite terrain-specific gfx
  81. // so these two fields can be interpreted as "strong affinity" and "weak affinity" towards terrains
  82. std::string & terrStr = strings[4]; // allowed terrains, 1 = object can be placed on this terrain
  83. assert(terrStr.size() == 9); // all terrains but rock
  84. for (size_t i=0; i<9; i++)
  85. {
  86. if (terrStr[8-i] == '1')
  87. allowedTerrains.insert(ETerrainType(i));
  88. }
  89. id = Obj(boost::lexical_cast<int>(strings[5]));
  90. subid = boost::lexical_cast<int>(strings[6]);
  91. int type = boost::lexical_cast<int>(strings[7]);
  92. printPriority = boost::lexical_cast<int>(strings[8]) * 100; // to have some space in future
  93. if (isOnVisitableFromTopList(id, type))
  94. visitDir = 0xff;
  95. else
  96. visitDir = (8|16|32|64|128);
  97. readMsk();
  98. }
  99. void ObjectTemplate::readMsk()
  100. {
  101. ResourceID resID("SPRITES/" + animationFile, EResType::MASK);
  102. if (CResourceHandler::get()->existsResource(resID))
  103. {
  104. auto msk = CResourceHandler::get()->load(resID)->readAll();
  105. setSize(msk.first.get()[0], msk.first.get()[1]);
  106. }
  107. else //maximum possible size of H3 object //TODO: remove hardcode and move this data into modding system
  108. {
  109. setSize(8, 6);
  110. }
  111. }
  112. void ObjectTemplate::readMap(CBinaryReader & reader)
  113. {
  114. animationFile = reader.readString();
  115. setSize(8, 6);
  116. ui8 blockMask[6];
  117. ui8 visitMask[6];
  118. for(auto & byte : blockMask)
  119. byte = reader.readUInt8();
  120. for(auto & byte : visitMask)
  121. byte = reader.readUInt8();
  122. for (size_t i=0; i<6; i++) // 6 rows
  123. {
  124. for (size_t j=0; j<8; j++) // 8 columns
  125. {
  126. auto & tile = usedTiles[5 - i][7 - j];
  127. tile |= VISIBLE; // assume that all tiles are visible
  128. if (((blockMask[i] >> j) & 1 ) == 0)
  129. tile |= BLOCKED;
  130. if (((visitMask[i] >> j) & 1 ) != 0)
  131. tile |= VISITABLE;
  132. }
  133. }
  134. reader.readUInt16();
  135. ui16 terrMask = reader.readUInt16();
  136. for (size_t i=0; i<9; i++)
  137. {
  138. if (((terrMask >> i) & 1 ) != 0)
  139. allowedTerrains.insert(ETerrainType(i));
  140. }
  141. id = Obj(reader.readUInt32());
  142. subid = reader.readUInt32();
  143. int type = reader.readUInt8();
  144. printPriority = reader.readUInt8() * 100; // to have some space in future
  145. if (isOnVisitableFromTopList(id, type))
  146. visitDir = 0xff;
  147. else
  148. visitDir = (8|16|32|64|128);
  149. reader.skip(16);
  150. readMsk();
  151. if (id == Obj::EVENT)
  152. {
  153. setSize(1,1);
  154. usedTiles[0][0] = VISITABLE;
  155. }
  156. }
  157. void ObjectTemplate::readJson(const JsonNode &node)
  158. {
  159. animationFile = node["animation"].String();
  160. const JsonVector & visitDirs = node["visitableFrom"].Vector();
  161. if (!visitDirs.empty())
  162. {
  163. if (visitDirs[0].String()[0] == '+') visitDir |= 1;
  164. if (visitDirs[0].String()[1] == '+') visitDir |= 2;
  165. if (visitDirs[0].String()[2] == '+') visitDir |= 4;
  166. if (visitDirs[1].String()[2] == '+') visitDir |= 8;
  167. if (visitDirs[2].String()[2] == '+') visitDir |= 16;
  168. if (visitDirs[2].String()[1] == '+') visitDir |= 32;
  169. if (visitDirs[2].String()[0] == '+') visitDir |= 64;
  170. if (visitDirs[1].String()[0] == '+') visitDir |= 128;
  171. }
  172. else
  173. visitDir = 0x00;
  174. if (!node["allowedTerrains"].isNull())
  175. {
  176. for (auto & entry : node["allowedTerrains"].Vector())
  177. allowedTerrains.insert(ETerrainType(vstd::find_pos(GameConstants::TERRAIN_NAMES, entry.String())));
  178. }
  179. else
  180. {
  181. for (size_t i=0; i< GameConstants::TERRAIN_TYPES; i++)
  182. allowedTerrains.insert(ETerrainType(i));
  183. }
  184. if (allowedTerrains.empty())
  185. logGlobal->warnStream() << "Loaded template without allowed terrains!";
  186. auto charToTile = [&](const char & ch) -> ui8
  187. {
  188. switch (ch)
  189. {
  190. case ' ' : return 0;
  191. case '0' : return 0;
  192. case 'V' : return VISIBLE;
  193. case 'B' : return VISIBLE | BLOCKED;
  194. case 'H' : return BLOCKED;
  195. case 'A' : return VISIBLE | BLOCKED | VISITABLE;
  196. case 'T' : return BLOCKED | VISITABLE;
  197. default:
  198. logGlobal->errorStream() << "Unrecognized char " << ch << " in template mask";
  199. return 0;
  200. }
  201. };
  202. const JsonVector & mask = node["mask"].Vector();
  203. size_t height = mask.size();
  204. size_t width = 0;
  205. for (auto & line : mask)
  206. vstd::amax(width, line.String().size());
  207. setSize(width, height);
  208. for (size_t i=0; i<mask.size(); i++)
  209. {
  210. const std::string & line = mask[i].String();
  211. for (size_t j=0; j < line.size(); j++)
  212. usedTiles[mask.size() - 1 - i][line.size() - 1 - j] = charToTile(line[j]);
  213. }
  214. const JsonNode zindex = node["zIndex"];
  215. if (!zindex.isNull())
  216. printPriority = node["zIndex"].Float();
  217. else
  218. printPriority = 0; //default value
  219. }
  220. void ObjectTemplate::writeJson(JsonNode & node) const
  221. {
  222. node.setType(JsonNode::DATA_STRUCT);
  223. //todo: ObjectTemplate::writeJson
  224. }
  225. ui32 ObjectTemplate::getWidth() const
  226. {
  227. return usedTiles.empty() ? 0 : usedTiles.front().size();
  228. }
  229. ui32 ObjectTemplate::getHeight() const
  230. {
  231. return usedTiles.size();
  232. }
  233. void ObjectTemplate::setSize(ui32 width, ui32 height)
  234. {
  235. usedTiles.resize(height);
  236. for (auto & line : usedTiles)
  237. line.resize(width, 0);
  238. }
  239. bool ObjectTemplate::isVisitable() const
  240. {
  241. for (auto & line : usedTiles)
  242. for (auto & tile : line)
  243. if (tile & VISITABLE)
  244. return true;
  245. return false;
  246. }
  247. bool ObjectTemplate::isWithin(si32 X, si32 Y) const
  248. {
  249. if (X < 0 || Y < 0)
  250. return false;
  251. if (X >= getWidth() || Y >= getHeight())
  252. return false;
  253. return true;
  254. }
  255. bool ObjectTemplate::isVisitableAt(si32 X, si32 Y) const
  256. {
  257. if (isWithin(X, Y))
  258. return usedTiles[Y][X] & VISITABLE;
  259. return false;
  260. }
  261. bool ObjectTemplate::isVisibleAt(si32 X, si32 Y) const
  262. {
  263. if (isWithin(X, Y))
  264. return usedTiles[Y][X] & VISIBLE;
  265. return false;
  266. }
  267. bool ObjectTemplate::isBlockedAt(si32 X, si32 Y) const
  268. {
  269. if (isWithin(X, Y))
  270. return usedTiles[Y][X] & BLOCKED;
  271. return false;
  272. }
  273. std::set<int3> ObjectTemplate::getBlockedOffsets() const
  274. {
  275. std::set<int3> ret;
  276. for(int w = 0; w < getWidth(); ++w)
  277. {
  278. for(int h = 0; h < getHeight(); ++h)
  279. {
  280. if (isBlockedAt(w, h))
  281. ret.insert(int3(-w, -h, 0));
  282. }
  283. }
  284. return ret;
  285. }
  286. int3 ObjectTemplate::getBlockMapOffset() const
  287. {
  288. for(int w = 0; w < getWidth(); ++w)
  289. {
  290. for(int h = 0; h < getHeight(); ++h)
  291. {
  292. if (isBlockedAt(w, h))
  293. return int3(w, h, 0);
  294. }
  295. }
  296. return int3(0,0,0);
  297. }
  298. bool ObjectTemplate::isVisitableFrom(si8 X, si8 Y) const
  299. {
  300. // visitDir uses format
  301. // 1 2 3
  302. // 8 4
  303. // 7 6 5
  304. int dirMap[3][3] =
  305. {
  306. { visitDir & 1, visitDir & 2, visitDir & 4 },
  307. { visitDir & 128, 1 , visitDir & 8 },
  308. { visitDir & 64, visitDir & 32, visitDir & 16 }
  309. };
  310. // map input values to range 0..2
  311. int dx = X < 0 ? 0 : X == 0 ? 1 : 2;
  312. int dy = Y < 0 ? 0 : Y == 0 ? 1 : 2;
  313. return dirMap[dy][dx] != 0;
  314. }
  315. int3 ObjectTemplate::getVisitableOffset() const
  316. {
  317. for(int y = 0; y < getHeight(); y++)
  318. for (int x = 0; x < getWidth(); x++)
  319. if (isVisitableAt(x, y))
  320. return int3(x,y,0);
  321. //logGlobal->warnStream() << "Warning: getVisitableOffset called on non-visitable obj!";
  322. return int3(0,0,0);
  323. }
  324. bool ObjectTemplate::isVisitableFromTop() const
  325. {
  326. return visitDir & 2;
  327. //for some reason the line below is never called :?
  328. //return isVisitableFrom (0, 1);
  329. }
  330. bool ObjectTemplate::canBePlacedAt(ETerrainType terrain) const
  331. {
  332. return allowedTerrains.count(terrain) != 0;
  333. }