SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Filterscripts

Reply
 
Thread Tools Display Modes
Old 12/09/2013, 10:29 AM   #1
Pottus
High-roller
 
Pottus's Avatar
 
Join Date: Jun 2012
Posts: 4,883
Reputation: 1321
Default Server Sided Vehicle Damage

Server Sided Vehicle Damage

Notes:
IsValidVehicle() is a real function but not defined in the SAMP includes you will need to add it or uncomment it from the FS.

Lets you shoot cars with no players in them, I accidentally found a way this would work when trying to do something else. It basically checks to see if your shot will intersect a sphere around a vehicle so the detection isn't perfect but actually works reasonably well I plan on releasing some more updates cheers.

There is only one callback to hook so this could very easily be implemented into a gamemode.

pawn Code:
// Creates server sided vehicle damage detection version 1.0
// By [uL]Pottus
//
// This uses a method of checking when a player shoots if a line representing an origin and destination intersects a sphere
// an intersection will result in the vehicle being damaged.
//
// Just compile and load

#include <a_samp>

// native IsValidVehicle(vehicleid);

forward OnPlayerShoot(playerid,weaponid,ammo);

new OldAmmo[MAX_PLAYERS], OldWeap[MAX_PLAYERS];
new CurrAmmo[MAX_PLAYERS], CurrWeap[MAX_PLAYERS];

//------------------------------------------------------------------------------

// Update the players weapon and possibly call OnPlayerShoot()
public OnPlayerUpdate(playerid)
{
    // Get the current weapon and ammo
    CurrWeap[playerid] = GetPlayerWeapon(playerid);
    CurrAmmo[playerid] = GetPlayerAmmo(playerid);

    // Player still has old weapon does this weapon now have less ammo?
    if(CurrWeap[playerid] == OldWeap[playerid] && CurrAmmo[playerid] < OldAmmo[playerid])
    {
        OnPlayerShoot(playerid, CurrWeap[playerid], CurrAmmo[playerid]);
    }
    OldWeap[playerid] = CurrWeap[playerid];
    OldAmmo[playerid] = CurrAmmo[playerid];
    return 1;
}

// Check if a vehicle is occupied
stock IsVehicleOccupied(vehicleid)
{
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(!IsPlayerConnected(i)) continue;

        if(IsPlayerInAnyVehicle(i))
        {
            if(GetPlayerVehicleID(i)==vehicleid)
            {
                return 1;
            }
        }
    }
    return 0;
}

public OnPlayerShoot(playerid, weaponid, ammo)
{
    for(new i = 0; i < MAX_VEHICLES; i++)
    {
        if(IsValidVehicle(i))
        {
            if(IsVehicleStreamedIn(i, playerid) && !IsVehicleOccupied(i))
            {
                new Float:vx, Float:vy, Float:vz, Float:radius, Float:dist, Float:amount;
                GetVehiclePos(i, vx, vy, vz);

                // This offsets the sphere (Add vehicle models later for better detection)
                vz -= 1.0;
                radius = 2.0;


                // Select damage amount and collision radius
                switch(weaponid)
                {
                    case 22: // 9MM
                    {
                        amount = 40.0;
                        dist = 30.0;
                    }
                    case 23: // Silencer
                    {
                        amount = 40.0;
                        dist = 50.0;
                    }
                    case 24: // Deagle
                    {
                        amount = 140.0;
                        dist = 50.0;
                    }
                    case 25: // Shotgun
                    {
                        amount = 80.0;
                        dist = 50.0;
                    }
                    case 26: // Sawn
                    {
                        amount = 80.0;
                        dist = 50.0;
                    }
                    case 27: // Combat
                    {
                        amount = 80.0;
                        dist = 50.0;
                    }
                    case 28: // Uzi
                    {
                        amount = 25.0;
                        dist = 20.0;
                    }
                    case 29: // MP5
                    {
                        amount = 25.0;
                        dist = 50.0;
                    }
                    case 30: // AK
                    {
                        amount = 30.0;
                        dist = 150.0;
                    }
                    case 31: // m4
                    {
                        amount = 30.0;
                        dist = 150.0;
                    }
                    case 32: // Tec
                    {
                        amount = 25.0;
                        dist = 30.0;
                    }
                    case 33: // Rifle
                    {
                        amount = 75.0;
                        dist = 200.0;
                    }
                    case 34: // Sniper
                    {
                        amount = 125.0;
                        dist = 300.0;
                    }
                    case 35: // RPG
                    {
                        amount = 751.0;
                        dist = 100.0;
                    }
                    case 36: // HEatseek
                    {
                        amount = 751.0;
                        dist = 100.0;
                    }
                    case 38: // Mini
                    {
                        amount = 50.0;
                        dist = 100.0;
                    }
                }

                if(BulletCollision(playerid, weaponid, vx, vy, vz, dist, radius))
                {
                    new Float:vhp;
                    GetVehicleHealth(i, vhp);
                    if(vhp >= 250.0)
                    {
                        vhp -= amount;
                        if(vhp < 250.0) SetTimerEx("ResetVehicle", 15000, false, "i", i);
                        SetVehicleHealth(i, vhp);
                    }
                    // SendClientMessage(playerid, -1, "Collision");
                }
                // else SendClientMessage(playerid, -1, "No Collision");
            }
        }
    }
    return 1;
}
// Respawn vehicle (They bug if you don't do this)
forward ResetVehicle(vid);
public ResetVehicle(vid)
{
    SetVehicleToRespawn(vid);
    return 1;
}

