creaturesImage work: add mutableCopy and tint methods to the base class (and change...
[openc2e.git] / c2eBrain.h
blob82a0c5533411e57d3b67ae9dd9017a603aab88fe
1 /*
2 * c2eBrain.h
3 * openc2e
5 * Created by Alyssa Milburn on Wed Apr 11 2007.
6 * Copyright (c) 2007 Alyssa Milburn. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
20 #ifndef __C2EBRAIN_H
21 #define __C2EBRAIN_H
23 #include "genome.h"
24 #include <boost/shared_ptr.hpp>
25 #include <set>
26 #include <map>
28 using boost::shared_ptr;
30 class Creature;
32 struct c2ebraincomponentorder {
33 bool operator()(const class c2eBrainComponent *b1, const class c2eBrainComponent *b2) const;
36 class c2eBrainComponent {
37 protected:
38 friend class c2ebraincomponentorder;
40 uint8 updatetime;
41 class c2eBrain *parent;
42 bool inited;
44 public:
45 virtual void init() = 0;
46 virtual void tick() = 0;
47 uint8 getUpdateTime() { return updatetime; }
48 bool wasInited() { return inited; }
50 c2eBrainComponent(class c2eBrain *b) : parent(b) { assert(b); inited = false; }
51 virtual ~c2eBrainComponent() { }
54 struct c2erule {
55 uint8 opcode;
56 uint8 operandtype;
57 uint8 operanddata;
58 float operandvalue;
61 class c2eSVRule {
62 protected:
63 std::vector<c2erule> rules;
65 public:
66 void init(uint8 ruledata[48]);
67 bool runRule(float acc, float srcneuron[8], float neuron[8], float spareneuron[8], float dendrite[8], class c2eCreature *creature);
70 struct c2eNeuron {
71 float variables[8];
72 float input;
75 struct c2eDendrite {
76 float variables[8];
77 c2eNeuron *source, *dest;
80 class c2eLobe : public c2eBrainComponent {
81 protected:
82 c2eBrainLobeGene *ourGene;
83 c2eSVRule initrule, updaterule;
84 std::vector<c2eNeuron> neurons;
85 unsigned int spare;
87 public:
88 c2eLobe(class c2eBrain *b, c2eBrainLobeGene *g);
89 void tick();
90 void init();
91 void wipe();
92 c2eBrainLobeGene *getGene() { return ourGene; }
93 unsigned int getNoNeurons() { return neurons.size(); }
94 c2eNeuron *getNeuron(unsigned int i) { return &neurons[i]; }
95 unsigned int getSpareNeuron() { return spare; }
96 void setNeuronInput(unsigned int i, float input);
97 std::string getId();
100 class c2eTract : public c2eBrainComponent {
101 protected:
102 c2eBrainTractGene *ourGene;
103 c2eSVRule initrule, updaterule;
104 std::vector<c2eDendrite> dendrites;
105 std::vector<c2eNeuron *> src_neurons, dest_neurons;
107 void setupTract();
108 c2eDendrite *getDendriteFromTo(c2eNeuron *, c2eNeuron *);
109 void doMigration();
111 public:
112 c2eTract(class c2eBrain *b, c2eBrainTractGene *g);
113 void tick();
114 void init();
115 void wipe();
116 c2eBrainTractGene *getGene() { return ourGene; }
117 unsigned int getNoDendrites() { return dendrites.size(); }
118 c2eDendrite *getDendrite(unsigned int i) { return &dendrites[i]; }
120 std::string dump();
123 class c2eBrain {
124 protected:
125 class c2eCreature *parent;
127 std::multiset<c2eBrainComponent *, c2ebraincomponentorder> components;
129 public:
130 std::map<std::string, c2eLobe *> lobes;
131 std::vector<c2eTract *> tracts;
133 c2eBrain(c2eCreature *p);
134 void processGenes();
135 void tick();
136 void init();
137 c2eLobe *getLobeById(std::string id);
138 c2eLobe *getLobeByTissue(unsigned int id);
139 c2eCreature *getParent() { return parent; }
142 #endif
144 /* vim: set noet: */