// put element to heap public void Put(HPNode n) { // find the appropriate place for element // start as new leaf and moves up accordingly int currentNode = ++this.size; while (currentNode != 1 // not at root && heap[currentNode / 2].cost > n.cost // current node is > new node ) { heap[currentNode] = heap[currentNode / 2]; // move current node down (cos its greater) currentNode /= 2; // move to the parent of current } // we found a place heap[currentNode] = n; }
// put element to heap public void Put(HPNode n) { // find the appropriate place for element // start as new leaf and moves up accordingly int currentNode = ++this.size; while (currentNode != 1 && // not at root heap[currentNode / 2].cost > n.cost // current node is > new node ) { heap[currentNode] = heap[currentNode / 2]; // move current node down (cos its greater) currentNode /= 2; // move to the parent of current } // we found a place heap[currentNode] = n; }
// remove the root (min element) public HPNode RemoveMin() { // if empty return null if (this.size == 0) { return(null); } HPNode minElement = heap[1]; // save min elem // --- re-heapify HPNode lastElement = heap[size--]; // find place for lastElement starting from root int currentNode = 1, // start at root child = 2; // child of currentNode while (child <= size) { // heap[child] should be < child of currentNode if (child < size && heap[child].cost > heap[child + 1].cost ) { child++; } // can we put lastElem in heap[currentNode]? if ( lastElement.cost <= heap[child].cost ) { break; //yes } // no heap[currentNode] = heap[child]; // move child up currentNode = child; // move currentNode down child *= 2; } heap[currentNode] = lastElement; return(minElement); }
private Point GetInjectionPoint() { HPNodeMinHeap hpHeap = new HPNodeMinHeap(this.Tissue.GetEntitiesByType(EntityEnum.HoshimiPoint).Count); int radius = 200; foreach (Entity e in this.Tissue.GetEntitiesByType(EntityEnum.HoshimiPoint)) { HPNode hp = new HPNode(0, new Point(e.X, e.Y)); // buat HPNode baru // hitung area cost if (this.Tissue[hp.Loc.X, hp.Loc.Y].AreaType == AreaEnum.LowDensity) { hp.cost -= 5; } else if (this.Tissue[hp.Loc.X, hp.Loc.Y].AreaType == AreaEnum.MediumDensity) { hp.cost -= 1; } else if (this.Tissue[hp.Loc.X, hp.Loc.Y].AreaType == AreaEnum.HighDensity) { hp.cost += 3; } /* * // perhitungkan PierreIP * foreach (InjectionPointInfo ipInfo in OtherInjectionPointsInfo) * { * if (ipInfo.PlayerID == 0) * if (MyAI.squareDist(ipInfo.Location, hp.Loc) <= radius) * hp.cost += 500; * }*/ // cek sekitar hp ini Point p; int numAzn = 0; int numHp = 0; foreach (Entity entity in this.Tissue.GetEntitiesByType(EntityEnum.AZN)) { p = new Point(entity.X, entity.Y); if (MyAI.squareDist(hp.Loc, p) <= radius) //if (Global.GetPathLength(hp.Loc, p) <= radius) { numAzn++; if (numAzn == 1) { hp.cost -= 8; } else { hp.cost -= 1; } } p = Point.Empty; } foreach (Entity entity in this.Tissue.GetEntitiesByType(EntityEnum.HoshimiPoint)) { if (e.X == hp.Loc.X && e.Y == hp.Loc.Y) { continue; // skip if it is here } p = new Point(entity.X, entity.Y); if (MyAI.squareDist(hp.Loc, p) <= radius) //if (Global.GetPathLength(hp.Loc, p) <= radius) { numHp++; if (numHp == 1) { hp.cost -= 1; } else if ((numHp <= 4) && (numHp > 1)) { hp.cost -= 3; } else { hp.cost -= 5; } } p = Point.Empty; } foreach (BaseObjective baseObj in Mission.Objectives) { if (baseObj is UniqueNavigationObjective) { /*foreach (NavPoint navp in ((UniqueNavigationObjective)baseObj).NavPoints) * { * //if (MyAI.squareDist(hp.Loc, navp.Location) <= (radius * 3)) * if (navp.EndTurn <= 500) * { * if (Global.GetPathLength(hp.Loc, navp.Location) < 100) * hp.cost -= 5; * else * hp.cost += 10; * } * }*/ if (Global.GetPathLength(hp.Loc, ((UniqueNavigationObjective)baseObj).NavPoints[0].Location) < 100) { if (((UniqueNavigationObjective)baseObj).NavPoints[0].EndTurn <= 500) { hp.cost -= 5; } } else { hp.cost += 10; } } else if (baseObj is NavigationObjective) { foreach (NavPoint navp in ((NavigationObjective)baseObj).NavPoints) { //if (MyAI.squareDist(hp.Loc, navp.Location) <= (radius * 3)) if (navp.EndTurn <= 500) { if (Global.GetPathLength(hp.Loc, navp.Location) < 100) { hp.cost -= 5; } else { hp.cost += 10; } } } } } //hp.cost += (int)(hp.cost * 0.001f); hpHeap.Put(hp); // masukkan ke heap } return(hpHeap.GetMin().Loc); // ambil minimum element }
private Point GetInjectionPoint() { HPNodeMinHeap hpHeap = new HPNodeMinHeap(this.Tissue.GetEntitiesByType(EntityEnum.HoshimiPoint).Count); int radius = 200; foreach (Entity e in this.Tissue.GetEntitiesByType(EntityEnum.HoshimiPoint)) { HPNode hp = new HPNode(0, new Point(e.X, e.Y)); // buat HPNode baru // hitung area cost if (this.Tissue[hp.Loc.X, hp.Loc.Y].AreaType == AreaEnum.LowDensity) hp.cost -= 5; else if (this.Tissue[hp.Loc.X, hp.Loc.Y].AreaType == AreaEnum.MediumDensity) hp.cost -= 1; else if (this.Tissue[hp.Loc.X, hp.Loc.Y].AreaType == AreaEnum.HighDensity) hp.cost += 3; /* // perhitungkan PierreIP foreach (InjectionPointInfo ipInfo in OtherInjectionPointsInfo) { if (ipInfo.PlayerID == 0) if (MyAI.squareDist(ipInfo.Location, hp.Loc) <= radius) hp.cost += 500; }*/ // cek sekitar hp ini Point p; int numAzn = 0; int numHp = 0; foreach (Entity entity in this.Tissue.GetEntitiesByType(EntityEnum.AZN)) { p = new Point(entity.X, entity.Y); if (MyAI.squareDist(hp.Loc, p) <= radius) //if (Global.GetPathLength(hp.Loc, p) <= radius) { numAzn++; if (numAzn == 1) hp.cost -= 8; else hp.cost -= 1; } p = Point.Empty; } foreach (Entity entity in this.Tissue.GetEntitiesByType(EntityEnum.HoshimiPoint)) { if (e.X == hp.Loc.X && e.Y == hp.Loc.Y) continue; // skip if it is here p = new Point(entity.X, entity.Y); if (MyAI.squareDist(hp.Loc, p) <= radius) //if (Global.GetPathLength(hp.Loc, p) <= radius) { numHp++; if (numHp == 1) hp.cost -= 1; else if ((numHp <= 4) && (numHp > 1)) hp.cost -= 3; else hp.cost -= 5; } p = Point.Empty; } foreach (BaseObjective baseObj in Mission.Objectives) { if (baseObj is UniqueNavigationObjective) { /*foreach (NavPoint navp in ((UniqueNavigationObjective)baseObj).NavPoints) { //if (MyAI.squareDist(hp.Loc, navp.Location) <= (radius * 3)) if (navp.EndTurn <= 500) { if (Global.GetPathLength(hp.Loc, navp.Location) < 100) hp.cost -= 5; else hp.cost += 10; } }*/ if (Global.GetPathLength(hp.Loc, ((UniqueNavigationObjective)baseObj).NavPoints[0].Location) < 100) { if (((UniqueNavigationObjective)baseObj).NavPoints[0].EndTurn <= 500) hp.cost -= 5; } else { hp.cost += 10; } } else if (baseObj is NavigationObjective) { foreach (NavPoint navp in ((NavigationObjective)baseObj).NavPoints) { //if (MyAI.squareDist(hp.Loc, navp.Location) <= (radius * 3)) if (navp.EndTurn <= 500) { if (Global.GetPathLength(hp.Loc, navp.Location) < 100) hp.cost -= 5; else hp.cost += 10; } } } } //hp.cost += (int)(hp.cost * 0.001f); hpHeap.Put(hp); // masukkan ke heap } return hpHeap.GetMin().Loc; // ambil minimum element }