nwnxee / unified

Binaries available under the Releases tab on Github
https://nwnxee.github.io/unified
GNU General Public License v3.0
131 stars 92 forks source link

Events: Toggle mode #15

Closed Liareth closed 5 years ago

Liareth commented 6 years ago

The StickyCombatModes plugin used to use the messaging bus to add two events - NWNX_ON_MODE_ON and NWNX_ON_MODE_OFF.

The code looked like this:

GetServices()->m_hooks->RequestExclusiveHook<API::Functions::CNWSCreature__SetCombatMode>(&SetCombatModeHook);

... later ...

void StickyCombatModes::SetCombatModeHook(API::CNWSCreature* thisPtr, uint8_t newMode, bool forceNewMode)
{
    if (thisPtr->m_bPlayerCharacter)
    {
        const uint8_t currentMode = thisPtr->m_nCombatMode;

        if (g_plugin->m_dispatchMessages && currentMode != newMode)
        {
            // ToggleMode gets called with modes which are +1 to what the script constants expect.
            // It then calls SetCombatMode with the 'proper' modes.
            // To simplify things, we just correct the mode we dispatch here, so the game events
            // receive what they expect to.
            auto messaging = g_plugin->GetServices()->m_messaging.get();

            if (currentMode != 0)
            {
                messaging->BroadcastMessage("NWNX_EVENT_PUSH_EVENT_DATA", { "MODE", std::to_string(currentMode + 1) } );
                messaging->BroadcastMessage("NWNX_EVENT_SIGNAL_EVENT", { "NWNX_ON_MODE_OFF", ObjectIDToString(thisPtr->m_idSelf) });
            }

            if (newMode != 0)
            {
                messaging->BroadcastMessage("NWNX_EVENT_PUSH_EVENT_DATA", { "MODE", std::to_string(newMode + 1) } );
                messaging->BroadcastMessage("NWNX_EVENT_SIGNAL_EVENT", { "NWNX_ON_MODE_ON", ObjectIDToString(thisPtr->m_idSelf) });
            }
        }
    }

    return g_setCombatModeHook->CallOriginal<void>(thisPtr, newMode, forceNewMode);
}

Now that we no longer have the StickyCombatModes plugin, we need to move this event to the Events plugin with the same effective outputs as above.

mtijanic commented 5 years ago

Done.