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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//! Lesser-used helpers for [`BlockBuilder`].

use alloc::borrow::Cow;
use alloc::sync::Arc;
use alloc::vec::Vec;

use arcstr::ArcStr;

use crate::block::{
    AnimationHint, Atom, Block, BlockAttributes, BlockCollision, BlockParts, BlockPtr, Modifier,
    Primitive, Resolution, RotationPlacementRule, AIR,
};
use crate::math::{Cube, GridAab, GridPoint, Rgb, Rgba};
use crate::space::{SetCubeError, Space};
use crate::transaction::{self, Merge, Transaction};
use crate::universe::{Handle, Name, Universe, UniverseTransaction};

/// Tool for constructing [`Block`] values conveniently.
///
/// To create one, call [`Block::builder()`].
/// ([`BlockBuilder::default()`] is also available.)
///
/// ```
/// use all_is_cubes::block::Block;
/// use all_is_cubes::math::Rgba;
///
/// let block = Block::builder()
///    .display_name("BROWN")
///    .color(Rgba::new(0.5, 0.5, 0., 1.))
///    .build();
///
/// assert_eq!(block.evaluate().unwrap().color, Rgba::new(0.5, 0.5, 0., 1.));
/// assert_eq!(
///     block.evaluate().unwrap().attributes.display_name.as_str(),
///     "BROWN",
/// );
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
#[must_use]
pub struct BlockBuilder<P, Txn> {
    attributes: BlockAttributes,
    primitive_builder: P,
    modifiers: Vec<Modifier>,

    /// If this is a [`UniverseTransaction`], then it must be produced for the caller to execute.
    /// If this is `()`, then it may be disregarded.
    transaction: Txn,
}

impl Default for BlockBuilder<NeedsPrimitive, ()> {
    fn default() -> Self {
        Self::new()
    }
}

impl BlockBuilder<NeedsPrimitive, ()> {
    /// Common implementation of [`Block::builder`] and [`Default::default`]; use one of those to call this.
    pub(super) const fn new() -> Self {
        BlockBuilder {
            attributes: BlockAttributes::default(),
            primitive_builder: NeedsPrimitive,
            modifiers: Vec::new(),
            transaction: (),
        }
    }
}

impl<P, Txn> BlockBuilder<P, Txn> {
    // TODO: When #![feature(const_precise_live_drops)] becomes stable, we can make
    // this builder mostly usable in const contexts.
    // https://github.com/rust-lang/rust/issues/73255
    // Doing that will also require creating non-trait-using alternate methods,
    // until const traits https://github.com/rust-lang/rust/issues/67792 is also available.

    /// Sets the [`BlockAttributes`] the block will have.
    /// This replaces individual attribute values set using other builder methods.
    pub fn attributes(mut self, value: BlockAttributes) -> Self {
        self.attributes = value;
        self
    }

    /// Sets the value for [`BlockAttributes::display_name`].
    pub fn display_name(mut self, value: impl Into<ArcStr>) -> Self {
        self.attributes.display_name = value.into();
        self
    }

    /// Sets the value for [`BlockAttributes::selectable`].
    pub const fn selectable(mut self, value: bool) -> Self {
        self.attributes.selectable = value;
        self
    }

    /// Sets the value for [`BlockAttributes::rotation_rule`].
    pub const fn rotation_rule(mut self, value: RotationPlacementRule) -> Self {
        self.attributes.rotation_rule = value;
        self
    }

    /// Sets the value for [`BlockAttributes::tick_action`].
    pub fn tick_action(mut self, value: impl Into<Option<super::TickAction>>) -> Self {
        self.attributes.tick_action = value.into();
        self
    }

    /// Sets the value for [`BlockAttributes::activation_action`].
    pub fn activation_action(mut self, value: impl Into<Option<crate::op::Operation>>) -> Self {
        self.attributes.activation_action = value.into();
        self
    }

    /// Sets the value for [`BlockAttributes::animation_hint`].
    pub fn animation_hint(mut self, value: AnimationHint) -> Self {
        self.attributes.animation_hint = value;
        self
    }

