-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenescience.sol
More file actions
249 lines (217 loc) · 9.28 KB
/
genescience.sol
File metadata and controls
249 lines (217 loc) · 9.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
pragma solidity ^0.4.18;
contract GeneScience {
uint32 public constant ATTRIBUTE_NUM = 21;
uint public _startTime;
mapping (uint32 => uint8[]) _attribute;
mapping (uint32 => uint8[]) _attribute_mixrule;
mapping (uint32 => uint8) _variation_rate; // 千分之几
mapping (uint32 => uint16[]) _variation_rate_distribution; // 万分之几
function GeneScience() public {
_startTime = now;
}
function init_attribute() external {
_attribute[0] = [47]; // 是否EXCLUSIVE
_attribute[1] = [117]; // 是否FANCY
_attribute[2] = [107,101,12,8]; // FANCY属性
_attribute[3] = [58,10]; // EXCLUSIVE属性
_attribute[4] = [124,113,97,95,93,87,76,61,60,36,33,30,28,17]; // BODY
_attribute[5] = [127,119,84,66,57,27,26,22,4]; // TAIL
_attribute[6] = [128,125,120,100,99,68,56,46,6]; // PRIMARY COLOR
_attribute[7] = [118,114,88,80,79,41,40,2,1]; // SECONDARY COLOR
_attribute[8] = [129,126,86,65,52,49,44,42,34,21]; // PATTERN TYPE
_attribute[9] = [123,122,111,102,90,64,54,53,38]; // PATTERN COLOR
_attribute[10] = [89,85,81,72,67,50,32,23,19]; // EYE TYPE
_attribute[11] = [116,110,103,77,35,29,24,15,9,3]; // EYE COLOR / BACKGROUND COLOR
_attribute[12] = [98,74,73,71,70,59,48,45,39]; // MOUTH
_attribute[13] = [109,96,91,75,31,25,20,16,0]; // BEARD
_attribute[14] = [108,105,82,63,62,55,51,43,13,5]; // ACCESSORIES
_attribute[15] = [115,112,106,94,69]; // 生育能力
_attribute[16] = [18,7]; // 性格
_attribute[17] = [92,78]; // 速度
_attribute[18] = [104,37]; // 智商
_attribute[19] = [83,14]; // 体力
_attribute[20] = [121,11]; // 技能
}
function init_mixrule() external {
_attribute_mixrule[0] = [3];
_attribute_mixrule[1] = [6];
_attribute_mixrule[2] = [1,2];
_attribute_mixrule[3] = [1,2];
_attribute_mixrule[4] = [1,2];
_attribute_mixrule[5] = [5];
_attribute_mixrule[6] = [1,2];
_attribute_mixrule[7] = [1,2];
_attribute_mixrule[8] = [1,2];
_attribute_mixrule[9] = [1,2];
_attribute_mixrule[10] = [1,2];
_attribute_mixrule[11] = [1,2];
_attribute_mixrule[12] = [1,2];
_attribute_mixrule[13] = [5];
_attribute_mixrule[14] = [6];
_attribute_mixrule[15] = [1,2];
_attribute_mixrule[16] = [1,2];
_attribute_mixrule[17] = [1,2];
_attribute_mixrule[18] = [1,2];
_attribute_mixrule[19] = [1,2];
_attribute_mixrule[20] = [1,2];
}
function init_rate() external {
_variation_rate[0] = 0;
_variation_rate[1] = 1;
_variation_rate[2] = 100;
_variation_rate[3] = 0;
_variation_rate[4] = 10;
_variation_rate[5] = 100;
_variation_rate[6] = 10;
_variation_rate[7] = 100;
_variation_rate[8] = 100;
_variation_rate[9] = 100;
_variation_rate[10] = 100;
_variation_rate[11] = 100;
_variation_rate[12] = 100;
_variation_rate[13] = 100;
_variation_rate[14] = 200;
_variation_rate[15] = 100;
_variation_rate[16] = 100;
_variation_rate[17] = 100;
_variation_rate[18] = 100;
_variation_rate[19] = 100;
_variation_rate[20] = 100;
}
function init_rate_distribution() external {
_variation_rate_distribution[0] = [10000,0]; // 是否EXCLUSIVE
_variation_rate_distribution[1] = [10000,0]; // 是否FANCY
_variation_rate_distribution[2] = [2500,2500,2500,2500]; // FANCY属性
_variation_rate_distribution[3] = [3000,4000,3000]; // EXCLUSIVE属性
_variation_rate_distribution[4] = [10,50,140,400,800,1000,1600,2000,1600,1000,800,400,140,50,10]; // BODY
_variation_rate_distribution[5] = [20,150,830,1600,2400,2400,1600,830,150,20]; // TAIL
_variation_rate_distribution[6] = [20,150,830,1600,2400,2400,1600,830,150,20]; // PRIMARY COLOR
_variation_rate_distribution[7] = [20,150,830,1600,2400,2400,1600,830,150,20]; // SECONDARY COLOR
_variation_rate_distribution[8] = [14,105,581,1120,1680,3000,1680,1120,581,105,14]; // PATTERN TYPE
_variation_rate_distribution[9] = [20,150,830,1600,2400,2400,1600,830,150,20]; // PATTERN COLOR
_variation_rate_distribution[10] = [20,150,830,1600,2400,2400,1600,830,150,20]; // EYE TYPE
_variation_rate_distribution[11] = [14,105,581,1120,1680,3000,1680,1120,581,105,14]; // EYE COLOR / BACKGROUND COLOR
_variation_rate_distribution[12] = [20,150,830,1600,2400,2400,1600,830,150,20]; // MOUTH
_variation_rate_distribution[13] = [20,150,830,1600,2400,2400,1600,830,150,20]; // BEARD
_variation_rate_distribution[14] = [900,900,900,900,900,900,900,900,900,900,1000]; // ACCESSORIES
_variation_rate_distribution[15] = [1000,1000,3000,3000,1000,1000]; // 生育能力
_variation_rate_distribution[16] = [3000,4000,3000]; // 性格
_variation_rate_distribution[17] = [3000,4000,3000]; // 速度
_variation_rate_distribution[18] = [3000,4000,3000]; // 智商
_variation_rate_distribution[19] = [3000,4000,3000]; // 体力
_variation_rate_distribution[20] = [3000,4000,3000]; // 技能
}
function random() internal view returns (uint256) {
return uint256(keccak256(block.difficulty, now, block.blockhash(block.number - 1), _startTime));
}
// index begin with 0
function getBitMask(uint8[] index) internal pure returns (bytes32) {
bytes32 r = 0x0;
for(uint32 i=0; i<index.length; i++) {
bytes32 t = bytes32(0x1) << index[i];
r = r | t;
}
return r;
}
function mixGenes(uint256 genes1, uint256 genes2) external view returns (uint256) {
bytes32 a = bytes32(genes1);
bytes32 b = bytes32(genes2);
bytes32 r = 0x0;
for(uint32 i=0; i<ATTRIBUTE_NUM; i++) {
bytes32 mask = getBitMask(_attribute[i]);
bytes32 ma = mask & a;
bytes32 mb = mask & b;
bytes32 mixed;
if(_attribute_mixrule[i].length == 2) {
uint256 rule = random() % 2 + 1;
if(rule == 1) {
mixed = ma;
} else {
mixed = mb;
}
} else if(_attribute_mixrule[i][0] == 3) {
// all is 0, do nothing
} else if(_attribute_mixrule[i][0] == 5) {
mixed = ma | mb;
} else if(_attribute_mixrule[i][0] == 6) {
mixed = ma & mb;
}
mixed = variation(i, mixed);
r = r | mixed;
}
return uint256(r);
}
function getCoolDown(uint256 genes) external view returns (uint16) {
bytes32 a = bytes32(genes);
uint8[] storage pos = _attribute[15];
uint16 firstPos = 5;
for(uint16 i=0; i<pos.length; i++) {
if((bytes32(0x1) << pos[i]) & a > 0) {
firstPos = i;
break;
}
}
return firstPos;
}
function fiveDValue(uint256 genes, bool gen0, uint8 attID) external view returns (uint8) {
bytes32 a = bytes32(genes);
uint8[] storage pos = _attribute[attID];
uint8 attV = 0;
if((bytes32(0x1) << pos[0]) & a > 0) {
attV = 2;
} else if((bytes32(0x1) << pos[1]) & a > 0) {
attV = 1;
} else {
attV = 0;
}
if(gen0 == false) {
if(attV == 0) {
return uint8(random() % 4) + 1;
}
if(attV == 1) {
return uint8(random() % 5) + 5;
}
return uint8(random() % 10) + 10;
}
else {
if(attV == 0) {
return uint8(random() % 5) + 5;
}
if(attV == 1) {
return uint8(random() % 10) + 10;
}
return uint8(random() % 10) + 20;
}
}
function variation(uint32 attID, bytes32 genes) internal view returns (bytes32) {
uint32 rate = _variation_rate[attID];
if((random() % 1000) >= rate)
return genes;
uint8[] storage pos = _attribute[attID];
uint16 firstPos = uint16(pos.length);
for(uint16 i=0; i<pos.length; i++) {
if((bytes32(0x1) << pos[i]) & genes > 0) {
firstPos = i;
break;
}
}
uint16[] storage rate_distribution = _variation_rate_distribution[attID];
uint16 rd = uint16(random() % 10000);
uint16 total = 10000 - rate_distribution[firstPos];
uint16 begin = 0;
uint16 variation_attr = firstPos;
for(i=0; i<rate_distribution.length; i++) {
if(i == firstPos)
continue;
uint16 end = uint16((rate_distribution[i] * 10000) / total + begin);
if(rd >= begin && rd < end) {
variation_attr = i;
break;
}
begin = end;
}
if(variation_attr == pos.length)
return bytes32(0x0);
return bytes32(0x1) << pos[variation_attr];
}
}