MCPcopy Create free account
hub / github.com/The-Powder-Toy/The-Powder-Toy / Load

Method Load

src/simulation/Simulation.cpp:50–340  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

48}
49
50void 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]() {

Callers 6

mainFunction · 0.80
PlaceSaveMethod · 0.80
SetSaveMethod · 0.80
SetSaveFileMethod · 0.80
RenderMethod · 0.80
loadStampFunction · 0.80

Calls 15

RectSizedFunction · 0.85
InBoundsFunction · 0.85
Element_FIGH_CanAllocFunction · 0.85
Element_STKM_init_legsFunction · 0.85
VersionClass · 0.85
Element_FIGH_AllocFunction · 0.85
Element_FIGH_NewFighterFunction · 0.85
OriginRectMethod · 0.80
TopLeftMethod · 0.80
ContainsMethod · 0.45
beginMethod · 0.45

Tested by

no test coverage detected