nodedef.cpp 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "nodedef.h"
  17. #include "itemdef.h"
  18. #ifndef SERVER
  19. #include "client/mesh.h"
  20. #include "client/shader.h"
  21. #include "client/client.h"
  22. #include "client/renderingengine.h"
  23. #include "client/tile.h"
  24. #include <IMeshManipulator.h>
  25. #endif
  26. #include "log.h"
  27. #include "settings.h"
  28. #include "nameidmapping.h"
  29. #include "util/numeric.h"
  30. #include "util/serialize.h"
  31. #include "util/string.h"
  32. #include "exceptions.h"
  33. #include "debug.h"
  34. #include "gamedef.h"
  35. #include "mapnode.h"
  36. #include <fstream> // Used in applyTextureOverrides()
  37. #include <algorithm>
  38. #include <cmath>
  39. /*
  40. NodeBox
  41. */
  42. void NodeBox::reset()
  43. {
  44. type = NODEBOX_REGULAR;
  45. // default is empty
  46. fixed.clear();
  47. // default is sign/ladder-like
  48. wall_top = aabb3f(-BS/2, BS/2-BS/16., -BS/2, BS/2, BS/2, BS/2);
  49. wall_bottom = aabb3f(-BS/2, -BS/2, -BS/2, BS/2, -BS/2+BS/16., BS/2);
  50. wall_side = aabb3f(-BS/2, -BS/2, -BS/2, -BS/2+BS/16., BS/2, BS/2);
  51. // no default for other parts
  52. connected.reset();
  53. }
  54. void NodeBox::serialize(std::ostream &os, u16 protocol_version) const
  55. {
  56. writeU8(os, 6); // version. Protocol >= 36
  57. switch (type) {
  58. case NODEBOX_LEVELED:
  59. case NODEBOX_FIXED:
  60. writeU8(os, type);
  61. writeU16(os, fixed.size());
  62. for (const aabb3f &nodebox : fixed) {
  63. writeV3F32(os, nodebox.MinEdge);
  64. writeV3F32(os, nodebox.MaxEdge);
  65. }
  66. break;
  67. case NODEBOX_WALLMOUNTED:
  68. writeU8(os, type);
  69. writeV3F32(os, wall_top.MinEdge);
  70. writeV3F32(os, wall_top.MaxEdge);
  71. writeV3F32(os, wall_bottom.MinEdge);
  72. writeV3F32(os, wall_bottom.MaxEdge);
  73. writeV3F32(os, wall_side.MinEdge);
  74. writeV3F32(os, wall_side.MaxEdge);
  75. break;
  76. case NODEBOX_CONNECTED: {
  77. writeU8(os, type);
  78. #define WRITEBOX(box) \
  79. writeU16(os, (box).size()); \
  80. for (const aabb3f &i: (box)) { \
  81. writeV3F32(os, i.MinEdge); \
  82. writeV3F32(os, i.MaxEdge); \
  83. };
  84. const auto &c = getConnected();
  85. WRITEBOX(fixed);
  86. WRITEBOX(c.connect_top);
  87. WRITEBOX(c.connect_bottom);
  88. WRITEBOX(c.connect_front);
  89. WRITEBOX(c.connect_left);
  90. WRITEBOX(c.connect_back);
  91. WRITEBOX(c.connect_right);
  92. WRITEBOX(c.disconnected_top);
  93. WRITEBOX(c.disconnected_bottom);
  94. WRITEBOX(c.disconnected_front);
  95. WRITEBOX(c.disconnected_left);
  96. WRITEBOX(c.disconnected_back);
  97. WRITEBOX(c.disconnected_right);
  98. WRITEBOX(c.disconnected);
  99. WRITEBOX(c.disconnected_sides);
  100. break;
  101. }
  102. default:
  103. writeU8(os, type);
  104. break;
  105. }
  106. }
  107. void NodeBox::deSerialize(std::istream &is)
  108. {
  109. if (readU8(is) < 6)
  110. throw SerializationError("unsupported NodeBox version");
  111. reset();
  112. type = (enum NodeBoxType)readU8(is);
  113. if(type == NODEBOX_FIXED || type == NODEBOX_LEVELED)
  114. {
  115. u16 fixed_count = readU16(is);
  116. while(fixed_count--)
  117. {
  118. aabb3f box;
  119. box.MinEdge = readV3F32(is);
  120. box.MaxEdge = readV3F32(is);
  121. fixed.push_back(box);
  122. }
  123. }
  124. else if(type == NODEBOX_WALLMOUNTED)
  125. {
  126. wall_top.MinEdge = readV3F32(is);
  127. wall_top.MaxEdge = readV3F32(is);
  128. wall_bottom.MinEdge = readV3F32(is);
  129. wall_bottom.MaxEdge = readV3F32(is);
  130. wall_side.MinEdge = readV3F32(is);
  131. wall_side.MaxEdge = readV3F32(is);
  132. }
  133. else if (type == NODEBOX_CONNECTED)
  134. {
  135. #define READBOXES(box) { \
  136. count = readU16(is); \
  137. (box).reserve(count); \
  138. while (count--) { \
  139. v3f min = readV3F32(is); \
  140. v3f max = readV3F32(is); \
  141. (box).emplace_back(min, max); }; }
  142. u16 count;
  143. auto &c = getConnected();
  144. READBOXES(fixed);
  145. READBOXES(c.connect_top);
  146. READBOXES(c.connect_bottom);
  147. READBOXES(c.connect_front);
  148. READBOXES(c.connect_left);
  149. READBOXES(c.connect_back);
  150. READBOXES(c.connect_right);
  151. READBOXES(c.disconnected_top);
  152. READBOXES(c.disconnected_bottom);
  153. READBOXES(c.disconnected_front);
  154. READBOXES(c.disconnected_left);
  155. READBOXES(c.disconnected_back);
  156. READBOXES(c.disconnected_right);
  157. READBOXES(c.disconnected);
  158. READBOXES(c.disconnected_sides);
  159. }
  160. }
  161. /*
  162. TileDef
  163. */
  164. #define TILE_FLAG_BACKFACE_CULLING (1 << 0)
  165. #define TILE_FLAG_TILEABLE_HORIZONTAL (1 << 1)
  166. #define TILE_FLAG_TILEABLE_VERTICAL (1 << 2)
  167. #define TILE_FLAG_HAS_COLOR (1 << 3)
  168. #define TILE_FLAG_HAS_SCALE (1 << 4)
  169. #define TILE_FLAG_HAS_ALIGN_STYLE (1 << 5)
  170. void TileDef::serialize(std::ostream &os, u16 protocol_version) const
  171. {
  172. // protocol_version >= 36
  173. u8 version = 6;
  174. writeU8(os, version);
  175. if (protocol_version > 39) {
  176. os << serializeString16(name);
  177. } else {
  178. // Before f018737, TextureSource::getTextureAverageColor did not handle
  179. // missing textures. "[png" can be used as base texture, but is not known
  180. // on older clients. Hence use "blank.png" to avoid this problem.
  181. // To be forward-compatible with future base textures/modifiers,
  182. // we apply the same prefix to any texture beginning with [,
  183. // except for the ones that are supported on older clients.
  184. bool pass_through = true;
  185. if (!name.empty() && name[0] == '[') {
  186. pass_through = str_starts_with(name, "[combine:") ||
  187. str_starts_with(name, "[inventorycube{") ||
  188. str_starts_with(name, "[lowpart:");
  189. }
  190. if (pass_through)
  191. os << serializeString16(name);
  192. else
  193. os << serializeString16("blank.png^" + name);
  194. }
  195. animation.serialize(os, version);
  196. bool has_scale = scale > 0;
  197. u16 flags = 0;
  198. if (backface_culling)
  199. flags |= TILE_FLAG_BACKFACE_CULLING;
  200. if (tileable_horizontal)
  201. flags |= TILE_FLAG_TILEABLE_HORIZONTAL;
  202. if (tileable_vertical)
  203. flags |= TILE_FLAG_TILEABLE_VERTICAL;
  204. if (has_color)
  205. flags |= TILE_FLAG_HAS_COLOR;
  206. if (has_scale)
  207. flags |= TILE_FLAG_HAS_SCALE;
  208. if (align_style != ALIGN_STYLE_NODE)
  209. flags |= TILE_FLAG_HAS_ALIGN_STYLE;
  210. writeU16(os, flags);
  211. if (has_color) {
  212. writeU8(os, color.getRed());
  213. writeU8(os, color.getGreen());
  214. writeU8(os, color.getBlue());
  215. }
  216. if (has_scale)
  217. writeU8(os, scale);
  218. if (align_style != ALIGN_STYLE_NODE)
  219. writeU8(os, align_style);
  220. }
  221. void TileDef::deSerialize(std::istream &is, NodeDrawType drawtype, u16 protocol_version)
  222. {
  223. if (readU8(is) < 6)
  224. throw SerializationError("unsupported TileDef version");
  225. name = deSerializeString16(is);
  226. animation.deSerialize(is, protocol_version);
  227. u16 flags = readU16(is);
  228. backface_culling = flags & TILE_FLAG_BACKFACE_CULLING;
  229. tileable_horizontal = flags & TILE_FLAG_TILEABLE_HORIZONTAL;
  230. tileable_vertical = flags & TILE_FLAG_TILEABLE_VERTICAL;
  231. has_color = flags & TILE_FLAG_HAS_COLOR;
  232. bool has_scale = flags & TILE_FLAG_HAS_SCALE;
  233. bool has_align_style = flags & TILE_FLAG_HAS_ALIGN_STYLE;
  234. if (has_color) {
  235. color.setRed(readU8(is));
  236. color.setGreen(readU8(is));
  237. color.setBlue(readU8(is));
  238. }
  239. scale = has_scale ? readU8(is) : 0;
  240. if (has_align_style)
  241. align_style = static_cast<AlignStyle>(readU8(is));
  242. else
  243. align_style = ALIGN_STYLE_NODE;
  244. }
  245. void TextureSettings::readSettings()
  246. {
  247. connected_glass = g_settings->getBool("connected_glass");
  248. opaque_water = g_settings->getBool("opaque_water");
  249. bool smooth_lighting = g_settings->getBool("smooth_lighting");
  250. enable_mesh_cache = g_settings->getBool("enable_mesh_cache");
  251. enable_minimap = g_settings->getBool("enable_minimap");
  252. node_texture_size = std::max<u16>(g_settings->getU16("texture_min_size"), 1);
  253. std::string leaves_style_str = g_settings->get("leaves_style");
  254. std::string world_aligned_mode_str = g_settings->get("world_aligned_mode");
  255. std::string autoscale_mode_str = g_settings->get("autoscale_mode");
  256. // Mesh cache is not supported in combination with smooth lighting
  257. if (smooth_lighting)
  258. enable_mesh_cache = false;
  259. if (leaves_style_str == "fancy") {
  260. leaves_style = LEAVES_FANCY;
  261. } else if (leaves_style_str == "simple") {
  262. leaves_style = LEAVES_SIMPLE;
  263. } else {
  264. leaves_style = LEAVES_OPAQUE;
  265. }
  266. if (world_aligned_mode_str == "enable")
  267. world_aligned_mode = WORLDALIGN_ENABLE;
  268. else if (world_aligned_mode_str == "force_solid")
  269. world_aligned_mode = WORLDALIGN_FORCE;
  270. else if (world_aligned_mode_str == "force_nodebox")
  271. world_aligned_mode = WORLDALIGN_FORCE_NODEBOX;
  272. else
  273. world_aligned_mode = WORLDALIGN_DISABLE;
  274. if (autoscale_mode_str == "enable")
  275. autoscale_mode = AUTOSCALE_ENABLE;
  276. else if (autoscale_mode_str == "force")
  277. autoscale_mode = AUTOSCALE_FORCE;
  278. else
  279. autoscale_mode = AUTOSCALE_DISABLE;
  280. }
  281. /*
  282. ContentFeatures
  283. */
  284. ContentFeatures::ContentFeatures()
  285. {
  286. reset();
  287. }
  288. ContentFeatures::~ContentFeatures()
  289. {
  290. #ifndef SERVER
  291. for (u16 j = 0; j < 6; j++) {
  292. delete tiles[j].layers[0].frames;
  293. delete tiles[j].layers[1].frames;
  294. }
  295. for (u16 j = 0; j < CF_SPECIAL_COUNT; j++)
  296. delete special_tiles[j].layers[0].frames;
  297. #endif
  298. }
  299. void ContentFeatures::reset()
  300. {
  301. /*
  302. Cached stuff
  303. */
  304. #ifndef SERVER
  305. solidness = 2;
  306. visual_solidness = 0;
  307. backface_culling = true;
  308. #endif
  309. has_on_construct = false;
  310. has_on_destruct = false;
  311. has_after_destruct = false;
  312. /*
  313. Actual data
  314. NOTE: Most of this is always overridden by the default values given
  315. in builtin.lua
  316. */
  317. name.clear();
  318. groups.clear();
  319. // Unknown nodes can be dug
  320. groups["dig_immediate"] = 2;
  321. drawtype = NDT_NORMAL;
  322. mesh.clear();
  323. #ifndef SERVER
  324. for (auto &i : mesh_ptr)
  325. i = NULL;
  326. minimap_color = video::SColor(0, 0, 0, 0);
  327. #endif
  328. visual_scale = 1.0;
  329. for (auto &i : tiledef)
  330. i = TileDef();
  331. for (auto &j : tiledef_special)
  332. j = TileDef();
  333. alpha = ALPHAMODE_OPAQUE;
  334. post_effect_color = video::SColor(0, 0, 0, 0);
  335. param_type = CPT_NONE;
  336. param_type_2 = CPT2_NONE;
  337. is_ground_content = false;
  338. light_propagates = false;
  339. sunlight_propagates = false;
  340. walkable = true;
  341. pointable = true;
  342. diggable = true;
  343. climbable = false;
  344. buildable_to = false;
  345. floodable = false;
  346. rightclickable = true;
  347. leveled = 0;
  348. leveled_max = LEVELED_MAX;
  349. liquid_type = LIQUID_NONE;
  350. liquid_alternative_flowing.clear();
  351. liquid_alternative_flowing_id = CONTENT_IGNORE;
  352. liquid_alternative_source.clear();
  353. liquid_alternative_source_id = CONTENT_IGNORE;
  354. liquid_viscosity = 0;
  355. liquid_renewable = true;
  356. liquid_range = LIQUID_LEVEL_MAX+1;
  357. drowning = 0;
  358. light_source = 0;
  359. damage_per_second = 0;
  360. node_box.reset();
  361. selection_box.reset();
  362. collision_box.reset();
  363. waving = 0;
  364. legacy_facedir_simple = false;
  365. legacy_wallmounted = false;
  366. sound_footstep = SoundSpec();
  367. sound_dig = SoundSpec("__group");
  368. sound_dug = SoundSpec();
  369. connects_to.clear();
  370. connects_to_ids.clear();
  371. connect_sides = 0;
  372. color = video::SColor(0xFFFFFFFF);
  373. palette_name.clear();
  374. palette = NULL;
  375. node_dig_prediction = "air";
  376. move_resistance = 0;
  377. liquid_move_physics = false;
  378. }
  379. void ContentFeatures::setAlphaFromLegacy(u8 legacy_alpha)
  380. {
  381. // No special handling for nodebox/mesh here as it doesn't make sense to
  382. // throw warnings when the server is too old to support the "correct" way
  383. switch (drawtype) {
  384. case NDT_NORMAL:
  385. alpha = legacy_alpha == 255 ? ALPHAMODE_OPAQUE : ALPHAMODE_CLIP;
  386. break;
  387. case NDT_LIQUID:
  388. case NDT_FLOWINGLIQUID:
  389. alpha = legacy_alpha == 255 ? ALPHAMODE_OPAQUE : ALPHAMODE_BLEND;
  390. break;
  391. default:
  392. alpha = legacy_alpha == 255 ? ALPHAMODE_CLIP : ALPHAMODE_BLEND;
  393. break;
  394. }
  395. }
  396. u8 ContentFeatures::getAlphaForLegacy() const
  397. {
  398. // This is so simple only because 255 and 0 mean wildly different things
  399. // depending on drawtype...
  400. return alpha == ALPHAMODE_OPAQUE ? 255 : 0;
  401. }
  402. void ContentFeatures::serialize(std::ostream &os, u16 protocol_version) const
  403. {
  404. writeU8(os, CONTENTFEATURES_VERSION);
  405. // general
  406. os << serializeString16(name);
  407. writeU16(os, groups.size());
  408. for (const auto &group : groups) {
  409. os << serializeString16(group.first);
  410. if (protocol_version < 41 && group.first.compare("bouncy") == 0) {
  411. // Old clients may choke on negative bouncy value
  412. writeS16(os, abs(group.second));
  413. } else {
  414. writeS16(os, group.second);
  415. }
  416. }
  417. writeU8(os, param_type);
  418. writeU8(os, param_type_2);
  419. // visual
  420. writeU8(os, drawtype);
  421. os << serializeString16(mesh);
  422. writeF32(os, visual_scale);
  423. writeU8(os, 6);
  424. for (const TileDef &td : tiledef)
  425. td.serialize(os, protocol_version);
  426. for (const TileDef &td : tiledef_overlay)
  427. td.serialize(os, protocol_version);
  428. writeU8(os, CF_SPECIAL_COUNT);
  429. for (const TileDef &td : tiledef_special) {
  430. td.serialize(os, protocol_version);
  431. }
  432. writeU8(os, getAlphaForLegacy());
  433. writeU8(os, color.getRed());
  434. writeU8(os, color.getGreen());
  435. writeU8(os, color.getBlue());
  436. os << serializeString16(palette_name);
  437. writeU8(os, waving);
  438. writeU8(os, connect_sides);
  439. writeU16(os, connects_to_ids.size());
  440. for (u16 connects_to_id : connects_to_ids)
  441. writeU16(os, connects_to_id);
  442. writeARGB8(os, post_effect_color);
  443. writeU8(os, leveled);
  444. // lighting
  445. writeU8(os, light_propagates);
  446. writeU8(os, sunlight_propagates);
  447. writeU8(os, light_source);
  448. // map generation
  449. writeU8(os, is_ground_content);
  450. // interaction
  451. writeU8(os, walkable);
  452. writeU8(os, pointable);
  453. writeU8(os, diggable);
  454. writeU8(os, climbable);
  455. writeU8(os, buildable_to);
  456. writeU8(os, rightclickable);
  457. writeU32(os, damage_per_second);
  458. // liquid
  459. LiquidType liquid_type_bc = liquid_type;
  460. if (protocol_version <= 39) {
  461. // Since commit 7f25823, liquid drawtypes can be used even with LIQUID_NONE
  462. // solution: force liquid type accordingly to accepted values
  463. if (drawtype == NDT_LIQUID)
  464. liquid_type_bc = LIQUID_SOURCE;
  465. else if (drawtype == NDT_FLOWINGLIQUID)
  466. liquid_type_bc = LIQUID_FLOWING;
  467. }
  468. writeU8(os, liquid_type_bc);
  469. os << serializeString16(liquid_alternative_flowing);
  470. os << serializeString16(liquid_alternative_source);
  471. writeU8(os, liquid_viscosity);
  472. writeU8(os, liquid_renewable);
  473. writeU8(os, liquid_range);
  474. writeU8(os, drowning);
  475. writeU8(os, floodable);
  476. // node boxes
  477. node_box.serialize(os, protocol_version);
  478. selection_box.serialize(os, protocol_version);
  479. collision_box.serialize(os, protocol_version);
  480. // sound
  481. sound_footstep.serializeSimple(os, protocol_version);
  482. sound_dig.serializeSimple(os, protocol_version);
  483. sound_dug.serializeSimple(os, protocol_version);
  484. // legacy
  485. writeU8(os, legacy_facedir_simple);
  486. writeU8(os, legacy_wallmounted);
  487. // new attributes
  488. os << serializeString16(node_dig_prediction);
  489. writeU8(os, leveled_max);
  490. writeU8(os, alpha);
  491. writeU8(os, move_resistance);
  492. writeU8(os, liquid_move_physics);
  493. }
  494. void ContentFeatures::deSerialize(std::istream &is, u16 protocol_version)
  495. {
  496. if (readU8(is) < CONTENTFEATURES_VERSION)
  497. throw SerializationError("unsupported ContentFeatures version");
  498. // general
  499. name = deSerializeString16(is);
  500. groups.clear();
  501. u32 groups_size = readU16(is);
  502. for (u32 i = 0; i < groups_size; i++) {
  503. std::string name = deSerializeString16(is);
  504. int value = readS16(is);
  505. groups[name] = value;
  506. }
  507. param_type = (enum ContentParamType) readU8(is);
  508. param_type_2 = (enum ContentParamType2) readU8(is);
  509. // visual
  510. drawtype = (enum NodeDrawType) readU8(is);
  511. mesh = deSerializeString16(is);
  512. visual_scale = readF32(is);
  513. if (readU8(is) != 6)
  514. throw SerializationError("unsupported tile count");
  515. for (TileDef &td : tiledef)
  516. td.deSerialize(is, drawtype, protocol_version);
  517. for (TileDef &td : tiledef_overlay)
  518. td.deSerialize(is, drawtype, protocol_version);
  519. if (readU8(is) != CF_SPECIAL_COUNT)
  520. throw SerializationError("unsupported CF_SPECIAL_COUNT");
  521. for (TileDef &td : tiledef_special)
  522. td.deSerialize(is, drawtype, protocol_version);
  523. setAlphaFromLegacy(readU8(is));
  524. color.setRed(readU8(is));
  525. color.setGreen(readU8(is));
  526. color.setBlue(readU8(is));
  527. palette_name = deSerializeString16(is);
  528. waving = readU8(is);
  529. connect_sides = readU8(is);
  530. u16 connects_to_size = readU16(is);
  531. connects_to_ids.clear();
  532. for (u16 i = 0; i < connects_to_size; i++)
  533. connects_to_ids.push_back(readU16(is));
  534. post_effect_color = readARGB8(is);
  535. leveled = readU8(is);
  536. // lighting-related
  537. light_propagates = readU8(is);
  538. sunlight_propagates = readU8(is);
  539. light_source = readU8(is);
  540. light_source = MYMIN(light_source, LIGHT_MAX);
  541. // map generation
  542. is_ground_content = readU8(is);
  543. // interaction
  544. walkable = readU8(is);
  545. pointable = readU8(is);
  546. diggable = readU8(is);
  547. climbable = readU8(is);
  548. buildable_to = readU8(is);
  549. rightclickable = readU8(is);
  550. damage_per_second = readU32(is);
  551. // liquid
  552. liquid_type = (enum LiquidType) readU8(is);
  553. liquid_move_physics = liquid_type != LIQUID_NONE;
  554. liquid_alternative_flowing = deSerializeString16(is);
  555. liquid_alternative_source = deSerializeString16(is);
  556. liquid_viscosity = readU8(is);
  557. move_resistance = liquid_viscosity; // set default move_resistance
  558. liquid_renewable = readU8(is);
  559. liquid_range = readU8(is);
  560. drowning = readU8(is);
  561. floodable = readU8(is);
  562. // node boxes
  563. node_box.deSerialize(is);
  564. selection_box.deSerialize(is);
  565. collision_box.deSerialize(is);
  566. // sounds
  567. sound_footstep.deSerializeSimple(is, protocol_version);
  568. sound_dig.deSerializeSimple(is, protocol_version);
  569. sound_dug.deSerializeSimple(is, protocol_version);
  570. // read legacy properties
  571. legacy_facedir_simple = readU8(is);
  572. legacy_wallmounted = readU8(is);
  573. try {
  574. node_dig_prediction = deSerializeString16(is);
  575. u8 tmp = readU8(is);
  576. if (is.eof()) /* readU8 doesn't throw exceptions so we have to do this */
  577. throw SerializationError("");
  578. leveled_max = tmp;
  579. tmp = readU8(is);
  580. if (is.eof())
  581. throw SerializationError("");
  582. alpha = static_cast<enum AlphaMode>(tmp);
  583. tmp = readU8(is);
  584. if (is.eof())
  585. throw SerializationError("");
  586. move_resistance = tmp;
  587. tmp = readU8(is);
  588. if (is.eof())
  589. throw SerializationError("");
  590. liquid_move_physics = tmp;
  591. } catch(SerializationError &e) {};
  592. }
  593. #ifndef SERVER
  594. static void fillTileAttribs(ITextureSource *tsrc, TileLayer *layer,
  595. const TileSpec &tile, const TileDef &tiledef, video::SColor color,
  596. u8 material_type, u32 shader_id, bool backface_culling,
  597. const TextureSettings &tsettings)
  598. {
  599. layer->shader_id = shader_id;
  600. layer->texture = tsrc->getTextureForMesh(tiledef.name, &layer->texture_id);
  601. layer->material_type = material_type;
  602. bool has_scale = tiledef.scale > 0;
  603. bool use_autoscale = tsettings.autoscale_mode == AUTOSCALE_FORCE ||
  604. (tsettings.autoscale_mode == AUTOSCALE_ENABLE && !has_scale);
  605. if (use_autoscale && layer->texture) {
  606. auto texture_size = layer->texture->getOriginalSize();
  607. float base_size = tsettings.node_texture_size;
  608. float size = std::fmin(texture_size.Width, texture_size.Height);
  609. layer->scale = std::fmax(base_size, size) / base_size;
  610. } else if (has_scale) {
  611. layer->scale = tiledef.scale;
  612. } else {
  613. layer->scale = 1;
  614. }
  615. if (!tile.world_aligned)
  616. layer->scale = 1;
  617. layer->flags_texture = tsrc->getShaderFlagsTexture(layer->normal_texture ? true : false);
  618. // Material flags
  619. layer->material_flags = 0;
  620. if (backface_culling)
  621. layer->material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
  622. if (tiledef.animation.type != TAT_NONE)
  623. layer->material_flags |= MATERIAL_FLAG_ANIMATION;
  624. if (tiledef.tileable_horizontal)
  625. layer->material_flags |= MATERIAL_FLAG_TILEABLE_HORIZONTAL;
  626. if (tiledef.tileable_vertical)
  627. layer->material_flags |= MATERIAL_FLAG_TILEABLE_VERTICAL;
  628. // Color
  629. layer->has_color = tiledef.has_color;
  630. if (tiledef.has_color)
  631. layer->color = tiledef.color;
  632. else
  633. layer->color = color;
  634. // Animation parameters
  635. int frame_count = 1;
  636. if (layer->material_flags & MATERIAL_FLAG_ANIMATION) {
  637. assert(layer->texture);
  638. int frame_length_ms;
  639. tiledef.animation.determineParams(layer->texture->getOriginalSize(),
  640. &frame_count, &frame_length_ms, NULL);
  641. layer->animation_frame_count = frame_count;
  642. layer->animation_frame_length_ms = frame_length_ms;
  643. }
  644. if (frame_count == 1) {
  645. layer->material_flags &= ~MATERIAL_FLAG_ANIMATION;
  646. } else {
  647. assert(layer->texture);
  648. if (!layer->frames)
  649. layer->frames = new std::vector<FrameSpec>();
  650. layer->frames->resize(frame_count);
  651. std::ostringstream os(std::ios::binary);
  652. for (int i = 0; i < frame_count; i++) {
  653. FrameSpec frame;
  654. os.str("");
  655. os << tiledef.name;
  656. tiledef.animation.getTextureModifer(os,
  657. layer->texture->getOriginalSize(), i);
  658. frame.texture = tsrc->getTextureForMesh(os.str(), &frame.texture_id);
  659. if (layer->normal_texture)
  660. frame.normal_texture = tsrc->getNormalTexture(os.str());
  661. frame.flags_texture = layer->flags_texture;
  662. (*layer->frames)[i] = frame;
  663. }
  664. }
  665. }
  666. bool ContentFeatures::textureAlphaCheck(ITextureSource *tsrc, const TileDef *tiles, int length)
  667. {
  668. video::IVideoDriver *driver = RenderingEngine::get_video_driver();
  669. static thread_local bool long_warning_printed = false;
  670. std::set<std::string> seen;
  671. for (int i = 0; i < length; i++) {
  672. if (seen.find(tiles[i].name) != seen.end())
  673. continue;
  674. seen.insert(tiles[i].name);
  675. // Load the texture and see if there's any transparent pixels
  676. video::ITexture *texture = tsrc->getTexture(tiles[i].name);
  677. video::IImage *image = driver->createImage(texture,
  678. core::position2d<s32>(0, 0), texture->getOriginalSize());
  679. if (!image)
  680. continue;
  681. core::dimension2d<u32> dim = image->getDimension();
  682. bool ok = true;
  683. for (u16 x = 0; x < dim.Width; x++) {
  684. for (u16 y = 0; y < dim.Height; y++) {
  685. if (image->getPixel(x, y).getAlpha() < 255) {
  686. ok = false;
  687. goto break_loop;
  688. }
  689. }
  690. }
  691. break_loop:
  692. image->drop();
  693. if (ok)
  694. continue;
  695. warningstream << "Texture \"" << tiles[i].name << "\" of "
  696. << name << " has transparency, assuming "
  697. "use_texture_alpha = \"clip\"." << std::endl;
  698. if (!long_warning_printed) {
  699. warningstream << " This warning can be a false-positive if "
  700. "unused pixels in the texture are transparent. However if "
  701. "it is meant to be transparent, you *MUST* update the "
  702. "nodedef and set use_texture_alpha = \"clip\"! This "
  703. "compatibility code will be removed in a few releases."
  704. << std::endl;
  705. long_warning_printed = true;
  706. }
  707. return true;
  708. }
  709. return false;
  710. }
  711. bool isWorldAligned(AlignStyle style, WorldAlignMode mode, NodeDrawType drawtype)
  712. {
  713. if (style == ALIGN_STYLE_WORLD)
  714. return true;
  715. if (mode == WORLDALIGN_DISABLE)
  716. return false;
  717. if (style == ALIGN_STYLE_USER_DEFINED)
  718. return true;
  719. if (drawtype == NDT_NORMAL)
  720. return mode >= WORLDALIGN_FORCE;
  721. if (drawtype == NDT_NODEBOX)
  722. return mode >= WORLDALIGN_FORCE_NODEBOX;
  723. return false;
  724. }
  725. void ContentFeatures::updateTextures(ITextureSource *tsrc, IShaderSource *shdsrc,
  726. scene::IMeshManipulator *meshmanip, Client *client, const TextureSettings &tsettings)
  727. {
  728. // minimap pixel color - the average color of a texture
  729. if (tsettings.enable_minimap && !tiledef[0].name.empty())
  730. minimap_color = tsrc->getTextureAverageColor(tiledef[0].name);
  731. // Figure out the actual tiles to use
  732. TileDef tdef[6];
  733. for (u32 j = 0; j < 6; j++) {
  734. tdef[j] = tiledef[j];
  735. if (tdef[j].name.empty()) {
  736. tdef[j].name = "no_texture.png";
  737. tdef[j].backface_culling = false;
  738. }
  739. }
  740. // also the overlay tiles
  741. TileDef tdef_overlay[6];
  742. for (u32 j = 0; j < 6; j++)
  743. tdef_overlay[j] = tiledef_overlay[j];
  744. // also the special tiles
  745. TileDef tdef_spec[6];
  746. for (u32 j = 0; j < CF_SPECIAL_COUNT; j++) {
  747. tdef_spec[j] = tiledef_special[j];
  748. }
  749. bool is_liquid = false;
  750. if (alpha == ALPHAMODE_LEGACY_COMPAT) {
  751. // Before working with the alpha mode, resolve any legacy kludges
  752. alpha = textureAlphaCheck(tsrc, tdef, 6) ? ALPHAMODE_CLIP : ALPHAMODE_OPAQUE;
  753. }
  754. MaterialType material_type = alpha == ALPHAMODE_OPAQUE ?
  755. TILE_MATERIAL_OPAQUE : (alpha == ALPHAMODE_CLIP ? TILE_MATERIAL_BASIC :
  756. TILE_MATERIAL_ALPHA);
  757. switch (drawtype) {
  758. default:
  759. case NDT_NORMAL:
  760. solidness = 2;
  761. break;
  762. case NDT_AIRLIKE:
  763. solidness = 0;
  764. break;
  765. case NDT_LIQUID:
  766. if (tsettings.opaque_water)
  767. alpha = ALPHAMODE_OPAQUE;
  768. solidness = 1;
  769. is_liquid = true;
  770. break;
  771. case NDT_FLOWINGLIQUID:
  772. solidness = 0;
  773. if (tsettings.opaque_water)
  774. alpha = ALPHAMODE_OPAQUE;
  775. is_liquid = true;
  776. break;
  777. case NDT_GLASSLIKE:
  778. solidness = 0;
  779. visual_solidness = 1;
  780. break;
  781. case NDT_GLASSLIKE_FRAMED:
  782. solidness = 0;
  783. visual_solidness = 1;
  784. break;
  785. case NDT_GLASSLIKE_FRAMED_OPTIONAL:
  786. solidness = 0;
  787. visual_solidness = 1;
  788. drawtype = tsettings.connected_glass ? NDT_GLASSLIKE_FRAMED : NDT_GLASSLIKE;
  789. break;
  790. case NDT_ALLFACES:
  791. solidness = 0;
  792. visual_solidness = 1;
  793. break;
  794. case NDT_ALLFACES_OPTIONAL:
  795. if (tsettings.leaves_style == LEAVES_FANCY) {
  796. drawtype = NDT_ALLFACES;
  797. solidness = 0;
  798. visual_solidness = 1;
  799. } else if (tsettings.leaves_style == LEAVES_SIMPLE) {
  800. for (u32 j = 0; j < 6; j++) {
  801. if (!tdef_spec[j].name.empty())
  802. tdef[j].name = tdef_spec[j].name;
  803. }
  804. drawtype = NDT_GLASSLIKE;
  805. solidness = 0;
  806. visual_solidness = 1;
  807. } else {
  808. if (waving >= 1) {
  809. // waving nodes must make faces so there are no gaps
  810. drawtype = NDT_ALLFACES;
  811. solidness = 0;
  812. visual_solidness = 1;
  813. } else {
  814. drawtype = NDT_NORMAL;
  815. solidness = 2;
  816. }
  817. for (TileDef &td : tdef)
  818. td.name += std::string("^[noalpha");
  819. }
  820. if (waving >= 1)
  821. material_type = TILE_MATERIAL_WAVING_LEAVES;
  822. break;
  823. case NDT_PLANTLIKE:
  824. solidness = 0;
  825. if (waving >= 1)
  826. material_type = TILE_MATERIAL_WAVING_PLANTS;
  827. break;
  828. case NDT_FIRELIKE:
  829. solidness = 0;
  830. break;
  831. case NDT_MESH:
  832. case NDT_NODEBOX:
  833. solidness = 0;
  834. if (waving == 1) {
  835. material_type = TILE_MATERIAL_WAVING_PLANTS;
  836. } else if (waving == 2) {
  837. material_type = TILE_MATERIAL_WAVING_LEAVES;
  838. } else if (waving == 3) {
  839. material_type = alpha == ALPHAMODE_OPAQUE ?
  840. TILE_MATERIAL_WAVING_LIQUID_OPAQUE : (alpha == ALPHAMODE_CLIP ?
  841. TILE_MATERIAL_WAVING_LIQUID_BASIC : TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT);
  842. }
  843. break;
  844. case NDT_TORCHLIKE:
  845. case NDT_SIGNLIKE:
  846. case NDT_FENCELIKE:
  847. case NDT_RAILLIKE:
  848. solidness = 0;
  849. break;
  850. case NDT_PLANTLIKE_ROOTED:
  851. solidness = 2;
  852. break;
  853. }
  854. if (is_liquid) {
  855. if (waving == 3) {
  856. material_type = alpha == ALPHAMODE_OPAQUE ?
  857. TILE_MATERIAL_WAVING_LIQUID_OPAQUE : (alpha == ALPHAMODE_CLIP ?
  858. TILE_MATERIAL_WAVING_LIQUID_BASIC : TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT);
  859. } else {
  860. material_type = alpha == ALPHAMODE_OPAQUE ? TILE_MATERIAL_LIQUID_OPAQUE :
  861. TILE_MATERIAL_LIQUID_TRANSPARENT;
  862. }
  863. }
  864. u32 tile_shader = shdsrc->getShader("nodes_shader", material_type, drawtype);
  865. MaterialType overlay_material = material_type;
  866. if (overlay_material == TILE_MATERIAL_OPAQUE)
  867. overlay_material = TILE_MATERIAL_BASIC;
  868. else if (overlay_material == TILE_MATERIAL_LIQUID_OPAQUE)
  869. overlay_material = TILE_MATERIAL_LIQUID_TRANSPARENT;
  870. u32 overlay_shader = shdsrc->getShader("nodes_shader", overlay_material, drawtype);
  871. // Tiles (fill in f->tiles[])
  872. for (u16 j = 0; j < 6; j++) {
  873. tiles[j].world_aligned = isWorldAligned(tdef[j].align_style,
  874. tsettings.world_aligned_mode, drawtype);
  875. fillTileAttribs(tsrc, &tiles[j].layers[0], tiles[j], tdef[j],
  876. color, material_type, tile_shader,
  877. tdef[j].backface_culling, tsettings);
  878. if (!tdef_overlay[j].name.empty())
  879. fillTileAttribs(tsrc, &tiles[j].layers[1], tiles[j], tdef_overlay[j],
  880. color, overlay_material, overlay_shader,
  881. tdef[j].backface_culling, tsettings);
  882. }
  883. MaterialType special_material = material_type;
  884. if (drawtype == NDT_PLANTLIKE_ROOTED) {
  885. if (waving == 1)
  886. special_material = TILE_MATERIAL_WAVING_PLANTS;
  887. else if (waving == 2)
  888. special_material = TILE_MATERIAL_WAVING_LEAVES;
  889. }
  890. u32 special_shader = shdsrc->getShader("nodes_shader", special_material, drawtype);
  891. // Special tiles (fill in f->special_tiles[])
  892. for (u16 j = 0; j < CF_SPECIAL_COUNT; j++)
  893. fillTileAttribs(tsrc, &special_tiles[j].layers[0], special_tiles[j], tdef_spec[j],
  894. color, special_material, special_shader,
  895. tdef_spec[j].backface_culling, tsettings);
  896. if (param_type_2 == CPT2_COLOR ||
  897. param_type_2 == CPT2_COLORED_FACEDIR ||
  898. param_type_2 == CPT2_COLORED_4DIR ||
  899. param_type_2 == CPT2_COLORED_WALLMOUNTED ||
  900. param_type_2 == CPT2_COLORED_DEGROTATE)
  901. palette = tsrc->getPalette(palette_name);
  902. if (drawtype == NDT_MESH && !mesh.empty()) {
  903. // Meshnode drawtype
  904. // Read the mesh and apply scale
  905. mesh_ptr[0] = client->getMesh(mesh);
  906. if (mesh_ptr[0]){
  907. v3f scale = v3f(1.0, 1.0, 1.0) * BS * visual_scale;
  908. scaleMesh(mesh_ptr[0], scale);
  909. recalculateBoundingBox(mesh_ptr[0]);
  910. meshmanip->recalculateNormals(mesh_ptr[0], true, false);
  911. }
  912. }
  913. //Cache 6dfacedir and wallmounted rotated clones of meshes
  914. if (tsettings.enable_mesh_cache && mesh_ptr[0] &&
  915. (param_type_2 == CPT2_FACEDIR
  916. || param_type_2 == CPT2_COLORED_FACEDIR)) {
  917. for (u16 j = 1; j < 24; j++) {
  918. mesh_ptr[j] = cloneMesh(mesh_ptr[0]);
  919. rotateMeshBy6dFacedir(mesh_ptr[j], j);
  920. recalculateBoundingBox(mesh_ptr[j]);
  921. meshmanip->recalculateNormals(mesh_ptr[j], true, false);
  922. }
  923. } else if (tsettings.enable_mesh_cache && mesh_ptr[0] &&
  924. (param_type_2 == CPT2_4DIR
  925. || param_type_2 == CPT2_COLORED_4DIR)) {
  926. for (u16 j = 1; j < 4; j++) {
  927. mesh_ptr[j] = cloneMesh(mesh_ptr[0]);
  928. rotateMeshBy6dFacedir(mesh_ptr[j], j);
  929. recalculateBoundingBox(mesh_ptr[j]);
  930. meshmanip->recalculateNormals(mesh_ptr[j], true, false);
  931. }
  932. } else if (tsettings.enable_mesh_cache && mesh_ptr[0]
  933. && (param_type_2 == CPT2_WALLMOUNTED ||
  934. param_type_2 == CPT2_COLORED_WALLMOUNTED)) {
  935. static const u8 wm_to_6d[6] = { 20, 0, 16 + 1, 12 + 3, 8, 4 + 2 };
  936. for (u16 j = 1; j < 6; j++) {
  937. mesh_ptr[j] = cloneMesh(mesh_ptr[0]);
  938. rotateMeshBy6dFacedir(mesh_ptr[j], wm_to_6d[j]);
  939. recalculateBoundingBox(mesh_ptr[j]);
  940. meshmanip->recalculateNormals(mesh_ptr[j], true, false);
  941. }
  942. rotateMeshBy6dFacedir(mesh_ptr[0], wm_to_6d[0]);
  943. recalculateBoundingBox(mesh_ptr[0]);
  944. meshmanip->recalculateNormals(mesh_ptr[0], true, false);
  945. }
  946. }
  947. #endif
  948. /*
  949. NodeDefManager
  950. */
  951. NodeDefManager::NodeDefManager()
  952. {
  953. clear();
  954. }
  955. NodeDefManager::~NodeDefManager()
  956. {
  957. #ifndef SERVER
  958. for (ContentFeatures &f : m_content_features) {
  959. for (auto &j : f.mesh_ptr) {
  960. if (j)
  961. j->drop();
  962. }
  963. }
  964. #endif
  965. }
  966. void NodeDefManager::clear()
  967. {
  968. m_content_features.clear();
  969. m_name_id_mapping.clear();
  970. m_name_id_mapping_with_aliases.clear();
  971. m_group_to_items.clear();
  972. m_next_id = 0;
  973. m_selection_box_union.reset(0,0,0);
  974. m_selection_box_int_union.reset(0,0,0);
  975. resetNodeResolveState();
  976. u32 initial_length = 0;
  977. initial_length = MYMAX(initial_length, CONTENT_UNKNOWN + 1);
  978. initial_length = MYMAX(initial_length, CONTENT_AIR + 1);
  979. initial_length = MYMAX(initial_length, CONTENT_IGNORE + 1);
  980. m_content_features.resize(initial_length);
  981. // Set CONTENT_UNKNOWN
  982. {
  983. ContentFeatures f;
  984. f.name = "unknown";
  985. for (int t = 0; t < 6; t++)
  986. f.tiledef[t].name = "unknown_node.png";
  987. // Insert directly into containers
  988. content_t c = CONTENT_UNKNOWN;
  989. m_content_features[c] = f;
  990. for (u32 ci = 0; ci <= CONTENT_MAX; ci++)
  991. m_content_lighting_flag_cache[ci] = f.getLightingFlags();
  992. addNameIdMapping(c, f.name);
  993. }
  994. // Set CONTENT_AIR
  995. {
  996. ContentFeatures f;
  997. f.name = "air";
  998. f.drawtype = NDT_AIRLIKE;
  999. f.param_type = CPT_LIGHT;
  1000. f.light_propagates = true;
  1001. f.sunlight_propagates = true;
  1002. f.walkable = false;
  1003. f.pointable = false;
  1004. f.diggable = false;
  1005. f.buildable_to = true;
  1006. f.floodable = true;
  1007. f.is_ground_content = true;
  1008. // Insert directly into containers
  1009. content_t c = CONTENT_AIR;
  1010. m_content_features[c] = f;
  1011. m_content_lighting_flag_cache[c] = f.getLightingFlags();
  1012. addNameIdMapping(c, f.name);
  1013. }
  1014. // Set CONTENT_IGNORE
  1015. {
  1016. ContentFeatures f;
  1017. f.name = "ignore";
  1018. f.drawtype = NDT_AIRLIKE;
  1019. f.param_type = CPT_NONE;
  1020. f.light_propagates = false;
  1021. f.sunlight_propagates = false;
  1022. f.walkable = false;
  1023. f.pointable = false;
  1024. f.diggable = false;
  1025. f.buildable_to = true; // A way to remove accidental CONTENT_IGNOREs
  1026. f.is_ground_content = true;
  1027. // Insert directly into containers
  1028. content_t c = CONTENT_IGNORE;
  1029. m_content_features[c] = f;
  1030. m_content_lighting_flag_cache[c] = f.getLightingFlags();
  1031. addNameIdMapping(c, f.name);
  1032. }
  1033. }
  1034. bool NodeDefManager::getId(const std::string &name, content_t &result) const
  1035. {
  1036. std::unordered_map<std::string, content_t>::const_iterator
  1037. i = m_name_id_mapping_with_aliases.find(name);
  1038. if(i == m_name_id_mapping_with_aliases.end())
  1039. return false;
  1040. result = i->second;
  1041. return true;
  1042. }
  1043. content_t NodeDefManager::getId(const std::string &name) const
  1044. {
  1045. content_t id = CONTENT_IGNORE;
  1046. getId(name, id);
  1047. return id;
  1048. }
  1049. bool NodeDefManager::getIds(const std::string &name,
  1050. std::vector<content_t> &result) const
  1051. {
  1052. //TimeTaker t("getIds", NULL, PRECISION_MICRO);
  1053. if (name.substr(0,6) != "group:") {
  1054. content_t id = CONTENT_IGNORE;
  1055. bool exists = getId(name, id);
  1056. if (exists)
  1057. result.push_back(id);
  1058. return exists;
  1059. }
  1060. std::string group = name.substr(6);
  1061. std::unordered_map<std::string, std::vector<content_t>>::const_iterator
  1062. i = m_group_to_items.find(group);
  1063. if (i == m_group_to_items.end())
  1064. return true;
  1065. const std::vector<content_t> &items = i->second;
  1066. result.insert(result.end(), items.begin(), items.end());
  1067. //printf("getIds: %dus\n", t.stop());
  1068. return true;
  1069. }
  1070. const ContentFeatures& NodeDefManager::get(const std::string &name) const
  1071. {
  1072. content_t id = CONTENT_UNKNOWN;
  1073. getId(name, id);
  1074. return get(id);
  1075. }
  1076. // returns CONTENT_IGNORE if no free ID found
  1077. content_t NodeDefManager::allocateId()
  1078. {
  1079. for (content_t id = m_next_id;
  1080. id >= m_next_id; // overflow?
  1081. ++id) {
  1082. while (id >= m_content_features.size()) {
  1083. m_content_features.emplace_back();
  1084. }
  1085. const ContentFeatures &f = m_content_features[id];
  1086. if (f.name.empty()) {
  1087. m_next_id = id + 1;
  1088. return id;
  1089. }
  1090. }
  1091. // If we arrive here, an overflow occurred in id.
  1092. // That means no ID was found
  1093. return CONTENT_IGNORE;
  1094. }
  1095. /*!
  1096. * Returns the smallest box that contains all boxes
  1097. * in the vector. Box_union is expanded.
  1098. * @param[in] boxes the vector containing the boxes
  1099. * @param[in, out] box_union the union of the arguments
  1100. */
  1101. void boxVectorUnion(const std::vector<aabb3f> &boxes, aabb3f *box_union)
  1102. {
  1103. for (const aabb3f &box : boxes) {
  1104. box_union->addInternalBox(box);
  1105. }
  1106. }
  1107. /*!
  1108. * Returns a box that contains the nodebox in every case.
  1109. * The argument node_union is expanded.
  1110. * @param[in] nodebox the nodebox to be measured
  1111. * @param[in] features used to decide whether the nodebox
  1112. * can be rotated
  1113. * @param[in, out] box_union the union of the arguments
  1114. */
  1115. void getNodeBoxUnion(const NodeBox &nodebox, const ContentFeatures &features,
  1116. aabb3f *box_union)
  1117. {
  1118. switch(nodebox.type) {
  1119. case NODEBOX_FIXED:
  1120. case NODEBOX_LEVELED: {
  1121. // Raw union
  1122. aabb3f half_processed(0, 0, 0, 0, 0, 0);
  1123. boxVectorUnion(nodebox.fixed, &half_processed);
  1124. // Set leveled boxes to maximal
  1125. if (nodebox.type == NODEBOX_LEVELED) {
  1126. half_processed.MaxEdge.Y = +BS / 2;
  1127. }
  1128. if (features.param_type_2 == CPT2_FACEDIR ||
  1129. features.param_type_2 == CPT2_COLORED_FACEDIR ||
  1130. features.param_type_2 == CPT2_4DIR ||
  1131. features.param_type_2 == CPT2_COLORED_4DIR) {
  1132. // Get maximal coordinate
  1133. f32 coords[] = {
  1134. fabsf(half_processed.MinEdge.X),
  1135. fabsf(half_processed.MinEdge.Y),
  1136. fabsf(half_processed.MinEdge.Z),
  1137. fabsf(half_processed.MaxEdge.X),
  1138. fabsf(half_processed.MaxEdge.Y),
  1139. fabsf(half_processed.MaxEdge.Z) };
  1140. f32 max = 0;
  1141. for (float coord : coords) {
  1142. if (max < coord) {
  1143. max = coord;
  1144. }
  1145. }
  1146. // Add the union of all possible rotated boxes
  1147. box_union->addInternalPoint(-max, -max, -max);
  1148. box_union->addInternalPoint(+max, +max, +max);
  1149. } else {
  1150. box_union->addInternalBox(half_processed);
  1151. }
  1152. break;
  1153. }
  1154. case NODEBOX_WALLMOUNTED: {
  1155. // Add fix boxes
  1156. box_union->addInternalBox(nodebox.wall_top);
  1157. box_union->addInternalBox(nodebox.wall_bottom);
  1158. // Find maximal coordinate in the X-Z plane
  1159. f32 coords[] = {
  1160. fabsf(nodebox.wall_side.MinEdge.X),
  1161. fabsf(nodebox.wall_side.MinEdge.Z),
  1162. fabsf(nodebox.wall_side.MaxEdge.X),
  1163. fabsf(nodebox.wall_side.MaxEdge.Z) };
  1164. f32 max = 0;
  1165. for (float coord : coords) {
  1166. if (max < coord) {
  1167. max = coord;
  1168. }
  1169. }
  1170. // Add the union of all possible rotated boxes
  1171. box_union->addInternalPoint(-max, nodebox.wall_side.MinEdge.Y, -max);
  1172. box_union->addInternalPoint(max, nodebox.wall_side.MaxEdge.Y, max);
  1173. break;
  1174. }
  1175. case NODEBOX_CONNECTED: {
  1176. const auto &c = nodebox.getConnected();
  1177. // Add all possible connected boxes
  1178. boxVectorUnion(nodebox.fixed, box_union);
  1179. boxVectorUnion(c.connect_top, box_union);
  1180. boxVectorUnion(c.connect_bottom, box_union);
  1181. boxVectorUnion(c.connect_front, box_union);
  1182. boxVectorUnion(c.connect_left, box_union);
  1183. boxVectorUnion(c.connect_back, box_union);
  1184. boxVectorUnion(c.connect_right, box_union);
  1185. boxVectorUnion(c.disconnected_top, box_union);
  1186. boxVectorUnion(c.disconnected_bottom, box_union);
  1187. boxVectorUnion(c.disconnected_front, box_union);
  1188. boxVectorUnion(c.disconnected_left, box_union);
  1189. boxVectorUnion(c.disconnected_back, box_union);
  1190. boxVectorUnion(c.disconnected_right, box_union);
  1191. boxVectorUnion(c.disconnected, box_union);
  1192. boxVectorUnion(c.disconnected_sides, box_union);
  1193. break;
  1194. }
  1195. default: {
  1196. // NODEBOX_REGULAR
  1197. box_union->addInternalPoint(-BS / 2, -BS / 2, -BS / 2);
  1198. box_union->addInternalPoint(+BS / 2, +BS / 2, +BS / 2);
  1199. }
  1200. }
  1201. }
  1202. inline void NodeDefManager::fixSelectionBoxIntUnion()
  1203. {
  1204. m_selection_box_int_union.MinEdge.X = floorf(
  1205. m_selection_box_union.MinEdge.X / BS + 0.5f);
  1206. m_selection_box_int_union.MinEdge.Y = floorf(
  1207. m_selection_box_union.MinEdge.Y / BS + 0.5f);
  1208. m_selection_box_int_union.MinEdge.Z = floorf(
  1209. m_selection_box_union.MinEdge.Z / BS + 0.5f);
  1210. m_selection_box_int_union.MaxEdge.X = ceilf(
  1211. m_selection_box_union.MaxEdge.X / BS - 0.5f);
  1212. m_selection_box_int_union.MaxEdge.Y = ceilf(
  1213. m_selection_box_union.MaxEdge.Y / BS - 0.5f);
  1214. m_selection_box_int_union.MaxEdge.Z = ceilf(
  1215. m_selection_box_union.MaxEdge.Z / BS - 0.5f);
  1216. }
  1217. void NodeDefManager::eraseIdFromGroups(content_t id)
  1218. {
  1219. // For all groups in m_group_to_items...
  1220. for (auto iter_groups = m_group_to_items.begin();
  1221. iter_groups != m_group_to_items.end();) {
  1222. // Get the group items vector.
  1223. std::vector<content_t> &items = iter_groups->second;
  1224. // Remove any occurrence of the id in the group items vector.
  1225. items.erase(std::remove(items.begin(), items.end(), id), items.end());
  1226. // If group is empty, erase its vector from the map.
  1227. if (items.empty())
  1228. iter_groups = m_group_to_items.erase(iter_groups);
  1229. else
  1230. ++iter_groups;
  1231. }
  1232. }
  1233. // IWritableNodeDefManager
  1234. content_t NodeDefManager::set(const std::string &name, const ContentFeatures &def)
  1235. {
  1236. // Pre-conditions
  1237. assert(!name.empty());
  1238. assert(name != "ignore");
  1239. assert(name == def.name);
  1240. content_t id = CONTENT_IGNORE;
  1241. if (!m_name_id_mapping.getId(name, id)) { // ignore aliases
  1242. // Get new id
  1243. id = allocateId();
  1244. if (id == CONTENT_IGNORE) {
  1245. warningstream << "NodeDefManager: Absolute "
  1246. "limit reached" << std::endl;
  1247. return CONTENT_IGNORE;
  1248. }
  1249. assert(id != CONTENT_IGNORE);
  1250. addNameIdMapping(id, name);
  1251. }
  1252. // If there is already ContentFeatures registered for this id, clear old groups
  1253. if (id < m_content_features.size())
  1254. eraseIdFromGroups(id);
  1255. m_content_features[id] = def;
  1256. m_content_features[id].floats = itemgroup_get(def.groups, "float") != 0;
  1257. m_content_lighting_flag_cache[id] = def.getLightingFlags();
  1258. verbosestream << "NodeDefManager: registering content id \"" << id
  1259. << "\": name=\"" << def.name << "\""<<std::endl;
  1260. getNodeBoxUnion(def.selection_box, def, &m_selection_box_union);
  1261. fixSelectionBoxIntUnion();
  1262. // Add this content to the list of all groups it belongs to
  1263. for (const auto &group : def.groups) {
  1264. const std::string &group_name = group.first;
  1265. m_group_to_items[group_name].push_back(id);
  1266. }
  1267. return id;
  1268. }
  1269. content_t NodeDefManager::allocateDummy(const std::string &name)
  1270. {
  1271. assert(!name.empty()); // Pre-condition
  1272. ContentFeatures f;
  1273. f.name = name;
  1274. return set(name, f);
  1275. }
  1276. void NodeDefManager::removeNode(const std::string &name)
  1277. {
  1278. // Pre-condition
  1279. assert(!name.empty());
  1280. // Erase name from name ID mapping
  1281. content_t id = CONTENT_IGNORE;
  1282. if (m_name_id_mapping.getId(name, id)) {
  1283. m_name_id_mapping.eraseName(name);
  1284. m_name_id_mapping_with_aliases.erase(name);
  1285. }
  1286. eraseIdFromGroups(id);
  1287. }
  1288. void NodeDefManager::updateAliases(IItemDefManager *idef)
  1289. {
  1290. std::set<std::string> all;
  1291. idef->getAll(all);
  1292. m_name_id_mapping_with_aliases.clear();
  1293. for (const std::string &name : all) {
  1294. const std::string &convert_to = idef->getAlias(name);
  1295. content_t id;
  1296. if (m_name_id_mapping.getId(convert_to, id)) {
  1297. m_name_id_mapping_with_aliases.insert(
  1298. std::make_pair(name, id));
  1299. }
  1300. }
  1301. }
  1302. void NodeDefManager::applyTextureOverrides(const std::vector<TextureOverride> &overrides)
  1303. {
  1304. infostream << "NodeDefManager::applyTextureOverrides(): Applying "
  1305. "overrides to textures" << std::endl;
  1306. for (const TextureOverride& texture_override : overrides) {
  1307. content_t id;
  1308. if (!getId(texture_override.id, id))
  1309. continue; // Ignore unknown node
  1310. ContentFeatures &nodedef = m_content_features[id];
  1311. auto apply = [&] (TileDef &tile) {
  1312. tile.name = texture_override.texture;
  1313. if (texture_override.world_scale > 0) {
  1314. tile.align_style = ALIGN_STYLE_WORLD;
  1315. tile.scale = texture_override.world_scale;
  1316. }
  1317. };
  1318. // Override tiles
  1319. if (texture_override.hasTarget(OverrideTarget::TOP))
  1320. apply(nodedef.tiledef[0]);
  1321. if (texture_override.hasTarget(OverrideTarget::BOTTOM))
  1322. apply(nodedef.tiledef[1]);
  1323. if (texture_override.hasTarget(OverrideTarget::RIGHT))
  1324. apply(nodedef.tiledef[2]);
  1325. if (texture_override.hasTarget(OverrideTarget::LEFT))
  1326. apply(nodedef.tiledef[3]);
  1327. if (texture_override.hasTarget(OverrideTarget::BACK))
  1328. apply(nodedef.tiledef[4]);
  1329. if (texture_override.hasTarget(OverrideTarget::FRONT))
  1330. apply(nodedef.tiledef[5]);
  1331. // Override special tiles, if applicable
  1332. if (texture_override.hasTarget(OverrideTarget::SPECIAL_1))
  1333. apply(nodedef.tiledef_special[0]);
  1334. if (texture_override.hasTarget(OverrideTarget::SPECIAL_2))
  1335. apply(nodedef.tiledef_special[1]);
  1336. if (texture_override.hasTarget(OverrideTarget::SPECIAL_3))
  1337. apply(nodedef.tiledef_special[2]);
  1338. if (texture_override.hasTarget(OverrideTarget::SPECIAL_4))
  1339. apply(nodedef.tiledef_special[3]);
  1340. if (texture_override.hasTarget(OverrideTarget::SPECIAL_5))
  1341. apply(nodedef.tiledef_special[4]);
  1342. if (texture_override.hasTarget(OverrideTarget::SPECIAL_6))
  1343. apply(nodedef.tiledef_special[5]);
  1344. }
  1345. }
  1346. void NodeDefManager::updateTextures(IGameDef *gamedef, void *progress_callback_args)
  1347. {
  1348. #ifndef SERVER
  1349. infostream << "NodeDefManager::updateTextures(): Updating "
  1350. "textures in node definitions" << std::endl;
  1351. Client *client = (Client *)gamedef;
  1352. ITextureSource *tsrc = client->tsrc();
  1353. IShaderSource *shdsrc = client->getShaderSource();
  1354. auto smgr = client->getSceneManager();
  1355. scene::IMeshManipulator *meshmanip = smgr->getMeshManipulator();
  1356. TextureSettings tsettings;
  1357. tsettings.readSettings();
  1358. u32 size = m_content_features.size();
  1359. for (u32 i = 0; i < size; i++) {
  1360. ContentFeatures *f = &(m_content_features[i]);
  1361. f->updateTextures(tsrc, shdsrc, meshmanip, client, tsettings);
  1362. client->showUpdateProgressTexture(progress_callback_args, i, size);
  1363. }
  1364. #endif
  1365. }
  1366. void NodeDefManager::serialize(std::ostream &os, u16 protocol_version) const
  1367. {
  1368. writeU8(os, 1); // version
  1369. u16 count = 0;
  1370. std::ostringstream os2(std::ios::binary);
  1371. for (u32 i = 0; i < m_content_features.size(); i++) {
  1372. if (i == CONTENT_IGNORE || i == CONTENT_AIR
  1373. || i == CONTENT_UNKNOWN)
  1374. continue;
  1375. const ContentFeatures *f = &m_content_features[i];
  1376. if (f->name.empty())
  1377. continue;
  1378. writeU16(os2, i);
  1379. // Wrap it in a string to allow different lengths without
  1380. // strict version incompatibilities
  1381. std::ostringstream wrapper_os(std::ios::binary);
  1382. f->serialize(wrapper_os, protocol_version);
  1383. os2<<serializeString16(wrapper_os.str());
  1384. // must not overflow
  1385. u16 next = count + 1;
  1386. FATAL_ERROR_IF(next < count, "Overflow");
  1387. count++;
  1388. }
  1389. writeU16(os, count);
  1390. os << serializeString32(os2.str());
  1391. }
  1392. void NodeDefManager::deSerialize(std::istream &is, u16 protocol_version)
  1393. {
  1394. clear();
  1395. if (readU8(is) < 1)
  1396. throw SerializationError("unsupported NodeDefinitionManager version");
  1397. u16 count = readU16(is);
  1398. std::istringstream is2(deSerializeString32(is), std::ios::binary);
  1399. ContentFeatures f;
  1400. for (u16 n = 0; n < count; n++) {
  1401. u16 i = readU16(is2);
  1402. // Read it from the string wrapper
  1403. std::string wrapper = deSerializeString16(is2);
  1404. std::istringstream wrapper_is(wrapper, std::ios::binary);
  1405. f.deSerialize(wrapper_is, protocol_version);
  1406. // Check error conditions
  1407. if (i == CONTENT_IGNORE || i == CONTENT_AIR || i == CONTENT_UNKNOWN) {
  1408. warningstream << "NodeDefManager::deSerialize(): "
  1409. "not changing builtin node " << i << std::endl;
  1410. continue;
  1411. }
  1412. if (f.name.empty()) {
  1413. warningstream << "NodeDefManager::deSerialize(): "
  1414. "received empty name" << std::endl;
  1415. continue;
  1416. }
  1417. // Ignore aliases
  1418. u16 existing_id;
  1419. if (m_name_id_mapping.getId(f.name, existing_id) && i != existing_id) {
  1420. warningstream << "NodeDefManager::deSerialize(): "
  1421. "already defined with different ID: " << f.name << std::endl;
  1422. continue;
  1423. }
  1424. // All is ok, add node definition with the requested ID
  1425. if (i >= m_content_features.size())
  1426. m_content_features.resize((u32)(i) + 1);
  1427. m_content_features[i] = f;
  1428. m_content_features[i].floats = itemgroup_get(f.groups, "float") != 0;
  1429. m_content_lighting_flag_cache[i] = f.getLightingFlags();
  1430. addNameIdMapping(i, f.name);
  1431. TRACESTREAM(<< "NodeDef: deserialized " << f.name << std::endl);
  1432. getNodeBoxUnion(f.selection_box, f, &m_selection_box_union);
  1433. fixSelectionBoxIntUnion();
  1434. }
  1435. // Since liquid_alternative_flowing_id and liquid_alternative_source_id
  1436. // are not sent, resolve them client-side too.
  1437. resolveCrossrefs();
  1438. }
  1439. void NodeDefManager::addNameIdMapping(content_t i, const std::string &name)
  1440. {
  1441. m_name_id_mapping.set(i, name);
  1442. m_name_id_mapping_with_aliases.emplace(name, i);
  1443. }
  1444. NodeDefManager *createNodeDefManager()
  1445. {
  1446. return new NodeDefManager();
  1447. }
  1448. void NodeDefManager::pendNodeResolve(NodeResolver *nr) const
  1449. {
  1450. nr->m_ndef = this;
  1451. if (m_node_registration_complete)
  1452. nr->nodeResolveInternal();
  1453. else
  1454. m_pending_resolve_callbacks.push_back(nr);
  1455. }
  1456. bool NodeDefManager::cancelNodeResolveCallback(NodeResolver *nr) const
  1457. {
  1458. size_t len = m_pending_resolve_callbacks.size();
  1459. for (size_t i = 0; i != len; i++) {
  1460. if (nr != m_pending_resolve_callbacks[i])
  1461. continue;
  1462. len--;
  1463. m_pending_resolve_callbacks[i] = m_pending_resolve_callbacks[len];
  1464. m_pending_resolve_callbacks.resize(len);
  1465. return true;
  1466. }
  1467. return false;
  1468. }
  1469. void NodeDefManager::runNodeResolveCallbacks()
  1470. {
  1471. for (size_t i = 0; i != m_pending_resolve_callbacks.size(); i++) {
  1472. NodeResolver *nr = m_pending_resolve_callbacks[i];
  1473. nr->nodeResolveInternal();
  1474. }
  1475. m_pending_resolve_callbacks.clear();
  1476. }
  1477. void NodeDefManager::resetNodeResolveState()
  1478. {
  1479. m_node_registration_complete = false;
  1480. m_pending_resolve_callbacks.clear();
  1481. }
  1482. static void removeDupes(std::vector<content_t> &list)
  1483. {
  1484. std::sort(list.begin(), list.end());
  1485. auto new_end = std::unique(list.begin(), list.end());
  1486. list.erase(new_end, list.end());
  1487. }
  1488. void NodeDefManager::resolveCrossrefs()
  1489. {
  1490. for (ContentFeatures &f : m_content_features) {
  1491. if (f.isLiquid() || f.isLiquidRender()) {
  1492. f.liquid_alternative_flowing_id = getId(f.liquid_alternative_flowing);
  1493. f.liquid_alternative_source_id = getId(f.liquid_alternative_source);
  1494. continue;
  1495. }
  1496. if (f.drawtype != NDT_NODEBOX || f.node_box.type != NODEBOX_CONNECTED)
  1497. continue;
  1498. for (const std::string &name : f.connects_to) {
  1499. getIds(name, f.connects_to_ids);
  1500. }
  1501. removeDupes(f.connects_to_ids);
  1502. }
  1503. }
  1504. bool NodeDefManager::nodeboxConnects(MapNode from, MapNode to,
  1505. u8 connect_face) const
  1506. {
  1507. const ContentFeatures &f1 = get(from);
  1508. if ((f1.drawtype != NDT_NODEBOX) || (f1.node_box.type != NODEBOX_CONNECTED))
  1509. return false;
  1510. // lookup target in connected set
  1511. if (!CONTAINS(f1.connects_to_ids, to.param0))
  1512. return false;
  1513. const ContentFeatures &f2 = get(to);
  1514. if ((f2.drawtype == NDT_NODEBOX) && (f2.node_box.type == NODEBOX_CONNECTED))
  1515. // ignores actually looking if back connection exists
  1516. return CONTAINS(f2.connects_to_ids, from.param0);
  1517. // does to node declare usable faces?
  1518. if (f2.connect_sides > 0) {
  1519. if ((f2.param_type_2 == CPT2_FACEDIR ||
  1520. f2.param_type_2 == CPT2_COLORED_FACEDIR ||
  1521. f2.param_type_2 == CPT2_4DIR ||
  1522. f2.param_type_2 == CPT2_COLORED_4DIR)
  1523. && (connect_face >= 4)) {
  1524. static const u8 rot[33 * 4] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1525. 0, 0, 0, 0, 4, 32, 16, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1526. 0, // 4 - back
  1527. 8, 4, 32, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1528. 0, // 8 - right
  1529. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 8, 4, 32, 0,
  1530. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1531. 0, // 16 - front
  1532. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1533. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1534. 0, 0, 0, 0, 0, 0, 32, 16, 8, 4 // 32 - left
  1535. };
  1536. if (f2.param_type_2 == CPT2_FACEDIR ||
  1537. f2.param_type_2 == CPT2_COLORED_FACEDIR) {
  1538. return (f2.connect_sides
  1539. & rot[(connect_face * 4) + (to.param2 & 0x1F)]);
  1540. } else if (f2.param_type_2 == CPT2_4DIR ||
  1541. f2.param_type_2 == CPT2_COLORED_4DIR) {
  1542. return (f2.connect_sides
  1543. & rot[(connect_face * 4) + (to.param2 & 0x03)]);
  1544. }
  1545. }
  1546. return (f2.connect_sides & connect_face);
  1547. }
  1548. // the target is just a regular node, so connect no matter back connection
  1549. return true;
  1550. }
  1551. ////
  1552. //// NodeResolver
  1553. ////
  1554. NodeResolver::NodeResolver()
  1555. {
  1556. reset();
  1557. }
  1558. NodeResolver::~NodeResolver()
  1559. {
  1560. if (!m_resolve_done && m_ndef)
  1561. m_ndef->cancelNodeResolveCallback(this);
  1562. }
  1563. void NodeResolver::cloneTo(NodeResolver *res) const
  1564. {
  1565. FATAL_ERROR_IF(!m_resolve_done, "NodeResolver can only be cloned"
  1566. " after resolving has completed");
  1567. /* We don't actually do anything significant. Since the node resolving has
  1568. * already completed, the class that called us will already have the
  1569. * resolved IDs in its data structures (which it copies on its own) */
  1570. res->m_ndef = m_ndef;
  1571. res->m_resolve_done = true;
  1572. }
  1573. void NodeResolver::nodeResolveInternal()
  1574. {
  1575. m_nodenames_idx = 0;
  1576. m_nnlistsizes_idx = 0;
  1577. resolveNodeNames();
  1578. m_resolve_done = true;
  1579. m_nodenames.clear();
  1580. m_nnlistsizes.clear();
  1581. }
  1582. bool NodeResolver::getIdFromNrBacklog(content_t *result_out,
  1583. const std::string &node_alt, content_t c_fallback, bool error_on_fallback)
  1584. {
  1585. if (m_nodenames_idx == m_nodenames.size()) {
  1586. *result_out = c_fallback;
  1587. errorstream << "NodeResolver: no more nodes in list" << std::endl;
  1588. return false;
  1589. }
  1590. content_t c;
  1591. std::string name = m_nodenames[m_nodenames_idx++];
  1592. bool success = m_ndef->getId(name, c);
  1593. if (!success && !node_alt.empty()) {
  1594. name = node_alt;
  1595. success = m_ndef->getId(name, c);
  1596. }
  1597. if (!success) {
  1598. if (error_on_fallback)
  1599. errorstream << "NodeResolver: failed to resolve node name '" << name
  1600. << "'." << std::endl;
  1601. c = c_fallback;
  1602. }
  1603. *result_out = c;
  1604. return success;
  1605. }
  1606. bool NodeResolver::getIdsFromNrBacklog(std::vector<content_t> *result_out,
  1607. bool all_required, content_t c_fallback)
  1608. {
  1609. bool success = true;
  1610. if (m_nnlistsizes_idx == m_nnlistsizes.size()) {
  1611. errorstream << "NodeResolver: no more node lists" << std::endl;
  1612. return false;
  1613. }
  1614. size_t length = m_nnlistsizes[m_nnlistsizes_idx++];
  1615. while (length--) {
  1616. if (m_nodenames_idx == m_nodenames.size()) {
  1617. errorstream << "NodeResolver: no more nodes in list" << std::endl;
  1618. return false;
  1619. }
  1620. content_t c;
  1621. std::string &name = m_nodenames[m_nodenames_idx++];
  1622. if (name.substr(0,6) != "group:") {
  1623. if (m_ndef->getId(name, c)) {
  1624. result_out->push_back(c);
  1625. } else if (all_required) {
  1626. errorstream << "NodeResolver: failed to resolve node name '"
  1627. << name << "'." << std::endl;
  1628. result_out->push_back(c_fallback);
  1629. success = false;
  1630. }
  1631. } else {
  1632. m_ndef->getIds(name, *result_out);
  1633. }
  1634. }
  1635. return success;
  1636. }
  1637. void NodeResolver::reset(bool resolve_done)
  1638. {
  1639. m_nodenames.clear();
  1640. m_nodenames_idx = 0;
  1641. m_nnlistsizes.clear();
  1642. m_nnlistsizes_idx = 0;
  1643. m_resolve_done = resolve_done;
  1644. m_nodenames.reserve(16);
  1645. m_nnlistsizes.reserve(4);
  1646. }