Jump to content

Fuel help!!


Sande

Recommended Posts

Hey, i try to edit this fuel script. Can someone do a function like, if fuel is 0%. Its turn engine off alltime.

  
local carFuel = {};
local gasStations = {};
local gasStationsBlip = {};
local gasStationsMarkers = {};
addEventHandler("onResourceStart", getResourceRootElement(getThisResource()),
    function()
        local xml = xmlLoadFile("carData.xml");
        local xmlNodes = xmlNodeGetChildren(xml);
        for i,node in ipairs(xmlNodes) do
           carFuel[tonumber(xmlNodeGetAttribute(node,'id'))] = tonumber(xmlNodeGetAttribute(node,'fuel'));
        end
        xmlUnloadFile(xml);
        local xml = xmlLoadFile("garageData.xml");
        local xmlNodes = xmlNodeGetChildren(xml);
        for i,node in ipairs(xmlNodes) do
            local name = xmlNodeGetAttribute(node,'name');
            local x = tonumber(xmlNodeGetAttribute(node,'x'));
            local y = tonumber(xmlNodeGetAttribute(node,'y'));
            local z = tonumber(xmlNodeGetAttribute(node,'z'));
            gasStationsBlip[name] = createBlip(x,y,z,51,0,0,0,0,0,0,450);
            local moreKids = xmlNodeGetChildren(node);
            gasStationsMarkers[name] = {};
            for i,v in ipairs(moreKids) do
                local mx = tonumber(xmlNodeGetAttribute(v,'x'));
                local my = tonumber(xmlNodeGetAttribute(v,'y'));
                local mz = tonumber(xmlNodeGetAttribute(v,'z'));
                gasStationsMarkers[name][i] = createMarker(mx,my,mz,'corona',3,128,128,0,128);
                setElementData(gasStationsMarkers[name][i],'gasStation',true);
            end
        end
        xmlUnloadFile(xml);
    end
)
 
function fuelUse(p)
    local vehicle = getPedOccupiedVehicle(p);
    if vehicle then
    local team = getPlayerTeam (source)
        if getTeamName (team) == "Freeroam" then
        return end
        setTimer(fuelUse,1000,1,p);
        if getVehicleType(vehicle) ~= 'Automobile' and getVehicleType(vehicle) ~= 'Bike' and getVehicleType(vehicle) ~= 'Monster Truck' and getVehicleType(vehicle) ~= 'Quad' then return end
        if getVehicleController(vehicle) ~= p then return end --###
        if getVehicleEngineState(vehicle) == false then return end --###
       
        local fuel = getCarFuel(vehicle)
        if fuel == 0 then setVehicleEngineState(vehicle,false); return end --###
       
        --[[local speedX, speedY, speedZ = getElementVelocity(vehicle);
        vel = (speedX^2 + speedY^2 + speedZ^2)^(0.5)+(math.random(100,1000)/25000);]]
        local newX,newY,newZ = getElementPosition(vehicle);
        local oldX = getElementData(vehicle,'oldX') or newX;
        local oldY = getElementData(vehicle,'oldY') or newY;
        local oldZ = getElementData(vehicle,'oldZ') or newZ;
        local vel = (getDistanceBetweenPoints2D(oldX,oldY,newX,newY)/70)+(math.random(100,1000)/50000);
       
        local oldX = setElementData(vehicle,'oldX',newX);
        local oldY = setElementData(vehicle,'oldY',newY);
        local oldZ = setElementData(vehicle,'oldZ',newZ)
       
        --if vel < 0.01 then return end --###
       
        local remainingFuel = takeCarFuel(vehicle,vel);
        if remainingFuel < 0.001 then
            remainingFuel = 0;
            outputChatBox('Your car has ran out of fuel.',getVehicleController(v), 255, 0, 0);
            setVehicleEngineState(vehicle,false);
        end
    end
end
 
function startFuelUse(p,seat,jacked)
    local team = getPlayerTeam (p)
    if getTeamName (team) == "Freeroam" then
    return end
    if getVehicleType(source) ~= 'Automobile' and getVehicleType(source) ~= 'Bike' and getVehicleType(source) ~= 'Monster Truck' and getVehicleType(source) ~= 'Quad' then return end
    if seat ~= 0 then return end
    if getCarFuel(source) <= 0 then setVehicleEngineState(source,false); end
    setTimer(fuelUse,1000,1,p);
    ajastin()
end
addEventHandler("onVehicleEnter",getRootElement(),startFuelUse)
 
function ajastin()
    setTimer(tarkasta,500,0)
end
 
function tarkasta()
    if getCarFuel(source) <= 0 then setVehicleEngineState(source,false);
end
end
 
function switchEngine(playerSource)
    local theVehicle = getPedOccupiedVehicle(playerSource);
    if theVehicle and getVehicleController(theVehicle) == playerSource then
        local state = getVehicleEngineState(theVehicle)
        if state then
            setVehicleEngineState(theVehicle, false);
            outputChatBox('Car engine turned off.',playerSource);
        else
            if getCarFuel(theVehicle) <= 0 then return end
            setVehicleEngineState(theVehicle, true);
            outputChatBox('Car engine turned on.',playerSource);
        end
    end
end
addCommandHandler("engine",switchEngine);
 
addEvent('pedSyphonVehicle',true);
function pedSyphon(v)
    if getCarFuel(source) >= carFuel[-1] then outputChatBox("You are holding as much fuel as you can carry.",getVehicleController(v), 255, 0, 0); return end
    local n = math.random(100,200)/1000;
    local left = takeCarFuel(v,n);
    local total = addCarFuel(source,n);
    if left <= 0 then outputChatBox("That car has no more fuel left.",getVehicleController(v), 255, 0, 0); return end
    triggerClientEvent("onPedSyphonFuel",source);
