GE Lua Documentation

Press F to search!

checkBoundary

Definition


-- @/lua/ge/extensions/gameplay/crawl/boundary.lua:286

local function checkBoundary(site, crawler, crawlStates)
  if not site then
    log('E', logTag, 'No site data available')
    return false
  end

  local state = crawlStates and crawlStates[crawler.id]
  if not state or not state.active then
    return false
  end

  local currentCorners = crawler.dynamicData.currentCorners
  if not currentCorners or #currentCorners == 0 then
    local vehPos = crawler.dynamicData.bbCenter
    if site:containsPoint2D(vehPos) then
      crawlerExitPoints[crawler.id] = nil
      crawlerDNFApplied[crawler.id] = nil
      return true
    else
      if not crawlerExitPoints[crawler.id] then
        crawlerExitPoints[crawler.id] = vec3(vehPos)
        log('D', logTag, string.format('Crawler %s exited boundary at %f, %f, %f', crawler.id, vehPos.x, vehPos.y, vehPos.z))
      else
        local exitPoint = crawlerExitPoints[crawler.id]
        local distanceFromExit = vehPos:distance(exitPoint)

        if distanceFromExit >= 10.0 then
          if not crawlerDNFApplied[crawler.id] then
            if gameplay_crawl_utils and gameplay_crawl_utils.applyPenalty then
              gameplay_crawl_utils.applyPenalty(crawler.id, 'dnf')
              crawlerDNFApplied[crawler.id] = true
              log('D', logTag, string.format('Applied DNF penalty for crawler %s - center point %f meters from exit point', crawler.id, distanceFromExit))
            end
          end
          return false
        end
      end

      if gameplay_crawl_utils.onBoundaryViolation then
        gameplay_crawl_utils.onBoundaryViolation(crawler.id)
      end
      return true
    end
  end

  -- Check all corners
  local outsideCorners = 0
  local totalCorners = #currentCorners
  local maxDistanceFromExit = 0

  for i, corner in ipairs(currentCorners) do
    if not site:containsPoint2D(corner) then
      outsideCorners = outsideCorners + 1

      if not crawlerExitPoints[crawler.id] then
        -- First time outside - save the exit point
        crawlerExitPoints[crawler.id] = vec3(corner)
        log('D', logTag, string.format('Crawler %s exited boundary at corner %f, %f, %f', crawler.id, corner.x, corner.y, corner.z))
      else
        -- Check distance from exit point
        local exitPoint = crawlerExitPoints[crawler.id]
        local distanceFromExit = corner:distance(exitPoint)
        maxDistanceFromExit = math.max(maxDistanceFromExit, distanceFromExit)
      end
    end
  end

  -- If any corners are inside, clear the exit point
  if outsideCorners < totalCorners then
    crawlerExitPoints[crawler.id] = nil
    crawlerDNFApplied[crawler.id] = nil
  end

  -- Check for DNF penalty based on distance from exit point
  -- Only apply DNF if ALL corners are outside AND distance is 10+ meters
  if outsideCorners >= totalCorners and maxDistanceFromExit >= 10.0 then
    -- Player is 10+ meters from exit point with all wheels outside - apply DNF penalty only once
    if not crawlerDNFApplied[crawler.id] then
      if gameplay_crawl_utils and gameplay_crawl_utils.applyPenalty then
        gameplay_crawl_utils.applyPenalty(crawler.id, 'dnf')
        crawlerDNFApplied[crawler.id] = true
        log('D', logTag, string.format('Applied DNF penalty for crawler %s - all %d wheels outside and %f meters from exit point', crawler.id, totalCorners, maxDistanceFromExit))
      end
    end
    return false
  end

  -- Apply boundary violation penalty if any corners are outside (but not disqualified)
  if outsideCorners > 0 then
    if gameplay_crawl_utils.onBoundaryViolation then
      gameplay_crawl_utils.onBoundaryViolation(crawler.id)
      log('D', logTag, string.format('Boundary violation penalty for crawler %s - %d/%d corners outside', crawler.id, outsideCorners, totalCorners))
    end
  end

  return true
end

Callers

@/lua/ge/extensions/gameplay/crawl/utils.lua
    if boundary then
      gameplay_crawl_boundary.checkBoundary(boundary, state.crawlerData, crawlStates)
    end