private void FixedUpdate() { if (ToCentered().sqrMagnitude < 1) { transform.position += ToCentered() * 0.5f; body.velocity *= 0.5f; } else { body.velocity *= 0.9f; body.velocity += 0.1f * ((Vector2)ToCentered()).normalized * 50 * energy; } if (Mathf.Abs(ToCentered().y) < 2f && Mathf.Abs(ToCentered().x) <= 8f) { var moves = new ChoiceStack <twin>(); moves.AddManyThenLock(twin.down); moves.AddManyThenLock(twin.left, twin.right); moves.RemoveAll(-lastMove); lastMove = moves.GetFirstTrue(TryMove); if (lastMove == twin.down) { energy = 1f; } else if (energy > .1f) { energy -= .1f; } } }
public override void ChooseNextMove() { if (windStrength > 0) { var options = new ChoiceStack <twin>(); options.AddManyThenLock(twin.right, twin.up, twin.left, twin.down); options.RemoveAll(-lastMove); options.GetFirstTrue(TryMove); // move!!! windStrength--; } else { gameObject.name = "plant"; box.isTrigger = false; gameObject.layer = LayerMask.NameToLayer("Solid"); if (CanMoveTo(my_cell_pos)) { BeginPregnancy(); } else { Object.Destroy(gameObject); } } spriter.sprite = spritelot[IsPregnant() ? 6 : 7]; }
protected void ChooseNewDir_Forward() { var dirs = new ChoiceStack <twin>(); dirs.Add(twin.down); // prefer 'down': gravity dirs.AddManyThenLock(twin.compass); dirs.RemoveAll(-lastMove); lastMove = dirs.GetFirstTrue(this.TryMove); }
protected void ChooseMove_no180() { var dirs = new ChoiceStack<twin>(); dirs.AddManyThenLock(twin.compass); dirs.RemoveAll(-lastMove); lastMove = dirs.GetFirstTrue(TryMove); if (lastMove.x != 0) { GetComponent<SpriteRenderer>().flipX = lastMove.x < 0; } }
public void TryDWMove() { twin.StraightenCompass(); ChoiceStack <twin> directions = new ChoiceStack <twin>(); if (lastMove.taxicabLength == 2) { directions.AddManyThenLock( new twin(lastMove.x, 0), new twin(0, lastMove.y), lastMove); // prefer to continue in your weird diagonal direction. } bool open_space = false; for (int i = 0; i < 4; i++) { var dira = twin.compass[i]; var dirb = twin.compass[(i + 1) % 4]; if ((dira == lastMove || dirb == lastMove) && CanMoveTo(my_cell_pos + dira) && CanMoveTo(my_cell_pos + dirb) && CanMoveTo(my_cell_pos + dira + dirb)) { directions.Add(dira + dirb); open_space = true; // moving through an open space. } } if (open_space) { directions.Add(lastMove); // in an open space, i can continue in the same direction. } directions.Lock(); directions.AddManyThenLock(twin.compass); directions.RemoveAll(-lastMove); lastMove = directions.GetFirstTrue(TryMove); }
void PickAMove() { var moves = new ChoiceStack <twin>(); moves.AddManyThenLock(twin.compass); moves.RemoveAll(-bfm.lastMove); moves.Add(-bfm.lastMove); moves.Lock(shuffle: false); twin iMoved = moves.GetFirstTrue(bfm.TryMove); if (iMoved == twin.zero) { bfm.facingDir = twin.compass[Random.Range(0, 4)]; } bfm.UpdateHelper_FaceFacingDir(); }
private void FixedUpdate() { if (IsWithinDistOfCentered(fullSpeedDist, offset: subtileOffset)) { var agents = 0; // what's in this cell? foreach (var agent in board.GetAgentsAt(my_cell_pos)) { agents++; if (agent is BaFoodSource) { if (pathNestToFood.Contains(this.my_cell_pos)) { // only take food if i know how to get back home with it. ((BaFoodSource)agent).TakeFood(); isCarryingFood = true; isWandering = false; isGoingHome = true; frustration = 0; } Dj.Tempf("Ant found food. Path has length {0}", this.pathNestToFood.Count); SnapMyCellPos(); } if (agent is BaNeonNest) { this.homeNest = (BaNeonNest)agent; if (isCarryingFood) { this.homeNest.ReceiveFood(); isCarryingFood = false; } isGoingHome = false; if (frustration > 100 + this.maxPathLength) { isWandering = true; } frustration = 0; if (isWandering) { this.maxPathLength += Random.Range(1, 3 + 1); // try a longer path this.pathNestToFood.Clear(); this.pathNestToFood.Add(my_cell_pos); } SnapMyCellPos(); } } bool do_wander = false; var on_path_index = pathNestToFood.IndexOf(my_cell_pos); if (isGoingHome && on_path_index > 0 && pathNestToFood.Count > 0 && TryMove(pathNestToFood[on_path_index - 1] - my_cell_pos)) { // ok, followed path! } else if (!isGoingHome && on_path_index >= 0 && on_path_index < pathNestToFood.Count - 1 && TryMove(pathNestToFood[on_path_index + 1] - my_cell_pos)) { // ok, followed path! } else { do_wander = true; } if (!isCarryingFood && pathNestToFood.Count > 0 && my_cell_pos == pathNestToFood[pathNestToFood.Count - 1]) { frustration = 1000; } if (do_wander) { ChoiceStack <twin> dirs = new ChoiceStack <twin>(); if (!isWandering) { // always prefer cells on the path! foreach (var dir in twin.compass) { if (pathNestToFood.Contains(my_cell_pos + dir)) { dirs.Add(dir); } } dirs.Lock(); } // move like a ghost. List <twin> forward_compass = new List <twin>(twin.compass); forward_compass.Remove(-lastMove); dirs.AddManyThenLock(forward_compass); lastMove = dirs.GetFirstTrue(TryMove); } } else { SetVelocityApproachTarget(); GetComponent <SpriteRenderer>().flipX = body.velocity.x < 0; if (bodyStuckFrames > 20) { frustration++; if (!isCarryingFood && frustration > 20) { isGoingHome = true; } bodyStuckFrames = 0; SnapMyCellPos(); RandomizeSubtileOffset(); if (Random.value < (isGoingHome?.75f : .25f)) { TryMove(-lastMove); } } } if (isCarryingFood) { GetComponent <BitsyAni>().spriteIds = new int[] { 54, 55, }; GetComponent <BitsyAni>().speed = 0.087f; } else { GetComponent <BitsyAni>().spriteIds = new int[] { 52, 53, }; GetComponent <BitsyAni>().speed = 0.050f; } }
public override void ChooseNextMove() { foreach (var mazer in master.GetBodiesAt(my_cell_pos)) { var plant = mazer.GetComponent <MazeBodyPlant>(); if (plant && plant.IsPregnant()) { fullness += 0.25f; plants_eaten++; Object.Destroy(plant.gameObject); if (plants_eaten > gluttony) { this.BeginPregnancy(); return; } else if (fullness > 1) { // overeating? reduce gluttony fullness = 1; gluttony--; } } if (mazer.GetComponent <MazeBodyHunter>()) { mazer.GetComponent <MazeBodyHunter>().Fight(this); } } var options = new ChoiceStack <twin>(); twin.StraightenCompass(); options.AddManyThenLock(twin.compass); var hunter_influence = new Vector3(); var hunter_count = 0; var herbivore_influence = new Vector3(); var herbivore_count = 0; var plant_influence = new Vector3(); var plant_count = 0; foreach (var mazer in GetMazeBodiesNear(24f)) { if (mazer.GetComponent <MazeBodyHunter>() && !mazer.GetComponent <MazeBodyHunter>().IsPregnant()) { hunter_influence += (mazer.transform.position - this.transform.position).normalized; hunter_count++; } if (mazer.GetComponent <MazeBodyHerbivore>()) { herbivore_influence += (mazer.transform.position - this.transform.position).normalized; herbivore_count++; } if (mazer.GetComponent <MazeBodyPlant>()) { if (mazer.GetComponent <MazeBodyPlant>().IsPregnant()) { plant_influence += (mazer.transform.position - this.transform.position).normalized; plant_count++; } } } hermitage = Mathf.Max(hermitage, herbivore_count); if (hunter_count > 0) // run from hunter { if (hunter_influence.x > 0) { options.RemoveAll(twin.right); } if (hunter_influence.x < 0) { options.RemoveAll(twin.left); } if (hunter_influence.y > 0) { options.RemoveAll(twin.up); } if (hunter_influence.y < 0) { options.RemoveAll(twin.down); } } else if (herbivore_count + hermitage > 20) // run from overcrowding { if (herbivore_influence.x > 0) { options.RemoveAll(twin.right); } if (herbivore_influence.x < 0) { options.RemoveAll(twin.left); } if (herbivore_influence.y > 0) { options.RemoveAll(twin.up); } if (herbivore_influence.y < 0) { options.RemoveAll(twin.down); } options.AddManyThenLock(twin.compass); } else if (plant_count > 0) // chase plant { gluttony = Mathf.Max(plant_count, gluttony); if (-plant_influence.x > 0) { options.RemoveAll(twin.right); } if (-plant_influence.x < 0) { options.RemoveAll(twin.left); } if (-plant_influence.y > 0) { options.RemoveAll(twin.up); } if (-plant_influence.y < 0) { options.RemoveAll(twin.down); } options.AddManyThenLock(twin.compass); } //else if (herbivore_count > 0 && Random.value < .5f) // stick with friends //{ // if (-herbivore_influence.x > 0) options.RemoveAll(twin.right); // if (-herbivore_influence.x < 0) options.RemoveAll(twin.left); // if (-herbivore_influence.y > 0) options.RemoveAll(twin.up); // if (-herbivore_influence.y < 0) options.RemoveAll(twin.down); // options.AddManyThenLock(twin.compass); //} else { options.RemoveAll(-lastMove); options.AddManyThenLock(-lastMove); } lastMove = options.GetFirstTrue(TryMove); }
public override void ChooseNextMove() { foreach (var mazer in master.GetBodiesAt(my_cell_pos)) { if (mazer.GetComponent <MazeBodyHerbivore>() != null) { Fight(mazer.GetComponent <MazeBodyHerbivore>()); return; } } var options = new ChoiceStack <twin>(); twin.StraightenCompass(); options.AddManyThenLock(twin.compass); var hunter_influence = new Vector3(); var hunter_count = 0; var herbivore_influence = new Vector3(); var herbivore_count = 0; foreach (var mazer in GetMazeBodiesNear(24f)) { if (mazer.GetComponent <MazeBodyHunter>()) { hunter_influence += (mazer.transform.position - this.transform.position).normalized; hunter_count++; } if (mazer.GetComponent <MazeBodyHerbivore>()) { herbivore_influence += (mazer.transform.position - this.transform.position).normalized; herbivore_count++; } } if (hunter_count > 0) // avoid other hunter { if (hunter_influence.x > 0) { options.RemoveAll(twin.right); } if (hunter_influence.x < 0) { options.RemoveAll(twin.left); } if (hunter_influence.y > 0) { options.RemoveAll(twin.up); } if (hunter_influence.y < 0) { options.RemoveAll(twin.down); } fullness -= 0.02f * hunter_count; // being near other hunters is stressful (we fight) if (hunter_count > 5) { overcrowded = true; } options.AddManyThenLock(twin.compass); } else if (herbivore_count > 0) // otherwise, approach herbivores { if (-herbivore_influence.x > 0) { options.RemoveAll(twin.right); } if (-herbivore_influence.x < 0) { options.RemoveAll(twin.left); } if (-herbivore_influence.y > 0) { options.RemoveAll(twin.up); } if (-herbivore_influence.y < 0) { options.RemoveAll(twin.down); } if (herbivore_count > 5) { killForSport = true; // too many? murder murder murder } options.AddManyThenLock(twin.compass); } else { killForSport = false; overcrowded = false; // no hunters/herbivores nearby? if (TryMove(lastMove)) // then just go straight if possible. { options = null; } } if (options != null) { options.RemoveAll(-lastMove); // hunters don't like turning back lastMove = options.GetFirstTrue(TryMove); } }