Skip to content

Commit

Permalink
Nodebox: Allow nodeboxes to "connect"
Browse files Browse the repository at this point in the history
We introduce a new nodebox type "connected", and allow these nodes to
have optional nodeboxes that connect it to other connecting nodeboxes.

This is all done at scenedraw time in the client. The client will
inspect the surrounding nodes and if they are to be connected to,
it will draw the appropriate connecting nodeboxes to make those
connections.

In the node_box definition, we have to specify separate nodeboxes for
each valid connection. This allows us to make nodes that connect only
horizontally (the common case) by providing optional nodeboxes for +x,
-x, +z, -z directions. Or this allows us to make wires that can connect
up and down, by providing nodeboxes that connect it up and down (+y,
-y) as well.

The optional nodeboxes can be arrays. They are named "connect_top,
"connect_bottom", "connect_front", "connect_left", "connect_back" and
"connect_right". Here, "front" means the south facing side of the node
that has facedir = 0.

Additionally, a "fixed" nodebox list present will always be drawn,
so one can make a central post, for instance. This "fixed" nodebox
can be omitted, or it can be an array of nodeboxes.

Collision boxes are also updated in exactly the same fashion, which
allows you to walk over the upper extremities of the individual
node boxes, or stand really close to them. You can also walk up
node noxes that are small in height, all as expected, and unlike the
NDT_FENCELIKE nodes.

I've posted a screenshot demonstrating the flexibility at
    http://i.imgur.com/zaJq8jo.png
In the screenshot, all connecting nodes are of this new subtype.

Transparent textures render incorrectly, Which I don't think is
related to this text, as other nodeboxes also have issues with this.

A protocol bump is performed in order to be able to send older clients
a nodeblock that is usable for them. In order to avoid abuse of users
we send older clients a "full-size" node, so that it's impossible for
them to try and walk through a fence or wall that's created in this
fashion. This was tested with a pre-bump client connected against a
server running the new protocol.

These nodes connect to other nodes, and you can select which ones
those are by specifying node names (or group names) in the
connects_to string array:
      connects_to = { "group:fence", "default:wood" }
By default, nodes do not connect to anything, allowing you to create
nodes that always have to be paired in order to connect. lua_api.txt
is updated to reflect the extension to the node_box API.

Example lua code needed to generate these nodes can be found here:
    https://gist.github.com/sofar/b381c8c192c8e53e6062
  • Loading branch information
sofar authored and ShadowNinja committed Mar 12, 2016
1 parent 8c951ca commit e737b1c
Show file tree
Hide file tree
Showing 13 changed files with 335 additions and 53 deletions.
16 changes: 16 additions & 0 deletions doc/lua_api.txt
Expand Up @@ -617,6 +617,18 @@ A nodebox is defined as any of:
wall_bottom = box,
wall_side = box
}
{
-- A node that has optional boxes depending on neighbouring nodes'
-- presence and type. See also `connects_to`.
type = "connected",
fixed = box OR {box1, box2, ...}
connect_top = box OR {box1, box2, ...}
connect_bottom = box OR {box1, box2, ...}
connect_front = box OR {box1, box2, ...}
connect_left = box OR {box1, box2, ...}
connect_back = box OR {box1, box2, ...}
connect_right = box OR {box1, box2, ...}
}

A `box` is defined as:

Expand Down Expand Up @@ -3461,6 +3473,10 @@ Definition tables
light_source = 0, -- Amount of light emitted by node
damage_per_second = 0, -- If player is inside node, this damage is caused
node_box = {type="regular"}, -- See "Node boxes"
connects_to = nodenames, --[[
* Used for nodebox nodes with the type == "connected"
* Specifies to what neighboring nodes connections will be drawn
* e.g. `{"group:fence", "default:wood"}` or `"default:stone"` ]]
mesh = "model",
selection_box = {type="regular"}, -- See "Node boxes" --[[
^ If drawtype "nodebox" is used and selection_box is nil, then node_box is used. ]]
Expand Down
40 changes: 38 additions & 2 deletions src/collision.cpp
Expand Up @@ -185,6 +185,13 @@ bool wouldCollideWithCeiling(
return false;
}

