-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prey.java
160 lines (126 loc) · 3.62 KB
/
Prey.java
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package sim.app.pvp;
import sim.engine.*;
import sim.field.grid.SparseGrid2D;
import sim.util.Bag;
import sim.util.Int2D;
public class Prey extends Animal implements Steppable{
/**
*
*/
private static final long serialVersionUID = 1L;
private int oldAge = 1000;
private double deathRate = .0001;
private double actualRepRate = .000001;
private double agingDeathMod = 1.0001;
private int randomRepoNumb = 1000;
private double hungerDeathMod = 1.01;
private double repAge = 56;
private int deathRandNum = 10000;
private int daysLM = 28;
Prey(SimState state){
int directionNum= state.random.nextInt(3);
if(directionNum == 0)
direction = 0;
else if(directionNum == 1)
direction = 1;
else if (directionNum == 2)
direction = 2;
else
direction = 3;
vP = new VisualProcessor(state);
}
@Override
public void step(SimState state) {
// TODO Auto-generated method stub
super.step(state);
//Death Chance
if(this.iDie(state))
return;
//Reproduction Chance
if(this.iReproduce(state))
return;
//Chance of Eating
// if(this.willEat(grid, state))
// return;
/*//Eating Food on the same location
if(grid.getObjectsAtLocationOfObject(this) != null && this != null){
int gridNum = grid.getObjectsAtLocationOfObject(this).numObjs;
for(int i = 0; i < gridNum; i++){
Object obj = (grid.getObjectsAtLocationOfObject(this)).get(i);
if(obj.getClass().equals(Food.class) && obj != null){
this.eat(obj);
break;
}// end of if
} // end of for loop
}// end of if statement*/
//Moving every time
super.move(grid, state);
}
public boolean willEat(SparseGrid2D grid, SimState state){
//Eating Prey on the same location
assert(grid.getObjectsAtLocationOfObject(this).size() != 0);
assert(grid.getObjectsAtLocationOfObject(this) !=null);
assert(this != null);
assert(grid !=null);
System.out.println(grid.getObjectsAtLocationOfObject(this).size());
int gridNum = grid.getObjectsAtLocationOfObject(this).size();
assert(gridNum != 0);
for(int i = 0; i < gridNum; i++){
Object obj = (grid.getObjectsAtLocationOfObject(this)).get(i);
if(obj.getClass().equals(Food.class) && obj != null){
this.eat(obj);
return true;
}// end of if
}// end of for loop
return false;
}
public void eat(Object p){
//System.out.println(p);
Food food = (Food) p;
if(food.isDiseased())
this.setDisease(true);
//System.out.println(this + " ate " + p);
lastMeal = 0;
grid.remove(p);
//System.out.println("Food is removed");
}
public void setDiseased(boolean dis){
isDiseased = dis;
}
public void reproduce(SimState state){
Prey p = new Prey(state);
grid.setObjectLocation(p, grid.getObjectLocation(this));
state.schedule.scheduleRepeating(p);
}
public boolean isDiseased(){
return isDiseased;
}
public boolean iDie(SimState state){
//older = more likely to die
if(age>oldAge)
deathRate = deathRate * agingDeathMod;
//Last meal, more likely to die
if(lastMeal > daysLM)
deathRate = deathRate * hungerDeathMod;
//System.out.println("deathRate: " + deathRate);
// Death Rate
double d = state.random.nextInt(deathRandNum);
double death = d/deathRandNum;
//System.out.println("d: " + d + " death: " + death);
if(death < deathRate && death != 0){
grid.remove(this);
return true;
}
return false;
}
public boolean iReproduce(SimState state){
// Reproduction Rate
double r = state.random.nextInt(randomRepoNumb);
double repo = r/randomRepoNumb;
if(repo <= actualRepRate && age >= repAge){
this.reproduce(state);
return true;
}
return false;
}
}