Skip to content

Commit 44957ce

Browse files
committed
More random initial positions
1 parent bd554fb commit 44957ce

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

src/core/execute.rs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -308,32 +308,50 @@ pub fn make_unit(player_id: PlayerId, pos: PosHex, type_name: &str) -> Unit {
308308
}
309309
}
310310

311+
fn random_free_pos(state: &State, player_id: PlayerId) -> Option<PosHex> {
312+
let attempts = 30;
313+
let radius = state.map().radius();
314+
let start_sector_width = radius.0;
315+
for _ in 0..attempts {
316+
let q = radius.0 - thread_rng().gen_range(0, start_sector_width);
317+
let pos = PosHex {
318+
q: match player_id.0 {
319+
0 => -q,
320+
1 => q,
321+
_ => unimplemented!(),
322+
},
323+
r: thread_rng().gen_range(-radius.0, radius.0),
324+
};
325+
if state.map().is_inboard(pos) && state.units_at(pos).is_empty() {
326+
return Some(pos);
327+
}
328+
}
329+
None
330+
}
331+
311332
// TODO: improve the API
312333
pub fn create_objects<F>(state: &mut State, cb: &mut F)
313334
where
314335
F: FnMut(&State, &Event, Phase),
315336
{
316337
let player_id_initial = state.player_id;
317-
for &(player_index, (q, r), typename) in &[
338+
for &(player_index, typename) in &[
318339
// player 0
319-
(0, (-3, 2), "swordsman"),
320-
(0, (-3, 1), "spearman"),
321-
(0, (-3, 0), "swordsman"),
322-
(0, (-3, -1), "spearman"),
340+
(0, "swordsman"),
341+
(0, "spearman"),
342+
(0, "swordsman"),
343+
(0, "spearman"),
323344
// player 1
324-
(1, (2, -4), "imp"),
325-
(1, (2, -3), "imp"),
326-
(1, (2, -2), "imp"),
327-
(1, (2, -1), "imp"),
328-
(1, (2, 0), "imp"),
329-
(1, (2, 1), "imp"),
330-
(1, (2, 2), "imp"),
345+
(1, "imp"),
346+
(1, "imp"),
347+
(1, "imp"),
348+
(1, "imp"),
349+
(1, "imp"),
350+
(1, "imp"),
351+
(1, "imp"),
331352
] {
332-
let pos = PosHex {
333-
q: q + thread_rng().gen_range(-1, 2),
334-
r,
335-
};
336353
let player_id = PlayerId(player_index);
354+
let pos = random_free_pos(state, player_id).unwrap();
337355
let unit = make_unit(player_id, pos, typename);
338356
let id = state.alloc_id();
339357
let command = command::Command::Create(command::Create { id, unit });

0 commit comments

Comments
 (0)