nodedef.cpp 51 KB

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