-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bird.java
127 lines (88 loc) · 3 KB
/
Bird.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
package sim.app.birds;
import sim.engine.*;
import sim.util.*;
public class Bird implements Steppable {
public static final boolean MALE = true;
public static final boolean FEMALE = false;
public static final double AVG_SPEED = 625;
private boolean gender;
private int age;
private boolean pregnant;
private float strategy;
private int id;
private Double2D pos;
public Bird() {
ec.util.MersenneTwisterFast random = BirdsModel.getInstance().random;
id = random.nextInt();
gender = random.nextBoolean();
pregnant = false;
strategy = random.nextFloat();
age = 0;
}
public Bird(int i) {
ec.util.MersenneTwisterFast random = BirdsModel.getInstance().random;
id = i;
gender = random.nextBoolean();
strategy = random.nextFloat();
age = 0;
pregnant = false;
}
public Bird(Bird mother, Bird father) {
ec.util.MersenneTwisterFast random = BirdsModel.getInstance().random;
gender = random.nextBoolean();
pregnant = false;
age = 0;
strategy = (mother.strategy + father.strategy) / 2;
}
public void setAge(int a) { age = a; }
public int getAge() { return age; }
public boolean getGender() { return gender; }
public float getStrategy() { return strategy; }
public void setPos(Double2D p) {
sim.field.continuous.Continuous2D world = BirdsModel.getInstance().getWorld();
pos = p;
world.setObjectLocation(this, pos);
}
public Double2D getPos() {
return pos;
}
public void setPregnant(boolean val) {
pregnant = val;
}
public boolean respond(Signal sig) {
if( gender == FEMALE && pregnant == false ) {
// Maybe mate? For the time being always mate.
Mating m = new Mating(sig.getSignaller(), this, pos);
Season s = BirdsModel.getInstance().getSeason();
s.addMating(m);
pregnant = true;
return true;
}
return false;
}
public void step(final SimState state) {
ec.util.MersenneTwisterFast random = state.random;
double visibility = ((BirdsModel)state).visibility(pos);
double audRange = ((BirdsModel)state).audRange(pos);
double dx = random.nextDouble();
double dy = random.nextDouble();
if( random.nextBoolean() ) dx += pos.getX(); else dx = pos.getX() - dx;
if( random.nextBoolean() ) dy += pos.getY(); else dy = pos.getY() - dy;
Double2D p = new Double2D(dx, dy);
this.setPos(p);
// Should we signal?
if( random.nextBoolean() && gender == MALE ) {
if( visibility >= strategy ) {
System.out.println("Signalling (Visually) - Vis: " + visibility + " Aud: " + audRange + " X: " + p.getX());
Signal s = new Signal(this, pos, visibility);
double current_time = state.schedule.getTime();
state.schedule.scheduleOnce(current_time, s);
} else if( audRange >= (1 - strategy) ) {
System.out.println("Signalling (Audibly) - Vis: " + visibility + " Aud: " + audRange + " X: " + p.getX());
Signal s = new Signal(this, pos, audRange);
double current_time = state.schedule.getTime();
state.schedule.scheduleOnce(current_time, s);
}
}
}
}