// Checks if a players bullet intersects a sphere
stock BulletCollision(playerid, weapon, Float:sx, Float:sy, Float:sz, Float:fScale = 30.0, Float:radius = 1.0)
{
    new
        Float:fP[3],
        Float:fV[3],
        Float:object[3],
        Float:sphere[3];

    sphere[0] = sx, sphere[1] = sy, sphere[2] = sz;

    GetPlayerCameraPos(playerid, fP[0], fP[1], fP[2]);
    GetPlayerCameraFrontVector(playerid, fV[0], fV[1], fV[2]);

    // Compensate (This is not perfect yet any ideas anyone?)
    if(weapon != 34 && weapon != 35 && weapon != 36)
    {
        new Float:FacingA;
        GetPlayerFacingAngle(playerid, FacingA);
        FacingA -= 90.0;
        if(FacingA < 0.0) FacingA += 360.0;
        else if(FacingA > 360.0) FacingA -= 360.0;

        fP[0] = (fP[0] + 0.6 * floatsin(-FacingA,degrees));
        fP[2] += 1.2;
    }

    object[0] = fP[0] + floatmul(fV[0], fScale);
    object[1] = fP[1] + floatmul(fV[1], fScale);
    object[2] = fP[2] + floatmul(fV[2], fScale);

    // Check if line intersects sphere
    if(RaySphere(fP, object, sphere, radius)) return 1;
    return 0;
}

// This checks to see if a line intersects a sphere
stock RaySphere(Float:p1[3],Float:p2[3],Float:sc[3],Float:r)
{
   new Float:a, Float:b, Float:c;
   new Float:bb4ac;
   new Float:dp[3];

   dp[0] = p2[0] - p1[0];
   dp[1] = p2[1] - p1[1];
   dp[2] = p2[2] - p1[2];
   a = dp[0] * dp[0] + dp[1] * dp[1] + dp[2] * dp[2];
   b = 2 * (dp[0] * (p1[0] - sc[0]) + dp[1] * (p1[1] - sc[1]) + dp[2] * (p1[2] - sc[2]));
   c = sc[0] * sc[0] + sc[1] * sc[1] + sc[2] * sc[2];
   c += p1[0] * p1[0] + p1[1] * p1[1] + p1[2] * p1[2];
   c -= 2 * (sc[0] * p1[0] + sc[1] * p1[1] + sc[2] * p1[2]);
   c -= r * r;
   bb4ac = b * b - 4 * a * c;
   if(bb4ac < 0) return 0;
   return 1;
}

Last edited by Pottus; 12/09/2013 at 11:06 AM.
Pottus is offline   Reply With Quote
Old 12/09/2013, 10:57 AM   #2
Dairyll
Huge Clucker
 
Dairyll's Avatar
 
Join Date: Feb 2009
Location: Manila, PH
Posts: 413
Reputation: 40
Default Re: Server Sided Vehicle Damage

I tried compiling it, but this happened:
Code:
E:\Rockstar Games\Server\samp03x_svr_win32\filterscripts\vehicledamage.pwn(57) : error 017: undefined symbol "IsValidVehicle"
Pawn compiler 3.2.3664	 	 	Copyright (c) 1997-2006, ITB CompuPhase


1 Error.
Interesting concept though.
__________________
MasayaHost - Affordable Web Hosting and VPS Hosting
$1/mo VPS Hosting | 70 GB Storage, 3 GB RAM, 3 TB Bandwidth
Chicago (USA), London (UK), Sydney (AU), Finland & Bulgaria
Deploy your Linux VPS and start your server today!
Dairyll is offline   Reply With Quote
Old 12/09/2013, 11:04 AM   #3
Pottus
High-roller
 
Pottus's Avatar
 
Join Date: Jun 2012
Posts: 4,883
Reputation: 1321
Default Re: Server Sided Vehicle Damage

Sorry I forgot to mention something, thanks for pointing that out.

http://wiki.sa-mp.com/wiki/IsValidVehicle
Pottus is offline   Reply With Quote
Old 12/09/2013, 11:07 AM   #4
Slice
High-roller
 
Join Date: Mar 2008
Posts: 1,831
Reputation: 1607
Default Re: Server Sided Vehicle Damage

Nice! I suppose you could use this to improve the precision.
Slice is offline   Reply With Quote
Old 12/09/2013, 11:12 AM   #5
Pottus
High-roller
 
Pottus's Avatar
 
Join Date: Jun 2012
Posts: 4,883
Reputation: 1321
Default Re: Server Sided Vehicle Damage

Quote:
Originally Posted by Slice View Post
Nice! I suppose you could use this to improve the precision.
Next update I was planning on doing that actually, first thing is i'd like to try and figure out how to calculate for GTA's aim offset right now it uses the camera which works great for sniper but there is a bit of loss of precision because the way I'm doing it right now is more of a bandaid solution.
Pottus is offline   Reply With Quote
Old 13/09/2013, 04:12 PM   #6
Memorex™
Little Clucker
 
Join Date: Nov 2011
Posts: 11
Reputation: 0
Default Re: Server Sided Vehicle Damage

Nice
__________________
Memorex™ is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[FilterScript] Vehicle Killer (Unoccupied vehicle damage) KoczkaHUN Filterscripts 28 27/10/2013 03:38 PM
[Include] OnEmptyVehicleDamage - Damage an empty vehicle (v0.1) [0.3c R3 Server] Nanory Includes 34 30/08/2013 05:49 AM
which server version for empty vehicle damage? timberleek General 2 27/08/2013 01:07 PM
How to detect vehicle damage, and get damage taken? Micius Scripting Help 4 31/07/2013 04:08 PM
[Help]Player damage when vehicle damage jujuv Help Archive 6 28/12/2010 12:25 AM


All times are GMT. The time now is 11:16 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.