Spellforce-Spell-framework
Loading...
Searching...
No Matches
sf_onhit_registry.cpp
Go to the documentation of this file.
1
2#include "sf_onhit_registry.h"
6
7#include <iostream>
8#include <map>
9#include <cstdint>
10#include <list>
11#include <utility>
12
13static std::map<OnHitPhase,
14 std::list<std::pair<uint16_t,
15 onhit_handler_ptr> > > s_onhit_handler_map;
16
17void registerOnHitHandler(uint16_t spell_line_id, onhit_handler_ptr handler,
18 OnHitPhase phase)
19{
20 // Check if there are existing handlers for the specified phase
21 auto &handler_list = s_onhit_handler_map[phase];
22
23 char buffer[256];
24 sprintf(buffer, "Mod [%s] setting on hit handler for Spell line ID [%d]",
25 g_current_mod->mod_id, spell_line_id);
26 log_info(buffer);
27
28 for (auto &entry : handler_list)
29 {
30 // Check if there's an existing handler for this spell_line_id
31 if (entry.first == spell_line_id)
32 {
33 // Log a warning since we are replacing an existing handler
34 char message[256];
35 sprintf(message,
36 "%s (v%s) has replaced an On Hit Handler [%d] (Was this on purpose?)",
38 spell_line_id);
39 log_warning(message);
40
41 // Replace the existing handler
42 entry.second = handler;
43 return; // Early exit since we updated an existing handler
44 }
45 }
46
47 // If we didn't find an existing handler, add a new handler to back of list. (we iterate in reverse)
48 handler_list.emplace_back(spell_line_id, handler);
49}
50
51std::list<std::pair<uint16_t,
53{
54 int enumValue = static_cast<int>(phase);
55 auto it = s_onhit_handler_map.find(phase);
56 if (it == s_onhit_handler_map.end())
57 {
58 // Element doesn't exist, log a warning and return an empty list
59 // log_warning("Unknown Phase ID for On Hit Handler.");
60 return {}; // Return an empty list
61 }
62 /* char info_msg[50];
63 sprintf(info_msg, "On Hit Handler Phase: %d", enumValue);
64 log_info(info_msg); */
65 return it->second; // Return the list associated with the phase
66}
void log_info(const char *message)
Definition sf_wrappers.c:89
void log_warning(const char *message)
Definition sf_wrappers.c:81
SFMod * g_current_mod
std::list< std::pair< uint16_t, onhit_handler_ptr > > get_onhit_phase(OnHitPhase phase)
void registerOnHitHandler(uint16_t spell_line_id, onhit_handler_ptr handler, OnHitPhase phase)
uint16_t(__thiscall * onhit_handler_ptr)(SF_CGdFigureJobs *, uint16_t source, uint16_t target, uint16_t damage)
char mod_id[64]
char mod_version[24]