Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
735264d
Adds event_tracker class for helping enforce event frequencies
cppcooper Mar 23, 2022
267df4f
Updates 4 event managers to send all events to handlers
cppcooper Mar 23, 2022
3df7848
Removes second buildings list initializer
cppcooper Mar 23, 2022
d2bfae2
Separates buildings list into created/destroyed lists
cppcooper Mar 23, 2022
7b7d46b
Adds CREATED_/DESTROYED_ building events while retaining BUILDING event
cppcooper Mar 23, 2022
b57a48a
Adds missing event manager prototypes
cppcooper Mar 23, 2022
8ea5c57
Adds plugin devel/event-testing
cppcooper Mar 24, 2022
950854f
Adds todo comment for BUILDING event (needs to be deprecated)
cppcooper Mar 24, 2022
b9ba42e
Adds missing return to event_tracker::erase(iterator)
cppcooper Mar 24, 2022
a010914
Implements several clion suggestions (foreach loops mostly)
cppcooper Mar 24, 2022
311ba9c
Updates job event managers
cppcooper Mar 24, 2022
2e1f42a
Updates all event managers with boiler plate frequency related code
cppcooper Mar 24, 2022
07c2c3e
Increases event-testing delay to ensure multiple events should send
cppcooper Mar 26, 2022
3495c73
Fixes bug in storing last tick values for event handlers
cppcooper Mar 26, 2022
47b5754
Revises foreach naming
cppcooper Mar 26, 2022
ab47f27
Moves tickQueue insertion/logic into a separate TU function
cppcooper Mar 26, 2022
5c52088
Rewrites manageTickEvent, simplifies, adds comments
cppcooper Mar 26, 2022
3fb1954
Fixes manageJobCompletedEvent
cppcooper Mar 26, 2022
d858837
Sets the eventLastTick for new registrants to the `current tick - 1`
cppcooper Mar 26, 2022
04f102b
Removes debug print line from manageJobCompletedEvent
cppcooper Mar 26, 2022
52f6e93
Fixes bug in manageBuildingEvent
cppcooper Mar 26, 2022
4c55d2d
Updates manageJobInitiatedEvent to send all events to handlers
cppcooper Mar 26, 2022
3af9892
Adds missing unload clean up for the newJobs list
cppcooper Mar 26, 2022
7104f16
Updates manageItemCreationEvent to send all events to handlers
cppcooper Mar 26, 2022
93e7193
Updates comments to match others
cppcooper Mar 26, 2022
9209833
Begins modifying manageConstructionEvent
cppcooper Mar 26, 2022
32f83fb
Adds std::hash<df::construction>
cppcooper Mar 26, 2022
caec9da
Adds more events to devel/event-testing
cppcooper Mar 26, 2022
2a6b310
Revises BUILDING events, adds CONSTRUCTION (added/removed) events
cppcooper Mar 26, 2022
6a52e22
Moves operator== from header to TU in which it is used
cppcooper Mar 26, 2022
b34f023
Adds more events to devel/event-testing
cppcooper Mar 26, 2022
642e79e
Adds missing include
cppcooper Mar 26, 2022
ad43347
Fixes build errors
cppcooper Mar 26, 2022
b22c1c1
Adds hasher for SyndromeData
cppcooper Mar 26, 2022
fff9826
Updates manageSyndromeEvent
cppcooper Mar 26, 2022
aefa5f0
Updates manageInvasionEvent
cppcooper Mar 26, 2022
8a7c2ca
Updates manageEquipmentEvent (now manageInventoryChangeEvent)
cppcooper Mar 27, 2022
dd8bc10
Adds to devel/event-testing
cppcooper Mar 27, 2022
fb71e21
Replaces dead unit check with the appropriate Units::isDead()
cppcooper Mar 27, 2022
4a81243
Updates manageReportEvent
cppcooper Mar 28, 2022
8843bf5
Fixes segfault waiting to happen
cppcooper Mar 28, 2022
14ece27
Adds comments about intentionally lost events
cppcooper Mar 28, 2022
d3d3dfe
Revises manageSyndromEvent
cppcooper Mar 28, 2022
b65a181
Reorders functions in file to keep all event managers together
cppcooper Mar 28, 2022
2faa3e8
Removes errant \
cppcooper Mar 28, 2022
88eb135
Moves operator== for EventManager types to header/class defs
cppcooper Mar 28, 2022
12bfad0
Adds UNIT_ATTACK event to devel/event-testing
cppcooper Mar 28, 2022
121a6c3
Updates onAttack
cppcooper Mar 28, 2022
a6567e5
Adds validity check to manageReportsEvent
cppcooper Mar 28, 2022
bee450b
Updates manageUnitAttackEvent
cppcooper Mar 28, 2022
e4518b2
Updates manageInteractionEvent
cppcooper Mar 28, 2022
a8ed873
Revises InteractionData::operator==
cppcooper Mar 29, 2022
9df30f1
Merge branch 'develop' into fix-2031
cppcooper Mar 30, 2022
b8f77a3
Adds backlog option for new event registrations
cppcooper Mar 30, 2022
7ed0dc6
Reorders created/destroyed events to send destroyed first
cppcooper Apr 7, 2022
4a89b80
Reorders events, and dynamically builds eventManager array
cppcooper Apr 7, 2022
d0a7904
Fixes build errors
cppcooper Apr 7, 2022
51e8980
Revises getManager so that missing Events cause a compiler error
cppcooper Apr 7, 2022
8b30cf4
Revises eventful.cpp to generate correct order of handlers in array
cppcooper Apr 7, 2022
4957523
Revises array generation
cppcooper Apr 7, 2022
c84dc5e
Adds comments about the omitted default cases
cppcooper Apr 8, 2022
d4f29f6
Merge remote-tracking branch 'upstream/develop' into fix-2031
cppcooper Apr 14, 2022
ce3a9f2
Merge branch 'develop' into fix-2031
cppcooper Apr 18, 2022
d259462
Moves operator== to df namespace instead of std
cppcooper Apr 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences:
## Misc Improvements
- `dig-now`: handle fortification carving
- `EventManager`: add new event type ``JOB_STARTED``, triggered when a job first gains a worker
- `EventManager`: add new event type ``NEW_UNIT_ACTIVE``, triggered when a new unit appears on the active list
- `EventManager`: add new event type ``UNIT_NEW_ACTIVE``, triggered when a new unit appears on the active list
- `EventManager`: now each registered handler for an event can have its own frequency instead of all handlers using the lowest frequency of all handlers
- `stocks`: allow search terms to match the full item label, even when the label is truncated for length
- `dfhack-examples-guide`: add mugs to ``basic`` manager orders
Expand Down
111 changes: 103 additions & 8 deletions library/include/modules/EventManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "df/unit.h"
#include "df/unit_inventory_item.h"
#include "df/unit_wound.h"
#include "df/construction.h"

