Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.42 KB | None | 0 0
  1. //===========================Section: Includes==================================
  2. #include <a_samp>
  3. //===========================Section: Definations===============================
  4. #define COLOR_GRAD1 0xB4B5B7FF
  5. #define COLOR_GRAD2 0xBFC0C2FF
  6. #define COLOR_GRAD3 0xCBCCCEFF
  7. #define COLOR_GRAD4 0xD8D8D8FF
  8. #define COLOR_GRAD5 0xE3E3E3FF
  9. #define COLOR_GRAD6 0xF0F0F0FF
  10. #define COLOR_GREEN 0x33AA33AA
  11. #define COLOR_GREY 0xAFAFAFAA
  12. #define COLOR_WHITE 0xFFFFFFAA
  13. #define COLOR_YELLOW 0xFFFF00AA
  14. #define COLOR_YELLOW2 0xF5DEB3AA
  15. //===========================Section: Forwards==================================
  16. forward split(const strsrc[], strdest[][], delimiter);
  17. forward LoadCar();
  18. forward CheckOwner(playerid);
  19. forward SaveCars();
  20. //===========================Section: Variables=================================
  21. enum pInfo
  22. {
  23.     pCarKey,
  24. }
  25. new PlayerInfo[256][pInfo];
  26. enum cInfo
  27. {
  28.     cModel,
  29.     Float:cLocationx,
  30.     Float:cLocationy,
  31.     Float:cLocationz,
  32.     Float:cAngle,
  33.     cColorOne,
  34.     cColorTwo,
  35.     cOwner[MAX_PLAYER_NAME],
  36.     cDescription[MAX_PLAYER_NAME],
  37.     cValue,
  38.     cLicense,
  39.     cRegistration,
  40.     cOwned,
  41.     cLock,
  42.     ownedvehicle,
  43. };
  44. new CarInfo[12][cInfo];
  45.  
  46. new CarAutolock[999]; // Variable for Autolocking Car Doors
  47. new cartrack[256];
  48. new CarOffered[256];
  49. //===========================Section: strtok & split============================
  50. strtok(const string[], &index)
  51. {
  52.     new length = strlen(string);
  53.     while ((index < length) && (string[index] <= ' '))
  54.     {
  55.         index++;
  56.     }
  57.  
  58.     new offset = index;
  59.     new result[20];
  60.     while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
  61.     {
  62.         result[index - offset] = string[index];
  63.         index++;
  64.     }
  65.     result[index - offset] = EOS;
  66.     return result;
  67. }
  68.  
  69. public split(const strsrc[], strdest[][], delimiter)
  70. {
  71.     new i, li;
  72.     new aNum;
  73.     new len;
  74.     while(i <= strlen(strsrc)){
  75.         if(strsrc[i]==delimiter || i==strlen(strsrc)){
  76.             len = strmid(strdest[aNum], strsrc, li, i, 128);
  77.             strdest[aNum][len] = 0;
  78.             li = i+1;
  79.             aNum++;
  80.         }
  81.         i++;
  82.     }
  83.     return 1;
  84. }
  85. //===========================Section: Callbacks & Functions=====================
  86. public OnFilterScriptInit()
  87. {
  88.     printf("Filterscript [FS]CarOwnership.amx Initiated\n");
  89.     LoadCar();
  90.     for(new i = 1; i < sizeof(CarInfo); i++)
  91.     {
  92.         CarInfo[i][ownedvehicle] = CreateVehicle(CarInfo[i][cModel],CarInfo[i][cLocationx],CarInfo[i][cLocationy],CarInfo[i][cLocationz],CarInfo[i][cAngle],CarInfo[i][cColorOne],CarInfo[i][cColorTwo],300000);
  93.     }
  94.     SetTimer("SaveCars",60000,1);
  95.     SetTimer("CheckOwner",5000,1);
  96.     return 1;
  97. }
  98. public OnVehicleSpawn(vehicleid)
  99. {
  100.     for(new i = 1; i < sizeof(CarInfo); i++)
  101.     {
  102.         ChangeVehicleColor(CarInfo[i][ownedvehicle],CarInfo[vehicleid][cColorOne],CarInfo[vehicleid][cColorTwo]);
  103.     }
  104.     return 1;
  105. }
  106. public CheckOwner(playerid)
  107. {
  108.     if(IsPlayerConnected(playerid))
  109.     {
  110.         for(new i = 1; i < sizeof(CarInfo); i++)
  111.         {
  112.             new playername[MAX_PLAYER_NAME];
  113.             GetPlayerName(playerid,playername,sizeof(playername));
  114.             if(strcmp(playername,CarInfo[i][cOwner],true)==0)
  115.             {
  116.                 PlayerInfo[i][pCarKey] = i;
  117.                 return i;
  118.             }
  119.         }
  120.     }
  121.     return 1;
  122. }
  123. public OnPlayerConnect(playerid)
  124. {
  125.     PlayerInfo[playerid][pCarKey] = 0;
  126.     CheckOwner(playerid);
  127.     return 1;
  128. }
  129. public OnPlayerCommandText(playerid, cmdtext[])
  130. {
  131.     new idx;
  132.     new string[256];
  133.     new cmd[256];
  134.     new tmp[256];
  135.     new sendername[MAX_PLAYER_NAME];
  136.     cmd = strtok(cmdtext, idx);
  137.     new vehid = GetPlayerVehicleID(playerid);
  138.     if(strcmp(cmd, "/carbuy", true) == 0)
  139.     {
  140.         if(IsPlayerConnected(playerid))
  141.         {
  142.             for(new i = 0; i < sizeof(CarInfo); i++)
  143.             {
  144.                 if(CarInfo[i][ownedvehicle] == vehid)
  145.                 {
  146.                     if(PlayerInfo[playerid][pCarKey]!=0)
  147.                     {
  148.                         SendClientMessage(playerid, COLOR_GREY, "You already own a car, type /car sell if you want to buy this one!");
  149.                         return 1;
  150.                     }
  151.                     if(CarInfo[i][cOwned]==1)
  152.                     {
  153.                         SendClientMessage(playerid, COLOR_GREY, "Someone already owns this car");
  154.                         return 1;
  155.                     }
  156.                     if(GetPlayerMoney(playerid) >= CarInfo[i][cValue])
  157.                     {
  158.                         PlayerInfo[playerid][pCarKey] = i;
  159.                         CarInfo[i][cOwned] = 1;
  160.                         CarOffered[playerid]=0;
  161.                         GetPlayerName(playerid, sendername, sizeof(sendername));
  162.                         strmid(CarInfo[i][cOwner], sendername, 0, strlen(sendername), 999);
  163.                         GivePlayerMoney(playerid,-CarInfo[i][cValue]);
  164.                         GameTextForPlayer(playerid, "~w~Congratulations~n~This is your car until you sell it!", 5000, 3);
  165.                         SendClientMessage(playerid, COLOR_GRAD2, "Congratulations on your new purchase!");
  166.                         SendClientMessage(playerid, COLOR_GRAD2, "Type /car manual to view the car manual!");
  167.                         TogglePlayerControllable(playerid, 1);
  168.                         SaveCars();
  169.                         return 1;
  170.                     }
  171.                     else
  172.                     {
  173.                         SendClientMessage(playerid, COLOR_GREY, "   You don't have the cash for that!");
  174.                         return 1;
  175.                     }
  176.                 }
  177.             }
  178.         }
  179.         return 1;
  180.     }
  181.     if(strcmp(cmd,"/car",true)==0)
  182.     {
  183.         new playername[MAX_PLAYER_NAME];
  184.         GetPlayerName(playerid,playername,sizeof(playername));
  185.         tmp = strtok(cmdtext, idx);
  186.         if(!strlen(tmp))
  187.         {
  188.             SendClientMessage(playerid,COLOR_WHITE,"(( Use /car manual for detailed instructions for: ))");
  189.             SendClientMessage(playerid,COLOR_WHITE,"(( [FS]CarOwnership by Ipuvaepe ))");
  190.             return 1;
  191.         }
  192.         if(strcmp(tmp,"exit",true)==0)
  193.         {
  194.             CarOffered[playerid]=0;
  195.             RemovePlayerFromVehicle(playerid);
  196.             TogglePlayerControllable(playerid, 1);
  197.             return 1;
  198.         }
  199.         if(strcmp(tmp, "manual", true) == 0)
  200.         {
  201.             if(IsPlayerConnected(playerid))
  202.             {
  203.                 if(PlayerInfo[playerid][pCarKey]!=0)
  204.                 {
  205.                     format(string,sizeof(string),"________________%s________________",CarInfo[CheckOwner(playerid)][cDescription]);
  206.                     SendClientMessage(playerid, COLOR_GREEN,string);
  207.                     SendClientMessage(playerid, COLOR_GRAD2,"** /carbuy - Buys the car (if for sale)");
  208.                     SendClientMessage(playerid, COLOR_GRAD2,"** /car sell - Sells the car");
  209.                     SendClientMessage(playerid, COLOR_GRAD2,"** /car manual - Shows this list");
  210.                     SendClientMessage(playerid, COLOR_GRAD2,"** /car exit - Exits the car");
  211.                     SendClientMessage(playerid, COLOR_GRAD2,"** /car locate - Uses the car's On-Star to locate");
  212.                     SendClientMessage(playerid, COLOR_GRAD2,"** /car lock - Locks the car");
  213.                     SendClientMessage(playerid, COLOR_GRAD2,"** /car unlock - Unlocks the car");
  214.                     SendClientMessage(playerid, COLOR_GRAD2,"** /car autolock - Automatically locks the car upon exiting");
  215.                     return 1;
  216.                 }
  217.                 else
  218.                 {
  219.                     SendClientMessage(playerid,COLOR_GRAD2,"** You do not currently own a car! **");
  220.                     return 1;
  221.                 }
  222.             }
  223.             return 1;
  224.         }
  225.         if(strcmp(tmp, "buy", true) == 0)
  226.         {
  227.             if(IsPlayerConnected(playerid))
  228.             {
  229.                 for(new i = 0; i < sizeof(CarInfo); i++)
  230.                 {
  231.                     if(CarInfo[i][ownedvehicle] == vehid)
  232.                     {
  233.                         if(PlayerInfo[playerid][pCarKey]!=0)
  234.                         {
  235.                             SendClientMessage(playerid, COLOR_GREY, "You already own a car, type /carsell if you want to buy this one!");
  236.                             return 1;
  237.                         }
  238.                         if(CarInfo[i][cOwned]==1)
  239.                         {
  240.                             SendClientMessage(playerid, COLOR_GREY, "Someone already owns this car");
  241.                             return 1;
  242.                         }
  243.                         if(GetPlayerMoney(playerid) >= CarInfo[i][cValue])
  244.                         {
  245.                             PlayerInfo[playerid][pCarKey] = i;
  246.                             CarInfo[i][cOwned] = 1;
  247.                             CarOffered[playerid]=0;
  248.                             GetPlayerName(playerid, sendername, sizeof(sendername));
  249.                             strmid(CarInfo[i][cOwner], sendername, 0, strlen(sendername), 999);
  250.                             GivePlayerMoney(playerid,-CarInfo[i][cValue]);
  251.                             GameTextForPlayer(playerid, "~w~Congratulations~n~This is your car until you sell it!", 5000, 3);
  252.                             SendClientMessage(playerid, COLOR_GRAD2, "Congratulations on your new purchase!");
  253.                             SendClientMessage(playerid, COLOR_GRAD2, "Type /manual to view the car manual!");
  254.                             TogglePlayerControllable(playerid, 1);
  255.                             SaveCars();
  256.                             return 1;
  257.                         }
  258.                         else
  259.                         {
  260.                             SendClientMessage(playerid, COLOR_GREY, "   You don't have the cash for that!");
  261.                             return 1;
  262.                         }
  263.                     }
  264.                 }
  265.             }
  266.             return 1;
  267.         }
  268.         if(strcmp(tmp, "sell", true) == 0)
  269.         {
  270.             if(IsPlayerConnected(playerid))
  271.             {
  272.                 GetPlayerName(playerid, playername, sizeof(playername));
  273.                 if(PlayerInfo[playerid][pCarKey] == 0)
  274.                 {
  275.                     SendClientMessage(playerid, COLOR_GREY, "You don't own a car.");
  276.                     return 1;
  277.                 }
  278.                 if(PlayerInfo[playerid][pCarKey] != 0 && strcmp(playername, CarInfo[PlayerInfo[playerid][pCarKey]][cOwner], true) == 0)
  279.                 {
  280.                     new car = PlayerInfo[playerid][pCarKey];
  281.                     CarInfo[car][cOwned] = 0;
  282.                     GetPlayerName(playerid, sendername, sizeof(sendername));
  283.                     strmid(CarInfo[car][cOwner], "Dealership", 0, strlen("Dealership"), 999);
  284.                     GivePlayerMoney(playerid,CarInfo[car][cValue]);
  285.                     PlayerPlaySound(playerid, 1052, 0.0, 0.0, 0.0);
  286.                     format(string, sizeof(string), "~w~You have sold your car for: ~n~~g~$%d", CarInfo[car][cValue]);
  287.                     GameTextForPlayer(playerid, string, 10000, 3);
  288.                     RemovePlayerFromVehicle(playerid);
  289.                     TogglePlayerControllable(playerid, 1);
  290.                     PlayerInfo[playerid][pCarKey] = 999;
  291.                     return 1;
  292.                 }
  293.             }
  294.             return 1;
  295.         }
  296.         if(strcmp(tmp, "locate", true) == 0)
  297.         {
  298.             if(!IsPlayerConnected(playerid)) { return 1; }
  299.             if(PlayerInfo[playerid][pCarKey] == 0) { GameTextForPlayer(playerid, "~w~You do not have a car to locate", 2500, 3); return 1; }
  300.             if(cartrack[playerid]==0)
  301.             {
  302.                 SendClientMessage(playerid,COLOR_WHITE,"On-Star: This is On-Star's automated vehicle tracking system");
  303.                 SendClientMessage(playerid,COLOR_WHITE,"On-Star: Please enter your PIN # and password now");
  304.                 SendClientMessage(playerid,COLOR_WHITE,"On-Star: Your vehicle's location is now uploaded to your phone");
  305.                 SetPlayerCheckpoint(playerid,CarInfo[PlayerInfo[playerid][pCarKey]][cLocationx], CarInfo[PlayerInfo[playerid][pCarKey]][cLocationy], CarInfo[PlayerInfo[playerid][pCarKey]][cLocationz], 5.0);
  306.                 cartrack[playerid] = 1;
  307.                 return 1;
  308.             }
  309.             else
  310.             {
  311.                 SendClientMessage(playerid,COLOR_WHITE,"On-Star: This is On-Star's automated vehicle tracking system");
  312.                 SendClientMessage(playerid,COLOR_WHITE,"On-Star: The tracking on your vehicle has been canceled");
  313.                 DisablePlayerCheckpoint(playerid);
  314.                 cartrack[playerid] = 0;
  315.                 return 1;
  316.             }
  317.         }
  318.         if(strcmp(tmp, "lock", true) == 0)
  319.         {
  320.             new keycar = PlayerInfo[playerid][pCarKey];
  321.             if(IsPlayerConnected(playerid))
  322.             {
  323.                 for(new i = 0; i < MAX_PLAYERS; i++)
  324.                 {
  325.                     SetVehicleParamsForPlayer(CarInfo[keycar][ownedvehicle],i,0,1);
  326.                 }
  327.                 format(string, sizeof(string), "~w~Car~n~~r~Locked");
  328.                 GameTextForPlayer(playerid, string, 10000, 3);
  329.                 CarInfo[keycar][cLock] = 1;
  330.                 return 1;
  331.             }
  332.         }
  333.         if(strcmp(tmp, "unlock", true) == 0)
  334.         {
  335.             new keycar = PlayerInfo[playerid][pCarKey];
  336.             if(IsPlayerConnected(playerid))
  337.             {
  338.                 for(new i = 0; i < MAX_PLAYERS; i++)
  339.                 {
  340.                     SetVehicleParamsForPlayer(CarInfo[keycar][ownedvehicle],i,0,0);
  341.                 }
  342.                 format(string, sizeof(string), "~w~Car~n~~g~Unlocked");
  343.                 GameTextForPlayer(playerid, string, 10000, 3);
  344.                 CarInfo[keycar][cLock] = 0;
  345.                 return 1;
  346.             }
  347.         }
  348.         if(strcmp(tmp, "autolock", true) == 0)
  349.         {
  350.             new keycar = PlayerInfo[playerid][pCarKey];
  351.             if(CarAutolock[CarInfo[keycar][ownedvehicle]] == 0) { CarAutolock[CarInfo[keycar][ownedvehicle]] = 1; format(string, sizeof(string), "~w~Car Autolock~n~~r~Engaged"); return 1; }
  352.             if(CarAutolock[CarInfo[keycar][ownedvehicle]] == 1) { CarAutolock[CarInfo[keycar][ownedvehicle]] = 0; format(string, sizeof(string), "~w~Car Autolock~n~~g~Disengaged"); return 1; }
  353.             GameTextForPlayer(playerid, string, 10000, 3);
  354.             return 1;
  355.         }
  356.     }
  357.     return 0;
  358. }
  359. /*----------Car Save Functions----------*/
  360. public LoadCar()
  361. {
  362.     new arrCoords[13][64];
  363.     new strFromFile2[256];
  364.     new File: file = fopen("[FS]CarOwnership.cfg", io_read);
  365.     if (file)
  366.     {
  367.         new idx = 0;
  368.         while (idx < sizeof(CarInfo))
  369.         {
  370.             fread(file, strFromFile2);
  371.             split(strFromFile2, arrCoords, ',');
  372.             CarInfo[idx][cModel] = strval(arrCoords[0]);
  373.             CarInfo[idx][cLocationx] = floatstr(arrCoords[1]);
  374.             CarInfo[idx][cLocationy] = floatstr(arrCoords[2]);
  375.             CarInfo[idx][cLocationz] = floatstr(arrCoords[3]);
  376.             CarInfo[idx][cAngle] = floatstr(arrCoords[4]);
  377.             CarInfo[idx][cColorOne] = strval(arrCoords[5]);
  378.             CarInfo[idx][cColorTwo] = strval(arrCoords[6]);
  379.             strmid(CarInfo[idx][cOwner], arrCoords[7], 0, strlen(arrCoords[7]), 255);
  380.             strmid(CarInfo[idx][cDescription], arrCoords[8], 0, strlen(arrCoords[8]), 255);
  381.             CarInfo[idx][cValue] = strval(arrCoords[9]);
  382.             CarInfo[idx][cLicense] = strval(arrCoords[10]);
  383.             CarInfo[idx][cOwned] = strval(arrCoords[11]);
  384.             CarInfo[idx][cLock] = strval(arrCoords[12]);
  385.             printf("CarInfo: %d Owner:%s LicensePlate %s",idx,CarInfo[idx][cOwner],CarInfo[idx][cLicense]);
  386.             idx++;
  387.         }
  388.     }
  389.     return 1;
  390. }
  391. public SaveCars()
  392. {
  393.     new idx;
  394.     new File: file2;
  395.     while (idx < sizeof(CarInfo))
  396.     {
  397.         new coordsstring[256];
  398.         format(coordsstring, sizeof(coordsstring), "%d,%f,%f,%f,%f,%d,%d,%s,%s,%d,%s,%d,%d\n",
  399.         CarInfo[idx][cModel],
  400.         CarInfo[idx][cLocationx],
  401.         CarInfo[idx][cLocationy],
  402.         CarInfo[idx][cLocationz],
  403.         CarInfo[idx][cAngle],
  404.         CarInfo[idx][cColorOne],
  405.         CarInfo[idx][cColorTwo],
  406.         CarInfo[idx][cOwner],
  407.         CarInfo[idx][cDescription],
  408.         CarInfo[idx][cValue],
  409.         CarInfo[idx][cLicense],
  410.         CarInfo[idx][cOwned],
  411.         CarInfo[idx][cLock]);
  412.         if(idx == 0)
  413.         {
  414.             file2 = fopen("[FS]CarOwnership.cfg", io_write);
  415.         }
  416.         else
  417.         {
  418.             file2 = fopen("[FS]CarOwnership.cfg", io_append);
  419.         }
  420.         fwrite(file2, coordsstring);
  421.         idx++;
  422.         fclose(file2);
  423.     }
  424.     return 1;
  425. }
  426.  
  427. public OnPlayerExitVehicle(playerid, vehicleid)
  428. {
  429.     if(IsPlayerConnected(playerid))
  430.     {
  431.         for(new i = 0; i < sizeof(CarInfo); i++)
  432.         {
  433.             if(vehicleid == CarInfo[i][ownedvehicle])
  434.             {
  435.                 new Float:x,Float:y,Float:z;
  436.                 new Float:a;
  437.                 GetVehiclePos(vehicleid, x, y, z);
  438.                 GetVehicleZAngle(vehicleid, a);
  439.                 CarInfo[i][cLocationx] = x;
  440.                 CarInfo[i][cLocationy] = y;
  441.                 CarInfo[i][cLocationz] = z;
  442.                 CarInfo[i][cAngle] = a;
  443.             }
  444.             if(CarAutolock[vehicleid] == 1)
  445.             {
  446.                 for(new j = 0; j < MAX_PLAYERS; i++)
  447.                 {
  448.                     if (IsPlayerConnected(j))
  449.                     {
  450.                         SetVehicleParamsForPlayer(vehicleid,j,0,1);
  451.                         GameTextForPlayer(playerid,"~w~Car~n~~r~Autolocked",2500,3);
  452.                         return 1;
  453.                     }
  454.                 }
  455.             }
  456.         }
  457.     }
  458.     return 1;
  459. }
  460. public OnPlayerStateChange(playerid, newstate, oldstate)
  461. {
  462.     if(newstate==2)
  463.     {
  464.         for(new i = 0; i < sizeof(CarInfo); i++)
  465.         {
  466.             new newcar = GetPlayerVehicleID(playerid);
  467.             new string[256];
  468.             if(newcar == CarInfo[i][ownedvehicle])
  469.             {
  470.                 if(CarInfo[i][cOwned]==0)
  471.                 {
  472.                     TogglePlayerControllable(playerid, 0);
  473.                     CarOffered[playerid]=1;
  474.                     format(string,sizeof(string),"~w~Car: %s~n~Price: ~g~%d~n~~w~/carbuy to buy this car",CarInfo[i][cDescription],CarInfo[i][cValue]);
  475.                     GameTextForPlayer(playerid,string,5000,5);
  476.                 }
  477.             }
  478.         }
  479.     }
  480.     return 1;
  481. }
  482. public OnPlayerEnterCheckpoint(playerid)
  483. {
  484.     if(cartrack[playerid]!=0)
  485.     {
  486.         SendClientMessage(playerid,COLOR_YELLOW,"SMS: On-Star: Our sensors show that you have come within 5.0 metres of your vehicle");
  487.         DisablePlayerCheckpoint(playerid);
  488.         cartrack[playerid] = 0;
  489.     }
  490.     return 1;
  491. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement