Changeset View
Changeset View
Standalone View
Standalone View
source/simulation2/components/CCmpObstructionManager.cpp
/* Copyright (C) 2019 Wildfire Games. | /* Copyright (C) 2020 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
▲ Show 20 Lines • Show All 810 Lines • ▼ Show 20 Lines | |||||
* We don't use squares because the are likely to overflow. | * We don't use squares because the are likely to overflow. | ||||
* We use a 0.0001 margin to avoid rounding errors. | * We use a 0.0001 margin to avoid rounding errors. | ||||
*/ | */ | ||||
bool CCmpObstructionManager::IsInPointRange(entity_id_t ent, entity_pos_t px, entity_pos_t pz, entity_pos_t minRange, entity_pos_t maxRange, bool opposite) const | bool CCmpObstructionManager::IsInPointRange(entity_id_t ent, entity_pos_t px, entity_pos_t pz, entity_pos_t minRange, entity_pos_t maxRange, bool opposite) const | ||||
{ | { | ||||
fixed dist = DistanceToPoint(ent, px, pz); | fixed dist = DistanceToPoint(ent, px, pz); | ||||
// Treat -1 max range as infinite | // Treat -1 max range as infinite | ||||
return dist != fixed::FromInt(-1) && | return dist != fixed::FromInt(-1) && | ||||
(dist <= (maxRange + fixed::FromFloat(0.0001)) || maxRange < fixed::Zero()) && | (dist <= (maxRange + fixed::FromFloat(0.0001f)) || maxRange < fixed::Zero()) && | ||||
(opposite ? MaxDistanceToPoint(ent, px, pz) : dist) >= minRange - fixed::FromFloat(0.0001); | (opposite ? MaxDistanceToPoint(ent, px, pz) : dist) >= minRange - fixed::FromFloat(0.0001f); | ||||
} | } | ||||
bool CCmpObstructionManager::IsInTargetRange(entity_id_t ent, entity_id_t target, entity_pos_t minRange, entity_pos_t maxRange, bool opposite) const | bool CCmpObstructionManager::IsInTargetRange(entity_id_t ent, entity_id_t target, entity_pos_t minRange, entity_pos_t maxRange, bool opposite) const | ||||
{ | { | ||||
fixed dist = DistanceToTarget(ent, target); | fixed dist = DistanceToTarget(ent, target); | ||||
// Treat -1 max range as infinite | // Treat -1 max range as infinite | ||||
return dist != fixed::FromInt(-1) && | return dist != fixed::FromInt(-1) && | ||||
(dist <= (maxRange + fixed::FromFloat(0.0001)) || maxRange < fixed::Zero()) && | (dist <= (maxRange + fixed::FromFloat(0.0001f)) || maxRange < fixed::Zero()) && | ||||
(opposite ? MaxDistanceToTarget(ent, target) : dist) >= minRange - fixed::FromFloat(0.0001); | (opposite ? MaxDistanceToTarget(ent, target) : dist) >= minRange - fixed::FromFloat(0.0001f); | ||||
} | } | ||||
bool CCmpObstructionManager::IsPointInPointRange(entity_pos_t x, entity_pos_t z, entity_pos_t px, entity_pos_t pz, entity_pos_t minRange, entity_pos_t maxRange) const | bool CCmpObstructionManager::IsPointInPointRange(entity_pos_t x, entity_pos_t z, entity_pos_t px, entity_pos_t pz, entity_pos_t minRange, entity_pos_t maxRange) const | ||||
{ | { | ||||
entity_pos_t distance = (CFixedVector2D(x, z) - CFixedVector2D(px, pz)).Length(); | entity_pos_t distance = (CFixedVector2D(x, z) - CFixedVector2D(px, pz)).Length(); | ||||
// Treat -1 max range as infinite | // Treat -1 max range as infinite | ||||
return (distance <= (maxRange + fixed::FromFloat(0.0001)) || maxRange < fixed::Zero()) && | return (distance <= (maxRange + fixed::FromFloat(0.0001f)) || maxRange < fixed::Zero()) && | ||||
distance >= minRange - fixed::FromFloat(0.0001); | distance >= minRange - fixed::FromFloat(0.0001f); | ||||
} | } | ||||
bool CCmpObstructionManager::AreShapesInRange(const ObstructionSquare& source, const ObstructionSquare& target, entity_pos_t minRange, entity_pos_t maxRange, bool opposite) const | bool CCmpObstructionManager::AreShapesInRange(const ObstructionSquare& source, const ObstructionSquare& target, entity_pos_t minRange, entity_pos_t maxRange, bool opposite) const | ||||
{ | { | ||||
fixed dist = DistanceBetweenShapes(source, target); | fixed dist = DistanceBetweenShapes(source, target); | ||||
// Treat -1 max range as infinite | // Treat -1 max range as infinite | ||||
return dist != fixed::FromInt(-1) && | return dist != fixed::FromInt(-1) && | ||||
(dist <= (maxRange + fixed::FromFloat(0.0001)) || maxRange < fixed::Zero()) && | (dist <= (maxRange + fixed::FromFloat(0.0001f)) || maxRange < fixed::Zero()) && | ||||
(opposite ? MaxDistanceBetweenShapes(source, target) : dist) >= minRange - fixed::FromFloat(0.0001); | (opposite ? MaxDistanceBetweenShapes(source, target) : dist) >= minRange - fixed::FromFloat(0.0001f); | ||||
} | } | ||||
bool CCmpObstructionManager::TestLine(const IObstructionTestFilter& filter, entity_pos_t x0, entity_pos_t z0, entity_pos_t x1, entity_pos_t z1, entity_pos_t r, bool relaxClearanceForUnits) const | bool CCmpObstructionManager::TestLine(const IObstructionTestFilter& filter, entity_pos_t x0, entity_pos_t z0, entity_pos_t x1, entity_pos_t z1, entity_pos_t r, bool relaxClearanceForUnits) const | ||||
{ | { | ||||
PROFILE("TestLine"); | PROFILE("TestLine"); | ||||
// Check that both end points are within the world (which means the whole line must be) | // Check that both end points are within the world (which means the whole line must be) | ||||
if (!IsInWorld(x0, z0, r) || !IsInWorld(x1, z1, r)) | if (!IsInWorld(x0, z0, r) || !IsInWorld(x1, z1, r)) | ||||
▲ Show 20 Lines • Show All 486 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator