« [ GameMode ] ADM Script v1.0
Sonmez's Vehicle System
[ #D~ Tools ] Map Editor »

Sonmez

  • Jr. Member
  • Yo! It's time to play the fair game!
  • Posts: 53
Sonmez's Vehicle System
«  »Last edited
Basic vehicle system.
Code: [Select]
function onScriptLoad()
{
    VehicleDB <- ConnectSQL( "VehicleDB.db" );
    QuerySQL(VehicleDB, "create table if not exists Vehicles (List NUMERIC DEFAULT 0, ID NUMERIC DEFAULT 0,"
                                        +"Model NUMERIC DEFAULT 0, World NUMERIC DEFAULT 0,"
                                        +"PosX TEXT,PosY TEXT, PosZ TEXT,"
                                        +"Col1 TEXT, Col2 TEXT, Angle TEXT,"
                                        +"Locked BOOLEAN DEFAULT true,Owner TEXT,Price NUMERIC DEFAULT 0)");
    LoadVehicles();
}
function ReloadVehicles()
{
    for(local i = 0; i<500; i++){local q = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE List = '" + i + "'");
    if (q)
    {
        local veh = FindVehicle(GetSQLColumnData(q, 1).tointeger());
  if (veh){veh.Delete();}
    }
 }
 LoadVehicles()
}
function LoadVehicles()
{
  for(local i = 0; i<500; i++){local q = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE List = '" + i + "'");
    if (q)
    {
        local Model = GetSQLColumnData(q, 2).tointeger();
        local PosX = GetSQLColumnData(q, 4).tofloat();
        local PosY = GetSQLColumnData(q, 5).tofloat();
        local PosZ = GetSQLColumnData(q, 6).tofloat();
        local Col1 = GetSQLColumnData(q, 7).tointeger();
        local Col2 = GetSQLColumnData(q, 8).tointeger();
        local Angle = GetSQLColumnData(q, 9).tofloat();
        CreateVehicle( Model, 1, Vector(PosX,PosY,PosZ), Angle, Col1 ,Col2 )
        local newID = GetVehicleCount();
        QuerySQL( VehicleDB, "UPDATE Vehicles SET ID='"+newID+"' WHERE List LIKE '" + i + "'" );
        }
     }
}

function onPlayerEnteringVehicle( player, vehicle, door )
{
    local q = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE ID = '" + vehicle.ID + "'")
    if (q)
    {
        local Locked = GetSQLColumnData(q, 10).tostring();
        if (Locked == "true")
        {
      local Owner = GetSQLColumnData(q, 11).tostring();
            local Price = GetSQLColumnData(q, 12).tointeger();
      if(Owner != "null"){MessagePlayer("This car locked by "+Owner,player);}
   if(Owner == "null"){MessagePlayer("You haven't got keys. Use /buycar "+vehicle.ID,player);}
            return false;
        }
        else
        {
            return true;
        }
    }else{return true;}
}
function onPlayerEnterVehicle( player, vehicle, door )
{
    local q = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE ID = '" + vehicle.ID + "'")
    if (q)
    {
        local Owner = GetSQLColumnData(q, 11).tostring();
        local Price = GetSQLColumnData(q, 12).tointeger();
        if (Owner != "null"){MessagePlayer("This car is owned by "+Owner+" for "+Price+"$",player);}
        if (Owner == "null"){MessagePlayer("This car is for sale "+Price+"$",player);}
    }
}

function onPlayerCommand( player, cmd, text )
{
    if (cmd == "addcar")
    {
        if (!text){MessagePlayer("/addcar <world> <model> <col1> <col2> <price>",player); return false;}
        if (!GetTok( text, " ", 1 )||!GetTok( text, " ", 2 )||!GetTok( text, " ", 3 )||!GetTok( text, " ", 4 )||!GetTok( text, " ", 5 )||GetTok( text, " ", 6 )){MessagePlayer("/addcar <world> <model> <col1> <col2> <price>",player); return false;}
        if (!IsNum(GetTok( text, " ", 1 ))||!IsNum(GetTok( text, " ", 2 ))||!IsNum(GetTok( text, " ", 3 ))||!(IsNumGetTok( text, " ", 4 ))||!IsNum(GetTok( text, " ", 5 ))){MessagePlayer("/addcar <world> <model> <col1> <col2> <price>",player); return false;}

local lastlist = 0;
 
        local world = GetTok( text, " ", 1 ).tointeger();
        local model = GetTok( text, " ", 2 ).tointeger();
        local col1 = GetTok( text, " ", 3 ).tointeger();
        local col2 = GetTok( text, " ", 4 ).tointeger();
        local price = GetTok( text, " ", 5 ).tointeger();
        local veh = CreateVehicle( model, world, Vector(player.Pos.x,player.Pos.y,player.Pos.z), player.Angle, col1 ,col2 );
        local vehID= GetVehicleCount();
        for(local i = 0; i<500; i++){local q = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE List = '" + i + "'");
        if (q)
        {
            lastlist++;
        }
        }
        QuerySQL(VehicleDB, "INSERT INTO Vehicles ( List, ID, Model , World, PosX, PosY, PosZ, Col1, Col2, Angle, Locked,Owner,Price) VALUES ( '"
        + lastlist + "','"
        + vehID + "','"
        + model + "','"
        + world + "','"
        + player.Pos.x + "','"
        + player.Pos.y  + "','"
        + player.Pos.z  + "','"
        + col1 + "','"
        + col2 + "','"
        + player.Angle + "','"
        + true + "','"
        + "null" + "','"
        + price + "' )");
    }
    if (cmd == "removecar")
    {
        if (!text){MessagePlayer("/removecar <vehicleID>",player); return false;}
        if (!GetTok( text, " ", 1 )||!IsNum(GetTok( text, " ", 1 )) || GetTok( text, " ", 2 )){MessagePlayer("/removecar <vehicleID>",player); return false;}
        local vehicle = FindVehicle(GetTok( text, " ", 1 ).tointeger());
        if (!vehicle){MessagePlayer("This car isn't available try another vehicle", player); return false;}
       
 
  local lastlist = 0;
        local List = 0;
  local i = 0;
        for(i = 0; i<500; i++){local a = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE List = '" + i + "'");
        if (a)
        {
            lastlist++;
        }
        }
        local b = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE ID = '" + vehicle.ID + "'");
        if (b)
        {
            List = GetSQLColumnData(b, 0).tointeger();
            QuerySQL( VehicleDB, "DELETE FROM Vehicles WHERE ID='"+vehicle.ID+"'" );
            vehicle.Delete();
        }
        for(i = List+1; i<lastlist; i++){local c = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE List = '" + i + "'");
        if (c)
        {
            local newID = GetSQLColumnData(c, 0).tointeger()-1;
            QuerySQL( VehicleDB, "UPDATE Vehicles SET ID='"+newID+"' WHERE List LIKE '" + i + "'" );
        }
        }
  ReloadVehicles()
    }
 if (cmd == "changecol")
    {
        if (!text){MessagePlayer("/changecol <vehicle id> <col1 id> <col2 id>",player); return false;}
        if(!GetTok( text, " ", 1 )||!GetTok( text, " ", 2 )||!GetTok( text, " ", 3 )||GetTok(text, " ", 4)){MessagePlayer("/changecol <vehicle id> <col1 id> <col2 id>",player); return false;}
        if(!IsNum(GetTok( text, " ", 1 ))||!IsNum(GetTok( text, " ", 2 ))||!IsNum(GetTok( text, " ", 3 ))){MessagePlayer("/changecol <vehicle id> <col1 id> <col2 id>",player); return false;}
local vehicle = FindVehicle(GetTok( text, " ", 1 ).tointeger())
        local col1 = GetTok( text, " ", 2 ).tointeger()
        local col2 = GetTok( text, " ", 3 ).tointeger()
        if (vehicle)
        {
            local q = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE ID = '" + vehicle.ID + "'");
            if (q)
            {
                QuerySQL( VehicleDB, "UPDATE Vehicles SET Col1='"+col1+"', Col2 = '"+col2+"' WHERE ID LIKE '" + vehicle.ID + "'" );
                MessagePlayer("Vehicle ID:"+vehicle.ID+"'s color has been changed with col1:"+col1+", col2: "+col2+"", player);
                vehicle.Colour1 = col1
    vehicle.Colour2 = col2
   }
            else
            {
                MessagePlayer("This car isn't available try another vehicle", player);
                return false;
            }
        }
    }
 if (cmd == "changepos")
    {
        if (!text){MessagePlayer("/changepos <vehicle id>",player); return false;}
        if (!GetTok( text, " ", 1 )||!IsNum(GetTok(text, " ", 1))||GetTok( text, " ", 2 )){MessagePlayer("/changepos <vehicle id>",player); return false;}
        local vehicle = FindVehicle(GetTok( text, " ", 1 ).tointeger())
        local px = player.Pos.x.tostring();
        local py = player.Pos.y.tostring();
        local pz = player.Pos.z.tostring();
  local angle = player.Angle;
        if (!vehicle){return false;}
        local q = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE ID = '" + vehicle.ID + "'");
        if (q)
        {
      QuerySQL( VehicleDB, "UPDATE Vehicles SET PosX='"+px+"', PosY = '"+py+"', PosZ = '"+pz+"', Angle = '"+angle+"' WHERE ID LIKE '" + vehicle.ID + "'" );
            MessagePlayer("Vehicle ID:"+vehicle.ID+"'s position has been changed with position:"+px+", "+py+", "+pz+"", player);
        }
        else
        {
            MessagePlayer("This car isn't available try another vehicle", player);
            return false;
        }
  ReloadVehicles();
    }
    if (cmd == "buycar")
    {
        if (!text){MessagePlayer("/buycar <id>",player); return false;}
        if (!IsNum(text)){MessagePlayer("/buycar <id>",player); return false;}
        local vehicle = FindVehicle(text.tointeger())
        if (vehicle)
        {
            local q = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE ID = '" + vehicle.ID + "'");
            if (q)
            {
                local Model = GetSQLColumnData(q, 2).tointeger();
                local Owner = GetSQLColumnData(q, 11).tostring();
                local Price = GetSQLColumnData(q, 12).tointeger();
                if (Owner != "null"){MessagePlayer("This car is owned by "+Owner+" for "+Price+"$", player); return false;}
                if (player.Cash < Price){MessagePlayer("Your money isn't enough for buy this car", player); return false;}
                QuerySQL( VehicleDB, "UPDATE Vehicles SET Owner='"+player.Name+"' WHERE ID LIKE '" + vehicle.ID + "'" );
                player.Cash -= Price;
                MessagePlayer("You bought "+GetVehicleNameFromModel( Model )+" with "+vehicle.ID+" ID", player);
            }
            else
            {
                MessagePlayer("This car isn't available try another vehicle", player);
                return false;
            }
        }
    }
    if (cmd == "sellcar")
    {
        if (!text){MessagePlayer("/buycar <id>",player); return false;}
        if (!IsNum(text)){MessagePlayer("/buycar <id>",player); return false;}
        local vehicle = FindVehicle(text.tointeger())
        if (vehicle)
        {
            local q = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE ID = '" + vehicle.ID + "'");
            if (q)
            {
                local Owner = GetSQLColumnData(q, 11).tostring();
                local Price = GetSQLColumnData(q, 12).tointeger();
                if (Owner != "null" && Owner != player.Name){MessagePlayer("This car is owned by "+Owner+" for "+Price+"$", player); return false;}
                if (Owner == "null"){MessagePlayer("This car isn't yours", player); return false;}
                QuerySQL( VehicleDB, "UPDATE Vehicles SET Owner='"+"null"+"' WHERE ID LIKE '" + vehicle.ID + "'" );
                MessagePlayer("You sold your vehicle for "+Price*0.5+"$", player);
                player.Cash += Price*0.5;
            }
            else
            {
                MessagePlayer("This car isn't available try another vehicle", player);
                return false;
            }
        }
    }
    if (cmd == "getcar")
    {
        if (!text){MessagePlayer("/getcar <id>",player); return false;}
        if (!IsNum(text)){MessagePlayer("/getcar <id>",player); return false;}
        local vehicle = FindVehicle(text.tointeger())
        if (vehicle)
        {
            local q = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE ID = '" + vehicle.ID + "'");
            if (q)
            {
                local Owner = GetSQLColumnData(q, 11).tostring();
                local Price = GetSQLColumnData(q, 12).tointeger();
                if (Owner != "null" && Owner != player.Name||Owner == "null"){MessagePlayer("This car isn't yours", player); return false;}
                vehicle.Pos = Vector(player.Pos.x+2, player.Pos.y, player.Pos.z);
                MessagePlayer("You teleported your vehicle to yourself", player);
            }
            else
            {
                MessagePlayer("This car isn't available try another vehicle", player);
                return false;
            }
        }
    }
    if (cmd == "lockcar")
    {
        if (!text){MessagePlayer("/lockcar <id>",player); return false;}
        if (!IsNum(text)){MessagePlayer("/lockcar <id>",player); return false;}
        local vehicle = FindVehicle(text.tointeger())
        if (vehicle)
        {
            local q = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE ID = '" + vehicle.ID + "'");
            if (q)
            {
                local Owner = GetSQLColumnData(q, 11).tostring();
                local Price = GetSQLColumnData(q, 12).tointeger();
                if (Owner != "null" && Owner != player.Name||Owner == "null"){MessagePlayer("This car isn't yours", player); return false;}
                QuerySQL( VehicleDB, "UPDATE Vehicles SET Locked='"+"true"+"' WHERE ID LIKE '" + vehicle.ID + "'" );
                MessagePlayer("You locked your car", player);
            }
            else
            {
                MessagePlayer("This car isn't available try another vehicle", player);
                return false;
            }
        }
    }
    if (cmd == "unlockcar")
    {
        if (!text){MessagePlayer("/unlockcar <id>",player); return false;}
        if (!IsNum(text)){MessagePlayer("/unlockcar <id>",player); return false;}
        local vehicle = FindVehicle(text.tointeger())
        if (vehicle)
        {
            local q = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE ID = '" + vehicle.ID + "'");
            if (q)
            {
                local Owner = GetSQLColumnData(q, 11).tostring();
                local Price = GetSQLColumnData(q, 12).tointeger();
                if (Owner != "null" && Owner != player.Name||Owner == "null"){MessagePlayer("This car isn't yours", player); return false;}
                QuerySQL( VehicleDB, "UPDATE Vehicles SET Locked='"+"false"+"' WHERE ID LIKE '" + vehicle.ID + "'" );
                MessagePlayer("You unlocked your car", player);
            }
            else
            {
                MessagePlayer("This car isn't available try another vehicle", player);
                return false;
            }
        }
    }
    if (cmd == "mycars")
    {
        local cars = "";
        for(local i = 0; i<500; i++){local q = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE List = '" + i + "'");
        if (q)
        {
            local Owner = GetSQLColumnData(q, 11).tostring();
            local ID = GetSQLColumnData(q, 1).tointeger();
            local Model = GetSQLColumnData(q, 2).tointeger();
            if (Owner == player.Name)
            {
                 if (cars !="")
                {
                    cars = cars+", "+GetVehicleNameFromModel( Model )+"("+ID+")";
                }
                if (cars == "")
                {
                    cars = ""+GetVehicleNameFromModel( Model )+"("+ID+")";
                }
            }
            }
        }
    local t = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE Owner = '" + player.Name + "'");
    if (t)
    {
        MessagePlayer("Your cars: " +cars, player);
    }
    else
    {
        MessagePlayer("You haven't got any car", player);
        return false;
    }
    }
}

function GetTok(string, separator, n, ...)
{
    local m = vargv.len() > 0 ? vargv[0] : n,
    tokenized = split(string, separator),
    text = "";
    if (n > tokenized.len() || n < 1) return null;
    for (; n <= m; n++)
    {
        text += text == "" ? tokenized[n-1] : separator + tokenized[n-1];
    }
    return text;
}

function NumTok( string, separator )
{
    local tokenized = split(string, separator);
    return tokenized.len();
}

Good luck with scripting...
    Update Notes:
    • Bug fixes.
    • Added changecol feature
    • Added changepos feature
if (!perfect) createAgain();

[email protected]

  • Newbie
  • VC-TR Administrator
  • Posts: 24
"The sun the moon and the stars would have disappeared long ago... had they happened to be in the reach of of predatory human hands." Havelock Ellis

Inferno

  • Jr. Member
  • Posts: 71
Viva la VU
VFS Developer
VCCNR Administrator

[email protected]

  • Newbie
  • VC-TR Administrator
  • Posts: 24
Re: Sonmez's Vehicle System
« Reply #3,  »
In addition, you should add /removecar code for removing wrong cars.
"The sun the moon and the stars would have disappeared long ago... had they happened to be in the reach of of predatory human hands." Havelock Ellis

Sonmez

  • Jr. Member
  • Yo! It's time to play the fair game!
  • Posts: 53
if (!perfect) createAgain();

Inferno

  • Jr. Member
  • Posts: 71
Re: Sonmez's Vehicle System
« Reply #5,  »
I remember that /removecar in KrlOzz Vehicle System was bugged as it bugged vehicle db after restarting hence messing up car models ids and owners.

Did you test this one if it doesn't bug the db like that?
Viva la VU
VFS Developer
VCCNR Administrator

Sonmez

  • Jr. Member
  • Yo! It's time to play the fair game!
  • Posts: 53
Re: Sonmez's Vehicle System
« Reply #6,  »
Quote from Inferno on November 16th, 2020, 05:31 PM
I remember that /removecar in KrlOzz Vehicle System was bugged as it bugged vehicle db after restarting hence messing up car models ids and owners.

Did you test this one if it doesn't bug the db like that?
There is no such error in my system. I solved all problems...
if (!perfect) createAgain();

Inferno

  • Jr. Member
  • Posts: 71
Viva la VU
VFS Developer
VCCNR Administrator

[email protected]

  • Newbie
  • VC-TR Administrator
  • Posts: 24
Re: Sonmez's Vehicle System
« Reply #8,  »
/unlockcar has a bug, i can unlock cars even though I don't own,
"The sun the moon and the stars would have disappeared long ago... had they happened to be in the reach of of predatory human hands." Havelock Ellis

Sonmez

  • Jr. Member
  • Yo! It's time to play the fair game!
  • Posts: 53
Re: Sonmez's Vehicle System
« Reply #10,  »Last edited
v1.1 update released.
    Now system has:
    • /addcar
    • /removecar
    • /changecol
    • /changepos
    • /mycars
    • /buycar
    • /sellcar
    • /getcar
    • /lockcar
    • /unlockcar
if (!perfect) createAgain();

Inferno

  • Jr. Member
  • Posts: 71
Re: Sonmez's Vehicle System
« Reply #11,  »
You have written /removecar twice.


Suggestion : Add /lights <on/off> . It will be good.
Viva la VU
VFS Developer
VCCNR Administrator

[email protected]

  • Newbie
  • VC-TR Administrator
  • Posts: 24
"The sun the moon and the stars would have disappeared long ago... had they happened to be in the reach of of predatory human hands." Havelock Ellis

« [ GameMode ] ADM Script v1.0
[ #D~ Tools ] Map Editor »