public CreatureAI(Creature creature, string name, EnemySensor sensor, PlanService planService) : base(name, creature.Physics) { Movement = new CreatureMovement(); GatherManager = new GatherManager(this); Blackboard = new Blackboard(); Creature = creature; CurrentPath = null; DrawPath = false; PlannerTimer = new Timer(0.1f, false); LocalControlTimeout = new Timer(5, false); WanderTimer = new Timer(1, false); Creature.Faction.Minions.Add(this); DrawAIPlan = false; WaitingOnResponse = false; PlanSubscriber = new PlanSubscriber(planService); ServiceTimeout = new Timer(2, false); Sensor = sensor; Sensor.OnEnemySensed += Sensor_OnEnemySensed; Sensor.Creature = this; CurrentTask = null; Tasks = new List <Task>(); Thoughts = new List <Thought>(); IdleTimer = new Timer(15.0f, true); SpeakTimer = new Timer(5.0f, true); XPEvents = new List <int>(); }
public void OnDeserialize(StreamingContext ctx) { if (Sensor == null) { Sensor = GetRoot().GetComponent <EnemySensor>(); } Sensor.OnEnemySensed += Sensor_OnEnemySensed; PlanSubscriber = new PlanSubscriber((ctx.Context as WorldManager).PlanService); }
public CreatureAI( ComponentManager Manager, string name, EnemySensor sensor) : base(name, Manager) { Movement = new CreatureMovement(this); PlanSubscriber = new PlanSubscriber(Manager.World.PlanService); Sensor = sensor; Sensor.OnEnemySensed += Sensor_OnEnemySensed; Sensor.Creature = this; }
public PlanAct(CreatureAI agent, string pathOut, string target, PlanType planType) : base(agent) { Type = planType; Name = "Plan to " + target; PlannerTimer = new Timer(1.0f, false); MaxExpansions = 1000; PathOut = pathOut; TargetName = target; PlanSubscriber = new PlanSubscriber(PlayState.PlanService); WaitingOnResponse = false; MaxTimeouts = 4; Timeouts = 0; Radius = 0; }
public PlanAct(CreatureAI agent, string pathOut, string target, PlanType planType) : base(agent) { Type = planType; Name = "Plan to " + target; PlannerTimer = new Timer(60.0f, false); MaxExpansions = 10000; PathOut = pathOut; TargetName = target; PlanSubscriber = new PlanSubscriber(agent.Manager.World.PlanService); WaitingOnResponse = false; MaxTimeouts = 4; Timeouts = 0; Radius = 0; Weights = new List <float> { 5.0f, 10.0f, 20.0f, 30.0f, 40.0f }; }
public override IEnumerable <Status> Run() { Path = null; Timeouts = 0; PlannerTimer.Reset(PlannerTimer.TargetTimeSeconds); Voxel voxUnder = new Voxel(); while (true) { if (Path != null) { yield return(Status.Success); break; } if (Timeouts > MaxTimeouts) { yield return(Status.Fail); break; } PlannerTimer.Update(DwarfTime.LastTime); ChunkManager chunks = PlayState.ChunkManager; if (PlannerTimer.HasTriggered || Timeouts == 0) { if (!chunks.ChunkData.GetVoxel(Agent.Position, ref voxUnder)) { Creature.DrawIndicator(IndicatorManager.StandardIndicators.Question); yield return(Status.Fail); break; } if (Target == null && Type != PlanType.Edge) { Creature.DrawIndicator(IndicatorManager.StandardIndicators.Question); yield return(Status.Fail); break; } if (voxUnder != null) { Path = null; AstarPlanRequest aspr = new AstarPlanRequest { Subscriber = PlanSubscriber, Start = voxUnder, MaxExpansions = MaxExpansions, Sender = Agent, HeuristicWeight = Weights[Timeouts] }; switch (Type) { case PlanType.Radius: aspr.GoalRegion = new SphereGoalRegion(Target, Radius); break; case PlanType.Into: aspr.GoalRegion = new VoxelGoalRegion(Target); break; case PlanType.Adjacent: aspr.GoalRegion = new AdjacentVoxelGoalRegion2D(Target); break; case PlanType.Edge: aspr.GoalRegion = new EdgeGoalRegion(); break; } PlanSubscriber.SendRequest(aspr); PlannerTimer.Reset(PlannerTimer.TargetTimeSeconds); WaitingOnResponse = true; yield return(Status.Running); } else { Path = null; Creature.DrawIndicator(IndicatorManager.StandardIndicators.Question); yield return(Status.Fail); break; } Timeouts++; } else { if (Target != null && Creature.AI.DrawAIPlan) { Drawer3D.DrawLine(Creature.AI.Position, Target.Position, Color.Blue, 0.25f); } Status statusResult = Status.Running; while (PlanSubscriber.Responses.Count > 0) { AStarPlanResponse response; PlanSubscriber.Responses.TryDequeue(out response); if (response.Success) { Path = response.Path; if (Type == PlanType.Adjacent && Path.Count > 0) { Path.RemoveAt(Path.Count - 1); } WaitingOnResponse = false; statusResult = Status.Success; } else { Creature.DrawIndicator(IndicatorManager.StandardIndicators.Question); statusResult = Status.Fail; } } yield return(statusResult); } } }
public void OnDeserialize(StreamingContext ctx) { PlanSubscriber = new PlanSubscriber((ctx.Context as WorldManager).PlanService); }
public override IEnumerable <Status> Run() { Path = null; Timeouts = 0; PlannerTimer.Reset(PlannerTimer.TargetTimeSeconds); while (true) { if (Path != null) { yield return(Status.Success); break; } if (Timeouts > MaxTimeouts) { yield return(Status.Fail); break; } PlannerTimer.Update(DwarfTime.LastTime); ChunkManager chunks = Creature.Manager.World.ChunkManager; if (PlannerTimer.HasTriggered || Timeouts == 0) { if (!Target.IsValid && Type != PlanType.Edge) { Creature.DrawIndicator(IndicatorManager.StandardIndicators.Question); yield return(Status.Fail); break; } var voxUnder = new VoxelHandle(chunks.ChunkData, GlobalVoxelCoordinate.FromVector3(Agent.Position)); if (!voxUnder.IsValid) { Creature.DrawIndicator(IndicatorManager.StandardIndicators.Question); yield return(Status.Fail); break; } Path = null; AstarPlanRequest aspr = new AstarPlanRequest { Subscriber = PlanSubscriber, Start = voxUnder, MaxExpansions = MaxExpansions, Sender = Agent, HeuristicWeight = Weights[Timeouts] }; aspr.GoalRegion = GetGoal(); if (!PlanSubscriber.SendRequest(aspr)) { yield return(Status.Fail); yield break; } PlannerTimer.Reset(PlannerTimer.TargetTimeSeconds); WaitingOnResponse = true; yield return(Status.Running); Timeouts++; } else { //if (Target.IsValid && Creature.AI.DrawAIPlan) // Drawer3D.DrawLine(Creature.AI.Position, Target.WorldPosition, Color.Blue, 0.25f); Status statusResult = Status.Running; while (PlanSubscriber.Responses.Count > 0) { AStarPlanResponse response; if (!PlanSubscriber.Responses.TryDequeue(out response)) { yield return(Status.Running); continue; } if (response.Success) { Path = response.Path; WaitingOnResponse = false; statusResult = Status.Success; } else if (Timeouts < MaxTimeouts) { yield return(Status.Running); } else { Creature.DrawIndicator(IndicatorManager.StandardIndicators.Question); statusResult = Status.Fail; PlanSubscriber.Service.RemoveSubscriber(PlanSubscriber); } } yield return(statusResult); } } }
public WaitForPlanHelper(float timeout, PlanService planService) { Subscriber = new PlanSubscriber(planService); Timeout = new Timer(timeout, true, Timer.TimerMode.Real); }
public override IEnumerable <Status> Run() { Path = null; Timeouts = 0; PlannerTimer.Reset(PlannerTimer.TargetTimeSeconds); Voxel voxUnder = new Voxel(); while (true) { if (Path != null) { yield return(Status.Success); break; } if (Timeouts > MaxTimeouts) { yield return(Status.Fail); break; } PlannerTimer.Update(DwarfTime.LastTime); ChunkManager chunks = PlayState.ChunkManager; if (PlannerTimer.HasTriggered || Timeouts == 0) { if (!chunks.ChunkData.GetVoxel(Agent.Position, ref voxUnder)) { Creature.DrawIndicator(IndicatorManager.StandardIndicators.Question); yield return(Status.Fail); break; } if (Target == null) { if (Creature.Faction == PlayState.Master.Faction) { PlayState.AnnouncementManager.Announce(Creature.Stats.FullName + " (" + Creature.Stats.CurrentLevel.Name + ")" + " got lost.", Creature.Stats.FullName + "'s target was lost.", () => Agent.ZoomToMe()); } Creature.DrawIndicator(IndicatorManager.StandardIndicators.Question); yield return(Status.Fail); break; } if (voxUnder != null) { Path = null; AstarPlanRequest aspr = new AstarPlanRequest { Subscriber = PlanSubscriber, Start = voxUnder, MaxExpansions = MaxExpansions, Sender = Agent }; if (Type == PlanType.Radius) { aspr.GoalRegion = new SphereGoalRegion(Target, Radius); } else if (Type == PlanType.Into) { aspr.GoalRegion = new VoxelGoalRegion(Target); } else if (Type == PlanType.Adjacent) { aspr.GoalRegion = new AdjacentVoxelGoalRegion2D(Target); } PlanSubscriber.SendRequest(aspr); PlannerTimer.Reset(PlannerTimer.TargetTimeSeconds); WaitingOnResponse = true; yield return(Status.Running); } else { Path = null; if (Creature.Faction == PlayState.Master.Faction) { PlayState.AnnouncementManager.Announce(Creature.Stats.FullName + " got lost.", Creature.Stats.FullName + " couldn't find a path. The target was invalid.", () => Agent.ZoomToMe()); } Creature.DrawIndicator(IndicatorManager.StandardIndicators.Question); yield return(Status.Fail); break; } Timeouts++; } else { Status statusResult = Status.Running; while (PlanSubscriber.Responses.Count > 0) { AStarPlanResponse response; PlanSubscriber.Responses.TryDequeue(out response); if (response.Success) { Path = response.Path; if (Type == PlanType.Adjacent && Path.Count > 0) { Path.RemoveAt(Path.Count - 1); } WaitingOnResponse = false; statusResult = Status.Success; } else { if (Creature.Faction == PlayState.Master.Faction) { PlayState.AnnouncementManager.Announce(Creature.Stats.FullName + " got lost.", Creature.Stats.FullName + " couldn't find a path in time.", Agent.ZoomToMe); } Creature.DrawIndicator(IndicatorManager.StandardIndicators.Question); statusResult = Status.Fail; } } yield return(statusResult); } } }