From 823061c6993a1d01aa766eefdd26fdf7124803e9 Mon Sep 17 00:00:00 2001
From: MagicBot <MagicBot@magicbane.com>
Date: Wed, 11 Oct 2023 11:23:20 -0400
Subject: [PATCH] Improved clamping

---
 src/engine/InterestManagement/Terrain.java | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/src/engine/InterestManagement/Terrain.java b/src/engine/InterestManagement/Terrain.java
index 9509b07b..7ffbcf6b 100644
--- a/src/engine/InterestManagement/Terrain.java
+++ b/src/engine/InterestManagement/Terrain.java
@@ -121,13 +121,10 @@ public class Terrain {
 
         Vector2f terrain_cell = new Vector2f(terrainLoc.x / this.cell_size.x, terrainLoc.y / this.cell_size.y);
 
-        // Clamp values when standing directly on max pole
+        // Clamp values when standing directly on pole
 
-        if (terrain_cell.x >= this.cell_count.x)
-            terrain_cell.x = terrain_cell.x - 1;
-
-        if (terrain_cell.y >= this.cell_count.y)
-            terrain_cell.y = terrain_cell.y - 1;
+        terrain_cell.x = Math.max(0, Math.min(this.cell_count.x - 1, terrain_cell.x));
+        terrain_cell.y = Math.max(0, Math.min(this.cell_count.y - 1, terrain_cell.y));
 
         return terrain_cell;
     }