Skip to content

Commit

Permalink
Fix bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
InfinityDevTech committed Oct 10, 2024
1 parent 8f8d1a7 commit cebef8b
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use rand::{rngs::StdRng, Rng, SeedableRng};
use room::{
cache::{hauling, traffic, RoomCache}, democracy::start_government, expansion::{attempt_expansion, can_expand}, spawning::spawn_manager::{self, run_spawning, SpawnManager}, visuals::visualise_scouted_rooms
};
use screeps::{find, game, OwnedStructureProperties};
use screeps::{find, game::{self, market::Player}, ObjectId, OwnedStructureProperties};
use traits::{creep::CreepExtensions, intents_tracking::{
ConstructionExtensionsTracking, CreepExtensionsTracking, StructureControllerExtensionsTracking,
StructureObjectTracking,
Expand Down
171 changes: 138 additions & 33 deletions src/room/creeps/combat/bulldozer.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
use log::info;
use rand::{rngs::StdRng, Rng, SeedableRng};
use screeps::{find, game, Color, Creep, HasPosition, SharedCreepProperties, StructureObject, StructureProperties, StructureType};
use screeps::{
find, game, Color, Creep, HasPosition, SharedCreepProperties, StructureObject,
StructureProperties, StructureType,
};

use crate::{
config, memory::{Role, ScreepsMemory}, movement::move_target::MoveOptions, room::cache::RoomCache, traits::{creep::CreepExtensions, intents_tracking::CreepExtensionsTracking}
config,
memory::{Role, ScreepsMemory},
movement::move_target::MoveOptions,
room::cache::RoomCache,
traits::{creep::CreepExtensions, intents_tracking::CreepExtensionsTracking},
};

#[cfg_attr(feature = "profile", screeps_timing_annotate::timing)]
Expand All @@ -15,29 +22,43 @@ pub fn run_bulldozer(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut Room
}

let owned = room_cache.creeps.owned_creeps.clone();
let mut nearby_creeps = owned.values().filter(|c| c.pos().get_range_to(creep.pos()) <= 3).collect::<Vec<&Creep>>();
let mut nearby_creeps = owned
.values()
.filter(|c| c.pos().get_range_to(creep.pos()) <= 3)
.collect::<Vec<&Creep>>();
// sort by lowest to highest hits
nearby_creeps.sort_by_key(|c| c.hits());

let health_percent = if !nearby_creeps.is_empty() {
let nearby_creep = nearby_creeps.first().unwrap();

nearby_creep.hits() as f32 / nearby_creep.hits_max() as f32 * 100.0
} else {
100.0
};

let my_health_percent = creep.hits() as f32 / creep.hits_max() as f32 * 100.0;

info!("{}: Health: {}% My Health: {}%", creep.name(), health_percent, my_health_percent);
info!(
"{}: Health: {}% My Health: {}%",
creep.name(),
health_percent,
my_health_percent
);

if (health_percent < 100.0 || my_health_percent < 100.0) && my_health_percent > health_percent {
if let Some(creep) = nearby_creeps.first() {
if creep.pos().is_near_to(creep.pos()) {
//let _ = creep.ITheal(*creep);
} else {
let _ = creep.ITranged_heal(*creep);
creep.better_move_to(memory, room_cache, creep.pos(), 1, MoveOptions::default().avoid_enemies(true).path_age(1));
creep.better_move_to(
memory,
room_cache,
creep.pos(),
1,
MoveOptions::default().avoid_enemies(true).path_age(1),
);
}
}
} else if my_health_percent < 100.0 {
Expand All @@ -52,18 +73,26 @@ pub fn run_bulldozer(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut Room
let mut structure = creep.room().unwrap().find(find::HOSTILE_STRUCTURES, None);
structure.retain(|x| x.structure_type() != StructureType::Controller);
let ramparts = creep.room().unwrap().find(find::STRUCTURES, None);
let ramparts = ramparts.iter().filter(|s| s.structure_type() == StructureType::Rampart).collect::<Vec<&StructureObject>>();
let ramparts = ramparts
.iter()
.filter(|s| s.structure_type() == StructureType::Rampart)
.collect::<Vec<&StructureObject>>();

let creep_memory = creep_memory.unwrap();

if let Some(flag) = game::flags().get("bulldozeRoom".to_string()) {
if creep.room().unwrap().name() == flag.pos().room_name() {

if flag.color() == Color::Blue {
if creep.pos().is_near_to(flag.pos()) {
creep.bsay("👁️", true);
} else {
creep.better_move_to(memory, room_cache, flag.pos(), 1, MoveOptions::default().avoid_enemies(true).path_age(1));
creep.better_move_to(
memory,
room_cache,
flag.pos(),
1,
MoveOptions::default().avoid_enemies(true).path_age(1),
);
}
return;
}
Expand All @@ -73,7 +102,13 @@ pub fn run_bulldozer(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut Room
creep.bsay("JK - <3 U", true);
} else {
creep.bsay("DIE DIE DIE", true);
creep.better_move_to(memory, room_cache, flag.pos(), 1, MoveOptions::default().path_age(1));
creep.better_move_to(
memory,
room_cache,
flag.pos(),
1,
MoveOptions::default().path_age(1),
);
}
return;
}
Expand All @@ -87,40 +122,71 @@ pub fn run_bulldozer(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut Room
return false;
}
}

true
});

if let Some(enemy) = enemies.first() {
if creep.ITattack(enemy) == Err(screeps::ErrorCode::NotInRange) {
creep.better_move_to(memory, room_cache, enemy.pos(), 1, MoveOptions::default().path_age(1));
creep.better_move_to(
memory,
room_cache,
enemy.pos(),
1,
MoveOptions::default().path_age(1),
);
}
}

return;
}

if let Some(structures_at_pos) = room_cache.structures.structures_at_pos.get(&flag.pos().xy()) {
if let Some(structures_at_pos) = room_cache
.structures
.structures_at_pos
.get(&flag.pos().xy())
{
if !structures_at_pos.is_empty() {
let t = structure.iter().filter(|s| s.pos() == flag.pos() && s.structure_type() == *structures_at_pos.first().unwrap()).collect::<Vec<&StructureObject>>();
let structure = t.first().unwrap();

if creep.pos().is_near_to(structure.pos()) {
let _ = creep.attack(structure.as_attackable().unwrap());
return;
} else {
creep.better_move_to(memory, room_cache, structure.pos(), 1, MoveOptions::default().path_age(1));
return;
let t = structure
.iter()
.filter(|s| {
s.pos() == flag.pos()
&& s.structure_type() == *structures_at_pos.first().unwrap()
})
.collect::<Vec<&StructureObject>>();
if let Some(structure) = t.first() {
if creep.pos().is_near_to(structure.pos()) {
let _ = creep.attack(structure.as_attackable().unwrap());
return;
} else {
creep.better_move_to(
memory,
room_cache,
structure.pos(),
1,
MoveOptions::default().path_age(1),
);
return;
}
}
}
}

if let Some(spawn) = structure.iter().find(|s| s.structure_type() == StructureType::Spawn) {
if let Some(spawn) = structure
.iter()
.find(|s| s.structure_type() == StructureType::Spawn)
{
if creep.pos().is_near_to(spawn.pos()) {
let _ = creep.attack(spawn.as_attackable().unwrap());
return;
} else {
creep.better_move_to(memory, room_cache, spawn.pos(), 1, MoveOptions::default().path_age(1));
creep.better_move_to(
memory,
room_cache,
spawn.pos(),
1,
MoveOptions::default().path_age(1),
);
return;
}
}
Expand All @@ -143,10 +209,17 @@ pub fn run_bulldozer(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut Room

if let Some(enemy) = enemies.first() {
if creep.ITattack(enemy) == Err(screeps::ErrorCode::NotInRange) {
creep.better_move_to(memory, room_cache, enemy.pos(), 1, MoveOptions::default().path_age(1));
creep.better_move_to(
memory,
room_cache,
enemy.pos(),
1,
MoveOptions::default().path_age(1),
);
}
} else {
structure.retain(| structure | structure.structure_type() != StructureType::Controller);
structure
.retain(|structure| structure.structure_type() != StructureType::Controller);
structure.sort_by_key(|structure| structure.pos().get_range_to(creep.pos()));
let structure = structure.first();
if let Some(structure) = structure {
Expand All @@ -155,12 +228,23 @@ pub fn run_bulldozer(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut Room
let _ = creep.ITattack(attackabke);
}
} else {
creep.better_move_to(memory, room_cache, structure.pos(), 1, MoveOptions::default().path_age(1));
creep.better_move_to(
memory,
room_cache,
structure.pos(),
1,
MoveOptions::default().path_age(1),
);
}
} else {
let mut structures = creep.room().unwrap().find(find::STRUCTURES, None);
structures.retain(| structure | structure.structure_type() != StructureType::Controller);
structures.retain(| structure | structure.structure_type() != StructureType::Controller || structure.structure_type() != StructureType::Rampart);
structures.retain(|structure| {
structure.structure_type() != StructureType::Controller
});
structures.retain(|structure| {
structure.structure_type() != StructureType::Controller
|| structure.structure_type() != StructureType::Rampart
});
structures.sort_by_key(|structure| structure.pos().get_range_to(creep.pos()));

structures.retain(|c| {
Expand All @@ -180,11 +264,23 @@ pub fn run_bulldozer(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut Room
let _ = creep.ITattack(attackabke);
}
} else {
creep.better_move_to(memory, room_cache, structure.pos(), 1, MoveOptions::default().path_age(1));
creep.better_move_to(
memory,
room_cache,
structure.pos(),
1,
MoveOptions::default().path_age(1),
);
}
} else {
creep.bsay("🚚", false);
creep.better_move_to(memory, room_cache, flag.pos(), 2, MoveOptions::default().avoid_enemies(true));
creep.better_move_to(
memory,
room_cache,
flag.pos(),
2,
MoveOptions::default().avoid_enemies(true),
);
//creep_memory.role = Role::Recycler;
}
}
Expand All @@ -196,7 +292,16 @@ pub fn run_bulldozer(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut Room
creep_memory.role = Role::Recycler;
}

creep.better_move_to(memory, room_cache, flag.pos(), 2, MoveOptions::default().avoid_enemies(true).path_age(15).force_find_route(true));
creep.better_move_to(
memory,
room_cache,
flag.pos(),
2,
MoveOptions::default()
.avoid_enemies(true)
.path_age(15)
.force_find_route(true),
);
}
} else {
creep.bsay("❓", false);
Expand Down

0 comments on commit cebef8b

Please sign in to comment.