Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // TODO:
- /*
- -Fix the border overlay system
- -Make the map dynamically resize to match the window(?)
- -Add zoom levels?
- -Add an actual territory class
- -Make a player class that controls territories
- -Add a sense of time (turns or steps?)
- -Concept of selecting a territory
- */
- PImage gameMap;
- color WHITE = color(255);
- color BLACK = color(0);
- color MAGENTA = color(255,0,255);
- color BLUE_L = color(120, 120, 255);
- color PINK_L = color(200,50,200);
- boolean isResized = false;
- boolean randomizePerSecond = false;
- boolean showDebug = true;
- double delta = 0;
- void settings(){
- size(640,400);
- noSmooth();
- }
- FrameBuffer buffer = new FrameBuffer(200);
- void setup(){
- colorMode(HSB, 360, 100, 100);
- textSize(10);
- fill(WHITE);
- surface.setResizable(true);
- String imageName = "map-us-edit.png";
- m = new Map(imageName);
- if(m.rawMap != null){
- m.discoverTerritories();
- gameMap = m.map.copy();
- } else {
- println("Failed to find " + "\""+imageName+"\"");
- exit();
- }
- }
- void draw(){
- frameRate(144);
- delta = System.nanoTime();
- resizeWindowOnce();
- m.update();
- m.display();
- drawDebugText();
- printFrameTime(delta);
- }
- void drawDebugText(){
- if(showDebug){
- text("FPS: "+ frameRate, 1,10);
- text("Randomize?: "+ randomizePerSecond, 1,30);
- text("FrameBuffer: "+ buffer.queryState(), 1,40);
- }
- }
- void resizeWindowOnce(){
- if(!isResized){
- surface.setSize(gameMap.width, gameMap.height);
- isResized = true;
- }
- }
- class FrameBuffer{
- ArrayList<PImage> frames = new ArrayList<PImage>();
- boolean shouldCaptureFrames = true;
- boolean hasDumped = false;
- int maxCapacity;
- int saveCounter;
- boolean dumpInProgress = false;
- FrameBuffer(int _maxCapacity){
- maxCapacity = _maxCapacity;
- }
- void dump(){
- if(frames.size() == 0){
- println("Attempted to dump buffer, but it was empty.");
- } else {
- for(int n = 0; n < frames.size(); n++){
- frames.get(n).save("f-"+ n +".png");
- }
- println("Dumped "+frames.size() +" images.");
- hasDumped = true;
- frames.clear();
- }
- }
- void progressiveDump(){
- if(dumpInProgress){
- if(frames.size() == 0){
- println("Dump finished - "+ saveCounter +" frames saved to disk.");
- dumpInProgress = false;
- saveCounter = 0;
- return;
- } else {
- frames.get(0).save("f-"+ (saveCounter) +".png");
- //println(frames.size());
- //println(frames.size() - saveCounter);
- println(saveCounter);
- saveCounter++;
- frames.remove(0);
- }
- }
- }
- String queryState(){
- if(hasDumped){
- return "Flushed";
- } else {
- return str(frames.size());
- }
- }
- boolean addFrame(PImage image){
- hasDumped = false;
- if(frames.size() < maxCapacity){
- frames.add(image);
- return true;
- } else {
- return false;
- }
- }
- }
- class ImageDumpThread extends Thread{
- FrameBuffer buffer;
- ImageDumpThread(FrameBuffer _buffer){
- buffer = _buffer;
- }
- void run(){
- println("ImageDumpThread started.");
- buffer.dump();
- println("ImageDumpThread finished");
- }
- }
- //Not in use, but random opacity overlays for texture should be considered
- PImage generateTextureEffect(PImage _map, float _frequency){
- PImage map = _map.copy();
- PImage overlay = createImage(map.width, map.height, ARGB);
- overlay.loadPixels();
- for(int i = 0; i < overlay.pixels.length; i++){
- if(random(1) > _frequency){
- overlay.pixels[i] = color(1,1,1,50);
- }
- }
- overlay.updatePixels();
- return overlay;
- }
- Map m;
- class Map{
- PVector pos;
- PVector dims;
- float widthMultiplier;
- float heightMultiplier;
- PImage map;
- PImage rawMap;
- IntList territoryIDs = new IntList();
- Map(String _mapImageFileName){
- rawMap = loadImage(_mapImageFileName);
- if(rawMap != null){
- pos = new PVector(0,0);
- dims = new PVector(rawMap.width, rawMap.height);
- println("Map dimensions: "+rawMap.width+","+rawMap.height);
- } else {
- exit();
- }
- }
- void discoverTerritories(){
- rawMap.loadPixels();
- int maxAttemps = 1;
- int lastIndex = 0;
- int steps = 0;
- int fillEvents = 0;
- nextTerritory();
- for(int n = 0; n < maxAttemps; n++){
- for(int i = lastIndex; i < rawMap.pixels.length; i++){
- steps++;
- if(validTarget(i)){
- floodFill(rawMap, i%rawMap.width, floor(i/rawMap.width), currentTerritory());
- nextTerritory();
- lastIndex = i;
- fillEvents++;
- if(buffer.shouldCaptureFrames){
- buffer.addFrame(rawMap.copy());
- }
- }
- }
- }
- rawMap.updatePixels();
- map = rawMap.copy();
- println("Pixels checked: "+ steps +"/"+ rawMap.pixels.length);
- println("Fill Events: "+ fillEvents);
- }
- void nextTerritory(){
- //territoryIDs.append(randomColor(200,255));
- //territoryIDs.append(color(random(360),random(60,80),random(60,80)));
- territoryIDs.append(color(random(360),random(60,70),90));
- }
- int currentTerritory(){
- return territoryIDs.get(territoryIDs.size()-1);
- }
- boolean validTarget(int t){
- if(rawMap.pixels[t] == BLACK){
- return false;
- }
- if(territoryIDs.hasValue(rawMap.pixels[t])){
- return false;
- }
- return true;
- }
- void colorReplace(int _tx, int _ty, color newColor){
- int tx = transformMouseX(_tx);
- int ty = transformMouseY(_ty);
- if(!isPointOutOfBounds(tx, ty)){
- rawMap.loadPixels();
- if(rawMap.pixels[tx+ty*rawMap.width] != BLACK){
- rawMap = floodFill(rawMap, tx, ty, newColor);
- }
- rawMap.updatePixels();
- map = rawMap.copy();
- }
- }
- void randomize(){
- territoryIDs.clear();
- discoverTerritories();
- }
- void printMapDims(){
- println("Map dimensions: "+rawMap.width+","+rawMap.height);
- }
- boolean isPointOutOfBounds(int x, int y){
- if(x < 0 || y < 0 || x >= map.width || y >= map.height){
- return true;
- } else {
- return false;
- }
- }
- boolean colorPick(int x, int y){
- int tx = transformMouseX(x);
- int ty = transformMouseY(y);
- if(!isPointOutOfBounds(tx, ty)){
- if(m.territoryIDs.hasValue(m.map.get(tx, ty))){
- currentColor = m.map.get(tx, ty);
- return true;
- }
- }
- return false;
- }
- void randomizePerSecond(){
- if(randomizePerSecond){
- if(frameCount % 144 == 1){
- randomize();
- }
- }
- }
- void updateMultipliers(int newWidth, int newHeight){
- widthMultiplier = (float)map.width/newWidth;
- heightMultiplier = (float)map.height/newHeight;
- }
- int transformMouseX(int mX){
- return floor((float)mX * widthMultiplier);
- }
- int transformMouseY(int mY){
- return floor((float)mY * heightMultiplier);
- }
- void update(){
- randomizePerSecond();
- updateMultipliers(width, height);
- }
- void display(){
- image(map, pos.x, pos.y, width, height);
- }
- }
- PImage floodFill(PImage image, int sr, int sc, int newColor){
- if(image.pixels[sr+sc*image.width] == newColor) return image;
- Fill(image, sr, sc, image.pixels[sr+sc*image.width], newColor);
- return image;
- }
- void Fill(PImage image, int sr, int sc, int curColor, int newColor){
- if(sr < 0 || sc < 0 || sr >= image.width || sc >= image.height || image.pixels[sr+sc*image.width] != curColor){
- return;
- }
- image.pixels[sr+sc*image.width] = newColor;
- Fill(image, sr-1, sc, curColor, newColor);
- Fill(image, sr+1, sc, curColor, newColor);
- Fill(image, sr, sc-1, curColor, newColor);
- Fill(image, sr, sc+1, curColor, newColor);
- }
- void mouseMoved(){
- if(mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height){
- }
- }
- color currentColor = BLUE_L;
- void mousePressed(){
- int mX = mouseX;
- int mY = mouseY;
- //println("Mouse: "+ mX + "," + mY);
- if(mouseButton == LEFT){
- m.colorReplace(mX, mY, currentColor);
- }
- if(mouseButton == RIGHT){
- m.colorPick(mouseX, mouseY);
- }
- }
- void mouseDragged(){
- if(mouseButton == LEFT){
- m.colorReplace(mouseX, mouseY, currentColor);
- }
- }
- void keyReleased(KeyEvent event){
- int e = event.getKey();
- if(e == '1'){ showDebug = !showDebug; }
- if(e == '2'){ randomizePerSecond = !randomizePerSecond; }
- //if(e == 'p'){ buffer.dumpInProgress = true; }
- if(e == 'p'){
- new ImageDumpThread(buffer).start();
- }
- if(e == 'd'){
- m.printMapDims();
- }
- }
- color randomColor(int min, int max){
- return color(int(random(min,max)),int(random(min,max)),int(random(min,max)));
- }
- void printFrameTime(double delta){
- delta -= System.nanoTime();
- if(showDebug){
- text("MS: "+ abs((float)(delta*0.000001)), 1,20);
- }
- }
Add Comment
Please, Sign In to add comment