adonthell-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Adonthell-commits] CVS: adonthell/src dialog.cc,1.21,1.22 dialog.h,1.47


From: Kai Sterker <address@hidden>
Subject: [Adonthell-commits] CVS: adonthell/src dialog.cc,1.21,1.22 dialog.h,1.47,1.48 dialog_screen.cc,1.4,1.5 label.cc,1.8,1.9 main.cc,1.41,1.42 py_object.cc,1.10,1.11 py_object.h,1.6,1.7 yarg.cc,1.7,1.8 yarg.h,1.2,1.3
Date: Fri, 12 Apr 2002 11:35:43 -0400

Update of /cvsroot/adonthell/adonthell/src
In directory subversions:/tmp/cvs-serv19047/src

Modified Files:
        dialog.cc dialog.h dialog_screen.cc label.cc main.cc 
        py_object.cc py_object.h yarg.cc yarg.h 
Log Message:
ADDED new dialogue engine with i18n support
FIXED bugs with the dialogue compiler


Index: dialog.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/dialog.cc,v
retrieving revision 1.21
retrieving revision 1.22
diff -C2 -r1.21 -r1.22
*** dialog.cc   23 Sep 2001 14:05:45 -0000      1.21
--- dialog.cc   12 Apr 2002 15:35:40 -0000      1.22
***************
*** 2,6 ****
     $Id$
  
!    (C) Copyright 2000/2001 Kai Sterker <address@hidden>
     Part of the Adonthell Project http://adonthell.linuxgames.com
  
--- 2,6 ----
     $Id$
  
!    (C) Copyright 2000/2001/2002 Kai Sterker <address@hidden>
     Part of the Adonthell Project http://adonthell.linuxgames.com
  
***************
*** 24,29 ****
   */
  
  
! 
  #include <iostream>
  #include <string>
--- 24,32 ----
   */
  
+ #ifdef HAVE_CONFIG_H
+ #include <config.h>
+ #endif
  
! #include <gettext.h>
  #include <iostream>
  #include <string>
***************
*** 33,38 ****
  #include "character.h"
  #include "dialog.h"
- #include "objimpl.h"
- 
  
  // Constructor
--- 36,39 ----
***************
*** 40,44 ****
  {
      strings = NULL;
-     _text = NULL;
  }
  
