nodedef.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "nodedef.h"
  17. #include "itemdef.h"
  18. #ifndef SERVER
  19. #include "mesh.h"
  20. #include "shader.h"
  21. #include "client.h"
  22. #include "client/renderingengine.h"
  23. #include "client/tile.h"
  24. #include <IMeshManipulator.h>
  25. #endif
  26. #include "log.h"
  27. #include "settings.h"
  28. #include "nameidmapping.h"
  29. #include "util/numeric.h"
  30. #include "util/serialize.h"
  31. #include "exceptions.h"
  32. #include "debug.h"
  33. #include "gamedef.h"
  34. #include "mapnode.h"
  35. #include <fstream> // Used in applyTextureOverrides()
  36. /*
  37. NodeBox
  38. */
  39. void NodeBox::reset()
  40. {
  41. type = NODEBOX_REGULAR;
  42. // default is empty
  43. fixed.clear();
  44. // default is sign/ladder-like
  45. wall_top = aabb3f(-BS/2, BS/2-BS/16., -BS/2, BS/2, BS/2, BS/2);
  46. wall_bottom = aabb3f(-BS/2, -BS/2, -BS/2, BS/2, -BS/2+BS/16., BS/2);
  47. wall_side = aabb3f(-BS/2, -BS/2, -BS/2, -BS/2+BS/16., BS/2, BS/2);
  48. // no default for other parts
  49. connect_top.clear();
  50. connect_bottom.clear();
  51. connect_front.clear();
  52. connect_left.clear();
  53. connect_back.clear();
  54. connect_right.clear();
  55. }
  56. void NodeBox::serialize(std::ostream &os, u16 protocol_version) const
  57. {
  58. // Protocol >= 21
  59. int version = 2;
  60. if (protocol_version >= 27)
  61. version = 3;
  62. writeU8(os, version);
  63. switch (type) {
  64. case NODEBOX_LEVELED:
  65. case NODEBOX_FIXED:
  66. if (version == 1)
  67. writeU8(os, NODEBOX_FIXED);
  68. else
  69. writeU8(os, type);
  70. writeU16(os, fixed.size());
  71. for (const aabb3f &nodebox : fixed) {
  72. writeV3F1000(os, nodebox.MinEdge);
  73. writeV3F1000(os, nodebox.MaxEdge);
  74. }
  75. break;
  76. case NODEBOX_WALLMOUNTED:
  77. writeU8(os, type);
  78. writeV3F1000(os, wall_top.MinEdge);
  79. writeV3F1000(os, wall_top.MaxEdge);
  80. writeV3F1000(os, wall_bottom.MinEdge);
  81. writeV3F1000(os, wall_bottom.MaxEdge);
  82. writeV3F1000(os, wall_side.MinEdge);
  83. writeV3F1000(os, wall_side.MaxEdge);
  84. break;
  85. case NODEBOX_CONNECTED:
  86. if (version <= 2) {
  87. // send old clients nodes that can't be walked through
  88. // to prevent abuse
  89. writeU8(os, NODEBOX_FIXED);
  90. writeU16(os, 1);
  91. writeV3F1000(os, v3f(-BS/2, -BS/2, -BS/2));
  92. writeV3F1000(os, v3f(BS/2, BS/2, BS/2));
  93. } else {
  94. writeU8(os, type);
  95. #define WRITEBOX(box) \
  96. writeU16(os, (box).size()); \
  97. for (const aabb3f &i: (box)) { \
  98. writeV3F1000(os, i.MinEdge); \
  99. writeV3F1000(os, i.MaxEdge); \
  100. };
  101. WRITEBOX(fixed);
  102. WRITEBOX(connect_top);
  103. WRITEBOX(connect_bottom);
  104. WRITEBOX(connect_front);
  105. WRITEBOX(connect_left);
  106. WRITEBOX(connect_back);
  107. WRITEBOX(connect_right);
  108. }
  109. break;
  110. default:
  111. writeU8(os, type);
  112. break;
  113. }
  114. }
  115. void NodeBox::deSerialize(std::istream &is)
  116. {
  117. int version = readU8(is);
  118. if (version < 1 || version > 3)
  119. throw SerializationError("unsupported NodeBox version");
  120. reset();
  121. type = (enum NodeBoxType)readU8(is);
  122. if(type == NODEBOX_FIXED || type == NODEBOX_LEVELED)
  123. {
  124. u16 fixed_count = readU16(is);
  125. while(fixed_count--)
  126. {
  127. aabb3f box;
  128. box.MinEdge = readV3F1000(is);
  129. box.MaxEdge = readV3F1000(is);
  130. fixed.push_back(box);
  131. }
  132. }
  133. else if(type == NODEBOX_WALLMOUNTED)
  134. {
  135. wall_top.MinEdge = readV3F1000(is);
  136. wall_top.MaxEdge = readV3F1000(is);
  137. wall_bottom.MinEdge = readV3F1000(is);
  138. wall_bottom.MaxEdge = readV3F1000(is);
  139. wall_side.MinEdge = readV3F1000(is);
  140. wall_side.MaxEdge = readV3F1000(is);
  141. }
  142. else if (type == NODEBOX_CONNECTED)
  143. {
  144. #define READBOXES(box) { \
  145. count = readU16(is); \
  146. (box).reserve(count); \
  147. while (count--) { \
  148. v3f min = readV3F1000(is); \
  149. v3f max = readV3F1000(is); \
  150. (box).emplace_back(min, max); }; }
  151. u16 count;
  152. READBOXES(fixed);
  153. READBOXES(connect_top);
  154. READBOXES(connect_bottom);
  155. READBOXES(connect_front);
  156. READBOXES(connect_left);
  157. READBOXES(connect_back);
  158. READBOXES(connect_right);
  159. }
  160. }
  161. /*
  162. TileDef
  163. */
  164. void TileDef::serialize(std::ostream &os, u16 protocol_version) const
  165. {
  166. if (protocol_version >= 30)
  167. writeU8(os, 4);
  168. else if (protocol_version >= 29)
  169. writeU8(os, 3);
  170. else if (protocol_version >= 26)
  171. writeU8(os, 2);
  172. else
  173. writeU8(os, 1);
  174. os << serializeString(name);
  175. animation.serialize(os, protocol_version);
  176. writeU8(os, backface_culling);
  177. if (protocol_version >= 26) {
  178. writeU8(os, tileable_horizontal);
  179. writeU8(os, tileable_vertical);
  180. }
  181. if (protocol_version >= 30) {
  182. writeU8(os, has_color);
  183. if (has_color) {
  184. writeU8(os, color.getRed());
  185. writeU8(os, color.getGreen());
  186. writeU8(os, color.getBlue());
  187. }
  188. }
  189. }
  190. void TileDef::deSerialize(std::istream &is, const u8 contenfeatures_version, const NodeDrawType drawtype)
  191. {
  192. int version = readU8(is);
  193. name = deSerializeString(is);
  194. animation.deSerialize(is, version >= 3 ? 29 : 26);
  195. if (version >= 1)
  196. backface_culling = readU8(is);
  197. if (version >= 2) {
  198. tileable_horizontal = readU8(is);
  199. tileable_vertical = readU8(is);
  200. }
  201. if (version >= 4) {
  202. has_color = readU8(is);
  203. if (has_color) {
  204. color.setRed(readU8(is));
  205. color.setGreen(readU8(is));
  206. color.setBlue(readU8(is));
  207. }
  208. }
  209. if ((contenfeatures_version < 8) &&
  210. ((drawtype == NDT_MESH) ||
  211. (drawtype == NDT_FIRELIKE) ||
  212. (drawtype == NDT_LIQUID) ||
  213. (drawtype == NDT_PLANTLIKE)))
  214. backface_culling = false;
  215. }
  216. /*
  217. SimpleSoundSpec serialization
  218. */
  219. static void serializeSimpleSoundSpec(const SimpleSoundSpec &ss,
  220. std::ostream &os, u8 version)
  221. {
  222. os<<serializeString(ss.name);
  223. writeF1000(os, ss.gain);
  224. if (version >= 11)
  225. writeF1000(os, ss.pitch);
  226. }
  227. static void deSerializeSimpleSoundSpec(SimpleSoundSpec &ss, std::istream &is, u8 version)
  228. {
  229. ss.name = deSerializeString(is);
  230. ss.gain = readF1000(is);
  231. if (version >= 11)
  232. ss.pitch = readF1000(is);
  233. }
  234. void TextureSettings::readSettings()
  235. {
  236. connected_glass = g_settings->getBool("connected_glass");
  237. opaque_water = g_settings->getBool("opaque_water");
  238. bool enable_shaders = g_settings->getBool("enable_shaders");
  239. bool enable_bumpmapping = g_settings->getBool("enable_bumpmapping");
  240. bool enable_parallax_occlusion = g_settings->getBool("enable_parallax_occlusion");
  241. bool smooth_lighting = g_settings->getBool("smooth_lighting");
  242. enable_mesh_cache = g_settings->getBool("enable_mesh_cache");
  243. enable_minimap = g_settings->getBool("enable_minimap");
  244. std::string leaves_style_str = g_settings->get("leaves_style");
  245. // Mesh cache is not supported in combination with smooth lighting
  246. if (smooth_lighting)
  247. enable_mesh_cache = false;
  248. use_normal_texture = enable_shaders &&
  249. (enable_bumpmapping || enable_parallax_occlusion);
  250. if (leaves_style_str == "fancy") {
  251. leaves_style = LEAVES_FANCY;
  252. } else if (leaves_style_str == "simple") {
  253. leaves_style = LEAVES_SIMPLE;
  254. } else {
  255. leaves_style = LEAVES_OPAQUE;
  256. }
  257. }
  258. /*
  259. ContentFeatures
  260. */
  261. ContentFeatures::ContentFeatures()
  262. {
  263. reset();
  264. }
  265. void ContentFeatures::reset()
  266. {
  267. /*
  268. Cached stuff
  269. */
  270. #ifndef SERVER
  271. solidness = 2;
  272. visual_solidness = 0;
  273. backface_culling = true;
  274. #endif
  275. has_on_construct = false;
  276. has_on_destruct = false;
  277. has_after_destruct = false;
  278. /*
  279. Actual data
  280. NOTE: Most of this is always overridden by the default values given
  281. in builtin.lua
  282. */
  283. name = "";
  284. groups.clear();
  285. // Unknown nodes can be dug
  286. groups["dig_immediate"] = 2;
  287. drawtype = NDT_NORMAL;
  288. mesh = "";
  289. #ifndef SERVER
  290. for (auto &i : mesh_ptr)
  291. i = NULL;
  292. minimap_color = video::SColor(0, 0, 0, 0);
  293. #endif
  294. visual_scale = 1.0;
  295. for (auto &i : tiledef)
  296. i = TileDef();
  297. for (auto &j : tiledef_special)
  298. j = TileDef();
  299. alpha = 255;
  300. post_effect_color = video::SColor(0, 0, 0, 0);
  301. param_type = CPT_NONE;
  302. param_type_2 = CPT2_NONE;
  303. is_ground_content = false;
  304. light_propagates = false;
  305. sunlight_propagates = false;
  306. walkable = true;
  307. pointable = true;
  308. diggable = true;
  309. climbable = false;
  310. buildable_to = false;
  311. floodable = false;
  312. rightclickable = true;
  313. leveled = 0;
  314. liquid_type = LIQUID_NONE;
  315. liquid_alternative_flowing = "";
  316. liquid_alternative_source = "";
  317. liquid_viscosity = 0;
  318. liquid_renewable = true;
  319. liquid_range = LIQUID_LEVEL_MAX+1;
  320. drowning = 0;
  321. light_source = 0;
  322. damage_per_second = 0;
  323. node_box = NodeBox();
  324. selection_box = NodeBox();
  325. collision_box = NodeBox();
  326. waving = 0;
  327. legacy_facedir_simple = false;
  328. legacy_wallmounted = false;
  329. sound_footstep = SimpleSoundSpec();
  330. sound_dig = SimpleSoundSpec("__group");
  331. sound_dug = SimpleSoundSpec();
  332. connects_to.clear();
  333. connects_to_ids.clear();
  334. connect_sides = 0;
  335. color = video::SColor(0xFFFFFFFF);
  336. palette_name = "";
  337. palette = NULL;
  338. }
  339. void ContentFeatures::serialize(std::ostream &os, u16 protocol_version) const
  340. {
  341. if (protocol_version < 31) {
  342. serializeOld(os, protocol_version);
  343. return;
  344. }
  345. // version
  346. u8 version = (protocol_version >= 34) ? 11 : 10;
  347. writeU8(os, version);
  348. // general
  349. os << serializeString(name);
  350. writeU16(os, groups.size());
  351. for (const auto &group : groups) {
  352. os << serializeString(group.first);
  353. writeS16(os, group.second);
  354. }
  355. writeU8(os, param_type);
  356. writeU8(os, param_type_2);
  357. // visual
  358. writeU8(os, drawtype);
  359. os << serializeString(mesh);
  360. writeF1000(os, visual_scale);
  361. writeU8(os, 6);
  362. for (const TileDef &td : tiledef)
  363. td.serialize(os, protocol_version);
  364. for (const TileDef &td : tiledef_overlay)
  365. td.serialize(os, protocol_version);
  366. writeU8(os, CF_SPECIAL_COUNT);
  367. for (const TileDef &td : tiledef_special) {
  368. td.serialize(os, protocol_version);
  369. }
  370. writeU8(os, alpha);
  371. writeU8(os, color.getRed());
  372. writeU8(os, color.getGreen());
  373. writeU8(os, color.getBlue());
  374. os << serializeString(palette_name);
  375. writeU8(os, waving);
  376. writeU8(os, connect_sides);
  377. writeU16(os, connects_to_ids.size());
  378. for (u16 connects_to_id : connects_to_ids)
  379. writeU16(os, connects_to_id);
  380. writeU8(os, post_effect_color.getAlpha());
  381. writeU8(os, post_effect_color.getRed());
  382. writeU8(os, post_effect_color.getGreen());
  383. writeU8(os, post_effect_color.getBlue());
  384. writeU8(os, leveled);
  385. // lighting
  386. writeU8(os, light_propagates);
  387. writeU8(os, sunlight_propagates);
  388. writeU8(os, light_source);
  389. // map generation
  390. writeU8(os, is_ground_content);
  391. // interaction
  392. writeU8(os, walkable);
  393. writeU8(os, pointable);
  394. writeU8(os, diggable);
  395. writeU8(os, climbable);
  396. writeU8(os, buildable_to);
  397. writeU8(os, rightclickable);
  398. writeU32(os, damage_per_second);
  399. // liquid
  400. writeU8(os, liquid_type);
  401. os << serializeString(liquid_alternative_flowing);
  402. os << serializeString(liquid_alternative_source);
  403. writeU8(os, liquid_viscosity);
  404. writeU8(os, liquid_renewable);
  405. writeU8(os, liquid_range);
  406. writeU8(os, drowning);
  407. writeU8(os, floodable);
  408. // node boxes
  409. node_box.serialize(os, protocol_version);
  410. selection_box.serialize(os, protocol_version);
  411. collision_box.serialize(os, protocol_version);
  412. // sound
  413. serializeSimpleSoundSpec(sound_footstep, os, version);
  414. serializeSimpleSoundSpec(sound_dig, os, version);
  415. serializeSimpleSoundSpec(sound_dug, os, version);
  416. // legacy
  417. writeU8(os, legacy_facedir_simple);
  418. writeU8(os, legacy_wallmounted);
  419. }
  420. void ContentFeatures::correctAlpha(TileDef *tiles, int length)
  421. {
  422. // alpha == 0 means that the node is using texture alpha
  423. if (alpha == 0 || alpha == 255)
  424. return;
  425. for (int i = 0; i < length; i++) {
  426. if (tiles[i].name.empty())
  427. continue;
  428. std::stringstream s;
  429. s << tiles[i].name << "^[noalpha^[opacity:" << ((int)alpha);
  430. tiles[i].name = s.str();
  431. }
  432. }
  433. void ContentFeatures::deSerialize(std::istream &is)
  434. {
  435. // version detection
  436. int version = readU8(is);
  437. if (version < 9) {
  438. deSerializeOld(is, version);
  439. return;
  440. }
  441. if (version > 11) {
  442. throw SerializationError("unsupported ContentFeatures version");
  443. }
  444. // general
  445. name = deSerializeString(is);
  446. groups.clear();
  447. u32 groups_size = readU16(is);
  448. for (u32 i = 0; i < groups_size; i++) {
  449. std::string name = deSerializeString(is);
  450. int value = readS16(is);
  451. groups[name] = value;
  452. }
  453. param_type = (enum ContentParamType) readU8(is);
  454. param_type_2 = (enum ContentParamType2) readU8(is);
  455. // visual
  456. drawtype = (enum NodeDrawType) readU8(is);
  457. mesh = deSerializeString(is);
  458. visual_scale = readF1000(is);
  459. if (readU8(is) != 6)
  460. throw SerializationError("unsupported tile count");
  461. for (TileDef &td : tiledef)
  462. td.deSerialize(is, version, drawtype);
  463. if (version >= 10)
  464. for (TileDef &td : tiledef_overlay)
  465. td.deSerialize(is, version, drawtype);
  466. if (readU8(is) != CF_SPECIAL_COUNT)
  467. throw SerializationError("unsupported CF_SPECIAL_COUNT");
  468. for (TileDef &td : tiledef_special)
  469. td.deSerialize(is, version, drawtype);
  470. alpha = readU8(is);
  471. color.setRed(readU8(is));
  472. color.setGreen(readU8(is));
  473. color.setBlue(readU8(is));
  474. palette_name = deSerializeString(is);
  475. waving = readU8(is);
  476. connect_sides = readU8(is);
  477. u16 connects_to_size = readU16(is);
  478. connects_to_ids.clear();
  479. for (u16 i = 0; i < connects_to_size; i++)
  480. connects_to_ids.insert(readU16(is));
  481. post_effect_color.setAlpha(readU8(is));
  482. post_effect_color.setRed(readU8(is));
  483. post_effect_color.setGreen(readU8(is));
  484. post_effect_color.setBlue(readU8(is));
  485. leveled = readU8(is);
  486. // lighting-related
  487. light_propagates = readU8(is);
  488. sunlight_propagates = readU8(is);
  489. light_source = readU8(is);
  490. light_source = MYMIN(light_source, LIGHT_MAX);
  491. // map generation
  492. is_ground_content = readU8(is);
  493. // interaction
  494. walkable = readU8(is);
  495. pointable = readU8(is);
  496. diggable = readU8(is);
  497. climbable = readU8(is);
  498. buildable_to = readU8(is);
  499. rightclickable = readU8(is);
  500. damage_per_second = readU32(is);
  501. // liquid
  502. liquid_type = (enum LiquidType) readU8(is);
  503. liquid_alternative_flowing = deSerializeString(is);
  504. liquid_alternative_source = deSerializeString(is);
  505. liquid_viscosity = readU8(is);
  506. liquid_renewable = readU8(is);
  507. liquid_range = readU8(is);
  508. drowning = readU8(is);
  509. floodable = readU8(is);
  510. // node boxes
  511. node_box.deSerialize(is);
  512. selection_box.deSerialize(is);
  513. collision_box.deSerialize(is);
  514. // sounds
  515. deSerializeSimpleSoundSpec(sound_footstep, is, version);
  516. deSerializeSimpleSoundSpec(sound_dig, is, version);
  517. deSerializeSimpleSoundSpec(sound_dug, is, version);
  518. // read legacy properties
  519. legacy_facedir_simple = readU8(is);
  520. legacy_wallmounted = readU8(is);
  521. }
  522. #ifndef SERVER
  523. void ContentFeatures::fillTileAttribs(ITextureSource *tsrc, TileLayer *tile,
  524. TileDef *tiledef, u32 shader_id, bool use_normal_texture,
  525. bool backface_culling, u8 material_type)
  526. {
  527. tile->shader_id = shader_id;
  528. tile->texture = tsrc->getTextureForMesh(tiledef->name, &tile->texture_id);
  529. tile->material_type = material_type;
  530. // Normal texture and shader flags texture
  531. if (use_normal_texture) {
  532. tile->normal_texture = tsrc->getNormalTexture(tiledef->name);
  533. }
  534. tile->flags_texture = tsrc->getShaderFlagsTexture(tile->normal_texture ? true : false);
  535. // Material flags
  536. tile->material_flags = 0;
  537. if (backface_culling)
  538. tile->material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
  539. if (tiledef->animation.type != TAT_NONE)
  540. tile->material_flags |= MATERIAL_FLAG_ANIMATION;
  541. if (tiledef->tileable_horizontal)
  542. tile->material_flags |= MATERIAL_FLAG_TILEABLE_HORIZONTAL;
  543. if (tiledef->tileable_vertical)
  544. tile->material_flags |= MATERIAL_FLAG_TILEABLE_VERTICAL;
  545. // Color
  546. tile->has_color = tiledef->has_color;
  547. if (tiledef->has_color)
  548. tile->color = tiledef->color;
  549. else
  550. tile->color = color;
  551. // Animation parameters
  552. int frame_count = 1;
  553. if (tile->material_flags & MATERIAL_FLAG_ANIMATION) {
  554. int frame_length_ms;
  555. tiledef->animation.determineParams(tile->texture->getOriginalSize(),
  556. &frame_count, &frame_length_ms, NULL);
  557. tile->animation_frame_count = frame_count;
  558. tile->animation_frame_length_ms = frame_length_ms;
  559. }
  560. if (frame_count == 1) {
  561. tile->material_flags &= ~MATERIAL_FLAG_ANIMATION;
  562. } else {
  563. std::ostringstream os(std::ios::binary);
  564. if (!tile->frames) {
  565. tile->frames = std::make_shared<std::vector<FrameSpec>>();
  566. }
  567. tile->frames->resize(frame_count);
  568. for (int i = 0; i < frame_count; i++) {
  569. FrameSpec frame;
  570. os.str("");
  571. os << tiledef->name;
  572. tiledef->animation.getTextureModifer(os,
  573. tile->texture->getOriginalSize(), i);
  574. frame.texture = tsrc->getTextureForMesh(os.str(), &frame.texture_id);
  575. if (tile->normal_texture)
  576. frame.normal_texture = tsrc->getNormalTexture(os.str());
  577. frame.flags_texture = tile->flags_texture;
  578. (*tile->frames)[i] = frame;
  579. }
  580. }
  581. }
  582. #endif
  583. #ifndef SERVER
  584. void ContentFeatures::updateTextures(ITextureSource *tsrc, IShaderSource *shdsrc,
  585. scene::IMeshManipulator *meshmanip, Client *client, const TextureSettings &tsettings)
  586. {
  587. // minimap pixel color - the average color of a texture
  588. if (tsettings.enable_minimap && !tiledef[0].name.empty())
  589. minimap_color = tsrc->getTextureAverageColor(tiledef[0].name);
  590. // Figure out the actual tiles to use
  591. TileDef tdef[6];
  592. for (u32 j = 0; j < 6; j++) {
  593. tdef[j] = tiledef[j];
  594. if (tdef[j].name.empty())
  595. tdef[j].name = "unknown_node.png";
  596. }
  597. // also the overlay tiles
  598. TileDef tdef_overlay[6];
  599. for (u32 j = 0; j < 6; j++)
  600. tdef_overlay[j] = tiledef_overlay[j];
  601. // also the special tiles
  602. TileDef tdef_spec[6];
  603. for (u32 j = 0; j < CF_SPECIAL_COUNT; j++)
  604. tdef_spec[j] = tiledef_special[j];
  605. bool is_liquid = false;
  606. u8 material_type = (alpha == 255) ?
  607. TILE_MATERIAL_BASIC : TILE_MATERIAL_ALPHA;
  608. switch (drawtype) {
  609. default:
  610. case NDT_NORMAL:
  611. material_type = (alpha == 255) ?
  612. TILE_MATERIAL_OPAQUE : TILE_MATERIAL_ALPHA;
  613. solidness = 2;
  614. break;
  615. case NDT_AIRLIKE:
  616. solidness = 0;
  617. break;
  618. case NDT_LIQUID:
  619. assert(liquid_type == LIQUID_SOURCE);
  620. if (tsettings.opaque_water)
  621. alpha = 255;
  622. solidness = 1;
  623. is_liquid = true;
  624. break;
  625. case NDT_FLOWINGLIQUID:
  626. assert(liquid_type == LIQUID_FLOWING);
  627. solidness = 0;
  628. if (tsettings.opaque_water)
  629. alpha = 255;
  630. is_liquid = true;
  631. break;
  632. case NDT_GLASSLIKE:
  633. solidness = 0;
  634. visual_solidness = 1;
  635. break;
  636. case NDT_GLASSLIKE_FRAMED:
  637. solidness = 0;
  638. visual_solidness = 1;
  639. break;
  640. case NDT_GLASSLIKE_FRAMED_OPTIONAL:
  641. solidness = 0;
  642. visual_solidness = 1;
  643. drawtype = tsettings.connected_glass ? NDT_GLASSLIKE_FRAMED : NDT_GLASSLIKE;
  644. break;
  645. case NDT_ALLFACES:
  646. solidness = 0;
  647. visual_solidness = 1;
  648. break;
  649. case NDT_ALLFACES_OPTIONAL:
  650. if (tsettings.leaves_style == LEAVES_FANCY) {
  651. drawtype = NDT_ALLFACES;
  652. solidness = 0;
  653. visual_solidness = 1;
  654. } else if (tsettings.leaves_style == LEAVES_SIMPLE) {
  655. for (u32 j = 0; j < 6; j++) {
  656. if (!tdef_spec[j].name.empty())
  657. tdef[j].name = tdef_spec[j].name;
  658. }
  659. drawtype = NDT_GLASSLIKE;
  660. solidness = 0;
  661. visual_solidness = 1;
  662. } else {
  663. drawtype = NDT_NORMAL;
  664. solidness = 2;
  665. for (TileDef &td : tdef)
  666. td.name += std::string("^[noalpha");
  667. }
  668. if (waving >= 1)
  669. material_type = TILE_MATERIAL_WAVING_LEAVES;
  670. break;
  671. case NDT_PLANTLIKE:
  672. solidness = 0;
  673. if (waving >= 1)
  674. material_type = TILE_MATERIAL_WAVING_PLANTS;
  675. break;
  676. case NDT_FIRELIKE:
  677. solidness = 0;
  678. break;
  679. case NDT_MESH:
  680. case NDT_NODEBOX:
  681. solidness = 0;
  682. if (waving == 1)
  683. material_type = TILE_MATERIAL_WAVING_PLANTS;
  684. else if (waving == 2)
  685. material_type = TILE_MATERIAL_WAVING_LEAVES;
  686. break;
  687. case NDT_TORCHLIKE:
  688. case NDT_SIGNLIKE:
  689. case NDT_FENCELIKE:
  690. case NDT_RAILLIKE:
  691. solidness = 0;
  692. break;
  693. case NDT_PLANTLIKE_ROOTED:
  694. solidness = 2;
  695. break;
  696. }
  697. if (is_liquid) {
  698. // Vertex alpha is no longer supported, correct if necessary.
  699. correctAlpha(tdef, 6);
  700. correctAlpha(tdef_overlay, 6);
  701. correctAlpha(tdef_spec, CF_SPECIAL_COUNT);
  702. material_type = (alpha == 255) ?
  703. TILE_MATERIAL_LIQUID_OPAQUE : TILE_MATERIAL_LIQUID_TRANSPARENT;
  704. }
  705. u32 tile_shader = shdsrc->getShader("nodes_shader", material_type, drawtype);
  706. u8 overlay_material = material_type;
  707. if (overlay_material == TILE_MATERIAL_OPAQUE)
  708. overlay_material = TILE_MATERIAL_BASIC;
  709. else if (overlay_material == TILE_MATERIAL_LIQUID_OPAQUE)
  710. overlay_material = TILE_MATERIAL_LIQUID_TRANSPARENT;
  711. u32 overlay_shader = shdsrc->getShader("nodes_shader", overlay_material, drawtype);
  712. // Tiles (fill in f->tiles[])
  713. for (u16 j = 0; j < 6; j++) {
  714. fillTileAttribs(tsrc, &tiles[j].layers[0], &tdef[j], tile_shader,
  715. tsettings.use_normal_texture,
  716. tdef[j].backface_culling, material_type);
  717. if (!tdef_overlay[j].name.empty())
  718. fillTileAttribs(tsrc, &tiles[j].layers[1], &tdef_overlay[j],
  719. overlay_shader, tsettings.use_normal_texture,
  720. tdef[j].backface_culling, overlay_material);
  721. }
  722. u8 special_material = material_type;
  723. if (drawtype == NDT_PLANTLIKE_ROOTED) {
  724. if (waving == 1)
  725. special_material = TILE_MATERIAL_WAVING_PLANTS;
  726. else if (waving == 2)
  727. special_material = TILE_MATERIAL_WAVING_LEAVES;
  728. }
  729. u32 special_shader = shdsrc->getShader("nodes_shader", special_material, drawtype);
  730. // Special tiles (fill in f->special_tiles[])
  731. for (u16 j = 0; j < CF_SPECIAL_COUNT; j++) {
  732. fillTileAttribs(tsrc, &special_tiles[j].layers[0], &tdef_spec[j],
  733. special_shader, tsettings.use_normal_texture,
  734. tdef_spec[j].backface_culling, special_material);
  735. }
  736. if (param_type_2 == CPT2_COLOR ||
  737. param_type_2 == CPT2_COLORED_FACEDIR ||
  738. param_type_2 == CPT2_COLORED_WALLMOUNTED)
  739. palette = tsrc->getPalette(palette_name);
  740. if (drawtype == NDT_MESH && !mesh.empty()) {
  741. // Meshnode drawtype
  742. // Read the mesh and apply scale
  743. mesh_ptr[0] = client->getMesh(mesh);
  744. if (mesh_ptr[0]){
  745. v3f scale = v3f(1.0, 1.0, 1.0) * BS * visual_scale;
  746. scaleMesh(mesh_ptr[0], scale);
  747. recalculateBoundingBox(mesh_ptr[0]);
  748. meshmanip->recalculateNormals(mesh_ptr[0], true, false);
  749. }
  750. } else if ((drawtype == NDT_NODEBOX) &&
  751. ((node_box.type == NODEBOX_REGULAR) ||
  752. (node_box.type == NODEBOX_FIXED)) &&
  753. (!node_box.fixed.empty())) {
  754. //Convert regular nodebox nodes to meshnodes
  755. //Change the drawtype and apply scale
  756. drawtype = NDT_MESH;
  757. mesh_ptr[0] = convertNodeboxesToMesh(node_box.fixed);
  758. v3f scale = v3f(1.0, 1.0, 1.0) * visual_scale;
  759. scaleMesh(mesh_ptr[0], scale);
  760. recalculateBoundingBox(mesh_ptr[0]);
  761. meshmanip->recalculateNormals(mesh_ptr[0], true, false);
  762. }
  763. //Cache 6dfacedir and wallmounted rotated clones of meshes
  764. if (tsettings.enable_mesh_cache && mesh_ptr[0] &&
  765. (param_type_2 == CPT2_FACEDIR
  766. || param_type_2 == CPT2_COLORED_FACEDIR)) {
  767. for (u16 j = 1; j < 24; j++) {
  768. mesh_ptr[j] = cloneMesh(mesh_ptr[0]);
  769. rotateMeshBy6dFacedir(mesh_ptr[j], j);
  770. recalculateBoundingBox(mesh_ptr[j]);
  771. meshmanip->recalculateNormals(mesh_ptr[j], true, false);
  772. }
  773. } else if (tsettings.enable_mesh_cache && mesh_ptr[0]
  774. && (param_type_2 == CPT2_WALLMOUNTED ||
  775. param_type_2 == CPT2_COLORED_WALLMOUNTED)) {
  776. static const u8 wm_to_6d[6] = { 20, 0, 16 + 1, 12 + 3, 8, 4 + 2 };
  777. for (u16 j = 1; j < 6; j++) {
  778. mesh_ptr[j] = cloneMesh(mesh_ptr[0]);
  779. rotateMeshBy6dFacedir(mesh_ptr[j], wm_to_6d[j]);
  780. recalculateBoundingBox(mesh_ptr[j]);
  781. meshmanip->recalculateNormals(mesh_ptr[j], true, false);
  782. }
  783. rotateMeshBy6dFacedir(mesh_ptr[0], wm_to_6d[0]);
  784. recalculateBoundingBox(mesh_ptr[0]);
  785. meshmanip->recalculateNormals(mesh_ptr[0], true, false);
  786. }
  787. }
  788. #endif
  789. /*
  790. CNodeDefManager
  791. */
  792. class CNodeDefManager: public IWritableNodeDefManager {
  793. public:
  794. CNodeDefManager();
  795. virtual ~CNodeDefManager();
  796. void clear();
  797. inline virtual const ContentFeatures& get(content_t c) const;
  798. inline virtual const ContentFeatures& get(const MapNode &n) const;
  799. virtual bool getId(const std::string &name, content_t &result) const;
  800. virtual content_t getId(const std::string &name) const;
  801. virtual bool getIds(const std::string &name, std::set<content_t> &result) const;
  802. virtual const ContentFeatures& get(const std::string &name) const;
  803. content_t allocateId();
  804. virtual content_t set(const std::string &name, const ContentFeatures &def);
  805. virtual content_t allocateDummy(const std::string &name);
  806. virtual void removeNode(const std::string &name);
  807. virtual void updateAliases(IItemDefManager *idef);
  808. virtual void applyTextureOverrides(const std::string &override_filepath);
  809. virtual void updateTextures(IGameDef *gamedef,
  810. void (*progress_cbk)(void *progress_args, u32 progress, u32 max_progress),
  811. void *progress_cbk_args);
  812. void serialize(std::ostream &os, u16 protocol_version) const;
  813. void deSerialize(std::istream &is);
  814. inline virtual void setNodeRegistrationStatus(bool completed);
  815. virtual void pendNodeResolve(NodeResolver *nr);
  816. virtual bool cancelNodeResolveCallback(NodeResolver *nr);
  817. virtual void runNodeResolveCallbacks();
  818. virtual void resetNodeResolveState();
  819. virtual void mapNodeboxConnections();
  820. virtual bool nodeboxConnects(MapNode from, MapNode to, u8 connect_face);
  821. virtual core::aabbox3d<s16> getSelectionBoxIntUnion() const
  822. {
  823. return m_selection_box_int_union;
  824. }
  825. private:
  826. void addNameIdMapping(content_t i, std::string name);
  827. /*!
  828. * Recalculates m_selection_box_int_union based on
  829. * m_selection_box_union.
  830. */
  831. void fixSelectionBoxIntUnion();
  832. // Features indexed by id
  833. std::vector<ContentFeatures> m_content_features;
  834. // A mapping for fast converting back and forth between names and ids
  835. NameIdMapping m_name_id_mapping;
  836. // Like m_name_id_mapping, but only from names to ids, and includes
  837. // item aliases too. Updated by updateAliases()
  838. // Note: Not serialized.
  839. std::unordered_map<std::string, content_t> m_name_id_mapping_with_aliases;
  840. // A mapping from groups to a list of content_ts (and their levels)
  841. // that belong to it. Necessary for a direct lookup in getIds().
  842. // Note: Not serialized.
  843. std::unordered_map<std::string, GroupItems> m_group_to_items;
  844. // Next possibly free id
  845. content_t m_next_id;
  846. // NodeResolvers to callback once node registration has ended
  847. std::vector<NodeResolver *> m_pending_resolve_callbacks;
  848. // True when all nodes have been registered
  849. bool m_node_registration_complete;
  850. //! The union of all nodes' selection boxes.
  851. aabb3f m_selection_box_union;
  852. /*!
  853. * The smallest box in node coordinates that
  854. * contains all nodes' selection boxes.
  855. */
  856. core::aabbox3d<s16> m_selection_box_int_union;
  857. };
  858. CNodeDefManager::CNodeDefManager()
  859. {
  860. clear();
  861. }
  862. CNodeDefManager::~CNodeDefManager()
  863. {
  864. #ifndef SERVER
  865. for (ContentFeatures &f : m_content_features) {
  866. for (auto &j : f.mesh_ptr) {
  867. if (j)
  868. j->drop();
  869. }
  870. }
  871. #endif
  872. }
  873. void CNodeDefManager::clear()
  874. {
  875. m_content_features.clear();
  876. m_name_id_mapping.clear();
  877. m_name_id_mapping_with_aliases.clear();
  878. m_group_to_items.clear();
  879. m_next_id = 0;
  880. m_selection_box_union.reset(0,0,0);
  881. m_selection_box_int_union.reset(0,0,0);
  882. resetNodeResolveState();
  883. u32 initial_length = 0;
  884. initial_length = MYMAX(initial_length, CONTENT_UNKNOWN + 1);
  885. initial_length = MYMAX(initial_length, CONTENT_AIR + 1);
  886. initial_length = MYMAX(initial_length, CONTENT_IGNORE + 1);
  887. m_content_features.resize(initial_length);
  888. // Set CONTENT_UNKNOWN
  889. {
  890. ContentFeatures f;
  891. f.name = "unknown";
  892. // Insert directly into containers
  893. content_t c = CONTENT_UNKNOWN;
  894. m_content_features[c] = f;
  895. addNameIdMapping(c, f.name);
  896. }
  897. // Set CONTENT_AIR
  898. {
  899. ContentFeatures f;
  900. f.name = "air";
  901. f.drawtype = NDT_AIRLIKE;
  902. f.param_type = CPT_LIGHT;
  903. f.light_propagates = true;
  904. f.sunlight_propagates = true;
  905. f.walkable = false;
  906. f.pointable = false;
  907. f.diggable = false;
  908. f.buildable_to = true;
  909. f.floodable = true;
  910. f.is_ground_content = true;
  911. // Insert directly into containers
  912. content_t c = CONTENT_AIR;
  913. m_content_features[c] = f;
  914. addNameIdMapping(c, f.name);
  915. }
  916. // Set CONTENT_IGNORE
  917. {
  918. ContentFeatures f;
  919. f.name = "ignore";
  920. f.drawtype = NDT_AIRLIKE;
  921. f.param_type = CPT_NONE;
  922. f.light_propagates = false;
  923. f.sunlight_propagates = false;
  924. f.walkable = false;
  925. f.pointable = false;
  926. f.diggable = false;
  927. f.buildable_to = true; // A way to remove accidental CONTENT_IGNOREs
  928. f.is_ground_content = true;
  929. // Insert directly into containers
  930. content_t c = CONTENT_IGNORE;
  931. m_content_features[c] = f;
  932. addNameIdMapping(c, f.name);
  933. }
  934. }
  935. inline const ContentFeatures& CNodeDefManager::get(content_t c) const
  936. {
  937. return c < m_content_features.size()
  938. ? m_content_features[c] : m_content_features[CONTENT_UNKNOWN];
  939. }
  940. inline const ContentFeatures& CNodeDefManager::get(const MapNode &n) const
  941. {
  942. return get(n.getContent());
  943. }
  944. bool CNodeDefManager::getId(const std::string &name, content_t &result) const
  945. {
  946. std::unordered_map<std::string, content_t>::const_iterator
  947. i = m_name_id_mapping_with_aliases.find(name);
  948. if(i == m_name_id_mapping_with_aliases.end())
  949. return false;
  950. result = i->second;
  951. return true;
  952. }
  953. content_t CNodeDefManager::getId(const std::string &name) const
  954. {
  955. content_t id = CONTENT_IGNORE;
  956. getId(name, id);
  957. return id;
  958. }
  959. bool CNodeDefManager::getIds(const std::string &name,
  960. std::set<content_t> &result) const
  961. {
  962. //TimeTaker t("getIds", NULL, PRECISION_MICRO);
  963. if (name.substr(0,6) != "group:") {
  964. content_t id = CONTENT_IGNORE;
  965. bool exists = getId(name, id);
  966. if (exists)
  967. result.insert(id);
  968. return exists;
  969. }
  970. std::string group = name.substr(6);
  971. std::unordered_map<std::string, GroupItems>::const_iterator
  972. i = m_group_to_items.find(group);
  973. if (i == m_group_to_items.end())
  974. return true;
  975. const GroupItems &items = i->second;
  976. for (const auto &item : items) {
  977. if (item.second != 0)
  978. result.insert(item.first);
  979. }
  980. //printf("getIds: %dus\n", t.stop());
  981. return true;
  982. }
  983. const ContentFeatures& CNodeDefManager::get(const std::string &name) const
  984. {
  985. content_t id = CONTENT_UNKNOWN;
  986. getId(name, id);
  987. return get(id);
  988. }
  989. // returns CONTENT_IGNORE if no free ID found
  990. content_t CNodeDefManager::allocateId()
  991. {
  992. for (content_t id = m_next_id;
  993. id >= m_next_id; // overflow?
  994. ++id) {
  995. while (id >= m_content_features.size()) {
  996. m_content_features.emplace_back();
  997. }
  998. const ContentFeatures &f = m_content_features[id];
  999. if (f.name.empty()) {
  1000. m_next_id = id + 1;
  1001. return id;
  1002. }
  1003. }
  1004. // If we arrive here, an overflow occurred in id.
  1005. // That means no ID was found
  1006. return CONTENT_IGNORE;
  1007. }
  1008. /*!
  1009. * Returns the smallest box that contains all boxes
  1010. * in the vector. Box_union is expanded.
  1011. * @param[in] boxes the vector containing the boxes
  1012. * @param[in, out] box_union the union of the arguments
  1013. */
  1014. void boxVectorUnion(const std::vector<aabb3f> &boxes, aabb3f *box_union)
  1015. {
  1016. for (const aabb3f &box : boxes) {
  1017. box_union->addInternalBox(box);
  1018. }
  1019. }
  1020. /*!
  1021. * Returns a box that contains the nodebox in every case.
  1022. * The argument node_union is expanded.
  1023. * @param[in] nodebox the nodebox to be measured
  1024. * @param[in] features used to decide whether the nodebox
  1025. * can be rotated
  1026. * @param[in, out] box_union the union of the arguments
  1027. */
  1028. void getNodeBoxUnion(const NodeBox &nodebox, const ContentFeatures &features,
  1029. aabb3f *box_union)
  1030. {
  1031. switch(nodebox.type) {
  1032. case NODEBOX_FIXED:
  1033. case NODEBOX_LEVELED: {
  1034. // Raw union
  1035. aabb3f half_processed(0, 0, 0, 0, 0, 0);
  1036. boxVectorUnion(nodebox.fixed, &half_processed);
  1037. // Set leveled boxes to maximal
  1038. if (nodebox.type == NODEBOX_LEVELED) {
  1039. half_processed.MaxEdge.Y = +BS / 2;
  1040. }
  1041. if (features.param_type_2 == CPT2_FACEDIR ||
  1042. features.param_type_2 == CPT2_COLORED_FACEDIR) {
  1043. // Get maximal coordinate
  1044. f32 coords[] = {
  1045. fabsf(half_processed.MinEdge.X),
  1046. fabsf(half_processed.MinEdge.Y),
  1047. fabsf(half_processed.MinEdge.Z),
  1048. fabsf(half_processed.MaxEdge.X),
  1049. fabsf(half_processed.MaxEdge.Y),
  1050. fabsf(half_processed.MaxEdge.Z) };
  1051. f32 max = 0;
  1052. for (float coord : coords) {
  1053. if (max < coord) {
  1054. max = coord;
  1055. }
  1056. }
  1057. // Add the union of all possible rotated boxes
  1058. box_union->addInternalPoint(-max, -max, -max);
  1059. box_union->addInternalPoint(+max, +max, +max);
  1060. } else {
  1061. box_union->addInternalBox(half_processed);
  1062. }
  1063. break;
  1064. }
  1065. case NODEBOX_WALLMOUNTED: {
  1066. // Add fix boxes
  1067. box_union->addInternalBox(nodebox.wall_top);
  1068. box_union->addInternalBox(nodebox.wall_bottom);
  1069. // Find maximal coordinate in the X-Z plane
  1070. f32 coords[] = {
  1071. fabsf(nodebox.wall_side.MinEdge.X),
  1072. fabsf(nodebox.wall_side.MinEdge.Z),
  1073. fabsf(nodebox.wall_side.MaxEdge.X),
  1074. fabsf(nodebox.wall_side.MaxEdge.Z) };
  1075. f32 max = 0;
  1076. for (float coord : coords) {
  1077. if (max < coord) {
  1078. max = coord;
  1079. }
  1080. }
  1081. // Add the union of all possible rotated boxes
  1082. box_union->addInternalPoint(-max, nodebox.wall_side.MinEdge.Y, -max);
  1083. box_union->addInternalPoint(max, nodebox.wall_side.MaxEdge.Y, max);
  1084. break;
  1085. }
  1086. case NODEBOX_CONNECTED: {
  1087. // Add all possible connected boxes
  1088. boxVectorUnion(nodebox.fixed, box_union);
  1089. boxVectorUnion(nodebox.connect_top, box_union);
  1090. boxVectorUnion(nodebox.connect_bottom, box_union);
  1091. boxVectorUnion(nodebox.connect_front, box_union);
  1092. boxVectorUnion(nodebox.connect_left, box_union);
  1093. boxVectorUnion(nodebox.connect_back, box_union);
  1094. boxVectorUnion(nodebox.connect_right, box_union);
  1095. break;
  1096. }
  1097. default: {
  1098. // NODEBOX_REGULAR
  1099. box_union->addInternalPoint(-BS / 2, -BS / 2, -BS / 2);
  1100. box_union->addInternalPoint(+BS / 2, +BS / 2, +BS / 2);
  1101. }
  1102. }
  1103. }
  1104. inline void CNodeDefManager::fixSelectionBoxIntUnion()
  1105. {
  1106. m_selection_box_int_union.MinEdge.X = floorf(
  1107. m_selection_box_union.MinEdge.X / BS + 0.5f);
  1108. m_selection_box_int_union.MinEdge.Y = floorf(
  1109. m_selection_box_union.MinEdge.Y / BS + 0.5f);
  1110. m_selection_box_int_union.MinEdge.Z = floorf(
  1111. m_selection_box_union.MinEdge.Z / BS + 0.5f);
  1112. m_selection_box_int_union.MaxEdge.X = ceilf(
  1113. m_selection_box_union.MaxEdge.X / BS - 0.5f);
  1114. m_selection_box_int_union.MaxEdge.Y = ceilf(
  1115. m_selection_box_union.MaxEdge.Y / BS - 0.5f);
  1116. m_selection_box_int_union.MaxEdge.Z = ceilf(
  1117. m_selection_box_union.MaxEdge.Z / BS - 0.5f);
  1118. }
  1119. // IWritableNodeDefManager
  1120. content_t CNodeDefManager::set(const std::string &name, const ContentFeatures &def)
  1121. {
  1122. // Pre-conditions
  1123. assert(name != "");
  1124. assert(name == def.name);
  1125. // Don't allow redefining ignore (but allow air and unknown)
  1126. if (name == "ignore") {
  1127. warningstream << "NodeDefManager: Ignoring "
  1128. "CONTENT_IGNORE redefinition"<<std::endl;
  1129. return CONTENT_IGNORE;
  1130. }
  1131. content_t id = CONTENT_IGNORE;
  1132. if (!m_name_id_mapping.getId(name, id)) { // ignore aliases
  1133. // Get new id
  1134. id = allocateId();
  1135. if (id == CONTENT_IGNORE) {
  1136. warningstream << "NodeDefManager: Absolute "
  1137. "limit reached" << std::endl;
  1138. return CONTENT_IGNORE;
  1139. }
  1140. assert(id != CONTENT_IGNORE);
  1141. addNameIdMapping(id, name);
  1142. }
  1143. m_content_features[id] = def;
  1144. verbosestream << "NodeDefManager: registering content id \"" << id
  1145. << "\": name=\"" << def.name << "\""<<std::endl;
  1146. getNodeBoxUnion(def.selection_box, def, &m_selection_box_union);
  1147. fixSelectionBoxIntUnion();
  1148. // Add this content to the list of all groups it belongs to
  1149. // FIXME: This should remove a node from groups it no longer
  1150. // belongs to when a node is re-registered
  1151. for (const auto &group : def.groups) {
  1152. const std::string &group_name = group.first;
  1153. std::unordered_map<std::string, GroupItems>::iterator
  1154. j = m_group_to_items.find(group_name);
  1155. if (j == m_group_to_items.end()) {
  1156. m_group_to_items[group_name].emplace_back(id, group.second);
  1157. } else {
  1158. GroupItems &items = j->second;
  1159. items.emplace_back(id, group.second);
  1160. }
  1161. }
  1162. return id;
  1163. }
  1164. content_t CNodeDefManager::allocateDummy(const std::string &name)
  1165. {
  1166. assert(name != ""); // Pre-condition
  1167. ContentFeatures f;
  1168. f.name = name;
  1169. return set(name, f);
  1170. }
  1171. void CNodeDefManager::removeNode(const std::string &name)
  1172. {
  1173. // Pre-condition
  1174. assert(name != "");
  1175. // Erase name from name ID mapping
  1176. content_t id = CONTENT_IGNORE;
  1177. if (m_name_id_mapping.getId(name, id)) {
  1178. m_name_id_mapping.eraseName(name);
  1179. m_name_id_mapping_with_aliases.erase(name);
  1180. }
  1181. // Erase node content from all groups it belongs to
  1182. for (std::unordered_map<std::string, GroupItems>::iterator iter_groups =
  1183. m_group_to_items.begin(); iter_groups != m_group_to_items.end();) {
  1184. GroupItems &items = iter_groups->second;
  1185. for (GroupItems::iterator iter_groupitems = items.begin();
  1186. iter_groupitems != items.end();) {
  1187. if (iter_groupitems->first == id)
  1188. items.erase(iter_groupitems++);
  1189. else
  1190. ++iter_groupitems;
  1191. }
  1192. // Check if group is empty
  1193. if (items.empty())
  1194. m_group_to_items.erase(iter_groups++);
  1195. else
  1196. ++iter_groups;
  1197. }
  1198. }
  1199. void CNodeDefManager::updateAliases(IItemDefManager *idef)
  1200. {
  1201. std::set<std::string> all;
  1202. idef->getAll(all);
  1203. m_name_id_mapping_with_aliases.clear();
  1204. for (const std::string &name : all) {
  1205. const std::string &convert_to = idef->getAlias(name);
  1206. content_t id;
  1207. if (m_name_id_mapping.getId(convert_to, id)) {
  1208. m_name_id_mapping_with_aliases.insert(
  1209. std::make_pair(name, id));
  1210. }
  1211. }
  1212. }
  1213. void CNodeDefManager::applyTextureOverrides(const std::string &override_filepath)
  1214. {
  1215. infostream << "CNodeDefManager::applyTextureOverrides(): Applying "
  1216. "overrides to textures from " << override_filepath << std::endl;
  1217. std::ifstream infile(override_filepath.c_str());
  1218. std::string line;
  1219. int line_c = 0;
  1220. while (std::getline(infile, line)) {
  1221. line_c++;
  1222. if (trim(line).empty())
  1223. continue;
  1224. std::vector<std::string> splitted = str_split(line, ' ');
  1225. if (splitted.size() != 3) {
  1226. errorstream << override_filepath
  1227. << ":" << line_c << " Could not apply texture override \""
  1228. << line << "\": Syntax error" << std::endl;
  1229. continue;
  1230. }
  1231. content_t id;
  1232. if (!getId(splitted[0], id))
  1233. continue; // Ignore unknown node
  1234. ContentFeatures &nodedef = m_content_features[id];
  1235. if (splitted[1] == "top")
  1236. nodedef.tiledef[0].name = splitted[2];
  1237. else if (splitted[1] == "bottom")
  1238. nodedef.tiledef[1].name = splitted[2];
  1239. else if (splitted[1] == "right")
  1240. nodedef.tiledef[2].name = splitted[2];
  1241. else if (splitted[1] == "left")
  1242. nodedef.tiledef[3].name = splitted[2];
  1243. else if (splitted[1] == "back")
  1244. nodedef.tiledef[4].name = splitted[2];
  1245. else if (splitted[1] == "front")
  1246. nodedef.tiledef[5].name = splitted[2];
  1247. else if (splitted[1] == "all" || splitted[1] == "*")
  1248. for (TileDef &i : nodedef.tiledef)
  1249. i.name = splitted[2];
  1250. else if (splitted[1] == "sides")
  1251. for (int i = 2; i < 6; i++)
  1252. nodedef.tiledef[i].name = splitted[2];
  1253. else {
  1254. errorstream << override_filepath
  1255. << ":" << line_c << " Could not apply texture override \""
  1256. << line << "\": Unknown node side \""
  1257. << splitted[1] << "\"" << std::endl;
  1258. continue;
  1259. }
  1260. }
  1261. }
  1262. void CNodeDefManager::updateTextures(IGameDef *gamedef,
  1263. void (*progress_callback)(void *progress_args, u32 progress, u32 max_progress),
  1264. void *progress_callback_args)
  1265. {
  1266. #ifndef SERVER
  1267. infostream << "CNodeDefManager::updateTextures(): Updating "
  1268. "textures in node definitions" << std::endl;
  1269. Client *client = (Client *)gamedef;
  1270. ITextureSource *tsrc = client->tsrc();
  1271. IShaderSource *shdsrc = client->getShaderSource();
  1272. scene::IMeshManipulator *meshmanip =
  1273. RenderingEngine::get_scene_manager()->getMeshManipulator();
  1274. TextureSettings tsettings;
  1275. tsettings.readSettings();
  1276. u32 size = m_content_features.size();
  1277. for (u32 i = 0; i < size; i++) {
  1278. ContentFeatures *f = &(m_content_features[i]);
  1279. f->updateTextures(tsrc, shdsrc, meshmanip, client, tsettings);
  1280. progress_callback(progress_callback_args, i, size);
  1281. }
  1282. #endif
  1283. }
  1284. void CNodeDefManager::serialize(std::ostream &os, u16 protocol_version) const
  1285. {
  1286. writeU8(os, 1); // version
  1287. u16 count = 0;
  1288. std::ostringstream os2(std::ios::binary);
  1289. for (u32 i = 0; i < m_content_features.size(); i++) {
  1290. if (i == CONTENT_IGNORE || i == CONTENT_AIR
  1291. || i == CONTENT_UNKNOWN)
  1292. continue;
  1293. const ContentFeatures *f = &m_content_features[i];
  1294. if (f->name.empty())
  1295. continue;
  1296. writeU16(os2, i);
  1297. // Wrap it in a string to allow different lengths without
  1298. // strict version incompatibilities
  1299. std::ostringstream wrapper_os(std::ios::binary);
  1300. f->serialize(wrapper_os, protocol_version);
  1301. os2<<serializeString(wrapper_os.str());
  1302. // must not overflow
  1303. u16 next = count + 1;
  1304. FATAL_ERROR_IF(next < count, "Overflow");
  1305. count++;
  1306. }
  1307. writeU16(os, count);
  1308. os << serializeLongString(os2.str());
  1309. }
  1310. void CNodeDefManager::deSerialize(std::istream &is)
  1311. {
  1312. clear();
  1313. int version = readU8(is);
  1314. if (version != 1)
  1315. throw SerializationError("unsupported NodeDefinitionManager version");
  1316. u16 count = readU16(is);
  1317. std::istringstream is2(deSerializeLongString(is), std::ios::binary);
  1318. ContentFeatures f;
  1319. for (u16 n = 0; n < count; n++) {
  1320. u16 i = readU16(is2);
  1321. // Read it from the string wrapper
  1322. std::string wrapper = deSerializeString(is2);
  1323. std::istringstream wrapper_is(wrapper, std::ios::binary);
  1324. f.deSerialize(wrapper_is);
  1325. // Check error conditions
  1326. if (i == CONTENT_IGNORE || i == CONTENT_AIR || i == CONTENT_UNKNOWN) {
  1327. warningstream << "NodeDefManager::deSerialize(): "
  1328. "not changing builtin node " << i << std::endl;
  1329. continue;
  1330. }
  1331. if (f.name.empty()) {
  1332. warningstream << "NodeDefManager::deSerialize(): "
  1333. "received empty name" << std::endl;
  1334. continue;
  1335. }
  1336. // Ignore aliases
  1337. u16 existing_id;
  1338. if (m_name_id_mapping.getId(f.name, existing_id) && i != existing_id) {
  1339. warningstream << "NodeDefManager::deSerialize(): "
  1340. "already defined with different ID: " << f.name << std::endl;
  1341. continue;
  1342. }
  1343. // All is ok, add node definition with the requested ID
  1344. if (i >= m_content_features.size())
  1345. m_content_features.resize((u32)(i) + 1);
  1346. m_content_features[i] = f;
  1347. addNameIdMapping(i, f.name);
  1348. verbosestream << "deserialized " << f.name << std::endl;
  1349. getNodeBoxUnion(f.selection_box, f, &m_selection_box_union);
  1350. fixSelectionBoxIntUnion();
  1351. }
  1352. }
  1353. void CNodeDefManager::addNameIdMapping(content_t i, std::string name)
  1354. {
  1355. m_name_id_mapping.set(i, name);
  1356. m_name_id_mapping_with_aliases.insert(std::make_pair(name, i));
  1357. }
  1358. IWritableNodeDefManager *createNodeDefManager()
  1359. {
  1360. return new CNodeDefManager();
  1361. }
  1362. //// Serialization of old ContentFeatures formats
  1363. void ContentFeatures::serializeOld(std::ostream &os, u16 protocol_version) const
  1364. {
  1365. u8 compatible_param_type_2 = param_type_2;
  1366. if ((protocol_version < 28)
  1367. && (compatible_param_type_2 == CPT2_MESHOPTIONS))
  1368. compatible_param_type_2 = CPT2_NONE;
  1369. else if (protocol_version < 30) {
  1370. if (compatible_param_type_2 == CPT2_COLOR)
  1371. compatible_param_type_2 = CPT2_NONE;
  1372. else if (compatible_param_type_2 == CPT2_COLORED_FACEDIR)
  1373. compatible_param_type_2 = CPT2_FACEDIR;
  1374. else if (compatible_param_type_2 == CPT2_COLORED_WALLMOUNTED)
  1375. compatible_param_type_2 = CPT2_WALLMOUNTED;
  1376. }
  1377. float compatible_visual_scale = visual_scale;
  1378. if (protocol_version < 30 && drawtype == NDT_PLANTLIKE)
  1379. compatible_visual_scale = sqrt(visual_scale);
  1380. TileDef compatible_tiles[6];
  1381. for (u8 i = 0; i < 6; i++) {
  1382. compatible_tiles[i] = tiledef[i];
  1383. if (!tiledef_overlay[i].name.empty()) {
  1384. std::stringstream s;
  1385. s << "(" << tiledef[i].name << ")^(" << tiledef_overlay[i].name
  1386. << ")";
  1387. compatible_tiles[i].name = s.str();
  1388. }
  1389. }
  1390. // Protocol >= 24
  1391. if (protocol_version < 31) {
  1392. writeU8(os, protocol_version < 27 ? 7 : 8);
  1393. os << serializeString(name);
  1394. writeU16(os, groups.size());
  1395. for (const auto &group : groups) {
  1396. os << serializeString(group.first);
  1397. writeS16(os, group.second);
  1398. }
  1399. writeU8(os, drawtype);
  1400. writeF1000(os, compatible_visual_scale);
  1401. writeU8(os, 6);
  1402. for (const auto &compatible_tile : compatible_tiles)
  1403. compatible_tile.serialize(os, protocol_version);
  1404. writeU8(os, CF_SPECIAL_COUNT);
  1405. for (const TileDef &i : tiledef_special)
  1406. i.serialize(os, protocol_version);
  1407. writeU8(os, alpha);
  1408. writeU8(os, post_effect_color.getAlpha());
  1409. writeU8(os, post_effect_color.getRed());
  1410. writeU8(os, post_effect_color.getGreen());
  1411. writeU8(os, post_effect_color.getBlue());
  1412. writeU8(os, param_type);
  1413. writeU8(os, compatible_param_type_2);
  1414. writeU8(os, is_ground_content);
  1415. writeU8(os, light_propagates);
  1416. writeU8(os, sunlight_propagates);
  1417. writeU8(os, walkable);
  1418. writeU8(os, pointable);
  1419. writeU8(os, diggable);
  1420. writeU8(os, climbable);
  1421. writeU8(os, buildable_to);
  1422. os << serializeString(""); // legacy: used to be metadata_name
  1423. writeU8(os, liquid_type);
  1424. os << serializeString(liquid_alternative_flowing);
  1425. os << serializeString(liquid_alternative_source);
  1426. writeU8(os, liquid_viscosity);
  1427. writeU8(os, liquid_renewable);
  1428. writeU8(os, light_source);
  1429. writeU32(os, damage_per_second);
  1430. node_box.serialize(os, protocol_version);
  1431. selection_box.serialize(os, protocol_version);
  1432. writeU8(os, legacy_facedir_simple);
  1433. writeU8(os, legacy_wallmounted);
  1434. serializeSimpleSoundSpec(sound_footstep, os, 10);
  1435. serializeSimpleSoundSpec(sound_dig, os, 10);
  1436. serializeSimpleSoundSpec(sound_dug, os, 10);
  1437. writeU8(os, rightclickable);
  1438. writeU8(os, drowning);
  1439. writeU8(os, leveled);
  1440. writeU8(os, liquid_range);
  1441. writeU8(os, waving);
  1442. os << serializeString(mesh);
  1443. collision_box.serialize(os, protocol_version);
  1444. writeU8(os, floodable);
  1445. writeU16(os, connects_to_ids.size());
  1446. for (content_t connects_to_id : connects_to_ids)
  1447. writeU16(os, connects_to_id);
  1448. writeU8(os, connect_sides);
  1449. } else {
  1450. throw SerializationError("ContentFeatures::serialize(): "
  1451. "Unsupported version requested");
  1452. }
  1453. }
  1454. void ContentFeatures::deSerializeOld(std::istream &is, int version)
  1455. {
  1456. if (version == 5) // In PROTOCOL_VERSION 13
  1457. {
  1458. name = deSerializeString(is);
  1459. groups.clear();
  1460. u32 groups_size = readU16(is);
  1461. for(u32 i=0; i<groups_size; i++){
  1462. std::string name = deSerializeString(is);
  1463. int value = readS16(is);
  1464. groups[name] = value;
  1465. }
  1466. drawtype = (enum NodeDrawType)readU8(is);
  1467. visual_scale = readF1000(is);
  1468. if (readU8(is) != 6)
  1469. throw SerializationError("unsupported tile count");
  1470. for (TileDef &i : tiledef)
  1471. i.deSerialize(is, version, drawtype);
  1472. if (readU8(is) != CF_SPECIAL_COUNT)
  1473. throw SerializationError("unsupported CF_SPECIAL_COUNT");
  1474. for (TileDef &i : tiledef_special)
  1475. i.deSerialize(is, version, drawtype);
  1476. alpha = readU8(is);
  1477. post_effect_color.setAlpha(readU8(is));
  1478. post_effect_color.setRed(readU8(is));
  1479. post_effect_color.setGreen(readU8(is));
  1480. post_effect_color.setBlue(readU8(is));
  1481. param_type = (enum ContentParamType)readU8(is);
  1482. param_type_2 = (enum ContentParamType2)readU8(is);
  1483. is_ground_content = readU8(is);
  1484. light_propagates = readU8(is);
  1485. sunlight_propagates = readU8(is);
  1486. walkable = readU8(is);
  1487. pointable = readU8(is);
  1488. diggable = readU8(is);
  1489. climbable = readU8(is);
  1490. buildable_to = readU8(is);
  1491. deSerializeString(is); // legacy: used to be metadata_name
  1492. liquid_type = (enum LiquidType)readU8(is);
  1493. liquid_alternative_flowing = deSerializeString(is);
  1494. liquid_alternative_source = deSerializeString(is);
  1495. liquid_viscosity = readU8(is);
  1496. light_source = readU8(is);
  1497. light_source = MYMIN(light_source, LIGHT_MAX);
  1498. damage_per_second = readU32(is);
  1499. node_box.deSerialize(is);
  1500. selection_box.deSerialize(is);
  1501. legacy_facedir_simple = readU8(is);
  1502. legacy_wallmounted = readU8(is);
  1503. deSerializeSimpleSoundSpec(sound_footstep, is, version);
  1504. deSerializeSimpleSoundSpec(sound_dig, is, version);
  1505. deSerializeSimpleSoundSpec(sound_dug, is, version);
  1506. } else if (version == 6) {
  1507. name = deSerializeString(is);
  1508. groups.clear();
  1509. u32 groups_size = readU16(is);
  1510. for (u32 i = 0; i < groups_size; i++) {
  1511. std::string name = deSerializeString(is);
  1512. int value = readS16(is);
  1513. groups[name] = value;
  1514. }
  1515. drawtype = (enum NodeDrawType)readU8(is);
  1516. visual_scale = readF1000(is);
  1517. if (readU8(is) != 6)
  1518. throw SerializationError("unsupported tile count");
  1519. for (TileDef &i : tiledef)
  1520. i.deSerialize(is, version, drawtype);
  1521. // CF_SPECIAL_COUNT in version 6 = 2
  1522. if (readU8(is) != 2)
  1523. throw SerializationError("unsupported CF_SPECIAL_COUNT");
  1524. for (u32 i = 0; i < 2; i++)
  1525. tiledef_special[i].deSerialize(is, version, drawtype);
  1526. alpha = readU8(is);
  1527. post_effect_color.setAlpha(readU8(is));
  1528. post_effect_color.setRed(readU8(is));
  1529. post_effect_color.setGreen(readU8(is));
  1530. post_effect_color.setBlue(readU8(is));
  1531. param_type = (enum ContentParamType)readU8(is);
  1532. param_type_2 = (enum ContentParamType2)readU8(is);
  1533. is_ground_content = readU8(is);
  1534. light_propagates = readU8(is);
  1535. sunlight_propagates = readU8(is);
  1536. walkable = readU8(is);
  1537. pointable = readU8(is);
  1538. diggable = readU8(is);
  1539. climbable = readU8(is);
  1540. buildable_to = readU8(is);
  1541. deSerializeString(is); // legacy: used to be metadata_name
  1542. liquid_type = (enum LiquidType)readU8(is);
  1543. liquid_alternative_flowing = deSerializeString(is);
  1544. liquid_alternative_source = deSerializeString(is);
  1545. liquid_viscosity = readU8(is);
  1546. liquid_renewable = readU8(is);
  1547. light_source = readU8(is);
  1548. damage_per_second = readU32(is);
  1549. node_box.deSerialize(is);
  1550. selection_box.deSerialize(is);
  1551. legacy_facedir_simple = readU8(is);
  1552. legacy_wallmounted = readU8(is);
  1553. deSerializeSimpleSoundSpec(sound_footstep, is, version);
  1554. deSerializeSimpleSoundSpec(sound_dig, is, version);
  1555. deSerializeSimpleSoundSpec(sound_dug, is, version);
  1556. rightclickable = readU8(is);
  1557. drowning = readU8(is);
  1558. leveled = readU8(is);
  1559. liquid_range = readU8(is);
  1560. } else if (version == 7 || version == 8){
  1561. name = deSerializeString(is);
  1562. groups.clear();
  1563. u32 groups_size = readU16(is);
  1564. for (u32 i = 0; i < groups_size; i++) {
  1565. std::string name = deSerializeString(is);
  1566. int value = readS16(is);
  1567. groups[name] = value;
  1568. }
  1569. drawtype = (enum NodeDrawType) readU8(is);
  1570. visual_scale = readF1000(is);
  1571. if (readU8(is) != 6)
  1572. throw SerializationError("unsupported tile count");
  1573. for (TileDef &i : tiledef)
  1574. i.deSerialize(is, version, drawtype);
  1575. if (readU8(is) != CF_SPECIAL_COUNT)
  1576. throw SerializationError("unsupported CF_SPECIAL_COUNT");
  1577. for (TileDef &i : tiledef_special)
  1578. i.deSerialize(is, version, drawtype);
  1579. alpha = readU8(is);
  1580. post_effect_color.setAlpha(readU8(is));
  1581. post_effect_color.setRed(readU8(is));
  1582. post_effect_color.setGreen(readU8(is));
  1583. post_effect_color.setBlue(readU8(is));
  1584. param_type = (enum ContentParamType) readU8(is);
  1585. param_type_2 = (enum ContentParamType2) readU8(is);
  1586. is_ground_content = readU8(is);
  1587. light_propagates = readU8(is);
  1588. sunlight_propagates = readU8(is);
  1589. walkable = readU8(is);
  1590. pointable = readU8(is);
  1591. diggable = readU8(is);
  1592. climbable = readU8(is);
  1593. buildable_to = readU8(is);
  1594. deSerializeString(is); // legacy: used to be metadata_name
  1595. liquid_type = (enum LiquidType) readU8(is);
  1596. liquid_alternative_flowing = deSerializeString(is);
  1597. liquid_alternative_source = deSerializeString(is);
  1598. liquid_viscosity = readU8(is);
  1599. liquid_renewable = readU8(is);
  1600. light_source = readU8(is);
  1601. light_source = MYMIN(light_source, LIGHT_MAX);
  1602. damage_per_second = readU32(is);
  1603. node_box.deSerialize(is);
  1604. selection_box.deSerialize(is);
  1605. legacy_facedir_simple = readU8(is);
  1606. legacy_wallmounted = readU8(is);
  1607. deSerializeSimpleSoundSpec(sound_footstep, is, version);
  1608. deSerializeSimpleSoundSpec(sound_dig, is, version);
  1609. deSerializeSimpleSoundSpec(sound_dug, is, version);
  1610. rightclickable = readU8(is);
  1611. drowning = readU8(is);
  1612. leveled = readU8(is);
  1613. liquid_range = readU8(is);
  1614. waving = readU8(is);
  1615. try {
  1616. mesh = deSerializeString(is);
  1617. collision_box.deSerialize(is);
  1618. floodable = readU8(is);
  1619. u16 connects_to_size = readU16(is);
  1620. connects_to_ids.clear();
  1621. for (u16 i = 0; i < connects_to_size; i++)
  1622. connects_to_ids.insert(readU16(is));
  1623. connect_sides = readU8(is);
  1624. } catch (SerializationError &e) {};
  1625. }else{
  1626. throw SerializationError("unsupported ContentFeatures version");
  1627. }
  1628. }
  1629. inline void CNodeDefManager::setNodeRegistrationStatus(bool completed)
  1630. {
  1631. m_node_registration_complete = completed;
  1632. }
  1633. void CNodeDefManager::pendNodeResolve(NodeResolver *nr)
  1634. {
  1635. nr->m_ndef = this;
  1636. if (m_node_registration_complete)
  1637. nr->nodeResolveInternal();
  1638. else
  1639. m_pending_resolve_callbacks.push_back(nr);
  1640. }
  1641. bool CNodeDefManager::cancelNodeResolveCallback(NodeResolver *nr)
  1642. {
  1643. size_t len = m_pending_resolve_callbacks.size();
  1644. for (size_t i = 0; i != len; i++) {
  1645. if (nr != m_pending_resolve_callbacks[i])
  1646. continue;
  1647. len--;
  1648. m_pending_resolve_callbacks[i] = m_pending_resolve_callbacks[len];
  1649. m_pending_resolve_callbacks.resize(len);
  1650. return true;
  1651. }
  1652. return false;
  1653. }
  1654. void CNodeDefManager::runNodeResolveCallbacks()
  1655. {
  1656. for (size_t i = 0; i != m_pending_resolve_callbacks.size(); i++) {
  1657. NodeResolver *nr = m_pending_resolve_callbacks[i];
  1658. nr->nodeResolveInternal();
  1659. }
  1660. m_pending_resolve_callbacks.clear();
  1661. }
  1662. void CNodeDefManager::resetNodeResolveState()
  1663. {
  1664. m_node_registration_complete = false;
  1665. m_pending_resolve_callbacks.clear();
  1666. }
  1667. void CNodeDefManager::mapNodeboxConnections()
  1668. {
  1669. for (ContentFeatures &f : m_content_features) {
  1670. if (f.drawtype != NDT_NODEBOX || f.node_box.type != NODEBOX_CONNECTED)
  1671. continue;
  1672. for (std::vector<std::string>::const_iterator it = f.connects_to.begin();
  1673. it != f.connects_to.end(); ++it) {
  1674. getIds(*it, f.connects_to_ids);
  1675. }
  1676. }
  1677. }
  1678. bool CNodeDefManager::nodeboxConnects(MapNode from, MapNode to, u8 connect_face)
  1679. {
  1680. const ContentFeatures &f1 = get(from);
  1681. if ((f1.drawtype != NDT_NODEBOX) || (f1.node_box.type != NODEBOX_CONNECTED))
  1682. return false;
  1683. // lookup target in connected set
  1684. if (f1.connects_to_ids.find(to.param0) == f1.connects_to_ids.end())
  1685. return false;
  1686. const ContentFeatures &f2 = get(to);
  1687. if ((f2.drawtype == NDT_NODEBOX) && (f2.node_box.type == NODEBOX_CONNECTED))
  1688. // ignores actually looking if back connection exists
  1689. return (f2.connects_to_ids.find(from.param0) != f2.connects_to_ids.end());
  1690. // does to node declare usable faces?
  1691. if (f2.connect_sides > 0) {
  1692. if ((f2.param_type_2 == CPT2_FACEDIR ||
  1693. f2.param_type_2 == CPT2_COLORED_FACEDIR)
  1694. && (connect_face >= 4)) {
  1695. static const u8 rot[33 * 4] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1696. 0, 0, 0, 0, 4, 32, 16, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1697. 0, // 4 - back
  1698. 8, 4, 32, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1699. 0, // 8 - right
  1700. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 8, 4, 32, 0,
  1701. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1702. 0, // 16 - front
  1703. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1704. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1705. 0, 0, 0, 0, 0, 0, 32, 16, 8, 4 // 32 - left
  1706. };
  1707. return (f2.connect_sides
  1708. & rot[(connect_face * 4) + (to.param2 & 0x1F)]);
  1709. }
  1710. return (f2.connect_sides & connect_face);
  1711. }
  1712. // the target is just a regular node, so connect no matter back connection
  1713. return true;
  1714. }
  1715. ////
  1716. //// NodeResolver
  1717. ////
  1718. NodeResolver::NodeResolver()
  1719. {
  1720. m_nodenames.reserve(16);
  1721. m_nnlistsizes.reserve(4);
  1722. }
  1723. NodeResolver::~NodeResolver()
  1724. {
  1725. if (!m_resolve_done && m_ndef)
  1726. m_ndef->cancelNodeResolveCallback(this);
  1727. }
  1728. void NodeResolver::nodeResolveInternal()
  1729. {
  1730. m_nodenames_idx = 0;
  1731. m_nnlistsizes_idx = 0;
  1732. resolveNodeNames();
  1733. m_resolve_done = true;
  1734. m_nodenames.clear();
  1735. m_nnlistsizes.clear();
  1736. }
  1737. bool NodeResolver::getIdFromNrBacklog(content_t *result_out,
  1738. const std::string &node_alt, content_t c_fallback)
  1739. {
  1740. if (m_nodenames_idx == m_nodenames.size()) {
  1741. *result_out = c_fallback;
  1742. errorstream << "NodeResolver: no more nodes in list" << std::endl;
  1743. return false;
  1744. }
  1745. content_t c;
  1746. std::string name = m_nodenames[m_nodenames_idx++];
  1747. bool success = m_ndef->getId(name, c);
  1748. if (!success && !node_alt.empty()) {
  1749. name = node_alt;
  1750. success = m_ndef->getId(name, c);
  1751. }
  1752. if (!success) {
  1753. errorstream << "NodeResolver: failed to resolve node name '" << name
  1754. << "'." << std::endl;
  1755. c = c_fallback;
  1756. }
  1757. *result_out = c;
  1758. return success;
  1759. }
  1760. bool NodeResolver::getIdsFromNrBacklog(std::vector<content_t> *result_out,
  1761. bool all_required, content_t c_fallback)
  1762. {
  1763. bool success = true;
  1764. if (m_nnlistsizes_idx == m_nnlistsizes.size()) {
  1765. errorstream << "NodeResolver: no more node lists" << std::endl;
  1766. return false;
  1767. }
  1768. size_t length = m_nnlistsizes[m_nnlistsizes_idx++];
  1769. while (length--) {
  1770. if (m_nodenames_idx == m_nodenames.size()) {
  1771. errorstream << "NodeResolver: no more nodes in list" << std::endl;
  1772. return false;
  1773. }
  1774. content_t c;
  1775. std::string &name = m_nodenames[m_nodenames_idx++];
  1776. if (name.substr(0,6) != "group:") {
  1777. if (m_ndef->getId(name, c)) {
  1778. result_out->push_back(c);
  1779. } else if (all_required) {
  1780. errorstream << "NodeResolver: failed to resolve node name '"
  1781. << name << "'." << std::endl;
  1782. result_out->push_back(c_fallback);
  1783. success = false;
  1784. }
  1785. } else {
  1786. std::set<content_t> cids;
  1787. std::set<content_t>::iterator it;
  1788. m_ndef->getIds(name, cids);
  1789. for (it = cids.begin(); it != cids.end(); ++it)
  1790. result_out->push_back(*it);
  1791. }
  1792. }
  1793. return success;
  1794. }