Advertisement
Guest User

Untitled

a guest
Mar 18th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.60 KB | None | 0 0
  1. pragma solidity ^0.4.0;
  2. contract GeneMagic {
  3.  
  4. uint8 constant BEAST_TYPE_LENGTH = 2;
  5. uint8 constant GENE_BASE_LENGTH = 254;
  6.  
  7. struct attributeDefinition {
  8. string name;
  9. uint[32] probabilities;
  10. }
  11.  
  12. attributeDefinition[] private dinosaurs_attribute_list;
  13. attributeDefinition[] private unicorns_attribute_list;
  14.  
  15. attributeDefinition[] private dino_attr_list;
  16.  
  17. function GeneMagic() public {
  18. dinosaurs_attribute_list.push(attributeDefinition("pedigree", [uint(100000), 1000, 100, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
  19. dinosaurs_attribute_list.push(attributeDefinition("ability", [uint(1), 10, 100, 1000, 323, 1, 10, 100, 1000, 323, 1, 10, 100, 1000, 323, 1, 10, 100, 1000, 323, 413, 124, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
  20. dinosaurs_attribute_list.push(attributeDefinition("element", [uint(1), 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
  21. dinosaurs_attribute_list.push(attributeDefinition("type", [uint(100), 100, 100, 10, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
  22. dinosaurs_attribute_list.push(attributeDefinition("eyes", [uint(100), 100, 50, 10, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
  23. dinosaurs_attribute_list.push(attributeDefinition("nose", [uint(100), 100, 50, 10, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
  24. dinosaurs_attribute_list.push(attributeDefinition("mouth", [uint(100), 100, 50, 10, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
  25. dinosaurs_attribute_list.push(attributeDefinition("tail", [uint(100), 100, 50, 10, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
  26. dinosaurs_attribute_list.push(attributeDefinition("plume", [uint(100), 100, 50, 10, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
  27. dinosaurs_attribute_list.push(attributeDefinition("legs", [uint(100), 100, 50, 10, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
  28. dinosaurs_attribute_list.push(attributeDefinition("spikes", [uint(100), 100, 50, 10, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
  29. dinosaurs_attribute_list.push(attributeDefinition("wings", [uint(100), 100, 50, 10, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
  30. }
  31.  
  32. function randomNumber(uint a, uint b) public constant returns (int) {
  33. return int(uint((block.timestamp - a)%b));
  34. }
  35.  
  36. function getBits(uint256 src, uint start, uint length) public pure returns (uint256){
  37. uint256 shifted = src / 2**start;
  38. return shifted & uint256(2 ** length - 1);
  39. }
  40.  
  41. function getLastBits(uint256 src, uint n) public pure returns (uint256) {
  42. return src / 2 ** n;
  43. }
  44.  
  45. function getBeastType(uint256 genes) public pure returns (uint8) {
  46. return uint8(getBits(genes, 0, BEAST_TYPE_LENGTH));
  47. }
  48.  
  49. function getGeneBase(uint256 genes) public pure returns (uint256) {
  50. return getLastBits(genes, BEAST_TYPE_LENGTH);
  51. }
  52.  
  53. function mixGene(uint16 geneMother, uint16 geneFather, uint[32] attributeProbabilities) private view returns (uint16){
  54. uint[32] memory actualProbabilities = attributeProbabilities;
  55. uint arraySum = 0;
  56. for (uint i = 0; i < attributeProbabilities.length; i++) {
  57. if (geneMother == i || geneFather == i) {
  58. actualProbabilities[i] = 2*attributeProbabilities[i];
  59. } else {
  60. actualProbabilities[i] = attributeProbabilities[i];
  61. }
  62. arraySum += actualProbabilities[i];
  63. }
  64. int256 dice = randomNumber(1, arraySum);
  65. for (uint j = 0; j < actualProbabilities.length; j++) {
  66. dice -= int256(actualProbabilities[j]);
  67. if (dice <= 0) {
  68. return uint16(j);
  69. }
  70. }
  71. return uint16(actualProbabilities.length - 1);
  72. }
  73.  
  74. function newGene(uint[32] attributeProbabilities) private view returns (uint16){
  75. uint[32] memory actualProbabilities = attributeProbabilities;
  76. uint arraySum = 0;
  77. for (uint i = 0; i < attributeProbabilities.length; i++) {
  78. arraySum += actualProbabilities[i];
  79. }
  80. int256 dice = randomNumber(1, arraySum);
  81. for (uint j = 0; j < actualProbabilities.length; j++) {
  82. dice -= int256(actualProbabilities[j]);
  83. if (dice <= 0) {
  84. return uint16(j);
  85. }
  86. }
  87. return uint16(actualProbabilities.length - 1);
  88. }
  89.  
  90. function mixGenes(uint256 genesMother, uint256 genesFather) public constant returns(uint256) {
  91. uint8 beastTypeMother = getBeastType(genesMother);
  92. if (beastTypeMother != getBeastType(genesFather)) {
  93. revert();
  94. }
  95. attributeDefinition[] memory attributeList;
  96. if (beastTypeMother == uint8(0)) {
  97. attributeList = dinosaurs_attribute_list;
  98. } else if (beastTypeMother == uint8(0)) {
  99. attributeList = unicorns_attribute_list;
  100. }
  101. uint256 childGeneBase = 0;
  102. uint256 fatherGeneBase = getGeneBase(genesFather);
  103. uint256 motherGeneBase = getGeneBase(genesMother);
  104. uint startingPosition = 0;
  105. for (uint i = 0; i < attributeList.length; i++) {
  106. attributeDefinition memory attr = attributeList[i];
  107. uint16 geneSegmentMother = uint16(getBits(motherGeneBase, startingPosition, 5));
  108. uint16 geneSegmentFather = uint16(getBits(fatherGeneBase, startingPosition, 5));
  109. uint16 newGenes = mixGene(geneSegmentMother, geneSegmentFather, attr.probabilities);
  110. childGeneBase = childGeneBase + newGenes * 2 ** (startingPosition);
  111. startingPosition += 5;
  112. }
  113. childGeneBase = childGeneBase * 2 ** 2 + beastTypeMother;
  114. return childGeneBase;
  115. }
  116.  
  117. function createGenes(uint8 beastType) public constant returns(uint256) {
  118. attributeDefinition[] memory attributeList;
  119. if (beastType == uint8(0)) {
  120. attributeList = dinosaurs_attribute_list;
  121. } else if (beastType == uint8(0)) {
  122. attributeList = unicorns_attribute_list;
  123. }
  124. uint256 childGeneBase = 0;
  125. uint startingPosition = 0;
  126. for (uint i = 0; i < attributeList.length; i++) {
  127. attributeDefinition memory attr = attributeList[i];
  128. uint16 newGenes = newGene(attr.probabilities);
  129. childGeneBase = childGeneBase + newGenes * 2 ** (startingPosition);
  130. startingPosition += 5;
  131. }
  132. childGeneBase = childGeneBase * 2 ** 2 + beastType;
  133. return childGeneBase;
  134. }
  135.  
  136. function numberOfAttributes(uint8 target) public constant returns (uint) {
  137. if (target == 0) {
  138. return dinosaurs_attribute_list.length;
  139. } else if (target == 1){
  140. return unicorns_attribute_list.length;
  141. } else {
  142. revert();
  143. }
  144. }
  145.  
  146. function getAttributeName(uint target, uint i) public constant returns (string) {
  147. if (target == 0) {
  148. return dinosaurs_attribute_list[i].name;
  149. } else if (target == 1) {
  150. return unicorns_attribute_list[i].name;
  151. } else {
  152. revert();
  153. }
  154. }
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement