ObjectTemplate.cpp 13 KB

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