| 311 | } |
| 312 | |
| 313 | static void assign_nobles(color_ostream &out) { |
| 314 | for (auto &[zone_id, group_codes] : noble_zones) { |
| 315 | auto zone = virtual_cast<df::building_civzonest>(df::building::find(zone_id)); |
| 316 | if (!zone) |
| 317 | continue; |
| 318 | auto owner = Buildings::getOwner(zone); |
| 319 | bool assigned = false; |
| 320 | for (auto it = group_codes.rbegin(); it != group_codes.rend(); it++) { |
| 321 | auto code = *it; |
| 322 | vector<df::unit *> units; |
| 323 | Units::getUnitsByNobleRole(units, code); |
| 324 | // if zone is already assigned to a proper unit (or their spouse), skip |
| 325 | if (linear_index(units, owner) >= 0 || |
| 326 | linear_index(get_spouse_ids(out, units), zone->assigned_unit_id) >= 0) |
| 327 | { |
| 328 | assigned = true; |
| 329 | break; |
| 330 | } |
| 331 | // assign to a relevant noble that does not already have a registered zone of this type assigned |
| 332 | for (auto unit : units) { |
| 333 | if (!Units::isCitizen(unit, true)) |
| 334 | continue; |
| 335 | bool found = false; |
| 336 | for (auto owned_zone : unit->owned_buildings) { |
| 337 | if (owned_zone->type == zone->type && is_noble_zone(owned_zone->id, group_codes[0])) { |
| 338 | found = true; |
| 339 | break; |
| 340 | } |
| 341 | } |
| 342 | if (found) |
| 343 | continue; |
| 344 | zone->spec_sub_flag.bits.active = true; |
| 345 | Buildings::setOwner(zone, unit); |
| 346 | assigned = true; |
| 347 | INFO(cycle,out).print("preserve-rooms: assigning {} to a {}-associated {}\n", |
| 348 | DF2CONSOLE(Units::getReadableName(unit)), |
| 349 | toLower_cp437(code), |
| 350 | ENUM_KEY_STR(civzone_type, zone->type)); |
| 351 | break; |
| 352 | } |
| 353 | if (assigned) |
| 354 | break; |
| 355 | } |
| 356 | if (!assigned && (zone->spec_sub_flag.bits.active || zone->assigned_unit_id != -1)) { |
| 357 | DEBUG(cycle,out).print("noble zone now reserved for eventual office holder: {}\n", zone_id); |
| 358 | zone->spec_sub_flag.bits.active = false; |
| 359 | Buildings::setOwner(zone, NULL); |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | static bool scrub_id_from_entries(int32_t id, int32_t key, unordered_map<int32_t, vector<int32_t>> & entries) { |
| 365 | auto it = entries.find(key); |
no test coverage detected