@@ -118,24 +118,26 @@ void LevelLoader::LoadFile(std::mt19937& gen) {
118118 if (cur_file_ == level_file_paths_.end ()) {
119119 throw std::runtime_error (" No more files to load." );
120120 }
121+ cur_level_file_++;
121122 file_path = *cur_file_;
122123 cur_file_++;
123124 } else {
124- const size_t load_file_idx = SafeUniformInt (
125- static_cast < size_t >( 0 ), level_file_paths_.size () - 1 , gen);
126- file_path = level_file_paths_.at (load_file_idx );
125+ cur_level_file_ = SafeUniformInt (static_cast < size_t >( 0 ),
126+ level_file_paths_.size () - 1 , gen);
127+ file_path = level_file_paths_.at (cur_level_file_ );
127128 }
128129 std::ifstream file (file_path);
129130
130131 levels_.clear ();
132+ int cur_level_idx = 0 ;
131133 std::string line;
132134 while (std::getline (file, line)) {
133135 if (line.empty ()) {
134136 continue ;
135137 }
136138
137139 if (line.at (0 ) == ' #' ) {
138- SokobanLevel& cur_level = levels_. emplace_back (0 );
140+ SokobanLevel cur_level (0 );
139141 cur_level.reserve (10 * 10 ); // In practice most levels are this size
140142
141143 // Count contiguous '#' characters and use this as the box dimension
@@ -163,6 +165,8 @@ void LevelLoader::LoadFile(std::mt19937& gen) {
163165 << " x" << dim_room << std::endl;
164166 throw std::runtime_error (msg.str ());
165167 }
168+ levels_.emplace_back (
169+ std::make_pair (cur_level_idx++, std::move (cur_level)));
166170 }
167171 }
168172 if (!load_sequentially_) {
@@ -178,20 +182,21 @@ void LevelLoader::LoadFile(std::mt19937& gen) {
178182 std::cout << " ***Loaded " << levels_.size () << " levels from " << file_path
179183 << std::endl;
180184 if (verbose >= 2 ) {
181- PrintLevel (std::cout, levels_.at (0 ));
185+ PrintLevel (std::cout, levels_.at (0 ). second );
182186 std::cout << std::endl;
183- PrintLevel (std::cout, levels_.at (1 ));
187+ PrintLevel (std::cout, levels_.at (1 ). second );
184188 std::cout << std::endl;
185189 }
186190 }
187191}
188192
189- std::vector<SokobanLevel>::iterator LevelLoader::GetLevel (std::mt19937& gen) {
193+ TaggedSokobanLevel LevelLoader::GetLevel (std::mt19937& gen) {
190194 if (n_levels_to_load_ > 0 && levels_loaded_ >= n_levels_to_load_) {
191195 // std::cerr << "Warning: All levels loaded. Looping around now." <<
192196 // std::endl;
193197 levels_loaded_ = 0 ;
194198 cur_file_ = level_file_paths_.begin ();
199+ cur_level_file_ = -1 ;
195200 LoadFile (gen);
196201 // re-start from the `env_id`th level, like we do in the constructor.
197202 cur_level_ = env_id_;
@@ -206,7 +211,9 @@ std::vector<SokobanLevel>::iterator LevelLoader::GetLevel(std::mt19937& gen) {
206211 auto out = levels_.begin () + cur_level_;
207212 cur_level_ += num_envs_;
208213 levels_loaded_++;
209- return out;
214+
215+ TaggedSokobanLevel tagged_level{cur_level_file_, out->first , out->second };
216+ return tagged_level;
210217}
211218
212219} // namespace sokoban
0 commit comments