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
|
/*
Hurry Curry! - a game about cooking
Copyright 2024 metamuffin
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use super::{Entity, EntityContext};
use anyhow::Result;
use hurrycurry_bot::{BotAlgo, DynBotAlgo};
use hurrycurry_protocol::{PacketS, PlayerID};
use log::info;
use rand::random;
use std::any::Any;
pub type DynBotDriver = BotDriver<DynBotAlgo>;
pub struct BotDriver<T> {
algo: T,
join_data: Option<(String, i32)>,
id: PlayerID,
interacting: bool,
left: bool,
}
impl<T: BotAlgo> BotDriver<T> {
pub fn new(name: String, character: i32, algo: T) -> Self {
Self {
algo,
id: PlayerID(0),
interacting: false,
join_data: Some((name, character)),
left: false,
}
}
}
impl<T: BotAlgo + Any> Entity for BotDriver<T> {
fn finished(&self) -> bool {
self.left
}
fn tick(&mut self, c: EntityContext<'_>) -> Result<()> {
if let Some((name, character)) = self.join_data.take() {
self.id = PlayerID(random()); // TODO bad code, can collide
info!("spawn {:?} ({name:?}, {character})", self.id);
c.packet_in.push_back(PacketS::Join {
name,
character,
id: Some(self.id),
})
}
let input = self.algo.tick(self.id, c.game, c.dt);
if input.leave {
info!("leave {:?}", self.id);
c.packet_in.push_back(PacketS::Leave { player: self.id });
self.left = true;
return Ok(());
}
if input.interact.is_some() != self.interacting {
self.interacting = input.interact.is_some();
c.packet_in.push_back(PacketS::Interact {
player: self.id,
pos: input.interact,
})
}
c.packet_in.push_back(PacketS::Movement {
player: self.id,
dir: input.direction,
boost: input.boost,
pos: None,
});
c.packet_in.extend(input.extra);
Ok(())
}
fn destructor(&mut self, c: EntityContext<'_>) {
if self.join_data.is_none() && !self.left {
c.packet_in.push_back(PacketS::Leave { player: self.id })
}
}
}
|