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
//! Methods related to approximating in-room positions
//! between other in-room positions.
use super::RoomXY;
impl RoomXY {
/// Calculates an approximate midpoint between this point and the target.
///
/// In case of a tie, rounds towards this point.
///
/// If `distance_towards_target` is bigger than the distance to the target,
/// the target is returned.
///
/// # Example
///
/// ```
/// # use screeps::RoomXY;
///
/// // Exact distances
/// let start = RoomXY::checked_new(10, 10).unwrap();
/// let target = RoomXY::checked_new(10, 15).unwrap();
/// assert_eq!(
/// start.towards(target, 1),
/// RoomXY::checked_new(10, 11).unwrap()
/// );
/// assert_eq!(
/// start.towards(target, 4),
/// RoomXY::checked_new(10, 14).unwrap()
/// );
/// assert_eq!(
/// start.towards(target, 10),
/// RoomXY::checked_new(10, 15).unwrap()
/// );
///
/// // Approximate/rounded distances
/// let start = RoomXY::checked_new(10, 10).unwrap();
/// let target_1 = RoomXY::checked_new(15, 20).unwrap();
/// let target_2 = RoomXY::checked_new(0, 5).unwrap();
/// assert_eq!(
/// start.towards(target_1, 1),
/// RoomXY::checked_new(10, 11).unwrap()
/// );
/// assert_eq!(
/// start.towards(target_1, 9),
/// RoomXY::checked_new(14, 19).unwrap()
/// );
/// assert_eq!(
/// start.towards(target_2, 1),
/// RoomXY::checked_new(9, 10).unwrap()
/// );
/// ```
pub fn towards(self, target: RoomXY, distance_towards_target: i8) -> RoomXY {
let (offset_x, offset_y) = target - self;
let total_distance = offset_x.abs().max(offset_y.abs());
if distance_towards_target > total_distance {
return target;
}
let new_offset_x = (offset_x * distance_towards_target) / total_distance;
let new_offset_y = (offset_y * distance_towards_target) / total_distance;
self + (new_offset_x, new_offset_y)
}
/// Calculates an approximate midpoint between this point and the target.
///
/// In case of a tie, rounds towards the target.
///
/// If `distance_from_target` is bigger than the distance to the target,
/// this position is returned.
///
/// Note: This is essentially the same as [`RoomXY::towards`], just rounding
/// towards the target instead of the starting position.
///
/// # Example
///
/// ```
/// # use screeps::RoomXY;
///
/// // Exact distances
/// let start = RoomXY::checked_new(10, 15).unwrap();
/// let target = RoomXY::checked_new(10, 10).unwrap();
/// assert_eq!(
/// start.between(target, 1),
/// RoomXY::checked_new(10, 11).unwrap()
/// );
/// assert_eq!(
/// start.between(target, 4),
/// RoomXY::checked_new(10, 14).unwrap()
/// );
/// assert_eq!(
/// start.between(target, 10),
/// RoomXY::checked_new(10, 15).unwrap()
/// );
///
/// // Approximate/rounded distances
/// let start_1 = RoomXY::checked_new(15, 20).unwrap();
/// let start_2 = RoomXY::checked_new(0, 5).unwrap();
/// let target = RoomXY::checked_new(10, 10).unwrap();
/// assert_eq!(
/// start_1.between(target, 1),
/// RoomXY::checked_new(10, 11).unwrap()
/// );
/// assert_eq!(
/// start_1.between(target, 9),
/// RoomXY::checked_new(14, 19).unwrap()
/// );
/// assert_eq!(
/// start_2.between(target, 1),
/// RoomXY::checked_new(9, 10).unwrap()
/// );
/// ```
pub fn between(self, target: RoomXY, distance_from_target: i8) -> RoomXY {
target.towards(self, distance_from_target)
}
/// Calculates an approximate midpoint between this point and the target.
///
/// In case of a tie, rounds towards the target.
///
/// # Example
///
/// ```
/// # use screeps::RoomXY;
///
/// // Exact distances
/// let start = RoomXY::checked_new(10, 10).unwrap();
///
/// let target_1 = RoomXY::checked_new(10, 16).unwrap();
/// assert_eq!(
/// start.midpoint_between(target_1),
/// RoomXY::checked_new(10, 13).unwrap()
/// );
///
/// let target_2 = RoomXY::checked_new(20, 10).unwrap();
/// assert_eq!(
/// start.midpoint_between(target_2),
/// RoomXY::checked_new(15, 10).unwrap()
/// );
///
/// let target_3 = RoomXY::checked_new(12, 12).unwrap();
/// assert_eq!(
/// start.midpoint_between(target_3),
/// RoomXY::checked_new(11, 11).unwrap()
/// );
///
/// let target_4 = RoomXY::checked_new(4, 4).unwrap();
/// assert_eq!(
/// start.midpoint_between(target_4),
/// RoomXY::checked_new(7, 7).unwrap()
/// );
///
/// // Approximate/rounded distances
/// let start = RoomXY::checked_new(10, 10).unwrap();
///
/// let target_1 = RoomXY::checked_new(10, 15).unwrap();
/// assert_eq!(
/// start.midpoint_between(target_1),
/// RoomXY::checked_new(10, 13).unwrap()
/// );
///
/// let target_2 = RoomXY::checked_new(19, 10).unwrap();
/// assert_eq!(
/// start.midpoint_between(target_2),
/// RoomXY::checked_new(15, 10).unwrap()
/// );
///
/// let target_3 = RoomXY::checked_new(11, 11).unwrap();
/// assert_eq!(
/// start.midpoint_between(target_3),
/// RoomXY::checked_new(11, 11).unwrap()
/// );
///
/// let target_4 = RoomXY::checked_new(15, 15).unwrap();
/// assert_eq!(
/// start.midpoint_between(target_4),
/// RoomXY::checked_new(13, 13).unwrap()
/// );
/// ```
pub fn midpoint_between(self, target: RoomXY) -> RoomXY {
let (offset_x, offset_y) = self - target;
let new_offset_x = offset_x / 2;
let new_offset_y = offset_y / 2;
target + (new_offset_x, new_offset_y)
}
}
#[cfg(test)]
mod test {
use super::RoomXY;
fn pos(x: u8, y: u8) -> RoomXY {
RoomXY::checked_new(x, y).unwrap()
}
#[test]
fn towards_accurate() {
let start = pos(10, 10);
assert_eq!(start.towards(pos(10, 15), 1), pos(10, 11));
assert_eq!(start.towards(pos(10, 15), 4), pos(10, 14));
assert_eq!(start.towards(pos(10, 15), 10), pos(10, 15));
assert_eq!(start.towards(pos(15, 15), 1), pos(11, 11));
assert_eq!(start.towards(pos(15, 15), 3), pos(13, 13));
assert_eq!(start.towards(pos(15, 20), 2), pos(11, 12));
assert_eq!(start.towards(pos(0, 5), 2), pos(8, 9));
}
#[test]
fn towards_approximate() {
let start = pos(10, 10);
assert_eq!(start.towards(pos(15, 20), 1), pos(10, 11));
assert_eq!(start.towards(pos(15, 20), 9), pos(14, 19));
assert_eq!(start.towards(pos(0, 5), 1), pos(9, 10));
}
#[test]
fn midpoint_accurate() {
let start = pos(10, 10);
assert_eq!(start.midpoint_between(pos(10, 16)), pos(10, 13));
assert_eq!(start.midpoint_between(pos(20, 10)), pos(15, 10));
assert_eq!(start.midpoint_between(pos(12, 12)), pos(11, 11));
assert_eq!(start.midpoint_between(pos(4, 4)), pos(7, 7));
}
#[test]
fn midpoint_approximate() {
let start = pos(10, 10);
assert_eq!(start.midpoint_between(pos(10, 15)), pos(10, 13));
assert_eq!(start.midpoint_between(pos(19, 10)), pos(15, 10));
assert_eq!(start.midpoint_between(pos(11, 11)), pos(11, 11));
assert_eq!(start.midpoint_between(pos(15, 15)), pos(13, 13));
assert_eq!(start.midpoint_between(pos(15, 25)), pos(13, 18));
assert_eq!(start.midpoint_between(pos(9, 10)), pos(9, 10));
assert_eq!(start.midpoint_between(pos(7, 10)), pos(8, 10));
assert_eq!(start.midpoint_between(pos(1, 3)), pos(5, 6));
}
}