This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
units.rs
192 lines (175 loc) · 5.87 KB
/
units.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
//! Units are organisms that can move freely.
use crate::curves::{BottomClampedLine, Mapping, Sigmoid};
use crate::organisms::pathfinding::get_weighted_random_passable_neighbor;
use crate::organisms::{OrganismBundle, OrganismType};
use crate::signals::emitters::{Emitter, StockEmitter};
use crate::signals::tile_signals::TileSignals;
use crate::terrain::generation::GenerationConfig;
use crate::terrain::ImpassableTerrain;
use crate::tiles::organisms::{OrganismStorage, OrganismStorageItem};
use crate::tiles::terrain::{TerrainStorage, TerrainStorageItem};
use crate::tiles::IntoTileBundle;
use bevy::prelude::*;
use bevy_ecs_tilemap::map::{TilemapId, TilemapSize};
use bevy_ecs_tilemap::prelude::TileBundle;
use bevy_ecs_tilemap::tiles::TilePos;
/// Marker component for [`UnitBundle`]
#[derive(Component, Clone, Default)]
pub struct Unit;
/// An organism that can move around freely.
#[derive(Bundle, Default)]
pub struct UnitBundle {
/// Marker component.
unit: Unit,
/// A unit is an organism.
#[bundle]
organism_bundle: OrganismBundle,
}
/// Marker component for worker ants
#[derive(Component, Clone, Default)]
pub struct Ant;
/// A worker ant
#[derive(Bundle, Default)]
pub struct AntBundle {
/// Marker struct.
ant: Ant,
/// Ants are units.
#[bundle]
unit_bundle: UnitBundle,
/// Data needed to visualize the ant.
#[bundle]
tile_bundle: TileBundle,
}
impl AntBundle {
/// Creates a new [`AntBundle`]
pub fn new(tilemap_id: TilemapId, position: TilePos) -> Self {
Self {
unit_bundle: UnitBundle {
organism_bundle: OrganismBundle {
..Default::default()
},
..Default::default()
},
tile_bundle: OrganismType::Ant.as_tile_bundle(tilemap_id, position),
..Default::default()
}
}
}
/// Contains unit behavior
pub struct UnitsPlugin;
impl Plugin for UnitsPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(UnitTimer(Timer::from_seconds(0.5, true)))
.insert_resource(PheromoneTransducer::<BottomClampedLine>::default())
.add_system(act);
}
}
/// Global timer that controls when units should act
struct UnitTimer(Timer);
/// System modelling ant behaviour.
#[allow(clippy::too_many_arguments)]
fn act(
time: Res<Time>,
mut timer: ResMut<UnitTimer>,
generation_config: Res<GenerationConfig>,
mut query: Query<(&Unit, &mut TilePos)>,
impassable_query: Query<&ImpassableTerrain>,
terrain_storage_query: Query<TerrainStorage>,
organism_storage_query: Query<OrganismStorage>,
tile_signals_query: Query<&TileSignals>,
pheromone_sensor: Res<PheromoneTransducer<BottomClampedLine>>,
) {
let terrain_tile_storage = terrain_storage_query.single();
let organism_tile_storage = organism_storage_query.single();
timer.0.tick(time.delta());
if timer.0.finished() {
for (_, mut position) in query.iter_mut() {
*position = wander(
&position,
&terrain_tile_storage,
&organism_tile_storage,
&impassable_query,
&tile_signals_query,
&pheromone_sensor,
&generation_config.map_size,
);
}
}
}
/// Transduces a pheromone signal into a weight used to make decisions.
///
/// The transduction is modelled by mapping the signal to a weight using a curve.
pub struct PheromoneTransducer<C: Mapping> {
/// Curve used to model transduction.
curve: C,
}
impl PheromoneTransducer<Sigmoid> {
/// Creates a [`Sigmoid`]-based transducer.
pub fn new(
min: f32,
max: f32,
first_percentile: f32,
last_percentile: f32,
) -> PheromoneTransducer<Sigmoid> {
PheromoneTransducer {
curve: Sigmoid::new(min, max, first_percentile, last_percentile),
}
}
/// Transduce a signal into a weight used for decision making.
pub fn signal_to_weight(&self, attraction: f32, repulsion: f32) -> f32 {
1.0 + self.curve.map(attraction) - self.curve.map(repulsion)
}
}
impl Default for PheromoneTransducer<Sigmoid> {
fn default() -> Self {
PheromoneTransducer {
curve: Sigmoid::new(0.0, 0.1, 0.01, 0.09),
}
}
}
impl PheromoneTransducer<BottomClampedLine> {
/// Creates a [`BottomClampedLine`]-based transducer.
pub fn new(p0: Vec2, p1: Vec2) -> PheromoneTransducer<BottomClampedLine> {
PheromoneTransducer {
curve: BottomClampedLine::new_from_points(p0, p1),
}
}
/// Transduce a signal into a weight used for decision making.
pub fn signal_to_weight(&self, attraction: f32, repulsion: f32) -> f32 {
1.0 + self.curve.map(attraction) - self.curve.map(repulsion)
}
}
impl Default for PheromoneTransducer<BottomClampedLine> {
fn default() -> Self {
PheromoneTransducer {
curve: BottomClampedLine::new_from_points(Vec2::new(0.0, 0.0), Vec2::new(0.01, 1.0)),
}
}
}
/// Pathfinding for ants.
fn wander(
position: &TilePos,
terrain_tile_storage: &TerrainStorageItem,
organism_tile_storage: &OrganismStorageItem,
impassable_query: &Query<&ImpassableTerrain>,
tile_signals_query: &Query<&TileSignals>,
pheromone_sensor: &PheromoneTransducer<BottomClampedLine>,
map_size: &TilemapSize,
) -> TilePos {
let signals_to_weight = |tile_signals: &TileSignals| {
pheromone_sensor.signal_to_weight(
tile_signals.get(&Emitter::Stock(StockEmitter::PheromoneAttract)),
tile_signals.get(&Emitter::Stock(StockEmitter::PheromoneRepulse)),
)
};
let target = get_weighted_random_passable_neighbor(
position,
terrain_tile_storage,
organism_tile_storage,
impassable_query,
tile_signals_query,
signals_to_weight,
map_size,
);
target.unwrap_or(*position)
}