Jump to content

[REL]Delivery system


Fresku

Recommended Posts

Hey there,

Here's the source code of my delivery system I made because I was bored.

You can call in any type of vehicle (Must be specified in the available vehicles list, or else it won't work)

Keep in mind that only 'Fast' delivery is working since it's not finished, and never will be I guess...

No errors as far as I know.

Have fun with it.

EDIT: Made Parachute a low LOD so it can be seen from a larger distance.

CLIENT

  
local planeTime = 20 
local normalTime = 50 
  
addEvent("client:startDelivery", true) 
addEventHandler("client:startDelivery", root, 
function(p, vehicleID, type) 
    if type == "Fast" then 
        createAirmailDelivery(vehicleID, p) --Create an awesome Andromada 
    elseif type == "Normal" then 
        createNormalDelivery(vehicleID, p) --Create a Helicopter lol not finished 
    end 
end) 
  
function createAirmailDelivery(vehicleID, p) 
    local x, y, z = getElementPosition(p) 
    local newZ = z+60 
    local plane = createObject(14553, x+1000, y, newZ, 20, 0, 0, true) 
    local px, py, pz = getElementPosition(plane) 
    setElementRotation(plane, 20, 0, findRotation(px, py, x, y)) 
    if p == localPlayer then blip = createBlipAttachedTo(plane, 0, 2, 255, 0, 0, 255, 0, 99999) setElementData(plane, "blip", blip) end 
    moveObject(plane, planeTime*1000, x-800, y, newZ) 
     
    setTimer(function(p)  
        if p == localPlayer then local blip = getElementData(plane, "blip") destroyElement(blip) end 
        if isElement(plane) then destroyElement(plane) end 
    end, planeTime*1000, 1, p) 
     
    setTimer(function() 
        local px, py, pz = getElementPosition(plane) 
        local rx, ry, rz = getElementRotation(plane) 
        triggerServerEvent("server:deliverVehicle", root, {vehicleID, px, py, pz, rx, ry, rz}) 
    end, planeTime*1000/2.1, 1) 
end 
  
function createNormalDelivery(vehicleID, p) 
     
end 
  
addEvent("client:deliverVehicle", true) 
addEventHandler("client:deliverVehicle", root, 
function(dumpy, dropTime) 
    local x, y, z = getElementPosition(dumpy) 
    local rx = math.random(10, 20) 
    local groundPos = getGroundPosition(x+rx, y, z+3) 
    moveObject(dumpy, dropTime*1000, x+rx, y, groundPos, 0, 0, math.random(180, 360)) 
end) 
  
function findRotation(x1,y1,x2,y2) 
  local t = math.deg(math.atan2(x2-x1,y2-y1)) 
  if t < 0 then t = t + 360 end; 
   
  return t; 
end 
  

SERVER

  
local deliveryTypes = { 
    ["Fast"]={extraCost=550}, 
    ["Normal"]={extraCost=65} 
} 
  
local vehicles = { 
    ["Infernus"]={cost=1, special=false, letter="an"}, 
    ["Sweeper"]={cost=1, special=false, letter="a"}, 
    ["Bullet"]={cost=1, special=false, letter="a"}, 
    ["Dumper"]={cost=1, special=true, letter="a"}, 
    ["Boxville"]={cost=1, special=false, letter="a"} 
} 
  
local dropTime = 20 
  
addCommandHandler("deliver", 
function(p, cmd, vehName, type) 
    if not vehName or not type or not tostring(vehName) or not tostring(type) or not isValidDeliveryType(type) then outputChatBox("SYNTAX: /"..cmd.."  (Delivery types: Fast ($"..comma(getVehicleDeliveryCost("Fast")).."), Normal ($"..comma(getVehicleDeliveryCost("Normal"))..") )", p, 255, 200, 0, false) return end 
     
    if isVehicleAvailable(vehName) then 
        local vehicleID, vehicleCost, letter, deliverCost = getVehicleModelFromName(vehName), getVehicleCost(vehName), getVehicleLetter(vehName), getVehicleDeliveryCost(type) 
        local enoughMoney, requiredMoney = hasPlayerMoney(p, tonumber(vehicleCost), deliverCost) 
        if enoughMoney then 
            outputChatBox("* You ordered "..letter.." "..vehName.." for $"..comma(vehicleCost+deliverCost).."!", p, 0, 230, 0, false) 
            outputChatBox("* Please wait while we deliver your "..vehName..".", p, 0, 230, 0, false) 
            takePlayerMoney(p, vehicleCost+deliverCost) 
            triggerClientEvent("client:startDelivery", root, p, vehicleID, type) 
        else 
            outputChatBox("* You require an additional $"..comma(requiredMoney).." to order "..letter.." "..vehName..".", p, 230, 0, 0, false) 
        end 
    else 
        outputChatBox("* We're sorry, we do not deliver that kind of vehicle.", p, 230, 0, 0, false) 
    end 
     
end) 
  
addEvent("server:deliverVehicle", true) 
addEventHandler("server:deliverVehicle", root, 
function(vehInfo) 
    local vehID, px, py, pz, rx, ry, rz = unpack(vehInfo) 
    local veh = createVehicle(vehID, px, py, pz, rx, ry, rz) 
    local parachute = createObject(2903, px, py, pz, 0, 0, 0, true) 
    local dumpy = createObject(1337, px, py, pz) 
    setObjectScale(parachute, 0.5) 
    setElementCollisionsEnabled(dumpy, false) 
    setElementCollisionsEnabled(parachute, false) 
    attachElements(veh, dumpy) 
    attachElements(parachute, dumpy, 0, 0, 3.3) 
    triggerClientEvent("client:deliverVehicle", root, dumpy, dropTime) 
    setTimer(function() 
        destroyElement(dumpy) 
        destroyElement(parachute) 
        setElementVelocity(veh, 0, 0, -0.05) 
    end, dropTime*1000-2000, 1) 
end) 
  
function hasPlayerMoney(p, cost, deliveryCost) 
    local cost = cost+deliveryCost 
     
    if (getPlayerMoney(p) >= cost) then 
        return true, 0 
    else 
        return false, cost - getPlayerMoney(p) 
    end 
end 
  
function getVehicleDeliveryCost(deliveryType) 
    if deliveryTypes[deliveryType] then 
        return deliveryTypes[deliveryType].extraCost 
    else 
        return false 
    end 
end 
  
function isValidDeliveryType(type) 
    if deliveryTypes[type] then 
        return true 
    else 
        return false 
    end 
end 
  
function isVehicleAvailable(vehName)  
    if vehicles[vehName] then 
        return true 
    else 
        return false 
    end 
end 
  
function getVehicleCost(vehName) 
    if vehicles[vehName] then 
        return vehicles[vehName].cost 
    else 
        return false 
    end 
end 
  
function getVehicleLetter(vehName) 
    if vehicles[vehName] then 
        return vehicles[vehName].letter 
    else 
        return false 
    end 
end 
  
function comma(amount) 
  local formatted = amount 
  while true do   
    formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2') 
    if (k==0) then 
      break 
    end 
  end 
  return formatted 
end 
  

Link to comment
  • 1 month later...

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...