Jump to content

(solved)how to find random empty spot in the ocean?

Recommended Posts

hello, i need help cause i cant figure out how to do this my head goes blank when i see complicated stuff with numbers


basically, my charatcer supposed to be shipwrecked when they join world and i want them to spawn in the ocean on a boat. so i was wondering is there a function to find a random empty space in the ocean then i can spawn a boat on it and then teleport my character on the boat?


in simutil.lua i see stuff like


function FindValidPositionByFan(start_angle, radius, attempts, test_fn)
    attempts = attempts or 8

    local TWOPI = 2 * PI
    local attempt_angle = TWOPI / attempts
    local tmp_angles = {}
    for i = 0, attempts - 1 do
        local a = i * attempt_angle
        table.insert(tmp_angles, a > PI and a - TWOPI or a)

    -- Make the angles fan out from the original point
    local angles = {}
    local iend = math.floor(attempts / 2)
    for i = 1, iend do
        table.insert(angles, tmp_angles)
        table.insert(angles, tmp_angles[attempts - i + 1])
    if iend * 2 < attempts then
        table.insert(angles, tmp_angles[iend + 1])

    for i, v in ipairs(angles) do
        local check_angle = start_angle + v
        if check_angle > TWOPI then
            check_angle = check_angle - TWOPI
        local offset = Vector3(radius * math.cos(check_angle), 0, -radius * math.sin(check_angle))
        if test_fn(offset) then
            return offset, check_angle, i > 1 --deflected if not first try


function FindSafeSpawnLocation(x, y, z)
    local ent = x ~= nil and z ~= nil and FindClosestPlayer(x, y, z) or nil
    if ent ~= nil then
        return ent.Transform:GetWorldPosition()
    elseif TheWorld.components.playerspawner ~= nil then
        -- we still don't have an enity, find a spawnpoint. That must be in a safe location
        return TheWorld.components.playerspawner:GetAnySpawnPoint()
        -- if everything failed, return origin  
        return 0, 0, 0

function FindNearbyLand(position, range)
    local finaloffset = FindValidPositionByFan(math.random() * 2 * PI, range or 8, 8, function(offset)
        local x, z = position.x + offset.x, position.z + offset.z
        return TheWorld.Map:IsAboveGroundAtPoint(x, 0, z)
            and not TheWorld.Map:IsPointNearHole(Vector3(x, 0, z))
    if finaloffset ~= nil then
        finaloffset.x = finaloffset.x + position.x
        finaloffset.z = finaloffset.z + position.z
        return finaloffset

function FindWalkableOffset(position, start_angle, radius, attempts, check_los, ignore_walls, customcheckfn)
    return FindValidPositionByFan(start_angle, radius, attempts,
                local x = position.x + offset.x
                local y = position.y + offset.y
                local z = position.z + offset.z
                return TheWorld.Map:IsAboveGroundAtPoint(x, y, z)
                    and (not check_los or
                            position.x, position.y, position.z,
                            x, y, z,
                            { ignorewalls = ignore_walls ~= false, ignorecreep = true }))
                    and (customcheckfn == nil or customcheckfn(Vector3(x, y, z)))


probably its possible to use or edit some of these to work how i need it to but i cant wrap my head around how to even use them at all lol


if anybody can help out that would be great cause my head is fried after crashing my self into oblivion


edit: i forgot to say i want to find a deep ocean tile and not like ocean near the shore since they are supposed to be shipwrecked at the ocean lol

Edited by --- -.-
Link to comment
Share on other sites

maybe look instead at the worldgeneration code from stuff that spawns in the ocean? Then generate a setpiece there with a boat and a spawnpoint on it. And look at the wilderness-mode code, to see if it is possible to spawn your character there, instead of the portal (if not you can teleport him after game start to the boat)

it is still quite difficult, so I won't help you more. But you may tkae a look at some of my mods which add/change setpieces:


but you will also find some functions like "SpawnPrefabAtLandPlotNearInst" here, maybe you can rewrite them to your use:

Edited by Serpens
  • Thanks 1
Link to comment
Share on other sites

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

  • Create New...