Merge lp:~widelands-dev/widelands/ai_remove_iterators into lp:widelands

Proposed by GunChleoc
Status: Work in progress
Proposed branch: lp:~widelands-dev/widelands/ai_remove_iterators
Merge into: lp:widelands
Prerequisite: lp:~widelands-dev/widelands/seafaring-ai
Diff against target: 1037 lines (+217/-239)
2 files modified
src/ai/ai_help_structs.h (+17/-0)
src/ai/defaultai.cc (+200/-239)
To merge this branch: bzr merge lp:~widelands-dev/widelands/ai_remove_iterators
Reviewer Review Type Date Requested Status
Widelands Developers Pending
Review via email: mp+243894@code.launchpad.net

Description of the change

This isn't quite ready yet, but I wanted to get some feedback before I finish.

I have replaced all easy iterator loops with ranged-based for loops. The remaining ones have erase operations in them for objects that don't have a comparator yet. In order for them to work, I will have to add them to the ai_help_structs.h like this: http://bazaar.launchpad.net/~widelands-dev/widelands/ai_remove_iterators/revision/7241

I think this makes the AI code more readable, but if you think this is overkill, I will leave it.

To post a comment you must log in.
Revision history for this message
SirVer (sirver) wrote :

I think this is a definitive improvement to the readability. I did not carefully review this yet though, I think the seafaring branch should be merged first.

Revision history for this message
GunChleoc (gunchleoc) wrote :

