1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
use crate::block::TickAction;
use crate::block::{
    self, Block, BlockAttributes, Evoxel, Evoxels, MinEval, Modifier, Resolution::R16, AIR,
};
use crate::math::{Face6, GridAab, GridCoordinate, GridVector, Vol};
use crate::op::Operation;
use crate::universe;

/// Data for [`Modifier::Move`]; displaces the block out of the grid, cropping it.
/// A pair of `Move`s can depict a block moving between two cubes.
///
/// # Animation
///
/// * If the `distance` is zero then the modifier will remove itself, if possible,
///   on the next tick.
/// * If the `distance` and `velocity` are such that the block is out of view and will
///   never strt being in view, the block will be replaced with [`AIR`].
///
/// (TODO: Define the conditions for “if possible”.)
#[non_exhaustive] // TODO: needs a constructor instead
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct Move {
    /// The direction in which the block is displaced.
    pub direction: Face6,
    /// The distance, in 1/256ths, by which it is displaced.
    pub distance: u16,
    /// The velocity **per tick** with which the displacement is changing.
    ///
    /// TODO: "Per tick" is a bad unit.
    pub velocity: i16,
}

impl Move {
    /// TODO: make a cleaner, less internals-ish constructor
    pub fn new(direction: Face6, distance: u16, velocity: i16) -> Self {
        Self {
            direction,
            distance,
            velocity,
        }
    }

    /// Create a pair of [`Modifier::Move`]s to displace a block.
    /// The first goes on the block being moved and the second on the air
    /// it's moving into.
    ///
    /// TODO: This is going to need to change again in order to support
    /// moving one block in and another out at the same time.
    pub fn paired_move(direction: Face6, distance: u16, velocity: i16) -> [Modifier; 2] {
        [
            Modifier::Move(Move {
                direction,
                distance,
                velocity,
            }),
            Modifier::Move(Move {
                direction: direction.opposite(),
                distance: 256 - distance,
                velocity: -velocity,
            }),
        ]
    }

    /// Note that `Modifier::Move` does some preprocessing to keep this simpler.
    pub(super) fn evaluate(
        &self,
        block: &Block,
        this_modifier_index: usize,
        mut input: MinEval,
        filter: &block::EvalFilter,
    ) -> Result<MinEval, block::InEvalError> {
        let Move {
            direction,
            distance,
            velocity,
        } = *self;

        // Apply Quote to ensure that the block's own `tick_action` and other effects
        // don't interfere with movement or cause duplication.
        // (In the future we may want a more nuanced policy that allows internal changes,
        // but that will involve some sort of predicate and transformation on tick actions.)
        input = block::Quote::default().evaluate(input, filter)?;

        let (original_bounds, effective_resolution) = match input.voxels {
            Evoxels::Many(resolution, ref array) => (array.bounds(), resolution),
            // Treat color blocks as having a resolution of 16. TODO: Improve on this hardcoded constant
            Evoxels::One(_) => (GridAab::for_block(R16), R16),
        };

        // For now, our strategy is to work in units of the block's resolution.
        // TODO: Generalize to being able to increase resolution to a chosen minimum.
        let distance_in_res =
            GridCoordinate::from(distance) * GridCoordinate::from(effective_resolution) / 256;
        let translation_in_res = direction.normal_vector() * distance_in_res;

        // This will be None if the displacement puts the block entirely out of view.
        let displaced_bounds: Option<GridAab> = original_bounds
            .translate(translation_in_res)
            .intersection_cubes(GridAab::for_block(effective_resolution));

        let animation_action: Option<TickAction> = if displaced_bounds.is_none() && velocity >= 0 {
            // Displaced to invisibility; turn into just plain air.
            Some(TickAction::from(Operation::Become(AIR)))
        } else if translation_in_res == GridVector::zero() && velocity == 0
            || distance == 0 && velocity < 0
        {
            // Either a stationary displacement which is invisible, or an animated one which has finished its work.
            assert!(
                matches!(&block.modifiers()[this_modifier_index], Modifier::Move(m) if m == self)
            );
            let mut new_block = block.clone();
            new_block.modifiers_mut().remove(this_modifier_index); // TODO: What if other modifiers want to do things?
            Some(TickAction::from(Operation::Become(new_block)))
        } else if velocity != 0 {
            // Movement in progress.
            assert!(
                matches!(&block.modifiers()[this_modifier_index], Modifier::Move(m) if m == self)
            );
            let mut new_block = block.clone();
            if let Modifier::Move(Move {
                distance, velocity, ..
            }) = &mut new_block.modifiers_mut()[this_modifier_index]
            {
                *distance = i32::from(*distance)
                            .saturating_add(i32::from(*velocity))
                            .clamp(0, i32::from(u16::MAX))
                            .try_into()
                            .unwrap(/* clamped to range */);
            }
            Some(TickAction::from(Operation::Become(new_block)))
        } else {
            // Stationary displacement; take no action
            None
        };

        let animation_hint = if animation_action.is_some() {
            input.attributes.animation_hint
                | block::AnimationHint::replacement(block::AnimationChange::Shape)
        } else {
            input.attributes.animation_hint
        };

        let attributes = BlockAttributes {
            animation_hint,
            tick_action: animation_action,
            ..input.attributes
        };

        Ok(match displaced_bounds {
            Some(displaced_bounds) => {
                block::Budget::decrement_voxels(
                    &filter.budget,
                    displaced_bounds.volume().unwrap(),
                )?;

                let displaced_voxels = match &input.voxels {
                    Evoxels::Many(_, voxels) => Evoxels::Many(
                        effective_resolution,
                        Vol::from_fn(displaced_bounds, |cube| voxels[cube - translation_in_res]),
                    ),
                    &Evoxels::One(voxel) => {
                        // Input block is a solid color; synthesize voxels.
                        // TODO: Also synthesize if the resolution is merely low
                        // compared to the velocity.
                        Evoxels::Many(
                            effective_resolution,
                            Vol::from_fn(displaced_bounds, |_| voxel),
                        )
                    }
                };
                MinEval {
                    attributes,
                    voxels: displaced_voxels,
                }
            }
            None => MinEval {
                attributes,
                voxels: Evoxels::One(Evoxel::AIR),
            },
        })
    }
}

