3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #ifndef NODEDEF_HEADER
21 #define NODEDEF_HEADER
23 #include "irrlichttypes_bloated.h"
33 #include "itemgroup.h"
34 #include "sound.h" // SimpleSoundSpec
35 #include "constants.h" // BS
37 class IItemDefManager;
42 typedef std::list<std::pair<content_t, int> > GroupItems;
43 typedef std::list<std::pair<std::string, std::vector<content_t> *> >
44 ContentVectorResolveList;
52 enum ContentParamType2
57 // Flowing liquid properties
59 // Direction for chests and furnaces and such
61 // Direction for signs, torches and such
63 // Block level like FLOWINGLIQUID
76 NODEBOX_REGULAR, // Regular block; allows buildable_to
77 NODEBOX_FIXED, // Static separately defined box(es)
78 NODEBOX_WALLMOUNTED, // Box for wall mounted nodes; (top, bottom, side)
79 NODEBOX_LEVELED, // Same as fixed, but with dynamic height from param2. for snow, ...
84 enum NodeBoxType type;
85 // NODEBOX_REGULAR (no parameters)
87 std::vector<aabb3f> fixed;
88 // NODEBOX_WALLMOUNTED
91 aabb3f wall_side; // being at the -X side
97 void serialize(std::ostream &os, u16 protocol_version) const;
98 void deSerialize(std::istream &is);
105 Stand-alone definition of a TileSpec (basically a server-side TileSpec)
107 enum TileAnimationType{
109 TAT_VERTICAL_FRAMES=1,
114 bool backface_culling; // Takes effect only in special cases
116 enum TileAnimationType type;
117 int aspect_w; // width for aspect ratio
118 int aspect_h; // height for aspect ratio
119 float length; // seconds
125 backface_culling = true;
126 animation.type = TAT_NONE;
127 animation.aspect_w = 1;
128 animation.aspect_h = 1;
129 animation.length = 1.0;
132 void serialize(std::ostream &os, u16 protocol_version) const;
133 void deSerialize(std::istream &is);
138 NDT_NORMAL, // A basic solid block
139 NDT_AIRLIKE, // Nothing is drawn
140 NDT_LIQUID, // Do not draw face towards same kind of flowing/source liquid
141 NDT_FLOWINGLIQUID, // A very special kind of thing
142 NDT_GLASSLIKE, // Glass-like, don't draw faces towards other glass
143 NDT_ALLFACES, // Leaves-like, draw all faces no matter what
144 NDT_ALLFACES_OPTIONAL, // Fancy -> allfaces, fast -> normal
151 NDT_GLASSLIKE_FRAMED, // Glass-like, draw connected frames and all all
153 // uses 2 textures, one for frames, second for faces
154 NDT_FIRELIKE, // Draw faces slightly rotated and only on connecting nodes,
155 NDT_GLASSLIKE_FRAMED_OPTIONAL, // enabled -> connected, disabled -> Glass-like
156 // uses 2 textures, one for frames, second for faces
157 NDT_MESH, // Uses static meshes
160 #define CF_SPECIAL_COUNT 6
162 struct ContentFeatures
169 // up down right left back front
172 // - Currently used for flowing liquids
173 TileSpec special_tiles[CF_SPECIAL_COUNT];
174 u8 solidness; // Used when choosing which face is drawn
175 u8 visual_solidness; // When solidness=0, this tells how it looks like
176 bool backface_culling;
179 // Server-side cached callback existence for fast skipping
180 bool has_on_construct;
181 bool has_on_destruct;
182 bool has_after_destruct;
188 std::string name; // "" = undefined node
189 ItemGroupList groups; // Same as in itemdef
192 enum NodeDrawType drawtype;
195 scene::IMesh *mesh_ptr[24];
197 float visual_scale; // Misc. scale parameter
199 TileDef tiledef_special[CF_SPECIAL_COUNT]; // eg. flowing liquid
202 // Post effect color, drawn when the camera is inside the node.
203 video::SColor post_effect_color;
204 // Type of MapNode::param1
205 ContentParamType param_type;
206 // Type of MapNode::param2
207 ContentParamType2 param_type_2;
208 // True for all ground-like things like stone and mud, false for eg. trees
209 bool is_ground_content;
210 bool light_propagates;
211 bool sunlight_propagates;
212 // This is used for collision detection.
213 // Also for general solidness queries.
215 // Player can point to these
217 // Player can dig these
219 // Player can climb these
221 // Player can build on these
223 // Player cannot build to these (placement prediction disabled)
225 // Flowing liquid or snow, value = default level
227 // Whether the node is non-liquid, source liquid or flowing liquid
228 enum LiquidType liquid_type;
229 // If the content is liquid, this is the flowing version of the liquid.
230 std::string liquid_alternative_flowing;
231 // If the content is liquid, this is the source version of the liquid.
232 std::string liquid_alternative_source;
233 // Viscosity for fluid flow, ranging from 1 to 7, with
234 // 1 giving almost instantaneous propagation and 7 being
235 // the slowest possible
237 // Is liquid renewable (new liquid source will be created between 2 existing)
238 bool liquid_renewable;
239 // Ice for water, water for ice
240 std::string freezemelt;
241 // Number of flowing liquids surrounding source
244 // Amount of light the node emits
246 u32 damage_per_second;
248 NodeBox selection_box;
249 NodeBox collision_box;
250 // Used for waving leaves/plants
252 // Compatibility with old maps
253 // Set to true if paramtype used to be 'facedir_simple'
254 bool legacy_facedir_simple;
255 // Set to true if wall_mounted used to be set to true
256 bool legacy_wallmounted;
259 SimpleSoundSpec sound_footstep;
260 SimpleSoundSpec sound_dig;
261 SimpleSoundSpec sound_dug;
270 void serialize(std::ostream &os, u16 protocol_version);
271 void deSerialize(std::istream &is);
272 void serializeOld(std::ostream &os, u16 protocol_version);
273 void deSerializeOld(std::istream &is, int version);
278 bool isLiquid() const{
279 return (liquid_type != LIQUID_NONE);
281 bool sameLiquid(const ContentFeatures &f) const{
282 if(!isLiquid() || !f.isLiquid()) return false;
283 return (liquid_alternative_flowing == f.liquid_alternative_flowing);
287 struct NodeResolveInfo {
288 std::string n_wanted;
290 content_t c_fallback;
294 #define NR_STATUS_FAILURE 0
295 #define NR_STATUS_PENDING 1
296 #define NR_STATUS_SUCCESS 2
301 NodeResolver attempts to resolve node names to content ID integers. If the
302 node registration phase has not yet finished at the time the resolution
303 request is placed, the request is marked as pending and added to an internal
304 queue. The name resolution request is later satisfied by writing directly
305 to the output location when the node registration phase has been completed.
307 This is primarily intended to be used for objects registered during script
308 initialization (i.e. while nodes are being registered) that reference
313 NodeResolver(INodeDefManager *ndef);
317 Add a request to resolve the node n_wanted and set *content to the
318 result, or alternatively, n_alt if n_wanted is not found. If n_alt
319 cannot be found either, or has not been specified, *content is set
322 If node registration is complete, the request is finished immediately
323 and NR_STATUS_SUCCESS is returned (or NR_STATUS_FAILURE if no node can
324 be found). Otherwise, NR_STATUS_PENDING is returned and the resolution
327 N.B. If the memory in which content is located has been deallocated
328 before the pending request had been satisfied, cancelNode() must be
331 @param n_wanted Name of node that is wanted.
332 @param n_alt Name of node in case n_wanted could not be found. Blank
333 if no alternative node is desired.
334 @param c_fallback Content ID that content is set to in case of node
335 resolution failure (should be CONTENT_AIR, CONTENT_IGNORE, etc.)
336 @param content Pointer to content_t that receives the result of the
337 node name resolution.
338 @return Status of node resolution request.
340 int addNode(std::string n_wanted, std::string n_alt,
341 content_t c_fallback, content_t *content);
344 Add a request to resolve the node(s) specified by nodename.
346 If node registration is complete, the request is finished immediately
347 and NR_STATUS_SUCCESS is returned if at least one node is resolved; if
348 zero were resolved, NR_STATUS_FAILURE. Otherwise, NR_STATUS_PENDING is
349 returned and the resolution request is queued.
351 N.B. If the memory in which content_vec is located has been deallocated
352 before the pending request had been satisfied, cancelNodeList() must be
355 @param nodename Name of node (or node group) to be resolved.
356 @param content_vec Pointer to content_t vector onto which the results
359 @return Status of node resolution request.
361 int addNodeList(const char *nodename, std::vector<content_t> *content_vec);
364 Removes all pending requests from the resolution queue to be satisfied
367 @param content Location of the content ID for the request being
369 @return Number of pending requests cancelled.
371 bool cancelNode(content_t *content);
374 Removes all pending requests from the resolution queue to be satisfied
377 @param content_vec Location of the content ID vector for requests being
379 @return Number of pending requests cancelled.
381 int cancelNodeList(std::vector<content_t> *content_vec);
384 Carries out all pending node resolution requests. Call this when the
385 node registration phase has completed.
387 Internally marks node registration as complete.
389 @return Number of failed pending requests.
394 Returns the status of the node registration phase.
396 @return Boolean of whether the registration phase is complete.
398 bool isNodeRegFinished() { return m_is_node_registration_complete; }
401 INodeDefManager *m_ndef;
402 bool m_is_node_registration_complete;
403 std::list<NodeResolveInfo *> m_pending_contents;
404 ContentVectorResolveList m_pending_content_vecs;
407 class INodeDefManager
411 virtual ~INodeDefManager(){}
412 // Get node definition
413 virtual const ContentFeatures& get(content_t c) const=0;
414 virtual const ContentFeatures& get(const MapNode &n) const=0;
415 virtual bool getId(const std::string &name, content_t &result) const=0;
416 virtual content_t getId(const std::string &name) const=0;
417 // Allows "group:name" in addition to regular node names
418 virtual void getIds(const std::string &name, std::set<content_t> &result)
420 virtual const ContentFeatures& get(const std::string &name) const=0;
422 virtual void serialize(std::ostream &os, u16 protocol_version)=0;
424 virtual NodeResolver *getResolver()=0;
427 class IWritableNodeDefManager : public INodeDefManager
430 IWritableNodeDefManager(){}
431 virtual ~IWritableNodeDefManager(){}
432 virtual IWritableNodeDefManager* clone()=0;
433 // Get node definition
434 virtual const ContentFeatures& get(content_t c) const=0;
435 virtual const ContentFeatures& get(const MapNode &n) const=0;
436 virtual bool getId(const std::string &name, content_t &result) const=0;
437 // If not found, returns CONTENT_IGNORE
438 virtual content_t getId(const std::string &name) const=0;
439 // Allows "group:name" in addition to regular node names
440 virtual void getIds(const std::string &name, std::set<content_t> &result)
442 // If not found, returns the features of CONTENT_UNKNOWN
443 virtual const ContentFeatures& get(const std::string &name) const=0;
445 // Register node definition by name (allocate an id)
446 // If returns CONTENT_IGNORE, could not allocate id
447 virtual content_t set(const std::string &name,
448 const ContentFeatures &def)=0;
449 // If returns CONTENT_IGNORE, could not allocate id
450 virtual content_t allocateDummy(const std::string &name)=0;
453 Update item alias mapping.
454 Call after updating item definitions.
456 virtual void updateAliases(IItemDefManager *idef)=0;
459 Update tile textures to latest return values of TextueSource.
461 virtual void updateTextures(IGameDef *gamedef)=0;
463 virtual void serialize(std::ostream &os, u16 protocol_version)=0;
464 virtual void deSerialize(std::istream &is)=0;
466 virtual NodeResolver *getResolver()=0;
469 IWritableNodeDefManager *createNodeDefManager();