add support to SDLBackend for rendering 24bit data
[openc2e.git] / Creature.h
blob7a4e20d9b681aabff7d6b9dd88288bfa87b4c77f
1 /*
2 * Creature.h
3 * openc2e
5 * Created by Alyssa Milburn on Tue May 25 2004.
6 * Copyright (c) 2004-2006 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 __CREATURE_H
21 #define __CREATURE_H
23 #include "Agent.h"
24 #include "genome.h"
26 #include <deque>
28 class CreatureAgent;
29 class Creature;
31 class Creature {
32 protected:
33 CreatureAgent *parent;
34 shared_ptr<genomeFile> genome;
36 // non-specific bits
37 unsigned short genus;
38 unsigned int variant;
39 bool female;
41 // state
42 bool alive, asleep, dreaming, tickage;
43 bool zombie;
45 unsigned int ticks;
46 unsigned int age; // in ticks
47 lifestage stage;
49 AgentRef attention;
50 int attn, decn;
52 std::vector<AgentRef> chosenagents;
53 bool agentInSight(AgentRef a);
54 void chooseAgents();
55 virtual AgentRef selectRepresentativeAgent(int type, std::vector<AgentRef> possibles) { return AgentRef(); } // TODO: make pure virtual?
57 // linguistic stuff
59 // to-be-processed instincts
60 std::deque<creatureInstinctGene *> unprocessedinstincts;
62 // conscious flag? brain/motor enabled flags? flags for each 'faculty'?
64 unsigned short tintinfo[5]; // red, green, blue, rotation, swap
66 virtual void processGenes();
67 virtual void addGene(gene *);
69 Creature(shared_ptr<genomeFile> g, bool is_female, unsigned char _variant, CreatureAgent *a);
70 void finishInit();
72 public:
73 virtual ~Creature();
74 virtual void tick();
76 virtual void ageCreature();
77 lifestage getStage() { return stage; }
79 void setAsleep(bool asleep);
80 bool isAsleep() { return asleep; }
81 void setDreaming(bool dreaming);
82 bool isDreaming() { return dreaming; }
83 bool isFemale() { return female; }
84 bool isAlive() { return alive; }
85 void setZombie(bool z) { zombie = z; }
86 bool isZombie() { return zombie; }
87 unsigned int getAge() { return age; }
88 shared_ptr<genomeFile> getGenome() { return genome; }
90 unsigned short getGenus() { return genus; }
91 unsigned int getVariant() { return variant; }
92 unsigned short getTint(unsigned int id) { return tintinfo[id]; }
94 AgentRef getAttentionFocus() { return attention; }
95 int getAttentionId() { return attn; }
96 int getDecisionId() { return decn; }
98 virtual unsigned int getGait() = 0;
100 void born();
101 void die();
103 bool shouldProcessGene(gene *);
106 // c1
108 struct c1Reaction {
109 bioReactionGene *data;
110 void init(bioReactionGene *);
113 struct c1Receptor {
114 bioReceptorGene *data;
115 unsigned char *locus;
116 void init(bioReceptorGene *, class c1Creature *);
119 struct c1Emitter {
120 bioEmitterGene *data;
121 unsigned char *locus;
122 void init(bioEmitterGene *, class c1Creature *);
125 class oldCreature : public Creature {
126 protected:
127 // biochemistry
128 unsigned char chemicals[256];
130 class oldBrain *brain;
132 unsigned int biochemticks;
133 bioHalfLivesGene *halflives;
135 // loci
136 unsigned char floatingloci[8];
137 unsigned char muscleenergy;
138 unsigned char lifestageloci[7];
139 unsigned char fertile, receptive, pregnant;
140 unsigned char dead;
141 unsigned char involaction[8];
143 void addGene(gene *);
144 void tickBrain();
145 virtual void tickBiochemistry();
147 inline unsigned int calculateTickMask(unsigned char);
148 inline unsigned int calculateMultiplier(unsigned char);
150 oldCreature(shared_ptr<genomeFile> g, bool is_female, unsigned char _variant, CreatureAgent *a);
152 void processGenes();
154 public:
155 void addChemical(unsigned char id, unsigned char val);
156 void subChemical(unsigned char id, unsigned char val);
157 unsigned char getChemical(unsigned char id) { return chemicals[id]; }
159 // TODO: is it really worth having drives outside oldCreature?
160 virtual unsigned char getDrive(unsigned int id) = 0;
162 oldBrain *getBrain() { return brain; }
165 class c1Creature : public oldCreature {
166 protected:
167 std::vector<shared_ptr<c1Reaction> > reactions;
168 std::vector<c1Receptor> receptors;
169 std::vector<c1Emitter> emitters;
171 // loci
172 unsigned char senses[6];
173 unsigned char gaitloci[8];
174 unsigned char drives[16];
176 void addGene(gene *);
177 void tickBiochemistry();
178 void processReaction(c1Reaction &);
179 void processEmitter(c1Emitter &);
180 void processReceptor(c1Receptor &);
182 public:
183 c1Creature(shared_ptr<genomeFile> g, bool is_female, unsigned char _variant, CreatureAgent *a);
185 void tick();
187 unsigned char getDrive(unsigned int id) { assert(id < 16); return drives[id]; }
189 unsigned char *getLocusPointer(bool receptor, unsigned char o, unsigned char t, unsigned char l);
191 unsigned int getGait();
194 class c2Creature : public oldCreature {
195 protected:
196 // loci
197 unsigned char senses[14];
198 unsigned char gaitloci[16];
199 unsigned char drives[17];
200 unsigned char mutationchance, mutationdegree;
202 public:
203 c2Creature(shared_ptr<genomeFile> g, bool is_female, unsigned char _variant, CreatureAgent *a);
205 unsigned char getDrive(unsigned int id) { assert(id < 17); return drives[id]; }
207 unsigned int getGait();
210 // c2e
212 struct c2eReaction {
213 bioReactionGene *data;
214 float rate;
215 unsigned int receptors;
216 void init(bioReactionGene *);
219 struct c2eReceptor {
220 bioReceptorGene *data;
221 bool processed;
222 float lastvalue;
223 float *locus;
224 unsigned int *receptors;
225 float nominal, threshold, gain;
226 void init(bioReceptorGene *, class c2eOrgan *, shared_ptr<c2eReaction>);
229 struct c2eEmitter {
230 bioEmitterGene *data;
231 unsigned char sampletick;
232 float *locus;
233 float threshold, gain;
234 void init(bioEmitterGene *, class c2eOrgan *);
237 class c2eOrgan {
238 protected:
239 friend struct c2eReceptor;
240 friend struct c2eEmitter;
242 class c2eCreature *parent;
243 organGene *ourGene;
245 std::vector<shared_ptr<c2eReaction> > reactions;
246 std::vector<c2eReceptor> receptors;
247 std::vector<c2eEmitter> emitters;
249 // data
250 float energycost, atpdamagecoefficient;
252 // variables
253 float lifeforce, shorttermlifeforce, longtermlifeforce;
255 // locuses
256 float biotick, damagerate, repairrate, clockrate, injurytoapply;
257 unsigned int clockratereceptors, repairratereceptors, injuryreceptors;
259 void processReaction(c2eReaction &);
260 void processEmitter(c2eEmitter &);
261 void processReceptor(c2eReceptor &, bool checkchem);
263 float *getLocusPointer(bool receptor, unsigned char o, unsigned char t, unsigned char l, unsigned int **receptors);
265 public:
266 c2eOrgan(c2eCreature *p, organGene *g);
267 void tick();
269 void processGenes();
271 float getClockRate() { return clockrate; }
272 float getRepairRate() { return repairrate; }
273 float getDamageRate() { return damagerate; }
274 float getEnergyCost() { return energycost; }
275 float getInjuryToApply() { return injurytoapply; }
276 float getInitialLifeforce() { return lifeforce; }
277 float getShortTermLifeforce() { return shorttermlifeforce; }
278 float getLongTermLifeforce() { return longtermlifeforce; }
279 float getATPDamageCoefficient() { return atpdamagecoefficient; }
281 unsigned int getReceptorCount() { return receptors.size(); }
282 unsigned int getEmitterCount() { return emitters.size(); }
283 unsigned int getReactionCount() { return reactions.size(); }
285 void applyInjury(float);
288 struct c2eStim {
289 int noun_id;
290 float noun_amount;
291 int verb_id;
292 float verb_amount;
294 int drive_id[4];
295 float drive_amount[4];
296 bool drive_silent[4];
298 c2eStim() { noun_id = -1; verb_id = -1; drive_id[0] = -1; drive_id[1] = -1; drive_id[2] = -1; drive_id[3] = -1; }
299 void setupDriveStim(unsigned int num, int id, float amt, bool si) { drive_id[num] = id; drive_amount[num] = amt; drive_silent[num] = si; }
302 class c2eCreature : public Creature {
303 protected:
304 // brain config: should possibly be global
305 std::vector<unsigned int> mappinginfo;
307 // biochemistry
308 std::vector<shared_ptr<c2eOrgan> > organs;
309 float chemicals[256];
311 // loci
312 float lifestageloci[7];
313 float muscleenergy;
314 float floatingloci[32];
315 float fertile, pregnant, ovulate, receptive, chanceofmutation, degreeofmutation;
316 float dead;
317 float senses[14], involaction[8], gaitloci[16];
318 float drives[20];
320 unsigned int involactionlatency[8];
322 bioHalfLivesGene *halflives;
324 class c2eBrain *brain;
326 void tickBrain();
327 bool processInstinct();
328 void tickBiochemistry();
329 void processGenes();
330 void addGene(gene *);
332 int reverseMapVerbToNeuron(unsigned int verb);
333 AgentRef selectRepresentativeAgent(int type, std::vector<AgentRef> possibles);
335 public:
336 c2eCreature(shared_ptr<genomeFile> g, bool is_female, unsigned char _variant, CreatureAgent *a);
338 void tick();
340 void adjustChemical(unsigned char id, float value);
341 float getChemical(unsigned char id) { return chemicals[id]; }
342 void adjustDrive(unsigned int id, float value);
343 float getDrive(unsigned int id) { assert(id < 20); return drives[id]; }
345 void setInvolActionLatency(unsigned int id, unsigned int n) { assert(id < 8); involactionlatency[id] = n; }
347 void handleStimulus(c2eStim &stim);
348 void handleStimulus(unsigned int id, float strength);
350 unsigned int noOrgans() { return organs.size(); }
351 shared_ptr<c2eOrgan> getOrgan(unsigned int i) { assert(i < organs.size()); return organs[i]; }
353 class c2eBrain *getBrain() { return brain; }
355 float *getLocusPointer(bool receptor, unsigned char o, unsigned char t, unsigned char l);
357 unsigned int getGait();
360 #endif
362 /* vim: set noet: */