Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (592)
Games in Android Showcase (168)
games submitted by our members
Games in WIP (645)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1] 2 3 ... 11
1  Game Development / Newbie & Debugging Questions / Re: Iterative flood fill on: 2015-06-30 08:56:45
Cheers Jesse,

Couldn't have done it without your help!

Looking to do some lighting next...not sure of which way to go with this though, looked at using a shader.

Thanks
2  Game Development / Newbie & Debugging Questions / Re: Iterative flood fill on: 2015-06-29 13:58:49
Iterative flood fill working a treat and so much faster to render the world with!

Please see here for latest screen shot:

https://sites.google.com/site/sterrialand/development/news/iterativefloodfillimplemented

The code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
package com.mygdx.game;

import java.awt.Point;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class FloodFill {

   BlankEntity[][] map;

   private CaveCell cell;

   public ArrayList<CaveCell> caveCells = new ArrayList<CaveCell>();

   public FloodFill() {
   }

   public FloodFill(BlankEntity[][] map) {
      this.map = map;
   }

   /*
    * Recursive flood fill - CANNOT handle large caves
    */

   public void fill(int x, int y) {

      try {
         if (x < 0)
            return;
         if (y < 0)
            return;
         if (x >= map.length)
            return;
         if (y >= map[x].length)
            return;

         BlankEntity currentTile = map[x][y]; // get current tile
         if (currentTile == null)
            return;

         if (currentTile.visited)
            return;

         if (!(currentTile instanceof CaveEntity))
            return;

         currentTile.visited = true;

         cell = new CaveCell(x, y);
         caveCells.add(cell);

         fill(x - 1, y);
         fill(x + 1, y);
         fill(x, y - 1);
         fill(x, y + 1);
      } catch (Exception ex) {
         System.out.println(":-( Cave too large");
      }
   }

   
    /*
     * Iterative Flood fill - stops stack overflow :-)
     */

   public void iterativeFill(int initialX, int initialY) {
      Queue<Point> points = new LinkedList<Point>();
      points.add(new Point(initialX, initialY));
      caveCells = new ArrayList<CaveCell>();
     
      while (!points.isEmpty()) {
         Point currentPoint = points.remove();
         int x = currentPoint.x;
         int y = currentPoint.y;

         BlankEntity currentTile = map[x][y]; // get current tile
   
         if(currentTile instanceof CaveEntity && !currentTile.visited)
         {
            cell = new CaveCell(x, y);
            caveCells.add(cell);
            currentTile.visited = true;
           
            if (x < map.length - 1 )
               points.add(new Point(x + 1, y));
            if (x > 0)
               points.add(new Point(x - 1, y));
            if (y < map[x].length -1)
               points.add(new Point(x, y + 1));
            if (y > 0)
               points.add(new Point(x, y - 1));
         }
      }
   }
}


class CaveCell {
   public int x, y;

   public CaveCell(int x, int y) {
      this.x = x;
      this.y = y;
   }

   @Override
   public String toString() {
      return "X:" + x + " Y:" + y;
   }
}

class Cave {
   public Cave() {
      cells = new ArrayList<CaveCell>();
   }

   public ArrayList<CaveCell> cells;
}


3  Game Development / Newbie & Debugging Questions / Re: Iterative flood fill on: 2015-06-25 16:31:53
Thanks Riven,

Makes sense :-)
4  Game Development / Newbie & Debugging Questions / Re: Iterative flood fill on: 2015-06-24 09:01:46
Hi,

Yeah, seems to work, gives me a list of caves - this is used to allow water to be added to the caves later.

Been on hols for 2 weeks so only just getting back to this!

No caves are the same due to the visited property :-)

