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);
45 pw.print("{\n\"classname\" \"worldspawn\"\n");
47 double xmax = (columns.length - 1) * units;
48 double ymax = (columns[0].length - 1) * units;
51 List<Block> fillers = genSkyFillers(columns);
52 for (Block b : fillers) {
53 double x = b.x * units;
54 double y = (b.y + b.ydim) * units;
55 x = x > xmax ? xmax : x;
56 y = y > ymax ? ymax : y;
57 Vector3D p1 = new Vector3D(x, -y, -32.0);
59 x = (b.x + b.xdim) * units;
61 x = x > xmax ? xmax : x;
62 y = y > ymax ? ymax : y;
63 Vector3D p2 = new Vector3D(x, -y, p.skyheight);
65 writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
71 int x = height.length - 1;
72 int y = height[0].length - 1;
75 Vector3D p1 = new Vector3D(0, -y * units, p.skyheight);
76 Vector3D p2 = new Vector3D(x * units, 0, p.skyheight + 32.0);
77 writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
80 p1 = new Vector3D(0, -y * units, -64.0);
81 p2 = new Vector3D(x * units, 0, -32.0);
82 writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
85 p1 = new Vector3D(0, 0, -32.0);
86 p2 = new Vector3D(x * units, 32, p.skyheight);
87 writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
90 p1 = new Vector3D(x * units, -y * units, -32.0);
91 p2 = new Vector3D(x * units + 32.0, 0, p.skyheight);
92 writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
95 p1 = new Vector3D(0, -y * units - 32, -32.0);
96 p2 = new Vector3D(x * units, -y * units, p.skyheight);
97 writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
101 p1 = new Vector3D(0 - 32.0, -y * units, -32.0);
102 p2 = new Vector3D(0, 0, p.skyheight);
103 writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
107 // genBlockers screws the columns array!
108 // this should be the last step!
110 List<Block> blockers = genBlockers(columns, 0.15);
111 for (Block b : blockers) {
112 double z = b.minheight * p.height - 1;
113 z = Math.floor(z / 16);
117 double x = b.x * units;
118 double y = (b.y + b.ydim) * units;
119 x = x > xmax ? xmax : x;
120 y = y > ymax ? ymax : y;
121 Vector3D p1 = new Vector3D(x, -y, -32.0);
123 x = (b.x + b.xdim) * units;
125 x = x > xmax ? xmax : x;
126 y = y > ymax ? ymax : y;
127 Vector3D p2 = new Vector3D(x, -y, z);
129 writeBoxBrush(pw, p1, p2, false, "common/caulk", 1.0);
139 pw.print("{\n\"classname\" \"func_group\"\n");
140 pw.print("\n\"terrain\" \"1\"\n");
141 // wander through grid
142 for (int x = 0; x < height.length - 1; ++x) {
143 for (int y = 0; y < height[0].length - 1; ++y) {
145 boolean skip = getMinMaxForRegion(height, x, y, 2)[0] < 0;
155 * c +-------+ d + f (e occluded, unused)
165 double grad1 = Math.abs(height[x][y] - height[x + 1][y + 1]);
168 double grad2 = Math.abs(height[x + 1][y] - height[x][y + 1]);
170 Vector3D a = new Vector3D(x * units, -y * units, Math.floor(height[x][y] * max));
171 Vector3D b = new Vector3D((x + 1) * units, -y * units, Math.floor(height[x + 1][y] * max));
172 Vector3D c = new Vector3D(x * units, -(y + 1) * units, Math.floor(height[x][y + 1] * max));
173 Vector3D d = new Vector3D((x + 1) * units, -(y + 1) * units, Math.floor(height[x + 1][y + 1] * max));
174 //Vector3D e = new Vector3D(x * units, -y * units, -16.0);
175 Vector3D f = new Vector3D((x + 1) * units, -y * units, -16.0);
176 Vector3D g = new Vector3D(x * units, -(y + 1) * units, -16.0);
177 Vector3D h = new Vector3D((x + 1) * units, -(y + 1) * units, -16.0);
181 pw.print(getMapPlaneString(a, b, c, p.detail, p.texture, p.texturescale));
182 pw.print(getMapPlaneString(f, b, a, p.detail, "common/caulk", p.texturescale));
183 pw.print(getMapPlaneString(a, c, g, p.detail, "common/caulk", p.texturescale));
184 pw.print(getMapPlaneString(g, h, f, p.detail, "common/caulk", p.texturescale));
185 pw.print(getMapPlaneString(g, c, b, p.detail, "common/caulk", p.texturescale));
190 pw.print(getMapPlaneString(b, d, c, p.detail, p.texture, p.texturescale));
191 pw.print(getMapPlaneString(d, h, g, p.detail, "common/caulk", p.texturescale));
192 pw.print(getMapPlaneString(d, b, f, p.detail, "common/caulk", p.texturescale));
193 pw.print(getMapPlaneString(f, b, c, p.detail, "common/caulk", p.texturescale));
194 pw.print(getMapPlaneString(g, h, f, p.detail, "common/caulk", p.texturescale));
200 pw.print(getMapPlaneString(a, b, d, p.detail, p.texture, p.texturescale));
201 pw.print(getMapPlaneString(d, b, f, p.detail, "common/caulk", p.texturescale));
202 pw.print(getMapPlaneString(f, b, a, p.detail, "common/caulk", p.texturescale));
203 pw.print(getMapPlaneString(a, d, h, p.detail, "common/caulk", p.texturescale));
204 pw.print(getMapPlaneString(g, h, f, p.detail, "common/caulk", p.texturescale));
209 pw.print(getMapPlaneString(d, c, a, p.detail, p.texture, p.texturescale));
210 pw.print(getMapPlaneString(g, c, d, p.detail, "common/caulk", p.texturescale));
211 pw.print(getMapPlaneString(c, g, a, p.detail, "common/caulk", p.texturescale));
212 pw.print(getMapPlaneString(h, d, a, p.detail, "common/caulk", p.texturescale));
213 pw.print(getMapPlaneString(g, h, f, p.detail, "common/caulk", p.texturescale));
226 private void writeBoxBrush(PrintWriter pw, Vector3D p1, Vector3D p2, boolean detail, String texture, double scale) {
227 Vector3D a = new Vector3D(p1.x, p2.y, p2.z);
229 Vector3D c = new Vector3D(p1.x, p1.y, p2.z);
230 Vector3D d = new Vector3D(p2.x, p1.y, p2.z);
232 Vector3D f = new Vector3D(p2.x, p2.y, p1.z);
234 Vector3D h = new Vector3D(p2.x, p1.y, p1.z);
237 pw.print(getMapPlaneString(a, b, d, detail, texture, scale));
238 pw.print(getMapPlaneString(d, b, f, detail, texture, scale));
239 pw.print(getMapPlaneString(c, d, h, detail, texture, scale));
240 pw.print(getMapPlaneString(a, c, g, detail, texture, scale));
241 pw.print(getMapPlaneString(f, b, a, detail, texture, scale));
242 pw.print(getMapPlaneString(g, h, f, detail, texture, scale));
247 private String getMapPlaneString(Vector3D p1, Vector3D p2, Vector3D p3, boolean detail, String material, double scale) {
254 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";
257 private double[][] getHeightmap(String file) {
259 BufferedImage bimg = ImageIO.read(new File(file));
260 Raster raster = bimg.getRaster();
261 int x = raster.getWidth();
262 int y = raster.getHeight();
264 double[][] result = new double[x][y];
266 for (int xi = 0; xi < x; ++xi) {
267 for (int yi = 0; yi < y; ++yi) {
268 float[] pixel = raster.getPixel(xi, yi, (float[]) null);
272 if (pixel.length == 3) {
276 } else if (pixel.length == 4) {
280 } else if (pixel.length == 1) {
285 // grayscale with alpha
291 for (int i = 0; i < channels; ++i) {
294 result[xi][yi] = tmp / (channels * 255f);
297 // mark this pixel to be skipped
298 if (pixel[pixel.length - 1] < 64.0) {
299 result[xi][yi] = -1.0;
307 } catch (IOException ex) {
308 Logger.getLogger(MapWriter.class.getName()).log(Level.SEVERE, null, ex);
314 private double[][] getColumns(double[][] heights) {
315 double[][] result = new double[heights.length][heights[0].length];
317 for (int x = 0; x < heights.length; ++x) {
318 for (int y = 0; y < heights[0].length; ++y) {
319 result[x][y] = getMinMaxForRegion(heights, x, y, 2)[0];
326 private double[] getMinMaxForRegion(double[][] field, int x, int y, int dim) {
327 return getMinMaxForRegion(field, x, y, dim, dim);
330 private double[] getMinMaxForRegion(double[][] field, int x, int y, int xdim, int ydim) {
334 for (int i = x; i < x + xdim; ++i) {
335 for (int j = y; j < y + ydim; ++j) {
336 if (i >= 0 && j >= 0 && i < field.length && j < field[0].length) {
337 min = field[i][j] < min ? field[i][j] : min;
338 max = field[i][j] > max ? field[i][j] : max;
343 double[] result = {min, max};
347 private List<Block> genBlockers(double[][] columns, double delta) {
349 Block[][] blockers = new Block[columns.length][columns[0].length];
350 LinkedList<Block> result = new LinkedList<Block>();
352 for (int x = 0; x < columns.length; ++x) {
353 for (int y = 0; y < columns[0].length; ++y) {
354 if (blockers[x][y] == null && columns[x][y] >= 0) {
355 // this pixel isn't covered by a blocker yet... so let's create one!
356 Block b = new Block();
360 b.minheight = b.origheight = columns[x][y];
362 // grow till the delta hits
365 boolean xgrow = true;
366 boolean ygrow = true;
367 double min = b.minheight;
368 for (; xdim < columns.length && ydim < columns[0].length;) {
369 double[] minmax = getMinMaxForRegion(columns, x, y, xdim + 1, ydim);
370 if (Math.abs(b.origheight - minmax[0]) > delta || Math.abs(b.origheight - minmax[1]) > delta) {
374 minmax = getMinMaxForRegion(columns, x, y, xdim, ydim + 1);
375 if (Math.abs(b.origheight - minmax[0]) > delta || Math.abs(b.origheight - minmax[1]) > delta) {
388 minmax = getMinMaxForRegion(columns, x, y, xdim, ydim);
391 if (!(xgrow || ygrow)) {
400 for (int i = x; i < x + b.xdim; ++i) {
401 for (int j = y; j < y + b.ydim; ++j) {
402 if (i >= 0 && j >= 0 && i < blockers.length && j < blockers[0].length) {
404 columns[i][j] = -1337.0;
415 private List<Block> genSkyFillers(double[][] columns) {
419 for (int x = 0; x < columns.length; ++x) {
420 for (int y = 0; y < columns[0].length; ++y) {
421 if (columns[x][y] < 0) {
422 // this is a skipped block, see if it neighbours a
424 if (getMinMaxForRegion(columns, x - 1, y - 1, 3)[1] >= 0) {
425 columns[x][y] = -100d;
432 Block[][] fillers = new Block[columns.length][columns[0].length];
433 LinkedList<Block> result = new LinkedList<Block>();
435 for (int x = 0; x < columns.length; ++x) {
436 for (int y = 0; y < columns[0].length; ++y) {
437 if (fillers[x][y] == null && columns[x][y] == -100d) {
438 // this pixel is marked to be skyfill
439 Block b = new Block();
443 b.minheight = b.origheight = columns[x][y];
445 // grow till the delta hits
448 boolean xgrow = true;
449 boolean ygrow = true;
450 double min = b.minheight;
451 for (; xdim < columns.length && ydim < columns[0].length;) {
452 double[] minmax = getMinMaxForRegion(columns, x, y, xdim + 1, ydim);
453 if (Math.abs(b.origheight - minmax[0]) > delta || Math.abs(b.origheight - minmax[1]) > delta) {
457 minmax = getMinMaxForRegion(columns, x, y, xdim, ydim + 1);
458 if (Math.abs(b.origheight - minmax[0]) > delta || Math.abs(b.origheight - minmax[1]) > delta) {
471 minmax = getMinMaxForRegion(columns, x, y, xdim, ydim);
474 if (!(xgrow || ygrow)) {
483 for (int i = x; i < x + b.xdim; ++i) {
484 for (int j = y; j < y + b.ydim; ++j) {
485 if (i >= 0 && j >= 0 && i < fillers.length && j < fillers[0].length) {
487 columns[i][j] = -1337.0;
498 private class Vector3D {
500 public double x, y, z;
506 public Vector3D(double x, double y, double z) {
512 public Vector3D crossproduct(Vector3D p1) {
513 Vector3D result = new Vector3D();
515 result.x = this.y * p1.z - this.z * p1.y;
516 result.y = this.z * p1.x - this.x * p1.z;
517 result.z = this.x * p1.y - this.y * p1.x;
522 public double dotproduct(Vector3D p1) {
523 return this.x * p1.x + this.y * p1.y + this.z * p1.z;
526 public Vector3D substract(Vector3D p1) {
527 Vector3D result = new Vector3D();
529 result.x = this.x - p1.x;
530 result.y = this.y - p1.y;
531 result.z = this.z - p1.z;
536 public void scale(double factor) {
542 public double length() {
543 return Math.sqrt((x * x) + (y * y) + (z * z));
546 public void normalize() {
555 private class Block {
557 public int x, y, xdim, ydim;
558 public double origheight, minheight;