    /// Adds a modifier to the end of the list of modifiers for the block.
    /// It will be applied after all previously specified modifiers.
    pub fn modifier(mut self, modifier: Modifier) -> Self {
        // TODO: implement a modifier canonicalization procedure here
        self.modifiers.push(modifier);
        self
    }

    /// Sets the color value for building a [`Primitive::Atom`].
    ///
    /// This will replace any previous color **or voxels.**
    pub fn color(self, color: impl Into<Rgba>) -> BlockBuilder<BlockBuilderAtom, ()> {
        BlockBuilder {
            attributes: self.attributes,
            primitive_builder: BlockBuilderAtom {
                color: color.into(),
                emission: Rgb::ZERO,
                collision: BlockCollision::Hard,
            },
            modifiers: Vec::new(),
            // TODO: This might not be the right thing in more general transaction usage.
            // For now, it's OK that we discard the transaction because it can only ever be
            // inserting a `Space`.
            transaction: (),
        }
    }

    /// Sets the space for building a [`Primitive::Recur`].
    ///
    /// This will replace any previous voxels **or color.**
    pub fn voxels_handle(
        self,
        resolution: Resolution,
        space: Handle<Space>,
    ) -> BlockBuilder<BlockBuilderVoxels, ()> {
        BlockBuilder {
            attributes: self.attributes,
            primitive_builder: BlockBuilderVoxels {
                space,
                resolution,
                offset: GridPoint::origin(),
            },
            modifiers: Vec::new(),
            transaction: (),
        }
    }

    /// Constructs a `Space` for building a [`Primitive::Recur`], and calls
    /// the given function to fill it with blocks, in the manner of [`Space::fill`].
    ///
    /// If the voxels do not fill the entire volume of the block being built — that is, there is
    /// some smaller region outside of which they are all [`AIR`] — then the [`Space`] will be
    /// shrunk to tightly enclose that region, to improve performance. However, this still requires
    /// the function to be called on all positions within the full block bounds, so for very high
    /// ratios of resolution to actual content, it may be wise to use
    /// [`voxels_handle()`](Self::voxels_handle) instead.
    ///
    /// Note that if the resulting builder is cloned, all clones will share the same
    /// space.
    // TODO: (doc) test for this
    pub fn voxels_fn<'a, F, B>(
        self,
        // TODO: Maybe resolution should be a separate method? Check usage patterns later.
        resolution: Resolution,
        mut function: F,
    ) -> Result<BlockBuilder<BlockBuilderVoxels, UniverseTransaction>, SetCubeError>
    where
        F: FnMut(Cube) -> B,
        B: Into<Cow<'a, Block>>,
    {
        // This is a worldgen convenience, not the most efficient possible path (which would be
        // `SpaceBuilder::palette_and_contents()`), so save quite a lot of code generation
        // by keeping it monomorphic and not inlined.
        #[inline(never)]
        fn voxels_fn_impl<'a>(
            attributes: BlockAttributes,
            modifiers: Vec<Modifier>,
            resolution: Resolution,
            function: &mut dyn FnMut(Cube) -> Cow<'a, Block>,
        ) -> Result<BlockBuilder<BlockBuilderVoxels, UniverseTransaction>, SetCubeError> {
            let mut not_air_bounds: Option<GridAab> = None;

            let mut space = Space::for_block(resolution).build();
            // TODO: Teach the SpaceBuilder to accept a function in the same way?
            space.fill(space.bounds(), |cube| {
                let block = function(cube);

                // Track which of the blocks are not equal to AIR, for later use.
                if block.as_ref() != &AIR {
                    let cube_bb = cube.grid_aab();
                    not_air_bounds = Some(if let Some(bounds) = not_air_bounds {
                        bounds.union_box(cube_bb)
                    } else {
                        cube_bb
                    });
                }

                Some(block)
            })?;

            // If the block bounding box is not full of non-AIR blocks, then construct a replacement
            // Space that is smaller. This is equivalent, but improves the performance of all future
            // uses of this block.
            let not_air_bounds = not_air_bounds.unwrap_or(GridAab::ORIGIN_EMPTY);
            if space.bounds() != not_air_bounds {
                // TODO: Eventually we should be able to ask the Space to resize itself,
                // but that is not yet an available operation.
                let mut shrunk = Space::builder(not_air_bounds)
                    .physics(space.physics().clone())
                    .build();
                shrunk.fill(not_air_bounds, |cube| Some(&space[cube]))?;
                space = shrunk;
            }

            let space_handle = Handle::new_pending(Name::Pending, space);

            Ok(BlockBuilder {
                attributes,
                primitive_builder: BlockBuilderVoxels {
                    space: space_handle.clone(),
                    resolution,
                    offset: GridPoint::origin(),
                },
                modifiers,
                transaction: UniverseTransaction::insert(space_handle),
            })
        }

        voxels_fn_impl(self.attributes, self.modifiers, resolution, &mut |cube| {
            function(cube).into()
        })
    }

    fn build_block_and_txn_internal(self) -> (Block, Txn)
    where
        P: BuildPrimitive,
    {
        let primitive = self.primitive_builder.build_primitive(self.attributes);
        let block = if matches!(primitive, Primitive::Air) && self.modifiers.is_empty() {
            // Avoid allocating an Arc.
            AIR
        } else {
            Block(BlockPtr::Owned(Arc::new(BlockParts {
                primitive,
                modifiers: self.modifiers,
            })))
        };
        (block, self.transaction)
    }
}