Thanks
5  Game Development / Newbie & Debugging Questions / Re: Iterative flood fill on: 2015-06-23 21:56:25
Well, this is what I have for an iterative flood fill, does this look correct?...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
public void iterativeFill(int initialX, int initialY) {
      Queue<Point> points = new LinkedList<Point>();
      points.add(new Point(initialX, initialY));
      caveCells = new ArrayList<CaveCell>();
     
      while (!points.isEmpty()) {
         Point currentPoint = points.remove();
         int x = currentPoint.x;
         int y = currentPoint.y;

         BlankEntity currentTile = mm[x][y]; // get current tile
   
         if(currentTile instanceof CaveEntity && !currentTile.visited)
         {
            cell = new CaveCell(x, y);
            caveCells.add(cell);
            currentTile.visited = true;
           
            if (x < mm.length - 1 )
               points.add(new Point(x + 1, y));
            if (x > 0)
               points.add(new Point(x - 1, y));
            if (y < mm[x].length -1)
               points.add(new Point(x, y + 1));
            if (y > 0)
               points.add(new Point(x, y - 1));
         }
      }

   }


Where caveCells is an ArrayList of cells for particular cave.  The above is called as:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
FloodFill f = new FloodFill();

      for (int x = 0; x < mm.length; x++) {
         for (int y = 0; y < mm[x].length; y++) {
            f.iterativeFill(x, y);
            ArrayList<CaveCell> cavec = FloodFill.caveCells;
            if (caveCells.size() > 0) {
               Cave c = new Cave();
               for (CaveCell cell : caveCells) {
                  c.cells.add(cell);
               }

               caves.add(c);
               System.out.println("Added to caves arraylist.\n");
            }
         }
      }


Thanks
6  Game Development / Newbie & Debugging Questions / Re: Iterative flood fill on: 2015-06-14 21:30:04
Thanks Jesse,

Yes, using the Point object so can get x,y incremented.  I've got this now:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
   public void iterativeFill(int initialX, int initialY, char target, char replacement){
       Queue<Point> points = new LinkedList<Point>();
       points.add(new Point(initialX, initialY));

       while(!points.isEmpty()) {
           Point currentPoint = points.remove();
           int x = currentPoint.x;
           int y = currentPoint.y;

           char current = m[x][y];
           if((current == target)){
               m[x][y] = replacement;
               if(x< m.length - 1)
                  points.add(new Point(x+1, y));
               if(x>0)
                  points.add(new Point(x-1, y));
                    if(y<m[x].length)
                   points.add(new Point(x, y+1));
               if(y>0)
                  points.add(new Point(x, y-1));
           }
       }
   }


Will need to put in the usual visited boolean like had before and then a loop:

1  
2  
3  
      for (int x = 0; x < m.length; x++) 
         for (int y = 0; y < m[x].length; y++)
           fill.IterativeFill(x,y,'c','w');


Think this could work, doing it this way before changing it to the 2d array in my game which you know is of BlankEntity objects.
7  Game Development / Newbie & Debugging Questions / Re: Iterative flood fill on: 2015-06-14 21:11:29
Did try something like this using Point object:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
public void BoundaryFill(int initialX, int initialY, char target, char replacement){
       Stack<Point> points = new Stack<Point>();
       points.add(new Point(initialX, initialY));

       while(!points.isEmpty()) {
           Point currentPoint = points.pop();
           int x = currentPoint.x;
           int y = currentPoint.y;

           char current = m[x][y];
           if((current == target)){
               m[x][y] = replacement;
           
               points.push(new Point(x+1, y));
               if(x>0)
                  points.push(new Point(x-1, y));
               points.push(new Point(x, y+1));
               if(y>0)
                  points.push(new Point(x, y-1));
           }
       }
   }


Still only does up to x being 2 then exits
8  Game Development / Newbie & Debugging Questions / Re: Iterative flood fill on: 2015-06-14 20:59:45
Hey Jesse,

Yes you are correct, the x & y don't change.  I'm taking it you would call this method once and it would do the whole array? 

Thus fill(0,0,'c','w'); 

is all that would be needed?

Thanks
9  Game Development / Newbie & Debugging Questions / Iterative flood fill isn't correct - thought it was working?! on: 2015-06-14 20:40:55
Hi,

I thought my iterative flood fill was working ok - obviously just a red herrin!

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
private void fill(int x, int y, char target, char replace) {
      if (m[x][y] == replace)
         return; // current is same as node - 1

      Queue<Character> q = new LinkedList();

      q.add(m[x][y]);

      while (!q.isEmpty()) {

         q.remove();

         if (m[x][y] != replace && m[x][y] == target)
         {
            m[x][y] = replace; //

            if (x < m.length - 1)
               if (m[x + 1][y] != replace)   // east
                  q.add(m[x + 1][y]);
            if (x > 0)
               if (m[x - 1][y] != replace)   // west
                  q.add(m[x - 1][y]);

            if (y < m[x].length - 1)
               if (m[x][y + 1] != replace)   // north
                  q.add(m[x][y + 1]);
            if (y > 0)
               if (m[x][y - 1] != replace)   // south
                  q.add(m[x][y - 1]);
         }
      }
   }


The above code can just be:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
private void fill(int x, int y, char target, char replace) {
      if (m[x][y] == replace)
         return; // current is same as node - 1

      Queue<Character> q = new LinkedList();

      q.add(m[x][y]);

      while (!q.isEmpty()) {

         q.remove();

         if (m[x][y] != replace && m[x][y] == target)
            m[x][y] = replace;
      }
}



1  
2  
3  
4  
5  
6  
7  
   public static void main(String[] args) {
      FloodFill f = new FloodFill();
      for (int x = 0; x < m.length; x++)
         for (int y = 0; y < m[x].length; y++)
            f.fill(x, y, 'c', 'w');
                       
               


Where m is a 2d array of chars:

1  
2  
3  
4  
5  
6  
7  
   static char[][] m = { 
          { 'c', 'x', 'x', 'c', 'c' },
         { 'c', 'x', 'x', 'c', 'x' },
         { 'x', 'c', 'c', 'c', 'x' },
         { 'x', 'c', 'x', 'x', 'c' },
         { 'x', 'c', 'c', 'c', 'c' },
   };


The queue is doing nothing...anything obvious wrong?  All the code I've written is just replacing the character c with W.

Thanks.
10  Game Development / Newbie & Debugging Questions / Re: Adding water tiles on: 2015-06-12 12:15:47
Got iterative flood fill to work with some test 2d char array but in my game code, only ever produces 3 cells for each cave?

Here is the fill method:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
   public void iterativeFill(int x, int y) {
      BlankEntity currentTile = map[x][y]; // get current tile
      if (currentTile == null)
         return;

      if (currentTile.visited)
         return;

      if (!(currentTile instanceof CaveEntity))
         return;

      currentTile.visited = true;

      Queue<BlankEntity> q = new LinkedList();

      q.add(map[x][y]);

      while (!q.isEmpty()) {

         BlankEntity e = q.remove();

         cell = new CaveCell(e.x, e.y);

         caveCells.add(cell);

         if (x < map.length - 1)
            if (map[x + 1][y] instanceof CaveEntity) {
               if (!map[x + 1][y].visited) {
                  q.add(map[x + 1][y]);
                  map[x + 1][y].visited = true;
               }
            }

         if (x > 0)
            if (map[x - 1][y] instanceof CaveEntity) {

               if (!map[x - 1][y].visited) {
                  q.add(map[x - 1][y]);
                  map[x - 1][y].visited = true;
               }
            }

         if (y < map[x].length - 1)
            if (map[x][y + 1] instanceof CaveEntity) {
               if (!map[x][y + 1].visited) {
                  q.add(map[x][y + 1]);
                  map[x][y + 1].visited = true;
               }
            }

         if (y > 0)
            if (map[x][y - 1] instanceof CaveEntity) {
               if (!map[x][y - 1].visited) {
                  q.add(map[x][y - 1]);
                  map[x][y - 1].visited = true;
               }
            }
      }

   }


Called up:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
for(int x=0; x<worldMap.length;x++)
      {
         for(int y=0;y<worldMap[x].length;y++)
         {
            fFill = new FloodFill(worldMap);
            fFill.iterativeFill(x,y);
            ArrayList<CaveCell> cavec = fFill.caveCells;
            if(cavec.size() > 0)
            {
               Cave c = new Cave();
               for(CaveCell cell : cavec)
               {
                  c.cells.add(cell);
               }
               
               this.caves.add(c);
            }
         }
      }


I'm sure x and y need incrementing or something in the iterative fill method?

Any ideas?

Thanks
11  Game Development / Newbie & Debugging Questions / Re: Iterative flood fill on: 2015-06-12 10:02:43
Now works, see modifications below :-)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
package com.mygdx.game;

