pawn Код:
native IsValidVehicle(vehicleid);
new missilet;
new vehhasrocket[MAX_VEHICLES]
,infmgun[MAX_VEHICLES]
,infmgunextra[MAX_VEHICLES]
,ClosestVehicle[MAX_VEHICLES]
;
forward missile(vehid);
public missile(vehid)
{
print("timer started");
new Float:ox,Float:oy,Float:oz;
RocketPosExtra(vehid,ox,oy,oz);
printf("rocket pos %f %f %f",ox,oy,oz);
printf("%f",GetVehicleDistanceFromPoint(ClosestVehicle[vehid],ox,oy,oz));
if(GetVehicleDistanceFromPoint(ClosestVehicle[vehid],ox,oy,oz) > 5)
{
UpdateMissile(infmgunextra[vehid],ClosestVehicle[vehid]);
print("missile updated");
}else{
DestroyObject(infmgunextra[vehid]);
print("object destroyed");
CreateExplosion(ox,oy,oz,7,10);
print("explosion created");
KillTimer(missilet);
}
return 1;
}
stock RocketPos(vehid,Float:x,Float:y,Float:z)
{
new model = GetVehicleModel(vehid);
switch(model)
{
case 411: return GetDynamicObjectPos(infmgun[vehid],x,y,z);
}
return 1;
}
stock RocketPosExtra(vehid,Float:x,Float:y,Float:z)
{
new model = GetVehicleModel(vehid);
switch(model)
{
case 411: return GetDynamicObjectPos(infmgunextra[vehid],x,y,z);
}
return 1;
}
forward VehicleHasDriver(vehicleid);
public VehicleHasDriver(vehicleid)
{
for(new i=0;i<=MAX_PLAYERS;i++)
{
if(IsPlayerInAnyVehicle(i))
{
if(GetPlayerVehicleID(i)==vehicleid)
{
if(GetPlayerState(i) == PLAYER_STATE_DRIVER)
{
return 1;
}
}
}
}
return 0;
}
GetClosestVehicleFromVehicle(vehicleid)
{
new
Float:X,
Float:Y,
Float:Z,
Float:Distance,
Float:DefaultDistance = 150,
vID = INVALID_VEHICLE_ID;
GetVehiclePos(vehicleid,X,Y,Z);
for(new carid = 1; carid != MAX_VEHICLES; carid++)
if(IsValidVehicle(carid))
{
if(carid != vehicleid)
{
if(VehicleHasDriver(carid))
{
Distance = GetVehicleDistanceFromPoint(carid, X, Y, Z);
if(Distance < DefaultDistance)
{
DefaultDistance = Distance;
vID = carid;
}
}
}
}
return vID;
}
stock Float:GetAngleFromPosToPos(Float:X, Float:Y, Float:PointX, Float:PointY)
{
new Float:Angle;
if(X > PointX && Y > PointY)
Angle = floatabs(atan2(floatsub(PointX, X), floatsub(PointY, Y)));
if(X > PointX && Y <= PointY)
Angle = floatadd(floatabs(atan2(floatsub(Y, PointY), floatsub(PointX, X))), 270.0);
if(X <= PointX && Y > PointY)
Angle = floatadd(floatabs(atan2(floatsub(PointY, Y), floatsub(X, PointX))), 90.0);
if(X <= PointX && Y <= PointY)
Angle = floatadd(floatabs(atan2(floatsub(X, PointX), floatsub(Y, PointY))), 180.0);
return Angle >= 360.0 ? floatsub(Angle, 360.0) : Angle;
}
//This function makes objectid point to vehicle and move towards it
stock UpdateMissile(objectid, targetvehicle)
{
new
Float: fFromX,
Float: fFromY,
Float: fFromZ,
Float: fToX,
Float: fToY,
Float: fToZ
;
//save pos of missile and target so we can do angle calculations
GetDynamicObjectPos(objectid, fFromX, fFromY, fFromZ);
GetVehiclePos(targetvehicle, fToX, fToY, fToZ);
//get angle to target
new Float: fTargetAngle = GetAngleFromPosToPos(fFromX, fFromY, fToX, fToY);
//This is where you need to get offsets
//these are offsets i made for the arrow object (works perfect with the white arrow object)
SetDynamicObjectRot(objectid, 0.0, 0, 180+fTargetAngle);
//use your own speeds for the missile
MoveDynamicObject(objectid, fToX, fToY, fToZ, 150.0);
}
public OnVehicleSpawn(vehicleid)
{
new model = GetVehicleModel( vehicleid );
switch(model)
{
case 411:
{
if( vehhasrocket[ vehicleid ] == 0 ) {
vehhasrocket[ vehicleid ] = 1;
infmgun[vehicleid] = CreateDynamicObject( 3790, 0, 0, 0, 0, 0, 0 );
AttachDynamicObjectToVehicle( infmgun[vehicleid], vehicleid, 02, 0, 0, 0.000000, 0.000000, 90); //Object Model: 2985 |
minigunnum ++;
}
}
}
return 1;
}
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
new vehid = GetPlayerVehicleID(playerid);
if(PRESSED(KEY_FIRE) && vehhasrocket[vehid] == 1)
{
new Float:x,Float:y,Float:z;
vehhasrocket[vehid] = 0;
RocketPos(vehid,x,y,z);
printf("rocket pos %f %f %f",x,y,z);
infmgunextra[vehid] = CreateDynamicObject(3790,x+2,y,z,0,0,180);
printf("created in %f %f %f",x+2,y,z);
DestroyDynamicObject(infmgun[vehid]);
ClosestVehicle[vehid] = GetClosestVehicleFromVehicle(vehid);
printf("Got closest vehicle : %i",ClosestVehicle[vehid]);
UpdateMissile(infmgunextra[vehid],ClosestVehicle[vehid]);
print("updated");
missilet = SetTimerEx("missile",250,1,"i",vehid);
print("timerSet");
}
return 1;
}