impl<P: BuildPrimitive> BlockBuilder<P, ()> {
    /// Converts this builder into a block value.
    ///
    /// This method may only be used when the builder has *not* been used with `voxels_fn()`,
    /// since in that case a universe transaction must be executed.
    pub fn build(self) -> Block {
        let (block, ()) = self.build_block_and_txn_internal();
        block
    }
}

impl<P: BuildPrimitive> BlockBuilder<P, UniverseTransaction> {
    // TODO: Also allow extracting the transaction for later use

    /// Converts this builder into a block value, and inserts its associated [`Space`] into the
    /// given universe.
    pub fn build_into(self, universe: &mut Universe) -> Block {
        let (block, transaction) = self.build_block_and_txn_internal();

        // The transaction is always an insert_anonymous, which cannot fail.
        transaction
            .execute(universe, &mut transaction::no_outputs)
            .unwrap();

        block
    }

    /// Converts this builder into a [`Block`] value, and modifies the given transaction to include
    /// inserting the associated space into the universe the block is to be used in.
    pub fn build_txn(self, transaction: &mut UniverseTransaction) -> Block {
        let (block, txn) = self.build_block_and_txn_internal();
        transaction.merge_from(txn).unwrap();
        block
    }
}

/// Atom-specific builder methods.
impl<Txn> BlockBuilder<BlockBuilderAtom, Txn> {
    /// Sets the collision behavior of a [`Primitive::Atom`] block.
    pub const fn collision(mut self, collision: BlockCollision) -> Self {
        self.primitive_builder.collision = collision;
        self
    }

    /// Sets the light emission of a [`Primitive::Atom`] block.
    ///
    /// This quantity is the [_luminous emittance_](https://en.wikipedia.org/wiki/Luminous_emittance)
    /// (the emitted portion of luminance) of the block surface, in unspecified units where 1.0 is
    /// the display white level (except for the effects of tone mapping).
    /// In the future, this may be redefined in terms of a physical unit, but with the same
    /// dimensions.
    ///
    /// TODO: Define the interpretation for non-opaque blocks.
    pub fn light_emission(mut self, value: impl Into<Rgb>) -> Self {
        self.primitive_builder.emission = value.into();
        self
    }
}

/// Voxel-specific builder methods.
impl<Txn> BlockBuilder<BlockBuilderVoxels, Txn> {
    /// Sets the coordinate offset for building a [`Primitive::Recur`]:
    /// the lower-bound corner of the region of the [`Space`]
    /// which will be used for block voxels. The default is zero.
    pub fn offset(mut self, offset: GridPoint) -> Self {
        self.primitive_builder.offset = offset;
        self
    }

    // TODO: It might be useful to have "offset equal to resolution"
    // and "add offset", but don't add those until use cases are seen.
}

