From 244f1d34216e3e90cd77ec1d5c6110a6cf1bed7f Mon Sep 17 00:00:00 2001
From: nollet <nollet>
Date: Wed, 18 May 2022 17:41:40 +0200
Subject: [PATCH] =?UTF-8?q?fin=20J3=20(gestion=20Bord=20+=20Reynolds=20OK?=
 =?UTF-8?q?=20avec=20Comportement=20Composite)=20(mais=20comptes=20rendus?=
 =?UTF-8?q?=20encore=20=C3=A0=20r=C3=A9diger...)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/Scripts/Agent.cs                       | 11 ++---
 src/Scripts/Comportement/Aleatoire.cs      |  3 +-
 src/Scripts/Comportement/Bord.cs           | 40 ++++++++++++++++
 src/Scripts/Comportement/Comportement.cs   |  8 +---
 src/Scripts/Comportement/CompositeSeq.cs   | 33 ++++++++++++++
 src/Scripts/Comportement/Paper.cs          | 13 ++++++
 src/Scripts/Comportement/Player.cs         | 12 +++++
 src/Scripts/Comportement/Predator.cs       | 13 ++++++
 src/Scripts/Comportement/Prey.cs           | 13 ++++++
 src/Scripts/Comportement/Reynolds.cs       | 10 ++--
 src/Scripts/Environnement/Cube.cs          | 53 ++++++++++++++++++++++
 src/Scripts/Environnement/Environnement.cs | 46 +++++--------------
 src/Scripts/Environnement/Topologie.cs     | 16 +++++++
 src/Scripts/Perception/Perception.cs       | 11 -----
 src/Scripts/Perception/Perception180.cs    |  8 ++++
 src/Scripts/Perception/Perception360.cs    | 11 -----
 src/Scripts/Perception/Perception90.cs     |  8 ++++
 src/Scripts/SMA.cs                         |  7 +--
 src/Scripts/Utils.cs                       | 22 ---------
 19 files changed, 233 insertions(+), 105 deletions(-)
 create mode 100755 src/Scripts/Comportement/Bord.cs
 create mode 100755 src/Scripts/Comportement/CompositeSeq.cs
 create mode 100755 src/Scripts/Comportement/Paper.cs
 create mode 100755 src/Scripts/Comportement/Player.cs
 create mode 100755 src/Scripts/Comportement/Predator.cs
 create mode 100755 src/Scripts/Comportement/Prey.cs
 create mode 100755 src/Scripts/Environnement/Cube.cs
 create mode 100755 src/Scripts/Environnement/Topologie.cs
 create mode 100755 src/Scripts/Perception/Perception180.cs
 create mode 100755 src/Scripts/Perception/Perception90.cs

diff --git a/src/Scripts/Agent.cs b/src/Scripts/Agent.cs
index f77a515..241c4b5 100755
--- a/src/Scripts/Agent.cs
+++ b/src/Scripts/Agent.cs
@@ -20,14 +20,13 @@ public class Agent : MonoBehaviour, Visible
     void Start()
     {
         system = GameObject.Find("SMA").GetComponent<SMA>();
-        comportement = new Reynolds(this);
-        perception = new Perception360(this, 15);
-        //perception = new Perception(this,20.0f,180.0f);
-        direction = Utils.randomDirection();
-        //speed = UnityEngine.Random.Range(0.8f,1.2f);
+        //comportement = new Reynolds(this);
+        List<Comportement> c = new List<Comportement> { new Reynolds(this), new Bord(this)};
+        comportement = new CompositeSeq(this,c);
+        perception = new Perception180(this,20.0f);
+        direction = Vector3.zero;
         speed = 0.7f;
         oldDirections = new List<Vector3>();
-        oldDirections.Add(direction);
     }
 
     // Update is called once per frame
diff --git a/src/Scripts/Comportement/Aleatoire.cs b/src/Scripts/Comportement/Aleatoire.cs
index aa34982..782a799 100755
--- a/src/Scripts/Comportement/Aleatoire.cs
+++ b/src/Scripts/Comportement/Aleatoire.cs
@@ -8,8 +8,7 @@ public class Aleatoire : Comportement
     public Aleatoire(Agent proprietaire) : base(proprietaire) {}
 
     public override Vector3 reagir(List<Observation> observation) {
-        //int bruit = UnityEngine.Random.Range(1,5);
-        return proprietaire.oldDirections[0].normalized;
+        return Utils.randomDirection();
     }
 
 }