end
addEventHandler('pedSyphonVehicle',getRootElement(),pedSyphon);
 
addEvent('pedRefuelVehicle',true);
function pedSyphon(v)
    if getCarFuel(source) <= 0 then outputChatBox("You have no more fuel left.",getVehicleController(v), 255, 0, 0); return end
    local maxFuel = carFuel[0];
    if carFuel[getElementModel(v)] then
        maxFuel = getFuel[getElementModel(v)];
    end
    if getCarFuel(v) >= maxFuel then
    outputChatBox("That car is fully refueled.",getVehicleController(v)); return
    end
    local n = math.random(100,200)/1000;
    local left = takeCarFuel(source,n);
    local total = addCarFuel(v,n);
    triggerClientEvent("onPedReFuel",source);
end
addEventHandler('pedRefuelVehicle',getRootElement(),pedSyphon);
 
addEvent('onVehicleRefuel',true);
function vehicleRefuel(v,m)
    if not getElementData(m,'gasStation') then return end
    if getElementType(v) ~= 'vehicle' then return end
    if getVehicleType(v) ~= 'Automobile' and getVehicleType(v) ~= 'Bike' and getVehicleType(v) ~= 'Monster Truck' and getVehicleType(v) ~= 'Quad' then return end
    local driver = getVehicleOccupants(v);
    if not driver[0] then return end
    if not isElementWithinMarker(v,m) then return end --###
    local maxFuel = carFuel[0];
    if getPlayerMoney(driver[0]) < 25 then outputChatBox("You can't afford any more fuel.",getVehicleController(v), 255, 0, 0); return end
    if carFuel[getElementModel(v)] then
        maxFuel = carFuel[getElementModel(v)];
    end
    if getCarFuel(v) >= maxFuel then return end
    addCarFuel(v,15);
    takePlayerMoney(driver[0],25);
    if getCarFuel(v) >= maxFuel then outputChatBox("Your car has been fully refueled.",getVehicleController(v), 255, 0, 0); return end
    setTimer(vehicleRefuel,500,1,v,m);
end
addEventHandler('onVehicleRefuel',getRootElement(),vehicleRefuel);
 
function hitTheMarker(e)
    setTimer(vehicleRefuel,1500,1,e,source);
end
addEventHandler('onMarkerHit',getRootElement(),hitTheMarker);
 
function getCarFuel(v)
    if getElementType(v) == 'vehicle' then
        if getVehicleType(v) == 'Automobile' or getVehicleType(v) == 'Bike' or getVehicleType(v) == 'Monster Truck' or getVehicleType(v) == 'Quad' then
            local fuel = getElementData(v,'fuel');
            local model = getElementModel(v);
            if not carFuel[model] then model = 0; end
            if not fuel then
                if carFuel[model] then
                    fuel = carFuel[model];
                    setElementData(v,'fuel',carFuel[model]);
                else
                    fuel = carFuel[0];
                    setElementData(v,'fuel',carFuel[0]);
                end
            end
            return fuel;
        end
    elseif getElementType(v) == 'player' then
        local fuel = getElementData(v,'fuel');
        if not fuel then
            setElementData(v,'fuel',0);
            return 0;
        end
        return fuel;
    end
end
 
function addCarFuel(v,a)
    if getElementType(v) == 'vehicle' then
        if getVehicleType(v) == 'Automobile' or getVehicleType(v) == 'Bike' or getVehicleType(v) == 'Monster Truck' or getVehicleType(v) == 'Quad' then
            local fuel = getElementData(v,'fuel');
            local model = getElementModel(v);
            if not carFuel[model] then model = 0; end
            if not fuel then
                if carFuel[model] then
                    fuel = carFuel[model];
                    setElementData(v,'fuel',carFuel[model]);
                else
                    fuel = carFuel[0];
                    setElementData(v,'fuel',carFuel[0]);
                end
            end
            fuel = fuel + a;
            if fuel > carFuel[model] then fuel = carFuel[model]; end
            setElementData(v,'fuel',fuel);
            return fuel;
        end
    elseif getElementType(v) == 'player' then
        local fuel = getElementData(v,'fuel');
        if not fuel then
            fuel = 0;
        end
        fuel = fuel + a;
        if fuel > carFuel[-1] then fuel = carFuel[-1]; end
        setElementData(v,'fuel',fuel);
        return fuel;
    end
end
 
function takeCarFuel(v,a)
    if getElementType(v) == 'vehicle' then
        if getVehicleType(v) == 'Automobile' or getVehicleType(v) == 'Bike' or getVehicleType(v) == 'Monster Truck' or getVehicleType(v) == 'Quad' then
        local team = getPlayerTeam (source)
        if getTeamName (team) == "Freeroam" then
        return end
            local fuel = getElementData(v,'fuel');
            local model = getElementModel(v);
            if not carFuel[model] then model = 0; end
                if not fuel then
                if carFuel[model] then
                    fuel = carFuel[model];
                    setElementData(v,'fuel',carFuel[model]);
                else
                    fuel = carFuel[0];
                    setElementData(v,'fuel',carFuel[0]);
                end
            end
            fuel = fuel - a;
            if fuel < 0 then fuel = 0; end
            setElementData(v,'fuel',fuel);
            return fuel;
        end
    elseif getElementType(v) == 'player' then
        local fuel = getElementData(v,'fuel');
        if not fuel then
            fuel = 0;
        end
        fuel = fuel - a;
       
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...