/// Allows implicitly converting `BlockBuilder` to the block it would build.
impl<C: BuildPrimitive> From<BlockBuilder<C, ()>> for Block {
    fn from(builder: BlockBuilder<C, ()>) -> Self {
        builder.build()
    }
}
/// Equivalent to `Block::builder().color(color)`.
impl From<Rgba> for BlockBuilder<BlockBuilderAtom, ()> {
    fn from(color: Rgba) -> Self {
        Block::builder().color(color)
    }
}
/// Equivalent to `Block::builder().color(color.with_alpha_one())`.
impl From<Rgb> for BlockBuilder<BlockBuilderAtom, ()> {
    fn from(color: Rgb) -> Self {
        Block::builder().color(color.with_alpha_one())
    }
}

/// Placeholder type for an incomplete [`BlockBuilder`]'s content. The builder
/// cannot create an actual block until this is replaced.
#[allow(clippy::exhaustive_structs)]
#[derive(Copy, Clone, Debug, Default, Eq, Hash, PartialEq)]
pub struct NeedsPrimitive;

/// Something that a parameterized [`BlockBuilder`] can use to construct a block's primitive.
#[doc(hidden)]
pub trait BuildPrimitive {
    fn build_primitive(self, attributes: BlockAttributes) -> Primitive;
}

/// Parameter type for [`BlockBuilder::color`], building [`Primitive::Atom`].
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct BlockBuilderAtom {
    color: Rgba,
    emission: Rgb,
    collision: BlockCollision,
}
impl BuildPrimitive for BlockBuilderAtom {
    fn build_primitive(self, attributes: BlockAttributes) -> Primitive {
        Primitive::Atom(Atom {
            attributes,
            color: self.color,
            emission: self.emission,
            collision: self.collision,
        })
    }
}

/// Parameter type for a [`BlockBuilder`] that is building a block with voxels
/// ([`Primitive::Recur`]).
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct BlockBuilderVoxels {
    space: Handle<Space>,
    resolution: Resolution,
    offset: GridPoint,
}
impl BuildPrimitive for BlockBuilderVoxels {
    fn build_primitive(self, attributes: BlockAttributes) -> Primitive {
        Primitive::Recur {
            attributes,
            offset: self.offset,
            resolution: self.resolution,
            space: self.space,
        }
    }
}

#[cfg(test)]
mod tests {
    use alloc::boxed::Box;

    use crate::block::{self, Resolution::*, TickAction};
    use crate::content::palette;
    use crate::math::{Face6, Vol};
    use crate::op::Operation;
    use crate::space::SpacePhysics;
    use crate::transaction::Transactional as _;

    use super::*;

    #[test]
    fn defaults() {
        let color = Rgba::new(0.1, 0.2, 0.3, 0.4);
        assert_eq!(
            Block::builder().color(color).build(),
            Block::from(Atom {
                attributes: BlockAttributes::default(),
                color,
                emission: Rgb::ZERO,
                collision: BlockCollision::Hard,
            }),
        );
    }

    #[test]
    fn default_equivalent() {
        assert_eq!(
            BlockBuilder::new(),
            <BlockBuilder<NeedsPrimitive, ()> as Default>::default()
        );
    }

    #[test]
    fn every_field_nondefault() {
        let color = Rgba::new(0.1, 0.2, 0.3, 0.4);
        let emission = Rgb::new(0.1, 3.0, 0.1);
        let rotation_rule = RotationPlacementRule::Attach { by: Face6::NZ };
        let tick_action = Some(TickAction::from(Operation::Become(AIR)));
        let activation_action = Some(Operation::Become(color_block!(1.0, 1.0, 1.0)));
        assert_eq!(
            Block::builder()
                .color(color)
                .display_name("hello world")
                .collision(BlockCollision::None)
                .rotation_rule(rotation_rule)
                .selectable(false)
                .light_emission(emission)
                .tick_action(tick_action.clone())
                .activation_action(activation_action.clone())
                .animation_hint(AnimationHint::replacement(block::AnimationChange::Shape))
                .build(),
            Block::from(Atom {
                attributes: BlockAttributes {
                    display_name: "hello world".into(),
                    rotation_rule,
                    selectable: false,
                    tick_action,
                    activation_action,
                    animation_hint: AnimationHint::replacement(block::AnimationChange::Shape),
                },
                color,
                emission,
                collision: BlockCollision::None,
            }),
        );
    }

