|
3 | 3 | import repast.simphony.context.Context; |
4 | 4 | import repast.simphony.engine.environment.RunEnvironment; |
5 | 5 | import repast.simphony.parameter.Parameters; |
| 6 | +import repast.simphony.random.RandomHelper; |
6 | 7 | import repast.simphony.space.continuous.ContinuousSpace; |
7 | 8 | import repast.simphony.space.grid.Grid; |
8 | 9 | import repast.simphony.util.ContextUtils; |
|
16 | 17 | public class Grass extends SimpleAgent { |
17 | 18 | private int countdown; // coundown timer for grass to re-grow |
18 | 19 | private boolean alive; // boolean for grass alive / dead |
19 | | - |
| 20 | + |
20 | 21 | private static final int ALIVE = 1; |
21 | 22 | private static final int DEAD = 0; |
22 | | - |
23 | | - // This constructor is used to create initial grass from the context creator |
| 23 | + |
| 24 | + // This constructor is used to create initial grass from the context creator |
24 | 25 | public Grass(Context context, int x, int y ){ |
25 | 26 | // Get the grass regrow time from the user parameters |
26 | 27 | Parameters p = RunEnvironment.getInstance().getParameters(); |
27 | 28 | int regrowTime = (Integer)p.getValue("grassregrowthtime"); |
28 | | - |
29 | | - // add the grass to the root context |
| 29 | + |
| 30 | + // add the grass to the root context |
30 | 31 | context.add(this); |
31 | | - |
| 32 | + |
32 | 33 | // Set the initial countdown time from 0 to regrowTime |
33 | | - countdown = (int)(Math.random() * regrowTime); |
34 | | - |
| 34 | + countdown = RandomHelper.nextIntFromTo(0, regrowTime); |
| 35 | + |
35 | 36 | // Randomly set the grass alive or dead |
36 | | - if (Math.random() <= 0.5) |
| 37 | + if (RandomHelper.nextDouble() <= 0.5) |
37 | 38 | alive = true; |
38 | | - |
| 39 | + |
39 | 40 | else |
40 | 41 | alive = false; |
41 | | - |
| 42 | + |
42 | 43 | Grid grid = (Grid)context.getProjection("Simple Grid"); |
43 | 44 | ContinuousSpace space = (ContinuousSpace)context.getProjection("Continuous Space"); |
44 | | - |
45 | | - // move the grass to its position on the patch grid |
| 45 | + |
| 46 | + // move the grass to its position on the patch grid |
46 | 47 | grid.moveTo(this, x, y); |
47 | | - |
48 | | - // and to its position on the continuous space |
| 48 | + |
| 49 | + // and to its position on the continuous space |
49 | 50 | space.moveTo(this, x, y, 0); |
50 | | - |
51 | | - GridValueLayer vl = (GridValueLayer)context.getValueLayer("Grass Field"); |
52 | | - |
| 51 | + |
| 52 | + GridValueLayer vl = (GridValueLayer)context.getValueLayer("Grass Field"); |
| 53 | + |
53 | 54 | if (alive) |
54 | 55 | vl.set(ALIVE, grid.getLocation(this).toIntArray(null)); |
55 | 56 | else |
56 | 57 | vl.set(DEAD, grid.getLocation(this).toIntArray(null)); |
57 | 58 | } |
58 | | - |
59 | | - @Override |
| 59 | + |
| 60 | + @Override |
60 | 61 | public void step(){ |
61 | 62 | // If the grass is not alive |
62 | 63 | if (!alive){ |
63 | 64 | // If the countdown has expired, then regrow the grass |
64 | | - if (countdown <= 0){ |
65 | | - // Get the grass regrow time from the user parameters |
66 | | - Parameters p = RunEnvironment.getInstance().getParameters(); |
| 65 | + if (countdown <= 0){ |
| 66 | + // Get the grass regrow time from the user parameters |
| 67 | + Parameters p = RunEnvironment.getInstance().getParameters(); |
67 | 68 | int regrowTime = (Integer)p.getValue("grassregrowthtime"); |
68 | | - |
69 | | - alive = true; |
70 | | - updateValueLayer(); |
71 | | - countdown = regrowTime; |
72 | | - } |
73 | | - // Otherwise continue the regrow countdown |
74 | | - else |
75 | | - countdown--; |
| 69 | + |
| 70 | + alive = true; |
| 71 | + updateValueLayer(); |
| 72 | + countdown = regrowTime; |
| 73 | + } |
| 74 | + // Otherwise continue the regrow countdown |
| 75 | + else |
| 76 | + countdown--; |
76 | 77 | } |
77 | 78 | } |
78 | 79 |
|
79 | | - /** |
80 | | - * Called by sheep when they eay this grass. |
81 | | - */ |
82 | | - public void consume(){ |
83 | | - alive = false; |
84 | | - updateValueLayer(); |
85 | | - } |
86 | | - |
87 | | - /** |
88 | | - * Update the value layer to reflect the status of the grass health. |
89 | | - */ |
90 | | - private void updateValueLayer(){ |
91 | | - GridValueLayer vl = (GridValueLayer)ContextUtils.getContext(this).getValueLayer("Grass Field"); |
92 | | - Grid grid = (Grid)ContextUtils.getContext(this).getProjection("Simple Grid"); |
93 | | - |
| 80 | + /** |
| 81 | + * Called by sheep when they eay this grass. |
| 82 | + */ |
| 83 | + public void consume(){ |
| 84 | + alive = false; |
| 85 | + updateValueLayer(); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Update the value layer to reflect the status of the grass health. |
| 90 | + */ |
| 91 | + private void updateValueLayer(){ |
| 92 | + GridValueLayer vl = (GridValueLayer)ContextUtils.getContext(this).getValueLayer("Grass Field"); |
| 93 | + Grid grid = (Grid)ContextUtils.getContext(this).getProjection("Simple Grid"); |
| 94 | + |
94 | 95 | if (alive) |
95 | 96 | vl.set(ALIVE, grid.getLocation(this).toIntArray(null)); |
96 | 97 | else |
97 | 98 | vl.set(DEAD, grid.getLocation(this).toIntArray(null)); |
98 | | - } |
99 | | - |
| 99 | + } |
| 100 | + |
100 | 101 | /** |
101 | 102 | * @return if the sheep is alive. |
102 | 103 | */ |
|
0 commit comments