Skip to content

Vehicle Data#66

Open
IanisCatalin15 wants to merge 2 commits intomainfrom
Vehicle
Open

Vehicle Data#66
IanisCatalin15 wants to merge 2 commits intomainfrom
Vehicle

Conversation

@IanisCatalin15
Copy link
Member

Vehicle Shop + Sell Car + State

Copy link
Collaborator

@Boss-Man-Dev Boss-Man-Dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cfg.lua

looks good, only thing to suggest is maybe more spawn locations for vehicles.

client.lua

  1. function Vehicle:spawnVehicle
  • added in local function for location checking. helps to prevent spawning vehicles in the same location
  • updated plateText to use state if present.
  • updated setVehicleState logic
function Vehicle:spawnVehicle(model, state, position, rotation)
  self:despawnVehicle(model)
	
	local function isLocationClear(x, y, z, radius)
    local veh = GetClosestVehicle(x, y, z, radius or 3.0, 0, 70)
    return not DoesEntityExist(veh)
  end

  -- Load vehicle model
  local mhash = GetHashKey(model)
  RequestModel(mhash)
  local i = 0
  while not HasModelLoaded(mhash) and i < 10000 do
    Citizen.Wait(10)
    i = i + 1
  end

  if HasModelLoaded(mhash) then
    local ped = GetPlayerPed(-1)
    local x, y, z

    if position and position.x and position.y and position.z then
      x, y, z = position.x, position.y, position.z
    else
      x, y, z = table.unpack(GetEntityCoords(ped))
    end
		
		-- Check if location is clear before spawning
    if not isLocationClear(x, y, z, 3.0) then
      print("[Vehicle] Spawn location is blocked.")  --debug
      return  -- stop spawning to avoid overlap
    end
		
    local nveh = CreateVehicle(mhash, x, y, z + 0.5, 0.0, true, false)
		
    -- Set rotation and heading if provided
    if rotation then
      SetEntityQuaternion(nveh, table.unpack(rotation))
    else
      SetEntityHeading(nveh, GetEntityHeading(ped))
    end
		
    -- Finalize vehicle setup
    SetVehicleOnGroundProperly(nveh)
    SetEntityInvincible(nveh, false)

    -- Put the player inside the vehicle if no position is provided
    if not position then
      SetPedIntoVehicle(ped, nveh, -1)
    end

    -- Set vehicle plate
    if not state.custom.plate_txt then
			SetVehicleNumberPlateText(nveh, "P "..vRP.EXT.Identity.registration)
		else
			SetVehicleNumberPlateText(nveh, state.custom.plate_txt)
		end

		-- Set vehicle ownership
    SetEntityAsMissionEntity(nveh, true, true)
    SetVehicleHasBeenOwnedByPlayer(nveh, true)
		
		-- set decorators
    DecorSetInt(nveh, "vRP.owner", vRP.EXT.Base.cid)
    self.vehicles[model] = nveh

    -- Set vehicle previous state or current 
		-- customizations on purchase should be empty
    if state and (not state.customization or next(state.customization) == nil) then
			self:setVehicleState(nveh, self:getVehicleCustomization(nveh))
		else
			self:setVehicleState(nveh, state)
		end

    -- Mark the model as no longer needed and trigger event
    SetModelAsNoLongerNeeded(mhash)
    vRP:triggerEvent("VehicleVehicleSpawn", model)
  end
end
  1. moved local custom = {} to be above function Vehicle:getVehicleCustomization(veh).
    not needed above that function
-- VEHICLE STATE
local custom = {}

function Vehicle:getVehicleCustomization(veh)

server.lua

  1. payment system not currently implemented in vrp3 just yet.
  2. altered local state. set them to either vstate.customization or {} if table and default false on locked
  3. didnt implement de-spawn function on sell.
local function m_buy(menu, model)
	local user = menu.user
	local uvehicles = user:getVehicles()
	local veh = menu.data.vehicles[model]
	if not veh then
		return vRP.EXT.Base.remote._notify(user.source, "Vehicle not found.")
	end

	-- Uncomment when implementing payment
	-- if not user:tryPayment(veh.price) then
	--   return vRP.EXT.Base.remote._notify(user.source, "Not enough money")
	-- end

	local shop_cfg = self.cfg.vehicleshops[menu.data.shop_key]
	if not shop_cfg or not shop_cfg.purchaseSpawn or #shop_cfg.purchaseSpawn == 0 then
		return vRP.EXT.Base.remote._notify(user.source, "Vehicle shop configuration error.")
	end

	local spawn = shop_cfg.purchaseSpawn[math.random(#shop_cfg.purchaseSpawn)]
	if not spawn or spawn.x == 0 then
		return vRP.EXT.Base.remote._notify(user.source, "No valid spawn point configured.")
	end

	vRP.EXT.Base.remote.teleport(user.source, spawn.x, spawn.y, spawn.z, spawn.w)

	-- Build and apply state
	local vstate = user:getVehicleState(model)
	local state = {
		customization = vstate.customization or {},
		condition = vstate.condition or {},
		locked = vstate.locked or false
	}

	uvehicles[model] = 0
	self.remote._spawnVehicle(user.source, model, state)
	self.remote._setOutVehicles(user.source, { [model] = {} })

	vRP.EXT.Base.remote._notify(user.source, "Purchased for $" .. veh.price)
	user:actualizeMenu()
	user:closeMenu(menu)
end
local function m_sell(menu, model)
    local user = menu.user
    local uvehicles = user:getVehicles()
    local veh = self:getVehicleByModel(model)

    if veh and uvehicles[model] then
      local zone_type = menu.data.zone_type
      local sell_cfg = self.cfg.sellvehicle[zone_type]
      
      if sell_cfg then
        local price = math.floor((veh.price or 0) * (sell_cfg.sellPrice / 100))
        uvehicles[model] = nil
				
        --user:addWallet(price)		-- no current implementation
				
				-- despawn vehicles
				self.remote._despawnVehicle(user.source, model)
				
        vRP.EXT.Base.remote._notify(user.source, "Vehicle sold for $" .. price)
        user:actualizeMenu()
      else
        vRP.EXT.Base.remote._notify(user.source, "Invalid sell zone.")
      end
    else
      vRP.EXT.Base.remote._notify(user.source, "You do not own this vehicle.")
    end
  end

@Sharky521
Copy link
Member

@IanisCatalin15
Have you made the changes requested?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants