Guest User

Car System

a guest
Jul 24th, 2011
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 11.41 KB | None | 0 0
  1. // This is a comment
  2. // uncomment the line below if you want to write a filterscript
  3. //#define FILTERSCRIPT
  4.  
  5. #include <a_samp>
  6. #include <Dini>
  7.  
  8. forward PlayerToPoint(Float:radi, playerid, Float:x, Float:y, Float:z);
  9.  
  10. enum cInfo {        //You have to use an enum here, because you have to put different variable types into one array
  11.     model,
  12.     Float:xspawn,
  13.     Float:yspawn,
  14.     Float:zspawn,
  15.     Float:anglespawn,
  16.     col1,
  17.     col2,
  18.     respawn,
  19.     owner[MAX_PLAYER_NAME]
  20. }
  21.  
  22. new CarInfo[MAX_VEHICLES][cInfo];
  23. new bool:validcar[MAX_VEHICLES];
  24. #if defined FILTERSCRIPT
  25.  
  26. public OnFilterScriptInit()
  27. {
  28.     print("\n--------------------------------------");
  29.     print(" Blank Filterscript by your name here");
  30.     print("--------------------------------------\n");
  31.     return 1;
  32. }
  33.  
  34. public OnFilterScriptExit()
  35. {
  36.     return 1;
  37. }
  38.  
  39. #else
  40.  
  41. main()
  42. {
  43.     print("\n----------------------------------");
  44.     print(" Blank Gamemode by your name here");
  45.     print("----------------------------------\n");
  46. }
  47.  
  48. #endif
  49.  
  50. public OnGameModeInit()
  51. {
  52.     // Don't use these lines if it's a filterscript
  53.     SetGameModeText("Blank Script");
  54.     AddPlayerClass(0, 110.4660,1106.0591,14.0094, 269.1425, 0, 0, 0, 0, 0, 0);
  55.     LoadAllVehicles();
  56.     return 1;
  57. }
  58.  
  59. public OnGameModeExit()
  60. {
  61.     SaveAllVehicles();
  62.     return 1;
  63. }
  64.  
  65. public OnPlayerRequestClass(playerid, classid)
  66. {
  67.     SetPlayerPos(playerid, 1958.3783, 1343.1572, 15.3746);
  68.     SetPlayerCameraPos(playerid, 1958.3783, 1343.1572, 15.3746);
  69.     SetPlayerCameraLookAt(playerid, 1958.3783, 1343.1572, 15.3746);
  70.     return 1;
  71. }
  72.  
  73. public OnPlayerConnect(playerid)
  74. {
  75.     return 1;
  76. }
  77.  
  78. public OnPlayerDisconnect(playerid, reason)
  79. {
  80.     return 1;
  81. }
  82.  
  83. public OnPlayerSpawn(playerid)
  84. {
  85.     return 1;
  86. }
  87.  
  88. public OnPlayerDeath(playerid, killerid, reason)
  89. {
  90.     return 1;
  91. }
  92.  
  93. public OnVehicleSpawn(vehicleid)
  94. {
  95.     return 1;
  96. }
  97.  
  98. public OnVehicleDeath(vehicleid, killerid)
  99. {
  100.     return 1;
  101. }
  102.  
  103. public OnPlayerText(playerid, text[])
  104. {
  105.     return 1;
  106. }
  107.  
  108. public OnPlayerCommandText(playerid, cmdtext[])
  109. {
  110.     if (strcmp("/buy", cmdtext, true, 10) == 0)
  111.     {
  112.         if (PlayerToPoint(15, playerid,110.4660,1106.0591,13.6094))
  113.         {
  114.             ShowPlayerDialog(playerid,0,DIALOG_STYLE_LIST,"CarShop","Sentinel\nSabre\nGreenwood\nBurrito","Rinktis","iseiti");
  115.         }
  116.         else
  117.         {
  118.             SendClientMessage(playerid, 0xFFFFFFFF, "asile ne cia");
  119.         }
  120.         return 1;
  121.     }
  122.     if (strcmp("/test", cmdtext, true, 10) == 0)
  123.     {
  124.         new carid = GetPlayerVehicleID(playerid);
  125.         new test[64];
  126.         format(test, sizeof(test), "%d, %d, %f, %f, %f, %s", carid, CarInfo[carid][model], CarInfo[carid][xspawn], CarInfo[carid][yspawn], CarInfo[carid][zspawn], CarInfo[carid][owner]);
  127.         SendClientMessage(playerid, 0xFFFFFFFF, test);
  128.         new object;
  129.         object = CreateObject(18646, 0, 0, 0, 0.0, 0.0, 96.0);
  130.         AttachObjectToVehicle(object, carid, -0.5, 0.0, 0.9, 0.0, 0.0, 0.0);
  131.         return 1;
  132.     }
  133.     if (strcmp("/respawn", cmdtext, true, 10) == 0)
  134.     {
  135.         SaveAllVehicles();
  136.         DestroyAllVehicles();
  137.         LoadAllVehicles();
  138.         return 1;
  139.     }
  140.     if (strcmp("/savev", cmdtext, true, 10) == 0)
  141.     {
  142.         SaveAllVehicles();
  143.         return 1;
  144.     }
  145.     if (strcmp("/park", cmdtext, true, 10) == 0)
  146.     {
  147.         if(IsPlayerInAnyVehicle(playerid))
  148.         {
  149.             new carid = GetPlayerVehicleID(playerid);
  150.             new Float:x, Float:y, Float:z;
  151.             GetVehiclePos(carid, x, y, z);
  152.             CarInfo[carid][xspawn] = x;
  153.             CarInfo[carid][yspawn] = y;
  154.             CarInfo[carid][zspawn] = z;
  155.             SendClientMessage(playerid, 0xFFFFFFFF, "Tavo masina buvo pastatyta.");
  156.         }
  157.         else
  158.         {
  159.             SendClientMessage(playerid, 0xFFFFFFFF, "Tu turi sedeti masinoje.");
  160.         }
  161.         return 1;
  162.     }
  163.     return 0;
  164. }
  165.  
  166. stock strmatch(const String1[], const String2[])
  167. {
  168.     if ((strcmp(String1, String2, true, strlen(String2)) == 0) && (strlen(String2) == strlen(String1)))
  169.     {
  170.         return true;
  171.     }
  172.     else
  173.     {
  174.         return false;
  175.     }
  176. }
  177.  
  178. public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
  179. {
  180.     new IsOwner[MAX_PLAYER_NAME];
  181.     GetPlayerName(playerid, IsOwner, sizeof(IsOwner));
  182.        // if(strcmp(CarInfo[vehicleid][owner], IsOwner, true, strlen(CarInfo[vehicleid][owner])))
  183.     if(!strmatch(CarInfo[vehicleid][owner], IsOwner))
  184.     {
  185.         SendClientMessage(playerid, 0xFFFFFFFF, "Jus negalite vairuoti sios masinos!");
  186.         TogglePlayerControllable(playerid,1);
  187.         RemovePlayerFromVehicle(playerid);
  188.     }
  189.     else
  190.     {
  191.     }
  192.     return 1;
  193. }
  194.  
  195. public OnPlayerExitVehicle(playerid, vehicleid)
  196. {
  197.     return 1;
  198. }
  199.  
  200. public OnPlayerStateChange(playerid, newstate, oldstate)
  201. {
  202.     return 1;
  203. }
  204.  
  205. public OnPlayerEnterCheckpoint(playerid)
  206. {
  207.     return 1;
  208. }
  209.  
  210. public OnPlayerLeaveCheckpoint(playerid)
  211. {
  212.     return 1;
  213. }
  214.  
  215. public OnPlayerEnterRaceCheckpoint(playerid)
  216. {
  217.     return 1;
  218. }
  219.  
  220. public OnPlayerLeaveRaceCheckpoint(playerid)
  221. {
  222.     return 1;
  223. }
  224.  
  225. public OnRconCommand(cmd[])
  226. {
  227.     return 1;
  228. }
  229.  
  230. public OnPlayerRequestSpawn(playerid)
  231. {
  232.     return 1;
  233. }
  234.  
  235. public OnObjectMoved(objectid)
  236. {
  237.     return 1;
  238. }
  239.  
  240. public OnPlayerObjectMoved(playerid, objectid)
  241. {
  242.     return 1;
  243. }
  244.  
  245. public OnPlayerPickUpPickup(playerid, pickupid)
  246. {
  247.     return 1;
  248. }
  249.  
  250. public OnVehicleMod(playerid, vehicleid, componentid)
  251. {
  252.     return 1;
  253. }
  254.  
  255. public OnVehiclePaintjob(playerid, vehicleid, paintjobid)
  256. {
  257.     return 1;
  258. }
  259.  
  260. public OnVehicleRespray(playerid, vehicleid, color1, color2)
  261. {
  262.     return 1;
  263. }
  264.  
  265. public OnPlayerSelectedMenuRow(playerid, row)
  266. {
  267.     return 1;
  268. }
  269.  
  270. public OnPlayerExitedMenu(playerid)
  271. {
  272.     return 1;
  273. }
  274.  
  275. public OnPlayerInteriorChange(playerid, newinteriorid, oldinteriorid)
  276. {
  277.     return 1;
  278. }
  279.  
  280. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  281. {
  282.     return 1;
  283. }
  284.  
  285. public OnRconLoginAttempt(ip[], password[], success)
  286. {
  287.     return 1;
  288. }
  289.  
  290. public OnPlayerUpdate(playerid)
  291. {
  292.     return 1;
  293. }
  294.  
  295. public OnPlayerStreamIn(playerid, forplayerid)
  296. {
  297.     return 1;
  298. }
  299.  
  300. public OnPlayerStreamOut(playerid, forplayerid)
  301. {
  302.     return 1;
  303. }
  304.  
  305. public OnVehicleStreamIn(vehicleid, forplayerid)
  306. {
  307.     return 1;
  308. }
  309.  
  310. public OnVehicleStreamOut(vehicleid, forplayerid)
  311. {
  312.     return 1;
  313. }
  314.  
  315. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  316. {
  317.     if(dialogid == 0) //Jei dialogid bus lygus 0
  318.     {
  319.         if(response) //Jei paspaude mygtuka "Sutinku"
  320.         {
  321.             new BuyerName[20];
  322.             GetPlayerName(playerid, BuyerName, sizeof(BuyerName));
  323.             if(listitem == 0) //Jei pirmas pasirinkimas
  324.             {
  325.             //    new CarBla[256];
  326. //              CreateVehicle(405, 102.0081,1109.1520,13.5795,181.9413,69,1,3600);
  327.             //    format(message, sizeof(message), "parkinai masina cord: %f %f %f", vehx, vehy, vehz);
  328.                 CreateVehicleEx(405, 102.0081, 1109.1520, 13.5795, 181.9413, 69, 1, 3600, BuyerName);
  329.  
  330.             }
  331.             else if(listitem == 1) //Jei antras pasirinkimas
  332.             {
  333.                 CreateVehicleEx(475, 102.0081, 1109.1520, 13.5795, 181.9413, 69, 1, 3600, BuyerName);
  334.             }
  335.             else if(listitem == 2) //Jei antras pasirinkimas
  336.             {
  337.                 CreateVehicleEx(492, 102.0081, 1109.1520, 13.5795, 181.9413, 69, 1, 3600, BuyerName);
  338.             }
  339.             else if(listitem == 3) //Jei antras pasirinkimas
  340.             {
  341.                 CreateVehicleEx(482, 102.0081, 1109.1520, 13.5795, 181.9413, 69, 1, 3600, BuyerName);
  342.             }
  343.         }
  344.         return 1;
  345.     }
  346.     return 1;
  347. }
  348.  
  349. public OnPlayerClickPlayer(playerid, clickedplayerid, source)
  350. {
  351.     return 1;
  352. }
  353.  
  354. public PlayerToPoint(Float:radi, playerid, Float:x, Float:y, Float:z)
  355. {
  356.     if(IsPlayerConnected(playerid))
  357.     {
  358.         new Float:oldposx, Float:oldposy, Float:oldposz;
  359.         new Float:tempposx, Float:tempposy, Float:tempposz;
  360.         GetPlayerPos(playerid, oldposx, oldposy, oldposz);
  361.         tempposx = (oldposx -x);
  362.         tempposy = (oldposy -y);
  363.         tempposz = (oldposz -z);
  364.         //printf("DEBUG: X:%f Y:%f Z:%f",posx,posy,posz);
  365.         if (((tempposx < radi) && (tempposx > -radi)) && ((tempposy < radi) && (tempposy > -radi)) && ((tempposz < radi) && (tempposz > -radi)))
  366.         {
  367.             return 1;
  368.         }
  369.     }
  370.     return 0;
  371. }
  372.  
  373. stock GetFreeVehicleSlot()
  374. {
  375.     for(new i = 1; i < sizeof(validcar); i ++)
  376.     {
  377.         if(!validcar[i]) return i;
  378.     }
  379.     return -1;
  380. }
  381.  
  382. stock CreateVehicleEx(modelid, Float:x, Float:y, Float:z, Float:angle, color1, color2, respawntime, ownername[])
  383. {
  384.     //now put all the data into the array
  385.     //To get the index, use the first free slot with the function before
  386.     new carid = GetFreeVehicleSlot();
  387.     CarInfo[carid][model] = modelid;
  388.     CarInfo[carid][xspawn] = x;
  389.     CarInfo[carid][yspawn] = y;
  390.     CarInfo[carid][zspawn] = z;
  391.     CarInfo[carid][anglespawn] = angle;
  392.     CarInfo[carid][col1] = color1;
  393.     CarInfo[carid][col2] = color2;
  394.     CarInfo[carid][respawn] = respawntime;
  395. //    CarInfo[carid][owner] = ownername;
  396.     strcat(CarInfo[carid][owner], ownername, MAX_PLAYER_NAME);
  397.     //... you can just add any data you like
  398.     //dont forget to mark the carslot as used in the validcar array
  399.     validcar[carid] = true;
  400.     //At last create the vehicle in the game
  401.     CreateVehicle(modelid, x, y, z, angle, color1, color2, respawntime);
  402.     return carid;         //Make the function return the carid/array index. This is not necessarily needed, but good style
  403. }
  404.  
  405. stock SaveVehicle(vehicle, filename[36])
  406. {
  407.     dini_Create(filename);
  408.     dini_IntSet( filename , "Model", CarInfo[vehicle][model]);
  409.     dini_FloatSet(filename , "XSpawn", CarInfo[vehicle][xspawn]);
  410.     dini_FloatSet(filename, "YSpawn", CarInfo[vehicle][yspawn]);
  411.     dini_FloatSet(filename, "ZSpawn", CarInfo[vehicle][zspawn]);
  412.     dini_FloatSet(filename, "Angle", CarInfo[vehicle][anglespawn]);
  413.     dini_IntSet(filename, "COL1", CarInfo[vehicle][col1]);
  414.     dini_IntSet(filename, "COL2", CarInfo[vehicle][col2]);
  415.     dini_IntSet(filename, "Respawn", CarInfo[vehicle][respawn]);
  416.     dini_Set(filename, "Owner", CarInfo[vehicle][owner]);
  417. }
  418.  
  419. //this function will save all vehicles in files with a continous number as name, because it will be easier to load
  420. //than some other name
  421. stock SaveAllVehicles()
  422. {
  423.     new saveindex = 1;
  424.     new fname[36];
  425.     for(new i = 1; i < MAX_VEHICLES; i ++)
  426.     {
  427.         if(validcar[i])
  428.         {
  429.             format(fname, sizeof(fname), "/vehicles/%d.ini", saveindex);  //You can also add a subfolder here<.
  430.             //ex: format(fname, sizeof(fname), "/vehicles/%d.ini", saveindex); //will put all the files onto /scriptfiles/vehicles/ if the folder exists
  431.             SaveVehicle(i, fname);
  432.             saveindex ++; //Increase the filename number for the next vehicle
  433.         }
  434.     }
  435. }
  436.  
  437. stock LoadVehicle(filename[36])
  438. {
  439. //    new iniid = ini_OpenIni(filename);
  440.     //Now just use CreateVehicleEx and load all the parameters from the ini file
  441.     //The function will do all the rest for you
  442.  //   ini_CloseIni(iniid);
  443.     CreateVehicleEx(dini_Int(filename, "Model"), dini_Float(filename, "XSpawn"), dini_Float(filename, "YSpawn"), dini_Float(filename, "ZSpawn"), dini_Float(filename, "Angle"), dini_Int(filename, "COL1"), dini_Int(filename, "COL2"),dini_Int(filename, "Respawn"), dini_Get(filename, "Owner") );
  444.  
  445. }
  446.  
  447.  
  448. stock LoadAllVehicles()
  449. {
  450.     new fname[36];
  451.     new index = 1;
  452.     format(fname, sizeof(fname), "/vehicles/%d.ini", index);
  453.     while(fexist(fname))  //Here's the reason why the ini files are named continuosly
  454.     //This function keeps loading the files with next number as long as there is a next one
  455.     //and then stops loading, so you do not need a plugin to get all files in a folder or so
  456.     {
  457.         LoadVehicle(fname);
  458.         index ++;
  459.         format(fname, sizeof(fname), "/vehicles/%d.ini", index);
  460.     }
  461. }
  462.  
  463. stock DestroyAllVehicles()
  464. {
  465.     for(new i = 0; i < MAX_VEHICLES; i++)
  466.     {
  467.         DestroyVehicle(i);
  468.     }
  469. }
Advertisement
Add Comment
Please, Sign In to add comment