diff --git a/src/Scripts/Comportement/Bord.cs b/src/Scripts/Comportement/Bord.cs
new file mode 100755
index 0000000..211ab3b
--- /dev/null
+++ b/src/Scripts/Comportement/Bord.cs
@@ -0,0 +1,40 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class Bord : Comportement
+{
+    static public float Urgent = 3.0f;
+    static public float Preoccuppant  = 5.0f;
+    static public float Intrigant = 8.0f;
+
+    public Bord(Agent proprietaire) : base(proprietaire) {}
+
+    public override Vector3 reagir(List<Observation> observation) {
+        List<Vector3> vectors = new List<Vector3>();
+        vectors.Add(proprietaire.direction);
+        foreach(Observation o in observation) {
+            if (!o.objet.isAlive()) {
+                if(grandDanger(o)) for(int i=0;i<3;i++) vectors.Add(fuir(o.distance));
+                else if(moyenDanger(o)) for(int i=0;i<2;i++) vectors.Add(fuir(o.distance));
+                else if(petitDanger(o)) vectors.Add(fuir(o.distance));
+            }
+        }
+        Vector3 res = Utils.meanVector(vectors);
+        return Utils.noiseVector(res,nbBruit,bruit);
+    }
+    
+    private bool grandDanger(Observation o) {
+        return (Utils.normVector(o.distance) < Urgent);
+    }
+
+    private bool moyenDanger(Observation o) {
+        return (Utils.normVector(o.distance) < Preoccuppant);
+    }
+
+    private bool petitDanger(Observation o) {
+        return (Utils.normVector(o.distance) < Intrigant);
+    }
+
+    private Vector3 fuir(Vector3 d) { return -d.normalized; }
+}
diff --git a/src/Scripts/Comportement/Comportement.cs b/src/Scripts/Comportement/Comportement.cs
index c1728d8..580374a 100755
--- a/src/Scripts/Comportement/Comportement.cs
+++ b/src/Scripts/Comportement/Comportement.cs
@@ -7,17 +7,13 @@ public abstract class Comportement
 
     protected Agent proprietaire;
 
-    protected int nbBruit = 5;
-    protected float bruit = 0.01f;
+    protected int nbBruit = 8;
+    protected float bruit = 0.05f;
 
     public Comportement(Agent a) {
         proprietaire = a;
     }
 
     public abstract Vector3 reagir(List<Observation> observation) ;
-    /*{
-        return Utils.noiseVector(proprietaire.direction,nbBruit,bruit);
-        //return Vector3.zero;
-    }*/
 
 }