import java.util.LinkedList;
import java.util.Queue;

public class IterativeFloodFill {

   static char[][] m = {
         {'c','x','x','c','c' },
         {'c','x','x','c','x' },
         {'x','c','x','c','x' },
         {'x','c','x','c','c' },
         {'x','c','c','c','c'},      
   };
   
   
   private static void fill(int x, int y, char target, char replace)
   {
      if(m[x][y] == replace) return;  // current is same as node - 1
     
      Queue<Character> q = new LinkedList();
         
      q.add(m[x][y]);  
   
      while(!q.isEmpty()) {  
         
         q.remove();
         
         if( m[x][y]!= replace && m[x][y]==target) // 7
         {
            m[x][y] = replace;  //
            if(y<m[x].length-1)
            q.add(m[x][y+1]);
            if(y>0)
               q.add(m[x][y-1]);
            if(x<m.length-1)
               q.add(m[x+1][y]);            
            if(x>0)
               q.add(m[x-1][y]);
         }
      }
     
   }
   
   
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      for(int x=0; x<m.length;x++)
         for(int y=0;y<m[x].length;y++)
            fill(x,y,'c','w');

   
      for(int x=0; x<m.length;x++,System.out.println())
         for(int y=0;y<m[x].length;y++)
            System.out.print(m[x][y]);

   }

}

12  Game Development / Newbie & Debugging Questions / Re: Iterative flood fill on: 2015-06-12 09:57:53
Hi Riven,

Modified it, this look ok (see previous post)?  But as stated, cannnot start from 0 and cannot go to max width, height of array, has to be minus 1...
13  Game Development / Newbie & Debugging Questions / Iterative flood fill on: 2015-06-12 09:50:50
Hi,

I've got a recursive flood fill to create caves, but if cave large, stack overflow.  So, I've been looking at iterative using a queue:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
public class IterativeFloodFill {

   static char[][] m = {
         {'x','x','x','c','x' },
         {'x','x','x','c','x' },
         {'x','x','x','c','x' },
         {'x','x','x','c','x' },
         {'x','x','x','c','x'},      
   };
   
   
   private static void fill(int x, int y, char target, char replace)
   {
      if(m[x][y] == replace) return;

      Queue<Character> q = new LinkedList();          
      q.add(m[x][y]);  
      while(!q.isEmpty()) {  
         q.remove();
         if( m[x][y]!= replace && m[x][y]==target)
         {
            m[x][y] = replace;  
            q.add(m[x][y+1]);
            q.add(m[x][y-1]);
            q.add(m[x+1][y]);            
            q.add(m[x-1][y]);
         }
      }
   }
     
public static void main(String[] args) {
      // TODO Auto-generated method stub
      for(int x=1; x<4;x++)
         for(int y=1;y<4;y++)
            fill(x,y,'c','w');

   
      for(int x=1; x<=4;x++,System.out.println())
         for(int y=1;y<=4;y++)
            System.out.print(m[x][y]);

   }


Now the obvious problem above would give arrayIndexOutOfBounds exception as y would become -1, and so would x as I start x,y off as 0.

I'm taking it you need to start off someplace else and call the above in a forloop?

Thanks
14  Game Development / Newbie & Debugging Questions / Re: Adding water tiles on: 2015-06-12 07:30:26
Thanks Jesse, and many thanks for all the invaluable help!

Looking like I will need to modify / rewrite the flood fill to be non-recursive, as if the caves are very big, sometimes get
a stack overflow...
15  Game Development / Newbie & Debugging Questions / Re: Adding water tiles on: 2015-06-11 13:26:11
Cave generation completed.  Water added.  I just add up to around half-way for now, will change later and I also add
water to all the caves, again, this will also change later.

Screen shot here:

https://sites.google.com/site/sterrialand/development/news/finallytherewaswater

The odd-time had a stack over flow, that will be the flood fill algorithm, may look at trying to do a non-recursive one...
16  Game Development / Newbie & Debugging Questions / Re: Adding water tiles on: 2015-06-10 20:17:59
Thanks again Jesse :-)

