Advertisement
Guest User

Untitled

a guest
Sep 12th, 2012
4,031
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Dynamic attach system for Vehicles.
  3.     Created by Siralos
  4. */
  5.  
  6. #define FILTERSCRIPT
  7. #include <a_samp>
  8. #include <YSI\y_commands>
  9. #include <YSI\y_master>
  10. #include <sscanf2>
  11.  
  12. #define MAX_OBJ_PER_VEHICLE 20
  13.  
  14. new AttachingObjects[MAX_PLAYERS];
  15. new AttachedObjects[MAX_VEHICLES][MAX_OBJ_PER_VEHICLE];
  16. new strout[128];
  17.  
  18. public OnScriptInit()
  19. {
  20.     printf("Attach object to Vehicle by Siralos");
  21.     return 1;
  22. }
  23.  
  24. public OnScriptExit()
  25. {
  26.     //Clear all objects created
  27.     for(new i=0; i<MAX_VEHICLES; i++)
  28.     {
  29.         if(GetVehicleModel(i) > 0)
  30.         {
  31.             for(new j=0; j<MAX_OBJ_PER_VEHICLE; j++)
  32.             {
  33.                 if(IsValidObject(AttachedObjects[i][j]))
  34.                 {
  35.                     DestroyObject(AttachedObjects[i][j]);
  36.                 }
  37.             }
  38.         }
  39.     }
  40.     return 1;
  41. }
  42.  
  43. YCMD:mycarid(playerid, params[], help)
  44. {
  45.     #pragma unused help
  46.     #pragma unused params
  47.     new car = GetPlayerVehicleID(playerid);
  48.     if(car == 0) return SendClientMessage(playerid, 0xfce80cFF, "You are not in a car");
  49.     format(strout, sizeof(strout), "Car ID: %d", car);
  50.     return SendClientMessage(playerid, 0xfce80cFF, strout);
  51. }
  52.  
  53. YCMD:deleteobject(playerid, params[], help)
  54. {
  55.     #pragma unused help
  56.     new arrayid, car;
  57.     if(sscanf(params, "ii", arrayid, car))
  58.     {
  59.         SendClientMessage(playerid, 0xfce80cFF, "Use: /deleteobject [arrayid] [SAMP Car ID]");
  60.         return 1;
  61.     }
  62.     if(0 <= arrayid < 20)
  63.     {
  64.         if(0 < car < MAX_VEHICLES)
  65.         {
  66.             if(!IsValidObject(AttachedObjects[car][arrayid]))
  67.             {
  68.                 SendClientMessage(playerid, 0xfce80cFF, "No object found at position");
  69.                 return 1;
  70.             }
  71.             DestroyObject(AttachedObjects[car][arrayid]);
  72.             SendClientMessage(playerid, 0xfce80cFF, "Object removed");
  73.             return 1;
  74.         }
  75.         else
  76.         {
  77.             SendClientMessage(playerid, 0xfce80cFF, "Invalid vehicle");
  78.         }
  79.     }
  80.     else
  81.     {
  82.         SendClientMessage(playerid, 0xfce80cFF, "No object found at position");
  83.     }
  84.     return 1;
  85. }
  86.  
  87. YCMD:attachobject(playerid, params[], help)
  88. {
  89.     #pragma unused help
  90.     new objectmodel, car;
  91.     if(sscanf(params, "ii", objectmodel, car))
  92.     {
  93.         SendClientMessage(playerid, 0xfce80cFF, "Use: /attachobject [object model] [SAMP Car ID]");
  94.         return 1;
  95.     }
  96.     if(0 < car < MAX_VEHICLES)
  97.     {
  98.         new Float:px, Float:py, Float:pz;
  99.         GetPlayerPos(playerid, px, py, pz);
  100.         AttachingObjects[playerid] = CreateObject(objectmodel, px, py, pz, 0.0, 0.0, 0.0);
  101.         SendClientMessage(playerid, 0xfce80cFF, "Object created. Editing...");
  102.         EditObject(playerid, AttachingObjects[playerid]);
  103.         SetPVarInt(playerid, "AttachingTo", car);
  104.     }
  105.     else
  106.     {
  107.         SendClientMessage(playerid, 0xfce80cFF, "Invalid vehicle");
  108.     }
  109.     return 1;
  110. }
  111.  
  112. public OnPlayerEditObject(playerid, playerobject, objectid, response, Float:fX, Float:fY, Float:fZ, Float:fRotX, Float:fRotY, Float:fRotZ)
  113. {
  114.     if(!IsValidObject(objectid)) return 0;
  115.     MoveObject(objectid, fX, fY, fZ, 10.0, fRotX, fRotY, fRotZ);
  116.    
  117.     new car = GetPVarInt(playerid, "AttachingTo");
  118.     if(car != 0)
  119.     {  
  120.         if(response == EDIT_RESPONSE_FINAL)
  121.         {
  122.             SendClientMessage(playerid, 0xfce80cFF, "Finished Edition.");
  123.             new carslot = FindFreeObjectSlotInCar(car);
  124.             if(carslot == -1)
  125.             {
  126.                 SendClientMessage(playerid, 0xfce80cFF, "No more objects can be added to the car.");
  127.                 DestroyObject(AttachingObjects[playerid]);
  128.                 DeletePVar(playerid, "AttachingTo");
  129.                 return 1;
  130.             }
  131.             new Float:ofx, Float:ofy, Float:ofz, Float:ofaz;
  132.             new Float:finalx, Float:finaly;
  133.             new Float:px, Float:py, Float:pz, Float:roz;
  134.             GetVehiclePos(car, px, py, pz);
  135.             GetVehicleZAngle(car, roz);
  136.             ofx = fX-px;
  137.             ofy = fY-py;
  138.             ofz = fZ-pz;
  139.             ofaz = fRotZ-roz;
  140.             finalx = ofx*floatcos(roz, degrees)+ofy*floatsin(roz, degrees);
  141.             finaly = -ofx*floatsin(roz, degrees)+ofy*floatcos(roz, degrees);
  142.             AttachObjectToVehicle(AttachingObjects[playerid], car, finalx, finaly, ofz, fRotX, fRotY, ofaz);
  143.             AttachedObjects[car][carslot] = AttachingObjects[playerid];
  144.             format(strout, sizeof(strout), "Created in array slot %d of car %d", carslot, car);
  145.             SendClientMessage(playerid, 0xfce80cFF, strout);
  146.             DeletePVar(playerid, "AttachingTo");
  147.             return 1;
  148.         }
  149.         if(response == EDIT_RESPONSE_CANCEL)
  150.         {
  151.             DestroyObject(AttachingObjects[playerid]);
  152.             DeletePVar(playerid, "AttachingTo");
  153.             return 1;
  154.         }
  155.     }
  156.     return 0;
  157. }
  158.  
  159. stock FindFreeObjectSlotInCar(vehid)
  160. {
  161.     for(new i=0; i<MAX_OBJ_PER_VEHICLE; i++)
  162.     {
  163.         if(!IsValidObject(AttachedObjects[vehid][i])) return i;
  164.     }
  165.     return -1;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement