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
|
use std::collections::HashSet;
use rand::Rng;
use specs::{
Entities,
ReadStorage,
WriteStorage,
System,
Join,
Read,
Write
};
use crate::{
components::{
Controller,
Position,
ControlCooldown,
Interactable,
TriggerBox,
Notification,
Ear,
Inventory,
Visible,
Player,
Whitelist,
Minable
},
controls::{Control},
resources::{Ground, NewEntities, Emigration},
hashmap,
playerstate::RoomPos,
PlayerId
};
pub struct Interact;
impl <'a> System<'a> for Interact {
type SystemData = (
Entities<'a>,
ReadStorage<'a, Controller>,
ReadStorage<'a, Position>,
Read<'a, Ground>,
WriteStorage<'a, ControlCooldown>,
ReadStorage<'a, Interactable>,
WriteStorage<'a, TriggerBox>,
Write<'a, NewEntities>,
WriteStorage<'a, Ear>,
WriteStorage<'a, Inventory>,
ReadStorage<'a, Visible>,
ReadStorage<'a, Player>,
Write<'a, Emigration>,
WriteStorage<'a, Whitelist>,
WriteStorage<'a, Minable>
);
fn run(&mut self, (entities, controllers, positions, ground, mut cooldowns, interactables, mut triggerbox, new, mut ears, mut inventories, visibles, players, mut emigration, mut whitelists, mut minables): Self::SystemData) {
for (actor, controller, position) in (&entities, &controllers, &positions).join(){
let mut target = None;
let ear = ears.get_mut(actor);
match &controller.control {
Control::Interact(directions, arg) => {
'targets: for direction in directions {
let pos = position.pos + direction.to_position();
for ent in ground.cells.get(&pos).unwrap_or(&HashSet::new()) {
if let Some(interactable) = interactables.get(*ent) {
if interactable.accepts_arg(arg){
target = Some((*ent, interactable, arg.clone()));
break 'targets;
}
}
}
}
}
_ => {}
}
if let Some((ent, interactable, arg)) = target {
let mut cooldown = 2;
let name = visibles.get(ent).map(|v| v.name.as_str());
match interactable {
Interactable::Trigger(trigger) => {
TriggerBox::add_message(&mut triggerbox, ent, *trigger);
}
Interactable::Say(text) => {
say(ear, text.clone(), name);
}
Interactable::Reply(text) => {
say(ear, text.replace("{}", &arg.unwrap()), name);
}
Interactable::Exchange(prefix, exchanges) => {
if let Some(txt) = arg {
if let Some(inventory) = inventories.get_mut(actor) {
if txt.starts_with(prefix){
let action = txt.split_at(prefix.len()).1;
if let Some(exchange) = exchanges.get(action) {
if exchange.can_trade(inventory){
exchange.trade(inventory, &new.encyclopedia);
say(ear, format!("Success! '{}' ({})", txt, exchange.show()), name);
} else {
say(ear, format!("You do not have the required items or inventory space for '{}' ({})", txt, exchange.show()), name);
}
} else {
say(ear, format!("Invalid option: {}", action), name);
}
}
}
} else {
say(
ear,
format!("options: {:?}", exchanges.iter().map(|(id, exchange)|
format!("{}{}: {}", prefix, id, exchange.show())
).collect::<Vec<String>>()),
name
);
}
}
Interactable::Visit(dest) => {
if let (Some(player), Some(whitelist)) = (players.get(actor), whitelists.get_mut(ent)){
let argument = arg.unwrap();
if argument.starts_with("visit ") {
let playername = argument.split_at("visit ".len()).1;
let destination = dest.format(hashmap!("{player}" => playername));
if let Some(set) = whitelist.allowed.get(&destination.name) {
if set.contains(&player.id){
emigration.emigrants.push((player.id.clone(), destination, RoomPos::Unknown));
} else {
say(ear, format!("not allowed to visit {}", playername), name);
}
} else {
say(ear, format!("unknown destination {}", playername), name);
}
} else if argument.starts_with("allow ") {
let playername = argument.split_at("allow ".len()).1;
let destination = dest.format(hashmap!("{player}" => player.id.name.as_str()));
whitelist.allowed.entry(destination.name).or_insert_with(HashSet::new).insert(PlayerId{name: playername.to_string()});
say(ear, format!("allowed {} to enter your home", playername), name);
} else if argument.starts_with("disallow ") {
let playername = argument.split_at("disallow ".len()).1;
let destination = dest.format(hashmap!("{player}" => player.id.name.as_str()));
whitelist.allowed.entry(destination.name).or_insert_with(HashSet::new).remove(&PlayerId{name: playername.to_string()});
say(ear, format!("disallowed {} to enter your home", playername), name);
} else if argument.starts_with("whitelist") {
let destination = dest.format(hashmap!("{player}" => player.id.name.as_str()));
let allowed = whitelist.allowed.entry(destination.name).or_insert_with(HashSet::new).iter().map(|id| id.name.as_str()).collect::<Vec<&str>>();
say(ear, format!("allowed players: {}", allowed.join(", ")), name);
}
}
}
Interactable::Mine(skill) => {
if let (Some(inventory), Some(minable)) = (inventories.get(actor), minables.get_mut(ent)) {
let stats = inventory.equipment_bonuses();
if let Some(skill_value) = stats.get(skill) {
// todo: give player feedback
cooldown = 20;
minable.progress += rand::thread_rng().gen_range(0, skill_value+1);
if minable.progress >= minable.total {
TriggerBox::add_message(&mut triggerbox, ent, minable.trigger);
minable.progress = 0;
}
}
}
}
}
cooldowns.insert(actor, ControlCooldown{amount: cooldown}).unwrap();
}
}
}
}
fn say(maybe_ear: Option<&mut Ear>, text: String, source: Option<&str>){
if let Some(ear) = maybe_ear {
ear.sounds.push(Notification::Sound{text, source: source.map(|s| s.to_string())});
}
}
|