Subversion Repositories Kolibri OS

Rev

Rev 5576 | Rev 5598 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5576 Rev 5582
1
/*******************************************************************************
1
/*******************************************************************************
2
 
2
 
3
    MenuetOS MineSweeper
3
    MenuetOS MineSweeper
4
    Copyright (C) 2003  Ivan Poddubny
4
    Copyright (C) 2003  Ivan Poddubny
5
 
5
 
6
    This program is free software; you can redistribute it and/or modify
6
    This program is free software; you can redistribute it and/or modify
7
    it under the terms of the GNU General Public License as published by
7
    it under the terms of the GNU General Public License as published by
8
    the Free Software Foundation; either version 2 of the License, or
8
    the Free Software Foundation; either version 2 of the License, or
9
    (at your option) any later version.
9
    (at your option) any later version.
10
 
10
 
11
    This program is distributed in the hope that it will be useful,
11
    This program is distributed in the hope that it will be useful,
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
    GNU General Public License for more details.
14
    GNU General Public License for more details.
15
 
15
 
16
    You should have received a copy of the GNU General Public License
16
    You should have received a copy of the GNU General Public License
17
    along with this program; if not, write to the Free Software
17
    along with this program; if not, write to the Free Software
18
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 
19
 
20
*******************************************************************************/
20
*******************************************************************************/
21
#define MASK_RAND 123456789
21
#define MASK_RAND 123456789
22
#define IQ_RAND 12773
22
#define IQ_RAND 12773
23
#define IA_RAND 16807
23
#define IA_RAND 16807
24
#define IR_RAND 2836
24
#define IR_RAND 2836
25
#define IM_RAND 2147483647
25
#define IM_RAND 2147483647
26
#define AM_RAND (1./2147483647)
26
#define AM_RAND (1./2147483647)
27
 
27
 
28
inline fastcall int random( ECX)
28
inline fastcall int random( ECX)
29
// get pseudo-random number - ïîëó÷èòü ïñåâäîñëó÷àéíîå ÷èñëî
29
// get pseudo-random number - ïîëó÷èòü ïñåâäîñëó÷àéíîå ÷èñëî
30
{
30
{
31
  $push ebx
31
  $push ebx
32
 
32
 
33
  $rdtsc        // eax & edx
33
  $rdtsc        // eax & edx
34
  $xor eax,edx
34
  $xor eax,edx
35
  $not eax
35
  $not eax
36
 
36
 
37
  EBX = __generator;
37
  EBX = __generator;
38
  $ror ebx,3
38
  $ror ebx,3
39
  $xor ebx,0xdeadbeef
39
  $xor ebx,0xdeadbeef
40
  EBX += EAX;
40
  EBX += EAX;
41
  __generator = EBX;
41
  __generator = EBX;
42
  
42
  
43
  EAX += EBX;
43
  EAX += EBX;
44
  EAX = EAX % ECX;
44
  EAX = EAX % ECX;
45
  
45
  
46
  $pop ebx  
46
  $pop ebx  
47
}
47
}
48
 
48
 
49
:inline long unirand0(void)
49
inline long unirand0(void)
50
{
50
{
51
	 long k,ans,tmp,save;
51
	 long k,ans,tmp,save;
52
	 save = __generator;
52
	 save = __generator;
53
	 __generator^=MASK_RAND;   /* avoid __generator==0 */
53
	 __generator^=MASK_RAND;   /* avoid __generator==0 */
54
	 k=__generator/IQ_RAND;
54
	 k=__generator/IQ_RAND;
55
	 tmp=__generator-k*IQ_RAND;
55
	 tmp=__generator-k*IQ_RAND;
56
	 __generator*=IA_RAND*tmp;
56
	 __generator*=IA_RAND*tmp;
57
	 __generator-=IR_RAND*k;
57
	 __generator-=IR_RAND*k;
58
	 if(__generator<0) __generator+=IM_RAND;
58
	 if(__generator<0) __generator+=IM_RAND;
59
	 if(save == __generator) return unirand0();
59
	 if(save == __generator) return unirand0();
60
	 ans=__generator;
60
	 ans=__generator;
61
	 __generator^=MASK_RAND;   /* restore unmasked dummy */
61
	 __generator^=MASK_RAND;   /* restore unmasked dummy */
62
	 return ans;
62
	 return ans;
63
}
63
}
64
 
64
 
65
:long RAND_A,RAND_C,RAND_TMP;
65
:long RAND_A,RAND_C,RAND_TMP;
66
:inline long rand(signed long x1,x2)
66
inline long rand(signed long x1,x2)
67
{
67
{
68
	long tmp,xx;
68
	long tmp,xx;
69
	RAND_A = __generator;
69
	RAND_A = __generator;
70
	__generator = RAND_A*__generator+RAND_C;
70
	__generator = RAND_A*__generator+RAND_C;
71
	RAND_C = __generator^RAND_A;
71
	RAND_C = __generator^RAND_A;
72
	RAND_C>>=1;
72
	RAND_C>>=1;
73
	RAND_A<<=1;
73
	RAND_A<<=1;
74
	__generator^=RAND_A;
74
	__generator^=RAND_A;
75
	xx=x2;
75
	xx=x2;
76
	if(x1<0)xx+=-x1;
76
	if(x1<0)xx+=-x1;
77
	tmp = __generator%xx;
77
	tmp = __generator%xx;
78
	if(tmp<0)tmp=-tmp;
78
	if(tmp<0)tmp=-tmp;
79
	tmp+=x1;
79
	tmp+=x1;
80
	return tmp;
80
	return tmp;
81
}
81
}
82
 
82
 
83
inline fastcall randomize()
83
inline fastcall randomize()
84
// initialize random number __generator - èíèöèàëèçèðîâàòü ãåíåðàòîð ñëó÷àéíûõ ÷èñåë
84
// initialize random number __generator - èíèöèàëèçèðîâàòü ãåíåðàòîð ñëó÷àéíûõ ÷èñåë
85
{
85
{
86
  asm
86
  asm
87
  {
87
  {
88
    mov eax,3
88
    mov eax,3
89
    int 0x40
89
    int 0x40
90
    ror eax,16
90
    ror eax,16
91
  }
91
  }
92
  //if(EAX == __generator)return randomize();
92
  //if(EAX == __generator)return randomize();
93
  __generator = EAX;
93
  __generator = EAX;
94
}
94
}
95
>
95
>
96
>
96
>
97
>
97
>
98
>
98
>