import org.xpilot.jxpmap.*; /** * Utility to check if a map objects is a regular wall polygon */ isWall(p) { return (p instanceof MapPolygon) ; } /** * Finds a style with the matching id from the list */ findStyle(id, list) { for (style:list) if (style.getId().equals(id)) return style; return null; } /** * Randomizes all wall polygons with the given amount of pixels. */ randomize(pixels) { print("randomizing walls with " + pixels + " pixels"); for(o:editor.getModel().getObjects()) { if (isWall(o)) { for (i = 0; i < o.getNumPoints(); i++) { p = o.getPoint(i); p.translate((int)((Math.random() - 0.5) * pixels * 64), (int)((Math.random() - 0.5) * pixels * 64)); o.setPoint(i, p.x, p.y); } } } editor.repaint(); } /** * Adds a drop shadow to each wall polygon by creating * a dark gray decoration polygon corresponding to each wall polygon. */ shadow() { model = editor.getModel(); es = findStyle("hidden", model.getEdgeStyles()); if (es == null) { es = new LineStyle("hidden", 1, Color.darkGray, LineStyle.STYLE_HIDDEN); model.getEdgeStyles().add(es); } ps = findStyle("shadow", model.getPolyStyles()); if (ps == null) { ps = new PolygonStyle(); ps.setColor(new Color(30,30,30)); ps.setFillStyle(PolygonStyle.FILL_COLOR); ps.setId("shadow"); ps.setVisibleInRadar(false); ps.setDefaultEdgeStyle(es); model.getPolyStyles().add(ps); } addShadow(mp) { p = mp.getPolygon(); sp = new Polygon(p.xpoints, p.ypoints, p.npoints); for(i = 0; i < sp.npoints; i++) { sp.xpoints[i] += 240; sp.ypoints[i] -= 640; } sp.invalidate(); model.addToBack(new Decoration(sp, ps, null)); } for(o:new ArrayList(model.getObjects())) if (isWall(o)) addShadow(o); editor.repaint(); } /** * Smoothens the corners of each wall polygon by adding one additional * vertex to each corner and moving the original vertex. Run multiple * times to get extra smoothness. The max parameter controls the maximum * amount of pixels a vertex gets moved. */ smooth(max) { print("smoothing walls with max " + max + " pixels"); move(p, dir, len) { a = len * Math.pow(p.distanceSq(dir), -0.5); p.translate((int)(a * (dir.x - p.x)), (int)(a * (dir.y - p.y))); } smooth(mp) { if (mp.getNumPoints() < 3) return; minDist = 1 * 64; maxMove = max * 64; p2 = mp.getPoint(mp.getNumPoints() - 1); p3 = mp.getPoint(0); for (i = 1; i <= mp.getNumPoints(); i++) { p1 = p2; p2 = p3; p3 = mp.getPoint(i % mp.getNumPoints()); d12 = p1.distance(p2); if (d12 <= minDist) continue; d23 = p2.distance(p3); if (d23 <= minDist) continue; pn = new Point(p2); move(pn, p1, Math.min(maxMove, (int)(d12 / 2))); move(p2, p3, Math.min(maxMove, (int)(d23 / 2))); mp.insertPoint(i - 1, pn); mp.setPoint(i, p2.x, p2.y); i++; } } for(o:new ArrayList(editor.getModel().getObjects())) if (isWall(o)) smooth(o); editor.repaint(); }