File: org.cc

package info (click to toggle)
achilles 2-13
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 684 kB
  • sloc: sh: 2,600; cpp: 2,069; xml: 25; makefile: 7
file content (132 lines) | stat: -rw-r--r-- 2,913 bytes parent folder | download | duplicates (8)
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
/* 

Copyright (C) 2000 Matthew Danish

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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
using namespace std;
#include<iostream>
#include"org.h"
#include"id.h"
#include"energy.h"
#include"hebbian.h"
#include"defines.h"

OrganismClass::OrganismClass(IdToken *token,
			     VectorClass *_position,
			     AngleClass *_heading,
			     GeneClass *_genes) :
  id(new IdClass(*token)),
  position(*_position),
  heading(*_heading),
  genes(*_genes)
{
  delete token;
  delete _position;
  delete _heading;
  delete _genes;
  size.X(genes.Size().X());
  size.Y(genes.Size().Y());
  size.Z(genes.Size().Z());
  lifespan = long(double(genes.Lifespan()) * (2 - genes.Metabolism()));
  energy = new EnergyClass(size);
  brain = new NeuralNet(genes.Brain().NumNeurodes(),
			genes.Brain().NumLayers(),
			genes.Brain().NumInputs(),
			genes.Brain().NumOutputs(),
			genes.Brain().TopoDist());
  Type(ORGANISM_LIVE);
}

OrganismClass::~OrganismClass() {
  delete id;
  delete energy;
  delete brain;
}

IdClass & OrganismClass::Id() {
  return *id;
}

VectorClass & OrganismClass::Pos() {
  return position;
}

AngleClass & OrganismClass::Heading() {
  return heading;
}

NeuralNet & OrganismClass::Brain() {
  return *brain;
}

EnergyClass & OrganismClass::Energy() {
  return *energy;
}

GeneClass & OrganismClass::Genes() {
  return genes;
}

VectorClass & OrganismClass::Size() {
  return size;
}

ColorClass & OrganismClass::Color() {
  return color;
}

short OrganismClass::Type() {
  return type;
}

short OrganismClass::Type(short t) {
  if(t==ORGANISM_LIVE) {
    color.R(0);
    color.G(0);
    color.B(genes.Color()*3/4 + 1/4);
  } else if(t==ORGANISM_FOOD) {
    color.R(1);
    color.G(1);
    color.B(0);
    lifespan = DECAY_SPAN;
  } else return type;
  type=t;
  return type;
}
  
bool OrganismClass::Lifetick() {

  if(type==ORGANISM_LIVE) {
    // Regenerate some health
    Energy().Regen(Genes().Metabolism());
    
    
    if(!--lifespan) {
      Type(ORGANISM_FOOD);
      Energy().TakeDamage(Energy().Health());
      return false;
    }
    if(Energy().TakeDamage(Genes().Metabolism() * ENERGY_LOSS_PER_TICK * Energy().HealthCap())==false) {
      Type(ORGANISM_FOOD);
      return false;
    }
  } else {
    if(--lifespan<=0) return false;
  }

  return true;
}