-
Notifications
You must be signed in to change notification settings - Fork 0
/
contract.sol
188 lines (142 loc) · 6.68 KB
/
contract.sol
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
// // SPDX-License-Identifier: MIT
pragma solidity ^0.8.8;
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
contract ReplBots is ERC721Enumerable {
using Strings for uint8;
uint256 public tokenCounter = 1; // no more token 0
string[] private headgear = [
"Cowboy Hat",
"Fro",
"Baseball Cap",
"Viking Helmet"
];
string[] private eargear = [
"Bunny Ears",
"Headphones"
];
string[] private facegear = [
"Sunglasses",
"Moustache",
"Nose",
"DOOM Mask"
];
struct Color {
uint8 red;
uint8 green;
uint8 blue;
}
struct ReplBot {
Color frame;
Color visor;
Color background;
uint8 head;
uint8 ears;
uint8 face;
uint256 generation; // new field
uint256 parentOneId; // new field
uint256 parentTwoId; // new field
}
mapping (uint => ReplBot) private replbots;
constructor() ERC721("ReplBots", "RBNFT") {
}
function mint(address recipient) public returns (uint256) {
// Get ID and increment counter
uint tokenId = tokenCounter;
tokenCounter++;
// Determine colors
Color memory frameCol = Color(
uint8(_random(tokenId, "QWERT") % 255),
uint8(_random(tokenId, "YUIOP") % 255),
uint8(_random(tokenId, "ASDFG") % 255));
Color memory visorCol = Color(
uint8(_random(tokenId, "HJKL;") % 255),
uint8(_random(tokenId, "ZXCVB") % 255),
uint8(_random(tokenId, "BNM,.") % 255));
Color memory backgroundCol = Color(
uint8(_random(tokenId, "12345") % 255),
uint8(_random(tokenId, "67890") % 255),
uint8(_random(tokenId, "[]{}'") % 255));
// Determine accessories
uint8 headIdx = uint8(_random(tokenId, "qwert") % headgear.length);
uint8 earIdx = uint8(_random(tokenId, "yuiop") % eargear.length);
uint8 faceIdx = uint8(_random(tokenId, "asdfg") % facegear.length);
// Create bot
replbots[tokenId] = ReplBot(frameCol, visorCol, backgroundCol, headIdx, earIdx, faceIdx, 0, 0, 0); // <-- ZEROS ADDED
// Mint token
_safeMint(recipient, tokenId);
emit ReplBotCreated(recipient, tokenId); // <-- NEW
return tokenId;
}
function breed(uint256 parentOneId, uint256 parentTwoId, address recipient) public returns (uint256) {
// Require two parents
require(parentOneId != parentTwoId, "ReplBots: Parents must be separate bots");
// Check ownership
require(ownerOf(parentOneId) == msg.sender, "ReplBots: You don't own parent 1");
require(ownerOf(parentTwoId) == msg.sender, "ReplBots: You don't own parent 2");
ReplBot storage parentOne = replbots[parentOneId];
ReplBot storage parentTwo = replbots[parentTwoId];
// Check age
require(parentOne.generation == parentTwo.generation, "ReplBots: Parents must belong to the same generation");
// Increment token counter
uint tokenId = tokenCounter;
tokenCounter++;
// Interpolate colors
Color memory frameCol = Color(_meanOfTwo(parentOne.frame.red, parentTwo.frame.red),
_meanOfTwo(parentOne.frame.green, parentTwo.frame.green),
_meanOfTwo(parentOne.frame.blue, parentTwo.frame.blue));
Color memory visorCol = Color(_meanOfTwo(parentOne.visor.red, parentTwo.visor.red),
_meanOfTwo(parentOne.visor.green, parentTwo.visor.green),
_meanOfTwo(parentOne.visor.blue, parentTwo.visor.blue));
Color memory backgroundCol = Color(_meanOfTwo(parentOne.background.red, parentTwo.background.red),
_meanOfTwo(parentOne.background.green, parentTwo.background.green),
_meanOfTwo(parentOne.background.blue, parentTwo.background.blue));
// Choose accessories
uint8 headIdx = parentOne.head;
uint8 earIdx = parentTwo.ears;
uint8 faceIdx = uint8(_random(tokenId, "asdfg") % facegear.length);
// Create bot
replbots[tokenId] = ReplBot(frameCol, visorCol, backgroundCol, headIdx, earIdx, faceIdx, parentOne.generation + 1, parentOneId, parentTwoId);
// Mint token
_safeMint(recipient, tokenId);
emit ReplBotBorn(recipient, tokenId, parentOneId, parentTwoId, parentOne.generation + 1); // <-- NEW
return tokenId;
}
function botAccessories(uint256 tokenId) public view returns (string memory, string memory, string memory) {
require(_exists(tokenId), "ReplBots: Query for nonexistent token");
ReplBot memory bot = replbots[tokenId];
return (headgear[bot.head], eargear[bot.ears], facegear[bot.face]);
}
function botColors(uint256 tokenId) public view returns (string memory, string memory, string memory) {
require(_exists(tokenId), "ReplBots: Query for nonexistent token");
ReplBot memory bot = replbots[tokenId];
return (_colorToString(bot.frame),
_colorToString(bot.visor),
_colorToString(bot.background));
}
function botParentage(uint256 tokenId) public view returns (uint, uint, uint) {
require(_exists(tokenId), "ReplBots: Query for nonexistent token");
ReplBot memory bot = replbots[tokenId];
return (bot.generation, bot.parentOneId, bot.parentTwoId);
}
function _colorToString(Color memory color) internal pure returns (string memory) {
string[7] memory parts;
parts = ["(",
color.red.toString(),
",",
color.blue.toString(),
",",
color.green.toString(),
")"];
return string(abi.encodePacked(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5], parts[6]));
}
function _random(uint tokenId, string memory input) internal view returns (uint256) {
bytes32 blckhash = blockhash(block.number - 1);
return uint256(keccak256(abi.encodePacked(block.difficulty, blckhash, tokenId, abi.encodePacked(input))));
}
function _meanOfTwo(uint8 first, uint8 second) internal pure returns (uint8) {
return uint8((uint16(first) + uint16(second))/2);
}
event ReplBotCreated(address recipient, uint tokenId);
event ReplBotBorn(address recipient, uint tokenId, uint parentOneId, uint parentTwoId, uint generation);
}