+static void checkArea(v3s16 &minp, v3s16 &maxp)
+{
+ auto volume = VoxelArea(minp, maxp).getVolume();
+ // Volume limit equal to 8 default mapchunks, (80 * 2) ^ 3 = 4,096,000
+ if (volume > 4096000) {
+ throw LuaError("Area volume exceeds allowed value of 4096000");
+ }
+
+ // Clamp to map range to avoid problems
+#define CLAMP(arg) core::clamp(arg, (s16)-MAX_MAP_GENERATION_LIMIT, (s16)MAX_MAP_GENERATION_LIMIT)
+ minp = v3s16(CLAMP(minp.X), CLAMP(minp.Y), CLAMP(minp.Z));
+ maxp = v3s16(CLAMP(maxp.X), CLAMP(maxp.Y), CLAMP(maxp.Z));
+#undef CLAMP
+}
+
+// find_nodes_in_area(minp, maxp, nodenames, [grouped])
+int ModApiEnvMod::l_find_nodes_in_area(lua_State *L)
+{
+ GET_PLAIN_ENV_PTR;
+
+ v3s16 minp = read_v3s16(L, 1);
+ v3s16 maxp = read_v3s16(L, 2);
+ sortBoxVerticies(minp, maxp);
+
+ const NodeDefManager *ndef = env->getGameDef()->ndef();
+ Map &map = env->getMap();
+
+#ifndef SERVER
+ if (Client *client = getClient(L)) {
+ minp = client->CSMClampPos(minp);
+ maxp = client->CSMClampPos(maxp);
+ }
+#endif
+
+ checkArea(minp, maxp);
+
+ std::vector<content_t> filter;
+ collectNodeIds(L, 3, ndef, filter);
+
+ bool grouped = lua_isboolean(L, 4) && readParam<bool>(L, 4);
+
+ if (grouped) {
+ // create the table we will be returning
+ lua_createtable(L, 0, filter.size());
+ int base = lua_gettop(L);
+
+ // create one table for each filter
+ std::vector<u32> idx;
+ idx.resize(filter.size());
+ for (u32 i = 0; i < filter.size(); i++)
+ lua_newtable(L);
+
+ v3s16 p;
+ for (p.X = minp.X; p.X <= maxp.X; p.X++)
+ for (p.Y = minp.Y; p.Y <= maxp.Y; p.Y++)
+ for (p.Z = minp.Z; p.Z <= maxp.Z; p.Z++) {
+ content_t c = map.getNode(p).getContent();
+
+ auto it = std::find(filter.begin(), filter.end(), c);
+ if (it != filter.end()) {
+ // Calculate index of the table and append the position
+ u32 filt_index = it - filter.begin();
+ push_v3s16(L, p);
+ lua_rawseti(L, base + 1 + filt_index, ++idx[filt_index]);
+ }
+ }
+
+ // last filter table is at top of stack
+ u32 i = filter.size() - 1;
+ do {
+ if (idx[i] == 0) {
+ // No such node found -> drop the empty table
+ lua_pop(L, 1);
+ } else {
+ // This node was found -> put table into the return table
+ lua_setfield(L, base, ndef->get(filter[i]).name.c_str());
+ }
+ } while (i-- != 0);
+
+ assert(lua_gettop(L) == base);
+ return 1;
+ } else {
+ std::vector<u32> individual_count;
+ individual_count.resize(filter.size());
+
+ lua_newtable(L);
+ u32 i = 0;
+ v3s16 p;
+ for (p.X = minp.X; p.X <= maxp.X; p.X++)
+ for (p.Y = minp.Y; p.Y <= maxp.Y; p.Y++)
+ for (p.Z = minp.Z; p.Z <= maxp.Z; p.Z++) {
+ content_t c = env->getMap().getNode(p).getContent();
+
+ auto it = std::find(filter.begin(), filter.end(), c);
+ if (it != filter.end()) {
+ push_v3s16(L, p);
+ lua_rawseti(L, -2, ++i);
+
+ u32 filt_index = it - filter.begin();
+ individual_count[filt_index]++;
+ }
+ }
+
+ lua_createtable(L, 0, filter.size());
+ for (u32 i = 0; i < filter.size(); i++) {
+ lua_pushinteger(L, individual_count[i]);
+ lua_setfield(L, -2, ndef->get(filter[i]).name.c_str());
+ }
+ return 2;
+ }
+}
+