Simple Garage System

Started by DMWarrior, Mar 24, 2020, 07:56 PM

Previous topic - Next topic

DMWarrior

VC:MP doesn't offer Pay n' Sprays and garages by default, you'll need to make them yourself. This, however, can be a quite difficult thing to do if you're just getting started, because all doors are closed too. Objects can be removed by using HideMapObject(), but it's still necessary to look for IDs and positions on the map.

Unfortunately, I couldn't find anything. And with no object IDs and positions available, the only option is to get a map editor and look for them by yourself.

I found this very annoying, specially because some servers already have this and it should be something really simple to do. So after going through all this, I decided to make a script to do just that: opening and closing garages. This should be a easy way to get started.
[noae]/**
 * @class Garage
 */
class Garage {
  /** Available garages. */
  static list = {

    /** Pay n' Spray (Little Haiti). */
     haiwshpnsdoor = {
      ID = 1573,
      Pos = Vector(-874.696, -116.695, 11.987),
      OpenPos = Vector(-874.696, -116.695, 15.987),
      Speed = 2000,
      SoundID = 176
    },

    /** Pay n' Spray (Viceport). */
     dk_paynspraydoor = {
      ID = 828,
      Pos = Vector(-910.001, -1264.709, 12.492),
      OpenPos = Vector(-910.001, -1264.709, 16.492),
      Speed = 2000,
      SoundID = 176
    },

    /** Pay n' Spray (Washington Beach). */
     wshpnsdoor = {
      ID = 3013,
      Pos = Vector(-7.804, -1257.642, 10.818),
      OpenPos = Vector(-7.804, -1257.642, 15.818),
      Speed = 2000,
      SoundID = 176
    },

    /** Pay n' Spray (Vice Point). */
     nbecpnsdoor = {
      ID = 4145,
      Pos = Vector(325.083, 431.136, 11.587),
      OpenPos = Vector(325.083, 431.136, 16.587),
      Speed = 2000,
      SoundID = 176,
      Rotation = Vector(0, 0, 2.810)
    },

    /** Bomb Shop (Viceport). */
    dk_bombdoor = {
      ID = 837,
      Pos = Vector(-1161.317, -1402.474, 12.798),
      OpenPos = Vector(-1161.317, -1402.474, 17.798),
      Speed = 2000,
      SoundID = 176
    },

    /** Import/Export (Sunshine Autos). */
    lh_showdoor1 = {
      ID = 1392,
      Pos = Vector(-975.454, -841.796, 8.159),
      OpenPos = (-975.454, -841.796, 12.159),
      Speed = 2000,
      SoundID = 176
    },

    /** Pay n' Spray (Sunshine Autos). */
    lh_showdoor03 = {
      ID = 1393,
      Pos = Vector(-1007.945, -841.778, 8.594),
      OpenPos = Vector(-1007.945, -841.778, 12.594),
      Speed = 2000,
      SoundID = 176
    },

    /** Tank door (Little Havana). */
     lhtankdoor = {
      ID = 1428,
      Pos = Vector(-1054.399, -478.073, 12.525),
      OpenPos = Vector(-1054.399, -478.073, 16.525),
      Speed = 2000,
      SoundID = 176
    },

    /** Garage #1 (Sunshine Autos). */
     hav_garagedoor1 = {
      ID = 1388,
      Pos = Vector(-988.085, -821.650, 8.512),
      OpenPos = Vector(-988.085, -821.650, 12.512),
      Speed = 2000,
      SoundID = 176
    },

    /** Garage #2 (Sunshine Autos). */
    hav_garagedoor02 = {
      ID = 1395,
      Pos = Vector(-998.851, -821.650, 8.580),
      OpenPos = Vector(-998.851, -821.650, 12.580),
      Speed = 2000,
      SoundID = 176
    },

    /** Garage #3 (Sunshine Autos). */
    hav_garagedoor03 = {
      ID = 1396,
      Pos = Vector(-1010.188, -821.650, 8.580),
      OpenPos = Vector(-1010.188, -821.650, 12.580),
      Speed = 2000,
      SoundID = 176
    },

    /** Garage #4 (Sunshine Autos). */
    hav_garagedoor04 = {
      ID = 1397,
      Pos = Vector(-1021.867, -821.650, 8.566),
      OpenPos = Vector(-1021.867, -821.650, 12.566),
      Speed = 2000,
      SoundID = 176
    }

  }

