Skip to content

Commit

Permalink
Desync Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
KWMSources committed Jul 24, 2020
1 parent b532a02 commit 697752a
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 35 deletions.
Binary file modified netcoreapp3.1/pedSyncer.dll
Binary file not shown.
Binary file modified netcoreapp3.1/pedSyncer.pdb
Binary file not shown.
19 changes: 15 additions & 4 deletions pedSyncer/client/pedSyncer/class/PedClass.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,10 @@ class PedClass {
else {
let veh = alt.Vehicle.all.filter(v => v.id == this.vehicle)[0];
this.scriptID = native.createPedInsideVehicle(veh.scriptID, 4, native.getHashKey(this.model), this.seat, false, false);
native.taskVehicleDriveWander(this.scriptID, veh.scriptID, 10, 786491);

this.taskParams = ["scriptID", veh.scriptID, 10, 786491];
this.task = "taskVehicleDriveWander";
this.sendTask();
}

//Store this ped by his scriptID as a key
Expand Down Expand Up @@ -276,6 +279,8 @@ class PedClass {
*/
becomeNetOwner() {
this.netOwner = alt.Player.local.id;

if (this.vehicle != null || this.task == "taskVehicleDriveWander") native.taskVehicleDriveWander(this.scriptID, alt.Vehicle.all.filter(v => v.id == this.vehicle)[0].scriptID, 10, 786491);
}

releaseNetOwner() {
Expand Down Expand Up @@ -373,8 +378,8 @@ class PedClass {
*/
native.taskFollowNavMeshToCoord(this.scriptID, this.navmashPositions[this.nextNavMeshStation].x, this.navmashPositions[this.nextNavMeshStation].y, this.navmashPositions[this.nextNavMeshStation].z, 1.0, -1, 0.0, true, 0.0);

this.taskParams = ["scriptID", this.navmashPositions[this.nextNavMeshStation].x, this.navmashPositions[this.nextNavMeshStation].y, this.navmashPositions[this.nextNavMeshStation].z, 1.0, -1, 0.0, true, 0.0];
this.task = "taskFollowNavMeshToCoord";
this.taskParams = [this.scriptID, this.navmashPositions[this.nextNavMeshStation].x, this.navmashPositions[this.nextNavMeshStation].y, this.navmashPositions[this.nextNavMeshStation].z, 1.0, -1, 0.0, true, 0.0];
this.sendTask();
}
}
Expand Down Expand Up @@ -418,8 +423,8 @@ class PedClass {
*/
native.taskFollowNavMeshToCoord(this.scriptID, this.navmashPositions[this.nextNavMeshStation].x, this.navmashPositions[this.nextNavMeshStation].y, this.navmashPositions[this.nextNavMeshStation].z, 1.0, -1, 0.0, true, 0.0);

this.taskParams = ["scriptID", this.navmashPositions[this.nextNavMeshStation].x, this.navmashPositions[this.nextNavMeshStation].y, this.navmashPositions[this.nextNavMeshStation].z, 1.0, -1, 0.0, true, 0.0];
this.task = "taskFollowNavMeshToCoord";
this.taskParams = [this.scriptID, this.navmashPositions[this.nextNavMeshStation].x, this.navmashPositions[this.nextNavMeshStation].y, this.navmashPositions[this.nextNavMeshStation].z, 1.0, -1, 0.0, true, 0.0];
this.sendTask();
}

Expand Down Expand Up @@ -673,7 +678,13 @@ export const Ped = new Proxy(PedClass, {
}
}

if(pedTarget.scriptID != 0) native[peds[pedTarget.id][property]](...peds[pedTarget.id]["taskParams"]);
if(
pedTarget.scriptID != 0 &&
(
peds[pedTarget.id][property] != "taskVehicleDriveWander" ||
pedTarget.netOwner == alt.Player.local.id
)
) native[peds[pedTarget.id][property]](...peds[pedTarget.id]["taskParams"]);
pedTarget.sendTask();
}
return true;
Expand Down
54 changes: 31 additions & 23 deletions pedSyncer/client/pedSyncer/control/PedControler.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ export function startPedControler() {
//Event which fires on ped get streamed in
alt.onServer("entitySync:create", (entityId, entityType, position, newEntityData) => {
if (entityType != pedType) return;
if (Ped.getByID(entityId).vehicle != null) return;
let ped = Ped.getByID(entityId);
if (
typeof ped === "undefined" ||
(ped.vehicle != null && ped.vehicle != "")
) return;
trySpawn(entityId, position, 0, 100, newEntityData);
});

Expand Down Expand Up @@ -92,7 +96,7 @@ export function startPedControler() {
if (entityType != pedType) return;

let ped = Ped.getByID(entityId);
if (typeof ped["vehicle"] !== "undefined") return;
if (typeof ped === "undefined" || ped.vehicle != null) return;

ped.outOfRange();
});
Expand Down Expand Up @@ -179,31 +183,35 @@ export function startPedControler() {

let streamedInVehicle = [];
function checkStreamedInVehicle() {
let newStreamedInVehicle = [];
for (let vehicle of alt.Vehicle.all) {
if (
vehicle.scriptID != 0 &&
vehicle.hasSyncedMeta("ped")
) {
newStreamedInVehicle.push(vehicle.id);

if (streamedInVehicle.indexOf(vehicle.id) == -1) {
let ped = Ped.getByID(vehicle.getSyncedMeta("ped"));
ped.pedSpawnTrys = 0;
ped.pedSpawnTryTime = 100;
ped.spawn();
try {
let newStreamedInVehicle = [];
for (let vehicle of alt.Vehicle.all) {
if (
vehicle.scriptID != 0 &&
vehicle.hasSyncedMeta("ped")
) {
newStreamedInVehicle.push(vehicle.id);

if (streamedInVehicle.indexOf(vehicle.id) == -1) {
let ped = Ped.getByID(parseInt(vehicle.getSyncedMeta("ped")));
ped.pedSpawnTrys = 0;
ped.pedSpawnTryTime = 100;
ped.spawn();
}
}
}
}

for (let vehicleId of streamedInVehicle.filter(vehicleIdCheck => newStreamedInVehicle.indexOf(vehicleIdCheck) == -1)) {
if (
alt.Vehicle.all.filter(v => v.id == vehicleId).length > 0 &&
alt.Vehicle.all.filter(v => v.id == vehicleId)[0].hasSyncedMeta("ped")
) Ped.getByID(alt.Vehicle.all.filter(v => v.id == vehicleId)[0].getSyncedMeta("ped")).outOfRange();
}
for (let vehicleId of streamedInVehicle.filter(vehicleIdCheck => newStreamedInVehicle.indexOf(vehicleIdCheck) == -1)) {
if (
alt.Vehicle.all.filter(v => v.id == vehicleId).length > 0 &&
alt.Vehicle.all.filter(v => v.id == vehicleId)[0].hasSyncedMeta("ped")
) Ped.getByID(alt.Vehicle.all.filter(v => v.id == vehicleId)[0].getSyncedMeta("ped")).outOfRange();
}

streamedInVehicle = newStreamedInVehicle;
streamedInVehicle = newStreamedInVehicle;
} catch (error) {

}

alt.setTimeout(checkStreamedInVehicle, 100);
}
Expand Down
Binary file modified pedSyncer/server/pedSyncer.dll
Binary file not shown.
Binary file modified pedSyncer/server/pedSyncer.pdb
Binary file not shown.
4 changes: 2 additions & 2 deletions src/PedSyncer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public override void OnStart()
*/
Alt.OnClient<IPlayer, Dictionary<string, string>>("pedSyncer:client:firstSpawn", Events.OnFirstSpawn);
Alt.OnClient<IPlayer, object[]>("pedSyncer:client:positions", Events.OnPositionUpdate);
Alt.OnClient<IPlayer, ulong, string, object[]>("pedSyncer:client:task", Events.OnTaskUpdate);
Alt.OnClient<IPlayer, ulong, string, string[]>("pedSyncer:client:task", Events.OnTaskUpdate);

Alt.OnPlayerConnect += Events.OnPlayerConnect;

Expand All @@ -66,7 +66,7 @@ public override void OnStart()
PedMovement.GetInstance();

//Create citizen vehicles
PedVehicles.GetInstance().SpawnRandomCitizenVehicles(2000);
PedVehicles.GetInstance().SpawnRandomCitizenVehicles(3000);

//Create citizen peds who wanders - delete this line if you don't wanna have citizens
Ped.CreateCitizenPeds();
Expand Down
6 changes: 3 additions & 3 deletions src/control/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public static void OnEntityRemove(IClient client, AltV.Net.EntitySync.IEntity en
{
IPlayer pNewPlayer = ((PlayerClient)pNewClient).GetPlayer();

if (pNewPlayer.Exists && pNewPlayer.Id != ((PlayerClient)ped.NetOwner).GetPlayer().Id)
if (pNewPlayer.Exists && (ped.NetOwner == null || pNewPlayer.Id != ((PlayerClient)ped.NetOwner).GetPlayer().Id))
{
ped.NetOwner = pNewClient;
return;
Expand All @@ -244,7 +244,7 @@ public static void OnEntityRemove(IClient client, AltV.Net.EntitySync.IEntity en
}
}

public static void OnTaskUpdate(IPlayer Player, ulong PedId, string TaskString, object[] TaskParams)
public static void OnTaskUpdate(IPlayer Player, ulong PedId, string TaskString, string[] TaskParams)
{
Ped? Ped = Ped.GetByID(PedId);

Expand All @@ -269,7 +269,7 @@ public static void OnTaskUpdate(IPlayer Player, ulong PedId, string TaskString,

if (Ped.Task != TaskString || ParamCheck)
{
Ped.TaskParams = TaskParams.ToList<object>();
Ped.TaskParams = TaskParams.ToList<string>();
Ped.Task = TaskString;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/model/Ped.cs
Original file line number Diff line number Diff line change
Expand Up @@ -587,12 +587,12 @@ public string Task
}
}

public List<object> TaskParams
public List<string> TaskParams
{
get
{
if (this.TryGetData<List<object>>("taskParams", out List<object> value)) return value;
return new List<object>();
if (this.TryGetData<List<string>>("taskParams", out List<string> value)) return value;
return new List<string>();
}
set
{
Expand Down

0 comments on commit 697752a

Please sign in to comment.