1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using System.Text; |
---|
5 | using CastleMaster.Players; |
---|
6 | using CastleMaster.Units.Mobs; |
---|
7 | using CastleMaster.Units; |
---|
8 | using CastleMaster.Graphics; |
---|
9 | using CastleMaster.Entities.TileEntities; |
---|
10 | using Microsoft.Xna.Framework; |
---|
11 | |
---|
12 | namespace CastleMaster.Ai.AIStates |
---|
13 | { |
---|
14 | public class CollectiveState : AIState |
---|
15 | { |
---|
16 | private List<MobWoodcutter> woodCutters; |
---|
17 | private List<MobWarrior> warriors; |
---|
18 | private List<MobRanger> rangers; |
---|
19 | private bool walkingToWood = false, gettingWood = false; |
---|
20 | |
---|
21 | public CollectiveState(PlayerAI player) |
---|
22 | : base(player) |
---|
23 | { |
---|
24 | } |
---|
25 | |
---|
26 | public override int Importance |
---|
27 | { |
---|
28 | get |
---|
29 | { |
---|
30 | if (AI.Store.IsDestroyed) return IMPORTANCE_LOW; |
---|
31 | |
---|
32 | if (AI.CoinsAmount <= Player.START_MONEY || AI.LumberAmount <= Player.START_LUMBER) |
---|
33 | return IMPORTANCE_HIGH; |
---|
34 | if (AI.CoinsAmount <= Player.START_MONEY * 3) |
---|
35 | return IMPORTANCE_MEDIUM; |
---|
36 | |
---|
37 | return IMPORTANCE_LOW; |
---|
38 | } |
---|
39 | } |
---|
40 | |
---|
41 | public override void OnStateChosen() |
---|
42 | { |
---|
43 | woodCutters = AI.GetUnitsByType<MobWoodcutter>(); |
---|
44 | warriors = AI.GetUnitsByType<MobWarrior>(); |
---|
45 | rangers = AI.GetUnitsByType<MobRanger>(); |
---|
46 | } |
---|
47 | |
---|
48 | public override void OnStateChange() |
---|
49 | { |
---|
50 | walkingToWood = false; |
---|
51 | gettingWood = false; |
---|
52 | if (woodCutters.Count > 0) |
---|
53 | { |
---|
54 | MobWoodcutter mw = woodCutters[0]; |
---|
55 | AI.ForestPoint = new Point((int)(mw.X / Viewport.TILESIZE), (int)(mw.Z / Viewport.TILESIZE)); |
---|
56 | woodCutters[0].OnGroupOrder<MobWoodcutter>(woodCutters, AI.SpawnPoints[Player.SPAWN_WOODCUTTER].X * Viewport.TILESIZE, AI.SpawnPoints[Player.SPAWN_WOODCUTTER].Y * Viewport.TILESIZE); |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | public override void ApplyState() |
---|
61 | { |
---|
62 | if (!AI.Armory.IsDestroyed && AI.CoinsAmount >= UnitArmory.PRICE_WOODCUTTER) |
---|
63 | { |
---|
64 | if (woodCutters.Count <= 5) |
---|
65 | { |
---|
66 | MobWoodcutter m = AI.Armory.BuyUnit<MobWoodcutter>(); |
---|
67 | bool solid = true; |
---|
68 | int xTile = 0, zTile = 0; |
---|
69 | while (solid) |
---|
70 | { |
---|
71 | xTile = AI.SpawnPoints[Player.SPAWN_WOODCUTTER].X + (int)(Math.Sin(Game.Random.NextDouble() * MathHelper.TwoPi) * Game.Random.NextDouble() * 4.0F); |
---|
72 | zTile = AI.SpawnPoints[Player.SPAWN_WOODCUTTER].Y + (int)(Math.Cos(Game.Random.NextDouble() * MathHelper.TwoPi) * Game.Random.NextDouble() * 4.0F); |
---|
73 | if (xTile < 0 || xTile >= AI.Level.Width || zTile < 0 || zTile >= AI.Level.Height) continue; |
---|
74 | if (!AI.Level.SolidTo(m, xTile, zTile)) solid = false; |
---|
75 | } |
---|
76 | |
---|
77 | AI.Level.AddEntity(m, xTile * Viewport.TILESIZE + 8.0F, zTile * Viewport.TILESIZE + 8.0F); |
---|
78 | woodCutters.Add(m); |
---|
79 | m.OnFunctionClick(AI.ForestPoint.X * Viewport.TILESIZE, AI.ForestPoint.Y * Viewport.TILESIZE, 1, false); |
---|
80 | return; |
---|
81 | } |
---|
82 | else if (warriors.Count + rangers.Count <= 5 && AI.CoinsAmount >= UnitArmory.PRICE_WARRIOR) |
---|
83 | { |
---|
84 | Type typeToBuy = null; |
---|
85 | int buyID = Game.Random.Next(AI.AttackerTypes.Count); |
---|
86 | typeToBuy = AI.AttackerTypes[buyID]; |
---|
87 | |
---|
88 | Mob m = AI.Armory.BuyUnit<Mob>(typeToBuy); |
---|
89 | Point spawnPoint = AI.SpawnPoints[buyID + 1]; |
---|
90 | |
---|
91 | bool solid = true; |
---|
92 | int xTile = 0, zTile = 0; |
---|
93 | while (solid) |
---|
94 | { |
---|
95 | xTile = spawnPoint.X + (int)(Math.Sin(Game.Random.NextDouble() * MathHelper.TwoPi) * Game.Random.NextDouble() * 4.0F); |
---|
96 | zTile = spawnPoint.Y + (int)(Math.Cos(Game.Random.NextDouble() * MathHelper.TwoPi) * Game.Random.NextDouble() * 4.0F); |
---|
97 | if (xTile < 0 || xTile >= AI.Level.Width || zTile < 0 || zTile >= AI.Level.Height) continue; |
---|
98 | if (!AI.Level.SolidTo(m, xTile, zTile)) solid = false; |
---|
99 | } |
---|
100 | |
---|
101 | AI.Level.AddEntity(m, xTile * Viewport.TILESIZE + 8.0F, zTile * Viewport.TILESIZE + 8.0F); |
---|
102 | if (typeToBuy == typeof(MobWarrior)) |
---|
103 | warriors.Add((MobWarrior)m); |
---|
104 | else |
---|
105 | rangers.Add((MobRanger)m); |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | if (!walkingToWood) |
---|
110 | { |
---|
111 | woodCutters[0].OnGroupOrder(woodCutters, AI.ForestPoint.X * Viewport.TILESIZE, AI.ForestPoint.Y * Viewport.TILESIZE); |
---|
112 | walkingToWood = true; |
---|
113 | } |
---|
114 | else |
---|
115 | { |
---|
116 | foreach (MobWoodcutter woodCutter in woodCutters) |
---|
117 | { |
---|
118 | if (!woodCutter.HasActiveOrders) |
---|
119 | { |
---|
120 | woodCutter.OrderChop(AI.Level.GetNearestEntity<TileEntityTree>(woodCutter, 5), 1, false); |
---|
121 | if (woodCutter.Removed) woodCutters.Remove(woodCutter); |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | if (!AI.Store.IsDestroyed) |
---|
126 | { |
---|
127 | while (AI.LumberAmount > Player.START_LUMBER + 1) |
---|
128 | { |
---|
129 | AI.LumberAmount--; |
---|
130 | AI.CoinsAmount += UnitStore.COINS_FOR_LUMBER; |
---|
131 | } |
---|
132 | } |
---|
133 | } |
---|
134 | } |
---|
135 | } |
---|
136 | } |
---|