Advertisement
Guest User

spawn vehicle gta5 javascript

a guest
Jan 22nd, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function spawnVehicle(model, options) {
  2.     // optional argument `options` defaults to `{}`
  3.     options = (options != null ? options : {});
  4.     // if passed a model name instead of a hash, convert it
  5.     if (_.isString(model))
  6.         model = joaat(model);
  7.  
  8.     // use our ped for location to spawn, unless we specify another
  9.     var pedid = options.pedid || window.pedid();
  10.     var coords = getCoords(pedid);
  11.     var heading = options.heading || ENTITY.GET_ENTITY_HEADING(pedid);
  12.  
  13.     // place vehicle 5 units ahead, unless we've specified a manual x,y,z
  14.     var forward = options.x ? 0 : 5.0;
  15.     var xVect = forward * Math.sin(toRadians(heading)) * -1;
  16.     var yVect = forward * Math.cos(toRadians(heading));
  17.  
  18.     if (STREAMING.IS_MODEL_IN_CDIMAGE(model) && STREAMING.IS_MODEL_A_VEHICLE(model)) {
  19.         // we might do something special for flying stuff
  20.         var isAircraft = VEHICLE.IS_THIS_MODEL_A_HELI(model) || VEHICLE.IS_THIS_MODEL_A_PLANE(model);
  21.  
  22.         // make initial model request
  23.         STREAMING.REQUEST_MODEL(model);
  24.  
  25.         // we'll make 10 attempts to see if it's loaded
  26.         var attempts = 10;
  27.  
  28.         // callback function (runs on a timer)
  29.         var requestFn = function() {
  30.             var request = STREAMING.HAS_MODEL_LOADED(model);
  31.             if (!request) {
  32.                 console.log("Model " + model.toString(16) + " hasn't loaded yet " + attempts + "");
  33.                 // try again in 100ms
  34.                 if (--attempts)
  35.                     setTimeout(requestFn, 100);
  36.                 // or give up
  37.                 else
  38.                     console.log("Failed to get model for " + model);
  39.                 return;
  40.             }
  41.  
  42.             // Complete vehicle spawning process
  43.  
  44.             // move x,y,z properties that may have been passed in options, into
  45.             //      the coords to spawn.
  46.             _.extend(coords, options);
  47.  
  48.             // debug the final location
  49.             var vehicle, x, y, z, h;
  50.  
  51.             // thing to warp into a air thing in mid-air
  52.             if (isAircraft && options.vehicleSpawnWarpInto) {
  53.                 vehicle = VEHICLE.CREATE_VEHICLE(model, coords.x + xVect, coords.y + yVect, coords.z + 1000, heading, true, true);
  54.                 VEHICLE.SET_VEHICLE_FORWARD_SPEED(vehicle, 500.0);
  55.                 VEHICLE.SET_HELI_BLADES_FULL_SPEED(vehicle);
  56.             } else {
  57.                 // create model, don't forget the heading
  58.                 vehicle = VEHICLE.CREATE_VEHICLE(model,
  59.                     x = coords.x + xVect,
  60.                     y = coords.y + yVect,
  61.                     z = coords.z + ((options.z || options.vehicleSpawnWarpInto) ? 0 : 1),
  62.                     h = options.heading,
  63.                     true, true   // what's this for again?
  64.                 );
  65.                 // prefer to drop vehicles from a height for effect
  66.                 // VEHICLE.SET_VEHICLE_ON_GROUND_PROPERLY(vehicle);
  67.  
  68.                 // prefer to actually get into a car the old fashioned way
  69.                 // PED.SET_PED_INTO_VEHICLE(PLAYER.GET_PLAYER_PED(), vehicle, -1);
  70.             }
  71.  
  72.             // This is meant to make shit work good in MP
  73.             DECORATOR.DECOR_SET_INT(vehicle, "MPBitset", 0);
  74.  
  75.             // log the pos the car should be at
  76.             console.info("Laz-e-log: x, y, z, h", x, y, z, h);
  77.             console.log("Spawned " + vehicle);
  78.  
  79.             // not sure why we do this, i guess it saves memory
  80.             STREAMING.SET_MODEL_AS_NO_LONGER_NEEDED(model);
  81.         };
  82.  
  83.         // start off timed function
  84.         requestFn();
  85.     }
  86.     else {
  87.         console.log("Stupid user that doesn't exist");
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement