Dzsozi (h03) Posted October 29, 2018 Posted October 29, 2018 I would like to have random spawnpoints inside a given area. I have the positions for minimum and maximum positions, but for some reason I can't manage to get the script working properly. I am using math.random from min to max values, but sometimes I get spawned outside the area, it doesn't return the right values. How can I get it working properly, so I spawn only inside the given area? spawnPositions = { ["Los Santos"] = {minPos = {1127.3712158203,-1632.8560791016}, maxPos = {1139.2634277344,-1621.2121582031}, z = 18.610622406006}, ["Las Venturas"] = {minPos = {1460.0589599609,2615.3452148438}, maxPos = {1478.0885009766,2622.806640625}, z = 10.8203125}, ["San Fierro"] = {minPos = {-2253.4848632813,521.00201416016}, maxPos = {-2250.8369140625,544.48956298828}, z = 35.174301147461}, ["Tierra Robada"] = {minPos = {-2259.4995117188,2352.9875488281}, maxPos = {-2253.8283691406,2372.5617675781}, z = 4.9140625}, ["Bone County"] = {minPos = {-244.1667175293,1210.9313964844}, maxPos = {-214.33074951172,1223.3796386719}, z = 19.735198974609}, ["Red County"] = {minPos = {243.48251342773,-160.65313720703}, maxPos = {252.89712524414,-151.75907897949}, z = 1.5703220367432}, ["Flint County"] = {minPos = {19.591306686401,-2641.8295898438}, maxPos = {37.629207611084,-2632.4833984375}, z = 40.432315826416}, ["Whetstone"] = {minPos = {-2122.837890625,-2481.69921875}, maxPos = {-2118.30859375,-2466.0988769531}, z = 30.625}, } function getRandomSpawnPoint(cityName) if spawnPositions[cityName] then local realTime = getRealTime() math.randomseed(realTime.second + realTime.minute + realTime.hour) local minX, minY = spawnPositions[cityName].minPos[1], spawnPositions[cityName].minPos[2] local maxX, maxY = spawnPositions[cityName].maxPos[1], spawnPositions[cityName].maxPos[2] local x, y = math.random(minX, maxX), math.random(minY, maxY) return x, y end end I guess it is because of the negative values, how to do the calculations for math.random?
Moderators IIYAMA Posted October 29, 2018 Moderators Posted October 29, 2018 (edited) -- Be a number #1 local value1 = 10 local value2 = 100 -- Be different! ;o local difference = value1 - value2 -- Be positive! :D local positiveDifference = math.abs(difference) -- Be ra ndo m! local randomPositiveDifference = math.random(positiveDifference) local randomDifference if difference < 0 then -- Be negative :( randomDifference = -randomPositiveDifference else -- Stay positive! :DDDD randomDifference = randomPositiveDifference end -- Be famous print(randomDifference) Not sure if there is an easier way, but this is a way to calculate it. Not understanding what the math.abs method/function does? Watch video: https://www.khanacademy.org/math/arithmetic/arith-review-negative-numbers/arith-review-abs-value/v/absolute-value-of-integers Edited October 29, 2018 by IIYAMA
Dzsozi (h03) Posted October 30, 2018 Author Posted October 30, 2018 I am not sure how I should use this with my code. I tried many ways of adding, substracting and calculating but it always happened that I got spawned outside the given area coordinates. This is my current script: spawnPositions = { ["Whetstone"] = {minPos = {-188.8860168457,267.83926391602}, maxPos = {-204.15943908691,251.66763305664}, z = 12.078125}, } function getRandomPositionInArea(minValue, maxValue) -- Be a number #1 local value1 = minValue local value2 = maxValue -- Be different! ;o local difference = (value1 - value2) -- Be positive! :D local positiveDifference = math.abs(difference) -- Be ra ndo m! local randomPositiveDifference = math.random(positiveDifference) local randomDifference if difference < 0 then -- Be negative :( randomDifference = -randomPositiveDifference else -- Stay positive! :DDDD randomDifference = randomPositiveDifference end -- Be famous print(value1+(randomDifference-value1)+value2) return value1+(randomDifference-value1)+value2 end function getRandomSpawnPoint(cityName) if spawnPositions[cityName] then local minX, minY = spawnPositions[cityName].minPos[1], spawnPositions[cityName].minPos[2] local maxX, maxY = spawnPositions[cityName].maxPos[1], spawnPositions[cityName].maxPos[2] local x, y = getRandomPositionInArea(minX, maxX), getRandomPositionInArea(minY, maxY) return x, y end end local cityName = "Whetstone" local x, y = getRandomSpawnPoint(cityName) setElementPosition(localPlayer, x, y, getGroundPosition(x, y, spawnPositions[cityName].z)+1)
Jayceon Posted October 30, 2018 Posted October 30, 2018 function getRandomSpawnPoint(cityName) if spawnPositions[cityName] then local realTime = getRealTime() math.randomseed(realTime.second + realTime.minute + realTime.hour) local minX = math.min(spawnPositions[cityName].minPos[1], spawnPositions[cityName].maxPos[1]) local minY = math.min(spawnPositions[cityName].minPos[2], spawnPositions[cityName].maxPos[2]) local maxX = math.max(spawnPositions[cityName].minPos[1], spawnPositions[cityName].maxPos[1]) local maxY = math.max(spawnPositions[cityName].minPos[2], spawnPositions[cityName].maxPos[2]) return math.random(minX, maxX), math.random(minY, maxY) end end
Moderators IIYAMA Posted October 30, 2018 Moderators Posted October 30, 2018 54 minutes ago, Dzsozi (h03) said: I am not sure how I should use this with my code. I tried many ways of adding, substracting and calculating but it always happened that I got spawned outside the given area coordinates. This is my current script: spawnPositions = { ["Whetstone"] = {minPos = {-188.8860168457,267.83926391602}, maxPos = {-204.15943908691,251.66763305664}, z = 12.078125}, } function getRandomPositionInArea(minValue, maxValue) -- Be a number #1 local value1 = minValue local value2 = maxValue -- Be different! ;o local difference = (value1 - value2) -- Be positive! :D local positiveDifference = math.abs(difference) -- Be ra ndo m! local randomPositiveDifference = math.random(positiveDifference) local randomDifference if difference < 0 then -- Be negative :( randomDifference = -randomPositiveDifference else -- Stay positive! :DDDD randomDifference = randomPositiveDifference end -- Be famous print(value1+(randomDifference-value1)+value2) return value1+(randomDifference-value1)+value2 end function getRandomSpawnPoint(cityName) if spawnPositions[cityName] then local minX, minY = spawnPositions[cityName].minPos[1], spawnPositions[cityName].minPos[2] local maxX, maxY = spawnPositions[cityName].maxPos[1], spawnPositions[cityName].maxPos[2] local x, y = getRandomPositionInArea(minX, maxX), getRandomPositionInArea(minY, maxY) return x, y end end local cityName = "Whetstone" local x, y = getRandomSpawnPoint(cityName) setElementPosition(localPlayer, x, y, getGroundPosition(x, y, spawnPositions[cityName].z)+1) function getRandomValueBetween (value1, value2) -- Be different! ;o local difference = value1 - value2 -- Be positive! :D local positiveDifference = math.abs(difference) -- Be ra ndo m! local randomPositiveDifference = math.random(positiveDifference) local randomDifference if difference < 0 then -- Be negative :( randomDifference = -randomPositiveDifference else -- Stay positive! :DDDD randomDifference = randomPositiveDifference end return value1 - randomDifference end -- Be famous print(getRandomValueBetween(-10, 10)) 1
Moderators IIYAMA Posted October 30, 2018 Moderators Posted October 30, 2018 4 hours ago, Dzsozi (h03) said: Thank you so much, it works! The only thing you should be aware of is that you can't give it the same values. if value1 == value2 then return value1 end Something like this would solve that. (Place it directly after line 1) 1
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now