1
// Copyright (C) 2001, 2003 Michael Bartl
2
// Copyright (C) 2004 Ulf Lorenz
3
// Copyright (C) 2005, 2006 Andrea Paternesi
4
// Copyright (C) 2007, 2008 Ben Asselstine
5
// Copyright (C) 2008 Ole Laursen
7
// This program is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License as published by
9
// the Free Software Foundation; either version 2 of the License, or
10
// (at your option) any later version.
12
// This program is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU Library General Public License for more details.
17
// You should have received a copy of the GNU General Public License
18
// along with this program; if not, write to the Free Software
19
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22
#ifndef LOCATIONLIST_H
23
#define LOCATIONLIST_H
30
/** A list for object instances
32
* This class extends the stl lists by adding the functions getObjectAt()
33
* which return the object at position (x,y). Necessary for such things as
37
template<class T> class LocationList : public std::list<T>
44
//! Returns the object at position (x,y).
45
T* getObjectAt(int x, int y)
47
for (typename LocationList<T>::iterator it = this->begin(); it != this->end(); ++it)
49
Vector<int> p = (*it).getPos();
50
int size = (*it).getSize() - 1;
52
if (p.x >= (x - size) && p.x <= x && p.y >= (y - size) && p.y <= y)
60
//! Returns the object at position pos.
61
T* getObjectAt(const Vector<int>& pos)
63
return getObjectAt(pos.x, pos.y);
66
T* getNearestObject (const Vector<int>& pos, std::list<bool (*)(void*)> *filters)
69
typename LocationList<T>::iterator diffit;
70
for (typename LocationList<T>::iterator it = this->begin(); it != this->end(); ++it)
72
Vector<int> p = (*it).getPos();
73
int delta = abs(p.x - pos.x) + abs(p.y - pos.y);
76
std::list<bool (*)(void*)>::iterator fit = filters->begin();
77
bool filtered = false;
78
for (; fit != filters->end(); fit++)
80
if ((*fit)(&*it) == true)
91
if ((diff > delta) || (diff == -1))
97
if (diff == -1) return 0;
101
T* getNearestObject (const Vector<int>& pos)
103
return getNearestObject (pos, NULL);
106
T* getNearestObjectBefore (const Vector<int>& pos, int dist)
108
T *t = getNearestObject(pos);
111
if (t->getPos().x <= pos.x + dist && t->getPos().x >= pos.x - dist &&
112
t->getPos().y <= pos.y + dist && t->getPos().y >= pos.y - dist)
117
T* getNearestObjectAfter(const Vector<int>& pos, int dist,
118
std::list<bool (*)(void*)> *filters)
121
typename LocationList<T>::iterator diffit;
123
for (typename LocationList<T>::iterator it = this->begin(); it != this->end(); ++it)
127
std::list<bool (*)(void*)>::iterator fit = filters->begin();
128
bool filtered = false;
129
for (; fit != filters->end(); fit++)
131
if ((*fit)(&*it) == true)
142
Vector<int> p = (*it).getPos();
143
int delta = abs(p.x - pos.x);
144
if (delta < abs(p.y - pos.y))
145
delta = abs(p.y - pos.y);
147
if ((diff > delta && delta >= dist) || (diff == -1))
154
if (diff == -1) return 0;
158
T* getById(Uint32 id)
160
for (typename LocationList<T>::iterator i = this->begin(); i != this->end(); ++i)
161
if (i->getId() == id)
167
#endif // LOCATIONLIST_H