Index: cpp/frams/genetics/fL/fL_general.h
===================================================================
--- cpp/frams/genetics/fL/fL_general.h	(revision 780)
+++ cpp/frams/genetics/fL/fL_general.h	(revision 797)
@@ -1,4 +1,7 @@
+// This file is a part of Framsticks SDK.  http://www.framsticks.com/
+// Copyright (C) 1999-2018  Maciej Komosinski and Szymon Ulatowski.
+// See LICENSE.txt for details.
+
 #ifndef _FL_GENERAL_
-
 #define _FL_GENERAL_
 
@@ -22,14 +25,20 @@
 /** @name Constants used in fL methods */
 //@{
-#define PART_PROPS_COUNT   4 ///<Count of part properties
-#define JOINT_PROPS_COUNT  3 ///<Count of joint properties
-#define PE_NEURO_DET       "d" ///<Id of details type definition in f0_neuro_paramtab
-#define PE_CONN_WEIGHT     "w" ///<Id of weight type definition in f0_neuroconn_paramtab
+#define FL_PART_PROPS_COUNT   4 ///<Count of part properties
+#define FL_JOINT_PROPS_COUNT  3 ///<Count of joint properties
+#define FL_PE_NEURO_DET       "d" ///<Id of details type definition in f0_neuro_paramtab
+#define FL_PE_CONN_WEIGHT     "w" ///<Id of weight type definition in f0_neuroconn_paramtab
+#define FL_PE_CONN_ATTR       "attr" ///<Id of attractor of neural connection
+#define FL_DEFAULT_LENGTH     1.0 ///<Default length of a stick in fL encoding
+#define FL_MINIMAL_LENGTH     0.0 ///<Minimal length of a stick in fL encoding
+#define FL_MAXIMAL_LENGTH     2.0 ///<Maximal length of a stick in fL encoding
+#define FL_MAXITER           "100.0" ///<Maximal iteration available in fL
+#define FL_MAXPARAMS          "3" ///<Maximal number of user-parameters
 //@}
 
 extern const char *fL_part_names[];
 extern const char *fL_joint_names[];
-extern const char *fL_joint_fullnames[JOINT_PROPS_COUNT];
-extern const char *fL_part_fullnames[PART_PROPS_COUNT];
+extern const char *fL_joint_fullnames[FL_JOINT_PROPS_COUNT];
+extern const char *fL_part_fullnames[FL_PART_PROPS_COUNT];
 
 #define LSYSTEM_PARAM_TYPE "s" ///< standard type of L-System elements
@@ -46,5 +55,9 @@
 	fLElementType type; ///< type of fL_Element
 
-	fL_Element(fLElementType type) : type(type) {}
+	int begin; ///<beginning of the element definition in genotype
+	int end; ///<end of the element definition in genotype
+
+
+	fL_Element(fLElementType type) : type(type) { begin = end = 0; }
 	virtual ~fL_Element() { }
 
@@ -78,8 +91,9 @@
 	void *data; ///<pointer to properties of word
 	bool builtin; ///<determines if word is built-in (true) or not (false).
+	PartBase *bodyelementpointer; ///<helper field for built-in words
 	std::vector<MathEvaluation *> parevals; ///<stores MathEvaluation objects with parameter functions
 	double creationiter; ///<this helper field is used during L-System iterations and determines when word was created
-	fL_Word(bool builtin = false) :
-		fL_Element(fLElementType::TERM), mut("Word", "Properties"), //TODO change Word to Term everywhere
+	fL_Word(bool builtin = false, int begin = 0, int end = 0) :
+		fL_Element(fLElementType::TERM), mut("Word", "Properties"),
 		builtin(builtin)
 	{
@@ -89,4 +103,7 @@
 		data = NULL;
 		creationiter = 0;
+		this->begin = begin;
+		this->end = end;
+		bodyelementpointer = NULL;
 	}
 
@@ -102,5 +119,5 @@
 		for (MathEvaluation *ev : parevals)
 		{
-			delete ev;
+			if (ev) delete ev;
 		}
 		parevals.clear();
@@ -125,9 +142,4 @@
 
 	/**
-	 * TODO
-	 */
-	bool operator==(const fL_Word& other) const;
-
-	/**
 	 * Returns 'w:' line defining word in genotype.
 	 * @return string representation of word definition
@@ -150,4 +162,13 @@
 	 */
 	int saveEvals(bool keepformulas);
+
+	/**
+	 * Computes distance between two L-System words in genotype. Distance has sense
+	 * only when two words have the same name. Otherwise, returned value equals -1.
+	 * The distance is computed as Euclidean distance between words arguments.
+	 * @param right the second word
+	 * @return Euclidean distance between words in genotype or -1 if words have different name
+	 */
+	double distance(fL_Word *right);
 };
 
@@ -164,5 +185,5 @@
 	};
 	BranchType btype; ///< branch parenthesis type
-	fL_Branch(BranchType branchtype) : fL_Word()
+	fL_Branch(BranchType branchtype, int begin, int end) : fL_Word(true, begin, end)
 	{
 		type = fLElementType::BRANCH;
@@ -192,9 +213,5 @@
 	std::list<fL_Word *> objsucc; ///<objec representation of successors
 
-	int start; ///<beginning of the rule definition in genotype
-	int end; ///<end of the rule definition in genotype
-
-	fL_Rule(int start, int end) : fL_Element(fLElementType::RULE),
-		start(start), end(end)
+	fL_Rule(int begin, int end)  : fL_Element(fLElementType::RULE)
 	{
 		predecessor = "";
@@ -203,4 +220,6 @@
 		objpred = NULL;
 		condeval = NULL;
+		this->begin = begin;
+		this->end = end;
 	}
 
@@ -231,13 +250,16 @@
 	 * pointer and iterator of list and inserting successor sequence into list.
 	 * Final iterator value points to the first word after processed word.
+	 * @param builder builder containing current genotype
 	 * @param in word that is currently processed
 	 * @param it iterator of genotype list which determines position of current analysis
-	 * @param genotype current genotype. Words on the left of genotype are processed and possibly replaced by previous rules
 	 * @param currtime value representing continuous time value for rule
 	 * @return 0 if rule processed current word, 1 if rule is no applicable
 	 */
-	int deploy(fL_Word *in, std::list<fL_Word *>::iterator &it, std::list<fL_Word *> &genotype, double currtime);
-};
-
+	int deploy(fL_Builder *builder, fL_Word *in, std::list<fL_Word *>::iterator &it, double currtime);
+};
+
+/**
+ * Structure for holding current Turtle state.
+ */
 struct fL_State
 {
@@ -245,5 +267,5 @@
 	Part *currpart; ///<Latest created part
 	Neuro *currneuron; ///<Latest created neuron
-	fL_State() : direction(1, 0, 0), currpart(NULL), currneuron(NULL)
+	fL_State() : direction(1,0,0), currpart(NULL), currneuron(NULL)
 	{ }
 };
@@ -266,14 +288,23 @@
 	double time; ///<time of creature development
 	int numckp; ///<number of checkpoints per growth step - checkpoints are always created after rules deployment in integer steps, numckp determines how many checkpoints should be created within single step (to watch changes of parameters)
+	int maxwords; ///<maximum number of words that can
+	bool using_checkpoints; ///<true if checkpoints should be created, or false
+	bool using_mapping; ///<true if mappings should be created, or false
+
+	int builtincount; ///<number of built-in words
 
 	std::list<fL_Word *> genotype; ///<list of current words of genotype
 	std::unordered_map<std::string, fL_Word *> words; ///<map from string to words existing in L-System
+	std::vector<std::string> wordnames;
 	std::vector<fL_Rule *> rules; ///<rules available in system
 
-	fL_Builder() : fL_Element(fLElementType::INFO)
+	fL_Builder(bool using_mapping = false, bool using_checkpoints = false) : fL_Element(fLElementType::INFO),
+			using_checkpoints(using_checkpoints), using_mapping(using_mapping)
 	{
 		axiom = "";
 		time = 1.0;
 		numckp = 1;
+		builtincount = 0;
+		maxwords = -1;
 	}
 	~fL_Builder();
@@ -337,4 +368,15 @@
 
 	/**
+	 * Creates fL_Word object for a given token.
+	 * @param worddef string representing single word object in sequence
+	 * @param word a reference to a pointer of created word by this method
+	 * @param numparams number of parameters for a given sequence (usually number of rule predecessor's arguments)
+	 * @param begin the begin of word definition in genotype
+	 * @param end the end of word definition in genotype
+	 * @return 0 if conversion went successfully, 1 when there is a problem with parsing
+	 */
+	int createWord(SString worddef, fL_Word *&word, int numparams, int begin, int end);
+
+	/**
 	 * Helper function that converts input sequence into list of L-System words.
 	 * @param sequence input sequence of stringified word objects
@@ -343,5 +385,5 @@
 	 * @return 0 if tokenizing finished successfully, 1 otherwise
 	 */
-	int tokenize(SString sequence, std::list<fL_Word *> &result, int numparams);
+	int tokenize(SString sequence, std::list<fL_Word *> &result, int numparams, int begin, int end);
 
 	/**
@@ -363,6 +405,10 @@
 	/**
 	 * Developes L-System from given genotype and builds Framsticks Model from it.
-	 */
-	int develop();
+	 * When using_checkpoints is enabled, method generates checkpoint for each
+	 * step defined in timestamp.
+	 * @param neededtime reference to a time value after stopping development (usually it will be equal to time specified in the time field, unless the number of allowed words will be exceeded earlier)
+	 * @return final model from a fL genotype
+	 */
+	Model* developModel(double &neededtime);
 
 	/**
@@ -371,5 +417,5 @@
 	 * @return 0 if developing went successfully, 1 otherwise
 	 */
-	int developModel(Model &model);
+	int buildModelFromSequence(Model *model);
 
 	/**
@@ -386,17 +432,71 @@
 
 	/**
-	 * TODO
+	 * Alters part properties according to informations stored in stickword.
+	 * Method takes current values of part's properties computed from previous
+	 * calls of the alterPartProperties and computes mean according to upcoming
+	 * values of properties.
+	 * @param part analysed part
+	 * @param stickword the L-System word that affects current part
+	 * @param alterationcount the number of times of part modifications - used to compute mean of every word properties
+	 * @return always 0
 	 */
 	int alterPartProperties(Part *part, fL_Word *stickword, double &alterationcount);
 
 	/**
-	 * TODO
-	 */
-	Neuro *findInputNeuron(std::list<fL_Word *> genotype, std::list<fL_Word *>::iterator it, fL_Word *attractor);
-
-	/**
-	 * TODO
-	 */
-	void findNext(fL_Word *word, std::list<fL_Word *> genotype, std::list<fL_Word *>::iterator &it);
+	 * Finds input neuron that is nearest to attractor or connection definition.
+	 * When attractor object is given, then the word with name matching attractor and
+	 * with nearest values of parameters is chosen as the point, from which input
+	 * neuron is looked for. The searching goes both sides.
+	 * @param currneu object storing informations about connection word iterator and current neuron
+	 * @param attractor pointer to an attractor definition presented in connection word
+	 * @return pointer to the input neuron, or NULL if no neuron could be found
+	 */
+	Neuro *findInputNeuron(std::pair<std::list<fL_Word *>::iterator, Neuro *> currneu, fL_Word *attractor);
+
+	/**
+	 * Removes joints, parts and neurons with its connections from current model, without
+	 * removing checkpoint data.
+	 * @param m model to clear
+	 */
+	void clearModelElements(Model *m);
+
+	/**
+	 * Converts parameters defined in built-in words into desired
+	 * range with use of sigmoid function, which ensures staying
+	 * within min and max value.
+	 * @param input the value from evaluation of parameter
+	 * @param min minimal value of property
+	 * @param max maximal value of property
+	 * @return value of body element property
+	 */
+	double sigmoidTransform(double input, double min, double max);
+
+	/**
+	 * Counts words defined in the genotype.
+	 * @return number of defined words
+	 */
+	int countDefinedWords();
+
+	/**
+	 * Counts number of sticks in a given sequence
+	 * @return number of sticks in sequence
+	 */
+	int countSticksInSequence(std::list<fL_Word *> sequence);
+
+	/**
+	 * Counts all definitions of words, all words in axiom and rule successors in a genotype.
+	 * Used for computing change between original genotype and mutation.
+	 * @return number of words in definitions, axiom and rule successors
+	 */
+	int countWordsInLSystem();
+
+	/**
+	 * Sorts rules and removes rules that will not be used. The "sorting" is
+	 * done in such way that all rules with conditions are first, and rules
+	 * without conditions go to the end. If there are more than one rule
+	 * for the same word with no condition or same condition, than the
+	 * second one is removed.
+	 */
+	void removeRedundantRules();
 };
 
