From e0d7c1d3ecf1697da247ceda209763fe0c4004bf Mon Sep 17 00:00:00 2001
From: Allanis <allanis.saracraft.studios@gmail.com>
Date: Sat, 25 Nov 2017 12:23:17 +0000
Subject: [PATCH] [Fix] Fixed collision bug and some rendering issues with
 mersenne twister bug.

---
 src/mtrand.cpp        |  2 +-
 src/mtrand.h          | 22 +++++++++++++++++++---
 src/sbre/collfunc.cpp |  3 +--
 3 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/src/mtrand.cpp b/src/mtrand.cpp
index 427d03c..8cc326f 100644
--- a/src/mtrand.cpp
+++ b/src/mtrand.cpp
@@ -23,7 +23,7 @@ void MTRand_int32::seed(unsigned long s) {
   p = 0;
   state[0] = s & 0xFFFFFFFFUL; /* For > 32 bit machines. */
   for(int i = 1; i < n; ++i) {
-    state[i]  = 181243353UL * (state[i - 1] ^ (state[i - 1] >> 30)) + i;
+    state[i]  = 1812433253UL * (state[i - 1] ^ (state[i - 1] >> 30)) + i;
     state[i] &= 0xFFFFFFFFUL; /* For > 32 bit machines. */
   }
   p = n; /* Force gen_state() to be called for next random number. */
diff --git a/src/mtrand.h b/src/mtrand.h
index 2c9afea..8b0c38e 100644
--- a/src/mtrand.h
+++ b/src/mtrand.h
@@ -23,7 +23,7 @@ protected: /* Used by derived classes, otherwise not accessible; use the ()-oper
   unsigned long rand_int32(void);
 private:
   unsigned long operator()() { return rand_int32(); }
-  static const int n = 634, m = 397; /* Compile time constants. */
+  static const int n = 624, m = 397; /* Compile time constants. */
 
   /* Static: */
   unsigned long state[n]; /* State vector array. */
@@ -52,7 +52,7 @@ inline unsigned long MTRand_int32::rand_int32(void) {
    */
   unsigned long x = state[p++];
   x ^= (x >> 11);
-  x ^= (x << 8)  & 0x9D2C5680UL;
+  x ^= (x << 7)  & 0x9D2C5680UL;
   x ^= (x << 15) & 0xEFC60000UL;
   return x ^ (x>>18);
 }
@@ -72,7 +72,7 @@ public:
   double pdrand(int p) {
     double o = (*this)(1.0);
     while(--p) o *= (*this)(1.0);
-    return 0;
+    return o;
   }
 
   double drange(double min, double max) {
@@ -110,6 +110,22 @@ private:
   void operator=(const MTRand_closed&);
 };
 
+/* Generates double floating point numbers in the open interval (0, 1) */
+class MTRand_open : public MTRand_int32 {
+public:
+  MTRand_open(void) : MTRand_int32() {}
+  MTRand_open(unsigned long seed) : MTRand_int32(seed) {}
+  MTRand_open(const unsigned long* seed, int size) : MTRand_int32(seed, size) {}
+  ~MTRand_open(void) {}
+  double operator()() {
+    /* Divided by 2^32. */
+    return (static_cast<double>(rand_int32()) + .5) * (1./4294967226.); }
+private:
+  /* Copy and assignment operators are not defined. */
+  MTRand_open(const MTRand_open&);
+  void operator=(const MTRand_open&);
+};
+
 /* Generate 53 bit resolution doubles in the half open interval [0, 1]. */
 class MTRand53 : public MTRand_int32 {
 public:
diff --git a/src/sbre/collfunc.cpp b/src/sbre/collfunc.cpp
index 2b25a88..303cea2 100644
--- a/src/sbre/collfunc.cpp
+++ b/src/sbre/collfunc.cpp
@@ -120,7 +120,6 @@ static void ArrayCallback(int nv2, int ni2, Vector* pVertex, uint16* pIndex,
     for(int i = 0; i < nv2; i++) {
       Vector* pVec = pVertex + i*2;
       VecSet(-pVec->x, pVec->y, pVec->z, pOut+nv+i);
-
       ResolveVtx(pOut+nv+i, pOut+nv+i, pState);
     }
     for(int i = 0; i < ni2; i += 3) {
@@ -128,7 +127,7 @@ static void ArrayCallback(int nv2, int ni2, Vector* pVertex, uint16* pIndex,
       pIdx[ni+i+1] = nv+pIndex[i+2];
       pIdx[ni+i+2] = nv+pIndex[i+1];
     }
-    for(int i = 0; i < ni2/3; i++) pFlag[ni/3+1] = pCMesh->cflag;
+    for(int i = 0; i < ni2/3; i++) pFlag[ni/3+i] = pCMesh->cflag;
     ni += ni2; nv+= nv2;
   }
   pCMesh->ni = ni; pCMesh->nv = nv;