-
Notifications
You must be signed in to change notification settings - Fork 0
/
genGray.js
139 lines (114 loc) · 3.28 KB
/
genGray.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
/*
author:Twinkling
version:0.1.0
*/
importPackage(Packages.java.lang);
importPackage(Packages.javax.imageio);
importPackage(Packages.com.sk89q.worldedit);
importPackage(Packages.com.sk89q.worldedit.regions);
importPackage(Packages.com.sk89q.worldedit.blocks);
var usage = "genGray [\"image_path\"] [--flags]\n";
usage += "flags :\n";
usage += " n : only natural block is considered as top block.\n";
context.checkArgs(0, 2, usage);
var defaultImagePath = "terrain_output.png";
var localSession = context.getSession();
var world = player.getWorld();
var region = localSession.getRegionSelector(world).getRegion();
if(!(region instanceof AbstractRegion)){
throw "improper region.\n";
}
var editSession = context.remember();
var mask = localSession.getMask();
if(mask != null){
editSession.setMask(mask);
}
var regionHelper = {
_regionPoly2D : null,
_ySize : 0,
_ox : 0,
_oy : 0,
_oz : 0,
init : function(region){
this._regionPoly2D = new Polygonal2DRegion();
this._ySize = region.getHeight();
var origin = region.getMinimumPoint();
this._ox = origin.getX();
this._oy = origin.getY();
this._oz = origin.getZ();
var ox = this._ox;
var oz = this._oz;
var regionPoly2D = this._regionPoly2D;
var points = region.polygonize(-1);
points.forEach(function(point){
regionPoly2D.addPoint(point.add(-ox,-oz));
});
},
getXSize : function(){
return this._regionPoly2D.getWidth();
},
getYSize : function(){
return this._ySize;
},
getZSize : function(){
return this._regionPoly2D.getLength();
},
getHighestTerrainBlock : function(x, z, naturalOnly){
var minY = this._oy - 1;
var maxY = this._oy + this._ySize - 1;
var y = editSession.getHighestTerrainBlock(this._ox + x, this._oz + z, minY, maxY, naturalOnly);
return y - this._oy;
},
getPoly2D : function(){
return this._regionPoly2D;
}
};
var naturalOnly = false;
var argvOffset = 1;
var otherArgvsLen = argv.length - 1;
if(otherArgvsLen > 0){
if(String(argv[argvOffset]).equals("?")){
player.print("usage : " + usage);
throw "This is a help.\n";
}
}
if(otherArgvsLen > 0){
var path = String(argv[argvOffset]);
if(path.charAt(0).equals("\"")){
path = path.slice(1,-1);
var file = new File(path);
argvOffset ++;
otherArgvsLen --;
}else{
var file = new File(defaultImagePath);
}
}
if(otherArgvsLen > 0){
var flags = String(argv[argvOffset]);
if(flags.substring(0,2).equals("--")){
if(flags.indexOf("n") != -1){
naturalOnly = true;
}
argvOffset ++;
otherArgvsLen --;
}
}
regionHelper.init(region);
var regionXSize = regionHelper.getXSize();
var regionZSize = regionHelper.getZSize();
var bufferedImg = new BufferedImage(regionXSize, regionZSize, BufferedImage.TYPE_INT_ARGB);
for(var x = 0 ; x < regionXSize ; x++){
for(var z = 0 ; z < regionZSize ; z++){
bufferedImg.setRGB(x, z, 0x0);
}
}
var regionPoly2D = regionHelper.getPoly2D();
regionPoly2D.forEach(function(point){
var x = point.getX();
var z = point.getZ();
var y = regionHelper.getHighestTerrainBlock(x, z, naturalOnly) + 1;
var argb = 0xFF << 24 | y << 16 | y << 8 | y;
bufferedImg.setRGB(x, z, argb);
});
ImageIO.write(bufferedImg, "png", file);
player.print("gray has been generated\n");