Simulation von Bürgern
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Map.pde 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import java.util.ArrayDeque;
  2. import pathfinder.*;
  3. PImage img_map;
  4. PImage img_houses;
  5. PImage img_streets;
  6. PGraphics pg_map;
  7. int i_mapOffsetX, i_mapOffsetY, i_mapOffsetStartX, i_mapOffsetStartY;
  8. int i_biggestHouseSize;
  9. int i_mapW, i_mapH;
  10. ArrayList<House> houses = new ArrayList<House>();
  11. void initHouses() {
  12. s_map = new String[img_houses.height/i_sMapReso];
  13. for (int y = 0; y < s_map.length; y++) {
  14. s_map[y] = str('.'); //because += char on string delivers "null" as first stringentry in each arrayentry?!
  15. for (int x = 0; x < img_houses.width/i_sMapReso; x++) {
  16. s_map[y]+= '.';
  17. }
  18. }
  19. img_houses.loadPixels();
  20. for (int x = 0; x < img_houses.width; x++) {
  21. for (int y = 0; y < img_houses.height; y++) {
  22. 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
  23. houses.add(new House(x, y));
  24. img_houses.updatePixels();
  25. img_houses.loadPixels();
  26. }
  27. }
  28. }
  29. img_houses.updatePixels();
  30. for (House house : houses) {
  31. //give houses their respective relative size (biggest house = 100)
  32. house.i_size = (int)map(house.i_size, 0, i_biggestHouseSize, 0, 100);
  33. //create ascii map in s_map
  34. for (PVector point : house.v2d_points) { //create ascii map in s_map
  35. s_map[int(point.y/i_sMapReso)] = replaceCharAt_v1(s_map[int(point.y/i_sMapReso)], int(point.x/i_sMapReso), '#');
  36. }
  37. }
  38. saveStrings(dataPath("server/map.txt"), s_map);
  39. }
  40. String replaceCharAt_v1(String s, int pos, char c) {
  41. char[] ch = new char[s.length()];
  42. s.getChars(0, ch.length, ch, 0);
  43. ch[pos] = c;
  44. return new String(ch);
  45. }
  46. class House {
  47. PVector v2_center;
  48. int i_size;
  49. int i_ID;
  50. void display() {
  51. pg_map.text("ID "+i_ID, v2_center.x, v2_center.y);
  52. pg_map.text("Size : " + i_size,v2_center.x,v2_center.y+10);
  53. }
  54. ArrayDeque<PVector> v2d_selected = new ArrayDeque<PVector>();
  55. ArrayList<PVector> v2d_points = new ArrayList<PVector>();
  56. boolean isToSelect(int px, int py, int[] pxl, int pw, int ph, int orgColor) {
  57. if (px < 0 || px >= pw || py < 0 || py >= ph)
  58. return false;
  59. return pxl[px + py * pw] == orgColor;
  60. }
  61. PVector v2_randomSpawnPoint() {
  62. return v2d_points.get(int(random(0, v2d_points.size())));
  63. }
  64. House(int startX, int startY) {
  65. i_ID = houses.size();
  66. int [] pxl = img_houses.pixels;
  67. int pw = img_houses.width;
  68. int ph = img_houses.height;
  69. int orgColor = pxl[startX + startY * pw];
  70. PVector v2_p = new PVector(startX, startY);
  71. //color randCol = color(int(random(0, 255)), int(random(0, 255)), int(random(0, 255)));
  72. v2d_selected.add(v2_p);
  73. int west, east;
  74. while (!v2d_selected.isEmpty () ) { //&& q.size() < 500) {
  75. v2_p = v2d_selected.removeFirst();
  76. if (isToSelect(int(v2_p.x), int(v2_p.y), pxl, pw, ph, orgColor)) {
  77. west = east = int(v2_p.x);
  78. while (isToSelect(--west, int(v2_p.y), pxl, pw, ph, orgColor));
  79. while (isToSelect(++east, int(v2_p.y), pxl, pw, ph, orgColor));
  80. for (int x = west + 1; x < east; x++) {
  81. v2d_points.add(new PVector(x, int(v2_p.y)));
  82. pxl[x + int(v2_p.y) * pw] = color(188, 188, 188); //important for not selecting the same area more than once
  83. if (isToSelect(x, int(v2_p.y) - 1, pxl, pw, ph, orgColor))
  84. v2d_selected.add(new PVector(x, int(v2_p.y) - 1));
  85. if (isToSelect(x, int(v2_p.y) + 1, pxl, pw, ph, orgColor))
  86. v2d_selected.add(new PVector(x, v2_p.y + 1));
  87. }
  88. }
  89. }
  90. i_size = v2d_points.size();
  91. PVector avgPoint = new PVector(0, 0);
  92. for (PVector point : v2d_points) {
  93. avgPoint.add(point);
  94. }
  95. avgPoint.x = avgPoint.x/i_size;
  96. avgPoint.y = avgPoint.y/i_size;
  97. v2_center = avgPoint;
  98. if (i_size > i_biggestHouseSize) i_biggestHouseSize = i_size;
  99. }
  100. }