1 
2 /* "Species" - a CoreWars evolver.  Copyright (C) 2003 'Varfar'
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the Free
6  * Software Foundation; either version 1, or (at your option) any later
7  * version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 
19 #include "length.hpp"
20 #include "species.hpp"
21 #include "rand.hpp"
22 
23 using namespace std;
24 
25 /***** CLength class implementation *******************/
26 
read_ini(INIFile & ini)27 void CLength::read_ini(INIFile &ini) { /* loads length values; values are REQUIRED */
28 	KeyValuePair *kvp;
29 	// min length
30 	kvp = ini.get("min_length");
31 	if(0 == kvp)
32 		PANIC(MISC,"min_length expected",NULL);
33 	_min = kvp->getValueAsInt();
34 	if(!(0 < _min < MAXLENGTH))
35 		PANIC(MISC,"min_length is illegal",NULL);
36 	// max length
37 	kvp = ini.get("max_length");
38 	if(0 == kvp)
39 		PANIC(MISC,"max_length expected",NULL);
40 	_max = kvp->getValueAsInt();
41 	if(!(_min < _max < MAXLENGTH))
42 		PANIC(MISC,"max_length is illegal",NULL);
43 }
44 
read_override_ini(INIFile & ini)45 CLength *CLength::read_override_ini(INIFile &ini) /* loads length values; if any length values are
46 	specified, then a copy of this length will be made; all unspecified values in the new
47 	class will be "inherited" from this one, and any specified values will be overriden.  If nothing
48 	is specified, then this will be returned */ {
49 	CLength *ret = this;
50 	KeyValuePair *kvp;
51 	// min length
52 	kvp = ini.get("min_length");
53 	if(0 != kvp) { // specified?
54 		ret = new CLength(*this); // create an overriden copy to return
55 		ret->_min = kvp->getValueAsInt();
56 		if(!((0 < ret->_min) && (ret->_min < MAXLENGTH)))
57 			PANIC(MISC,"min_length is illegal",NULL);
58 	}
59 	// max length
60 	kvp = ini.get("max_length");
61 	if(0 != kvp) { // specified?
62 		if(this == ret) // not overriden for min?
63 			ret = new CLength(*this); // create an overriden copy to return
64 		ret->_max = kvp->getValueAsInt();
65 		if(!((ret->_min <= ret->_max) && (ret->_max < MAXLENGTH)))
66 			PANIC(MISC,"max_length is illegal",NULL);
67 	}
68 	return ret;
69 }
70 
write_ini(ostream & os)71 void CLength::write_ini(ostream &os) {
72 	os	<< "min_length=" << _min << endl
73 		<< "max_length=" << _max << endl;
74 }
75 
write_override_ini(ostream & os,const CLength * parent)76 void CLength::write_override_ini(ostream &os,const CLength *parent) {
77 	if(this == parent) // nothing overriden?
78 		return;
79 	if(parent->_min != _min) // overriden?
80 		os << "min_length=" << _min << endl;
81 	if(parent->_max != _max) // overriden?
82 		os << "max_length=" << _max << endl;
83 }
84 
rnd() const85 int CLength::rnd() const { // returns a random length that is ok()
86 	if(_min == _max) // fixed length?
87 		return _min;
88 	return (CRand::irand(_max-_min)+_min);
89 }
90