00001 //////////////////////////////////////////////////////////////////////////////// 00002 // Scorched3D (c) 2000-2009 00003 // 00004 // This file is part of Scorched3D. 00005 // 00006 // Scorched3D is free software; you can redistribute it and/or modify 00007 // it under the terms of the GNU General Public License as published by 00008 // the Free Software Foundation; either version 2 of the License, or 00009 // (at your option) any later version. 00010 // 00011 // Scorched3D is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 // 00016 // You should have received a copy of the GNU General Public License 00017 // along with Scorched3D; if not, write to the Free Software 00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 //////////////////////////////////////////////////////////////////////////////// 00020 00021 #include <common/RandomGenerator.h> 00022 #include <common/Defines.h> 00023 #include <net/NetBuffer.h> 00024 #include <stdlib.h> 00025 #include <stdio.h> 00026 00027 // Yes you guessed it, a really sucky way to create cross platform 00028 // random numbers. Store them in a file and read them in each system! 00029 00030 unsigned int RandomGenerator::bufferSize_ = 0; 00031 unsigned int *RandomGenerator::buffer_ = 0; 00032 00033 RandomGenerator::RandomGenerator() : 00034 position_(0) 00035 { 00036 // Cache the buffer so we only read it once 00037 // We can create many random generators easily now 00038 if (!buffer_) 00039 { 00040 std::string fileName = S3D::getDataFile("data/random.no"); 00041 FILE *in = fopen(fileName.c_str(), "rb"); 00042 DIALOG_ASSERT(in); 00043 bufferSize_= 100000; 00044 unsigned int *tmpbuffer = new unsigned int[bufferSize_]; 00045 int size = fread(tmpbuffer, sizeof(unsigned int), bufferSize_, in); 00046 fclose(in); 00047 DIALOG_ASSERT(size == bufferSize_); 00048 00049 buffer_ = new unsigned int[bufferSize_]; 00050 for (unsigned int i=0; i<bufferSize_; i++) 00051 { 00052 unsigned int value = tmpbuffer[i]; 00053 buffer_[i] = SDLNet_Read32(&value); 00054 } 00055 delete [] tmpbuffer; 00056 } 00057 } 00058 00059 RandomGenerator::~RandomGenerator() 00060 { 00061 } 00062 00063 void RandomGenerator::seed(unsigned int seed) 00064 { 00065 position_ = seed; 00066 } 00067 00068 unsigned int RandomGenerator::getSeed() 00069 { 00070 return position_; 00071 } 00072 00073 unsigned int RandomGenerator::getRandUInt() 00074 { 00075 unsigned int pos = position_ % bufferSize_; 00076 position_++; 00077 00078 unsigned int maskpos = (position_ / bufferSize_) % 32; 00079 unsigned int value = buffer_[pos]; 00080 unsigned int lvalue = value << (32 - maskpos); 00081 unsigned int rvalue = value >> maskpos; 00082 return lvalue | rvalue; 00083 } 00084 00085 fixed RandomGenerator::getRandFixed() 00086 { 00087 unsigned int rd = getRandUInt(); 00088 unsigned int fract = rd % 10000; 00089 fixed result(true, fract); 00090 00091 return result; 00092 }
1.5.3