aboutsummaryrefslogtreecommitdiff
path: root/bot
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2023-02-04 19:32:27 -0600
committerGitHub <noreply@github.com>2023-02-04 19:32:27 -0600
commita5672815ccef520b433363ac622dbb6d6af60c91 (patch)
treef9bb1b41876d81423ac3f188f4d368e6d362eed1 /bot
parent7c7446ab1e467c29f86e9bfba260741fc469389a (diff)
downloadazalea-drasl-a5672815ccef520b433363ac622dbb6d6af60c91.tar.xz
Use an ECS (#52)
* add EntityData::kind * start making metadata use hecs * make entity codegen generate ecs stuff * fix registry codegen * get rid of worldhaver it's not even used * add bevy_ecs to deps * rename Component to FormattedText also start making the metadata use bevy_ecs but bevy_ecs doesn't let you query on Bundles so it's annoying * generate metadata.rs correctly for bevy_ecs * start switching more entity stuff to use ecs * more ecs stuff for entity storage * ok well it compiles but it definitely doesn't work * random fixes * change a bunch of entity things to use the components * some ecs stuff in az-client * packet handler uses the ecs now and other fun changes i still need to make ticking use the ecs but that's tricker, i'm considering using bevy_ecs systems for those bevy_ecs systems can't be async but the only async things in ticking is just sending packets which can just be done as a tokio task so that's not a big deal * start converting some functions in az-client into systems committing because i'm about to try something that might go horribly wrong * start splitting client i'm probably gonna change it so azalea entity ids are separate from minecraft entity ids next (so stuff like player ids can be consistent and we don't have to wait for the login packet) * separate minecraft entity ids from azalea entity ids + more ecs stuff i guess i'm using bevy_app now too huh it's necessary for plugins and it lets us control the tick rate anyways so it's fine i think i'm still not 100% sure how packet handling that interacts with the world will work, but i think if i can sneak the ecs world into there it'll be fine. Can't put packet handling in the schedule because that'd make it tick-bound, which it's not (technically it'd still work but it'd be wrong and anticheats might realize). * packet handling now it runs the schedule only when we get a tick or packet :smile: also i systemified some more functions and did other random fixes so az-world and az-physics compile making azalea-client use the ecs is almost done! all the hard parts are done now i hope, i just have to finish writing all the code so it actually works * start figuring out how functions in Client will work generally just lifetimes being annoying but i think i can get it all to work * make writing packets work synchronously* * huh az-client compiles * start fixing stuff * start fixing some packets * make packet handler work i still haven't actually tested any of this yet lol but in theory it should all work i'll probably either actually test az-client and fix all the remaining issues or update the azalea crate next ok also one thing that i'm not particularly happy with is how the packet handlers are doing ugly queries like ```rs let local_player = ecs .query::<&LocalPlayer>() .get_mut(ecs, player_entity) .unwrap(); ``` i think the right way to solve it would be by putting every packet handler in its own system but i haven't come up with a way to make that not be really annoying yet * fix warnings * ok what if i just have a bunch of queries and a single packet handler system * simple example for azalea-client * :bug: * maybe fix deadlock idk can't test it rn lmao * make physicsstate its own component * use the default plugins * azalea compiles lol * use systemstate for packet handler * fix entities basically moved some stuff from being in the world to just being components * physics (ticking) works * try to add a .entity_by function still doesn't work because i want to make the predicate magic * try to make entity_by work well it does work but i couldn't figure out how to make it look not terrible. Will hopefully change in the future * everything compiles * start converting swarm to use builder * continue switching swarm to builder and fix stuff * make swarm use builder still have to fix some stuff and make client use builder * fix death event * client builder * fix some warnings * document plugins a bit * start trying to fix tests * azalea-ecs * azalea-ecs stuff compiles * az-physics tests pass :tada: * fix all the tests * clippy on azalea-ecs-macros * remove now-unnecessary trait_upcasting feature * fix some clippy::pedantic warnings lol * why did cargo fmt not remove the trailing spaces * FIX ALL THE THINGS * when i said 'all' i meant non-swarm bugs * start adding task pool * fix entity deduplication * fix pathfinder not stopping * fix some more random bugs * fix panic that sometimes happens in swarms * make pathfinder run in task * fix some tests * fix doctests and clippy * deadlock * fix systems running in wrong order * fix non-swarm bots
Diffstat (limited to 'bot')
-rw-r--r--bot/src/main.rs133
1 files changed, 83 insertions, 50 deletions
diff --git a/bot/src/main.rs b/bot/src/main.rs
index 1bd407e8..f748de0b 100644
--- a/bot/src/main.rs
+++ b/bot/src/main.rs
@@ -1,14 +1,19 @@
+#![feature(type_alias_impl_trait)]
+
+use azalea::ecs::query::With;
+use azalea::entity::metadata::Player;
+use azalea::entity::Position;
use azalea::pathfinder::BlockPosGoal;
// use azalea::ClientInformation;
-use azalea::{prelude::*, BlockPos, Swarm, SwarmEvent, WalkDirection};
+use azalea::{prelude::*, BlockPos, GameProfileComponent, Swarm, SwarmEvent, WalkDirection};
use azalea::{Account, Client, Event};
use azalea_protocol::packets::game::serverbound_client_command_packet::ServerboundClientCommandPacket;
use std::time::Duration;
-#[derive(Default, Clone)]
+#[derive(Default, Clone, Component)]
struct State {}
-#[derive(Default, Clone)]
+#[derive(Default, Clone, Component)]
struct SwarmState {}
#[tokio::main]
@@ -48,23 +53,17 @@ async fn main() -> anyhow::Result<()> {
}
loop {
- let e = azalea::start_swarm(azalea::SwarmOptions {
- accounts: accounts.clone(),
- address: "localhost",
-
- states: states.clone(),
- swarm_state: SwarmState::default(),
-
- plugins: plugins![],
- swarm_plugins: swarm_plugins![],
-
- handle,
- swarm_handle,
-
- join_delay: Some(Duration::from_millis(1000)),
- // join_delay: None,
- })
- .await;
+ let e = azalea::SwarmBuilder::new()
+ .add_accounts(accounts.clone())
+ .set_handler(handle)
+ .set_swarm_handler(swarm_handle)
+ .join_delay(Duration::from_millis(1000))
+ .start("localhost")
+ .await;
+ // let e = azalea::ClientBuilder::new()
+ // .set_handler(handle)
+ // .start(Account::offline("bot"), "localhost")
+ // .await;
println!("{e:?}");
}
}
@@ -72,6 +71,7 @@ async fn main() -> anyhow::Result<()> {
async fn handle(mut bot: Client, event: Event, _state: State) -> anyhow::Result<()> {
match event {
Event::Init => {
+ println!("bot init");
// bot.set_client_information(ClientInformation {
// view_distance: 2,
// ..Default::default()
@@ -79,43 +79,76 @@ async fn handle(mut bot: Client, event: Event, _state: State) -> anyhow::Result<
// .await?;
}
Event::Login => {
- bot.chat("Hello world").await?;
+ bot.chat("Hello world");
}
Event::Chat(m) => {
+ println!("client chat message: {}", m.content());
if m.content() == bot.profile.name {
- bot.chat("Bye").await?;
+ bot.chat("Bye");
tokio::time::sleep(Duration::from_millis(50)).await;
- bot.disconnect().await?;
+ bot.disconnect();
}
- let entity = bot
- .world()
- .entity_by_uuid(&uuid::uuid!("6536bfed-8695-48fd-83a1-ecd24cf2a0fd"));
+ let Some(sender) = m.username() else {
+ return Ok(())
+ };
+ // let mut ecs = bot.ecs.lock();
+ // let entity = bot
+ // .ecs
+ // .lock()
+ // .query::<&Player>()
+ // .iter(&mut ecs)
+ // .find(|e| e.name() == Some(sender));
+ // let entity = bot.entity_by::<With<Player>>(|name: &Name| name == sender);
+ let entity = bot.entity_by::<With<Player>, (&GameProfileComponent,)>(
+ |profile: &&GameProfileComponent| {
+ println!("entity {profile:?}");
+ profile.name == sender
+ },
+ );
+ println!("sender entity: {entity:?}");
if let Some(entity) = entity {
- if m.content() == "goto" {
- let target_pos_vec3 = entity.pos();
- let target_pos: BlockPos = target_pos_vec3.into();
- bot.goto(BlockPosGoal::from(target_pos));
- } else if m.content() == "look" {
- let target_pos_vec3 = entity.pos();
- let target_pos: BlockPos = target_pos_vec3.into();
- println!("target_pos: {target_pos:?}");
- bot.look_at(&target_pos.center());
- } else if m.content() == "jump" {
- bot.set_jumping(true);
- } else if m.content() == "walk" {
- bot.walk(WalkDirection::Forward);
- } else if m.content() == "stop" {
- bot.set_jumping(false);
- bot.walk(WalkDirection::None);
- } else if m.content() == "lag" {
- std::thread::sleep(Duration::from_millis(1000));
+ match m.content().as_str() {
+ "whereami" => {
+ let pos = bot.entity_component::<Position>(entity);
+ bot.chat(&format!("You're at {pos:?}",));
+ }
+ "whereareyou" => {
+ let pos = bot.component::<Position>();
+ bot.chat(&format!("I'm at {pos:?}",));
+ }
+ "goto" => {
+ let entity_pos = bot.entity_component::<Position>(entity);
+ let target_pos: BlockPos = entity_pos.into();
+ println!("going to {target_pos:?}");
+ bot.goto(BlockPosGoal::from(target_pos));
+ }
+ "look" => {
+ let entity_pos = bot.entity_component::<Position>(entity);
+ let target_pos: BlockPos = entity_pos.into();
+ println!("target_pos: {target_pos:?}");
+ bot.look_at(target_pos.center());
+ }
+ "jump" => {
+ bot.set_jumping(true);
+ }
+ "walk" => {
+ bot.walk(WalkDirection::Forward);
+ }
+ "stop" => {
+ bot.set_jumping(false);
+ bot.walk(WalkDirection::None);
+ }
+ "lag" => {
+ std::thread::sleep(Duration::from_millis(1000));
+ }
+ _ => {}
}
}
}
Event::Death(_) => {
bot.write_packet(ServerboundClientCommandPacket {
action: azalea_protocol::packets::game::serverbound_client_command_packet::Action::PerformRespawn,
- }.get()).await?;
+ }.get());
}
_ => {}
}
@@ -124,7 +157,7 @@ async fn handle(mut bot: Client, event: Event, _state: State) -> anyhow::Result<
}
async fn swarm_handle(
- mut swarm: Swarm<State>,
+ mut swarm: Swarm,
event: SwarmEvent,
_state: SwarmState,
) -> anyhow::Result<()> {
@@ -137,10 +170,10 @@ async fn swarm_handle(
SwarmEvent::Chat(m) => {
println!("swarm chat message: {}", m.message().to_ansi());
if m.message().to_string() == "<py5> world" {
- for (name, world) in &swarm.worlds.read().worlds {
+ for (name, world) in &swarm.world_container.read().worlds {
println!("world name: {name}");
if let Some(w) = world.upgrade() {
- for chunk_pos in w.chunk_storage.read().chunks.values() {
+ for chunk_pos in w.read().chunks.chunks.values() {
println!("chunk: {chunk_pos:?}");
}
} else {
@@ -149,8 +182,8 @@ async fn swarm_handle(
}
}
if m.message().to_string() == "<py5> hi" {
- for (bot, _) in swarm {
- bot.chat("hello").await?;
+ for bot in swarm {
+ bot.chat("hello");
}
}
}