Definitely, I just wanted an opinion before I continue :)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/ai/ai_help_structs.h'
2--- src/ai/ai_help_structs.h 2014-12-16 11:37:49 +0000
3+++ src/ai/ai_help_structs.h 2014-12-16 11:37:49 +0000
4@@ -189,6 +189,14 @@
5 Widelands::FCoords coords;
6 int32_t blocked_until_;
7
8+ bool operator== (const BlockedField& other) const {
9+ return coords == other.coords;
10+ }
11+
12+ bool operator!= (const BlockedField& other) const {
13+ return coords != other.coords;
14+ }
15+
16 BlockedField(Widelands::FCoords c, int32_t until) : coords(c), blocked_until_(until) {
17 }
18 };
19@@ -387,6 +395,15 @@
20 };
21
22 struct ShipObserver {
23+
24+ bool operator== (const ShipObserver& other) const {
25+ return ship->serial() == other.ship->serial();
26+ }
27+
28+ bool operator!= (const ShipObserver& other) const {
29+ return ship->serial() != other.ship->serial();
30+ }
31+
32 Widelands::Ship* ship;
33 Widelands::Coords expedition_start_point_;
34 std::unordered_set<uint32_t> visited_spots_;
35
36=== modified file 'src/ai/defaultai.cc'
37--- src/ai/defaultai.cc 2014-12-16 11:37:49 +0000
38+++ src/ai/defaultai.cc 2014-12-16 11:37:49 +0000
39@@ -181,19 +181,17 @@
40 }
41 break;
42
43- case NoteShipMessage::Message::kLost:
44- for (std::list<ShipObserver>::iterator i = allships.begin(); i != allships.end(); ++i) {
45- if (i->ship == note.ship) {
46- allships.erase(i);
47+ case NoteShipMessage::Message::kLost: {
48+ for (const ShipObserver& ship_observer : allships) {
49+ if (ship_observer.ship == note.ship) {
50+ allships.remove(ship_observer);
51 break;
52- }
53- }
54- break;
55-
56- case NoteShipMessage::Message::kWaitingForCommand:
57- for (std::list<ShipObserver>::iterator i = allships.begin(); i != allships.end(); ++i) {
58- if (i->ship == note.ship) {
59- i->waiting_for_command_ = true;
60+ }
61+ }
62+ case NoteShipMessage::Message::kWaitingForCommand:
63+ for (ShipObserver& ship_observer : allships) {
64+ if (ship_observer.ship == note.ship) {
65+ ship_observer.waiting_for_command_ = true;
66 break;
67 }
68 }
69@@ -201,7 +199,8 @@
70 default:
71 ;
72 }
73- });
74+ }
75+ });
76 }
77
78 DefaultAI::~DefaultAI() {
79@@ -1032,10 +1031,8 @@
80 for (int32_t i = 0; i < 4; ++i)
81 spots_avail.at(i) = 0;
82
83- for (std::list<BuildableField*>::iterator i = buildable_fields.begin();
84- i != buildable_fields.end();
85- ++i)
86- ++spots_avail.at((*i)->coords.field->nodecaps() & BUILDCAPS_SIZEMASK);
87+ for (const BuildableField* buildable_field : buildable_fields)
88+ ++spots_avail.at(buildable_field->coords.field->nodecaps() & BUILDCAPS_SIZEMASK);
89
90 spots_ = spots_avail.at(BUILDCAPS_SMALL);
91 spots_ += spots_avail.at(BUILDCAPS_MEDIUM);
92@@ -1125,12 +1122,11 @@
93 Coords proposed_coords;
94
95 // Remove outdated fields from blocker list
96- for (std::list<BlockedField>::iterator i = blocked_fields.begin(); i != blocked_fields.end();)
97- if (i->blocked_until_ < game().get_gametime()) {
98- i = blocked_fields.erase(i);
99- } else {
100- ++i;
101+ for (const BlockedField& blocked_field : blocked_fields) {
102+ if (blocked_field.blocked_until_ < game().get_gametime()) {
103+ blocked_fields.remove(blocked_field);
104 }
105+ }
106
107 // testing big military buildings, whether critical construction
108 // material is available (at least in amount of
109@@ -1169,21 +1165,17 @@
110 int16_t max_needed_preciousness = 0; // preciousness_ of most precious NEEDED output
111
112 // first scan all buildable fields for regular buildings
113- for (std::list<BuildableField*>::iterator i = buildable_fields.begin();
114- i != buildable_fields.end();
115- ++i) {
116- BuildableField* const bf = *i;
117+ for (const BuildableField* buildable_field : buildable_fields) {
118
119- if (bf->next_update_due_ < gametime - 8000) {
120+ if (buildable_field->next_update_due_ < gametime - 8000) {
121 continue;
122 }
123
124 // Continue if field is blocked at the moment
125 field_blocked = false;
126
127- for (std::list<BlockedField>::iterator j = blocked_fields.begin(); j != blocked_fields.end();
128- ++j) {
129- if (j->coords == bf->coords) {
130+ for (const BlockedField& blocked_field : blocked_fields) {
131+ if (blocked_field.coords == buildable_field->coords) {
132 field_blocked = true;
133 }
134 }
135@@ -1194,7 +1186,7 @@
136 }
137
138 assert(player_);
139- int32_t const maxsize = player_->get_buildcaps(bf->coords) & BUILDCAPS_SIZEMASK;
140+ int32_t const maxsize = player_->get_buildcaps(buildable_field->coords) & BUILDCAPS_SIZEMASK;
141
142 // For every field test all buildings
143 for (uint32_t j = 0; j < buildings_.size(); ++j) {
144@@ -1215,7 +1207,7 @@
145
146 // testing for reserved ports
147 if (!bo.is_port_) {
148- if (port_reserved_coords.count(coords_hash(bf->coords)) > 0) {
149+ if (port_reserved_coords.count(coords_hash(buildable_field->coords)) > 0) {
150 continue;
151 }
152 }
153@@ -1251,13 +1243,13 @@
154 if (bo.type == BuildingObserver::PRODUCTIONSITE) {
155
156 // exclude spots on border
157- if (bf->near_border_ && !bo.need_trees_ && !bo.need_stones_ && !bo.is_fisher_) {
158+ if (buildable_field->near_border_ && !bo.need_trees_ && !bo.need_stones_ && !bo.is_fisher_) {
159 continue;
160 }
161
162 // this can be only a well (as by now)
163 if (bo.mines_water_) {
164- if (bf->ground_water_ < 2) {
165+ if (buildable_field->ground_water_ < 2) {
166 continue;
167 }
168
169@@ -1283,8 +1275,8 @@
170 if (bo.stocklevel_ > 50 + productionsites.size() * 5) {
171 continue;
172 }
173- prio += bf->ground_water_ - 2;
174- prio = recalc_with_border_range(*bf, prio);
175+ prio += buildable_field->ground_water_ - 2;
176+ prio = recalc_with_border_range(*buildable_field, prio);
177
178 } else if (bo.need_trees_) { // LUMBERJACS
179
180@@ -1292,14 +1284,14 @@
181 3 + static_cast<int32_t>(mines_.size() + productionsites.size()) / 15;
182
183 if (bo.total_count() == 0) {
184- prio = 500 + bf->trees_nearby_;
185+ prio = 500 + buildable_field->trees_nearby_;
186 }
187
188 else if (bo.total_count() == 1) {
189- prio = 400 + bf->trees_nearby_;
190+ prio = 400 + buildable_field->trees_nearby_;
191 }
192
193- else if (bf->trees_nearby_ < 2) {
194+ else if (buildable_field->trees_nearby_ < 2) {
195 continue;
196 }
197
198@@ -1311,15 +1303,15 @@
199 prio = 0;
200 }
201
202- if (bf->producers_nearby_.at(bo.outputs_.at(0)) > 1) {
203+ if (buildable_field->producers_nearby_.at(bo.outputs_.at(0)) > 1) {
204 continue;
205 }
206
207- prio += 2 * bf->trees_nearby_ - 10 -
208- bf->producers_nearby_.at(bo.outputs_.at(0)) * 5 -
209+ prio += 2 * buildable_field->trees_nearby_ - 10 -
210+ buildable_field->producers_nearby_.at(bo.outputs_.at(0)) * 5 -
211 new_buildings_stop_ * 15;
212
213- if (bf->near_border_) {
214+ if (buildable_field->near_border_) {
215 prio = prio / 2;
216 }
217 }
218@@ -1332,7 +1324,7 @@
219 if (bo.cnt_under_construction_ > 0) {
220 continue;
221 }
222- prio = bf->stones_nearby_;
223+ prio = buildable_field->stones_nearby_;
224
225 if (prio <= 0) {
226 continue;
227@@ -1352,14 +1344,14 @@
228 }
229
230 // to prevent to many quaries on one spot
231- prio = prio - 50 * bf->producers_nearby_.at(bo.outputs_.at(0));
232+ prio = prio - 50 * buildable_field->producers_nearby_.at(bo.outputs_.at(0));
233
234- if (bf->near_border_) {
235+ if (buildable_field->near_border_) {
236 prio = prio / 2;
237 }
238
239 } else if (bo.is_hunter_) {
240- if (bf->critters_nearby_ < 5) {
241+ if (buildable_field->critters_nearby_ < 5) {
242 continue;
243 }
244
245@@ -1368,7 +1360,7 @@
246 }
247
248 prio +=
249- (bf->critters_nearby_ * 2) - 8 - 5 * bf->producers_nearby_.at(bo.outputs_.at(0));
250+ (buildable_field->critters_nearby_ * 2) - 8 - 5 * buildable_field->producers_nearby_.at(bo.outputs_.at(0));
251
252 } else if (bo.is_fisher_) { // fisher
253
254@@ -1381,7 +1373,7 @@
255 continue;
256 }
257
258- if (bf->water_nearby_ < 2) {
259+ if (buildable_field->water_nearby_ < 2) {
260 continue;
261 }
262
263@@ -1400,11 +1392,11 @@
264 continue;
265 }
266
267- if (bf->producers_nearby_.at(bo.outputs_.at(0)) >= 1) {
268+ if (buildable_field->producers_nearby_.at(bo.outputs_.at(0)) >= 1) {
269 continue;
270 }
271
272- prio = bf->fish_nearby_ - new_buildings_stop_ * 15 * bo.total_count();
273+ prio = buildable_field->fish_nearby_ - new_buildings_stop_ * 15 * bo.total_count();
274
275 } else if (bo.production_hint_ >= 0) {
276 // first setting targets (needed also for dismantling)
277@@ -1423,7 +1415,7 @@
278 if (bo.plants_trees_) { // RANGERS
279
280 // if there are too many trees nearby
281- if (bf->trees_nearby_ > 25 && bo.total_count() >= 1) {
282+ if (buildable_field->trees_nearby_ > 25 && bo.total_count() >= 1) {
283 continue;
284 }
285
286@@ -1451,12 +1443,12 @@
287 }
288
289 // considering near trees and producers
290- prio += (30 - bf->trees_nearby_) * 2 +
291- bf->producers_nearby_.at(bo.production_hint_) * 5 -
292+ prio += (30 - buildable_field->trees_nearby_) * 2 +
293+ buildable_field->producers_nearby_.at(bo.production_hint_) * 5 -
294 new_buildings_stop_ * 15;
295
296 // considering space consumers nearby
297- prio -= bf->space_consumers_nearby_ * 5;
298+ prio -= buildable_field->space_consumers_nearby_ * 5;
299
300 } else { // FISH BREEDERS and GAME KEEPERS
301 if (new_buildings_stop_ && bo.total_count() > 0) {
302@@ -1464,11 +1456,11 @@
303 }
304
305 // especially for fish breeders
306- if (bo.need_water_ && bf->water_nearby_ < 2) {
307+ if (bo.need_water_ && buildable_field->water_nearby_ < 2) {
308 continue;
309 }
310 if (bo.need_water_) {
311- prio += bf->water_nearby_ / 5;
312+ prio += buildable_field->water_nearby_ / 5;
313 }
314
315 if (bo.total_count() > bo.cnt_target_) {
316@@ -1485,14 +1477,14 @@
317 }
318
319 if (bo.total_count() == 0 && gametime > 45 * 1000) {
320- prio += 100 + bf->producers_nearby_.at(bo.production_hint_) * 10;
321- } else if (bf->producers_nearby_.at(bo.production_hint_) == 0) {
322+ prio += 100 + buildable_field->producers_nearby_.at(bo.production_hint_) * 10;
323+ } else if (buildable_field->producers_nearby_.at(bo.production_hint_) == 0) {
324 continue;
325 } else {
326- prio += bf->producers_nearby_.at(bo.production_hint_) * 10;
327+ prio += buildable_field->producers_nearby_.at(bo.production_hint_) * 10;
328 }
329
330- if (bf->enemy_nearby_) {
331+ if (buildable_field->enemy_nearby_) {
332 prio -= 10;
333 }
334 }
335@@ -1536,34 +1528,34 @@
336 prio += max_needed_preciousness + kDefaultPrioBoost;
337
338 if (bo.space_consumer_) { // need to consider trees nearby
339- prio += 20 - (bf->trees_nearby_ / 3);
340+ prio += 20 - (buildable_field->trees_nearby_ / 3);
341 }
342
343 // we attempt to cluster space consumers together
344 if (bo.space_consumer_) { // need to consider trees nearby
345- prio += bf->space_consumers_nearby_ * 2;
346+ prio += buildable_field->space_consumers_nearby_ * 2;
347 }
348
349- if (bo.space_consumer_ && !bf->water_nearby_) { // not close to water
350+ if (bo.space_consumer_ && !buildable_field->water_nearby_) { // not close to water
351 prio += 1;
352 }
353
354 if (bo.space_consumer_ &&
355- !bf->unowned_mines_pots_nearby_) { // not close to mountains
356+ !buildable_field->unowned_mines_pots_nearby_) { // not close to mountains
357 prio += 1;
358 }
359
360 if (!bo.space_consumer_) {
361- prio -= bf->producers_nearby_.at(bo.outputs_.at(0)) * 20;
362+ prio -= buildable_field->producers_nearby_.at(bo.outputs_.at(0)) * 20;
363 } // leave some free space between them
364
365- prio -= bf->space_consumers_nearby_ * 3;
366+ prio -= buildable_field->space_consumers_nearby_ * 3;
367 }
368
369 else if (bo.is_shipyard_) {
370 // for now AI builds only one shipyard
371- if (bf->water_nearby_ > 3 && bo.total_count() == 0 && seafaring_economy) {
372- prio += kDefaultPrioBoost + productionsites.size() * 5 + bf->water_nearby_;
373+ if (buildable_field->water_nearby_ > 3 && bo.total_count() == 0 && seafaring_economy) {
374+ prio += kDefaultPrioBoost + productionsites.size() * 5 + buildable_field->water_nearby_;
375 }
376
377 } else if (!bo.inputs_.empty()) {
378@@ -1584,7 +1576,7 @@
379 consumers_nearby_count = 0;
380
381 for (size_t k = 0; k < bo.outputs_.size(); ++k)
382- consumers_nearby_count += bf->consumers_nearby_.at(bo.outputs_.at(k));
383+ consumers_nearby_count += buildable_field->consumers_nearby_.at(bo.outputs_.at(k));
384
385 if (consumers_nearby_count > 0) {
386 prio += 1;
387@@ -1595,11 +1587,11 @@
388
389 // we allow 1 exemption from big buildings prohibition
390 if (bo.build_material_shortage_ &&
391- (bo.cnt_under_construction_ > 0 || !(bf->enemy_nearby_))) {
392+ (bo.cnt_under_construction_ > 0 || !(buildable_field->enemy_nearby_))) {
393 continue;
394 }
395
396- if (!bf->unowned_land_nearby_) {
397+ if (!buildable_field->unowned_land_nearby_) {
398 continue;
399 }
400
401@@ -1611,21 +1603,21 @@
402 continue;
403 }
404
405- if (expansion_mode == kDefenseOnly && !bf->enemy_nearby_) {
406+ if (expansion_mode == kDefenseOnly && !buildable_field->enemy_nearby_) {
407 continue;
408 }
409
410- if (bf->enemy_nearby_ && bo.fighting_type_) {
411+ if (buildable_field->enemy_nearby_ && bo.fighting_type_) {
412 ;
413 } // it is ok, go on
414- else if (bf->unowned_mines_pots_nearby_ > 2 &&
415+ else if (buildable_field->unowned_mines_pots_nearby_ > 2 &&
416 (bo.mountain_conqueror_ || bo.expansion_type_)) {
417 ;
418 } // it is ok, go on
419- else if (bf->unowned_land_nearby_ && bo.expansion_type_ &&
420+ else if (buildable_field->unowned_land_nearby_ && bo.expansion_type_ &&
421 num_milit_constructionsites <= 1) {
422 ; // we allow big buildings now
423- } else if (bf->unowned_land_nearby_ &&
424+ } else if (buildable_field->unowned_land_nearby_ &&
425 bo.expansion_type_) { // decreasing probability for big buidlings
426 if (bo.desc->get_size() == 2 && gametime % 15 >= 1) {
427 continue;
428@@ -1640,8 +1632,8 @@
429 } // the building is not suitable for situation
430
431 // not to build so many military buildings nearby
432- if (!bf->enemy_nearby_ &&
433- (bf->military_in_constr_nearby_ + bf->military_unstationed_) > 0) {
434+ if (!buildable_field->enemy_nearby_ &&
435+ (buildable_field->military_in_constr_nearby_ + buildable_field->military_unstationed_) > 0) {
436 continue;
437 }
438
439@@ -1651,14 +1643,14 @@
440 local_boost = 200;
441 }
442
443- prio = (bf->unowned_land_nearby_ * 2 * resource_necessity_territory_ / 255 +
444- bf->unowned_mines_pots_nearby_ * resource_necessity_mines_ / 255 +
445- bf->stones_nearby_ / 2 + bf->military_loneliness_ / 10 - 60 + local_boost +
446- bf->water_nearby_ * resource_necessity_water_ / 255);
447+ prio = (buildable_field->unowned_land_nearby_ * 2 * resource_necessity_territory_ / 255 +
448+ buildable_field->unowned_mines_pots_nearby_ * resource_necessity_mines_ / 255 +
449+ buildable_field->stones_nearby_ / 2 + buildable_field->military_loneliness_ / 10 - 60 + local_boost +
450+ buildable_field->water_nearby_ * resource_necessity_water_ / 255);
451
452 // special bonus due to remote water for atlanteans
453 if (resource_necessity_water_needed_)
454- prio += bf->distant_water_ * resource_necessity_water_ / 255;
455+ prio += buildable_field->distant_water_ * resource_necessity_water_ / 255;
456
457 if (bo.desc->get_size() < maxsize) {
458 prio = prio - 5;
459@@ -1671,22 +1663,22 @@
460 // for expansion)
461 const int16_t bottom_treshold =
462 15 - ((virtual_mines <= 4) ? (5 - virtual_mines) * 2 : 0);
463- if (bf->enemy_nearby_ && bf->military_capacity_ < bottom_treshold) {
464- prio += 50 + (bottom_treshold - bf->military_capacity_) * 20;
465+ if (buildable_field->enemy_nearby_ && buildable_field->military_capacity_ < bottom_treshold) {
466+ prio += 50 + (bottom_treshold - buildable_field->military_capacity_) * 20;
467 }
468
469- if (bf->enemy_nearby_ && bf->military_capacity_ > bottom_treshold + 4) {
470- prio -= (bf->military_capacity_ - (bottom_treshold + 4)) * 5;
471+ if (buildable_field->enemy_nearby_ && buildable_field->military_capacity_ > bottom_treshold + 4) {
472+ prio -= (buildable_field->military_capacity_ - (bottom_treshold + 4)) * 5;
473 }
474
475 } else if (bo.type == BuildingObserver::WAREHOUSE) {
476
477 // exclude spots on border
478- if (bf->near_border_ && !bo.is_port_) {
479+ if (buildable_field->near_border_ && !bo.is_port_) {
480 continue;
481 }
482
483- if (!bf->is_portspace_ && bo.is_port_) {
484+ if (!buildable_field->is_portspace_ && bo.is_port_) {
485 continue;
486 }
487
488@@ -1714,7 +1706,7 @@
489
490 // special boost for first port
491 if (bo.is_port_ && bo.total_count() == 0 && productionsites.size() > 5 &&
492- !bf->enemy_nearby_ && bf->is_portspace_ && seafaring_economy) {
493+ !buildable_field->enemy_nearby_ && buildable_field->is_portspace_ && seafaring_economy) {
494 prio += kDefaultPrioBoost + productionsites.size();
495 warehouse_needed = true;
496 }
497@@ -1728,7 +1720,7 @@
498 uint16_t nearest_distance = std::numeric_limits<uint16_t>::max();
499 for (const WarehouseSiteObserver& wh_obs : warehousesites) {
500 const uint16_t actual_distance =
501- map.calc_distance(bf->coords, wh_obs.site->get_position());
502+ map.calc_distance(buildable_field->coords, wh_obs.site->get_position());
503 nearest_distance = std::min(nearest_distance, actual_distance);
504 }
505 //but limit to 15
506@@ -1737,11 +1729,11 @@
507 prio += nearest_distance;
508
509 // take care about and enemies
510- if (bf->enemy_nearby_) {
511+ if (buildable_field->enemy_nearby_) {
512 prio /= 2;
513 }
514
515- if (bf->unowned_land_nearby_ && !bo.is_port_) {
516+ if (buildable_field->unowned_land_nearby_ && !bo.is_port_) {
517 prio /= 2;
518 }
519
520@@ -1752,7 +1744,7 @@
521 }
522
523 // exclude spots on border
524- if (bf->near_border_) {
525+ if (buildable_field->near_border_) {
526 continue;
527 }
528
529@@ -1767,17 +1759,17 @@
530 }
531
532 // take care about borders and enemies
533- if (bf->enemy_nearby_) {
534+ if (buildable_field->enemy_nearby_) {
535 prio /= 2;
536 }
537
538- if (bf->unowned_land_nearby_) {
539+ if (buildable_field->unowned_land_nearby_) {
540 prio /= 2;
541 }
542 }
543
544 // think of space consuming buildings nearby like farms or vineyards
545- prio -= bf->space_consumers_nearby_ * 10;
546+ prio -= buildable_field->space_consumers_nearby_ * 10;
547
548 // Stop here, if priority is 0 or less.
549 if (prio <= 0) {
550@@ -1786,25 +1778,25 @@
551
552 // testing also vicinity
553 if (!bo.is_port_) {
554- if (port_reserved_coords.count(coords_hash(bf->coords)) > 0) {
555+ if (port_reserved_coords.count(coords_hash(buildable_field->coords)) > 0) {
556 continue;
557 }
558 }
559
560 // Prefer road side fields
561- prio += bf->preferred_ ? 1 : 0;
562+ prio += buildable_field->preferred_ ? 1 : 0;
563 // don't waste good land for small huts
564 prio -= (maxsize - bo.desc->get_size()) * 5;
565
566 // prefer vicinity of ports (with exemption of warehouses)
567- if (bf->port_nearby_ && bo.type == BuildingObserver::MILITARYSITE) {
568+ if (buildable_field->port_nearby_ && bo.type == BuildingObserver::MILITARYSITE) {
569 prio *= 2;
570 }
571
572 if (prio > proposed_priority) {
573 best_building = &bo;
574 proposed_priority = prio;
575- proposed_coords = bf->coords;
576+ proposed_coords = buildable_field->coords;
577 }
578 } // ending loop over buildings
579 } // ending loop over fields
580@@ -1862,18 +1854,16 @@
581 }
582
583 // iterating over fields
584- for (std::list<MineableField*>::iterator j = mineable_fields.begin();
585- j != mineable_fields.end();
586- ++j) {
587+ for (const MineableField* mineable_field : mineable_fields) {
588
589- if ((*j)->coords.field->get_resources() != bo.mines_) {
590+ if (mineable_field->coords.field->get_resources() != bo.mines_) {
591 continue;
592 }
593
594- int32_t prio = (*j)->coords.field->get_resources_amount();
595+ int32_t prio = mineable_field->coords.field->get_resources_amount();
596
597 // applying nearnes penalty
598- prio = prio - (*j)->mines_nearby_ * nearness_penalty;
599+ prio = prio - mineable_field->mines_nearby_ * nearness_penalty;
600
601 // Only build mines_ on locations where some material can be mined
602 if (prio < 2) {
603@@ -1883,10 +1873,8 @@
604 // Continue if field is blocked at the moment
605 bool blocked = false;
606
607- for (std::list<BlockedField>::iterator k = blocked_fields.begin();
608- k != blocked_fields.end();
609- ++k)
610- if ((*j)->coords == k->coords) {
611+ for (const BlockedField& blocked_field :blocked_fields)
612+ if (mineable_field->coords == blocked_field.coords) {
613 blocked = true;
614 break;
615 }
616@@ -1897,13 +1885,13 @@
617 }
618
619 // Prefer road side fields
620- prio += (*j)->preferred_ ? 1 : 0;
621+ prio += mineable_field->preferred_ ? 1 : 0;
622
623 if (prio > proposed_priority) {
624 // proposed_building = bo.id;
625 best_building = &bo;
626 proposed_priority = prio;
627- proposed_coords = (*j)->coords;
628+ proposed_coords = mineable_field->coords;
629 mine = true;
630 }
631 } // end of evaluation of field
632@@ -2098,10 +2086,10 @@
633 continue;
634 }
635
636- std::vector<NearFlag>::iterator f =
637+ std::vector<NearFlag>::iterator near_flag_it =
638 find(reachableflags.begin(), reachableflags.end(), queue.top().flag);
639
640- if (f != reachableflags.end()) {
641+ if (near_flag_it != reachableflags.end()) {
642 queue.pop();
643 continue;
644 }
645@@ -2280,10 +2268,10 @@
646
647 // algorithm to walk on roads
648 while (!queue.empty()) {
649- std::vector<NearFlag>::iterator f =
650+ std::vector<NearFlag>::iterator near_flag_it =
651 find(nearflags_tmp.begin(), nearflags_tmp.end(), queue.top().flag);
652
653- if (f != nearflags_tmp.end()) {
654+ if (near_flag_it != nearflags_tmp.end()) {
655 queue.pop();
656 continue;
657 }
658@@ -2318,19 +2306,16 @@
659 // iterating over nearflags_tmp, each item in nearflags_tmp should be contained also in nearflags
660 // so for each corresponding field in nearflags we update "cost" (distance on existing roads)
661 // to actual value
662- for (std::vector<NearFlag>::iterator nf_walk_it = nearflags_tmp.begin();
663- nf_walk_it != nearflags_tmp.end();
664- ++nf_walk_it) {
665- uint32_t const hash_walk = coords_hash(nf_walk_it->flag->get_position());
666+ for (const NearFlag& temp_near_flag : nearflags_tmp) {
667+ uint32_t const hash_walk = coords_hash(temp_near_flag.flag->get_position());
668 if (lookuptable.count(hash_walk) > 0) {
669 // iterating over nearflags
670- for (std::vector<NearFlag>::iterator nf_it = nearflags.begin(); nf_it != nearflags.end();
671- ++nf_it) {
672- uint32_t const hash = coords_hash(nf_it->flag->get_position());
673+ for (NearFlag& near_flag : nearflags) {
674+ uint32_t const hash = coords_hash(near_flag.flag->get_position());
675 if (hash == hash_walk) {
676 // decreasing "cost" (of walking via roads)
677- if (nf_it->cost_ > nf_walk_it->cost_) {
678- nf_it->cost_ = nf_walk_it->cost_;
679+ if (near_flag.cost_ > temp_near_flag.cost_) {
680+ near_flag.cost_ = temp_near_flag.cost_;
681 }
682 }
683 }
684@@ -2403,26 +2388,21 @@
685 get_economy_observer(flag.economy())->flags.push_back(&flag);
686 }
687
688- for (std::list<EconomyObserver*>::iterator obs_iter = economies.begin();
689- obs_iter != economies.end();
690- ++obs_iter) {
691+ for (EconomyObserver* economy_observer : economies) {
692 // check if any flag has changed its economy
693- std::list<Flag const*>& fl = (*obs_iter)->flags;
694+ std::list<Flag const*>& fl = economy_observer->flags;
695
696- for (std::list<Flag const*>::iterator j = fl.begin(); j != fl.end();) {
697- if (&(*obs_iter)->economy != &(*j)->economy()) {
698- get_economy_observer((*j)->economy())->flags.push_back(*j);
699- j = fl.erase(j);
700- } else {
701- ++j;
702+ for (const Flag* flag : fl) {
703+ if (&economy_observer->economy != &flag->economy()) {
704+ get_economy_observer(flag->economy())->flags.push_back(flag);
705+ fl.remove(flag);
706 }
707 }
708
709 // if there are no more flags in this economy,
710 // we no longer need it's observer
711- if ((*obs_iter)->flags.empty()) {
712- delete *obs_iter;
713- economies.erase(obs_iter);
714+ if (economy_observer->flags.empty()) {
715+ economies.remove(economy_observer);
716 return true;
717 }
718 }
719@@ -2800,20 +2780,19 @@
720 }
721
722 // and now over ships
723- for (std::list<ShipObserver>::iterator sp_iter = allships.begin(); sp_iter != allships.end();
724- ++sp_iter) {
725- if (sp_iter->ship->state_is_expedition()) {
726+ for (const ShipObserver& ship_observer : allships) {
727+ if (ship_observer.ship->state_is_expedition()) {
728 expeditions_in_progress += 1;
729 }
730 }
731
732 // we must verify that all remote ports are still ours (and exists at all)
733 bool still_ours;
734- for (std::unordered_set<uint32_t>::iterator ports_iter = remote_ports_coords.begin();
735- ports_iter != remote_ports_coords.end();
736- ++ports_iter) {
737+ for (std::unordered_set<uint32_t>::iterator ports_it = remote_ports_coords.begin();
738+ ports_it != remote_ports_coords.end();
739+ ++ports_it) {
740 still_ours = false;
741- FCoords fcoords = game().map().get_fcoords(coords_unhash(*ports_iter));
742+ FCoords fcoords = game().map().get_fcoords(coords_unhash(*ports_it));
743 if (fcoords.field->get_owned_by() == player_number()) {
744 if (upcast(PlayerImmovable, imm, fcoords.field->get_immovable())) {
745 still_ours = true;
746@@ -2821,7 +2800,7 @@
747 }
748
749 if (!still_ours) {
750- remote_ports_coords.erase(*ports_iter);
751+ remote_ports_coords.erase(*ports_it);
752 break;
753 }
754 }
755@@ -2892,26 +2871,25 @@
756 }
757
758 if (!allships.empty()) {
759- // iterating over ships and executing what is needed
760- for (std::list<ShipObserver>::iterator i = allships.begin(); i != allships.end(); ++i) {
761-
762- // only two states need an attention
763- if ((i->ship->get_ship_state() == Widelands::Ship::EXP_WAITING ||
764- i->ship->get_ship_state() == Widelands::Ship::EXP_FOUNDPORTSPACE) &&
765- !i->waiting_for_command_) {
766- if (gametime - i->last_command_time > 180 * 1000) {
767- i->waiting_for_command_ = true;
768+ // Iterating over ships and executing what is needed.
769+ for (ShipObserver& ship_observer : allships) {
770+ // Only two states need attention.
771+ if ((ship_observer.ship->get_ship_state() == Widelands::Ship::EXP_WAITING ||
772+ ship_observer.ship->get_ship_state() == Widelands::Ship::EXP_FOUNDPORTSPACE) &&
773+ !ship_observer.waiting_for_command_) {
774+ if (gametime - ship_observer.last_command_time > 180 * 1000) {
775+ ship_observer.waiting_for_command_ = true;
776 log(" %1d: last command for ship at %3dx%3d was %3d seconds ago, something wrong "
777 "here?...\n",
778 player_number(),
779- i->ship->get_position().x,
780- i->ship->get_position().y,
781- (gametime - i->last_command_time) / 1000);
782+ ship_observer.ship->get_position().x,
783+ ship_observer.ship->get_position().y,
784+ (gametime - ship_observer.last_command_time) / 1000);
785 }
786 }
787- // if ships is waiting for command
788- if (i->waiting_for_command_) {
789- expedition_management(*i);
790+ // If ship is waiting for command.
791+ if (ship_observer.waiting_for_command_) {
792+ expedition_management(ship_observer);
793 }
794 }
795 }
796@@ -2920,33 +2898,27 @@
797 while (!marineTaskQueue_.empty()) {
798 if (marineTaskQueue_.back() == kStopShipyard) {
799 // iterate over all production sites searching for shipyard
800- for (std::list<ProductionSiteObserver>::iterator site = productionsites.begin();
801- site != productionsites.end();
802- ++site) {
803- if (site->bo->is_shipyard_) {
804- if (!site->site->is_stopped()) {
805- game().send_player_start_stop_building(*site->site);
806+ for (const ProductionSiteObserver& prod_site_observer : productionsites) {
807+ if (prod_site_observer.bo->is_shipyard_) {
808+ if (!prod_site_observer.site->is_stopped()) {
809+ game().send_player_start_stop_building(*prod_site_observer.site);
810 }
811 }
812 }
813 }
814
815 if (marineTaskQueue_.back() == kReprioritize) {
816- for (std::list<ProductionSiteObserver>::iterator site = productionsites.begin();
817- site != productionsites.end();
818- ++site) {
819- if (site->bo->is_shipyard_) {
820- for (uint32_t k = 0; k < site->bo->inputs_.size(); ++k) {
821+ for (const ProductionSiteObserver& prod_site_observer : productionsites) {
822+ if (prod_site_observer.bo->is_shipyard_) {
823+ for (uint32_t k = 0; k < prod_site_observer.bo->inputs_.size(); ++k) {
824 game().send_player_set_ware_priority(
825- *site->site, wwWARE, site->bo->inputs_.at(k), HIGH_PRIORITY);
826+ *prod_site_observer.site, wwWARE, prod_site_observer.bo->inputs_.at(k), HIGH_PRIORITY);
827 }
828 }
829 }
830 }
831-
832 marineTaskQueue_.pop_back();
833 }
834-
835 return true;
836 }
837
838@@ -3127,13 +3099,9 @@
839 // perhaps it will be able to replace get_stocklevel
840 uint32_t DefaultAI::get_warehoused_stock(WareIndex wt) {
841 uint32_t count = 0;
842-
843- for (std::list<WarehouseSiteObserver>::iterator i = warehousesites.begin();
844- i != warehousesites.end();
845- ++i) {
846- count += i->site->get_wares().stock(wt);
847+ for (const WarehouseSiteObserver& warehouse_observer : warehousesites) {
848+ count += warehouse_observer.site->get_wares().stock(wt);
849 }
850-
851 return count;
852 }
853
854@@ -3155,13 +3123,10 @@
855 } else {
856 new_priority = DEFAULT_PRIORITY;
857 }
858- for (std::list<TrainingSiteObserver>::iterator site = trainingsites.begin();
859- site != trainingsites.end();
860- ++site) {
861-
862- for (uint32_t k = 0; k < site->bo->inputs_.size(); ++k) {
863+ for (const TrainingSiteObserver& trainingsite_observer :trainingsites) {
864+ for (uint32_t k = 0; k < trainingsite_observer.bo->inputs_.size(); ++k) {
865 game().send_player_set_ware_priority(
866- *site->site, wwWARE, site->bo->inputs_.at(k), new_priority);
867+ *trainingsite_observer.site, wwWARE, trainingsite_observer.bo->inputs_.at(k), new_priority);
868 }
869 }
870 return true;
871@@ -3186,10 +3151,8 @@
872 // construction
873 unstationed_milit_buildings_ = 0;
874
875- for (std::list<MilitarySiteObserver>::iterator it = militarysites.begin();
876- it != militarysites.end();
877- ++it) {
878- if (it->site->stationed_soldiers().size() == 0) {
879+ for (const MilitarySiteObserver& militarysite_observer : militarysites) {
880+ if (militarysite_observer.site->stationed_soldiers().size() == 0) {
881 unstationed_milit_buildings_ += 1;
882 }
883 }
884@@ -3387,9 +3350,11 @@
885
886 /// \returns the economy observer containing \arg economy
887 EconomyObserver* DefaultAI::get_economy_observer(Economy& economy) {
888- for (std::list<EconomyObserver*>::iterator i = economies.begin(); i != economies.end(); ++i)
889- if (&(*i)->economy == &economy)
890- return *i;
891+ for (EconomyObserver* economy_observer : economies) {
892+ if (&economy_observer->economy == &economy) {
893+ return economy_observer;
894+ }
895+ }
896
897 economies.push_front(new EconomyObserver(economy));
898 return economies.front();
899@@ -3427,20 +3392,16 @@
900 lose_building(*building);
901 } else if (upcast(Flag const, flag, &pi)) {
902 for (EconomyObserver* eco_obs : economies) {
903- for (std::list<Flag const*>::iterator flag_iter = eco_obs->flags.begin();
904- flag_iter != eco_obs->flags.end();
905- ++flag_iter) {
906- if (*flag_iter == flag) {
907- eco_obs->flags.erase(flag_iter);
908+ for (const Flag* economy_flag : eco_obs->flags) {
909+ if (economy_flag == flag) {
910+ eco_obs->flags.remove(economy_flag);
911 return;
912 }
913 }
914 }
915- for (std::list<Flag const*>::iterator flag_iter = new_flags.begin();
916- flag_iter != new_flags.end();
917- ++flag_iter) {
918- if (*flag_iter == flag) {
919- new_flags.erase(flag_iter);
920+ for (const Flag* new_flag : new_flags) {
921+ if (new_flag == flag) {
922+ new_flags.remove(new_flag);
923 return;
924 }
925 }
926@@ -3453,9 +3414,9 @@
927 void DefaultAI::out_of_resources_site(const ProductionSite& site) {
928
929 // we must identify which mine matches the productionsite a note reffers to
930- for (std::list<ProductionSiteObserver>::iterator i = mines_.begin(); i != mines_.end(); ++i)
931- if (i->site == &site) {
932- i->no_resources_count += 1;
933+ for (ProductionSiteObserver& mine_observer : mines_)
934+ if (mine_observer.site == &site) {
935+ mine_observer.no_resources_count += 1;
936 break;
937 }
938 }
939@@ -3752,11 +3713,11 @@
940
941 if (bo.type == BuildingObserver::PRODUCTIONSITE) {
942
943- for (std::list<ProductionSiteObserver>::iterator i = productionsites.begin();
944- i != productionsites.end();
945- ++i)
946- if (i->site == &b) {
947- productionsites.erase(i);
948+ for (std::list<ProductionSiteObserver>::iterator prod_site_observer_it = productionsites.begin();
949+ prod_site_observer_it != productionsites.end();
950+ ++prod_site_observer_it)
951+ if (prod_site_observer_it->site == &b) {
952+ productionsites.erase(prod_site_observer_it);
953 break;
954 }
955
956@@ -3768,10 +3729,10 @@
957 --wares.at(bo.inputs_.at(i)).consumers_;
958 }
959 } else if (bo.type == BuildingObserver::MINE) {
960- for (std::list<ProductionSiteObserver>::iterator i = mines_.begin(); i != mines_.end();
961- ++i) {
962- if (i->site == &b) {
963- mines_.erase(i);
964+ for (std::list<ProductionSiteObserver>::iterator mines_observer_it = mines_.begin(); mines_observer_it != mines_.end();
965+ ++mines_observer_it) {
966+ if (mines_observer_it->site == &b) {
967+ mines_.erase(mines_observer_it);
968 break;
969 }
970 }
971@@ -3785,21 +3746,21 @@
972 }
973 } else if (bo.type == BuildingObserver::MILITARYSITE) {
974
975- for (std::list<MilitarySiteObserver>::iterator i = militarysites.begin();
976- i != militarysites.end();
977- ++i) {
978- if (i->site == &b) {
979- militarysites.erase(i);
980+ for (std::list<MilitarySiteObserver>::iterator militarysite_observer_it = militarysites.begin();
981+ militarysite_observer_it != militarysites.end();
982+ ++militarysite_observer_it) {
983+ if (militarysite_observer_it->site == &b) {
984+ militarysites.erase(militarysite_observer_it);
985 break;
986 }
987 }
988 } else if (bo.type == BuildingObserver::TRAININGSITE) {
989
990- for (std::list<TrainingSiteObserver>::iterator i = trainingsites.begin();
991- i != trainingsites.end();
992- ++i) {
993- if (i->site == &b) {
994- trainingsites.erase(i);
995+ for (std::list<TrainingSiteObserver>::iterator trainingsite_observer_it = trainingsites.begin();
996+ trainingsite_observer_it != trainingsites.end();
997+ ++trainingsite_observer_it) {
998+ if (trainingsite_observer_it->site == &b) {
999+ trainingsites.erase(trainingsite_observer_it);
1000 break;
1001 }
1002 }
1003@@ -3810,11 +3771,11 @@
1004 --num_ports;
1005 }
1006
1007- for (std::list<WarehouseSiteObserver>::iterator i = warehousesites.begin();
1008- i != warehousesites.end();
1009- ++i) {
1010- if (i->site == &b) {
1011- warehousesites.erase(i);
1012+ for (std::list<WarehouseSiteObserver>::iterator warehouse_observer_it = warehousesites.begin();
1013+ warehouse_observer_it != warehousesites.end();
1014+ ++warehouse_observer_it) {
1015+ if (warehouse_observer_it->site == &b) {
1016+ warehousesites.erase(warehouse_observer_it);
1017 break;
1018 }
1019 }
1020@@ -3982,12 +3943,12 @@
1021 for (uint32_t position = gametime % test_every; position < militarysites.size();
1022 position += test_every) {
1023
1024- std::list<MilitarySiteObserver>::iterator mso = militarysites.begin();
1025- std::advance(mso, position);
1026-
1027- MilitarySite* ms = mso->site;
1028-
1029- if (!mso->enemies_nearby_) {
1030+ std::list<MilitarySiteObserver>::iterator militarysite_observer_it = militarysites.begin();
1031+ std::advance(militarysite_observer_it, position);
1032+
1033+ MilitarySite* ms = militarysite_observer_it->site;
1034+
1035+ if (!militarysite_observer_it->enemies_nearby_) {
1036 continue;
1037 }
1038

Subscribers

People subscribed via source and target branches

to status/vote changes: