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
=== modified file 'src/ai/ai_help_structs.h'
--- src/ai/ai_help_structs.h 2014-12-16 11:37:49 +0000
+++ src/ai/ai_help_structs.h 2014-12-16 11:37:49 +0000
@@ -189,6 +189,14 @@
189 Widelands::FCoords coords;189 Widelands::FCoords coords;
190 int32_t blocked_until_;190 int32_t blocked_until_;
191191
192 bool operator== (const BlockedField& other) const {
193 return coords == other.coords;
194 }
195
196 bool operator!= (const BlockedField& other) const {
197 return coords != other.coords;
198 }
199
192 BlockedField(Widelands::FCoords c, int32_t until) : coords(c), blocked_until_(until) {200 BlockedField(Widelands::FCoords c, int32_t until) : coords(c), blocked_until_(until) {
193 }201 }
194};202};
@@ -387,6 +395,15 @@
387};395};
388396
389struct ShipObserver {397struct ShipObserver {
398
399 bool operator== (const ShipObserver& other) const {
400 return ship->serial() == other.ship->serial();
401 }
402
403 bool operator!= (const ShipObserver& other) const {
404 return ship->serial() != other.ship->serial();
405 }
406
390 Widelands::Ship* ship;407 Widelands::Ship* ship;
391 Widelands::Coords expedition_start_point_;408 Widelands::Coords expedition_start_point_;
392 std::unordered_set<uint32_t> visited_spots_;409 std::unordered_set<uint32_t> visited_spots_;
393410
=== modified file 'src/ai/defaultai.cc'
--- src/ai/defaultai.cc 2014-12-16 11:37:49 +0000
+++ src/ai/defaultai.cc 2014-12-16 11:37:49 +0000
@@ -181,19 +181,17 @@
181 }181 }
182 break;182 break;
183183
184 case NoteShipMessage::Message::kLost:184 case NoteShipMessage::Message::kLost: {
185 for (std::list<ShipObserver>::iterator i = allships.begin(); i != allships.end(); ++i) {185 for (const ShipObserver& ship_observer : allships) {
186 if (i->ship == note.ship) {186 if (ship_observer.ship == note.ship) {
187 allships.erase(i);187 allships.remove(ship_observer);
188 break;188 break;
189 }189 }
190 }190 }
191 break;191 case NoteShipMessage::Message::kWaitingForCommand:
192192 for (ShipObserver& ship_observer : allships) {
193 case NoteShipMessage::Message::kWaitingForCommand:193 if (ship_observer.ship == note.ship) {
194 for (std::list<ShipObserver>::iterator i = allships.begin(); i != allships.end(); ++i) {194 ship_observer.waiting_for_command_ = true;
195 if (i->ship == note.ship) {
196 i->waiting_for_command_ = true;
197 break;195 break;
198 }196 }
199 }197 }
@@ -201,7 +199,8 @@
201 default:199 default:
202 ;200 ;
203 }201 }
204 });202 }
203 });
205}204}
206205
207DefaultAI::~DefaultAI() {206DefaultAI::~DefaultAI() {
@@ -1032,10 +1031,8 @@
1032 for (int32_t i = 0; i < 4; ++i)1031 for (int32_t i = 0; i < 4; ++i)
1033 spots_avail.at(i) = 0;1032 spots_avail.at(i) = 0;
10341033
1035 for (std::list<BuildableField*>::iterator i = buildable_fields.begin();1034 for (const BuildableField* buildable_field : buildable_fields)
1036 i != buildable_fields.end();1035 ++spots_avail.at(buildable_field->coords.field->nodecaps() & BUILDCAPS_SIZEMASK);
1037 ++i)
1038 ++spots_avail.at((*i)->coords.field->nodecaps() & BUILDCAPS_SIZEMASK);
10391036
1040 spots_ = spots_avail.at(BUILDCAPS_SMALL);1037 spots_ = spots_avail.at(BUILDCAPS_SMALL);
1041 spots_ += spots_avail.at(BUILDCAPS_MEDIUM);1038 spots_ += spots_avail.at(BUILDCAPS_MEDIUM);
@@ -1125,12 +1122,11 @@
1125 Coords proposed_coords;1122 Coords proposed_coords;
11261123
1127 // Remove outdated fields from blocker list1124 // Remove outdated fields from blocker list
1128 for (std::list<BlockedField>::iterator i = blocked_fields.begin(); i != blocked_fields.end();)1125 for (const BlockedField& blocked_field : blocked_fields) {
1129 if (i->blocked_until_ < game().get_gametime()) {1126 if (blocked_field.blocked_until_ < game().get_gametime()) {
1130 i = blocked_fields.erase(i);1127 blocked_fields.remove(blocked_field);
1131 } else {
1132 ++i;
1133 }1128 }
1129 }
11341130
1135 // testing big military buildings, whether critical construction1131 // testing big military buildings, whether critical construction
1136 // material is available (at least in amount of1132 // material is available (at least in amount of
@@ -1169,21 +1165,17 @@
1169 int16_t max_needed_preciousness = 0; // preciousness_ of most precious NEEDED output1165 int16_t max_needed_preciousness = 0; // preciousness_ of most precious NEEDED output
11701166
1171 // first scan all buildable fields for regular buildings1167 // first scan all buildable fields for regular buildings
1172 for (std::list<BuildableField*>::iterator i = buildable_fields.begin();1168 for (const BuildableField* buildable_field : buildable_fields) {
1173 i != buildable_fields.end();
1174 ++i) {
1175 BuildableField* const bf = *i;
11761169
1177 if (bf->next_update_due_ < gametime - 8000) {1170 if (buildable_field->next_update_due_ < gametime - 8000) {
1178 continue;1171 continue;
1179 }1172 }
11801173
1181 // Continue if field is blocked at the moment1174 // Continue if field is blocked at the moment
1182 field_blocked = false;1175 field_blocked = false;
11831176
1184 for (std::list<BlockedField>::iterator j = blocked_fields.begin(); j != blocked_fields.end();1177 for (const BlockedField& blocked_field : blocked_fields) {
1185 ++j) {1178 if (blocked_field.coords == buildable_field->coords) {
1186 if (j->coords == bf->coords) {
1187 field_blocked = true;1179 field_blocked = true;
1188 }1180 }
1189 }1181 }
@@ -1194,7 +1186,7 @@
1194 }1186 }
11951187
1196 assert(player_);1188 assert(player_);
1197 int32_t const maxsize = player_->get_buildcaps(bf->coords) & BUILDCAPS_SIZEMASK;1189 int32_t const maxsize = player_->get_buildcaps(buildable_field->coords) & BUILDCAPS_SIZEMASK;
11981190
1199 // For every field test all buildings1191 // For every field test all buildings
1200 for (uint32_t j = 0; j < buildings_.size(); ++j) {1192 for (uint32_t j = 0; j < buildings_.size(); ++j) {
@@ -1215,7 +1207,7 @@
12151207
1216 // testing for reserved ports1208 // testing for reserved ports
1217 if (!bo.is_port_) {1209 if (!bo.is_port_) {
1218 if (port_reserved_coords.count(coords_hash(bf->coords)) > 0) {1210 if (port_reserved_coords.count(coords_hash(buildable_field->coords)) > 0) {
1219 continue;1211 continue;
1220 }1212 }
1221 }1213 }
@@ -1251,13 +1243,13 @@
1251 if (bo.type == BuildingObserver::PRODUCTIONSITE) {1243 if (bo.type == BuildingObserver::PRODUCTIONSITE) {
12521244
1253 // exclude spots on border1245 // exclude spots on border
1254 if (bf->near_border_ && !bo.need_trees_ && !bo.need_stones_ && !bo.is_fisher_) {1246 if (buildable_field->near_border_ && !bo.need_trees_ && !bo.need_stones_ && !bo.is_fisher_) {
1255 continue;1247 continue;
1256 }1248 }
12571249
1258 // this can be only a well (as by now)1250 // this can be only a well (as by now)
1259 if (bo.mines_water_) {1251 if (bo.mines_water_) {
1260 if (bf->ground_water_ < 2) {1252 if (buildable_field->ground_water_ < 2) {
1261 continue;1253 continue;
1262 }1254 }
12631255
@@ -1283,8 +1275,8 @@
1283 if (bo.stocklevel_ > 50 + productionsites.size() * 5) {1275 if (bo.stocklevel_ > 50 + productionsites.size() * 5) {
1284 continue;1276 continue;
1285 }1277 }
1286 prio += bf->ground_water_ - 2;1278 prio += buildable_field->ground_water_ - 2;
1287 prio = recalc_with_border_range(*bf, prio);1279 prio = recalc_with_border_range(*buildable_field, prio);
12881280
1289 } else if (bo.need_trees_) { // LUMBERJACS1281 } else if (bo.need_trees_) { // LUMBERJACS
12901282
@@ -1292,14 +1284,14 @@
1292 3 + static_cast<int32_t>(mines_.size() + productionsites.size()) / 15;1284 3 + static_cast<int32_t>(mines_.size() + productionsites.size()) / 15;
12931285
1294 if (bo.total_count() == 0) {1286 if (bo.total_count() == 0) {
1295 prio = 500 + bf->trees_nearby_;1287 prio = 500 + buildable_field->trees_nearby_;
1296 }1288 }
12971289
1298 else if (bo.total_count() == 1) {1290 else if (bo.total_count() == 1) {
1299 prio = 400 + bf->trees_nearby_;1291 prio = 400 + buildable_field->trees_nearby_;
1300 }1292 }
13011293
1302 else if (bf->trees_nearby_ < 2) {1294 else if (buildable_field->trees_nearby_ < 2) {
1303 continue;1295 continue;
1304 }1296 }
13051297
@@ -1311,15 +1303,15 @@
1311 prio = 0;1303 prio = 0;
1312 }1304 }
13131305
1314 if (bf->producers_nearby_.at(bo.outputs_.at(0)) > 1) {1306 if (buildable_field->producers_nearby_.at(bo.outputs_.at(0)) > 1) {
1315 continue;1307 continue;
1316 }1308 }
13171309
1318 prio += 2 * bf->trees_nearby_ - 10 -1310 prio += 2 * buildable_field->trees_nearby_ - 10 -
1319 bf->producers_nearby_.at(bo.outputs_.at(0)) * 5 -1311 buildable_field->producers_nearby_.at(bo.outputs_.at(0)) * 5 -
1320 new_buildings_stop_ * 15;1312 new_buildings_stop_ * 15;
13211313
1322 if (bf->near_border_) {1314 if (buildable_field->near_border_) {
1323 prio = prio / 2;1315 prio = prio / 2;
1324 }1316 }
1325 }1317 }
@@ -1332,7 +1324,7 @@
1332 if (bo.cnt_under_construction_ > 0) {1324 if (bo.cnt_under_construction_ > 0) {
1333 continue;1325 continue;
1334 }1326 }
1335 prio = bf->stones_nearby_;1327 prio = buildable_field->stones_nearby_;
13361328
1337 if (prio <= 0) {1329 if (prio <= 0) {
1338 continue;1330 continue;
@@ -1352,14 +1344,14 @@
1352 }1344 }
13531345
1354 // to prevent to many quaries on one spot1346 // to prevent to many quaries on one spot
1355 prio = prio - 50 * bf->producers_nearby_.at(bo.outputs_.at(0));1347 prio = prio - 50 * buildable_field->producers_nearby_.at(bo.outputs_.at(0));
13561348
1357 if (bf->near_border_) {1349 if (buildable_field->near_border_) {
1358 prio = prio / 2;1350 prio = prio / 2;
1359 }1351 }
13601352
1361 } else if (bo.is_hunter_) {1353 } else if (bo.is_hunter_) {
1362 if (bf->critters_nearby_ < 5) {1354 if (buildable_field->critters_nearby_ < 5) {
1363 continue;1355 continue;
1364 }1356 }
13651357
@@ -1368,7 +1360,7 @@
1368 }1360 }
13691361
1370 prio +=1362 prio +=
1371 (bf->critters_nearby_ * 2) - 8 - 5 * bf->producers_nearby_.at(bo.outputs_.at(0));1363 (buildable_field->critters_nearby_ * 2) - 8 - 5 * buildable_field->producers_nearby_.at(bo.outputs_.at(0));
13721364
1373 } else if (bo.is_fisher_) { // fisher1365 } else if (bo.is_fisher_) { // fisher
13741366
@@ -1381,7 +1373,7 @@
1381 continue;1373 continue;
1382 }1374 }
13831375
1384 if (bf->water_nearby_ < 2) {1376 if (buildable_field->water_nearby_ < 2) {
1385 continue;1377 continue;
1386 }1378 }
13871379
@@ -1400,11 +1392,11 @@
1400 continue;1392 continue;
1401 }1393 }
14021394
1403 if (bf->producers_nearby_.at(bo.outputs_.at(0)) >= 1) {1395 if (buildable_field->producers_nearby_.at(bo.outputs_.at(0)) >= 1) {
1404 continue;1396 continue;
1405 }1397 }
14061398
1407 prio = bf->fish_nearby_ - new_buildings_stop_ * 15 * bo.total_count();1399 prio = buildable_field->fish_nearby_ - new_buildings_stop_ * 15 * bo.total_count();
14081400
1409 } else if (bo.production_hint_ >= 0) {1401 } else if (bo.production_hint_ >= 0) {
1410 // first setting targets (needed also for dismantling)1402 // first setting targets (needed also for dismantling)
@@ -1423,7 +1415,7 @@
1423 if (bo.plants_trees_) { // RANGERS1415 if (bo.plants_trees_) { // RANGERS
14241416
1425 // if there are too many trees nearby1417 // if there are too many trees nearby
1426 if (bf->trees_nearby_ > 25 && bo.total_count() >= 1) {1418 if (buildable_field->trees_nearby_ > 25 && bo.total_count() >= 1) {
1427 continue;1419 continue;
1428 }1420 }
14291421
@@ -1451,12 +1443,12 @@
1451 }1443 }
14521444
1453 // considering near trees and producers1445 // considering near trees and producers
1454 prio += (30 - bf->trees_nearby_) * 2 +1446 prio += (30 - buildable_field->trees_nearby_) * 2 +
1455 bf->producers_nearby_.at(bo.production_hint_) * 5 -1447 buildable_field->producers_nearby_.at(bo.production_hint_) * 5 -
1456 new_buildings_stop_ * 15;1448 new_buildings_stop_ * 15;
14571449
1458 // considering space consumers nearby1450 // considering space consumers nearby
1459 prio -= bf->space_consumers_nearby_ * 5;1451 prio -= buildable_field->space_consumers_nearby_ * 5;
14601452
1461 } else { // FISH BREEDERS and GAME KEEPERS1453 } else { // FISH BREEDERS and GAME KEEPERS
1462 if (new_buildings_stop_ && bo.total_count() > 0) {1454 if (new_buildings_stop_ && bo.total_count() > 0) {
@@ -1464,11 +1456,11 @@
1464 }1456 }
14651457
1466 // especially for fish breeders1458 // especially for fish breeders
1467 if (bo.need_water_ && bf->water_nearby_ < 2) {1459 if (bo.need_water_ && buildable_field->water_nearby_ < 2) {
1468 continue;1460 continue;
1469 }1461 }
1470 if (bo.need_water_) {1462 if (bo.need_water_) {
1471 prio += bf->water_nearby_ / 5;1463 prio += buildable_field->water_nearby_ / 5;
1472 }1464 }
14731465
1474 if (bo.total_count() > bo.cnt_target_) {1466 if (bo.total_count() > bo.cnt_target_) {
@@ -1485,14 +1477,14 @@
1485 }1477 }
14861478
1487 if (bo.total_count() == 0 && gametime > 45 * 1000) {1479 if (bo.total_count() == 0 && gametime > 45 * 1000) {
1488 prio += 100 + bf->producers_nearby_.at(bo.production_hint_) * 10;1480 prio += 100 + buildable_field->producers_nearby_.at(bo.production_hint_) * 10;
1489 } else if (bf->producers_nearby_.at(bo.production_hint_) == 0) {1481 } else if (buildable_field->producers_nearby_.at(bo.production_hint_) == 0) {
1490 continue;1482 continue;
1491 } else {1483 } else {
1492 prio += bf->producers_nearby_.at(bo.production_hint_) * 10;1484 prio += buildable_field->producers_nearby_.at(bo.production_hint_) * 10;
1493 }1485 }
14941486
1495 if (bf->enemy_nearby_) {1487 if (buildable_field->enemy_nearby_) {
1496 prio -= 10;1488 prio -= 10;
1497 }1489 }
1498 }1490 }
@@ -1536,34 +1528,34 @@
1536 prio += max_needed_preciousness + kDefaultPrioBoost;1528 prio += max_needed_preciousness + kDefaultPrioBoost;
15371529
1538 if (bo.space_consumer_) { // need to consider trees nearby1530 if (bo.space_consumer_) { // need to consider trees nearby
1539 prio += 20 - (bf->trees_nearby_ / 3);1531 prio += 20 - (buildable_field->trees_nearby_ / 3);
1540 }1532 }
15411533
1542 // we attempt to cluster space consumers together1534 // we attempt to cluster space consumers together
1543 if (bo.space_consumer_) { // need to consider trees nearby1535 if (bo.space_consumer_) { // need to consider trees nearby
1544 prio += bf->space_consumers_nearby_ * 2;1536 prio += buildable_field->space_consumers_nearby_ * 2;
1545 }1537 }
15461538
1547 if (bo.space_consumer_ && !bf->water_nearby_) { // not close to water1539 if (bo.space_consumer_ && !buildable_field->water_nearby_) { // not close to water
1548 prio += 1;1540 prio += 1;
1549 }1541 }
15501542
1551 if (bo.space_consumer_ &&1543 if (bo.space_consumer_ &&
1552 !bf->unowned_mines_pots_nearby_) { // not close to mountains1544 !buildable_field->unowned_mines_pots_nearby_) { // not close to mountains
1553 prio += 1;1545 prio += 1;
1554 }1546 }
15551547
1556 if (!bo.space_consumer_) {1548 if (!bo.space_consumer_) {
1557 prio -= bf->producers_nearby_.at(bo.outputs_.at(0)) * 20;1549 prio -= buildable_field->producers_nearby_.at(bo.outputs_.at(0)) * 20;
1558 } // leave some free space between them1550 } // leave some free space between them
15591551
1560 prio -= bf->space_consumers_nearby_ * 3;1552 prio -= buildable_field->space_consumers_nearby_ * 3;
1561 }1553 }
15621554
1563 else if (bo.is_shipyard_) {1555 else if (bo.is_shipyard_) {
1564 // for now AI builds only one shipyard1556 // for now AI builds only one shipyard
1565 if (bf->water_nearby_ > 3 && bo.total_count() == 0 && seafaring_economy) {1557 if (buildable_field->water_nearby_ > 3 && bo.total_count() == 0 && seafaring_economy) {
1566 prio += kDefaultPrioBoost + productionsites.size() * 5 + bf->water_nearby_;1558 prio += kDefaultPrioBoost + productionsites.size() * 5 + buildable_field->water_nearby_;
1567 }1559 }
15681560
1569 } else if (!bo.inputs_.empty()) {1561 } else if (!bo.inputs_.empty()) {
@@ -1584,7 +1576,7 @@
1584 consumers_nearby_count = 0;1576 consumers_nearby_count = 0;
15851577
1586 for (size_t k = 0; k < bo.outputs_.size(); ++k)1578 for (size_t k = 0; k < bo.outputs_.size(); ++k)
1587 consumers_nearby_count += bf->consumers_nearby_.at(bo.outputs_.at(k));1579 consumers_nearby_count += buildable_field->consumers_nearby_.at(bo.outputs_.at(k));
15881580
1589 if (consumers_nearby_count > 0) {1581 if (consumers_nearby_count > 0) {
1590 prio += 1;1582 prio += 1;
@@ -1595,11 +1587,11 @@
15951587
1596 // we allow 1 exemption from big buildings prohibition1588 // we allow 1 exemption from big buildings prohibition
1597 if (bo.build_material_shortage_ &&1589 if (bo.build_material_shortage_ &&
1598 (bo.cnt_under_construction_ > 0 || !(bf->enemy_nearby_))) {1590 (bo.cnt_under_construction_ > 0 || !(buildable_field->enemy_nearby_))) {
1599 continue;1591 continue;
1600 }1592 }
16011593
1602 if (!bf->unowned_land_nearby_) {1594 if (!buildable_field->unowned_land_nearby_) {
1603 continue;1595 continue;
1604 }1596 }
16051597
@@ -1611,21 +1603,21 @@
1611 continue;1603 continue;
1612 }1604 }
16131605
1614 if (expansion_mode == kDefenseOnly && !bf->enemy_nearby_) {1606 if (expansion_mode == kDefenseOnly && !buildable_field->enemy_nearby_) {
1615 continue;1607 continue;
1616 }1608 }
16171609
1618 if (bf->enemy_nearby_ && bo.fighting_type_) {1610 if (buildable_field->enemy_nearby_ && bo.fighting_type_) {
1619 ;1611 ;
1620 } // it is ok, go on1612 } // it is ok, go on
1621 else if (bf->unowned_mines_pots_nearby_ > 2 &&1613 else if (buildable_field->unowned_mines_pots_nearby_ > 2 &&
1622 (bo.mountain_conqueror_ || bo.expansion_type_)) {1614 (bo.mountain_conqueror_ || bo.expansion_type_)) {
1623 ;1615 ;
1624 } // it is ok, go on1616 } // it is ok, go on
1625 else if (bf->unowned_land_nearby_ && bo.expansion_type_ &&1617 else if (buildable_field->unowned_land_nearby_ && bo.expansion_type_ &&
1626 num_milit_constructionsites <= 1) {1618 num_milit_constructionsites <= 1) {
1627 ; // we allow big buildings now1619 ; // we allow big buildings now
1628 } else if (bf->unowned_land_nearby_ &&1620 } else if (buildable_field->unowned_land_nearby_ &&
1629 bo.expansion_type_) { // decreasing probability for big buidlings1621 bo.expansion_type_) { // decreasing probability for big buidlings
1630 if (bo.desc->get_size() == 2 && gametime % 15 >= 1) {1622 if (bo.desc->get_size() == 2 && gametime % 15 >= 1) {
1631 continue;1623 continue;
@@ -1640,8 +1632,8 @@
1640 } // the building is not suitable for situation1632 } // the building is not suitable for situation
16411633
1642 // not to build so many military buildings nearby1634 // not to build so many military buildings nearby
1643 if (!bf->enemy_nearby_ &&1635 if (!buildable_field->enemy_nearby_ &&
1644 (bf->military_in_constr_nearby_ + bf->military_unstationed_) > 0) {1636 (buildable_field->military_in_constr_nearby_ + buildable_field->military_unstationed_) > 0) {
1645 continue;1637 continue;
1646 }1638 }
16471639
@@ -1651,14 +1643,14 @@
1651 local_boost = 200;1643 local_boost = 200;
1652 }1644 }
16531645
1654 prio = (bf->unowned_land_nearby_ * 2 * resource_necessity_territory_ / 255 +1646 prio = (buildable_field->unowned_land_nearby_ * 2 * resource_necessity_territory_ / 255 +
1655 bf->unowned_mines_pots_nearby_ * resource_necessity_mines_ / 255 +1647 buildable_field->unowned_mines_pots_nearby_ * resource_necessity_mines_ / 255 +
1656 bf->stones_nearby_ / 2 + bf->military_loneliness_ / 10 - 60 + local_boost +1648 buildable_field->stones_nearby_ / 2 + buildable_field->military_loneliness_ / 10 - 60 + local_boost +
1657 bf->water_nearby_ * resource_necessity_water_ / 255);1649 buildable_field->water_nearby_ * resource_necessity_water_ / 255);
16581650
1659 // special bonus due to remote water for atlanteans1651 // special bonus due to remote water for atlanteans
1660 if (resource_necessity_water_needed_)1652 if (resource_necessity_water_needed_)
1661 prio += bf->distant_water_ * resource_necessity_water_ / 255;1653 prio += buildable_field->distant_water_ * resource_necessity_water_ / 255;
16621654
1663 if (bo.desc->get_size() < maxsize) {1655 if (bo.desc->get_size() < maxsize) {
1664 prio = prio - 5;1656 prio = prio - 5;
@@ -1671,22 +1663,22 @@
1671 // for expansion)1663 // for expansion)
1672 const int16_t bottom_treshold =1664 const int16_t bottom_treshold =
1673 15 - ((virtual_mines <= 4) ? (5 - virtual_mines) * 2 : 0);1665 15 - ((virtual_mines <= 4) ? (5 - virtual_mines) * 2 : 0);
1674 if (bf->enemy_nearby_ && bf->military_capacity_ < bottom_treshold) {1666 if (buildable_field->enemy_nearby_ && buildable_field->military_capacity_ < bottom_treshold) {
1675 prio += 50 + (bottom_treshold - bf->military_capacity_) * 20;1667 prio += 50 + (bottom_treshold - buildable_field->military_capacity_) * 20;
1676 }1668 }
16771669
1678 if (bf->enemy_nearby_ && bf->military_capacity_ > bottom_treshold + 4) {1670 if (buildable_field->enemy_nearby_ && buildable_field->military_capacity_ > bottom_treshold + 4) {
1679 prio -= (bf->military_capacity_ - (bottom_treshold + 4)) * 5;1671 prio -= (buildable_field->military_capacity_ - (bottom_treshold + 4)) * 5;
1680 }1672 }
16811673
1682 } else if (bo.type == BuildingObserver::WAREHOUSE) {1674 } else if (bo.type == BuildingObserver::WAREHOUSE) {
16831675
1684 // exclude spots on border1676 // exclude spots on border
1685 if (bf->near_border_ && !bo.is_port_) {1677 if (buildable_field->near_border_ && !bo.is_port_) {
1686 continue;1678 continue;
1687 }1679 }
16881680
1689 if (!bf->is_portspace_ && bo.is_port_) {1681 if (!buildable_field->is_portspace_ && bo.is_port_) {
1690 continue;1682 continue;
1691 }1683 }
16921684
@@ -1714,7 +1706,7 @@
17141706
1715 // special boost for first port1707 // special boost for first port
1716 if (bo.is_port_ && bo.total_count() == 0 && productionsites.size() > 5 &&1708 if (bo.is_port_ && bo.total_count() == 0 && productionsites.size() > 5 &&
1717 !bf->enemy_nearby_ && bf->is_portspace_ && seafaring_economy) {1709 !buildable_field->enemy_nearby_ && buildable_field->is_portspace_ && seafaring_economy) {
1718 prio += kDefaultPrioBoost + productionsites.size();1710 prio += kDefaultPrioBoost + productionsites.size();
1719 warehouse_needed = true;1711 warehouse_needed = true;
1720 }1712 }
@@ -1728,7 +1720,7 @@
1728 uint16_t nearest_distance = std::numeric_limits<uint16_t>::max();1720 uint16_t nearest_distance = std::numeric_limits<uint16_t>::max();
1729 for (const WarehouseSiteObserver& wh_obs : warehousesites) {1721 for (const WarehouseSiteObserver& wh_obs : warehousesites) {
1730 const uint16_t actual_distance =1722 const uint16_t actual_distance =
1731 map.calc_distance(bf->coords, wh_obs.site->get_position());1723 map.calc_distance(buildable_field->coords, wh_obs.site->get_position());
1732 nearest_distance = std::min(nearest_distance, actual_distance);1724 nearest_distance = std::min(nearest_distance, actual_distance);
1733 }1725 }
1734 //but limit to 151726 //but limit to 15
@@ -1737,11 +1729,11 @@
1737 prio += nearest_distance;1729 prio += nearest_distance;
17381730
1739 // take care about and enemies1731 // take care about and enemies
1740 if (bf->enemy_nearby_) {1732 if (buildable_field->enemy_nearby_) {
1741 prio /= 2;1733 prio /= 2;
1742 }1734 }
17431735
1744 if (bf->unowned_land_nearby_ && !bo.is_port_) {1736 if (buildable_field->unowned_land_nearby_ && !bo.is_port_) {
1745 prio /= 2;1737 prio /= 2;
1746 }1738 }
17471739
@@ -1752,7 +1744,7 @@
1752 }1744 }
17531745
1754 // exclude spots on border1746 // exclude spots on border
1755 if (bf->near_border_) {1747 if (buildable_field->near_border_) {
1756 continue;1748 continue;
1757 }1749 }
17581750
@@ -1767,17 +1759,17 @@
1767 }1759 }
17681760
1769 // take care about borders and enemies1761 // take care about borders and enemies
1770 if (bf->enemy_nearby_) {1762 if (buildable_field->enemy_nearby_) {
1771 prio /= 2;1763 prio /= 2;
1772 }1764 }
17731765
1774 if (bf->unowned_land_nearby_) {1766 if (buildable_field->unowned_land_nearby_) {
1775 prio /= 2;1767 prio /= 2;
1776 }1768 }
1777 }1769 }
17781770
1779 // think of space consuming buildings nearby like farms or vineyards1771 // think of space consuming buildings nearby like farms or vineyards
1780 prio -= bf->space_consumers_nearby_ * 10;1772 prio -= buildable_field->space_consumers_nearby_ * 10;
17811773
1782 // Stop here, if priority is 0 or less.1774 // Stop here, if priority is 0 or less.
1783 if (prio <= 0) {1775 if (prio <= 0) {
@@ -1786,25 +1778,25 @@
17861778
1787 // testing also vicinity1779 // testing also vicinity
1788 if (!bo.is_port_) {1780 if (!bo.is_port_) {
1789 if (port_reserved_coords.count(coords_hash(bf->coords)) > 0) {1781 if (port_reserved_coords.count(coords_hash(buildable_field->coords)) > 0) {
1790 continue;1782 continue;
1791 }1783 }
1792 }1784 }
17931785
1794 // Prefer road side fields1786 // Prefer road side fields
1795 prio += bf->preferred_ ? 1 : 0;1787 prio += buildable_field->preferred_ ? 1 : 0;
1796 // don't waste good land for small huts1788 // don't waste good land for small huts
1797 prio -= (maxsize - bo.desc->get_size()) * 5;1789 prio -= (maxsize - bo.desc->get_size()) * 5;
17981790
1799 // prefer vicinity of ports (with exemption of warehouses)1791 // prefer vicinity of ports (with exemption of warehouses)
1800 if (bf->port_nearby_ && bo.type == BuildingObserver::MILITARYSITE) {1792 if (buildable_field->port_nearby_ && bo.type == BuildingObserver::MILITARYSITE) {
1801 prio *= 2;1793 prio *= 2;
1802 }1794 }
18031795
1804 if (prio > proposed_priority) {1796 if (prio > proposed_priority) {
1805 best_building = &bo;1797 best_building = &bo;
1806 proposed_priority = prio;1798 proposed_priority = prio;
1807 proposed_coords = bf->coords;1799 proposed_coords = buildable_field->coords;
1808 }1800 }
1809 } // ending loop over buildings1801 } // ending loop over buildings
1810 } // ending loop over fields1802 } // ending loop over fields
@@ -1862,18 +1854,16 @@
1862 }1854 }
18631855
1864 // iterating over fields1856 // iterating over fields
1865 for (std::list<MineableField*>::iterator j = mineable_fields.begin();1857 for (const MineableField* mineable_field : mineable_fields) {
1866 j != mineable_fields.end();
1867 ++j) {
18681858
1869 if ((*j)->coords.field->get_resources() != bo.mines_) {1859 if (mineable_field->coords.field->get_resources() != bo.mines_) {
1870 continue;1860 continue;
1871 }1861 }
18721862
1873 int32_t prio = (*j)->coords.field->get_resources_amount();1863 int32_t prio = mineable_field->coords.field->get_resources_amount();
18741864
1875 // applying nearnes penalty1865 // applying nearnes penalty
1876 prio = prio - (*j)->mines_nearby_ * nearness_penalty;1866 prio = prio - mineable_field->mines_nearby_ * nearness_penalty;
18771867
1878 // Only build mines_ on locations where some material can be mined1868 // Only build mines_ on locations where some material can be mined
1879 if (prio < 2) {1869 if (prio < 2) {
@@ -1883,10 +1873,8 @@
1883 // Continue if field is blocked at the moment1873 // Continue if field is blocked at the moment
1884 bool blocked = false;1874 bool blocked = false;
18851875
1886 for (std::list<BlockedField>::iterator k = blocked_fields.begin();1876 for (const BlockedField& blocked_field :blocked_fields)
1887 k != blocked_fields.end();1877 if (mineable_field->coords == blocked_field.coords) {
1888 ++k)
1889 if ((*j)->coords == k->coords) {
1890 blocked = true;1878 blocked = true;
1891 break;1879 break;
1892 }1880 }
@@ -1897,13 +1885,13 @@
1897 }1885 }
18981886
1899 // Prefer road side fields1887 // Prefer road side fields
1900 prio += (*j)->preferred_ ? 1 : 0;1888 prio += mineable_field->preferred_ ? 1 : 0;
19011889
1902 if (prio > proposed_priority) {1890 if (prio > proposed_priority) {
1903 // proposed_building = bo.id;1891 // proposed_building = bo.id;
1904 best_building = &bo;1892 best_building = &bo;
1905 proposed_priority = prio;1893 proposed_priority = prio;
1906 proposed_coords = (*j)->coords;1894 proposed_coords = mineable_field->coords;
1907 mine = true;1895 mine = true;
1908 }1896 }
1909 } // end of evaluation of field1897 } // end of evaluation of field
@@ -2098,10 +2086,10 @@
2098 continue;2086 continue;
2099 }2087 }
21002088
2101 std::vector<NearFlag>::iterator f =2089 std::vector<NearFlag>::iterator near_flag_it =
2102 find(reachableflags.begin(), reachableflags.end(), queue.top().flag);2090 find(reachableflags.begin(), reachableflags.end(), queue.top().flag);
21032091
2104 if (f != reachableflags.end()) {2092 if (near_flag_it != reachableflags.end()) {
2105 queue.pop();2093 queue.pop();
2106 continue;2094 continue;
2107 }2095 }
@@ -2280,10 +2268,10 @@
22802268
2281 // algorithm to walk on roads2269 // algorithm to walk on roads
2282 while (!queue.empty()) {2270 while (!queue.empty()) {
2283 std::vector<NearFlag>::iterator f =2271 std::vector<NearFlag>::iterator near_flag_it =
2284 find(nearflags_tmp.begin(), nearflags_tmp.end(), queue.top().flag);2272 find(nearflags_tmp.begin(), nearflags_tmp.end(), queue.top().flag);
22852273
2286 if (f != nearflags_tmp.end()) {2274 if (near_flag_it != nearflags_tmp.end()) {
2287 queue.pop();2275 queue.pop();
2288 continue;2276 continue;
2289 }2277 }
@@ -2318,19 +2306,16 @@
2318 // iterating over nearflags_tmp, each item in nearflags_tmp should be contained also in nearflags2306 // iterating over nearflags_tmp, each item in nearflags_tmp should be contained also in nearflags
2319 // so for each corresponding field in nearflags we update "cost" (distance on existing roads)2307 // so for each corresponding field in nearflags we update "cost" (distance on existing roads)
2320 // to actual value2308 // to actual value
2321 for (std::vector<NearFlag>::iterator nf_walk_it = nearflags_tmp.begin();2309 for (const NearFlag& temp_near_flag : nearflags_tmp) {
2322 nf_walk_it != nearflags_tmp.end();2310 uint32_t const hash_walk = coords_hash(temp_near_flag.flag->get_position());
2323 ++nf_walk_it) {
2324 uint32_t const hash_walk = coords_hash(nf_walk_it->flag->get_position());
2325 if (lookuptable.count(hash_walk) > 0) {2311 if (lookuptable.count(hash_walk) > 0) {
2326 // iterating over nearflags2312 // iterating over nearflags
2327 for (std::vector<NearFlag>::iterator nf_it = nearflags.begin(); nf_it != nearflags.end();2313 for (NearFlag& near_flag : nearflags) {
2328 ++nf_it) {2314 uint32_t const hash = coords_hash(near_flag.flag->get_position());
2329 uint32_t const hash = coords_hash(nf_it->flag->get_position());
2330 if (hash == hash_walk) {2315 if (hash == hash_walk) {
2331 // decreasing "cost" (of walking via roads)2316 // decreasing "cost" (of walking via roads)
2332 if (nf_it->cost_ > nf_walk_it->cost_) {2317 if (near_flag.cost_ > temp_near_flag.cost_) {
2333 nf_it->cost_ = nf_walk_it->cost_;2318 near_flag.cost_ = temp_near_flag.cost_;
2334 }2319 }
2335 }2320 }
2336 }2321 }
@@ -2403,26 +2388,21 @@
2403 get_economy_observer(flag.economy())->flags.push_back(&flag);2388 get_economy_observer(flag.economy())->flags.push_back(&flag);
2404 }2389 }
24052390
2406 for (std::list<EconomyObserver*>::iterator obs_iter = economies.begin();2391 for (EconomyObserver* economy_observer : economies) {
2407 obs_iter != economies.end();
2408 ++obs_iter) {
2409 // check if any flag has changed its economy2392 // check if any flag has changed its economy
2410 std::list<Flag const*>& fl = (*obs_iter)->flags;2393 std::list<Flag const*>& fl = economy_observer->flags;
24112394
2412 for (std::list<Flag const*>::iterator j = fl.begin(); j != fl.end();) {2395 for (const Flag* flag : fl) {
2413 if (&(*obs_iter)->economy != &(*j)->economy()) {2396 if (&economy_observer->economy != &flag->economy()) {
2414 get_economy_observer((*j)->economy())->flags.push_back(*j);2397 get_economy_observer(flag->economy())->flags.push_back(flag);
2415 j = fl.erase(j);2398 fl.remove(flag);
2416 } else {
2417 ++j;
2418 }2399 }
2419 }2400 }
24202401
2421 // if there are no more flags in this economy,2402 // if there are no more flags in this economy,
2422 // we no longer need it's observer2403 // we no longer need it's observer
2423 if ((*obs_iter)->flags.empty()) {2404 if (economy_observer->flags.empty()) {
2424 delete *obs_iter;2405 economies.remove(economy_observer);
2425 economies.erase(obs_iter);
2426 return true;2406 return true;
2427 }2407 }
2428 }2408 }
@@ -2800,20 +2780,19 @@
2800 }2780 }
28012781
2802 // and now over ships2782 // and now over ships
2803 for (std::list<ShipObserver>::iterator sp_iter = allships.begin(); sp_iter != allships.end();2783 for (const ShipObserver& ship_observer : allships) {
2804 ++sp_iter) {2784 if (ship_observer.ship->state_is_expedition()) {
2805 if (sp_iter->ship->state_is_expedition()) {
2806 expeditions_in_progress += 1;2785 expeditions_in_progress += 1;
2807 }2786 }
2808 }2787 }
28092788
2810 // we must verify that all remote ports are still ours (and exists at all)2789 // we must verify that all remote ports are still ours (and exists at all)
2811 bool still_ours;2790 bool still_ours;
2812 for (std::unordered_set<uint32_t>::iterator ports_iter = remote_ports_coords.begin();2791 for (std::unordered_set<uint32_t>::iterator ports_it = remote_ports_coords.begin();
2813 ports_iter != remote_ports_coords.end();2792 ports_it != remote_ports_coords.end();
2814 ++ports_iter) {2793 ++ports_it) {
2815 still_ours = false;2794 still_ours = false;
2816 FCoords fcoords = game().map().get_fcoords(coords_unhash(*ports_iter));2795 FCoords fcoords = game().map().get_fcoords(coords_unhash(*ports_it));
2817 if (fcoords.field->get_owned_by() == player_number()) {2796 if (fcoords.field->get_owned_by() == player_number()) {
2818 if (upcast(PlayerImmovable, imm, fcoords.field->get_immovable())) {2797 if (upcast(PlayerImmovable, imm, fcoords.field->get_immovable())) {
2819 still_ours = true;2798 still_ours = true;
@@ -2821,7 +2800,7 @@
2821 }2800 }
28222801
2823 if (!still_ours) {2802 if (!still_ours) {
2824 remote_ports_coords.erase(*ports_iter);2803 remote_ports_coords.erase(*ports_it);
2825 break;2804 break;
2826 }2805 }
2827 }2806 }
@@ -2892,26 +2871,25 @@
2892 }2871 }
28932872
2894 if (!allships.empty()) {2873 if (!allships.empty()) {
2895 // iterating over ships and executing what is needed2874 // Iterating over ships and executing what is needed.
2896 for (std::list<ShipObserver>::iterator i = allships.begin(); i != allships.end(); ++i) {2875 for (ShipObserver& ship_observer : allships) {
28972876 // Only two states need attention.
2898 // only two states need an attention2877 if ((ship_observer.ship->get_ship_state() == Widelands::Ship::EXP_WAITING ||
2899 if ((i->ship->get_ship_state() == Widelands::Ship::EXP_WAITING ||2878 ship_observer.ship->get_ship_state() == Widelands::Ship::EXP_FOUNDPORTSPACE) &&
2900 i->ship->get_ship_state() == Widelands::Ship::EXP_FOUNDPORTSPACE) &&2879 !ship_observer.waiting_for_command_) {
2901 !i->waiting_for_command_) {2880 if (gametime - ship_observer.last_command_time > 180 * 1000) {
2902 if (gametime - i->last_command_time > 180 * 1000) {2881 ship_observer.waiting_for_command_ = true;
2903 i->waiting_for_command_ = true;
2904 log(" %1d: last command for ship at %3dx%3d was %3d seconds ago, something wrong "2882 log(" %1d: last command for ship at %3dx%3d was %3d seconds ago, something wrong "
2905 "here?...\n",2883 "here?...\n",
2906 player_number(),2884 player_number(),
2907 i->ship->get_position().x,2885 ship_observer.ship->get_position().x,
2908 i->ship->get_position().y,2886 ship_observer.ship->get_position().y,
2909 (gametime - i->last_command_time) / 1000);2887 (gametime - ship_observer.last_command_time) / 1000);
2910 }2888 }
2911 }2889 }
2912 // if ships is waiting for command2890 // If ship is waiting for command.
2913 if (i->waiting_for_command_) {2891 if (ship_observer.waiting_for_command_) {
2914 expedition_management(*i);2892 expedition_management(ship_observer);
2915 }2893 }
2916 }2894 }
2917 }2895 }
@@ -2920,33 +2898,27 @@
2920 while (!marineTaskQueue_.empty()) {2898 while (!marineTaskQueue_.empty()) {
2921 if (marineTaskQueue_.back() == kStopShipyard) {2899 if (marineTaskQueue_.back() == kStopShipyard) {
2922 // iterate over all production sites searching for shipyard2900 // iterate over all production sites searching for shipyard
2923 for (std::list<ProductionSiteObserver>::iterator site = productionsites.begin();2901 for (const ProductionSiteObserver& prod_site_observer : productionsites) {
2924 site != productionsites.end();2902 if (prod_site_observer.bo->is_shipyard_) {
2925 ++site) {2903 if (!prod_site_observer.site->is_stopped()) {
2926 if (site->bo->is_shipyard_) {2904 game().send_player_start_stop_building(*prod_site_observer.site);
2927 if (!site->site->is_stopped()) {
2928 game().send_player_start_stop_building(*site->site);
2929 }2905 }
2930 }2906 }
2931 }2907 }
2932 }2908 }
29332909
2934 if (marineTaskQueue_.back() == kReprioritize) {2910 if (marineTaskQueue_.back() == kReprioritize) {
2935 for (std::list<ProductionSiteObserver>::iterator site = productionsites.begin();2911 for (const ProductionSiteObserver& prod_site_observer : productionsites) {
2936 site != productionsites.end();2912 if (prod_site_observer.bo->is_shipyard_) {
2937 ++site) {2913 for (uint32_t k = 0; k < prod_site_observer.bo->inputs_.size(); ++k) {
2938 if (site->bo->is_shipyard_) {
2939 for (uint32_t k = 0; k < site->bo->inputs_.size(); ++k) {
2940 game().send_player_set_ware_priority(2914 game().send_player_set_ware_priority(
2941 *site->site, wwWARE, site->bo->inputs_.at(k), HIGH_PRIORITY);2915 *prod_site_observer.site, wwWARE, prod_site_observer.bo->inputs_.at(k), HIGH_PRIORITY);
2942 }2916 }
2943 }2917 }
2944 }2918 }
2945 }2919 }
2946
2947 marineTaskQueue_.pop_back();2920 marineTaskQueue_.pop_back();
2948 }2921 }
2949
2950 return true;2922 return true;
2951}2923}
29522924
@@ -3127,13 +3099,9 @@
3127// perhaps it will be able to replace get_stocklevel3099// perhaps it will be able to replace get_stocklevel
3128uint32_t DefaultAI::get_warehoused_stock(WareIndex wt) {3100uint32_t DefaultAI::get_warehoused_stock(WareIndex wt) {
3129 uint32_t count = 0;3101 uint32_t count = 0;
31303102 for (const WarehouseSiteObserver& warehouse_observer : warehousesites) {
3131 for (std::list<WarehouseSiteObserver>::iterator i = warehousesites.begin();3103 count += warehouse_observer.site->get_wares().stock(wt);
3132 i != warehousesites.end();
3133 ++i) {
3134 count += i->site->get_wares().stock(wt);
3135 }3104 }
3136
3137 return count;3105 return count;
3138}3106}
31393107
@@ -3155,13 +3123,10 @@
3155 } else {3123 } else {
3156 new_priority = DEFAULT_PRIORITY;3124 new_priority = DEFAULT_PRIORITY;
3157 }3125 }
3158 for (std::list<TrainingSiteObserver>::iterator site = trainingsites.begin();3126 for (const TrainingSiteObserver& trainingsite_observer :trainingsites) {
3159 site != trainingsites.end();3127 for (uint32_t k = 0; k < trainingsite_observer.bo->inputs_.size(); ++k) {
3160 ++site) {
3161
3162 for (uint32_t k = 0; k < site->bo->inputs_.size(); ++k) {
3163 game().send_player_set_ware_priority(3128 game().send_player_set_ware_priority(
3164 *site->site, wwWARE, site->bo->inputs_.at(k), new_priority);3129 *trainingsite_observer.site, wwWARE, trainingsite_observer.bo->inputs_.at(k), new_priority);
3165 }3130 }
3166 }3131 }
3167 return true;3132 return true;
@@ -3186,10 +3151,8 @@
3186 // construction3151 // construction
3187 unstationed_milit_buildings_ = 0;3152 unstationed_milit_buildings_ = 0;
31883153
3189 for (std::list<MilitarySiteObserver>::iterator it = militarysites.begin();3154 for (const MilitarySiteObserver& militarysite_observer : militarysites) {
3190 it != militarysites.end();3155 if (militarysite_observer.site->stationed_soldiers().size() == 0) {
3191 ++it) {
3192 if (it->site->stationed_soldiers().size() == 0) {
3193 unstationed_milit_buildings_ += 1;3156 unstationed_milit_buildings_ += 1;
3194 }3157 }
3195 }3158 }
@@ -3387,9 +3350,11 @@
33873350
3388/// \returns the economy observer containing \arg economy3351/// \returns the economy observer containing \arg economy
3389EconomyObserver* DefaultAI::get_economy_observer(Economy& economy) {3352EconomyObserver* DefaultAI::get_economy_observer(Economy& economy) {
3390 for (std::list<EconomyObserver*>::iterator i = economies.begin(); i != economies.end(); ++i)3353 for (EconomyObserver* economy_observer : economies) {
3391 if (&(*i)->economy == &economy)3354 if (&economy_observer->economy == &economy) {
3392 return *i;3355 return economy_observer;
3356 }
3357 }
33933358
3394 economies.push_front(new EconomyObserver(economy));3359 economies.push_front(new EconomyObserver(economy));
3395 return economies.front();3360 return economies.front();
@@ -3427,20 +3392,16 @@
3427 lose_building(*building);3392 lose_building(*building);
3428 } else if (upcast(Flag const, flag, &pi)) {3393 } else if (upcast(Flag const, flag, &pi)) {
3429 for (EconomyObserver* eco_obs : economies) {3394 for (EconomyObserver* eco_obs : economies) {
3430 for (std::list<Flag const*>::iterator flag_iter = eco_obs->flags.begin();3395 for (const Flag* economy_flag : eco_obs->flags) {
3431 flag_iter != eco_obs->flags.end();3396 if (economy_flag == flag) {
3432 ++flag_iter) {3397 eco_obs->flags.remove(economy_flag);
3433 if (*flag_iter == flag) {
3434 eco_obs->flags.erase(flag_iter);
3435 return;3398 return;
3436 }3399 }
3437 }3400 }
3438 }3401 }
3439 for (std::list<Flag const*>::iterator flag_iter = new_flags.begin();3402 for (const Flag* new_flag : new_flags) {
3440 flag_iter != new_flags.end();3403 if (new_flag == flag) {
3441 ++flag_iter) {3404 new_flags.remove(new_flag);
3442 if (*flag_iter == flag) {
3443 new_flags.erase(flag_iter);
3444 return;3405 return;
3445 }3406 }
3446 }3407 }
@@ -3453,9 +3414,9 @@
3453void DefaultAI::out_of_resources_site(const ProductionSite& site) {3414void DefaultAI::out_of_resources_site(const ProductionSite& site) {
34543415
3455 // we must identify which mine matches the productionsite a note reffers to3416 // we must identify which mine matches the productionsite a note reffers to
3456 for (std::list<ProductionSiteObserver>::iterator i = mines_.begin(); i != mines_.end(); ++i)3417 for (ProductionSiteObserver& mine_observer : mines_)
3457 if (i->site == &site) {3418 if (mine_observer.site == &site) {
3458 i->no_resources_count += 1;3419 mine_observer.no_resources_count += 1;
3459 break;3420 break;
3460 }3421 }
3461}3422}
@@ -3752,11 +3713,11 @@
37523713
3753 if (bo.type == BuildingObserver::PRODUCTIONSITE) {3714 if (bo.type == BuildingObserver::PRODUCTIONSITE) {
37543715
3755 for (std::list<ProductionSiteObserver>::iterator i = productionsites.begin();3716 for (std::list<ProductionSiteObserver>::iterator prod_site_observer_it = productionsites.begin();
3756 i != productionsites.end();3717 prod_site_observer_it != productionsites.end();
3757 ++i)3718 ++prod_site_observer_it)
3758 if (i->site == &b) {3719 if (prod_site_observer_it->site == &b) {
3759 productionsites.erase(i);3720 productionsites.erase(prod_site_observer_it);
3760 break;3721 break;
3761 }3722 }
37623723
@@ -3768,10 +3729,10 @@
3768 --wares.at(bo.inputs_.at(i)).consumers_;3729 --wares.at(bo.inputs_.at(i)).consumers_;
3769 }3730 }
3770 } else if (bo.type == BuildingObserver::MINE) {3731 } else if (bo.type == BuildingObserver::MINE) {
3771 for (std::list<ProductionSiteObserver>::iterator i = mines_.begin(); i != mines_.end();3732 for (std::list<ProductionSiteObserver>::iterator mines_observer_it = mines_.begin(); mines_observer_it != mines_.end();
3772 ++i) {3733 ++mines_observer_it) {
3773 if (i->site == &b) {3734 if (mines_observer_it->site == &b) {
3774 mines_.erase(i);3735 mines_.erase(mines_observer_it);
3775 break;3736 break;
3776 }3737 }
3777 }3738 }
@@ -3785,21 +3746,21 @@
3785 }3746 }
3786 } else if (bo.type == BuildingObserver::MILITARYSITE) {3747 } else if (bo.type == BuildingObserver::MILITARYSITE) {
37873748
3788 for (std::list<MilitarySiteObserver>::iterator i = militarysites.begin();3749 for (std::list<MilitarySiteObserver>::iterator militarysite_observer_it = militarysites.begin();
3789 i != militarysites.end();3750 militarysite_observer_it != militarysites.end();
3790 ++i) {3751 ++militarysite_observer_it) {
3791 if (i->site == &b) {3752 if (militarysite_observer_it->site == &b) {
3792 militarysites.erase(i);3753 militarysites.erase(militarysite_observer_it);
3793 break;3754 break;
3794 }3755 }
3795 }3756 }
3796 } else if (bo.type == BuildingObserver::TRAININGSITE) {3757 } else if (bo.type == BuildingObserver::TRAININGSITE) {
37973758
3798 for (std::list<TrainingSiteObserver>::iterator i = trainingsites.begin();3759 for (std::list<TrainingSiteObserver>::iterator trainingsite_observer_it = trainingsites.begin();
3799 i != trainingsites.end();3760 trainingsite_observer_it != trainingsites.end();
3800 ++i) {3761 ++trainingsite_observer_it) {
3801 if (i->site == &b) {3762 if (trainingsite_observer_it->site == &b) {
3802 trainingsites.erase(i);3763 trainingsites.erase(trainingsite_observer_it);
3803 break;3764 break;
3804 }3765 }
3805 }3766 }
@@ -3810,11 +3771,11 @@
3810 --num_ports;3771 --num_ports;
3811 }3772 }
38123773
3813 for (std::list<WarehouseSiteObserver>::iterator i = warehousesites.begin();3774 for (std::list<WarehouseSiteObserver>::iterator warehouse_observer_it = warehousesites.begin();
3814 i != warehousesites.end();3775 warehouse_observer_it != warehousesites.end();
3815 ++i) {3776 ++warehouse_observer_it) {
3816 if (i->site == &b) {3777 if (warehouse_observer_it->site == &b) {
3817 warehousesites.erase(i);3778 warehousesites.erase(warehouse_observer_it);
3818 break;3779 break;
3819 }3780 }
3820 }3781 }
@@ -3982,12 +3943,12 @@
3982 for (uint32_t position = gametime % test_every; position < militarysites.size();3943 for (uint32_t position = gametime % test_every; position < militarysites.size();
3983 position += test_every) {3944 position += test_every) {
39843945
3985 std::list<MilitarySiteObserver>::iterator mso = militarysites.begin();3946 std::list<MilitarySiteObserver>::iterator militarysite_observer_it = militarysites.begin();
3986 std::advance(mso, position);3947 std::advance(militarysite_observer_it, position);
39873948
3988 MilitarySite* ms = mso->site;3949 MilitarySite* ms = militarysite_observer_it->site;
39893950
3990 if (!mso->enemies_nearby_) {3951 if (!militarysite_observer_it->enemies_nearby_) {
3991 continue;3952 continue;
3992 }3953 }
39933954

Subscribers

People subscribed via source and target branches

to status/vote changes: