Jump to content

[WIP] Vehicle system


Smart.

Recommended Posts

From what I can see there is no proper vehicle/car system so I've decided to work on one.

Features:

  • * Dealer shops, where you will be able to preview a car, select color, [thanks to
https://community.multitheftauto.com/ind ... anddescr=1] see prices etc..
* Management GUI, See basic information such as location, health and a few functions like lock, hide, blip
* Saved using SQL, Vehicles will be stored in a sql database so when you restart the server or the resource they'll be saved.
* Recover positions (if the vehicle is stuck or w/e)
* Vehicle will break down and become undriveable instead of exploding or being destroyed.
* Sell function (sell your vehicle for 5% less than what you bought it for, 10% if the vehicle is damaged)
* Easy to add shops and cars/vehicles (see screenshots)

If you have any ideas of features just post them here and I'll do my best to add them :)

  • Screenshots:

a6057f6090fefe55c5a9c3dea884e85c.png

1d04ab3e3d362fe8f61097407ba87b5a.png

592de3d6e52fa45c2376d26359db0e48.png

1901250c6c9775d6d0f6aa71795cf9d5.png

Ohh, I'm also planning on released it uncompiled because apparently there is already a car system but it's compiled

Edited by Guest
Link to comment

Can't be bothered to finish, if anyone wants to work on it themselves here is the current code:

server

  
marker = {} 
  
function createShops() 
    for index, dat in pairs(shops) do 
        outputChatBox(dat[1]) 
        marker[dat[4]] = createMarker(dat[1], dat[2], dat[3] - 1, "cylinder", 1.5, 255, 255, 255) 
    end 
end 
addEventHandler("onResourceStart", resourceRoot, createShops) 
  
function enterMarker(player, match) 
    if (not match) then return end 
    if (not isElement(player) or getElementType(player) ~= "player") then return end 
    if (isPedInVehicle(player)) then 
        outputChatBox("You can't enter the shop while being inside a vehicle", player, 200, 0, 0) 
        return 
    end 
     
    triggerClientEvent(player, "carsystem.enterMarker", player) 
end 
addEventHandler("onMarkerHit", resourceRoot, enterMarker) 
  
function buyCar(car, cost) 
    if (getPlayerMoney(client) <= tonumber(cost)) then 
        outputChatBox("You can't afford "..car..", you are missing $"..formatNumber(tonumber(cost) - getPlayerMoney(client)), client, 200, 200, 0) 
        return 
    end 
     
    local model = getVehicleModelFromName(car) 
     
    if (not model) then 
        outputChatBox("An error occured while attempting to purchase this vehicle", client, 255, 0, 0) 
        return 
    end 
     
    --takePlayerMoney(client, cost) 
    triggerClientEvent(client, "carsystem.boughtCar", client, car, cost) 
    --addCarToDB(client, car) 
end 
addEvent("carsystem.buyCar", true) 
addEventHandler("carsystem.buyCar", root, buyCar) 
  
function finalizeDeal(r, g, b, car, cost) 
    if (getPlayerMoney(client) <= tonumber(cost)) then 
        outputChatBox("You can't afford "..car..", you are missing $"..formatNumber(tonumber(cost) - getPlayerMoney(client)), client, 200, 200, 0) 
        return 
    end 
     
    local model = getVehicleModelFromName(car) 
     
    if (not model) then 
        outputChatBox("An error occured while attempting to purchase this vehicle", client, 255, 0, 0) 
        return 
    end 
     
    if (not mycars[client]) then mycars[client] = {} end 
     
    takePlayerMoney(client, cost) 
    outputChatBox(getPlayerName(client).." you have succesfully bought a "..car.." for $"..formatNumber(cost), client, 0, 255, 0) 
     
    closestShop(client) 
    local x, y, z, rotation = closest[client][5], closest[client][6], closest[client][7], closest[client][8] 
    local veh = createVehicle(model, x, y, z, 0, 0, rotation) 
    table.insert(mycars[client], veh) 
    warpPedIntoVehicle(client, veh) 
    setVehicleColor(veh, r, g, b) 
end 
addEvent("carsystem.finalizeDeal", true) 
addEventHandler("carsystem.finalizeDeal", root, finalizeDeal) 

client

  
local carsGui = {} 
  
function formatNumber(n) 
    if (not n) then return "Error catching data" end 
    local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$') 
    if (not num) then return end 
    return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right 
end 
  
function createDealerWindow() 
    buyDealerButton = guiCreateButton(929, 536, 146, 44, "PURCHASE", false) 
    guiSetFont(buyDealerButton, "clear-normal") 
    closeDealerButton = guiCreateButton(1121, 534, 146, 44, "CLOSE", false) 
    guiSetFont(closeDealerButton, "clear-normal") 
  
    dealerGridList = guiCreateGridList(927, 58, 350, 466, false) 
    guiGridListAddColumn(dealerGridList, "Vehicle", 0.5) 
    guiGridListAddColumn(dealerGridList, "Price", 0.5) 
  
    for index, dat in pairs(cars) do 
        local row = guiGridListAddRow(dealerGridList) 
        carsGui[dat[1]] = dat[2] 
        guiGridListSetItemText(dealerGridList, row, 1, tostring(dat[1]), false, false) 
        guiGridListSetItemText(dealerGridList, row, 2, "$" ..tostring(formatNumber(dat[2])), false, false) 
    end 
     
    addEventHandler("onClientGUIClick", dealerGridList, clickedOnGrid, false) 
    addEventHandler("onClientGUIClick", buyDealerButton, buyVehicleFromDealer, false) 
    addEventHandler("onClientGUIClick", closeDealerButton, closeDealerFunc, false) 
    dealerGuiStuff = {buyDealerButton, dealerGridList, closeDealerButton} 
     
    for ind, elem in pairs(dealerGuiStuff) do 
        guiSetVisible(elem, false) 
    end 
end 
addEventHandler("onClientResourceStart", resourceRoot, createDealerWindow) 
  
function createDealerDX() 
    dxDrawRectangle(920, 0, 360, 595, tocolor(0, 0, 0, 200), false) 
    dxDrawLine(920, 35, 1277, 35, tocolor(255, 255, 255, 255), 1, true) 
    dxDrawText("VEHICLE SHOP", 919, 0, 1277, 35, tocolor(255, 255, 255, 200), 2.00, "default-bold", "center", "center", false, false, true, false, false) 
end 
  
function enterMarker(mTable) 
    currentShop = mTable 
    showCursor(true) 
    addEventHandler("onClientRender", root, createDealerDX) 
    for ind, elem in pairs(dealerGuiStuff) do 
        guiSetVisible(elem, true) 
    end 
end 
addEvent("carsystem.enterMarker", true) 
addEventHandler("carsystem.enterMarker", root, enterMarker) 
  
function closeDealerFunc() 
    removeEventHandler("onClientRender", root, createDealerDX) 
    for ind, elem in pairs(dealerGuiStuff) do 
        guiSetVisible(elem, false) 
    end 
    setElementDimension(localPlayer, 0) 
    setCameraTarget(localPlayer) 
    showCursor(false) 
end 
  
function buyVehicleFromDealer() 
    local car = guiGridListGetItemText(dealerGridList, guiGridListGetSelectedItem(dealerGridList), 1) 
    if (not car or not carsGui[car]) then return end 
    triggerServerEvent("carsystem.buyCar", root, car, carsGui[car]) 
end 
  
function clickedOnGrid() 
    local car = guiGridListGetItemText(source, guiGridListGetSelectedItem(source), 1) 
    if (not car or not carsGui[car]) then return end 
    local cost = carsGui[car] 
    viewCar(car, cost) 
    outputChatBox(tostring(car).." - "..cost) 
end 
  
function viewCar(name, cost) 
    if (previewCar and isElement(previewCar)) then 
        destroyElement(previewCar) 
        previewCar = false 
    end 
    local id = getVehicleModelFromName(name) 
    local randomDim = math.random(1, 1000) 
    previewCar = createVehicle(id, 1417.6, -1478.2, 125.3) 
    setElementDimension(localPlayer, randomDim) 
    setElementDimension(previewCar, randomDim) 
    setElementFrozen(previewCar, true) 
    setCameraMatrix(1426.9, -1474.4, 126.3, 1426.0, -1474.8, 126.2) 
     
    if (not rotating) then 
        addEventHandler("onClientRender", root, rotateCar) 
    end  
end 
  
function rotateCar() 
    if (not previewCar) then removeEventHandler("onClientRender", root, rotateCar) rotating = false  return end 
    local _, _, z = getElementRotation(previewCar)  
    setElementRotation(previewCar, 0, 0, z + 0.5) 
    rotating = true 
end 
  
function changeCarColor(_, _, r, g, b) 
    if (not isElement(previewCar)) then return end 
    setVehicleColor(previewCar, r, g, b) 
end 
addEvent("onColorPickerChange", true) 
addEventHandler("onColorPickerChange", root, changeCarColor) 
  
function boughtCar(car, cost) 
    openPicker(localPlayer, "FFAA00", "Select car color") 
    acar = car 
    acost = cost 
end 
addEvent("carsystem.boughtCar", true) 
addEventHandler("carsystem.boughtCar", root, boughtCar) 
  
function finalizeDeal() 
    if (not isElement(previewCar)) then closeDealerFunc() return end 
    local r, g, b = getVehicleColor(previewCar) 
    triggerServerEvent("carsystem.finalizeDeal", root, r, g, b, acar, acost) 
    closeDealerFunc() 
end 
addEvent("onColorPickerOK", true) 
addEventHandler("onColorPickerOK", root, finalizeDeal) 

client & lua

closest = {} 
mycars = {} 
  
-- {"Vehicle Model Name", costInNumbers}, 
cars = { 
    {"Infernus", 500000}, 
    {"Bullet", 200000}, 
    {"NRG-500", 1000}, 
} 
  
shops = { 
    -- makerX, markerY, markerZ, shopName, spawnPointX, spawnPointY, spawnPointZ, rotationZ 
    {1121.6, -1460.0, 15.8, "Los Santos DealerShip", 1122.16772, -1408.44250, 13.41691, 270}, 
} 
  
function formatNumber(n) 
    if (not n) then return "Error catching data" end 
    local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$') 
    if (not num) then return end 
    return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right 
end 
  
function closestShop(plr) 
    if (isElement(plr)) then 
        local px, py, pz = getElementPosition(plr) 
        local shop = nil 
        local miniumDistance = 2000 
        for index, pos in pairs(shops) do 
            local x, y = pos[1], pos[2] 
            local distance = getDistanceBetweenPoints2D(px, py, x, y) 
            if (distance < miniumDistance) then 
                shop = pos 
                miniumDistance = distance 
            end 
        end 
        closest[plr] = shop 
        return closest[plr] 
    end 
end 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...