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
//! The client's world border as 26.1 moves it: a lerping size advanced once per tick (in
//! `ClientLevel.tick`, after the entities), and the size the collision code reads.
//!
//! The engine keeps one [`BorderState`] per level, applies the border packets to it
//! (`initialize_border`, `set_border_center`, `set_border_size`, `set_border_lerp_size`), calls
//! [`BorderState::tick`] after the tick's entities (the player included), and hands
//! [`BorderState::collision_border`] to the player's tick through its world view
//! (`BlockView::world_border`).
use crate::world::WorldBorder;
/// The size part of the border: fixed, or moving from one size to another.
#[derive(Clone, Copy, Debug, PartialEq)]
enum Extent {
Static(f64),
Moving {
from: f64,
to: f64,
duration: f64,
progress: i64,
size: f64,
previous: f64,
},
}
/// The client's world border: centre and (possibly moving) size.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BorderState {
center_x: f64,
center_z: f64,
extent: Extent,
}
impl Default for BorderState {
fn default() -> Self {
BorderState {
center_x: 0.0,
center_z: 0.0,
extent: Extent::Static(5.999_996_8e7),
}
}
}
fn lerp(delta: f64, a: f64, b: f64) -> f64 {
a + delta * (b - a)
}
fn moving_size(from: f64, to: f64, duration: f64, progress: i64) -> f64 {
let t = (duration - progress as f64) / duration;
if t < 1.0 { lerp(t, from, to) } else { to }
}
impl BorderState {
/// The border a client has before any border packet (centre 0, 0, size 59,999,968).
#[must_use]
pub fn new() -> BorderState {
BorderState::default()
}
/// `setCenter` (set-border-center packet, or the centre of initialize-border).
pub fn set_center(&mut self, x: f64, z: f64) {
self.center_x = x;
self.center_z = z;
}
/// `setSize`: a static border (set-border-size packet).
pub fn set_size(&mut self, size: f64) {
self.extent = Extent::Static(size);
}
/// `lerpSizeBetween(from, to, ticks)` (set-border-lerp-size packet; initialize-border with a
/// lerp time): equal sizes give a static border.
pub fn lerp_size(&mut self, from: f64, to: f64, ticks: i64) {
self.extent = if from == to {
Extent::Static(to)
} else {
let size = moving_size(from, to, ticks as f64, ticks);
Extent::Moving {
from,
to,
duration: ticks as f64,
progress: ticks,
size,
previous: size,
}
};
}
/// `WorldBorder.tick` (in `ClientLevel.tick`, after the entities): a moving border advances
/// one tick and becomes static at its target.
pub fn tick(&mut self) {
if let Extent::Moving {
from,
to,
duration,
progress,
size,
..
} = self.extent
{
let progress = progress - 1;
let new_size = moving_size(from, to, duration, progress);
self.extent = if progress <= 0 {
Extent::Static(to)
} else {
Extent::Moving {
from,
to,
duration,
progress,
size: new_size,
previous: size,
}
};
}
}
/// `getSize()`: the current size.
#[must_use]
pub fn size(&self) -> f64 {
match self.extent {
Extent::Static(s) => s,
Extent::Moving { size, .. } => size,
}
}
/// The border as collision reads it (`getMinX()` = `getMinX(0.0F)`): a moving border is at
/// its size before the last [`BorderState::tick`].
#[must_use]
pub fn collision_border(&self) -> WorldBorder {
let size = match self.extent {
Extent::Static(s) => s,
Extent::Moving { size, previous, .. } => lerp(0.0, previous, size),
};
WorldBorder {
center_x: self.center_x,
center_z: self.center_z,
size,
}
}
}