also assigns units to zone if they have already claimed the nestbox and are not assigned elsewhere; returns number assigned
| 245 | // also assigns units to zone if they have already claimed the nestbox |
| 246 | // and are not assigned elsewhere; returns number assigned |
| 247 | static size_t getFreeNestboxZones(color_ostream &out, vector<df::building_civzonest *> &free_zones) { |
| 248 | size_t assigned = 0; |
| 249 | for (auto zone : world->buildings.other.ZONE_PEN) { |
| 250 | TRACE(cycle,out).print("scanning pasture {} ({})\n", zone->id, zone->name); |
| 251 | if (!Buildings::isActive(zone)) { |
| 252 | TRACE(cycle,out).print("pasture {} is inactive\n", zone->id); |
| 253 | continue; |
| 254 | } |
| 255 | if (!isEmptyPasture(zone)) { |
| 256 | TRACE(cycle,out).print("pasture {} is not empty\n", zone->id); |
| 257 | continue; |
| 258 | } |
| 259 | |
| 260 | // nestbox must be in upper left corner |
| 261 | // this could be made more flexible |
| 262 | df::coord pos(zone->x1, zone->y1, zone->z); |
| 263 | auto bld = Buildings::findAtTile(pos); |
| 264 | if (!bld || bld->getType() != df::building_type::NestBox) { |
| 265 | TRACE(cycle,out).print("pasture {} does not have nestbox in upper left corner\n", zone->id); |
| 266 | continue; |
| 267 | } |
| 268 | TRACE(cycle,out).print("found nestbox {} in pasture {}\n", bld->id, zone->id); |
| 269 | |
| 270 | df::building_nest_boxst *nestbox = virtual_cast<df::building_nest_boxst>(bld); |
| 271 | if (!nestbox) { |
| 272 | TRACE(cycle,out).print("nestbox {} is somehow not a nestbox\n", bld->id); |
| 273 | continue; |
| 274 | } |
| 275 | |
| 276 | if (nestbox->claimed_by >= 0) { |
| 277 | if (auto unit = df::unit::find(nestbox->claimed_by)) { |
| 278 | TRACE(cycle,out).print("nestbox {} is claimed by unit {} ({})\n", bld->id, |
| 279 | nestbox->claimed_by, Units::getReadableName(unit)); |
| 280 | if (!isFreeEgglayer(unit)) { |
| 281 | DEBUG(cycle,out).print("cannot assign unit {} to nestbox {}: not a free egg layer\n", unit->id, bld->id); |
| 282 | } else { |
| 283 | // if the nestbox is claimed by a free egg layer, attempt to assign that unit to the zone |
| 284 | if (assignUnitToZone(out, unit, zone)) |
| 285 | ++assigned; |
| 286 | } |
| 287 | } |
| 288 | } else if (nestbox->contained_items.size() == 1) { |
| 289 | free_zones.push_back(zone); |
| 290 | } |
| 291 | } |
| 292 | return assigned; |
| 293 | } |
| 294 | |
| 295 | static vector<df::unit *> getFreeEggLayers(color_ostream &out) { |
| 296 | vector<df::unit *> ret; |
no test coverage detected