MCPcopy Create free account
hub / github.com/chrxh/alien / randomMultiply

Method randomMultiply

source/EngineInterface/DescriptionEditService.cpp:266–337  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

264}
265
266DataDescription DescriptionEditService::randomMultiply(
267 DataDescription const& input,
268 RandomMultiplyParameters const& parameters,
269 IntVector2D const& worldSize,
270 DataDescription&& existentData,
271 bool& overlappingCheckSuccessful)
272{
273 overlappingCheckSuccessful = true;
274 SpaceCalculator spaceCalculator(worldSize);
275 std::unordered_map<IntVector2D, std::vector<RealVector2D>> cellPosBySlot;
276
277 //create map for overlapping check
278 if (parameters._overlappingCheck) {
279 int index = 0;
280 for (auto const& cell : existentData.cells) {
281 auto intPos = toIntVector2D(spaceCalculator.getCorrectedPosition(cell.pos));
282 cellPosBySlot[intPos].emplace_back(cell.pos);
283 ++index;
284 }
285 }
286
287 //do multiplication
288 DataDescription result = input;
289 generateNewIds(result);
290 auto& numberGen = NumberGenerator::get();
291 for (int i = 0; i < parameters._number; ++i) {
292 bool overlapping = false;
293 DataDescription copy;
294 int attempts = 0;
295 do {
296 copy = input;
297 removeMetadata(copy);
298 copy.shift({toFloat(numberGen.getRandomReal(0, toInt(worldSize.x))), toFloat(numberGen.getRandomReal(0, toInt(worldSize.y)))});
299 copy.rotate(toInt(numberGen.getRandomReal(parameters._minAngle, parameters._maxAngle)));
300 copy.accelerate(
301 {toFloat(numberGen.getRandomReal(parameters._minVelX, parameters._maxVelX)),
302 toFloat(numberGen.getRandomReal(parameters._minVelY, parameters._maxVelY))},
303 toFloat(numberGen.getRandomReal(parameters._minAngularVel, parameters._maxAngularVel)));
304
305 //overlapping check
306 overlapping = false;
307 if (parameters._overlappingCheck) {
308 for (auto const& cell : copy.cells) {
309 auto pos = spaceCalculator.getCorrectedPosition(cell.pos);
310 if (isCellPresent(cellPosBySlot, spaceCalculator, pos, 2.0f)) {
311 overlapping = true;
312 }
313 }
314 }
315 ++attempts;
316 } while (overlapping && attempts < 200 && overlappingCheckSuccessful);
317 if (attempts == 200) {
318 overlappingCheckSuccessful = false;
319 }
320
321 generateNewIds(copy);
322 generateNewCreatureIds(copy);
323 result.add(copy);

Callers 1

onBuildMethod · 0.80

Calls 10

toIntVector2DFunction · 0.85
generateNewIdsFunction · 0.85
toFloatFunction · 0.85
toIntFunction · 0.85
getCorrectedPositionMethod · 0.80
shiftMethod · 0.80
getRandomRealMethod · 0.80
rotateMethod · 0.80
accelerateMethod · 0.80
sizeMethod · 0.80

Tested by

no test coverage detected