override public void Update(DwarfTime gameTime, ChunkManager chunks, Camera camera) { if (!Active) { return; } Creature.NoiseMaker.BasePitch = Stats.VoicePitch; AutoGatherTimer.Update(gameTime); if (AutoGatherTimer.HasTriggered) { foreach (var body in World.EnumerateIntersectingObjects(Physics.BoundingBox.Expand(3.0f)).OfType <ResourceEntity>().Where(r => r.Active && r.AnimationQueue.Count == 0)) { Creature.GatherImmediately(body, Inventory.RestockType.RestockResource); } OrderEnemyAttack(); } DeleteBadTasks(); PreEmptTasks(); if (CurrentTask.HasValue(out var currentTask)) { bool processAct = true; if (!CurrentAct.HasValue()) // Should be impossible to have a current task and no current act. { // Try and recover the correct act. // <blecki> I always run with a breakpoint set here... just in case. ChangeAct(currentTask.CreateScript(Creature)); // This is a bad situation! if (!CurrentAct.HasValue()) { ChangeTask(null); processAct = false; } } if (processAct && CurrentAct.HasValue(out Act currentAct)) { var status = currentAct.Tick(); var retried = false; if (CurrentAct.HasValue(out Act newCurrentAct)) { if (status == Act.Status.Fail) { LastFailedAct = newCurrentAct.Name; if (!FailedTasks.Any(task => task.TaskFailure.Equals(currentTask))) { FailedTasks.Add(new FailedTask() { TaskFailure = currentTask, FailedTime = World.Time.CurrentDate }); } if (currentTask.ShouldRetry(Creature)) { if (!Tasks.Contains(currentTask)) { ReassignCurrentTask(); retried = true; } } } } if (currentTask.IsComplete(World)) { ChangeTask(null); } else if (status != Act.Status.Running && !retried) { ChangeTask(null); } } } else { var goal = GetEasiestTask(Tasks); if (goal == null) { goal = World.TaskManager.GetBestTask(this); } if (goal != null) { ChangeTask(goal); } else { ChangeTask(ActOnIdle()); } } if (PositionConstraint.Contains(Physics.LocalPosition) == ContainmentType.Disjoint) { Physics.LocalPosition = MathFunctions.Clamp(Physics.Position, PositionConstraint); Physics.PropogateTransforms(); } }
override public void Update(DwarfTime gameTime, ChunkManager chunks, Camera camera) { base.Update(gameTime, chunks, camera); if (!Active) { return; } Creature.NoiseMaker.BasePitch = Stats.VoicePitch; // Non-dwarves are always at full energy. Stats.Energy.CurrentValue = 100.0f; BehaviorTimer.Update(gameTime); if (BehaviorTimer.HasTriggered) { if (Faction.Race.HasValue(out var race)) { if (!String.IsNullOrEmpty(race.BecomeWhenEvil) && MathFunctions.RandEvent(0.01f)) { Faction.Minions.Remove(this); Faction = World.Factions.Factions[race.BecomeWhenEvil]; Faction.AddMinion(this); } else if (!String.IsNullOrEmpty(race.BecomeWhenNotEvil) && MathFunctions.RandEvent(0.01f)) { Faction.Minions.Remove(this); Faction = World.Factions.Factions[race.BecomeWhenNotEvil]; Faction.AddMinion(this); } foreach (var body in World.EnumerateIntersectingObjects(Physics.BoundingBox.Expand(3.0f)).OfType <ResourceEntity>().Where(r => r.Active && r.AnimationQueue.Count == 0)) { if (Library.GetResourceType(body.Resource.TypeName).HasValue(out var resource) && resource.Tags.Contains("Edible")) { if ((race.EatsMeat && resource.Tags.Contains("AnimalProduct")) || (race.EatsPlants && !resource.Tags.Contains("AnimalProduct"))) { Creature.GatherImmediately(body); AssignTask(new ActWrapperTask(new EatFoodAct(this, false))); } } } } OrderEnemyAttack(); } DeleteBadTasks(); PreEmptTasks(); HandleReproduction(); // Try to find food if we are hungry. Wait - doesn't this rob the player? if (Stats.Hunger.IsDissatisfied() && World.CountResourcesWithTag("Edible") > 0) { var eatTask = new SatisfyHungerTask(); if (Stats.Hunger.IsCritical()) { eatTask.Priority = TaskPriority.Urgent; } if (!Tasks.Contains(eatTask) && CurrentTask.HasValue(out var task) && task != eatTask) // Really should just leave the current task in the task list. { AssignTask(eatTask); } } if (CurrentTask.HasValue(out var currentTask)) { if (!CurrentAct.HasValue()) // Should be impossible to have a current task and no current act. { // Try and recover the correct act. // <blecki> I always run with a breakpoint set here... just in case. ChangeAct(currentTask.CreateScript(Creature)); // This is a bad situation! if (!CurrentAct.HasValue()) { ChangeTask(null); } } if (CurrentAct.HasValue(out Act currentAct)) { var status = currentAct.Tick(); bool retried = false; if (CurrentAct.HasValue(out Act newCurrentAct) && currentTask != null) { if (status == Act.Status.Fail) { LastFailedAct = newCurrentAct.Name; if (!FailedTasks.Any(task => task.TaskFailure.Equals(currentTask))) { FailedTasks.Add(new FailedTask() { TaskFailure = currentTask, FailedTime = World.Time.CurrentDate }); } if (currentTask.ShouldRetry(Creature)) { if (!Tasks.Contains(currentTask)) { ReassignCurrentTask(); retried = true; } } } } if (currentTask != null && currentTask.IsComplete(World)) { ChangeTask(null); } else if (status != Act.Status.Running && !retried) { ChangeTask(null); } } } else { var goal = GetEasiestTask(Tasks); if (goal != null) { ChangeTask(goal); } else { var newTask = ActOnIdle(); if (newTask != null) { ChangeTask(newTask); } } } // With a small probability, the creature will drown if its under water. if (MathFunctions.RandEvent(GameSettings.Current.DrownChance)) { var above = VoxelHelpers.GetVoxelAbove(Physics.CurrentVoxel); var below = VoxelHelpers.GetVoxelBelow(Physics.CurrentVoxel); bool shouldDrown = (above.IsValid && (!above.IsEmpty || above.LiquidLevel > 0)); if ((Physics.IsInLiquid || (!Movement.CanSwim && (below.IsValid && (below.LiquidLevel > 5)))) && (!Movement.CanSwim || shouldDrown)) { Creature.Damage(Movement.CanSwim ? 1.0f : 30.0f, Health.DamageType.Normal); } } if (PositionConstraint.Contains(Physics.LocalPosition) == ContainmentType.Disjoint) { Physics.LocalPosition = MathFunctions.Clamp(Physics.Position, PositionConstraint); Physics.PropogateTransforms(); } }
override public void Update(DwarfTime gameTime, ChunkManager chunks, Camera camera) { //base.Update(gameTime, chunks, camera); if (!Active) { return; } Creature.NoiseMaker.BasePitch = Stats.VoicePitch; AutoGatherTimer.Update(gameTime); IdleTimer.Update(gameTime); SpeakTimer.Update(gameTime); if (AutoGatherTimer.HasTriggered) { foreach (var body in World.EnumerateIntersectingObjects(Physics.BoundingBox.Expand(3.0f)).OfType <ResourceEntity>().Where(r => r.Active && r.AnimationQueue.Count == 0)) { Creature.GatherImmediately(body, Inventory.RestockType.RestockResource); } OrderEnemyAttack(); } DeleteBadTasks(); PreEmptTasks(); if (CurrentTask != null) { Stats.Boredom.CurrentValue -= (float)(CurrentTask.BoredomIncrease * gameTime.ElapsedGameTime.TotalSeconds); if (Stats.Boredom.IsCritical()) { Creature.AddThought("I have been overworked recently.", new TimeSpan(0, 4, 0, 0), -2.0f); } Stats.Energy.CurrentValue += (float)(CurrentTask.EnergyDecrease * gameTime.ElapsedGameTime.TotalSeconds); } // Heal thyself if (Stats.Health.IsDissatisfied()) { Task toReturn = new GetHealedTask(); if (!Tasks.Contains(toReturn) && CurrentTask != toReturn) { AssignTask(toReturn); } } // Try to go to sleep if we are low on energy and it is night time. if (Stats.Energy.IsCritical()) { Task toReturn = new SatisfyTirednessTask(); if (!Tasks.Contains(toReturn) && CurrentTask != toReturn) { AssignTask(toReturn); } } // Try to find food if we are hungry. if (Stats.Hunger.IsDissatisfied() && World.CountResourcesWithTag(Resource.ResourceTags.Edible) > 0) { Task toReturn = new SatisfyHungerTask() { MustPay = true }; if (Stats.Hunger.IsCritical()) { toReturn.Priority = TaskPriority.Urgent; } if (!Tasks.Contains(toReturn) && CurrentTask != toReturn) { AssignTask(toReturn); } } if (Stats.Boredom.IsDissatisfied()) { if (!Tasks.Any(task => task.BoredomIncrease < 0)) { Task toReturn = SatisfyBoredom(); if (toReturn != null && !Tasks.Contains(toReturn) && CurrentTask != toReturn) { AssignTask(toReturn); } } } restockTimer.Update(DwarfTime.LastTime); if (restockTimer.HasTriggered && Creature.Inventory.Resources.Count > 10) { Creature.RestockAllImmediately(); } if (CurrentTask == null) // We need something to do. { if (Stats.Happiness.IsSatisfied()) // We're happy, so make sure we aren't on strike. { Stats.IsOnStrike = false; UnhappinessTime = 0.0f; } if (Stats.IsOnStrike) // We're on strike, so track how long this job has sucked. { UnhappinessTime += gameTime.ElapsedGameTime.TotalMinutes; if (UnhappinessTime > GameSettings.Default.HoursUnhappyBeforeQuitting) // If we've been unhappy long enough, quit. { if (GetRoot().GetComponent <DwarfThoughts>().HasValue(out var thoughts)) { Manager.World.MakeAnnouncement( // Can't use a popup because the dwarf will soon not exist. Also - this is a serious event! Message: String.Format("{0} has quit! The last straw: {1}", Stats.FullName, thoughts.Thoughts.Last(t => t.HappinessModifier < 0.0f).Description), ClickAction: null, logEvent: true, eventDetails: String.Join("\n", thoughts.Thoughts.Where(t => t.HappinessModifier < 0.0f).Select(t => t.Description))); thoughts.Thoughts.Clear(); } else { Manager.World.MakeAnnouncement( // Can't use a popup because the dwarf will soon not exist. Also - this is a serious event! Message: String.Format("{0} has quit!", Stats.FullName), ClickAction: null, logEvent: true, eventDetails: "So sick of this place!"); } LeaveWorld(); if (GetRoot().GetComponent <Inventory>().HasValue(out var inv)) { inv.Die(); } if (GetRoot().GetComponent <SelectionCircle>().HasValue(out var sel)) { sel.Die(); } Faction.Minions.Remove(this); World.PersistentData.SelectedMinions.Remove(this); return; } } else if (Stats.Happiness.IsDissatisfied()) // We aren't on strike, but we hate this place. { if (MathFunctions.Rand(0, 1) < 0.25f) // We hate it so much that we might just go on strike! This can probably be tweaked. As it stands, // dorfs go on strike almost immediately every time. { Manager.World.UserInterface.MakeWorldPopup(String.Format("{0} ({1}) refuses to work!", Stats.FullName, Stats.CurrentClass.Name), Creature.Physics, -10, 10); Manager.World.Tutorial("happiness"); SoundManager.PlaySound(ContentPaths.Audio.Oscar.sfx_gui_negative_generic, 0.25f); Stats.IsOnStrike = true; } } if (!Stats.IsOnStrike) // We aren't on strike, so find a new task. { var goal = GetEasiestTask(Tasks); if (goal == null) { goal = World.TaskManager.GetBestTask(this); } if (goal != null) { IdleTimer.Reset(IdleTimer.TargetTimeSeconds); ChangeTask(goal); } else { var newTask = ActOnIdle(); if (newTask != null) { ChangeTask(newTask); } } } else { ChangeTask(ActOnIdle()); } } else { if (!CurrentAct.HasValue()) // Should be impossible to have a current task and no current act. { // Try and recover the correct act. // <blecki> I always run with a breakpoint set here... just in case. ChangeAct(CurrentTask.CreateScript(Creature)); // This is a bad situation! if (!CurrentAct.HasValue()) { ChangeTask(null); } } if (CurrentAct.HasValue(out Act currentAct)) { var status = currentAct.Tick(); bool retried = false; if (CurrentAct.HasValue(out Act newCurrentAct) && CurrentTask != null) { if (status == Act.Status.Fail) { LastFailedAct = newCurrentAct.Name; if (!FailedTasks.Any(task => task.TaskFailure.Equals(CurrentTask))) { FailedTasks.Add(new FailedTask() { TaskFailure = CurrentTask, FailedTime = World.Time.CurrentDate }); } if (CurrentTask.ShouldRetry(Creature)) { if (!Tasks.Contains(CurrentTask)) { ReassignCurrentTask(); retried = true; } } } } if (CurrentTask != null && CurrentTask.IsComplete(World)) { ChangeTask(null); } else if (status != Act.Status.Running && !retried) { ChangeTask(null); } } } // With a small probability, the creature will drown if its under water. if (MathFunctions.RandEvent(0.01f)) { var above = VoxelHelpers.GetVoxelAbove(Physics.CurrentVoxel); var below = VoxelHelpers.GetVoxelBelow(Physics.CurrentVoxel); bool shouldDrown = (above.IsValid && (!above.IsEmpty || above.LiquidLevel > 0)); if ((Physics.IsInLiquid || (!Movement.CanSwim && (below.IsValid && (below.LiquidLevel > 5)))) && (!Movement.CanSwim || shouldDrown)) { Creature.Damage(Movement.CanSwim ? 1.0f : 30.0f, Health.DamageType.Normal); } } if (PositionConstraint.Contains(Physics.LocalPosition) == ContainmentType.Disjoint) { Physics.LocalPosition = MathFunctions.Clamp(Physics.Position, PositionConstraint); Physics.PropogateTransforms(); } }