nodedef.cpp 51 KB

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