public override IEnumerator Enter(Machine owner, Brain controller) { mainMachine = owner; myBrain = controller; Legs myLeg = myBrain.legs; //get sheep target sheepTarget = myBrain.memory.GetValue<SensedObject>("hasCommand"); sheepBrain = (Brain)sheepTarget.getObject().GetComponent("Brain"); sheepMemory = sheepBrain.memory; sheepMemory.SetValue("BeingEaten", true); arriveBehaviour = new Arrive(); arriveBehaviour.Init(myLeg); arriveBehaviour.setTarget(sheepTarget.getObject()); //speed is zero myBrain.legs.maxSpeed = 0f; Debug.Log("I'm eating:" + sheepTarget.getObject() + " HP: " + sheepMemory.GetValue<float>("HP")); yield return null; }
int Scariest(SensedObject wolf1, SensedObject wolf2) { Legs wolfLeg1 = wolf1.getObject().GetComponent<Brain>().legs; Vector2 sheepPos = myBrain.legs.getPosition(); Vector2 wolfPos1 = wolfLeg1.getPosition(); float distance1 = Vector2.Distance(sheepPos, wolfPos1); Vector2 wolfFacing1 = new Vector2(wolfLeg1.transform.forward.x, wolfLeg1.transform.forward.z); float relativeVelocity1 = Vector2.Dot(wolfFacing1, sheepPos - wolfPos1); float scariness1 = (1 / distance1) * (relativeVelocity1 + 1); Legs wolfLeg2 = wolf2.getObject().GetComponent<Brain>().legs; Vector2 wolfPos2 = wolfLeg2.getPosition(); float distance2 = Vector2.Distance(sheepPos, wolfPos2); Vector2 wolfFacing2 = new Vector2(wolfLeg2.transform.forward.x, wolfLeg2.transform.forward.z); float relativeVelocity2 = Vector2.Dot(wolfFacing2, sheepPos - wolfPos2); float scariness2 = (1 / distance2) * (relativeVelocity2 + 1); if(scariness1 > scariness2) { return 1; } if(scariness1 < scariness2) { return -1; } return 0; }
public override IEnumerator Enter(Machine owner, Brain controller) { mainMachine = owner; myBrain = controller; Legs myLeg = myBrain.legs; arriveBehaviour = new PathfindToPoint(); seekBehaviour = new Seek(); arriveBehaviour.Init(myLeg, myBrain.levelGrid); seekBehaviour.Init(myLeg); myLeg.addSteeringBehaviour(arriveBehaviour); myLeg.addSteeringBehaviour(seekBehaviour); myLeg.maxSpeed = 11f; time = 0f; ferocityRate = controller.memory.GetValue<float>("ferocity"); sheepTarget = myBrain.memory.GetValue<SensedObject>("hasCommand"); sheepMemory = sheepTarget.getMemory(); sheepBrain = (Brain)sheepTarget.getObject().GetComponent("Brain"); arriveBehaviour.setTarget(sheepTarget.getObject()); //for machine learning float panic = sheepBrain.memory.GetValue<float>("Panic"); float courage = sheepBrain.memory.GetValue<float>("cowardLevel"); float chasedBy = sheepBrain.memory.GetValue<List<Brain>>("chasedBy").Count; float distanceWithMe = Vector2.Distance(sheepBrain.legs.getPosition(), myBrain.legs.getPosition()); float distanceWithSheperd = Vector2.Distance(sheepBrain.legs.getPosition(), ((Legs)GameObject.FindGameObjectWithTag("Player").GetComponent<Legs>()).getPosition()); float hungry = myBrain.memory.GetValue<float>("hungryLevel"); float sheepHP = sheepBrain.memory.GetValue<float>("HP"); string sheep = panic + "," + courage + "," + chasedBy + "," + distanceWithMe + "," + distanceWithSheperd + "," + hungry + "," + sheepHP; myBrain.memory.SetValue("targeting", sheep); 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; }