Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  2D Tiled Randomly Generated  (Read 2475 times)
0 Members and 1 Guest are viewing this topic.
Offline Seiya02

JGO Coder


Medals: 3
Projects: 4
Exp: 5 years


No Text Here :x


« Posted 2014-06-26 13:44:58 »

Okay, so i want to make some game where it randomly generates an island, the noise functions and island masks are all good but i have the following problem: if i want to do this with tiles, i don't have the tiles for some specific positions :/

example:


And this is the tiles i have:



I know that I could simply draw the missing tiles, but I would prefer to have the code change the map so that it doesn't need those tiles.

What i mean is like the following: (Tiles = X and O)

Case 1:

XXX     
XOX
XXX
gets changed to
XXX
XXX
XXX

Case 2:

OOXXOO
OOOOOO
OOXXOO
gets changed to
OOXXOO
OOXXOO
OOXXOO

Because these Tiles aren't supported but I have no idea how this would work for the case in the picture so that i don't need to draw additional tiles :/

Code fixing the other 2 Issues and an attempt to count the ones that are still there:
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  
      boolean done = false;
      int looped = 0;
      while (!done) {
         Start.errors = 0;
         looped++;
         done = true;
         // REDO
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
               // Check null
              if (!exist(x - 1, y) || !exist(x + 1, y)
                     || !exist(x, y - 1) || !exist(x, y + 1)) {
                  // Do nothing
              } else {
                  int amount = 0;
                  type t = null;
                  if (tiles[(x - 1) + y * w] == tiles[x + y * w]) {
                     amount++;
                  } else {
                     t = tiles[(x - 1) + y * w];
                  }
                  if (tiles[(x + 1) + y * w] == tiles[x + y * w]) {
                     amount++;
                  } else {
                     t = tiles[(x + 1) + y * w];
                  }
                  if (tiles[x + (y - 1) * w] == tiles[x + y * w]) {
                     amount++;
                  } else {
                     t = tiles[x + (y - 1) * w];
                  }
                  if (tiles[x + (y + 1) * w] == tiles[x + y * w]) {
                     amount++;
                  } else {
                     t = tiles[x + (y + 1) * w];
                  }
                  if (amount <= 1) {
                     // Change
                    tiles[x + y * w] = t;
                     done = false;
                  }
               }

            }
         }
         // Lines
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
               // Check null
              if (!exist(x - 1, y) || !exist(x + 1, y)
                     || !exist(x, y - 1) || !exist(x, y + 1)) {
                  // Do nothing
              } else {
                  int amount = 0;
                  type t = null;
                  if (tiles[(x - 1) + y * w] == tiles[x + y * w]) {
                     amount++;
                  } else {
                     t = tiles[(x - 1) + y * w];
                  }
                  if (tiles[(x + 1) + y * w] == tiles[x + y * w]) {
                     amount++;
                  } else {
                     t = tiles[(x + 1) + y * w];
                  }
                  if (tiles[x + (y - 1) * w] == tiles[x + y * w]) {
                     amount++;
                  } else {
                     t = tiles[x + (y - 1) * w];
                  }
                  if (tiles[x + (y + 1) * w] == tiles[x + y * w]) {
                     amount++;
                  } else {
                     t = tiles[x + (y + 1) * w];
                  }
                  if (amount == 2) {
                     if (tiles[x + (y - 1) * w] == tiles[x + (y + 1) * w]) {
                        if (tiles[x + (y - 1) * w] == tiles[x + y * w]) {
                           tiles[x + (y - 1) * w] = t;
                           tiles[x + (y + 1) * w] = t;
                        } else {

                        }
                        tiles[x + y * w] = t;
                        done = false;
                     }
                  }
               }

            }
         }
         // Edge to edge
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
               // Check null
              if (!exist(x - 1, y) || !exist(x + 1, y)
                     || !exist(x, y - 1) || !exist(x, y + 1)) {
                  // Do nothing
              } else {
                  int amount = 0;
                  if (tiles[(x - 1) + (y - 1) * w] == tiles[x + y * w]) {
                     amount++;
                  }
                  if (tiles[(x + 1) + (y - 1) * w] == tiles[x + y * w]) {
                     amount++;
                  }
                  if (tiles[(x - 1) + (y + 1) * w] == tiles[x + y * w]) {
                     amount++;
                  }
                  if (tiles[(x + 1) + (y + 1) * w] == tiles[x + y * w]) {
                     amount++;
                  }
                  if (amount == 2) {
                     if (tiles[(x + 1) + (y + 1) * w] == tiles[(x - 1)
                           + (y - 1) * w]) {
                        Start.errors++;
                        if (done) {
                           tiles[x + y * w] = type.ERROR;
                        }
                     }
                  }
               }

            }
         }

         if (looped >= 10) {
            Start.errors = -1;
            continue;
         }
      }