    #[test]
    fn voxels_from_space() {
        let mut universe = Universe::new();
        let space_handle = universe.insert_anonymous(Space::empty_positive(1, 1, 1));

        assert_eq!(
            Block::builder()
                .display_name("hello world")
                .voxels_handle(R2, space_handle.clone())
                .build(),
            Block::from_primitive(Primitive::Recur {
                attributes: BlockAttributes {
                    display_name: "hello world".into(),
                    ..BlockAttributes::default()
                },
                offset: GridPoint::origin(),
                resolution: R2, // not same as space size
                space: space_handle
            }),
        );
    }

    #[test]
    fn voxels_from_fn_basic() {
        let mut universe = Universe::new();

        let resolution = R4;
        let expected_bounds = GridAab::for_block(resolution);
        let atom = color_block!(palette::DIRT);
        let block = Block::builder()
            .display_name("hello world")
            .voxels_fn(resolution, |_cube| &atom)
            .unwrap()
            .build_into(&mut universe);

        // Extract the implicitly constructed space handle
        let space_handle = if let Primitive::Recur { space, .. } = block.primitive() {
            space.clone()
        } else {
            panic!("expected Recur, found {block:?}");
        };

        assert_eq!(
            block,
            Block::from_primitive(Primitive::Recur {
                attributes: BlockAttributes {
                    display_name: "hello world".into(),
                    ..BlockAttributes::default()
                },
                offset: GridPoint::origin(),
                resolution,
                space: space_handle.clone()
            }),
        );

        // Check the space's characteristics
        let space = space_handle.read().unwrap();
        assert_eq!(space.bounds(), expected_bounds);
        assert_eq!(space.physics(), &SpacePhysics::DEFAULT_FOR_BLOCK);
        assert_eq!(
            space.extract(expected_bounds, |e| e.block_data().block()),
            Vol::<Box<[&Block]>>::from_fn(expected_bounds, |_| &atom)
        );
    }

    /// `voxels_fn()` automatically shrinks the space bounds to fit only the nonair blocks.
    #[test]
    fn voxels_from_fn_shrinkwrap() {
        let mut universe = Universe::new();

        let resolution = R4;
        let expected_bounds = GridAab::from_lower_upper([0, 0, 0], [2, 4, 4]);
        let atom = color_block!(palette::DIRT);
        let block = Block::builder()
            .display_name("hello world")
            .voxels_fn(resolution, |cube| {
                if expected_bounds.contains_cube(cube) {
                    &atom
                } else {
                    &AIR
                }
            })
            .unwrap()
            .build_into(&mut universe);

        // Extract the implicitly constructed space handle
        let space_handle = if let Primitive::Recur { space, .. } = block.primitive() {
            space.clone()
        } else {
            panic!("expected Recur, found {block:?}");
        };

        // Check the space's characteristics; not just that it has the smaller bounds, but that
        // it has the expected physics and contents.
        let space = space_handle.read().unwrap();
        assert_eq!(space.bounds(), expected_bounds);
        assert_eq!(space.physics(), &SpacePhysics::DEFAULT_FOR_BLOCK);
        assert_eq!(
            space.extract(expected_bounds, |e| e.block_data().block()),
            Vol::<Box<[&Block]>>::from_fn(expected_bounds, |_| &atom)
        );
    }

    #[test]
    fn explicit_txn() {
        let resolution = R8;
        let mut universe = Universe::new();
        let _block = universe
            .transact(|txn, _| {
                Ok(Block::builder()
                    .display_name("hello world")
                    .voxels_fn(resolution, |_cube| &AIR)
                    .unwrap()
                    .build_txn(txn))
            })
            .unwrap();

        assert_eq!(universe.iter_by_type::<Space>().count(), 1);
    }
}