void OnTriggerEnter(Collider c) { if (c.gameObject.CompareTag("pheromone")) { returningPheromone = c.gameObject.GetComponent <Pheromone>(); // p.Add(QVAL/ distanceSum); } else if (c.gameObject.CompareTag("sugar")) { SetReturning(true); sugarPeace = c.gameObject.GetComponent <Sugar>().GetPeace(); positionHistory.Add(transform.position); SetNextDestination(); } else if (c.gameObject.CompareTag("anthill")) { returningAnthill = c.gameObject.GetComponent <Anthill>(); // if (!isReturning) { // positionHistory.Clear(); // SetReturning(false); // if (sugarPeace != null) { // c.gameObject.GetComponent<Anthill>().AddSugarPeace(sugarPeace); // sugarPeace = null; // } // SetNextDestination(); // } } }
void OnTriggerExit(Collider c) { if (c.gameObject.CompareTag("anthill")) { returningAnthill = null; } }
public void Init(Anthill anthill) { this.anthill = anthill; moveGenerator.Init(this); moveValidator.Init(this); move.OnMoveFinish += OnMoveFinish; SetNextDestination(); }
/// <summary> /// Creates a new coordinate instance. /// </summary> /// <param name="anthill">The anthill as reference object.</param> /// <param name="ant">The ant for the coordinate.</param> public RelativeCoordinate(Anthill anthill, BaseAnt ant) { // Calculate distance and degree double distance = Coordinate.GetDistanceBetween(anthill, ant); double degrees = Coordinate.GetDegreesBetween(anthill, ant); // Calculate coordinates with distance and degree SetCoordinatesBeDegrees(distance, degrees); }
public override AbstractEnvironment CreateEnvironment() { var anthill = new Anthill() { HeightWorld = _heightWorld, WidthWorld = _widthWorld }; return(anthill); }
/// <summary> /// Initializes the behavior instance. /// </summary> private void Init() { // Set destination to anthill _ant.GoToAnthill(); // Read anthill from destination Anthill = (Anthill)Destination; // Mark as initialized initialized = true; }
public void Rpc_dealDamage(GameObject attackingUnit, GameObject enemy) { Debug.Log(enemy.name); if (enemy.GetComponent <BasicAnt> () && attackingUnit.GetComponent <BasicAnt> ()) { BasicAnt enemyAnt = enemy.GetComponent <BasicAnt> (); UnitIdentity enemyAntIdentity = enemy.GetComponent <UnitIdentity> (); BasicAnt myAnt = attackingUnit.GetComponent <BasicAnt> (); UnitIdentity myAntIdentity = attackingUnit.GetComponent <UnitIdentity> (); if (myAntIdentity.id == enemyAntIdentity.id) { return; } enemyAnt.health -= Random.Range(0, myAnt.damage); myAnt.health -= Random.Range(0, enemyAnt.damage); } if (enemy.GetComponent <Anthill> () && attackingUnit.GetComponent <BasicAnt> ()) { Anthill enemyAntHill = enemy.GetComponent <Anthill> (); BasicAnt myAnt = attackingUnit.GetComponent <BasicAnt> (); UnitIdentity antHillIdentity = enemy.GetComponent <UnitIdentity> (); UnitIdentity myAntIdentity = attackingUnit.GetComponent <UnitIdentity> (); Debug.Log("shit works up to here1"); if (myAntIdentity.id == antHillIdentity.id) { return; } Debug.Log("shit works up to here2"); enemyAntHill.health -= Random.Range(0, myAnt.damage); Debug.Log("shit works up to here3"); } else { Debug.Log(enemy.name); Debug.Log(attackingUnit.name); } }
/// <summary> /// If the ant has no assigned tasks, it waits for new tasks. This method /// is called to inform you that it is waiting. /// Read more: "http://wiki.antme.net/en/API1:Waiting" /// </summary> public override void Waiting() { if (Anthill == null) { GoToAnthill(); // Sets Destination to anthill Anthill = Destination as Anthill; Stop(); // stop to reset Destination } if (Caste == Consts.Collector) { task = TaskManager.Instance.GetTask(this.Caste); if (task != null) { GoTo(((FoodTask)task).Target); } else { TurnByDegrees(RandomNumber.Number(-30, 30)); GoForward(100); } } else if (Caste == Consts.Scout) { TurnByDegrees(RandomNumber.Number(-40, 40)); GoForward(100); } else if (Caste == Consts.Hunter) { var enemyTask = TaskManager.Instance.GetTask(Caste); if (enemyTask != null) { var t = ((InsectTask)enemyTask).Target; if (t.CurrentEnergy > 0) { GoTo(t); return; } } TurnByDegrees(RandomNumber.Number(-20, 20)); GoForward(20); } }
public override void Waiting() { if (this.Home == null) { this.GoToAnthill(); this.Home = this.Destination as Anthill; this.Stop(); } if (this.Caste == WorkerCasteName) { var item = SuperSwarmBrain.Instance.GetNearestSugar(this); if (item != null) { this.CustomGoToDestination(item); return; } } if (this.Caste == FighterCasteName) { var item = SuperSwarmBrain.Instance.GetNearestEnemy(this); if (item != null) { this.GoToDestination(item); return; } } if (this.Caste == FruitWorkerCasteName) { var item = SuperSwarmBrain.Instance.GetNearestFruit(this); if (item != null) { this.CustomGoToDestination(item); return; } } this.GoForward(); }
public void CreateAnthill(Anthill home, World world) { var Queens = home.ListCharacter.Where(c => c.GetType() == typeof(AntQueen)).ToList(); if (Queens.Count > 1 && Queens.Last() == this) { // limit max 3 anthill if (world.ListAnthill.Count <= 3) { home.ListCharacter.Remove(this); _factoryAnthill = new FactoryAnthill(world.Fields, home.WidthWorld, home.HeightWorld); var newAnthill = (Anthill)_factoryAnthill.CreateEnvironment(); newAnthill.LoadEnvironment(_factoryAnthill); world.ListAnthill.Add(newAnthill); } else { home.ListCharacter.Remove(this); this.Life = 0; } } }
// Update is called once per frame protected override void Update() { base.Update(); loadParentSelectable(); if (!antUnitParent) antUnitParent = GameObject.Find("Units"); if (!mapManager) mapManager = GameObject.Find("MapManager").GetComponent<MapManager>(); if (!anthillScript) anthillScript = transform.parent.GetComponent<Anthill>(); if (!netMan && GameObject.Find("NetworkManager")) netMan = GameObject.Find("NetworkManager").GetComponent<NetworkManager>(); // If we our parent object is selected, show this button if (parentSelectable.isNeutralOrFriendly() && parentSelectable.isSelected()) { renderer.enabled = true; // If we can't afford to make this unit, disable the button if (anthillScript.getStoredFoodPoints() < foodCost || hasReachedUnitCap()) { buttonEnabled = false; renderer.material.color = Color.grey; } else { buttonEnabled = true; renderer.material.color = Color.white; } } else { renderer.enabled = false; buttonEnabled = false; } }
/// <summary> /// Reads the next block out of stream. /// </summary> /// <param name="block">the found block</param> /// <returns>type of found block</returns> public BlockType Read(out ISerializable block) { BlockType blockType = (BlockType)stream.ReadByte(); block = null; switch (blockType) { case BlockType.Ant: block = new Ant(this); break; case BlockType.Anthill: block = new Anthill(this); break; case BlockType.AnthillLost: throw new InvalidOperationException(string.Format(Resource.AntvideoSerializerInvalidBlockType, blockType)); case BlockType.AnthillUpdate: throw new InvalidOperationException(string.Format(Resource.AntvideoSerializerInvalidBlockType, blockType)); case BlockType.AntLost: block = new Lost(this); break; case BlockType.AntUpdate: block = new AntUpdate(this); break; case BlockType.Bug: block = new Bug(this); break; case BlockType.BugLost: block = new Lost(this); break; case BlockType.BugUpdate: block = new BugUpdate(this); break; case BlockType.Team: block = new Team(this); break; case BlockType.TeamLost: throw new InvalidOperationException(string.Format(Resource.AntvideoSerializerInvalidBlockType, blockType)); case BlockType.TeamUpdate: throw new InvalidOperationException(string.Format(Resource.AntvideoSerializerInvalidBlockType, blockType)); case BlockType.Colony: block = new Colony(this); break; case BlockType.ColonyLost: throw new InvalidOperationException(string.Format(Resource.AntvideoSerializerInvalidBlockType, blockType)); case BlockType.ColonyUpdate: block = new ColonyUpdate(this); break; case BlockType.Frame: block = new Frame(this); break; case BlockType.StreamEnd: break; case BlockType.FrameLost: throw new InvalidOperationException(string.Format(Resource.AntvideoSerializerInvalidBlockType, blockType)); case BlockType.FrameUpdate: block = new FrameUpdate(this); break; case BlockType.Fruit: block = new Fruit(this); break; case BlockType.FruitLost: block = new Lost(this); break; case BlockType.FruitUpdate: block = new FruitUpdate(this); break; case BlockType.Marker: block = new Marker(this); break; case BlockType.MarkerLost: block = new Lost(this); break; case BlockType.MarkerUpdate: block = new MarkerUpdate(this); break; case BlockType.Caste: block = new Caste(this); break; case BlockType.CasteLost: throw new InvalidOperationException(string.Format(Resource.AntvideoSerializerInvalidBlockType, blockType)); case BlockType.CasteUpdate: throw new InvalidOperationException(string.Format(Resource.AntvideoSerializerInvalidBlockType, blockType)); case BlockType.Sugar: block = new Sugar(this); break; case BlockType.SugarLost: block = new Lost(this); break; case BlockType.SugarUpdate: block = new SugarUpdate(this); break; case BlockType.FrameStart: break; case BlockType.FrameEnd: break; default: throw new InvalidOperationException(string.Format(Resource.AntvideoSerializerInvalidBlockType, blockType)); } return(blockType); }
public ClientAnt(Anthill anthill, string id) { _anthill = anthill; }
/// <summary> /// Reads a new simulation-state out of stream. /// </summary> /// <returns>New simulation-state or null, if stream is over</returns> public SimulationState Read() { // if stream is at his end, return null if (complete) { return(null); } // first block have to be a frame-start ISerializable block; BlockType blockType = serializer.Read(out block); // detect stream-end if (blockType == BlockType.StreamEnd) { complete = true; return(null); } // unexpected block-type if (blockType != BlockType.FrameStart) { throw new InvalidOperationException( string.Format( CultureInfo.CurrentCulture, Resource.AntvideoReaderInvalidBlockType, blockType)); } // block-loop while (blockType != BlockType.FrameEnd) { blockType = serializer.Read(out block); switch (blockType) { case BlockType.Ant: Ant ant = (Ant)block; antList.Add(ant.Id, ant); break; case BlockType.Anthill: Anthill anthill = (Anthill)block; anthillList.Add(anthill.Id, anthill); break; case BlockType.AntLost: Lost antLost = (Lost)block; antList.Remove(antLost.Id); break; case BlockType.AntUpdate: AntUpdate antUpdate = (AntUpdate)block; antList[antUpdate.Id].Update(antUpdate); break; case BlockType.Bug: Bug bug = (Bug)block; bugList.Add(bug.Id, bug); break; case BlockType.BugLost: Lost bugLost = (Lost)block; bugList.Remove(bugLost.Id); break; case BlockType.BugUpdate: BugUpdate bugUpdate = (BugUpdate)block; bugList[bugUpdate.Id].Update(bugUpdate); break; case BlockType.Caste: Caste caste = (Caste)block; casteList[caste.ColonyId].Add(caste.Id, caste); break; case BlockType.Team: Team team = (Team)block; teamList.Add(team.Id, team); colonyList.Add(team.Id, new Dictionary <int, Colony>()); break; case BlockType.Colony: Colony colony = (Colony)block; colonyList[colony.TeamId].Add(colony.Id, colony); casteList.Add(colony.Id, new Dictionary <int, Caste>()); break; case BlockType.ColonyUpdate: ColonyUpdate colonyUpdate = (ColonyUpdate)block; colonyList[colonyUpdate.TeamId][colonyUpdate.Id].Update(colonyUpdate); break; case BlockType.Frame: frame = (Frame)block; break; case BlockType.FrameUpdate: FrameUpdate frameUpdate = (FrameUpdate)block; frame.Update(frameUpdate); break; case BlockType.Fruit: Fruit fruit = (Fruit)block; fruitList.Add(fruit.Id, fruit); break; case BlockType.FruitLost: Lost fruitLost = (Lost)block; fruitList.Remove(fruitLost.Id); break; case BlockType.FruitUpdate: FruitUpdate fruitUpdate = (FruitUpdate)block; fruitList[fruitUpdate.Id].Update(fruitUpdate); break; case BlockType.Marker: Marker marker = (Marker)block; markerList.Add(marker.Id, marker); break; case BlockType.MarkerLost: Lost markerLost = (Lost)block; markerList.Remove(markerLost.Id); break; case BlockType.MarkerUpdate: MarkerUpdate markerUpdate = (MarkerUpdate)block; markerList[markerUpdate.Id].Update(markerUpdate); break; case BlockType.Sugar: Sugar sugar = (Sugar)block; sugarList.Add(sugar.Id, sugar); break; case BlockType.SugarLost: Lost sugarLost = (Lost)block; sugarList.Remove(sugarLost.Id); break; case BlockType.SugarUpdate: SugarUpdate sugarUpdate = (SugarUpdate)block; sugarList[sugarUpdate.Id].Update(sugarUpdate); break; } } // Detect streamend if ((BlockType)serializer.Peek() == BlockType.StreamEnd) { complete = true; } // Interpolate all elements and buildup state frame.Interpolate(); SimulationState state = frame.GenerateState(); foreach (Bug bug in bugList.Values) { bug.Interpolate(); state.BugStates.Add(bug.GenerateState()); } foreach (Fruit fruit in fruitList.Values) { fruit.Interpolate(); state.FruitStates.Add(fruit.GenerateState()); } foreach (Sugar sugar in sugarList.Values) { sugar.Interpolate(); state.SugarStates.Add(sugar.GenerateState()); } foreach (Team team in teamList.Values) { TeamState teamState = team.GenerateState(); state.TeamStates.Add(teamState); foreach (Colony colony in colonyList[team.Id].Values) { colony.Interpolate(); ColonyState colonyState = colony.GenerateState(); teamState.ColonyStates.Add(colonyState); foreach (Caste caste in casteList[colony.Id].Values) { colonyState.CasteStates.Add(caste.GenerateState()); } foreach (Anthill anthill in anthillList.Values) { if (anthill.ColonyId == colony.Id) { colonyState.AnthillStates.Add(anthill.GenerateState()); } } foreach (Ant ant in antList.Values) { if (ant.ColonyId == colony.Id) { ant.Interpolate(); colonyState.AntStates.Add(ant.GenerateState()); } } foreach (Marker marker in markerList.Values) { if (marker.ColonyId == colony.Id) { marker.Interpolate(); colonyState.MarkerStates.Add(marker.GenerateState()); } } } } // deliver return(state); }
/// <summary> /// Every time that a new ant is born, its job group must be set. You can /// do so with the help of the value returned by this method. /// Read more: "http://wiki.antme.net/en/API1:ChooseCaste" /// </summary> /// <param name="typeCount">Number of ants for every caste</param> /// <returns>Caste-Name for the next ant</returns> public override string ChooseCaste(Dictionary<string, int> typeCount) { if (timera == null) timera = this; if (hill == null) { GoToAnthill(); hill = Destination as Anthill; Stop(); } Ameisenliste.Add(this); int r = RandomNumber.Number(0, 20); bool alle = true; for (int i = 0; i < 4; i++) { if (!absolute[i]) { alle = false; } } for (int i = 0; i < 4; i++) { if (spawned[i] < 1) { spawned[i]++; if (i == 0) return "south"; else if (i == 1) return "west"; else if (i == 2) return "north"; else if (i == 3) return "east"; } } if (searcher.Count < 8) { searcher.Add(this); listda = true; return "searcher"; } if (stars[0] < 0) { stars[0]++; for (int i = 1; i < 20; i++) { if (stars[i] == 0) { stars[i] = Ameisenliste.IndexOf(this); break; } } return "star"; } if (r < 0) return "stand"; else if (r < 8) return "default"; else if (r < 19) return "sugar"; else if (r < 20) return "fighter2"; else if (r < 20) return "fighter"; else return "fighter3"; }
public ChatHub(Anthill clusterClient, MessageRepository repository) { _anthill = clusterClient; _repository = repository; }
/// <summary> /// If the ant has no assigned tasks, it waits for new tasks. This method /// is called to inform you that it is waiting. /// Read more: "http://wiki.antme.net/en/API1:Waiting" /// </summary> public override void Waiting() { if (CurrentLoad > 5) { if (DistanceToAnthill > 20) { TurnToDetination(hill); GoForward(DistanceToAnthill - 10); } else GoToAnthill(); return; } if (timera == null) timera = this; if (hill == null) { GoToAnthill(); hill = Destination as Anthill; Stop(); } if (Caste == "north") { if (Direction != 270) TurnToDirection(270); if (!absolute[0]) GoForward(); else GoForward(50); return; } if (Caste == "east") { if (Direction != 0) TurnToDirection(0); if (!absolute[1]) GoForward(); else GoForward(50); if (!absolute[2]) GoForward(); else GoForward(50); return; } if (Caste == "south") { if (Direction != 90) TurnToDirection(90); if (!absolute[2]) GoForward(); else GoForward(50); return; } if (Caste == "west") { if (Direction != 180) TurnToDirection(180); if (!absolute[3]) GoForward(); else GoForward(50); return; } if (Caste == "fighter2" && !IsTired && spotted) { double distance, direction, x, y; getcordsa(DistanceToAnthill, Coordinate.GetDegreesBetween(this, hill), out x, out y); getdistance(x, y, basex, basey, out distance); if (distance > 50) { getdirection(x, y, basex, basey, out direction); TurnToDirection(Convert.ToInt32(direction)); GoForward(Convert.ToInt32(distance)); Think("Infiltrieren"); return; } else { } } if (IsTired && (Caste != "sugar" || (CurrentLoad > MaximumLoad / 10)) && (Caste != "fighter2" || !spotted)) { if (DistanceToAnthill > 10) { if (Direction != Coordinate.GetDegreesBetween(this, hill)) TurnToDetination(hill); GoForward(DistanceToAnthill - 5); } else GoToDestination(hill); return; } if (Caste == "searcher") { int distance = 0; double x, y, lx, ly, direction, distanz, angle; distance = (searcher.IndexOf(this)) * Viewrange + Viewrange; getcordsa(DistanceToAnthill, Coordinate.GetDegreesBetween(this, hill), out x, out y); if (distance == 0) { Think("ERROR"); GoToDestination(hill); return; } if (DistanceToAnthill < distance - 20) { bool alle = true; Think("falsch"); if (DistanceToAnthill > 5) { Think("korrigiere" + Convert.ToInt32(distance) + " " + DistanceToAnthill); if (x > 0) { if (y > 0) { angle = 135; } else { angle = 225; } } else { if (y > 0) { angle = 45; } else { angle = 315; } } getcordsa(distance, angle, out lx, out ly); getdirection(x, y, lx, ly, out direction); getdistance(x, y, lx, ly, out distanz); TurnToDirection(Convert.ToInt32(direction)); GoForward(Convert.ToInt32(distanz) - 10); return; } else { int rd = 0; Think("richte aus"); for (int i = 0; i < 4; i++) { if (!absolute[i]) { if (Direction != 45) TurnToDirection(45); alle = false; rd = 1; } } if (alle) { double distanceh; getdistance(x, y, basex, basey, out distanceh); if (entfernung[0] > entfernung[2]) { if (entfernung[1] > entfernung[3]) { if (Direction != 315) TurnToDirection(315); rd = 0; } else { if (Direction != 225) TurnToDirection(225); rd = 3; } } else { if (entfernung[1] > entfernung[3]) { if (Direction != 45) TurnToDirection(45); rd = 1; } else { if (Direction != 135) TurnToDirection(135); rd = 2; } } } if (alle || time > 13000) GoForward(distance); } return; } else { Think("aufgestellt" + distance); bool r = false; if (searcher.IndexOf(this) % 2 == 0) r = true; if (Coordinate.GetDegreesBetween(this, hill) >= 210 && Coordinate.GetDegreesBetween(this, hill) <= 240) { if (r) { if (Direction != 270) TurnToDirection(270); } else if (Direction != 180) TurnToDirection(180); GoForward(Convert.ToInt32(DistanceToAnthill * 1.15 / 0.70710678118)); return; } else if (Coordinate.GetDegreesBetween(this, hill) >= 120 && Coordinate.GetDegreesBetween(this, hill) <= 150) { if (r) { if (Direction != 180) TurnToDirection(180); } else if (Direction != 90) TurnToDirection(90); GoForward(Convert.ToInt32(DistanceToAnthill * 1.15 / 0.70710678118)); return; } else if (Coordinate.GetDegreesBetween(this, hill) >= 30 && Coordinate.GetDegreesBetween(this, hill) <= 60) { if (r) { if (Direction != 90) TurnToDirection(90); } else if (Direction != 0) TurnToDirection(0); GoForward(Convert.ToInt32(DistanceToAnthill * 1.15 / 0.70710678118)); return; } else if (Coordinate.GetDegreesBetween(this, hill) >= 300 && Coordinate.GetDegreesBetween(this, hill) <= 330) { if (r) { if (Direction != 0) TurnToDirection(0); } else if (Direction != 270) TurnToDirection(270); GoForward(Convert.ToInt32(DistanceToAnthill * 1.15 / 0.70710678118)); return; } else { Think("falsch"); if (DistanceToAnthill > 5) { Think("korrigiere" + Coordinate.GetDegreesBetween(this, hill)); getcordsa(DistanceToAnthill, Coordinate.GetDegreesBetween(this, hill), out x, out y); if (x > 0) { if (y > 0) { angle = 135; } else { angle = 225; } } else { if (y > 0) { angle = 45; } else { angle = 315; } } getcordsa(distance, angle, out lx, out ly); getdirection(x, y, lx, ly, out direction); getdistance(x, y, lx, ly, out distanz); TurnToDirection(Convert.ToInt32(direction)); GoForward(Convert.ToInt32(distanz) - 10); return; } else { bool alle = true; Think("richte aus"); for (int i = 0; i < 4; i++) { if (!absolute[i]) { if (Direction != 45) TurnToDirection(45); alle = false; } } if (alle) { if (entfernung[0] > entfernung[2]) { if (entfernung[1] > entfernung[3]) { if (Direction != 315) TurnToDirection(315); } else { if (Direction != 225) TurnToDirection(225); } } else { if (entfernung[1] > entfernung[3]) { if (Direction != 45) TurnToDirection(45); } else { if (Direction != 135) TurnToDirection(135); } } } GoForward(distance); return; } } } } else if (Caste == "star") { int that = 0; for (int i = 1; i < 20; i++) { if (stars[i] == Ameisenliste.IndexOf(this)) { that = i; } } if (DistanceToAnthill > 5 && (Caste != "sugar" || (CurrentLoad < MaximumLoad / 10)) && (Caste != "fighter2" || !spotted)) { Think("back"); if (DistanceToAnthill > 10) { if (Direction != Coordinate.GetDegreesBetween(this, hill)) TurnToDetination(hill); GoForward(DistanceToAnthill - 5); } else GoToDestination(hill); return; } else { if (Direction != (359 / 8) * that) TurnToDirection((359 / 8) * that); GoForward(Convert.ToInt32(Range / 2.1)); Think("Ich bin ein Star!"); return; } } if (hill == null) { GoToAnthill(); hill = Destination as Anthill; Stop(); } if (IsTired && (Caste != "sugar" || (CurrentLoad < MaximumLoad / 10)) && (Caste != "fighter2" || !spotted)) { if (DistanceToAnthill > 10) { if (Direction != Coordinate.GetDegreesBetween(this, hill)) if (Direction != Coordinate.GetDegreesBetween(this, hill)) TurnToDetination(hill); GoForward(DistanceToAnthill - 5); } else GoToDestination(hill); return; } else if (Caste != "sugar" || CurrentLoad < MaximumLoad / 10 && CarryingFruit == null) { if (Coordinate.GetDistanceBetween(this, hill) < 10) TurnToDirection(norandom); norandom += 5; GoForward(50); } if (wirdangegriffen[Ameisenliste.IndexOf(this)] != 0) { for (int i = 0; i < 300; i++) { if (verfugbar[i, 0] == wirdangegriffen[Ameisenliste.IndexOf(this)]) { Think(i + "Vergessen weil unbeschäftigt"); aimedbug[i] = null; for (int u = 2; u < 300; u++) { verfugbar[i, u] = 0; } verfugbar[i, 1] = 0; verfugbar[i, 0] = 0; aimedposition[i, 0] = 0; aimedposition[i, 1] = 0; } } wirdangegriffen[Ameisenliste.IndexOf(this)] = 0; } if (Caste == "fighter" || Caste == "fighter3") { if (Coordinate.GetDistanceBetween(this, hill) < 20) TurnByDegrees(RandomNumber.Number(360)); GoForward(40); for (int i = 0; i < 300; i++) { for (int u = 2; u < 300; u++) { if (verfugbar[i, u] == Ameisenliste.IndexOf(this) && aimedbug[i] != null && (Caste != "sugar" || (CurrentLoad < MaximumLoad / 10 || Direction != Coordinate.GetDegreesBetween(this, hill))) && (Caste != "fighter2" || !spotted)) { if (!IsTired && WalkedRange < Range / 3) Attack(aimedbug[i]); else GoToDestination(hill); return; } } } int ld = 1000000; int angriff = 0; for (int i = 0; i < 300; i++) { if (aimedbug[i] != null) if (Coordinate.GetDistanceBetween(aimedbug[i], hill) < ld && verfugbar[i, 1] < 15) { ld = Coordinate.GetDistanceBetween(aimedbug[i], hill); angriff = i; } } if (aimedbug[angriff] != null && verfugbar[angriff, 1] < 15 && !IsTired) for (int i = 2; i < 300; i++) { if (verfugbar[angriff, i] == 0 && (Caste != "sugar" || (CurrentLoad < MaximumLoad / 10)) && (Caste != "fighter2" || !spotted)) { verfugbar[angriff, i] = Ameisenliste.IndexOf(this); verfugbar[angriff, 1]++; if (!IsTired && WalkedRange < Range / 3) Attack(aimedbug[angriff]); else GoToDestination(hill); Think("Angriff" + angriff); return; } } } if (Caste == "sugar") { double x, y; double direction, distance; int nearest = 0; double lowestdistance = 3000000000000000; getcordsa(Coordinate.GetDistanceBetween(this, hill), Coordinate.GetDegreesBetween(this, hill), out x, out y); for (int i = 0; i < 4; i++) { if (zucker[i] != null) { distance = Coordinate.GetDistanceBetween(this, zucker[i]); if (distance < lowestdistance && distance < Range / 3) { lowestdistance = distance; nearest = i; } } } if (lowestdistance < this.Range / 3 && zucker[nearest] != null) { if (unterwegs[nearest, 1] * MaximumLoad < zucker[nearest].Amount * 1.8 && CurrentLoad < MaximumLoad / 10) { if (Coordinate.GetDistanceBetween(this, zucker[nearest]) > 2 && Range - WalkedRange - Coordinate.GetDistanceBetween(this, zucker[nearest]) > Range * 2 / 3 && !IsTired && (Caste != "sugar" || (CurrentLoad < MaximumLoad / 10))) { if (Direction != Coordinate.GetDegreesBetween(this, zucker[nearest])) TurnToDetination(zucker[nearest]); GoForward(Convert.ToInt32(Coordinate.GetDistanceBetween(this, zucker[nearest]) / 1.1)); return; } else if (Range - WalkedRange - Coordinate.GetDistanceBetween(this, zucker[nearest]) > Range * 2 / 3 && !IsTired) GoToDestination(zucker[nearest]); } else if (CurrentLoad < MaximumLoad / 10 && Range - WalkedRange - Coordinate.GetDistanceBetween(this, zucker[nearest]) > Range * 2 / 3 && !IsTired) { for (int i = 0; i < 4; i++) { if (zucker[i] != null) { int distanz = Convert.ToInt32(Coordinate.GetDistanceBetween(this, zucker[i])); if (zucker[i] != null && Range - WalkedRange - distanz > Range * 2 / 3) { TurnToDetination(zucker[i]); GoForward(distanz); return; } } } if (Direction != Coordinate.GetDegreesBetween(this, zucker[nearest])) TurnToDetination(zucker[nearest]); GoForward(Coordinate.GetDistanceBetween(this, (zucker[nearest])) / 2); } for (int i = 0; i < 4; i++) { if (zucker[nearest].Id == unterwegs[i, 0]) { for (int u = 2; u < 100; u++) { if (unterwegs[i, u] == Ameisenliste.IndexOf(this)) break; else if (unterwegs[i, u] == 0) { unterwegs[i, u] = Ameisenliste.IndexOf(this); unterwegs[i, 1]++; break; } } } } } else { if (Coordinate.GetDistanceBetween(this, hill) < 20) { TurnToDirection(RandomNumber.Number(180)); GoForward(50); } } } else if (Caste == "default" && CarryingFruit == null) { for (int i = 0; i < 10; i++) { for (int u = 2; u < 10; u++) { if (apfelliste[i, u] == Ameisenliste.IndexOf(this) && apple[i] != null && Range - WalkedRange - Coordinate.GetDistanceBetween(this, apple[i]) > Range * 2 / 3) { if (Coordinate.GetDistanceBetween(this, apple[i]) > 20) { if (Direction != Coordinate.GetDegreesBetween(this, apple[i])) TurnToDetination(apple[i]); GoForward(Coordinate.GetDistanceBetween(this, apple[i]) - 10); } else GoToDestination(apple[i]); return; } } if ((apfelliste[i, 0] != 0 || apfelliste[i, 1] != 0) && apfelliste[i, 1] < 5 && apple[i] != null && Range - WalkedRange - Coordinate.GetDistanceBetween(this, apple[i]) > Range * 2 / 3) { apfelliste[i, 1]++; for (int u = 2; u < 10; u++) { if (apfelliste[i, u] == 0) { apfelliste[i, u] = Ameisenliste.IndexOf(this); if (Coordinate.GetDistanceBetween(this, apple[i]) > 20) { if (Direction != Coordinate.GetDegreesBetween(this, apple[i])) TurnToDetination(apple[i]); GoForward(Coordinate.GetDistanceBetween(this, apple[i]) - 10); } else GoToDestination(apple[i]); break; } } } } } else if (Caste == "stand" && (Caste != "fighter2" || !spotted)) { if (DistanceToAnthill > 10) { if (Direction != Coordinate.GetDegreesBetween(this, hill)) TurnToDetination(hill); GoForward(DistanceToAnthill - 5); } else GoToDestination(hill); return; } else if ((Caste != "sugar" || (CurrentLoad < MaximumLoad / 10))) { if (Coordinate.GetDistanceBetween(this, hill) < 10) TurnToDirection(norandom); norandom += 5; GoForward(50); } if (DistanceToAnthill > 10 && Caste != "fighter2" && false) GoToAnthill(); else if (CurrentLoad < 5 && CarryingFruit == null) { if (Coordinate.GetDistanceBetween(this, hill) < 10) TurnToDirection(norandom); norandom += 5; GoForward(50); } }
/// <summary> /// This method is called in every simulation round, regardless of additional /// conditions. It is ideal for actions that must be executed but that are not /// addressed by other methods. /// Read more: "http://wiki.antme.net/en/API1:Tick" /// </summary> public override void Tick() { if (norandom > 360) norandom -= 360; if (timera == null) timera = this; if (Caste == "fighter2" && spotted) { double distance, x, y; getcordsa(DistanceToAnthill, Coordinate.GetDegreesBetween(this, hill), out x, out y); getdistance(x, y, basex, basey, out distance); if (distance > 50) { } else if (!(Destination is Ant) && Destination == null && CurrentSpeed > 2) { Stop(); } } if (verfolgen != null) if (verfolgen.CurrentEnergy < verfolgen.MaximumEnergy / 2 || time - lastv > 20000) verfolgen = null; if (verfolgen != null && !spotted) Think("verfolge..."); if (verfolgen != null) if ((verfolgen.CurrentLoad < verfolgen.MaximumLoad / 5 && verfolgen.CarriedFruit == null) && !spotted) { double xm, ym, xtm, ytm, x, y; getcordsa(DistanceToAnthill, Coordinate.GetDegreesBetween(this, hill), out xm, out ym); getcordsa(Coordinate.GetDistanceBetween(verfolgen, this), Coordinate.GetDegreesBetween(verfolgen, this), out xtm, out ytm); x = xm + xtm; y = ym + ytm; basex = x; basey = y; if (Caste == "fighter2") { TurnToDetination(verfolgen); GoForward(Coordinate.GetDistanceBetween(this, verfolgen)); Think("Infiltriere"); } Think("gefunden; X:" + Convert.ToInt32(x) + " Y: " + Convert.ToInt32(y)); spotted = true; verfolgen = null; } if (Caste == "fighter2" && spotted) return; double xt, yt; getcordsa(DistanceToAnthill, Coordinate.GetDegreesBetween(this, hill), out xt, out yt); if (Caste == "star") Think("star"); if (hill == null) { GoToAnthill(); hill = Destination as Anthill; Stop(); } if (this == timera) { time += 50; } if (Caste == "star") return; if (Caste == "north") { if (DistanceToAnthill > entfernung[0]) { entfernung[0] = DistanceToAnthill; } else if (DistanceToAnthill < entfernung[0] - 5) absolute[0] = true; Think(entfernung[0].ToString() + absolute[0]); if (DistanceToAnthill == 0 && Direction != 270) { if (Direction != 270) TurnToDirection(270); if (!absolute[0]) GoForward(); else GoForward(50); } return; } if (Caste == "east") { if (DistanceToAnthill > entfernung[1]) { entfernung[1] = DistanceToAnthill; } else if (DistanceToAnthill < entfernung[1] - 5) absolute[1] = true; Think(entfernung[1].ToString() + absolute[1]); if (DistanceToAnthill == 0 && Direction != 0) { if (Direction != 0) TurnToDirection(0); if (!absolute[1]) GoForward(); else GoForward(50); } return; } if (Caste == "south") { if (DistanceToAnthill > entfernung[2]) { entfernung[2] = DistanceToAnthill; } else if (DistanceToAnthill < entfernung[2] - 5) absolute[2] = true; Think(entfernung[2].ToString() + absolute[2]); if (DistanceToAnthill == 0 && Direction != 90) { if (Direction != 90) TurnToDirection(90); if (!absolute[2]) GoForward(); else GoForward(50); } return; } if (Caste == "west") { if (DistanceToAnthill >= entfernung[3]) { entfernung[3] = DistanceToAnthill; } else if (DistanceToAnthill < entfernung[3] - 5) absolute[3] = true; Think(entfernung[3].ToString() + absolute[3]); if (DistanceToAnthill == 0 && Direction != 180) { if (Direction != 180) TurnToDirection(180); if (!absolute[3]) GoForward(); else GoForward(50); } return; } for (int i = 0; i < 100; i++) { if (hill == null) { GoToAnthill(); hill = Destination as Anthill; Stop(); } double x, y, direction, distance; getcordsa(DistanceToAnthill, Coordinate.GetDegreesBetween(this, hill), out x, out y); } if (hill == null) { GoToAnthill(); hill = Destination as Anthill; Stop(); } if (Destination is Bug) { for (int i = 0; i < aimedbug.Length; i++) { if (Destination == aimedbug[i]) { break; } else if (i == aimedbug.Length - 1 || aimedbug[i] == null && (Caste != "sugar" || (CurrentLoad < MaximumLoad / 10)) && (Caste != "fighter2" || !spotted)) { GoToDestination(hill); return; } } } else if (Caste == "fighter" && (Caste != "fighter2" || !spotted) && (IsTired || WalkedRange > Range / 3 || DistanceToAnthill > (Range - WalkedRange) / 1.5) && (Caste != "sugar" || (CurrentLoad < MaximumLoad / 10)) && (Caste != "fighter2" || !spotted)) { if (DistanceToAnthill > 10) { if (Direction != Coordinate.GetDegreesBetween(this, hill)) TurnToDetination(hill); GoForward(DistanceToAnthill - 5); } else GoToDestination(hill); return; } if (Caste == "fighter" && (Caste != "fighter2" || !spotted) && (IsTired || WalkedRange > Range / 3 || DistanceToAnthill > (Range - WalkedRange) / 1.5) && (Destination == null || !(Destination is Bug)) && (Caste != "sugar" || (CurrentLoad < MaximumLoad / 10)) && (Caste != "fighter2" || !spotted)) { if (DistanceToAnthill > 10) { if (Direction != Coordinate.GetDegreesBetween(this, hill)) TurnToDetination(hill); GoForward(DistanceToAnthill - 5); } else GoToDestination(hill); return; } if ((((Caste != "fighter" && (Caste != "fighter2" || !spotted)) || Destination == null && Caste != "fighter")) && IsTired && (Caste != "sugar" || (CurrentLoad < MaximumLoad / 10)) && (Caste != "fighter2" || !spotted) && (Caste != "sugar" || (CurrentLoad < MaximumLoad / 10))) { if (DistanceToAnthill > 10) { if (Direction != Coordinate.GetDegreesBetween(this, hill)) TurnToDetination(hill); GoForward(DistanceToAnthill - 5); } else { GoToDestination(hill); } return; } if (hill != null) { for (int i = 0; i < 300; i++) { double distance, x, y; getcordsa(Coordinate.GetDistanceBetween(this, hill), Coordinate.GetDegreesBetween(this, hill), out x, out y); getdistance(x, y, aimedposition[i, 0], aimedposition[i, 1], out distance); if ((time - lastact[i] > 10000) && aimedbug[i] != null) { Think(i + "vergessen " + Convert.ToInt32(distance)); aimedbug[i] = null; for (int u = 2; u < 300; u++) { verfugbar[i, u] = 0; } verfugbar[i, 1] = 0; verfugbar[i, 0] = 0; aimedposition[i, 0] = 0; aimedposition[i, 1] = 0; wirdangegriffen[Ameisenliste.IndexOf(this)] = 0; } } } if (hill == null) { GoToAnthill(); hill = Destination as Anthill; Stop(); } if (CarryingFruit != null && DistanceToAnthill < 20) { Think("Aha"); for (int i = 0; i < 10; i++) { if (apple[i] == CarryingFruit) { Think("weg"); apple[i] = null; for (int u = 0; u < 10; u++) { apfelliste[i, u] = 0; } } } } for (int i = 0; i < 10; i++) { if (apple[i] != null) if (time - lastacta[i] > 1000 || apple[i].Amount == 0) { Think("weg"); apple[i] = null; for (int u = 0; u < 10; u++) { apfelliste[i, u] = 0; } } } if (CarryingFruit != null) { for (int i = 0; i < 10; i++) { if (apple[i] == CarryingFruit) { Think(apfelliste[i, 1].ToString()); lastacta[i] = time; break; } } } if (Caste == "fighter" && Destination == null) { } for (int i = 0; i < 4; i++) { if (zucker[i] != null) if (zucker[i].Amount == 0) { Think("gelöscht"); zucker[i] = null; for (int u = 0; u < 100; u++) { unterwegs[i, u] = 0; } aimedsugar[i, 0] = 3000000; aimedsugar[i, 1] = 3000000; } } if (CarryingFruit != null && Direction != Coordinate.GetDegreesBetween(this, hill)) { TurnToDetination(hill); if (DistanceToAnthill > 20) GoForward(DistanceToAnthill - 10); else GoToAnthill(); } }
/// <summary> /// Writes a new state to the stream. /// </summary> /// <param name="state">New state</param> public void Write(SimulationState state) { serializer.Write(BlockType.FrameStart); int[] keys; #region Framestart // The first call creates the frame if (frame == null) { // Create new frame frame = new Frame(state); serializer.Write(BlockType.Frame, frame); } else { // Send frame-update FrameUpdate update = frame.GenerateUpdate(state); if (update != null) { serializer.Write(BlockType.FrameUpdate, update); } } #endregion #region Teams and ColonyStates #region ant-reset // reset alive-flag foreach (Ant ant in antList.Values) { ant.IsAlive = false; } #endregion #region marker-reset // reset alive-flag foreach (Marker marker in markerList.Values) { marker.IsAlive = false; } #endregion // Teams are static and need no update // enumerate all teams foreach (TeamState teamState in state.TeamStates) { // Check, if team is known if (teamList.ContainsKey(teamState.Id)) { // No Teamupdate needed } else { Team team = new Team(teamState); serializer.Write(BlockType.Team, team); teamList.Add(teamState.Id, team); } // ColonyStates are static and need no update // enumerate all colonies foreach (ColonyState colonyState in teamState.ColonyStates) { // Check, if colony is known if (colonyList.ContainsKey(colonyState.Id)) { // colony-update ColonyUpdate update = colonyList[colonyState.Id].GenerateUpdate(colonyState); if (update != null) { serializer.Write(BlockType.ColonyUpdate, update); } colonyList[colonyState.Id].Interpolate(); } else { // new colony Colony colony = new Colony(colonyState, teamState.Id); serializer.Write(BlockType.Colony, colony); colonyList.Add(colonyState.Id, colony); casteList.Add(colonyState.Id, new Dictionary <int, Caste>()); #region Castes // Casts are static and need no update Dictionary <int, Caste> castes = casteList[colonyState.Id]; // enumerate casts for (ushort i = 0; i < colonyState.CasteStates.Count; i++) { // Check, if caste is known if (!castes.ContainsKey(i)) { // add caste Caste caste = new Caste(colonyState.CasteStates[i]); serializer.Write(BlockType.Caste, caste); } } #endregion #region Anthills // Anthills are static and need no update // enumerate anthills foreach (AnthillState anthill in colonyState.AnthillStates) { if (!anthillList.ContainsKey(anthill.Id)) { Anthill hill = new Anthill(anthill); serializer.Write(BlockType.Anthill, hill); anthillList.Add(anthill.Id, hill); } } #endregion } #region Ants // enumerate ants foreach (AntState antState in colonyState.AntStates) { // Check, if ant is known if (antList.ContainsKey(antState.Id)) { // ant-update AntUpdate update = antList[antState.Id].GenerateUpdate(antState); if (update != null) { serializer.Write(BlockType.AntUpdate, update); } antList[antState.Id].Interpolate(); } else { // create ant Ant ant = new Ant(antState); serializer.Write(BlockType.Ant, ant); antList.Add(ant.Id, ant); } antList[antState.Id].IsAlive = true; } #endregion #region Marker // enumerate marker foreach (MarkerState markerState in colonyState.MarkerStates) { // Check, if marker is known if (markerList.ContainsKey(markerState.Id)) { // marker-update MarkerUpdate update = markerList[markerState.Id].GenerateUpdate(markerState); if (update != null) { serializer.Write(BlockType.MarkerUpdate, update); } markerList[markerState.Id].Interpolate(); } else { // create marker Marker marker = new Marker(markerState); serializer.Write(BlockType.Marker, marker); markerList.Add(markerState.Id, marker); } markerList[markerState.Id].IsAlive = true; } #endregion } } #region Ant-Cleanup // remove dead ants keys = new int[antList.Keys.Count]; antList.Keys.CopyTo(keys, 0); for (int i = 0; i < keys.Length; i++) { if (!antList[keys[i]].IsAlive) { serializer.Write(BlockType.AntLost, new Lost(keys[i])); antList.Remove(keys[i]); } } #endregion #region Marker-Cleanup // remove dead marker keys = new int[markerList.Keys.Count]; markerList.Keys.CopyTo(keys, 0); for (int i = 0; i < keys.Length; i++) { if (!markerList[keys[i]].IsAlive) { serializer.Write(BlockType.MarkerLost, new Lost(keys[i])); markerList.Remove(keys[i]); } } #endregion #endregion #region Fruit // reset alive-flag foreach (Fruit fruit in fruitList.Values) { fruit.IsAlive = false; } // enumerate fruit foreach (FruitState fruitState in state.FruitStates) { // Check, if fruit is known if (fruitList.ContainsKey(fruitState.Id)) { // fruit-update FruitUpdate update = fruitList[fruitState.Id].GenerateUpdate(fruitState); if (update != null) { serializer.Write(BlockType.FruitUpdate, update); } fruitList[fruitState.Id].Interpolate(); } else { // create fruit Fruit fruit = new Fruit(fruitState); serializer.Write(BlockType.Fruit, fruit); fruitList.Add(fruitState.Id, fruit); } fruitList[fruitState.Id].IsAlive = true; } // remove dead fruits keys = new int[fruitList.Keys.Count]; fruitList.Keys.CopyTo(keys, 0); for (int i = 0; i < keys.Length; i++) { if (!fruitList[keys[i]].IsAlive) { serializer.Write(BlockType.FruitLost, new Lost(keys[i])); fruitList.Remove(keys[i]); } } #endregion #region Sugar // reset alive-flag foreach (Sugar sugar in sugarList.Values) { sugar.IsAlive = false; } // enumerate sugar foreach (SugarState sugarState in state.SugarStates) { // Check, if sugar is known if (sugarList.ContainsKey(sugarState.Id)) { // sugar-update SugarUpdate update = sugarList[sugarState.Id].GenerateUpdate(sugarState); if (update != null) { serializer.Write(BlockType.SugarUpdate, update); } sugarList[sugarState.Id].Interpolate(); } else { // create sugar Sugar sugar = new Sugar(sugarState); serializer.Write(BlockType.Sugar, sugar); sugarList.Add(sugarState.Id, sugar); } sugarList[sugarState.Id].IsAlive = true; } // remove dead sugar keys = new int[sugarList.Keys.Count]; sugarList.Keys.CopyTo(keys, 0); for (int i = 0; i < keys.Length; i++) { if (!sugarList[keys[i]].IsAlive) { serializer.Write(BlockType.SugarLost, new Lost(keys[i])); sugarList.Remove(keys[i]); } } #endregion #region Bugs // reset alive-flag foreach (Bug bug in bugList.Values) { bug.IsAlive = false; } // enumerate bugs foreach (BugState bugState in state.BugStates) { // Check, if bug is known if (bugList.ContainsKey(bugState.Id)) { // bug-update BugUpdate update = bugList[bugState.Id].GenerateUpdate(bugState); if (update != null) { serializer.Write(BlockType.BugUpdate, update); } bugList[bugState.Id].Interpolate(); } else { // create bug Bug bug = new Bug(bugState); serializer.Write(BlockType.Bug, bug); bugList.Add(bugState.Id, bug); } bugList[bugState.Id].IsAlive = true; } // remove dead bugs keys = new int[bugList.Keys.Count]; bugList.Keys.CopyTo(keys, 0); for (int i = 0; i < keys.Length; i++) { if (!bugList[keys[i]].IsAlive) { serializer.Write(BlockType.BugLost, new Lost(keys[i])); bugList.Remove(keys[i]); } } #endregion serializer.Write(BlockType.FrameEnd); }
public override void Init(Ant ant) { base.Init(ant); anthill = ant.anthill; }