nodedef.cpp 45 KB

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