From 1784d7e90adaf8dbd10f43b7f00e35924e435b95 Mon Sep 17 00:00:00 2001 From: fengchiw Date: Tue, 28 Nov 2023 20:32:42 -0500 Subject: [PATCH 1/2] Improve robustness of the line of sight function Previously, for the case where the `distance` between the `observer` and `target` is zero the function will crash with a Division by Zero Error. This error can happen if someone designs a chasing enemy that ends up at the players location. I added a check will return True indicating there is line of sight in such a case. Perhaps this should instead be a configurable option? Additionally, the Line of Sight function did not ensure that we have a positive non-zero value for the check resolution. I added a check that will raise a value error in such a case. Also added unit test for the same position for LOS --- arcade/paths.py | 6 ++++++ tests/unit/paths/test_line_of_sight.py | 3 +++ 2 files changed, 9 insertions(+) diff --git a/arcade/paths.py b/arcade/paths.py index 0ddd3d15e..9aff0fb00 100644 --- a/arcade/paths.py +++ b/arcade/paths.py @@ -348,8 +348,14 @@ def has_line_of_sight(observer: Point, """ if max_distance <= 0: raise ValueError("max_distance must be greater than zero") + if check_resolution <= 0: + raise ValueError("check_resolution must be greater than zero") + distance = get_distance(observer[0], observer[1], target[0], target[1]) + if distance == 0: + return False + steps = int(distance // check_resolution) for step in range(steps + 1): step_distance = step * check_resolution diff --git a/tests/unit/paths/test_line_of_sight.py b/tests/unit/paths/test_line_of_sight.py index cace5d6da..944b62f30 100644 --- a/tests/unit/paths/test_line_of_sight.py +++ b/tests/unit/paths/test_line_of_sight.py @@ -21,6 +21,9 @@ def test_line_of_sight(window): result = arcade.has_line_of_sight(player.position, enemy.position, wall_list, 20) assert not result + result = arcade.has_line_of_sight(enemy.position, enemy.position, wall_list) + assert result + wall = arcade.Sprite(":resources:images/tiles/grassCenter.png") wall.center_x = 0 wall.center_y = 0 From d7e39f3a500d3f5e05939033db0438421b5ca8e9 Mon Sep 17 00:00:00 2001 From: fengchiw Date: Wed, 29 Nov 2023 22:38:48 -0500 Subject: [PATCH 2/2] Fixed LOS Function\nWhen two positions are the same the LOS should return True --- arcade/paths.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arcade/paths.py b/arcade/paths.py index 9aff0fb00..84e06c411 100644 --- a/arcade/paths.py +++ b/arcade/paths.py @@ -354,7 +354,7 @@ def has_line_of_sight(observer: Point, distance = get_distance(observer[0], observer[1], target[0], target[1]) if distance == 0: - return False + return True steps = int(distance // check_resolution) for step in range(steps + 1):