|
| 1 | +--[[ |
| 2 | +This file is part of Courseplay (https://github.com/Courseplay/Courseplay_FS25) |
| 3 | +Copyright (C) 2024 Courseplay Dev Team |
| 4 | +
|
| 5 | +This program is free software: you can redistribute it and/or modify |
| 6 | +it under the terms of the GNU General Public License as published by |
| 7 | +the Free Software Foundation, either version 3 of the License, or |
| 8 | +(at your option) any later version. |
| 9 | +
|
| 10 | +This program is distributed in the hope that it will be useful, |
| 11 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +GNU General Public License for more details. |
| 14 | +
|
| 15 | +You should have received a copy of the GNU General Public License |
| 16 | +along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +]] |
| 18 | + |
| 19 | +------------------------------------------------------------------------------------------------------------------------ |
| 20 | +-- A collision detector used by the pathfinder |
| 21 | +--------------------------------------------------------------------------------------------------------------------------- |
| 22 | +---@class PathfinderCollisionDetector |
| 23 | +PathfinderCollisionDetector = CpObject() |
| 24 | + |
| 25 | +--- Nodes of a trigger for example, that will be ignored as collision. |
| 26 | +PathfinderCollisionDetector.NODES_TO_IGNORE = {} |
| 27 | + |
| 28 | +function PathfinderCollisionDetector:init(vehicle, vehiclesToIgnore, objectsToIgnore, ignoreFruitHeaps, collisionMask) |
| 29 | + self.logger = Logger('PathfinderCollisionDetector', Logger.level.debug, CpDebug.DBG_PATHFINDER) |
| 30 | + self.vehicle = vehicle |
| 31 | + self.vehiclesToIgnore = vehiclesToIgnore or {} |
| 32 | + self.objectsToIgnore = objectsToIgnore or {} |
| 33 | + self.ignoreFruitHeaps = ignoreFruitHeaps |
| 34 | + self.collidingShapes = 0 |
| 35 | + self.collisionMask = collisionMask or CpUtil.getDefaultCollisionFlags() |
| 36 | +end |
| 37 | + |
| 38 | +--- Adds a node which collision will be ignored global for every pathfinder. |
| 39 | +---@param node number |
| 40 | +function PathfinderCollisionDetector.addNodeToIgnore(node) |
| 41 | + PathfinderCollisionDetector.NODES_TO_IGNORE[node] = true |
| 42 | +end |
| 43 | + |
| 44 | +--- Removes a node, so it's collision is no longer applied. |
| 45 | +---@param node number |
| 46 | +function PathfinderCollisionDetector.removeNodeToIgnore(node) |
| 47 | + PathfinderCollisionDetector.NODES_TO_IGNORE[node] = nil |
| 48 | +end |
| 49 | + |
| 50 | +function PathfinderCollisionDetector:_overlapBoxCallback(transformId) |
| 51 | + if PathfinderCollisionDetector.NODES_TO_IGNORE[transformId] then |
| 52 | + --- Global node, that needs to be ignored |
| 53 | + return |
| 54 | + end |
| 55 | + |
| 56 | + local collidingObject = g_currentMission.nodeToObject[transformId] |
| 57 | + if collidingObject and PathfinderUtil.elementOf(self.objectsToIgnore, collidingObject) then |
| 58 | + -- an object we want to ignore |
| 59 | + return |
| 60 | + end |
| 61 | + local text, rootVehicle |
| 62 | + if collidingObject then |
| 63 | + if collidingObject.getRootVehicle then |
| 64 | + rootVehicle = collidingObject:getRootVehicle() |
| 65 | + elseif collidingObject:isa(Bale) and collidingObject.mountObject then |
| 66 | + rootVehicle = collidingObject.mountObject:getRootVehicle() |
| 67 | + end |
| 68 | + if rootVehicle == self.vehicle:getRootVehicle() or |
| 69 | + PathfinderUtil.elementOf(self.vehiclesToIgnore, rootVehicle) then |
| 70 | + -- just bumped into myself or a vehicle we want to ignore |
| 71 | + return |
| 72 | + end |
| 73 | + if collidingObject:isa(Bale) then |
| 74 | + text = string.format('bale %d', collidingObject.id) |
| 75 | + else |
| 76 | + text = CpUtil.getName(collidingObject) |
| 77 | + end |
| 78 | + end |
| 79 | + if getHasClassId(transformId, ClassIds.TERRAIN_TRANSFORM_GROUP) then |
| 80 | + |
| 81 | + local x, y, z = unpack(self.currentOverlapBoxPosition.pos) |
| 82 | + local dirX, dirZ = unpack(self.currentOverlapBoxPosition.direction) |
| 83 | + local size = self.currentOverlapBoxPosition.size |
| 84 | + --- Roughly checks the overlap box for any dropped fill type to the ground. |
| 85 | + --- TODO: DensityMapHeightUtil.getFillTypeAtArea() would be better. |
| 86 | + local fillType = DensityMapHeightUtil.getFillTypeAtLine(x, y, z, x + dirX * size, y, z + dirZ * size, size) |
| 87 | + if not self.ignoreFruitHeaps and fillType and fillType ~= FillType.UNKNOWN then |
| 88 | + text = string.format('terrain and fillType: %s.', |
| 89 | + g_fillTypeManager:getFillTypeByIndex(fillType).title) |
| 90 | + else |
| 91 | + --- Ignore terrain hits, if no fillType is dropped to the ground was detected. |
| 92 | + return |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + if text == nil then |
| 97 | + text = transformId .. ':' |
| 98 | + for key, classId in pairs(ClassIds) do |
| 99 | + if getHasClassId(transformId, classId) then |
| 100 | + text = text .. ' ' .. key |
| 101 | + end |
| 102 | + end |
| 103 | + end |
| 104 | + self.collidingShapesText = text |
| 105 | + self.collidingShapes = self.collidingShapes + 1 |
| 106 | +end |
| 107 | + |
| 108 | +function PathfinderCollisionDetector:findCollidingShapes(node, vehicleToLog, overlapBoxParams) |
| 109 | + local xRot, yRot, zRot = getWorldRotation(node) |
| 110 | + local x, y, z = localToWorld(node, overlapBoxParams.xOffset, 1, overlapBoxParams.zOffset) |
| 111 | + local dirX, dirZ = MathUtil.getDirectionFromYRotation(yRot) |
| 112 | + --- Save these for the overlap box callback. |
| 113 | + self.currentOverlapBoxPosition = { |
| 114 | + pos = { x, y, z }, |
| 115 | + direction = { dirX, dirZ }, |
| 116 | + size = math.max(overlapBoxParams.width, overlapBoxParams.length) |
| 117 | + } |
| 118 | + self.collidingShapes = 0 |
| 119 | + self.collidingShapesText = 'unknown' |
| 120 | + |
| 121 | + overlapBox(x, y + 0.2, z, xRot, yRot, zRot, overlapBoxParams.width, 1, overlapBoxParams.length, '_overlapBoxCallback', |
| 122 | + self, self.collisionMask, true, true, true, true) |
| 123 | + |
| 124 | + if true and self.collidingShapes > 0 then |
| 125 | + table.insert(PathfinderUtil.overlapBoxes, |
| 126 | + { x = x, y = y + 0.2, z = z, xRot = xRot, yRot = yRot, zRot = zRot, width = overlapBoxParams.width, |
| 127 | + length = overlapBoxParams.length }) |
| 128 | + self.logger:debug(self.vehicle,'my %s (%.1fx%.1f) is colliding with %s at x = %.1f, z = %.1f, yRot = %d', |
| 129 | + CpUtil.getName(vehicleToLog), 2 * overlapBoxParams.width, 2 * overlapBoxParams.length, self.collidingShapesText, x, z, math.deg(yRot)) |
| 130 | + end |
| 131 | + |
| 132 | + return self.collidingShapes |
| 133 | +end |
0 commit comments