nodedef.cpp 48 KB

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