File: Organism.java

package info (click to toggle)
biogenesis 0.8-3.1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 864 kB
  • sloc: java: 6,254; xml: 53; makefile: 51; sh: 4
file content (1268 lines) | stat: -rw-r--r-- 41,352 bytes parent folder | download | duplicates (4)
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
/* Copyright (C) 2006-2010  Joan Queralt Molina
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */
package biogenesis;

import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
/**
 * This class implements an organism.
 * The body of the organism is drawn inside the Rectangle from which it inherits.
 */
public class Organism extends Rectangle {
	/**
	 * The version of this class
	 */
	private static final long serialVersionUID = Utils.FILE_VERSION;
	/**
	 * A reference to the genetic code of this organism
	 */
	protected GeneticCode _geneticCode;
	/**
	 * If this organism has been infected by a white segment, here we have the
	 * genetic code that this organism will reproduce.
	 */
	protected GeneticCode _infectedGeneticCode = null;
	/**
	 * Number of children that this organism will produce at once. This
	 * is the number of yellow segments in its genetic code with a
	 * maximum of 8 and a minimum of 1.
	 */
	protected int _nChildren;
	/**
	 * Reference to the world where the organism lives.
	 */
	protected World _world;
	/**
	 * Reference to the visual part of the world where the organism lives.
	 */
	transient protected VisibleWorld _visibleWorld;
	/**
	 * Identification number of this organism's parent.
	 */
	protected int _parentID;
	/**
	 * Identification number of this organism.
	 */
	protected int _ID;
	/**
	 * Generation number
	 */
	protected int _generation;
	/**
	 * Number of children it has produced.
	 */
	protected int _nTotalChildren=0;
	/**
	 * Number of organism that has killed
	 */
	protected int _nTotalKills=0;
	/**
	 * Number of organism that has infected
	 */
	protected int _nTotalInfected=0;
	/**
	 * X coordinates of the starting point of each organism's segments.
	 */
	protected int[] _startPointX;
	/**
	 * Y coordinates of the starting point of each organism's segments.
	 */
	protected int[] _startPointY;
	/**
	 * X coordinates of the ending point of each organism's segments.
	 */
	protected int[] _endPointX;
	/**
	 * Y coordinates of the ending point of each organism's segments.
	 */
	protected int[] _endPointY;
	/**
	 * Precalculated distance from the origin to the starting point of each segment.
	 * Used to calculate rotations.
	 */
	protected double[] _m1;
	/**
	 * Precalculated distance from the origin to the ending point of each segment.
	 * Used to calculate rotations.
	 */
	protected double[] _m2;
	/**
	 * Precalculated modulus of each segment.
	 */
	protected double[] _m;
	/**
	 * X coordinate of this organim's center of gravity.
	 */
	protected int _centerX;
	/**
	 * Y coordinate of this organim's center of gravity.
	 */
	protected int _centerY;
	/**
	 * Like _centerX but with double precision to be able to make movements slower than a pixel.
	 */
	protected double _dCenterX;
	/**
	 * Like _centerY but with double precision to be able to make movements slower than a pixel.
	 */
	protected double _dCenterY;
	/**
	 * Effective segment colors, taken from the genetic code if alive or brown if dead.
	 */
	protected Color[] _segColor;
	/**
	 * The total number of segments of the organism
	 */
	protected int _segments;
	/**
	 * Growth ratio of the organism. Used to calculate segments when the organism is not
	 * fully grown.
	 */
	protected int _growthRatio;
	/**
	 * Total mass of this organism. The mass is calculated as the sum of all segment lengths.
	 * Used to calculate the effect of collisions.
	 */
	protected double _mass = 0;
	/**
	 * Moment of inertia of this organism, used to calculate the effect of collisions.
	 */
	protected double _I = 0;
	/**
	 * Chemical energy stored by this organism
	 */
	protected double _energy;
	/**
	 * Organism size independent on its position in the world.
	 * Let p be a point in the organism. Then, p.x + x - _sizeRect.x is the x coordinate
	 * of p representation in the world.
	 */
	protected Rectangle _sizeRect = new Rectangle();
	/**
	 * Rotation angle that this organism has at a given moment.
	 */
	protected double _theta;
	/**
	 * Last frame angle, used to avoid calculating point rotations when the angle doesn't
	 * change between two consecutive frames.
	 */
	protected double _lastTheta = -1;
	/**
	 * Rotated segments of the last frame, to use when _theta == _lastTheta
	 */
	protected int x1[],y1[],x2[],y2[];
	/**
	 * Speed. Variation applied to organism coordinates at every frame.
	 */
	protected double dx=0d, dy=0d;
	/**
	 * Angular speed. Organism angle variation at every frame.
	 */
	protected double dtheta = 0d;
	/**
	 * Number of frames of life of this organism
	 */
	protected int _age=0;
	/**
	 * Color used to draw the organism when a collision occurs. We save the color that
	 * should be shown and the number of frames that it should be shown. If the number
	 * if frames is 0, each segment is shown in its color.
	 */
	protected Color _color;
	/**
	 * Number of frames in which the organism will be drawn in _color.
	 */
	protected int _framesColor = 0;
	/**
	 * Number of frame that need to pass between two reproductions, even they are not
	 * successfully.
	 */
	protected int _timeToReproduce = 0;
	/**
	 * Indicates if the organism has grown at the last frame. If it has grown it is
	 * necessary to recalculate its segments.
	 */
	protected int hasGrown;
	/**
	 * Indicates if it has moved at the last frame. If it has moved it is necessary
	 * to repaint it.
	 */
	protected boolean hasMoved = true;
	/**
	 * The place that this organism occupies at the last frame. If the organism
	 * moves, this rectangle must be painted too.
	 */
	protected Rectangle lastFrame = new Rectangle();
	/**
	 * Indicates if the organism is alive.
	 */
	protected boolean alive = true;
	private static transient Vector2D v = new Vector2D();
	/**
	 * Returns true if this organism is alive, false otherwise.
	 * 
	 * @return  true if this organism is alive, false otherwise.
	 */
	public boolean isAlive() {
		return alive;
	}
	/**
	 * Returns the amount of chemical energy stored by this organism.
	 * 
	 * @return  The amount of chemical energy stored by this organism.
	 */
	public double getEnergy() {
		return _energy;
	}
	/**
	 * Returns the identification number of this organism.
	 * 
	 * @return  The identification number of this organism.
	 */
	public int getID() {
		return _ID;
	}
	/**
	 * Returns the identification number of this organism's parent.
	 * 
	 * @return  The identification number of this organism's parent.
	 */
	public int getParentID() {
		return _parentID;
	}
	/**
	 * Returns the generation number of this organism.
	 * 
	 * @return  The generation number of this organism.
	 */
	public int getGeneration() {
		return _generation;
	}
	/**
	 * Returns the age of this organism.
	 * 
	 * @return  The age of this organism, in number of frames.
	 */
	public int getAge() {
		return _age;
	}
	/**
	 * Returns the number of children that this organism produced.
	 * 
	 * @return  The number of children that this organism produced.
	 */
	public int getTotalChildren() {
		return _nTotalChildren;
	}
	/**
	 * Returns the number of organisms killed by this organism.
	 * 
	 * @return  The number of organisms killed by this organism.
	 */
	public int getTotalKills() {
		return _nTotalKills;
	}
	/**
	 * Returns the number of organisms infected by this organism.
	 * 
	 * @return  The number of organisms infected by this organism.
	 */
	public int getTotalInfected() {
		return _nTotalInfected;
	}
	/**
	 * Returns a reference to this organism's genetic code.
	 * 
	 * @return  A reference to this organism's genetic code.
	 */
	public GeneticCode getGeneticCode() {
		return _geneticCode;
	}
	/**
	 * Returns the total mass of this organism.
	 * 
	 * @return  The total mass of this organism calculated as the sum
	 * of all its segments length.
	 */
	public double getMass() {
		return _mass;
	}
	/**
	 * Basic constructor. Doesn't initialize it: use {@link randomCreate}
	 * or {@link inherit} to do this.
	 * 
	 * @param world  A reference to the world where this organism is in.
	 */
	public Organism(World world) {
		_world = world;
		_visibleWorld = world._visibleWorld;
		_theta = Utils.random.nextDouble() * Math.PI * 2d;
	}
	/**
	 * Construct an organism with a given genetic code. Doesn't initialize it:
	 * use {@link pasteOrganism} to do it. Use {@link World.addOrganism} to add
	 * it to the world.
	 * 
	 * @param world  A reference to the world where this organism is in.
	 * @param geneticCode  A reference to the genetic code of this organism.
	 */
	public Organism(World world, GeneticCode geneticCode) {
		_world = world;
		_visibleWorld = world._visibleWorld;
		_theta = Utils.random.nextDouble() * Math.PI * 2d;
		_geneticCode = geneticCode;
	}
	/**
	 * Creates all data structures of this organism. Must be used after the organism
	 * has a genetic code assigned.
	 */
	protected void create() {
		_segments = _geneticCode.getNGenes() * _geneticCode.getSymmetry();
		_segColor = new Color[_segments];
		for (int i = 0; i < _segments; i++)
			_segColor[i] = _geneticCode.getGene(i%_geneticCode.getNGenes()).getColor();
		_startPointX = new int[_segments];
		_startPointY = new int[_segments];
		_endPointX = new int[_segments];
		_endPointY = new int[_segments];
		_m1 = new double[_segments];
		_m2 = new double[_segments];
		_m = new double[_segments];
		x1 = new int[_segments];
		y1 = new int[_segments];
		x2 = new int[_segments];
		y2 = new int[_segments];
	}
	/**
	 * Initializes variables for a new random organism and finds a place
	 * to put it in the world.
	 * 
	 * @return  true if it found a place for this organism or false otherwise.
	 */
	public boolean randomCreate() {
		// Generates a random genetic code
		_geneticCode = new GeneticCode();
		// it has no parent
		_parentID = -1;
		_generation = 1;
		_growthRatio = 16;
		// initial energy
		_energy = Math.min(Utils.INITIAL_ENERGY,_world.getCO2());
		_world.decreaseCO2(_energy);
		_world.addO2(_energy);
		// initialize
		create();
		symmetric();
		// put it in the world
		return placeRandom();
	}
	/**
	 * Initializes variables for a new organism born from an existing
	 * organism. Generates a mutated genetic code based on the parent's one
	 * and finds a place in the world to put it.
	 * 
	 * @param parent  The organism from which this organism is born. 
	 * @return  true if it found a place for this organism or false otherwise.
	 */
	public boolean inherit(Organism parent, boolean first) {
		GeneticCode inheritGeneticCode;
		boolean ok = true;
		
		// Create the inherited genetic code
		if (parent._infectedGeneticCode != null)
			inheritGeneticCode = parent._infectedGeneticCode;
		else
			inheritGeneticCode = parent._geneticCode;
		_geneticCode = new GeneticCode(inheritGeneticCode);
		// Take a reference to the parent
		_parentID = parent.getID();
		_generation = parent.getGeneration() + 1;
		_growthRatio = 16;
		// Initial energy: minimum energy required to reproduce is divided
		// between all children and the parent.
		_energy = Math.min((inheritGeneticCode._reproduceEnergy / (double)(parent._nChildren + 1)), parent._energy);
		if (first || parent._energy >= _energy+Utils.YELLOW_ENERGY_CONSUMPTION) {
			// Initialize
			create();
			symmetric();
			// Put it in the world, near its parent
			ok = placeNear(parent);
			if (ok && !first)
				parent.useEnergy(Utils.YELLOW_ENERGY_CONSUMPTION);
		} else
			ok = false;
		
		return ok;
	}
	/**
	 * Places the organism at the specified position in the world and initializes its
	 * variables. The organism must has an assigned genetic code.
	 * 
	 * @param posx  The x coordinate of the position in the world we want to put this organism.
	 * @param posy  The y coordinate of the position in the world we want to put this organism.
	 * @return  true if there were enough space to put the organism, false otherwise.
	 */
	public boolean pasteOrganism(int posx, int posy) {
		_parentID = -1;
		_generation = 1;
		_growthRatio = 16;
		create();
		symmetric();
		_dCenterX = _centerX = posx;
		_dCenterY = _centerY = posy;
		calculateBounds(true);
		// Check that the position is inside the world
		if (isInsideWorld()) {
			// Check that the organism will not overlap other organisms
			if (_world.fastCheckHit(this) == null) {
				// Generem identificador
				_ID = _world.getNewId();
				_energy = Math.min(Utils.INITIAL_ENERGY,_world.getCO2());
				_world.decreaseCO2(_energy);
				_world.addO2(_energy);
				return true;
			}
		}
		// It can't be placed		
		return false;
	}
	/**
	 * Translates the genetic code of this organism to its segments representation in the world.
	 * Also, calculates some useful information like segments length, inertia, etc.
	 * This method must be called when an organism is firstly displayed on the world and every
	 * time it changes its size.
	 * inherit, randomCreate and pasteOrganism are the standard ways to add an organism to a world
	 * and they already call this method.
	 */
	public void symmetric() {
		int i,j,segment=0;
		int symmetry = _geneticCode.getSymmetry();
		int mirror = _geneticCode.getMirror();
		int sequence = _segments / symmetry;
		int left=0, right=0, top=0, bottom=0;
		int centerX, centerY;
		double cx, cy;

		for (i=0; i<symmetry; i++) {
			for (j=0; j<sequence; j++,segment++) {
				// Here, we take the vector that forms the segment, scale it depending on
				// the relative size of the organism and rotate it depending on the
				// symmetry and mirroring.
				v.setModulus(_geneticCode.getGene(j).getLength()/Utils.scale[_growthRatio-1]);
				if (j==0) {
					_startPointX[segment] = 0;
					_startPointY[segment] = 0;
					if (mirror == 0 || i%2==0)
						v.setTheta(_geneticCode.getGene(j).getTheta()+i*2*Math.PI/symmetry);
					else {
						v.setTheta(_geneticCode.getGene(j).getTheta()+(i-1)*2*Math.PI/symmetry);
						v.invertX();
					}
				} else {
					_startPointX[segment] = _endPointX[segment - 1];
					_startPointY[segment] = _endPointY[segment - 1];
					if (mirror == 0 || i%2==0)
						v.addDegree(_geneticCode.getGene(j).getTheta());
					else
						v.addDegree(-_geneticCode.getGene(j).getTheta());
				}
				// Apply the vector to the starting point to get the ending point.
				_endPointX[segment] = (int) Math.round(v.getX() + _startPointX[segment]);
				_endPointY[segment] = (int) Math.round(v.getY() + _startPointY[segment]);
			    // Calculate the bounding rectangle of this organism
			    left = Math.min(left, _endPointX[segment]);
			    right = Math.max(right, _endPointX[segment]);
			    top = Math.min(top, _endPointY[segment]);
			    bottom = Math.max(bottom, _endPointY[segment]);
			}
		}
		_sizeRect.setBounds(left, top, right-left+1, bottom-top+1);
		// image center
		centerX = (left+right)>>1;
		centerY = (top+bottom)>>1;
		_mass = 0;
		_I = 0;
		for (i=0; i<_segments; i++) {
			// express points relative to the image center
			_startPointX[i]-=centerX;
			_startPointY[i]-=centerY;
			_endPointX[i]-=centerX;
			_endPointY[i]-=centerY;
			// calculate points distance of the origin and modulus
			_m1[i] = Math.sqrt(_startPointX[i]*_startPointX[i]+_startPointY[i]*_startPointY[i]);
			_m2[i] = Math.sqrt(_endPointX[i]*_endPointX[i]+_endPointY[i]*_endPointY[i]);
			_m[i] = Math.sqrt(Math.pow(_endPointX[i]-_startPointX[i],2) + 
					Math.pow(_endPointY[i]-_startPointY[i],2));
			_mass += _m[i];
			// calculate inertia moment
			// the mass center of a segment is its middle point
			cx = (_startPointX[i] + _endPointX[i]) / 2d;
			cy = (_startPointY[i] + _endPointY[i]) / 2d;
			// add the effect of this segment, following the parallel axis theorem
			_I += Math.pow(_m[i],3)/12d +
				_m[i] * cx*cx + cy*cy;// mass * length^2 (center is at 0,0)
		}
	}
	/**
	 * Given a vector, calculates the resulting vector after a rotation, a scalation and possibly
	 * after mirroring it.
	 * The rotation degree and the mirroring is found using the Utils.degree array, where parameter
	 * mirror is the row and step is the column. The step represents the repetition of this vector
	 * following the organism symmetry.
	 * The scalation is calculated using the Utils.scale coefficients, using the organism's
	 * _growthRatio to find the appropriate value. 
	 * 
	 * @param p  The end point of the vector. The starting point is (0,0).
	 * @param step  The repetition of the vectors pattern  we are calculating.
	 * @param mirror  If mirroring is applied to this organism 1, otherwise 0.
	 * @return  The translated vector.
	 */
/*	private Vector2D translate(Point p, int step, int mirror) {
		if (p.x == 0 && p.y == 0)
			return new Vector2D();

		double px = p.x;
		double py = p.y;

		px /= Utils.scale[_growthRatio - 1];
		py /= Utils.scale[_growthRatio - 1];

		Vector2D v = new Vector2D(px,py);
		v.addDegree(Utils.degree[mirror][step]);

		if (Utils.invertX[mirror][step] != 0)
			v.invertX();
		if (Utils.invertY[mirror][step] != 0)
			v.invertY();

		return v;
	}*/
	/**
	 * Tries to find a spare place in the world for this organism and place it.
	 * It also generates an identification number for the organism if it can be placed
	 * somewhere.
	 * 
	 * @return  true if a suitable place has been found, false if not.
	 */
	private boolean placeRandom() {
		/* We try to place the organism in 12 different positions. If all of them
		 * are occupied, we return false.
		 */
		for (int i=12; i>0; i--) {
			/* Get a random point for the top left corner of the organism
			 * making sure it is inside the world.
			 */
			Point origin = new Point(
				Utils.random.nextInt(_world.getWidth()-_sizeRect.width),
				Utils.random.nextInt(_world.getHeight()-_sizeRect.height));
			setBounds(origin.x,origin.y,_sizeRect.width,_sizeRect.height);
			_dCenterX = _centerX = origin.x + (_sizeRect.width>>1);
			_dCenterY = _centerY = origin.y + (_sizeRect.height>>1);
			// Check that the position is not occupied.
			if (_world.fastCheckHit(this) == null) {
				// Generate an identification
				_ID = _world.getNewId();
				return true;
			}
		}
		// If we get here, we haven't find a place for this organism.
		return false;
	}
	/**
	 * Tries to find a spare place near its parent for this organism and place it.
	 * It also generates an identification number for the organism if it can be placed
	 * somewhere and substracts its energy from its parent's energy.
	 * 
	 * @return  true if a suitable place has been found, false if not.
	 */
	private boolean placeNear(Organism parent) {
		int nPos = Utils.random.nextInt(8);
		// Try to put it in any possible position, starting from a randomly chosen one.
		for (int nSide = 0; nSide < 8; nSide++) {
			// Calculate candidate position
			_dCenterX = parent._dCenterX + (parent.width / 2 + width / 2+ 1) * Utils.side[nPos][0]; 
			_dCenterY = parent._dCenterY + (parent.height / 2 + height / 2 + 1) * Utils.side[nPos][1];
			_centerX = (int) _dCenterX;
			_centerY = (int) _dCenterY;
			calculateBounds(true);
			// Check this position is inside the world.
			if (isInsideWorld()) {
				// Check that it doesn't overlap with other organisms.
				if (_world.fastCheckHit(this) == null) {
					if (parent._geneticCode.getDisperseChildren()) {
						dx = Utils.side[nPos][0];
						dy = Utils.side[nPos][1];
					} else {
						dx = parent.dx;
						dy = parent.dy;
					}
					// Generate an identification
					_ID = _world.getNewId();
					// Substract the energy from the parent
					parent._energy -= _energy;
					return true;
				}
			}
			nPos = (nPos + 1) % 8;
		}
		// It can't be placed.
		return false;
	}
	/**
	 * Draws this organism to a graphics context.
	 * The organism is drawn at its position in the world.
	 * 
	 * @param g  The graphics context to draw to.
	 */
	public void draw(Graphics g) {
		int i;
		if (_framesColor > 0) {
			// Draw all the organism in the same color
			g.setColor(_color);
			_framesColor--;
			for (i=0; i<_segments; i++)
				g.drawLine(
					x1[i] + _centerX,
					y1[i] + _centerY,
					x2[i] + _centerX,
					y2[i] + _centerY);
		} else {
			if (alive) {
				for (i=0; i<_segments; i++) {
					g.setColor(_segColor[i]);
					g.drawLine(
							x1[i] + _centerX,
							y1[i] + _centerY,
							x2[i] + _centerX,
							y2[i] + _centerY);
				}
			} else {
				g.setColor(Utils.ColorBROWN);
				for (i=0; i<_segments; i++) {
					g.drawLine(
							x1[i] + _centerX,
							y1[i] + _centerY,
							x2[i] + _centerX,
							y2[i] + _centerY);
				}
			}
		}
	}
	/**
	 * Calculates the position of all organism points in the world, depending on
	 * its rotation. It also calculates the bounding rectangle of the organism.
	 * This method must be called from outside this class only when doing
	 * manual drawing.  
	 * 
	 * @param force  To avoid calculations, segments position are only calculated
	 * if the organism's rotation has changed in the last frame. If it is necessary
	 * to calculate them even when the rotation hasn't changed, assign true to this
	 * parameter.
	 */
	public void calculateBounds(boolean force) {
		double left=java.lang.Double.MAX_VALUE, right=java.lang.Double.MIN_VALUE, 
		top=java.lang.Double.MAX_VALUE, bottom=java.lang.Double.MIN_VALUE;
		
		double theta;
		for (int i=_segments-1; i>=0; i--) {
			/* Save calculation: if rotation hasn't changed and it is not forced,
			 * don't calculate points again.
			 */
			if (_lastTheta != _theta || force) {
				theta=_theta+Math.atan2(_startPointY[i] ,_startPointX[i]);
				x1[i]=(int)(_m1[i]*Math.cos(theta));
				y1[i]=(int)(_m1[i]*Math.sin(theta));
				theta=_theta+Math.atan2(_endPointY[i], _endPointX[i]);
				x2[i]=(int)(_m2[i]*Math.cos(theta));
				y2[i]=(int)(_m2[i]*Math.sin(theta));
			}
			// Finds the rectangle that comprises the organism
			left = Utils.min(left, x1[i]+ _dCenterX, x2[i]+ _dCenterX);
			right = Utils.max(right, x1[i]+ _dCenterX, x2[i]+ _dCenterX);
			top = Utils.min(top, y1[i]+ _dCenterY, y2[i]+ _dCenterY);
			bottom = Utils.max(bottom, y1[i]+ _dCenterY, y2[i]+ _dCenterY);
		}
		setBounds((int)left, (int)top, (int)(right-left+1)+1, (int)(bottom-top+1)+1);
		_lastTheta = _theta;
	}
	/**
	 * If its the time for this organism to grow, calculates its new segments and speed.
	 * An alive organism can grow once every 8 frames until it gets its maximum size.
	 */
	private void grow() {
		if (_growthRatio > 1 && (_age & 0x07) == 0x07 && alive && _energy >= _mass/10) {
			_growthRatio--;
			double m = _mass;
			double I = _I;
			symmetric();
			// Cynetic energy is constant. If mass changes, speed must also change.
			m = Math.sqrt(m/_mass);
			dx *= m;
			dy *= m;
			dtheta *= Math.sqrt(I/_I);
			hasGrown = 1;
		} else {
			if (_growthRatio < 15 && _energy < _mass/12) {
				_growthRatio++;
				double m = _mass;
				double I = _I;
				symmetric();
				// Cynetic energy is constant. If mass changes, speed must also change.
				m = Math.sqrt(m/_mass);
				dx *= m;
				dy *= m;
				dtheta *= Math.sqrt(I/_I);
				hasGrown = -1;
			} else
				hasGrown = 0;
		}
	}
	
	/**
	 * Makes this organism reproduce. It tries to create at least one
	 * child and at maximum 8 (depending on the number of yellow segments
	 * of the organism) and put them in the world.
	 */
	public void reproduce() {
		Organism newOrg;
		
		for (int i=0; i < Utils.between(_nChildren,1,8); i++) {
			newOrg = new Organism(_world);
			if (newOrg.inherit(this, i==0)) {
				// It can be created
				_nTotalChildren++;
				_world.addOrganism(newOrg,this);
				_infectedGeneticCode = null;
			}
			_timeToReproduce = 20;
		}
	}
	/**
	 * Executes the organism's movement for this frame.
	 * This includes segments upkeep and activation,
	 * movement, growth, collision detection, reproduction,
	 * respiration and death.
	 */
	public boolean move() {
		boolean collision = false;
		hasMoved = false;
		lastFrame.setBounds(this);
		if (Math.abs(dx) < Utils.tol) dx = 0;
		if (Math.abs(dy) < Utils.tol) dy = 0;
		if (Math.abs(dtheta) < Utils.tol) dtheta = 0;
		// Apply segment effects for this frame.
		segmentsFrameEffects();
		// Apply rubbing effects
		rubbingFramesEffects();
		// Check if it can grow or shrink
		grow();
		// Movement
		double dxbak=dx, dybak=dy, dthetabak=dtheta;
		offset(dx,dy,dtheta);
		calculateBounds(hasGrown!=0);
		
		if (hasGrown!=0 || dx!=0 || dy!=0 || dtheta!=0) {
			hasMoved = true;
			// Check it is inside the world
			collision = !isInsideWorld();
			// Collision detection with biological corridors
			if (alive) {
				OutCorridor c = _world.checkHitCorridor(this);
				if (c != null && c.canSendOrganism()) {
					if (c.sendOrganism(this))
						return false;
				}
			}
			// Collision detection with other organisms.
			if (_world.checkHit(this) != null)
				collision = true;
			// If there is a collision, undo movement.
			if (collision) {
				hasMoved = false;
				offset(-dxbak,-dybak,-dthetabak);
				if (hasGrown!=0) {
					_growthRatio+=hasGrown;
					symmetric();
				}
				calculateBounds(hasGrown!=0);
			}
		}
		// Substract one to the time needed to reproduce
		if (_timeToReproduce > 0)
			_timeToReproduce--;
		// Check if it can reproduce: it needs enough energy and to be adult
		if (_energy > _geneticCode.getReproduceEnergy() + Utils.YELLOW_ENERGY_CONSUMPTION*(_nChildren-1)
				&& _growthRatio==1 && _timeToReproduce==0 && alive)
			reproduce();
		// Check that it don't exceed the maximum chemical energy
		if (_energy > 2*_geneticCode.getReproduceEnergy())
			useEnergy(_energy - 2*_geneticCode.getReproduceEnergy());
		// Maintenance
		breath();
		// Check that the organism has energy after this frame
		return _energy > Utils.tol;
	}
	/**
	 * Makes the organism spend an amount of energy using the
	 * respiration process.
	 * 
	 * @param q  The quantity of energy to spend.
	 * @return  true if the organism has enough energy and there are
	 * enough oxygen in the atmosphere, false otherwise.
	 */
	public boolean useEnergy(double q) {
		if (_energy < q) {
			return false;
		}
		double respiration = _world.respiration(q);
		_energy -= respiration;
		if (respiration < q)
			return false;
		return true;
	}
	/**
	 * Realize the respiration process to maintain its structure.
	 * Aging is applied here too.
	 */
	public void breath() {
		if (alive) {
			_age++;
			// Respiration process
			boolean canBreath = useEnergy(Math.min(_mass / Utils.SEGMENT_COST_DIVISOR, _energy));
			if ((_age >> 8) > _geneticCode.getMaxAge() || !canBreath) {
				// It's dead, but still may have energy
				die(null);
			} else {
				if (_energy <= Utils.tol) {
					alive = false;
					_world.decreasePopulation();
					_world.organismHasDied(this, null);
				}
			}
		} else {
			// The corpse slowly decays
			useEnergy(Math.min(_energy, Utils.DECAY_ENERGY));
		}
	}
	/**
	 * Kills the organism. Sets its segments to brown and tells the world
	 * about the event.
	 * 
	 * @param killingOrganism  The organism that has killed this organism,
	 * or null if it has died of natural causes.
	 */
	public void die(Organism killingOrganism) {
		alive = false;
		hasMoved = true;
		for (int i=0; i<_segments; i++) {
			_segColor[i] = Utils.ColorBROWN;
		}
		_world.decreasePopulation();
		if (killingOrganism != null)
			killingOrganism._nTotalKills++;
		_world.organismHasDied(this, killingOrganism);
	}
	/**
	 * Infects this organism with a genetic code.
	 * Tells the world about this event.
	 * Not currently used.
	 * 
	 * @param infectingCode  The genetic code that infects this organism.
	 */
	public void infectedBy(GeneticCode infectingCode) {
		_infectedGeneticCode = infectingCode;
		_world.organismHasBeenInfected(this, null);
	}
	/**
	 * Infects this organism with the genetic code of another organism.
	 * Tells the world about this event.
	 * 
	 * @param infectingOrganism  The organism that is infecting this one.
	 */
	public void infectedBy(Organism infectingOrganism) {
		infectingOrganism._nTotalInfected++;
		_infectedGeneticCode = infectingOrganism.getGeneticCode();
		_world.organismHasBeenInfected(this, infectingOrganism);
	}
	/**
	 * Calculates the resulting speeds after a collision between two organisms, following
	 * physical rules.
	 * 
	 * @param org  The other organism in the collision.
	 * @param p  Intersection point between the organisms.
	 * @param l  Line that has collided. Of the two lines, this is the one that collided
	 * on the center, not on the vertex.
	 * @param thisOrganism  true if l is a line of this organism, false if l is a line of org.
	 */
	private final void touchMove(Organism org, Point2D.Double p, Line2D l, boolean thisOrganism) {
		// Distance vector between centers of mass and p
		double rapx = p.x - _dCenterX;
		double rapy = p.y - _dCenterY;
		double rbpx = p.x - org._dCenterX;
		double rbpy = p.y - org._dCenterY;
		// Speeds of point p in the body A and B, before collision.
		double vap1x = dx - dtheta * rapy + hasGrown*rapx/10d;
		double vap1y = dy + dtheta * rapx + hasGrown*rapy/10d;
		double vbp1x = org.dx - org.dtheta * rbpy;
		double vbp1y = org.dy + org.dtheta * rbpx;
		// Relative speeds between the two collision points.
		double vab1x = vap1x - vbp1x;
		double vab1y = vap1y - vbp1y;
		// Normal vector to the impact line
		//First: perpendicular vector to the line
		double nx = l.getY1() - l.getY2();
		double ny = l.getX2() - l.getX1();
		//Second: normalize, modulus 1
		double modn = Math.sqrt(nx * nx + ny * ny);
		nx /= modn;
		ny /= modn;
		/*Third: of the two possible normal vectors we need the one that points to the
		 * outside; we choose the one that its final point is the nearest to the center
		 * of the other line.
		 */
		if (thisOrganism) {
			if ((p.x+nx-org._dCenterX)*(p.x+nx-org._dCenterX)+(p.y+ny-org._dCenterY)*(p.y+ny-org._dCenterY) <
				(p.x-nx-org._dCenterX)*(p.x-nx-org._dCenterX)+(p.y-ny-org._dCenterY)*(p.y-ny-org._dCenterY)) {
				nx = -nx;
				ny = -ny;
			}
		} else {
			if ((p.x+nx-_dCenterX)*(p.x+nx-_dCenterX)+(p.y+ny-_dCenterY)*(p.y+ny-_dCenterY) >
				(p.x-nx-_dCenterX)*(p.x-nx-_dCenterX)+(p.y-ny-_dCenterY)*(p.y-ny-_dCenterY)) {
				nx = -nx;
				ny = -ny;
			}
		}
		// This is the j in the parallel axis theorem
		double j = (-(1+Utils.ELASTICITY) * (vab1x * nx + vab1y * ny)) / 
			(1/_mass + 1/org._mass + Math.pow(rapx * ny - rapy * nx, 2) / _I +
					Math.pow(rbpx * ny - rbpy * nx, 2) / org._I);
		// Final speed
		dx = Utils.between(dx + j*nx/_mass, -Utils.MAX_VEL, Utils.MAX_VEL);
		dy = Utils.between(dy + j*ny/_mass, -Utils.MAX_VEL, Utils.MAX_VEL);
		org.dx = Utils.between(org.dx - j*nx/org._mass, -Utils.MAX_VEL, Utils.MAX_VEL);
		org.dy = Utils.between(org.dy - j*ny/org._mass, -Utils.MAX_VEL, Utils.MAX_VEL);
		dtheta = Utils.between(dtheta + j * (rapx * ny - rapy * nx) / _I, -Utils.MAX_ROT, Utils.MAX_ROT);
		org.dtheta = Utils.between(org.dtheta - j * (rbpx * ny - rbpy * ny) / org._I, -Utils.MAX_ROT, Utils.MAX_ROT);
	}
	/**
	 * Checks if the organism is inside the world. If it is not, calculates its
	 * speed after the collision with the world border.
	 * This calculation should be updated to follow the parallel axis theorem, just
	 * like the collision between two organisms.
	 * 
	 * @return  true if the organism is inside the world, false otherwise.
	 */
	private final boolean isInsideWorld() {
		// Check it is inside the world
		if (x<0 || y<0 || x+width>=_world.getWidth() || y+height>=_world.getHeight()) {
			// Adjust direction
			if (x < 0 || x + width >= _world.getWidth())
				dx = -dx;
			if (y < 0 || y + height >= _world.getHeight())
				dy = -dy;
			dtheta = 0;
			return false;
		}
		return true;
	}
	/**
	 * Moves the organism and rotates it.
	 * 
	 * @param offsetx  displacement on the x axis.
	 * @param offsety  displacement on the y axis.
	 * @param offsettheta  rotation degree.
	 */
	private final void offset(double offsetx, double offsety, double offsettheta) {
		_dCenterX += offsetx; _dCenterY += offsety; _theta += offsettheta;
		_centerX = (int)_dCenterX; _centerY = (int)_dCenterY; 
	}
	/**
	 * Finds if two organism are touching and if so applies the effects of the
	 * collision.
	 * 
	 * @param org  The organism to check for collisions.
	 * @return  true if the two organisms are touching, false otherwise.
	 */
	public final boolean contact(Organism org) {
		int i,j;
		ExLine2DDouble line = new ExLine2DDouble();
		ExLine2DDouble bline = new ExLine2DDouble();
		// Check collisions for all segments
		for (i = _segments-1; i >= 0; i--) {
			// Consider only segments with modulus greater than 1
			if (_m[i]>=1) { 
				line.setLine(x1[i]+_centerX, y1[i]+_centerY, x2[i]+_centerX, y2[i]+_centerY);
				// First check if the line intersects the bounding box of the other organism
				if (org.intersectsLine(line)) {
					// Do the same for the other organism's segments.
					for (j = org._segments-1; j >= 0; j--) {
						if (org._m[j]>=1) {
							bline.setLine(org.x1[j] + org._centerX, org.y1[j] + org._centerY,
									org.x2[j] + org._centerX, org.y2[j] + org._centerY);
							if (intersectsLine(bline) && line.intersectsLine(bline)) {
								// If we found two intersecting segments, apply effects
								touchEffects(org,i,j,true);
								// Intersection point
								Point2D.Double intersec= line.getIntersection(bline);
								/* touchMove needs to know which is the line that collides from the middle (not
								 * from a vertex). Try to guess it by finding the vertex nearest to the
								 * intersection point.
								 */
								double dl1, dl2, dbl1, dbl2;
								dl1 = intersec.distanceSq(line.getP1());
								dl2 = intersec.distanceSq(line.getP2());
								dbl1 = intersec.distanceSq(bline.getP1());
								dbl2 = intersec.distanceSq(bline.getP2());
								// Use this to send the best choice to touchMove
								if (Math.min(dl1, dl2) < Math.min(dbl1, dbl2))
									touchMove(org,intersec,bline,false);
								else
									touchMove(org,intersec,line,true);
								// Find only one collision to speed up.
								return true;
							}
						}
					}
				}
			}
		}
		return false;
	}
	/**
	 * Applies the effects produced by two touching segments.
	 * 
	 * @param org  The organism which is touching.
	 * @param seg  Index of this organism's segment. 
	 * @param oseg  Index of the other organism's segment.
	 * @param firstCall  Indicates if this organism is the one that has detected the collision
	 * or this method is called by this same method in the other organism. 
	 */
	private final void touchEffects(Organism org, int seg, int oseg, boolean firstCall) {
		if ((_parentID == org._ID || _ID == org._parentID) && org.alive)
			return;
		double takenEnergy = 0;
		switch (getTypeColor(_segColor[seg])) {
		case RED:
		// Red segment: try to get energy from the other organism
			// If the other segment is blue, it acts as a shield
			switch (getTypeColor(org._segColor[oseg])) {
			case BLUE:
				if (org.useEnergy(Utils.BLUE_ENERGY_CONSUMPTION)) {
					org.setColor(Color.BLUE);
				} else {
					// Doesn't have energy to use the shield
					if (useEnergy(Utils.RED_ENERGY_CONSUMPTION)) {
						// Get energy depending on segment length
						takenEnergy = Utils.between(_m[seg] * Utils.ORGANIC_OBTAINED_ENERGY, 0, org._energy);
						// The other organism will be shown in yellow
						org.setColor(Color.YELLOW);						
					}	
				}
				break;
			case RED:
				if (useEnergy(Utils.RED_ENERGY_CONSUMPTION)) {
					// Get energy depending on segment length
					takenEnergy = Utils.between(_m[seg] * Utils.ORGANIC_OBTAINED_ENERGY, 0, org._energy);
					// The other organism will be shown in red
					org.setColor(Color.RED);
				}
				break;
			default:
				if (useEnergy(Utils.RED_ENERGY_CONSUMPTION)) {
					// Get energy depending on segment length
					takenEnergy = Utils.between(_m[seg] * Utils.ORGANIC_OBTAINED_ENERGY, 0, org._energy);
					// The other organism will be shown in yellow
					org.setColor(Color.YELLOW);
				}
			}
			// energy interchange
			org._energy -= takenEnergy;
			_energy += takenEnergy;
			double CO2freed = takenEnergy * Utils.ORGANIC_SUBS_PRODUCED;
			useEnergy(CO2freed);
			// This organism will be shown in red
			setColor(Color.RED);
			break;
		case WHITE:
			// White segment: try to infect the other organism
			switch (getTypeColor(org._segColor[oseg])) {
			case BLUE:
				if (org.useEnergy(Utils.BLUE_ENERGY_CONSUMPTION)) {
					setColor(Color.WHITE);
					org.setColor(Color.BLUE);
				} else {
					if (org._infectedGeneticCode != _geneticCode) {
						if (useEnergy(Utils.WHITE_ENERGY_CONSUMPTION)) {
							org.infectedBy(this);
							org.setColor(Color.YELLOW);
							setColor(Color.WHITE);
						}
					}	
				}
				break;
			case BROWN:
				break;
			default:
				if (org._infectedGeneticCode != _geneticCode) {
					if (useEnergy(Utils.WHITE_ENERGY_CONSUMPTION)) {
						org.infectedBy(this);
						org.setColor(Color.YELLOW);
						setColor(Color.WHITE);
					}
				}
			}
			break;
		case GRAY:
			switch (getTypeColor(org._segColor[oseg])) {
			case BLUE:
				if (org.useEnergy(Utils.BLUE_ENERGY_CONSUMPTION)) {
					org.setColor(Color.BLUE);
					setColor(Color.GRAY);
				} else {
					if (useEnergy(Utils.GRAY_ENERGY_CONSUMPTION)) {
						org.die(this);
						setColor(Color.GRAY);
					}
				}
				break;
			case BROWN:
				break;
			default:
				if (useEnergy(Utils.GRAY_ENERGY_CONSUMPTION)) {
					org.die(this);
					setColor(Color.GRAY);
				}
			}
			break;
		}
		// Check if the other organism has died
		if (org.isAlive() && org._energy < Utils.tol) {
			org.die(this);
		}
		if (firstCall)
			org.touchEffects(this, oseg, seg, false);
	}
	
	/*
	 * Perd velocitat pel fregament.
	 */
	private final void rubbingFramesEffects() {
		dx *= Utils.RUBBING;
		if (Math.abs(dx) < Utils.tol) dx=0;
		dy *= Utils.RUBBING;
		if (Math.abs(dy) < Utils.tol) dy = 0;
		dtheta *= Utils.RUBBING;
		if (Math.abs(dtheta) < Utils.tol) dtheta = 0;
	}
	
	/*
	 * Perd el cost de manteniment dels segments
	 * Aplica l'efecte de cadascun dels segments
	 */
	private final void segmentsFrameEffects() {
		if (alive) {
			int i;
			// Energy obtained through photosynthesis
			double photosynthesis = 0;
			_nChildren = 1;
			for (i=_segments-1; i>=0; i--) {
				// Manteniment
				switch (getTypeColor(_segColor[i])) {
				// 	Segments cilis
				case CYAN:
					if (Utils.random.nextInt(100)<8 && useEnergy(Utils.CYAN_ENERGY_CONSUMPTION)) {
						dx=Utils.between(dx+12d*(x2[i]-x1[i])/_mass, -Utils.MAX_VEL, Utils.MAX_VEL);
						dy=Utils.between(dy+12d*(y2[i]-y1[i])/_mass, -Utils.MAX_VEL, Utils.MAX_VEL);
						dtheta=Utils.between(dtheta+Utils.randomSign()*_m[i]*Math.PI/_I, -Utils.MAX_ROT, Utils.MAX_ROT);
					}
					break;
					// Segments fotosint�tics
				case GREEN:
					if (useEnergy(Utils.GREEN_ENERGY_CONSUMPTION))
						photosynthesis += _m[i];
					break;
					// Segments que obtenen energia de subs1
					// 	Segments relacionats amb la fertilitat
				case YELLOW:
					_nChildren++;
					break;
				}
			}
			// Photosynthesis process
			//Get sun's energy
			_energy += _world.photosynthesis(photosynthesis);
		}
	}
	
