1 /**
2  ** Objiter.h - Game objects iterator.
3  **
4  ** Written: 5/27/2002 - JSF
5  **/
6 
7 /*
8 Copyright (C) 2002  The Exult Team
9 
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation; either version 2
13 of the License, or (at your option) any later version.
14 
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23 */
24 
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28 #include "objiter.h"
29 #include "contain.h"
30 #include "gamewin.h"
31 #include "chunks.h"
32 
33 /*
34  *  Create to start after/before a given object within a chunk.
35  */
36 
37 template<class D>
D_Recursive_object_iterator(Game_object * start)38 D_Recursive_object_iterator<D>::D_Recursive_object_iterator(
39     Game_object *start      // Start here.
40 ) : child(nullptr), elems(start->get_outermost()->get_chunk()->get_objects()) {
41 	// Get what obj. is in (or itself).
42 	Game_object *owner = start->get_outermost();
43 	Game_object *obj;       // Find owner within its chunk.
44 	while ((obj = get_next()) != nullptr && obj != owner)
45 		;
46 	if (!obj)
47 		return;         // Bad.  It wasn't found.
48 	if (obj != start)       // Given object contained?
49 		// Look within for it.
50 		while ((obj = get_next()) != nullptr && obj != start)
51 			;
52 }
53 
54 /*
55  *  Get next game object, going down recursively into containers.
56  *
57  *  Output: Next in world, or nullptr if done.
58  */
59 
get_next()60 template<class D> Game_object *D_Recursive_object_iterator<D>::get_next(
61 ) {
62 	Game_object *obj;
63 	if (child) {        // Going through container?
64 		obj = child->get_next();
65 		if (obj)
66 			return obj;
67 		delete child;
68 		child = nullptr;      // Child done.
69 	}
70 	obj = elems.get_next();     // Get next from our list.
71 	if (!obj)
72 		return nullptr;       // All done.
73 	// Is it a container?
74 	Container_game_object *c = obj->as_container();
75 	if (c)              // Container?  Set to go through it.
76 		child = new D_Recursive_object_iterator<D>(
77 		    c->get_objects());
78 	return obj;
79 }
80