-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNicoCarrierAI.cs
263 lines (220 loc) · 8.76 KB
/
NicoCarrierAI.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Linq;
using NoxCore.Controllers;
using NoxCore.Fittings.Devices;
using NoxCore.Fittings.Weapons;
using NoxCore.Helm;
using NoxCore.Managers;
using NoxCore.Placeables;
using NoxCore.Placeables.Ships;
using NoxCore.Utilities;
using NoxCore.Rules;
namespace Formaggio.Controllers
{
[RequireComponent(typeof(BasicThreatEvaluator))]
public class NicoCarrierAI : AIStateController
{
public List<Vector2> waypoints = new List<Vector2>();
public int currentWaypoint = 0;
public bool forceWaypointNavigation;
protected SeekBehaviour seekBehaviour;
protected OrbitBehaviour orbitBehaviour;
protected AvoidBehaviour avoidBehaviour;
BasicThreatEvaluator threatSys;
protected List<Structure> squad;
public override void boot(Structure structure, HelmController helm = null)
{
base.boot(structure, helm);
aiActions.Add("SEARCH", searchAction);
aiActions.Add("COMBAT", combatAction);
state = "SEARCH";
// note: leave this as false if you want the ship to orbit its target
forceWaypointNavigation = false;
waypoints.Add(new Vector2(-250, 0));
waypoints.Add(new Vector2(0, 250));
waypoints.Add(new Vector2(250, 0));
waypoints.Add(new Vector2(0, -250));
seekBehaviour = Helm.getBehaviourByName("SEEK") as SeekBehaviour;
orbitBehaviour = Helm.getBehaviourByName("ORBIT") as OrbitBehaviour;
avoidBehaviour = Helm.getBehaviourByName("AVOID") as AvoidBehaviour;
if (orbitBehaviour != null)
{
orbitBehaviour.OrbitRange = structure.scanner.getRadius();
}
if (avoidBehaviour != null)
{
// note: could add additional layers to the following.
// E.G. avoidLayerMask = structure.Gamemode.getCollisionMask("ship").GetValueOrDefault() ^ (1 << LayerMask.NameToLayer("NavPoint"));
LayerMask avoidLayerMask = structure.Gamemode.getCollisionMask("SHIP").GetValueOrDefault();
avoidBehaviour.setCollidables(avoidLayerMask);
}
foreach(FireGroup fireGroup in structure.FireGroupManager.FireGroups)
{
foreach(Weapon weapon in fireGroup.getAllWeapons())
{
ProjectileLauncher launcher = weapon as ProjectileLauncher;
if (launcher != null)
{
launcher.WeaponFired += LauncherFired;
}
}
}
GameEventManager.MatchIsWaitingToStart += AI_MatchIsWaitingToStart;
threatSys = GetComponent<BasicThreatEvaluator>();
booted = true;
}
public void LauncherFired(object sender, WeaponFiredEventArgs args)
{
//Gui.setMessage(args.weaponFired + " has fired!");
}
protected virtual Vector2 setHelmDestination()
{
Vector2 nextPoint = waypoints[currentWaypoint];
currentWaypoint++;
if (currentWaypoint == waypoints.Count)
{
currentWaypoint = 0;
}
return nextPoint;
}
public virtual string searchAction()
{
if (Helm != null)
{
if (structure.scanner.isActiveOn() == true)
{
if (structure.scanner.getEnemiesInRange().Count == 0)
{
#region search pattern
if (seekBehaviour != null && seekBehaviour.Active == false)
{
seekBehaviour.enableExclusively();
currentWaypoint = 0;
}
if (avoidBehaviour != null && avoidBehaviour.Active == false)
{
avoidBehaviour.enable();
}
// run search pattern
if (Helm.destination == null)
{
Helm.destination = setHelmDestination();
}
// don't allow a destination outside of the arena
if (ArenaRules.radius > 0)
{
if (Helm.destination.GetValueOrDefault().magnitude > ArenaRules.radius)
{
Helm.destination = null;
}
}
// draw a line to the destination
if (Helm.destination != null && Cam.followTarget != null && Cam.followTarget.gameObject == Helm.ShipStructure.gameObject)
{
Debug.DrawLine(structure.transform.position, Helm.destination.GetValueOrDefault(), Color.blue, Time.deltaTime, true);
}
return "SEARCH";
#endregion
}
else
{
return "COMBAT";
}
}
else
{
return "SEARCH";
}
}
else
{
return null;
}
}
protected override void newScannerData(Scanner sender)
{
List<GameObject> enemiesInRange = structure.scanner.getEnemiesInRange();
if (state == "COMBAT" && enemiesInRange.Count > 0)
{
if (orbitBehaviour != null)
{
if (orbitBehaviour.Active == false)
{
orbitBehaviour.enableExclusively();
}
}
// get sorted threat ratios for all enemy ships and structures in range
List<Tuple<GameObject, float>> threats = threatSys.calculateThreatRatios(structure, enemiesInRange);
// tell all fire groups to acquire the first target's hull (hence null for 2nd parameter)
foreach (FireGroup fireGroup in structure.FireGroupManager.FireGroups)
{
fireGroup.setTarget(threats[0]._1);
}
// use the first target as the ship/structure to orbit around
orbitBehaviour.OrbitObject = threats[0]._1.transform;
// use the first weapon's maximum range to determie a suitable orbit range
if (structure.weapons.Count > 0)
{
orbitBehaviour.OrbitRange = structure.weapons[0].MaxRange - 1;
}
}
}
public virtual string combatAction()
{
if (Helm != null)
{
if (structure.scanner.isActiveOn() == true)
{
#region target and orbit enemy
List<GameObject> enemiesInRange = structure.scanner.getEnemiesInRange();
if (enemiesInRange.Count > 0)
{
// new scanner data?
return "COMBAT";
}
else
{
foreach (Weapon weap in structure.weapons)
{
TargetableWeapon tWeap = (TargetableWeapon)weap;
if (tWeap != null)
{
tWeap.unacquireTarget();
}
}
return "SEARCH";
}
#endregion
}
else
{
return "SEARCH";
}
}
else
{
return null;
}
}
protected void AI_MatchIsWaitingToStart(object sender)
{
Faction faction = FactionManager.Instance.findFaction(structure.FactionName);
List<Ship> ships = faction.fleetManager.getAllShips();
squad = ships.Cast<Structure>().ToList();
foreach (Ship ship in squad)
{
if (ship.Classification == ShipClassification.FIGHTER || ship.Classification == ShipClassification.BOMBER)
{
ILand landingController = ship.Controller as ILand;
if (landingController != null)
{
landingController.setHangerStructure(structure);
}
}
}
}
}
}