diff --git a/src/Scripts/Comportement/CompositeSeq.cs b/src/Scripts/Comportement/CompositeSeq.cs
new file mode 100755
index 0000000..4138ade
--- /dev/null
+++ b/src/Scripts/Comportement/CompositeSeq.cs
@@ -0,0 +1,33 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class CompositeSeq : Comportement
+{
+
+    protected List<Comportement> comportements;
+
+    public CompositeSeq(Agent proprietaire,List<Comportement> c) : base(proprietaire) {
+        comportements = c;
+    }
+
+    public CompositeSeq(Agent proprietaire) : base(proprietaire) {
+        comportements = new List<Comportement>();
+    }
+
+    public void removeComportement(int i) { comportements.RemoveAt(i); }
+
+    public void AddComportement(Comportement c) { comportements.Add(c); }
+
+    public override Vector3 reagir(List<Observation> observation) {
+        List<Vector3> vectors = new List<Vector3>();
+        vectors.Add(proprietaire.direction);
+        Vector3 add;
+        for(int i=0;i<comportements.Count;i++) {
+            add = comportements[i].reagir(observation);
+            vectors.Add(add);
+        }
+        return Utils.meanVector(vectors);
+    }
+
+}
diff --git a/src/Scripts/Comportement/Paper.cs b/src/Scripts/Comportement/Paper.cs
new file mode 100755
index 0000000..93a675b
--- /dev/null
+++ b/src/Scripts/Comportement/Paper.cs
@@ -0,0 +1,13 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class Paper : Comportement
+{
+    public Paper(Agent proprietaire) : base(proprietaire) {}
+
+    public override Vector3 reagir(List<Observation> observation) {
+        return Utils.randomDirection();
+    }
+    
+}
diff --git a/src/Scripts/Comportement/Player.cs b/src/Scripts/Comportement/Player.cs
new file mode 100755
index 0000000..6e16e35
--- /dev/null
+++ b/src/Scripts/Comportement/Player.cs
@@ -0,0 +1,12 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class Player : Comportement
+{
+    public Player(Agent proprietaire) : base(proprietaire) {}
+
+    public override Vector3 reagir(List<Observation> observation) {
+        return Utils.randomDirection();
+    }
+}
diff --git a/src/Scripts/Comportement/Predator.cs b/src/Scripts/Comportement/Predator.cs
new file mode 100755
index 0000000..600118f
--- /dev/null
+++ b/src/Scripts/Comportement/Predator.cs
@@ -0,0 +1,13 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class Predator : Comportement
+{
+    public Predator(Agent proprietaire) : base(proprietaire) {}
+
+    public override Vector3 reagir(List<Observation> observation) {
+        return Utils.randomDirection();
+    }
+    
+}
diff --git a/src/Scripts/Comportement/Prey.cs b/src/Scripts/Comportement/Prey.cs
new file mode 100755
index 0000000..e2eee65
--- /dev/null
+++ b/src/Scripts/Comportement/Prey.cs
@@ -0,0 +1,13 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class Prey : Comportement
+{
+    public Prey(Agent proprietaire) : base(proprietaire) {}
+
+    public override Vector3 reagir(List<Observation> observation) {
+        return Utils.randomDirection();
+    }
+    
+}
diff --git a/src/Scripts/Comportement/Reynolds.cs b/src/Scripts/Comportement/Reynolds.cs
index f5fac29..ddb7a7b 100755
--- a/src/Scripts/Comportement/Reynolds.cs
+++ b/src/Scripts/Comportement/Reynolds.cs
@@ -20,12 +20,8 @@ public class Reynolds : Comportement
                 else if(normal(o)) vectors.Add(suivre(o.objet));
                 else if(isolement(o)) vectors.Add(rapprocher(o.distance));
             }
-            else {
-                if(danger(o)) for(int i=0;i<5;i++) vectors.Add(fuir(o.distance));
-            }
         }
         Vector3 res = Utils.meanVector(vectors);
-        //return proprietaire.direction;
         return Utils.noiseVector(res,nbBruit,bruit);
     }
     
@@ -33,7 +29,7 @@ public class Reynolds : Comportement
         return (Utils.normVector(o.distance) < Escape);
     }
 
-    private Vector3 fuir(Vector3 d) { return -d; }
+    private Vector3 fuir(Vector3 d) { return -d.normalized; }
 
     private bool normal(Observation o) { 
         return Utils.normVector(o.distance) < Together;
@@ -41,13 +37,13 @@ public class Reynolds : Comportement
 
     private Vector3 suivre(Visible a) { 
         Agent agent = (Agent) a;
-        return agent.direction;
+        return agent.direction.normalized;
     }
 
     private bool isolement(Observation o) {
         return Utils.normVector(o.distance) <= Follow;
     }
 
-    private Vector3 rapprocher(Vector3 d) { return d; }
+    private Vector3 rapprocher(Vector3 d) { return d.normalized; }
 
 }
diff --git a/src/Scripts/Environnement/Cube.cs b/src/Scripts/Environnement/Cube.cs
new file mode 100755
index 0000000..20916b1
--- /dev/null
+++ b/src/Scripts/Environnement/Cube.cs
@@ -0,0 +1,53 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class Cube : Topologie
+{
+
+    public float minX;
+    public float maxX;
+    public float minY;
+    public float maxY;
+    public float minZ;
+    public float maxZ;
+    public GameObject cube;
+
+
+    public Cube(GameObject c) : base() { 
+        cube = c;
+        float scaleX = cube.transform.localScale.x;
+        float scaleY = cube.transform.localScale.y;
+        float scaleZ = cube.transform.localScale.z;
+        minX = -scaleX/2.0f;maxX = scaleX/2.0f;
+        minY = -scaleY/2.0f;maxY = scaleY/2.0f;
+        minZ = -scaleZ/2.0f;maxZ = scaleZ/2.0f;
+        }
+
+    public override GameObject getGameObject() { return cube;}
+
+    public override Observation detectEnv(Agent current) {
+        Vector3 pos = current.transform.position;
+        List<Vector3> p = new List<Vector3>
+        { 
+            new Vector3(minX,pos.y,pos.z), new Vector3(maxX,pos.y,pos.z),
+            new Vector3(pos.x,minY,pos.z), new Vector3(pos.x,maxY,pos.z),
+            new Vector3(pos.x,pos.y,minZ), new Vector3(pos.x,pos.y,maxZ),
+        };
+        float dist = Utils.normVector(p[0] - pos);
+        float d; Vector3 res = p[0];
+        for(int i=1;i<p.Count;i++) {
+            d = Utils.normVector(p[i] - pos);
+            if(d<dist) {
+                dist = d;
+                res = p[i];
+            }
+        }
+        return new Observation(new Obstacle(res),res-pos);
+    }
+
+    public override Vector3 validPosition() {
+        return Utils.randomVector(minX, maxX, minY, maxY, minZ, maxZ);
+    }
+    
+}
diff --git a/src/Scripts/Environnement/Environnement.cs b/src/Scripts/Environnement/Environnement.cs
index e024985..36ac2cd 100755
--- a/src/Scripts/Environnement/Environnement.cs
+++ b/src/Scripts/Environnement/Environnement.cs
@@ -6,45 +6,17 @@ public class Environnement : MonoBehaviour
 {
 
     private SMA system;
-    public GameObject topologie;
-    public float minX;
-    public float maxX;
-    public float minY;
-    public float maxY;
-    public float minZ;
-    public float maxZ;
+    private Topologie topologie;
+    public GameObject cube;
+    private List<Obstacle> obstacles;
 
     // Start is called before the first frame update
     void Start()
     {
         system = GameObject.Find("SMA").GetComponent<SMA>();
-        Instantiate(topologie, Vector3.zero, Quaternion.identity,transform);
-        float scaleX = topologie.transform.localScale.x;
-        float scaleY = topologie.transform.localScale.y;
-        float scaleZ = topologie.transform.localScale.z;
-        minX = -scaleX/2.0f;maxX = scaleX/2.0f;
-        minY = -scaleY/2.0f;maxY = scaleY/2.0f;
-        minZ = -scaleZ/2.0f;maxZ = scaleZ/2.0f;
-    }
-
-    public Observation detectEnv(Agent current) {
-        Vector3 pos = current.transform.position;
-        List<Vector3> p = new List<Vector3>
-        { 
-            new Vector3(minX,pos.y,pos.z), new Vector3(maxX,pos.y,pos.z),
-            new Vector3(pos.x,minY,pos.z), new Vector3(pos.x,maxY,pos.z),
-            new Vector3(pos.x,pos.y,minZ), new Vector3(pos.x,pos.y,maxZ),
-        };
-        float dist = Utils.normVector(p[0] - pos);
-        float d; Vector3 res = p[0];
-        for(int i=1;i<p.Count;i++) {
-            d = Utils.normVector(p[i] - pos);
-            if(d<dist) {
-                dist = d;
-                res = p[i];
-            }
-        }
-        return new Observation(new Obstacle(res),res-pos);
+        obstacles = new List<Obstacle>();
+        topologie = new Cube(cube);
+        Instantiate(topologie.getGameObject(), Vector3.zero, Quaternion.identity,transform);   
     }
 
     public List<Observation> voisinage(Agent current, float rayon, float angle) {
@@ -56,12 +28,16 @@ public class Environnement : MonoBehaviour
             a = Vector3.Angle(current.direction,dist);
             if (system.agents[i] != current && l <= rayon && a <= angle/2.0f) res.Add(new Observation(system.agents[i],dist));
         }
-        Observation bord = detectEnv(current);
+        Observation bord = topologie.detectEnv(current);
         l = Utils.normVector(bord.distance); a = Vector3.Angle(current.direction,bord.distance);
         if(l <= rayon && a <= angle/2.0f) res.Add(bord);
         return res;
     }
 
+    public Vector3 validPosition() {
+        return topologie.validPosition();
+    }
+
     // Update is called once per frame
     void Update()
     {
diff --git a/src/Scripts/Environnement/Topologie.cs b/src/Scripts/Environnement/Topologie.cs
new file mode 100755
index 0000000..7ad7de2
--- /dev/null
+++ b/src/Scripts/Environnement/Topologie.cs
@@ -0,0 +1,16 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public abstract class Topologie
+{
+    
+    public Topologie() {}
+
+    public abstract GameObject getGameObject();
+
+    public abstract Observation detectEnv(Agent current);
+
+    public abstract Vector3 validPosition();
+
+}
diff --git a/src/Scripts/Perception/Perception.cs b/src/Scripts/Perception/Perception.cs
index 0cf080e..ce872e4 100755
--- a/src/Scripts/Perception/Perception.cs
+++ b/src/Scripts/Perception/Perception.cs
@@ -19,15 +19,4 @@ public class Perception
         return proprietaire.system.environnement.voisinage(proprietaire, rayon, angle);    
     }
 
