/* "Species" - a CoreWars evolver. Copyright (C) 2003 'Varfar' * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 1, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "length.hpp" #include "species.hpp" #include "rand.hpp" using namespace std; /***** CLength class implementation *******************/ void CLength::read_ini(INIFile &ini) { /* loads length values; values are REQUIRED */ KeyValuePair *kvp; // min length kvp = ini.get("min_length"); if(0 == kvp) PANIC(MISC,"min_length expected",NULL); _min = kvp->getValueAsInt(); if(!(0 < _min < MAXLENGTH)) PANIC(MISC,"min_length is illegal",NULL); // max length kvp = ini.get("max_length"); if(0 == kvp) PANIC(MISC,"max_length expected",NULL); _max = kvp->getValueAsInt(); if(!(_min < _max < MAXLENGTH)) PANIC(MISC,"max_length is illegal",NULL); } CLength *CLength::read_override_ini(INIFile &ini) /* loads length values; if any length values are specified, then a copy of this length will be made; all unspecified values in the new class will be "inherited" from this one, and any specified values will be overriden. If nothing is specified, then this will be returned */ { CLength *ret = this; KeyValuePair *kvp; // min length kvp = ini.get("min_length"); if(0 != kvp) { // specified? ret = new CLength(*this); // create an overriden copy to return ret->_min = kvp->getValueAsInt(); if(!((0 < ret->_min) && (ret->_min < MAXLENGTH))) PANIC(MISC,"min_length is illegal",NULL); } // max length kvp = ini.get("max_length"); if(0 != kvp) { // specified? if(this == ret) // not overriden for min? ret = new CLength(*this); // create an overriden copy to return ret->_max = kvp->getValueAsInt(); if(!((ret->_min <= ret->_max) && (ret->_max < MAXLENGTH))) PANIC(MISC,"max_length is illegal",NULL); } return ret; } void CLength::write_ini(ostream &os) { os << "min_length=" << _min << endl << "max_length=" << _max << endl; } void CLength::write_override_ini(ostream &os,const CLength *parent) { if(this == parent) // nothing overriden? return; if(parent->_min != _min) // overriden? os << "min_length=" << _min << endl; if(parent->_max != _max) // overriden? os << "max_length=" << _max << endl; } int CLength::rnd() const { // returns a random length that is ok() if(_min == _max) // fixed length? return _min; return (CRand::irand(_max-_min)+_min); }