View Javadoc
1   package org.woehlke.simulation.evolution.model;
2   
3   import java.util.Random;
4   
5   /**
6    * Map of World where every Place can have food needed by the Bacteria Cells for eating.
7    *
8    * Simulated Evolution.
9    * Artificial Life Simulation of Bacteria Motion depending on DNA.
10   *
11   * © 2006 - 2013 Thomas Woehlke.
12   * http://thomas-woehlke.de/p/simulated-evolution/
13   * @author Thomas Woehlke
14   * Date: 24.08.13
15   * Time: 12:37
16   */
17  public class WorldMapFood {
18  
19      /**
20       * Grid of World where every Place can have food.
21       */
22      private int worldMapFood[][];
23  
24      /**
25       * How much food per Time Step (a day) shall be placed in this World.
26       */
27      private final int FOOD_PER_DAY = 10;
28  
29      /**
30       * A Garden of Eden is an Area where much more Food grows within the same time.
31       * As a Result of Evolution you will find sucessful Bacteria Cells with a different DNA and Motion as outside the Garden of Eden.
32       */
33      private final boolean EABLE_GARDEN_OF_EDEN = true;
34  
35      /**
36       * Random Generator used for placing food, coming from another Object.
37       */
38      private Random random;
39  
40      /**
41       * Dimension for the Grid inside the WorldMapFood which is the Same as in World Data Model and in the View.
42       */
43      private Point dimensions;
44  
45      public WorldMapFood(Point dimensions,Random random){
46          this.dimensions=dimensions;
47          worldMapFood = new int[this.dimensions.getX()][this.dimensions.getY()];
48          this.random=random;
49      }
50  
51      /**
52       * Delivers new food to random positions.
53       */
54      public void letFoodGrow() {
55          int f = 0;
56          while (f < FOOD_PER_DAY) {
57              f++;
58              int x = random.nextInt(this.dimensions.getX());
59              int y = random.nextInt(this.dimensions.getY());
60              if (x < 0) {
61                  x *= -1;
62              }
63              if (y < 0) {
64                  y *= -1;
65              }
66              worldMapFood[x][y]++;
67          }
68          if(EABLE_GARDEN_OF_EDEN){
69              f = 0;
70              int startx = this.dimensions.getX() / 5;
71              int starty = this.dimensions.getY() / 5;
72              while (f < FOOD_PER_DAY*4) {
73                  f++;
74                  int x = random.nextInt(startx);
75                  int y = random.nextInt(starty);
76                  if (x < 0) {
77                      x *= -1;
78                  }
79                  if (y < 0) {
80                      y *= -1;
81                  }
82                  worldMapFood[x+startx*2][y+starty*2]++;
83              }
84          }
85      }
86  
87      public boolean hasFood(int x, int y) {
88          return worldMapFood[x][y] > 0;
89      }
90  
91      /**
92       * Reduces Food in the Grid by eating and delivers the food energy to the eating Cell.
93       *
94       * @see LifeCycle
95       *
96       * @param position where is the food and the eating cell
97       * @return the engergy of the food, will be added to cell's fat.
98       */
99      public int eat(Point position) {
100         Point neighbourhood[] = position.getNeighbourhood(this.dimensions);
101         int food=0;
102         for (Point neighbourhoodPosition:neighbourhood){
103             food += worldMapFood[neighbourhoodPosition.getX()][neighbourhoodPosition.getY()];
104             worldMapFood[neighbourhoodPosition.getX()][neighbourhoodPosition.getY()]=0;
105         }
106         return food;
107     }
108 }