-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMapGen.java
More file actions
146 lines (136 loc) · 4.3 KB
/
MapGen.java
File metadata and controls
146 lines (136 loc) · 4.3 KB
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package se;
/**
*
* @author acer
*/
class MapGen {
int width;
int height;
float chanceToStartAlive; //= 0.45f;
MapGen (int w, int h, float c)
{
width = w;
height = h;
chanceToStartAlive = c;
}
short[][] initialiseMap(short[][] map){
for(int x=0; x<width; x++){
for(int y=0; y<height; y++){
if(Math.random() < chanceToStartAlive){
map[x][y] = 1;
}
}
}
// map[0][0]=2;
return map;
}
short[][] generateMap(short map[][])
{
short map1[][];
map1 = doSimulationStep(map);
return map1;
}
short[][] doSimulationStep(short[][] oldMap){
short[][] newMap = new short[width][height];
//Loop over each row and column of the map
for(int x=0; x<oldMap.length; x++){
for(int y=0; y<oldMap[0].length; y++){
if(oldMap[x][y]==2) {newMap[x][y]=2;continue;}
int nbs = countAliveNeighbours(oldMap, x, y);
if(oldMap[x][y] == 1){
if(nbs < 3){
newMap[x][y] = 0;
}
else{
newMap[x][y] = 1;
}
}
else if(oldMap[x][y] == 0){
if(nbs > 4){
newMap[x][y] = 1;
}
else{
newMap[x][y] = 0;
}
}
}
}
return newMap;
}
short[][] doSmoothSimulationStep(short[][] oldMap){
short[][] newMap = new short[width][height];
//Loop over each row and column of the map
for(int x=0; x<oldMap.length; x++){
for(int y=0; y<oldMap[0].length; y++){
if(oldMap[x][y]==2) {newMap[x][y]=2;continue;}
int nbs = countAliveNeighbours(oldMap, x, y);
//The new value is based on our simulation rules
//First, if a cell is alive but has too few neighbours, kill it.
if(oldMap[x][y] == 0){
if(nbs < 5){
newMap[x][y] = 0;
}
else{
newMap[x][y] = 1;
}
} //Otherwise, if the cell is dead now, check if it has the right number of neighbours to be 'born'
else if(oldMap[x][y] == 1){
if(nbs > 5){
newMap[x][y] = 1;
}
else{
newMap[x][y] = 0;
}
}
}
}
return newMap;
}
short[][] placeTreasure(short[][] map){
//How hidden does a spot need to be for treasure?
//I find 5 or 6 is good. 6 for very rare treasure.
int treasureHiddenLimit = 4;
for (int x=0; x < width; x++){
for (int y=0; y < height; y++){
if(map[x][y] == 0){
int nbs = countAliveNeighbours(map, x, y);
//System.out.println(nbs);
if(nbs >= treasureHiddenLimit){
//System.out.println("Changed");
map[x][y] = 5;
}
}
}
}
return map;
}
//Returns the number of cells in a ring around (x,y) that are alive.
int countAliveNeighbours(short[][] map, int x, int y){
int count = 0;
for(int i=-1; i<2; i++){
for(int j=-1; j<2; j++){
int neighbour_x = x+i;
int neighbour_y = y+j;
//If we're looking at the middle point
if(i == 0 && j == 0){
//Do nothing, we don't want to add ourselves in!
}
//In case the index we're looking at it off the edge of the map
else if(neighbour_x < 0 || neighbour_y < 0 || neighbour_x >= map.length || neighbour_y >= map[0].length){
count = count + 1;
}
//Otherwise, a normal check of the neighbour
//counting alive cells
else if(map[neighbour_x][neighbour_y] == 1){
count = count + 1;
}
}
}
return count;
}
}