pawn Код:
if(vehicleid >= 184 && vehicleid <= MAX_VEHICLES)
and
pawn Код:
if((carid >= 16) && (carid <= 38))
is so-called "hardcoding". that means, if you have lots of CreateVehicle(); in OnGamemodeInit, the vehicleid will increase from 1 to 2000 (2000 is the MAX_VEHICLES define), so imagine this GameModeInit:
pawn Код:
CreateVehicle(596,blablabla);
CreateVehicle(415,blablabla);
CreateVehicle(560,blablabla);
assuming that these 3 vehicles gets created first, they WILL get assigned vehicleid 1 to 3. remark this crappy code, i will refer to it later...
this:
pawn Код:
if(vehicleid>=1 && vehicleid <= 3)
..will directly check for the first 3 vehicleid-s, regardless of their model (vehicleid1==modelid597==LSPD car, vehicleid2==modelid415==cheetah, vehicleid3==modelid560==sultan). as you see, the modelid can be anything, even a boat, or a kart, or the sultan and cheetah. the script will fail to detect entering a "cop" car, because its depending on the vehicleid (1-3), but their modelid is not checked. by adding some more modelids 596 (LSPD car), will force you to rescript all direct checkings for vehicleid, since by inserting something into the CreateVehicle(); order, will mess up the order of vehidleid-s. if you add more cop cars at the trails end, you need to re-script the code aswell.
this way of "hard"-scripting/coding/programming means more trouble than benefits, so:
AVOID IT AT ALL COSTS!
if you check for the modelid instead of directly checking the vehicleid (and assuming the correct vehicle got created by creating the correct modelid), you can avoid that id-issues, PLUS: you can add more cop cars (we used modelid 596) anywhere in the script, the vehicleid ist not relevant anymore:
pawn Код:
CreateVehicle(596,blablabla);
CreateVehicle(415,blablabla);
CreateVehicle(560,blablabla);
CreateVehicle(596,blablabla);
CreateVehicle(596,blablabla);
CreateVehicle(560,blablabla);
CreateVehicle(415,blablabla);
CreateVehicle(596,blablabla);
this is not ordered well, but it wont matter by checking the modelid:
pawn Код:
public IsACopCar(carid)
{
if(GetVehicleModel(vehicleid)==596 || GetVehicleModel(vehicleid)==597 || GetVehicleModel(vehicleid)==598)
{
return 1;
}
return 0;
}
already returns 1 at the cop cars 596,597 and 598, ignoring the order the vehicles got created. there is no loop for 2000 vehicles required, but the 3 checks (GetVehicleModel) already looks bad, if you want to add all cop vehicles, there would be needed like 20 checks.
an even better way is to waste (i prefer to call it "trade") some memory for speed. in order to let the CopCar check run in linear time (minimum time in this case), use an array, which already stores the return value:
pawn Код:
new IsCopVehicleModel[212];
this array is 212 big indeeddue to the fact that there are 212 vehicle ids. the simple trick to obtain the "correct" vehicle id, is to add 400 to the array. in reverse, the vehiclemodel 596 in game - 400 = 196. oh, 497,198are cop cars aswell. adding them into the array looks like
pawn Код:
IsCopVehicleModel[196]=1;
IsCopVehicleModel[197]=1;
IsCopVehicleModel[198]=1;
...added in GamemodeInit btw - the old cop car check function changed a bit,
pawn Код:
public IsACopCar(carid)
{
if(IsCopVehicleModel[carid]==1)
{
return 1;
}
return 0;
}
but can be done quicker, without ANY check:
pawn Код:
public IsACopCar(carid)
{
return IsCopVehicleModel[carid];
}
and this just looks just funny, a function returning the content of an array only. instead of
you can simply use
pawn Код:
IsCopVehicleModel[vehicleid]
with no need to call an extra callback. the time is linear, regardless of the amount of cars in the server, or the vehicleid.
oh, it can be modified - even in-game:
pawn Код:
CMD:copcar(playerid,params[])
{
new ModelID;
if(!sscanf(params,"d",ModelID))
{
ModelID=ModelID%212;
IsCopVehicleModel[ModelID-400]=1-IsCopVehicleModel[ModelID-400];
new string[128];
format(string,sizeof(string),"vehicle model %d is cop: %d",ModelID,IsCopVehicleModel[ModelID-400])
SendClientMessage(playerid,0xffffffff,string);
}
return 1;
}
use the /dl to see some vehicle infos, then switch a cop car to civilian with
/copcar 596
..then enter it, repeat last 2 steps as often you want ^^
edit: the "ModelID=ModelID%212;" avoids invalid array cells, feel free to try to set modelid 123456