| 48 | } |
| 49 | |
| 50 | void Simulation::Load(const GameSave *save, bool includePressure, Vec2<int> blockP) // block coordinates |
| 51 | { |
| 52 | auto partP = blockP * CELL; |
| 53 | |
| 54 | auto &sd = SimulationData::CRef(); |
| 55 | auto &elements = sd.elements; |
| 56 | |
| 57 | RecalcFreeParticles(false); |
| 58 | |
| 59 | struct ExistingParticle |
| 60 | { |
| 61 | int id; |
| 62 | Vec2<int> pos; |
| 63 | }; |
| 64 | std::vector<ExistingParticle> existingParticles; |
| 65 | auto pasteArea = RES.OriginRect() & RectSized(partP, save->blockSize * CELL); |
| 66 | for (int i = 0; i < parts.active; i++) |
| 67 | { |
| 68 | if (parts[i].type) |
| 69 | { |
| 70 | auto p = Vec2<int>{ int(parts[i].x + 0.5f), int(parts[i].y + 0.5f) }; |
| 71 | if (pasteArea.Contains(p)) |
| 72 | { |
| 73 | existingParticles.push_back({ i, p }); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | std::sort(existingParticles.begin(), existingParticles.end(), [](const auto &lhs, const auto &rhs) { |
| 78 | return std::tie(lhs.pos.Y, lhs.pos.X) < std::tie(rhs.pos.Y, rhs.pos.X); |
| 79 | }); |
| 80 | PlaneAdapter<std::vector<size_t>> existingParticleIndices(pasteArea.size, existingParticles.size()); |
| 81 | { |
| 82 | auto lastPos = Vec2<int>{ -1, -1 }; // not a valid pos in existingParticles |
| 83 | for (auto it = existingParticles.begin(); it != existingParticles.end(); ++it) |
| 84 | { |
| 85 | if (lastPos != it->pos) |
| 86 | { |
| 87 | existingParticleIndices[it->pos - pasteArea.pos] = it - existingParticles.begin(); |
| 88 | lastPos = it->pos; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | auto removeExistingParticles = [this, pasteArea, &existingParticles, &existingParticleIndices](Vec2<int> p) { |
| 93 | auto rp = p - pasteArea.pos; |
| 94 | if (existingParticleIndices.Size().OriginRect().Contains(rp)) |
| 95 | { |
| 96 | auto index = existingParticleIndices[rp]; |
| 97 | for (auto it = existingParticles.begin() + index; it != existingParticles.end() && it->pos == p; ++it) |
| 98 | { |
| 99 | kill_part(it->id); |
| 100 | } |
| 101 | existingParticleIndices[rp] = existingParticles.size(); |
| 102 | } |
| 103 | }; |
| 104 | |
| 105 | auto oldPrettyPowders = pretty_powder; |
| 106 | pretty_powder = false; |
| 107 | Defer restorePrettyPowders([this, oldPrettyPowders]() { |
no test coverage detected