PetrolMod

[tab]PetrolMod 2.0[/tab]

 [release]PetrolMod: A simple mod that gives you a limited amount of time before you vehicle runs out of fuel this varies from speed, This mod can be easily implanted into RP Gamemodes, The addon is fully configureable from consumption rates to max petrol. [/release]

[tab]Features: [/tab]

1.  Fully configureable
2.  Easy implation into gamemodes
3.  A horn! (right click sounds a horn that can be heard by nearby players)

    Todo:

[ol]
[li]Create a better methord to refill[/li][li]Add more features[/li][/ol]


PetrolMod = {}

PetrolMod.Vehicles = {
    "prop_vehicle_airboat",
    "prop_vehicle_jeep"
};

PetrolMod.MaxPetrol = 3000;

function PetrolMod.IsPetrolVehicle(ent)
    local class = ent:GetClass();
    for k, v in pairs(PetrolMod.Vehicles) do
        if(v == class) then
            return true;
        end
    end
    return false;
end

if (SERVER) then
    PetrolMod.NextCheck = 0;

    function PetrolMod.PlayerSpawnedVehicle(ply, vehicle)
        if(PetrolMod.IsPetrolVehicle(vehicle)) then
            vehicle:SetNWFloat("petrol", PetrolMod.MaxPetrol);
        end
    end
    hook.Add("PlayerSpawnedVehicle", "PetrolMod.PlayerSpawnedVehicle", PetrolMod.PlayerSpawnedVehicle);
    
    function PetrolMod.Handler()
        if (CurTime() <= PetrolMod.NextCheck) then
            return;
        end
        PetrolMod.NextCheck = CurTime() + 0.2;
        for k, v in pairs(player.GetAll()) do
            if (v:InVehicle()) then    
                local vehicle = v:GetVehicle();
                local petrol = vehicle:GetNWFloat("petrol");
                if(petrol == 0) then
                    vehicle:Fire("TurnOff");
                else
                    vehicle:Fire("TurnOn");
                    local newpetrol;
                    if(v:KeyDown(IN_FORWARD)) then
                        newpetrol = petrol - 1.4;
                    elseif(v:KeyDown(IN_BACK)) then
                        newpetrol = petrol - 0.7;
                    end
                    if(newpetrol != nil) then
                        if(newpetrol < 0) then
                            newpetrol = 0;
                        end
                        vehicle:SetNWFloat("petrol", newpetrol);
                    end
                end
            end
        end
    end
    hook.Add("Think", "PetrolMod.Handler", PetrolMod.Handler)
end

if(CLIENT) then
    function PetrolMod.HUDPaint()
        local ply = LocalPlayer();
        if(ply:InVehicle()) then
            local vehicle = ply:GetVehicle();
            if(PetrolMod.IsPetrolVehicle(vehicle)) then
                local petrol = math.ceil(100 / PetrolMod.MaxPetrol * vehicle:GetNWFloat("petrol"));
                draw.RoundedBox(3, 4, 4, 100, 25, Color(51, 58, 51, 255))
                draw.SimpleText(" Petrol: ".. petrol .. "%", "ScoreboardText", 8, 8, Color(86, 104, 86, 255), 0, 0)
            end
        end
    end
    hook.Add("HUDPaint", "PetrolMod.HUDPaint", PetrolMod.HUDPaint);
end


Code Works but as you can see i have done no refill yet.

Download link?

Still currently ironing a few bugs should have a beta out in about 5 - 10 minutes

Okay.

How do you stop them from moving the vehicle once the petrol has run out?

it automatically turns of and needs to be refiled vehicle:Fire( “TurnOff” )

Nice idea, very good for a first project. (This is your first project right?)

Woah, Your notepad++ text is big…

If its not in the release section then its not released yet.

I lol’d when I read the refill part.

So um… It’s been 5 hours. Can we have a link now please? :smiley:

But it says Home > Garry’s Mod > LUA Scripting > RELEASES > PetrolMod

“Well, we made the fuel tank in the car, but then when we ran out of gas, we realized we never cut a hole to pour more gas in there. So we’re gonna have to impale it and just dump it in or something.”

This looks like a really cool mod, hope you get finished with it.

I dont rember putting it in releases tbh at the moment im working consumption rates, and yes it is my project also my notepad++ text is big because im working on a 26 inch LCD and need big text :slight_smile:

You didn’t put it in releases, it was moved :smile:

http://img248.imageshack.us/img248/3668/movedd.png

Oh i see

[editline]12:32PM[/editline]

Just a little update im just having problems making each vehicle unique for example when the Fuel is set it seems to like to set it for all vehicles. Any Suggestions would be appreciated

[editline]12:42PM[/editline]

Fixed, Now im just working on the Think bcoz atm its lopping to fast

Version 1.0 Beta

Bugs:
None that i know off

Cons:
No Refill Method as of yet

Download:

http://www.garrysmod.org/img/?t=dll&id=87016

It’s good so far, sorry to be a bit nit-picky but:
[lua]function PetrolMod.IsPetrolVehicle(ent)
local class = ent:GetClass();
for k, v in pairs(PetrolMod.Vehicles) do
if(v == class) then
return true;
end
end
return false;
end[/lua]

should probably be:

[lua]function PetrolMod.IsPetrolVehicle(ent)
return table.HasValue(PetrolMod.Vehicles, ent:GetClass());
end[/lua]

[editline]02:57PM[/editline]

Also, you aren’t sending the Lua to the client. You need to AddCSLuaFile it serverside.

[editline]03:09PM[/editline]

The DrawRoundedBox function should have a first argument which is divisible by two or it doesn’t look right. You have it set to 3, which looks like this:

http://img245.imageshack.us/img245/4618/boxbad.png

You should set it to a number like 4 or 6 so it looks like this:

http://img99.imageshack.us/img99/1851/boxgood.png

Thanks man much appreciated

[editline]03:51PM[/editline]

Made the Changes now working on a refill Swep,

Ugh i hate SWEP Coding so this may take longer than expected.