ObjectTemplate.cpp 12 KB

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