public override bool Maintain(ISimulationWorld isw) { if (!IsAlive()) { return(false); } if (Randomizer.NextDouble() < AntHillConfig.queenLayEggProbability) { isw.CreateEgg(this.Position); } if (this.TurnsToBecomeHungry <= 0) { if (foodQuantity > 0) { this.Eat(); foodQuantity--; this.TurnsToBecomeHungry = AntHillConfig.antTurnNumberToBecomeHungry; } else { isw.CreateMessage(this.Position, MessageType.QueenIsHungry, this.Position); } } if (isw.GetVisibleSpiders(this).Count != 0) { isw.CreateMessage(this.Position, MessageType.QueenInDanger, this.Position); } return(true); }
public override bool Maintain(ISimulationWorld isw) { if (--timeToHatch < 0) { isw.CreateAnt(this.Position); return(false); } return(true); }
public override bool Maintain(ISimulationWorld isw) { quantity--; if (quantity <= 0) { isw.DeleteFood(this); return(false); } return(true); }
public virtual void SpreadSignal(ISimulationWorld isw) { MaintainRememberedMessages(); for (int i = 0; i < 4; i++) { if (_messages[i] != null) { _messages[i].Spread(isw, this.Position, _intensities[i] - 1); } } }
public override bool Maintain(ISimulationWorld isw) {//TODO czy na pewno dobrze znajduje droge? (pobieranie astar) czy spider nie szuka krolowej? Ant ant = FindNearestAnt(); if (ant == null) { MoveRandomly(isw); return(true); } randomDestination.X = -1; MoveRotateOrAttack(this, ant, isw); return(true); }
protected bool MoveOrRotateOrDig(ISimulationWorld isw, KeyValuePair <int, int> where) {// nie chce mi sie obrotu zrobic do kopania.. Tile t = isw.GetMap().GetTile(where.Key, where.Value); if (t.TileType == TileType.Wall) {// destroy wall nie ma zabawy z regionami if (this.IsMoveOrRotate(where)) { isw.GetMap().DestroyWall(t); } return(false); } return(MoveOrRotate(where)); }
protected void MoveRandomly(ISimulationWorld isw) { randomMovementCount++; if (randomMovementCount < currentTrail.Count) { if ((this.Position.X == currentTrail[randomMovementCount].Key) && (this.Position.Y == currentTrail[randomMovementCount].Value)) { randomMovementCount++; } } if ((randomMovementCount >= currentTrail.Count) || (randomDestination.X < 0)) { randomMovementCount = 0; randomDestination = new Position(isw.GetMap().GetRandomIndoorOrOutdoorTile().Position); currentTrail = Astar.Search(new KeyValuePair <int, int>(this.Position.X, this.Position.Y), new KeyValuePair <int, int>(randomDestination.X, randomDestination.Y), new AstarOtherObject()); } if (currentTrail == null) { randomDestination.X = -1; return; } if (currentTrail.Count <= 1) { randomDestination.X = -1; return; } if (!MoveOrRotate(currentTrail[randomMovementCount])) { randomMovementCount--; } if (randomMovementCount >= 10) { randomDestination.X = -1; } }
/* * private int GetPointIndex(int x, int y) * { * LIList<PointWithIntensity>.Enumerator e = points.GetEnumerator(); * int i = 0; * while (e.MoveNext()) * { * Tile t = e.Current.Tile; * if (t.Position.X == x && t.Position.Y == y) * return i; * i++; * } * return -1; * } */ public override bool Maintain(ISimulationWorld isw) { LinkedListNode <PointWithIntensity> msg = points.First; LinkedListNode <PointWithIntensity> msgT; while (msg != null) { if (--msg.Value.Intensity <= 0) { isw.GetMap().RemoveMessage(this.GetMessageType, msg.Value.Tile.Position); msg.Value.Tile.messages.Remove(this); msgT = msg; msg = msg.Next; points.Remove(msgT); } else { msg = msg.Next; } } return(true); }
public void Spread(ISimulationWorld isw, Position point, int intensity) { int radius = AntHillConfig.messageRadius, radius2 = radius * radius; int i2, j2; Map map = isw.GetMap(); Tile t; Position p; for (int i = -radius; i <= radius; i++) { i2 = i * i; for (int j = -radius; j <= radius; j++) { j2 = j * j; if (i2 + j2 <= radius2) { p = new Position(i + point.X, j + point.Y); if (map.IsInside(p)) {// czy wogole w srodku t = map.GetTile(p); if (t.TileType == TileType.Wall) { continue; } if (!t.messages.Contains(this)) {// nie ma w danym tile - wrzucamy t.messages.AddLast(this); this.points.AddLast(new PointWithIntensity(t, intensity)); //update map map.AddMessage(this.GetMessageType, p); } } } } } }
public override bool Maintain(ISimulationWorld isw) { if (--timeToLive < 0) { isw.DeleteRain(); return(false); } LIList <Food> lFood = isw.GetVisibleFood(this); LIList <Ant> lAnt = isw.GetVisibleAnts(this); LIList <Spider> lSpider = isw.GetVisibleSpiders(this); LIList <Message> lMessage = isw.GetVisibleMessages(this); while (lFood.Count > 0) { isw.DeleteFood(lFood.First.Value); lFood.RemoveFirst(); } while (lAnt.Count > 0) { isw.DeleteAnt(lAnt.First.Value); lAnt.RemoveFirst(); } while (lSpider.Count > 0) { isw.DeleteSpider(lSpider.First.Value); lSpider.RemoveFirst(); } Map map = isw.GetMap(); if (lMessage != null) { LinkedListNode <Message> enumMsg = lMessage.First; LinkedListNode <PointWithIntensity> enumPwI, enumPwItemp; while (enumMsg != null) { enumPwI = enumMsg.Value.Points.First; while (enumPwI != null) { if (IsRainOver(enumPwI.Value.Position)) { map.RemoveMessage(enumMsg.Value.GetMessageType, enumPwI.Value.Position); enumPwItemp = enumPwI; enumPwI = enumPwI.Next; enumMsg.Value.Points.Remove(enumPwItemp); } else { enumPwI = enumPwI.Next; } } enumMsg = enumMsg.Next; } } // Rain is always on the map for (int i = 0; i < AntHillConfig.rainWidth; i++) // && i+this.Position.X < map.Width; i++) { for (int j = 0; j < AntHillConfig.rainWidth; j++) // && j+this.Position.Y < map.Height; j++) { map.GetTile(this.Position.X + i, this.Position.Y + j).messages.Clear(); } } return(true); }
public void MoveRotateOrAttack(Creature aggressor, Creature prey, ISimulationWorld isw) { int distance = DistanceMeasurer.Taxi(aggressor.Position, prey.Position); if (distance == 0) { isw.Attack(aggressor, prey); } if (distance == 1) { if (aggressor.Position.X == prey.Position.X) { if (aggressor.Position.Y == prey.Position.Y + 1) //ant 1 tile above { if (aggressor.Direction == Dir.N) { isw.Attack(aggressor, prey); return; } else { aggressor.Direction = Dir.N; } } else { if (aggressor.Direction == Dir.S) { isw.Attack(aggressor, prey); return; } else { aggressor.Direction = Dir.S; } } } else { if (aggressor.Position.X == prey.Position.X + 1) //ant 1 tile left { if (aggressor.Direction == Dir.W) { isw.Attack(aggressor, prey); return; } else { aggressor.Direction = Dir.W; } } else { if (aggressor.Direction == Dir.E) { isw.Attack(aggressor, prey); return; } else { aggressor.Direction = Dir.E; } } } return; } if (distance > 1) { List <KeyValuePair <int, int> > trail = Astar.Search(new KeyValuePair <int, int>(aggressor.Position.X, aggressor.Position.Y), new KeyValuePair <int, int>(prey.Position.X, prey.Position.Y), new AstarOtherObject()); if (trail == null) { return; } if (trail.Count <= 1) { return; } MoveOrRotate(trail[1]); } }
public abstract bool Maintain(ISimulationWorld isw);
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 void LoadFood(ISimulationWorld isw, Food f) { isw.DeleteFood(f); foodQuantity += f.GetQuantity; }
public void Dig(ISimulationWorld isw, Position pos) { isw.GetMap().DestroyWall(isw.GetMap().GetTile(pos)); }
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); }