public void AstarTest() { XmlReaderWriter reader = new XmlReaderWriter(); reader.ReadMe("..\\..\\tests\\test-ASTAR-anthill.xml"); AHGraphics.Init(); Astar.Init(AntHillConfig.mapColCount, AntHillConfig.mapRowCount); Simulation test_isw = new Simulation(new Map(AntHillConfig.mapColCount, AntHillConfig.mapRowCount, AntHillConfig.tiles)); Spider test_spider = new Spider(new Position(5, 0)); Ant test_ant1 = new Warrior(new Position(5, 8)); Ant test_ant2 = new Warrior(new Position(0, 3)); List <KeyValuePair <int, int> > trail = Astar.Search(new KeyValuePair <int, int>(test_spider.Position.X, test_spider.Position.Y), new KeyValuePair <int, int>(test_ant1.Position.X, test_ant1.Position.Y), new TestAstarObject(test_isw)); List <KeyValuePair <int, int> > test_trail1 = new List <KeyValuePair <int, int> >(); /* Laduje ponizsza mape, gdzie uzywam oznaczen: * S - spider; Q-Queen; 1,2 - Ants i standardowych... * <Map row="sssssSssss" /> * <Map row="sxooooooxs" /> * <Map row="sxooooooxs" /> * <Map row="2xooooooxs" /> * <Map row="sxooooooxs" /> * <Map row="sxooooooxs" /> * <Map row="sxoooQooxs" /> * <Map row="sxooooooxs" /> * <Map row="sxooo1ooxs" /> * <Map row="ssssssssss" /> */ test_trail1.Add(new KeyValuePair <int, int>(5, 0)); test_trail1.Add(new KeyValuePair <int, int>(5, 1)); test_trail1.Add(new KeyValuePair <int, int>(5, 2)); test_trail1.Add(new KeyValuePair <int, int>(5, 3)); test_trail1.Add(new KeyValuePair <int, int>(5, 4)); test_trail1.Add(new KeyValuePair <int, int>(5, 5)); test_trail1.Add(new KeyValuePair <int, int>(5, 6)); test_trail1.Add(new KeyValuePair <int, int>(5, 7)); test_trail1.Add(new KeyValuePair <int, int>(5, 8)); Assert.IsNotNull(trail, "Trail is null"); Assert.AreEqual(test_trail1.Count, trail.Count, "Trail {0} and trail_test {1} count is not equal", trail.Count, test_trail1.Count); for (int i = 0; i < test_trail1.Count; i++) { Assert.AreEqual(test_trail1[i], trail[i], "Astar_path element EQUAL problem - is {0}, should be {1}", trail[i], test_trail1[i]); } trail = Astar.Search(new KeyValuePair <int, int>(test_spider.Position.X, test_spider.Position.Y), new KeyValuePair <int, int>(test_ant2.Position.X, test_ant2.Position.Y), new TestAstarObject(test_isw)); List <KeyValuePair <int, int> > test_trail2 = new List <KeyValuePair <int, int> >(); test_trail2.Add(new KeyValuePair <int, int>(5, 0)); test_trail2.Add(new KeyValuePair <int, int>(4, 0)); test_trail2.Add(new KeyValuePair <int, int>(3, 0)); test_trail2.Add(new KeyValuePair <int, int>(2, 0)); test_trail2.Add(new KeyValuePair <int, int>(1, 0)); test_trail2.Add(new KeyValuePair <int, int>(0, 0)); test_trail2.Add(new KeyValuePair <int, int>(0, 1)); test_trail2.Add(new KeyValuePair <int, int>(0, 2)); test_trail2.Add(new KeyValuePair <int, int>(0, 3)); Assert.IsNotNull(trail, "Trail is null"); Assert.AreEqual(test_trail2.Count, trail.Count, "Trail {0} and trail_test {1} count is not equal", trail.Count, test_trail2.Count); for (int i = 0; i < test_trail2.Count; i++) { Assert.AreEqual(test_trail2[i], trail[i], "Astar_path element EQUAL problem - is {0}, should be {1}", trail[i], test_trail2[i]); } }
public override bool Maintain(ISimulationWorld isw) { if (!base.IsAlive()) { return(false); } SpreadSignal(isw); LIList <Food> food; LIList <Spider> spiders; spiders = isw.GetVisibleSpiders(this); if (spiders.Count != 0) { Spider s = this.GetNearestSpider(spiders); if (s != lastSpider) { if (!FindEqualSignal(MessageType.SpiderLocalization, s.Position)) { isw.CreateMessage(this.Position, MessageType.SpiderLocalization, s.Position); lastSpider = s; } } } LIList <Message> .Enumerator msg = isw.GetVisibleMessages(this).GetEnumerator(); while (msg.MoveNext()) { this.AddToSet(msg.Current, msg.Current.GetPointWithIntensity(this.Position).Intensity); } if (this.TurnsToBecomeHungry <= 0) { if (this.foodQuantity > 0) { foodQuantity--; Eat(); } } if (this.foodQuantity == 0) //search for food { path = null; food = isw.GetVisibleFood(this); if (food.Count != 0) {// idzie do jedzenia Food nearestFood = this.GetNearestFood(food); int dist = DistanceMeasurer.Taxi(this.Position, nearestFood.Position); if (dist == 0) { this.FoodQuantity = nearestFood.GetQuantity; isw.DeleteFood(nearestFood); } else { if (nearestFood != lastFood) { if (!FindEqualSignal(MessageType.FoodLocalization, nearestFood.Position)) { isw.CreateMessage(this.Position, MessageType.FoodLocalization, nearestFood.Position); lastFood = nearestFood; } } // znajdujemy t¹ krótk¹ œcie¿kê - wyliczane co 'maintain' List <KeyValuePair <int, int> > trail = Astar.Search(new KeyValuePair <int, int>(this.Position.X, this.Position.Y), new KeyValuePair <int, int>(nearestFood.Position.X, nearestFood.Position.Y), new AstarOtherObject()); if (trail.Count >= 2) { MoveOrRotateOrDig(isw, trail[1]); randomDestination.X = -1; return(true); } } } else {// nie widzi Message m = _messages[(int)MessageType.FoodLocalization]; if (m != null) { // ma sygnal o najwiekszej intensywnosci List <KeyValuePair <int, int> > trail = Astar.Search(new KeyValuePair <int, int>(this.Position.X, this.Position.Y), new KeyValuePair <int, int>(m.Position.X, m.Position.Y), new AstarWorkerObject()); if (trail.Count >= 2) { MoveOrRotateOrDig(isw, trail[1]); randomDestination.X = -1; return(true); } } } } else { int dist = DistanceMeasurer.Taxi(this.Position, Simulation.simulation.queen.Position); if (dist == 0) { isw.FeedQueen(this); path = null; } else { if (path == null || path.Count < 2) { path = Astar.Search(new KeyValuePair <int, int>(this.Position.X, this.Position.Y), new KeyValuePair <int, int>(Simulation.simulation.queen.Position.X, Simulation.simulation.queen.Position.Y), new AstarWorkerObject()); } if (path.Count >= 2) { if (MoveOrRotateOrDig(isw, path[1])) { path.RemoveAt(0); } randomDestination.X = -1; return(true); } } } MoveRandomly(isw); return(true); }
public bool DeleteSpider(Spider spider) { this.food.AddLast(new Food(spider.Position, AntHillConfig.spiderFoodQuantityAfterDeath)); spiders.Remove(spider); return(true); }
public override bool Maintain(ISimulationWorld isw) {//TODO malo:) if (!base.IsAlive()) { return(false); } SpreadSignal(isw); LIList <Message> .Enumerator msg = isw.GetVisibleMessages(this).GetEnumerator(); while (msg.MoveNext()) { this.AddToSet(msg.Current, msg.Current.GetPointWithIntensity(this.Position).Intensity); } LIList <Spider> spiders; if ((spiders = isw.GetVisibleSpiders(this)).Count != 0) { Spider spider = GetNearestSpider(spiders); if (spider != lastSpider) { if (!FindEqualSignal(MessageType.SpiderLocalization, spider.Position)) { isw.CreateMessage(this.Position, MessageType.SpiderLocalization, spider.Position); lastSpider = spider; } } MoveRotateOrAttack(this, spider, isw); randomDestination.X = -1; return(true); } if (MaintainSignals(MessageType.QueenInDanger)) { randomDestination.X = -1; return(true); } if (MaintainSignals(MessageType.SpiderLocalization)) { randomDestination.X = -1; return(true); } // teraz wcinamy LIList <Food> foods = isw.GetVisibleFood(this); if (foods.Count != 0) { Food food = GetNearestFood(foods); int distance = DistanceMeasurer.Taxi(this.Position, food.Position); if (food != lastFood) { if (!FindEqualSignal(MessageType.FoodLocalization, food.Position)) { isw.CreateMessage(this.Position, MessageType.FoodLocalization, food.Position); lastFood = food; } } if (this.TurnsToBecomeHungry <= 0) { if (distance == 0) { food.Maintain(isw); this.Eat(); randomDestination.X = -1; return(true); } List <KeyValuePair <int, int> > trail = Astar.Search(new KeyValuePair <int, int>(this.Position.X, this.Position.Y), new KeyValuePair <int, int>(food.Position.X, food.Position.Y), new AstarOtherObject()); if (trail == null) { randomDestination.X = -1; return(true); } if (trail.Count <= 1) { randomDestination.X = -1; return(true); } MoveOrRotate(trail[1]); randomDestination.X = -1; return(true); } } else { if (MaintainSignals(MessageType.FoodLocalization)) { randomDestination.X = -1; return(true); } } MoveRandomly(isw); return(true); }