Offline KevinWorkman

JGO Wizard


Medals: 70
Projects: 11
Exp: 12 years


klaatu barada nikto


« Reply #1 - Posted 2014-06-26 14:20:48 »

The way I see it, you have 3 options:
  • Come up with a data structure that maps combinations of bad tiles to tiles that should replace them.
  • Tweak your generator so it doesn't generate bad tiles in the first place.
  • Create sprites for the bad tiles so there aren't any bad tiles anymore.

Which option are you trying to do?

It seems like you're going for option 1. How are you representing the underlying data that tells you which tiles to draw?

Keep in mind that there might be an extremely large number of possible bad combinations, in which case option 2 or even option 3 might end up being less work.

As always, the answer is to break your problem down into smaller steps.

Can you write a function that takes a 2D array (or whatever) that represents a bad combination and returns whether that can be found in the data structure (is it another 2D array?) that represents your whole map?

Can you modify that function to return the position of the bad combination?

Can you write another function that takes a 2D array (or whatever) that represents a GOOD combination, as well as a position, and inserts that combination into the whole map at that position?

Once you have those functions working, then you can combine them in another method that loops through all of the bad combinations and tries to replace them.

Static Void Games - Play indie games, learn game programming, upload your own games!
Offline Rayvolution

JGO Kernel


Medals: 199
Projects: 2
Exp: 1 year


Resident Crazyman


« Reply #2 - Posted 2014-07-03 18:14:47 »

I suggest simply just drawing every tile combo you need. It'll make for a more natural looking map in the long run. An alternative instead of adding tiles could be removing them instead, just have some way to detect the tiles around the tile you're placing, if it creates an "illegal" situation, skip placing or delete that tile.

One idea; build a byte[3][3] array to check surrounding tiles on every tile of your map, making a 3x3 grid, center tile being the one checked. Then you'll have an output of something like this:
(1 == matching tile found, 0 == no tile, or mismatched tile)
111
110
100

Then compare that to a set of byte[3][3] arrays that you deem "illegal" tile placement. If it matches any of them, delete the tile. It's not the fastest way to do it, but it's a simple solution. Another, slightly more hacky solution would be to use an int string, and make an int like this: 111110100, and compare that to a list of "illegal ints".

These are all not really the best solutions, but the coding is simple.

- Raymond "Rayvolution" Doerr.
Retro-Pixel Castles - Survival Sim/Builder/Roguelike!
LIVE-STREAMING DEVELOPMENT: http://www.twitch.tv/SG_Rayvolution
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Seiya02

JGO Coder


Medals: 3
Projects: 4
Exp: 5 years


No Text Here :x


« Reply #3 - Posted 2014-07-03 20:47:47 »

I just drew myself the missing tiles so it's fine now Smiley and improved the way it detects "wrong tiles"
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (22 views)
2014-09-19 03:14:18

Dwinin (35 views)
2014-09-12 09:08:26

Norakomi (63 views)
2014-09-10 13:57:51

TehJavaDev (90 views)
2014-09-10 06:39:09

Tekkerue (44 views)
2014-09-09 02:24:56

mitcheeb (65 views)
2014-09-08 06:06:29

BurntPizza (48 views)
2014-09-07 01:13:42

Longarmx (35 views)
2014-09-07 01:12:14

Longarmx (40 views)
2014-09-07 01:11:22

Longarmx (37 views)
2014-09-07 01:10:19
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!