/* Compute the fraction of corridor tiles with litter, of the parcels owned by the given player. */ static int l_map_get_litter_fraction(lua_State *L) { THMap* pMap = luaT_testuserdata<THMap>(L); int owner = static_cast<int>(luaL_checkinteger(L, 2)); if (owner == 0) { lua_pushnumber(L, 0.0); // Outside has no litter. return 1; } double tile_count = 0; double litter_count = 0; for (int x = 0; x < pMap->getWidth(); x++) { for (int y = 0; y < pMap->getHeight(); y++) { const THMapNode* pNode = pMap->getNodeUnchecked(x, y); if (pNode->iParcelId == 0 || owner != pMap->getParcelOwner(pNode->iParcelId) || pNode->iRoomId != 0) { continue; } tile_count++; for(auto iter = pNode->objects.begin(); iter != pNode->objects.end(); iter++) { if(*iter == THObjectType::litter) { litter_count++; break; } } } } double fraction = (tile_count == 0) ? 0.0 : litter_count / tile_count; lua_pushnumber(L, fraction); return 1; }
static int l_map_get_parcel_owner(lua_State *L) { THMap* pMap = luaT_testuserdata<THMap>(L); lua_pushinteger(L, pMap->getParcelOwner(static_cast<int>(luaL_checkinteger(L, 2)))); return 1; }