00001 /****************************************************************************** 00002 * Wormux is a convivial mass murder game. 00003 * Copyright (C) 2001-2004 Lawrence Azzoug. 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program 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 program; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA 00018 ****************************************************************************** 00019 * Functions to generate random datas (number, boolean, etc.). 00020 *****************************************************************************/ 00021 00022 #include <time.h> 00023 #include <math.h> 00024 #include <stdlib.h> 00025 #include <assert.h> 00026 #include "randomsync.h" 00027 #include "network.h" 00028 #include "../include/action_handler.h" 00029 00030 const uint table_size = 128; //Number of pregerated numbers 00031 00032 RandomSync randomSync; 00033 00034 RandomSync::RandomSync(){ 00035 } 00036 00037 void RandomSync::Init(){ 00038 //If we are a client on the network, we don't generate any random number 00039 if(network.IsClient()) return; 00040 00041 srand( time(NULL) ); 00042 00043 rnd_table.clear(); 00044 00045 //Fill the pregenerated tables: 00046 for(uint i=0; i < table_size; i++) 00047 GenerateTable(); 00048 } 00049 00050 void RandomSync::GenerateTable() 00051 { 00052 //Add a random number to the table, send it over network if needed 00053 double nbr = rand(); 00054 AddToTable(nbr); 00055 if(network.IsServer()) ActionHandler::GetInstance()->NewAction(new Action(Action::ACTION_SEND_RANDOM,nbr)); 00056 } 00057 00058 void RandomSync::AddToTable(double nbr) 00059 { 00060 rnd_table.push_back(nbr); 00061 } 00062 00063 double RandomSync::GetRand() 00064 { 00065 if(network.IsServer() || network.IsLocal()) GenerateTable(); 00066 00067 // If the table is empty freeze until the server have sent something 00068 while(rnd_table.size() == 0) 00069 { 00070 SDL_Delay(100); 00071 ActionHandler::GetInstance()->ExecActions(); 00072 } 00073 00074 double nbr = rnd_table.front(); 00075 rnd_table.pop_front(); 00076 return nbr; 00077 } 00078 00079 bool RandomSync::GetBool(){ 00080 int moitie = RAND_MAX/2; 00081 return (GetRand() <= moitie); 00082 } 00083 00087 long RandomSync::GetLong(long min, long max){ 00088 return min + (long)GetDouble(max - min + 1); 00089 } 00090 00091 double RandomSync::GetDouble(double min, double max){ 00092 return min + GetDouble(max - min); 00093 } 00094 00095 double RandomSync::GetDouble(double max){ 00096 return max * GetDouble(); 00097 } 00098 00104 double RandomSync::GetDouble(){ 00105 return 1.0*GetRand()/(RAND_MAX + 1.0); 00106 } 00107 00114 Point2i RandomSync::GetPoint(const Rectanglei &rect){ 00115 Point2i topPoint = rect.GetPosition(); 00116 Point2i bottomPoint = rect.GetBottomRightPoint(); 00117 00118 return Point2i( GetLong(topPoint.x, bottomPoint.x), 00119 GetLong(topPoint.y, bottomPoint.y) ); 00120 } 00121 00122 Point2i RandomSync::GetPoint(const Point2i &pt){ 00123 return Point2i( GetLong(0, pt.x - 1), GetLong(0, pt.y - 1) ); 00124 }