AardCrowley / Search-and-Destroy

Safe, Legal Search and Destroy
10 stars 14 forks source link

Emit events when target changes #59

Closed AardNaricain closed 3 years ago

AardNaricain commented 3 years ago

Makes it so we emit an event for other plugins to read any time the target is changed, whether it's cleareed, set to a campaign/gquest/quest target, or set to an ad-hoc target like when you do qw/ht <mob name>. It also has a new method target_as_json that is to be called by other plugins to get details on the target. If we want we can add another method for getting the data in the serialize format but I'd much rather deal with json so that's what I started with.

All changes to the target should go through change_target so that events are broadcast. There's some helper methods, set_target_from_main_target_list, set_target_from_quest, set_adhoc_target, and clear_target to make this easy.

I refactored how targets are handled internally. Instead of having a few, separate pieces of global state (xcp_index, full_mob_name, short_mob_name) we now have a single table that tells us about the target. It's set to nil when there's no target, otherwise it's a structure that contains data relevant to the target. One nice thing about this is it means we don't have multiple pieces of state to change when the target changes, notably not having to clear out a bunch of state when clearing target. Instead we just replace the one current_target variable with the full table of the new target and we're done. It also means no more ugly checks like if (short_mob_name == nil) or (short_mob_name == "") or (short_mob_name == "-1") or (short_mob_name == -1) then This can be replaced by a has_target() method.

There's also some helper method to examine the current target for common patterns: