-
Notifications
You must be signed in to change notification settings - Fork 7
/
mm_boss_healthbar_setter.sp
329 lines (254 loc) · 8.23 KB
/
mm_boss_healthbar_setter.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#pragma semicolon 1
#include <sourcemod>
#include <berobot_constants>
#include <berobot>
#include <sdkhooks>
#pragma newdecls required
#include <sdktools>
//#define BOSSTUNE "music.mvm_end_mid_wave"
#define BOSSTUNE "music/mvm_start_last_wave.wav"
float g_spawn_sound_cd = 60.0;
float SoundClamp[MAXPLAYERS+1] = {0.0,...};
enum TFBossHealthState {
HealthState_Default = 0,
HealthState_Healing // displays a green health bar
};
methodmap TFMonsterResource {
property int Index {
public get() {
return EntRefToEntIndex(view_as<int>(this));
}
}
property int BossHealthPercentageByte {
public get() {
return GetEntProp(this.Index, Prop_Send, "m_iBossHealthPercentageByte");
}
public set(int value) {
value = value > 0xFF? 0xFF : value;
value = value < 0? 0 : value;
SetEntProp(this.Index, Prop_Send, "m_iBossHealthPercentageByte", value);
}
}
property TFBossHealthState BossHealthState {
public get() {
int index = this.Index;
return view_as<TFBossHealthState>(GetEntProp(index, Prop_Send, "m_iBossState"));
}
public set(TFBossHealthState value) {
SetEntProp(this.Index, Prop_Send, "m_iBossState", value);
}
}
/**
* Updates the monster resource health display to display the current health of the
* specified entity.
*/
public void LinkHealth(int entity) {
int hEntity = EntRefToEntIndex(entity);
if (IsValidEntity(hEntity)) {
int iMaxHealth = GetEntProp(hEntity, Prop_Data, "m_iMaxHealth");
// account for max unbuffed health on clients, stored in player resource entity
if (entity > 0 && entity <= MaxClients) {
int resource = GetPlayerResourceEntity();
if (IsValidEntity(resource)) {
iMaxHealth = GetEntProp(resource, Prop_Send, "m_iMaxHealth", _, entity);
}
}
int iHealth = GetEntProp(hEntity, Prop_Data, "m_iHealth");
this.BossHealthPercentageByte = RoundToCeil(float(iHealth) / iMaxHealth * 255);
}
}
/**
* Returns the first monster_resource entity, with an option to create it if it doesn't
* exist.
*/
public static TFMonsterResource GetEntity(bool create = false) {
int hMonsterResource = FindEntityByClassname(-1, "monster_resource");
if (hMonsterResource == -1) {
hMonsterResource = CreateEntityByName("monster_resource");
if (hMonsterResource == -1) {
DispatchSpawn(hMonsterResource);
}
}
return view_as<TFMonsterResource>(EntIndexToEntRef(hMonsterResource));
}
}
#define PLUGIN_VERSION "0.0.0"
public Plugin myinfo = {
name = "[TF2] Assign Boss Healthbar",
author = "nosoop",
description = "Attempts to assign the boss healthbar to a player.",
version = PLUGIN_VERSION,
url = "https://github.com/nosoop/"
}
public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
{
// CreateNative("SetBossHealth", Native_SetBossHealth);
// CreateNative("UnSetBossHealth", Native_UnSetBossHealth);
return APLRes_Success;
}
int g_iBossTarget = -1;
public void OnPluginStart() {
LoadTranslations("common.phrases");
RegAdminCmd("sm_setboss", SetBossHealthTarget, ADMFLAG_ROOT);
//RegAdminCmd("sm_pt", PlayTune, ADMFLAG_ROOT);
//RegAdminCmd("sm_setbosshud", SetBossHealth, ADMFLAG_ROOT);
// RegServerCmd("sm_unsetbosshud", UnSetBossHealth);
HookEvent("post_inventory_application", OnInventoryApplied);
//HookEvent("player_death", Event_Death_RemoveHUD, EventHookMode_Post);
}
public void OnPluginEnd() {
TFMonsterResource resource = TFMonsterResource.GetEntity(false);
if (IsValidEntity(resource.Index)) {
resource.BossHealthPercentageByte = 0;
}
}
public void OnMapStart()
{
//PrecacheScriptSound(BOSSTUNE);
PrecacheSound(BOSSTUNE);
}
// public Action Event_Death_RemoveHUD(Event event, const char[] name, bool dontBroadcast)
// {
// int client = GetClientOfUserId(GetEventInt(event, "userid"));
// //The client was a boss and died
// if (client == g_iBossTarget) {
// SDKUnhook(g_iBossTarget, SDKHook_PostThink, OnBossPostThink);
// //RemoveHUD();
// g_iBossTarget = -1;
// }
// }
public void RemoveHUD(){
int iEnt = MaxClients + 1;
while ((iEnt = FindEntityByClassname(iEnt, "monster_resource")) != -1)
{
if (IsValidEntity(iEnt))
{
DispatchKeyValue(iEnt, "rendermode", "0");
DispatchSpawn(iEnt);
//AcceptEntityInput(iEnt, "Kill");
// float fPos[3];
// fPos[0] = 15.0;
// fPos[1] = 15.0;
// fPos[2] = 15.0;
// TeleportEntity(iEnt, fPos, NULL_VECTOR, NULL_VECTOR);
}
}
}
public void OnInventoryApplied(Event event, const char[] name, bool dontBroadcast) {
int client = GetClientOfUserId(event.GetInt("userid"));
if (TF2_IsGameModeMvM() && client == g_iBossTarget) {
// should be forced to regenerate on next think
SetEntProp(client, Prop_Send, "m_bUseBossHealthBar", false);
}
if (IsBoss(client) && IsPlayerAlive(client))
{
//PrintToChatAll("WAS BOSS!");
SetBossHealthTargetCommand(client);
}
}
public Action SetBossHealthTarget(int client, int argc) {
if (!argc) {
return Plugin_Handled;
}
//EmitGameSoundToAll(BOSSTUNE);
char target[MAX_NAME_LENGTH + 1];
GetCmdArg(1, target, sizeof(target));
int iTarget = FindTarget(client, target, false, false);
if (iTarget != -1 && g_iBossTarget != iTarget) {
if (IsValidEntity(g_iBossTarget)) {
SDKUnhook(g_iBossTarget, SDKHook_PostThink, OnBossPostThink);
// SetEntProp(g_iBossTarget, Prop_Send, "m_bGlowEnabled", 0);
}
g_iBossTarget = iTarget;
SDKHook(iTarget, SDKHook_PostThink, OnBossPostThink);
// if (IsValidEntity(g_iBossTarget)){
// SetEntProp(g_iBossTarget, Prop_Send, "m_bGlowEnabled", 0);
// }
// EmitGameSoundToAll(BOSSTUNE,_,SNDCHAN_AUTO);
// for(int i = 1; i < MaxClients; i++) {
// if(IsClientInGame(i) && !IsFakeClient(i)) {
// EmitSoundToClient(i, BOSSTUNE);
// }
// ReplyToCommand(client, "Switched boss target to %N", iTarget);
} else {
g_iBossTarget = -1;
// ReplyToCommand(client, "Removed boss target");
}
return Plugin_Handled;
}
public Action SetBossHealthTargetCommand(int client) {
int iTarget = client;
if (iTarget != -1 && g_iBossTarget != iTarget) {
if (IsValidEntity(g_iBossTarget)) {
SDKUnhook(g_iBossTarget, SDKHook_PostThink, OnBossPostThink);
// SetEntProp(g_iBossTarget, Prop_Send, "m_bGlowEnabled", 1);
}
g_iBossTarget = iTarget;
SDKHook(iTarget, SDKHook_PostThink, OnBossPostThink);
// if (IsValidEntity(g_iBossTarget)){
// SetEntProp(iTarget, Prop_Send, "m_bGlowEnabled", 0);
// }
// ReplyToCommand(client, "Switched boss target to %N", iTarget);
}
// EmitSoundToAll(BOSSTUNE, _, SNDCHAN_USER_BASE); //Works for all, but cancels on bot change
// PrintToChatAll("SETTING");
if (SoundClamp[client] < GetEngineTime())
{
for(int i = 1; i <= MAXPLAYERS+1; i++)
{
if (IsValidClient(i) && !IsFakeClient(i))
{
if (SoundClamp[i] < GetEngineTime())
{
EmitSoundToClient(i,BOSSTUNE);
SoundClamp[client] = GetEngineTime() + g_spawn_sound_cd;
}
// PrintToChat(i,"Playing sound %s to %N ", BOSSTUNE, i);
}
}
}
// PrintToChatAll("Emitting sound");
// else {
// g_iBossTarget = -1;
// // ReplyToCommand(client, "Removed boss target");
// }
return Plugin_Handled;
}
public void OnBossPostThink(int client) {
//PrintToChatAll("THINK");
if (client != g_iBossTarget || !IsBoss(client)) {
SDKUnhook(client, SDKHook_PostThink, OnBossPostThink);
g_iBossTarget = -1;
} else {
if (!TF2_IsGameModeMvM()) {
// non-MvM, use monster resource health bar
if (IsPlayerAlive(client) && IsClientInGame(client)) {
//PrintToChatAll("Player is alive");
TFMonsterResource.GetEntity(true).LinkHealth(client);
} else {
TFMonsterResource.GetEntity(true).BossHealthPercentageByte = 0;
//PrintToChatAll("Player is dead");
g_iBossTarget = -1;
//RemoveHUD();
}
} else if (!GetEntProp(client, Prop_Send, "m_bUseBossHealthBar")) {
// MvM, display boss health bar if it isn't already
SetEntProp(client, Prop_Send, "m_bUseBossHealthBar", true);
}
}
//PrintToChatAll("Player is alive");
if (g_iBossTarget == -1){
RemoveHUD();
}
}
public void OnClientDisconnect(int client) {
if (client == g_iBossTarget) {
SDKUnhook(g_iBossTarget, SDKHook_PostThink, OnBossPostThink);
g_iBossTarget = -1;
RemoveHUD();
}
}
// Powerlord's MvM stock
stock bool TF2_IsGameModeMvM() {
return GameRules_GetProp("m_bPlayingMannVsMachine")? true : false;
}