import java.util.ArrayDeque; import pathfinder.*; PImage img_map; PImage img_houses; PImage img_streets; PGraphics pg_map; int i_mapOffsetX, i_mapOffsetY, i_mapOffsetStartX, i_mapOffsetStartY; int i_biggestHouseSize; int i_mapW, i_mapH; ArrayList houses = new ArrayList(); void initHouses() { s_map = new String[img_houses.height/i_sMapReso]; for (int y = 0; y < s_map.length; y++) { s_map[y] = str('.'); //because += char on string delivers "null" as first stringentry in each arrayentry?! for (int x = 0; x < img_houses.width/i_sMapReso; x++) { s_map[y]+= '.'; } } img_houses.loadPixels(); for (int x = 0; x < img_houses.width; x++) { for (int y = 0; y < img_houses.height; y++) { if ((int)red(img_houses.pixels[y*img_houses.width+x]) == 187) { //red must be 187 for a pixel to be part of a house houses.add(new House(x, y)); img_houses.updatePixels(); img_houses.loadPixels(); } } } img_houses.updatePixels(); for (House house : houses) { //give houses their respective relative size (biggest house = 100) house.i_size = (int)map(house.i_size, 0, i_biggestHouseSize, 0, 100); //create ascii map in s_map for (PVector point : house.v2d_points) { //create ascii map in s_map s_map[int(point.y/i_sMapReso)] = replaceCharAt_v1(s_map[int(point.y/i_sMapReso)], int(point.x/i_sMapReso), '#'); } } saveStrings(dataPath("server/map.txt"), s_map); } String replaceCharAt_v1(String s, int pos, char c) { char[] ch = new char[s.length()]; s.getChars(0, ch.length, ch, 0); ch[pos] = c; return new String(ch); } class House { PVector v2_center; int i_size; int i_ID; void display() { pg_map.text("ID "+i_ID, v2_center.x, v2_center.y); pg_map.text("Size : " + i_size,v2_center.x,v2_center.y+10); } ArrayDeque v2d_selected = new ArrayDeque(); ArrayList v2d_points = new ArrayList(); boolean isToSelect(int px, int py, int[] pxl, int pw, int ph, int orgColor) { if (px < 0 || px >= pw || py < 0 || py >= ph) return false; return pxl[px + py * pw] == orgColor; } PVector v2_randomSpawnPoint() { return v2d_points.get(int(random(0, v2d_points.size()))); } House(int startX, int startY) { i_ID = houses.size(); int [] pxl = img_houses.pixels; int pw = img_houses.width; int ph = img_houses.height; int orgColor = pxl[startX + startY * pw]; PVector v2_p = new PVector(startX, startY); //color randCol = color(int(random(0, 255)), int(random(0, 255)), int(random(0, 255))); v2d_selected.add(v2_p); int west, east; while (!v2d_selected.isEmpty () ) { //&& q.size() < 500) { v2_p = v2d_selected.removeFirst(); if (isToSelect(int(v2_p.x), int(v2_p.y), pxl, pw, ph, orgColor)) { west = east = int(v2_p.x); while (isToSelect(--west, int(v2_p.y), pxl, pw, ph, orgColor)); while (isToSelect(++east, int(v2_p.y), pxl, pw, ph, orgColor)); for (int x = west + 1; x < east; x++) { v2d_points.add(new PVector(x, int(v2_p.y))); pxl[x + int(v2_p.y) * pw] = color(188, 188, 188); //important for not selecting the same area more than once if (isToSelect(x, int(v2_p.y) - 1, pxl, pw, ph, orgColor)) v2d_selected.add(new PVector(x, int(v2_p.y) - 1)); if (isToSelect(x, int(v2_p.y) + 1, pxl, pw, ph, orgColor)) v2d_selected.add(new PVector(x, v2_p.y + 1)); } } } i_size = v2d_points.size(); PVector avgPoint = new PVector(0, 0); for (PVector point : v2d_points) { avgPoint.add(point); } avgPoint.x = avgPoint.x/i_size; avgPoint.y = avgPoint.y/i_size; v2_center = avgPoint; if (i_size > i_biggestHouseSize) i_biggestHouseSize = i_size; } }