Yeah will hopefully get this done tomorrow while at work.

So don't use floodfill to fill cave with water then, I already got highest Y and lowest Y, so guess just do what you have said.

What I was doing was floodfilling cave and setting to water then trying to remove some of the water tiles without much success!

Of course will do some screen shots of it ;-)
17  Game Development / Newbie & Debugging Questions / Re: Adding water tiles on: 2015-06-10 12:36:05
Well, think this does the trick, only done it as a test class at mo in C#, but simple to put to my Java code, what do you think?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
class Program
    {
        static List<Cave> Caves = new List<Cave>();
        static void Main(string[] args)
        {
            BlankEntity[,] m = { { new CaveEntity() ,      new CaveEntity(),       new LandscapeEntity(), new LandscapeEntity() },
                                 { new LandscapeEntity() , new LandscapeEntity() , new CaveEntity(),      new LandscapeEntity() },
                                 { new LandscapeEntity() , new LandscapeEntity() , new LandscapeEntity(),      new LandscapeEntity() },
                               };

   
            for (int y = 0; y < m.GetLength(0); y++)   // depth
            {
                for (int x = 0; x < m.GetLength(1); x++)  // width
                {
                    FloodFill fFill = new FloodFill(m);    
                    fFill.fill(y, x);                    
                   
                    var CaveC = fFill.GetCaveCells();
                    if (CaveC.Count > 0)
                    {
                        var c = new Cave();
                        foreach (CaveCell cell in CaveC)
                        {
                            c.Cells.Add(cell);
                        }
                        Caves.Add(c);
                    }
                }
            }
           
            for (int y = 0; y < m.GetLength(0); y++, Console.WriteLine())
            {
                for (int x = 0; x < m.GetLength(1); x++)
                {
                    Console.Write(m[y,x]);
                }
            }
            foreach (Cave cave in Caves)
            {
                foreach (CaveCell cell in cave.Cells)
                {
                    Console.WriteLine(cell);
                }
                Console.WriteLine("Next cave\n");
            }

        }
    }

    class BlankEntity
    {
        public int x { get; set; }
        public int y { get; set; }
        public bool Visited { get; set; }

        public override string ToString()
        {
            return " ";
        }
    }

    class LandscapeEntity : BlankEntity
    {
        public override string ToString()
        {
            return "C";
        }
    }
    class CaveEntity : BlankEntity
    {
        public override string ToString()
        {
            return "C";
        }
   

    }

    class WaterEntity : BlankEntity
    {
        public override string ToString()
        {
            return "W";
        }
    }
}


namespace ConsoleApplication8
{
    class FloodFill
    {
        BlankEntity[,] m;
     
        List<CaveCell> CaveCells = new List<CaveCell>();

        public FloodFill(BlankEntity[,] map)
        {
            this.m = map;
        }

        public List<CaveCell> GetCaveCells()
        {
            return CaveCells;
        }

        public void fill(int x, int y)
        {
            if (x < 0) return;
            if (y < 0) return;
            if (x >= m.GetLength(0)) return;
            if (y >= m.GetLength(1)) return;

            BlankEntity currentTile = m[x,y];  // get current tile
            if (m[x, y].Visited) return;
            if (!(currentTile is CaveEntity)) return; //
         
            m[x,y] = new WaterEntity();   // store x,y position
            m[x, y].Visited = true;

            CaveCell cell = new CaveCell(x, y);
            CaveCells.Add(cell);

            fill(x - 1, y);
            fill(x + 1, y);
            fill(x, y - 1);
            fill(x, y + 1);
        }
    }

