/
EllipsoidTag.java
604 lines (546 loc) · 22.3 KB
/
EllipsoidTag.java
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
package com.denizenscript.denizen.objects;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizencore.flags.AbstractFlagTracker;
import com.denizenscript.denizencore.flags.FlaggableObject;
import com.denizenscript.denizencore.flags.SavableMapFlagTracker;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.notable.NoteManager;
import com.denizenscript.denizencore.tags.ObjectTagProcessor;
import com.denizenscript.denizencore.objects.notable.Notable;
import com.denizenscript.denizencore.objects.notable.Note;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.YamlConfiguration;
import org.bukkit.Location;
import org.bukkit.util.Vector;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
public class EllipsoidTag implements ObjectTag, Notable, Cloneable, AreaContainmentObject, FlaggableObject {
// <--[ObjectType]
// @name EllipsoidTag
// @prefix ellipsoid
// @base ElementTag
// @implements FlaggableObject, AreaObject
// @ExampleTagBase ellipsoid[my_noted_ellipsoid]
// @ExampleValues my_ellipsoid_note
// @ExampleForReturns
// - note %VALUE% as:my_new_ellipsoid
// @format
// The identity format for ellipsoids is <x>,<y>,<z>,<world>,<x-radius>,<y-radius>,<z-radius>
// For example, 'ellipsoid@1,2,3,space,7,7,7'.
//
// @description
// An EllipsoidTag represents an ellipsoidal region in the world.
//
// The word 'ellipsoid' means a less strict sphere.
// Basically: an "ellipsoid" is to a 3D "sphere" what an "ellipse" (or "oval") is to a 2D "circle".
//
// This object type can be noted.
//
// This object type is flaggable when it is noted.
// Flags on this object type will be stored in the notables.yml file.
//
// -->
public static List<EllipsoidTag> getNotableEllipsoidsContaining(Location location) {
List<EllipsoidTag> ellipsoids = new ArrayList<>();
for (EllipsoidTag ellipsoid : NoteManager.getAllType(EllipsoidTag.class)) {
if (ellipsoid.contains(location)) {
ellipsoids.add(ellipsoid);
}
}
return ellipsoids;
}
//////////////////
// OBJECT FETCHER
////////////////
@Deprecated
public static EllipsoidTag valueOf(String string) {
return valueOf(string, null);
}
/**
* Gets an Ellipsoid Object from a string form.
*
* @param string the string
*/
@Fetchable("ellipsoid")
public static EllipsoidTag valueOf(String string, TagContext context) {
if (string.startsWith("ellipsoid@")) {
string = string.substring(10);
}
if (string.contains("@")) {
return null;
}
Notable noted = NoteManager.getSavedObject(string);
if (noted instanceof EllipsoidTag) {
return (EllipsoidTag) noted;
}
List<String> split = CoreUtilities.split(string, ',');
if (split.size() != 7) {
return null;
}
WorldTag world = WorldTag.valueOf(split.get(3), false);
if (world == null) {
return null;
}
for (int i = 0; i < 7; i++) {
if (i != 3 && !ArgumentHelper.matchesDouble(split.get(i))) {
if (context == null || context.showErrors()) {
Debug.echoError("EllipsoidTag input is not a valid decimal number: " + split.get(i));
return null;
}
}
}
LocationTag location = new LocationTag(world.getWorld(), Double.parseDouble(split.get(0)), Double.parseDouble(split.get(1)), Double.parseDouble(split.get(2)));
LocationTag size = new LocationTag(null, Double.parseDouble(split.get(4)), Double.parseDouble(split.get(5)), Double.parseDouble(split.get(6)));
return new EllipsoidTag(location, size);
}
/**
* Determines whether a string is a valid ellipsoid.
*
* @param arg the string
* @return true if matched, otherwise false
*/
public static boolean matches(String arg) {
try {
return EllipsoidTag.valueOf(arg, CoreUtilities.noDebugContext) != null;
}
catch (Exception e) {
return false;
}
}
@Override
public EllipsoidTag clone() {
return new EllipsoidTag(center.clone(), size.clone());
}
@Override
public ObjectTag duplicate() {
if (noteName != null) {
return this;
}
return clone();
}
///////////////
// Constructors
/////////////
public EllipsoidTag(LocationTag center, LocationTag size) {
this.center = center;
this.size = size;
}
/////////////////////
// INSTANCE FIELDS/METHODS
/////////////////
public LocationTag center;
public LocationTag size;
public String noteName = null;
public AbstractFlagTracker flagTracker = null;
@Override
public ListTag getBlocks(Predicate<Location> test) {
return getCuboidBoundary().getBlocks(test == null ? this::contains : (l) -> (test.test(l) && contains(l)));
}
public List<LocationTag> getBlockLocationsUnfiltered(boolean doMax) {
List<LocationTag> initial = getCuboidBoundary().getBlockLocationsUnfiltered(doMax);
List<LocationTag> locations = new ArrayList<>();
for (LocationTag loc : initial) {
if (contains(loc)) {
locations.add(loc);
}
}
return locations;
}
@Override
public ListTag getShell() {
ListTag output = new ListTag();
double yScale = size.getY();
int maxY = (int) Math.floor(yScale);
output.addObject(new LocationTag(center.getBlockX(), center.getBlockY() - maxY, center.getBlockZ(), center.getWorldName()));
if (maxY != 0) {
output.addObject(new LocationTag(center.getBlockX(), center.getBlockY() + maxY, center.getBlockZ(), center.getWorldName()));
}
for (int y = -maxY; y <= maxY; y++) {
double yProgMin = Math.min(1.0, (Math.abs(y) + 1) / yScale);
double yProgMax = Math.abs(y) / yScale;
double minSubWidth = Math.sqrt(1.0 - yProgMin * yProgMin);
double maxSubWidth = Math.sqrt(1.0 - yProgMax * yProgMax);
double minX = size.getX() * minSubWidth - 1;
double minZ = size.getZ() * minSubWidth - 1;
double maxX = size.getX() * maxSubWidth;
double maxZ = size.getZ() * maxSubWidth;
for (int x = 0; x < maxX; x++) {
for (int z = 0; z < maxZ; z++) {
double scaleTestMin = (x * x) / (minX * minX) + (z * z) / (minZ * minZ);
double scaleTestMax = (x * x) / (maxX * maxX) + (z * z) / (maxZ * maxZ);
if (scaleTestMin >= 1.0 && scaleTestMax <= 1.0) {
output.addObject(new LocationTag(center.getBlockX() + x, center.getBlockY() + y, center.getBlockZ() + z, center.getWorldName()));
if (x != 0) {
output.addObject(new LocationTag(center.getBlockX() - x, center.getBlockY() + y, center.getBlockZ() + z, center.getWorldName()));
}
if (z != 0) {
output.addObject(new LocationTag(center.getBlockX() + x, center.getBlockY() + y, center.getBlockZ() - z, center.getWorldName()));
}
if (x != 0 && z != 0) {
output.addObject(new LocationTag(center.getBlockX() - x, center.getBlockY() + y, center.getBlockZ() - z, center.getWorldName()));
}
}
}
}
}
return output;
}
public boolean contains(Location test) {
if (test.getWorld() == null || !test.getWorld().getName().equals(center.getWorld().getName())) {
return false;
}
double xbase = test.getX() - center.getX();
double ybase = test.getY() - center.getY();
double zbase = test.getZ() - center.getZ();
return ((xbase * xbase) / (size.getX() * size.getX())
+ (ybase * ybase) / (size.getY() * size.getY())
+ (zbase * zbase) / (size.getZ() * size.getZ()) <= 1);
}
public boolean intersects(ChunkTag chunk) {
int xMin = chunk.getX() * 16;
int zMin = chunk.getZ() * 16;
LocationTag locTest = chunk.getCenter();
// This mess gets a position within the chunk that is as closes as possible to the ellipsoid's center
locTest.setY(center.getY());
if (center.getX() > xMin) {
if (center.getX() < xMin + 16) {
locTest.setX(center.getX());
}
else {
locTest.setX(center.getX());
}
}
if (center.getZ() > zMin) {
if (center.getZ() < zMin + 16) {
locTest.setZ(center.getZ());
}
else {
locTest.setZ(center.getZ());
}
}
return contains(locTest);
}
String prefix = "ellipsoid";
@Override
public String getPrefix() {
return prefix;
}
@Override
public String debuggable() {
if (isUnique()) {
return "<LG>ellipsoid@<Y>" + noteName + "<GR> (" + identifyFull() + ")";
}
else {
return identifyFull();
}
}
@Override
public boolean isUnique() {
return noteName != null;
}
@Override
@Note("Ellipsoids")
public Object getSaveObject() {
YamlConfiguration section = new YamlConfiguration();
section.set("object", identifyFull());
section.set("flags", flagTracker.toString());
return section;
}
@Override
public void makeUnique(String id) {
EllipsoidTag toNote = clone();
toNote.noteName = id;
toNote.flagTracker = new SavableMapFlagTracker();
NoteManager.saveAs(toNote, id);
}
@Override
public void forget() {
NoteManager.remove(this);
noteName = null;
flagTracker = null;
}
@Override
public int hashCode() {
if (noteName != null) {
return noteName.hashCode();
}
return center.hashCode() + size.hashCode();
}
@Override
public boolean equals(Object other) {
if (!(other instanceof EllipsoidTag)) {
return false;
}
EllipsoidTag ellipsoid2 = (EllipsoidTag) other;
if ((noteName == null) != (ellipsoid2.noteName == null)) {
return false;
}
if (noteName != null && !noteName.equals(ellipsoid2.noteName)) {
return false;
}
if (!center.getWorldName().equals(ellipsoid2.center.getWorldName())) {
return false;
}
if (center.distanceSquaredNoWorld(ellipsoid2.center) >= 0.25) {
return false;
}
if (size.distanceSquaredNoWorld(ellipsoid2.size) >= 0.25) {
return false;
}
return true;
}
@Override
public String getObjectType() {
return "Ellipsoid";
}
@Override
public String identify() {
if (isUnique()) {
return "ellipsoid@" + noteName;
}
else {
return identifyFull();
}
}
@Override
public String identifySimple() {
return identify();
}
public String identifyFull() {
return "ellipsoid@" + center.getX() + "," + center.getY() + "," + center.getZ() + "," + center.getWorldName()
+ "," + size.getX() + "," + size.getY() + "," + size.getZ();
}
@Override
public String toString() {
return identify();
}
@Override
public ObjectTag setPrefix(String prefix) {
if (prefix != null) {
this.prefix = prefix;
}
return this;
}
@Override
public AbstractFlagTracker getFlagTracker() {
return flagTracker;
}
@Override
public void reapplyTracker(AbstractFlagTracker tracker) {
if (noteName != null) {
this.flagTracker = tracker;
}
}
@Override
public String getReasonNotFlaggable() {
if (noteName == null) {
return "the area is not noted - only noted areas can hold flags";
}
return "unknown reason - something went wrong";
}
@Override
public CuboidTag getCuboidBoundary() {
return new CuboidTag(center.clone().subtract(size.toVector()), center.clone().add(size.toVector()));
}
@Override
public WorldTag getWorld() {
return new WorldTag(center.getWorldName());
}
@Override
public EllipsoidTag withWorld(WorldTag world) {
LocationTag loc = center.clone();
loc.setWorld(world.getWorld());
return new EllipsoidTag(loc, size.clone());
}
public static void registerTags() {
AbstractFlagTracker.registerFlagHandlers(tagProcessor);
AreaContainmentObject.registerTags(EllipsoidTag.class, tagProcessor);
// <--[tag]
// @attribute <EllipsoidTag.random>
// @returns LocationTag
// @description
// Returns a random decimal location within the ellipsoid.
// Note that distribution of results will not be completely even.
// -->
tagProcessor.registerTag(LocationTag.class, "random", (attribute, object) -> {
// This is an awkward hack to try to weight towards the center a bit (to counteract the weight-away-from-center that would otherwise happen).
double y = (Math.sqrt(CoreUtilities.getRandom().nextDouble()) * 2 - 1) * object.size.getY();
Vector result = new Vector();
result.setY(y);
double yProg = Math.abs(y) / object.size.getY();
double subWidth = Math.sqrt(1.0 - yProg * yProg);
double maxX = object.size.getX() * subWidth;
double maxZ = object.size.getZ() * subWidth;
result.setX(maxX * (CoreUtilities.getRandom().nextDouble() * 2 - 1));
result.setZ(maxZ * (CoreUtilities.getRandom().nextDouble() * 2 - 1));
LocationTag out = object.center.clone();
out.add(result);
return out;
});
// <--[tag]
// @attribute <EllipsoidTag.location>
// @returns LocationTag
// @description
// Returns the center location of the ellipsoid.
// -->
tagProcessor.registerTag(LocationTag.class, "location", (attribute, object) -> {
return object.center;
});
// <--[tag]
// @attribute <EllipsoidTag.size>
// @returns LocationTag
// @description
// Returns the size of the ellipsoid.
// -->
tagProcessor.registerTag(LocationTag.class, "size", (attribute, object) -> {
return object.size;
});
// <--[tag]
// @attribute <EllipsoidTag.add[<location>]>
// @returns EllipsoidTag
// @description
// Returns a copy of this ellipsoid, shifted by the input location.
// -->
tagProcessor.registerTag(EllipsoidTag.class, "add", (attribute, object) -> {
if (!attribute.hasParam()) {
attribute.echoError("ellipsoid.add[...] tag must have an input.");
return null;
}
return new EllipsoidTag(object.center.clone().add(attribute.paramAsType(LocationTag.class)), object.size.clone());
});
// <--[tag]
// @attribute <EllipsoidTag.include[<location>]>
// @returns EllipsoidTag
// @description
// Returns a copy of this ellipsoid, with the size value adapted to include the specified world location.
// -->
tagProcessor.registerTag(EllipsoidTag.class, "include", (attribute, object) -> {
if (!attribute.hasParam()) {
attribute.echoError("ellipsoid.include[...] tag must have an input.");
return null;
}
LocationTag target = attribute.paramAsType(LocationTag.class);
if (object.contains(target)) {
return object;
}
LocationTag size = object.size.clone();
Vector relative = target.toVector().subtract(object.center.toVector());
// Cuboid minimum expansion
size.setX(Math.max(size.getX(), Math.abs(relative.getX())));
size.setY(Math.max(size.getY(), Math.abs(relative.getY())));
size.setZ(Math.max(size.getZ(), Math.abs(relative.getZ())));
EllipsoidTag result = new EllipsoidTag(object.center.clone(), new LocationTag(size));
if (result.contains(target)) {
return result;
}
double sizeLen = size.length();
// Ellipsoid additional expand
while (!result.contains(target)) {
// I gave up on figuring out the math for this, so here's an awful loop-hack
double projX = (relative.getX() * relative.getX()) / (size.getX() * size.getX());
double projY = (relative.getY() * relative.getY()) / (size.getY() * size.getY());
double projZ = (relative.getZ() * relative.getZ()) / (size.getZ() * size.getZ());
double scale = Math.max(projX + projY + projZ, sizeLen * 0.01);
if (projX >= projY && projX >= projZ) {
size.setX(size.getX() + scale);
}
else if (projY >= projX && projY >= projZ) {
size.setY(size.getY() + scale);
}
else if (projZ >= projX && projZ >= projY) {
size.setZ(size.getZ() + scale);
}
else {
size = size.add(scale, scale, scale);
}
result.size = size;
}
return result;
});
// <--[tag]
// @attribute <EllipsoidTag.with_location[<location>]>
// @returns EllipsoidTag
// @description
// Returns a copy of this ellipsoid, set to the specified location.
// -->
tagProcessor.registerTag(EllipsoidTag.class, "with_location", (attribute, object) -> {
if (!attribute.hasParam()) {
attribute.echoError("ellipsoid.with_location[...] tag must have an input.");
return null;
}
return new EllipsoidTag(attribute.paramAsType(LocationTag.class), object.size.clone());
});
// <--[tag]
// @attribute <EllipsoidTag.with_size[<location>]>
// @returns EllipsoidTag
// @description
// Returns a copy of this ellipsoid, set to the specified size.
// -->
tagProcessor.registerTag(EllipsoidTag.class, "with_size", (attribute, object) -> {
if (!attribute.hasParam()) {
attribute.echoError("ellipsoid.with_size[...] tag must have an input.");
return null;
}
return new EllipsoidTag(object.center.clone(), attribute.paramAsType(LocationTag.class));
});
// <--[tag]
// @attribute <EllipsoidTag.chunks>
// @returns ListTag(ChunkTag)
// @description
// Returns a list of all chunks that this ellipsoid touches at all (note that no valid ellipsoid tag can ever totally contain a chunk, due to vertical limits and roundness).
// -->
tagProcessor.registerTag(ListTag.class, "chunks", (attribute, object) -> {
ListTag chunks = new ListTag();
double minPossibleX = object.center.getX() - object.size.getX();
double minPossibleZ = object.center.getZ() - object.size.getZ();
double maxPossibleX = object.center.getX() + object.size.getX();
double maxPossibleZ = object.center.getZ() + object.size.getZ();
int minChunkX = (int) Math.floor(minPossibleX / 16);
int minChunkZ = (int) Math.floor(minPossibleZ / 16);
int maxChunkX = (int) Math.ceil(maxPossibleX / 16);
int maxChunkZ = (int) Math.ceil(maxPossibleZ / 16);
ChunkTag testChunk = new ChunkTag(object.center);
for (int x = minChunkX; x <= maxChunkX; x++) {
testChunk.chunkX = x;
for (int z = minChunkZ; z <= maxChunkZ; z++) {
testChunk.chunkZ = z;
if (object.intersects(testChunk)) {
chunks.addObject(new ChunkTag(testChunk.world, testChunk.chunkX, testChunk.chunkZ));
}
}
}
return chunks;
});
// <--[tag]
// @attribute <EllipsoidTag.note_name>
// @returns ElementTag
// @description
// Gets the name of a noted EllipsoidTag. If the ellipsoid isn't noted, this is null.
// -->
tagProcessor.registerTag(ElementTag.class, "note_name", (attribute, ellipsoid) -> {
String noteName = NoteManager.getSavedId(ellipsoid);
if (noteName == null) {
return null;
}
return new ElementTag(noteName);
});
}
public static ObjectTagProcessor<EllipsoidTag> tagProcessor = new ObjectTagProcessor<>();
@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
return tagProcessor.getObjectAttribute(this, attribute);
}
@Override
public String getNoteName() {
return noteName;
}
@Override
public boolean doesContainLocation(Location loc) {
return contains(loc);
}
}