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));
100 for (int x = 0; x < columns.length; ++x) {
101 for (int y = 0; y < columns[0].length; ++y) {
102 if (columns[x][y] < 0) {
103 // this is a skipped block, see if it neighbours a
105 double tmp = getMinMaxForRegion(columns, x - 1, y - 1, 3)[1];
107 Vector3D p1 = new Vector3D(x * units, -(y + 1) * units, -32.0);
108 Vector3D p2 = new Vector3D((x + 1) * units, -y * units, p.skyheight);
110 writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
119 int x = height.length - 1;
120 int y = height[0].length - 1;
123 Vector3D p1 = new Vector3D(0, -y * units, p.skyheight);
124 Vector3D p2 = new Vector3D(x * units, 0, p.skyheight + 32.0);
125 writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
128 p1 = new Vector3D(0, -y * units, -64.0);
129 p2 = new Vector3D(x * units, 0, -32.0);
130 writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
133 p1 = new Vector3D(0, 0, -32.0);
134 p2 = new Vector3D(x * units, 32, p.skyheight);
135 writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
138 p1 = new Vector3D(x * units, -y * units, -32.0);
139 p2 = new Vector3D(x * units + 32.0, 0, p.skyheight);
140 writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
143 p1 = new Vector3D(0, -y * units - 32, -32.0);
144 p2 = new Vector3D(x * units, -y * units, p.skyheight);
145 writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
149 p1 = new Vector3D(0 - 32.0, -y * units, -32.0);
150 p2 = new Vector3D(0, 0, p.skyheight);
151 writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
155 // genBlockers screws the columns array!
156 // this should be the last step!
158 List<VisBlocker> blockers = genBlockers(columns, 0.15);
159 double xmax = (columns.length - 1) * units;
160 double ymax = (columns[0].length - 1) * units;
161 for (VisBlocker b : blockers) {
162 double z = b.minheight * p.height;
163 z = Math.floor(z / 16);
169 double x = b.x * units;
170 double y = (b.y + dim) * units;
171 x = x > xmax ? xmax : x;
172 y = y > ymax ? ymax : y;
173 Vector3D p1 = new Vector3D(x, -y, -32.0);
175 x = (b.x + dim) * units;
177 x = x > xmax ? xmax : x;
178 y = y > ymax ? ymax : y;
179 Vector3D p2 = new Vector3D(x, -y, z);
181 writeBoxBrush(pw, p1, p2, false, "common/caulk", 1.0);
193 private void writeBoxBrush(PrintWriter pw, Vector3D p1, Vector3D p2, boolean detail, String texture, double scale) {
194 Vector3D a = new Vector3D(p1.x, p2.y, p2.z);
196 Vector3D c = new Vector3D(p1.x, p1.y, p2.z);
197 Vector3D d = new Vector3D(p2.x, p1.y, p2.z);
199 Vector3D f = new Vector3D(p2.x, p2.y, p1.z);
201 Vector3D h = new Vector3D(p2.x, p1.y, p1.z);
204 pw.print(getMapPlaneString(a, b, d, detail, texture, scale));
205 pw.print(getMapPlaneString(d, b, f, detail, texture, scale));
206 pw.print(getMapPlaneString(c, d, h, detail, texture, scale));
207 pw.print(getMapPlaneString(a, c, g, detail, texture, scale));
208 pw.print(getMapPlaneString(f, b, a, detail, texture, scale));
209 pw.print(getMapPlaneString(g, h, f, detail, texture, scale));
214 private String getMapPlaneString(Vector3D p1, Vector3D p2, Vector3D p3, boolean detail, String material, double scale) {
221 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";
224 private double[][] getHeightmap(String file) {
226 BufferedImage bimg = ImageIO.read(new File(file));
227 Raster raster = bimg.getRaster();
228 int x = raster.getWidth();
229 int y = raster.getHeight();
231 double[][] result = new double[x][y];
233 for (int xi = 0; xi < x; ++xi) {
234 for (int yi = 0; yi < y; ++yi) {
235 float[] pixel = raster.getPixel(xi, yi, (float[]) null);
239 if (pixel.length == 3) {
243 } else if (pixel.length == 4) {
247 } else if (pixel.length == 1) {
252 // grayscale with alpha
258 for (int i = 0; i < channels; ++i) {
261 result[xi][yi] = tmp / (channels * 255f);
264 // mark this pixel to be skipped
265 if (pixel[pixel.length - 1] < 64.0) {
266 result[xi][yi] = -1.0;
274 } catch (IOException ex) {
275 Logger.getLogger(MapWriter.class.getName()).log(Level.SEVERE, null, ex);
281 private double[][] getColumns(double[][] heights) {
282 double[][] result = new double[heights.length][heights[0].length];
284 for (int x = 0; x < heights.length; ++x) {
285 for (int y = 0; y < heights[0].length; ++y) {
286 result[x][y] = getMinMaxForRegion(heights, x, y, 2)[0];
293 private double[] getMinMaxForRegion(double[][] field, int x, int y, int dim) {
297 for (int i = x; i < x + dim; ++i) {
298 for (int j = y; j < y + dim; ++j) {
299 if (i >= 0 && j >= 0 && i < field.length && j < field[0].length) {
300 min = field[i][j] < min ? field[i][j] : min;
301 max = field[i][j] > max ? field[i][j] : max;
306 double[] result = {min, max};
310 public List<VisBlocker> genBlockers(double[][] columns, double delta) {
312 VisBlocker[][] blockers = new VisBlocker[columns.length][columns[0].length];
313 LinkedList<VisBlocker> result = new LinkedList<VisBlocker>();
315 for (int x = 0; x < columns.length; ++x) {
316 for (int y = 0; y < columns[0].length; ++y) {
317 if (blockers[x][y] == null && columns[x][y] >= 0) {
318 // this pixel isn't covered by a blocker yet... so let's create one!
319 VisBlocker b = new VisBlocker();
323 b.minheight = b.origheight = columns[x][y];
325 // grow till the delta hits
327 double min = b.minheight;
328 for (dim = 1; dim < columns.length; ++dim) {
329 double[] minmax = getMinMaxForRegion(columns, x, y, dim);
330 if (Math.abs(b.origheight - minmax[0]) > delta || Math.abs(b.origheight - minmax[1]) > delta) {
340 for (int i = x; i < x + b.dim; ++i) {
341 for (int j = y; j < y + b.dim; ++j) {
342 if (i >= 0 && j >= 0 && i < blockers.length && j < blockers[0].length) {
344 columns[i][j] = -1337.0;
355 private class Vector3D {
357 public double x, y, z;
363 public Vector3D(double x, double y, double z) {
369 public Vector3D crossproduct(Vector3D p1) {
370 Vector3D result = new Vector3D();
372 result.x = this.y * p1.z - this.z * p1.y;
373 result.y = this.z * p1.x - this.x * p1.z;
374 result.z = this.x * p1.y - this.y * p1.x;
379 public double dotproduct(Vector3D p1) {
380 return this.x * p1.x + this.y * p1.y + this.z * p1.z;
383 public Vector3D substract(Vector3D p1) {
384 Vector3D result = new Vector3D();
386 result.x = this.x - p1.x;
387 result.y = this.y - p1.y;
388 result.z = this.z - p1.z;
393 public void scale(double factor) {
399 public double length() {
400 return Math.sqrt((x * x) + (y * y) + (z * z));
403 public void normalize() {
412 private class VisBlocker {
414 public int x, y, dim;
415 public double origheight, minheight;