From 04e3b29c21031edb1c9c40d8e98515bc2dbcbd77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Mon, 25 May 2020 01:24:17 +0200 Subject: [PATCH] If there are is no food left, then make decision tree return water. --- src/AI/SurvivalDT.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/AI/SurvivalDT.py b/src/AI/SurvivalDT.py index 9d7ad70..2337dad 100644 --- a/src/AI/SurvivalDT.py +++ b/src/AI/SurvivalDT.py @@ -30,15 +30,6 @@ class SurvivalDT: playerStats = DTPlayerStats.dtStatsFromPlayerStats(player.statistics) - # Get foods sorted by distance from player - dtFoods: List[DTSurvivalInteractable] = [] - for food in foods: - dtFood = DTSurvivalInteractable.dtInteractableFromInteractable(food, player.x, player.y) - dtFoods.append(dtFood) - - dtFoods.sort(key=lambda x: x.accurateDistanceFromPlayer) - nearestDtFood = dtFoods[0] - # Get waters sorted by distance from player dtWaters: List[DTSurvivalInteractable] = [] for water in waters: @@ -47,6 +38,19 @@ class SurvivalDT: dtWaters.sort(key=lambda x: x.accurateDistanceFromPlayer) nearestDtWater = dtWaters[0] + # Get foods sorted by distance from player + dtFoods: List[DTSurvivalInteractable] = [] + for food in foods: + dtFood = DTSurvivalInteractable.dtInteractableFromInteractable(food, player.x, player.y) + dtFoods.append(dtFood) + + dtFoods.sort(key=lambda x: x.accurateDistanceFromPlayer) + # If there is no food on map return nearest water. + try: + nearestDtFood = dtFoods[0] + except IndexError: + return nearestDtWater.interactable + # Get rest places sorted by distance from player dtRestPlaces: List[DTSurvivalInteractable] = [] for rest in rests: