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

Last change on this file since 1279 was 1260, checked in by Maciej Komosinski, 18 months ago

Cosmetic

File size: 7.8 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_Legacy : public GenePropsOp
31{
32        double minvalue, maxvalue, defvalue, change, revchange;
33public:
34        GenePropsOp_Legacy(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_AllChange05;
38};
39
40class GenePropsOp_NormalizedAndScaled : public GenePropsOp
41{
42        GenePropsOp_Legacy 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_Legacy : public GenePropsOps
82{
83public:
84        GenePropsOps_Legacy();
85};
86
87/**
88* Just as legacy (before 2023-06), but the influence of each modifier is uniform and simpler (no normalizeBiol4()).
89* This is what is currently used in Framsticks.
90*/
91class GenePropsOps_AllChange05 : public GenePropsOps_Legacy
92{
93public:
94        GenePropsOps_AllChange05();
95};
96
97/**
98* Just an experiment with a different scaling of the influence of individual modifier characters to cover
99* the entire range of parameter values. After investigations we decided that it does not
100* provide any advantages over "AllChange05".
101*/
102class GenePropsOps_Exponential : public GenePropsOps
103{
104public:
105        GenePropsOps_Exponential();
106};
107
108/**
109 * Contains physical, biological and other properties of a Part and a Joint (as handled
110 * by the f1 and f4 encodings), except for rotation. The constructor initializes properties with
111 * default values. In order to change a property, the executeModifier() method
112 * should be called. Modification of length, curvedness and twist properties
113 * usually affects further sticks, so new sticks should have properties of
114 * parents (prop) modified with the prop.propagateAlong() method.
115 * "Biological" properties (assimilation, stamina, muscle strength and
116 * ingestion) can be normalized after modification with normalizeBiol4().
117 * See also geneprops_test.cpp
118 */
119struct GeneProps
120{
121public:
122        double length;            ///<incremented by L, decremented by l. Physical property, length of stick
123        double curvedness;        ///<incremented by C, decremented by c. Curvedness of sticks
124        double weight;            ///<incremented by W, decremented by w. Physical property, weight of stick (in water environment light sticks swim on the surface)
125        double friction;          ///<incremented by F, decremented by f. Physical property, friction of a stick (sticks will slide on the ground or stick to it)
126
127        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
128        double assimilation;      ///<incremented by A, decremented by a. Biological property, assimilation, photosynthesis (a vertical stick can assimilate twice as much as horizontal one)
129        double stamina;           ///<incremented by S, decremented by s. Biological property, stamina (increases chance of survival during fights)
130        double ingestion;         ///<incremented by I, decremented by i. Biological property, ingestion (ability to gain/transfer energy from food)
131
132        double twist;             ///<incremented by Q, decremented by q. Twist of a stick
133        double energy;            ///<incremented by E, decremented by e. Energy of a creature
134
135        double muscle_bend_range; ///<Used only by conv_f1
136        bool muscle_reset_range;  ///<Used only by conv_f1
137
138        double cred;              ///<incremented by D, decremented by d. Part's red color proportion
139        double cgreen;            ///<incremented by B, decremented by b. Part's blue color proportion
140        double cblue;             ///<incremented by G, decremented by g. Part's green color proportion
141
142        static GeneProps standard_values;
143       
144        static GenePropsOps* getDefaultOps();
145        static void setDefaultOps(GenePropsOps* ops);
146
147        static GenePropsOps* getAllChange05Ops();
148        static GenePropsOps* getLegacyOps();
149       
150        /**
151         * Constructor initializing all properties with default values.
152         */
153        GeneProps();
154
155        /**
156         * Normalizes biological properties (muscle_power, assimilation, stamina, and ingestion).
157         * This method is called in executeModifier() when any of the biological properties is modified
158         * and \a use_normalizebiol4 is true. All values of those properties sum up to 1.
159         */
160        void normalizeBiol4();
161
162        /**
163         * Checks whether the given character is property modifier. If yes, the property
164         * is modified and properties are normalized when needed.
165         * @param modif character that might be a property modifier
166         * @return 0 if the provided character was property modifier, -1 otherwise
167         */
168        int executeModifier(char modif, GenePropsOps* ops = NULL);
169
170        /**
171         * Adjusts current properties for the next stick. In order to set
172         * new properties to the created stick, the copy of the previous stick should be created,
173         * and propagateAlong() should be used for that copy.
174         * @param use_reset_range true if this method should modify muscle_bend_range (used in f1 conversion).
175         */
176        void propagateAlong(bool use_f1_muscle_reset_range, GenePropsOps* ops = NULL);
177};
178
179#endif // _GENEPROPS_H
Note: See TracBrowser for help on using the repository browser.