[Snippet]Vehicle Creating In Database

Started by MacTavish, Jul 30, 2015, 10:47 AM

Previous topic - Next topic

MacTavish

Vehicle Creating Script v0.1 By Beztone & MatheuS(aka Takanaue)

Just Put The Following Data In You Script in Correct Places

Actually this system is just like an map editor :)

To spawn a vehicle use /addcar
/addcar <ModelID> <COLOR1> <COLOR2>
To erase current vehicle press Delete key

To save current vehicle in db press P key after parking the vehicle to your desired position

Note: Vehicle wont be edited/erased once you save it in db. Vehicle ModelIDs will be found Here and colorIDs will be found Here

class PlayerStats
{
IdBeingEdit = null;
IsBeingEdited =false;
}
function onScriptLoad()
{

status <- array(GetMaxPlayers(), null);
del <- BindKey(true,0x2E,0,0);
p <- BindKey(true,0x50,0,0);
vdb <- ConnectSQL("vehicles.sqlite");
if (vdb) print("Vehicles Database loaded Successfully");
::QuerySQL( vdb, "CREATE TABLE IF NOT EXISTS Vehicles ( Model INT, World INT, PX FLOAT, PY FLOAT, PZ FLOAT, Angle FLOAT, col1 INT, col2 INT)" );
LoadCars();
}

function onPlayerJoin( player )
{
status[player.ID] = PlayerStats();
}

function onPlayerCommand( player, cmd, text )
{
 if ( cmd == "addcar" )
 {
 if (player.Name == "YOURNICK") //this would be just like as admin system
 {
 if ( !text ) MessagePlayer( ">> Use /" + cmd + " <Model> <Col1> <Col2>", player );
  else if ( status[player.ID].IsBeingEdited==true ) MessagePlayer( "Recent vehicle Not Saved Yet", player );
  else
  {
   local
    model = GetTok(text," ",1),
    color1 = GetTok(text," ",2),
    color2 = GetTok(text," ",3);
   if ( !model || !color1 || !color2 ) MessagePlayer( ">> Use /" + cmd + " <Model> <Col1> <Col2>", player );
   else
   {
   status[player.ID].IsBeingEdited=true;
    player.Vehicle = CreateVehicle( model.tointeger(), player.World, Vector( player.Pos.x.tofloat(), player.Pos.y.tofloat(), player.Pos.z.tofloat() ), player.Angle.tofloat(), color1.tointeger(), color2.tointeger() );
    Message( "[#FF0000]Vehicle Created, Park The Vehicle To You Pos and [#FFFFFF]Press P to Save It In Database" );
   status[player.ID].IdBeingEdit = player.Vehicle.ID;
   }
  }
 }
 }
return 1;
}

function LoadCars()
{
 local q = QuerySQL( vdb, "SELECT * FROM Vehicles" ), i = 0;
 while( GetSQLColumnData( q, 0 ) )
 {
  local
   Model = GetSQLColumnData( q, 0 ),
   World = GetSQLColumnData( q, 1 ),
   PX= GetSQLColumnData( q, 2 ),
   PY = GetSQLColumnData( q, 3 ),
   PZ = GetSQLColumnData( q, 4 ),
   Angle = GetSQLColumnData( q, 5 ),
   col1 = GetSQLColumnData( q, 6 ),
   col2 = GetSQLColumnData( q, 7 );
 
  CreateVehicle( Model.tointeger(), World.tointeger(), Vector( PX.tofloat(), PY.tofloat(), PZ.tofloat() ), Angle.tofloat(), col1, col2 );
  GetSQLNextRow( q );
  i++;
 }
 print( "Cars loaded - " + i );

}

function onPlayerEnterVehicle( player, vehicle, door )
{
Message("Vehicle ID: "+vehicle.ID+" Model ID: "+vehicle.Model)
}


