openclonk/src/lib/C4Random.cpp

71 lines
1.6 KiB
C++
Raw Normal View History

2009-05-08 13:28:41 +00:00
/*
* OpenClonk, http://www.openclonk.org
*
* Copyright (c) 1998-2000 Matthes Bender
2009-05-08 13:28:41 +00:00
* Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de
*
* Portions might be copyrighted by other authors who have contributed
* to OpenClonk.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
* See isc_license.txt for full license and disclaimer.
*
* "Clonk" is a registered trademark of Matthes Bender.
* See clonk_trademark_license.txt for full license.
*/
/* Buffered fast and network-safe random */
#include <Standard.h>
2009-08-25 15:50:51 +00:00
#include "C4Prototypes.h"
2009-05-08 13:28:41 +00:00
#include <C4Random.h>
2009-08-25 15:50:51 +00:00
#include <C4Record.h>
2009-05-08 13:28:41 +00:00
2009-08-04 22:53:38 +00:00
int RandomCount = 0;
unsigned int RandomHold = 0;
2009-05-08 13:28:41 +00:00
//--------------------------------------- Random3 -------------------------------------------------------
const int FRndRes=500;
int32_t FRndBuf3[FRndRes];
int32_t FRndPtr3;
void Randomize3()
{
FRndPtr3 = 0;
for (int cnt=0; cnt<FRndRes; cnt++) FRndBuf3[cnt]=Random(3)-1;
}
int Rnd3()
{
FRndPtr3++; if (FRndPtr3==FRndRes) FRndPtr3=0;
#ifdef DEBUGREC
AddDbgRec(RCT_Rn3, &FRndPtr3, sizeof(int));
#endif
return FRndBuf3[FRndPtr3];
}
2009-08-25 15:50:51 +00:00
#ifdef DEBUGREC
int Random(int iRange)
{
// next pseudorandom value
RandomCount++;
C4RCRandom rc;
rc.Cnt=RandomCount;
rc.Range=iRange;
if (iRange==0)
rc.Val=0;
else
{
RandomHold = RandomHold * 214013L + 2531011L;
rc.Val=(RandomHold >> 16) % iRange;
}
AddDbgRec(RCT_Random, &rc, sizeof(rc));
return rc.Val;
}
#endif