static inline void getNeighborConnectingFace(v3s16 p, INodeDefManager *nodedef,
Map *map, MapNode n, int v, int *neighbors)
{
MapNode n2 = map->getNodeNoEx(p);
if (nodedef->nodeboxConnects(n, n2))
*neighbors |= v;
}

collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
f32 pos_max_d, const aabb3f &box_0,
Expand Down Expand Up @@ -261,12 +268,41 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
// Object collides into walkable nodes

any_position_valid = true;
const ContentFeatures &f = gamedef->getNodeDefManager()->get(n);
INodeDefManager *nodedef = gamedef->getNodeDefManager();
const ContentFeatures &f = nodedef->get(n);
if(f.walkable == false)
continue;
int n_bouncy_value = itemgroup_get(f.groups, "bouncy");

std::vector<aabb3f> nodeboxes = n.getCollisionBoxes(gamedef->ndef());
int neighbors = 0;
if (f.drawtype == NDT_NODEBOX && f.node_box.type == NODEBOX_CONNECTED) {
v3s16 p2 = p;

p2.Y++;
getNeighborConnectingFace(p2, nodedef, map, n, 1, &neighbors);

p2 = p;
p2.Y--;
getNeighborConnectingFace(p2, nodedef, map, n, 2, &neighbors);

p2 = p;
p2.Z--;
getNeighborConnectingFace(p2, nodedef, map, n, 4, &neighbors);

p2 = p;
p2.X--;
getNeighborConnectingFace(p2, nodedef, map, n, 8, &neighbors);

p2 = p;
p2.Z++;
getNeighborConnectingFace(p2, nodedef, map, n, 16, &neighbors);

p2 = p;
p2.X++;
getNeighborConnectingFace(p2, nodedef, map, n, 32, &neighbors);
}
std::vector<aabb3f> nodeboxes;
n.getCollisionBoxes(gamedef->ndef(), &nodeboxes, neighbors);
for(std::vector<aabb3f>::iterator
i = nodeboxes.begin();
i != nodeboxes.end(); ++i)
Expand Down
41 changes: 40 additions & 1 deletion src/content_mapblock.cpp
Expand Up @@ -163,6 +163,14 @@ void makeCuboid(MeshCollector *collector, const aabb3f &box,
}
}

static inline void getNeighborConnectingFace(v3s16 p, INodeDefManager *nodedef,
MeshMakeData *data, MapNode n, int v, int *neighbors)
{
MapNode n2 = data->m_vmanip.getNodeNoEx(p);
if (nodedef->nodeboxConnects(n, n2))
*neighbors |= v;
}

