forked from RSPSApp/elvarg-rsps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RegionManager.java
946 lines (884 loc) · 40.4 KB
/
RegionManager.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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
package com.elvarg.game.collision;
import com.elvarg.game.GameConstants;
import com.elvarg.game.definition.ObjectDefinition;
import com.elvarg.game.entity.impl.Mobile;
import com.elvarg.game.entity.impl.object.GameObject;
import com.elvarg.game.entity.impl.object.MapObjects;
import com.elvarg.game.model.Direction;
import com.elvarg.game.model.Location;
import com.elvarg.game.model.areas.impl.PrivateArea;
import com.elvarg.util.CompressionUtil;
import com.elvarg.util.FileUtil;
import javax.naming.OperationNotSupportedException;
import java.io.File;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
/**
* This manager handles all regions and their related functions, such as
* clipping.
*
* @author Professor Oak
*/
public class RegionManager {
public static final int PROJECTILE_NORTH_WEST_BLOCKED = 0x200;
public static final int PROJECTILE_NORTH_BLOCKED = 0x400;
public static final int PROJECTILE_NORTH_EAST_BLOCKED = 0x800;
public static final int PROJECTILE_EAST_BLOCKED = 0x1000;
public static final int PROJECTILE_SOUTH_EAST_BLOCKED = 0x2000;
public static final int PROJECTILE_SOUTH_BLOCKED = 0x4000;
public static final int PROJECTILE_SOUTH_WEST_BLOCKED = 0x8000;
public static final int PROJECTILE_WEST_BLOCKED = 0x10000;
public static final int PROJECTILE_TILE_BLOCKED = 0x20000;
public static final int UNKNOWN = 0x80000;
public static final int BLOCKED_TILE = 0x200000;
public static final int UNLOADED_TILE = 0x1000000;
public static final int OCEAN_TILE = 2097152;
/**
* The map with all of our regions.
*/
public static Map<Integer, Region> regions = new HashMap<Integer, Region>();
/**
* Loads the client's map_index file and constructs new regions based on the
* data it holds.
*
* @throws Exception
*/
public static void init() throws Exception {
// Load object definitions..
ObjectDefinition.init();
// Load regions..
File map_index = new File(GameConstants.CLIPPING_DIRECTORY + "map_index");
if (!map_index.exists()) {
throw new OperationNotSupportedException("map_index was not found!");
}
byte[] data = Files.readAllBytes(map_index.toPath());
Buffer stream = new Buffer(data);
int size = stream.readUShort();
for (int i = 0; i < size; i++) {
int regionId = stream.readUShort();
int terrainFile = stream.readUShort();
int objectFile = stream.readUShort();
RegionManager.regions.put(regionId, new Region(regionId, terrainFile, objectFile));
}
}
/**
* Attempts to get a {@link Region} based on an id.
*
* @param regionId
* @return
*/
public static Optional<Region> getRegion(int regionId) {
return Optional.ofNullable(regions.get(regionId));
}
/**
* Attempts to get a {@link Region} based on coordinates.
*
* @param x
* @param y
* @return
*/
public static Optional<Region> getRegion(int x, int y) {
loadMapFiles(x, y);
int regionX = x >> 3;
int regionY = y >> 3;
int regionId = ((regionX / 8) << 8) + (regionY / 8);
return getRegion(regionId);
}
/**
* Attempts to add clipping for a variable object.
*
* @param x
* @param y
* @param height
* @param type
* @param direction
* @param tall
*/
private static void addClippingForVariableObject(int x, int y, int height, int type, int direction, boolean tall, PrivateArea privateArea) {
if (type == 0) {
if (direction == 0) {
addClipping(x, y, height, 128, privateArea);
addClipping(x - 1, y, height, 8, privateArea);
} else if (direction == 1) {
addClipping(x, y, height, 2, privateArea);
addClipping(x, y + 1, height, 32, privateArea);
} else if (direction == 2) {
addClipping(x, y, height, 8, privateArea);
addClipping(x + 1, y, height, 128, privateArea);
} else if (direction == 3) {
addClipping(x, y, height, 32, privateArea);
addClipping(x, y - 1, height, 2, privateArea);
}
} else if (type == 1 || type == 3) {
if (direction == 0) {
addClipping(x, y, height, 1, privateArea);
addClipping(x - 1, y, height, 16, privateArea);
} else if (direction == 1) {
addClipping(x, y, height, 4, privateArea);
addClipping(x + 1, y + 1, height, 64, privateArea);
} else if (direction == 2) {
addClipping(x, y, height, 16, privateArea);
addClipping(x + 1, y - 1, height, 1, privateArea);
} else if (direction == 3) {
addClipping(x, y, height, 64, privateArea);
addClipping(x - 1, y - 1, height, 4, privateArea);
}
} else if (type == 2) {
if (direction == 0) {
addClipping(x, y, height, 130, privateArea);
addClipping(x - 1, y, height, 8, privateArea);
addClipping(x, y + 1, height, 32, privateArea);
} else if (direction == 1) {
addClipping(x, y, height, 10, privateArea);
addClipping(x, y + 1, height, 32, privateArea);
addClipping(x + 1, y, height, 128, privateArea);
} else if (direction == 2) {
addClipping(x, y, height, 40, privateArea);
addClipping(x + 1, y, height, 128, privateArea);
addClipping(x, y - 1, height, 2, privateArea);
} else if (direction == 3) {
addClipping(x, y, height, 160, privateArea);
addClipping(x, y - 1, height, 2, privateArea);
addClipping(x - 1, y, height, 8, privateArea);
}
}
if (tall) {
// If an object is tall, it blocks projectiles too
if (type == 0) {
if (direction == 0) {
addClipping(x, y, height, 65536, privateArea);
addClipping(x - 1, y, height, 4096, privateArea);
} else if (direction == 1) {
addClipping(x, y, height, 1024, privateArea);
addClipping(x, y + 1, height, 16384, privateArea);
} else if (direction == 2) {
addClipping(x, y, height, 4096, privateArea);
addClipping(x + 1, y, height, 65536, privateArea);
} else if (direction == 3) {
addClipping(x, y, height, 16384, privateArea);
addClipping(x, y - 1, height, 1024, privateArea);
}
}
if (type == 1 || type == 3) {
if (direction == 0) {
addClipping(x, y, height, 512, privateArea);
addClipping(x - 1, y + 1, height, 8192, privateArea);
} else if (direction == 1) {
addClipping(x, y, height, 2048, privateArea);
addClipping(x + 1, y + 1, height, 32768, privateArea);
} else if (direction == 2) {
addClipping(x, y, height, 8192, privateArea);
addClipping(x + 1, y + 1, height, 512, privateArea);
} else if (direction == 3) {
addClipping(x, y, height, 32768, privateArea);
addClipping(x - 1, y - 1, height, 2048, privateArea);
}
} else if (type == 2) {
if (direction == 0) {
addClipping(x, y, height, 66560, privateArea);
addClipping(x - 1, y, height, 4096, privateArea);
addClipping(x, y + 1, height, 16384, privateArea);
} else if (direction == 1) {
addClipping(x, y, height, 5120, privateArea);
addClipping(x, y + 1, height, 16384, privateArea);
addClipping(x + 1, y, height, 65536, privateArea);
} else if (direction == 2) {
addClipping(x, y, height, 20480, privateArea);
addClipping(x + 1, y, height, 65536, privateArea);
addClipping(x, y - 1, height, 1024, privateArea);
} else if (direction == 3) {
addClipping(x, y, height, 81920, privateArea);
addClipping(x, y - 1, height, 1024, privateArea);
addClipping(x - 1, y, height, 4096, privateArea);
}
}
}
}
/**
* Attempts to remove clipping for a variable object.
*
* @param x
* @param y
* @param height
* @param type
* @param direction
* @param tall
*/
private static void removeClippingForVariableObject(int x, int y, int height, int type, int direction,
boolean tall, PrivateArea privateArea) {
if (type == 0) {
if (direction == 0) {
removeClipping(x, y, height, 128, privateArea);
removeClipping(x - 1, y, height, 8, privateArea);
} else if (direction == 1) {
removeClipping(x, y, height, 2, privateArea);
removeClipping(x, y + 1, height, 32, privateArea);
} else if (direction == 2) {
removeClipping(x, y, height, 8, privateArea);
removeClipping(x + 1, y, height, 128, privateArea);
} else if (direction == 3) {
removeClipping(x, y, height, 32, privateArea);
removeClipping(x, y - 1, height, 2, privateArea);
}
} else if (type == 1 || type == 3) {
if (direction == 0) {
removeClipping(x, y, height, 1, privateArea);
removeClipping(x - 1, y, height, 16, privateArea);
} else if (direction == 1) {
removeClipping(x, y, height, 4, privateArea);
removeClipping(x + 1, y + 1, height, 64, privateArea);
} else if (direction == 2) {
removeClipping(x, y, height, 16, privateArea);
removeClipping(x + 1, y - 1, height, 1, privateArea);
} else if (direction == 3) {
removeClipping(x, y, height, 64, privateArea);
removeClipping(x - 1, y - 1, height, 4, privateArea);
}
} else if (type == 2) {
if (direction == 0) {
removeClipping(x, y, height, 130, privateArea);
removeClipping(x - 1, y, height, 8, privateArea);
removeClipping(x, y + 1, height, 32, privateArea);
} else if (direction == 1) {
removeClipping(x, y, height, 10, privateArea);
removeClipping(x, y + 1, height, 32, privateArea);
removeClipping(x + 1, y, height, 128, privateArea);
} else if (direction == 2) {
removeClipping(x, y, height, 40, privateArea);
removeClipping(x + 1, y, height, 128, privateArea);
removeClipping(x, y - 1, height, 2, privateArea);
} else if (direction == 3) {
removeClipping(x, y, height, 160, privateArea);
removeClipping(x, y - 1, height, 2, privateArea);
removeClipping(x - 1, y, height, 8, privateArea);
}
}
if (tall) {
// If an object is tall, it blocks projectiles too
if (type == 0) {
if (direction == 0) {
removeClipping(x, y, height, 65536, privateArea);
removeClipping(x - 1, y, height, 4096, privateArea);
} else if (direction == 1) {
removeClipping(x, y, height, 1024, privateArea);
removeClipping(x, y + 1, height, 16384, privateArea);
} else if (direction == 2) {
removeClipping(x, y, height, 4096, privateArea);
removeClipping(x + 1, y, height, 65536, privateArea);
} else if (direction == 3) {
removeClipping(x, y, height, 16384, privateArea);
removeClipping(x, y - 1, height, 1024, privateArea);
}
}
if (type == 1 || type == 3) {
if (direction == 0) {
removeClipping(x, y, height, 512, privateArea);
removeClipping(x - 1, y + 1, height, 8192, privateArea);
} else if (direction == 1) {
removeClipping(x, y, height, 2048, privateArea);
removeClipping(x + 1, y + 1, height, 32768, privateArea);
} else if (direction == 2) {
removeClipping(x, y, height, 8192, privateArea);
removeClipping(x + 1, y + 1, height, 512, privateArea);
} else if (direction == 3) {
removeClipping(x, y, height, 32768, privateArea);
removeClipping(x - 1, y - 1, height, 2048, privateArea);
}
} else if (type == 2) {
if (direction == 0) {
removeClipping(x, y, height, 66560, privateArea);
removeClipping(x - 1, y, height, 4096, privateArea);
removeClipping(x, y + 1, height, 16384, privateArea);
} else if (direction == 1) {
removeClipping(x, y, height, 5120, privateArea);
removeClipping(x, y + 1, height, 16384, privateArea);
removeClipping(x + 1, y, height, 65536, privateArea);
} else if (direction == 2) {
removeClipping(x, y, height, 20480, privateArea);
removeClipping(x + 1, y, height, 65536, privateArea);
removeClipping(x, y - 1, height, 1024, privateArea);
} else if (direction == 3) {
removeClipping(x, y, height, 81920, privateArea);
removeClipping(x, y - 1, height, 1024, privateArea);
removeClipping(x - 1, y, height, 4096, privateArea);
}
}
}
}
/**
* Attempts to add clipping for a solid object.
*
* @param x
* @param y
* @param height
* @param xLength
* @param yLength
* @param flag
*/
private static void addClippingForSolidObject(int x, int y, int height, int xLength, int yLength, boolean flag, PrivateArea privateArea) {
int clipping = 256;
if (flag) {
clipping += 0x20000;
}
for (int i = x; i < x + xLength; i++) {
for (int i2 = y; i2 < y + yLength; i2++) {
addClipping(i, i2, height, clipping, privateArea);
}
}
}
/**
* Attempts to remove clipping for a solid object.
*
* @param x
* @param y
* @param height
* @param xLength
* @param yLength
* @param flag
*/
private static void removeClippingForSolidObject(int x, int y, int height, int xLength, int yLength, boolean flag, PrivateArea privateArea) {
int clipping = 256;
if (flag) {
clipping += 0x20000;
}
for (int x_ = x; x_ < x + xLength; x_++) {
for (int y_ = y; y_ < y + yLength; y_++) {
removeClipping(x_, y_, height, clipping, privateArea);
}
}
}
/**
* Adds an object to a region.
*
* @param objectId
* @param x
* @param y
* @param height
* @param type
* @param direction
*/
public static void addObject(int objectId, int x, int y, int height, int type, int direction) {
final Location position = new Location(x, y, height);
// Edge walls removal
if (height == 0) {
if (x >= 3092 && x <= 3094 && (y == 3513 || y == 3514 || y == 3507 || y == 3506)) {
objectId = -1;
}
}
switch (objectId) {
case 14233: // pest control gates
case 14235: // pest control gates
return;
}
if (objectId == -1) {
MapObjects.clear(position, type);
} else {
MapObjects.add(new GameObject(objectId, position, type, direction, null));
}
}
public static void addObjectClipping(GameObject object) {
int id = object.getId();
int x = object.getLocation().getX();
int y = object.getLocation().getY();
int height = object.getLocation().getZ();
int type = object.getType();
int direction = object.getFace();
if (id == -1) {
removeClipping(x, y, height, 0x000000, object.getPrivateArea());
return;
}
ObjectDefinition def = object.getDefinition();
if (def == null) {
return;
}
int xLength;
int yLength;
if (direction != 1 && direction != 3) {
xLength = def.getSizeX();
yLength = def.getSizeY();
} else {
yLength = def.getSizeX();
xLength = def.getSizeY();
}
if (type == 22) {
if (def.hasActions() && def.solid) {
addClipping(x, y, height, 0x200000, object.getPrivateArea());
}
} else if (type >= 9) {
if (def.solid) {
addClippingForSolidObject(x, y, height, xLength, yLength, def.impenetrable, object.getPrivateArea());
}
} else if (type >= 0 && type <= 3) {
if (def.solid) {
addClippingForVariableObject(x, y, height, type, direction, def.impenetrable, object.getPrivateArea());
}
}
}
public static void removeObjectClipping(GameObject object) {
int x = object.getLocation().getX();
int y = object.getLocation().getY();
int height = object.getLocation().getZ();
int type = object.getType();
int direction = object.getFace();
if (object.getId() == -1) {
removeClipping(x, y, height, 0x000000, object.getPrivateArea());
return;
}
ObjectDefinition def = object.getDefinition();
if (def == null) {
return;
}
int xLength;
int yLength;
if (direction != 1 && direction != 3) {
xLength = def.getSizeX();
yLength = def.getSizeY();
} else {
yLength = def.getSizeX();
xLength = def.getSizeY();
}
if (type == 22) {
if (def.hasActions() && def.solid) {
removeClipping(x, y, height, 0x200000, object.getPrivateArea());
}
} else if (type >= 9) {
if (def.solid) {
removeClippingForSolidObject(x, y, height, xLength, yLength, def.solid, object.getPrivateArea());
}
} else if (type >= 0 && type <= 3) {
if (def.solid) {
removeClippingForVariableObject(x, y, height, type, direction, def.solid, object.getPrivateArea());
}
}
}
/**
* Attempts to add clipping to a region.
*
* @param x
* @param y
* @param height
* @param shift
*/
public static void addClipping(int x, int y, int height, int shift, PrivateArea privateArea) {
if (privateArea != null) {
privateArea.setClip(new Location(x, y, height), shift);
return;
}
Optional<Region> r = getRegion(x, y);
if (r.isPresent()) {
r.get().addClip(x, y, height, shift);
}
}
/**
* Attempts to remove clipping from a region
*
* @param x
* @param y
* @param height
* @param shift
*/
public static void removeClipping(int x, int y, int height, int shift, PrivateArea privateArea) {
if (privateArea != null) {
privateArea.removeClip(new Location(x, y, height));
return;
}
Optional<Region> r = getRegion(x, y);
if (r.isPresent()) {
r.get().removeClip(x, y, height, shift);
}
}
/**
* Attempts to get the clipping for a region.
*
* @param x
* @param y
* @param height
* @return
*/
public static int getClipping(int x, int y, int height, PrivateArea privateArea) {
if (privateArea != null) {
int privateClip = privateArea.getClip(new Location(x, y, height));
if (privateClip != 0) {
return privateClip;
}
}
Optional<Region> r = getRegion(x, y);
if (r.isPresent()) {
return r.get().getClip(x, y, height);
}
return 0;
}
public static boolean wallExists(Location location, PrivateArea area, int type) {
GameObject object = MapObjects.get(location, type, area);
if (object != null) {
ObjectDefinition objectDef = object.getDefinition();
if (objectDef.name == null || objectDef.name.equals("null")) {
return true;
}
}
return false;
}
public static boolean wallsExist(Location location, PrivateArea area) {
if (wallExists(location, area, 0)) {
return true;
}
if (wallExists(location, area, 1)) {
return true;
}
if (wallExists(location, area, 2)) {
return true;
}
if (wallExists(location, area, 3)) {
return true;
}
if (wallExists(location, area, 9)) {
return true;
}
return false;
}
/**
* Tells you if this direction is walkable.
*
* @param pos The destination.
* @param direction the direction.
* @return if the direction is walkable.
*/
public static boolean canMove(Location pos, int direction, PrivateArea privateArea) {
if (direction == 0) {
return !blockedNorthWest(pos, privateArea) && !blockedNorth(pos, privateArea) && !blockedWest(pos, privateArea);
} else if (direction == 1) {
return !blockedNorth(pos, privateArea);
} else if (direction == 2) {
return !blockedNorthEast(pos, privateArea) && !blockedNorth(pos, privateArea) && !blockedEast(pos, privateArea);
} else if (direction == 3) {
return !blockedWest(pos, privateArea);
} else if (direction == 4) {
return !blockedEast(pos, privateArea);
} else if (direction == 5) {
return !blockedSouthWest(pos, privateArea) && !blockedSouth(pos, privateArea) && !blockedWest(pos, privateArea);
} else if (direction == 6) {
return !blockedSouth(pos, privateArea);
} else if (direction == 7) {
return !blockedSouthEast(pos, privateArea) && !blockedSouth(pos, privateArea) && !blockedEast(pos, privateArea);
}
return false;
}
public static boolean blockedProjectile(Location position, PrivateArea privateArea) {
return (getClipping(position.getX(), position.getY(), position.getZ(), privateArea) & 0x20000) == 0;
}
public static boolean blocked(Location pos, PrivateArea privateArea) {
return (getClipping(pos.getX(), pos.getY(), pos.getZ(), privateArea) & 0x1280120) != 0;
}
public static boolean blockedNorth(Location pos, PrivateArea privateArea) {
return (getClipping(pos.getX(), pos.getY() + 1, pos.getZ(), privateArea) & 0x1280120) != 0;
}
public static boolean blockedEast(Location pos, PrivateArea privateArea) {
return (getClipping(pos.getX() + 1, pos.getY(), pos.getZ(), privateArea) & 0x1280180) != 0;
}
public static boolean blockedSouth(Location pos, PrivateArea privateArea) {
return (getClipping(pos.getX(), pos.getY() - 1, pos.getZ(), privateArea) & 0x1280102) != 0;
}
public static boolean blockedWest(Location pos, PrivateArea privateArea) {
return (getClipping(pos.getX() - 1, pos.getY(), pos.getZ(), privateArea) & 0x1280108) != 0;
}
public static boolean blockedNorthEast(Location pos, PrivateArea privateArea) {
return (getClipping(pos.getX() + 1, pos.getY() + 1, pos.getZ(), privateArea) & 0x12801e0) != 0;
}
public static boolean blockedNorthWest(Location pos, PrivateArea privateArea) {
return (getClipping(pos.getX() - 1, pos.getY() + 1, pos.getZ(), privateArea) & 0x1280138) != 0;
}
public static boolean blockedSouthEast(Location pos, PrivateArea privateArea) {
return (getClipping(pos.getX() + 1, pos.getY() - 1, pos.getZ(), privateArea) & 0x1280183) != 0;
}
public static boolean blockedSouthWest(Location pos, PrivateArea privateArea) {
return (getClipping(pos.getX() - 1, pos.getY() - 1, pos.getZ(), privateArea) & 0x128010e) != 0;
}
public static boolean canProjectileAttack(Mobile attacker, Location from, Location to) {
Location a = from;
Location b = to;
if (a.getX() > b.getX()) {
a = to;
b = from;
}
return canProjectileAttack(a, b, attacker.size(), attacker.getPrivateArea());
}
public static boolean canProjectileAttack(Mobile from, Mobile to) {
Location a = from.getLocation();
Location b = to.getLocation();
if (from.isPlayer() && to.isPlayer()) {
// If both participants are players, line of sight is calculated from the player on the western side.
if (a.getX() > b.getX()) {
a = to.getLocation();
b = from.getLocation();
}
} else if (to.isPlayer()) {
a = to.getLocation();
b = from.getLocation();
}
return canProjectileAttack(a, b, from.size(), from.getPrivateArea());
}
public static boolean canProjectileAttack(Location a, Location b, int size, PrivateArea area) {
return canProjectileMove(a.getX(), a.getY(), b.getX(),
b.getY(), a.getZ(), size, size, area);
}
public static boolean canProjectileMove(int startX, int startY, int endX, int endY, int height, int xLength,
int yLength, PrivateArea privateArea) {
int diffX = endX - startX;
int diffY = endY - startY;
// height %= 4;
int max = Math.max(Math.abs(diffX), Math.abs(diffY));
for (int ii = 0; ii < max; ii++) {
int currentX = endX - diffX;
int currentY = endY - diffY;
for (int i = 0; i < xLength; i++) {
for (int i2 = 0; i2 < yLength; i2++) {
if (diffX < 0 && diffY < 0) {
if ((getClipping(currentX + i - 1, currentY + i2 - 1, height, privateArea) & (UNLOADED_TILE
| /* BLOCKED_TILE | */UNKNOWN | PROJECTILE_TILE_BLOCKED | PROJECTILE_EAST_BLOCKED
| PROJECTILE_NORTH_EAST_BLOCKED | PROJECTILE_NORTH_BLOCKED)) != 0
|| (getClipping(currentX + i - 1, currentY + i2, height, privateArea)
& (UNLOADED_TILE | /* BLOCKED_TILE | */UNKNOWN | PROJECTILE_TILE_BLOCKED
| PROJECTILE_EAST_BLOCKED)) != 0
|| (getClipping(currentX + i, currentY + i2 - 1, height, privateArea)
& (UNLOADED_TILE | /* BLOCKED_TILE | */UNKNOWN | PROJECTILE_TILE_BLOCKED
| PROJECTILE_NORTH_BLOCKED)) != 0) {
return false;
}
} else if (diffX > 0 && diffY > 0) {
if ((getClipping(currentX + i + 1, currentY + i2 + 1, height, privateArea) & (UNLOADED_TILE
| /* BLOCKED_TILE | */UNKNOWN | PROJECTILE_TILE_BLOCKED | PROJECTILE_WEST_BLOCKED
| PROJECTILE_SOUTH_WEST_BLOCKED | PROJECTILE_SOUTH_BLOCKED)) != 0
|| (getClipping(currentX + i + 1, currentY + i2, height, privateArea)
& (UNLOADED_TILE | /* BLOCKED_TILE | */UNKNOWN | PROJECTILE_TILE_BLOCKED
| PROJECTILE_WEST_BLOCKED)) != 0
|| (getClipping(currentX + i, currentY + i2 + 1, height, privateArea)
& (UNLOADED_TILE | /* BLOCKED_TILE | */UNKNOWN | PROJECTILE_TILE_BLOCKED
| PROJECTILE_SOUTH_BLOCKED)) != 0) {
return false;
}
} else if (diffX < 0 && diffY > 0) {
if ((getClipping(currentX + i - 1, currentY + i2 + 1, height, privateArea) & (UNLOADED_TILE
| /* BLOCKED_TILE | */UNKNOWN | PROJECTILE_TILE_BLOCKED | PROJECTILE_SOUTH_BLOCKED
| PROJECTILE_SOUTH_EAST_BLOCKED | PROJECTILE_EAST_BLOCKED)) != 0
|| (getClipping(currentX + i - 1, currentY + i2, height, privateArea)
& (UNLOADED_TILE | /* BLOCKED_TILE | */UNKNOWN | PROJECTILE_TILE_BLOCKED
| PROJECTILE_EAST_BLOCKED)) != 0
|| (getClipping(currentX + i, currentY + i2 + 1, height, privateArea)
& (UNLOADED_TILE | /* BLOCKED_TILE | */UNKNOWN | PROJECTILE_TILE_BLOCKED
| PROJECTILE_SOUTH_BLOCKED)) != 0) {
return false;
}
} else if (diffX > 0 && diffY < 0) {
if ((getClipping(currentX + i + 1, currentY + i2 - 1, height, privateArea) & (UNLOADED_TILE
| /* BLOCKED_TILE | */UNKNOWN | PROJECTILE_TILE_BLOCKED | PROJECTILE_WEST_BLOCKED
| PROJECTILE_NORTH_BLOCKED | PROJECTILE_NORTH_WEST_BLOCKED)) != 0
|| (getClipping(currentX + i + 1, currentY + i2, height, privateArea)
& (UNLOADED_TILE | /* BLOCKED_TILE | */UNKNOWN | PROJECTILE_TILE_BLOCKED
| PROJECTILE_WEST_BLOCKED)) != 0
|| (getClipping(currentX + i, currentY + i2 - 1, height, privateArea)
& (UNLOADED_TILE | /* BLOCKED_TILE | */UNKNOWN | PROJECTILE_TILE_BLOCKED
| PROJECTILE_NORTH_BLOCKED)) != 0) {
return false;
}
} else if (diffX > 0 && diffY == 0) {
if ((getClipping(currentX + i + 1, currentY + i2, height, privateArea)
& (UNLOADED_TILE | /* BLOCKED_TILE | */UNKNOWN | PROJECTILE_TILE_BLOCKED
| PROJECTILE_WEST_BLOCKED)) != 0) {
return false;
}
} else if (diffX < 0 && diffY == 0) {
if ((getClipping(currentX + i - 1, currentY + i2, height, privateArea)
& (UNLOADED_TILE | /* BLOCKED_TILE | */UNKNOWN | PROJECTILE_TILE_BLOCKED
| PROJECTILE_EAST_BLOCKED)) != 0) {
return false;
}
} else if (diffX == 0 && diffY > 0) {
if ((getClipping(currentX + i, currentY + i2 + 1, height, privateArea) & (UNLOADED_TILE
| /*
* BLOCKED_TILE |
*/UNKNOWN | PROJECTILE_TILE_BLOCKED | PROJECTILE_SOUTH_BLOCKED)) != 0) {
return false;
}
} else if (diffX == 0 && diffY < 0) {
if ((getClipping(currentX + i, currentY + i2 - 1, height, privateArea) & (UNLOADED_TILE
| /*
* BLOCKED_TILE |
*/UNKNOWN | PROJECTILE_TILE_BLOCKED | PROJECTILE_NORTH_BLOCKED)) != 0) {
return false;
}
}
}
}
if (diffX < 0) {
diffX++;
} else if (diffX > 0) {
diffX--;
}
if (diffY < 0) {
diffY++; // change
} else if (diffY > 0) {
diffY--;
}
}
return true;
}
public static boolean canMove(int startX, int startY, int endX, int endY, int height, int xLength, int yLength, PrivateArea privateArea) {
int diffX = endX - startX;
int diffY = endY - startY;
int max = Math.max(Math.abs(diffX), Math.abs(diffY));
for (int ii = 0; ii < max; ii++) {
int currentX = endX - diffX;
int currentY = endY - diffY;
for (int i = 0; i < xLength; i++) {
for (int i2 = 0; i2 < yLength; i2++)
if (diffX < 0 && diffY < 0) {
if ((getClipping((currentX + i) - 1, (currentY + i2) - 1, height, privateArea) & 0x128010e) != 0
|| (getClipping((currentX + i) - 1, currentY + i2, height, privateArea) & 0x1280108) != 0
|| (getClipping(currentX + i, (currentY + i2) - 1, height, privateArea) & 0x1280102) != 0)
return false;
} else if (diffX > 0 && diffY > 0) {
if ((getClipping(currentX + i + 1, currentY + i2 + 1, height, privateArea) & 0x12801e0) != 0
|| (getClipping(currentX + i + 1, currentY + i2, height, privateArea) & 0x1280180) != 0
|| (getClipping(currentX + i, currentY + i2 + 1, height, privateArea) & 0x1280120) != 0)
return false;
} else if (diffX < 0 && diffY > 0) {
if ((getClipping((currentX + i) - 1, currentY + i2 + 1, height, privateArea) & 0x1280138) != 0
|| (getClipping((currentX + i) - 1, currentY + i2, height, privateArea) & 0x1280108) != 0
|| (getClipping(currentX + i, currentY + i2 + 1, height, privateArea) & 0x1280120) != 0)
return false;
} else if (diffX > 0 && diffY < 0) {
if ((getClipping(currentX + i + 1, (currentY + i2) - 1, height, privateArea) & 0x1280183) != 0
|| (getClipping(currentX + i + 1, currentY + i2, height, privateArea) & 0x1280180) != 0
|| (getClipping(currentX + i, (currentY + i2) - 1, height, privateArea) & 0x1280102) != 0)
return false;
} else if (diffX > 0 && diffY == 0) {
if ((getClipping(currentX + i + 1, currentY + i2, height, privateArea) & 0x1280180) != 0)
return false;
} else if (diffX < 0 && diffY == 0) {
if ((getClipping((currentX + i) - 1, currentY + i2, height, privateArea) & 0x1280108) != 0)
return false;
} else if (diffX == 0 && diffY > 0) {
if ((getClipping(currentX + i, currentY + i2 + 1, height, privateArea) & 0x1280120) != 0)
return false;
} else if (diffX == 0 && diffY < 0
&& (getClipping(currentX + i, (currentY + i2) - 1, height, privateArea) & 0x1280102) != 0)
return false;
}
if (diffX < 0)
diffX++;
else if (diffX > 0)
diffX--;
if (diffY < 0)
diffY++;
else if (diffY > 0)
diffY--;
}
return true;
}
public static boolean canMove(Location start, Location end, int xLength, int yLength, PrivateArea privateArea) {
return canMove(start.getX(), start.getY(), end.getX(), end.getY(), start.getZ(), xLength, yLength, privateArea);
}
public static boolean canMove(Location position, Direction direction, int size, PrivateArea privateArea) {
Location end = position.transform(direction.getX(), direction.getY());
return canMove(position.getX(), position.getY(), end.getX(), end.getY(), position.getZ(), size, size, privateArea);
}
/**
* Attemps to load the map files related to this region...
*/
public static void loadMapFiles(int x, int y) {
try {
int regionX = x >> 3;
int regionY = y >> 3;
int regionId = ((regionX / 8) << 8) + (regionY / 8);
Optional<Region> r = getRegion(regionId);
if (!r.isPresent()) {
return;
}
if (r.get().isLoaded()) {
return;
}
r.get().setLoaded(true);
// Attempt to create streams..
byte[] oFileData = CompressionUtil.gunzip(
FileUtil.readFile(GameConstants.CLIPPING_DIRECTORY + "maps/" + r.get().getObjectFile() + ".dat"));
byte[] gFileData = CompressionUtil.gunzip(
FileUtil.readFile(GameConstants.CLIPPING_DIRECTORY + "maps/" + r.get().getTerrainFile() + ".dat"));
// Don't allow ground file to be invalid..
if (gFileData == null) {
return;
}
// Read values using our streams..
Buffer groundStream = new Buffer(gFileData);
int absX = (r.get().getRegionId() >> 8) * 64;
int absY = (r.get().getRegionId() & 0xff) * 64;
byte[][][] heightMap = new byte[4][64][64];
for (int z = 0; z < 4; z++) {
for (int tileX = 0; tileX < 64; tileX++) {
for (int tileY = 0; tileY < 64; tileY++) {
while (true) {
int tileType = groundStream.readUnsignedByte();
if (tileType == 0) {
break;
} else if (tileType == 1) {
groundStream.readUnsignedByte();
break;
} else if (tileType <= 49) {
groundStream.readUnsignedByte();
} else if (tileType <= 81) {
heightMap[z][tileX][tileY] = (byte) (tileType - 49);
}
}
}
}
}
for (int i = 0; i < 4; i++) {
for (int i2 = 0; i2 < 64; i2++) {
for (int i3 = 0; i3 < 64; i3++) {
if ((heightMap[i][i2][i3] & 1) == 1) {
int height = i;
if ((heightMap[1][i2][i3] & 2) == 2) {
height--;
}
if (height >= 0 && height <= 3) {
RegionManager.addClipping(absX + i2, absY + i3, height, 0x200000, null);
}
}
}
}
}
if (oFileData != null) {
Buffer objectStream = new Buffer(oFileData);
int objectId = -1;
int incr;
while ((incr = objectStream.getUSmart()) != 0) {
objectId += incr;
int location = 0;
int incr2;
while ((incr2 = objectStream.getUSmart()) != 0) {
location += incr2 - 1;
int localX = (location >> 6 & 0x3f);
int localY = (location & 0x3f);
int height = location >> 12;
int hash = objectStream.readUnsignedByte();
int type = hash >> 2;
int direction = hash & 0x3;
if (localX < 0 || localX >= 64 || localY < 0 || localY >= 64) {
continue;
}
if ((heightMap[1][localX][localY] & 2) == 2) {
height--;
}
// Add object..
if (height >= 0 && height <= 3) {
RegionManager.addObject(objectId, absX + localX, absY + localY, height, type, direction); // Add
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}