    class CaveCell
    {
        public CaveCell(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
        public int x { get; set; }
        public int y { get; set; }

        public override string ToString()
        {
            return String.Format("[{0},{1}]",x,y);
        }
    }
    class Cave
    {
        public Cave() {
            Cells = new List<CaveCell>();
        }
        public List<CaveCell> Cells { get; set; }
    }
}


Just trying to work out now how I would fill these caves to a certain height - this a matter of calling floodfill again passing in a cave and then some how removing tops of the water in the cave?

Thanks
18  Game Development / Newbie & Debugging Questions / Re: Adding water tiles on: 2015-06-09 20:32:39
Yeah, think you could be right there.

Hoping to implement tomorrow, will let you know!

Thanks
19  Game Development / Newbie & Debugging Questions / Re: Adding water tiles on: 2015-06-09 10:21:46
Thanks,

Yes, not got to that yet, I think to move onto the next cave, I need the max X of previous one or something or like you state, if the cell has been visited or not.

Thanks for the pseudo, looks great to me.
20  Game Development / Newbie & Debugging Questions / Re: Adding water tiles on: 2015-06-09 09:48:35
This is what I have come up with in C#:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
namespace ConsoleApplication8
{
    class FloodFill
    {
        char[,] map;

     
        List<CaveCell> CaveCells = new List<CaveCell>();

        public FloodFill(char[,] map)
        {
            this.map = map;
        }

        public List<CaveCell> GetCaveCells()
        {
            return CaveCells;
        }

        public void fill(int x, int y, char newTile, char oldTile)
        {
            if (x < 0) return;
            if (y < 0) return;
            if (x >= map.GetLength(1)) return;
            if (y >= map.GetLength(0)) return;

            char currentTile = map[x,y];  // get current tile
            if (oldTile != currentTile) return; //
            if (newTile == currentTile) return;  // same

           // map[x,y] = newTile;   // store x,y position

            CaveCell cell = new CaveCell(x, y);
            CaveCells.Add(cell);

            fill(x - 1, y, newTile, oldTile);
            fill(x + 1, y, newTile, oldTile);
            fill(x, y - 1, newTile, oldTile);
            fill(x, y + 1, newTile, oldTile);
        }

    }

    class CaveCell
    {
        public CaveCell(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
        public int x { get; set; }
        public int y { get; set; }

        public override string ToString()
        {
            return String.Format("[{0},{1}]",x,y);
        }
    }
    class Cave
    {
        public Cave() {
            Cells = new List<CaveCell>();
        }
        public List<CaveCell> Cells { get; set; }
    }
}

class Program
    {
        static List<Cave> Caves = new List<Cave>();
        static void Main(string[] args)
        {
            char[,] map = {
           { 'O', 'O', 'O', 'O', 'X','O','O','X' },
           { 'X', 'O', 'O', 'O', 'X','O','O','X' },
           { 'X', 'O', 'O', 'O', 'X','O','O','X' },
           { 'X', 'O', 'X', 'O', 'X','X','O','X' },
           { 'X', 'X', 'X', 'X', 'X','X','O','X' },
           { 'X', 'X', 'X', 'X', 'X','X','O','X' },
           { 'X', 'X', 'X', 'X', 'X','X','X','X' }
           };
            FloodFill floodFill = new FloodFill(map);
            floodFill.fill(0, 0, '~', 'O');

          //  floodFill.fill(0, 6, '~', 'O');

            for (int y = 0; y < map.GetLength(0); y++, Console.WriteLine())
                for (int x = 0; x < map.GetLength(1); x++)
                    Console.Write(map[y, x]);

            List<CaveCell> CaveCells = floodFill.GetCaveCells();
            Cave Cave = new Cave();

            foreach (CaveCell cell in CaveCells)
            {
                Cave.Cells.Add(cell);
            }
            Caves.Add(Cave);

            foreach (Cave cave in Caves)
            {
                foreach (CaveCell cell in cave.Cells)
                {
                    Console.WriteLine(cell);
                }
            }
        }
    }


So the Caves list holds list of Caves, where each Cave has a list of it's cells, this look ok?

Thanks
21  Game Development / Newbie & Debugging Questions / Re: Adding water tiles on: 2015-06-09 09:15:56
Thanks Jesse,

I'm doing some code in cough cough C# to do this then going port to Java for my game.
The flood fill will need to store the cave cells x,y position I'm taking it.
22  Game Development / Newbie & Debugging Questions / Re: Adding water tiles on: 2015-06-09 08:01:10
Thanks Jesse,

All sounds good to me.  So on completion of flood fill, I can say this is a cave, add all the positions of the cells here into an array - would you do this as the flood fill is processing so you
have the positions of the cells for the 'cave' in question?

Thanks again,
Steve
23  Game Development / Newbie & Debugging Questions / Re: Adding water tiles on: 2015-06-08 13:19:30
This is my flood fill for filling map when map is made up of BlankEntity objects like in my game:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
public class FloodFill {