	private static final int NOCOLOR=-1;
	private static final int GREEN=0;
	private static final int RED=1;
	private static final int CYAN=2;
	private static final int BLUE=3;
	private static final int MAGENTA=4;
	private static final int PINK=5;
	private static final int ORANGE=6;
	private static final int WHITE=7;
	private static final int GRAY=8;
	private static final int YELLOW=9;
	private static final int BROWN=10;
	private static final int getTypeColor(Color c) {
		if (c.equals(Color.RED) || c.equals(Utils.ColorDARK_RED))
			return RED;
		if (c.equals(Color.GREEN) || c.equals(Utils.ColorDARK_GREEN))
			return GREEN;
		if (c.equals(Color.CYAN) || c.equals(Utils.ColorDARK_CYAN))
			return CYAN;
		if (c.equals(Color.BLUE) || c.equals(Utils.ColorDARK_BLUE))
			return BLUE;
		if (c.equals(Color.MAGENTA) || c.equals(Utils.ColorDARK_MAGENTA))
			return MAGENTA;
		if (c.equals(Color.PINK) || c.equals(Utils.ColorDARK_PINK))
			return PINK;
		if (c.equals(Color.ORANGE) || c.equals(Utils.ColorDARK_ORANGE))
			return ORANGE;
		if (c.equals(Color.WHITE) || c.equals(Utils.ColorDARK_WHITE))
			return WHITE;
		if (c.equals(Color.GRAY) || c.equals(Utils.ColorDARK_GRAY))
			return GRAY;
		if (c.equals(Color.YELLOW) || c.equals(Utils.ColorDARK_YELLOW))
			return YELLOW;
		if (c.equals(Utils.ColorBROWN))
			return BROWN;
		return NOCOLOR;
	}
	
	private final void setColor(Color c) {
		_color = c;
		_framesColor = 10;
	}
	
	public BufferedImage getImage() {
		BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
		Graphics2D g = image.createGraphics();
		g.setBackground(Color.BLACK);
		g.clearRect(0,0,width,height);
		for (int i=_segments-1; i>=0; i--) {
				g.setColor(_segColor[i]);
				g.drawLine(x1[i] -x + _centerX, y1[i] - y + _centerY, x2[i] - x + _centerX, y2[i] - y+_centerY);
		}
		return image;
	}
}