// finding home logic private void OnHome(Ant pAnt, AntPopulation ap) { Vector2 v1 = new Vector2(pAnt.mAIRT.transform.position.x, pAnt.mAIRT.transform.position.z); Vector2 v2 = new Vector2(pAnt.mNowPositionTarget.x, pAnt.mNowPositionTarget.z); float dis = Vector2.Distance(v1, v2); //Debug.Log ("OnHome"); if (pAnt.mNowPositionTarget == Vector3.zero || dis < 0.35f) { if (pAnt.mInfoFood > 0.0f) { UpdatePheromone(ap.mInfluenceDistinationTag, pAnt.mInfoFood, ap.mRadiationRiaus, pAnt.mPositionIndex, 1600, pAnt.mClosedArray.Count); } GameObject g_target = FindTarget(pAnt.mAIRT.transform.position, pAnt.mObserverDistance, ap.mTagDistination); GameObject g_home = FindTarget(pAnt.mAIRT.transform.position, pAnt.mObserverDistance, ap.mTagHome); if (g_home != null) { pAnt.mInfoFood = -1.0f; pAnt.mInfoHome = 200.0f; pAnt.mNowTarget = NowTarget.FOOD; pAnt.mNowPositionTarget = g_home.transform.position; pAnt.mClosedArray.Clear(); pAnt.mLastDir = Vector2.zero; return; } if (g_target != null) { pAnt.mInfoFood = 200.0f; pAnt.mInfoHome = -1.0f; } pAnt.mNowPositionTarget = ComputeDirction(pAnt, ap); } else { Vector3 offset = pAnt.mNowPositionTarget - pAnt.mAIRT.transform.position; offset.Normalize(); offset.y = 0; pAnt.mAIRT.transform.Translate(offset * Time.deltaTime * 6); } }
/* * @ param pTagAnt:the tag of the Ant * @ param pTagHome: the tag of the home * @ param pTagDistination:the tag of the food * @ param ObserverDistance:the observer distance of the ant */ public void Init(string pTagAnt, string pTagHome, string pTagDistination, int ObserverDistance) { mInfluenceMap = InfluenceMap.getInstance(); GameObject[] mAntRTs = GameObject.FindGameObjectsWithTag(pTagAnt); mAnts = new Ant[mAntRTs.Length]; for (int i = 0; i < mAnts.Length; i++) { mAnts [i] = new Ant(); mAnts [i].mAIRT = mAntRTs [i]; mAnts [i].mObserverDistance = ObserverDistance; mAnts [i].mNowTarget = NowTarget.NONE; mAnts [i].mInfoFood = -1.0f; mAnts [i].mInfoHome = -1.0f; mAnts [i].mPositionIndex = mInfluenceMap.getTilefromPosition(new Vector2(mAntRTs[i].transform.position.x, mAntRTs[i].transform.position.z)); } mTagHome = pTagHome; mTagDistination = pTagDistination; mTagAnt = pTagAnt; mAntPopulationList.Add(this); mListIndex = mNowListCount; mNowListCount++; mInfluenceMap.AddConverage(0.0f, pTagAnt + pTagHome, false, 0.82f); mInfluenceMap.AddConverage(0.0f, pTagAnt + pTagDistination, false, 0.82f); }
// compute new dirction private Vector3 ComputeDirction(Ant pAnt, AntPopulation ap) { Vector3 result = Vector3.zero; Vector2 resultNineGrid = Vector2.zero; int index = -1; if (pAnt.mNowTarget == NowTarget.FOOD) { index = InfluenceMap.getInstance().mDictionary [ap.mInfluenceDistinationTag]; } else { index = InfluenceMap.getInstance().mDictionary [ap.mInfluenceHomeTag]; } float tMax = InfluenceMap.getInstance().getMax(pAnt.mPositionIndex, 1, index); // find the information if (tMax > 0.0f) { //Debug.Log (pAnt.mNowTarget.ToString()+" "+tMax+" infoFood:"+pAnt.mInfoFood+" infoHome"+pAnt.mInfoHome); float minusRate = 0.0002f; if (Random.Range(0.0f, 1.0f) < minusRate) { // just a little rate for random dir int RandomIndex = Random.Range(0, InfluenceMap.getInstance().NineGrid.Length); resultNineGrid = InfluenceMap.getInstance().NineGrid [RandomIndex]; if (pAnt.mLastDir != Vector2.zero) { resultNineGrid = pAnt.mLastDir; } float minusRate_ = 0.2f; if (Random.Range(0.0f, 1.0f) < minusRate_) { RandomIndex = Random.Range(0, InfluenceMap.getInstance().NineGrid.Length); resultNineGrid = resultNineGrid = InfluenceMap.getInstance().NineGrid [RandomIndex]; } int count = 0; while ((InfluenceMap.getInstance().isWall(resultNineGrid + pAnt.mPositionIndex) || pAnt.Contains(resultNineGrid + pAnt.mPositionIndex)) && count < 10) { RandomIndex = Random.Range(0, InfluenceMap.getInstance().NineGrid.Length); resultNineGrid = InfluenceMap.getInstance().NineGrid [RandomIndex]; count++; } } else { // compute the new dir by RW with rate float sum = 0.0f; float max = -1.0f; for (int i = 0; i < InfluenceMap.getInstance().NineGrid.Length; i++) { Vector2 delta = InfluenceMap.getInstance().NineGrid [i]; Vector2 real = pAnt.mPositionIndex + delta; if (real.x >= 0 && real.x < InfluenceMap.getInstance().wtileCount&&real.y >= 0 && real.y < InfluenceMap.getInstance().htileCount) { sum += InfluenceMap.getInstance().IMData [index] [(int)real.x] [(int)real.y]; } } int index1 = Random.Range(0, InfluenceMap.getInstance().NineGrid.Length); for (int i = 0; i < InfluenceMap.getInstance().NineGrid.Length; i++) { int realI = (i + index1) % InfluenceMap.getInstance().NineGrid.Length; Vector2 delta = InfluenceMap.getInstance().NineGrid [realI]; Vector2 real = pAnt.mPositionIndex + delta; float tRate = 0.0f; if (real.x >= 0 && real.x < InfluenceMap.getInstance().wtileCount&&real.y >= 0 && real.y < InfluenceMap.getInstance().htileCount) { tRate = InfluenceMap.getInstance().IMData [index] [(int)real.x] [(int)real.y] / sum; } if (Random.Range(0.0f, 1.0f) < tRate && !InfluenceMap.getInstance().isWall(delta + pAnt.mPositionIndex)) { resultNineGrid = new Vector2(delta.x, delta.y); break; } } } } else { // no find the information so the new dirtion is a random dir int RandomIndex = Random.Range(0, InfluenceMap.getInstance().NineGrid.Length); resultNineGrid = InfluenceMap.getInstance().NineGrid [RandomIndex]; if (pAnt.mLastDir != Vector2.zero) { resultNineGrid = pAnt.mLastDir; } float minusRate_ = 0.2f; if (Random.Range(0.0f, 1.0f) < minusRate_) { RandomIndex = Random.Range(0, InfluenceMap.getInstance().NineGrid.Length); resultNineGrid = resultNineGrid = InfluenceMap.getInstance().NineGrid [RandomIndex]; } int count = 0; while ((InfluenceMap.getInstance().isWall(resultNineGrid + pAnt.mPositionIndex) || pAnt.Contains(resultNineGrid + pAnt.mPositionIndex)) && count < 10) { RandomIndex = Random.Range(0, InfluenceMap.getInstance().NineGrid.Length); resultNineGrid = InfluenceMap.getInstance().NineGrid [RandomIndex]; count++; } //Debug.Log (pAnt.Contains(resultNineGrid+pAnt.mPositionIndex)); } pAnt.mLastDir = new Vector2(resultNineGrid.x, resultNineGrid.y); float tx = pAnt.mPositionIndex.x + resultNineGrid.x; float ty = pAnt.mPositionIndex.y + resultNineGrid.y; tx = Mathf.Clamp(tx, 0, InfluenceMap.getInstance().wtileCount - 1); ty = Mathf.Clamp(ty, 0, InfluenceMap.getInstance().htileCount - 1); Vector2 tile = new Vector2(tx, ty); // Debug.Log (pAnt.Contains(tile)); result = InfluenceMap.getInstance().GetPositionByTile(tile); return(result); }