-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmm_attribute_caber_throw.sp
290 lines (229 loc) · 7.83 KB
/
mm_attribute_caber_throw.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
/*
Caber throw ability
Attribute name = "caber-throw"
variables:
damage = "caber_blast_damage" - float
speed = "caber_grenade_speed" - float
blast radius = "caber_blast_radius" - float
regen time = "caber_regen_time" - float
det on impact = "caber_det_impact" - integer [0, 1]
*/
#pragma semicolon 1
#include <sdkhooks>
#include <ilib>
#include <ilib_objects>
#include <tf2>
#include <tf2_stocks>
#include <tf2attributes>
#include <tf_custom_attributes>
#include <stocksoup/var_strings>
public Plugin MyInfo =
{
name = "Caber Throw",
author = "IvoryPal",
description = "Demoman can throw cabers!"
};
enum struct FCaber
{
float Damage;
float Speed;
float Radius;
bool DetOnImpact;
bool OnCooldown;
float RegenDuration;
FTimer RegenTimer;
}
FCaber CaberGrenade[2049];
bool ThrownCaber[2049] = {false, ...}; // is this entity a thrown caber
FTimer HudTimer[MAXPLAYERS+1];
const float HudRefreshRate = 0.2;
Handle HudSync;
Handle SDKCallInitGrenade;
// int index = 0;
bool HasStat(int weapon, FCaber caber)
{
if (weapon == -1)
{
return false;
}
FWeapon melee;
melee = ConstructWeapon(weapon);
if (melee.DefIndex != 307) // this is only valid for the caber
{
return false;
}
char stat_buffer[256];
if (!TF2CustAttr_GetString(weapon, "caber-throw", stat_buffer, sizeof(stat_buffer)))
{
return false;
}
caber.Damage = ReadFloatVar(stat_buffer, "caber_blast_damage", 100.0);
caber.Radius = ReadFloatVar(stat_buffer, "caber_blast_radius", 176.0);
caber.DetOnImpact = view_as<bool>(ReadIntVar(stat_buffer, "caber_det_impact", 0));
caber.RegenDuration = ReadFloatVar(stat_buffer, "caber_regen_time", 20.0);
caber.Speed = ReadFloatVar(stat_buffer, "caber_grenade_speed", 1600.0);
return true;
}
public void OnPluginStart()
{
// index = 0;
Handle hGameConf = LoadGameConfigFile("tf2.cattr_starterpack");
if (!hGameConf)
{
SetFailState("Failed to load gamedata (tf2.cattr_starterpack).");
}
StartPrepSDKCall(SDKCall_Entity);
PrepSDKCall_SetFromConf(hGameConf, SDKConf_Virtual, "CTFWeaponBaseGrenadeProj::InitGrenade(int float)");
PrepSDKCall_AddParameter(SDKType_Vector, SDKPass_Pointer);
PrepSDKCall_AddParameter(SDKType_Vector, SDKPass_Pointer);
PrepSDKCall_AddParameter(SDKType_CBasePlayer, SDKPass_Pointer);
PrepSDKCall_AddParameter(SDKType_PlainOldData, SDKPass_Plain);
PrepSDKCall_AddParameter(SDKType_Float, SDKPass_Plain);
SDKCallInitGrenade = EndPrepSDKCall();
HudSync = CreateHudSynchronizer();
for (int i = 1; i <= MaxClients; i++)
{
if (IsClientInGame(i))
{
OnClientPutInServer(i);
}
}
}
public void OnClientPutInServer(int client)
{
HudTimer[client] = ConstructTimer(HudRefreshRate, false, true, false, HudRefreshRate * -1.0);
}
public void OnEntityDestroyed(int entity)
{
if (entity <= 0 || entity > 2048) return; //prevent ent refs being used
if (IsValidEntity(entity))
{
ThrownCaber[entity] = false;
}
}
public void OnPlayerRunCmdPost(int clientId, int buttons)
{
FClient client;
client = ConstructClient(clientId);
if (client.Alive() && client.GetClass() == TFClass_DemoMan)
{
int weaponId = client.GetSlot(TFWeaponSlot_Melee);
if (weaponId == -1)
return;
if (HasStat(weaponId, CaberGrenade[weaponId]))
{
if (CaberGrenade[weaponId].OnCooldown)
{
if (CaberGrenade[weaponId].RegenDuration > 1.0 && HudTimer[clientId].Expired())
{
float timeLeft = CaberGrenade[weaponId].RegenTimer.GetTimeRemaining();
SetHudTextParams(-1.0, 0.65, HudRefreshRate, 255, 255, 255, 255);
ShowSyncHudText(clientId, HudSync, "Caber Cooldown: %.0fs", timeLeft+1.0);
}
if (CaberGrenade[weaponId].RegenTimer.Expired())
{
CaberGrenade[weaponId].OnCooldown = false; // Ready to throw again
}
return;
}
if (buttons & IN_ATTACK2 && !CaberGrenade[weaponId].OnCooldown)
{
if (GetEntProp(weaponId, Prop_Send, "m_bBroken")) // Can't throw a broken grenade..
return;
if (GetEntProp(weaponId, Prop_Send, "m_iDetonated"))
return;
ThrowCaber(client, weaponId, CaberGrenade[weaponId]);
}
}
}
}
void ThrowCaber(FClient client, int weaponId, FCaber caber)
{
caber.OnCooldown = true;
caber.RegenTimer = ConstructTimer(caber.RegenDuration, false, false, true);
PrecacheModel("models/weapons/c_models/c_caber/c_caber.mdl");
FVector position;
position = FMath.OffsetVector(client.GetEyePosition(), client.GetEyeAngles(), ConstructVector(0.0, -6.5, -15.25));
// Now create the actual grenade
FObject grenade;
grenade = FGameplayStatics.CreateObjectDeferred("tf_projectile_pipe");
SetVariantInt(client.GetTeam());
grenade.Input("SetTeam");
SetVariantInt(client.GetTeam());
grenade.Input("TeamNum");
SetProjectileLauncher(ConstructObject(weaponId), grenade);
grenade.SetOwner(client.GetObject());
FTransform transform;
transform.Position = position;
transform.Rotation = client.GetEyeAngles();
transform.Rotation.Pitch -= 10.0;
FGameplayStatics.FinishSpawn(grenade, transform);
FVector velocity, angVelocity;
velocity = transform.Rotation.GetForwardVector();
velocity.Scale(caber.Speed);
angVelocity = ConstructVector(0.0, 500.0, 0.0);
SDKCall(SDKCallInitGrenade, grenade.Get(), velocity.ToFloat(), angVelocity.ToFloat(), client.Get(), RoundFloat(caber.Damage), caber.Radius);
grenade.SetModel("models/weapons/c_models/c_caber/c_caber.mdl");
grenade.SetPropFloat(Prop_Send, "m_flModelScale", 1.75);
PrecacheSound("weapons/cleaver_throw.wav");
EmitSoundToAll("weapons/cleaver_throw.wav", client.Get(), SNDCHAN_AUTO, SNDLEVEL_NORMAL);
if (!caber.DetOnImpact)
{
grenade.SetProp(Prop_Send, "m_bTouched", 1); // Do not detonate on impact
}
ThrownCaber[grenade.Get()] = true;
bool crit = ClientHasCrits(client);
SetProjectileProperties(UBaseProjectile(grenade), caber, crit);
int iclient = client.Get();
SetViewmodelAnimation(iclient, 33);
// 32,33,35,55 throw-ish
// PrintToChatAll("Index was %i", index);
// index++;
// TE_WriteEnt("m_hPlayer", client);
// int clientMask = iclient & 0x7FFF;
//64 or 65
int clientMask = GetEntProp(GetEntPropEnt(iclient, Prop_Send, "m_hActiveWeapon"), Prop_Send, "m_hOwnerEntity");
TE_Start("PlayerAnimEvent");
TE_WriteNum("m_hPlayer", clientMask);
TE_WriteNum("m_iEvent", 2);
TE_SendToAll();
}
bool ClientHasCrits(FClient client)
{
if (client.InCondition(TFCond_CritCanteen) || client.InCondition(TFCond_HalloweenCritCandy) || client.InCondition(TFCond_Kritzkrieged) ||
client.InCondition(TFCond_CritOnKill) || client.InCondition(TFCond_CritOnFirstBlood))
return true;
return false;
}
void SetViewmodelAnimation(int client, int sequence)
{
int ent = GetEntPropEnt(client, Prop_Send, "m_hViewModel");
if (!IsValidEdict(ent)) return;
SetEntProp(ent, Prop_Send, "m_nSequence", sequence);
// SetEntPropFloat(ent, Prop_Send, "m_flPlaybackRate", 0.5);
}
void SetProjectileProperties(UBaseProjectile grenade, FCaber caber, bool crit)
{
// If invalid, stop here
if (!grenade.Valid())
return;
// Set our properties for the newly spawned projectile
grenade.Damage = caber.Damage;
grenade.Critical = crit;
if (caber.DetOnImpact) // Pipes need m_flFullDamage set for impact damage
SetFullDamage(grenade, caber.Damage);
}
void SetFullDamage(UBaseProjectile grenade, float damage)
{
// not a networked property so we need to get its offset
int damageOffset = FindSendPropInfo("CTFGrenadePipebombProjectile", "m_bDefensiveBomb") - 4; // 4 bytes before m_bDefensiveBomb
if (damageOffset > 0)
{
SetEntDataFloat(grenade.Get(), damageOffset, damage);
}
}
void SetProjectileLauncher(FObject entity, FObject child)
{
child.SetPropEnt(Prop_Send, "m_hOriginalLauncher", entity);
child.SetPropEnt(Prop_Send, "m_hLauncher", entity);
}