Fix UNIT_NEW_ACTIVE events (#2197)

* Updates eventful.lua to use UNIT_NEW_ACTIVE

* Fixes bug #2189

* Revises activeUnits declaration/initialization

* Fixes build error

* Update changelog.txt

* reword changelog entry

* add changelog entry for event name change

Co-authored-by: Myk <myk002@yahoo.com>
develop
Josh Cooper 2022-06-17 09:34:41 -07:00 committed by GitHub
parent ae5b00523e
commit d38ab1d152
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 2 deletions

@ -38,6 +38,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences:
## New Tweaks ## New Tweaks
## Fixes ## Fixes
- `eventful`: fixed UNIT_NEW_ACTIVE event firing too often
- ``widgets.CycleHotkeyLabel``: allow initial option values to be specified as an index instead of an option value - ``widgets.CycleHotkeyLabel``: allow initial option values to be specified as an index instead of an option value
- ``job.removeJob()``: fixes regression in DFHack 0.47.05-r5 where items/buildings associated with the job were not getting disassociated when the job is removed. Now `build-now` can build buildings and `gui/mass-remove` can cancel building deconstruction again - ``job.removeJob()``: fixes regression in DFHack 0.47.05-r5 where items/buildings associated with the job were not getting disassociated when the job is removed. Now `build-now` can build buildings and `gui/mass-remove` can cancel building deconstruction again
- `eventful`: fix ``eventful.registerReaction`` to correctly pass ``call_native`` argument thus allowing canceling vanilla item creation. Updated related documentation. - `eventful`: fix ``eventful.registerReaction`` to correctly pass ``call_native`` argument thus allowing canceling vanilla item creation. Updated related documentation.
@ -49,6 +50,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences:
- `dfhack-examples-guide`: reduce required stock of dye for "Dye cloth" orders - `dfhack-examples-guide`: reduce required stock of dye for "Dye cloth" orders
- `dfhack-examples-guide`: fix material conditions for making jugs and pots - `dfhack-examples-guide`: fix material conditions for making jugs and pots
- `dfhack-examples-guide`: make wooden jugs by default to differentiate them from other stone tools. this allows players to more easily select jugs out with a properly-configured stockpile (i.e. the new ``woodentools`` alias) - `dfhack-examples-guide`: make wooden jugs by default to differentiate them from other stone tools. this allows players to more easily select jugs out with a properly-configured stockpile (i.e. the new ``woodentools`` alias)
- `eventful`: renamed NEW_UNIT_ACTIVE event to UNIT_NEW_ACTIVE to match the ``EventManager`` event name
- `quickfort-alias-guide`: new aliases: ``forbidsearch``, ``permitsearch``, and ``togglesearch`` use the `search-plugin` plugin to alter the settings for a filtered list of item types when configuring stockpiles - `quickfort-alias-guide`: new aliases: ``forbidsearch``, ``permitsearch``, and ``togglesearch`` use the `search-plugin` plugin to alter the settings for a filtered list of item types when configuring stockpiles
- `quickfort-alias-guide`: new aliases: ``stonetools`` and ``woodentools``. the ``jugs`` alias is deprecated. please use ``stonetools`` instead, which is the same as the old ``jugs`` alias. - `quickfort-alias-guide`: new aliases: ``stonetools`` and ``woodentools``. the ``jugs`` alias is deprecated. please use ``stonetools`` instead, which is the same as the old ``jugs`` alias.
- `quickfort-alias-guide`: new aliases: ``usablehair``, ``permitusablehair``, and ``forbidusablehair`` alter settings for the types of hair/wool that can be made into cloth: sheep, llama, alpaca, and troll. The ``craftrefuse`` aliases have been altered to use this alias as well. - `quickfort-alias-guide`: new aliases: ``usablehair``, ``permitusablehair``, and ``forbidusablehair`` alter settings for the types of hair/wool that can be made into cloth: sheep, llama, alpaca, and troll. The ``craftrefuse`` aliases have been altered to use this alias as well.

@ -199,6 +199,9 @@ static int32_t lastJobId = -1;
//job completed //job completed
static unordered_map<int32_t, df::job*> prevJobs; static unordered_map<int32_t, df::job*> prevJobs;
//active units
static unordered_set<int32_t> activeUnits;
//unit death //unit death
static unordered_set<int32_t> livingUnits; static unordered_set<int32_t> livingUnits;
@ -256,6 +259,7 @@ void DFHack::EventManager::onStateChange(color_ostream& out, state_change_event
buildings.clear(); buildings.clear();
constructions.clear(); constructions.clear();
equipmentLog.clear(); equipmentLog.clear();
activeUnits.clear();
Buildings::clearBuildings(out); Buildings::clearBuildings(out);
lastReport = -1; lastReport = -1;
@ -314,6 +318,9 @@ void DFHack::EventManager::onStateChange(color_ostream& out, state_change_event
} }
lastSyndromeTime = -1; lastSyndromeTime = -1;
for (auto unit : df::global::world->units.all) { for (auto unit : df::global::world->units.all) {
if (Units::isActive(unit)) {
activeUnits.emplace(unit->id);
}
for (auto syndrome : unit->syndromes.active) { for (auto syndrome : unit->syndromes.active) {
int32_t startTime = syndrome->year*ticksPerYear + syndrome->year_time; int32_t startTime = syndrome->year*ticksPerYear + syndrome->year_time;
if ( startTime > lastSyndromeTime ) if ( startTime > lastSyndromeTime )
@ -592,7 +599,6 @@ static void manageNewUnitActiveEvent(color_ostream& out) {
if (!df::global::world) if (!df::global::world)
return; return;
static unordered_set<int32_t> activeUnits;
multimap<Plugin*,EventHandler> copy(handlers[EventType::UNIT_NEW_ACTIVE].begin(), handlers[EventType::UNIT_NEW_ACTIVE].end()); multimap<Plugin*,EventHandler> copy(handlers[EventType::UNIT_NEW_ACTIVE].begin(), handlers[EventType::UNIT_NEW_ACTIVE].end());
// iterate event handler callbacks // iterate event handler callbacks
for (auto &key_value : copy) { for (auto &key_value : copy) {
@ -600,6 +606,7 @@ static void manageNewUnitActiveEvent(color_ostream& out) {
for (df::unit* unit : df::global::world->units.active) { for (df::unit* unit : df::global::world->units.active) {
int32_t id = unit->id; int32_t id = unit->id;
if (!activeUnits.count(id)) { if (!activeUnits.count(id)) {
activeUnits.emplace(id);
handler.eventHandler(out, (void*) intptr_t(id)); // intptr_t() avoids cast from smaller type warning handler.eventHandler(out, (void*) intptr_t(id)); // intptr_t() avoids cast from smaller type warning
} }
} }

@ -154,7 +154,7 @@ eventType=invertTable{
"JOB_INITIATED", "JOB_INITIATED",
"JOB_STARTED", "JOB_STARTED",
"JOB_COMPLETED", "JOB_COMPLETED",
"NEW_UNIT_ACTIVE", "UNIT_NEW_ACTIVE",
"UNIT_DEATH", "UNIT_DEATH",
"ITEM_CREATED", "ITEM_CREATED",
"BUILDING", "BUILDING",