Advertisement
Guest User

SAMP Vehicle Spawner (Binary Search)

a guest
Jan 26th, 2015
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 5.79 KB | None | 0 0
  1. //Vehicles ordered alphabetically
  2. new VehiclesSorted[][] =
  3. {
  4.         "Admiral","Alpha","Ambulance","Andromada","AT-400","Baggage","Bandito",
  5.         "Banshee","Barracks","Beagle","Benson","Berkley's RC_Van","BF_Injection",
  6.         "BF-400","Bike","Blade","Blista Compact","Bloodring_Banger","BMX","Bobcat",
  7.         "Boxville","Bravura","Broadway","Buccaneer","Buffalo","Bullet","Burrito",
  8.         "Bus","Cabbie","Caddy","Cadrona","Camper","Cargobob","Cement_Truck",
  9.         "Cheetah","Clover","Club","Coach","Coastguard","Combine","Comet","Cropduster",
  10.         "DFT-30","Dinghy","Dodo","Dozer","Dumper","Dune","Elegant","Elegy","Emperor",
  11.         "Enforcer","Esperanto","Euros","Faggio","FBI_Rancher","FBI_Truck","FCR-900",
  12.         "Feltzer","Firetruck_Ladder","Firetruck","Flash","Flatbed","Forklift",
  13.         "Fortune","Freeway","Freight_Box","Freight_Flat","Freight",
  14.         "Glendale","Greenwood","Hermes","Hotdog","Hotknife","Hotring_Racer_A",
  15.         "Hotring_Racer_B","Hotring","HPV1000","Hunter","Huntley","Hustler","Hydra",
  16.         "Infernus","Intruder","Jester","Jetmax","Journey","Kart","Landstalker",
  17.         "Launch","Leviathan","Linerunner","Majestic","Manana","Marquis","Maverick",
  18.         "Merit","Mesa","Monster_A","Monster_B","Moonbeam","Mountain_Bike","Mower",
  19.         "Mule","Nebula","Nevada","News_Chopper","News_Van","NRG-500","Oceanic",
  20.         "Packer","Patriot","PCJ-600","Perenniel","Phoenix","Picador","Pizzaboy",
  21.         "Police_LS","Police_LV","Police_Maverick","Police_Ranger","Police_SF","Pony",
  22.         "Predator","Premier","Previon","Primo","Quad","Raindance","Rancher",
  23.         "RC_Bandit","RC_Baron","RC_Cam","RC_Goblin","RC_Raider","RC_Tiger","Reefer",
  24.         "Regina","Remington","Rhino","Roadtrain","Romero","Rumpo","Rustler","S.W.A.T",
  25.         "Sabre","Sadler","Sanchez","Sandking","Savanna","Seasparrow",
  26.         "Securicar","Sentinel","Shamal","Skimmer","Slamvan","Solair","Sparrow",
  27.         "Speeder","Squallo","Stafford","Stairs","Stallion","Stratum",
  28.         "Stretch","Stunt","Sultan","Sunrise","Super_GT","Sweeper","Tahoma",
  29.         "Tampa","Tanker","Taxi","Tiller","Tornado","Towtruck","Tractor","Trailer",
  30.         "Trashmaster","Tropic","Tug","Turismo","Uranus","Utility_Trailer",
  31.         "Utility_van","Vincent","Virgo","Voodoo","Vortex","Walton","Washington","Wayfarer",
  32.         "Whoopee","Willard","Windsor","Yankee","Yosemite","ZR-350"
  33. };
  34.  
  35. //This list of id's maps to the above list of ordered vehicles
  36. new VehiclesSortedIDs[] =
  37. {
  38.         445, 602, 416, 592, 577, 485, 568, 429,
  39.         433, 511, 499, 459, 424, 581, 509,
  40.         536, 496, 504, 481, 422, 498,
  41.         401, 575, 518, 402, 541, 482, 431, 438, 457,
  42.         527, 483, 548, 524, 415, 542, 589, 437,
  43.         472, 532, 480, 512, 578, 473, 593, 486, 406,
  44.         573, 507, 562, 585, 427, 419, 587, 462,
  45.         490, 528, 521, 533, 407, 544, 565,
  46.         455, 530, 526, 463, 590, 569, 537,
  47.         466, 492, 474, 588, 434, 494,
  48.         502, 503, 523, 425, 579, 545, 520, 411,
  49.         546, 559, 493, 508, 571, 400, 595, 417,
  50.         403, 517, 410, 484, 487, 551,
  51.         500, 556, 557, 418, 510, 572, 414,
  52.         516, 553, 488, 582, 522, 467, 443, 470,
  53.         461, 404, 603, 600, 448, 596, 598,
  54.         497, 599, 597, 413,
  55.         430, 426, 436, 547, 471, 563, 489,
  56.         441, 464, 594, 501, 465, 564, 453,
  57.         479, 534, 432, 515, 442, 440, 476, 601,
  58.         475, 543, 468, 495, 567, 447,
  59.         428, 405, 519, 460, 535, 458, 469,
  60.         452, 446, 580, 608, 439, 561,
  61.         409, 513, 560, 550, 506, 574, 566,
  62.         549, 514, 420, 610, 576, 525, 531, 435,
  63.         408, 454, 583, 451, 558, 611,
  64.         552, 540, 491, 412, 539, 478, 421, 586,
  65.         423, 529, 555, 456, 554, 477
  66. };
  67.  
  68. //Search for vehicle ID using a binary search to reduce comparison count and increase efficiency
  69. //The two optional parameters are not needed when calling this function, they are used by the function itself when doing recursion
  70. SearchVehicleId(playerid, vehname[], index = -1, character = 0) {
  71.     new suggestions[200];
  72.     if(index == -1) {
  73.         new low = 0; new high = 199; new mid;
  74.         while(low <= high) {
  75.             mid = (low + high)/2;
  76.             if(toupper(VehiclesSorted[mid][0]) == toupper(vehname[0])) {
  77.                 index = mid;
  78.                 return SearchVehicleId(playerid,vehname,index,character++);
  79.             }
  80.             else {
  81.                 if(toupper(VehiclesSorted[mid][0]) > toupper(vehname[0])) {
  82.                     high = mid - 1;
  83.                 }
  84.                 if(toupper(VehiclesSorted[mid][0]) < toupper(vehname[0])) {
  85.                     low = mid + 1;
  86.                 }
  87.             }
  88.         }
  89.     }
  90.     else {
  91.         if(toupper(VehiclesSorted[index][character]) > toupper(vehname[character])) {
  92.             while(toupper(VehiclesSorted[index][character]) >= toupper(vehname[character]) && toupper(VehiclesSorted[index][0]) == toupper(vehname[0])) {
  93.                 if(strcmp(VehiclesSorted[index],vehname,true) == 0) return VehiclesSortedIDs[index];
  94.                 else index--;
  95.             }
  96.         }
  97.         else if (toupper(VehiclesSorted[index][character]) < toupper(vehname[character])) {
  98.             while(toupper(VehiclesSorted[index][character]) <= toupper(vehname[character]) && toupper(VehiclesSorted[index][0]) == toupper(vehname[0])) {
  99.                 if(strcmp(VehiclesSorted[index],vehname,true) == 0) return VehiclesSortedIDs[index];
  100.                 else index++;
  101.             }
  102.         }
  103.         else {
  104.             if(strcmp(VehiclesSorted[index],vehname,true) == 0) return VehiclesSortedIDs[index];
  105.             else return SearchVehicleId(playerid,vehname,index,(character + 1));
  106.         }
  107.     }
  108.     format(suggestions, sizeof(suggestions),"%s, %s, %s, %s, %s",VehiclesSorted[(index - 2)], VehiclesSorted[(index - 1)], VehiclesSorted[index], VehiclesSorted[(index + 1)], VehiclesSorted[(index + 2)]);
  109.     SendClientMessage(playerid, COLOR_RED, "Vehicle not found, Close matches:");
  110.     SendClientMessage(playerid, COLOR_RED, suggestions);
  111.     return -1;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement