nodedef.cpp 45 KB

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