rust_sc2/
ramp.rs

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
//! Data structures for storing data of ramps on the map
//! with methods for extracting useful info from them.

use crate::{bot::Rs, distance::*, geometry::Point2, pixel_map::ByteMap};
use std::{
	cmp::{Ordering, Reverse},
	convert::TryInto,
	fmt,
};

/// Structured collection of ramps.
#[derive(Default)]
pub struct Ramps {
	/// All ramps on the map.
	pub all: Vec<Ramp>,
	/// Ramp to your main base.
	pub my: Ramp,
	/// Ramp to opponent's main base.
	pub enemy: Ramp,
}

type Pos = (usize, usize);

/// Ramp data structure with some helpful methods.
/// All ramps stored in [`Ramps`] in [`ramps`](crate::bot::Bot::ramps) field of bot.
#[derive(Default, Clone)]
pub struct Ramp {
	/// All points which belong to this ramp.
	pub points: Vec<Pos>,
	height: Rs<ByteMap>,
	start_location: Point2,
}
impl Ramp {
	pub(crate) fn new(points: Vec<Pos>, height: &Rs<ByteMap>, start_location: Point2) -> Self {
		Self {
			points,
			height: Rs::clone(&height),
			start_location,
		}
	}
	/// Returns only upper points of the ramp.
	pub fn upper(&self) -> Vec<Pos> {
		let mut max = u8::MIN;
		let mut result = Vec::new();

		for &p in &self.points {
			let h = self.height[p];
			match h.cmp(&max) {
				Ordering::Greater => {
					max = h;
					result = vec![p];
				}
				Ordering::Equal => result.push(p),
				_ => {}
			}
		}

		result
	}
	/// Returns only lower points of the ramp.
	pub fn lower(&self) -> Vec<Pos> {
		let mut min = u8::MAX;
		let mut result = Vec::new();

		for &p in &self.points {
			let h = self.height[p];
			match h.cmp(&min) {
				Ordering::Less => {
					min = h;
					result = vec![p];
				}
				Ordering::Equal => result.push(p),
				_ => {}
			}
		}

		result
	}
	/// Returns center of upper points of the ramp.
	pub fn top_center(&self) -> Option<Pos> {
		let ps = self.upper();
		if ps.is_empty() {
			None
		} else {
			// Some(ps.iter().sum::<Point2>() / ps.len())
			let (x, y) = ps.iter().fold((0, 0), |(ax, ay), (x, y)| (ax + x, ay + y));
			Some((x / ps.len(), y / ps.len()))
		}
	}
	/// Returns center of lower points of the ramp.
	pub fn bottom_center(&self) -> Option<Pos> {
		let ps = self.lower();
		if ps.is_empty() {
			None
		} else {
			let (x, y) = ps.iter().fold((0, 0), |(ax, ay), (x, y)| (ax + x, ay + y));
			Some((x / ps.len(), y / ps.len()))
		}
	}
	fn upper2_for_ramp_wall(&self) -> Option<[Pos; 2]> {
		let mut upper = self.upper();
		if upper.len() > 5 {
			return None;
		}
		match upper.len().cmp(&2) {
			Ordering::Greater => self.bottom_center().and_then(|(center_x, center_y)| {
				upper.sort_unstable_by_key(|(x, y)| {
					let dx = x.checked_sub(center_x).unwrap_or_else(|| center_x - x);
					let dy = y.checked_sub(center_y).unwrap_or_else(|| center_y - y);
					Reverse(dx * dx + dy * dy)
				});
				upper[..2].try_into().ok()
			}),
			Ordering::Equal => upper.as_slice().try_into().ok(),
			Ordering::Less => None,
		}
	}
	/// Returns correct positions to build corner supplies in terran wall.
	pub fn corner_depots(&self) -> Option<[Point2; 2]> {
		if let Some(ps) = self.upper2_for_ramp_wall() {
			let p1 = Point2::from(ps[0]);
			let p2 = Point2::from(ps[1]);

			let center = (p1 + p2) / 2.0;
			return center.circle_intersection(self.depot_in_middle()?, 5_f32.sqrt());
		}
		None
	}
	/// Returns correct position to build barrack in terran wall without addon.
	pub fn barracks_in_middle(&self) -> Option<Point2> {
		let upper_len = self.upper().len();
		if upper_len != 2 && upper_len != 5 {
			return None;
		}
		if let Some(ps) = self.upper2_for_ramp_wall() {
			let p1 = Point2::from(ps[0]);
			let p2 = Point2::from(ps[1]);

			let intersects = p1.circle_intersection(p2, 5_f32.sqrt())?;
			let (x, y) = *self.lower().first()?;
			let lower = Point2::new(x as f32, y as f32);

			return intersects.iter().furthest(lower).copied();
		}
		None
	}
	/// Returns correct position to build barrack in terran wall with addon.
	pub fn barracks_correct_placement(&self) -> Option<Point2> {
		self.barracks_in_middle().map(|pos| {
			if self
				.corner_depots()
				.map_or(false, |depots| pos.x + 1.0 > depots[0].x.max(depots[1].x))
			{
				pos
			} else {
				pos.offset(-2.0, 0.0)
			}
		})
	}
	/// Returns correct position to build supply in middle of wall from 3 supplies.
	pub fn depot_in_middle(&self) -> Option<Point2> {
		let upper_len = self.upper().len();
		if upper_len != 2 && upper_len != 5 {
			return None;
		}
		if let Some(ps) = self.upper2_for_ramp_wall() {
			let p1 = Point2::from(ps[0]);
			let p2 = Point2::from(ps[1]);

			let intersects = p1.circle_intersection(p2, 1.581_138_8)?; // 2.5_f32.sqrt()
			let (x, y) = *self.lower().first()?;
			let lower = Point2::new(x as f32, y as f32);

			return intersects.iter().furthest(lower).copied();
		}
		None
	}
	/// Returns correct position to build pylon in protoss wall.
	pub fn protoss_wall_pylon(&self) -> Option<Point2> {
		let middle = self.depot_in_middle()?;
		Some(middle + (self.barracks_in_middle()? - middle) * 6.0)
	}
	/// Returns correct positions of 3x3 buildings in protoss wall.
	pub fn protoss_wall_buildings(&self) -> Option<[Point2; 2]> {
		let middle = self.depot_in_middle()?;
		let direction = self.barracks_in_middle()? - middle;

		let mut depots = self.corner_depots()?.to_vec();
		let start = self.start_location;
		depots.sort_unstable_by(|d1, d2| {
			d1.distance_squared(start)
				.partial_cmp(&d2.distance_squared(start))
				.unwrap()
		});

		let wall1 = depots[1] + direction;
		Some([wall1, middle + direction + (middle - wall1) / 1.5])
	}
	/// Returns correct position of unit to close protoss wall.
	pub fn protoss_wall_warpin(&self) -> Option<Point2> {
		let middle = self.depot_in_middle()?;
		let direction = self.barracks_in_middle()? - middle;

		let mut depots = self.corner_depots()?.to_vec();
		let start = self.start_location;
		depots.sort_unstable_by(|d1, d2| {
			d1.distance_squared(start)
				.partial_cmp(&d2.distance_squared(start))
				.unwrap()
		});

		Some(depots[0] - direction)
	}
}
impl fmt::Debug for Ramp {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		write!(f, "Ramp({:?})", self.points)
	}
}