-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
blind.sp
141 lines (118 loc) · 4.17 KB
/
blind.sp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Blind perk.
* Copyright (C) 2023 Filip Tomaszewski
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define Alpha Int[0]
#define ShowAnnotations Int[1]
#define TriggerUnblind Int[2]
#define AnnotationLifetime Float[0]
DEFINE_CALL_APPLY_REMOVE(Blind)
public void Blind_Init(const Perk perk)
{
Events.OnPlayerAttacked(perk, Blind_OnPlayerAttacked);
}
void Blind_ApplyPerk(const int client, const Perk perk)
{
int iAlpha = perk.GetPrefCell("alpha", 254);
Cache[client].Alpha = iAlpha;
Cache[client].ShowAnnotations = perk.GetPrefCell("annotations", 1);
Cache[client].TriggerUnblind = perk.GetPrefCell("unblind", 1);
Cache[client].AnnotationLifetime = GetPerkTimeFloat(perk);
Cache[client].Flags.Reset();
Blind_SendFade(client, iAlpha);
SetOverlay(client, ClientOverlay_Stealth);
if (Cache[client].ShowAnnotations)
{
Blind_UpdateAnnotations(client);
Cache[client].Repeat(1.0, Blind_UpdateAnnotationsCheck);
}
}
public void Blind_RemovePerk(const int client, const RTDRemoveReason eRemoveReason)
{
Blind_SendFade(client, 0);
SetOverlay(client, ClientOverlay_None);
if (Cache[client].ShowAnnotations)
Blind_UpdateAnnotations(client, true);
}
public Action Blind_UpdateAnnotationsCheck(const int client)
{
Blind_UpdateAnnotations(client);
return Plugin_Continue;
}
void Blind_UpdateAnnotations(const int client, const bool bForceDisable=false)
{
int iOtherTeam = GetOppositeTeamOf(client);
for (int i = 1; i <= MaxClients; ++i)
{
bool bSet = Cache[client].Flags.Test(i);
bool bShouldSet = Blind_IsValidTarget(client, i, iOtherTeam) && !bForceDisable;
if (!bSet && bShouldSet)
{
ShowAnnotationFor(client, i, Cache[client].AnnotationLifetime, "<!>");
Cache[client].Flags.Set(i);
}
else if (bSet && !bShouldSet)
{
HideAnnotationFor(client, i);
Cache[client].Flags.Unset(i);
}
}
}
public void Blind_OnPlayerAttacked(const int client, const int iVictim, const int iDamage, const int iRemainingHealth)
{
if(!Cache[client].TriggerUnblind)
return;
if (client == iVictim)
return;
Blind_SendFade(client, 0);
Blind_SendFade(client, Cache[client].Alpha, true);
}
bool Blind_IsValidTarget(const int client, const int iTarget, const int iTargetTeam)
{
if (iTarget == client || !IsClientInGame(iTarget) || !IsPlayerAlive(iTarget) || TF2_IsPlayerInCondition(iTarget, TFCond_Cloaked))
return false;
if (GetClientTeam(iTarget) != iTargetTeam)
return false;
/*
* Updating annotation position is a client-side functionality. However, the client might not
* have an up-to-date position of the other player if that player is far away (ex. died and
* respawned). This causes annotations to linger there in the last known position.
*
* We can fix this by manually checking if the Blind player can see the target, meaning their
* client knows their coordinates. This unfortunately ends up a bit too expensive than it needs
* be, but it works.
*/
return CanEntitySeeTarget(client, iTarget);
}
void Blind_SendFade(const int client, const int iAlpha, const bool bFast=false)
{
int iTargets[2];
iTargets[0] = client;
int iDuration = 200 + 1336 * view_as<int>(!bFast);
Handle hMsg = StartMessageEx(UserMessages.Fade, iTargets, 1);
BfWriteShort(hMsg, iDuration);
BfWriteShort(hMsg, iDuration);
BfWriteShort(hMsg, iAlpha > 0 ? (0x0002 | 0x0008) : (0x0001 | 0x0010));
BfWriteByte(hMsg, 0);
BfWriteByte(hMsg, 0);
BfWriteByte(hMsg, 0);
BfWriteByte(hMsg, iAlpha);
EndMessage();
}
#undef Alpha
#undef ShowAnnotations
#undef TriggerUnblind
#undef AnnotationLifetime