-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdomain.rs
116 lines (104 loc) · 3.57 KB
/
domain.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
/*
* SPDX-License-Identifier: Apache-2.0 OR MIT
* © 2020-2022 ETH Zurich and other contributors, see AUTHORS.txt for details
*/
use std::{collections::BTreeSet, fmt};
use npc_engine_core::{AgentId, AgentValue, Behavior, Context, Domain, StateDiffRef};
use npc_engine_utils::OptionDiffDomain;
use num_traits::Zero;
use crate::{
behavior::{
agent::AgentBehavior,
world::{WorldBehavior, WORLD_AGENT_ID},
},
constants::MAP,
map::Location,
state::{Diff, State},
};
pub enum DisplayAction {
Wait,
Pick,
Shoot(AgentId),
StartCapturing(Location),
Capturing(Location),
StartMoving(Location),
Moving(Location),
WorldStep,
}
impl Default for DisplayAction {
fn default() -> Self {
Self::Wait
}
}
impl fmt::Debug for DisplayAction {
fn fmt(&self, f: &'_ mut fmt::Formatter) -> fmt::Result {
match &self {
Self::Wait => f.write_str("Wait"),
Self::Pick => f.write_str("Pick"),
Self::Shoot(target) => f.write_fmt(format_args!("Shoot {:?}", target)),
Self::StartCapturing(loc) => f.write_fmt(format_args!("StartCapturing {:?}", loc)),
Self::Capturing(loc) => f.write_fmt(format_args!("Capturing {:?}", loc)),
Self::StartMoving(loc) => f.write_fmt(format_args!("StartMoving {:?}", loc)),
Self::Moving(loc) => f.write_fmt(format_args!("Moving {:?}", loc)),
Self::WorldStep => f.write_str("WorldStep"),
}
}
}
pub struct CaptureDomain;
impl Domain for CaptureDomain {
type State = State;
type Diff = Diff;
type DisplayAction = DisplayAction;
fn list_behaviors() -> &'static [&'static dyn Behavior<Self>] {
&[&AgentBehavior, &WorldBehavior]
}
fn get_current_value(_tick: u64, state_diff: StateDiffRef<Self>, agent: AgentId) -> AgentValue {
let state = Self::get_cur_state(state_diff);
state
.agents
.get(&agent)
.map_or(AgentValue::zero(), |agent_state| {
AgentValue::from(agent_state.acc_capture)
})
}
fn update_visible_agents(_start_tick: u64, ctx: Context<Self>, agents: &mut BTreeSet<AgentId>) {
let state = Self::get_cur_state(ctx.state_diff);
agents.clear();
agents.extend(state.agents.keys());
agents.insert(WORLD_AGENT_ID);
}
fn get_state_description(state_diff: StateDiffRef<Self>) -> String {
let state = Self::get_cur_state(state_diff);
let mut s = format!(
"World: ❤️ {} ({}), • {} ({}), ⚡: ",
state.medkit, state.medkit_tick, state.ammo, state.ammo_tick
);
s += &(0..MAP.capture_locations_count())
.map(|index| format!("{:?}", state.capture_points[index as usize]))
.collect::<Vec<_>>()
.join(" ");
for (id, state) in &state.agents {
if let Some(target) = state.next_location {
s += &format!(
"\nA{} in {}-{}, ❤️ {}, • {}, ⚡{}",
id.0,
state.cur_or_last_location.get(),
target.get(),
state.hp,
state.ammo,
state.acc_capture
);
} else {
s += &format!(
"\nA{} @ {}, ❤️ {}, • {}, ⚡{}",
id.0,
state.cur_or_last_location.get(),
state.hp,
state.ammo,
state.acc_capture
);
}
}
s
}
}