public float GetIntensityF(int playerId, PheromoneType pheromoneType) { PheromoneType lookForThis = pheromoneType; if (pheromoneType == PheromoneType.AwayFromEnergy) { lookForThis = PheromoneType.Energy; } float intensity = 0; foreach (PheromoneItem pheromoneItem in PheromoneItems) { if ((playerId == 0 || pheromoneItem.PlayerId == playerId) && pheromoneItem.PheromoneType == lookForThis) { intensity += pheromoneItem.Intensity; } } if (intensity > 1) { intensity = 1; } if (pheromoneType == PheromoneType.AwayFromEnergy) { intensity = 1 - intensity; if (intensity < 0.1f) { intensity = 0.1f; } } return(intensity); }
public PheromoneItem Deposit(int playerId, float intensity, PheromoneType pheromoneType, bool isStatic) { PheromoneItem pheromoneItem = new PheromoneItem(); pheromoneItem.PlayerId = playerId; pheromoneItem.Intensity = intensity; pheromoneItem.PheromoneType = pheromoneType; pheromoneItem.IsStatic = isStatic; PheromoneItems.Add(pheromoneItem); /* * if (pheromoneType == PheromoneType.ToFood) * { * IntensityHome += intensity; * if (IntensityHome > 1) * IntensityHome = 1; * } * if (pheromoneType == PheromoneType.ToHome) * { * IntensityFood += intensity; * if (IntensityFood > 1) * IntensityFood = 1; * }*/ return(pheromoneItem); }
public void RemoveAllStaticPheromones(Player player, PheromoneType pheromoneType) { List <Pheromone> tobeRemoved = new List <Pheromone>(); foreach (Pheromone pheromone in Items.Values) { List <PheromoneItem> itemTobeRemoved = new List <PheromoneItem>(); foreach (PheromoneItem pheromoneItem in pheromone.PheromoneItems) { if (pheromoneItem.PlayerId == player.PlayerModel.Id && pheromoneItem.PheromoneType == pheromoneType) { itemTobeRemoved.Add(pheromoneItem); } } foreach (PheromoneItem pheromoneItem1 in itemTobeRemoved) { pheromone.PheromoneItems.Remove(pheromoneItem1); } if (pheromone.PheromoneItems.Count == 0) { tobeRemoved.Add(pheromone); } } foreach (Pheromone pheromone1 in tobeRemoved) { Items.Remove(pheromone1.Pos); } }
/* * * // Home Trail * float Settings::HOME_TRAIL_DEPOSIT_RATE = 0.5f; * float Settings::HOME_TRAIL_FORGET_RATE = 0.01f; * float Settings::HOME_ALPHA = 0.5f; * float Settings::HOME_BETA = 0.8f; * * // Food Trail * float Settings::FOOD_TRAIL_DEPOSIT_RATE = 0.2f; * float Settings::FOOD_TRAIL_FORGET_RATE = 0.05f; * float Settings::FOOD_ALPHA = 0.5f; * float Settings::FOOD_BETA = 1.0f; * * void PheromoneAnt::DepositPheromone() * { * float foodStorage = this->GetFoodStorage(); * float energy = this->GetEnergy(); * * // If ant is carrying food it leaves food trails, otherwise leaves home trail * if (foodStorage > 0) * { * // Leave food pheromobe trail * m_grid->DepositPheromone(m_position, PheromoneType::food, (GetFoodStorage() / Settings::MAX_FOOD_STORAGE) * Settings::FOOD_TRAIL_DEPOSIT_RATE); * } * else * { * // Leave home pheromone trail * m_grid->DepositPheromone(m_position, PheromoneType::home, (GetEnergy() / Settings::MAX_ENERGY) * Settings::HOME_TRAIL_DEPOSIT_RATE); * } * }*/ private void DropPheromone(Player player, PheromoneType pheromoneType) { // For now //if (player.PlayerModel.Id == 9999) { if (pheromoneType == PheromoneType.ToHome) { Energy -= 3; if (Energy < 0) { Energy = 0; return; } } else { if (GotLostNoWayHome) { // Do not drop phromone to food if not on the way home return; } FoodIntensity -= 5; if (FoodIntensity < 0) { FoodIntensity = 0; //return; } } Unit cntrlUnit = PlayerUnit.Unit; /* * if (pheromoneType == PheromoneType.Food && StepsFromHome >= Pheromone.MaxStepsFromHome) * return; * * if (pheromoneType == PheromoneType.Home && StepsFromFood >= Pheromone.MaxStepsFromFood) * return; */ /* * Pheromone pheromone = Pheromones.FindAt(cntrlUnit.Pos); * if (pheromone == null) * { * pheromone = new Pheromone(); * pheromone.Pos = cntrlUnit.Pos; * Pheromones.Add(pheromone); * }*/ float intensity; switch (pheromoneType) { case PheromoneType.ToHome: intensity = (Energy / MaxEnergy) * HomeTrailDepositRate; //pheromone.Deposit(intensity, pheromoneType, false); break; case PheromoneType.ToFood: intensity = 0.5f; // (FoodIntensity / MaxFoodIntensity) * FoodTrailDepositRate; player.Game.Pheromones.Deposit(player, cntrlUnit.Pos, pheromoneType, intensity, false); break; } } }
public Pheromone(PheromoneType type, Vector3 target, Bug dropper) { Type = type; Target = target; Dropper = dropper; m_InstanciationTime = Time.time; }
public Pheromone(PheromoneType type, Vector3 target, Bug dropper) { Type = type; Target = target; Dropper = dropper; m_InstanciationTime = Time.time; }
private void Awake() { InvokeRepeating("GetNetPos", 0, 1); type = PheromoneType.Queen; targetType = PheromoneType.Food; // pheromoneScale = 100; pos = PheromoneMap.GetPos(this.transform.position); nextPos = pos; targetLayer = (1 << LayerMask.NameToLayer("Food")); }
public void SetPheromoneType(PheromoneType pheromoneType) { myPheromoneType = pheromoneType; if (myPheromoneType == PheromoneType.ToHome) { gameObject.GetComponentInChildren <Renderer>().material.SetColor("_Color", Color.white); } else if (myPheromoneType == PheromoneType.ToFood) { gameObject.GetComponentInChildren <Renderer>().material.SetColor("_Color", Color.green); } }
public PheromoneItem Deposit(Player player, Position pos, PheromoneType pheromoneType, float intensity, bool isStatic) { Pheromone pheromone; if (!Items.ContainsKey(pos)) { pheromone = new Pheromone(); pheromone.Pos = pos; player.Game.Pheromones.Add(pheromone); } else { pheromone = Items[pos]; } return(pheromone.Deposit(player.PlayerModel.Id, intensity, pheromoneType, isStatic)); }
public int DropPheromones(Player player, Position pos, int range, PheromoneType pheromoneType, float intensity, bool isStatic) { PheromoneStack pheromoneStack = new PheromoneStack(); PheromoneStack.pheromoneStackCounter++; int counter = PheromoneStack.pheromoneStackCounter; pheromoneStacks.Add(counter, pheromoneStack); Dictionary <Position, TileWithDistance> tiles = player.Game.Map.EnumerateTiles(pos, range, false); foreach (TileWithDistance tileWithDistance in tiles.Values) { float totaldistance = range - tileWithDistance.Distance; float distance = (totaldistance * 100) / range / 100; float relativIntensity = distance * intensity; PheromoneStackItem pheromoneStackItem = new PheromoneStackItem(); Pheromone pheromone; pheromone = FindAt(tileWithDistance.Pos); if (pheromone == null) { pheromone = new Pheromone(); pheromone.Pos = tileWithDistance.Pos; player.Game.Pheromones.Add(pheromone); } pheromoneStackItem.Pheromone = pheromone; pheromoneStackItem.Distance = distance; pheromoneStackItem.PheromoneItem = pheromone.Deposit(player.PlayerModel.Id, relativIntensity, pheromoneType, isStatic); pheromoneStack.PheromoneItems.Add(pheromoneStackItem); } return(counter); }
private void SetPheromone() { if (pheromoneScale <= 10f) { targetType = PheromoneType.Queen; type = PheromoneType.none; targetLayer = (1 << LayerMask.NameToLayer("Queen")); return; } if (type == PheromoneType.none) { return; } var p = pheromone.GetPreomone(pos); //Debug.Log(pos[0] + "," + pos[1]); p.scale[type] += pheromoneScale; if (p.scale[type] > Pheromone.MaxScale) { p.scale[type] = Pheromone.MaxScale; } pheromoneScale *= 0.7f; }
private void TargetCheck() { if (Physics.CheckSphere(transform.position, 0.1f, targetLayer)) { if (targetType == PheromoneType.Food) { targetType = PheromoneType.Queen; foods = 100; targetLayer = (1 << LayerMask.NameToLayer("Queen")); type = PheromoneType.Food; pheromoneScale = 200; } else if (targetType == PheromoneType.Queen) { targetType = PheromoneType.Food; foods = 0; type = PheromoneType.Queen; targetLayer = (1 << LayerMask.NameToLayer("Food")); pheromoneScale = 200; queen.food += 200; } SetPheromone(); } }
public PheromoneDescriptor(PheromoneType type, HexDirection direction) { this.type = type; this.direction = direction; }
public bool MoveUnit(Player player, List <Move> moves) { if (MoveAttempts > 0) { } Unit cntrlUnit = PlayerUnit.Unit; // Follow trail if possible. Position moveToPosition = null; if (FollowThisRoute != null) { if (FollowThisRoute.Count == 0) { FollowThisRoute = null; } else { moveToPosition = FollowThisRoute[0]; if (Control.IsOccupied(player, moves, moveToPosition)) { moveToPosition = null; FollowThisRoute = null; } else { FollowThisRoute.RemoveAt(0); if (FollowThisRoute.Count == 0) { FollowThisRoute = null; } } } } List <Tile> tiles = MakeForwardTilesList(player, cntrlUnit); /* * Tile tileForward = GetNextPosition(player, cntrlUnit.Pos, cntrlUnit.Direction, moves); * if (tileForward != null) tiles.Add(tileForward); * * Tile tileLeft = GetNextPosition(player, cntrlUnit.Pos, TurnLeft(cntrlUnit.Direction), moves); * if (tileLeft != null) tiles.Add(tileLeft); * * Tile tileRight = GetNextPosition(player, cntrlUnit.Pos, TurnRight(cntrlUnit.Direction), moves); * if (tileRight != null) tiles.Add(tileRight); */ PheromoneType pheromoneType = PheromoneType.AwayFromEnergy; // Minerals needed? if (cntrlUnit.Weapon != null && !cntrlUnit.Weapon.WeaponLoaded) { pheromoneType = PheromoneType.ToFood; } else if (IsWorker) { if (cntrlUnit.Container != null && cntrlUnit.Container.Metal < cntrlUnit.Container.Capacity) { // Fill up with food! pheromoneType = PheromoneType.ToFood; } else { // Look for a target to unload pheromoneType = PheromoneType.ToHome; } } else if (!IsWorker) { pheromoneType = PheromoneType.Enemy; } List <AntDestination> possibleTiles = ComputePossibleTiles(player, tiles, pheromoneType); if (possibleTiles.Count == 0 && pheromoneType == PheromoneType.ToFood) { moveToPosition = Control.FindFood(player, this); if (moveToPosition != null && Control.IsOccupied(player, moves, moveToPosition)) { moveToPosition = null; FollowThisRoute = null; } } if (IsWorker && possibleTiles.Count == 0 && pheromoneType == PheromoneType.ToHome) { moveToPosition = Control.FindContainer(player, this); if (moveToPosition != null && Control.IsOccupied(player, moves, moveToPosition)) { moveToPosition = null; FollowThisRoute = null; } } if (possibleTiles.Count == 0 && pheromoneType == PheromoneType.Enemy) { moveToPosition = Control.FindEnemy(player, this); if (moveToPosition != null && Control.IsOccupied(player, moves, moveToPosition)) { moveToPosition = null; FollowThisRoute = null; } } if (moveToPosition == null) { if (possibleTiles.Count == 0 && pheromoneType != PheromoneType.AwayFromEnergy) { // Fighter may try to move to border until food is found pheromoneType = PheromoneType.AwayFromEnergy; possibleTiles = ComputePossibleTiles(player, tiles, pheromoneType); } /* * if (possibleTiles.Count == 0 && StuckCounter > 0) * { * Tile tileHardRight; * Tile tileHardLeft; * tileHardRight = GetNextPosition(player, cntrlUnit.Pos, TurnRight(TurnRight(cntrlUnit.Direction)), moves); * AddDestination(possibleTiles, player, tileHardRight, 0.3f, true, pheromoneType); * * tileHardLeft = GetNextPosition(player, cntrlUnit.Pos, TurnLeft(TurnLeft(cntrlUnit.Direction)), moves); * AddDestination(possibleTiles, player, tileHardLeft, 0.3f, true, pheromoneType); * * tileHardLeft = GetNextPosition(player, cntrlUnit.Pos, TurnAround(cntrlUnit.Direction), moves); * AddDestination(possibleTiles, player, tileHardLeft, 0.1f, true, pheromoneType); * }*/ AntDestination moveToTile = null; while (possibleTiles.Count > 0 && moveToTile == null) { moveToTile = FindBest(player, possibleTiles); if (moveToTile == null) { break; } if (Control.IsOccupied(player, moves, moveToTile.Tile.Pos)) { possibleTiles.Remove(moveToTile); moveToTile = null; } } if (moveToTile == null) { if (pheromoneType == PheromoneType.AwayFromEnergy) { // out of reactor range moveToPosition = Control.FindReactor(player, this); if (moveToPosition != null && Control.IsOccupied(player, moves, moveToPosition)) { moveToPosition = null; FollowThisRoute = null; } else { cntrlUnit.Direction = TurnAround(cntrlUnit.Direction); return(true); } } } else { moveToPosition = moveToTile.Tile.Pos; } } /* * if (moveToTile != null && StuckCounter > 2) * { * // If it is really occupied * if (Control.IsOccupied(player, moves, moveToTile.Tile.Pos)) * { * moveToTile = null; * } * }*/ /* Does not help * if (moveToTile != null) // && ReturnHome) * { * // Wrong way check * List<AntDestination> checkPossibleTiles = new List<AntDestination>(); * AddDestination(checkPossibleTiles, player, moveToTile.Tile, 1f, true, pheromoneType); * * Tile back = GetNextPosition(player, cntrlUnit.Pos, TurnAround(cntrlUnit.Direction), moves); * AddDestination(checkPossibleTiles, player, back, 1f, true, pheromoneType); * if (checkPossibleTiles.Count == 2) * { * AntDestination isBackBetter = FindBest(player, possibleTiles, pheromoneType); * if (isBackBetter != null && isBackBetter != moveToTile) * { * moveToTile = isBackBetter; * } * } * }*/ /* * if (moveToTile == null || StuckCounter > 2) * { * // No pheromone found, move randomly * if (ReturnHome) // pheromoneType == PheromoneType.ToHome) * { * //return GoHome(player, moves, true); * GotLostNoWayHome = true; * } * else * { * // Smell Food * bool smelled = SmellFood(player, moves); * if (smelled) * { * return Move(player, moves, pheromoneType); * } * } * * possibleTiles.Clear(); * // Add tiles without pheromones * AddDestination(possibleTiles, player, tileForward, 1f, true, PheromoneType.None); * AddDestination(possibleTiles, player, tileLeft, 1f, true, PheromoneType.None); * AddDestination(possibleTiles, player, tileRight, 1f, true, PheromoneType.None); * * // Move random forward * while (moveToTile == null && possibleTiles.Count > 0) * { * int idx = player.Game.Random.Next(possibleTiles.Count); * moveToTile = possibleTiles[idx]; * * if (Control.IsOccupied(player, moves, moveToTile.Tile.Pos)) * { * possibleTiles.Remove(moveToTile); * moveToTile = null; * } * } * * if (moveToTile == null) * { * // Move random backward * possibleTiles.Clear(); * * Tile tileHardRight; * Tile tileHardLeft; * tileHardRight = GetNextPosition(player, cntrlUnit.Pos, TurnRight(TurnRight(cntrlUnit.Direction)), moves); * AddDestination(possibleTiles, player, tileHardRight, 0.3f, true, PheromoneType.None); * * tileHardLeft = GetNextPosition(player, cntrlUnit.Pos, TurnLeft(TurnLeft(cntrlUnit.Direction)), moves); * AddDestination(possibleTiles, player, tileHardLeft, 0.3f, true, PheromoneType.None); * * tileHardLeft = GetNextPosition(player, cntrlUnit.Pos, TurnAround(cntrlUnit.Direction), moves); * AddDestination(possibleTiles, player, tileHardLeft, 0.1f, true, PheromoneType.None); * * while (moveToTile == null && possibleTiles.Count > 0) * { * int idx = player.Game.Random.Next(possibleTiles.Count); * moveToTile = possibleTiles[idx]; * * if (Control.IsOccupied(player, moves, moveToTile.Tile.Pos)) * { * possibleTiles.Remove(moveToTile); * moveToTile = null; * } * } * } * } */ /* * if (moveToTile != null) * { * if (CheckHandover(moveToTile.Tile.Pos, moves)) * return true; * * // If it is really occupied * if (Control.IsOccupied(player, moves, moveToTile.Tile.Pos)) * { * moveToTile = null; * } * }*/ if (moveToPosition != null && Control.IsOccupied(player, moves, moveToPosition)) { moveToPosition = null; } Move move = null; if (moveToPosition != null) { move = new Move(); move.MoveType = MoveType.Move; move.UnitId = cntrlUnit.UnitId; move.PlayerId = player.PlayerModel.Id; move.Positions = new List <Position>(); move.Positions.Add(cntrlUnit.Pos); move.Positions.Add(moveToPosition); moves.Add(move); /* * if (dropPheromone && !NothingFound) * { * if (pheromoneType == PheromoneType.ToFood) * DropPheromone(player, PheromoneType.ToHome); * else if (pheromoneType == PheromoneType.ToHome) * DropPheromone(player, PheromoneType.ToFood); * }*/ } return(move != null); }
private List <AntDestination> ComputePossibleTiles(Player player, List <Tile> tiles, PheromoneType pheromoneType) { List <Tile> copyOfTiles = new List <Tile>(); copyOfTiles.AddRange(tiles); List <AntDestination> possibleTiles = new List <AntDestination>(); while (copyOfTiles.Count > 0) { int idx = player.Game.Random.Next(copyOfTiles.Count); Tile t = copyOfTiles[idx]; AddDestination(possibleTiles, player, t, 0.6f, true, pheromoneType); copyOfTiles.RemoveAt(idx); } return(possibleTiles); }
public void AddDestination(List <AntDestination> possibleTiles, Player player, Tile t, float phem_d, bool onlyIfMovable, PheromoneType pheromoneType) { if (t != null) { if (onlyIfMovable && !t.CanMoveTo()) { return; } AntDestination antDestination = new AntDestination(); antDestination.Tile = t; antDestination.phem_d = phem_d; if (pheromoneType != PheromoneType.None) { antDestination.Pheromone = player.Game.Pheromones.FindAt(t.Pos); if (antDestination.Pheromone == null) { return; } float intensity = antDestination.Pheromone.GetIntensityF(player.PlayerModel.Id, pheromoneType); if (pheromoneType == PheromoneType.AwayFromEnergy) { if (intensity == 1) { return; } } else { if (intensity == 0) { return; } } antDestination.Intensity = intensity; } possibleTiles.Add(antDestination); } }
public PheromoneDigest(PheromoneType type, HexDirection direction) { this.type = type; this.direction = direction; }