2
0

ObjectTemplate.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. ObjectTemplate::ObjectTemplate(const ObjectTemplate& other):
  55. visitDir(other.visitDir),
  56. allowedTerrains(other.allowedTerrains),
  57. id(other.id),
  58. subid(other.subid),
  59. printPriority(other.printPriority),
  60. animationFile(other.animationFile)
  61. {
  62. //default copy constructor is failing with usedTiles this for unknown reason
  63. usedTiles.resize(other.usedTiles.size());
  64. for(size_t i = 0; i < usedTiles.size(); i++)
  65. std::copy(other.usedTiles[i].begin(), other.usedTiles[i].end(), std::back_inserter(usedTiles[i]));
  66. }
  67. ObjectTemplate & ObjectTemplate::operator=(const ObjectTemplate & rhs)
  68. {
  69. visitDir = rhs.visitDir;
  70. allowedTerrains = rhs.allowedTerrains;
  71. id = rhs.id;
  72. subid = rhs.subid;
  73. printPriority = rhs.printPriority;
  74. animationFile = rhs.animationFile;
  75. usedTiles.clear();
  76. usedTiles.resize(rhs.usedTiles.size());
  77. for(size_t i = 0; i < usedTiles.size(); i++)
  78. std::copy(rhs.usedTiles[i].begin(), rhs.usedTiles[i].end(), std::back_inserter(usedTiles[i]));
  79. return *this;
  80. }
  81. void ObjectTemplate::readTxt(CLegacyConfigParser & parser)
  82. {
  83. std::string data = parser.readString();
  84. std::vector<std::string> strings;
  85. boost::split(strings, data, boost::is_any_of(" "));
  86. assert(strings.size() == 9);
  87. animationFile = strings[0];
  88. stringID = strings[0];
  89. std::string & blockStr = strings[1]; //block map, 0 = blocked, 1 = unblocked
  90. std::string & visitStr = strings[2]; //visit map, 1 = visitable, 0 = not visitable
  91. assert(blockStr.size() == 6*8);
  92. assert(visitStr.size() == 6*8);
  93. setSize(8, 6);
  94. for (size_t i=0; i<6; i++) // 6 rows
  95. {
  96. for (size_t j=0; j<8; j++) // 8 columns
  97. {
  98. auto & tile = usedTiles[i][j];
  99. tile |= VISIBLE; // assume that all tiles are visible
  100. if (blockStr[i*8 + j] == '0')
  101. tile |= BLOCKED;
  102. if (visitStr[i*8 + j] == '1')
  103. tile |= VISITABLE;
  104. }
  105. }
  106. // strings[3] most likely - terrains on which this object can be placed in editor.
  107. // e.g. Whirpool can be placed manually only on water while mines can be placed everywhere despite terrain-specific gfx
  108. // so these two fields can be interpreted as "strong affinity" and "weak affinity" towards terrains
  109. std::string & terrStr = strings[4]; // allowed terrains, 1 = object can be placed on this terrain
  110. assert(terrStr.size() == 9); // all terrains but rock
  111. for (size_t i=0; i<9; i++)
  112. {
  113. if (terrStr[8-i] == '1')
  114. allowedTerrains.insert(ETerrainType(i));
  115. }
  116. id = Obj(boost::lexical_cast<int>(strings[5]));
  117. subid = boost::lexical_cast<int>(strings[6]);
  118. int type = boost::lexical_cast<int>(strings[7]);
  119. printPriority = boost::lexical_cast<int>(strings[8]) * 100; // to have some space in future
  120. if (isOnVisitableFromTopList(id, type))
  121. visitDir = 0xff;
  122. else
  123. visitDir = (8|16|32|64|128);
  124. readMsk();
  125. }
  126. void ObjectTemplate::readMsk()
  127. {
  128. ResourceID resID("SPRITES/" + animationFile, EResType::MASK);
  129. if (CResourceHandler::get()->existsResource(resID))
  130. {
  131. auto msk = CResourceHandler::get()->load(resID)->readAll();
  132. setSize(msk.first.get()[0], msk.first.get()[1]);
  133. }
  134. else //maximum possible size of H3 object //TODO: remove hardcode and move this data into modding system
  135. {
  136. setSize(8, 6);
  137. }
  138. }
  139. void ObjectTemplate::readMap(CBinaryReader & reader)
  140. {
  141. animationFile = reader.readString();
  142. setSize(8, 6);
  143. ui8 blockMask[6];
  144. ui8 visitMask[6];
  145. for(auto & byte : blockMask)
  146. byte = reader.readUInt8();
  147. for(auto & byte : visitMask)
  148. byte = reader.readUInt8();
  149. for (size_t i=0; i<6; i++) // 6 rows
  150. {
  151. for (size_t j=0; j<8; j++) // 8 columns
  152. {
  153. auto & tile = usedTiles[5 - i][7 - j];
  154. tile |= VISIBLE; // assume that all tiles are visible
  155. if (((blockMask[i] >> j) & 1 ) == 0)
  156. tile |= BLOCKED;
  157. if (((visitMask[i] >> j) & 1 ) != 0)
  158. tile |= VISITABLE;
  159. }
  160. }
  161. reader.readUInt16();
  162. ui16 terrMask = reader.readUInt16();
  163. for (size_t i=0; i<9; i++)
  164. {
  165. if (((terrMask >> i) & 1 ) != 0)
  166. allowedTerrains.insert(ETerrainType(i));
  167. }
  168. id = Obj(reader.readUInt32());
  169. subid = reader.readUInt32();
  170. int type = reader.readUInt8();
  171. printPriority = reader.readUInt8() * 100; // to have some space in future
  172. if (isOnVisitableFromTopList(id, type))
  173. visitDir = 0xff;
  174. else
  175. visitDir = (8|16|32|64|128);
  176. reader.skip(16);
  177. readMsk();
  178. if (id == Obj::EVENT)
  179. {
  180. setSize(1,1);
  181. usedTiles[0][0] = VISITABLE;
  182. }
  183. }
  184. void ObjectTemplate::readJson(const JsonNode &node, const bool withTerrain)
  185. {
  186. animationFile = node["animation"].String();
  187. const JsonVector & visitDirs = node["visitableFrom"].Vector();
  188. if (!visitDirs.empty())
  189. {
  190. if (visitDirs[0].String()[0] == '+') visitDir |= 1;
  191. if (visitDirs[0].String()[1] == '+') visitDir |= 2;
  192. if (visitDirs[0].String()[2] == '+') visitDir |= 4;
  193. if (visitDirs[1].String()[2] == '+') visitDir |= 8;
  194. if (visitDirs[2].String()[2] == '+') visitDir |= 16;
  195. if (visitDirs[2].String()[1] == '+') visitDir |= 32;
  196. if (visitDirs[2].String()[0] == '+') visitDir |= 64;
  197. if (visitDirs[1].String()[0] == '+') visitDir |= 128;
  198. }
  199. else
  200. visitDir = 0x00;
  201. if(withTerrain && !node["allowedTerrains"].isNull())
  202. {
  203. for (auto & entry : node["allowedTerrains"].Vector())
  204. allowedTerrains.insert(ETerrainType(vstd::find_pos(GameConstants::TERRAIN_NAMES, entry.String())));
  205. }
  206. else
  207. {
  208. for (size_t i=0; i< GameConstants::TERRAIN_TYPES; i++)
  209. allowedTerrains.insert(ETerrainType(i));
  210. allowedTerrains.erase(ETerrainType::ROCK);
  211. }
  212. if(withTerrain && allowedTerrains.empty())
  213. logGlobal->warnStream() << "Loaded template without allowed terrains!";
  214. auto charToTile = [&](const char & ch) -> ui8
  215. {
  216. switch (ch)
  217. {
  218. case ' ' : return 0;
  219. case '0' : return 0;
  220. case 'V' : return VISIBLE;
  221. case 'B' : return VISIBLE | BLOCKED;
  222. case 'H' : return BLOCKED;
  223. case 'A' : return VISIBLE | BLOCKED | VISITABLE;
  224. case 'T' : return BLOCKED | VISITABLE;
  225. default:
  226. logGlobal->errorStream() << "Unrecognized char " << ch << " in template mask";
  227. return 0;
  228. }
  229. };
  230. const JsonVector & mask = node["mask"].Vector();
  231. size_t height = mask.size();
  232. size_t width = 0;
  233. for (auto & line : mask)
  234. vstd::amax(width, line.String().size());
  235. setSize(width, height);
  236. for (size_t i=0; i<mask.size(); i++)
  237. {
  238. const std::string & line = mask[i].String();
  239. for (size_t j=0; j < line.size(); j++)
  240. usedTiles[mask.size() - 1 - i][line.size() - 1 - j] = charToTile(line[j]);
  241. }
  242. printPriority = node["zIndex"].Float();
  243. }
  244. void ObjectTemplate::writeJson(JsonNode & node, const bool withTerrain) const
  245. {
  246. node["animation"].String() = animationFile;
  247. if(visitDir != 0x0 && isVisitable())
  248. {
  249. JsonVector & visitDirs = node["visitableFrom"].Vector();
  250. visitDirs.resize(3);
  251. visitDirs[0].String().resize(3);
  252. visitDirs[1].String().resize(3);
  253. visitDirs[2].String().resize(3);
  254. visitDirs[0].String()[0] = (visitDir & 1) ? '+' : '-';
  255. visitDirs[0].String()[1] = (visitDir & 2) ? '+' : '-';
  256. visitDirs[0].String()[2] = (visitDir & 4) ? '+' : '-';
  257. visitDirs[1].String()[2] = (visitDir & 8) ? '+' : '-';
  258. visitDirs[2].String()[2] = (visitDir & 16) ? '+' : '-';
  259. visitDirs[2].String()[1] = (visitDir & 32) ? '+' : '-';
  260. visitDirs[2].String()[0] = (visitDir & 64) ? '+' : '-';
  261. visitDirs[1].String()[0] = (visitDir & 128) ? '+' : '-';
  262. visitDirs[1].String()[1] = '-';
  263. }
  264. if(withTerrain)
  265. {
  266. //assumed that ROCK terrain not included
  267. if(allowedTerrains.size() < (GameConstants::TERRAIN_TYPES - 1))
  268. {
  269. JsonVector & data = node["allowedTerrains"].Vector();
  270. for(auto type : allowedTerrains)
  271. {
  272. JsonNode value(JsonNode::DATA_STRING);
  273. value.String() = GameConstants::TERRAIN_NAMES[type.num];
  274. data.push_back(value);
  275. }
  276. }
  277. }
  278. auto tileToChar = [&](const ui8 & tile) -> char
  279. {
  280. if(tile & VISIBLE)
  281. {
  282. if(tile & BLOCKED)
  283. {
  284. if(tile & VISITABLE)
  285. return 'A';
  286. else
  287. return 'B';
  288. }
  289. else
  290. return 'V';
  291. }
  292. else
  293. {
  294. if(tile & BLOCKED)
  295. {
  296. if(tile & VISITABLE)
  297. return 'T';
  298. else
  299. return 'H';
  300. }
  301. else
  302. return '0';
  303. }
  304. };
  305. size_t height = getHeight();
  306. size_t width = getWidth();
  307. JsonVector & mask = node["mask"].Vector();
  308. for(size_t i=0; i < height; i++)
  309. {
  310. JsonNode lineNode(JsonNode::DATA_STRING);
  311. std::string & line = lineNode.String();
  312. line.resize(width);
  313. for(size_t j=0; j < width; j++)
  314. line[j] = tileToChar(usedTiles[height - 1 - i][width - 1 - j]);
  315. mask.push_back(lineNode);
  316. }
  317. if(printPriority != 0)
  318. node["zIndex"].Float() = printPriority;
  319. }
  320. ui32 ObjectTemplate::getWidth() const
  321. {
  322. //TODO: Use 2D array
  323. //TODO: better precalculate and store constant value
  324. ui32 ret = 0;
  325. for (const auto &row : usedTiles) //copy is expensive
  326. {
  327. ret = std::max<ui32>(ret, row.size());
  328. }
  329. return ret;
  330. }
  331. ui32 ObjectTemplate::getHeight() const
  332. {
  333. //TODO: Use 2D array
  334. return usedTiles.size();
  335. }
  336. void ObjectTemplate::setSize(ui32 width, ui32 height)
  337. {
  338. usedTiles.resize(height);
  339. for (auto & line : usedTiles)
  340. line.resize(width, 0);
  341. }
  342. bool ObjectTemplate::isVisitable() const
  343. {
  344. for (auto & line : usedTiles)
  345. for (auto & tile : line)
  346. if (tile & VISITABLE)
  347. return true;
  348. return false;
  349. }
  350. bool ObjectTemplate::isWithin(si32 X, si32 Y) const
  351. {
  352. if (X < 0 || Y < 0)
  353. return false;
  354. if (X >= getWidth() || Y >= getHeight())
  355. return false;
  356. return true;
  357. }
  358. bool ObjectTemplate::isVisitableAt(si32 X, si32 Y) const
  359. {
  360. if (isWithin(X, Y))
  361. return usedTiles[Y][X] & VISITABLE;
  362. return false;
  363. }
  364. bool ObjectTemplate::isVisibleAt(si32 X, si32 Y) const
  365. {
  366. if (isWithin(X, Y))
  367. return usedTiles[Y][X] & VISIBLE;
  368. return false;
  369. }
  370. bool ObjectTemplate::isBlockedAt(si32 X, si32 Y) const
  371. {
  372. if (isWithin(X, Y))
  373. return usedTiles[Y][X] & BLOCKED;
  374. return false;
  375. }
  376. std::set<int3> ObjectTemplate::getBlockedOffsets() const
  377. {
  378. std::set<int3> ret;
  379. for(int w = 0; w < getWidth(); ++w)
  380. {
  381. for(int h = 0; h < getHeight(); ++h)
  382. {
  383. if (isBlockedAt(w, h))
  384. ret.insert(int3(-w, -h, 0));
  385. }
  386. }
  387. return ret;
  388. }
  389. int3 ObjectTemplate::getBlockMapOffset() const
  390. {
  391. for(int w = 0; w < getWidth(); ++w)
  392. {
  393. for(int h = 0; h < getHeight(); ++h)
  394. {
  395. if (isBlockedAt(w, h))
  396. return int3(w, h, 0);
  397. }
  398. }
  399. return int3(0,0,0);
  400. }
  401. bool ObjectTemplate::isVisitableFrom(si8 X, si8 Y) const
  402. {
  403. // visitDir uses format
  404. // 1 2 3
  405. // 8 4
  406. // 7 6 5
  407. int dirMap[3][3] =
  408. {
  409. { visitDir & 1, visitDir & 2, visitDir & 4 },
  410. { visitDir & 128, 1 , visitDir & 8 },
  411. { visitDir & 64, visitDir & 32, visitDir & 16 }
  412. };
  413. // map input values to range 0..2
  414. int dx = X < 0 ? 0 : X == 0 ? 1 : 2;
  415. int dy = Y < 0 ? 0 : Y == 0 ? 1 : 2;
  416. return dirMap[dy][dx] != 0;
  417. }
  418. int3 ObjectTemplate::getVisitableOffset() const
  419. {
  420. for(int y = 0; y < getHeight(); y++)
  421. for (int x = 0; x < getWidth(); x++)
  422. if (isVisitableAt(x, y))
  423. return int3(x,y,0);
  424. //logGlobal->warnStream() << "Warning: getVisitableOffset called on non-visitable obj!";
  425. return int3(0,0,0);
  426. }
  427. bool ObjectTemplate::isVisitableFromTop() const
  428. {
  429. return visitDir & 2;
  430. //for some reason the line below is never called :?
  431. //return isVisitableFrom (0, 1);
  432. }
  433. bool ObjectTemplate::canBePlacedAt(ETerrainType terrain) const
  434. {
  435. return allowedTerrains.count(terrain) != 0;
  436. }