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, std::list<std::pair<uint16_t, onhit_handler_ptr> > > s_onhit_handler_map;
14
15void registerOnHitHandler(uint16_t spell_line_id, onhit_handler_ptr handler, OnHitPhase phase)
16{
17 // Check if there are existing handlers for the specified phase
18 auto &handler_list = s_onhit_handler_map[phase];
19
20 char buffer[256];
21 sprintf(buffer, "Mod [%s] setting on hit handler for Spell line ID [%d]", g_current_mod->mod_id, spell_line_id);
22 log_debug(DEBUG_LOW, buffer);
23
24 for (auto &entry : handler_list)
25 {
26 // Check if there's an existing handler for this spell_line_id
27 if (entry.first == spell_line_id)
28 {
29 // Log a warning since we are replacing an existing handler
30 char message[256];
31 sprintf(message, "%s (v%s) has replaced an On Hit Handler [%d] (Was this on purpose?)",
32 g_current_mod->mod_id, g_current_mod->mod_version, spell_line_id);
33 log_warning(message);
34
35 // Replace the existing handler
36 entry.second = handler;
37 return; // Early exit since we updated an existing handler
38 }
39 }
40
41 // If we didn't find an existing handler, add a new handler to back of list. (we iterate in reverse)
42 handler_list.emplace_back(spell_line_id, handler);
43}
44
45std::list<std::pair<uint16_t, onhit_handler_ptr> > get_onhit_phase(OnHitPhase phase)
46{
47 //int enumValue = static_cast<int>(phase);
48 auto it = s_onhit_handler_map.find(phase);
49 if (it == s_onhit_handler_map.end())
50 {
51 // Element doesn't exist, log a warning and return an empty list
52 // log_warning("Unknown Phase ID for On Hit Handler.");
53 return {}; // Return an empty list
54 }
55 /* char info_msg[50];
56 sprintf(info_msg, "On Hit Handler Phase: %d", enumValue);
57 log_info(info_msg); */
58 return it->second; // Return the list associated with the phase
59}
void log_warning(const char *format,...)
Logs a warning at DEBUG_INFO, i.e. always shown.
void log_debug(DebugLevel level, const char *format,...)
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)