qtgui: parent the agent injector/brain viewer correctly, and fix some onSelect functi...
[openc2e.git] / genome.h
blobe640ba020322ed8fb0e91813af98b6c945dfc41d
1 /*
2 * genome.h
3 * openc2e
5 * Created by Alyssa Milburn on Sat 13 Nov 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.
19 #ifndef _C2E_GENOME_H
20 #define _C2E_GENOME_H
22 #include "endianlove.h"
23 #include "exceptions.h"
25 #include <vector>
26 #include <string>
28 using std::ostream;
29 using std::istream;
30 using std::vector;
33 not 100% sure about this enum ... is 'baby' equal to 'embryo', or is 'embryo'
34 zero and the rest shifted?
36 enum lifestage { baby = 0, child = 1, adolescent = 2, youth = 3, adult = 4, old = 5, senile = 6 };
38 class gene;
39 struct geneNote;
40 class organGene;
42 //! A class for handling Creatures genome (.gen) files.
43 class genomeFile {
44 uint8 cversion;
45 organGene *currorgan;
47 gene *nextGene(istream &); // returns NULL upon 'gend'
48 geneNote *findNote(uint8 type, uint8 subtype, uint8 which);
50 friend ostream &operator << (ostream &, const genomeFile &);
51 friend istream &operator >> (istream &, genomeFile &);
53 public:
54 vector<gene *> genes;
56 void readNotes(istream &);
57 void writeNotes(ostream &) const;
59 uint8 getVersion() { return cversion; }
61 gene *getGene(uint8 type, uint8 subtype, unsigned int seq);
64 struct geneNote {
65 std::string description;
66 std::string comments;
67 uint8 which;
70 //! header flags of a gene
71 struct geneFlags {
72 bool _mutable; // 'mutable' is a reserved keyword
73 bool dupable;
74 bool delable;
75 bool maleonly;
76 bool femaleonly;
77 bool notexpressed;
78 bool reserved1;
79 bool reserved2;
81 uint8 operator () () const;
82 void operator () (uint8);
84 geneFlags() { _mutable = true; dupable = true; delable = true; maleonly = false; femaleonly = false; notexpressed = false; reserved1 = false; reserved2 = false; }
87 //! header of a gene
88 struct geneHeader {
89 geneFlags flags;
91 uint8 generation;
92 lifestage switchontime;
93 uint8 mutweighting; // c2/c2e only
94 uint8 variant; // c2e only
96 // todo: are these values 'correct' for default values? - Fuzzie
97 geneHeader() { generation = 0; switchontime = baby; mutweighting = 255; variant = 0; }
101 nextGene does the 'dirty work' of working out which class to create, and then calls
102 the << operator as appropriate
104 however the >> operator must do this work itself ('gene' and the type/subtype)
107 //! The base class for all Creatures genes.
108 class gene {
109 friend ostream &operator << (ostream &, const gene &);
110 friend istream &operator >> (istream &, gene &);
112 protected:
113 uint8 cversion;
115 virtual uint8 type() const = 0;
116 virtual uint8 subtype() const = 0;
118 virtual void write(ostream &) const = 0;
119 virtual void read(istream &) = 0;
121 friend class genomeFile;
123 public:
124 geneNote note;
125 geneHeader header;
127 gene(uint8 v) : cversion(v) { }
128 virtual ~gene() { }
130 virtual const char *name() = 0;
131 virtual const char *typeName() = 0;
134 //! gene: organ, either brain or normal
135 class organGene : public gene {
136 bool brainorgan;
138 protected:
139 uint8 type() const { if (brainorgan) return 0; else return 3; }
140 uint8 subtype() const { if (brainorgan) return 1; else return 0; }
142 void write(ostream &) const;
143 void read(istream &);
145 public:
146 vector<gene *> genes;
148 bool isBrain() { return brainorgan; }
150 uint8 clockrate;
151 uint8 damagerate;
152 uint8 lifeforce;
153 uint8 biotickstart;
154 uint8 atpdamagecoefficient;
156 organGene(uint8 v, bool b) : gene(v), brainorgan(b) { }
157 const char *name() { return "Organ"; }
158 const char *typeName() { if (brainorgan) return "Brain"; return "Biochemistry"; }
161 //! gene: brain-type base class (not including brain organ)
162 class brainGene : public gene {
163 protected:
164 uint8 type() const { return 0; }
166 brainGene(uint8 v) : gene(v) { }
167 const char *typeName() { return "Brain"; }
170 //! gene: c2e brain lobe
171 class c2eBrainLobeGene : public brainGene {
172 protected:
173 uint8 subtype() const { return 0; }
175 void write(ostream &) const;
176 void read(istream &);
178 public:
179 uint8 id[4]; // todo: string
180 uint16 updatetime; // 0 for never
181 uint16 x;
182 uint16 y;
183 uint8 width;
184 uint8 height;
185 uint8 red;
186 uint8 green;
187 uint8 blue;
188 uint8 WTA; // unused in final game?
189 uint8 tissue;
190 uint8 initrulealways;
191 uint8 spare[7];
192 uint8 initialiserule[48];
193 uint8 updaterule[48];
195 c2eBrainLobeGene(uint8 v) : brainGene(v) { }
196 const char *name() { return "Lobe"; }
199 struct oldDendriteInfo {
200 uint8 cversion;
202 // rules have size 8 for C1, size 12 for C2
204 uint8 srclobe;
205 uint8 min;
206 uint8 max;
207 uint8 spread;
208 uint8 fanout;
209 uint8 minLTW;
210 uint8 maxLTW;
211 uint8 minstr;
212 uint8 maxstr;
213 uint8 migrateflag;
214 uint8 relaxsuscept;
215 uint8 relaxSTW;
216 uint8 LTWgainrate;
217 uint8 strgain;
218 uint8 strloss;
220 uint8 strgainrule[12];
221 uint8 strlossrule[12];
222 uint8 susceptrule[12];
223 uint8 relaxrule[12];
225 // back/forward propogation is C2 only
226 uint8 backproprule[12];
227 uint8 forproprule[12];
229 friend ostream &operator << (ostream &, const oldDendriteInfo &);
230 friend istream &operator >> (istream &, oldDendriteInfo &);
232 oldDendriteInfo(uint8 v) { cversion = v; }
235 //! gene: old-style (c1/c2) brain lobe
236 class oldBrainLobeGene : public brainGene {
237 protected:
238 uint8 subtype() const { return 0; }
240 void write(ostream &) const;
241 void read(istream &);
243 public:
244 // rules have size 8 for C1, size 12 for C2
246 uint8 x;
247 uint8 y;
248 uint8 width;
249 uint8 height;
250 uint8 perceptflag;
251 uint8 nominalthreshold;
252 uint8 leakagerate;
253 uint8 reststate;
254 uint8 inputgain;
255 uint8 flags;
257 uint8 staterule[12];
259 oldDendriteInfo dendrite1;
260 oldDendriteInfo dendrite2;
262 oldBrainLobeGene(uint8 v) : brainGene(v), dendrite1(v), dendrite2(v) { }
263 virtual const char *name() { return "Lobe"; }
266 //! gene: c2e brain tract
267 class c2eBrainTractGene : public brainGene {
268 protected:
269 uint8 subtype() const { return 2; }
271 void write(ostream &) const;
272 void read(istream &);
274 public:
275 uint16 updatetime;
276 uint8 srclobe[4];
277 uint16 srclobe_lowerbound;
278 uint16 srclobe_upperbound;
279 uint16 src_noconnections;
280 uint8 destlobe[4];
281 uint16 destlobe_lowerbound;
282 uint16 destlobe_upperbound;
283 uint16 dest_noconnections;
284 uint8 migrates; // flag
285 uint8 norandomconnections; // flag
286 uint8 srcvar;
287 uint8 destvar;
288 uint8 initrulealways; // flag
289 uint8 spare[5];
290 uint8 initialiserule[48];
291 uint8 updaterule[48];
293 c2eBrainTractGene(uint8 v) : brainGene(v) { }
294 const char *name() { return "Tract"; }
297 class bioGene : public gene {
298 protected:
299 uint8 type() const { return 1; }
301 bioGene(uint8 v) : gene(v) { }
302 const char *typeName() { return "Biochemistry"; }
305 //! gene: receptor
306 class bioReceptorGene : public bioGene {
307 protected:
308 uint8 subtype() const { return 0; }
310 void write(ostream &) const;
311 void read(istream &);
313 public:
314 uint8 organ;
315 uint8 tissue;
316 uint8 locus;
317 uint8 chemical;
318 uint8 threshold;
319 uint8 nominal;
320 uint8 gain;
321 bool inverted; // 1 in flags
322 bool digital; // 2 in flags
324 bioReceptorGene(uint8 v) : bioGene(v) { }
325 const char *name() { return "Receptor"; }
328 //! gene: emitter
329 class bioEmitterGene : public bioGene {
330 protected:
331 uint8 subtype() const { return 1; }
333 void write(ostream &) const;
334 void read(istream &);
336 public:
337 uint8 organ;
338 uint8 tissue;
339 uint8 locus;
340 uint8 chemical;
341 uint8 threshold;
342 uint8 rate;
343 uint8 gain;
344 bool clear; // 1 in flags
345 bool digital; // 2 in flags
346 bool invert; // 4 in flags
348 bioEmitterGene(uint8 v) : bioGene(v) { }
349 const char *name() { return "Emitter"; }
352 //! gene: reaction
353 class bioReactionGene : public bioGene {
354 protected:
355 uint8 subtype() const { return 2; }
357 void write(ostream &) const;
358 void read(istream &);
360 public:
361 uint8 reactant[4];
362 uint8 quantity[4];
363 uint8 rate;
365 bioReactionGene(uint8 v) : bioGene(v) { }
366 const char *name() { return "Reaction"; }
369 //! gene: half-lives
370 class bioHalfLivesGene : public bioGene {
371 protected:
372 uint8 subtype() const { return 3; }
374 void write(ostream &) const;
375 void read(istream &);
377 public:
378 uint8 halflives[256];
380 bioHalfLivesGene(uint8 v) : bioGene(v) { }
381 const char *name() { return "Half-Life"; }
384 //! gene: initial concentration
385 class bioInitialConcentrationGene : public bioGene {
386 protected:
387 uint8 subtype() const { return 4; }
389 void write(ostream &) const;
390 void read(istream &);
392 public:
393 uint8 chemical;
394 uint8 quantity;
396 bioInitialConcentrationGene(uint8 v) : bioGene(v) { }
397 const char *name() { return "Initial Concentration"; }
400 //! gene: neuroemitter
401 class bioNeuroEmitterGene : public bioGene {
402 protected:
403 uint8 subtype() const { return 5; }
405 void write(ostream &) const;
406 void read(istream &);
408 public:
409 uint8 lobes[3];
410 uint8 neurons[3];
411 uint8 rate;
412 uint8 chemical[4];
413 uint8 quantity[4];
415 bioNeuroEmitterGene(uint8 v) : bioGene(v) { }
416 const char *name() { return "Neuro Emitter"; }
419 //! gene: creature-type base class
420 class creatureGene : public gene {
421 protected:
422 uint8 type() const { return 2; }
424 creatureGene(uint8 v) : gene(v) { }
425 const char *typeName() { return "Creature"; }
428 //! gene: stimulus
429 class creatureStimulusGene : public creatureGene {
430 protected:
431 uint8 subtype() const { return 0; }
433 void write(ostream &) const;
434 void read(istream &);
436 public:
437 uint8 stim;
438 uint8 significance;
439 uint8 sensoryneuron;
440 uint8 intensity;
441 bool modulate; // 1 in flags
442 bool addoffset; // 2 in flags
443 bool whenasleep; // 4 in flags
444 bool silent[4]; // 16, 32, 64, 128 in flags
445 uint8 drives[4];
446 uint8 amounts[4];
448 creatureStimulusGene(uint8 v) : creatureGene(v) { }
449 const char *name() { return "Stimulus"; }
452 //! gene: genus (must be first gene in file, must only be one instance)
453 class creatureGenusGene : public creatureGene {
454 protected:
455 uint8 subtype() const { return 1; }
457 void write(ostream &) const;
458 void read(istream &);
460 public:
461 uint8 genus;
462 std::string mum; // length is: 4 for C1/C2, 32 for c2e
463 std::string dad;
465 creatureGenusGene(uint8 v) : creatureGene(v) { }
466 const char *name() { return "Genus"; }
469 //! gene: appearance
470 class creatureAppearanceGene : public creatureGene {
471 protected:
472 uint8 subtype() const { return 2; }
474 void write(ostream &) const;
475 void read(istream &);
477 public:
478 uint8 part;
479 uint8 variant;
480 uint8 species; // genusofdonor; not present in c1
482 creatureAppearanceGene(uint8 v) : creatureGene(v) { }
483 const char *name() { return "Appearance"; }
486 //! gene: pose
487 class creaturePoseGene : public creatureGene {
488 protected:
489 uint8 subtype() const { return 3; }
491 void write(ostream &) const;
492 void read(istream &);
494 public:
495 uint8 poseno;
496 uint8 pose[16];
498 int poseLength() const { return (cversion == 3) ? 16 : 15; }
499 std::string getPoseString() const { return std::string((char *)pose, poseLength()); }
501 creaturePoseGene(uint8 v) : creatureGene(v) { }
502 const char *name() { return "Pose"; }
505 //! gene: gait
506 class creatureGaitGene : public creatureGene {
507 protected:
508 uint8 subtype() const { return 4; }
510 void write(ostream &) const;
511 void read(istream &);
513 public:
514 uint8 drive;
515 uint8 pose[8];
517 int gaitLength() const { return 8; }
519 creatureGaitGene(uint8 v) : creatureGene(v) { }
520 const char *name() { return "Gait"; }
523 //! gene: instinct
524 class creatureInstinctGene : public creatureGene {
525 protected:
526 uint8 subtype() const { return 5; }
528 void write(ostream &) const;
529 void read(istream &);
531 public:
532 uint8 lobes[3];
533 uint8 neurons[3];
534 uint8 action;
535 uint8 drive;
536 uint8 level;
538 creatureInstinctGene(uint8 v) : creatureGene(v) { }
539 const char *name() { return "Instinct"; }
542 //! gene: pigment
543 class creaturePigmentGene : public creatureGene {
544 protected:
545 uint8 subtype() const { return 6; }
547 void write(ostream &) const;
548 void read(istream &);
550 public:
551 uint8 color;
552 uint8 amount;
554 creaturePigmentGene(uint8 v) : creatureGene(v) { }
555 const char *name() { return "Pigment"; }
558 //! gene: pigment bleed
559 class creaturePigmentBleedGene : public creatureGene {
560 protected:
561 uint8 subtype() const { return 7; }
563 void write(ostream &) const;
564 void read(istream &);
566 public:
567 uint8 rotation;
568 uint8 swap;
570 creaturePigmentBleedGene(uint8 v) : creatureGene(v) { }
571 const char *name() { return "Pigment Bleed"; }
574 //! gene: facial expression
575 class creatureFacialExpressionGene : public creatureGene {
576 protected:
577 uint8 subtype() const { return 8; }
579 void write(ostream &) const;
580 void read(istream &);
582 public:
583 uint16 expressionno;
584 uint8 weight;
585 uint8 drives[4];
586 uint8 amounts[4];
588 creatureFacialExpressionGene(uint8 v) : creatureGene(v) { }
589 const char *name() { return "Facial Expression"; }
592 #endif
594 /* vim: set noet: */