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 // wander through grid
141 for (int x = 0; x < height.length - 1; ++x) {
142 for (int y = 0; y < height[0].length - 1; ++y) {
144 boolean skip = getMinMaxForRegion(height, x, y, 2)[0] < 0;
154 * c +-------+ d + f (e occluded, unused)
162 Vector3D a = new Vector3D(x * units, -y * units, height[x][y] * max);
163 Vector3D b = new Vector3D((x + 1) * units, -y * units, height[x + 1][y] * max);
164 Vector3D c = new Vector3D(x * units, -(y + 1) * units, height[x][y + 1] * max);
165 Vector3D d = new Vector3D((x + 1) * units, -(y + 1) * units, height[x + 1][y + 1] * max);
166 //Vector3D e = new Vector3D(x * units, -y * units, -16.0);
167 Vector3D f = new Vector3D((x + 1) * units, -y * units, -16.0);
168 Vector3D g = new Vector3D(x * units, -(y + 1) * units, -16.0);
169 Vector3D h = new Vector3D((x + 1) * units, -(y + 1) * units, -16.0);
172 pw.print(getMapPlaneString(a, b, d, p.detail, p.texture, p.texturescale));
173 pw.print(getMapPlaneString(d, b, f, p.detail, "common/caulk", p.texturescale));
174 pw.print(getMapPlaneString(f, b, a, p.detail, "common/caulk", p.texturescale));
175 pw.print(getMapPlaneString(a, d, h, p.detail, "common/caulk", p.texturescale));
176 pw.print(getMapPlaneString(g, h, f, p.detail, "common/caulk", p.texturescale));
181 pw.print(getMapPlaneString(d, c, a, p.detail, p.texture, p.texturescale));
182 pw.print(getMapPlaneString(g, c, d, p.detail, "common/caulk", p.texturescale));
183 pw.print(getMapPlaneString(c, g, a, p.detail, "common/caulk", p.texturescale));
184 pw.print(getMapPlaneString(h, d, a, p.detail, "common/caulk", p.texturescale));
185 pw.print(getMapPlaneString(g, h, f, p.detail, "common/caulk", p.texturescale));
197 private void writeBoxBrush(PrintWriter pw, Vector3D p1, Vector3D p2, boolean detail, String texture, double scale) {
198 Vector3D a = new Vector3D(p1.x, p2.y, p2.z);
200 Vector3D c = new Vector3D(p1.x, p1.y, p2.z);
201 Vector3D d = new Vector3D(p2.x, p1.y, p2.z);
203 Vector3D f = new Vector3D(p2.x, p2.y, p1.z);
205 Vector3D h = new Vector3D(p2.x, p1.y, p1.z);
208 pw.print(getMapPlaneString(a, b, d, detail, texture, scale));
209 pw.print(getMapPlaneString(d, b, f, detail, texture, scale));
210 pw.print(getMapPlaneString(c, d, h, detail, texture, scale));
211 pw.print(getMapPlaneString(a, c, g, detail, texture, scale));
212 pw.print(getMapPlaneString(f, b, a, detail, texture, scale));
213 pw.print(getMapPlaneString(g, h, f, detail, texture, scale));
218 private String getMapPlaneString(Vector3D p1, Vector3D p2, Vector3D p3, boolean detail, String material, double scale) {
225 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";
228 private double[][] getHeightmap(String file) {
230 BufferedImage bimg = ImageIO.read(new File(file));
231 Raster raster = bimg.getRaster();
232 int x = raster.getWidth();
233 int y = raster.getHeight();
235 double[][] result = new double[x][y];
237 for (int xi = 0; xi < x; ++xi) {
238 for (int yi = 0; yi < y; ++yi) {
239 float[] pixel = raster.getPixel(xi, yi, (float[]) null);
243 if (pixel.length == 3) {
247 } else if (pixel.length == 4) {
251 } else if (pixel.length == 1) {
256 // grayscale with alpha
262 for (int i = 0; i < channels; ++i) {
265 result[xi][yi] = tmp / (channels * 255f);
268 // mark this pixel to be skipped
269 if (pixel[pixel.length - 1] < 64.0) {
270 result[xi][yi] = -1.0;
278 } catch (IOException ex) {
279 Logger.getLogger(MapWriter.class.getName()).log(Level.SEVERE, null, ex);
285 private double[][] getColumns(double[][] heights) {
286 double[][] result = new double[heights.length][heights[0].length];
288 for (int x = 0; x < heights.length; ++x) {
289 for (int y = 0; y < heights[0].length; ++y) {
290 result[x][y] = getMinMaxForRegion(heights, x, y, 2)[0];
297 private double[] getMinMaxForRegion(double[][] field, int x, int y, int dim) {
298 return getMinMaxForRegion(field, x, y, dim, dim);
301 private double[] getMinMaxForRegion(double[][] field, int x, int y, int xdim, int ydim) {
305 for (int i = x; i < x + xdim; ++i) {
306 for (int j = y; j < y + ydim; ++j) {
307 if (i >= 0 && j >= 0 && i < field.length && j < field[0].length) {
308 min = field[i][j] < min ? field[i][j] : min;
309 max = field[i][j] > max ? field[i][j] : max;
314 double[] result = {min, max};
318 private List<Block> genBlockers(double[][] columns, double delta) {
320 Block[][] blockers = new Block[columns.length][columns[0].length];
321 LinkedList<Block> result = new LinkedList<Block>();
323 for (int x = 0; x < columns.length; ++x) {
324 for (int y = 0; y < columns[0].length; ++y) {
325 if (blockers[x][y] == null && columns[x][y] >= 0) {
326 // this pixel isn't covered by a blocker yet... so let's create one!
327 Block b = new Block();
331 b.minheight = b.origheight = columns[x][y];
333 // grow till the delta hits
336 boolean xgrow = true;
337 boolean ygrow = true;
338 double min = b.minheight;
339 for (; xdim < columns.length && ydim < columns[0].length;) {
340 double[] minmax = getMinMaxForRegion(columns, x, y, xdim + 1, ydim);
341 if (Math.abs(b.origheight - minmax[0]) > delta || Math.abs(b.origheight - minmax[1]) > delta) {
345 minmax = getMinMaxForRegion(columns, x, y, xdim, ydim + 1);
346 if (Math.abs(b.origheight - minmax[0]) > delta || Math.abs(b.origheight - minmax[1]) > delta) {
359 minmax = getMinMaxForRegion(columns, x, y, xdim, ydim);
362 if (!(xgrow || ygrow)) {
371 for (int i = x; i < x + b.xdim; ++i) {
372 for (int j = y; j < y + b.ydim; ++j) {
373 if (i >= 0 && j >= 0 && i < blockers.length && j < blockers[0].length) {
375 columns[i][j] = -1337.0;
386 private List<Block> genSkyFillers(double[][] columns) {
390 for (int x = 0; x < columns.length; ++x) {
391 for (int y = 0; y < columns[0].length; ++y) {
392 if (columns[x][y] < 0) {
393 // this is a skipped block, see if it neighbours a
395 if (getMinMaxForRegion(columns, x - 1, y - 1, 3)[1] >= 0) {
396 columns[x][y] = -100d;
403 Block[][] fillers = new Block[columns.length][columns[0].length];
404 LinkedList<Block> result = new LinkedList<Block>();
406 for (int x = 0; x < columns.length; ++x) {
407 for (int y = 0; y < columns[0].length; ++y) {
408 if (fillers[x][y] == null && columns[x][y] == -100d) {
409 // this pixel is marked to be skyfill
410 Block b = new Block();
414 b.minheight = b.origheight = columns[x][y];
416 // grow till the delta hits
419 boolean xgrow = true;
420 boolean ygrow = true;
421 double min = b.minheight;
422 for (; xdim < columns.length && ydim < columns[0].length;) {
423 double[] minmax = getMinMaxForRegion(columns, x, y, xdim + 1, ydim);
424 if (Math.abs(b.origheight - minmax[0]) > delta || Math.abs(b.origheight - minmax[1]) > delta) {
428 minmax = getMinMaxForRegion(columns, x, y, xdim, ydim + 1);
429 if (Math.abs(b.origheight - minmax[0]) > delta || Math.abs(b.origheight - minmax[1]) > delta) {
442 minmax = getMinMaxForRegion(columns, x, y, xdim, ydim);
445 if (!(xgrow || ygrow)) {
454 for (int i = x; i < x + b.xdim; ++i) {
455 for (int j = y; j < y + b.ydim; ++j) {
456 if (i >= 0 && j >= 0 && i < fillers.length && j < fillers[0].length) {
458 columns[i][j] = -1337.0;
469 private class Vector3D {
471 public double x, y, z;
477 public Vector3D(double x, double y, double z) {
483 public Vector3D crossproduct(Vector3D p1) {
484 Vector3D result = new Vector3D();
486 result.x = this.y * p1.z - this.z * p1.y;
487 result.y = this.z * p1.x - this.x * p1.z;
488 result.z = this.x * p1.y - this.y * p1.x;
493 public double dotproduct(Vector3D p1) {
494 return this.x * p1.x + this.y * p1.y + this.z * p1.z;
497 public Vector3D substract(Vector3D p1) {
498 Vector3D result = new Vector3D();
500 result.x = this.x - p1.x;
501 result.y = this.y - p1.y;
502 result.z = this.z - p1.z;
507 public void scale(double factor) {
513 public double length() {
514 return Math.sqrt((x * x) + (y * y) + (z * z));
517 public void normalize() {
526 private class Block {
528 public int x, y, xdim, ydim;
529 public double origheight, minheight;