  /**
   * Hide a garage.
   *
   * @param {string} name Garage name (see Garage.list).
   * @return {boolean}
   */
  static function hide(name) {
    if(name in Garage.list) {
      local garage = Garage.list[name];
      ::HideMapObject(garage.ID, garage.Pos.x, garage.Pos.y, garage.Pos.z);
      return true;
    }

    return false;
  }

  /**
   * Create a garage.
   *
   * @param {string} name Garage name (see Garage.list).
   * @return {boolean}
   */
  static function create(name) {
    if(name in Garage.list) {
      Garage.hide(name);

      local garage = Garage.list[name];
      local instance = ::CreateObject(garage.ID, 0, garage.Pos, 255);

      if("Rotation" in garage) {
        instance.RotateToEuler(garage.Rotation, 0);
      }

      return Garage(instance, garage.OpenPos, garage.Pos, garage.Speed, garage.SoundID);
    }

    return null;
  }

  /** Check if garage is open or closed. */
  _closed = true

  /** Garage object reference. */
  instance = null

  /** Garage position when it's open. */
  openPosition = Vector(0, 0, 0)

  /** Garage position when it's closed. */
  closedPosition = Vector(0, 0, 0)

  /** Opening and closing speed. */
  speed = 1000

  /** Sound played when a garage opens or closes. */
  soundID = 176

  /**
   * @constructor
   *
   * @param {instance} instance Garage object reference.
   * @param {Vector} openPosition Garage position when it's open.
   * @param {Vector} closedPosition Garage position when it's closed.
   * @param {number} speed Opening and closing speed.
   * @param {number} soundID Sound played when a garage opens or closes.
   */
  constructor(instance, openPosition, closedPosition, speed, soundID) {
    this._closed = true;
    this.instance = instance;
    this.openPosition = openPosition;
    this.closedPosition = closedPosition;
    this.speed = speed;
    this.soundID = soundID;
  }

  /**
   * Open this garage.
   *
   * @return {boolean}
   */
  function open() {
    if(this.instance != null && this._closed == true) {
      ::PlaySound(0, this.soundID, this.closedPosition);
      this.instance.MoveTo(this.openPosition, this.speed);
      this._closed = false;
      return true;
    }

    return false;
  }

  /**
   * Close this garage.
   *
   * @return {boolean}
   */
  function close() {
    if(this.instance != null && this._closed == false) {
      ::PlaySound(0, this.soundID, this.closedPosition);
      this.instance.MoveTo(this.closedPosition, this.speed);
      this._closed = true;
      return true;
    }

    return false;
  }

  /**
   * Switch between opening and closing the garage.
   *
   * @param {boolean}
   */
  function toggle() {
    if(this._closed == true) {
      this.open();
    }
    else {
      this.close();
    }

    return true;
  }

  /**
   * Check if garage is open or closed.
   *
   * @return {boolean}
   */
  function isClosed() {
    return this._closed;
  }
}
[/noae]
Let's say you want to use the Pay n' Sprays. All you'll need to do now is this:
[noae]// Available Pay n' Sprays.
PayNSprays <- {
LittleHaiti = Garage.create("haiwshpnsdoor"),
Viceport = Garage.create("dk_paynspraydoor"),
WashingtonBeach = Garage.create("wshpnsdoor"),
VicePoint = Garage.create("nbecpnsdoor"),
}

PayNSprays.LittleHaiti.open(); // Open the Little Haiti's Pay n Spray.
PayNSprays.LittleHaiti.close(); // Close the Little Haiti's Pay n Spray.
PayNSprays.LittleHaiti.toggle(); // Open if it's closed, close if it's open.

if(PayNSprays.LittleHaiti.isClosed()) {
  MessagePlayer("This Pay n' Spray is closed.", player);
}
[/noae]
It does have some flaws, though. Some garages are not well positioned and you may want to change some values if you find necessary (like the opening/closing sounds and the speed). Also, some IDs and positions are still missing, like the safehouse's garages. But this should be good enough for a quick start.

And if you want to set a new garage instead of just replacing the already existing ones, you can try something like this:
[noae]// Create a new object
randomObject <- CreateObject(1, Vector(0, 0, 0));

// Create the garage
randomGarage <- Garage(randomObject, Vector(0, 0, 10), Vector(0, 0, 0), 1000, 176);

// Open the garage
randomGarage.open();
[/noae]

habi

#1
cool. first time vcmp i saw a garage opening itself.
........
I was learning the code.
so garage means the shutter.
hide function simply removes the shutter, so vehicles can in and out.