/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package wge3.model.ai;

import static com.badlogic.gdx.math.MathUtils.random;
import static com.badlogic.gdx.math.MathUtils.randomBoolean;
import wge3.model.Tile;
import wge3.model.Creature;
import wge3.model.actors.Thief;

/**
 *
 * @author chang
 */

// The thief is a coward: he only acts when buddies are around. He closes in on his enemy FAST and takes an object he is carrying.
// He then runs away. If the player can catch the thief and kill him, he can recover his items. The thief will do anything he can to avoid capture.
public class ThiefAI extends AI {
    
    private int cowardLevel; // How many friendlies should be nearby for the thief to become active
    private boolean itemStolen;
    
    public ThiefAI(Thief creature) {
        super(creature);
        cowardLevel = 4;
        itemStolen = false;
    }
    
    public void setCowardLevel(int newLevel) {
        if (newLevel > 0) {
            cowardLevel = newLevel;
        }
    }
    
    @Override
    public void update() {
        if (!isStealing()) {
            checkForEnemies();
        }
        
        if (!currentTask.isFinished()) {
            currentTask.execute();
            return;
        }
        
        if (randomBoolean(2/3f)) {
            currentTask = new MoveTask(NPC, getNewMovementDestination(NPC));
        } else {
            currentTask = new WaitTask(random(3000));
        }
    }
    
    @Override
    public void checkForEnemies() {
        for (Creature enemy : NPC.getEnemiesWithinFOV()) {
            if (!enoughFriendliesNearby() || !NPC.getInventory().getItems().isEmpty() || enemy.getInventory().getItems().isEmpty()) {
                currentTask = new MoveTask(NPC, whereToRun(enemy));
                return;
            }
            currentTask = new StealTask((Thief) NPC, enemy);
        }
    }
    
    public boolean enoughFriendliesNearby() {
        return this.NPC.getFriendliesWithinFOV().size() >= cowardLevel;
    }
    
    public boolean isStealing() {
        return currentTask.getClass() == StealTask.class;
    }
    
    // Plots a place to run away from enemy
    private Tile whereToRun(Creature enemy) {
        return enemy.getArea().getTiles().get(3);
    }
}

@@ -902,16 +902,45 @@
 Stolen;%0A
+    private boolean fleeing;%0A
     %0A   
@@ -1051,16 +1051,41 @@
  false;%0A
+        fleeing = false;%0A
     %7D%0A  
@@ -1897,24 +1897,93 @@
 sEmpty()) %7B%0A
+                if (fleeing) return;%0A                fleeing = true;%0A
             
@@ -2068,32 +2068,61 @@
 ;%0A            %7D%0A
+            fleeing = false;%0A
             curr
@@ -2166,16 +2166,164 @@
 enemy);%0A
+            break; // TODO: Should steal from the nearest enemy instead of%0A                   // always picking the first one in the returned list.%0A
         

Create new MoveTask only if Thief is currently not fleeing