| 10 | } |
| 11 | |
| 12 | void FallingBlocksAgent::update() { |
| 13 | HashSet<Vec2I> processing = take(m_pending); |
| 14 | |
| 15 | while (!processing.empty()) { |
| 16 | List<Vec2I> positions; |
| 17 | for (auto const& pos : take(processing)) |
| 18 | positions.append(pos); |
| 19 | |
| 20 | m_random.shuffle(positions); |
| 21 | |
| 22 | positions.sort([](auto const& a, auto const& b) { |
| 23 | return a[1] < b[1]; |
| 24 | }); |
| 25 | |
| 26 | for (auto const& pos : positions) { |
| 27 | Vec2I belowPos = pos + Vec2I(0, -1); |
| 28 | Vec2I belowLeftPos = pos + Vec2I(-1, -1); |
| 29 | Vec2I belowRightPos = pos + Vec2I(1, -1); |
| 30 | |
| 31 | FallingBlockType thisBlock = m_facade->blockType(pos); |
| 32 | FallingBlockType belowBlock = m_facade->blockType(belowPos); |
| 33 | |
| 34 | Maybe<Vec2I> moveTo; |
| 35 | |
| 36 | if (thisBlock == FallingBlockType::Falling) { |
| 37 | if (belowBlock == FallingBlockType::Open) |
| 38 | moveTo = belowPos; |
| 39 | } else if (thisBlock == FallingBlockType::Cascading) { |
| 40 | if (belowBlock == FallingBlockType::Open) { |
| 41 | moveTo = belowPos; |
| 42 | } else { |
| 43 | FallingBlockType belowLeftBlock = m_facade->blockType(belowLeftPos); |
| 44 | FallingBlockType belowRightBlock = m_facade->blockType(belowRightPos); |
| 45 | |
| 46 | if (belowLeftBlock == FallingBlockType::Open && belowRightBlock == FallingBlockType::Open) |
| 47 | moveTo = m_random.randb() ? belowLeftPos : belowRightPos; |
| 48 | else if (belowLeftBlock == FallingBlockType::Open) |
| 49 | moveTo = belowLeftPos; |
| 50 | else if (belowRightBlock == FallingBlockType::Open) |
| 51 | moveTo = belowRightPos; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | if (moveTo) { |
| 56 | m_facade->moveBlock(pos, *moveTo); |
| 57 | if (m_random.randf() < m_immediateUpwardPropagateProbability) { |
| 58 | processing.add(pos + Vec2I(0, 1)); |
| 59 | processing.add(pos + Vec2I(-1, 1)); |
| 60 | processing.add(pos + Vec2I(1, 1)); |
| 61 | } |
| 62 | |
| 63 | visitLocation(pos); |
| 64 | visitLocation(*moveTo); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |