private static void InitializeBeaconInfoList() { foreach (var beacon in db.Beacons) { BeaconInfo info = new BeaconInfo(); info.ProductId = beacon.ProductId; info.InFilter = beacon.InFilter.GetValueOrDefault(); info.OutFilter = beacon.OutFilter.GetValueOrDefault(); BeaconInfoList.Add(beacon.BeaconId, info); } }
public override IEnumerator Run(Brain controller) { List<SensedObject> sensedWolf = new List<SensedObject>(); bool thereIsSheperd = false; int totalSheep = 1; Transform playerTrans = null; foreach (SensedObject obj in controller.senses.GetSensedObjects()) { if (obj.getAgentType().Equals(AgentClassification.Wolf)) { sensedWolf.Add (obj); } if (obj.getAgentType().Equals(AgentClassification.Shepherd)) { playerTrans = obj.getObject().transform; thereIsSheperd = true; } // Let the the crowd influence the mood of individual sheep. This will make single scared sheep less panicky, but allow mass confusion to ensue given enough panic. if (obj.getAgentType() == AgentClassification.Sheep) { totalSheep++; } } SensedObject scariestWolf = myBrain.memory.GetValue<SensedObject>("Scariest"); if(scariestWolf != null) { sensedWolf.Add(scariestWolf); } //get current BeaconInfo if (controller.memory.GetValue<BeaconInfo>("LastBeacon") != null) { if (curBeacon != null) { if (curBeacon.GetTime() < controller.memory.GetValue<BeaconInfo>("LastBeacon").GetTime()) { curBeacon = controller.memory.GetValue<BeaconInfo>("LastBeacon"); } } else { curBeacon = controller.memory.GetValue<BeaconInfo>("LastBeacon"); } } if (curBeacon != null) { //StopCurBeacon++; //beaconHelper = 0.2f; //if (StopCurBeacon==1) //{ //if(Time.time - curBeacon.GetTime() <= Time.deltaTime) //{ controller.memory.SetValue("cowardLevel", controller.memory.GetValue<float>("cowardLevel") - beaconHelper); controller.memory.SetValue<float>("Panic", 0f); //Debug.Log(controller.getGameObject() + ": " + (controller.memory.GetValue<float>("cowardLevel"))); if (controller.memory.GetValue<float>("cowardLevel") <= 0f) { controller.memory.SetValue("cowardLevel", 0.01f); } //} //} //delete curBeacon curBeacon = null; controller.memory.SetValue("LastBeacon", null); } if (thereIsSheperd) { Vector2 playerPos = new Vector2(playerTrans.position.x, playerTrans.position.z); Vector2 playerFacing = new Vector2(playerTrans.forward.x, playerTrans.forward.z); Vector2 positionOffset = myBrain.legs.getPosition() - playerPos; //decrease cowardLevel when there is sheperd looking at it if (Vector2.Dot(playerFacing.normalized, positionOffset.normalized) > 0.71f) { controller.memory.SetValue("cowardLevel", controller.memory.GetValue<float>("cowardLevel") - (Time.deltaTime * 0.02f)); if (controller.memory.GetValue<float>("cowardLevel") <= 0f) { controller.memory.SetValue("cowardLevel", 0.001f); } } //normalise cowardLevel back to normal if Sheperd isn't looking else { controller.memory.SetValue("cowardLevel", controller.memory.GetValue<float>("cowardLevel") + (Time.deltaTime * 0.01f)); if (controller.memory.GetValue<float>("cowardLevel") >= 0.5f) { controller.memory.SetValue("cowardLevel", 0.5f); } } } else { //decrease cowardLevel when there are more than 4 sheep around it. if (totalSheep >= 4) { controller.memory.SetValue("cowardLevel", controller.memory.GetValue<float>("cowardLevel") - (Time.deltaTime * 0.01f)); if (controller.memory.GetValue<float>("cowardLevel") <= 0f) { controller.memory.SetValue("cowardLevel", 0.01f); } } //normalise cowardLevel to its default value else { if (sensedWolf.Count == 0) { if (controller.memory.GetValue<float>("cowardLevel") > 0.5f) { controller.memory.SetValue("cowardLevel", controller.memory.GetValue<float>("cowardLevel") - (Time.deltaTime * 0.01f)); if (controller.memory.GetValue<float>("cowardLevel") <= 0.5f) { controller.memory.SetValue("cowardLevel", 0.5f); } } else { controller.memory.SetValue("cowardLevel", controller.memory.GetValue<float>("cowardLevel") + (Time.deltaTime * 0.02f)); if (controller.memory.GetValue<float>("cowardLevel") >= 0.5f) { controller.memory.SetValue("cowardLevel", 0.5f); } } } else { controller.memory.SetValue("cowardLevel", controller.memory.GetValue<float>("cowardLevel") + (Time.deltaTime * 0.01f * sensedWolf.Count)); if (controller.memory.GetValue<float>("cowardLevel") >= 1f) { controller.memory.SetValue("cowardLevel", 0.99f); } } } } sensedWolf.Sort(Scariest); if (sensedWolf.Count > 0) { scariestWolf = sensedWolf[0]; myBrain.memory.SetValue("Scariest", scariestWolf); if (thereIsSheperd) { //the less cowardLevel is, the less Panic increases controller.memory.SetValue<float>("Panic", controller.memory.GetValue<float>("Panic") - ((Time.deltaTime * (sensedWolf.Count * 0.5f) * decayPanicRate * controller.memory.GetValue<float>("cowardLevel")) / shepherdInfluence)); } else { Vector2 sheepPos = myBrain.legs.getPosition(); Vector2 wolfPos = scariestWolf.getObject().GetComponent<Brain>().legs.getPosition(); float wolfDistance = Vector2.Distance(sheepPos, wolfPos); //the less cowardLevel is, the less Panic increases controller.memory.SetValue<float>("Panic", controller.memory.GetValue<float>("Panic") + (Time.deltaTime * sensedWolf.Count * increasePanicRate * controller.memory.GetValue<float>("cowardLevel") * (2 / wolfDistance))); } fleeBehaviour.setTarget(scariestWolf.getObject()); fleeBehaviour.setWeight(controller.memory.GetValue<float>("Panic"));// * 1.25f); } else { if (thereIsSheperd) { //the less cowardLevel is, the more Panic decreases controller.memory.SetValue<float>("Panic", controller.memory.GetValue<float>("Panic") - (Time.deltaTime * decayPanicRate * shepherdInfluence * (1 - controller.memory.GetValue<float>("cowardLevel")))); time = 0f; } else { if (time > 3f) { //the less cowardLevel is, the more Panic decreases controller.memory.SetValue<float>("Panic", controller.memory.GetValue<float>("Panic") - (Time.deltaTime * decayPanicRate * (1 - controller.memory.GetValue<float>("cowardLevel")))); } else { time += Time.deltaTime; } } //set the minimum Panic level for sheep if (controller.memory.GetValue<float>("Panic") < 0f) { controller.memory.SetValue<float>("Panic", 0f); fleeBehaviour.setTarget(null); } //fleeBehaviour weight fleeBehaviour.setWeight(controller.memory.GetValue<float>("Panic")); } //if the sheep get caught if (controller.memory.GetValue<bool>("BeingEaten") == true) { mainMachine.RequestStateTransition(eaten.GetTarget()); } // if panic level larger than 30, change to gonenuts state. if (controller.memory.GetValue<float>("Panic") >= 20f) { Debug.Log("I'm so scared!"); mainMachine.RequestStateTransition(nuts.GetTarget()); } // if can't see wolf and panic level has decreased, change to roaming state else if (controller.memory.GetValue<float>("Panic") < fleeThreshold) { Debug.Log("Wolf's gone!"); mainMachine.RequestStateTransition(calmed.GetTarget()); } yield return null; }
public override IEnumerator Run(Brain controller) { List<GameObject> seenSheep = new List<GameObject>(); bool thereIsShepherd = false; Transform playerTrans = null; foreach (SensedObject obj in controller.senses.GetSensedObjects()) { if(obj.getAgentType().Equals(AgentClassification.Sheep)) { seenSheep.Add(obj.getObject()); } if(obj.getAgentType().Equals(AgentClassification.Shepherd)) { playerTrans = obj.getObject().transform; thereIsShepherd = true; } } //get current BeaconInfo if (controller.memory.GetValue<BeaconInfo>("LastBeacon") != null) { if (curBeacon != null) { if (curBeacon.GetTime() < controller.memory.GetValue<BeaconInfo>("LastBeacon").GetTime()) { curBeacon = controller.memory.GetValue<BeaconInfo>("LastBeacon"); } } else { curBeacon = controller.memory.GetValue<BeaconInfo>("LastBeacon"); } } if (curBeacon != null) { //delete curBeacon curBeacon = null; controller.memory.SetValue("LastBeacon", null); controller.memory.SetValue("shouldHide", 3f); } //increase its decayHungryLevel when hunting if (controller.memory.GetValue<float>("hungryLevel") > 0f) { controller.memory.SetValue("hungryLevel", controller.memory.GetValue<float>("hungryLevel") - (decayHungryLevel * 2 * (myBrain.memory.GetValue<float>("ferocity") / 5))); } else { controller.memory.SetValue("hungryLevel", 0f); Debug.Log("I died because I used up my energy"); createARFF(myBrain.memory.GetValue<string>("targeting"), "FAIL"); //delete itself in sheepTarget memory List<Brain> wolvesChasing = sheepMemory.GetValue<List<Brain>>("chasedBy"); if (wolvesChasing.Count > 0) { wolvesChasing.Remove(this.myBrain); sheepMemory.SetValue("chasedBy", wolvesChasing); } myBrain.getGameObject().SetActiveRecursively(false); } myBrain.memory.SetValue("caution", myBrain.memory.GetValue<float>("caution") - cautionLevelDecay * Time.deltaTime); if(thereIsShepherd) { UpdateCaution(playerTrans); } else { myBrain.memory.SetValue ("watched", myBrain.memory.GetValue<float>("watched") - watchedLevelDecay * Time.deltaTime); } myBrain.memory.SetValue("watched", Mathf.Clamp(myBrain.memory.GetValue<float>("watched"), 0, Mathf.Infinity)); if (seenSheep.Count > 0) { if(seenSheep.Contains(myBrain.memory.GetValue<SensedObject>("hasCommand").getObject())) { stillSeeTarget = true; } else { stillSeeTarget = false; } } else { stillSeeTarget = false; } if (stillSeeTarget) { //increase weight based on ferocityRate. arriveBehaviour.setWeight(arriveBehaviour.getWeight() + (Time.deltaTime * ferocityRate)); seekBehaviour.setWeight(0f); if (arriveBehaviour.getWeight() > 10f) { arriveBehaviour.setWeight(10f); } oldTargetPosition = arriveBehaviour.getTarget(); time = 0f; } else { time += Time.deltaTime; if (time > 1.5f) { //keep chasing seekBehaviour.setTarget(oldTargetPosition); seekBehaviour.setWeight(seekBehaviour.getWeight() + (Time.deltaTime * ferocityRate)); //arrive's weight decreases arriveBehaviour.setWeight(arriveBehaviour.getWeight() - (Time.deltaTime / ferocityRate)); if (seekBehaviour.getWeight() > 5f) { seekBehaviour.setWeight(5f); } if (arriveBehaviour.getWeight() < 0f) { arriveBehaviour.setWeight(0f); } } if (arriveBehaviour.getWeight() == 0f) { //change to roaming state UnityEngine.Debug.Log("Give up finding"); //delete itself in sheepTarget memory List<Brain> wolvesChasing = sheepMemory.GetValue<List<Brain>>("chasedBy"); if (wolvesChasing.Count > 0) { wolvesChasing.Remove(this.myBrain); sheepMemory.SetValue("chasedBy", wolvesChasing); } result = "FAIL"; mainMachine.RequestStateTransition(roam.GetTarget()); } } //if the wolf catches the sheep float distance = Vector2.Distance(myBrain.legs.getPosition(), sheepBrain.legs.getPosition()); if (distance < 0f) { distance = distance * (-1); //distance can't be negative } if (distance <= 1f) { if (sheepBrain.memory.GetValue<float>("HP") >= 60f) { UnityEngine.Debug.Log("Distance of " + myBrain.getGameObject() + " and " + sheepBrain.getGameObject() + " is: " + distance); sheepMemory.SetValue("BeingEaten", true); result = "EATING"; mainMachine.RequestStateTransition(eating.GetTarget()); } else { result = "FAIL"; mainMachine.RequestStateTransition(roam.GetTarget()); } } else { if (sheepBrain.memory.GetValue<float>("HP") < 60f) { result = "FAIL"; mainMachine.RequestStateTransition(roam.GetTarget()); } } yield return null; }
public override IEnumerator Run(Brain controller) { bool thereIsSheperd = false; List<SensedObject> sensedWolf = new List<SensedObject>(); foreach (SensedObject obj in controller.senses.GetSensedObjects()) { if (obj.getAgentType().Equals(AgentClassification.Wolf)) { Legs wolfLeg = (Legs)obj.getObject().GetComponent<Legs>(); Vector2 wolfFacing = new Vector2(wolfLeg.transform.forward.x, wolfLeg.transform.forward.z); Vector2 sheepPos = myBrain.legs.getPosition(); Vector2 wolfPos = wolfLeg.getPosition(); float dot = Vector2.Dot(wolfFacing, sheepPos - wolfPos); if (dot > 0) { sensedWolf.Add(obj); } if (controller.memory.GetValue<List<Brain>>("chasedBy") != null) { List<Brain> chaseByList = (List<Brain>)controller.memory.GetValue<List<Brain>>("chasedBy"); Brain objBrain = (Brain)obj.getObject().GetComponent<Brain>(); if (chaseByList.Contains(objBrain)) { sensedWolf.Add(obj); } } } if (obj.getAgentType().Equals(AgentClassification.Shepherd)) { thereIsSheperd = true; } } //get current BeaconInfo if (controller.memory.GetValue<BeaconInfo>("LastBeacon") != null) { if (curBeacon != null) { if (curBeacon.GetTime() < controller.memory.GetValue<BeaconInfo>("LastBeacon").GetTime()) { curBeacon = controller.memory.GetValue<BeaconInfo>("LastBeacon"); } } else { curBeacon = controller.memory.GetValue<BeaconInfo>("LastBeacon"); } } if (curBeacon != null) { //StopCurBeacon++; //beaconHelper = 0.2f; //if (StopCurBeacon==1) //{ //if(Time.time - curBeacon.GetTime() <= Time.deltaTime) //{ controller.memory.SetValue<float>("Panic", 0f); //Debug.Log(controller.getGameObject() + ": " + (controller.memory.GetValue<float>("cowardLevel"))); //} //} //delete curBeacon curBeacon = null; controller.memory.SetValue("LastBeacon", null); } if (sensedWolf.Count > 0) { //if there is wolf around and it can see the Sheperd, the recover rate from Panic is similar to w.o wolf. if (thereIsSheperd) { //the less cowardLevel is, the less Panic increases controller.memory.SetValue<float>("Panic", controller.memory.GetValue<float>("Panic") - (Time.deltaTime * decayPanicRate * (1 - controller.memory.GetValue<float>("cowardLevel")))); } else { //the less cowardLevel is, the less Panic increases controller.memory.SetValue<float>("Panic", controller.memory.GetValue<float>("Panic") + (Time.deltaTime * sensedWolf.Count * increasePanicRate * controller.memory.GetValue<float>("cowardLevel"))); } if (controller.memory.GetValue<float>("Panic") >= 30f) { controller.memory.SetValue<float>("Panic", 30f); } } else { //if there is no wolf around and it can see the Sheperd, the recover rate from Panic is tripled. if (thereIsSheperd) { //the less cowardLevel is, the more Panic decreases controller.memory.SetValue<float>("Panic", controller.memory.GetValue<float>("Panic") - (Time.deltaTime * decayPanicRate * shepherdInfluence * (1 - controller.memory.GetValue<float>("cowardLevel")))); time = 0f; } else { if (time > 7f) { //the less cowardLevel is, the more Panic decreases controller.memory.SetValue<float>("Panic", controller.memory.GetValue<float>("Panic") - (Time.deltaTime * decayPanicRate * (1 - controller.memory.GetValue<float>("cowardLevel")))); } else { time += Time.deltaTime; } } //set the minimum Panic level for sheep if (controller.memory.GetValue<float>("Panic") < 0f) { controller.memory.SetValue<float>("Panic", 0f); } } // if can't see wolf and panic level has decreased, change to roaming state if (controller.memory.GetValue<float>("Panic") < 3f) { Debug.Log("Wolf's gone and I'm calm now!"); mainMachine.RequestStateTransition(roaming.GetTarget()); } //if the sheep get caught if (controller.memory.GetValue<bool>("BeingEaten") == true) { mainMachine.RequestStateTransition(eaten.GetTarget()); } yield return null; }
private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e) { selectedInfo = (BeaconInfo)(sender as ListView).SelectedItem; }
public override IEnumerator Run(Brain controller) { bool thereIsShepherd = false; Transform playerTrans = null; foreach (SensedObject obj in controller.senses.GetSensedObjects()) { if (obj.getAgentType().Equals(AgentClassification.Shepherd)) { playerTrans = obj.getObject().transform; thereIsShepherd = true; } } //get current BeaconInfo if (controller.memory.GetValue<BeaconInfo>("LastBeacon") != null) { if (curBeacon != null) { if (curBeacon.GetTime() < controller.memory.GetValue<BeaconInfo>("LastBeacon").GetTime()) { curBeacon = controller.memory.GetValue<BeaconInfo>("LastBeacon"); } } else { curBeacon = controller.memory.GetValue<BeaconInfo>("LastBeacon"); } } if (curBeacon != null) { //delete curBeacon curBeacon = null; controller.memory.SetValue("LastBeacon", null); controller.memory.SetValue("shouldHide", 3f); } myBrain.memory.SetValue("caution", myBrain.memory.GetValue<float>("caution") - cautionLevelDecay * Time.deltaTime); if(thereIsShepherd) { UpdateCaution(playerTrans); } else { myBrain.memory.SetValue ("watched", myBrain.memory.GetValue<float>("watched") - watchedLevelDecay * Time.deltaTime); } myBrain.memory.SetValue("watched", Mathf.Clamp(myBrain.memory.GetValue<float>("watched"), 0, Mathf.Infinity)); arriveBehaviour.setWeight(arriveBehaviour.getWeight() + Time.deltaTime * myBrain.memory.GetValue<float>("ferocity")); if (arriveBehaviour.getWeight() > 10f) { arriveBehaviour.setWeight(10f); } myBrain.legs.maxSpeed = 0f; sheepMemory.SetValue("HP", sheepMemory.GetValue<float>("HP") - (Time.deltaTime * myBrain.memory.GetValue<float>("damage"))); //Debug.Log("Sheep's HP is: " + sheepMemory.GetValue<float>("HP")); if (sheepMemory.GetValue<float>("HP") <= 0f) { sheepTarget.getObject().SetActiveRecursively(false); Debug.Log("I ate the sheep"); controller.memory.SetValue("hungryLevel", myBrain.memory.GetValue<float>("hungryLevel") + 10f); result = "SUCCESS"; controller.memory.GetValue<Genome>("Genome").incrementFitness(1); mainMachine.RequestStateTransition(roam.GetTarget()); } yield return null; }
public override IEnumerator Run(Brain controller) { bool thereIsShepherd = false; float highestLeaderLevel = 0f; List<SensedObject> seenSheep = new List<SensedObject>(); List<SensedObject> seenWolf = new List<SensedObject>(); Transform playerTrans = null; foreach (SensedObject obj in controller.senses.GetSensedObjects()) { if (obj.getAgentType().Equals(AgentClassification.Sheep)) { seenSheep.Add(obj); } else if (obj.getAgentType().Equals(AgentClassification.Wolf)) { Memory wolfMemory = obj.getMemory(); if (highestLeaderLevel <= wolfMemory.GetValue<float>("leaderLevel")) { highestLeaderLevel = wolfMemory.GetValue<float>("leaderLevel"); } seenWolf.Add(obj); } else if(obj.getAgentType().Equals(AgentClassification.Shepherd)) { playerTrans = obj.getObject().transform; thereIsShepherd = true; } } //get current BeaconInfo if (controller.memory.GetValue<BeaconInfo>("LastBeacon") != null) { if (curBeacon != null) { if (curBeacon.GetTime() < controller.memory.GetValue<BeaconInfo>("LastBeacon").GetTime()) { curBeacon = controller.memory.GetValue<BeaconInfo>("LastBeacon"); } } else { curBeacon = controller.memory.GetValue<BeaconInfo>("LastBeacon"); } } if (curBeacon != null) { //delete curBeacon curBeacon = null; controller.memory.SetValue("LastBeacon", null); controller.memory.SetValue("shouldHide", 3f); } myBrain.memory.SetValue("caution", myBrain.memory.GetValue<float>("caution") - cautionLevelDecay * Time.deltaTime); if(thereIsShepherd) { UpdateCaution(playerTrans); } else { myBrain.memory.SetValue ("watched", myBrain.memory.GetValue<float>("watched") - watchedLevelDecay * Time.deltaTime); } myBrain.memory.SetValue("watched", Mathf.Clamp(myBrain.memory.GetValue<float>("watched"), 0, Mathf.Infinity)); if(myBrain.memory.GetValue<float>("watched") > 0) { //Debug.Log (myBrain.memory.GetValue<float>("watched")); } //check if this wolf has been given command to attack or not if (controller.memory.GetValue<SensedObject>("hasCommand") != null) { //decrease leaderLevel because it has been given command by others controller.memory.SetValue("leaderLevel", controller.memory.GetValue<float>("leaderLevel") - (decreaseLeaderLevel * Time.deltaTime)); //set the minimum leaderLevel for wolf if (controller.memory.GetValue<float>("leaderLevel") < 10f) { controller.memory.SetValue("leaderLevel", 10f); } //Change to hunting phase Debug.Log("I've received command. I'm hunting! Target: " + controller.memory.GetValue<SensedObject>("hasCommand").getObject()); } else { if (seenSheep.Count > 0) { //choose the target //if the wolf hasn't have his target, pick it target = chooseTarget(seenSheep); if (target != null) { //target is alive if (target.getObject().GetComponent<Brain>().memory.GetValue<float>("HP") > 0) { //set the target for this wolf controller.memory.SetValue("hasCommand", target); //calling sheep that it is being targeted Memory sheepMemory = target.getMemory(); //get a list of wolves that are chasing this sheep List<Brain> wolvesChasing = sheepMemory.GetValue<List<Brain>>("chasedBy"); //add itself in if (wolvesChasing != null) { wolvesChasing.Add(this.myBrain); sheepMemory.SetValue("chasedBy", wolvesChasing); } //send signal to other wolf in its sensing radius, tell them to change to hunting phase if (controller.memory.GetValue<float>("leaderLevel") >= highestLeaderLevel) { //increase its leaderLevel whenever it issue a decision to hunt if (controller.memory.GetValue<float>("leaderLevel") < 100f) { controller.memory.SetValue("leaderLevel", controller.memory.GetValue<float>("leaderLevel") + increaseLeaderLevel); } //set the maximum leaderLevel for wolf if (controller.memory.GetValue<float>("leaderLevel") > 100f) { controller.memory.SetValue("leaderLevel", 100f); } //call other to change to hunting phase foreach (SensedObject objWolf in seenWolf) { //give out command to attack the same target Memory wolfMemory = objWolf.getMemory(); wolfMemory.SetValue("hasCommand", target); Debug.Log("I'm the leader! I sent command!"); } } //Change to hunting phasemyBrain.memory.SetValue ("watched", myBrain.memory.GetValue<float>("watched") - watchedLevelDecay * Time.deltaTime); Debug.Log("I'm hunting. Target: " + controller.memory.GetValue<SensedObject>("hasCommand").getObject()); } } } } if (controller.memory.GetValue<SensedObject>("hasCommand") != null) { time = 0f; //decrease its hungryLevel when it change to hunting state //controller.memory.SetValue("hungryLevel", controller.memory.GetValue<float>("hungryLevel") - 4f); mainMachine.RequestStateTransition(hunting.GetTarget()); } else { if (time >= 20f) //wait for 20 sec { //decrease its leaderLevel if it can't find any sheep or cant issue and command if (controller.memory.GetValue<float>("leaderLevel") > 10f) { controller.memory.SetValue("leaderLevel", controller.memory.GetValue<float>("leaderLevel") - (decayLeaderLevel / myBrain.memory.GetValue<float>("ferocity"))); } if (controller.memory.GetValue<float>("hungryLevel") > 0f) { myBrain.memory.SetValue("hungryLevel", controller.memory.GetValue<float>("hungryLevel") - (decayHungryLevel * (myBrain.memory.GetValue<float>("ferocity") / 5))); } //set the minimum leaderLevel for wolf if (controller.memory.GetValue<float>("leaderLevel") < 10f) { controller.memory.SetValue("leaderLevel", 10f); } //set the minimum hungryLevel for wolf if (controller.memory.GetValue<float>("hungryLevel") < 0f) { controller.memory.SetValue("hungryLevel", 0f); Debug.Log("I died because I'm too hungry"); myBrain.getGameObject().SetActiveRecursively(false); controller.memory.GetValue<Genome>("Genome").manager.SpawnWolf(controller.memory.GetValue<Vector2>("StartPoint")); } } else { time += Time.deltaTime; } } yield return null; }
public override IEnumerator Run(Brain controller) { bool thereIsSheperd = false; List<SensedObject> seenWolf = new List<SensedObject>(); Transform playerTrans = null; float averagePanicLevel = controller.memory.GetValue<float>("Panic"); int totalSheep = 1; foreach (SensedObject obj in controller.senses.GetSensedObjects()) { if (obj.getAgentType().Equals(AgentClassification.Wolf)) { seenWolf.Add(obj); } if (obj.getAgentType().Equals(AgentClassification.Shepherd)) { playerTrans = obj.getObject().transform; thereIsSheperd = true; } // Let the the crowd influence the mood of individual sheep. This will make single scared sheep less panicky, but allow mass confusion to ensue given enough panic. if (obj.getAgentType() == AgentClassification.Sheep) { averagePanicLevel += obj.getObject().GetComponent<Brain>().memory.GetValue<float>("Panic"); totalSheep++; } } //get current BeaconInfo if (controller.memory.GetValue<BeaconInfo>("LastBeacon") != null) { if (curBeacon != null) { if (curBeacon.GetTime() < controller.memory.GetValue<BeaconInfo>("LastBeacon").GetTime()) { curBeacon = controller.memory.GetValue<BeaconInfo>("LastBeacon"); } } else { curBeacon = controller.memory.GetValue<BeaconInfo>("LastBeacon"); } } if (curBeacon != null) { //StopCurBeacon++; //beaconHelper = 0.2f; //if (StopCurBeacon==1) //{ //if(Time.time - curBeacon.GetTime() <= Time.deltaTime) //{ controller.memory.SetValue("cowardLevel", controller.memory.GetValue<float>("cowardLevel") - beaconHelper); controller.memory.SetValue<float>("Panic", 0f); //Debug.Log(controller.getGameObject() + ": " + (controller.memory.GetValue<float>("cowardLevel"))); if (controller.memory.GetValue<float>("cowardLevel") <= 0f) { controller.memory.SetValue("cowardLevel", 0.01f); } //} //} //delete curBeacon curBeacon = null; controller.memory.SetValue("LastBeacon", null); } if (thereIsSheperd) { Vector2 playerPos = new Vector2(playerTrans.position.x, playerTrans.position.z); Vector2 playerFacing = new Vector2(playerTrans.forward.x, playerTrans.forward.z); Vector2 positionOffset = myBrain.legs.getPosition() - playerPos; //decrease cowardLevel when there is sheperd looking at it if (Vector2.Dot(playerFacing.normalized, positionOffset.normalized) > 0.71f) { controller.memory.SetValue("cowardLevel", controller.memory.GetValue<float>("cowardLevel") - (Time.deltaTime * 0.02f)); if (controller.memory.GetValue<float>("cowardLevel") <= 0f) { controller.memory.SetValue("cowardLevel", 0.01f); } } //normalise cowardLevel back to normal if Sheperd isn't looking else { controller.memory.SetValue("cowardLevel", controller.memory.GetValue<float>("cowardLevel") + (Time.deltaTime * 0.01f)); if (controller.memory.GetValue<float>("cowardLevel") >= 0.5f) { controller.memory.SetValue("cowardLevel", 0.5f); } } } else { //decrease cowardLevel when there are more than 4 sheep around it. if (totalSheep >= 4) { controller.memory.SetValue("cowardLevel", controller.memory.GetValue<float>("cowardLevel") - (Time.deltaTime * 0.01f)); if (controller.memory.GetValue<float>("cowardLevel") <= 0f) { controller.memory.SetValue("cowardLevel", 0.01f); } } //normalise cowardLevel to its default value else { if (controller.memory.GetValue<float>("cowardLevel") > 0.5f) { controller.memory.SetValue("cowardLevel", controller.memory.GetValue<float>("cowardLevel") - (Time.deltaTime * 0.01f)); if (controller.memory.GetValue<float>("cowardLevel") <= 0.5f) { controller.memory.SetValue("cowardLevel", 0.5f); } } else { controller.memory.SetValue("cowardLevel", controller.memory.GetValue<float>("cowardLevel") + (Time.deltaTime * 0.01f)); if (controller.memory.GetValue<float>("cowardLevel") >= 0.5f) { controller.memory.SetValue("cowardLevel", 0.5f); } } } } averagePanicLevel /= totalSheep; controller.memory.SetValue<float>("Panic", Mathf.Lerp(controller.memory.GetValue<float>("Panic"), averagePanicLevel, Time.deltaTime * 0.3f)); foreach(SensedObject obj in seenWolf) { Legs wolfLeg = (Legs)obj.getObject().GetComponent<Legs>(); Vector2 wolfFacing = wolfLeg.getVelocity(); Vector2 sheepPos = myBrain.legs.getPosition(); Vector2 wolfPos = wolfLeg.getPosition(); float dot = Vector2.Dot(wolfFacing, sheepPos - wolfPos); float distance = Vector2.Distance(sheepPos, wolfPos); float scariness = (4 / distance) * (dot + 1); controller.memory.SetValue<float>("Panic", controller.memory.GetValue<float>("Panic") + (Time.deltaTime * scariness * increasePanicRate * controller.memory.GetValue<float>("cowardLevel"))); } if (thereIsSheperd) { //the less cowardLevel is, the more Panic decreases controller.memory.SetValue<float>("Panic", controller.memory.GetValue<float>("Panic") - (Time.deltaTime * decayPanicRate * shepherdInfluence * (1 - controller.memory.GetValue<float>("cowardLevel")))); time = 0f; } else { if (time > 5f) { //the less cowardLevel is, the more Panic decreases controller.memory.SetValue<float>("Panic", controller.memory.GetValue<float>("Panic") - (Time.deltaTime * decayPanicRate * (1 - controller.memory.GetValue<float>("cowardLevel")))); } else { time += Time.deltaTime; } } //set the minimum Panic level for sheep if (controller.memory.GetValue<float>("Panic") < 0f) { controller.memory.SetValue<float>("Panic", 0f); } if (thereIsSheperd) { //set the target arriveBehaviour.setTarget(GameObject.FindGameObjectWithTag("Player")); //set the weight, this is top priority arriveBehaviour.setWeight(arriveBehaviour.getWeight() + Time.deltaTime * increaseFollowRate); //set maximum weight- the sheep stop following if they are afraid (the player must catch them) float maxWeight = (fleeThreshold - controller.memory.GetValue<float>("Panic")) + 5; //plus 5 to make the sheep more responsible to follow the Sheperd if (arriveBehaviour.getWeight() > maxWeight) { arriveBehaviour.setWeight(maxWeight); } oldPlayerPosition = arriveBehaviour.getTarget(); //if the sheep sees the shephered, it stops seeking seekBehaviour.setWeight(seekBehaviour.getWeight() - (Time.deltaTime * decayFollowRate)); //minimum 0 weight if (seekBehaviour.getWeight() < 0f) { seekBehaviour.setWeight(0f); } seekingTime = 0f; } else { arriveBehaviour.setWeight(arriveBehaviour.getWeight() - (Time.deltaTime * decayFollowRate)); //get last player's location so the sheep will seek around that seekBehaviour.setTarget(oldPlayerPosition); seekBehaviour.setWeight(seekBehaviour.getWeight() + (Time.deltaTime * decayFollowRate)); float maxWeight = fleeThreshold - controller.memory.GetValue<float>("Panic"); if (arriveBehaviour.getWeight() > maxWeight) { arriveBehaviour.setWeight(maxWeight); } if (seekingTime > 7f) { seekBehaviour.setWeight(0f); } else { seekingTime += Time.deltaTime; } //set minimum weight if (arriveBehaviour.getWeight() < 0f) { arriveBehaviour.setWeight(0f); seekBehaviour.setWeight(seekBehaviour.getWeight() - Time.deltaTime); if (seekBehaviour.getWeight() < 0f) { seekBehaviour.setWeight(0f); } } } // if panic level larger than a given threshold, change to running state. if (controller.memory.GetValue<float>("Panic") >= fleeThreshold) { Debug.Log("I saw wolf. RUN!: " + myBrain.getGameObject()); mainMachine.RequestStateTransition(running.GetTarget()); } //if the sheep get caught if (controller.memory.GetValue<bool>("BeingEaten") == true) { mainMachine.RequestStateTransition(eaten.GetTarget()); } // Debug.Log("Hey" +controller.memory.GetValue<BeaconInfo>("LastBeacon").ToString()); yield return null; }
private void DidLoseBeacon(BeaconInfo lostBeaconInfo) { }
private async Task DidFindBeacon(BeaconInfo beaconInfo) { await BeaconsService.Instance.ActivateEddystoneUid(beaconInfo.BeaconID.ToString()); }
private void DidUpdateBeacon(BeaconInfo beaconInfo) { }
public override void DiscoveredPeripheral(CBCentralManager central, CBPeripheral peripheral, NSDictionary advertisementData, NSNumber RSSI) { if (advertisementData.ContainsKey(CBAdvertisement.DataServiceDataKey)) { var tmp = advertisementData.ObjectForKey(CBAdvertisement.DataManufacturerDataKey); var serviceData = advertisementData.ValueForKey(CBAdvertisement.DataServiceDataKey) as NSDictionary; var eft = BeaconInfo.FrameTypeForFrame(serviceData); if (eft == EddystoneFrameType.TelemetryFrameType) { if (!_deviceIDCache.ContainsKey(peripheral.Identifier)) { _deviceIDCache.Add(peripheral.Identifier, BeaconInfo.TelemetryDataForFrame(serviceData)); } } else if (eft == EddystoneFrameType.UIDFrameType || eft == EddystoneFrameType.EIDFrameType) { if (peripheral.Identifier != null && _deviceIDCache.ContainsKey(peripheral.Identifier)) { var telemetry = _deviceIDCache[peripheral.Identifier]; var rssi = RSSI.Int32Value; var beaconInfo = (eft == EddystoneFrameType.UIDFrameType ? BeaconInfo.BeaconInfoForUIDFrameData(serviceData, telemetry, rssi) : BeaconInfo.BeaconInfoForEIDFrameData(serviceData, telemetry, rssi)); if (beaconInfo != null) { _deviceIDCache.Remove(peripheral.Identifier); if (_seenEddystoneCache.ContainsKey(new NSString(beaconInfo.BeaconID.Description))) { var timer = _seenEddystoneCache.ObjectForKey(new NSString(beaconInfo.BeaconID.Description + "_onLostTimer")) as NSTimer; timer.Invalidate(); timer = NSTimer.CreateScheduledTimer(_onLostTimeout, t => { var cacheKey = beaconInfo.BeaconID.Description; if (_seenEddystoneCache.ContainsKey(new NSString(cacheKey))) { var lostBeaconInfo = _seenEddystoneCache.ObjectForKey(new NSString(cacheKey)) as BeaconInfo; if (lostBeaconInfo != null) { _seenEddystoneCache.Remove(new NSString(beaconInfo.BeaconID.Description)); _seenEddystoneCache.Remove(new NSString(beaconInfo.BeaconID.Description + "_onLostTimer")); DidLoseBeacon(lostBeaconInfo); } } }); DidUpdateBeacon(beaconInfo); } else { DidFindBeacon(beaconInfo); var timer = NSTimer.CreateScheduledTimer(_onLostTimeout, t => { var cacheKey = beaconInfo.BeaconID.Description; if (_seenEddystoneCache.ContainsKey(new NSString(cacheKey))) { var lostBeaconInfo = _seenEddystoneCache.ObjectForKey(new NSString(cacheKey)) as BeaconInfo; if (lostBeaconInfo != null) { _seenEddystoneCache.Remove(new NSString(beaconInfo.BeaconID.Description)); _seenEddystoneCache.Remove(new NSString(beaconInfo.BeaconID.Description + "_onLostTimer")); DidLoseBeacon(lostBeaconInfo); } } }); _seenEddystoneCache.SetValueForKey(beaconInfo, new NSString(beaconInfo.BeaconID.Description)); _seenEddystoneCache.SetValueForKey(timer, new NSString(beaconInfo.BeaconID.Description + "_onLostTimer")); } } } } else if (eft == EddystoneFrameType.URLFrameType) { var rssi = RSSI.Int32Value; var url = BeaconInfo.ParseUrlFromFrame(serviceData); var name = peripheral.Name; var services = peripheral.Services; DidObserveURLBeacon(url, rssi); } else { MvxTrace.TaggedTrace(MvxTraceLevel.Diagnostic, "Beacons", "Unable to find service data; can't process Eddystone"); } } else { } }