This repository was archived by the owner on Dec 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathRopes.cs
381 lines (318 loc) · 13.7 KB
/
Ropes.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
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
377
378
379
380
381
using CitizenFX.Core;
using CitizenFX.Core.Native;
using PocceMod.Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace PocceMod.Client
{
public class Ropes : BaseScript
{
public static readonly float MaxLength = Config.GetConfigFloat("MaxRopeLength");
private const uint Ropegun = 0x44AE7910; // WEAPON_POCCE_ROPEGUN
public static readonly Controls.InputPair RopegunWindKey = Config.GetConfigControl("RopegunWindKey");
private static readonly bool RopegunLateWind = Config.GetConfigBool("RopegunLateWind");
public static readonly Controls.InputPair RopeClearKey = Config.GetConfigControl("RopeClearKey");
private static int _nextRopeID;
private static bool _ropegunWindStarted;
private static readonly RopeSet _ropes = new RopeSet();
private static readonly RopegunState _ropegunState = new RopegunState();
private static readonly HashSet<int> _ropegunRopes = new HashSet<int>();
private static readonly Dictionary<int, DateTime> _expirations = new Dictionary<int, DateTime>();
private static readonly Vector3 _skelRootOffset;
[Flags]
public enum ModeFlag
{
Normal = 0,
Tow = 1,
Ropegun = 2,
Grapple = 4
}
static Ropes()
{
API.AddTextEntryByHash(0x6FCC4E8A, "Pocce Ropegun"); // WT_POCCE_ROPEGUN
var playerPed = API.GetPlayerPed(-1);
var skelRootCoords = API.GetPedBoneCoords(playerPed, 0x0, 0f, 0f, 0f);
_skelRootOffset = API.GetOffsetFromEntityGivenWorldCoords(playerPed, skelRootCoords.X, skelRootCoords.Y, skelRootCoords.Z);
}
public Ropes()
{
EventHandlers["PocceMod:AddRope"] += new Func<string, int, int, int, Vector3, Vector3, float, Task>(NetAddRope);
EventHandlers["PocceMod:SetRopeLength"] += new Action<string, int, float>(NetSetRopeLength);
EventHandlers["PocceMod:RemoveRope"] += new Action<string, int>(NetRemoveRope);
TriggerServerEvent("PocceMod:RequestRopes");
Tick += Telemetry.Wrap("ropegun", UpdateRopegun);
Tick += Telemetry.Wrap("ropes", UpdateRopes);
Tick += Telemetry.Wrap("rope_hotkeys", UpdateRopeHotkeys);
}
private static void AdjustOffsetForTowing(int entity, ref Vector3 offset, float towOffset)
{
if (!API.IsEntityAVehicle(entity))
return;
var model = (uint)API.GetEntityModel(entity);
var min = Vector3.Zero;
var max = Vector3.Zero;
API.GetModelDimensions(model, ref min, ref max);
offset = new Vector3(0f, 1f, 0f);
if (towOffset > 0)
offset *= (max.X * towOffset);
else
offset *= (-min.X * towOffset);
}
private static bool IsOtherPlayerEntity(int entity)
{
var playerID = API.PlayerId();
if (API.IsEntityAPed(entity))
{
return API.IsPedAPlayer(entity) && API.NetworkGetPlayerIndexFromPed(entity) != playerID;
}
else if (API.IsEntityAVehicle(entity))
{
if (API.GetPedInVehicleSeat(entity, -1) == API.GetPlayerPed(-1))
return false;
var players = Vehicles.GetPlayers(entity);
return players.Any(player => player != playerID);
}
return false;
}
public static void PlayerAttach(int entity, Vector3 offset, ModeFlag mode = ModeFlag.Normal)
{
var player = API.GetPlayerPed(-1);
if (API.IsPedInAnyVehicle(player, false))
Attach(API.GetVehiclePedIsIn(player, false), entity, Vector3.Zero, offset, mode);
else
Attach(player, entity, _skelRootOffset, offset, mode);
}
public static void AttachToClosest(IEnumerable<int> entities, bool tow = false)
{
if (Common.GetClosestEntity(entities, out int closest))
PlayerAttach(closest, Vector3.Zero, tow ? ModeFlag.Tow : ModeFlag.Normal);
else
Common.Notification("Nothing in range");
}
public static int? Attach(int entity1, int entity2, Vector3 offset1, Vector3 offset2, ModeFlag mode = ModeFlag.Normal)
{
if (!Permission.CanDo(Ability.RopeOtherPlayer) && (IsOtherPlayerEntity(entity1) || IsOtherPlayerEntity(entity2)))
{
Common.Notification("You are not allowed to attach rope to another player");
return null;
}
if ((mode & ModeFlag.Tow) == ModeFlag.Tow)
{
AdjustOffsetForTowing(entity1, ref offset1, -0.75f);
AdjustOffsetForTowing(entity2, ref offset2, 0.75f);
}
if ((mode & ModeFlag.Ropegun) == ModeFlag.Ropegun)
{
_ropegunState.Update(ref entity1, ref entity2, ref offset1, ref offset2, out bool clearLast);
if (clearLast)
ClearLast();
}
else
{
_ropegunState.Clear();
}
if (entity1 == entity2 && entity1 > 0)
return null;
int ObjToNet(int entity)
{
if (entity == 0)
return 0;
return API.ObjToNet(entity);
}
Vector3 GetPos(int entity, Vector3 offset)
{
if (entity == 0)
return offset;
return API.GetOffsetFromEntityInWorldCoords(entity, offset.X, offset.Y, offset.Z);
}
var id = ++_nextRopeID;
var length = (GetPos(entity1, offset1) - GetPos(entity2, offset2)).Length();
TriggerServerEvent("PocceMod:AddRope", id, ObjToNet(entity1), ObjToNet(entity2), offset1, offset2, length);
if ((mode & ModeFlag.Ropegun) == ModeFlag.Ropegun)
_ropegunRopes.Add(id);
if ((mode & ModeFlag.Grapple) == ModeFlag.Grapple)
TriggerServerEvent("PocceMod:SetRopeLength", id, 1f);
if (entity1 == 0 && entity2 == 0)
_expirations.Add(id, DateTime.Now + TimeSpan.FromMinutes(1));
return id;
}
public static void ClearAll()
{
foreach (var rope in _ropes.GetPlayerRopes(Common.PlayerID.ToString()).ToArray())
TriggerServerEvent("PocceMod:RemoveRope", rope.ID);
}
public static void ClearLast()
{
var ropes = _ropes.GetPlayerRopes(Common.PlayerID.ToString()).ToArray();
if (ropes.Length > 0)
{
var lastRopeID = ropes.Max(rope => rope.ID);
TriggerServerEvent("PocceMod:RemoveRope", lastRopeID);
}
}
public static void ClearPlayer()
{
var player = API.GetPlayerPed(-1);
if (_ropes.IsAnyRopeAttachedToEntity(player))
TriggerServerEvent("PocceMod:RemoveEntityRopes", API.PedToNet(player));
if (API.IsPedInAnyVehicle(player, false))
{
int vehicle = API.GetVehiclePedIsIn(player, false);
if (_ropes.IsAnyRopeAttachedToEntity(vehicle))
TriggerServerEvent("PocceMod:RemoveEntityRopes", API.VehToNet(vehicle));
}
}
private static async Task NetAddRope(string player, int id, int netEntity1, int netEntity2, Vector3 offset1, Vector3 offset2, float length)
{
var rope = await RopeWrapper.Create(player, id, netEntity1, netEntity2, offset1, offset2, length);
if (player == Common.PlayerID.ToString())
rope.OnDespawn += _ => TriggerServerEvent("PocceMod:RemoveRope", id);
_ropes.AddRope(rope);
if (!API.RopeAreTexturesLoaded())
API.RopeLoadTextures();
}
private static void NetSetRopeLength(string player, int id, float length)
{
var rope = _ropes.GetRope(player, id);
if (rope != null)
rope.Length = length;
}
private static void NetRemoveRope(string player, int id)
{
if (player == Common.PlayerID.ToString())
_ropegunRopes.Remove(id);
_ropes.RemoveRope(player, id);
}
public static void EquipRopeGun()
{
var player = API.GetPlayerPed(-1);
Weapons.Give(player, Ropegun);
API.SetCurrentPedVehicleWeapon(player, Ropegun);
}
private static bool TryShootRopegun(float distance, out int target, out Vector3 offset)
{
var player = Common.GetPlayerPedOrVehicle();
Common.GetAimCoords(out Vector3 rayBegin, out Vector3 rayEnd, distance);
var ray = API.StartShapeTestRay(rayBegin.X, rayBegin.Y, rayBegin.Z, rayEnd.X, rayEnd.Y, rayEnd.Z, 1 | 2 | 4 | 8 | 16 | 32, player, 0);
target = 0;
offset = Vector3.Zero;
bool hit = false;
var coords = Vector3.Zero;
var normal = Vector3.Zero;
API.GetShapeTestResult(ray, ref hit, ref coords, ref normal, ref target);
if (hit)
{
switch (API.GetEntityType(target))
{
case 1:
case 2:
if (coords == Vector3.Zero)
offset = coords;
else
offset = API.GetOffsetFromEntityGivenWorldCoords(target, coords.X, coords.Y, coords.Z);
return true;
case 3:
if (API.NetworkGetEntityIsNetworked(target))
{
if (coords == Vector3.Zero)
offset = coords;
else
offset = API.GetOffsetFromEntityGivenWorldCoords(target, coords.X, coords.Y, coords.Z);
return true;
}
break;
}
if (Permission.CanDo(Ability.RopeGunStaticObjects) && coords != Vector3.Zero)
{
offset = coords;
target = 0;
return true;
}
}
return false;
}
private static async Task UpdateRopegun()
{
var playerID = API.PlayerId();
var player = API.GetPlayerPed(-1);
if (API.GetSelectedPedWeapon(player) != (int)Ropegun)
{
await Delay(100);
return;
}
bool isAiming = false;
if (API.IsPlayerFreeAiming(playerID))
{
isAiming = true;
}
else if (API.IsControlPressed(0, 25)) // INPUT_AIM
{
API.ShowHudComponentThisFrame(14); // crosshair
isAiming = true;
}
if (!isAiming)
return;
var attackControl = API.IsPedInAnyVehicle(player, false) ? 69 : 24; // INPUT_VEH_ATTACK; INPUT_ATTACK
var grapple = RopegunWindKey.IsPressed;
if (API.IsControlJustPressed(0, attackControl) && TryShootRopegun(MaxLength, out int target, out Vector3 offset))
{
PlayerAttach(target, offset, grapple ? ModeFlag.Ropegun | ModeFlag.Grapple : ModeFlag.Ropegun);
if (!API.IsPlayerFreeAiming(playerID)) // force shoot effect
{
var coords = (target == 0) ? offset : API.GetOffsetFromEntityInWorldCoords(target, offset.X, offset.Y, offset.Z);
if (API.IsPedInAnyVehicle(player, false))
API.SetVehicleShootAtTarget(player, 0, coords.X, coords.Y, coords.Z);
else
API.SetPedShootsAtCoord(player, coords.X, coords.Y, coords.Z, false);
}
}
}
private static async Task UpdateRopes()
{
await Delay(10);
foreach (var rope in _ropes.Ropes.ToArray())
{
rope.Update();
}
var now = DateTime.Now;
foreach (var rope in _expirations.ToArray())
{
if (rope.Value < now)
{
TriggerServerEvent("PocceMod:RemoveRope", rope.Key);
_expirations.Remove(rope.Key);
}
}
}
private static Task UpdateRopeHotkeys()
{
if (RopeClearKey.IsJustPressed)
{
ClearPlayer();
}
if (RopegunLateWind && RopegunWindKey.Exists)
{
if (RopegunWindKey.IsJustPressed && !Common.IsPlayerAiming())
{
_ropegunWindStarted = true;
}
if (RopegunWindKey.IsJustReleased && _ropegunWindStarted)
{
_ropegunWindStarted = false;
if (!Common.IsPlayerAiming())
{
var player = Common.PlayerID.ToString();
var ropes = _ropes.GetEntityRopes(Common.GetPlayerPedOrVehicle()).Where(rope => rope.Player == player).Select(rope => rope.ID).Intersect(_ropegunRopes);
foreach (var rope in ropes.ToArray())
{
TriggerServerEvent("PocceMod:SetRopeLength", rope, 1f);
}
}
}
}
return Task.FromResult(0);
}
}
}