~ubuntu-branches/ubuntu/raring/lordsawar/raring

« back to all changes in this revision

Viewing changes to src/LocationList.h

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese, Barry deFreese, Gonéri Le Bouder
  • Date: 2008-06-17 11:15:26 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080617111526-yjyvu9df50zmpdo0
Tags: 0.0.9-1
[ Barry deFreese ]
* New upstream release.
  + Fixes gcc-4.3 builds so drop ftbfs_gcc-4.3_fix.diff.
  + Add new build-dependency for libgnet-dev.
* Add simple man page for new lordsawar-tile-editor.
* Add desktop file for lordsawar-tile-editor.
* Remove French translation on install.

[ Gonéri Le Bouder ]
* bump Debian Policy to 3.8.0. No change needed.
* fix wording in the 0.0.8-3 entry of the Debian changelog

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
6
//
 
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.
 
11
//
 
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.
 
16
//
 
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 
 
20
//  02110-1301, USA.
 
21
 
 
22
#ifndef LOCATIONLIST_H
 
23
#define LOCATIONLIST_H
 
24
 
 
25
#include <list>
 
26
#include "ruin.h"
 
27
#include "temple.h"
 
28
#include "city.h"
 
29
 
 
30
/** A list for object instances
 
31
  * 
 
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
 
34
  * the city list.
 
35
  */
 
36
 
 
37
template<class T> class LocationList : public std::list<T>
 
38
{
 
39
 public:
 
40
  
 
41
  LocationList(){};  
 
42
  ~LocationList() {};
 
43
 
 
44
  //! Returns the object at position (x,y).  
 
45
  T* getObjectAt(int x, int y) 
 
46
    {
 
47
      for (typename LocationList<T>::iterator it = this->begin(); it != this->end(); ++it)
 
48
        {
 
49
          Vector<int> p = (*it).getPos();
 
50
          int size = (*it).getSize() - 1;
 
51
 
 
52
          if (p.x >= (x - size) && p.x <= x && p.y >= (y - size) && p.y <= y)
 
53
            {
 
54
              return &(*it);
 
55
            }
 
56
        }
 
57
      return 0;
 
58
    }
 
59
 
 
60
  //! Returns the object at position pos.  
 
61
  T* getObjectAt(const Vector<int>& pos) 
 
62
    {
 
63
      return getObjectAt(pos.x, pos.y);
 
64
    }
 
65
 
 
66
  T* getNearestObject (const Vector<int>& pos, std::list<bool (*)(void*)> *filters)
 
67
    {
 
68
      int diff = -1;
 
69
      typename LocationList<T>::iterator diffit;
 
70
      for (typename LocationList<T>::iterator it = this->begin(); it != this->end(); ++it)
 
71
        {
 
72
          Vector<int> p = (*it).getPos();
 
73
          int delta = abs(p.x - pos.x) + abs(p.y - pos.y);
 
74
          if (filters)
 
75
            {
 
76
              std::list<bool (*)(void*)>::iterator fit = filters->begin();
 
77
              bool filtered = false;
 
78
              for (; fit != filters->end(); fit++)
 
79
                {
 
80
                  if ((*fit)(&*it) == true)
 
81
                    {
 
82
                      filtered = true;
 
83
                      break;
 
84
                    }
 
85
                    
 
86
                }
 
87
              if (filtered)
 
88
                continue;
 
89
            }
 
90
 
 
91
          if ((diff > delta) || (diff == -1))
 
92
            {
 
93
              diff = delta;
 
94
              diffit = it;
 
95
            }
 
96
        }
 
97
      if (diff == -1) return 0;
 
98
      return &(*diffit);
 
99
    }
 
100
 
 
101
  T* getNearestObject (const Vector<int>& pos)
 
102
    {
 
103
      return getNearestObject (pos, NULL);
 
104
    }
 
105
 
 
106
  T* getNearestObjectBefore (const Vector<int>& pos, int dist)
 
107
    {
 
108
      T *t = getNearestObject(pos);
 
109
      if (!t)
 
110
        return NULL;
 
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)
 
113
        return t;
 
114
      return NULL;
 
115
    }
 
116
 
 
117
  T* getNearestObjectAfter(const Vector<int>& pos, int dist, 
 
118
                           std::list<bool (*)(void*)> *filters)
 
119
    {
 
120
      int diff = -1;
 
121
      typename LocationList<T>::iterator diffit;
 
122
 
 
123
      for (typename LocationList<T>::iterator it = this->begin(); it != this->end(); ++it)
 
124
        {
 
125
          if (filters)
 
126
            {
 
127
              std::list<bool (*)(void*)>::iterator fit = filters->begin();
 
128
              bool filtered = false;
 
129
              for (; fit != filters->end(); fit++)
 
130
                {
 
131
                  if ((*fit)(&*it) == true)
 
132
                    {
 
133
                      filtered = true;
 
134
                      break;
 
135
                    }
 
136
                    
 
137
                }
 
138
              if (filtered)
 
139
                continue;
 
140
            }
 
141
          
 
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);
 
146
          
 
147
            if ((diff > delta && delta >= dist) || (diff == -1))
 
148
              {
 
149
                diff = delta;
 
150
                diffit = it;
 
151
              }
 
152
        }
 
153
    
 
154
      if (diff == -1) return 0;
 
155
      return &(*diffit);
 
156
    }
 
157
 
 
158
  T* getById(Uint32 id)
 
159
  {
 
160
    for (typename LocationList<T>::iterator i = this->begin(); i != this->end(); ++i)
 
161
      if (i->getId() == id)
 
162
        return &(*i);
 
163
    return 0;
 
164
  }
 
165
};
 
166
 
 
167
#endif // LOCATIONLIST_H
 
168
 
 
169
// End of file