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); }
public void TakeAnyMove() { var dirs = new ChoiceStack <twin>(twin.compass); if (no_turn_around) { dirs.RemoveAll(-lastMove); } lastMove = dirs.GetFirstTrue(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; } }
private void FixedUpdate() { if (IsWithinDistOfCentered(2f)) { var dirs = new ChoiceStack <twin>(twin.compass); dirs.RemoveAll(-lastMove); lastMove = dirs.GetFirstTrue(TryMove); } else { body.velocity = body.velocity * .5f + .5f * (Vector2)ToCentered() * speed; } }
// do nothing special. private void FixedUpdate() { if (IsWithinDistOfCentered(2f)) { // dope var dirs = new ChoiceStack <twin>(twin.compass); dirs.RemoveAll(-lastMove); lastMove = dirs.GetFirstTrue(TryMove); } else { GetComponent <Rigidbody2D>().velocity = moveSpeed * ToCentered().normalized; } }
private void TryToSpawnBranch() { if (branchDelay > 0) { branchDelay -= .5f + .75f * xxi.energyPerNode; // no branch, just growth } else { //ChoiceStack<twin> dirs; //if (parent == null || parent.my_cell_pos.y == this.my_cell_pos.y) //{ // dirs = new ChoiceStack<twin>(twin.up, twin.up + twin.left, twin.up + twin.right); //} //else //{ // dirs = new ChoiceStack<twin>(twin.up, twin.left, twin.up + twin.left, twin.right, twin.up + twin.right); //} var dirs = new ChoiceStack <twin>(hexCompass); //if (this.my_cell_pos.y > 0) //{ // dirs.RemoveAll(twin.down); // dirs.RemoveAll(twin.down + twin.left); // dirs.RemoveAll(twin.down + twin.right); //} //if (parent != null && parent.my_cell_pos.y == this.my_cell_pos.y && my_cell_pos.y > 0) //{ // dirs.RemoveAll(twin.left); // dirs.RemoveAll(twin.right); //} if (dirs.GetFirstTrue(SpawnNewNode) == twin.zero) { // failed? i'm no longer an active branch. bye! retired = true; xxi.RetireNode(this); } else { this.branchDelay = Random.Range(minBranchDelay, maxBranchDelay); } } }
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() { switch (agentType) { case AgentType.Player: twin pin = new twin(Util.sign(Input.GetAxisRaw("Horizontal")), Util.sign(Input.GetAxisRaw("Vertical"))); body.velocity = body.velocity * 0.9f + 0.1f * (Vector2)pin * 50; if (pin.taxicabLength == 1) { GetComponent <FacingData>().facing = pin; } transform.rotation = Quaternion.Euler(0, 0, Vector2.SignedAngle(twin.up, GetComponent <FacingData>().facing)); break; case AgentType.Pill: AgentScripts.PillFixedUpdate(this); break; case AgentType.RedBlood: case AgentType.WhiteBlood: case AgentType.Virus: case AgentType.Oxygen: if (IsWithinDistOfCentered(2f)) { // great, keep going var dirs = new ChoiceStack <twin>(twin.compass); dirs.RemoveAll(-lastMove); lastMove = dirs.GetFirstTrue(TryMove); } else { body.velocity = ToCentered().normalized * 20; } break; case AgentType.BodyHeart: body.velocity = ToCentered().normalized * 20; break; } }
public static void PillFixedUpdate(InsiderBasicAgent agent) { var pd = agent.GetComponent <PillData>(); if (pd.mode_bullet) { agent.GetComponent <BitsyAni>().speed = .4f; agent.SnapMyCellPos(); if ((agent.body.velocity - pd.bullet_velocity).SqrMagnitude() > 10) { pd.mode_medicine = true; pd.mode_bullet = false; var random_dirs = new ChoiceStack <twin>(twin.compass); random_dirs.GetFirstTrue(agent.TryMove); agent.body.velocity = Quaternion.Euler(0, 0, Random.Range(90, 270)) * pd.bullet_velocity; // bounce backwards agent.GetComponent <SpriteRenderer>().flipX = !agent.GetComponent <SpriteRenderer>().flipX; // flip sprite } } else if (pd.mode_medicine) { if (pd.medicine_energize < 1) { pd.medicine_energize = Mathf.Clamp01(pd.medicine_energize + .02f); agent.GetComponent <BitsyAni>().speed = Mathf.Lerp(.6f, 0, pd.medicine_energize); agent.body.velocity *= 0.99f; } else { agent.GetComponent <BitsyAni>().speed = .12f; if (agent.IsWithinDistOfCentered(2f)) { var dirs = new ChoiceStack <twin>(twin.compass); dirs.RemoveAll(-agent.lastMove); agent.lastMove = dirs.GetFirstTrue(agent.TryMove); } else { agent.body.velocity = agent.body.velocity * .95f + .05f * (Vector2)agent.ToCentered().normalized * 25f; } } } }
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); }
public twin?GetRandomFreeCell(System.Func <twin, bool> cellVerifier) { var freeCells = new ChoiceStack <twin>(); new twinrect(twin.zero, new twin(32, 32) - twin.one).DoEach(cell => { if (!IsSolidTile(cell) && cellVerifier(cell)) { freeCells.Add(cell); } }); freeCells.Lock(shuffle: true); var freeCell = freeCells.GetFirstTrue(cell => { return(true); }, defaultValue: twin.one *-1000); if (freeCell == twin.one * -1000) { return(null); } else { return(freeCell); } }
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); } }
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); }
private void FixedUpdate() { if (this.mature < 1) { if (IsWithinDistOfCentered(2f, offset: subtileOffset)) { this.mature += Random.value * 0.5f; if (this.mature >= 1) { transform.localScale = Vector3.one; } else { var dirs = new ChoiceStack <twin>(twin.compass); dirs.RemoveAll(-lastMove); lastMove = dirs.GetFirstTrue(TryMove); } } else if (bodyStuckFrames > 20) { bodyStuckFrames = 0; SnapMyCellPos(); TryMove(-lastMove); } } SetVelocityApproachTarget(); this.toNextCycle--; if (this.toNextCycle <= 0) { // too many plants in one space will choke + die! float[] nutrients_need = { 0, 0, 0 }; HashSet <twin> myCellAndOrthos = new HashSet <twin>(); myCellAndOrthos.Add(my_cell_pos); foreach (var dir in twin.compass) { myCellAndOrthos.Add(my_cell_pos + dir); } foreach (var agent in board.GetAgentsAt(myCellAndOrthos)) { if (agent is BaPlant) { var plant = (BaPlant)agent; if (plant.mature >= 1) { nutrients_need[0] += plant.nutrientColour.r; nutrients_need[1] += plant.nutrientColour.g; nutrients_need[2] += plant.nutrientColour.b; } } if (agent is BaFoodSource) { this.thriving += 0.25f; // food sources are great for thriving plants :) } } // if there's a need of 10, and 5 are available, my nutrient share will be 50%. that's simple. // if there's a need of 15, and 5 are avail, i'll get 33%. get the reciprocal, simple. // then i need to take all those according to how much i need to see how many nutrients i'll have altogether, right? // so if i need .75 green and my neighbour does too... we have a combined need of 1.5 // my thriving will go down by (.75)*(random)*(1-(66%)) // <-- we'll each be getting 66% // therefore the 'shortage' is 1-33% // my thriving will go down by (.75)*(random*(.33)), which means i'll be losing thriving: // i'll lose 0.12375 on average each frame, too much for my 0.1 to offset. if (nutrients_need[0] > 1) { this.thriving -= this.nutrientColour.r * Random.value * (1 - (1 / nutrients_need[0])); } if (nutrients_need[1] > 1) { this.thriving -= this.nutrientColour.g * Random.value * (1 - (1 / nutrients_need[1])); } if (nutrients_need[2] > 1) { this.thriving -= this.nutrientColour.b * Random.value * (1 - (1 / nutrients_need[2])); } this.thriving = Mathf.Clamp01(this.thriving + (mature < 1?0:0.1f)); this.heart = Mathf.Clamp01(this.heart + 0.2f * (this.thriving - 0.5f)); if (this.heart >= 1 && this.mature >= 1) { Spread(); } transform.localScale = Vector3.one; GetComponent <SpriteRenderer>().color = Color.white; if (mature < 1) { transform.localScale *= 0.5f; } if (this.heart < .4f) { GetComponent <SpriteRenderer>().color = Color.grey; } if (this.heart < .2f) { transform.localScale *= 0.5f; } if (this.heart <= 0) { Object.Destroy(this.gameObject); } else { this.toNextCycle = this.cycleLength; } } }