-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMoveToBestPosition.cs
329 lines (275 loc) · 9.52 KB
/
MoveToBestPosition.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveToBestPosition
{
public PositionProximityToSelf positionProximityToSelf;
public ProximityToNearestEnemy proximityToNearestEnemy;
public OverRangeToClosestEnemy overRangeToClosestEnemy;
public OverRangeToAnyEnemy overRangeToAnyEnemy;
public LineOfSightToClosestEnemy lineOfSightToClosestEnemy;
public LineOfSightToAnyEnemy lineOfSightToAnyEnemy;
public ProximityToClosestPickup proximityToClosestPickup;
public ProximityToPlayerSpawner proximityToPlayerSpawner;
public float[] scores;
int greaterScoreIndex;
float greaterScore;
public MoveToBestPosition()
{
positionProximityToSelf = new PositionProximityToSelf();
proximityToNearestEnemy = new ProximityToNearestEnemy();
overRangeToClosestEnemy = new OverRangeToClosestEnemy();
overRangeToAnyEnemy = new OverRangeToAnyEnemy();
lineOfSightToClosestEnemy = new LineOfSightToClosestEnemy();
lineOfSightToAnyEnemy = new LineOfSightToAnyEnemy();
proximityToClosestPickup = new ProximityToClosestPickup();
proximityToPlayerSpawner = new ProximityToPlayerSpawner();
}
public Vector3 GetBest(Context context)
{
List<Vector3> positions = context.sampledPositions;
int positionsCount = positions.Count;
scores = new float[positionsCount];
greaterScoreIndex = -1;
greaterScore = -1f;
for (int i = 0; i < positionsCount; i++)
{
scores[i] += positionProximityToSelf.Score(context, positions[i]);
scores[i] += proximityToNearestEnemy.Score(context, positions[i]);
scores[i] += overRangeToClosestEnemy.Score(context, positions[i]);
scores[i] += overRangeToAnyEnemy.Score(context, positions[i]);
scores[i] += lineOfSightToClosestEnemy.Score(context, positions[i]);
scores[i] += lineOfSightToAnyEnemy.Score(context, positions[i]);
scores[i] += proximityToClosestPickup.Score(context, positions[i]);
scores[i] += proximityToPlayerSpawner.Score(context, positions[i]);
if (greaterScore < scores[i])
{
greaterScoreIndex = i;
greaterScore = scores[i];
}
}
return positions[greaterScoreIndex];
}
}
public sealed class PositionProximityToSelf : CustomScorer<Vector3>
{
public new float factor = 0.1f;
public new float score = 10f;
public override float Score(Context context, Vector3 position)
{
float range = (position - context.player.transform.position).magnitude;
return factor * Mathf.Max(0f, (this.score - range));
}
}
public sealed class ProximityToNearestEnemy : CustomScorer<Vector3>
{
public new float desiredRange = 8f;
public new float score = 50f;
public override float Score(Context context, Vector3 position)
{
List<Enemy> enemies = context.enemies;
float count = enemies.Count;
if (count == 0) { return 0f; }
Vector3 nearest = Vector3.zero;
float shortest = float.MaxValue;
for (int i = 0; i < count; i++)
{
Enemy enemy = enemies[i];
if (enemy)
{
float distance = (position - enemy.transform.position).sqrMagnitude;
if (distance < shortest)
{
shortest = distance;
nearest = enemy.transform.position;
}
}
}
if (nearest.sqrMagnitude == 0f)
{
return 0f;
}
float range = (position - nearest).magnitude;
return Mathf.Max(0f, (this.score - Mathf.Abs(this.desiredRange - range)));
}
}
public sealed class OverRangeToClosestEnemy : CustomScorer<Vector3>
{
new public float desiredRange = 5f;
new public float score = 100f;
public override float Score(Context context, Vector3 position)
{
Player player = context.player;
List<Enemy> enemies = context.enemies;
int count = enemies.Count;
if (count == 0)
{
return 0f;
}
Vector3 nearest = Vector3.zero;
float shortest = float.MaxValue;
for (int i = 0; i < count; i++)
{
Enemy enemy = enemies[i];
if (enemy)
{
float distance = (player.transform.position - enemy.transform.position).sqrMagnitude;
if (distance < shortest)
{
shortest = distance;
nearest = enemy.transform.position;
}
}
}
float range = (position - nearest).magnitude;
if (range > desiredRange)
{
return this.score;
}
else
{
return 0;
}
}
}
public sealed class OverRangeToAnyEnemy : CustomScorer<Vector3>
{
new public float desiredRange = 5f;
new public float score = 50f;
public override float Score(Context context, Vector3 position)
{
Player player = context.player;
List<Enemy> enemies = context.enemies;
int count = enemies.Count;
if (count == 0)
{
return 0f;
}
float sqrDesiredRange = desiredRange * desiredRange;
for (int i = 0; i < count; i++)
{
Enemy enemy = enemies[i];
if (enemy)
{
Vector3 dirPlayerToEnemy = (enemy.transform.position - player.transform.position);
Vector3 dirPositionToEnemy = (enemy.transform.position - position);
dirPlayerToEnemy = new Vector3(dirPlayerToEnemy.x, 0f, dirPlayerToEnemy.z);
dirPositionToEnemy = new Vector3(dirPositionToEnemy.x, 0f, dirPositionToEnemy.z);
//all positions behind the enemy or closer than the desired range are not of interest
if (Vector3.Dot(dirPlayerToEnemy, dirPositionToEnemy) < 0f || dirPositionToEnemy.sqrMagnitude < sqrDesiredRange)
{
return 0f;
}
}
}
return this.score;
}
}
public sealed class LineOfSightToClosestEnemy : CustomScorer<Vector3>
{
new public float score = 50f;
public override float Score(Context context, Vector3 position)
{
Player player = context.player;
List<Enemy> enemies = context.enemies;
int count = enemies.Count;
if (count == 0)
{
return 0f;
}
var nearest = Vector3.zero;
var shortest = float.MaxValue;
for (int i = 0; i < count; i++)
{
Enemy enemy = enemies[i];
if (enemy)
{
float distance = (player.transform.position - enemy.transform.position).sqrMagnitude;
if (distance < shortest)
{
shortest = distance;
nearest = enemy.transform.position;
}
}
}
Vector3 dir = (nearest - position);
float range = dir.magnitude;
Ray ray = new Ray(position + Vector3.up, dir);
if (!Physics.Raycast(ray, range, LayerMask.GetMask("Obstacle")))
{
return this.score;
}
return 0f;
}
}
public sealed class LineOfSightToAnyEnemy : CustomScorer<Vector3>
{
new public float score = 50f;
public override float Score(Context context, Vector3 position)
{
List<Enemy> enemies = context.enemies;
int count = enemies.Count;
if (count == 0)
{
return 0f;
}
for (int i = 0; i < count; i++)
{
Enemy enemy = enemies[i];
if (enemy)
{
Vector3 dir = enemy.transform.position - position;
float range = dir.magnitude;
Ray ray = new Ray(position + Vector3.up, dir);
if (!Physics.Raycast(ray, range, LayerMask.GetMask("Obstacle")))
{
return this.score;
}
}
}
return 0;
}
}
public sealed class ProximityToClosestPickup : CustomScorer<Vector3>
{
new public float multiplier = 2f;
new public float score = 20f;
public override float Score(Context context, Vector3 position)
{
List<Pickup> pickups = context.pickups;
int count = pickups.Count;
if (count == 0)
{
return 0f;
}
Vector3 closest = Vector3.zero;
float shortest = float.MaxValue;
for (int i = 0; i < count; i++)
{
var pickup = pickups[i];
/* sometimes this pickup was already destroyed (picked) in the scene */
if (pickup != null)
{
float distance = (position - pickup.transform.position).sqrMagnitude;
if (distance < shortest)
{
shortest = distance;
closest = pickup.transform.position;
}
}
}
var range = (position - closest).magnitude;
return Mathf.Max(0f, (this.score - range) * multiplier);
}
}
public sealed class ProximityToPlayerSpawner : CustomScorer<Vector3>
{
new public float multiplier = 1f;
new public float score = 100f;
public override float Score(Context context, Vector3 position)
{
float range = (position - context.player.spawnPoint).magnitude;
return Mathf.Max(0f, (this.score - range) * multiplier);
}
}