-    // Start is called before the first frame update
-    void Start()
-    {
-        
-    }
-
-    // Update is called once per frame
-    void Update()
-    {
-        
-    }
 }
diff --git a/src/Scripts/Perception/Perception180.cs b/src/Scripts/Perception/Perception180.cs
new file mode 100755
index 0000000..8d77ca2
--- /dev/null
+++ b/src/Scripts/Perception/Perception180.cs
@@ -0,0 +1,8 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class Perception180 : Perception
+{
+    public Perception180(Agent agent, float r) : base(agent, r, 180.0f) {}
+}
diff --git a/src/Scripts/Perception/Perception360.cs b/src/Scripts/Perception/Perception360.cs
index d650015..0c4878f 100755
--- a/src/Scripts/Perception/Perception360.cs
+++ b/src/Scripts/Perception/Perception360.cs
@@ -7,15 +7,4 @@ public class Perception360 : Perception
 
     public Perception360(Agent agent, float r) : base(agent, r, 360.0f) {}
 
-    // Start is called before the first frame update
-    void Start()
-    {
-        
-    }
-
-    // Update is called once per frame
-    void Update()
-    {
-        
-    }
 }
diff --git a/src/Scripts/Perception/Perception90.cs b/src/Scripts/Perception/Perception90.cs
new file mode 100755
index 0000000..237e5ab
--- /dev/null
+++ b/src/Scripts/Perception/Perception90.cs
@@ -0,0 +1,8 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class Perception90 : Perception
+{
+    public Perception90(Agent agent, float r) : base(agent, r, 90.0f) {}
+}
diff --git a/src/Scripts/SMA.cs b/src/Scripts/SMA.cs
index 98d32db..810ea05 100755
--- a/src/Scripts/SMA.cs
+++ b/src/Scripts/SMA.cs
@@ -6,7 +6,7 @@ public class SMA : MonoBehaviour
 {
     public List<Agent> agents;
     public Environnement environnement;
-    public int population = 50;
+    public int population = 80;
     public GameObject boid;
 
 
@@ -32,10 +32,7 @@ public class SMA : MonoBehaviour
     void createAgents() {
         GameObject a;
         for(int i=0; i<population; i++) {
-            a = Instantiate(boid, 
-                Utils.randomVector(environnement.minX, environnement.maxX, 
-                environnement.minY, environnement.maxY, 
-                environnement.minZ, environnement.maxZ), Quaternion.identity,environnement.transform) as GameObject;
+            a = Instantiate(boid, environnement.validPosition(), Quaternion.identity,environnement.transform) as GameObject;
             agents.Add(a.GetComponent<Agent>());
         }
 
diff --git a/src/Scripts/Utils.cs b/src/Scripts/Utils.cs
index 3efaabd..461669f 100755
--- a/src/Scripts/Utils.cs
+++ b/src/Scripts/Utils.cs
@@ -80,26 +80,4 @@ public class Utils
         return normalizedVector(x,y,z);
     }
 
-    /* List<Component> neighbourhood() {
-        List<Component> neighbours = new List<Component>();
-        float dist;
-        Vector3 link;
-        Agent bird;
-        for(int i=0; i<birds.Length; i++) {
-            bird = (Agent) birds[i];
-            link = bird.transform.position - transform.position;
-            dist = (float) Math.Sqrt(Math.Pow(link.x,2) + Math.Pow(link.y,2) + Math.Pow(link.z,2));
-            if(bird != this && dist < system.RaynoldFollowTresh) neighbours.Add(birds[i]);
-        }
-        return neighbours;
-    }
-
-    void SMA() {
-        Vector3 res = Vector3.zero;
-        res += direction;
-        if(transform.position.x < system.minX || transform.position.x > system.maxX) res.x = -direction.x;
-        if(transform.position.y < system.minY || transform.position.y > system.maxY) res.y = -direction.y;
-        if(transform.position.z < system.minZ || transform.position.z > system.maxZ) res.z = -direction.z;
-        nextDirections.Add(res);
-    }*/
 }
-- 
GitLab