Repository: hsahovic/poke-env

Issue:
battle.fields
Hi Haris,

It seems to me like sometimes more than one terrain is returned by battle.fields
Could you please look into it


Discussion:
codecov[bot]: # [Codecov](https://codecov.io/gh/hsahovic/poke-env/pull/158?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Haris+Sahovic) Report
> Merging [#158](https://codecov.io/gh/hsahovic/poke-env/pull/158?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Haris+Sahovic) (ab7b839) into [master](https://codecov.io/gh/hsahovic/poke-env/commit/ac8182abbcaf27c776916987382121605933ae23?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Haris+Sahovic) (ac8182a) will **increase** coverage by `0.01%`.
> The diff coverage is `100.00%`.


```diff
@@            Coverage Diff             @@
##           master     #158      +/-   ##
==========================================
+ Coverage   87.73%   87.75%   +0.01%     
==========================================
  Files          34       34              
  Lines        3287     3292       +5     
==========================================
+ Hits         2884     2889       +5     
  Misses        403      403              
```


Patch:
diff --git a/src/poke_env/environment/abstract_battle.py b/src/poke_env/environment/abstract_battle.py
index eb70606..ed07d0d 100644
--- a/src/poke_env/environment/abstract_battle.py
+++ b/src/poke_env/environment/abstract_battle.py
@@ -241,6 +241,14 @@ class AbstractBattle(ABC):
 
     def _field_start(self, field):
         field = Field.from_showdown_message(field)
+
+        if field.is_terrain:
+            self._fields = {
+                field: turn
+                for field, turn in self._fields.items()
+                if not field.is_terrain
+            }
+
         self._fields[field] = self.turn
 
     def _parse_message(self, split_message: List[str]) -> None:
diff --git a/src/poke_env/environment/field.py b/src/poke_env/environment/field.py
index 8822656..de213ff 100644
--- a/src/poke_env/environment/field.py
+++ b/src/poke_env/environment/field.py
@@ -54,3 +54,8 @@ class Field(Enum):
                 message,
             )
             return Field._UNKNOWN
+
+    @property
+    def is_terrain(self) -> bool:
+        """Wheter this field is a terrain."""
+        return self.name.endswith("_TERRAIN")


Test patch:
diff --git a/unit_tests/environment/test_battle.py b/unit_tests/environment/test_battle.py
index 4fc832b..2dbb7bc 100644
--- a/unit_tests/environment/test_battle.py
+++ b/unit_tests/environment/test_battle.py
@@ -524,3 +524,23 @@ def test_rules_are_tracked():
     battle._parse_message(["", "rule", "this is a rule!"])
 
     assert battle._rules == ["hello", "hi", "this is a rule!"]
+
+
+def test_field_terrain_interactions():
+    logger = MagicMock()
+    battle = Battle("tag", "username", logger)
+
+    battle._field_start("electricterrain")
+    assert battle.fields == {Field.ELECTRIC_TERRAIN: 0}
+    battle.turn = battle.turn + 1
+
+    battle._field_start("mistyterrain")
+    assert battle.fields == {Field.MISTY_TERRAIN: 1}
+    battle.turn = battle.turn + 1
+
+    battle._field_start("gravity")
+    assert battle.fields == {Field.MISTY_TERRAIN: 1, Field.GRAVITY: 2}
+    battle.turn = battle.turn + 1
+
+    battle._field_start("psychicterrain")
+    assert battle.fields == {Field.GRAVITY: 2, Field.PSYCHIC_TERRAIN: 3}
diff --git a/unit_tests/environment/test_enumerations.py b/unit_tests/environment/test_enumerations.py
index 6d20556..5e700b4 100644
--- a/unit_tests/environment/test_enumerations.py
+++ b/unit_tests/environment/test_enumerations.py
@@ -26,6 +26,18 @@ def test_field_str():
     assert str(Field["ELECTRIC_TERRAIN"])
 
 
+def test_field_is_terrain():
+    terrains = {
+        Field.ELECTRIC_TERRAIN,
+        Field.MISTY_TERRAIN,
+        Field.PSYCHIC_TERRAIN,
+        Field.GRASSY_TERRAIN,
+    }
+
+    for field in Field:
+        assert field.is_terrain == (field in terrains)
+
+
 def test_field_build():
     assert Field["ELECTRIC_TERRAIN"] == Field.from_showdown_message("electric terrain")
     assert Field["ELECTRIC_TERRAIN"] == Field.from_showdown_message(