impl From<Move> for Modifier {
    fn from(value: Move) -> Self {
        Modifier::Move(value)
    }
}

impl universe::VisitHandles for Move {
    fn visit_handles(&self, _visitor: &mut dyn universe::HandleVisitor) {
        let Move {
            direction: _,
            distance: _,
            velocity: _,
        } = self;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::block::{Composite, EvaluatedBlock, Resolution::*};
    use crate::content::make_some_blocks;
    use crate::math::{notnan, rgba_const, FaceMap, GridPoint, OpacityCategory, Rgb, Rgba};
    use crate::space::Space;
    use crate::time;
    use crate::universe::Universe;
    use ordered_float::NotNan;
    use pretty_assertions::assert_eq;

    #[test]
    fn move_atom_block_evaluation() {
        let color = rgba_const!(1.0, 0.0, 0.0, 1.0);
        let original = Block::from(color);
        let moved = original.clone().with_modifier(Move {
            direction: Face6::PY,
            distance: 128, // distance 1/2 block × scale factor of 256
            velocity: 0,
        });

        let expected_bounds = GridAab::from_lower_size([0, 8, 0], [16, 8, 16]);

        let ev_original = original.evaluate().unwrap();
        assert_eq!(
            moved.evaluate().unwrap(),
            EvaluatedBlock {
                attributes: ev_original.attributes.clone(),
                color: color.to_rgb().with_alpha(NotNan::new(2. / 3.).unwrap()),
                face_colors: FaceMap {
                    nx: color.to_rgb().with_alpha(notnan!(0.5)),
                    ny: color.to_rgb().with_alpha(notnan!(1.0)),
                    nz: color.to_rgb().with_alpha(notnan!(0.5)),
                    px: color.to_rgb().with_alpha(notnan!(0.5)),
                    py: color.to_rgb().with_alpha(notnan!(1.0)),
                    pz: color.to_rgb().with_alpha(notnan!(0.5)),
                },
                light_emission: Rgb::ZERO,
                voxels: Evoxels::Many(
                    R16,
                    Vol::repeat(expected_bounds, Evoxel::from_block(&ev_original))
                ),
                opaque: FaceMap::repeat(false).with(Face6::PY, true),
                visible: true,
                uniform_collision: None,
                voxel_opacity_mask: Some(Vol::repeat(expected_bounds, OpacityCategory::Opaque)),
                cost: block::Cost {
                    components: ev_original.cost.components + 1,
                    voxels: expected_bounds.volume_f64() as u32,
                    recursion: 0
                }
            }
        );
    }

    #[test]
    fn move_voxel_block_evaluation() {
        let mut universe = Universe::new();
        let resolution = R2;
        let color = rgba_const!(1.0, 0.0, 0.0, 1.0);
        let original = Block::builder()
            .voxels_fn(resolution, |_| Block::from(color))
            .unwrap()
            .build_into(&mut universe);

        let moved = original.clone().with_modifier(Move {
            direction: Face6::PY,
            distance: 128, // distance 1/2 block × scale factor of 256
            velocity: 0,
        });

        let expected_bounds = GridAab::from_lower_size([0, 1, 0], [2, 1, 2]);

        let ev_original = original.evaluate().unwrap();
        assert_eq!(
            moved.evaluate().unwrap(),
            EvaluatedBlock {
                attributes: ev_original.attributes.clone(),
                color: color.to_rgb().with_alpha(NotNan::new(2. / 3.).unwrap()),
                face_colors: FaceMap {
                    nx: color.to_rgb().with_alpha(notnan!(0.5)),
                    ny: color.to_rgb().with_alpha(notnan!(1.0)),
                    nz: color.to_rgb().with_alpha(notnan!(0.5)),
                    px: color.to_rgb().with_alpha(notnan!(0.5)),
                    py: color.to_rgb().with_alpha(notnan!(1.0)),
                    pz: color.to_rgb().with_alpha(notnan!(0.5)),
                },
                light_emission: Rgb::ZERO,
                voxels: Evoxels::Many(
                    resolution,
                    Vol::repeat(expected_bounds, Evoxel::from_block(&ev_original))
                ),
                opaque: FaceMap::repeat(false).with(Face6::PY, true),
                visible: true,
                uniform_collision: None,
                voxel_opacity_mask: Some(Vol::repeat(expected_bounds, OpacityCategory::Opaque)),
                cost: block::Cost {
                    components: ev_original.cost.components + 1,
                    voxels: 2u32.pow(3) * 3 / 2, // original recur + 1/2 block of Move
                    recursion: 0
                }
            }
        );
    }

    /// [`Modifier::Move`] incorporates [`Modifier::Quote`] to ensure that no conflicting
    /// effects happen.
    #[test]
    fn move_also_quotes() {
        let original = Block::builder()
            .color(Rgba::WHITE)
            .tick_action(Some(TickAction::from(Operation::Become(AIR))))
            .build();
        let moved = original.with_modifier(Move {
            direction: Face6::PY,
            distance: 128,
            velocity: 0,
        });

        assert_eq!(moved.evaluate().unwrap().attributes.tick_action, None);
    }

    /// Set up a `Modifier::Move`, let it run, and then allow assertions to be made about the result.
    fn move_block_test(direction: Face6, velocity: i16, checker: impl FnOnce(&Space, &Block)) {
        let [block] = make_some_blocks();
        let mut space = Space::empty(GridAab::from_lower_upper([-1, -1, -1], [2, 2, 2]));
        let [move_out, move_in] = Move::paired_move(direction, 0, velocity);
        space
            .set([0, 0, 0], block.clone().with_modifier(move_out))
            .unwrap();
        space
            .set(
                GridPoint::origin() + direction.normal_vector(),
                block.clone().with_modifier(move_in),
            )
            .unwrap();
        let mut universe = Universe::new();
        let space = universe.insert_anonymous(space);
        // TODO: We need a "step until idle" function, or for the UniverseStepInfo to convey how many blocks were updated / are waiting
        // TODO: Some tests will want to look at the partial results
        for _ in 0..257 {
            universe.step(false, time::DeadlineNt::Whenever);
        }
        checker(&space.read().unwrap(), &block);
    }

    #[test]
    fn velocity_zero() {
        move_block_test(Face6::PX, 0, |space, block| {
            assert_eq!(&space[[0, 0, 0]], block);
            assert_eq!(&space[[1, 0, 0]], &AIR);
        });
    }

    #[test]
    fn velocity_slow() {
        move_block_test(Face6::PX, 1, |space, block| {
            assert_eq!(&space[[0, 0, 0]], &AIR);
            assert_eq!(&space[[1, 0, 0]], block);
        });
    }

    #[test]
    fn velocity_whole_cube_in_one_tick() {
        move_block_test(Face6::PX, 256, |space, block| {
            assert_eq!(&space[[0, 0, 0]], &AIR);
            assert_eq!(&space[[1, 0, 0]], block);
        });
    }

    /// Test [`Move`] acting within another modifier ([`Composite`]).
    #[test]
    fn move_inside_composite_destination() {
        let [base, extra] = make_some_blocks();
        let composite = Composite::new(extra, block::CompositeOperator::Over);

        let block = base
            .clone()
            .with_modifier(Move {
                direction: Face6::PX,
                distance: 10,
                velocity: 10,
            })
            .with_modifier(composite.clone());

        let expected_after_tick = base
            .clone()
            .with_modifier(Move {
                direction: Face6::PX,
                distance: 20,
                velocity: 10,
            })
            .with_modifier(composite);

        assert_eq!(
            block.evaluate().unwrap().attributes.tick_action,
            Some(TickAction::from(Operation::Become(expected_after_tick)))
        );
    }

    /// Test [`Move`] acting within the `source` position of a [`Modifier::Composite`].
    ///
    /// TODO: This is not yet implemented, but should be.
    #[test]
    fn move_inside_composite_source() {
        let [base, extra] = make_some_blocks();

        let block = extra.clone().with_modifier(Composite::new(
            base.clone().with_modifier(Move {
                direction: Face6::PX,
                distance: 10,
                velocity: 10,
            }),
            block::CompositeOperator::Over,
        ));

        let expected_after_tick = extra.clone().with_modifier(Composite::new(
            base.clone().with_modifier(Move {
                direction: Face6::PX,
                distance: 10,
                velocity: 10,
            }),
            block::CompositeOperator::Over,
        ));

        if false {
            // This is what we want to happen
            assert_eq!(
                block.evaluate().unwrap().attributes.tick_action,
                Some(TickAction::from(Operation::Become(expected_after_tick)))
            );
        } else {
            // Placeholder to fail if the current behavior changes
            assert_eq!(block.evaluate().unwrap().attributes.tick_action, None);
        }
    }
}