-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainThread.cs
278 lines (263 loc) · 16.7 KB
/
MainThread.cs
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
using System;
using System.Linq; //for checking strings
using Rage;
using System.Drawing;
namespace RealisticTaser
{
class MainThread
{
public static Ped player = Game.LocalPlayer.Character;
public static Entity Suspect;
private static int FinalTaserSuccess;
public static int ShotCount = 0;
public static bool DisplayAmmoCount;
public static void Main()
{
GameFiber.StartNew(delegate
{
try
{
while (true)
{
GameFiber.Yield();
while (player.Exists()) //this is to sleep the plugin when the player does not using any weapon. This is also a workaround to a crash when LSPDFR reloads.
{
GameFiber.Yield();
player = Game.LocalPlayer.Character; //test this
if (player.Exists())
{
if (player.IsAiming || player.IsShooting)
{
break;
}
}
}
if (player.Exists()) //this is to prevent the plugin from crashing when lspdfr reloads and resets the ped and its inventory.
{
if (player.Inventory.EquippedWeapon != null)
{
if (player.Inventory.EquippedWeapon.Hash == WeaponHash.StunGun)
{
Suspect = Game.LocalPlayer.GetFreeAimingTarget(); //gets the closest target the player is aiming at
if (Suspect.Exists() && Suspect.IsValid())
{
if (Config.LogDebugMessages) Game.LogTrivial("REALISTICTASER: SUSPECT VALID");
System.Random rando = new System.Random();
if (Config.TaserSuccessRange) //Taser Success based on Range Enabled
{
float distance = player.DistanceTo(Suspect);
FinalTaserSuccess = (int)(Config.ScaleFactor * (60.7539 / Math.Pow(distance, 0.824732)) - 12.821); //taser success rate based on npr article (in metres)
if (Config.LogDebugMessages) Game.LogTrivial("REALISTICTASER: TASER SUCCESS PROBABILITY: " + FinalTaserSuccess);
int TaserSuccess = rando.Next(Config.ScaleFactor * 5, 101); //remove the bottom Scale Factor * 5 percent chance of success
if (Config.LogDebugMessages) Game.LogTrivial("REALISTICTASER: TASER SUCCESS PROBABILITY MUST BE HIGHER THAN: " + TaserSuccess);
if (FinalTaserSuccess > TaserSuccess)
{
if (Config.LogDebugMessages) Game.LogTrivial("REALISTICTASER: TASER SUCCESS");
if (Suspect.Exists()) Suspect.IsInvincible = false;
if (player.Exists()) while (player.IsWeaponReadyToShoot) GameFiber.Wait(0);
}
else
{
if (Suspect.Exists()) Suspect.IsInvincible = true;
if (Config.LogDebugMessages) Game.LogTrivial("REALISTICTASER: TASER NOT SUCCESS");
try
{
if (player.Exists()) while (Game.LocalPlayer.Character.IsWeaponReadyToShoot) GameFiber.Wait(0);
}
catch (Rage.Exceptions.InvalidHandleableException)
{
Game.LogTrivial("REALISTICTASER - KNOWN ISSUE / INVALID HANDABLE LINE 72 CAUGHT / CAUSED BY EUP CHARACTER SWITCH. PLEASE RELOAD LSPDFR TO FIX IT.");
}
if (Suspect.Exists()) Suspect.IsInvincible = false;
}
}
else //Taser Success based on Range Disabled
{
int TaserSuccess = rando.Next(0, 101);
if (Config.LogDebugMessages) Game.LogTrivial("REALISTICTASER: TASER SUCCESS PROBABILITY: " + TaserSuccess);
if (TaserSuccess < Config.TaserSuccess)
{
if (Config.LogDebugMessages) Game.LogTrivial("REALISTICTASER: TASER SUCCESS");
if (Suspect.Exists()) Suspect.IsInvincible = false;
if (player.Exists()) while (player.IsWeaponReadyToShoot) GameFiber.Wait(0);
}
else
{
if (Suspect.Exists()) Suspect.IsInvincible = true;
if (Config.LogDebugMessages) Game.LogTrivial("REALISTICTASER: TASER NOT SUCCESS");
if (player.Exists()) while (player.IsWeaponReadyToShoot) GameFiber.Wait(0);
if (Suspect.Exists()) Suspect.IsInvincible = false;
}
}
}
}
}
}
//end of loop
}
}
catch (System.Threading.ThreadAbortException) { }
catch (Exception e)
{
Game.LogTrivial("==========REALISTICTASER: ERROR CAUGHT==========");
string error = e.ToString();
Game.LogTrivial("ERROR: " + error);
Game.LogTrivial("IN - REALISTICTASER MAIN THREAD");
Game.DisplayNotification("There was an ~r~Error~w~ Caught with ~b~RealisticTaser. ~w~Please Check Your ~g~Log File.~w~ Sorry for the Inconvenience!");
//Game.DisplayNotification("Error: ~r~" + error);
Game.LogTrivial("If You Believe this is a Bug, Please Report it on my Discord Server. Thanks!");
Game.LogTrivial("==========REALISTICTASER: ERROR CAUGHT==========");
}
});
}
public static void ShotCounter()
{
GameFiber.StartNew(delegate
{
Game.LogTrivial("REALISTICTASER: SHOTCOUNTER STARTED");
try
{
while (player.Exists())
{
GameFiber.Yield();
DisplayAmmoCount = false; //always shut off taser UI unless player is aiming with taser in hand
if (player.Exists())
{
if (player.IsInAnyPoliceVehicle && Config.ReplenishShots)
{
Game.LogTrivial("REALISTICTASER: Player entered Police Vehicle. Reloading...");
ShotCount = 0; //replenish taser ammo if in vehicle
//some audio? or notification?
//
}
}
if (player.Exists() && player.IsAiming)
{
if (player.Inventory.EquippedWeapon != null)
{
if (player.Inventory.EquippedWeapon.Hash == WeaponHash.StunGun)
{
DisplayAmmoCount = true; //player aiming, enable ammo UI
if (Config.LimitShots) Game.FrameRender += DrawAmmoCount; //call the ammo UI with FrameRender (moved)
while (player.IsAiming)
{
//if (Config.LimitShots) Game.FrameRender += DrawAmmoCount; //call the ammo UI with FrameRender
GameFiber.Yield();
if (ShotCount >= Config.Shots) //Taser out of Ammo
{
if (Config.LogDebugMessages) Game.LogTrivial("REALISTICTASER: TASER OUT OF AMMO");
if (Game.IsKeyDown(Config.TaserDeployKey)) PlaySound(); //Taser Dry Fire audio if mouse1/main weapon key depressed
if (player.Exists()) Rage.Native.NativeFunction.Natives.DISABLE_PLAYER_FIRING(player, true); //prevent the taser from firing
}
if (player.Exists() && player.IsShooting)
{
if (Config.LogDebugMessages) Game.LogTrivial("REALISTICTASER: TASER VALID");
if (ShotCount >= Config.Shots) //Taser out of ammo
{
if (Config.LogDebugMessages) Game.LogTrivial("REALISTICTASER: TASER OUT OF AMMO");
if (player.Exists()) Rage.Native.NativeFunction.Natives.DISABLE_PLAYER_FIRING(player, true);
}
else
{
ShotCount++; //Increase the amount of shots on this Taser
if (Config.LogDebugMessages) Game.LogTrivial("REALISTICTASER: Shot Count on this Taser is " + ShotCount);
if (Config.DoReloads && player.Exists())
{
if (ShotCount < Config.Shots) //If there are more shots left in the taser, reload animation
{
player.Tasks.PlayAnimation("anim@weapons@pistol@flare_str", "reload_aim", 0.8f, AnimationFlags.SecondaryTask | AnimationFlags.UpperBodyOnly);
}
}
}
if (player.Exists()) Rage.Native.NativeFunction.Natives.DISABLE_PLAYER_FIRING(player, false); //Allow player to fire again
break; //Break back into loop
}
if (!player.IsAiming) //set player firing back after shot is fired
{
if (player.Exists()) Rage.Native.NativeFunction.Natives.DISABLE_PLAYER_FIRING(player, false);
break;
}
}
}
}
}
}
}
catch (System.Threading.ThreadAbortException) { }
catch (Exception e)
{
Game.LogTrivial("==========REALISTICTASER: ERROR CAUGHT==========");
string error = e.ToString();
Game.LogTrivial("ERROR: " + error);
Game.LogTrivial("IN - REALISTICTASER SHOT COUNTER");
Game.DisplayNotification("There was an ~r~Error~w~ Caught with ~b~RealisticTaser. ~w~Please Check Your ~g~Log File.~w~ Sorry for the Inconvenience!");
//Game.DisplayNotification("Error: ~r~" + error);
Game.LogTrivial("If You Believe this is a Bug, Please Report it on my Discord Server. Thanks!");
Game.LogTrivial("==========REALISTICTASER: ERROR CAUGHT==========");
}
});
}
public static void PlaySound()
{
try
{
if (Config.LogDebugMessages) Game.LogTrivial("REALISTICTASER: SOUND PLAY");
System.Media.SoundPlayer sound = new System.Media.SoundPlayer();
sound.SoundLocation = @"lspdfr\audio\sfx\GTA5_SHT_DRYFIRE_COPY.wav";
GameFiber.StartNew(delegate
{
sound.Load();
sound.Play();
GameFiber.Wait(300);
sound.Stop();
});
}
catch (System.Threading.ThreadAbortException) { }
catch (System.IO.FileNotFoundException)
{
Game.DisplayNotification("The ~b~Audio File~w~ for ~g~RealisticTaser~w~ is ~r~not Installed Properly.~w~ Please ~b~Reinstall~w~ the Plugin Properly.");
Game.LogTrivial("==========REALISTICTASER: ERROR CAUGHT==========");
Game.LogTrivial("AUDIO FILE FOR REALISTICTASER NOT INSTALLED. PLEASE REINSTALL THE PLUGIN PROPERLY.");
Game.LogTrivial("==========REALISTICTASER: ERROR CAUGHT==========");
}
catch (Exception e)
{
Game.LogTrivial("==========REALISTICTASER: ERROR CAUGHT==========");
string error = e.ToString();
Game.LogTrivial("ERROR: " + error);
Game.LogTrivial("IN - REALISTICTASER SOUND PLAYER");
Game.DisplayNotification("There was an ~r~Error~w~ Caught with ~b~RealisticTaser. ~w~Please Check Your ~g~Log File.~w~ Sorry for the Inconvenience!");
//Game.DisplayNotification("Error: ~r~" + error);
Game.LogTrivial("If You Believe this is a Bug, Please Report it on my Discord Server. Thanks!");
Game.LogTrivial("==========REALISTICTASER: ERROR CAUGHT==========");
}
}
public static void DrawAmmoCount(System.Object sender, Rage.GraphicsEventArgs e)
{
try
{
if (DisplayAmmoCount)
{
if (ShotCount < Config.Shots) e.Graphics.DrawText("" + (Config.Shots - ShotCount) + "", "pricedown", Config.ShotCountSize, new PointF(Config.ShotCountX, Config.ShotCountY), System.Drawing.Color.White);
else e.Graphics.DrawText("" + (Config.Shots - ShotCount) + "", "pricedown", Config.ShotCountSize, new PointF(Config.ShotCountX, Config.ShotCountY), System.Drawing.Color.Crimson);
}
else
{
Game.FrameRender -= DrawAmmoCount;
}
}
catch (System.Threading.ThreadAbortException) { }
catch (Exception f)
{
Game.LogTrivial("==========REALISTICTASER: ERROR CAUGHT==========");
string error = f.ToString();
Game.LogTrivial("ERROR: " + error);
Game.LogTrivial("IN - REALISTICTASER AMMO COUNT UI DRAWER");
Game.DisplayNotification("There was an ~r~Error~w~ Caught with ~b~RealisticTaser. ~w~Please Check Your ~g~Log File.~w~ Sorry for the Inconvenience!");
//Game.DisplayNotification("Error: ~r~" + error);
Game.LogTrivial("If You Believe this is a Bug, Please Report it on my Discord Server. Thanks!");
Game.LogTrivial("==========REALISTICTASER: ERROR CAUGHT==========");
}
}
}
}