   BlankEntity[][] map;

   public FloodFill(BlankEntity[][] map) {
      this.map = map;
   }

   public void fill(int x, int y, WaterEntity newTile, CaveEntity oldTile) {
      if (x < 0)
         return;
      if (y < 0)
         return;
      if (x >= map.length)
         return;
      if (y >= map[x].length)
         return;

      BlankEntity currentTile = map[x][y]; // get current tile
      if (!(currentTile instanceof CaveEntity))
         return;

      map[x][y] = new WaterEntity(x, y);// newTile;

      fill(x - 1, y, newTile, oldTile);
      fill(x + 1, y, newTile, oldTile);
      fill(x, y - 1, newTile, oldTile);
      fill(x, y + 1, newTile, oldTile);
   }

   public static void main(String[] argv) {
      BlankEntity map[][] = {
            { new CaveEntity(0, 0), new CaveEntity(1, 0),
                  new CaveEntity(2, 0) },
            { new LandscapeEntity(0, 1), new LandscapeEntity(1, 1),
                  new LandscapeEntity(2, 1) }, };

      // display original map
      for (int x = 0; x < map.length; x++, System.out.println())
         for (int y = 0; y < map[x].length; y++)
            System.out.print(map[x][y]);

      FloodFill floodFill = new FloodFill(map);
      floodFill.fill(0, 0, new WaterEntity(0, 0), new CaveEntity(0, 0));

      // display after flood fill
      System.out.println();
      for (int x = 0; x < map.length; x++, System.out.println())
         for (int y = 0; y < map[x].length; y++)
            System.out.print(map[x][y]);

   }

}


Seems to work, but working out position of caves still confusing?

I've put the above into my game now and wrote a method to find the first cave entity cell so we can get a flood fill, I've put a screenshot of this here:

https://sites.google.com/site/sterrialand/development/news/floodfillfirsteffort

Now, how to move to next cave? Or, how not to fill this cave completely?

Thanks
24  Game Development / Newbie & Debugging Questions / Re: Adding water tiles on: 2015-06-08 10:34:25
Hi,

It will do what you say, but it is determining where a cave is - guess just where there is a cave entity, seems obvious now, lol.

Just need to fill some with water and others not.

Thanks
25  Game Development / Newbie & Debugging Questions / Re: Adding water tiles on: 2015-06-08 09:40:38
Hi,

What I meant was, it would fill all the caves in my map completely - I do want some of them not to have water in them and others to have some :-)

I know what you mean about the recursion, but think my caves aren't that big so could be ok but may later look into using one without recursion using a queue.

I used this before using the simple flood fill you mentioned:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
for (int x = 0; x < GRASS_START - (DIRT_START + 2); x++) {
         for (int y = 1; y < w - 1; y++) {
            int index = 0;
            int start = 0;
            int end = 0;
            boolean found = false;
            if (worldMap[y][x + DIRT_START] instanceof CaveLeftEntity ||
                  worldMap[y][x + DIRT_START] instanceof CaveBottomEntity ||
                  worldMap[y][x+DIRT_START] instanceof CaveTopEntity) // bottom
                                                         // of
                                                         // a
                                                         // cave
            {
               start = y;
               do {
                  if (index + y >= w - 1)
                     break;

                  index++;

                  if (worldMap[y + index][x + DIRT_START] instanceof CaveRightEntity) {
                     found = true;
                     end = y+index;
                     break;
                  }
               } while (!(worldMap[y + index][x + DIRT_START] instanceof CaveRightEntity));
               if (found) {
                  System.out.printf("Start from:%d end at:%d", start,end);
                  for(int j=start; j<=end;j++) {
                     worldMap[j][x + DIRT_START] = new WaterEntity(j,x + DIRT_START);
               //       break;  
                  }
               }
               
            }
         }


Thanks again.
26  Game Development / Newbie & Debugging Questions / Re: Adding water tiles on: 2015-06-08 09:01:26
Many thanks Jesse, really appreciate your help and advice with this.

I've coded this class up for testing purposes for flood fill:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
package com.mygdx.utils;


public class FloodFill {

   char[][] map;
   
   public FloodFill(char[][] map)
   {
      this.map = map;
   }
   
   public void fill(int x, int y, char newTile, char oldTile) {
      if(x <0) return;
      if(y<0) return;
      if(x > map.length) return;
      if(y > map[x].length) return;
     
      char currentTile = map[x][y];  // get current tile
      if(oldTile != currentTile) return; //
      if(newTile == currentTile) return;  // same
     
      map[x][y] = newTile;
     
      fill(x-1,y, newTile, oldTile);
      fill(x+1,y, newTile, oldTile);
      fill(x,y -1, newTile, oldTile);
      fill(x,y+1, newTile, oldTile);
   }
   
   public static void main(String[] argv)
   {
      char map[][] =
           {
               { 'O', 'O', 'O', 'O', 'X' },
               { 'X', 'O', 'O', 'O', 'X' },
               { 'X', 'O', 'O', 'O', 'X' },
               { 'X', 'O', 'O', 'O', 'X' },
               { 'X', 'X', 'X', 'X', 'X' },
               { 'X', 'X', 'X', 'X', 'X' },
               { 'X', 'X', 'X', 'X', 'X' }
           };
           FloodFill floodFill = new FloodFill(map);
           floodFill.fill(0, 0, '*', 'O');
           
           for(int x=0;x<map.length;x++, System.out.println())
              for(int y=0;y<map[x].length;y++)
                 System.out.print(map[x][y]);
           
   }
   
}


Seems to work, as long as the cell you are starting with is the one to be replaced. Thus, would start with a cave cell and use the above, problem is,
the array width, this would do my whole map, not just the width of a cave as I don't know what this would be.  This is why it would be good if I could get the caves
width.  Is it possible to do this?

Thanks


27  Game Development / Newbie & Debugging Questions / Re: Adding water tiles on: 2015-06-08 07:49:39
Hi,

Thanks again.

Yes, I do know what cells are the CaveEntity ones.  So, you are saying if I pick one of these cells, a flood fill will fill the cave - does this not need a start, end position or something?
Also, I'd be struggling to move onto the next cave as they are just cells, I've nothing stored that says there is a cave at this position, one at that position etc if you know what I mean.

A floodfill usually has a start node, target color (tile), replacement color(tile).

Maybe I'm over complicating this?!

Thanks
28  Game Development / Newbie & Debugging Questions / Re: Adding water tiles on: 2015-06-07 20:35:05
Hi thanks for that,

Yeah,just want to fill parts of the caves.

Again, the caves are just of type CaveEntity,CaveLeftEntity,CaveRightEntity,CaveBottomEntity and CaveTopEntity - basically just a tile - so I cannot
determine atm the start and end of a cave.

Thanks
29  Game Development / Newbie & Debugging Questions / Re: Adding water tiles on: 2015-06-07 20:05:44
Will do.

How do I put an image in the post btw?!

I've uploaded images of what I am after:

https://sites.google.com/site/sterrialand/development/news/addingwaterhow

Thanks
30  Game Development / Newbie & Debugging Questions / Re: Adding water tiles on: 2015-06-07 19:03:22
Still struggling with this, thinking somehow working out how to determine start and end of a cave...

How do other people add water tiles to their caves?

Any ideas are welcome and appreciated.

Thanks
Pages: [1] 2 3 ... 11
 
Za\'Anzabar (17 views)
2015-06-29 05:44:54

TritonDreyja (34 views)
2015-06-24 17:10:40

CopyableCougar4 (29 views)
2015-06-23 00:34:45

BurntPizza (32 views)
2015-06-21 20:36:46

cookiecompiler (77 views)
2015-06-11 15:42:53

cookiecompiler (39 views)
2015-06-11 15:41:14

NegativeZero (66 views)
2015-06-11 09:49:18

Fairy Tailz (89 views)
2015-06-11 01:59:47

chrislo27 (61 views)
2015-06-06 18:12:44

Burnsalan20 (74 views)
2015-06-05 03:00:51
How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!