/*
TODO: Fix alpha blending for special nodes
Currently only the last element rendered is blended correct
Expand Down Expand Up @@ -1501,7 +1509,38 @@ void mapblock_mesh_generate_special(MeshMakeData *data,

v3f pos = intToFloat(p, BS);

std::vector<aabb3f> boxes = n.getNodeBoxes(nodedef);
int neighbors = 0;

// locate possible neighboring nodes to connect to
if (f.node_box.type == NODEBOX_CONNECTED) {
v3s16 p2 = p;

p2.Y++;
getNeighborConnectingFace(blockpos_nodes + p2, nodedef, data, n, 1, &neighbors);

p2 = p;
p2.Y--;
getNeighborConnectingFace(blockpos_nodes + p2, nodedef, data, n, 2, &neighbors);

p2 = p;
p2.Z--;
getNeighborConnectingFace(blockpos_nodes + p2, nodedef, data, n, 4, &neighbors);

p2 = p;
p2.X--;
getNeighborConnectingFace(blockpos_nodes + p2, nodedef, data, n, 8, &neighbors);

p2 = p;
p2.Z++;
getNeighborConnectingFace(blockpos_nodes + p2, nodedef, data, n, 16, &neighbors);

p2 = p;
p2.X++;
getNeighborConnectingFace(blockpos_nodes + p2, nodedef, data, n, 32, &neighbors);
}

std::vector<aabb3f> boxes;
n.getNodeBoxes(nodedef, &boxes, neighbors);
for(std::vector<aabb3f>::iterator
i = boxes.begin();
i != boxes.end(); ++i)
Expand Down
7 changes: 5 additions & 2 deletions src/game.cpp
Expand Up @@ -358,7 +358,9 @@ PointedThing getPointedThing(Client *client, Hud *hud, const v3f &player_positio
if (!isPointableNode(n, client, liquids_pointable)) {
continue;
}
std::vector<aabb3f> boxes = n.getSelectionBoxes(nodedef);

std::vector<aabb3f> boxes;
n.getSelectionBoxes(nodedef, &boxes);

v3s16 np(x, y, z);
v3f npf = intToFloat(np, BS);
Expand Down Expand Up @@ -389,7 +391,8 @@ PointedThing getPointedThing(Client *client, Hud *hud, const v3f &player_positio
f32 d = 0.001 * BS;
MapNode n = map.getNodeNoEx(pointed_pos);
v3f npf = intToFloat(pointed_pos, BS);
std::vector<aabb3f> boxes = n.getSelectionBoxes(nodedef);
std::vector<aabb3f> boxes;
n.getSelectionBoxes(nodedef, &boxes);
f32 face_min_distance = 1000 * BS;
for (std::vector<aabb3f>::const_iterator
i = boxes.begin();
Expand Down
3 changes: 2 additions & 1 deletion src/localplayer.cpp
Expand Up @@ -310,7 +310,8 @@ void LocalPlayer::move(f32 dtime, Environment *env, f32 pos_max_d,
if (sneak_node_found) {
f32 cb_max = 0;
MapNode n = map->getNodeNoEx(m_sneak_node);
std::vector<aabb3f> nodeboxes = n.getCollisionBoxes(nodemgr);
std::vector<aabb3f> nodeboxes;
n.getCollisionBoxes(nodemgr, &nodeboxes);
for (std::vector<aabb3f>::iterator it = nodeboxes.begin();
it != nodeboxes.end(); ++it) {
aabb3f box = *it;
Expand Down
65 changes: 52 additions & 13 deletions src/mapnode.cpp
Expand Up @@ -214,12 +214,12 @@ void MapNode::rotateAlongYAxis(INodeDefManager *nodemgr, Rotation rot)
}
}

static std::vector<aabb3f> transformNodeBox(const MapNode &n,
const NodeBox &nodebox, INodeDefManager *nodemgr)
void transformNodeBox(const MapNode &n, const NodeBox &nodebox,
INodeDefManager *nodemgr, std::vector<aabb3f> *p_boxes, u8 neighbors = 0)
{
std::vector<aabb3f> boxes;
if(nodebox.type == NODEBOX_FIXED || nodebox.type == NODEBOX_LEVELED)
{
std::vector<aabb3f> &boxes = *p_boxes;

if (nodebox.type == NODEBOX_FIXED || nodebox.type == NODEBOX_LEVELED) {
const std::vector<aabb3f> &fixed = nodebox.fixed;
int facedir = n.getFaceDir(nodemgr);
u8 axisdir = facedir>>2;
Expand Down Expand Up @@ -395,32 +395,71 @@ static std::vector<aabb3f> transformNodeBox(const MapNode &n,
boxes.push_back(box);
}
}
else if (nodebox.type == NODEBOX_CONNECTED)
{
size_t boxes_size = boxes.size();
boxes_size += nodebox.fixed.size();
if (neighbors & 1)
boxes_size += nodebox.connect_top.size();
if (neighbors & 2)
boxes_size += nodebox.connect_bottom.size();
if (neighbors & 4)
boxes_size += nodebox.connect_front.size();
if (neighbors & 8)
boxes_size += nodebox.connect_left.size();
if (neighbors & 16)
boxes_size += nodebox.connect_back.size();
if (neighbors & 32)
boxes_size += nodebox.connect_right.size();
boxes.reserve(boxes_size);

#define BOXESPUSHBACK(c) do { \
for (std::vector<aabb3f>::const_iterator \
it = (c).begin(); \
it != (c).end(); ++it) \
(boxes).push_back(*it); \
} while (0)

BOXESPUSHBACK(nodebox.fixed);

if (neighbors & 1)
BOXESPUSHBACK(nodebox.connect_top);
if (neighbors & 2)
BOXESPUSHBACK(nodebox.connect_bottom);
if (neighbors & 4)
BOXESPUSHBACK(nodebox.connect_front);
if (neighbors & 8)
BOXESPUSHBACK(nodebox.connect_left);
if (neighbors & 16)
BOXESPUSHBACK(nodebox.connect_back);
if (neighbors & 32)
BOXESPUSHBACK(nodebox.connect_right);
}
else // NODEBOX_REGULAR
{
boxes.push_back(aabb3f(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2));
}
return boxes;
}

std::vector<aabb3f> MapNode::getNodeBoxes(INodeDefManager *nodemgr) const
void MapNode::getNodeBoxes(INodeDefManager *nodemgr, std::vector<aabb3f> *boxes, u8 neighbors)
{
const ContentFeatures &f = nodemgr->get(*this);
return transformNodeBox(*this, f.node_box, nodemgr);
transformNodeBox(*this, f.node_box, nodemgr, boxes, neighbors);
}

std::vector<aabb3f> MapNode::getCollisionBoxes(INodeDefManager *nodemgr) const
void MapNode::getCollisionBoxes(INodeDefManager *nodemgr, std::vector<aabb3f> *boxes, u8 neighbors)
{
const ContentFeatures &f = nodemgr->get(*this);
if (f.collision_box.fixed.empty())
return transformNodeBox(*this, f.node_box, nodemgr);
transformNodeBox(*this, f.node_box, nodemgr, boxes, neighbors);
else
return transformNodeBox(*this, f.collision_box, nodemgr);
transformNodeBox(*this, f.collision_box, nodemgr, boxes, neighbors);
}

std::vector<aabb3f> MapNode::getSelectionBoxes(INodeDefManager *nodemgr) const
void MapNode::getSelectionBoxes(INodeDefManager *nodemgr, std::vector<aabb3f> *boxes)
{
const ContentFeatures &f = nodemgr->get(*this);
return transformNodeBox(*this, f.selection_box, nodemgr);
transformNodeBox(*this, f.selection_box, nodemgr, boxes);
}

u8 MapNode::getMaxLevel(INodeDefManager *nodemgr) const
Expand Down
6 changes: 3 additions & 3 deletions src/mapnode.h
Expand Up @@ -240,17 +240,17 @@ struct MapNode
/*
Gets list of node boxes (used for rendering (NDT_NODEBOX))
*/
std::vector<aabb3f> getNodeBoxes(INodeDefManager *nodemgr) const;
void getNodeBoxes(INodeDefManager *nodemgr, std::vector<aabb3f> *boxes, u8 neighbors = 0);

/*
Gets list of selection boxes
*/
std::vector<aabb3f> getSelectionBoxes(INodeDefManager *nodemgr) const;
void getSelectionBoxes(INodeDefManager *nodemg, std::vector<aabb3f> *boxes);

/*
Gets list of collision boxes
*/
std::vector<aabb3f> getCollisionBoxes(INodeDefManager *nodemgr) const;
void getCollisionBoxes(INodeDefManager *nodemgr, std::vector<aabb3f> *boxes, u8 neighbors = 0);

/*
Liquid helpers
Expand Down
1 change: 1 addition & 0 deletions src/network/networkprotocol.h
Expand Up @@ -135,6 +135,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
PROTOCOL_VERSION 27:
backface_culling: backwards compatibility for playing with
newer client on pre-27 servers.
Add nodedef v3 - connected nodeboxes
*/

#define LATEST_PROTOCOL_VERSION 27
Expand Down

0 comments on commit e737b1c

Please sign in to comment.