/* * update all the ant operator * */ private void UpdateAntPopulation(AntPopulation pAntPopulation) { Ant[] tAnts = pAntPopulation.mAnts; for (int i = 0; i < tAnts.Length; i++) { tAnts [i].mPositionIndex = InfluenceMap.getInstance().getTilefromPosition(new Vector2(tAnts[i].mAIRT.transform.position.x, tAnts[i].mAIRT.transform.position.z)); tAnts [i].Add(tAnts[i].mPositionIndex); if (tAnts [i].mInfoFood > 0.0f) { tAnts [i].mAIRT.GetComponent <MeshRenderer> ().material.color = Color.green; } else if (tAnts [i].mInfoHome > 0.0f) { tAnts [i].mAIRT.GetComponent <MeshRenderer> ().material.color = Color.blue; } else { tAnts [i].mAIRT.GetComponent <MeshRenderer> ().material.color = Color.red; } // update ant with finding food logic or finding home logic if (tAnts [i].mNowTarget == NowTarget.FOOD) { OnFood(tAnts[i], pAntPopulation); } else if (tAnts [i].mNowTarget == NowTarget.HOME) { OnHome(tAnts[i], pAntPopulation); } } }
public static float AStarByRiskRate(Vector3 target, Vector3 des) { Vector2 v = InfluenceMap.getInstance().getTilefromPosition(new Vector2(target.x, target.z)); float f = InfluenceMap.getInstance().getData(v, "Risk"); return(f); }
public override void Update(UEntity uEntity) { // Debug.Log ("UpdateAntSystem"); base.Update(uEntity); List <AntPopulation> tList = uEntity.GetComponent <AntPopulationComponent> ().mAntPopulationSet; for (int i = 0; i < AntPopulation.mNowListCount; i++) { if (tList [i].isStartFind) { if (tList[i].shouldWhat == 0) { InfluenceMap.getInstance().Show(3); } else if (tList[i].shouldWhat == 1) { InfluenceMap.getInstance().Show(4); } UpdateAntPopulation(tList [i]); } } }
public static float AStarByEulerDistance(Vector3 target, Vector3 des) { Vector2 vp = new Vector2(target.x - des.x, target.z - des.z); vp = new Vector2(vp.x / InfluenceMap.getInstance().deltaTileSize.x, vp.y / InfluenceMap.getInstance().deltaTileSize.y); return(vp.magnitude * 10); }
public override void Init() { base.Init(); string tWhere = mWhere; //Debug.Log (tWhere); InfluenceMap tInfluenceMap = InfluenceMap.getInstance(); int index = tInfluenceMap.mDictionary [tWhere]; bool isStatic = tInfluenceMap.mDicStatic [index]; if (isStatic) { Vector3 position = mGameObject.GetComponent <MeshRenderer> ().bounds.center; Vector3 size = mGameObject.GetComponent <MeshRenderer> ().bounds.size; Vector2 LeftDown = new Vector2(position.x - size.x / 2, position.z - size.z / 2); Vector2 tileLeftDown = InfluenceMap.getInstance().getTilefromPosition(LeftDown); Vector2 RightUp = new Vector2(position.x + size.x / 2, position.z + size.z / 2); Vector2 tileRightUp = InfluenceMap.getInstance().getTilefromPosition(RightUp); float IMdata = mIMComputer(mUEntity); for (int i = (int)tileLeftDown.x; i <= tileRightUp.x; i++) { for (int j = (int)tileLeftDown.y; j <= tileRightUp.y; j++) { Vector2 tTile = new Vector2(i, j); fillTile(tTile, index, IMdata); } } } }
public static float AStarByFriendRate(Vector3 target, Vector3 des) { Vector2 v = InfluenceMap.getInstance().getTilefromPosition(new Vector2(target.x, target.z)); float f = InfluenceMap.getInstance().getData(v, "friend"); //Debug.Log (f); return(-f * 0.3f); }
public static InfluenceMap getInstance() { if (mInfluenceMapsingleton == null) { mInfluenceMapsingleton = new InfluenceMap(); } return(mInfluenceMapsingleton); }
public override void Update(UEntity uEntity) { base.Update(uEntity); if (uEntity.GetComponent <LODComponent> () != null) { int lod = uEntity.GetComponent <LODComponent> ().mLOD; if (lod > LODComponent.maxLOD / 2) { return; } } string tWhere = uEntity.GetComponent <InfluenceMapTrigger> ().mWhere; InfluenceMap tInfluenceMap = InfluenceMap.getInstance(); int index = tInfluenceMap.mDictionary [tWhere]; bool isStatic = tInfluenceMap.mDicStatic [index]; if (!isStatic) { Vector3 position; Vector3 size; if (uEntity.GetComponent <InfluenceMapTrigger> ().mGameObject.GetComponent <MeshRenderer> ()) { position = uEntity.GetComponent <InfluenceMapTrigger>().mGameObject.GetComponent <MeshRenderer> ().bounds.center; size = uEntity.GetComponent <InfluenceMapTrigger>().mGameObject.GetComponent <MeshRenderer> ().bounds.size; } else { position = uEntity.GetComponent <InfluenceMapTrigger>().mGameObject.GetComponentInChildren <MeshRenderer> ().bounds.center; size = uEntity.GetComponent <InfluenceMapTrigger>().mGameObject.GetComponentInChildren <MeshRenderer> ().bounds.size; } Vector2 LeftDown = new Vector2(position.x - size.x / 2, position.z - size.z / 2); Vector2 tileLeftDown = InfluenceMap.getInstance().getTilefromPosition(LeftDown); Vector2 RightUp = new Vector2(position.x + size.x / 2, position.z + size.z / 2); Vector2 tileRightUp = InfluenceMap.getInstance().getTilefromPosition(RightUp); float IMdata = uEntity.GetComponent <InfluenceMapTrigger> ().mIMComputer(uEntity); float maxInfluence = uEntity.GetComponent <InfluenceMapTrigger> ().maxInfluence; for (int i = (int)tileLeftDown.x; i <= tileRightUp.x; i++) { for (int j = (int)tileLeftDown.y; j <= tileRightUp.y; j++) { Vector2 tTile = new Vector2(i, j); fillTile(tTile, index, IMdata, maxInfluence); } } } }
public static float AStarByTerrian(Vector3 target, Vector3 des) { Vector2 v = InfluenceMap.getInstance().getTilefromPosition(new Vector2(target.x, target.z)); float sum = 0.0f; for (int i = -2; i <= 2; i++) { for (int j = -2; j <= 2; j++) { float f = InfluenceMap.getInstance().getData(new Vector2(v.x + i, v.y + j), "Collision"); sum += f; } } sum /= 25.0f; return(-sum * 0.4f); }
// update the Pheromone private void UpdatePheromone(string code, float f, int radioriaus, Vector2 center, float max, int pGoingTile) { int index = InfluenceMap.getInstance().mDictionary [code]; int ti = Mathf.Clamp((int)center.x, 0, InfluenceMap.getInstance().wtileCount - 1); int tj = Mathf.Clamp((int)center.y, 0, InfluenceMap.getInstance().htileCount - 1); // update the Pheromone of this tile with the going distance if (pGoingTile < radioriaus) { InfluenceMap.getInstance().IMData[index] [ti] [tj] += f * (1 - (float)pGoingTile / (float)radioriaus); } if (InfluenceMap.getInstance().IMData [index] [ti] [tj] > max) { InfluenceMap.getInstance().IMData[index] [ti] [tj] = max; } }
private void fillTile(Vector2 tile, int index, float IMdata, float maxInfluence) { for (int i = (int)(tile.x - maxInfluence / 2); i <= (int)(tile.x + maxInfluence / 2); i++) { for (int j = (int)(tile.y - maxInfluence / 2); j <= (int)(tile.y + maxInfluence / 2); j++) { int ti = Mathf.Clamp(i, 0, InfluenceMap.getInstance().wtileCount - 1); int tj = Mathf.Clamp(j, 0, InfluenceMap.getInstance().htileCount - 1); Vector2 v; if (maxInfluence > 0.0005f) { v = new Vector2((ti - tile.x) / maxInfluence, (tj - tile.y) / maxInfluence); } else { v = new Vector2(0, 0); } InfluenceMap.getInstance().IMData[index] [ti] [tj] += IMdata * Gauss(v); } } }
/* * @ 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); }
public override void Init() { base.Init(); this.AddComponent <InfluenceMap> (InfluenceMap.getInstance()); }