package za.redbridge.simulator.phenotype;

import org.jbox2d.dynamics.Fixture;

import sim.util.Double2D;
import za.redbridge.simulator.object.ResourceObject;
import za.redbridge.simulator.object.TargetAreaObject;
import za.redbridge.simulator.sensor.AgentSensor;
import za.redbridge.simulator.sensor.ProximityAgentSensor;
import za.redbridge.simulator.sensor.SensorReading;

import java.util.ArrayList;
import java.util.List;

public class ChasingPhenotype implements Phenotype {
    private int cooldown = 10;
    private int cooldownCounter = 0;
    private Double2D lastMove = null;
    private final List<AgentSensor> sensors;

    public ChasingPhenotype() {
        AgentSensor leftSensor = new ChasingSensor((float) ((7 / 4.0f) * Math.PI));
        AgentSensor forwardSensor = new ChasingSensor(0.0f);
        AgentSensor rightSensor = new ChasingSensor((float) (Math.PI/4));
        sensors = new ArrayList<>();
        sensors.add(leftSensor);
        sensors.add(forwardSensor);
        sensors.add(rightSensor);
    }

    @Override
    public List<AgentSensor> getSensors() {
        return sensors;
    }

    @Override
    public Double2D step(List<SensorReading> list) {
        Double2D left = new Double2D(0.5,1.0);
        Double2D forward = new Double2D(1.0,1.0);
        Double2D right = new Double2D(1.0,0.5);
        Double2D random = new Double2D((float)Math.random()*2f - 1f, (float)Math.random()*2f - 1f);

        if(cooldownCounter > 0) {
            cooldownCounter--;
            return lastMove;
        }else {
            cooldownCounter = cooldown;
        }

        double leftReading = list.get(0).getValues().get(0);
        double forwardReading = list.get(1).getValues().get(0);
        double rightReading = list.get(2).getValues().get(0);
        double max = Math.max(leftReading,Math.max(forwardReading,rightReading));
        if(max < 0.0001){
            lastMove = random;
            return random;
        }else if(leftReading == max) {
            lastMove = left;
            return left;
        }else if(rightReading == max) {
            lastMove = right;
            return right;
        }else {
            lastMove = forward;
            return forward;
        }
    }

    @Override
    public Phenotype clone() {
        return new ChasingPhenotype();
    }

    private static class ChasingSensor extends ProximityAgentSensor {

        public ChasingSensor(float bearing) {
            super(bearing);
        }

        public ChasingSensor(float bearing, float orientation, float range, float fieldOfView) {
            super(bearing, orientation, range, fieldOfView);
        }

        @Override
        public boolean isRelevantObject(Fixture otherFixture) {
            Object obj = otherFixture.getBody().getUserData();
            return super.isRelevantObject(otherFixture)
                    && !(obj instanceof TargetAreaObject)
                    && !(obj instanceof ResourceObject
                    && ((ResourceObject) obj).isCollected());
        }
    }
}

@@ -490,27 +490,45 @@
 ate 
-int cooldown
+static final int COOLDOWN
  = 10;%0A
+    %0A
     
@@ -757,16 +757,30 @@
 Math.PI)
+, 0f, 1f, 0.2f
 );%0A     
@@ -829,18 +829,30 @@
 Sensor(0
-.0
+f, 0f, 1f, 0.2
 f);%0A    
@@ -918,16 +918,30 @@
 th.PI/4)
+, 0f, 1f, 0.2f
 );%0A     
@@ -1624,24 +1624,24 @@
 unter = 
-cooldown
+COOLDOWN
 ;%0A      

Tweak ChasingPhenotype sensor config