source: cpp/frams/genetics/geneprops.h @ 1252

Last change on this file since 1252 was 1249, checked in by Maciej Komosinski, 19 months ago

Multiple issues fixed when calculating bending range for the '|' bending muscle (earlier it was always 1.0 despite the code that supposedly calculated this)

File size: 7.3 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2023  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
4
5#ifndef _GENEPROPS_H
6#define _GENEPROPS_H
7
8#include <common/nonstd_math.h>
9#include <frams/model/model.h>
10
11
12
13
14
15#define F14_MODIFIERS_VISUAL "DdGgBb"
16#define F14_MODIFIERS_RARE "EeWwSsAaIi" //An expdef would need to handle many of these properly/specifically to ensure reasonable behavior, and hardly any expdef does. Modifying initial energy of a creature as a result of its genes (Ee) is in general not a good idea, but may make sense in some specific competitive biological simulations. Weight (Ww) works only in water, and in water sinking/going up should usually be caused by real "intentional" activity of a creature, not by its inherited weight. Stamina (Ss) is no longer needed as destructive collisions are not supported, and even if they were, some expdef would need to impose reasonable restrictions on the value of this parameter (e.g. similar to normalizeBiol4()) so there is some cost associated with it, and the specific consequences of destructions should be defined as needed. For assimilation (Aa), there is a dedicated parameter in CreaturesGroup. Ingestion (Ii) influences how fast energy is transferred to Parts of a creature, and optimizing this property may make sense in experiments where energy is present in the environment and can be ingested - if needed, this modifier should be enabled for mutation.
17#define F14_MODIFIERS "LlRrCcQqFfMm" F14_MODIFIERS_RARE F14_MODIFIERS_VISUAL
18
19
20
21class GenePropsOp
22{
23public:
24        virtual ~GenePropsOp() {}
25        virtual double increase(double value) const = 0;
26        virtual double decrease(double value) const = 0;
27        void apply(double &value, char modif) const;
28};
29
30class GenePropsOp_Old : public GenePropsOp
31{
32        double minvalue, maxvalue, defvalue, change, revchange;
33public:
34        GenePropsOp_Old(double minvalue, double maxvalue, double defvalue, double change, double revchange = -1);
35        double increase(double value) const;
36        double decrease(double value) const;
37        friend class GenePropsOps_New05;
38};
39
40class GenePropsOp_NormalizedAndScaled : public GenePropsOp
41{
42        GenePropsOp_Old normalized;
43public:
44        GenePropsOp_NormalizedAndScaled(double change) :normalized(-1, 1, 0, change) {}
45        virtual double scale(double value) const { return value; }
46        virtual double scaleInv(double value) const { return value; }
47        double increase(double value) const { return scale(normalized.increase(scaleInv(value))); }
48        double decrease(double value) const { return scale(normalized.decrease(scaleInv(value))); }
49};
50
51class GenePropsOp_Exponential : public GenePropsOp_NormalizedAndScaled
52{
53        double a, b, c;
54        double log_a;
55        bool linear;
56public:
57        GenePropsOp_Exponential(double minvalue, double maxvalue, double defvalue, double change = 0.5);
58        double scale(double) const;
59        double scaleInv(double) const;
60};
61
62class GenePropsOps
63{
64public:
65        ~GenePropsOps();
66
67        GenePropsOp* length;
68        GenePropsOp* curvedness;
69        GenePropsOp* weight;
70        GenePropsOp* friction;
71        GenePropsOp* muscle_power;
72        GenePropsOp* assimilation;
73        GenePropsOp* stamina;
74        GenePropsOp* ingestion;
75        GenePropsOp* twist;
76        GenePropsOp* energy;
77        GenePropsOp* cred, *cgreen, *cblue;
78        bool use_normalizebiol4;
79};
80
81class GenePropsOps_Old : public GenePropsOps
82{
83public:
84        GenePropsOps_Old();
85};
86
87class GenePropsOps_New05 : public GenePropsOps_Old
88{
89public:
90        GenePropsOps_New05();
91};
92
93class GenePropsOps_Exponential : public GenePropsOps
94{
95public:
96        GenePropsOps_Exponential();
97};
98
99/**
100 * Contains physical, biological and other properties of a Part and a Joint (as handled
101 * by the f1 and f4 encodings), except for rotation. The constructor initializes properties with
102 * default values. In order to change a property, the executeModifier() method
103 * should be called. Modification of length, curvedness and twist properties
104 * usually affects further sticks, so new sticks should have properties of
105 * parents (prop) modified with the prop.propagateAlong() method.
106 * "Biological" properties (assimilation, stamina, muscle strength and
107 * ingestion) can be normalized after modification with normalizeBiol4().
108 * See also geneprops_test.cpp
109 */
110struct GeneProps
111{
112public:
113        double length;            ///<incremented by L, decremented by l. Physical property, length of stick
114        double curvedness;        ///<incremented by C, decremented by c. Curvedness of sticks
115        double weight;            ///<incremented by W, decremented by w. Physical property, weight of stick (in water environment light sticks swim on the surface)
116        double friction;          ///<incremented by F, decremented by f. Physical property, friction of a stick (sticks will slide on the ground or stick to it)
117
118        double muscle_power;      ///<incremented by M, decremented by m. Biological property, muscle strength (muscle speed). Strong muscles act with bigger force, gain higher speed, can resist bigger stress, and use more energy
119        double assimilation;      ///<incremented by A, decremented by a. Biological property, assimilation, photosynthesis (a vertical stick can assimilate twice as much as horizontal one)
120        double stamina;           ///<incremented by S, decremented by s. Biological property, stamina (increases chance of survival during fights)
121        double ingestion;         ///<incremented by I, decremented by i. Biological property, ingestion (ability to gain/transfer energy from food)
122
123        double twist;             ///<incremented by Q, decremented by q. Twist of a stick
124        double energy;            ///<incremented by E, decremented by e. Energy of a creature
125
126        double muscle_bend_range; ///<Used only by conv_f1
127        bool muscle_reset_range;  ///<Used only by conv_f1
128
129        double cred;              ///<incremented by D, decremented by d. Part's red color proportion
130        double cgreen;            ///<incremented by B, decremented by b. Part's blue color proportion
131        double cblue;             ///<incremented by G, decremented by g. Part's green color proportion
132
133        static GeneProps standard_values;
134        static GenePropsOps* standard_ops;
135        static GenePropsOps* getStandardOps();
136
137        /**
138         * Constructor initializing all properties with default values.
139         */
140        GeneProps();
141
142        /**
143         * Normalizes biological properties (muscle_power, assimilation, stamina, and ingestion).
144         * This method is called in executeModifier() when any of the biological properties is modified
145         * and \a use_normalizebiol4 is true. All values of those properties sum up to 1.
146         */
147        void normalizeBiol4();
148
149        /**
150         * Checks whether the given character is property modifier. If yes, the property
151         * is modified and properties are normalized when needed.
152         * @param modif character that might be a property modifier
153         * @return 0 if the provided character was property modifier, -1 otherwise
154         */
155        int executeModifier(char modif, GenePropsOps* ops = NULL);
156        int executeModifier_Legacy(char modif);
157
158        /**
159         * Adjusts current properties for the next stick. In order to set
160         * new properties to the created stick, the copy of the previous stick should be created,
161         * and propagateAlong() should be used for that copy.
162         * @param use_reset_range true if this method should modify muscle_bend_range (used in f1 conversion).
163         */
164        void propagateAlong(bool use_f1_muscle_reset_range, GenePropsOps* ops = NULL);
165};
166
167#endif // _GENEPROPS_H
Note: See TracBrowser for help on using the repository browser.