add wraparound support to C2 physics
[openc2e.git] / Creature.h
blob48d8b2fbf2c91520104a747ed39b9b4d3c89eea8
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/c2
108 class oldCreature : public Creature {
109 protected:
110 // biochemistry
111 unsigned char chemicals[256];
113 class oldBrain *brain;
115 unsigned int biochemticks;
116 bioHalfLivesGene *halflives;
118 // loci
119 unsigned char floatingloci[8];
120 unsigned char muscleenergy;
121 unsigned char lifestageloci[7];
122 unsigned char fertile, receptive, pregnant;
123 unsigned char dead;
124 unsigned char involaction[8];
126 void addGene(gene *);
127 void tickBrain();
128 virtual void tickBiochemistry();
130 inline unsigned int calculateTickMask(unsigned char);
131 inline unsigned int calculateMultiplier(unsigned char);
133 oldCreature(shared_ptr<genomeFile> g, bool is_female, unsigned char _variant, CreatureAgent *a);
135 void processGenes();
137 public:
138 void addChemical(unsigned char id, unsigned char val);
139 void subChemical(unsigned char id, unsigned char val);
140 unsigned char getChemical(unsigned char id) { return chemicals[id]; }
142 // TODO: is it really worth having drives outside oldCreature?
143 virtual unsigned char getDrive(unsigned int id) = 0;
145 oldBrain *getBrain() { return brain; }
148 // c1
150 struct c1Reaction {
151 bioReactionGene *data;
152 void init(bioReactionGene *);
155 struct c1Receptor {
156 bioReceptorGene *data;
157 unsigned char *locus;
158 void init(bioReceptorGene *, class c1Creature *);
161 struct c1Emitter {
162 bioEmitterGene *data;
163 unsigned char *locus;
164 void init(bioEmitterGene *, class c1Creature *);
167 class c1Creature : public oldCreature {
168 protected:
169 std::vector<shared_ptr<c1Reaction> > reactions;
170 std::vector<c1Receptor> receptors;
171 std::vector<c1Emitter> emitters;
173 // loci
174 unsigned char senses[6];
175 unsigned char gaitloci[8];
176 unsigned char drives[16];
178 void addGene(gene *);
179 void tickBiochemistry();
180 void processReaction(c1Reaction &);
181 void processEmitter(c1Emitter &);
182 void processReceptor(c1Receptor &);
184 public:
185 c1Creature(shared_ptr<genomeFile> g, bool is_female, unsigned char _variant, CreatureAgent *a);
187 void tick();
189 unsigned char getDrive(unsigned int id) { assert(id < 16); return drives[id]; }
191 unsigned char *getLocusPointer(bool receptor, unsigned char o, unsigned char t, unsigned char l);
193 unsigned int getGait();
196 // c2
198 struct c2Reaction {
199 bioReactionGene *data;
200 float rate;
201 unsigned int receptors;
202 void init(bioReactionGene *);
205 struct c2Receptor {
206 bioReceptorGene *data;
207 bool processed;
208 float lastvalue;
209 float *locus;
210 unsigned int *receptors;
211 float nominal, threshold, gain;
212 void init(bioReceptorGene *, class c2Organ *, shared_ptr<c2Reaction>);
215 struct c2Emitter {
216 bioEmitterGene *data;
217 unsigned char sampletick;
218 float *locus;
219 float threshold, gain;
220 void init(bioEmitterGene *, class c2Organ *);
224 class c2Organ {
225 protected:
226 class c2Creature *parent;
227 organGene *ourGene;
229 std::vector<shared_ptr<c2Reaction> > reactions;
230 std::vector<c2Receptor> receptors;
231 std::vector<c2Emitter> emitters;
233 // data
234 unsigned char energycost, atpdamagecoefficient;
236 // variables
237 float lifeforce, shorttermlifeforce, longtermlifeforce;
239 // locuses
240 unsigned char biotick, damagerate, repairrate, clockrate, injurytoapply;
241 unsigned int clockratereceptors, repairratereceptors, injuryreceptors;
243 void processReaction(c2Reaction &);
244 void processEmitter(c2Emitter &);
245 void processReceptor(c2Receptor &, bool checkchem);
247 unsigned char *getLocusPointer(bool receptor, unsigned char o, unsigned char t, unsigned char l, unsigned int **receptors);
249 public:
250 c2Organ(c2Creature *p, organGene *g);
251 void tick();
253 void processGenes();
255 unsigned char getClockRate() { return clockrate; }
256 unsigned char getRepairRate() { return repairrate; }
257 unsigned char getDamageRate() { return damagerate; }
258 unsigned char getEnergyCost() { return energycost; }
259 unsigned char getInjuryToApply() { return injurytoapply; }
260 float getInitialLifeforce() { return lifeforce; }
261 float getShortTermLifeforce() { return shorttermlifeforce; }
262 float getLongTermLifeforce() { return longtermlifeforce; }
263 unsigned char getATPDamageCoefficient() { return atpdamagecoefficient; }
265 unsigned int getReceptorCount() { return receptors.size(); }
266 unsigned int getEmitterCount() { return emitters.size(); }
267 unsigned int getReactionCount() { return reactions.size(); }
269 void applyInjury(float);
273 class c2Creature : public oldCreature {
274 protected:
275 // biochemistry
276 std::vector<shared_ptr<c2Organ> > organs;
278 // loci
279 unsigned char senses[14];
280 unsigned char gaitloci[16];
281 unsigned char drives[17];
282 unsigned char mutationchance, mutationdegree;
284 void addGene(gene *);
285 void tickBiochemistry();
286 void processGenes();
288 public:
289 c2Creature(shared_ptr<genomeFile> g, bool is_female, unsigned char _variant, CreatureAgent *a);
291 void tick();
293 unsigned char getDrive(unsigned int id) { assert(id < 17); return drives[id]; }
295 unsigned char *getLocusPointer(bool receptor, unsigned char o, unsigned char t, unsigned char l);
297 unsigned int getGait();
300 // c2e
302 struct c2eReaction {
303 bioReactionGene *data;
304 float rate;
305 unsigned int receptors;
306 void init(bioReactionGene *);
309 struct c2eReceptor {
310 bioReceptorGene *data;
311 bool processed;
312 float lastvalue;
313 float *locus;
314 unsigned int *receptors;
315 float nominal, threshold, gain;
316 void init(bioReceptorGene *, class c2eOrgan *, shared_ptr<c2eReaction>);
319 struct c2eEmitter {
320 bioEmitterGene *data;
321 unsigned char sampletick;
322 float *locus;
323 float threshold, gain;
324 void init(bioEmitterGene *, class c2eOrgan *);
327 class c2eOrgan {
328 protected:
329 friend struct c2eReceptor;
330 friend struct c2eEmitter;
332 class c2eCreature *parent;
333 organGene *ourGene;
335 std::vector<shared_ptr<c2eReaction> > reactions;
336 std::vector<c2eReceptor> receptors;
337 std::vector<c2eEmitter> emitters;
339 // data
340 float energycost, atpdamagecoefficient;
342 // variables
343 float lifeforce, shorttermlifeforce, longtermlifeforce;
345 // locuses
346 float biotick, damagerate, repairrate, clockrate, injurytoapply;
347 unsigned int clockratereceptors, repairratereceptors, injuryreceptors;
349 void processReaction(c2eReaction &);
350 void processEmitter(c2eEmitter &);
351 void processReceptor(c2eReceptor &, bool checkchem);
353 float *getLocusPointer(bool receptor, unsigned char o, unsigned char t, unsigned char l, unsigned int **receptors);
355 public:
356 c2eOrgan(c2eCreature *p, organGene *g);
357 void tick();
359 void processGenes();
361 float getClockRate() { return clockrate; }
362 float getRepairRate() { return repairrate; }
363 float getDamageRate() { return damagerate; }
364 float getEnergyCost() { return energycost; }
365 float getInjuryToApply() { return injurytoapply; }
366 float getInitialLifeforce() { return lifeforce; }
367 float getShortTermLifeforce() { return shorttermlifeforce; }
368 float getLongTermLifeforce() { return longtermlifeforce; }
369 float getATPDamageCoefficient() { return atpdamagecoefficient; }
371 unsigned int getReceptorCount() { return receptors.size(); }
372 unsigned int getEmitterCount() { return emitters.size(); }
373 unsigned int getReactionCount() { return reactions.size(); }
375 void applyInjury(float);
378 struct c2eStim {
379 int noun_id;
380 float noun_amount;
381 int verb_id;
382 float verb_amount;
384 int drive_id[4];
385 float drive_amount[4];
386 bool drive_silent[4];
388 c2eStim() { noun_id = -1; verb_id = -1; drive_id[0] = -1; drive_id[1] = -1; drive_id[2] = -1; drive_id[3] = -1; }
389 void setupDriveStim(unsigned int num, int id, float amt, bool si) { drive_id[num] = id; drive_amount[num] = amt; drive_silent[num] = si; }
392 class c2eCreature : public Creature {
393 protected:
394 // brain config: should possibly be global
395 std::vector<unsigned int> mappinginfo;
397 // biochemistry
398 std::vector<shared_ptr<c2eOrgan> > organs;
399 float chemicals[256];
401 // loci
402 float lifestageloci[7];
403 float muscleenergy;
404 float floatingloci[32];
405 float fertile, pregnant, ovulate, receptive, chanceofmutation, degreeofmutation;
406 float dead;
407 float senses[14], involaction[8], gaitloci[16];
408 float drives[20];
410 unsigned int involactionlatency[8];
412 bioHalfLivesGene *halflives;
414 class c2eBrain *brain;
416 void tickBrain();
417 bool processInstinct();
418 void tickBiochemistry();
419 void processGenes();
420 void addGene(gene *);
422 int reverseMapVerbToNeuron(unsigned int verb);
423 AgentRef selectRepresentativeAgent(int type, std::vector<AgentRef> possibles);
425 public:
426 c2eCreature(shared_ptr<genomeFile> g, bool is_female, unsigned char _variant, CreatureAgent *a);
428 void tick();
430 void adjustChemical(unsigned char id, float value);
431 float getChemical(unsigned char id) { return chemicals[id]; }
432 void adjustDrive(unsigned int id, float value);
433 float getDrive(unsigned int id) { assert(id < 20); return drives[id]; }
435 void setInvolActionLatency(unsigned int id, unsigned int n) { assert(id < 8); involactionlatency[id] = n; }
437 void handleStimulus(c2eStim &stim);
438 void handleStimulus(unsigned int id, float strength);
440 unsigned int noOrgans() { return organs.size(); }
441 shared_ptr<c2eOrgan> getOrgan(unsigned int i) { assert(i < organs.size()); return organs[i]; }
443 class c2eBrain *getBrain() { return brain; }
445 float *getLocusPointer(bool receptor, unsigned char o, unsigned char t, unsigned char l);
447 unsigned int getGait();
450 #endif
452 /* vim: set noet: */