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.
22 #include "endianlove.h"
23 #include "exceptions.h"
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 };
42 //! A class for handling Creatures genome (.gen) files.
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
&);
56 void readNotes(istream
&);
57 void writeNotes(ostream
&) const;
59 uint8
getVersion() { return cversion
; }
61 gene
*getGene(uint8 type
, uint8 subtype
, unsigned int seq
);
65 std::string description
;
70 //! header flags of a gene
72 bool _mutable
; // 'mutable' is a reserved keyword
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; }
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.
109 friend ostream
&operator << (ostream
&, const gene
&);
110 friend istream
&operator >> (istream
&, gene
&);
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
;
127 gene(uint8 v
) : cversion(v
) { }
130 virtual const char *name() = 0;
131 virtual const char *typeName() = 0;
134 //! gene: organ, either brain or normal
135 class organGene
: public gene
{
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
&);
146 vector
<gene
*> genes
;
148 bool isBrain() { return brainorgan
; }
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
{
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
{
173 uint8
subtype() const { return 0; }
175 void write(ostream
&) const;
176 void read(istream
&);
179 uint8 id
[4]; // todo: string
180 uint16 updatetime
; // 0 for never
188 uint8 WTA
; // unused in final game?
190 uint8 initrulealways
;
192 uint8 initialiserule
[48];
193 uint8 updaterule
[48];
195 c2eBrainLobeGene(uint8 v
) : brainGene(v
) { }
196 const char *name() { return "Lobe"; }
199 struct oldDendriteInfo
{
202 // rules have size 8 for C1, size 12 for C2
220 uint8 strgainrule
[12];
221 uint8 strlossrule
[12];
222 uint8 susceptrule
[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
{
238 uint8
subtype() const { return 0; }
240 void write(ostream
&) const;
241 void read(istream
&);
244 // rules have size 8 for C1, size 12 for C2
251 uint8 nominalthreshold
;
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
{
269 uint8
subtype() const { return 2; }
271 void write(ostream
&) const;
272 void read(istream
&);
277 uint16 srclobe_lowerbound
;
278 uint16 srclobe_upperbound
;
279 uint16 src_noconnections
;
281 uint16 destlobe_lowerbound
;
282 uint16 destlobe_upperbound
;
283 uint16 dest_noconnections
;
284 uint8 migrates
; // flag
285 uint8 norandomconnections
; // flag
288 uint8 initrulealways
; // flag
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
{
299 uint8
type() const { return 1; }
301 bioGene(uint8 v
) : gene(v
) { }
302 const char *typeName() { return "Biochemistry"; }
306 class bioReceptorGene
: public bioGene
{
308 uint8
subtype() const { return 0; }
310 void write(ostream
&) const;
311 void read(istream
&);
321 bool inverted
; // 1 in flags
322 bool digital
; // 2 in flags
324 bioReceptorGene(uint8 v
) : bioGene(v
) { }
325 const char *name() { return "Receptor"; }
329 class bioEmitterGene
: public bioGene
{
331 uint8
subtype() const { return 1; }
333 void write(ostream
&) const;
334 void read(istream
&);
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"; }
353 class bioReactionGene
: public bioGene
{
355 uint8
subtype() const { return 2; }
357 void write(ostream
&) const;
358 void read(istream
&);
365 bioReactionGene(uint8 v
) : bioGene(v
) { }
366 const char *name() { return "Reaction"; }
370 class bioHalfLivesGene
: public bioGene
{
372 uint8
subtype() const { return 3; }
374 void write(ostream
&) const;
375 void read(istream
&);
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
{
387 uint8
subtype() const { return 4; }
389 void write(ostream
&) const;
390 void read(istream
&);
396 bioInitialConcentrationGene(uint8 v
) : bioGene(v
) { }
397 const char *name() { return "Initial Concentration"; }
400 //! gene: neuroemitter
401 class bioNeuroEmitterGene
: public bioGene
{
403 uint8
subtype() const { return 5; }
405 void write(ostream
&) const;
406 void read(istream
&);
415 bioNeuroEmitterGene(uint8 v
) : bioGene(v
) { }
416 const char *name() { return "Neuro Emitter"; }
419 //! gene: creature-type base class
420 class creatureGene
: public gene
{
422 uint8
type() const { return 2; }
424 creatureGene(uint8 v
) : gene(v
) { }
425 const char *typeName() { return "Creature"; }
429 class creatureStimulusGene
: public creatureGene
{
431 uint8
subtype() const { return 0; }
433 void write(ostream
&) const;
434 void read(istream
&);
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
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
{
455 uint8
subtype() const { return 1; }
457 void write(ostream
&) const;
458 void read(istream
&);
462 std::string mum
; // length is: 4 for C1/C2, 32 for c2e
465 creatureGenusGene(uint8 v
) : creatureGene(v
) { }
466 const char *name() { return "Genus"; }
470 class creatureAppearanceGene
: public creatureGene
{
472 uint8
subtype() const { return 2; }
474 void write(ostream
&) const;
475 void read(istream
&);
480 uint8 species
; // genusofdonor; not present in c1
482 creatureAppearanceGene(uint8 v
) : creatureGene(v
) { }
483 const char *name() { return "Appearance"; }
487 class creaturePoseGene
: public creatureGene
{
489 uint8
subtype() const { return 3; }
491 void write(ostream
&) const;
492 void read(istream
&);
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"; }
506 class creatureGaitGene
: public creatureGene
{
508 uint8
subtype() const { return 4; }
510 void write(ostream
&) const;
511 void read(istream
&);
517 int gaitLength() const { return 8; }
519 creatureGaitGene(uint8 v
) : creatureGene(v
) { }
520 const char *name() { return "Gait"; }
524 class creatureInstinctGene
: public creatureGene
{
526 uint8
subtype() const { return 5; }
528 void write(ostream
&) const;
529 void read(istream
&);
538 creatureInstinctGene(uint8 v
) : creatureGene(v
) { }
539 const char *name() { return "Instinct"; }
543 class creaturePigmentGene
: public creatureGene
{
545 uint8
subtype() const { return 6; }
547 void write(ostream
&) const;
548 void read(istream
&);
554 creaturePigmentGene(uint8 v
) : creatureGene(v
) { }
555 const char *name() { return "Pigment"; }
558 //! gene: pigment bleed
559 class creaturePigmentBleedGene
: public creatureGene
{
561 uint8
subtype() const { return 7; }
563 void write(ostream
&) const;
564 void read(istream
&);
570 creaturePigmentBleedGene(uint8 v
) : creatureGene(v
) { }
571 const char *name() { return "Pigment Bleed"; }
574 //! gene: facial expression
575 class creatureFacialExpressionGene
: public creatureGene
{
577 uint8
subtype() const { return 8; }
579 void write(ostream
&) const;
580 void read(istream
&);
588 creatureFacialExpressionGene(uint8 v
) : creatureGene(v
) { }
589 const char *name() { return "Facial Expression"; }