Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   Related Pages  

Ability.h

00001 /*
00002  * Ability.h -- class interface for ability scores
00003  * Copyright (C) 2002  Eric Lemings <elemings@users.sourceforge.net>
00004  *
00005  * This software is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This software is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this software; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00018  * 02111-1307, USA
00019  *
00020  * RCS: $Id: Ability.h,v 1.7 2003/04/18 01:41:02 elemings Exp $
00021  */
00022 
00023 #ifdef __cplusplus
00024 
00025 #  ifndef OGS_CORE_ABILITY_H
00026 #    define OGS_CORE_ABILITY_H
00027 
00028 #    include <ogs/Support.h>
00029 #    include <ogs/core/Die.h>
00030 #    include <ogs/core/Experience.h>
00031 #    include <ogs/core/Modifiers.h>
00032 #    include <ogs/core/Namespace.h>
00033 
00034 OGS_BEGIN_CORE_NAMESPACE
00035 
00051 class Ability: public ogs::support::Object,
00052                private ogs::support::Observer {
00053   public:
00060     enum Type {
00066       STR = 1,
00067 
00076       DEX,
00077 
00084       CON,
00085 
00087       INT,
00088 
00095       WIS,
00096 
00098       CHA,
00099 
00101       NUM = 6
00102     };
00103 
00105     typedef unsigned short Score;
00106 
00107     struct Method;
00108     struct StandardMethod;
00109     struct AverageMethod;
00110     struct HighPoweredMethod;
00111     struct DirectMethod;
00112 
00113     class Event;
00114 
00115     static bool isValidType (int i);
00116     static unsigned getIncreaseCount (XP::Level xpLevel);
00117     static XP::Level getIncreaseCountLevel (unsigned increaseCount);
00118 
00119     Ability (Type type);
00120     Ability (Type type, Method& method);
00121 
00122     Type getType () const;
00123     Score getOriginalScore () const;
00124     Modifiers& getModifiers ();
00125     Score getCurrentScore () const;
00126     const Modifier& getModifier () const;
00127     Modifier& getModifier ();
00128 
00129   private:
00130     Type _type;
00131     Score _originalScore;
00132     Modifiers _modifiers;
00133     Modifier _modifier;
00134 
00135     static Score rollStandard ();
00136     static Score rollHighPowered ();
00137 
00138     static Modifier::Value getModifier (Score score);
00139 
00140     void handleEvent (ogs::support::Event& event);
00141 };
00142 
00149 struct Ability::Method {
00158   virtual Ability::Score operator() (Ability::Type type) = 0;
00159 
00160   virtual ~Method () { }
00161 };
00162 
00169 struct Ability::StandardMethod: public Ability::Method {
00176   Ability::Score operator() (Ability::Type) {
00177     return (Ability::rollStandard ());
00178   }
00179 };
00180 
00186 struct Ability::AverageMethod: public Ability::Method {
00193   Ability::Score operator() (Ability::Type) {
00194     return (Score (Die::roll (Die::d6, 3)));
00195   }
00196 };
00197 
00204 struct Ability::HighPoweredMethod: public Ability::Method {
00211   Ability::Score operator() (Ability::Type) {
00212     return (Ability::rollHighPowered ());
00213   }
00214 };
00215 
00222 struct Ability::DirectMethod: public Ability::Method {
00224   Ability::Score score;
00225 
00231   DirectMethod (Ability::Score score) {
00232     this->score = score;
00233   }
00234 
00241   Ability::Score operator() (Ability::Type) {
00242     return (score);
00243   }
00244 };
00245 
00252 inline bool
00253 Ability::isValidType (int i) {
00254   return (i == STR || i == DEX || i == CON ||
00255           i == INT || i == WIS || i == CHA);
00256 }
00257 
00266 inline Modifier::Value
00267 Ability::getModifier (Score score) {
00268   return (score / 2 - 5);
00269 }
00270 
00278 inline unsigned
00279 Ability::getIncreaseCount (XP::Level xpLevel) {
00280   return (xpLevel == 0? 0: xpLevel / 4);
00281 }
00282 
00288 inline Ability::Type
00289 Ability::getType () const {
00290   return (this->_type);
00291 }
00292 
00298 inline Ability::Score
00299 Ability::getOriginalScore () const {
00300   return (this->_originalScore);
00301 }
00302 
00310 inline Modifiers&
00311 Ability::getModifiers () {
00312   return (this->_modifiers);
00313 }
00314 
00320 inline Ability::Score
00321 Ability::getCurrentScore () const {
00322   return (this->_originalScore + this->_modifiers.getValue ());
00323 }
00324 
00332 inline const Modifier&
00333 Ability::getModifier () const {
00334   return (this->_modifier);
00335 }
00336 
00344 inline Modifier&
00345 Ability::getModifier () {
00346   return (this->_modifier);
00347 }
00348 
00361 class Ability::Event: public Modifiers::Event {
00362   public:
00363     Ability::Score getPreviousScore () const;
00364 
00365   private:
00366     Ability::Score _score;
00367 
00368     Event (Ability& ability, Modifiers::Event& event);
00369 
00370     friend void Ability::handleEvent (ogs::support::Event& event);
00371 };
00372 
00379 inline Ability::Event::Event       (Ability& ability,
00380                                     Modifiers::Event& event):
00381   Modifiers::Event (event),
00382   _score (ability.getCurrentScore ()) {
00383   // empty constructor body
00384 }
00385 
00391 inline Ability::Score
00392 Ability::Event::getPreviousScore () const {
00393   return (this->_score);
00394 }
00395 
00396 OGS_END_CORE_NAMESPACE
00397 
00398 #  endif /* !defined OGS_CORE_ABILITY_H */
00399 
00400 #endif /* defined __cplusplus */
00401 

Generated on Sun Apr 20 03:36:13 2003 for Open Gaming System (OGS) by doxygen1.3