Skip to content

Commit

Permalink
Task Sync
Browse files Browse the repository at this point in the history
  • Loading branch information
KWMSources committed Jul 23, 2020
1 parent 08bef01 commit 405a30d
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 16 deletions.
Binary file modified netcoreapp3.1/pedSyncer.dll
Binary file not shown.
Binary file modified netcoreapp3.1/pedSyncer.pdb
Binary file not shown.
43 changes: 39 additions & 4 deletions pedSyncer/client/pedSyncer/class/PedClass.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ class PedClass {
//Currently inactive - Aim-Position of the Ped
weaponAimPos = {x: 0, y: 0, z: 0};

//Currently inactive - Current Task of the Ped with its params
currentTask = null;
currentTaskParams = [];
//Current Task of the Ped with its params
task = null;
taskParams = [];

//Current Scenario the ped is playing
scenario = null;
Expand Down Expand Up @@ -381,6 +381,10 @@ class PedClass {
* maybe by linear regression.
*/
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.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 @@ -422,6 +426,10 @@ class PedClass {
* maybe by linear regression.
*/
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.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 All @@ -437,8 +445,13 @@ class PedClass {
*/
startScenario() {
native.setEntityHeading(this.scriptID, this.heading);
let that = this;
alt.setTimeout(() => {
native.taskStartScenarioInPlace(this.scriptID, this.scenario, 0, false);
native.taskStartScenarioInPlace(that.scriptID, that.scenario, 0, false);

that.task = "taskStartScenarioInPlace";
that.taskParams = [that.scriptID, that.scenario, 0, false];
this.sendTask();
}, 1000);
}

Expand Down Expand Up @@ -489,6 +502,18 @@ class PedClass {
this.heading = ped.heading;
}

sendTask() {
if (this.netOwner == alt.Player.local.id) {
let params = [];
for (let key in this.taskParams) {
if (this.taskParams[key] == this.scriptID) params.push("scriptID");
else params.push(this.taskParams[key]);
}

alt.emitServer("pedSyncer:client:task", this.id, this.task, params);
}
}

/**
* Meta-Data
*/
Expand Down Expand Up @@ -649,6 +674,16 @@ export const Ped = new Proxy(PedClass, {
if (property == "wandering") {
if (value == true) pedTarget.startPath();
else if(pedTarget.scriptID != 0) native.clearPedTasks(pedTarget.scriptID);
} else if (property == "task" && value != "" && peds[pedTarget.id]["wandering"] == false) {
for (let key in peds[pedTarget.id]["taskParams"]) {
if (peds[pedTarget.id]["taskParams"][key] == "scriptID") {
peds[pedTarget.id]["taskParams"][key] = pedTarget.scriptID;
break;
}
}

if(pedTarget.scriptID != 0) native[peds[pedTarget.id][property]](...peds[pedTarget.id]["taskParams"]);
pedTarget.sendTask();
}
return true;
},
Expand Down
Binary file modified pedSyncer/server/pedSyncer.dll
Binary file not shown.
Binary file modified pedSyncer/server/pedSyncer.pdb
Binary file not shown.
1 change: 1 addition & 0 deletions src/PedSyncer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +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.OnPlayerConnect += Events.OnPlayerConnect;

Expand Down
30 changes: 30 additions & 0 deletions src/control/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,5 +243,35 @@ public static void OnEntityRemove(IClient client, AltV.Net.EntitySync.IEntity en
PedMovement.GetInstance().AddPedMovementCalculcation(ped);
}
}

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

if (Ped == null) return;

if (Ped.NetOwner != null && ((PlayerClient)Ped.NetOwner).GetPlayer().Id != Player.Id) return;

bool ParamCheck = false;
if (TaskParams.Length != Ped.TaskParams.Count) ParamCheck = true;
else
{
for (int i = 0; i < TaskParams.Length; i++)
{
if (TaskParams[i] != Ped.TaskParams[i])
{
ParamCheck = true;
break;
}
}
}


if (Ped.Task != TaskString || ParamCheck)
{
Ped.TaskParams = TaskParams.ToList<object>();
Ped.Task = TaskString;
}
}
}
}
41 changes: 29 additions & 12 deletions src/model/Ped.cs
Original file line number Diff line number Diff line change
Expand Up @@ -560,12 +560,32 @@ public Dictionary<int, int> Ammo
public Vector3 WeaponAimPos
{ get; set; }

//Currently inactive - Current Task of the Ped with its params
public string CurrentTask
{ get; set; }
//Current Task of the Ped with its params
public string Task
{
get
{
if (this.TryGetData<string>("task", out string value)) return value;
return "";
}
set
{
this.SetData("task", value);
}
}

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

//Current Scenario the ped is playing
public string Scenario
Expand Down Expand Up @@ -663,9 +683,6 @@ public int CurrentNavmashPositionsIndex
this.Weapons = new List<string>();
this.Ammo = new Dictionary<int, int>();

this.CurrentTask = null;
this.CurrentTaskParams = new List<string>();

this.Flags = new bool[426];

this.NearFinalPosition = false;
Expand Down Expand Up @@ -876,12 +893,12 @@ public void OnWrite(IMValueWriter writer)
writer.Name("health");
writer.Value(this.Health);

writer.Name("currentTask");
writer.Value(this.CurrentTask);
writer.Name("task");
writer.Value(this.Task);

writer.Name("currentTaskParams");
writer.Name("taskParams");
writer.BeginArray();
foreach (string value in this.CurrentTaskParams) writer.Value(value);
foreach (string value in this.TaskParams) writer.Value(value);
writer.EndArray();

writer.Name("scenario");
Expand Down

0 comments on commit 405a30d

Please sign in to comment.