Skip to content

Commit

Permalink
Merge pull request #9 from Trisfald/objects
Browse files Browse the repository at this point in the history
Objects
  • Loading branch information
Trisfald authored Feb 21, 2020
2 parents 8691e19 + 5b68cd8 commit dae5122
Show file tree
Hide file tree
Showing 25 changed files with 1,260 additions and 125 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Doc tests for all events and few other structs.
- `Originated` decorator.
- Introduced inanimate objects.
- Improved public API for `Battle` and its submodules.

### Changed
- It's now possible to manually set an event's origin.
- New associated type `ObjectId` in `CharacterRules`.

## [0.3.1] - 2020-02-17
### Added
Expand Down
2 changes: 2 additions & 0 deletions examples/initiative/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pub struct CustomCharacterRules {}
impl CharacterRules<CustomRules> for CustomCharacterRules {
// Just use an integer as creature id.
type CreatureId = u8;
// Same for objects.
type ObjectId = u8;
// Use statistics with integers as both id and value.
type Statistic = SimpleStatistic<u8, u16>;
// The seed will contain the value of speed.
Expand Down
2 changes: 2 additions & 0 deletions examples/pirates/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ pub struct PiratesCharacterRules {}
impl CharacterRules<PiratesRules> for PiratesCharacterRules {
// We want an integer as creature id.
type CreatureId = u8;
// No inanimate objects in this game.
type ObjectId = ();
// Use statistics with integer as id and as value.
type Statistic = SimpleStatistic<u8, i16>;
// No need for a seed. All ships have the same statistics.
Expand Down
28 changes: 24 additions & 4 deletions src/battle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,16 @@ impl<R: BattleRules + 'static> Battle<R> {
self.state.phase
}

/// Returns the entities manager for this battle.
/// Returns a reference to the entities manager for this battle.
pub fn entities(&self) -> &Entities<R> {
&self.state.entities
}

/// Returns a mutable reference to the entities manager for this battle.
pub fn entities_mut(&mut self) -> &mut Entities<R> {
&mut self.state.entities
}

/// Returns the history of this battle.
pub fn history(&self) -> &History<R> {
&self.history
Expand All @@ -158,21 +163,36 @@ impl<R: BattleRules + 'static> Battle<R> {
&self.rules
}

/// Returns this battle's space representation.
/// Returns a reference to this battle's space representation.
pub fn space(&self) -> &Space<R> {
&self.state.space
}

/// Returns the entropy manager for this battle.
/// Returns a mutable reference to this battle's space representation.
pub fn space_mut(&mut self) -> &mut Space<R> {
&mut self.state.space
}

/// Returns a reference to the entropy manager for this battle.
pub fn entropy(&self) -> &Entropy<R> {
&self.entropy
}

/// Returns the rounds manager for this battle.
/// Returns a mutable reference to the entropy manager for this battle.
pub fn entropy_mut(&mut self) -> &mut Entropy<R> {
&mut self.entropy
}

/// Returns a reference to the rounds manager for this battle.
pub fn rounds(&self) -> &Rounds<R> {
&self.state.rounds
}

/// Returns a mutable reference to the rounds manager for this battle.
pub fn rounds_mut(&mut self) -> &mut Rounds<R> {
&mut self.state.rounds
}

/// Returns a handle from which metrics can be read.
pub fn metrics(&self) -> ReadMetrics<R> {
self.metrics.read_handle()
Expand Down
7 changes: 7 additions & 0 deletions src/character.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ pub trait CharacterRules<R: BattleRules> {
/// See [CreatureId](../creature/type.CreatureId.html).
type CreatureId: Hash + Eq + Clone + Debug + Serialize + for<'a> Deserialize<'a>;

#[cfg(not(feature = "serialization"))]
/// See [ObjectId](../object/type.ObjectId.html).
type ObjectId: Hash + Eq + Clone + Debug;
#[cfg(feature = "serialization")]
/// See [ObjectId](../object/type.ObjectId.html).
type ObjectId: Hash + Eq + Clone + Debug + Serialize + for<'a> Deserialize<'a>;

/// See [Statistic](type.Statistic.html).
type Statistic: Id + 'static;

Expand Down
45 changes: 10 additions & 35 deletions src/creature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ use crate::metric::system::*;
use crate::round::RoundState;
use crate::space::{Position, PositionClaim};
use crate::team::{EntityAddition, TeamId, TeamRules};
use crate::util::Id;
use crate::util::{collect_from_iter, Id};
#[cfg(feature = "serialization")]
use serde::{Deserialize, Serialize};
use std::any::Any;
use std::collections::hash_map::Values;
use std::collections::HashMap;
use std::fmt::{Debug, Formatter, Result};

Expand Down Expand Up @@ -45,16 +44,6 @@ pub struct Creature<R: BattleRules> {
}

impl<R: BattleRules> Creature<R> {
/// Returns an iterator over abilities.
pub fn abilities(&self) -> Values<AbilityId<R>, Ability<R>> {
self.abilities.values()
}

/// Returns the ability with the given id.
pub fn ability(&self, id: &AbilityId<R>) -> Option<&Ability<R>> {
self.abilities.get(id)
}

pub(crate) fn set_team_id(&mut self, id: TeamId<R>) {
self.team_id = id;
}
Expand All @@ -64,8 +53,11 @@ impl<R: BattleRules> Id for Creature<R> {
type Id = CreatureId<R>;

fn id(&self) -> &CreatureId<R> {
let EntityId::Creature(id) = &self.id;
id
if let EntityId::Creature(id) = &self.id {
id
} else {
panic!("constraint violated: creature's id has a wrong type")
}
}
}

Expand Down Expand Up @@ -228,24 +220,6 @@ impl<R: BattleRules> Clone for CreateCreature<R> {
}

impl<R: BattleRules> CreateCreature<R> {
/// Collects an iterator into an hashmap.
/// Subsequent values with same key are ignored.
fn collect_from_iter<I>(
it: I,
) -> HashMap<<<I as Iterator>::Item as Id>::Id, <I as Iterator>::Item>
where
I: Iterator,
<I as Iterator>::Item: Id,
{
let mut map = HashMap::new();
for e in it {
if !map.contains_key(e.id()) {
map.insert(e.id().clone(), e);
}
}
map
}

/// Returns a trigger for this event.
pub fn trigger<'a, P: EventProcessor<R>>(
processor: &'a mut P,
Expand Down Expand Up @@ -324,14 +298,14 @@ impl<R: BattleRules + 'static> Event<R> for CreateCreature<R> {
&mut battle.entropy,
&mut battle.metrics.write_handle(),
);
let statistics = CreateCreature::<R>::collect_from_iter(it);
let statistics = collect_from_iter(it);
// Abilities' generation is influenced by the given abilities_seed, if present.
let it = battle.rules.actor_rules().generate_abilities(
&self.abilities_seed,
&mut battle.entropy,
&mut battle.metrics.write_handle(),
);
let abilities = CreateCreature::<R>::collect_from_iter(it);
let abilities = collect_from_iter(it);
// Create the creature.
let creature = Creature {
id: EntityId::Creature(self.id.clone()),
Expand All @@ -352,7 +326,7 @@ impl<R: BattleRules + 'static> Event<R> for CreateCreature<R> {
&mut battle.entropy,
&mut battle.metrics.write_handle(),
);
// Add the creature to the actors.
// Add the creature to the entities.
battle
.state
.entities
Expand Down Expand Up @@ -804,6 +778,7 @@ mod tests {

impl<R: BattleRules> CharacterRules<R> for CustomCharacterRules {
type CreatureId = u32;
type ObjectId = ();
type Statistic = SimpleStatistic<u32, u32>;
type StatisticsSeed = ();
type StatisticsAlteration = ();
Expand Down
Loading

0 comments on commit dae5122

Please sign in to comment.