diff --git a/resources/[tools]/nordi_taxi/client/main.lua b/resources/[tools]/nordi_taxi/client/main.lua index 0c5601bde..d5aa8722f 100644 --- a/resources/[tools]/nordi_taxi/client/main.lua +++ b/resources/[tools]/nordi_taxi/client/main.lua @@ -109,85 +109,46 @@ function CallTaxi() end function GetImprovedTaxiSpawnPosition(playerCoords) - print("^2[TAXI DEBUG]^7 Finding improved spawn position...") + print("^2[TAXI DEBUG]^7 Finding spawn position at least 100m away from player...") - -- Maximale Entfernung, die wir akzeptieren - local maxAcceptableDistance = 100.0 + local minDistance = 100.0 -- Mindestabstand in Metern + local maxDistance = 300.0 -- Maximaler Abstand in Metern local bestPosition = nil - local bestDistance = 999999.0 + local bestDistance = 999999.0 -- Für die nächste Position, die weit genug weg ist - -- Versuche zuerst einen Straßenknotenpunkt zu finden - local roadPosition = nil - local foundNode = false - local nodePos = vector3(0.0, 0.0, 0.0) - - -- Versuche einen Straßenknotenpunkt in optimaler Entfernung zu finden - foundNode, nodePos = GetClosestVehicleNode(playerCoords.x, playerCoords.y, playerCoords.z, 1, 3.0, 0) - - if foundNode then - local nodeDistance = #(playerCoords - nodePos) - if nodeDistance < maxAcceptableDistance then - roadPosition = nodePos - print("^2[TAXI DEBUG]^7 Found road node for spawn at distance: " .. nodeDistance) - return {x = roadPosition.x, y = roadPosition.y, z = roadPosition.z, w = 0.0} - else - -- Speichern für später, falls wir nichts Besseres finden - if nodeDistance < bestDistance then - bestDistance = nodeDistance - bestPosition = {x = nodePos.x, y = nodePos.y, z = nodePos.z, w = 0.0} - end - end - end - - -- Versuche einen weiteren Knotenpunkt mit größerem Radius - foundNode, nodePos = GetClosestMajorVehicleNode(playerCoords.x, playerCoords.y, playerCoords.z, 100.0, 0) - - if foundNode then - local nodeDistance = #(playerCoords - nodePos) - if nodeDistance < maxAcceptableDistance then - roadPosition = nodePos - print("^2[TAXI DEBUG]^7 Found major road node for spawn at distance: " .. nodeDistance) - return {x = roadPosition.x, y = roadPosition.y, z = roadPosition.z, w = 0.0} - else - -- Speichern für später, falls wir nichts Besseres finden - if nodeDistance < bestDistance then - bestDistance = nodeDistance - bestPosition = {x = nodePos.x, y = nodePos.y, z = nodePos.z, w = 0.0} - end - end - end - - -- Fallback auf Config-Positionen + -- PRIORITÄT 1: Config-Positionen prüfen if Config.MobileTaxiSpawns and #Config.MobileTaxiSpawns > 0 then + print("^2[TAXI DEBUG]^7 Checking config spawn positions first") + -- Alle Spawn-Positionen nach Entfernung sortieren local sortedSpawns = {} for i, spawnPos in ipairs(Config.MobileTaxiSpawns) do local distance = #(playerCoords - vector3(spawnPos.x, spawnPos.y, spawnPos.z)) - table.insert(sortedSpawns, { - coords = spawnPos, - distance = distance - }) + + -- Nur Positionen berücksichtigen, die mindestens minDistance entfernt sind + if distance >= minDistance then + table.insert(sortedSpawns, { + coords = spawnPos, + distance = distance + }) + + -- Beste Position merken (für Fallback) + if distance < bestDistance then + bestDistance = distance + bestPosition = spawnPos + end + end end - -- Nach Entfernung sortieren (nächste zuerst) + -- Nach Entfernung sortieren (nächste zuerst, aber nur die, die weit genug weg sind) table.sort(sortedSpawns, function(a, b) return a.distance < b.distance end) - -- Prüfen ob die nächsten Positionen frei und nah genug sind + -- Prüfen ob die Positionen frei sind for i, spawn in ipairs(sortedSpawns) do local spawnCoords = spawn.coords - -- Wenn Position zu weit weg ist, überspringen - if spawn.distance > maxAcceptableDistance then - -- Speichern für später, falls wir nichts Besseres finden - if spawn.distance < bestDistance then - bestDistance = spawn.distance - bestPosition = spawnCoords - end - goto continue - end - -- Prüfen ob Position frei ist local clearArea = true local vehicles = GetGamePool('CVehicle') @@ -206,26 +167,77 @@ function GetImprovedTaxiSpawnPosition(playerCoords) print("^2[TAXI DEBUG]^7 Using spawn position from config: " .. tostring(spawnCoords.x) .. ", " .. tostring(spawnCoords.y) .. ", " .. tostring(spawnCoords.z) .. " (Distance: " .. spawn.distance .. "m)") return spawnCoords end - - ::continue:: + end + + -- Wenn wir hier sind, waren alle Config-Positionen belegt + -- Aber wenn wir eine gute Position gefunden haben, die weit genug weg ist, verwenden wir sie trotzdem + if bestPosition then + print("^3[TAXI DEBUG]^7 All config positions occupied, using best one anyway: " .. tostring(bestPosition.x) .. ", " .. tostring(bestPosition.y) .. ", " .. tostring(bestPosition.z) .. " (Distance: " .. bestDistance .. "m)") + return bestPosition end end - -- Wenn wir hier sind, haben wir keine nahe Position gefunden - -- Wenn wir eine "beste" Position haben, die nur etwas zu weit weg ist, verwenden wir diese + -- PRIORITÄT 2: Straßenknotenpunkte suchen + print("^2[TAXI DEBUG]^7 No suitable config positions, trying road nodes") + + -- Beste Position zurücksetzen für Straßensuche + bestPosition = nil + bestDistance = 999999.0 + + -- Versuche mehrere Straßenknotenpunkte zu finden + for i = 1, 10 do + local foundNode, nodePos = GetNthClosestVehicleNode(playerCoords.x, playerCoords.y, playerCoords.z, i, 1, 3.0, 0) + + if foundNode then + local nodeDistance = #(playerCoords - nodePos) + + -- Wenn der Knotenpunkt im gewünschten Bereich liegt + if nodeDistance >= minDistance and nodeDistance <= maxDistance then + print("^2[TAXI DEBUG]^7 Found road node at good distance: " .. nodeDistance .. "m") + + -- Prüfe ob Position frei ist + local clearArea = true + local vehicles = GetGamePool('CVehicle') + + for _, vehicle in ipairs(vehicles) do + local vehCoords = GetEntityCoords(vehicle) + if #(nodePos - vehCoords) < 5.0 then + clearArea = false + break + end + end + + if clearArea then + return {x = nodePos.x, y = nodePos.y, z = nodePos.z, w = 0.0} + else + -- Speichern für später, falls wir nichts Besseres finden + if nodeDistance < bestDistance then + bestDistance = nodeDistance + bestPosition = {x = nodePos.x, y = nodePos.y, z = nodePos.z, w = 0.0} + end + end + elseif nodeDistance >= minDistance and nodeDistance < bestDistance then + -- Speichere den besten Knotenpunkt, auch wenn er nicht im idealen Bereich liegt + bestDistance = nodeDistance + bestPosition = {x = nodePos.x, y = nodePos.y, z = nodePos.z, w = 0.0} + end + end + end + + -- Wenn wir einen guten Knotenpunkt gefunden haben, verwende ihn if bestPosition then - print("^3[TAXI DEBUG]^7 No position within " .. maxAcceptableDistance .. "m found, using best available at " .. bestDistance .. "m") + print("^2[TAXI DEBUG]^7 Using best road node found at distance: " .. bestDistance .. "m") return bestPosition end - -- Wenn alles fehlschlägt: Generiere eine zufällige Position in der Nähe des Spielers - print("^3[TAXI DEBUG]^7 Generating random position within " .. maxAcceptableDistance .. "m of player") + -- PRIORITÄT 3: Zufällige Position generieren + print("^3[TAXI DEBUG]^7 Generating random position between " .. minDistance .. "m and " .. maxDistance .. "m from player") -- Versuche bis zu 10 Mal, eine gültige Position zu finden for attempt = 1, 10 do - -- Zufällige Position im Umkreis + -- Zufällige Position im gewünschten Abstandsbereich local angle = math.random() * 2 * math.pi - local distance = math.random(30, maxAcceptableDistance) + local distance = math.random(minDistance, maxDistance) local x = playerCoords.x + math.cos(angle) * distance local y = playerCoords.y + math.sin(angle) * distance local z = playerCoords.z @@ -237,15 +249,15 @@ function GetImprovedTaxiSpawnPosition(playerCoords) local isOnRoad = IsPointOnRoad(x, y, groundZ) if isOnRoad then - print("^2[TAXI DEBUG]^7 Found random position on road at distance: " .. distance) + print("^2[TAXI DEBUG]^7 Found random position on road at distance: " .. distance .. "m") return {x = x, y = y, z = groundZ, w = 0.0} end end end - -- Absolute Notfall-Fallback: Einfach irgendwo in der Nähe + -- Absolute Notfall-Fallback: Einfach irgendwo in der gewünschten Entfernung local angle = math.random() * 2 * math.pi - local distance = math.random(30, maxAcceptableDistance) + local distance = math.random(minDistance, maxDistance) local x = playerCoords.x + math.cos(angle) * distance local y = playerCoords.y + math.sin(angle) * distance local z = playerCoords.z @@ -256,11 +268,12 @@ function GetImprovedTaxiSpawnPosition(playerCoords) z = groundZ end - print("^3[TAXI DEBUG]^7 Using emergency random spawn position at distance: " .. distance) + print("^3[TAXI DEBUG]^7 Using emergency random spawn position at distance: " .. distance .. "m") return {x = x, y = y, z = z, w = 0.0} end + function SpawnTaxi(coords) print("^2[TAXI DEBUG]^7 Spawning taxi at: " .. tostring(coords.x) .. ", " .. tostring(coords.y) .. ", " .. tostring(coords.z))