-
Notifications
You must be signed in to change notification settings - Fork 491
Adds an onUnitAction event #2094
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 2 commits
d1f933b
ec11406
2763e47
d96dcc6
62deece
a244d59
d199b5b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -28,6 +28,7 @@ | |||||||||||||||||||||||||
| #include "df/report.h" | ||||||||||||||||||||||||||
| #include "df/ui.h" | ||||||||||||||||||||||||||
| #include "df/unit.h" | ||||||||||||||||||||||||||
| #include "df/unit_action.h" | ||||||||||||||||||||||||||
| #include "df/unit_flags1.h" | ||||||||||||||||||||||||||
| #include "df/unit_inventory_item.h" | ||||||||||||||||||||||||||
| #include "df/unit_report_type.h" | ||||||||||||||||||||||||||
|
|
@@ -137,6 +138,7 @@ static void manageReportEvent(color_ostream& out); | |||||||||||||||||||||||||
| static void manageUnitAttackEvent(color_ostream& out); | ||||||||||||||||||||||||||
| static void manageUnloadEvent(color_ostream& out){}; | ||||||||||||||||||||||||||
| static void manageInteractionEvent(color_ostream& out); | ||||||||||||||||||||||||||
| static void manageActionEvent(color_ostream& out); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| typedef void (*eventManager_t)(color_ostream&); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
@@ -157,6 +159,7 @@ static const eventManager_t eventManager[] = { | |||||||||||||||||||||||||
| manageUnitAttackEvent, | ||||||||||||||||||||||||||
| manageUnloadEvent, | ||||||||||||||||||||||||||
| manageInteractionEvent, | ||||||||||||||||||||||||||
| manageActionEvent, | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| //job initiated | ||||||||||||||||||||||||||
|
|
@@ -200,6 +203,9 @@ static int32_t reportToRelevantUnitsTime = -1; | |||||||||||||||||||||||||
| //interaction | ||||||||||||||||||||||||||
| static int32_t lastReportInteraction; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| //unit action | ||||||||||||||||||||||||||
| static std::map<int32_t,std::vector<int32_t> > unitToKnownActions; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
| namespace std { | |
| template <> | |
| struct hash<df::coord> { | |
| std::size_t operator()(const df::coord& c) const { | |
| 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; | |
| } | |
| }; |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you don't use the index for anything other than getting the unit out of the vector, consider
for (auto unit : df::global::world->units.all)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. Fairly sure I've replaced any that could be via clion's clang-tidy suggestions. So if I end up merging these into PR #2044 I'll be getting this one too.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To reduce indentation, how about
if (!Units::isActive(unit)) {
unitToKnownActions.erase(unit->id);
continue;
}
This way you don't need an else clause
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dunno, I wouldn't worry about it until it's not just the indentation but also the length of the block below. This all kinda fits on one screen.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nah, I generally prefer things be structured in the way mentioned for stuff like this, I just sort of forget to do it
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider using a reference instead of a pointer:
auto & knownActions = unitToKnownActions[unit->id];
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-
So like all the events (that I can think of) in EventManager refactoring #2044 this would need to be split into two parts. Data generation, and event messaging.
-
Beyond that we'd be looking at knowing the current tick as to log the "time" that data was generated, and then the messaging loop would look the same as all the others.
At a later point, we're probably going to want to merge the data generators cause many iterate the same structures. But that's not part of this discussion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only one that might apply to this PR would be 1. With 2, it would just introduce an unused variable.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you explain what is happening here? Is an action type of None invalidating all more recent actions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the erase-removed idiom, though perhaps more verbose than it needs to be
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. So it's only removing a single element. Does order matter for these actions, or would we be better off using a set instead of a vector?
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| #include "df/reaction_reagent_itemst.h" | ||
| #include "df/reaction_product_itemst.h" | ||
| #include "df/unit.h" | ||
| #include "df/unit_action.h" | ||
| #include "df/unit_inventory_item.h" | ||
| #include "df/unit_wound.h" | ||
| #include "df/world.h" | ||
|
|
@@ -109,6 +110,7 @@ DEFINE_LUA_EVENT_NH_1(onReport, int32_t); | |
| DEFINE_LUA_EVENT_NH_3(onUnitAttack, int32_t, int32_t, int32_t); | ||
| DEFINE_LUA_EVENT_NH_0(onUnload); | ||
| DEFINE_LUA_EVENT_NH_6(onInteraction, std::string, std::string, int32_t, int32_t, int32_t, int32_t); | ||
| DEFINE_LUA_EVENT_NH_3(onUnitAction, int32_t, df::unit_action*, int32_t); | ||
|
|
||
| DFHACK_PLUGIN_LUA_EVENTS { | ||
| DFHACK_LUA_EVENT(onWorkshopFillSidebarMenu), | ||
|
|
@@ -136,6 +138,7 @@ DFHACK_PLUGIN_LUA_EVENTS { | |
| DFHACK_LUA_EVENT(onUnitAttack), | ||
| DFHACK_LUA_EVENT(onUnload), | ||
| DFHACK_LUA_EVENT(onInteraction), | ||
| DFHACK_LUA_EVENT(onUnitAction), | ||
| DFHACK_LUA_END | ||
| }; | ||
|
|
||
|
|
@@ -220,6 +223,10 @@ static void ev_mng_interaction(color_ostream& out, void* ptr) { | |
| EventManager::InteractionData* data = (EventManager::InteractionData*)ptr; | ||
| onInteraction(out, data->attackVerb, data->defendVerb, data->attacker, data->defender, data->attackReport, data->defendReport); | ||
| } | ||
| static void ev_mng_action(color_ostream& out, void* ptr) { | ||
|
||
| EventManager::ActionData* data = (EventManager::ActionData*)ptr; | ||
| onUnitAction(out, data->unitId, data->action, data->actionId); | ||
| } | ||
| std::vector<int> enabledEventManagerEvents(EventManager::EventType::EVENT_MAX,-1); | ||
| typedef void (*handler_t) (color_ostream&,void*); | ||
|
|
||
|
|
@@ -242,6 +249,7 @@ static const handler_t eventHandlers[] = { | |
| ev_mng_unitAttack, | ||
| ev_mng_unload, | ||
| ev_mng_interaction, | ||
| ev_mng_action, | ||
|
||
| }; | ||
| static void enableEvent(int evType,int freq) | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -166,6 +166,7 @@ eventType=invertTable{ | |
| "UNIT_ATTACK", | ||
| "UNLOAD", | ||
| "INTERACTION", | ||
| "UNIT_ACTION", | ||
| "EVENT_MAX" | ||
| } | ||
| return _ENV | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
actionIdis a field ofaction, why pass the id as a separate field to the handler?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Midunderstanding of the comment
//it has to keep the id of an item because the item itself may have been deallocated, most likely; I didn't quite put together that this is for inventory_item in particular