summaryrefslogtreecommitdiff
path: root/src/scriptapi_env.cpp
diff options
context:
space:
mode:
authorsapier <Sapier at GMX dot net>2013-03-17 17:03:44 +0000
committerkwolekr <kwolekr@minetest.net>2013-04-06 11:24:03 -0400
commit69367aa7998d3817db1d4b101f36a6e25b1becf8 (patch)
treeeed4ec1b61f6f6cefe372c31f5a2ace42a513af6 /src/scriptapi_env.cpp
parent97f0bb03423b6d2e22058166b677e568c53d7567 (diff)
downloadminetest-69367aa7998d3817db1d4b101f36a6e25b1becf8.tar.gz
minetest-69367aa7998d3817db1d4b101f36a6e25b1becf8.tar.bz2
minetest-69367aa7998d3817db1d4b101f36a6e25b1becf8.zip
Add Dijkstra A* and A* without prefetching pathfind algorithms
Diffstat (limited to 'src/scriptapi_env.cpp')
-rw-r--r--src/scriptapi_env.cpp66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/scriptapi_env.cpp b/src/scriptapi_env.cpp
index 4e068e377..9bf7f0b55 100644
--- a/src/scriptapi_env.cpp
+++ b/src/scriptapi_env.cpp
@@ -26,6 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "content_sao.h"
#include "script.h"
#include "treegen.h"
+#include "pathfinder.h"
#include "util/pointedthing.h"
#include "scriptapi_types.h"
#include "scriptapi_noise.h"
@@ -647,6 +648,69 @@ int EnvRef::l_clear_objects(lua_State *L)
return 0;
}
+int EnvRef::l_line_of_sight(lua_State *L) {
+ float stepsize = 1.0;
+
+ //infostream<<"EnvRef::l_get_node()"<<std::endl;
+ EnvRef *o = checkobject(L, 1);
+ ServerEnvironment *env = o->m_env;
+ if(env == NULL) return 0;
+
+ // read position 1 from lua
+ v3f pos1 = checkFloatPos(L, 2);
+ // read position 2 from lua
+ v3f pos2 = checkFloatPos(L, 2);
+ //read step size from lua
+ if(lua_isnumber(L, 3))
+ stepsize = lua_tonumber(L, 3);
+
+ return (env->line_of_sight(pos1,pos2,stepsize));
+}
+
+int EnvRef::l_find_path(lua_State *L)
+{
+ EnvRef *o = checkobject(L, 1);
+ ServerEnvironment *env = o->m_env;
+
+ if(env == NULL) return 0;
+
+ v3s16 pos1 = read_v3s16(L, 2);
+ v3s16 pos2 = read_v3s16(L, 3);
+ unsigned int searchdistance = luaL_checkint(L, 4);
+ unsigned int max_jump = luaL_checkint(L, 5);
+ unsigned int max_drop = luaL_checkint(L, 6);
+ algorithm algo = A_PLAIN_NP;
+ if(! lua_isnil(L, 7)) {
+ std::string algorithm = luaL_checkstring(L,7);
+
+ if (algorithm == "A*")
+ algo = A_PLAIN;
+
+ if (algorithm == "Dijkstra")
+ algo = DIJKSTRA;
+ }
+
+ std::vector<v3s16> path =
+ get_Path(env,pos1,pos2,searchdistance,max_jump,max_drop,algo);
+
+ if (path.size() > 0)
+ {
+ lua_newtable(L);
+ int top = lua_gettop(L);
+ unsigned int index = 1;
+ for (std::vector<v3s16>::iterator i = path.begin(); i != path.end();i++)
+ {
+ lua_pushnumber(L,index);
+ push_v3s16(L, *i);
+ lua_settable(L, top);
+ index++;
+ }
+ return 1;
+ }
+
+ return 0;
+}
+
int EnvRef::l_spawn_tree(lua_State *L)
{
EnvRef *o = checkobject(L, 1);
@@ -780,6 +844,8 @@ const luaL_reg EnvRef::methods[] = {
luamethod(EnvRef, get_perlin_map),
luamethod(EnvRef, clear_objects),
luamethod(EnvRef, spawn_tree),
+ luamethod(EnvRef, line_of_sight),
+ luamethod(EnvRef, find_path),
{0,0}
};