2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
7 import java.awt.image.BufferedImage;
8 import java.awt.image.Raster;
10 import java.io.FileNotFoundException;
11 import java.io.FileOutputStream;
12 import java.io.IOException;
13 import java.io.PrintWriter;
14 import java.util.LinkedList;
15 import java.util.List;
16 import java.util.logging.Level;
17 import java.util.logging.Logger;
18 import javax.imageio.ImageIO;
24 public class MapWriter {
26 public int writeMap(Parameters p) {
27 if (!(new File(p.infile).exists())) {
31 double[][] height = getHeightmap(p.infile);
32 double[][] columns = getColumns(height);
33 double units = 1d * p.pixelsize;
34 double max = p.height;
36 PrintWriter pw = null;
38 pw = new PrintWriter(new FileOutputStream(new File(p.outfile)));
39 } catch (FileNotFoundException ex) {
40 Logger.getLogger(MapWriter.class.getName()).log(Level.SEVERE, null, ex);
46 pw.print("{\n\"classname\" \"worldspawn\"\n");
48 // wander through grid
49 for (int x = 0; x < height.length - 1; ++x) {
50 for (int y = 0; y < height[0].length - 1; ++y) {
52 boolean skip = getMinMaxForRegion(height, x, y, 2)[0] < 0;
62 * c +-------+ d + f (e occluded, unused)
70 Vector3D a = new Vector3D(x * units, -y * units, height[x][y] * max);
71 Vector3D b = new Vector3D((x + 1) * units, -y * units, height[x + 1][y] * max);
72 Vector3D c = new Vector3D(x * units, -(y + 1) * units, height[x][y + 1] * max);
73 Vector3D d = new Vector3D((x + 1) * units, -(y + 1) * units, height[x + 1][y + 1] * max);
74 //Vector3D e = new Vector3D(x * units, -y * units, -16.0);
75 Vector3D f = new Vector3D((x + 1) * units, -y * units, -16.0);
76 Vector3D g = new Vector3D(x * units, -(y + 1) * units, -16.0);
77 Vector3D h = new Vector3D((x + 1) * units, -(y + 1) * units, -16.0);
80 pw.print(getMapPlaneString(a, b, d, p.detail, p.texture, p.texturescale));
81 pw.print(getMapPlaneString(d, b, f, p.detail, "common/caulk", p.texturescale));
82 pw.print(getMapPlaneString(f, b, a, p.detail, "common/caulk", p.texturescale));
83 pw.print(getMapPlaneString(a, d, h, p.detail, "common/caulk", p.texturescale));
84 pw.print(getMapPlaneString(g, h, f, p.detail, "common/caulk", p.texturescale));
89 pw.print(getMapPlaneString(d, c, a, p.detail, p.texture, p.texturescale));
90 pw.print(getMapPlaneString(g, c, d, p.detail, "common/caulk", p.texturescale));
91 pw.print(getMapPlaneString(c, g, a, p.detail, "common/caulk", p.texturescale));
92 pw.print(getMapPlaneString(h, d, a, p.detail, "common/caulk", p.texturescale));
93 pw.print(getMapPlaneString(g, h, f, p.detail, "common/caulk", p.texturescale));
99 double xmax = (columns.length - 1) * units;
100 double ymax = (columns[0].length - 1) * units;
104 List<Block> fillers = genSkyFillers(columns);
105 for (Block b : fillers) {
106 double x = b.x * units;
107 double y = (b.y + b.ydim) * units;
108 x = x > xmax ? xmax : x;
109 y = y > ymax ? ymax : y;
110 Vector3D p1 = new Vector3D(x, -y, -32.0);
112 x = (b.x + b.xdim) * units;
114 x = x > xmax ? xmax : x;
115 y = y > ymax ? ymax : y;
116 Vector3D p2 = new Vector3D(x, -y, p.skyheight);
118 writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
124 int x = height.length - 1;
125 int y = height[0].length - 1;
128 Vector3D p1 = new Vector3D(0, -y * units, p.skyheight);
129 Vector3D p2 = new Vector3D(x * units, 0, p.skyheight + 32.0);
130 writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
133 p1 = new Vector3D(0, -y * units, -64.0);
134 p2 = new Vector3D(x * units, 0, -32.0);
135 writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
138 p1 = new Vector3D(0, 0, -32.0);
139 p2 = new Vector3D(x * units, 32, p.skyheight);
140 writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
143 p1 = new Vector3D(x * units, -y * units, -32.0);
144 p2 = new Vector3D(x * units + 32.0, 0, p.skyheight);
145 writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
148 p1 = new Vector3D(0, -y * units - 32, -32.0);
149 p2 = new Vector3D(x * units, -y * units, p.skyheight);
150 writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
154 p1 = new Vector3D(0 - 32.0, -y * units, -32.0);
155 p2 = new Vector3D(0, 0, p.skyheight);
156 writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
160 // genBlockers screws the columns array!
161 // this should be the last step!
163 List<Block> blockers = genBlockers(columns, 0.15);
164 for (Block b : blockers) {
165 double z = b.minheight * p.height - 1;
166 z = Math.floor(z / 16);
170 double x = b.x * units;
171 double y = (b.y + b.ydim) * units;
172 x = x > xmax ? xmax : x;
173 y = y > ymax ? ymax : y;
174 Vector3D p1 = new Vector3D(x, -y, -32.0);
176 x = (b.x + b.xdim) * units;
178 x = x > xmax ? xmax : x;
179 y = y > ymax ? ymax : y;
180 Vector3D p2 = new Vector3D(x, -y, z);
182 writeBoxBrush(pw, p1, p2, false, "common/caulk", 1.0);
194 private void writeBoxBrush(PrintWriter pw, Vector3D p1, Vector3D p2, boolean detail, String texture, double scale) {
195 Vector3D a = new Vector3D(p1.x, p2.y, p2.z);
197 Vector3D c = new Vector3D(p1.x, p1.y, p2.z);
198 Vector3D d = new Vector3D(p2.x, p1.y, p2.z);
200 Vector3D f = new Vector3D(p2.x, p2.y, p1.z);
202 Vector3D h = new Vector3D(p2.x, p1.y, p1.z);
205 pw.print(getMapPlaneString(a, b, d, detail, texture, scale));
206 pw.print(getMapPlaneString(d, b, f, detail, texture, scale));
207 pw.print(getMapPlaneString(c, d, h, detail, texture, scale));
208 pw.print(getMapPlaneString(a, c, g, detail, texture, scale));
209 pw.print(getMapPlaneString(f, b, a, detail, texture, scale));
210 pw.print(getMapPlaneString(g, h, f, detail, texture, scale));
215 private String getMapPlaneString(Vector3D p1, Vector3D p2, Vector3D p3, boolean detail, String material, double scale) {
222 return "( " + p1.x + " " + p1.y + " " + p1.z + " ) ( " + p2.x + " " + p2.y + " " + p2.z + " ) ( " + p3.x + " " + p3.y + " " + p3.z + " ) " + material + " 0 0 0 " + scale + " " + scale + " " + flag + " 0 0\n";
225 private double[][] getHeightmap(String file) {
227 BufferedImage bimg = ImageIO.read(new File(file));
228 Raster raster = bimg.getRaster();
229 int x = raster.getWidth();
230 int y = raster.getHeight();
232 double[][] result = new double[x][y];
234 for (int xi = 0; xi < x; ++xi) {
235 for (int yi = 0; yi < y; ++yi) {
236 float[] pixel = raster.getPixel(xi, yi, (float[]) null);
240 if (pixel.length == 3) {
244 } else if (pixel.length == 4) {
248 } else if (pixel.length == 1) {
253 // grayscale with alpha
259 for (int i = 0; i < channels; ++i) {
262 result[xi][yi] = tmp / (channels * 255f);
265 // mark this pixel to be skipped
266 if (pixel[pixel.length - 1] < 64.0) {
267 result[xi][yi] = -1.0;
275 } catch (IOException ex) {
276 Logger.getLogger(MapWriter.class.getName()).log(Level.SEVERE, null, ex);
282 private double[][] getColumns(double[][] heights) {
283 double[][] result = new double[heights.length][heights[0].length];
285 for (int x = 0; x < heights.length; ++x) {
286 for (int y = 0; y < heights[0].length; ++y) {
287 result[x][y] = getMinMaxForRegion(heights, x, y, 2)[0];
294 private double[] getMinMaxForRegion(double[][] field, int x, int y, int dim) {
295 return getMinMaxForRegion(field, x, y, dim, dim);
298 private double[] getMinMaxForRegion(double[][] field, int x, int y, int xdim, int ydim) {
302 for (int i = x; i < x + xdim; ++i) {
303 for (int j = y; j < y + ydim; ++j) {
304 if (i >= 0 && j >= 0 && i < field.length && j < field[0].length) {
305 min = field[i][j] < min ? field[i][j] : min;
306 max = field[i][j] > max ? field[i][j] : max;
311 double[] result = {min, max};
315 private List<Block> genBlockers(double[][] columns, double delta) {
317 Block[][] blockers = new Block[columns.length][columns[0].length];
318 LinkedList<Block> result = new LinkedList<Block>();
320 for (int x = 0; x < columns.length; ++x) {
321 for (int y = 0; y < columns[0].length; ++y) {
322 if (blockers[x][y] == null && columns[x][y] >= 0) {
323 // this pixel isn't covered by a blocker yet... so let's create one!
324 Block b = new Block();
328 b.minheight = b.origheight = columns[x][y];
330 // grow till the delta hits
333 boolean xgrow = true;
334 boolean ygrow = true;
335 double min = b.minheight;
336 for (; xdim < columns.length && ydim < columns[0].length;) {
337 double[] minmax = getMinMaxForRegion(columns, x, y, xdim + 1, ydim);
338 if (Math.abs(b.origheight - minmax[0]) > delta || Math.abs(b.origheight - minmax[1]) > delta) {
342 minmax = getMinMaxForRegion(columns, x, y, xdim, ydim + 1);
343 if (Math.abs(b.origheight - minmax[0]) > delta || Math.abs(b.origheight - minmax[1]) > delta) {
356 minmax = getMinMaxForRegion(columns, x, y, xdim, ydim);
359 if (!(xgrow || ygrow)) {
368 for (int i = x; i < x + b.xdim; ++i) {
369 for (int j = y; j < y + b.ydim; ++j) {
370 if (i >= 0 && j >= 0 && i < blockers.length && j < blockers[0].length) {
372 columns[i][j] = -1337.0;
383 private List<Block> genSkyFillers(double[][] columns) {
387 for (int x = 0; x < columns.length; ++x) {
388 for (int y = 0; y < columns[0].length; ++y) {
389 if (columns[x][y] < 0) {
390 // this is a skipped block, see if it neighbours a
392 if (getMinMaxForRegion(columns, x - 1, y - 1, 3)[1] >= 0) {
393 columns[x][y] = -100d;
400 Block[][] fillers = new Block[columns.length][columns[0].length];
401 LinkedList<Block> result = new LinkedList<Block>();
403 for (int x = 0; x < columns.length; ++x) {
404 for (int y = 0; y < columns[0].length; ++y) {
405 if (fillers[x][y] == null && columns[x][y] == -100d) {
406 // this pixel is marked to be skyfill
407 Block b = new Block();
411 b.minheight = b.origheight = columns[x][y];
413 // grow till the delta hits
416 boolean xgrow = true;
417 boolean ygrow = true;
418 double min = b.minheight;
419 for (; xdim < columns.length && ydim < columns[0].length;) {
420 double[] minmax = getMinMaxForRegion(columns, x, y, xdim + 1, ydim);
421 if (Math.abs(b.origheight - minmax[0]) > delta || Math.abs(b.origheight - minmax[1]) > delta) {
425 minmax = getMinMaxForRegion(columns, x, y, xdim, ydim + 1);
426 if (Math.abs(b.origheight - minmax[0]) > delta || Math.abs(b.origheight - minmax[1]) > delta) {
439 minmax = getMinMaxForRegion(columns, x, y, xdim, ydim);
442 if (!(xgrow || ygrow)) {
451 for (int i = x; i < x + b.xdim; ++i) {
452 for (int j = y; j < y + b.ydim; ++j) {
453 if (i >= 0 && j >= 0 && i < fillers.length && j < fillers[0].length) {
455 columns[i][j] = -1337.0;
466 private class Vector3D {
468 public double x, y, z;
474 public Vector3D(double x, double y, double z) {
480 public Vector3D crossproduct(Vector3D p1) {
481 Vector3D result = new Vector3D();
483 result.x = this.y * p1.z - this.z * p1.y;
484 result.y = this.z * p1.x - this.x * p1.z;
485 result.z = this.x * p1.y - this.y * p1.x;
490 public double dotproduct(Vector3D p1) {
491 return this.x * p1.x + this.y * p1.y + this.z * p1.z;
494 public Vector3D substract(Vector3D p1) {
495 Vector3D result = new Vector3D();
497 result.x = this.x - p1.x;
498 result.y = this.y - p1.y;
499 result.z = this.z - p1.z;
504 public void scale(double factor) {
510 public double length() {
511 return Math.sqrt((x * x) + (y * y) + (z * z));
514 public void normalize() {
523 private class Block {
525 public int x, y, xdim, ydim;
526 public double origheight, minheight;