namespace DFHack {
namespace EventManager {
Expand All @@ -28,8 +29,12 @@ namespace DFHack {
UNIT_NEW_ACTIVE,
UNIT_DEATH,
ITEM_CREATED,
BUILDING,
CONSTRUCTION,
BUILDING_DESTROYED,
BUILDING_CREATED,
BUILDING, // todo: deprecate this event
CONSTRUCTION_REMOVED,
CONSTRUCTION_ADDED,
CONSTRUCTION, // todo: deprecate this event
SYNDROME,
INVASION,
INVENTORY_CHANGE,
Expand All @@ -45,6 +50,7 @@ namespace DFHack {
typedef void (*callback_t)(color_ostream&, void*); //called when the event happens
callback_t eventHandler;
int32_t freq; //how often event is allowed to fire (in ticks) use 0 to always fire when possible
int32_t when; //when to fire event (global tick count)

EventHandler(callback_t eventHandlerIn, int32_t freqIn): eventHandler(eventHandlerIn), freq(freqIn) {
}
Expand All @@ -60,8 +66,11 @@ namespace DFHack {
struct SyndromeData {
int32_t unitId;
int32_t syndromeIndex;
SyndromeData(int32_t unitId_in, int32_t syndromeIndex_in): unitId(unitId_in), syndromeIndex(syndromeIndex_in) {

SyndromeData(int32_t unitId_in, int32_t syndromeIndex_in)
: unitId(unitId_in), syndromeIndex(syndromeIndex_in) {
}
bool operator==(const SyndromeData &other) const {
return unitId == other.unitId && syndromeIndex == other.syndromeIndex;
}
};

Expand All @@ -72,18 +81,37 @@ namespace DFHack {
InventoryItem() {}
InventoryItem(int32_t id_in, df::unit_inventory_item item_in): itemId(id_in), item(item_in) {}
};

struct InventoryChangeData {
int32_t unitId;
InventoryItem* item_old;
InventoryItem* item_new;
// todo: don't use pointers
std::shared_ptr<InventoryItem> item_old;
std::shared_ptr<InventoryItem> item_new;
InventoryChangeData() {}
InventoryChangeData(int32_t id_in, InventoryItem* old_in, InventoryItem* new_in): unitId(id_in), item_old(old_in), item_new(new_in) {}
InventoryChangeData(int32_t id_in, InventoryItem* old_in, InventoryItem* new_in) :
unitId(id_in),
item_old(old_in),
item_new(new_in) {}
bool operator==(const InventoryChangeData &other) const {
bool unit = unitId == other.unitId;
bool newItem = (item_new && other.item_new && item_new->itemId == other.item_new->itemId) ||
(!item_new && item_new == other.item_new);
bool oldItem = (item_old && other.item_old && item_old->itemId == other.item_old->itemId) ||
(!item_old && item_old == other.item_old);
return unit && newItem && oldItem;
}
};

struct UnitAttackData {
int32_t report_id;
int32_t attacker;
int32_t defender;
int32_t wound;

bool operator==(const UnitAttackData &other) const {
// fairly sure the report_id is the only thing that matters
return report_id == other.report_id && wound == other.wound;
}
};

struct InteractionData {
Expand All @@ -93,9 +121,15 @@ namespace DFHack {
int32_t defender;
int32_t attackReport;
int32_t defendReport;
bool operator==(const InteractionData &other) const {
bool reports = attackReport == other.attackReport && defendReport == other.defendReport;
// based on code in the manager it doesn't need reports or verbs checked.
// since the units are deduced (it seems) from the reports... this
return reports;
}
};

DFHACK_EXPORT void registerListener(EventType::EventType e, EventHandler handler, Plugin* plugin);
DFHACK_EXPORT void registerListener(EventType::EventType e, EventHandler handler, Plugin* plugin, bool backlog = false);
DFHACK_EXPORT int32_t registerTick(EventHandler handler, int32_t when, Plugin* plugin, bool absolute=false);
DFHACK_EXPORT void unregister(EventType::EventType e, EventHandler handler, Plugin* plugin);
DFHACK_EXPORT void unregisterAll(Plugin* plugin);
Expand Down Expand Up @@ -126,6 +160,67 @@ namespace std {
return r;
}
};
template <>
struct hash<df::construction> {
std::size_t operator()(const df::construction& construct) const {
auto &c = construct.pos;
size_t r = 17;
const size_t m = 65537;
r = m*(r+c.x);
r = m*(r+c.y);
r = m*(r+c.z);
return r;
}
};
template <>
struct hash<DFHack::EventManager::SyndromeData> {
std::size_t operator()(const DFHack::EventManager::SyndromeData& syndrome) const {
size_t r = 43;
const size_t m = 65537;
r = m*(r+syndrome.unitId);
r = m*(r+syndrome.syndromeIndex);
return r;
}
};
template <>
struct hash<DFHack::EventManager::InventoryChangeData> {
std::size_t operator()(const DFHack::EventManager::InventoryChangeData& icd) const {
size_t r = 43;
const size_t m = 65537;
r = m*(r+icd.unitId);
if (icd.item_new) {
r=m*(r+icd.item_new->itemId);
}
if (icd.item_old) {
r=m*(r+(2*icd.item_old->itemId));
}
return r;
}
};
template <>
struct hash<DFHack::EventManager::UnitAttackData> {
std::size_t operator()(const DFHack::EventManager::UnitAttackData& uad) const {
size_t r = 43;
const size_t m = 65537;
r = m*(r+uad.report_id);
r = m*(r+uad.attacker);
r = m*(r+uad.defender);
r = m*(r+uad.wound);
return r;
}
};
template <>
struct hash<DFHack::EventManager::InteractionData> {
std::size_t operator()(const DFHack::EventManager::InteractionData& interactionData) const {
size_t r = 43;
const size_t m = 65537;
r = m*(r+interactionData.attackReport);
r = m*(r+interactionData.defendReport);
r = m*(r+interactionData.attacker);
r = m*(r+interactionData.defender);
return r;
}
};
}

#endif
Loading