function onKeyDown( player, key )
{
 if(key==del)
    {
      if(status[player.ID].IsBeingEdited==false) Message(player.Name+": [#FFFFFF]First turn on editing");
  else if(!player.Vehicle) Message(player.Name+": [#FFFFFF]Must Be In Vehicle To Remove");
  else if(!player.Vehicle.ID == status[player.ID].IdBeingEdit) Message(player.Name+": [#FFFFFF]This Vehicle Is Not In Editing Process");
   else
   {
   FindVehicle(player.Vehicle.ID).Delete();
   status[player.ID].IsBeingEdited=false;
   status[player.ID].IdBeingEdit = null;
    Message( "[#FF0000]Admin [#FFFFFF]" + player.Name + " [#FF0000]Erased Vehicle from [#FFFFFF]" + GetDistrictName( player.Pos.x, player.Pos.y ) + "." );
   }
    }
    else if(key==p)
    {
      if(status[player.ID].IsBeingEdited==false) Message(player.Name+": [#FFFFFF]First turn on editing");
      else if(!player.Vehicle) Message(player.Name+": [#FFFFFF]Must Be In Vehicle To Save Vehicle");
  else if(!player.Vehicle.ID == status[player.ID].IdBeingEdit) Message(player.Name+": [#FFFFFF]This Vehicle Is Not In Editing Process");
   else {
      QuerySQL( vdb, "INSERT INTO Vehicles ( Model, World, PX, PY, PZ, Angle, col1, col2 ) VALUES ( '" + player.Vehicle.Model + "', '" + player.Vehicle.World + "', '" + player.Vehicle.Pos.x + "', '" + player.Vehicle.Pos.y + "', '" + player.Vehicle.Pos.z + "', '" + player.Vehicle.EulerAngle.z + "', '" + player.Vehicle.Colour1 + "', '" + player.Vehicle.Colour2 + "')" );
    Message(player.Name+": [#FFFFFF]Vehicle Saved In Database");
status[player.ID].IsBeingEdited=false;
status[player.ID].IdBeingEdit = null;
}
    }
}

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();
}

The System Is Tested By me So If You Found Any BUG The Comment Bellow

Updated: classes was missed

Edited on 07/01/2016: forgot to mention Keys and command

Grand Hunting Project
Join #SLC, #KAKAN, #Doom, #GHP @LUnet

Retired VC:MP Player/Scripter :P

MatheuS

if( !sucess ) tryAgain();
Thanks to the VCMP community. It was the happiest period of my life.

KAKAN

oh no

DizzasTeR

#3
"DizzasTeR" This is my nick, everyone knows me on it, Really?!

Edit: Change it...

Joao^


KAKAN

Why don't u change the
function onKeyDown( player, key ) to case?
For ex
function onKeyDown( player, key )
{
  {
    switch(key)
{
case del:
<Edited snippet>
break;

case p:
<Edited snippet>
break;
}
}
}
oh no

MacTavish

Because i'm in love with if,elsif system

Grand Hunting Project
Join #SLC, #KAKAN, #Doom, #GHP @LUnet

Retired VC:MP Player/Scripter :P

KAKAN

lol, then also convert it, it'll help me in other way
Or else I'll post the function onkeydown with case
oh no

MacTavish

#8
It doesnt make lag and it will be easy for newbies to learn

Grand Hunting Project
Join #SLC, #KAKAN, #Doom, #GHP @LUnet

Retired VC:MP Player/Scripter :P

DizzasTeR

Just for your information @KAKAN, Using case everywhere is not necessary, specially for something like this since he is just checking two conditions.

Use case if you have to go through alot of condition checks.

MatheuS

each has its own way of developing.
if( !sucess ) tryAgain();
Thanks to the VCMP community. It was the happiest period of my life.

KAKAN

oh no

Spidey

gta vc and san are my favourite games

KAKAN

In your script.
If NO THEN GO KILL YOURSELF
oh no

Thijn

Quote from: KAKAN on Aug 25, 2015, 05:34 PMIn your script.
If NO THEN GO KILL YOURSELF
Please keep it nice...



Quote from: Spidey on Aug 25, 2015, 05:29 PMhey where do i add this

Please stop scripting if this is a genuine question.