Skip to content

Commit eb369a0

Browse files
committed
Optimize 36a3b56 TEvent fix
1 parent 36a3b56 commit eb369a0

File tree

3 files changed

+24
-27
lines changed

3 files changed

+24
-27
lines changed

src/Ext/TEvent/Body.cpp

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@ void TEventExt::ExtData::SaveToStream(PhobosStreamWriter& Stm)
3333
this->Serialize(Stm);
3434
}
3535

36-
bool TEventExt::Execute(TEventClass* pThis, int iEvent, HouseClass* pHouse, ObjectClass* pObject,
37-
CDTimerClass* pTimer, bool* isPersitant, TechnoClass* pSource, bool& bHandled)
36+
std::optional<bool> TEventExt::Execute(TEventClass* pThis, int iEvent, HouseClass* pHouse,
37+
ObjectClass* pObject, CDTimerClass* pTimer, bool* isPersitant, TechnoClass* pSource)
3838
{
39-
bHandled = true;
4039
const auto eventKind = static_cast<PhobosTriggerEvent>(pThis->EventKind);
4140

4241
switch (eventKind)
4342
{
43+
// The triggering conditions that need to be checked at any time are written here
44+
4445
// helper struct
4546
struct and_with { bool operator()(int a, int b) { return a & b; } };
4647

@@ -128,30 +129,23 @@ bool TEventExt::Execute(TEventClass* pThis, int iEvent, HouseClass* pHouse, Obje
128129
case PhobosTriggerEvent::CellHasAnyTechnoTypeFromList:
129130
return TEventExt::CellHasAnyTechnoTypeFromListTEvent(pThis, pObject, pHouse);
130131

132+
131133
// If it requires an additional object as like mapping events 7 or 48, please fill it in here.
134+
135+
// They must be the same, but for other triggers to take effect normally, this cannot be judged outside case.
136+
auto isSameEvent = [&]() { return eventKind == static_cast<PhobosTriggerEvent>(iEvent); };
137+
138+
// SomeTriggerAttachedToObject needs to be restricted to situations where ...
132139
// case PhobosTriggerEvent::SomeTriggerAttachedToObject:
133-
case PhobosTriggerEvent::ShieldBroken:
134-
// They must be the same.
135-
if (eventKind == static_cast<PhobosTriggerEvent>(iEvent))
136-
{
137-
switch (eventKind)
138-
{
139-
// SomeTriggerAttachedToObject needs to be restricted to situations where ...
140-
// case PhobosTriggerEvent::SomeTriggerAttachedToObject:
141-
// return ...::ThisAttachedToObjectTEvent(pObject, ...);
140+
// return isSameEvent() && ...::ThisAttachedToObjectTEvent(pObject, ...);
142141

143-
// ShieldBroken needs to be restricted to situations where the shield is being attacked.
144-
case PhobosTriggerEvent::ShieldBroken:
145-
return ShieldClass::ShieldIsBrokenTEvent(pObject);
142+
// ShieldBroken needs to be restricted to situations where the shield is being attacked.
143+
case PhobosTriggerEvent::ShieldBroken:
144+
return isSameEvent() && ShieldClass::ShieldIsBrokenTEvent(pObject);
146145

147-
default:
148-
break;
149-
}
150-
}
151146

152147
default:
153-
bHandled = false;
154-
return true;
148+
return std::nullopt;
155149
};
156150
}
157151

src/Ext/TEvent/Body.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
#include <Utilities/Container.h>
44
#include <Utilities/Template.h>
5-
65
#include <Helpers/Template.h>
76

7+
#include <optional>
8+
89
#include <TEventClass.h>
910

1011
class HouseClass;
@@ -82,8 +83,8 @@ class TEventExt
8283
void Serialize(T& Stm);
8384
};
8485

85-
static bool Execute(TEventClass* pThis, int iEvent, HouseClass* pHouse, ObjectClass* pObject,
86-
CDTimerClass* pTimer, bool* isPersitant, TechnoClass* pSource, bool& bHandled);
86+
static std::optional<bool> Execute(TEventClass* pThis, int iEvent, HouseClass* pHouse,
87+
ObjectClass* pObject, CDTimerClass* pTimer, bool* isPersitant, TechnoClass* pSource);
8788

8889
template<bool IsGlobal, typename _Pr>
8990
static bool VariableCheck(TEventClass* pThis);

src/Ext/TEvent/Hooks.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ DEFINE_HOOK(0x71E940, TEventClass_Execute, 0x5)
2020
GET_STACK(bool*, isPersitant, 0x14);
2121
GET_STACK(TechnoClass*, pSource, 0x18);
2222

23-
bool handled;
23+
const auto result = TEventExt::Execute(pThis, iEvent, pHouse, pObject, pTimer, isPersitant, pSource);
2424

25-
R->AL(TEventExt::Execute(pThis, iEvent, pHouse, pObject, pTimer, isPersitant, pSource, handled));
25+
if (!result.has_value())
26+
return 0;
2627

27-
return handled ? 0x71EA2D : 0;
28+
R->AL(result.value());
29+
return 0x71EA2D;
2830
}
2931

3032
DEFINE_HOOK(0x7271F9, TEventClass_GetFlags, 0x5)

0 commit comments

Comments
 (0)