Advertisement
Guest User

CK2AutoBorder2

a guest
Aug 24th, 2016
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.41 KB | None | 0 0
  1. //DESCRIPTION:
  2. //This program (for Processing 3: http://processing.org) adds black, two-pixel (or one-pixel) borders between any two countries, but not between a country and water or between a country and wasteland.
  3. //It's been tested to work on any automatically-exported map from Crusader Kings II, Europa Universalis IV, or Victoria II (after that map has been converted from BMP to PNG format, if necessary), as well as on PNG-format province maps.
  4. //It also presumably works on maps exported from other games, but I haven't gotten around to testing them yet, since those three are the only ones that I play.
  5.  
  6. //INSTRUCTIONS:
  7. //1. Make sure that ONLY the RELEVANT color settings below are active. If you're using a CK2 map, comment out the EU4 and V2 lines; if you're using an EU4 map, comment out the CK2 and V2 lines; etc.
  8. //2. Rename your screenshot to "map.png" and copy it into a folder named "data" in the same directory as this program.
  9. //3. Run the program and wait for a few seconds.
  10. //4. Open the "output" folder in the same directory as this program to see the final result!
  11.  
  12. PImage map0;
  13. //color water=color(51,67,85);//CK2
  14. //color wasteland=color(38,38,38);//CK2
  15. //color uncolonized=color(38,38,38);//CK2
  16. color water=color(68,107,163);//EU4
  17. color wasteland=color(94,94,94);//EU4
  18. color uncolonized=color(150,150,150);//EU4
  19. //color water=color(255,255,255);//V2
  20. //color wasteland=color(0,0,0);//V2--not applicable
  21. //color uncolonized=color(0,0,0);//V2
  22.  
  23. int expand=0;//Set to 0 if you don't want colors to bleed into the wasteland (recommended for V2)
  24. int wastelandBorders=0;//Set to 0 if you don't want black borders to appear where wastelands would be (meant for disputed "borders" in the middle of a desert--doesn't work too well with narrow mountain ranges)
  25. int borderWidth=3;//Set to the desired border width, in pixels
  26.  
  27. //-----------------------------------------------------------------------------
  28. //-----------------------------------------------------------------------------
  29. //-----------------------------------------------------------------------------
  30.  
  31. void setup(){
  32.   map0=loadImage("map.png");//Actually "data/map.png"
  33.   map0.loadPixels();
  34. }
  35.  
  36. void draw(){
  37.   //1. Load pixels
  38.   color[][] mapPixels=new color[map0.width][map0.height];
  39.   color[][] mapPixels2=new color[map0.width][map0.height];
  40.   for(int x=0;x<map0.width;x++){
  41.     for(int y=0;y<map0.height;y++){
  42.       mapPixels[x][y]=map0.pixels[x+y*map0.width];
  43.       mapPixels2[x][y]=map0.pixels[x+y*map0.width];
  44.     }
  45.   }
  46.   //1.5. If the uncolonized-land color is black, change it to EU4's gray. Black land interferes with borders.
  47.   if(uncolonized==color(0,0,0)){
  48.     for(int x=0;x<map0.width;x++){
  49.       for(int y=0;y<map0.height;y++){
  50.         if(mapPixels[x][y]==uncolonized){
  51.           mapPixels[x][y]=color(150,150,150);
  52.           mapPixels2[x][y]=color(150,150,150);
  53.         }
  54.       }
  55.     }
  56.     uncolonized=color(150,150,150);
  57.   }
  58.   //2. Expand color into wastelands
  59.   color[][] mapPixels3=new color[map0.width][map0.height];//Preserves original wastelands
  60.   for(int x=0;x<map0.width;x++){
  61.     for(int y=0;y<map0.height;y++){
  62.       mapPixels3[x][y]=mapPixels[x][y];
  63.     }
  64.   }
  65.   if(expand==1){
  66.     int expansionOccurred=1;
  67.     while(expansionOccurred==1){
  68.       expansionOccurred=0;
  69.       for(int x=0;x<map0.width;x++){
  70.         for(int y=0;y<map0.height;y++){
  71.           if(mapPixels[x][y]==wasteland){
  72.             if(x+1<map0.width&&mapPixels[x+1][y]!=wasteland&&mapPixels[x+1][y]!=water){//000
  73.               mapPixels2[x][y]=mapPixels[x+1][y];
  74.               expansionOccurred=1;
  75.             }else if(x+1<map0.width&&y+1<map0.height&&mapPixels[x+1][y+1]!=wasteland&&mapPixels[x+1][y+1]!=water){//045
  76.               mapPixels2[x][y]=mapPixels[x+1][y+1];
  77.               expansionOccurred=1;
  78.             }else if(y+1<map0.height&&mapPixels[x][y+1]!=wasteland&&mapPixels[x][y+1]!=water){//090
  79.               mapPixels2[x][y]=mapPixels[x][y+1];
  80.               expansionOccurred=1;
  81.             }else if(x-1>-1&&y+1<map0.height&&mapPixels[x-1][y+1]!=wasteland&&mapPixels[x-1][y+1]!=water){//135
  82.               mapPixels2[x][y]=mapPixels[x-1][y+1];
  83.               expansionOccurred=1;
  84.             }else if(x-1>-1&&mapPixels[x-1][y]!=wasteland&&mapPixels[x-1][y]!=water){//180
  85.               mapPixels2[x][y]=mapPixels[x-1][y];
  86.               expansionOccurred=1;
  87.             }else if(x-1>-1&&y-1>-1&&mapPixels[x-1][y-1]!=wasteland&&mapPixels[x-1][y-1]!=water){//225
  88.               mapPixels2[x][y]=mapPixels[x-1][y-1];
  89.               expansionOccurred=1;
  90.             }else if(y-1>-1&&mapPixels[x][y-1]!=wasteland&&mapPixels[x][y-1]!=water){//270
  91.               mapPixels2[x][y]=mapPixels[x][y-1];
  92.               expansionOccurred=1;
  93.             }else if(x+1<map0.width&&y-1>-1&&mapPixels[x+1][y-1]!=wasteland&&mapPixels[x+1][y-1]!=water){//315
  94.               mapPixels2[x][y]=mapPixels[x+1][y-1];
  95.               expansionOccurred=1;
  96.             }
  97.           }
  98.         }
  99.       }
  100.       for(int x=0;x<map0.width;x++){
  101.         for(int y=0;y<map0.height;y++){
  102.           mapPixels[x][y]=mapPixels2[x][y];
  103.         }
  104.       }
  105.     }
  106.     for(int x=0;x<map0.width;x++){
  107.       for(int y=0;y<map0.height;y++){
  108.         if(mapPixels[x][y]==wasteland){
  109.           mapPixels[x][y]=water;
  110.           mapPixels2[x][y]=water;
  111.         }
  112.         map0.pixels[x+y*map0.width]=mapPixels[x][y];
  113.       }
  114.     }
  115.     map0.updatePixels();
  116.     map0.save("output/Expanded.png");
  117.   }
  118.   //3. First stage: Either one-pixel or two-pixel borders, depending on whether borderWidth is odd or even
  119.   int borderWidthNow=0;//Tracks current border width (as opposed to desired border width, the borderWidth variable)
  120.   color[][] mapPixels4=new color[map0.width][map0.height];//For the second border
  121.   for(int x=0;x<map0.width;x++){
  122.     for(int y=0;y<map0.height;y++){
  123.       mapPixels4[x][y]=mapPixels2[x][y];
  124.     }
  125.   }
  126.   //One-pixel border: Checks east and north, then checks northeast
  127.   for(int x=0;x<map0.width;x++){
  128.     for(int y=0;y<map0.height;y++){
  129.       if(mapPixels[x][y]!=wasteland&&mapPixels[x][y]!=water&&mapPixels[x][y]!=uncolonized&&(wastelandBorders==1||mapPixels3[x][y]!=wasteland)){
  130.         if(x+1<map0.width&&mapPixels[x+1][y]!=mapPixels[x][y]&&mapPixels[x+1][y]!=wasteland&&mapPixels[x+1][y]!=water&&mapPixels[x+1][y]!=uncolonized){
  131.           mapPixels2[x][y]=color(0,0,0);
  132.         }else if(y-1>-1&&mapPixels[x][y-1]!=mapPixels[x][y]&&mapPixels[x][y-1]!=wasteland&&mapPixels[x][y-1]!=water&&mapPixels[x][y-1]!=uncolonized){
  133.           mapPixels2[x][y]=color(0,0,0);
  134.         }
  135.       }
  136.     }
  137.   }
  138.   for(int x=0;x<map0.width;x++){
  139.     for(int y=0;y<map0.height;y++){
  140.       if(mapPixels[x][y]!=wasteland&&mapPixels[x][y]!=water&&mapPixels[x][y]!=uncolonized&&(wastelandBorders==1||mapPixels3[x][y]!=wasteland)){
  141.         if(x+1<map0.width&&y-1>-1&&mapPixels2[x+1][y]==color(0,0,0)&&mapPixels2[x+1][y-1]!=color(0,0,0)&&mapPixels2[x][y-1]==color(0,0,0)){
  142.           mapPixels2[x][y]=color(0,0,0);
  143.         }
  144.       }
  145.     }
  146.   }
  147.   borderWidthNow++;
  148.   if(borderWidth%2==0){//Two-pixel addition: Checks west and south, then checks southwest
  149.     for(int x=0;x<map0.width;x++){
  150.       for(int y=0;y<map0.height;y++){
  151.         if(mapPixels[x][y]!=wasteland&&mapPixels[x][y]!=water&&mapPixels[x][y]!=uncolonized&&(wastelandBorders==1||mapPixels3[x][y]!=wasteland)){
  152.           if(x-1>-1&&mapPixels[x-1][y]!=mapPixels[x][y]&&mapPixels[x-1][y]!=wasteland&&mapPixels[x-1][y]!=water&&mapPixels[x-1][y]!=uncolonized){
  153.             mapPixels4[x][y]=color(0,0,0);
  154.           }else if(y+1<map0.height&&mapPixels[x][y+1]!=mapPixels[x][y]&&mapPixels[x][y+1]!=wasteland&&mapPixels[x][y+1]!=water&&mapPixels[x][y+1]!=uncolonized){
  155.             mapPixels4[x][y]=color(0,0,0);
  156.           }
  157.         }
  158.       }
  159.     }
  160.     for(int x=0;x<map0.width;x++){
  161.       for(int y=0;y<map0.height;y++){
  162.         if(mapPixels[x][y]!=wasteland&&mapPixels[x][y]!=water&&mapPixels[x][y]!=uncolonized&&(wastelandBorders==1||mapPixels3[x][y]!=wasteland)){
  163.           if(x-1>-1&&y+1<map0.height&&mapPixels2[x-1][y]==color(0,0,0)&&mapPixels2[x-1][y+1]!=color(0,0,0)&&mapPixels2[x][y+1]==color(0,0,0)){
  164.             mapPixels4[x][y]=color(0,0,0);
  165.           }
  166.         }
  167.       }
  168.     }
  169.     borderWidthNow++;
  170.   }
  171.   //Copy second pixel of borders
  172.   for(int x=0;x<map0.width;x++){
  173.     for(int y=0;y<map0.height;y++){
  174.       if(mapPixels4[x][y]==color(0,0,0)){
  175.         mapPixels2[x][y]=color(0,0,0);
  176.       }
  177.     }
  178.   }
  179.   for(int x=0;x<map0.width;x++){
  180.     for(int y=0;y<map0.height;y++){
  181.       mapPixels[x][y]=mapPixels2[x][y];
  182.       map0.pixels[x+y*map0.width]=mapPixels[x][y];
  183.     }
  184.   }
  185.   //Second stage: Adding more pixels in layers of 2
  186.   while(borderWidthNow<borderWidth){
  187.     for(int x=0;x<map0.width;x++){
  188.       for(int y=0;y<map0.height;y++){
  189.         if(mapPixels[x][y]!=wasteland&&mapPixels[x][y]!=water&&mapPixels[x][y]!=uncolonized&&(wastelandBorders==1||mapPixels3[x][y]!=wasteland)){
  190.           if((x+1<map0.width&&mapPixels[x+1][y]==color(0,0,0))||(y+1<map0.height&&mapPixels[x][y+1]==color(0,0,0))||(x-1>-1&&mapPixels[x-1][y]==color(0,0,0))||(y-1>-1&&mapPixels[x][y-1]==color(0,0,0))){
  191.             mapPixels2[x][y]=color(0,0,0);
  192.           }
  193.         }
  194.       }
  195.     }
  196.     for(int x=0;x<map0.width;x++){
  197.       for(int y=0;y<map0.height;y++){
  198.         mapPixels[x][y]=mapPixels2[x][y];
  199.         map0.pixels[x+y*map0.width]=mapPixels[x][y];
  200.       }
  201.     }
  202.     borderWidthNow+=2;
  203.   }
  204.   map0.updatePixels();
  205.   map0.save(savePath("output/Bordered.png"));
  206.   exit();
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement