-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBear.java
485 lines (432 loc) · 12.5 KB
/
Bear.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
public class Bear implements Organism{
// Constants (specific to this animal type)
static final String type = "Bear";
static final ArrayList<String> preyTypes = new ArrayList<String>(Arrays.asList("Mouse", "Rabbit" ,"Plant", "Fox"));
static final ArrayList<String> predatorTypes = new ArrayList<String>();
static final double maxHealth = 100.0;
static final double hungerHealth = 50;
static final double healthLostPerGameTick = 0.2;
static final double healthGainedEatingPerGameTick = 5;
static final int maxSpeed = 6; // pixels
static final int sightRadius = 100; // pixels
static final int eatingRadius = 10; // pixels
static final double probabilityGivingBirth = 0.000027 / 2;
static final int avgNumBabies = 2;
// Variables (to be set and changed)
int id;
int X, Y;
int prevX, prevY;
Dimension D;
double health;
Point2D.Double targetLocation;
Organism prey;
Organism predator;
/* Organisms can be in various states represented by an integer
0. idle
1. eating
2. beingEaten
3. hunting
4. escaping */
int state = 0;
// Constructors ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Bear(int id, Point2D.Double randomPoint, Dimension D){
// Create a mouse at a location X,Y
this.id = id;
this.X = (int)randomPoint.x;
this.Y = (int)randomPoint.y;
this.prevX = this.X;
this.prevY = this.Y;
this.D = D;
// set initial health
this.health = generateRandomInitialHealth();
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* ~~~~~~~~~~~~~ Control Methods: To be called by AnimalSimulator ~~~~~~~~~~~~~ */
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
// Step 1: Update Health
public void updateHealthTime(){
// lose health due to time
this.health = this.health - this.healthLostPerGameTick;
}
// Step 2: Update State
public void updateState(ArrayList<Organism> organisms) {
/*
Organisms can be in various states represented by an integer
0. idle
1. eating
2. beingEaten
3. hunting
4. escaping
If the organism is eating or being eaten, their health
will be updated in this method to reflect that.
*/
ArrayList<Organism> nearbyOrganisms = getOrganismsWithinSightRadius(organisms);
ArrayList<Organism> nearbyPrey = getNearbyPrey(nearbyOrganisms);
ArrayList<Organism> nearbyPredators = getNearbyPredators(nearbyOrganisms);
if(!nearbyPredators.isEmpty()){
predator = getClosestPredator(nearbyPredators);
if(getXY().distance(predator.getXY()) <= eatingRadius){
// being eaten
state = 2;
}
else{
// escaping
state = 4;
}
}
else if(state == 1){ // If organism is already eating, continue eating
eatPrey();
}
else if(health <= hungerHealth){ // Else if organism is below hungerHealth and there exists prey within sight radius
if(!nearbyPrey.isEmpty()){
prey = getClosestPrey(nearbyPrey);
if(getXY().distance(prey.getXY()) <= eatingRadius){
// eating
state = 1;
eatPrey();
}
else{
// hunting
state = 3;
}
}
}
else{
// else - Idle, Random Walk
state = 0;
}
}
// Step 3: Move
public void move() {
// System.out.println(toString()+"state="+state+" move()");
Point2D.Double temp = null;
/* Organisms can be in various states represented by an integer
0. idle
1. eating
2. beingEaten
3. hunting
4. escaping */
switch (state) {
case 0: /* Idle - random walk */
temp = randomWalk();
break;
case 1: /* Eating (no movement) */
temp = getXY();
break;
case 2: /* Being eaten (no movement) */
temp = getXY();
break;
case 3: /* Hunting */
temp = hunt();
break;
case 4: /* Escaping */
temp = escape();
break;
default:
temp = randomWalk();
break;
}
this.X = (int)temp.x;
this.Y = (int)temp.y;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* ~~~~~~~~~~~~~~ Utility methods - called by control methods ~~~~~~~~~~~~~~~~~ */
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
public Point2D.Double randomWalk(){ // Called in move()
// initialize newLocation point that is not within boundary
Point2D.Double newLocation = new Point2D.Double(-1,-1);
// loop until generated point is within boundary
while(!isPointWithinBoundary(newLocation)){
String prevDirection = "none";
int newX = this.X;
int newY = this.Y;
// int speed = (int)RandTool.gaussian(maxSpeed/2.0, 1);
int speed = maxSpeed;
// get previous direction of travel from X,Y and prevX and prevY
if(this.X - this.prevX == 0 && this.Y - this.prevY < 0){
prevDirection = "north";
}
else if(this.X - this.prevX == 0 && this.Y - this.prevY > 0){
prevDirection = "south";
}
else if(this.X - this.prevX < 0 && this.Y - this.prevY == 0){
prevDirection = "west";
}
else if(this.X - this.prevX > 0 && this.Y - this.prevY == 0){
prevDirection = "east";
}
// chance of this animal moving in the same direction
double sameDirectionChance = 0.8;
// chance of this animal staying in the same location
if(prevDirection == "none"){
sameDirectionChance = 0.4;
}
if(Math.random() < sameDirectionChance){
// continue moving in same direction as previous move
if(prevDirection == "north"){
newY -= speed;
}
else if(prevDirection == "south"){
newY += speed;
}
else if(prevDirection == "east"){
newX += speed;
}
else if(prevDirection == "west"){
newX -= speed;
}
else if(prevDirection == "none"){
// do nothing
}
}
else{
// change direction
double newDirection = Math.random();
if(newDirection < 0.2){ // north
newY -= speed;
}
else if(newDirection < 0.4){ // south
newY += speed;
}
else if(newDirection < 0.6){ // east
newX += speed;
}
else if(newDirection < 0.8){ // west
newX -= speed;
}
else if(newDirection < 1.0){ // same spot
// do nothing
}
}
newLocation = new Point2D.Double(newX,newY);
}
return newLocation;
}
public Point2D.Double hunt(){ // Called in move()
// System.out.println(" this.X="+this.X+" this.Y="+this.Y);
Point2D.Double preyXY = prey.getXY();
double xDist = this.X - preyXY.x;
double yDist = this.Y - preyXY.y;
if(Math.abs(xDist) > maxSpeed){
if(xDist < 0){
xDist = -1*maxSpeed;
}
else{
xDist = maxSpeed;
}
}
if(Math.abs(yDist) > maxSpeed){
if(yDist < 0){
yDist = -1*maxSpeed;
}
else{
yDist = maxSpeed;
}
}
// System.out.println(" xDist:"+xDist+" yDist:"+yDist);
if(Math.abs(xDist) >= Math.abs(yDist)){
// move along x at top speed
return (new Point2D.Double(this.X-xDist, this.Y));
}
else{
// move along y at top speed
return (new Point2D.Double(this.X, this.Y-yDist));
}
}
public Point2D.Double escape(){ // Called in move()
// System.out.println(" this.X="+this.X+" this.Y="+this.Y);
double distance = getXY().distance(predator.getXY());
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
Point2D.Double predatorXY = predator.getXY();
double xDist = this.X - predatorXY.x;
double yDist = this.Y - predatorXY.y;
if(Math.abs(xDist) > maxSpeed){
if(xDist < 0){
xDist = -1*maxSpeed;
}
else{
xDist = maxSpeed;
}
}
if(Math.abs(yDist) > maxSpeed){
if(yDist < 0){
yDist = -1*maxSpeed;
}
else{
yDist = maxSpeed;
}
}
// System.out.println(" xDist:"+xDist+" yDist:"+yDist);
if(Math.abs(xDist) >= Math.abs(yDist)){
// move along x at top speed
return (new Point2D.Double(this.X-xDist, this.Y));
}
else{
// move along y at top speed
return (new Point2D.Double(this.X, this.Y-yDist));
}
}
public ArrayList<Organism> getOrganismsWithinSightRadius(ArrayList<Organism> organisms){
// Create emtpy list of organisms to fill up then return
ArrayList<Organism> organismsWithinRadius = new ArrayList<Organism>();
//iterate through all organisms
for (Organism o : organisms){
double distToCurrPoint = this.getXY().distance(o.getXY());
if(distToCurrPoint <= this.getSightRadius() && o.getID() != this.id){
organismsWithinRadius.add(o);
}
}
return organismsWithinRadius;
}
public ArrayList<Organism> getNearbyPrey(ArrayList<Organism> nearbyOrganisms){
/* Argument is result of calling getOrganismsWithinSightRadius() */
// create empty list of organisms to fill up then return
ArrayList<Organism> nearbyPrey = new ArrayList<Organism>();
// iterate through nearbyOrganisms
for(Organism o : nearbyOrganisms){
if( this.preyTypes.contains(o.getType()) ){
nearbyPrey.add(o);
}
}
return nearbyPrey;
}
public Organism getClosestPrey(ArrayList<Organism> nearbyPrey){
Organism org = nearbyPrey.get(0);
double dist = getXY().distance(org.getXY());
// find closest prey
for(Organism o : nearbyPrey){
double d = getXY().distance(o.getXY());
if(d < dist){
dist = d;
org = o;
}
}
return org;
}
public ArrayList<Organism> getNearbyPredators(ArrayList<Organism> nearbyOrganisms){
/* Argument is result of calling getOrganismsWithinSightRadius() */
// create empty list of organisms to fill up then return
ArrayList<Organism> nearbyPredators = new ArrayList<Organism>();
// iterate through nearbyOrganisms
for(Organism o : nearbyOrganisms){
if( this.predatorTypes.contains(o.getType()) ){
nearbyPredators.add(o);
}
}
return nearbyPredators;
}
public Organism getClosestPredator(ArrayList<Organism> nearbyPredators){
Organism org = nearbyPredators.get(0);
double dist = getXY().distance(org.getXY());
// find closest predator
for(Organism o : nearbyPredators){
double d = getXY().distance(o.getXY());
if(d < dist){
dist = d;
org = o;
}
}
return org;
}
public void eatPrey(){
// System.out.println(this.type+" id:"+this.id + " eating...");
if(health < maxHealth && prey.getHealth() > 0){
health += healthGainedEatingPerGameTick;
double h = prey.getHealth() - healthGainedEatingPerGameTick;
prey.setHealth(h);
}
else{
// finish eating, go back to random walk
state = 0;
}
}
public boolean isPointWithinBoundary(Point2D.Double point){
/* Check to see if a point is within screen boundary */
int x = (int)point.x;
int y = (int)point.y;
int screenWidth = D.width;
int screenHeight = D.height;
if(x >= 0 && y >= 0 && x < screenWidth && y < screenHeight){
return true;
}
else{
return false;
}
}
public double generateRandomInitialHealth(){ // Called by constructor
// generate a starting health point value between hungerHealth and maxHealth
Random r = new Random();
double rangeMin = (hungerHealth);
double range = maxHealth - rangeMin + 1.0;
double randomHealth = rangeMin + (range) * r.nextDouble();
return randomHealth;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* ~~~~~~~~~~ Get and Set (These should be the same for all organisms) ~~~~~~~~ */
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
// Get
public int getID(){
return this.id;
}
public String getType(){
return this.type;
}
public int getX(){
return this.X;
}
public int getY(){
return this.Y;
}
public Point2D.Double getXY(){
return (new Point2D.Double(this.X, this.Y));
}
public double getHealth(){
return this.health;
}
public double getMaxHealth(){
return this.maxHealth;
}
public int getSightRadius(){
return this.sightRadius;
}
public int getState(){
return state;
}
public boolean isGivingBirth(){
if(Math.random() <= probabilityGivingBirth){
return true;
}
return false;
}
public int getNumBabiesProduced(){
return (int)RandTool.gaussian(avgNumBabies, 1);
}
// Set
public void setX(int x){
this.X = x;
}
public void setY(int y){
this.Y = y;
}
public void setXY(Point2D.Double point){
// update prevX and prevY
this.prevX = this.X;
this.prevY = this.Y;
// set new X and Y
this.X = (int)point.x;
this.Y = (int)point.y;
}
public void setTargetLocation(Point2D.Double point){
targetLocation = new Point2D.Double(point.x, point.y);
}
public void setHealth(double health) {
this.health = health;
}
// To String ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public String toString(){
return this.type + " " + this.id;
}
}