Skip to content

Commit

Permalink
Citizen Vehicle Sync
Browse files Browse the repository at this point in the history
  • Loading branch information
KWMSources committed Jul 24, 2020
1 parent 405a30d commit b532a02
Show file tree
Hide file tree
Showing 14 changed files with 417 additions and 50 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ src/bin/
.vs/

src/pedSyncer.csproj.user
src/Properties/PublishProfiles/FolderProfile.pubxml
src/Properties/PublishProfiles/FolderProfile.pubxml.user
*.pubxml
src/Properties/PublishProfiles/FolderProfile.pubxml.user
src/Properties/PublishProfiles/FolderProfile.pubxml
61 changes: 26 additions & 35 deletions pedSyncer/client/pedSyncer/class/PedClass.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import native from 'natives';
import { loadModel } from "../utils/functions.mjs";
import { unloadModel } from "../utils/functions.mjs";

var i = 0;
var peds = {};
var pedsToScriptID = {};
var pedsProxies = {};
Expand Down Expand Up @@ -92,10 +91,10 @@ class PedClass {
//Will contain information if the ped is invincible
invincible = null;

//Currently inactive - The vehicle the ped sits in
//The vehicle the ped sits in
vehicle = null;

//Currently inactive - If the ped is in a vehicle, this tells the current seat of the ped
//If the ped is in a vehicle, this tells the current seat of the ped
seat = null;

//Currently inactive
Expand Down Expand Up @@ -154,9 +153,7 @@ class PedClass {
this.flags = {};
peds[ped.id] = this;

for (let key of Object.keys(this)) {
if (typeof this[key] !== "undefined") this[key] = ped[key];
}
for (let key of Object.keys(ped)) if (typeof this[key] !== "undefined") this[key] = ped[key];
}

/**
Expand All @@ -165,21 +162,12 @@ class PedClass {
* users. This client decide the style of this ped and gives it back to the
* server for sync.
*/
async firstSpawn() {
firstSpawn() {
//Check if created - if so: stop!
if (this.created) return;
this.created = true;

//Load the model of this ped
await loadModel(this.model);

//Create a random ped with a random style fitting to the current location
this.scriptID = native.createPed(4, native.getHashKey(this.model), this.pos.x, this.pos.y, this.pos.z);
native.setPedRandomComponentVariation(this.scriptID, 0);

//Store this ped by his scriptID as a key
pedsToScriptID[this.scriptID] = this;

/**
* Get all important information to give it back to the server for sync
*/
Expand Down Expand Up @@ -213,9 +201,6 @@ class PedClass {
this.texture11 = native.getPedTextureVariation(this.scriptID, 11);
this.created = true;

native.setEntityInvincible(this.scriptID, this.invincible);
native.freezeEntityPosition(this.scriptID, this.freeze);

//Send back to the server for sync with other players
alt.emitServer("pedSyncer:client:firstSpawn", Ped.emitParse(this));
}
Expand All @@ -224,13 +209,7 @@ class PedClass {
* This will be executed by the clients coming into range of the ped and the ped
* was already created by an other client which already decided the peds style.
*/
async respawn() {
//Load the model of this ped
await loadModel(this.model);

//Create the ped with the giving properties of the ped
this.scriptID = native.createPed(this.gender=="male"?4:5, native.getHashKey(this.model), this.pos.x, this.pos.y, this.pos.z, this.heading, false, false);

respawn() {
//Set the heading of the ped
native.setEntityHeading(this.scriptID, this.heading);

Expand All @@ -240,27 +219,38 @@ class PedClass {
//Set the peds style
this.setPedComponentVariation();

//Store this ped by his scriptID as a key
pedsToScriptID[this.scriptID] = this;

//Set Ped-Attributes
if (this.invincible) native.setEntityInvincible(this.scriptID, true);
native.setPedArmour(this.scriptID, this.armour);
native.setEntityHealth(this.scriptID, this.health, 0);
if (this.dead) native.setPedToRagdoll(this.scriptID, -1, -1, 0, false, false, false);
native.setEntityInvincible(this.scriptID, this.invincible);
native.freezeEntityPosition(this.scriptID, this.freeze);
}

/**
* Spawn the ped, decide if this is the first time this ped will ever be created
*/
async spawn() {
//Load the model of this ped
await loadModel(this.model);

//Create a random ped with a random style fitting to the current location
if (this.vehicle == null) this.scriptID = native.createPed(4, native.getHashKey(this.model), this.pos.x, this.pos.y, this.pos.z);
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);
}

//Store this ped by his scriptID as a key
pedsToScriptID[this.scriptID] = this;

native.setEntityInvincible(this.scriptID, this.invincible);
native.freezeEntityPosition(this.scriptID, this.freeze);

let spawned = false;
//If already created, respawn it
if (this.created) await this.respawn();
if (this.created) this.respawn();
//If it wasn't created and this client is the netOwner: spawn it first time
else if (this.netOwner == alt.Player.local.id) await this.firstSpawn();
else if (this.netOwner == alt.Player.local.id) this.firstSpawn();

//Start the peds wandering
if (typeof this.scriptID !== "undefined" && this.scriptID != 0) {
Expand Down Expand Up @@ -300,6 +290,7 @@ class PedClass {
*/
outOfRange() {
//Delete ped
native.deleteEntity(this.scriptID);
native.deletePed(this.scriptID);

//Delete ped scriptID reference
Expand Down Expand Up @@ -481,7 +472,7 @@ class PedClass {
updateProperties(ped) {
let componentSet = false;
for (let key of Object.keys(ped)) {
if (typeof this[key] !== "undefined") this[key] = ped[key];
this[key] = ped[key];
if (
key.includes("drawable") ||
key.includes("texture") ||
Expand Down
38 changes: 37 additions & 1 deletion pedSyncer/client/pedSyncer/control/PedControler.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ 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;
trySpawn(entityId, position, 0, 100, newEntityData);
});

Expand All @@ -90,7 +91,10 @@ export function startPedControler() {
alt.onServer("entitySync:remove", (entityId, entityType) => {
if (entityType != pedType) return;

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

ped.outOfRange();
});

//Event which fires on peds data update
Expand Down Expand Up @@ -172,6 +176,38 @@ export function startPedControler() {
ped.becomeNetOwner();
}
}

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();
}
}
}

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;

alt.setTimeout(checkStreamedInVehicle, 100);
}
alt.setTimeout(checkStreamedInVehicle, 5000);

//Event which fires if a ped has a new path
alt.onServer('pedSyncer:server:path', (pedId, path) => {
Expand Down
1 change: 1 addition & 0 deletions pedSyncer/server/StreetNodes.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pedSyncer/server/data/CarColorsNum.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[0,1,2,3,4,5,6,7,8,9,10,11,27,28,29,30,31,32,33,34,35,36,37,38,49,50,51,52,53,54,61,62,63,64,65,66,67,68,69,70,71,72,73,74,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,111,112,125,137,141,142,143,145,146,150,12,13,14,39,40,41,42,55,82,83,84,128,129,131,148,149,151,152,153,154,155,15,16,17,18,19,20,43,44,45,56,57,75,76,77,78,79,80,81,108,109,110,122,21,22,23,24,25,26,46,47,48,58,59,60,85,86,87,113,114,115,116,121,123,124,126,130,132,133,117,118,119]
114 changes: 114 additions & 0 deletions pedSyncer/server/data/CarModels.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
[
"mule",
"mule2",
"mule3",
"mule4",
"asbo",
"blista",
"brioso",
"dilettante",
"issi2",
"prairie",
"rhapsody",
"cogcabrio",
"exemplar",
"f620",
"felon",
"felon2",
"jackal",
"oracle",
"oracle2",
"sentinel",
"sentinel2",
"windsor",
"windsor2",
"zion",
"zion2",
"akuma",
"bagger",
"bati",
"bati2",
"bf400",
"carbonrs",
"defiler",
"diablous",
"diablous2",
"double",
"faggio",
"faggio2",
"faggio3",
"hakuchou",
"lectro",
"nightblade",
"pcj",
"ruffian",
"vortex",
"thrust",
"blade",
"buccaneer",
"chino",
"deviant",
"dominator",
"dominator3",
"virgo",
"virgo2",
"voodoo",
"voodoo2",
"baller",
"baller2",
"baller3",
"baller4",
"cavalcade",
"cavalcade2",
"fq2",
"gresley",
"habanero",
"huntley",
"landstalker",
"patriot",
"seminole",
"toros",
"xls",
"xls2",
"asterope",
"fugitive",
"primo",
"primo2",
"warrener",
"washington",
"buffalo",
"buffalo2",
"comet2",
"coquette",
"feltzer2",
"fusilade",
"jugular",
"penumbra",
"raiden",
"schafter2",
"schafter3",
"schafter4",
"schafter5",
"schafter6",
"schlagen",
"schwarzer",
"specter",
"sugoi",
"fmj",
"italigtb2",
"xa21",
"zentorno",
"bison",
"bison2",
"bison3",
"bobcatxl",
"boxville",
"boxville2",
"boxville3",
"boxville4",
"burrito",
"burrito2",
"burrito3",
"burrito4",
"burrito5"
]
40 changes: 40 additions & 0 deletions pedSyncer/server/data/ColorlessCars.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[
"fbi",
"fbi2",
"police",
"police2",
"police3",
"police4",
"policet",
"policeb",
"sheriff",
"bulldozer",
"cutter",
"dump",
"flatbed",
"guardian",
"mixer",
"mixer2",
"rubble",
"tiptruck",
"tiptruck2",
"apc",
"barracks",
"barracks2",
"barracks3",
"barrage",
"chernobog",
"crusader",
"halftrack",
"khanjali",
"minitank",
"rhino",
"scarab",
"scarab2",
"scarab3",
"thruster",
"airtug",
"ripley",
"caddy3",
"airbus"
]
8 changes: 0 additions & 8 deletions pedSyncer/server/resource.cfg

This file was deleted.

3 changes: 3 additions & 0 deletions src/PedSyncer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public override void OnStart()
//Start serverside ped movement calculation
PedMovement.GetInstance();

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

//Create citizen peds who wanders - delete this line if you don't wanna have citizens
Ped.CreateCitizenPeds();

Expand Down
Loading

0 comments on commit b532a02

Please sign in to comment.