Skip to content

Commit 43670d3

Browse files
committed
Fix grass sometimes dies when a canvas was loaded from a brush template
1 parent bf62e20 commit 43670d3

File tree

3 files changed

+43
-27
lines changed

3 files changed

+43
-27
lines changed

sg2-app/src/cz/hartrik/sg2/app/sandbox/element/ElementList.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ public class ElementList {
116116

117117
static final FireSettings FS = new FireSettings(rc(5), 400, 1200, rc(50));
118118

119-
static final Grass GRASS_1 = new Grass(c(39, 87, 28), rc(400), 9, GRASS_DEAD_COL, FS);
120-
static final Grass GRASS_2 = new Grass(c( 0, 68, 0), rc(400), 7, GRASS_DEAD_COL, FS);
121-
static final Grass GRASS_3 = new Grass(c( 0, 60, 0), rc(400), 8, GRASS_DEAD_COL, FS);
119+
static final Grass GRASS_1 = new Grass(c(39, 87, 28), rc(400), 9, 9, GRASS_DEAD_COL, FS);
120+
static final Grass GRASS_2 = new Grass(c( 0, 68, 0), rc(400), 7, 9, GRASS_DEAD_COL, FS);
121+
static final Grass GRASS_3 = new Grass(c( 0, 60, 0), rc(400), 8, 9, GRASS_DEAD_COL, FS);
122122
public static final Grass GRASS[] = { GRASS_1, GRASS_2, GRASS_3 };
123123

124124
static final Seed SEED_1 = new Seed(c(160, 160, 0), 50, Chance.ALWAYS, GRASS_1);
@@ -128,9 +128,9 @@ public class ElementList {
128128
//</editor-fold>
129129

130130
//<editor-fold defaultstate="collapsed" desc="Tráva - divoká">
131-
static final WildGrass GRASS_WILD_1 = new WildGrass(c(44, 92, 33), rc(400), 8, GRASS_DEAD_COL, FS);
132-
static final WildGrass GRASS_WILD_2 = new WildGrass(c( 0, 72, 0), rc(400), 6, GRASS_DEAD_COL, FS);
133-
static final WildGrass GRASS_WILD_3 = new WildGrass(c( 0, 65, 0), rc(400), 7, GRASS_DEAD_COL, FS);
131+
static final WildGrass GRASS_WILD_1 = new WildGrass(c(44, 92, 33), rc(400), 8, 8, GRASS_DEAD_COL, FS);
132+
static final WildGrass GRASS_WILD_2 = new WildGrass(c( 0, 72, 0), rc(400), 6, 8, GRASS_DEAD_COL, FS);
133+
static final WildGrass GRASS_WILD_3 = new WildGrass(c( 0, 65, 0), rc(400), 7, 8, GRASS_DEAD_COL, FS);
134134
public static final WildGrass[] WILD_GRASS = { GRASS_WILD_1, GRASS_WILD_2, GRASS_WILD_3 };
135135

136136
static final Seed SEED_WILD_1 = new Seed(c(178, 178, 0), 50, Chance.ALWAYS, GRASS_WILD_1);
@@ -146,10 +146,10 @@ public class ElementList {
146146
//</editor-fold>
147147

148148
//<editor-fold defaultstate="collapsed" desc="Tráva - rákosí">
149-
static final Grass REED_1 = new Grass(c( 83, 146, 35), rc(400), 15, new Element[] { GRASS_DEAD_1, AIR}, FS); // světlé
150-
static final Grass REED_2 = new Grass(c(131, 160, 2), rc(400), 10, new Element[] { GRASS_DEAD_2, AIR}, FS); // žluté
151-
static final Grass REED_3 = new Grass(c( 31, 93, 3), rc(400), 14, new Element[] { GRASS_DEAD_3, AIR}, FS); // tmavší
152-
static final Grass REED_4 = new Grass(c( 75, 129, 7), rc(400), 13, new Element[] { GRASS_DEAD_3, AIR}, FS); // světlé
149+
static final Grass REED_1 = new Grass(c( 83, 146, 35), rc(400), 15, 15, new Element[] { GRASS_DEAD_1, AIR}, FS); // světlé
150+
static final Grass REED_2 = new Grass(c(131, 160, 2), rc(400), 10, 15, new Element[] { GRASS_DEAD_2, AIR}, FS); // žluté
151+
static final Grass REED_3 = new Grass(c( 31, 93, 3), rc(400), 14, 15, new Element[] { GRASS_DEAD_3, AIR}, FS); // tmavší
152+
static final Grass REED_4 = new Grass(c( 75, 129, 7), rc(400), 13, 15, new Element[] { GRASS_DEAD_3, AIR}, FS); // světlé
153153
public static final Grass REED[] = { REED_1, REED_2, REED_3, REED_4 };
154154

155155
static final Seed REED_SEED_1 = new Seed(c(143, 104, 62), 50, Chance.ALWAYS, REED_1);

sg2-core/src/cz/hartrik/sg2/world/element/flora/Grass.java

+27-10
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,17 @@
1313
import cz.hartrik.sg2.world.element.temperature.FireSettings;
1414

1515
/**
16-
* Element představující trávu.
16+
* <p>Element představující trávu.</p>
1717
*
18-
* @version 2016-06-16
18+
* <p>
19+
* <b>Poznámka k growHeight vs maxHeight:</b><br/>
20+
* growHeight - výška, do které tráva může vyrůst, obvykle náhodná<br/>
21+
* maxHeight - výška, do které tráva může existovat<br/>
22+
* Vzhledem k tomu, že je growHeight náhodná, po vložený by bez znalosti
23+
* maxHeight některá tráva odumřela.
24+
* </p>
25+
*
26+
* @version 2017-09-02
1927
* @author Patrik Harag
2028
*/
2129
public class Grass extends Plant implements BurnableDef, FireAffected {
@@ -27,16 +35,20 @@ public class Grass extends Plant implements BurnableDef, FireAffected {
2735
protected int maxElementDensity = 150;
2836

2937
private final Color color;
38+
protected final int growHeight;
3039
protected final int maxHeight;
3140
protected final Chance growChance;
3241
protected final Element[] deadGrass;
3342
protected final FireSettings fireSettings;
3443

35-
public Grass(Color color, Chance chance, int maxHeight, Element[] deadGrass,
36-
FireSettings fireSettings) {
44+
public Grass(Color color, Chance chance, int growHeight, int maxHeight,
45+
Element[] deadGrass, FireSettings fireSettings) {
46+
47+
assert growHeight <= maxHeight;
3748

3849
this.color = color;
3950
this.growChance = chance;
51+
this.growHeight = growHeight;
4052
this.maxHeight = maxHeight;
4153
this.deadGrass = deadGrass;
4254
this.fireSettings = fireSettings;
@@ -59,11 +71,13 @@ public void doAction(int x, int y, Tools tools, World world) {
5971

6072
final int findSoil = findSoil(x, y, maxHeight, world);
6173

62-
if (findSoil == SOIL_NOT_FOUND)
74+
if (findSoil == SOIL_NOT_FOUND) {
6375
// uschne - nemá půdu
6476
world.setAndChange(x, y, randomDeadGrass(tools));
65-
else
77+
78+
} else {
6679
checkElementAbove(x, y, findSoil, tools, world);
80+
}
6781
}
6882

6983
protected void checkElementAbove(int x, int y, int findSoil,
@@ -75,7 +89,7 @@ protected void checkElementAbove(int x, int y, int findSoil,
7589

7690
final Element element = world.get(x, up);
7791
if (element instanceof Air) {
78-
if (findSoil < maxHeight && growChance.nextBoolean())
92+
if (findSoil < growHeight && growChance.nextBoolean())
7993
// vyroste
8094
world.setAndChange(x, up, this);
8195

@@ -102,9 +116,12 @@ public boolean testAction(int x, int y, Tools tools, World world) {
102116
if (!world.valid(x, y - 1)) return true;
103117
final Element over = world.get(x, y - 1);
104118

105-
return (over instanceof Air)
106-
? findSoil(x, y, maxHeight, world) < maxHeight
107-
: !(over instanceof Grass);
119+
if (over instanceof Air) {
120+
int findSoil = findSoil(x, y, maxHeight, world);
121+
return (findSoil < growHeight || findSoil > maxHeight);
122+
} else {
123+
return !(over instanceof Grass);
124+
}
108125
}
109126

110127
@Override

sg2-core/src/cz/hartrik/sg2/world/element/flora/WildGrass.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
/**
1414
* Element představující trávu, která se sama rozšiřuje.
1515
*
16-
* @version 2016-06-16
16+
* @version 2017-09-02
1717
* @author Patrik Harag
1818
*/
1919
public class WildGrass extends Grass {
@@ -22,10 +22,9 @@ public class WildGrass extends Grass {
2222

2323
protected Grass[] grass;
2424

25-
public WildGrass(Color color, Chance chance, int maxHeight,
26-
Element[] deadGrass, FireSettings fireSettings) {
27-
28-
super(color, chance, maxHeight, deadGrass, fireSettings);
25+
public WildGrass(Color color, Chance chance, int growHeight, int maxHeight,
26+
Element[] deadGrass, FireSettings fireSettings) {
27+
super(color, chance, growHeight, maxHeight, deadGrass, fireSettings);
2928
}
3029

3130
public void setGrass(Grass... grass) {
@@ -53,7 +52,7 @@ public void doAction(int x, int y, Tools tools, World world) {
5352
}
5453
}
5554

56-
private static enum Result { ERROR, WARNING, SUCCESS }
55+
private enum Result { ERROR, WARNING, SUCCESS }
5756

5857
private void growNextTo(int x, int y, Tools tools, World world) {
5958
if (grass == null || grass.length == 0) return;
@@ -113,7 +112,7 @@ public boolean testAction(int x, int y, Tools tools, World world) {
113112
Element over = world.get(x, y - 1);
114113
if (over instanceof Air) {
115114
int findSoil = findSoil(x, y, maxHeight, world);
116-
if (findSoil < maxHeight) return true;
115+
if (findSoil < growHeight || findSoil > maxHeight) return true;
117116
} else if (over instanceof Grass) {
118117
if (!trySide(x + 1, y, false, tools, world))
119118
return trySide(x - 1, y, false, tools, world);

0 commit comments

Comments
 (0)