Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boulders #147

Merged
merged 2 commits into from Oct 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
118 changes: 51 additions & 67 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions src/core/check.rs
Expand Up @@ -17,7 +17,7 @@ pub enum Error {
NotEnoughMovePoints,
BadActorId,
BadTargetId,
TileIsOccupied,
TileIsBlocked,
DistanceIsTooBig,
CanNotCommandEnemyUnits,
NotEnoughMoves,
Expand All @@ -43,8 +43,8 @@ fn check_move_to(state: &State, command: &command::MoveTo) -> Result<(), Error>
}
}
for step in command.path.steps() {
if !core::object_ids_at(state, step.to).is_empty() {
return Err(Error::TileIsOccupied);
if core::is_tile_blocked(state, step.to) {
return Err(Error::TileIsBlocked);
}
}
let cost = command.path.cost_for(state, command.id);
Expand All @@ -58,8 +58,8 @@ fn check_create(state: &State, command: &command::Create) -> Result<(), Error> {
if !state.map().is_inboard(command.pos) {
return Err(Error::BadPos);
}
if !core::object_ids_at(state, command.pos).is_empty() {
return Err(Error::TileIsOccupied);
if core::is_tile_blocked(state, command.pos) {
return Err(Error::TileIsBlocked);
}
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/command.rs
Expand Up @@ -12,7 +12,7 @@ pub enum Command {

#[derive(Debug, Clone)]
pub struct Create {
pub owner: PlayerId,
pub owner: Option<PlayerId>,
pub pos: PosHex,
pub prototype: String,
}
Expand Down
4 changes: 4 additions & 0 deletions src/core/component.rs
Expand Up @@ -3,6 +3,9 @@ use core::{self, map, Attacks, Jokers, MovePoints, Moves, PlayerId};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Pos(pub map::PosHex);

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Blocker;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Strength {
pub base_strength: core::Strength,
Expand Down Expand Up @@ -40,4 +43,5 @@ pub enum Component {
Meta(Meta),
BelongsTo(BelongsTo),
Agent(Agent),
Blocker(Blocker),
}
1 change: 1 addition & 0 deletions src/core/event.rs
Expand Up @@ -87,6 +87,7 @@ fn apply_event_create(state: &mut State, event: &Create) {
Component::Meta(c) => state.parts.meta.insert(id, c),
Component::BelongsTo(c) => state.parts.belongs_to.insert(id, c),
Component::Agent(c) => state.parts.agent.insert(id, c),
Component::Blocker(c) => state.parts.blocker.insert(id, c),
}
}
}
Expand Down
66 changes: 40 additions & 26 deletions src/core/execute.rs
Expand Up @@ -115,8 +115,10 @@ where
F: FnMut(&State, &Event, Phase),
{
let mut components = state.prototypes.0[&command.prototype].clone();
if let Some(player_id) = command.owner {
components.push(Component::BelongsTo(component::BelongsTo(player_id)));
}
components.extend_from_slice(&[
Component::BelongsTo(component::BelongsTo(command.owner)),
Component::Pos(component::Pos(command.pos)),
Component::Meta(component::Meta {
name: command.prototype.clone(),
Expand Down Expand Up @@ -278,7 +280,22 @@ fn next_player_id(state: &State) -> PlayerId {
}
}

fn random_free_pos(state: &State, player_id: PlayerId) -> Option<PosHex> {
fn random_free_pos(state: &State) -> Option<PosHex> {
let attempts = 30;
let radius = state.map().radius();
for _ in 0..attempts {
let pos = PosHex {
q: thread_rng().gen_range(-radius.0, radius.0),
r: thread_rng().gen_range(-radius.0, radius.0),
};
if state.map().is_inboard(pos) && !core::is_tile_blocked(state, pos) {
return Some(pos);
}
}
None
}

fn random_free_sector_pos(state: &State, player_id: PlayerId) -> Option<PosHex> {
let attempts = 30;
let radius = state.map().radius();
let start_sector_width = radius.0;
Expand All @@ -292,7 +309,7 @@ fn random_free_pos(state: &State, player_id: PlayerId) -> Option<PosHex> {
},
r: thread_rng().gen_range(-radius.0, radius.0),
};
if state.map().is_inboard(pos) && core::object_ids_at(state, pos).is_empty() {
if state.map().is_inboard(pos) && !core::is_tile_blocked(state, pos) {
return Some(pos);
}
}
Expand All @@ -305,30 +322,27 @@ where
F: FnMut(&State, &Event, Phase),
{
let player_id_initial = state.player_id;
for &(player_index, typename) in &[
// player 0
(0, "swordsman"),
(0, "spearman"),
(0, "swordsman"),
(0, "spearman"),
// player 1
(1, "imp"),
(1, "imp"),
(1, "imp"),
(1, "imp"),
(1, "imp"),
(1, "imp"),
(1, "imp"),
for &(owner, typename, count) in &[
(None, "boulder", 10),
(Some(PlayerId(0)), "swordsman", 2),
(Some(PlayerId(0)), "spearman", 2),
(Some(PlayerId(1)), "imp", 9),
] {
let player_id = PlayerId(player_index);
let pos = random_free_pos(state, player_id).unwrap();
let command = Command::Create(command::Create {
prototype: typename.into(),
pos,
owner: player_id,
});
state.player_id = player_id;
execute(state, &command, cb).expect("Can't create object");
if let Some(player_id) = owner {
state.player_id = player_id;
}
for _ in 0..count {
let pos = match owner {
Some(player_id) => random_free_sector_pos(state, player_id),
None => random_free_pos(state),
}.unwrap();
let command = Command::Create(command::Create {
prototype: typename.into(),
pos,
owner,
});
execute(state, &command, cb).expect("Can't create object");
}
}
state.player_id = player_id_initial;
}
10 changes: 10 additions & 0 deletions src/core/mod.rs
Expand Up @@ -58,6 +58,7 @@ rancor_storage!(Parts<ObjId>: {
meta: component::Meta,
belongs_to: component::BelongsTo,
agent: component::Agent,
blocker: component::Blocker,
});

#[derive(Clone, Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -122,3 +123,12 @@ pub fn object_ids_at(state: &State, pos: PosHex) -> Vec<ObjId> {
}
ids
}

pub fn is_tile_blocked(state: &State, pos: PosHex) -> bool {
for id in state.parts.blocker.ids() {
if state.parts.pos.get(id).0 == pos {
return true;
}
}
false
}
2 changes: 1 addition & 1 deletion src/core/movement.rs
Expand Up @@ -172,7 +172,7 @@ impl Pathfinder {
assert!(self.map.is_inboard(pos));
for dir in dirs() {
let neighbor_pos = Dir::get_neighbor_pos(pos, dir);
if !core::object_ids_at(state, neighbor_pos).is_empty() {
if core::is_tile_blocked(state, neighbor_pos) {
continue;
}
if self.map.is_inboard(neighbor_pos) {
Expand Down
5 changes: 5 additions & 0 deletions src/screen/game.rs
Expand Up @@ -363,6 +363,7 @@ impl Game {

fn select_unit(&mut self, context: &mut Context, id: ObjId) {
self.deselect(context);
assert!(self.state.parts().agent.get_opt(id).is_some());
self.selected_unit_id = Some(id);
self.pathfinder.fill_map(&self.state, id);
self.show_selection_marker(id);
Expand All @@ -387,6 +388,10 @@ impl Game {
if !object_ids.is_empty() {
assert_eq!(object_ids.len(), 1);
let id = object_ids[0];
if self.state.parts().agent.get_opt(id).is_none() {
// only agents can be selected
return;
}
let other_unit_player_id = self.state.parts().belongs_to.get(id).0;
if let Some(selected_unit_id) = self.selected_unit_id {
let selected_unit_player_id =
Expand Down
2 changes: 2 additions & 0 deletions src/visualize.rs
Expand Up @@ -195,10 +195,12 @@ fn visualize_event_create(
event: &event::Create,
) -> Box<Action> {
let point = map::hex_to_point(view.tile_size(), event.pos);
// TODO: Move to some .ron config:
let sprite_name = match event.prototype.as_str() {
"swordsman" => "swordsman.png",
"spearman" => "spearman.png",
"imp" => "imp.png",
"boulder" => "boulder.png",
_ => unimplemented!(),
};
let size = view.tile_size() * 2.0;
Expand Down