summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPerttu Ahola <celeron55@gmail.com>2011-12-28 18:18:08 +0200
committerPerttu Ahola <celeron55@gmail.com>2011-12-28 18:18:08 +0200
commit1c15f53318d49ccd148ec42b0a4345c4a8cd06bf (patch)
tree008ffa85e6b118dbe0a16da2f52e72e2e7b41cdf /src
parent7937813c98255736c6847fe2d1302e0c6b309b04 (diff)
downloadminetest-1c15f53318d49ccd148ec42b0a4345c4a8cd06bf.tar.gz
minetest-1c15f53318d49ccd148ec42b0a4345c4a8cd06bf.tar.bz2
minetest-1c15f53318d49ccd148ec42b0a4345c4a8cd06bf.zip
Add EnvRef:get_objects_inside_radius(pos, radius)
Diffstat (limited to 'src')
-rw-r--r--src/environment.cpp17
-rw-r--r--src/environment.h12
-rw-r--r--src/scriptapi.cpp31
3 files changed, 57 insertions, 3 deletions
diff --git a/src/environment.cpp b/src/environment.cpp
index aa2b45f8f..88f1527fc 100644
--- a/src/environment.cpp
+++ b/src/environment.cpp
@@ -752,6 +752,23 @@ void ServerEnvironment::addActiveBlockModifier(ActiveBlockModifier *abm)
m_abms.push_back(ABMWithState(abm));
}
+std::set<u16> ServerEnvironment::getObjectsInsideRadius(v3f pos, float radius)
+{
+ std::set<u16> objects;
+ for(core::map<u16, ServerActiveObject*>::Iterator
+ i = m_active_objects.getIterator();
+ i.atEnd()==false; i++)
+ {
+ ServerActiveObject* obj = i.getNode()->getValue();
+ u16 id = i.getNode()->getKey();
+ v3f objectpos = obj->getBasePosition();
+ if(objectpos.getDistanceFrom(pos) > radius)
+ continue;
+ objects.insert(id);
+ }
+ return objects;
+}
+
void ServerEnvironment::clearAllObjects()
{
infostream<<"ServerEnvironment::clearAllObjects(): "
diff --git a/src/environment.h b/src/environment.h
index e14a9c485..3ebbee910 100644
--- a/src/environment.h
+++ b/src/environment.h
@@ -261,18 +261,24 @@ public:
void activateBlock(MapBlock *block, u32 additional_dtime=0);
/*
- ActiveBlockModifiers (TODO)
+ ActiveBlockModifiers
-------------------------------------------
- NOTE: Not used currently (TODO: Use or remove)
*/
void addActiveBlockModifier(ActiveBlockModifier *abm);
- /* Other stuff */
+ /*
+ Other stuff
+ -------------------------------------------
+ */
+
+ // Find all active objects inside a radius around a point
+ std::set<u16> getObjectsInsideRadius(v3f pos, float radius);
// Clear all objects, loading and going through every MapBlock
void clearAllObjects();
+ // This makes stuff happen
void step(f32 dtime);
private:
diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp
index 23336e32d..1a50e1e34 100644
--- a/src/scriptapi.cpp
+++ b/src/scriptapi.cpp
@@ -2592,6 +2592,36 @@ private:
return 1;
}
+ // EnvRef:get_objects_inside_radius(pos, radius)
+ static int l_get_objects_inside_radius(lua_State *L)
+ {
+ // Get the table insert function
+ lua_getglobal(L, "table");
+ lua_getfield(L, -1, "insert");
+ int table_insert = lua_gettop(L);
+ // Get environemnt
+ EnvRef *o = checkobject(L, 1);
+ ServerEnvironment *env = o->m_env;
+ if(env == NULL) return 0;
+ // Do it
+ v3f pos = readFloatPos(L, 2);
+ float radius = luaL_checknumber(L, 3) * BS;
+ std::set<u16> ids = env->getObjectsInsideRadius(pos, radius);
+ lua_newtable(L);
+ int table = lua_gettop(L);
+ for(std::set<u16>::const_iterator
+ i = ids.begin(); i != ids.end(); i++){
+ ServerActiveObject *obj = env->getActiveObject(*i);
+ // Insert object reference into table
+ lua_pushvalue(L, table_insert);
+ lua_pushvalue(L, table);
+ objectref_get_or_create(L, obj);
+ if(lua_pcall(L, 2, 0, 0))
+ script_error(L, "error: %s", lua_tostring(L, -1));
+ }
+ return 1;
+ }
+
static int gc_object(lua_State *L) {
EnvRef *o = *(EnvRef **)(lua_touserdata(L, 1));
delete o;
@@ -2668,6 +2698,7 @@ const luaL_reg EnvRef::methods[] = {
method(EnvRef, add_firefly),
method(EnvRef, get_meta),
method(EnvRef, get_player_by_name),
+ method(EnvRef, get_objects_inside_radius),
{0,0}
};