Skip to content
This repository has been archived by the owner on Jun 5, 2024. It is now read-only.

Commit

Permalink
Vehicle Extras Interface / Saving / Setting / Getting (#413)
Browse files Browse the repository at this point in the history
* add vehicleExtras to owned vehicle

* load extras from DB on spawn

* add functions for setting and getting vehicleExtra

* add example commands to modify extras

* add return type to description for docs

* fix some @params and @returns
  • Loading branch information
Dav-Renz committed Apr 7, 2023
1 parent d9ae327 commit 6beb5a6
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { LocaleController } from '@AthenaShared/locale/locale';
import { VehicleState } from '@AthenaShared/interfaces/vehicleState';
import IVehicleTuning from '@AthenaShared/interfaces/vehicleTuning';
import IVehicleMod from '@AthenaShared/interfaces/vehicleMod';
import VehicleExtra from '@AthenaShared/interfaces/vehicleExtra';

Athena.systems.messenger.commands.register(
'tempvehicle',
Expand Down Expand Up @@ -289,6 +290,82 @@ Athena.systems.messenger.commands.register(
},
);

Athena.systems.messenger.commands.register(
'activateExtra',
'/activateExtra [id]',
['admin'],
(player: alt.Player, id: string) => {
const vehicle = player.vehicle ? player.vehicle : Athena.utility.closest.getClosestVehicle(player.pos);

if (!vehicle) {
Athena.player.emit.message(player, 'No spawned vehicle.');
return;
}

if (Athena.utility.vector.distance(player.pos, vehicle.pos) > 4 && !player.vehicle) {
Athena.player.emit.message(player, 'No vehicle in range.');
return;
}

if (isNaN(parseInt(id))) {
Athena.player.emit.message(player, `Dirt level passed was not a number.`);
return;
}

Athena.vehicle.tuning.setExtra(vehicle, [{ id: parseInt(id), state: true }]);
Athena.vehicle.controls.updateLastUsed(vehicle);
Athena.vehicle.controls.update(vehicle);

const extraData: Array<VehicleExtra> = Athena.vehicle.tuning.getExtras(vehicle);

Athena.document.vehicle.set(vehicle, 'extras', extraData);

const hash = typeof vehicle.model === 'number' ? vehicle.model : alt.hash(vehicle.model);

let vehInfo = Athena.utility.hashLookup.vehicle.hash(hash);

Athena.player.emit.message(player, `Extra ${id} of ${vehInfo.displayName} was activated.`);
},
);

Athena.systems.messenger.commands.register(
'deactivateExtra',
'/deactivateExtra [id]',
['admin'],
(player: alt.Player, id: string) => {
const vehicle = player.vehicle ? player.vehicle : Athena.utility.closest.getClosestVehicle(player.pos);

if (!vehicle) {
Athena.player.emit.message(player, 'No spawned vehicle.');
return;
}

if (Athena.utility.vector.distance(player.pos, vehicle.pos) > 4 && !player.vehicle) {
Athena.player.emit.message(player, 'No vehicle in range.');
return;
}

if (isNaN(parseInt(id))) {
Athena.player.emit.message(player, `Dirt level passed was not a number.`);
return;
}

Athena.vehicle.tuning.setExtra(vehicle, [{ id: parseInt(id), state: false }]);
Athena.vehicle.controls.updateLastUsed(vehicle);
Athena.vehicle.controls.update(vehicle);

const extraData: Array<VehicleExtra> = Athena.vehicle.tuning.getExtras(vehicle);

Athena.document.vehicle.set(vehicle, 'extras', extraData);

const hash = typeof vehicle.model === 'number' ? vehicle.model : alt.hash(vehicle.model);

let vehInfo = Athena.utility.hashLookup.vehicle.hash(hash);

Athena.player.emit.message(player, `Extra ${id} of ${vehInfo.displayName} was deactivated.`);
},
);

// import { command } from '@AthenaServer/decorators/commands';
// import { PERMISSIONS } from '@AthenaShared/flags/permissionFlags';
// import { LOCALE_KEYS } from '@AthenaShared/locale/languages/keys';
Expand Down
4 changes: 4 additions & 0 deletions src/core/server/vehicle/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ export function persistent(document: OwnedVehicle): alt.Vehicle | undefined {
Athena.vehicle.tuning.applyTuning(vehicle, document.tuning);
}

if (document.extras) {
Athena.vehicle.tuning.setExtra(vehicle, document.extras);
}

if (document.damage) {
Athena.vehicle.damage.apply(vehicle, document.damage);
}
Expand Down
47 changes: 46 additions & 1 deletion src/core/server/vehicle/tuning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as Athena from '@AthenaServer/api';

import VehicleTuning from '@AthenaShared/interfaces/vehicleTuning';
import { VehicleState } from '@AthenaShared/interfaces/vehicleState';
import VehicleExtra from '@AthenaShared/interfaces/vehicleExtra';
import IVehicleMod from '@AthenaShared/interfaces/vehicleMod';

/**
Expand All @@ -23,6 +24,46 @@ export function applyState(vehicle: alt.Vehicle, state: Partial<VehicleState> |
});
}

/**
* Get all mods of the specified vehicle.
*
*
* @param {alt.Vehicle} vehicle An alt:V Vehicle Entity
* @return {Array<VehicleExtra>}
*/
export function getExtras(vehicle: alt.Vehicle): Array<VehicleExtra> {
if (Overrides.getExtras) {
return Overrides.getExtras(vehicle);
}

let extraData: Array<VehicleExtra> = [];

for (let id = 0; id < 15; ++id) {
let state: boolean = !vehicle.getExtra(id);
extraData.push({ id, state });
}

return extraData;
}

/**
* Applies specified properties to a vehicle in bulk.
* These match the alt:V API, and can be pulled from a database.
*
*
* @param {alt.Vehicle} vehicle An alt:V Vehicle Entity
* @param {Array<VehicleExtra>} extras
*/
export function setExtra(vehicle: alt.Vehicle, extras: Array<VehicleExtra>) {
if (Overrides.setExtra) {
return Overrides.setExtra(vehicle, extras);
}

for (let extra of extras) {
vehicle.setExtra(extra.id, extra.state);
}
}

/**
* Apply tuning to the specified vehicle.
*
Expand Down Expand Up @@ -55,7 +96,7 @@ export function applyTuning(vehicle: alt.Vehicle, tuning: VehicleTuning | Partia
*
*
* @param {alt.Vehicle} vehicle An alt:V Vehicle Entity
* @param {VehicleTuning } tuning
* @returns {VehicleTuning}
*/
export function getTuning(vehicle: alt.Vehicle): VehicleTuning {
if (Overrides.getTuning) {
Expand Down Expand Up @@ -135,6 +176,8 @@ export function getMods(vehicle: alt.Vehicle): Array<IVehicleMod> {

interface VehicleTuningFuncs {
applyState: typeof applyState;
setExtra: typeof setExtra;
getExtras: typeof getExtras;
applyTuning: typeof applyTuning;
getTuning: typeof getTuning;
applyMods: typeof applyMods;
Expand All @@ -144,6 +187,8 @@ interface VehicleTuningFuncs {
const Overrides: Partial<VehicleTuningFuncs> = {};

export function override(functionName: 'applyState', callback: typeof applyState);
export function override(functionName: 'setExtra', callback: typeof setExtra);
export function override(functionName: 'getExtras', callback: typeof getExtras);
export function override(functionName: 'applyTuning', callback: typeof applyTuning);
export function override(functionName: 'getTuning', callback: typeof getTuning);
export function override(functionName: 'applyMods', callback: typeof applyMods);
Expand Down
10 changes: 10 additions & 0 deletions src/core/shared/interfaces/vehicleExtra.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Used to describe a vehicle extra, and the state it should have.
*
*
* @interface VehicleExtra
*/
export default interface VehicleExtra {
id: number;
state: boolean;
}
9 changes: 9 additions & 0 deletions src/core/shared/interfaces/vehicleOwned.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BaseVehicle } from './vehicleBase';
import VehicleExtra from './vehicleExtra';
import { VehicleState } from './vehicleState';
import VehicleTuning from './vehicleTuning';

Expand Down Expand Up @@ -85,4 +86,12 @@ export interface OwnedVehicle extends BaseVehicle {
*
*/
damage?: VehicleDamage;

/**
* Damage to store / apply on a vehicle
*
* @type {VehicleDamage}
*
*/
extras?: Array<VehicleExtra>;
}

0 comments on commit 6beb5a6

Please sign in to comment.