-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomEntityService.cs
340 lines (275 loc) · 11.5 KB
/
RandomEntityService.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using CitizenFX.Core;
using CitizenFX.Core.Native;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using PoliceMP.Core.Server.Networking;
using PoliceMP.Main.Core.Server.Enums;
using PoliceMP.Main.Core.Shared;
using PoliceMP.Shared.Enums;
using PoliceMP.Shared.Models;
using PoliceMP.Shared.Options;
using Debug = CitizenFX.Core.Debug;
namespace PoliceMP.Server.Services
{
public enum RandomPedType
{
Random,
Male,
Female
}
public interface IRandomEntityService
{
PedHash FetchRandomPed(RandomPedType pedType = RandomPedType.Random);
Task<Ped> GenerateRandomPed(Vector3 spawnPosition, float heading, RandomPedType pedType);
uint FetchRandomVehicle();
uint FetchRandomVehicle(VehicleDataClass vehicleDataClass);
Task<Vehicle> GenerateRandomVehicle(Vector3 spawnPosition, float heading, bool randomClass,
VehicleDataClass vehicleDataClass);
Task<List<Entity>> GenerateRandomVehicleWithPed(Vector3 spawnPosition, float heading, bool randomClass,
VehicleDataClass vehicleDataClass, uint pedHash);
}
public class RandomEntityService : Controller , IRandomEntityService
{
private static List<PedData> _pedList;
private static List<VehicleData> _vehicleList;
public RandomEntityService()
{
}
public override Task Started()
{
var pedFileContents = File.ReadAllText($"{AppContext.BaseDirectory}\\server\\Options\\Peds.conf.json");
_pedList = JsonConvert.DeserializeObject<List<PedData>>(pedFileContents);
var vehicleFileContents = File.ReadAllText($"{AppContext.BaseDirectory}\\server\\Options\\Vehicles.conf.json");
_vehicleList = JsonConvert.DeserializeObject<List<VehicleData>>(vehicleFileContents);
return Task.CompletedTask;
}
/// <summary>
/// Generates a Random Ped at a Position
/// </summary>
/// <param name="spawnPosition"></param>
/// <param name="heading"></param>
/// <param name="pedType"></param>
/// <returns>Returns Ped if spawned and has owner, else Returns Null</returns>
public async Task<Ped> GenerateRandomPed(Vector3 spawnPosition, float heading, RandomPedType pedType)
{
// Wait for main thread
await Delay(0);
var pedHash = FetchRandomPed(pedType);
var pedHandle = API.CreatePed(1, (uint) pedHash, spawnPosition.X, spawnPosition.Y, spawnPosition.Z, heading,
true, true);
await BaseScript.Delay(1000);
var sw = new Stopwatch();
sw.Start();
while (!API.DoesEntityExist(pedHandle))
{
if (sw.Elapsed > TimeSpan.FromSeconds(3))
{
return null;
}
await Delay(10);
}
var ped = (Ped) Entity.FromHandle(pedHandle);
sw.Restart();
while (ped == null)
{
if (sw.Elapsed > TimeSpan.FromSeconds(3))
{
return null;
}
ped = (Ped) Entity.FromHandle(pedHandle);
await Delay(10);
}
sw.Restart();
while (ped.NetworkId == 0)
{
if (sw.Elapsed > TimeSpan.FromSeconds(3))
{
return null;
}
await Delay(10);
}
sw.Restart();
while (ped.Owner == null && sw.Elapsed.Minutes < 5)
{
await Delay(10);
}
return ped.Owner == null ? null : ped;
}
public PedHash FetchRandomPed(RandomPedType pedType = RandomPedType.Random)
{
var random = new Random();
if (pedType == RandomPedType.Random)
{
var probability = random.Next(101);
pedType = probability <= 20 ? RandomPedType.Female : RandomPedType.Male;
}
if (pedType == RandomPedType.Male)
{
var maleList =
_pedList.Where(x => x.Pedtype.ToLower().Contains("civmale") && x.CanSpawnInCar).ToList();
var rndMaleIndex = random.Next(maleList.Count -1);
var randomMalePedData = maleList[rndMaleIndex];
var malePedHashUint = Convert.ToUInt32(randomMalePedData.Hash);
var malePedHash = (PedHash) malePedHashUint;
return malePedHash;
}
var femaleList =
_pedList.Where(x => x.Pedtype.ToLower().Contains("civfemale") && x.CanSpawnInCar).ToList();
var rndFemaleIndex = random.Next(femaleList.Count - 1);
var randomFemalePedData = femaleList[rndFemaleIndex];
var femalePedHashUint = Convert.ToUInt32(randomFemalePedData.Hash);
var femalePedHash = (PedHash) femalePedHashUint;
return femalePedHash;
}
/// <summary>
/// Gets a Random Vehicle and returns when spawned
/// </summary>
/// <param name="spawnPosition"></param>
/// <param name="heading"></param>
/// <param name="randomClass">Ignore vehicleDataClass</param>
/// <param name="vehicleDataClass"></param>
/// <returns>Vehicle if Spawned, Null if not!</returns>
public async Task<Vehicle> GenerateRandomVehicle(Vector3 spawnPosition, float heading, bool randomClass,
VehicleDataClass vehicleDataClass)
{
await Delay(0);
var vehicleHash = randomClass ? FetchRandomVehicle() : FetchRandomVehicle(vehicleDataClass);
var vehicleHandle = API.CreateVehicle(vehicleHash, spawnPosition.X, spawnPosition.Y, spawnPosition.Z,
heading, true, true);
Debug.WriteLine($"VehicleHandle: {vehicleHandle}");
await BaseScript.Delay(1000);
var sw = new Stopwatch();
sw.Start();
while (!API.DoesEntityExist(vehicleHandle))
{
if (sw.Elapsed > TimeSpan.FromSeconds(3))
{
return null;
}
Debug.WriteLine("Entity not existing");
await Delay(10);
}
var vehicle = (Vehicle) Entity.FromHandle(vehicleHandle);
sw.Restart();
while (vehicle == null)
{
if (sw.Elapsed > TimeSpan.FromSeconds(3))
{
return null;
}
Debug.WriteLine("Vehicle is NULL");
vehicle = (Vehicle) Entity.FromHandle(vehicleHandle);
await Delay(10);
}
sw.Restart();
while (vehicle.NetworkId == 0)
{
if (sw.Elapsed > TimeSpan.FromSeconds(3))
{
return null;
}
Debug.WriteLine("NetworkID 0");
await Delay(10);
}
sw.Restart();
while (vehicle.Owner == null && sw.Elapsed.Minutes < 5)
{
Debug.WriteLine("Vehicle Owner is null");
await Delay(10);
}
return vehicle.Owner == null ? null : vehicle;
}
/// <summary>
///
/// </summary>
/// <param name="spawnPosition"></param>
/// <param name="heading"></param>
/// <param name="randomClass"></param>
/// <param name="vehicleDataClass"></param>
/// <param name="pedHash"></param>
/// <returns>Entity 1 = Vehicle, Entity 2 = Ped</returns>
public async Task<List<Entity>> GenerateRandomVehicleWithPed(Vector3 spawnPosition, float heading, bool randomClass,
VehicleDataClass vehicleDataClass, uint pedHash)
{
Debug.WriteLine("Generating Random Vehicle");
var vehicle = await GenerateRandomVehicle(spawnPosition, heading, randomClass, vehicleDataClass);
await BaseScript.Delay(1000);
var sw = new Stopwatch();
sw.Start();
while (vehicle == null)
{
if (sw.Elapsed > TimeSpan.FromSeconds(3))
{
return null;
}
Debug.WriteLine("Vehicle is NULL");
vehicle = await GenerateRandomVehicle(spawnPosition, heading, randomClass, vehicleDataClass);
await Delay(10);
}
sw.Restart();
while (vehicle.NetworkId == 0)
{
if (sw.Elapsed > TimeSpan.FromSeconds(3))
{
return null;
}
Debug.WriteLine("NetworkID 0");
await Delay(10);
}
sw.Restart();
while (vehicle.Owner == null && sw.Elapsed.Minutes < 5)
{
Debug.WriteLine("Vehicle Owner is null");
await Delay(10);
}
var ped = await GenerateRandomPed(spawnPosition, heading, RandomPedType.Random);
sw.Restart();
while (!API.DoesEntityExist(ped.Handle))
{
if (sw.Elapsed.Seconds > 5)
{
return null;
}
Debug.WriteLine("Ped Doesn't Exist!");
await Delay(10);
}
API.TaskWarpPedIntoVehicle(ped.Handle, vehicle.Handle, (int)VehicleSeat.Driver);
Debug.WriteLine($"Spawned {ped.Handle} into {vehicle.Handle}");
var returnList = new List<Entity>
{
vehicle,
ped
};
return returnList;
}
public uint FetchRandomVehicle()
{
Debug.WriteLine($"Fetching Random Vehicle");
var random = new Random();
Debug.WriteLine(_vehicleList.Count.ToString());
var vehicleList = _vehicleList.Where(v => v.Class != VehicleDataClass.Emergency && v.Class != VehicleDataClass.Military && v.Class != VehicleDataClass.Helicopter && v.Class != VehicleDataClass.Plane && v.Class != VehicleDataClass.Boat && v.Class != VehicleDataClass.Rail).ToList();
var randomVehicleIndex = random.Next(vehicleList.Count - 1);
var randomVehicleData = vehicleList[randomVehicleIndex];
var vehicleHashUint = Convert.ToUInt32(randomVehicleData.Hash);
return vehicleHashUint;
}
public uint FetchRandomVehicle(VehicleDataClass vehicleDataClass)
{
Debug.WriteLine($"Fetching Vehicle by class");
var random = new Random();
var vehicleList = _vehicleList.Where(v => v.Class == vehicleDataClass).ToList();
Debug.WriteLine($"Found {vehicleList.Count} Vehicles");
var randomIndex = random.Next(_vehicleList.Count - 1);
var randomVehicle = vehicleList[randomIndex];
var vehicleHashUint = Convert.ToUInt32(randomVehicle.Hash);
return vehicleHashUint;
}
}
}