void Filter() { //using my@(Beliefs,Intentions,Desires) update myIntentions //Get my biggest desires var biggestDesireValue = 0.0f; foreach (var desire in myDesires.Keys) { if(myDesires [desire] > biggestDesireValue) { biggestDesireValue = myDesires [desire]; } } //All the biggest desire IList<Desire> myBiggestDesires = new List<Desire>() ; foreach (var desire in myDesires.Keys) { if(myDesires [desire] == biggestDesireValue) { myBiggestDesires.Add(desire); } } //Best intention IList<IntentionDetails> myIntentions = RetrieveIntentionsFromDesires (myBiggestDesires); myCurrentIntention = null; float best_weight = 0.0f; foreach(var intentionDetail in myIntentions) { float weight = intentionDetail.Weight (); if(weight > best_weight) { best_weight = weight; myCurrentIntention = intentionDetail; } } }
IList<IntentionDetails> RetrieveIntentionsFromDesires(IList<Desire> desires) { IList<IntentionDetails> myCurrentIntentions = new List<IntentionDetails> () ; foreach (var desire in desires) { if (desire == Desire.HELP_SELF) { float eat_food_weight = 1.0f; if (EnemyAhead () && enemy != null) { IntentionDetails intention = new IntentionDetails(Intention.ATTACK_MONSTER_AT,1.0f,enemy.transform.position); myCurrentIntentions.Add (intention); continue; } //FIXME /*else if (EnemyOnSight ()) { IntentionDetails intention = new IntentionDetails(Intention.ATTACK_MONSTER_AT,1f,enemyOnSight.transform.position); myCurrentIntentions.Add (intention); eat_food_weight = 0.5f; }*/ if (HasFood() && HasLowLife ()) { IntentionDetails intention = new IntentionDetails(Intention.EAT_FOOD,1.0f*eat_food_weight,this.transform.position); myCurrentIntentions.Add (intention); continue; } float colony_multiplier = 0.9f; //Food source is always a better option if(KnowWhereFoodSourceIs ()) { Vector3 closestFoodSource = ClosestFoodSource (); float distance_to_foodsource = DistanceBetweenMeAndPoint(closestFoodSource); float distance_to_col = DistanceBetweenMeAndPoint(myColonyPosition); float weight = 0.0f; if (distance_to_foodsource < distance_to_col) { weight = 0.9f; IntentionDetails intention = new IntentionDetails(Intention.GET_FOOD_AT,1.0f*weight,myColonyPosition); myCurrentIntentions.Add (intention); continue; } } else if(KnowWhereSpecialFoodIs ()) { Vector3 closestFood = ClosestSpecialFood (); float distance_to_food = DistanceBetweenMeAndPoint(closestFood); float distance_to_col = DistanceBetweenMeAndPoint(myColonyPosition); float weight = 0.0f; if (distance_to_food < distance_to_col) { weight = 0.9f; //colony_multiplier = 0.5f; IntentionDetails intention = new IntentionDetails(Intention.GET_FOOD_AT,1.0f*weight,myColonyPosition); myCurrentIntentions.Add (intention); continue; } } else if (KnowWhereFoodIs ()) { Vector3 closestFood = ClosestFood (); float distance_to_food = DistanceBetweenMeAndPoint(closestFood); float distance_to_col = DistanceBetweenMeAndPoint(myColonyPosition); float weight = 0.0f; if (distance_to_food < distance_to_col) { weight = 0.9f; //colony_multiplier = 0.5f; IntentionDetails intention = new IntentionDetails(Intention.GET_FOOD_AT,1.0f*weight,myColonyPosition); myCurrentIntentions.Add (intention); //continue; } } IntentionDetails intention_details = new IntentionDetails(Intention.GOTO_COL_AT,1.0f*colony_multiplier, myColonyPosition); myCurrentIntentions.Add (intention_details); } else if (desire == Desire.POPULATE) { float weight = 0.8f; float distance_to_col = Mathf.Sqrt(DistanceBetweenMeAndPoint(myColonyPosition)); float delta = 4.0f; if (distance_to_col < delta || AtBase ()) { weight = 0.9f; } IntentionDetails intention = new IntentionDetails(Intention.POPULATE_AT, weight, myColonyPosition); myCurrentIntentions.Add (intention); } else if (desire == Desire.DEFEND_COL) { float defend_col_multiplier = 1.0f; float number_of_ind_at_base = HowManyAtBase (); //The more there are at base the less I want to go there if (number_of_ind_at_base > 0 && !HasFood ()) { defend_col_multiplier = defend_col_multiplier / number_of_ind_at_base; } IntentionDetails intention = new IntentionDetails(Intention.ATTACK_MONSTER_AT, 1f*defend_col_multiplier, myColonyPosition); myCurrentIntentions.Add (intention); } else if (desire == Desire.GET_FOOD) { //FIXME bool knowWhereSpecialFoodIs = KnowWhereSpecialFoodIs (); if (knowWhereSpecialFoodIs) { float special_food_multiplier = 0.9f; Vector3 closestSpecialFood = ClosestSpecialFood (); bool canMakeToSpecFood = CanMakeItThere(closestSpecialFood); if(canMakeToSpecFood) { IntentionDetails intention = new IntentionDetails(Intention.GET_FOOD_AT, 1f*special_food_multiplier, closestSpecialFood); myCurrentIntentions.Add (intention); continue; } } //Only goes when there is not food on sight nor ahead bool knowWhereFoodSourceIs = KnowWhereFoodSourceIs (); if (knowWhereFoodSourceIs && !FoodOnSight () && !FoodAhead ()) { float foodsource_mul = 0.9f; Vector3 closestFoodSource = ClosestFoodSource (); bool canMakeToFoodSource = CanMakeItThere(closestFoodSource); if(canMakeToFoodSource) { IntentionDetails intention = new IntentionDetails(Intention.GOTO_FOODSOURCE_AT, 1f*foodsource_mul, closestFoodSource); myCurrentIntentions.Add (intention); continue; } } bool knowWhereFoodIs = KnowWhereFoodIs (); bool knowWhereObsIs = KnowWhereObstacleIs (); if (knowWhereFoodIs) { float food_mul = 0.9f; Vector3 closestFood = ClosestFood (); bool canMakeToFood = CanMakeItThere(closestFood); if(canMakeToFood) { if ((!knowWhereObsIs) || (knowWhereObsIs && !HasHighLife ())) { IntentionDetails intention = new IntentionDetails(Intention.GET_FOOD_AT, 1f*food_mul, closestFood); myCurrentIntentions.Add (intention); continue; } } } if (knowWhereObsIs){ Vector3 closestObstacle = ClosestObstacle (); bool canMakeToObs = CanMakeItThere(closestObstacle); if(canMakeToObs) { IntentionDetails intention = new IntentionDetails(Intention.DESTROY_WALL_AT, 0.9f, closestObstacle); myCurrentIntentions.Add (intention); continue; } } //Search for food randomly IntentionDetails intention_det = new IntentionDetails(Intention.SEARCH_FOOD, 0.7f, MoveRandomly()); myCurrentIntentions.Add (intention_det); } else { //desire == Desire.HELP_OTHER float help_multiplier = 1.0f; /*if(HasLowLife ()) { help_multiplier = 0.9f; }*/ IntentionDetails intention = new IntentionDetails(Intention.HELP_OTHER_AT, 1f*help_multiplier, help_position); myCurrentIntentions.Add (intention); } } return myCurrentIntentions; }
//bool help_request; void Awake() { navMeshAgent = this.GetComponent<NavMeshAgent> (); endOfWorld = false; foodAhead = false; hasFood = false; atBase = false; enemyAhead = false; agentAhead = false; isFoodOnSight = false; isEnemyOnSight = false; isObstacleOnSight = false; isSpecFoodOnSight = false; currentPlan = null; currentActionHasEnded = false; //help_request = false; currentAction = null; //myColor = transform.GetChild (0).GetChild (0).gameObject.GetComponent<Renderer> ().material.color; myDesires = new Dictionary<Desire,float> (); InitializeDesires (); commModule = GetComponent<ComunicationModule>(); Vector3 moveRand = MoveRandomly (); myCurrentIntention = new IntentionDetails(Intention.SEARCH_FOOD, 1f, moveRand); myBeliefs = new Dictionary<Vector3,string> (new Vector3Comparer()); }