-
Notifications
You must be signed in to change notification settings - Fork 7
/
ability_free_damage_rockets_soldier_shounic_ability.sp
376 lines (314 loc) · 9.17 KB
/
ability_free_damage_rockets_soldier_shounic_ability.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#pragma semicolon 1
#include <sourcemod>
#include <tf2_stocks>
#include <tf2attributes>
#include <sm_logger>
#include <berobot_constants>
#include <berobot>
#include <tf_custom_attributes>
#include <sdkhooks>
#include <sdktools>
#define PLUGIN_VERSION "1.0"
#define ROBOT_NAME "Shounic"
#define ROCKETMODEL "models/weapons/c_models/c_rocketlauncher/c_rocketlauncher.mdl"
public Plugin:myinfo =
{
name = "[TF2] Shounic Ability",
author = "Erofix using the code from: Pelipoika, PC Gamer, Jaster and StormishJustice",
description = "Shounic Ability",
version = PLUGIN_VERSION,
url = "www.sourcemod.com"
}
enum struct Reference
{
int ref;
void set(int entity)
{
this.ref = EntIndexToEntRef(entity);
}
int get()
{
return EntRefToEntIndex(this.ref);
}
bool valid()
{
int entity = this.get();
if (IsValidEntity(entity) && entity > MaxClients)
return true;
return false;
}
}
enum struct Rocket
{
float fire_delay;
float attack_time;
bool critical;
Reference particle;
Reference particle_crit;
}
Rocket Launcher[2049]; // :)
bool RemoteRocket[2049];
bool OtherRocket[2049];
int RocketModel;
ConVar AutoAim;
ConVar AttackTime;
char LOG_TAGS[][] = {"VERBOSE", "INFO", "ERROR"};
enum(<<= 1)
{
SML_VERBOSE = 1,
SML_INFO,
SML_ERROR,
}
public OnPluginStart()
{
SMLoggerInit(LOG_TAGS, sizeof(LOG_TAGS), SML_ERROR, SML_FILE);
LoadTranslations("common.phrases");
AutoAim = CreateConVar("shounic_rocket_launcher_auto_aim", "0", "Should a fired rocket launcher automatically target players", _, true, 0.0, true, 1.0);
AttackTime = CreateConVar("shounic_rocket_launcher_fire_delay", "0.9", "Attack delay for fired rocket launchers");
}
public void OnPluginEnd()
{
RemoveRobot(ROBOT_NAME);
}
public APLRes:AskPluginLoad2(Handle:myself, bool:late, String:error[], err_max)
{
// CreateNative("BeGiantPyro_MakeGiantSoldier", Native_SetGiantPyro);
// CreateNative("BeGiantPyro_IsGiantPyro", Native_IsGiantPyro);
return APLRes_Success;
}
public OnMapStart()
{
RocketModel = PrecacheModel(ROCKETMODEL);
}
///ROCKET CODE
public void OnEntityDestroyed(int entity)
{
if (entity > 1 && entity < 2049)
{
OtherRocket[entity] = false;
if (Launcher[entity].particle.valid())
{
int particle = Launcher[entity].particle.get();
RemoveEntity(particle);
}
if (Launcher[entity].particle_crit.valid())
{
int particle = Launcher[entity].particle_crit.get();
RemoveEntity(particle);
}
}
}
public void OnEntityCreated(int entity, const char[] classname)
{
if (entity > 1 && entity < 2049) //exclude entity references...
{
RemoteRocket[entity] = false;
if (StrContains(classname, "tf_projectile_rocket") != -1)
SDKHook(entity, SDKHook_SpawnPost, OnRocketSpawned);
}
}
void OnRocketSpawned(int entity)
{
if (OtherRocket[entity])
return;
int owner = GetEntPropEnt(entity, Prop_Data, "m_hOwnerEntity");
if (IsRobot(owner, ROBOT_NAME))
{
SetModelOverride(entity, RocketModel); //Set model without altering collision bounds
int particle = CreateParticle(entity, "rockettrail"); //critical_rocket_red/blue
Launcher[entity].particle.set(particle);
RemoteRocket[entity] = true;
Launcher[entity].fire_delay = AttackTime.FloatValue;
Launcher[entity].attack_time = GetGameTime() + AttackTime.FloatValue;
RequestFrame(OnRocketSpawnedPost, entity);
}
}
void OnRocketSpawnedPost(int entity)
{
int owner = GetEntPropEnt(entity, Prop_Data, "m_hOwnerEntity");
Launcher[entity].critical = view_as<bool>(GetEntProp(entity, Prop_Send, "m_bCritical"));
if(Launcher[entity].critical)
{
int team = GetClientTeam(owner);
int crit_particle;
switch (team)
{
case 2: crit_particle = CreateParticle(entity, "critical_rocket_red");
case 3: crit_particle = CreateParticle(entity, "critical_rocket_blue");
default: crit_particle = CreateParticle(entity, "critical_rocket_blue");
}
Launcher[entity].particle_crit.set(crit_particle);
}
}
void SetModelOverride(int rocket, int model)
{
for (int i = 0; i < 4; i++)
SetEntProp(rocket, Prop_Send, "m_nModelIndexOverrides", model, _, i);
}
public void OnGameFrame()
{
int rocket = MaxClients + 1;
while ((rocket = FindEntityByClassname2(rocket, "tf_projectile_rocket")) != -1)
{
if (RemoteRocket[rocket] && !OtherRocket[rocket])
{
bool auto = AutoAim.BoolValue;
Reference reference;
reference.set(rocket);
SimulateLauncher(reference, Launcher[rocket], auto);
}
}
}
void SimulateLauncher(Reference rocket, Rocket launcher, bool auto)
{
if (rocket.valid())
{
//Determine how this launcher should aim
float angles[3], aim[3], pos[3], rocket_pos[3];
float aim_vector[3], aim_angles[3];
int owner = GetEntPropEnt(rocket.get(), Prop_Data, "m_hOwnerEntity");
if (!IsValidClient(owner))
return;
if (!auto)
{
GetClientEyePosition(owner, pos);
GetClientEyeAngles(owner, angles);
GetAimPosition(owner, pos, angles, aim);
}
else
{
int target = FindBestTarget(rocket.get(), owner, GetClientTeam(owner));
if (IsValidClient(target))
GetClientAbsOrigin(target, aim); //target's position will be the aim position
else
return;
}
//Aim rocket towards our aim position
GetEntPropVector(rocket.get(), Prop_Data, "m_vecOrigin", rocket_pos);
MakeVectorFromPoints(rocket_pos, aim, aim_vector);
GetVectorAngles(aim_vector, aim_angles);
TeleportEntity(rocket.get(), NULL_VECTOR, aim_angles, NULL_VECTOR);
//Fire the launcher on the given interval
if (launcher.attack_time <= GetGameTime())
{
launcher.attack_time = GetGameTime() + launcher.fire_delay;
int weapon = GetEntPropEnt(rocket.get(), Prop_Send, "m_hOriginalLauncher");
int proj = CreateEntityByName("tf_projectile_rocket");
OtherRocket[proj] = true;
PrecacheSound("weapons/rocket_shoot.wav");
EmitSoundToAll("weapons/rocket_shoot.wav", proj, SNDCHAN_AUTO, 80);
SetEntPropEnt(proj, Prop_Data, "m_hOwnerEntity", owner);
int team = GetClientTeam(owner);
SetVariantInt(team);
AcceptEntityInput(proj, "TeamNum");
SetVariantInt(team);
AcceptEntityInput(proj, "SetTeam");
SetEntProp(proj, Prop_Send, "m_bCritical", view_as<int>(launcher.critical));
SetEntPropEnt(proj, Prop_Send, "m_hOriginalLauncher", weapon); //proper damage rampup and falloff values
//Get forward position from launcher and offset by a few units to prevent collisions
NormalizeVector(aim_vector, aim_vector);
ScaleVector(aim_vector, 20.0);
AddVectors(rocket_pos, aim_vector, rocket_pos);
//Set velocity of rocket
float vel[3];
vel = aim_vector;
NormalizeVector(vel, vel);
ScaleVector(vel, 1100.0);
TeleportEntity(proj, rocket_pos, aim_angles, vel);
DispatchSpawn(proj);
SetEntDataFloat(proj, FindSendPropInfo("CTFProjectile_Rocket", "m_iDeflected") + 4, 90.0); //90 damage for stock rockets
}
}
}
///
/// Get aim position from player view angles
///
void GetAimPosition(int client, float pos[3], float angles[3], float buffer[3])
{
Handle trace = TR_TraceRayFilterEx(pos, angles, MASK_SHOT, RayType_Infinite, FilterSelf, client);
if (TR_DidHit(trace))
TR_GetEndPosition(buffer, trace);
CloseHandle(trace);
}
bool FilterSelf(int entity, int mask, int exclude)
{
if (entity == exclude)
return false;
char classname[64];
GetEntityClassname(entity, classname, sizeof classname);
if (StrContains(classname, "tf_projectile_") != -1)
return false;
return true;
}
///
/// Automatically aquire aim position from nearby targets
///
int FindBestTarget(int rocket, int owner, int team)
{
float closest = 8192.0;
float pos[3], rocket_pos[3];
int best;
GetEntPropVector(rocket, Prop_Data, "m_vecOrigin", rocket_pos);
for (int i = 1; i <= MaxClients; i++)
{
if (i == owner)
continue;
if (!IsValidClient(i))
continue;
if (GetClientTeam(i) == team)
continue;
if (!IsPlayerAlive(i))
continue;
GetClientAbsOrigin(i, pos);
pos[2] += 40.0;
float distance = GetVectorDistance(pos, rocket_pos);
if (distance < closest)
{
Handle trace = TR_TraceRayFilterEx(rocket_pos, pos, MASK_SHOT, RayType_EndPoint, FilterSelf, rocket);
if (TR_DidHit(trace))
{
int entity = TR_GetEntityIndex(trace);
if (entity != i) //not visible, ignore this player
{
CloseHandle(trace);
continue;
}
CloseHandle(trace);
//Player is visible and closer than the last, set them as the new best target
closest = distance;
best = i;
}
else //cant find the player for whatever reason, move on to the next
{
CloseHandle(trace);
continue;
}
}
}
return best;
}
stock int FindEntityByClassname2(int startEnt, const char[] classname)
{
/* If startEnt isn't valid shifting it back to the nearest valid one */
while (startEnt > -1 && !IsValidEntity(startEnt))
startEnt--;
return FindEntityByClassname(startEnt, classname);
}
int CreateParticle(int entity, char[] name)
{
int particle = CreateEntityByName("info_particle_system");
if (IsValidEntity(particle))
{
float pos[3];
GetEntPropVector(entity, Prop_Data, "m_vecOrigin", pos);
pos[2] += 5.0;
TeleportEntity(particle, pos, NULL_VECTOR, NULL_VECTOR);
DispatchKeyValue(particle, "effect_name", name);
SetVariantString("!activator");
AcceptEntityInput(particle, "SetParent", entity, particle, 0);
DispatchSpawn(particle);
ActivateEntity(particle);
AcceptEntityInput(particle, "Start");
}
return particle;
}