-
Notifications
You must be signed in to change notification settings - Fork 481
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
Adds an onUnitAction event #2094
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, | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This task will need to be moved to a switch when/if #2097 is merged |
||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
//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; | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we don't iterate through this structure, an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
True. But an even better idea is to add a hash for the data we actually care about, since the This way we avoid whatever kind of search edit: examples dfhack/library/include/modules/EventManager.h Lines 107 to 118 in 9bf9a79
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
void DFHack::EventManager::onStateChange(color_ostream& out, state_change_event event) { | ||||||||||||||||||||||||||
static bool doOnce = false; | ||||||||||||||||||||||||||
// const string eventNames[] = {"world loaded", "world unloaded", "map loaded", "map unloaded", "viewscreen changed", "core initialized", "begin unload", "paused", "unpaused"}; | ||||||||||||||||||||||||||
|
@@ -222,6 +228,7 @@ void DFHack::EventManager::onStateChange(color_ostream& out, state_change_event | |||||||||||||||||||||||||
buildings.clear(); | ||||||||||||||||||||||||||
constructions.clear(); | ||||||||||||||||||||||||||
equipmentLog.clear(); | ||||||||||||||||||||||||||
unitToKnownActions.clear(); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
Buildings::clearBuildings(out); | ||||||||||||||||||||||||||
lastReport = -1; | ||||||||||||||||||||||||||
|
@@ -1254,3 +1261,32 @@ static void manageInteractionEvent(color_ostream& out) { | |||||||||||||||||||||||||
//TODO: deduce attacker from latest defend event first | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
static void manageActionEvent(color_ostream& out) { | ||||||||||||||||||||||||||
if (!df::global::world) | ||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||
multimap<Plugin*,EventHandler> copy(handlers[EventType::UNIT_ACTION].begin(), handlers[EventType::UNIT_ACTION].end()); | ||||||||||||||||||||||||||
for ( size_t a = 0; a < df::global::world->units.all.size(); a++ ) { | ||||||||||||||||||||||||||
df::unit* unit = df::global::world->units.all[a]; | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||||||||||||||||||||||||||
if ( Units::isActive(unit) ) { | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To reduce indentation, how about
This way you don't need an else clause There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 |
||||||||||||||||||||||||||
auto knownActions = &unitToKnownActions[unit->id]; | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. consider using a reference instead of a pointer: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree |
||||||||||||||||||||||||||
for ( df::unit_action* action : unit->actions ) { | ||||||||||||||||||||||||||
if ( action->type != df::unit_action_type::None) { | ||||||||||||||||||||||||||
if ( std::find(knownActions->begin(), knownActions->end(), action->id) == knownActions->end() ) { | ||||||||||||||||||||||||||
knownActions->push_back(action->id); | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||||||||||
for ( auto b = copy.begin(); b != copy.end(); b++ ) { | ||||||||||||||||||||||||||
EventHandler handle = (*b).second; | ||||||||||||||||||||||||||
ActionData data = {unit->id, action, action->id}; | ||||||||||||||||||||||||||
handle.eventHandler(out, (void*)&data); | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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 commentThe 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. |
||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||
auto newEnd = std::remove(knownActions->begin(), knownActions->end(), action->id); | ||||||||||||||||||||||||||
knownActions->erase(newEnd, knownActions->end()); | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe 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? |
||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||
unitToKnownActions.erase(unit->id); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} |
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest naming this |
||
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This too will get moved to a switch when/if #2097 is merged. |
||
}; | ||
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
actionId
is 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