1241 // Pick a random piece 3 out of 4 times; otherwise, pick either |
1241 // Pick a random piece 3 out of 4 times; otherwise, pick either |
1242 // one of the most common or the least common pieces available, |
1242 // one of the most common or the least common pieces available, |
1243 // depending on the state we're in. |
1243 // depending on the state we're in. |
1244 int pieceIndex = 0; |
1244 int pieceIndex = 0; |
1245 if (d->state == WarmingUp || (qrand() & 4) == 0) { |
1245 if (d->state == WarmingUp || (qrand() & 4) == 0) { |
1246 int *occurrances = new int[d->pieceCount]; |
1246 int *occurrences = new int[d->pieceCount]; |
1247 memset(occurrances, 0, d->pieceCount * sizeof(int)); |
1247 memset(occurrences, 0, d->pieceCount * sizeof(int)); |
1248 |
1248 |
1249 // Count how many of each piece are available. |
1249 // Count how many of each piece are available. |
1250 foreach (PeerWireClient *peer, d->connections) { |
1250 foreach (PeerWireClient *peer, d->connections) { |
1251 QBitArray peerPieces = peer->availablePieces(); |
1251 QBitArray peerPieces = peer->availablePieces(); |
1252 int peerPiecesSize = peerPieces.size(); |
1252 int peerPiecesSize = peerPieces.size(); |
1253 for (int i = 0; i < peerPiecesSize; ++i) { |
1253 for (int i = 0; i < peerPiecesSize; ++i) { |
1254 if (peerPieces.testBit(i)) |
1254 if (peerPieces.testBit(i)) |
1255 ++occurrances[i]; |
1255 ++occurrences[i]; |
1256 } |
1256 } |
1257 } |
1257 } |
1258 |
1258 |
1259 // Find the rarest or most common pieces. |
1259 // Find the rarest or most common pieces. |
1260 int numOccurrances = d->state == WarmingUp ? 0 : 99999; |
1260 int numOccurrences = d->state == WarmingUp ? 0 : 99999; |
1261 QList<int> piecesReadyForDownload; |
1261 QList<int> piecesReadyForDownload; |
1262 for (int i = 0; i < d->pieceCount; ++i) { |
1262 for (int i = 0; i < d->pieceCount; ++i) { |
1263 if (d->state == WarmingUp) { |
1263 if (d->state == WarmingUp) { |
1264 // Add common pieces |
1264 // Add common pieces |
1265 if (occurrances[i] >= numOccurrances |
1265 if (occurrences[i] >= numOccurrences |
1266 && incompletePiecesAvailableToClient.testBit(i)) { |
1266 && incompletePiecesAvailableToClient.testBit(i)) { |
1267 if (occurrances[i] > numOccurrances) |
1267 if (occurrences[i] > numOccurrences) |
1268 piecesReadyForDownload.clear(); |
1268 piecesReadyForDownload.clear(); |
1269 piecesReadyForDownload.append(i); |
1269 piecesReadyForDownload.append(i); |
1270 numOccurrances = occurrances[i]; |
1270 numOccurrences = occurrences[i]; |
1271 } |
1271 } |
1272 } else { |
1272 } else { |
1273 // Add rare pieces |
1273 // Add rare pieces |
1274 if (occurrances[i] <= numOccurrances |
1274 if (occurrences[i] <= numOccurrences |
1275 && incompletePiecesAvailableToClient.testBit(i)) { |
1275 && incompletePiecesAvailableToClient.testBit(i)) { |
1276 if (occurrances[i] < numOccurrances) |
1276 if (occurrences[i] < numOccurrences) |
1277 piecesReadyForDownload.clear(); |
1277 piecesReadyForDownload.clear(); |
1278 piecesReadyForDownload.append(i); |
1278 piecesReadyForDownload.append(i); |
1279 numOccurrances = occurrances[i]; |
1279 numOccurrences = occurrences[i]; |
1280 } |
1280 } |
1281 } |
1281 } |
1282 } |
1282 } |
1283 |
1283 |
1284 // Select one piece randomly |
1284 // Select one piece randomly |
1285 pieceIndex = piecesReadyForDownload.at(qrand() % piecesReadyForDownload.size()); |
1285 pieceIndex = piecesReadyForDownload.at(qrand() % piecesReadyForDownload.size()); |
1286 delete [] occurrances; |
1286 delete [] occurrences; |
1287 } else { |
1287 } else { |
1288 // Make up a list of available piece indices, and pick |
1288 // Make up a list of available piece indices, and pick |
1289 // a random one. |
1289 // a random one. |
1290 QList<int> values; |
1290 QList<int> values; |
1291 int incompletePiecesAvailableToClientSize = incompletePiecesAvailableToClient.size(); |
1291 int incompletePiecesAvailableToClientSize = incompletePiecesAvailableToClient.size(); |