1 #include "AppHdr.h"
2 
3 #include "act-iter.h"
4 
5 #include "env.h"
6 #include "losglobal.h"
7 
actor_near_iterator(coord_def c,los_type los)8 actor_near_iterator::actor_near_iterator(coord_def c, los_type los)
9     : center(c), _los(los), viewer(nullptr), i(-1)
10 {
11     if (!valid(&you))
12         advance();
13 }
14 
actor_near_iterator(const actor * a,los_type los)15 actor_near_iterator::actor_near_iterator(const actor* a, los_type los)
16     : center(a->pos()), _los(los), viewer(a), i(-1)
17 {
18     if (!valid(&you))
19         advance();
20 }
21 
operator bool() const22 actor_near_iterator::operator bool() const
23 {
24     return valid(**this);
25 }
26 
operator *() const27 actor* actor_near_iterator::operator*() const
28 {
29     if (i == -1)
30         return &you;
31     else if (i < MAX_MONSTERS)
32         return &env.mons[i];
33     else
34         return nullptr;
35 }
36 
operator ->() const37 actor* actor_near_iterator::operator->() const
38 {
39     return **this;
40 }
41 
operator ++()42 actor_near_iterator& actor_near_iterator::operator++()
43 {
44     advance();
45     return *this;
46 }
47 
operator ++(int)48 actor_near_iterator actor_near_iterator::operator++(int)
49 {
50     actor_near_iterator copy = *this;
51     ++(*this);
52     return copy;
53 }
54 
valid(const actor * a) const55 bool actor_near_iterator::valid(const actor* a) const
56 {
57     if (!a || !a->alive())
58         return false;
59     if (viewer && !a->visible_to(viewer))
60         return false;
61     return cell_see_cell(center, a->pos(), _los);
62 }
63 
advance()64 void actor_near_iterator::advance()
65 {
66     do
67          if (++i >= MAX_MONSTERS)
68              return;
69     while (!valid(**this));
70 }
71 
72 //////////////////////////////////////////////////////////////////////////
73 
monster_near_iterator(coord_def c,los_type los)74 monster_near_iterator::monster_near_iterator(coord_def c, los_type los)
75     : center(c), _los(los), viewer(nullptr), i(0)
76 {
77     if (!valid(&env.mons[0]))
78         advance();
79     begin_point = i;
80 }
81 
monster_near_iterator(const actor * a,los_type los)82 monster_near_iterator::monster_near_iterator(const actor *a, los_type los)
83     : center(a->pos()), _los(los), viewer(a), i(0)
84 {
85     if (!valid(&env.mons[0]))
86         advance();
87     begin_point = i;
88 }
89 
operator bool() const90 monster_near_iterator::operator bool() const
91 {
92     return valid(**this);
93 }
94 
operator *() const95 monster* monster_near_iterator::operator*() const
96 {
97     if (i < MAX_MONSTERS)
98         return &env.mons[i];
99     else
100         return nullptr;
101 }
102 
operator ->() const103 monster* monster_near_iterator::operator->() const
104 {
105     return **this;
106 }
107 
operator ++()108 monster_near_iterator& monster_near_iterator::operator++()
109 {
110     advance();
111     return *this;
112 }
113 
operator ==(const monster_near_iterator & other)114 bool monster_near_iterator::operator==(const monster_near_iterator &other)
115 {
116     return i == other.i;
117 }
118 
operator !=(const monster_near_iterator & other)119 bool monster_near_iterator::operator!=(const monster_near_iterator &other)
120 {
121     return !(operator==(other));
122 }
123 
operator ++(int)124 monster_near_iterator monster_near_iterator::operator++(int)
125 {
126     monster_near_iterator copy = *this;
127     ++(*this);
128     return copy;
129 }
130 
begin()131 monster_near_iterator monster_near_iterator::begin()
132 {
133     monster_near_iterator copy = *this;
134     copy.i = begin_point;
135     return copy;
136 }
137 
end()138 monster_near_iterator monster_near_iterator::end()
139 {
140     monster_near_iterator copy = *this;
141     copy.i = MAX_MONSTERS;
142     return copy;
143 }
144 
valid(const monster * a) const145 bool monster_near_iterator::valid(const monster* a) const
146 {
147     if (!a || !a->alive())
148         return false;
149     if (viewer && !a->visible_to(viewer))
150         return false;
151     return cell_see_cell(center, a->pos(), _los);
152 }
153 
advance()154 void monster_near_iterator::advance()
155 {
156     do
157          if (++i >= MAX_MONSTERS)
158              return;
159     while (!valid(**this));
160 }
161 
162 //////////////////////////////////////////////////////////////////////////
163 
monster_iterator()164 monster_iterator::monster_iterator()
165     : i(0)
166 {
167     while (i < MAX_MONSTERS && !env.mons[i].alive())
168         i++;
169 }
170 
operator bool() const171 monster_iterator::operator bool() const
172 {
173     return i < MAX_MONSTERS && (*this)->alive();
174 }
175 
operator *() const176 monster* monster_iterator::operator*() const
177 {
178     if (i < MAX_MONSTERS)
179         return &env.mons[i];
180     else
181         return nullptr;
182 }
183 
operator ->() const184 monster* monster_iterator::operator->() const
185 {
186     return **this;
187 }
188 
operator ++()189 monster_iterator& monster_iterator::operator++()
190 {
191     while (++i < MAX_MONSTERS)
192         if (env.mons[i].alive())
193             break;
194     return *this;
195 }
196 
operator ++(int)197 monster_iterator monster_iterator::operator++(int)
198 {
199     monster_iterator copy = *this;
200     ++(*this);
201     return copy;
202 }
203 
advance()204 void monster_iterator::advance()
205 {
206     do
207          if (++i >= MAX_MONSTERS)
208              return;
209     while (!(*this)->alive());
210 }
211 
operator ()(const actor * a,const actor * b)212 bool far_to_near_sorter::operator()(const actor* a, const actor* b)
213 {
214     return a->pos().distance_from(pos) > b->pos().distance_from(pos);
215 }
216 
operator ()(const actor * a,const actor * b)217 bool near_to_far_sorter::operator()(const actor* a, const actor* b)
218 {
219     return a->pos().distance_from(pos) < b->pos().distance_from(pos);
220 }
221