const DEFORMATION_DAMAGE_THRESHOLD = 0.01;
const MAX_DEFORM_ITERATIONS = 2;
function getVehicleOffsetsForDeformation(vehicle) {
const model = vehicle.model;
const dimensions = mp.game.gameplay.getModelDimensions(model);
const min = dimensions.min;
const max = dimensions.max;
const X = (max.x - min.x) * 0.5;
const Y = (max.y - min.y) * 0.5;
const Z = (max.z - min.z) * 0.5;
return [
new mp.Vector3(0.0, Y, Z * 0.5),
new mp.Vector3(0.0, Y, 0.0),
new mp.Vector3(0.0, -Y, Z * 0.5),
new mp.Vector3(0.0, -Y, 0.0),
new mp.Vector3(-X, 0.0, Z * 0.5),
new mp.Vector3(-X, 0.0, 0.0),
new mp.Vector3(X, 0.0, Z * 0.5),
new mp.Vector3(X, 0.0, 0.0),
new mp.Vector3(-X * 0.5, Y * 0.5, Z * 0.5),
new mp.Vector3(X * 0.5, Y * 0.5, Z * 0.5),
new mp.Vector3(-X * 0.5, -Y * 0.5, Z * 0.5),
new mp.Vector3(X * 0.5, -Y * 0.5, Z * 0.5),
new mp.Vector3(-X, Y, Z * 0.5),
new mp.Vector3(X, Y, Z * 0.5),
new mp.Vector3(-X, -Y, Z * 0.5),
new mp.Vector3(X, -Y, Z * 0.5)
];
}
PS : playerEnterVehicle убираем с клеинт сайда что бы в двойне не деформировал машину при входе в нёе
function getVehicleDeformation(vehicle) {
if (!mp.vehicles.exists(vehicle)) return [];
const offsets = getVehicleOffsetsForDeformation(vehicle);
const deformationPoints = [];
offsets.forEach(offset => {
const damageVector = vehicle.getDeformationAtPos(offset.x, offset.y, offset.z);
const dmg = Math.sqrt(damageVector.x ** 2 + damageVector.y ** 2 + damageVector.z ** 2);
if (dmg > DEFORMATION_DAMAGE_THRESHOLD) {
deformationPoints.push({ offset, dmg });
}
});
return deformationPoints;
}
function applyDeformation(vehicle, deformation) {
if (!mp.vehicles.exists(vehicle)) return;
if (deformation && deformation.length > 0) {
setVehicleDeformation(vehicle, deformation);
} else {
vehicle.setFixed();
}
}
function setVehicleDeformation(vehicle, deformationPoints) {
let iteration = 0;
let deform = true;
const deformationDamageMult = vehicle.getHandling('fDeformationDamageMult');
let damageMult = deformationDamageMult > 0 ? deformationDamageMult * 100.0 : 0;
while (deform && iteration < MAX_DEFORM_ITERATIONS) {
deform = false;
deformationPoints.forEach(def => {
const damageVector = vehicle.getDeformationAtPos(def.offset.x, def.offset.y, def.offset.z);
const dmg = Math.sqrt(damageVector.x ** 2 + damageVector.y ** 2 + damageVector.z ** 2);
if (dmg < def.dmg) {
vehicle.setDamage(def.offset.x, def.offset.y, def.offset.z, def.dmg * damageMult, 1000.0, true);
deform = true;
}
});
iteration++;
}
}
function isDeformationWorse(newDef, oldDef) {
if (!oldDef || newDef.length > oldDef.length) {
return true;
} else if (newDef.length < oldDef.length) {
return false;
}
for (let i = 0; i < newDef.length; i++) {
let found = false;
for (let j = 0; j < oldDef.length; j++) {
if (newDef[i].offset.x === oldDef[j].offset.x &&
newDef[i].offset.y === oldDef[j].offset.y &&
newDef[i].offset.z === oldDef[j].offset.z) {
found = true;
if (newDef[i].dmg > oldDef[j].dmg) {
return true;
}
}
}
if (!found) {
return true;
}
}
return false;
}
function isDeformationEqual(newDef, oldDef) {
if (oldDef == null && newDef == null) return true;
if (oldDef == null || newDef == null || newDef.length !== oldDef.length) return false;
for (let i = 0; i < newDef.length; i++) {
if (newDef[i].dmg !== oldDef[i].dmg) {
return false;
}
}
return true;
}
function handleDeformationUpdate(vehicle) {
if (!mp.vehicles.exists(vehicle)) return;
const deformation = getVehicleDeformation(vehicle);
const currentDeformation = JSON.parse(vehicle.getVariable('deformation') || '[]');
if (!isDeformationEqual(deformation, currentDeformation) && isDeformationWorse(deformation, currentDeformation)) {
mp.events.callRemote('storeVehicleDeformationState', vehicle.remoteId, JSON.stringify(deformation));
}
}
mp.events.add('playerLeaveVehicle', vehicle => {
handleDeformationUpdate(vehicle);
});
mp.events.add('playerEnterVehicle', (vehicle, seat) => {
setTimeout(() => {
if (seat === -1) {
const deformation = vehicle.getVariable('deformation');
if (deformation) {
applyDeformation(vehicle, JSON.parse(deformation));
}
}
}, 200);
});
mp.events.add('entityStreamIn', entity => {
if (entity.type === 'vehicle') {
setTimeout(() => {
const deformation = entity.getVariable('deformation');
if (deformation) {
applyDeformation(entity, JSON.parse(deformation));
}
}, 2000);
}
});