-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfab.js
2619 lines (2272 loc) · 105 KB
/
fab.js
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
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// $Id$
/***** FAB.JS v1.0
* City block generator CraftScript for WorldEdit
* (parts of this is based on WorldEdit's sample Maze generator)
* Copyright (C) 2011 echurch <http://www.virtualchurchill.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
importPackage(Packages.java.io);
importPackage(Packages.java.awt);
importPackage(Packages.com.sk89q.worldedit);
importPackage(Packages.com.sk89q.worldedit.blocks);
importPackage(Packages.com.sk89q.worldedit.regions);
importPackage(Packages.com.sk89q.worldedit.util);
// for future reference
var editsess = context.remember();
var world = editsess.getWorld();
var session = context.getSession();
var origin = player.getBlockIn();
var rand = new java.util.Random();
var originOffsetX = 0;
var originOffsetZ = 0;
// stats
var blocksTotal = 0;
var blocksSet = 0;
// what do you want to create
var helpString = "[HIGHRISE | MIDRISE | LOWRISE | TOWN | PARK | DIRTLOT | PARKINGLOT | JUSTSTREETS | FARM | FARMHOUSE | HOUSES] [HELP] [RANDOM] [FIRSTTIME] [NOUNDO]"
context.checkArgs(0, -1, helpString);
var createMode = { "HIGHRISE": 0, "MIDRISE": 1, "LOWRISE": 2, "TOWN": 3,
"PARK": 4, "DIRTLOT": 5, "PARKINGLOT": 6, "JUSTSTREETS": 7,
"FARM": 8, "FARMHOUSE": 9, "HOUSES": 10, "HELP": -1
};
// got to default with something
var modeCreate = createMode.MIDRISE;
var floorRatio = 0.66;
var offsetX = 0;
var offsetZ = 0;
var randSeed = false;
var firstTime = false;
var noundo = false;
// parse those args! last create mode wins!
for (var i = 1; i < argv.length; i++) {
var arg = argv[i];
// look for longest params first, that way FARMHOUSE can be found instead of FARM
if (/JUSTSTREET/i.test(arg)) // also JUSTSTREETS
modeCreate = createMode.JUSTSTREETS;
else if (/PARKING/i.test(arg)) // also PARKINGLOT
modeCreate = createMode.PARKINGLOT
else if (/FARMHOUSE/i.test(arg))
modeCreate = createMode.FARMHOUSE
else if (/HIGHRISE/i.test(arg))
modeCreate = createMode.HIGHRISE
else if (/MIDRISE/i.test(arg))
modeCreate = createMode.MIDRISE
else if (/LOWRISE/i.test(arg))
modeCreate = createMode.LOWRISE
else if (/HOUSE/i.test(arg)) // also HOUSES
modeCreate = createMode.HOUSES
else if (/DIRT/i.test(arg)) // also DIRTLOT
modeCreate = createMode.DIRTLOT
else if (/FARM/i.test(arg))
modeCreate = createMode.FARM
else if (/TOWN/i.test(arg))
modeCreate = createMode.TOWN
else if (/PARK/i.test(arg))
modeCreate = createMode.PARK
else if (/RANDOM/i.test(arg))
randSeed = true
else if (/FIRSTTIME/i.test(arg))
firstTime = true
else if (/NOUN/i.test(arg))
noundo = true
else if (/HELP/i.test(arg))
modeCreate = createMode.HELP;
else if (/\?/.test(arg))
modeCreate = createMode.HELP;
}
//var config = context.getConfiguration;
//context.print(config);
//context.print(config.scriptTimeout);
// some constants
var squareBlocks = 15;
var cornerWidth = squareBlocks / 3;
var lotBlocks = ((squareBlocks * 3) - 1) / 2;
var linesOffset = Math.floor(squareBlocks / 2);
var squaresWidth = 5;
var squaresLength = 5;
var plumbingHeight = 5;
var sewerHeight = 4;
var streetHeight = 2;
var floorHeight = 4;
var roofHeight = 1; // extra bit for roof fluff like railings
// say hello!
context.print(argv[0] + " v1.0a");
// see if we can find the offset
if (!FindOriginOffset()) {
// we can't, go anyway?
if (firstTime) {
originOffsetX = -linesOffset;
originOffsetZ = -linesOffset;
// if not tell the user what happened
} else {
context.print("ERROR: Cannot find the crossroads.");
context.print(" Either walk to a crossroad or...")
context.print(" use FIRSTTIME option to force creation from this spot.");
modeCreate = createMode.HELP;
}
} else
firstTime = true;
// show help
if (modeCreate == createMode.HELP)
context.print("USAGE: " + argv[0] + " " + helpString);
// check the timeout
//else if (context.getConfiguration.scriptTimeout < 10000)
// context.print("ERROR: You need to increase your script timeout, see readme")
// otherwise let's do something!
else {
// stair direction
var Ascending_South = 0;
var Ascending_North = 1;
var Ascending_West = 2;
var Ascending_East = 3;
// extended BlockID types
var ExtendedID = {
"NORTHWARD_LADDER": EncodeBlock(BlockID.LADDER, 4),
"EASTWARD_LADDER": EncodeBlock(BlockID.LADDER, 2),
"SOUTHWARD_LADDER": EncodeBlock(BlockID.LADDER, 5),
"WESTWARD_LADDER": EncodeBlock(BlockID.LADDER, 3),
"NORTHFACING_WOODEN_DOOR": EncodeBlock(BlockID.WOODEN_DOOR, 0),
"EASTFACING_WOODEN_DOOR": EncodeBlock(BlockID.WOODEN_DOOR, 1),
"SOUTHFACING_WOODEN_DOOR": EncodeBlock(BlockID.WOODEN_DOOR, 2),
"WESTFACING_WOODEN_DOOR": EncodeBlock(BlockID.WOODEN_DOOR, 3),
"NORTHFACING_REVERSED_WOODEN_DOOR": EncodeBlock(BlockID.WOODEN_DOOR, 3 + 4),
"EASTFACING_REVERSED_WOODEN_DOOR": EncodeBlock(BlockID.WOODEN_DOOR, 0 + 4),
"SOUTHFACING_REVERSED_WOODEN_DOOR": EncodeBlock(BlockID.WOODEN_DOOR, 1 + 4),
"WESTFACING_REVERSED_WOODEN_DOOR": EncodeBlock(BlockID.WOODEN_DOOR, 2 + 4),
"NORTHFACING_TRAP_DOOR": EncodeBlock(BlockID.TRAP_DOOR, 2),
"EASTFACING_TRAP_DOOR": EncodeBlock(BlockID.TRAP_DOOR, 0),
"SOUTHFACING_TRAP_DOOR": EncodeBlock(BlockID.TRAP_DOOR, 3),
"WESTFACING_TRAP_DOOR": EncodeBlock(BlockID.TRAP_DOOR, 1),
"WHITE_CLOTH": EncodeBlock(BlockID.CLOTH, 0),
"ORANGE_CLOTH": EncodeBlock(BlockID.CLOTH, 1),
"MAGENTA_CLOTH": EncodeBlock(BlockID.CLOTH, 2),
"LIGHT_BLUE_CLOTH": EncodeBlock(BlockID.CLOTH, 3),
"YELLOW_CLOTH": EncodeBlock(BlockID.CLOTH, 4),
"LIGHT_GREEN_CLOTH": EncodeBlock(BlockID.CLOTH, 5),
"PINK_CLOTH": EncodeBlock(BlockID.CLOTH, 6),
"GRAY_CLOTH": EncodeBlock(BlockID.CLOTH, 7),
"LIGHT_GRAY_CLOTH": EncodeBlock(BlockID.CLOTH, 8),
"CYAN_CLOTH": EncodeBlock(BlockID.CLOTH, 9),
"PURPLE_CLOTH": EncodeBlock(BlockID.CLOTH, 10),
"BLUE_CLOTH": EncodeBlock(BlockID.CLOTH, 11),
"BROWN_CLOTH": EncodeBlock(BlockID.CLOTH, 12),
"DARK_GREEN_CLOTH": EncodeBlock(BlockID.CLOTH, 13),
"RED_CLOTH": EncodeBlock(BlockID.CLOTH, 14),
"BLACK_CLOTH": EncodeBlock(BlockID.CLOTH, 15),
"STONE_STEP": EncodeBlock(BlockID.STEP, 0),
"SANDSTONE_STEP": EncodeBlock(BlockID.STEP, 1),
"WOOD_STEP": EncodeBlock(BlockID.STEP, 2),
"COBBLESTONE_STEP": EncodeBlock(BlockID.STEP, 3),
"STONE_DOUBLESTEP": EncodeBlock(BlockID.DOUBLE_STEP, 0),
"SANDSTONE_DOUBLESTEP": EncodeBlock(BlockID.DOUBLE_STEP, 1),
"WOOD_DOUBLESTEP": EncodeBlock(BlockID.DOUBLE_STEP, 2),
"COBBLESTONE_DOUBLESTEP": EncodeBlock(BlockID.DOUBLE_STEP, 3),
"SOUTHASCENDING_WOODEN_STAIRS": EncodeBlock(BlockID.WOODEN_STAIRS, Ascending_South),
"NORTHASCENDING_WOODEN_STAIRS": EncodeBlock(BlockID.WOODEN_STAIRS, Ascending_North),
"WESTASCENDING_WOODEN_STAIRS": EncodeBlock(BlockID.WOODEN_STAIRS, Ascending_West),
"EASTASCENDING_WOODEN_STAIRS": EncodeBlock(BlockID.WOODEN_STAIRS, Ascending_East),
"SOUTHASCENDING_COBBLESTONE_STAIRS": EncodeBlock(BlockID.COBBLESTONE_STAIRS, Ascending_South),
"NORTHASCENDING_COBBLESTONE_STAIRS": EncodeBlock(BlockID.COBBLESTONE_STAIRS, Ascending_North),
"WESTASCENDING_COBBLESTONE_STAIRS": EncodeBlock(BlockID.COBBLESTONE_STAIRS, Ascending_West),
"EASTASCENDING_COBBLESTONE_STAIRS": EncodeBlock(BlockID.COBBLESTONE_STAIRS, Ascending_East)
};
// calculate the floor height ratio
var floorRatio = 0.66;
switch (modeCreate) {
case createMode.HIGHRISE:
floorRatio = 1.00;
break;
case createMode.MIDRISE:
floorRatio = 0.66;
break;
case createMode.LOWRISE:
floorRatio = 0.33;
break;
case createMode.TOWN:
floorRatio = 0.25;
break;
default:
break;
}
// derived pseudo-constants
var skyHigh = Math.floor(127 - origin.y);
var belowGround = plumbingHeight + sewerHeight + streetHeight - 1;
var cornerBlocks = squareBlocks / 3;
var sewerFloor = plumbingHeight;
var sewerCeiling = sewerFloor + sewerHeight - 1;
var streetLevel = sewerCeiling + streetHeight - 1;
var floorOverhead = belowGround + streetHeight + roofHeight;
var floorCount = Math.floor(((127 - origin.y) * floorRatio - roofHeight) / floorHeight);
if (floorCount < 1)
floorCount = 1;
// how large of an area are we building?
var arrayWidth = squaresWidth * squareBlocks;
var arrayDepth = squaresLength * squareBlocks;
var arrayHeight = floorOverhead + skyHigh;
// what direction are we facing?
var yaw = FindYaw();
if (yaw >= 0 && yaw < 90) {
offsetX = -1;
offsetZ = -1;
} else if (yaw >= 90 && yaw < 180) {
offsetX = 0;
offsetZ = -1;
} else if (yaw >= 180 && yaw < 270) {
offsetX = 0;
offsetZ = 0;
} else {
offsetX = -1;
offsetZ = 0;
}
// move the offset if needed
origin = origin.add(offsetX * (squaresWidth - 1) * squareBlocks,
0,
offsetZ * (squaresLength - 1) * squareBlocks);
// offset the start so we are standing in the middle of the road near the manhole
origin = origin.add(originOffsetX, -belowGround, originOffsetZ);
// is random based on where we are?
if (!randSeed) {
var blockSeed = Math.floor(origin.x);
blockSeed = blockSeed * 65536 + Math.floor(origin.y);
blockSeed = blockSeed * 65536 + Math.floor(origin.z);
rand.setSeed(blockSeed);
}
// making room to create
var blocks = new Array(arrayWidth);
InitializeBlocks();
InitializeFixups();
InitializeTrees();
// add plumbing level (based on Maze.js from WorldEdit)
AddPlumbingLevel();
// add streets
AddStreets();
// add the inside bits
switch (modeCreate) {
case createMode.HIGHRISE:
case createMode.MIDRISE:
case createMode.LOWRISE:
case createMode.TOWN:
AddCitySquares();
break;
case createMode.PARK:
AddParkLot();
break;
case createMode.FARMHOUSE:
case createMode.FARM:
case createMode.HOUSES:
AddFarmAndHousesLot();
break;
case createMode.DIRTLOT:
AddDirtLot();
break;
case createMode.PARKINGLOT:
AddParkingLot();
break;
default: // createMode.JUSTSTEETS
AddJustStreets();
break;
}
// add access points (will modify the player "origin" correctly as well)
AddManholes();
// and we are nearly done
TranscribeBlocks();
// finally fix the things that need to be fixed up
FinalizeColumns();
FinalizeTrees();
FinalizeFixups();
// clean up any left over detritus
world.removeEntities(EntityType.ITEMS,
origin.add(Math.floor(2.5 * squareBlocks), 0, Math.floor(2.5 * squareBlocks)),
4 * squareBlocks);
// poof, we are done!
//context.print("fini " + blocksSet + " of " + blocksTotal + " blocks placed");
context.print("fini");
}
////////////////////////////////////////////////////
// all the supporting bits
////////////////////////////////////////////////////
function EncodeBlock(type, data) {
//context.print(type + " " + data);
return (data << 8) | type;
}
function DecodeID(block) {
return block & 0xFF;
}
function DecodeData(block) {
return block >> 8;
}
function InitializeBlocks() {
//context.print(arrayWidth + " " + arrayHeight + " " + arrayDepth);
context.print("Initializing");
for (var x = 0; x < arrayWidth; x++) {
blocks[x] = new Array(arrayHeight);
for (var y = 0; y < arrayHeight; y++) {
blocks[x][y] = new Array(arrayDepth);
for (var z = 0; z < arrayDepth; z++)
blocks[x][y][z] = BlockID.AIR;
}
}
}
// etch our array of ints into the "real" world
function TranscribeBlocks() {
context.print("Transcribing");
var newblock;
for (x = 0; x < arrayWidth; x++) {
for (var y = 0; y < arrayHeight; y++) {
for (var z = 0; z < arrayDepth; z++) {
// decode the new block
newblock = blocks[x][y][z];
SetBlockIfNeeded(origin.add(x, y, z),
DecodeID(newblock), DecodeData(newblock), true);
}
}
}
}
function SetBlockIfNeeded(at, blockID, blockData, force) {
var oldBlock = editsess.rawGetBlock(at);
var oldID = oldBlock.getType();
blocksTotal++;
// if it isn't the same then set it!
if (oldID != blockID || oldBlock.getData() != blockData) {
// do force our way or only do so if there is air there?
if (force || oldID == BlockID.AIR) {
if (noundo)
editsess.rawSetBlock(at, new BaseBlock(blockID, blockData));
else
editsess.setBlock(at, new BaseBlock(blockID, blockData));
blocksSet++;
}
}
}
function FindYaw() {
var yaw = (player.getYaw() - 90) % 360;
if (yaw < 0)
yaw += 360;
return yaw;
}
function FindOriginOffset() {
var deltaBad = 999;
var deltaKeepLooking = 0;
var deltaNorth = deltaKeepLooking;
var deltaSouth = deltaKeepLooking;
var deltaEast = deltaKeepLooking;
var deltaWest = deltaKeepLooking;
// look around
for (var a = 0; a < 12; a++) {
deltaNorth = BlockTest(deltaNorth, a, 0, a);
deltaSouth = BlockTest(deltaSouth, -a, 0, a);
deltaEast = BlockTest(deltaEast, 0, a, a);
deltaWest = BlockTest(deltaWest, 0, -a, a);
}
// patch the offsets
if (deltaNorth + deltaSouth == 10 && deltaEast + deltaWest == 10) {
originOffsetX = -(deltaSouth + 2);
originOffsetZ = -(deltaWest + 2);
return true;
} else {
return false;
}
function BlockTest(deltaValue, atX, atZ, value) {
if (deltaValue == deltaKeepLooking) {
switch (editsess.rawGetBlock(origin.add(atX, -1, atZ)).getType()) {
case BlockID.CLOTH:
return value;
case BlockID.STONE:
case BlockID.LADDER:
case BlockID.AIR: // just in case a creeper exploded nearby
return deltaKeepLooking;
default:
return deltaBad;
}
} else
return deltaValue;
}
}
// need to standarize on one param style, these five are not consistent!
function AddWalls(blockID, minX, minY, minZ, maxX, maxY, maxZ) {
for (var x = minX; x <= maxX; x++)
for (var y = minY; y <= maxY; y++) {
blocks[x][y][minZ] = blockID;
blocks[x][y][maxZ] = blockID;
}
for (var y = minY; y <= maxY; y++)
for (var z = minZ; z <= maxZ; z++) {
blocks[minX][y][z] = blockID;
blocks[maxX][y][z] = blockID;
}
}
function FillCube(blockID, minX, minY, minZ, maxX, maxY, maxZ) {
for (var x = minX; x <= maxX; x++)
for (var y = minY; y <= maxY; y++)
for (var z = minZ; z <= maxZ; z++)
blocks[x][y][z] = blockID;
}
function FillAtLayer(blockID, blockX, blockZ, atY, layerW, layerL) {
FillCube(blockID, blockX, atY, blockZ,
blockX + layerW - 1, atY, blockZ + layerL - 1);
}
function FillCellLayer(blockID, blockX, blockZ, atY, cellW, cellL) {
FillCube(blockID, blockX, atY, blockZ,
blockX + cellW * squareBlocks - 1, atY, blockZ + cellL * squareBlocks - 1);
}
function FillStrataLayer(blockID, at) {
FillAtLayer(blockID, 0, 0, at, arrayWidth, arrayDepth);
}
////////////////////////////////////////////////////
// random instances of random
////////////////////////////////////////////////////
function OneInTwoChance() { return rand.nextInt(2) == 0 }
function OneInThreeChance() { return rand.nextInt(3) == 0 }
function OneInFourChance() { return rand.nextInt(4) == 0 }
function OneInNChance(N) { return rand.nextInt(N) == 0 }
////////////////////////////////////////////////////
// fix up logic for blocks
////////////////////////////////////////////////////
var fixups;
function InitializeFixups() {
fixups = new Array();
}
function FinalizeFixups() {
if (fixups.length > 0) {
context.print("Placing special items");
for (i = 0; i < fixups.length; i++)
fixups[i].setBlock(origin);
}
}
function SetLateBlock(atX, atY, atZ, id) {
SetLateBlockEx(atX, atY, atZ, id, false);
}
function SetLateGroundBlock(atX, atY, atZ, id) {
SetLateBlockEx(atX, atY, atZ, id, true);
}
function SetLateBlockEx(atX, atY, atZ, id, groundCheck) {
// make sure there is room
blocks[atX][atY][atZ] = BlockID.AIR;
// handle any "more than one cube" items
if (DecodeID(id) == BlockID.WOODEN_DOOR)
blocks[atX][atY + 1][atZ] = BlockID.AIR;
// keep a note of things to go
fixups.push(new LateItem(atX, atY, atZ, id, groundCheck));
}
function LateItem(atX, atY, atZ, id, groundCheck) {
this.blockId = id;
this.blockX = atX
this.blockY = atY
this.blockZ = atZ;
this.groundCheck = groundCheck;
this.setBlock = function (origin) {
if (!this.groundCheck || blocks[this.blockX][this.blockY - 1][this.blockZ] != BlockID.AIR) {
var id = DecodeID(this.blockId);
var data = DecodeData(this.blockId);
var at = origin.add(this.blockX, this.blockY, this.blockZ);
SetBlockIfNeeded(at, id, data, false);
if (id == BlockID.WOODEN_DOOR)
SetBlockIfNeeded(at.add(0, 1, 0), id, data + 8, false);
}
}
}
////////////////////////////////////////////////////
// fix up logic for trees
////////////////////////////////////////////////////
var trees;
var archTypeTree;
var archTypeBigTree;
function InitializeTrees() {
trees = new Array();
archTypeTree = new TreeGenerator(TreeGenerator.TreeType.TREE);
archTypeBigTree = new TreeGenerator(TreeGenerator.TreeType.BIG_TREE);
}
function FinalizeTrees() {
if (trees.length > 0) {
context.print("Growing some trees");
for (i = 0; i < trees.length; i++)
trees[i].generate(origin);
}
}
function SetLateForest(blockX, blockZ, sizeW, sizeL) {
var border = 3;
var spacing = 3;
var odds = 10;
for (var x = blockX + border; x <= blockX + sizeW - border; x = x + spacing)
for (var z = blockZ + border; z <= blockZ + sizeL - border; z = z + spacing) {
// odds% of the time plant a tree
if (rand.nextInt(100) < odds)
SetLateTree(x, streetLevel + 1, z, false);
}
}
function SetLateTree(blockX, blockY, blockZ, bigTree) {
trees.push(new Tree(blockX, blockY, blockZ, bigTree));
}
function Tree(blockX, blockY, blockZ, bigTree) {
this.atX = blockX;
this.atY = blockY;
this.atZ = blockZ;
this.big = bigTree;
this.generate = function (origin) {
if (editsess.rawGetBlock(origin.add(this.atX, this.atY, this.atZ)).getType() == BlockID.GRASS)
if (this.big)
archTypeBigTree.generate(editsess, origin.add(this.atX, this.atY + 1, this.atZ))
else
archTypeTree.generate(editsess, origin.add(this.atX, this.atY + 1, this.atZ));
}
}
////////////////////////////////////////////////////
// fix up logic for support columns
////////////////////////////////////////////////////
function FinalizeColumns() {
context.print("Driving pilings");
var columnOffset = squareBlocks / 3;
var y;
for (var x = columnOffset; x < squareBlocks * 5; x += columnOffset)
for (var z = columnOffset; z < squareBlocks * 5; z += columnOffset) {
// find the lowest airy point
for (y = 0; y < skyHigh; y++)
if (blocks[x][y][z] != BlockID.AIR) {
y--;
break;
}
// grow the column at...
var columnAt = origin.add(x, y, z);
for (y = columnAt.getY(); y >= 0; y--) {
columnAt = columnAt.setY(y);
// time to quit?
type = editsess.rawGetBlock(columnAt).getType();
if (type == BlockID.STONE || type == BlockID.BEDROCK)
break;
// big columns!
SetBlockIfNeeded(columnAt, BlockID.STONE, 0, true);
SetBlockIfNeeded(columnAt.add(1, 0, 0), BlockID.STONE, 0, true);
SetBlockIfNeeded(columnAt.add(-1, 0, 0), BlockID.STONE, 0, true);
SetBlockIfNeeded(columnAt.add(0, 0, 1), BlockID.STONE, 0, true);
SetBlockIfNeeded(columnAt.add(0, 0, -1), BlockID.STONE, 0, true);
}
}
}
2
////////////////////////////////////////////////////
// specific city block construction
////////////////////////////////////////////////////
// borrowed from WorldEdit's Maze script
function AddPlumbingLevel() {
context.print("Plumbing");
// add some strata
FillStrataLayer(BlockID.OBSIDIAN, 0);
// figure out the size
w = Math.floor(arrayWidth / 2);
d = Math.floor(arrayDepth / 2);
var stack = [];
var visited = {};
var noWallLeft = new Array(w * d);
var noWallAbove = new Array(w * d);
var current = 0;
stack.push(id(0, 0))
while (stack.length > 0) {
var cell = stack.pop();
var x = $x(cell), z = $z(cell);
visited[cell] = true;
var neighbors = []
if (x > 0) neighbors.push(id(x - 1, z));
if (x < w - 1) neighbors.push(id(x + 1, z));
if (z > 0) neighbors.push(id(x, z - 1));
if (z < d - 1) neighbors.push(id(x, z + 1));
shuffle(neighbors);
while (neighbors.length > 0) {
var neighbor = neighbors.pop();
var nx = $x(neighbor), nz = $z(neighbor);
if (visited[neighbor] != true) {
stack.push(cell);
if (z == nz) {
if (nx < x) {
noWallLeft[cell] = true;
} else {
noWallLeft[neighbor] = true;
}
} else {
if (nz < z) {
noWallAbove[cell] = true;
} else {
noWallAbove[neighbor] = true;
}
}
stack.push(neighbor);
break;
}
}
}
for (var z = 0; z < d; z++) {
for (var x = 0; x < w; x++) {
var cell = id(x, z);
if (!noWallLeft[cell] && z < d) {
blocks[x * 2 + 1][1][z * 2] = BlockID.OBSIDIAN;
blocks[x * 2 + 1][2][z * 2] = BlockID.OBSIDIAN;
blocks[x * 2 + 1][3][z * 2] = BlockID.OBSIDIAN;
}
if (!noWallAbove[cell] && x < w) {
blocks[x * 2][1][z * 2 + 1] = BlockID.OBSIDIAN;
blocks[x * 2][2][z * 2 + 1] = BlockID.OBSIDIAN;
blocks[x * 2][3][z * 2 + 1] = BlockID.OBSIDIAN;
}
blocks[x * 2 + 1][1][z * 2 + 1] = BlockID.OBSIDIAN;
blocks[x * 2 + 1][2][z * 2 + 1] = BlockID.OBSIDIAN;
blocks[x * 2 + 1][3][z * 2 + 1] = BlockID.OBSIDIAN;
switch (rand.nextInt(20)) {
case 0:
case 1:
if (OneInThreeChance())
blocks[x * 2][1][z * 2] = BlockID.OBSIDIAN
else
blocks[x * 2][1][z * 2] = BlockID.DIRT;
SetLateGroundBlock(x * 2, 2, z * 2, BlockID.BROWN_MUSHROOM);
break;
case 2:
case 3:
if (OneInThreeChance())
blocks[x * 2][1][z * 2] = BlockID.OBSIDIAN
else
blocks[x * 2][1][z * 2] = BlockID.DIRT;
SetLateGroundBlock(x * 2, 1, z * 2, BlockID.RED_MUSHROOM);
break;
case 4:
PlaceItem(x, z, BlockID.GOLD_BLOCK);
break;
case 5:
PlaceItem(x, z, BlockID.DIAMOND_BLOCK);
break;
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
PlaceItem(x, z, BlockID.WATER);
break;
default:
if (OneInFourChance())
blocks[x * 2][3][z * 2] = BlockID.OBSIDIAN
else
blocks[x * 2][1][z * 2] = BlockID.OBSIDIAN
break;
}
}
}
// top off the plumbing
FillStrataLayer(BlockID.CLAY, 4);
//======================================================
function PlaceItem(x, z, id) {
if (OneInFourChance()) {
if (OneInTwoChance()) {
blocks[x * 2][3][z * 2] = BlockID.OBSIDIAN
blocks[x * 2][1][z * 2] = id;
} else {
blocks[x * 2][1][z * 2] = BlockID.OBSIDIAN;
blocks[x * 2][2][z * 2] = id;
}
} else
blocks[x * 2][1][z * 2] = id;
}
function id(x, z) {
return z * (w + 1) + x;
}
function $x(i) {
return i % (w + 1);
}
function $z(i) {
return Math.floor(i / (w + 1));
}
function shuffle(arr) {
var i = arr.length;
if (i == 0) return false;
while (--i) {
var j = rand.nextInt(i + 1);
var tempi = arr[i];
var tempj = arr[j];
arr[i] = tempj;
arr[j] = tempi;
}
}
}
function DrawParkCell(blockX, blockZ, cellX, cellZ, cellW, cellL) {
var cellWidth = cellW * squareBlocks;
var cellLength = cellL * squareBlocks;
// reservoir's walls
FillCellLayer(BlockID.SANDSTONE, blockX, blockZ, 0, cellW, cellL);
AddWalls(BlockID.SANDSTONE, blockX, 1, blockZ,
blockX + cellWidth - 1, streetLevel - 1, blockZ + cellLength - 1);
// fill up reservoir with water
FillCube(BlockID.AIR, blockX + 1, 1, blockZ + 1,
blockX + cellWidth - 2, 3, blockZ + cellLength - 2);
FillCube(BlockID.WATER, blockX + 1, 1, blockZ + 1,
blockX + cellWidth - 2, rand.nextInt(3) + 2, blockZ + cellLength - 2);
// pillars to hold things up
for (var x = cornerWidth; x < cellWidth; x = x + cornerWidth)
for (var z = cornerWidth; z < cellLength; z = z + cornerWidth)
for (var y = 1; y < streetLevel; y++) {
// every other column has a lit base
if (y == 1 && ((x % 2 == 0 && z % 2 == 1) ||
(x % 2 == 1 && z % 2 == 0)))
blocks[blockX + x][y][blockZ + z] = BlockID.LIGHTSTONE;
else
blocks[blockX + x][y][blockZ + z] = BlockID.SANDSTONE;
}
// cap it off
FillCellLayer(BlockID.SANDSTONE, blockX, blockZ, streetLevel, cellW, cellL);
// add some grass
FillCellLayer(BlockID.GRASS, blockX, blockZ, streetLevel + 1, cellW, cellL);
// steps up to access point
blocks[blockX + 7][streetLevel - 6][blockZ + 1] = BlockID.SANDSTONE;
blocks[blockX + 6][streetLevel - 5][blockZ + 1] = BlockID.SANDSTONE;
blocks[blockX + 5][streetLevel - 4][blockZ + 1] = BlockID.SANDSTONE;
// platform for access point
blocks[blockX + 4][streetLevel - 3][blockZ + 1] = BlockID.SANDSTONE;
blocks[blockX + 3][streetLevel - 3][blockZ + 1] = BlockID.SANDSTONE;
blocks[blockX + 2][streetLevel - 3][blockZ + 1] = BlockID.SANDSTONE;
blocks[blockX + 1][streetLevel - 3][blockZ + 1] = BlockID.SANDSTONE;
// backfill the wall behind the ladders
blocks[blockX + 2][streetLevel - 2][blockZ + 1] = BlockID.SANDSTONE;
blocks[blockX + 1][streetLevel - 2][blockZ + 1] = BlockID.SANDSTONE;
blocks[blockX + 2][streetLevel - 1][blockZ + 1] = BlockID.SANDSTONE;
blocks[blockX + 1][streetLevel - 1][blockZ + 1] = BlockID.SANDSTONE;
// and now the ladders and trapdoor
SetLateBlock(blockX + 3, streetLevel + 1, blockZ + 1, ExtendedID.SOUTHWARD_LADDER);
SetLateBlock(blockX + 3, streetLevel, blockZ + 1, ExtendedID.SOUTHWARD_LADDER);
SetLateBlock(blockX + 3, streetLevel - 1, blockZ + 1, ExtendedID.SOUTHWARD_LADDER);
SetLateBlock(blockX + 3, streetLevel - 2, blockZ + 1, ExtendedID.SOUTHWARD_LADDER);
SetLateBlock(blockX + 3, streetLevel + 2, blockZ + 1, ExtendedID.WESTFACING_TRAP_DOOR);
// add some fencing
var fenceHole = 3;
for (var x = 0; x < cellWidth; x++) {
// simple columns
if (x == fenceHole || x == cellWidth - fenceHole - 1) {
blocks[blockX + x][streetLevel + 2][blockZ] = BlockID.SAND;
blocks[blockX + x][streetLevel + 2][blockZ + cellLength - 1] = BlockID.SAND;
blocks[blockX + x][streetLevel + 3][blockZ] = BlockID.SANDSTONE;
blocks[blockX + x][streetLevel + 3][blockZ + cellLength - 1] = BlockID.SANDSTONE;
blocks[blockX + x][streetLevel + 4][blockZ] = ExtendedID.SANDSTONE_STEP;
blocks[blockX + x][streetLevel + 4][blockZ + cellLength - 1] = ExtendedID.SANDSTONE_STEP;
// fence itself
} else if (x > fenceHole && x < cellWidth - fenceHole) {
blocks[blockX + x][streetLevel + 2][blockZ] = BlockID.FENCE;
blocks[blockX + x][streetLevel + 2][blockZ + cellLength - 1] = BlockID.FENCE;
}
}
// another fence along the other axis
for (var z = 0; z < cellLength; z++) {
if (z == fenceHole || z == cellLength - fenceHole - 1) {
blocks[blockX][streetLevel + 2][blockZ + z] = BlockID.SAND;
blocks[blockX + cellWidth - 1][streetLevel + 2][blockZ + z] = BlockID.SAND;
blocks[blockX][streetLevel + 3][blockZ + z] = BlockID.SANDSTONE;
blocks[blockX + cellWidth - 1][streetLevel + 3][blockZ + z] = BlockID.SANDSTONE;
blocks[blockX][streetLevel + 4][blockZ + z] = ExtendedID.SANDSTONE_STEP;
blocks[blockX + cellWidth - 1][streetLevel + 4][blockZ + z] = ExtendedID.SANDSTONE_STEP;
} else if (z > fenceHole && z < cellLength - fenceHole) {
blocks[blockX][streetLevel + 2][blockZ + z] = BlockID.FENCE;
blocks[blockX + cellWidth - 1][streetLevel + 2][blockZ + z] = BlockID.FENCE;
}
}
// little park, give it a big tree
if (cellW == 1 && cellL == 1)
SetLateTree(blockX + Math.floor(cellWidth / 2),
streetLevel + 1,
blockZ + Math.floor(cellLength / 2), true);
else {
// where is the center?
var fountainDiameter = 5;
var fountainX = blockX + Math.floor((cellWidth - fountainDiameter) / 2);
var fountainZ = blockZ + Math.floor((cellLength - fountainDiameter) / 2);
// add a fountain
FillAtLayer(BlockID.GLASS, fountainX + 1, fountainZ + 1, streetLevel, fountainDiameter - 2, fountainDiameter - 2);
AddWalls(BlockID.STONE, fountainX, streetLevel + 1, fountainZ,
fountainX + fountainDiameter - 1, streetLevel + 2, fountainZ + fountainDiameter - 1);
FillAtLayer(BlockID.WATER, fountainX + 1, fountainZ + 1, streetLevel + 1, fountainDiameter - 2, fountainDiameter - 2);
blocks[fountainX + 2][streetLevel + 1][fountainZ + 2] = BlockID.LIGHTSTONE;
blocks[fountainX + 2][streetLevel + 2][fountainZ + 2] = BlockID.GLASS;
blocks[fountainX + 2][streetLevel + 3][fountainZ + 2] = BlockID.GLASS;
blocks[fountainX + 2][streetLevel + 4][fountainZ + 2] = BlockID.GLASS;
blocks[fountainX + 2][streetLevel + 5][fountainZ + 2] = BlockID.WATER;
// add some trees
SetLateForest(blockX, blockZ, cellWidth, cellLength);
}
}
function AddCitySquares() {
// upper limits
var maxSize = 3;
var maxParkSize = 1;
var sizeX = 1;
var sizeZ = 1;
// if we are in a town then change them a bit
if (modeCreate == createMode.TOWN) {
maxSize = 2;
maxParkSize = 2;
} else if (modeCreate == createMode.HIGHRISE) {
maxSize = 2;
}
// initialize the building cells
var cells = new Array(3);
for (var x = 0; x < 3; x++) {
cells[x] = new Array(3);
for (var z = 0; z < 3; z++)
cells[x][z] = false;
}
// does this block have a park?
if (modeCreate == createMode.TOWN ||
modeCreate != createMode.HIGHRISE && OneInTwoChance()) {
sizeX = RandomLotSize(maxParkSize);
sizeZ = RandomLotSize(maxParkSize);
var atX = rand.nextInt(maxSize - sizeX + 1);
var atZ = rand.nextInt(maxSize - sizeZ + 1);
// build the park then!
MarkLotUsed(atX, atZ, sizeX, sizeZ);
DrawParkCell((atX + 1) * squareBlocks, (atZ + 1) * squareBlocks,
atX, atZ, sizeX, sizeZ);
}
// work our way through the cells
for (var atX = 0; atX < 3; atX++) {
for (var atZ = 0; atZ < 3; atZ++) {
if (!cells[atX][atZ]) { // nothing here yet.. build!
sizeX = RandomLotSize(maxSize - atX);
sizeZ = RandomLotSize(maxSize - atZ);
// is there really width for it?
for (var x = atX; x < atX + sizeX; x++) {
if (cells[x][atZ]) {
sizeX = x - atX;
break;
}
}
// is there really depth for it?
for (var x = atX; x < atX + sizeX; x++) {
for (var z = atZ; z < atZ + sizeZ; z++) {