/******************************************************************************/
#include "pathfinder.h"
-#include "serverenvironment.h"
-#include "server.h"
+#include "map.h"
#include "nodedef.h"
//#define PATHFINDER_DEBUG
class Pathfinder {
public:
- /**
- * default constructor
- */
- Pathfinder() = default;
+ Pathfinder() = delete;
+ Pathfinder(Map *map, const NodeDefManager *ndef) : m_map(map), m_ndef(ndef) {}
~Pathfinder();
* @param max_drop maximum number of blocks a path may drop
* @param algo Algorithm to use for finding a path
*/
- std::vector<v3s16> getPath(ServerEnvironment *env,
- v3s16 source,
+ std::vector<v3s16> getPath(v3s16 source,
v3s16 destination,
unsigned int searchdistance,
unsigned int max_jump,
friend class GridNodeContainer;
GridNodeContainer *m_nodes_container = nullptr;
- ServerEnvironment *m_env = 0; /**< minetest environment pointer */
+ Map *m_map = nullptr;
+
+ const NodeDefManager *m_ndef = nullptr;
friend class PathfinderCompareHeuristic;
/* implementation */
/******************************************************************************/
-std::vector<v3s16> get_path(ServerEnvironment* env,
- v3s16 source,
- v3s16 destination,
- unsigned int searchdistance,
- unsigned int max_jump,
- unsigned int max_drop,
- PathAlgorithm algo)
+std::vector<v3s16> get_path(Map* map, const NodeDefManager *ndef,
+ v3s16 source,
+ v3s16 destination,
+ unsigned int searchdistance,
+ unsigned int max_jump,
+ unsigned int max_drop,
+ PathAlgorithm algo)
{
- Pathfinder searchclass;
-
- return searchclass.getPath(env,
- source, destination,
+ return Pathfinder(map, ndef).getPath(source, destination,
searchdistance, max_jump, max_drop, algo);
}
void GridNodeContainer::initNode(v3s16 ipos, PathGridnode *p_node)
{
- const NodeDefManager *ndef = m_pathf->m_env->getGameDef()->ndef();
+ const NodeDefManager *ndef = m_pathf->m_ndef;
PathGridnode &elem = *p_node;
v3s16 realpos = m_pathf->getRealPos(ipos);
- MapNode current = m_pathf->m_env->getMap().getNode(realpos);
- MapNode below = m_pathf->m_env->getMap().getNode(realpos + v3s16(0, -1, 0));
+ MapNode current = m_pathf->m_map->getNode(realpos);
+ MapNode below = m_pathf->m_map->getNode(realpos + v3s16(0, -1, 0));
if ((current.param0 == CONTENT_IGNORE) ||
/******************************************************************************/
-std::vector<v3s16> Pathfinder::getPath(ServerEnvironment *env,
- v3s16 source,
+std::vector<v3s16> Pathfinder::getPath(v3s16 source,
v3s16 destination,
unsigned int searchdistance,
unsigned int max_jump,
#endif
std::vector<v3s16> retval;
- //check parameters
- if (env == 0) {
- ERROR_TARGET << "Missing environment pointer" << std::endl;
- return retval;
- }
-
//initialization
m_searchdistance = searchdistance;
- m_env = env;
m_maxjump = max_jump;
m_maxdrop = max_drop;
m_start = source;
#endif
//fail if source or destination is walkable
- const NodeDefManager *ndef = m_env->getGameDef()->ndef();
- MapNode node_at_pos = m_env->getMap().getNode(destination);
- if (ndef->get(node_at_pos).walkable) {
+ MapNode node_at_pos = m_map->getNode(destination);
+ if (m_ndef->get(node_at_pos).walkable) {
VERBOSE_TARGET << "Destination is walkable. " <<
"Pos: " << PP(destination) << std::endl;
return retval;
}
- node_at_pos = m_env->getMap().getNode(source);
- if (ndef->get(node_at_pos).walkable) {
+ node_at_pos = m_map->getNode(source);
+ if (m_ndef->get(node_at_pos).walkable) {
VERBOSE_TARGET << "Source is walkable. " <<
"Pos: " << PP(source) << std::endl;
return retval;
/******************************************************************************/
PathCost Pathfinder::calcCost(v3s16 pos, v3s16 dir)
{
- const NodeDefManager *ndef = m_env->getGameDef()->ndef();
PathCost retval;
retval.updated = true;
return retval;
}
- MapNode node_at_pos2 = m_env->getMap().getNode(pos2);
+ MapNode node_at_pos2 = m_map->getNode(pos2);
//did we get information about node?
if (node_at_pos2.param0 == CONTENT_IGNORE ) {
return retval;
}
- if (!ndef->get(node_at_pos2).walkable) {
+ if (!m_ndef->get(node_at_pos2).walkable) {
MapNode node_below_pos2 =
- m_env->getMap().getNode(pos2 + v3s16(0, -1, 0));
+ m_map->getNode(pos2 + v3s16(0, -1, 0));
//did we get information about node?
if (node_below_pos2.param0 == CONTENT_IGNORE ) {
}
//test if the same-height neighbor is suitable
- if (ndef->get(node_below_pos2).walkable) {
+ if (m_ndef->get(node_below_pos2).walkable) {
//SUCCESS!
retval.valid = true;
retval.value = 1;
else {
//test if we can fall a couple of nodes (m_maxdrop)
v3s16 testpos = pos2 + v3s16(0, -1, 0);
- MapNode node_at_pos = m_env->getMap().getNode(testpos);
+ MapNode node_at_pos = m_map->getNode(testpos);
while ((node_at_pos.param0 != CONTENT_IGNORE) &&
- (!ndef->get(node_at_pos).walkable) &&
+ (!m_ndef->get(node_at_pos).walkable) &&
(testpos.Y > m_limits.MinEdge.Y)) {
testpos += v3s16(0, -1, 0);
- node_at_pos = m_env->getMap().getNode(testpos);
+ node_at_pos = m_map->getNode(testpos);
}
//did we find surface?
if ((testpos.Y >= m_limits.MinEdge.Y) &&
(node_at_pos.param0 != CONTENT_IGNORE) &&
- (ndef->get(node_at_pos).walkable)) {
+ (m_ndef->get(node_at_pos).walkable)) {
if ((pos2.Y - testpos.Y - 1) <= m_maxdrop) {
//SUCCESS!
retval.valid = true;
v3s16 targetpos = pos2; // position for jump target
v3s16 jumppos = pos; // position for checking if jumping space is free
- MapNode node_target = m_env->getMap().getNode(targetpos);
- MapNode node_jump = m_env->getMap().getNode(jumppos);
+ MapNode node_target = m_map->getNode(targetpos);
+ MapNode node_jump = m_map->getNode(jumppos);
bool headbanger = false; // true if anything blocks jumppath
while ((node_target.param0 != CONTENT_IGNORE) &&
- (ndef->get(node_target).walkable) &&
+ (m_ndef->get(node_target).walkable) &&
(targetpos.Y < m_limits.MaxEdge.Y)) {
//if the jump would hit any solid node, discard
if ((node_jump.param0 == CONTENT_IGNORE) ||
- (ndef->get(node_jump).walkable)) {
+ (m_ndef->get(node_jump).walkable)) {
headbanger = true;
break;
}
targetpos += v3s16(0, 1, 0);
jumppos += v3s16(0, 1, 0);
- node_target = m_env->getMap().getNode(targetpos);
- node_jump = m_env->getMap().getNode(jumppos);
+ node_target = m_map->getNode(targetpos);
+ node_jump = m_map->getNode(jumppos);
}
//check headbanger one last time
if ((node_jump.param0 == CONTENT_IGNORE) ||
- (ndef->get(node_jump).walkable)) {
+ (m_ndef->get(node_jump).walkable)) {
headbanger = true;
}
//did we find surface without banging our head?
if ((!headbanger) && (targetpos.Y <= m_limits.MaxEdge.Y) &&
- (!ndef->get(node_target).walkable)) {
+ (!m_ndef->get(node_target).walkable)) {
if (targetpos.Y - pos2.Y <= m_maxjump) {
//SUCCESS!
if (max_down == 0)
return pos;
v3s16 testpos = v3s16(pos);
- MapNode node_at_pos = m_env->getMap().getNode(testpos);
- const NodeDefManager *ndef = m_env->getGameDef()->ndef();
+ MapNode node_at_pos = m_map->getNode(testpos);
unsigned int down = 0;
while ((node_at_pos.param0 != CONTENT_IGNORE) &&
- (!ndef->get(node_at_pos).walkable) &&
+ (!m_ndef->get(node_at_pos).walkable) &&
(testpos.Y > m_limits.MinEdge.Y) &&
(down <= max_down)) {
testpos += v3s16(0, -1, 0);
down++;
- node_at_pos = m_env->getMap().getNode(testpos);
+ node_at_pos = m_map->getNode(testpos);
}
//did we find surface?
if ((testpos.Y >= m_limits.MinEdge.Y) &&
(node_at_pos.param0 != CONTENT_IGNORE) &&
- (ndef->get(node_at_pos).walkable)) {
+ (m_ndef->get(node_at_pos).walkable)) {
if (down == 0) {
pos = testpos;
} else if ((down - 1) <= max_down) {
{
MAP_LOCK_REQUIRED;
NodeTimerRef *o = checkobject(L, 1);
- ServerEnvironment *env = o->m_env;
- if(env == NULL) return 0;
f32 t = readParam<float>(L,2);
f32 e = readParam<float>(L,3);
- env->getMap().setNodeTimer(NodeTimer(t, e, o->m_p));
+ o->m_map->setNodeTimer(NodeTimer(t, e, o->m_p));
return 0;
}
{
MAP_LOCK_REQUIRED;
NodeTimerRef *o = checkobject(L, 1);
- ServerEnvironment *env = o->m_env;
- if(env == NULL) return 0;
f32 t = readParam<float>(L,2);
- env->getMap().setNodeTimer(NodeTimer(t, 0, o->m_p));
+ o->m_map->setNodeTimer(NodeTimer(t, 0, o->m_p));
return 0;
}
{
MAP_LOCK_REQUIRED;
NodeTimerRef *o = checkobject(L, 1);
- ServerEnvironment *env = o->m_env;
- if(env == NULL) return 0;
- env->getMap().removeNodeTimer(o->m_p);
+ o->m_map->removeNodeTimer(o->m_p);
return 0;
}
{
MAP_LOCK_REQUIRED;
NodeTimerRef *o = checkobject(L, 1);
- ServerEnvironment *env = o->m_env;
- if(env == NULL) return 0;
-
- NodeTimer t = env->getMap().getNodeTimer(o->m_p);
+ NodeTimer t = o->m_map->getNodeTimer(o->m_p);
lua_pushboolean(L,(t.timeout != 0));
return 1;
}
{
MAP_LOCK_REQUIRED;
NodeTimerRef *o = checkobject(L, 1);
- ServerEnvironment *env = o->m_env;
- if(env == NULL) return 0;
-
- NodeTimer t = env->getMap().getNodeTimer(o->m_p);
+ NodeTimer t = o->m_map->getNodeTimer(o->m_p);
lua_pushnumber(L,t.timeout);
return 1;
}
{
MAP_LOCK_REQUIRED;
NodeTimerRef *o = checkobject(L, 1);
- ServerEnvironment *env = o->m_env;
- if(env == NULL) return 0;
-
- NodeTimer t = env->getMap().getNodeTimer(o->m_p);
+ NodeTimer t = o->m_map->getNodeTimer(o->m_p);
lua_pushnumber(L,t.elapsed);
return 1;
}
-
-NodeTimerRef::NodeTimerRef(v3s16 p, ServerEnvironment *env):
- m_p(p),
- m_env(env)
-{
-}
-
// Creates an NodeTimerRef and leaves it on top of stack
// Not callable from Lua; all references are created on the C side.
-void NodeTimerRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)
+void NodeTimerRef::create(lua_State *L, v3s16 p, ServerMap *map)
{
- NodeTimerRef *o = new NodeTimerRef(p, env);
+ NodeTimerRef *o = new NodeTimerRef(p, map);
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
luaL_getmetatable(L, className);
lua_setmetatable(L, -2);
}
-void NodeTimerRef::set_null(lua_State *L)
-{
- NodeTimerRef *o = checkobject(L, -1);
- o->m_env = NULL;
-}
-
void NodeTimerRef::Register(lua_State *L)
{
lua_newtable(L);