162 return town->xy != 0; |
162 return town->xy != 0; |
163 } |
163 } |
164 |
164 |
165 VARDEF uint _total_towns; |
165 VARDEF uint _total_towns; |
166 |
166 |
167 static inline TownID GetTownArraySize(void) |
167 static inline TownID GetMaxTownIndex(void) |
168 { |
168 { |
169 /* TODO - This isn't the real content of the function, but |
169 /* TODO - This isn't the real content of the function, but |
170 * with the new pool-system this will be replaced with one that |
170 * with the new pool-system this will be replaced with one that |
171 * _really_ returns the highest index + 1. Now it just returns |
171 * _really_ returns the highest index. Now it just returns |
172 * the next safe value we are sure about everything is below. |
172 * the next safe value we are sure about everything is below. |
173 */ |
173 */ |
|
174 return _total_towns - 1; |
|
175 } |
|
176 |
|
177 static inline uint GetNumTowns(void) |
|
178 { |
174 return _total_towns; |
179 return _total_towns; |
175 } |
180 } |
176 |
181 |
177 /** |
182 /** |
178 * Return a random valid town. |
183 * Return a random valid town. |
179 */ |
184 */ |
180 static inline Town *GetRandomTown(void) |
185 static inline Town *GetRandomTown(void) |
181 { |
186 { |
182 uint num = RandomRange(GetTownArraySize()); |
187 uint num = RandomRange(GetNumTowns()); |
183 uint index = 0; |
188 uint index = 0; |
184 |
189 |
185 while (num > 0) { |
190 while (num > 0) { |
186 num--; |
191 num--; |
187 |
192 |
188 index++; |
193 index++; |
189 /* Make sure we have a valid industry */ |
194 /* Make sure we have a valid industry */ |
190 while (GetTown(index) == NULL) { |
195 while (GetTown(index) == NULL) { |
191 index++; |
196 index++; |
192 if (index == GetTownArraySize()) index = 0; |
197 if (index > GetMaxTownIndex()) index = 0; |
193 } |
198 } |
194 } |
199 } |
195 |
200 |
196 return GetTown(index); |
201 return GetTown(index); |
197 } |
202 } |