ObjectTemplate.cpp 12 KB

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