Database Vehicle System

Started by ysc3839, Jul 31, 2015, 04:06 PM

Previous topic - Next topic

ysc3839

I haven't tested it. Please test it and reply me! :D

function CVehicle::GetRadiansAngle()
{
local angle = ::asin(this.Rotation.z) * -2;
return this.Rotation.w < 0 ? 3.14159 - angle : 6.28319 - angle;
}

function CreateTable()
{
QuerySQL(VehDB, "CREATE TABLE IF NOT EXISTS Vehicles ( ID INTEGER, Model INTEGER, World INTEGER, Color1 INTEGER, Color2 INTEGER, PosX REAL, PosY REAL, PosZ REAL, Angle REAL )" );
}

function LoadVehicle()
{
local q = QuerySQL(VehDB, "SELECT * FROM Vehicles");
if (q)
{
local count = 1;
do
{
local model = GetSQLColumnData(q, 1);
local world = GetSQLColumnData(q, 2);
local col1 = GetSQLColumnData(q, 3);
local col2 = GetSQLColumnData(q, 4);
local x = GetSQLColumnData(q, 5);
local y = GetSQLColumnData(q, 6);
local z = GetSQLColumnData(q, 7);
local angle = GetSQLColumnData(q, 8);
local ID = CreateVehicle(model, world, x, y, z, angle, col1 , col2);
count++;
}
while (GetSQLNextRow(q));
FreeSQLQuery(q);

if (count > 0)
for (local i = 1; i <= count; i++)
QuerySQL(VehDB, "UPDATE Vehicles SET ID=" + i + " WHERE rowid=" + i);
}
}

function UpdateVehicle(veh, Pos, Angle, Color1, Color2, Model)
{
if (!Pos) Pos = veh.Pos;
if (!Angle) Angle = veh.GetRadiansAngle();
if (!Color1 || !Color2)
{
Color1 = veh.Colour1;
Color2 = veh.Colour2;
}
if (!Model) Model = veh.Model;
QuerySQL(VehDB, "UPDATE Vehicles SET Color1="+Color1+", Color2="+Color2+", PosX="+Pos.x+", PosY="+Pos.y+", PosZ="+Pos.z+", Angle="+Angle+", Model="+Model+" WHERE ID="+veh.ID);
}

function AddVehicle(Model, Colour1, Colour2, Pos, Angle)
{
local veh = CreateVehicle(model, player.Pos, Angle, Colour1, Colour1);
QuerySQL(VehDB, "INSERT INTO Vehicles ( ID, Model, World, Color1, Color2, PosX, PosY, PosZ, Angle ) VALUES ( "+veh.ID+", "+Model+", 1, "+Colour1+", "+Colour2+", "+Pos.x+", "+Pos.y+", "+Pos.z+", "+Angle+" )");
return veh;
}

function DeleteVehicle(ID)
{
QuerySQL(VehDB, "DELETE FROM Vehicles WHERE ID="+ID);
}

function SaveServerVehicles()
{
for (local i = 1; i <= GetVehicleCount(); i++)
{
local veh = FindVehicle(i);
if (veh)
AddVehicle(veh.Model, veh.Colour1, veh.Colour2, veh.Pos, veh.GetRadiansAngle());
}
}

BrYaN_FuRy

#1
Sorry Good Work

ysc3839


wilber32


KAKAN

WHat you mean?
Just fix the little bugs, and you're done.
Its working for me, but sometimes the SaveServerFunction doesn't saves to DB
oh no

wilber32

I only say that where is the command to create the vehicle

MacTavish


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

Retired VC:MP Player/Scripter :P

wilber32