Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pragma solidity ^0.4.0;
- contract GeneMagic {
- uint8 constant BEAST_TYPE_LENGTH = 2;
- uint8 constant GENE_BASE_LENGTH = 254;
- struct attributeDefinition {
- string name;
- uint[32] probabilities;
- }
- attributeDefinition[] private dinosaurs_attribute_list;
- attributeDefinition[] private unicorns_attribute_list;
- attributeDefinition[] private dino_attr_list;
- function GeneMagic() public {
- 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]));
- 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]));
- 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]));
- 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]));
- 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]));
- 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]));
- 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]));
- 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]));
- 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]));
- 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]));
- 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]));
- 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]));
- }
- function randomNumber(uint a, uint b) public constant returns (int) {
- return int(uint((block.timestamp - a)%b));
- }
- function getBits(uint256 src, uint start, uint length) public pure returns (uint256){
- uint256 shifted = src / 2**start;
- return shifted & uint256(2 ** length - 1);
- }
- function getLastBits(uint256 src, uint n) public pure returns (uint256) {
- return src / 2 ** n;
- }
- function getBeastType(uint256 genes) public pure returns (uint8) {
- return uint8(getBits(genes, 0, BEAST_TYPE_LENGTH));
- }
- function getGeneBase(uint256 genes) public pure returns (uint256) {
- return getLastBits(genes, BEAST_TYPE_LENGTH);
- }
- function mixGene(uint16 geneMother, uint16 geneFather, uint[32] attributeProbabilities) private view returns (uint16){
- uint[32] memory actualProbabilities = attributeProbabilities;
- uint arraySum = 0;
- for (uint i = 0; i < attributeProbabilities.length; i++) {
- if (geneMother == i || geneFather == i) {
- actualProbabilities[i] = 2*attributeProbabilities[i];
- } else {
- actualProbabilities[i] = attributeProbabilities[i];
- }
- arraySum += actualProbabilities[i];
- }
- int256 dice = randomNumber(1, arraySum);
- for (uint j = 0; j < actualProbabilities.length; j++) {
- dice -= int256(actualProbabilities[j]);
- if (dice <= 0) {
- return uint16(j);
- }
- }
- return uint16(actualProbabilities.length - 1);
- }
- function newGene(uint[32] attributeProbabilities) private view returns (uint16){
- uint[32] memory actualProbabilities = attributeProbabilities;
- uint arraySum = 0;
- for (uint i = 0; i < attributeProbabilities.length; i++) {
- arraySum += actualProbabilities[i];
- }
- int256 dice = randomNumber(1, arraySum);
- for (uint j = 0; j < actualProbabilities.length; j++) {
- dice -= int256(actualProbabilities[j]);
- if (dice <= 0) {
- return uint16(j);
- }
- }
- return uint16(actualProbabilities.length - 1);
- }
- function mixGenes(uint256 genesMother, uint256 genesFather) public constant returns(uint256) {
- uint8 beastTypeMother = getBeastType(genesMother);
- if (beastTypeMother != getBeastType(genesFather)) {
- revert();
- }
- attributeDefinition[] memory attributeList;
- if (beastTypeMother == uint8(0)) {
- attributeList = dinosaurs_attribute_list;
- } else if (beastTypeMother == uint8(0)) {
- attributeList = unicorns_attribute_list;
- }
- uint256 childGeneBase = 0;
- uint256 fatherGeneBase = getGeneBase(genesFather);
- uint256 motherGeneBase = getGeneBase(genesMother);
- uint startingPosition = 0;
- for (uint i = 0; i < attributeList.length; i++) {
- attributeDefinition memory attr = attributeList[i];
- uint16 geneSegmentMother = uint16(getBits(motherGeneBase, startingPosition, 5));
- uint16 geneSegmentFather = uint16(getBits(fatherGeneBase, startingPosition, 5));
- uint16 newGenes = mixGene(geneSegmentMother, geneSegmentFather, attr.probabilities);
- childGeneBase = childGeneBase + newGenes * 2 ** (startingPosition);
- startingPosition += 5;
- }
- childGeneBase = childGeneBase * 2 ** 2 + beastTypeMother;
- return childGeneBase;
- }
- function createGenes(uint8 beastType) public constant returns(uint256) {
- attributeDefinition[] memory attributeList;
- if (beastType == uint8(0)) {
- attributeList = dinosaurs_attribute_list;
- } else if (beastType == uint8(0)) {
- attributeList = unicorns_attribute_list;
- }
- uint256 childGeneBase = 0;
- uint startingPosition = 0;
- for (uint i = 0; i < attributeList.length; i++) {
- attributeDefinition memory attr = attributeList[i];
- uint16 newGenes = newGene(attr.probabilities);
- childGeneBase = childGeneBase + newGenes * 2 ** (startingPosition);
- startingPosition += 5;
- }
- childGeneBase = childGeneBase * 2 ** 2 + beastType;
- return childGeneBase;
- }
- function numberOfAttributes(uint8 target) public constant returns (uint) {
- if (target == 0) {
- return dinosaurs_attribute_list.length;
- } else if (target == 1){
- return unicorns_attribute_list.length;
- } else {
- revert();
- }
- }
- function getAttributeName(uint target, uint i) public constant returns (string) {
- if (target == 0) {
- return dinosaurs_attribute_list[i].name;
- } else if (target == 1) {
- return unicorns_attribute_list[i].name;
- } else {
- revert();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement