MCPcopy Create free account
hub / github.com/DFHack/dfhack / getFreeNestboxZones

Function getFreeNestboxZones

plugins/autonestbox.cpp:247–293  ·  view source on GitHub ↗

also assigns units to zone if they have already claimed the nestbox and are not assigned elsewhere; returns number assigned

Source from the content-addressed store, hash-verified

245// also assigns units to zone if they have already claimed the nestbox
246// and are not assigned elsewhere; returns number assigned
247static 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
295static vector<df::unit *> getFreeEggLayers(color_ostream &out) {
296 vector<df::unit *> ret;

Callers 1

assign_nestboxesFunction · 0.85

Calls 7

isEmptyPastureFunction · 0.85
isFreeEgglayerFunction · 0.85
assignUnitToZoneFunction · 0.85
getTypeMethod · 0.80
findFunction · 0.50
printMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected