ObjectTemplate.cpp 13 KB

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