nodedef.cpp 48 KB

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