y_vehicledata.txt 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. [size=5][color=red][b][u][color=green]Introduction[/color][/u][/b][/color][/size]
  2. I have time and again seen code defining properties of vehicles that uses loops or vast chunks of memory, when far far less is needed. Many of those times I've even explained HOW to do it smaller, but no-one has, so I did.
  3. Thus, I present [B]y_vehicledata[/B] (formally [B]YSI_Game\y_vehicledata[/B], but not yet in any YSI repository).
  4. The include has three prefixes - that is, three groups of functions - "Vehicle_", "Model_", and "VIM_". All three groups have the same functions, but take different inputs.
  5. [list][*][size=2][color=red][b][u][color=green]Vehicle_[/color][/u][/b][/color][/size][/list]
  6. These functions take a [b]vehicleid[/b], i.e. a value returned by [b]CreateVehicle[/b] or [b]GetPlayerSurfingVehicleID[/b] etc.
  7. [list][*][size=2][color=red][b][u][color=green]Model_[/color][/u][/b][/color][/size][/list]
  8. These functions take a [b]modelid[/b], i.e. an object as passed to [b]AddStaticVehicle[/b] or returned by [b]GetVehicleModel[/b] etc.
  9. [list][*][size=2][color=red][b][u][color=green]VIM_[/color][/u][/b][/color][/size][/list]
  10. These functions take a special [b]VIM[/b] variable, that is, one with a tag of [b]VIM:[/b]. [b]VIM[/b] stands for Vehicle Internal Model, and is specific to this include (but others are free to use it if they want). This is different to normal models in three ways:
  11. 1) They are always valid.
  12. 2) They can be used as indexes.
  13. 3) They have a strong tag to differentiate them.
  14. This second point is very important. Standard model IDs start at 400 and go up to 611, so if you want to use them to index an array you either need 400 empty slots in the array, or to subtract 400 from the model ID (which is exactly what [b]VIM[/b]s are).
  15. [size=5][color=red][b][u][color=green]Functions[/color][/u][/b][/color][/size]
  16. [list]
  17. [*][b]Vehicle_GetCategory(vehicleid)[/b] - Return the category of the vehicle, as defined on [url=http://wiki.sa-mp.com/]http://wiki.sa-mp.com/[/url]. The return values are;
  18. [pawn]
  19. CATEGORY_UNKNOWN
  20. CATEGORY_AIRPLANE
  21. CATEGORY_HELICOPTER
  22. CATEGORY_BIKE
  23. CATEGORY_CONVERTIBLE
  24. CATEGORY_INDUSTRIAL
  25. CATEGORY_LOWRIDER
  26. CATEGORY_OFFROAD // Two
  27. CATEGORY_OFF_ROAD // versions
  28. CATEGORY_PUBLIC
  29. CATEGORY_SALOON
  30. CATEGORY_SPORT
  31. CATEGORY_STATIONWAGON // Two
  32. CATEGORY_STATION_WAGON // versions
  33. CATEGORY_BOAT
  34. CATEGORY_TRAILER
  35. CATEGORY_UNIQUE
  36. CATEGORY_RC
  37. [/pawn]
  38. Some vehicles may seem to fit in to multiple categories, more information on those can be got with the functions below. These ONLY return data from the wiki.
  39. [*][b]Vehicle_IsValid(vehicleid)[/b] - Checks if this vehicle is valid, by checking that the model is valid.
  40. [*][b]Vehicle_IsCar(vehicleid)[/b] - This function, and those below, return various pieces of data on vehicles. Several vehicles may have seemingly conflicting data, such as being listed as both a Boat AND a Plane (or Boat and Car for the Vortex). Check the include for what vehicles have what flags.
  41. [*][b]Vehicle_IsTruck(vehicleid)[/b] -
  42. [*][b]Vehicle_IsVan(vehicleid)[/b] -
  43. [*][b]Vehicle_IsFire(vehicleid)[/b] -
  44. [*][b]Vehicle_IsPolice(vehicleid)[/b] -
  45. [*][b]Vehicle_IsFBI(vehicleid)[/b] -
  46. [*][b]Vehicle_IsSWAT(vehicleid)[/b] -
  47. [*][b]Vehicle_IsMilitary(vehicleid)[/b] -
  48. [*][b]Vehicle_IsWeaponised(vehicleid)[/b] - Any vehicle that can "fire" something (including water).
  49. [*][b]Vehicle_IsHelicopter(vehicleid)[/b] -
  50. [*][b]Vehicle_IsBoat(vehicleid)[/b] -
  51. [*][b]Vehicle_IsPlane(vehicleid)[/b] -
  52. [*][b]Vehicle_IsBike(vehicleid)[/b] -
  53. [*][b]Vehicle_IsManual(vehicleid)[/b] - This refers to pedal power, not stick-shift.
  54. [*][b]Vehicle_IsAmbulance(vehicleid)[/b] -
  55. [*][b]Vehicle_IsTaxi(vehicleid)[/b] -
  56. [*][b]Vehicle_IsOnWater(vehicleid)[/b] - Vehicles not in [b]CATEGORY_BOAT[/b] that can go on water.
  57. [*][b]Vehicle_IsCoastguard(vehicleid)[/b] -
  58. [*][b]Vehicle_IsTrain(vehicleid)[/b] - Trailers and engines.
  59. [*][b]Vehicle_IsLS(vehicleid)[/b] - For city-specific police vehicles.
  60. [*][b]Vehicle_IsSF(vehicleid)[/b] - For city-specific police vehicles.
  61. [*][b]Vehicle_IsLV(vehicleid)[/b] - For city-specific police vehicles.
  62. [*][b]Vehicle_IsTank(vehicleid)[/b] -
  63. [*][b]Vehicle_IsFlowerpot(vehicleid)[/b] -
  64. [*][b]Vehicle_IsTransport(vehicleid)[/b] -
  65. [*][b]Vehicle_GetName(vehicleid)[/b] - Returns a 32 character packed string of the vehicle's name.
  66. [/list]
  67. Equivalents exist for [b]Model_[/b], taking a modelid instead of a vehicleid - replace the [b]Vehicle_[/b] prefix with [b]Model_[/b].
  68. [size=5][color=red][b][u][color=green]VIM Conversion[/color][/u][/b][/color][/size]
  69. Equivalents to all the above functions also exist for [b]VIM:[/b] tagged variables - replace the [b]Vehicle_[/b] prefix with [b]VIM_[/b].
  70. There are two ways to get a [b]VIM:[/b] variable - from a vehicleid or from a modelid:
  71. [pawn]
  72. new
  73. VIM:vimFromVehicleID = Vehicle_GetVIM(vehicleid),
  74. VIM:vimFromModelID = Model_ToVIM(vehicleid),
  75. [/pawn]
  76. Note the difference in "Get" and "To" - this is by design, as models are closer to VIMs than vehicleids are.
  77. So why use these instead? They just save a level of conversion:
  78. [pawn]#define Vehicle_IsPoliceCar(%0) (Vehicle_IsCar(%0) && Vehicle_IsPolice(%0))[/pawn]
  79. That is all fine, but internally calls several functions twice (including [b]GetVehicleModel[/b]). This is slightly better:
  80. [pawn]#define Model_IsPoliceCar(%0) (Model_IsCar(%0) && Model_IsPolice(%0))[/pawn]
  81. But that still has to do the "- 400" subtraction discussed earlier (and check that the result is valid).
  82. Using VIM functions you can do the conversion and bounds checks once, then use the result far more efficiently:
  83. [pawn]
  84. #define VIM_IsPoliceCar(%0) (VIM_IsCar(%0) && VIM_IsPolice(%0))
  85. new
  86. VIM:vim = Vehicle_GetVIM(vehicleid);
  87. if (VIM_IsPoliceCar(vim) || VIM_IsSWAT(vim))
  88. {
  89. // Whatever.
  90. }
  91. [/pawn]