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

Function impregnateMany

plugins/pet-uncapper.cpp:56–125  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

54}
55
56void impregnateMany(color_ostream &out, bool verbose = false) {
57 // mark that we have recently run
58 cycle_timestamp = world->frame_counter;
59
60 map<int32_t, vector<df::unit *>> males;
61 map<int32_t, vector<df::unit *>> females;
62 map<int32_t, int32_t> popcount;
63
64 std::random_device seed;
65 std::mt19937 gen{seed()};
66
67 const int popcap = config.get_int(CONFIG_POP_CAP);
68 int pregnancies = 0;
69
70 for (auto unit : world->units.active) {
71 // not restricted to fort pets since merchant/wild animals can participate
72 if (!Units::isActive(unit) || unit->flags1.bits.active_invader || unit->flags2.bits.underworld || unit->flags2.bits.visitor_uninvited || unit->flags2.bits.visitor)
73 continue;
74 popcount[unit->race]++;
75 if (unit->pregnancy_genes) {
76 // already pregnant -- if remaining time is less than the current setting, speed it up
77 if ((int)unit->pregnancy_timer > config.get_int(CONFIG_PREG_TIME))
78 unit->pregnancy_timer = config.get_int(CONFIG_PREG_TIME);
79 // for player convenience and population stability, count the fetus toward the population cap
80 popcount[unit->race]++;
81 continue;
82 }
83 if (unit->flags1.bits.caged)
84 continue;
85 // must have PET or PET_EXOTIC
86 if (!Units::isTamable(unit))
87 continue;
88 // check for adulthood
89 if (Units::isBaby(unit) || Units::isChild(unit))
90 continue;
91 if (Units::isMale(unit))
92 males[unit->race].push_back(unit);
93 else if (Units::isFemale(unit))
94 females[unit->race].push_back(unit);
95 }
96
97 for (auto [race, femalesList] : females) {
98 if (!males.contains(race))
99 continue;
100
101 for (auto female : femalesList) {
102 if (popcap > 0 && popcount[race] >= popcap)
103 break;
104
105 vector<df::unit *> compatibles;
106 for (auto male : males[race]) {
107 if (Maps::canWalkBetween(female->pos, male->pos) )
108 compatibles.push_back(male);
109 }
110 if (compatibles.empty())
111 continue;
112
113 std::uniform_int_distribution<> dist{0, (int)compatibles.size() - 1};

Callers 3

do_commandFunction · 0.85

Calls 5

impregnateFunction · 0.85
containsMethod · 0.80
emptyMethod · 0.45
sizeMethod · 0.45
printMethod · 0.45

Tested by

no test coverage detected