--- 41,44 ----
***************
*** 67,71 ****
  {
      // Extract the dialogue's strings
!     PyObject *list = dialogue.get_attribute ("strings");
      if (!list) return false;
  
--- 67,71 ----
  {
      // Extract the dialogue's strings
!     PyObject *list = dialogue.get_attribute ("text");
      if (!list) return false;
  
***************
*** 75,79 ****
      strings = new char*[index];
  
!     for (i = 0; i < index; i++)
      {
          s = PyList_GetItem (list, i);
--- 75,79 ----
      strings = new char*[index];
  
!     for (i = 1; i < index; i++)
      {
          s = PyList_GetItem (list, i);
***************
*** 106,239 ****
  void dialog::clear ()
  {
-     dialogue.call_method ("clear", NULL);
      if (strings) delete[] strings;
  }
  
! // Gets the index of either the player or npc array
! void dialog::run (u_int32 index)
  {
!     u_int32 nsz, psz, i, j = 0, k = 0, l = 1;
!     yarg randgen;
!     s_int32 s;
! 
!     PyObject *npc, *player, *cont;
! 
!     // (Re)Init dialog::text
!     if (_text)
      {
!         for (i = 0; i < _text_size; i++) delete _text[i];
!         delete _text;
! 
!         _text_size = 0;
!         _text = NULL;
      }
  
!     // End of dialogue:
!     if (answers[index] == -1)
          return;
      
-     // Execute the next part of the dialogue
-     PyObject *arg = Py_BuildValue ("(i)", answers[index]);
-     dialogue.run (arg);
-     Py_XDECREF (arg);
- 
- #ifdef PY_DEBUG
-     python::show_traceback ();
- #endif
- 
      // Mark the Player's text (if any) as used unless loops allowed
!     if (index != 0)
      {
!         s = choices[index-1];
!         PyObject * loopattr = dialogue.get_attribute ("loop");
!         if (!PySequence_In (loopattr, PyInt_FromLong (s)))
!             used.push_back (s);
!         Py_DECREF (loopattr); 
      }
      
!     // Empty helper arrays
      answers.clear ();
!     choices.clear ();
! 
!     // Now fill in the NPC's and Player's responses:
!     // 1. Get the neccesary attributes of the dialogue class
!     npc = dialogue.get_attribute ("npc");
!     player = dialogue.get_attribute ("player");
!     cont = dialogue.get_attribute ("cont");
!     PyObject *attrcolor = dialogue.get_attribute ("color");
!     _npc_color = PyInt_AsLong (attrcolor);
!     Py_XDECREF (attrcolor); 
! 
!     // 2. Search the NPC part for used text
!     for (i = 0; (int)i < PyList_Size (npc); i++)
      {
!         s = PyInt_AsLong (PyList_GetItem (npc, i));
! 
!         // Remove NPC text that was already used and isn't allowed to loop
!         if (find (used.begin (), used.end (), s) != used.end ())
!             PySequence_DelItem (npc, i--);
!     }
! 
!     nsz = PyList_Size (npc);
!     psz = PyList_Size (player);
  
!     if (nsz != 0)
!     {
!         _text = new char*[nsz+psz];
!         // 3. Randomly chose between possible NPC replies
!         randgen.init (" ", 0, nsz-1);
!     }
!     // End of dialogue
!     else return;
  
!     randgen.randomize ();
!     i = randgen.get (5);
  
!     // The first value of text is the NPC Part
!     s = PyInt_AsLong (PyList_GetItem (npc, i));
  
!     // scan the string for { python code }
!     _text[0] = scan_string (strings[s]);
!     answers.push_back (-1);
! 
!     // 4. Mark the NPC text as used unless it's allowed to loop
!     PyObject * loopattr = dialogue.get_attribute ("loop");
!     if (!PySequence_In (loopattr, PyInt_FromLong (s)))
!         used.push_back (s);
!     Py_XDECREF (loopattr); 
  
!     // 5. Extract the matching player strings
!     while (j <= i)
!     {
!         s = PyInt_AsLong (PyList_GetItem (player, k));
!         
!         if (s == -1) j++;
!         
!         // These are the strings belonging to the chosen NPC text
!         if (j == i && s != -1)
          {
!             // Only display unused text
!             if (find (used.begin (), used.end (), s) == used.end ())
              {
!                 // add string to current text list
!                 _text[l++] = scan_string (strings[s]);
!                 
!                 // Remember Player's possible replies to avoid loops
!                 choices.push_back (s);               
!                 answers.push_back (PyInt_AsLong (PyList_GetItem (cont, k+1)));
              }
          }
-         
-         k++;
      }
  
!     // Insert the target of the NPC text
!     answers[0] = PyInt_AsLong (PyList_GetItem (cont, k-1));
!     _text_size = l;
! 
!     // Free the three lists
!     Py_XDECREF (npc);
!     Py_XDECREF (player);
!     Py_XDECREF (cont);
  }
  
--- 106,235 ----
  void dialog::clear ()
  {
      if (strings) delete[] strings;
  }
  
! // iterate over the dialogue text
! string dialog::text ()
  {
!     string text = "";
!     
!     if (i_text != text_.end ())
      {
!         text = *i_text;
!         i_text++;
      }
+     else i_text = text_.begin ();
+     
+     return text;
+ }
  
! // Gets the index of either the player or npc array
! void dialog::run (u_int32 index)
! {
!     PyObject *arg, *result, *speaker, *speech;
!     s_int32 s, answer = answers[index];
!     u_int32 stop, size;
!     
!     // end of dialogue
!     if (answer == -1)
          return;
      
      // Mark the Player's text (if any) as used unless loops allowed
!     if (index > 0)
      {
!         used.push_back (answer);
      }
      
!     // empty previous dialogue text
!     text_.clear ();
      answers.clear ();
!     
!     do
      {
!         // Execute the next part of the dialogue
!         arg = Py_BuildValue ("(i)", answer);
!         dialogue.run (arg);
! #ifdef PY_DEBUG
!         python::show_traceback ();
! #endif
!         Py_XDECREF (arg);
!     
!         // Now fill in the NPC's and Player's responses:
!         // 1. Get the neccesary attributes of the dialogue class
!         speaker = dialogue.get_attribute ("speaker");
!         speech = dialogue.get_attribute ("speech");
  
!         // 2. Search the NPC part for used text
!         for (int i = 0; i < PyList_Size (speech); i++)
!         {
!             s = PyInt_AsLong (PyList_GetItem (speech, i));
  
!             // Remove text that was already used and isn't allowed to loop
!             if (find (used.begin (), used.end (), s) != used.end ())
!             {
!                 PySequence_DelItem (speaker, i);
!                 PySequence_DelItem (speech, i--);
!             }
!         }
  
!         // check if some text is left at all
!         size = PyList_Size (speech);
!         if (size == 0) return;
  
!         // prepare the random number generator        
!         yarg::range (0, size - 1);
  
!         // check type of speaker
!         if (PyList_GetItem (speaker, 0) != Py_None)
          {
!             // got NPC text, so let the engine decide
!             int rnd = yarg::get ();
!             
!             // get the text
!             answer = PyInt_AsLong (PyList_GetItem (speech, rnd));
!             text_.push_back (gettext (scan_string (strings[answer])));
!             
!             // get the NPC color
!             char *npc = PyString_AsString (PyList_GetItem (speaker, rnd));
!             if (npc != NULL)
              {
!                 if (strcmp ("Narrator", npc) == 0) npc_color_ = 0;
!                 else npc_color_ = data::characters[npc]->get_color ();
              }
+             
+             // check whether we shall continue or not
+             arg = Py_BuildValue ("(i)", answer);
+             result = dialogue.call_method_ret ("stop", arg);
+             stop = PyInt_AsLong (result);
+             Py_XDECREF (result);
+             Py_XDECREF (arg);
+             
+             // make sure this NPC text can't be used any more
+             used.push_back (answer);
+             answers.push_back (answer);
+         }
+         else
+         {
+             // got Player text, so let the player decide
+             for (u_int32 i = 0; i < size; i++)
+             {
+                 // simply add all text to let the player select an answer
+                 answer = PyInt_AsLong (PyList_GetItem (speech, i));
+                 text_.push_back (gettext (scan_string (strings[answer])));
+                 answers.push_back (answer);
+             }
+             
+             // let the player make his decision
+             stop = true;
          }
      }
+     while (!stop);            
  
!     // init the iterator for dialogue text retrieval
!     i_text = text_.begin ();
!     
!     // cleanup
!     Py_XDECREF (speaker);
!     Py_XDECREF (speech);
  }
  
***************
*** 329,333 ****
          if (result)
              if (PyString_Check (result))
!                 mid = PyString_AS_STRING (result);    
          
          // Replace existing with new, changed string
--- 325,329 ----
          if (result)
              if (PyString_Check (result))
!                 mid = (char*) gettext (PyString_AS_STRING (result));    
          
          // Replace existing with new, changed string

Index: dialog.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/dialog.h,v
retrieving revision 1.47
retrieving revision 1.48
diff -C2 -r1.47 -r1.48
*** dialog.h    15 Oct 2001 21:26:52 -0000      1.47
--- dialog.h    12 Apr 2002 15:35:40 -0000      1.48
***************
*** 2,6 ****
     $Id$
  
!    (C) Copyright 2000/2001 Kai Sterker <address@hidden>
     Part of the Adonthell Project http://adonthell.linuxgames.com
  
--- 2,6 ----
     $Id$
  
!    (C) Copyright 2000/2001/2002 Kai Sterker <address@hidden>
     Part of the Adonthell Project http://adonthell.linuxgames.com
  
***************
*** 115,119 ****
       * @return the npc color.
       */
!     u_int32 npc_color () { return _npc_color; }
  
      /** 
--- 115,119 ----
       * @return the npc color.
       */
!     u_int32 npc_color () { return npc_color_; }
  
      /** 
***************
*** 126,152 ****
       * @sa text()
       */
!     u_int32 text_size () { return _text_size; }
  
      /** 
!      * Returns the dialogue's %text. Depending on the current state
       * of the dialogue, there can be multiple alternatives. The first
       * string is always the NPC's speech. Any following strings are
!      * the player's possible reactions. The value passed to the run()
       * method is the (zero-based) index of the alternative chosen by
       * the player.
       *
!      * 
!      * @return array of strings containing the %text of the dialog or
!      *         \e NULL when the dialogue is finished.
       * @sa text_size()
       */
!     char** text () { return _text; }
      
  private:
      py_object dialogue;             // Points to the instantiated dialogue 
class
      char **strings;                 // The dialogue text
!     char **_text;                   // NPC's speech and according Player 
responses
!     u_int32 _npc_color;             // The color of the NPC's text
!     u_int32 _text_size;             // Number of strings in text
  
      vector<s_int32> answers;        // The indices with which to call 
instance.run () 
--- 126,151 ----
       * @sa text()
       */
!     u_int32 text_size () { return text_.size (); }
  
      /** 
!      * Iterates over the dialogue's %text. Depending on the current state
       * of the dialogue, there can be multiple alternatives. The first
       * string is always the NPC's speech. Any following strings are
!      * the player's possible reactions. The value passed to the run ()
       * method is the (zero-based) index of the alternative chosen by
       * the player.
       *
!      * @return the next string in the list of text, or the empty string ""
!      *      when the end of the array of strings has been reached.
       * @sa text_size()
       */
!     string text ();
      
  private:
      py_object dialogue;             // Points to the instantiated dialogue 
class
      char **strings;                 // The dialogue text
!     vector<string> text_;           // NPC's speech and according Player 
responses
!     vector<string>::iterator i_text;// Iterator for the text_ vector
!     u_int32 npc_color_;             // The color of the NPC's text
  
      vector<s_int32> answers;        // The indices with which to call 
instance.run () 

Index: dialog_screen.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/dialog_screen.cc,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -r1.4 -r1.5
*** dialog_screen.cc    23 Sep 2001 14:05:45 -0000      1.4
--- dialog_screen.cc    12 Apr 2002 15:35:40 -0000      1.5
***************
*** 148,173 ****
          answer = -1;  
      }
!     else
!     {
!         // Make the set_portrait/name/npc functions available to the dialogue 
script
!         PyObject *instance = python::pass_instance (this, "dialog_screen");
! 
!         PyObject *setname = PyObject_GetAttrString (instance, "set_name");
!         PyObject *setportrait = PyObject_GetAttrString (instance, 
"set_portrait");
!         PyObject *setnpc = PyObject_GetAttrString (instance, "set_npc");
! 
!         PyObject *dlg_instance = dlg->get_instance ();
! 
!         PyObject_SetAttrString (dlg_instance, "set_name", setname);
!         PyObject_SetAttrString (dlg_instance, "set_portrait", setportrait);
!         PyObject_SetAttrString (dlg_instance, "set_npc", setnpc);
! 
!         Py_DECREF (setname);
!         Py_DECREF (setportrait);
!         Py_DECREF (setnpc);
!         Py_DECREF (instance);
! 
!         answer = 0;
!     }
  
      // Clean up
--- 148,152 ----
          answer = -1;  
      }
!     else answer = 0;
  
      // Clean up
***************
*** 187,191 ****
  void dialog_screen::run ()
  {
!     u_int32 i;
      win_label *l;
  
--- 166,170 ----
  void dialog_screen::run ()
  {
!     u_int32 i = 0;
      win_label *l;
  
***************
*** 201,205 ****
  
      // End of dialogue
!     if (!dlg->text ())
      {
          is_running = false;
--- 180,184 ----
  
      // End of dialogue
!     if (dlg->text_size () == 0)
      {
          is_running = false;
***************
*** 208,219 ****
  
      // Add NPC text and all player reactions to container
!     for (i = 0; i < dlg->text_size (); i++)
      {
          l = new win_label();
!         l->set_font (i == 0 ? *fonts[dlg->npc_color()] : *fonts[1]);
          l->move(0,0);
          ((label*)l)->resize(180,0);
          l->set_form(label::AUTO_HEIGHT);
!         l->set_text (dlg->text()[i]);
          l->set_visible (true);
          l->pack();
--- 187,198 ----
  
      // Add NPC text and all player reactions to container
!     for (string txt = dlg->text (); txt != ""; txt = dlg->text (), i++)
      {
          l = new win_label();
!         l->set_font (i == 0 ? *fonts[dlg->npc_color ()] : *fonts[1]);
          l->move(0,0);
          ((label*)l)->resize(180,0);
          l->set_form(label::AUTO_HEIGHT);
!         l->set_text (txt);
          l->set_visible (true);
          l->pack();
***************
*** 223,227 ****
  
      // Either select the single NPC speech ...
!     if (dlg->text_size() == 1)
          sel->set_default_object (cur_answers.front ());
  
--- 202,206 ----
  
      // Either select the single NPC speech ...
!     if (dlg->text_size () == 1)
          sel->set_default_object (cur_answers.front ());
  

Index: label.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/label.cc,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -r1.8 -r1.9
*** label.cc    23 Nov 2001 13:24:54 -0000      1.8
--- label.cc    12 Apr 2002 15:35:40 -0000      1.9
***************
*** 497,502 ****
           j++)
      {
!         (*my_font_) [my_text_[j]].draw (tx, ty, NULL, this);
!         tx += (*my_font_) [my_text_[j]].length (); 
      }
      ty += my_font_->height ();
--- 497,505 ----
           j++)
      {
!         if (my_font_->in_table (my_text_[j]))
!         {
!             (*my_font_) [my_text_[j]].draw (tx, ty, NULL, this);
!             tx += (*my_font_) [my_text_[j]].length (); 
!         }
      }
      ty += my_font_->height ();
***************
*** 512,517 ****
               j++)
          {
!             (*my_font_) [my_text_[j]].draw (tx, ty, NULL, this);
!             tx += (*my_font_) [my_text_[j]].length (); 
          }
          ty += my_font_->height ();
--- 515,523 ----
               j++)
          {
!             if (my_font_->in_table (my_text_[j]))
!             {
!                 (*my_font_) [my_text_[j]].draw (tx, ty, NULL, this);
!                 tx += (*my_font_) [my_text_[j]].length (); 
!             }
          }
          ty += my_font_->height ();

Index: main.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/main.cc,v
retrieving revision 1.41
retrieving revision 1.42
diff -C2 -r1.41 -r1.42
*** main.cc     7 Apr 2002 11:56:29 -0000       1.41
--- main.cc     12 Apr 2002 15:35:40 -0000      1.42
***************
*** 22,26 ****
--- 22,32 ----
   * 
   */
+  
+ #ifdef HAVE_CONFIG_H
+ #include <config.h>
+ #endif
  
+ #include <locale.h>
+ #include "gettext.h"
  #include "audio.h"
  #include "character.h"
***************
*** 30,33 ****
--- 36,40 ----
  #include "python_class.h"
  #include "screen.h"
+ #include "yarg.h"
  #include "win_manager.h"
  #include "win_theme.h"
***************
*** 105,108 ****
--- 112,122 ----
      game::init (myconfig.gamedir);
      
+     // Init i18n
+     setlocale (LC_MESSAGES, "");
+     
+     // open the catalogue
+     bindtextdomain (myconfig.game_name.c_str (), "/usr/local/share/locale");
+     textdomain (myconfig.game_name.c_str ());
+ 
      // init game loading/saving system
      gamedata::init (myconfig.get_adonthellrc (), myconfig.gamedir, 
myconfig.game_name); 
***************
*** 121,126 ****
      // init python interpreter
      python::init (); 
- 
      init_python();
      // init the game data
      data::engine = new adonthell;
--- 135,140 ----
      // init python interpreter
      python::init (); 
      init_python();
+ 
      // init the game data
      data::engine = new adonthell;
***************
*** 130,133 ****
--- 144,151 ----
      win_manager::init (); 
  
+     // init random number generator
+     yarg::init (myconfig.game_name);
+     yarg::randomize ();
+     
      // It's up to the game what happens here
      python::exec_file ("init");

Index: py_object.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/py_object.cc,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -r1.10 -r1.11
*** py_object.cc        7 Jan 2002 13:41:38 -0000       1.10
--- py_object.cc        12 Apr 2002 15:35:40 -0000      1.11
***************
*** 94,100 ****
  }
  
! // Execute the body of the script
! void py_object::call_method (const string & name, PyObject * args = NULL)
  {
      if (instance)
      {
--- 94,102 ----
  }
  
! // Execute a method of the script
! PyObject* py_object::call_method_ret (const string & name, PyObject * args = 
NULL)
  {
+     PyObject *result = NULL;
+      
      if (instance)
      {
***************
*** 103,108 ****
          if (PyCallable_Check (tocall) == 1)
          {    
!             PyObject *res = PyObject_CallObject (tocall, args);
!             Py_XDECREF (res); 
              Py_DECREF (tocall); 
          }
--- 105,109 ----
          if (PyCallable_Check (tocall) == 1)
          {    
!             result = PyObject_CallObject (tocall, args);
              Py_DECREF (tocall); 
          }
***************
*** 111,114 ****
--- 112,117 ----
  #endif
      }
+     
+     return result;
  }
  

Index: py_object.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/py_object.h,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -r1.6 -r1.7
*** py_object.h 22 Sep 2001 20:22:13 -0000      1.6
--- py_object.h 12 Apr 2002 15:35:40 -0000      1.7
***************
*** 107,110 ****
--- 107,120 ----
          return script_file_;
      }
+     
+     /** 
+      * Call a method of this object.
+      * 
+      * @param name name of the method to call.
+      * @param args Python tuple containing the arguments to pass to the 
method.
+      * @return the return value of the method as PyObject. Needs to be 
+      *     Py_DECREF'd when no longer needed.
+      */
+     PyObject *call_method_ret (const string & name, PyObject * args = NULL);
       
      /** 
***************
*** 114,118 ****
       * @param args Python tuple containing the arguments to pass to the 
method.
       */
!     void call_method (const string & name, PyObject * args = NULL); 
      
      /** 
--- 124,132 ----
       * @param args Python tuple containing the arguments to pass to the 
method.
       */
!     void call_method (const string & name, PyObject * args = NULL)
!     {
!         PyObject *result = call_method_ret (name, args);
!         Py_XDECREF (result);
!     }
      
      /** 

Index: yarg.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/yarg.cc,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -r1.7 -r1.8
*** yarg.cc     21 May 2001 20:44:49 -0000      1.7
--- yarg.cc     12 Apr 2002 15:35:40 -0000      1.8
***************
*** 2,6 ****
     $Id$
  
!    (C) Copyright 1998 Kai Sterker <address@hidden>
     Part of the Adonthell Project http://adonthell.linuxgames.com
  
--- 2,6 ----
     $Id$
  
!    (C) Copyright 1998/2002 Kai Sterker <address@hidden>
     Part of the Adonthell Project http://adonthell.linuxgames.com
  
***************
*** 13,38 ****
  */
  
! #include <string.h>
  #include <time.h>
  #include "yarg.h"
  
! yarg::yarg ()
! { 
!    init(" ", 0, 100); 
! }
! 
! yarg::yarg (char *s, int a, int e) 
! { 
!    init(s, a, e);   
! }
  
  // Init the generator with a seed and a range 
! void yarg::init (char *str, int mn, int mx)
  {
!       int s = strlen (str);
  
        for (int i = 0; i < 16; i++)
                schieberegister[i] = str[i%s];
  
        min = (mx < mn ? mx : mn);
        max = (mn > mx ? mn : mx);
--- 13,46 ----
  */
  
! /**
!  * @file   yarg.cc
!  * @author Kai Sterker <address@hidden>
!  * 
!  * @brief  Yet Another Random Number Genarator.
!  * 
!  * 
!  */
! 
  #include <time.h>
  #include "yarg.h"
  
! unsigned char yarg::schieberegister[16];// Here the random numbers are created
! int yarg::min;                          // Smallest possible numbe
! int yarg::max;                          // Largest possible number
  
  // Init the generator with a seed and a range 
! void yarg::init (std::string str, int mn, int mx)
  {
!       int s = str.length ();
  
        for (int i = 0; i < 16; i++)
                schieberegister[i] = str[i%s];
  
+     range (mn, mx);
+ }
+ 
+ // change the range of the generator
+ void yarg::range (int mn, int mx)
+ {
        min = (mx < mn ? mx : mn);
        max = (mn > mx ? mn : mx);
***************
*** 85,87 ****
      return (zahl%(max - min + 1)) + min;
  }
- 
--- 93,94 ----

Index: yarg.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/yarg.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -r1.2 -r1.3
*** yarg.h      7 Mar 2001 06:43:22 -0000       1.2
--- yarg.h      12 Apr 2002 15:35:40 -0000      1.3
***************
*** 2,6 ****
     $Id$
  
!    (C) Copyright 1998 Kai Sterker <address@hidden>
     Part of the Adonthell Project http://adonthell.linuxgames.com
  
--- 2,6 ----
     $Id$
  
!    (C) Copyright 1998/2002 Kai Sterker <address@hidden>
     Part of the Adonthell Project http://adonthell.linuxgames.com
  
***************
*** 13,37 ****
  */
  
! // yet another random generator :)
  
! #include <string.h>
  
  class yarg
  {
  public:
!     ~yarg () { }
!     yarg ();
!     yarg (char *s, int a, int e);          // Random seed and number range
!               
!     int get (int = 1);                     // Return next (nth) random number
!     void randomize ();                     // Mix with current time
!     void init (char*, int, int);           // Like the constructor
! 
  private:
!     void folgezustand ();                  // Calculate next state of register
!     int       zufallszahl ();                    // Calculate the number from 
the register
  
!     unsigned char schieberegister[16];     // Here the random numbers are 
created
!     int min;                               // Smallest possible numbe
!     int max;                               // Largest possible number
  };
--- 13,41 ----
  */
  
! /**
!  * @file   yarg.h
!  * @author Kai Sterker <address@hidden>
!  * 
!  * @brief  Yet Another Random Number Genarator.
!  * 
!  * 
!  */
  
! #include <string>
  
  class yarg
  {
  public:
!     static int get (int = 1);                // Return next (nth) random 
number
!     static void randomize ();                // Mix with current time
!     static void init (std::string, int = 0, int = 100);// Seed the generator
!     static void range (int, int);            // Change the range
!     
  private:
!     static void folgezustand ();             // Calculate next state of 
register
!     static int        zufallszahl ();              // Calculate the number 
from the register
  
!     static unsigned char schieberegister[16];// Here the random numbers are 
created
!     static int min;                          // Smallest possible numbe
!     static int max;                          // Largest possible number
  };




reply via email to

[Prev in Thread] Current Thread [Next in Thread]