Skip to content

Commit

Permalink
Scout bug fixing. Changed how multiple rooms are handled.
Browse files Browse the repository at this point in the history
  • Loading branch information
InfinityDevTech committed Jun 6, 2024
1 parent 6e17581 commit 8261afe
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 51 deletions.
28 changes: 12 additions & 16 deletions src/combat/rank_room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,30 +73,26 @@ pub fn rank_room(room: &Room, memory: &mut ScreepsMemory, cache: &mut RoomCache)
}

if reserved.is_some() {
if memory
.enemy_players
.contains_key(&reserved.clone().unwrap().to_string())
{
let enemy_player = memory
.enemy_players
.get_mut(&owner.clone().unwrap().to_string())
.unwrap();

if !enemy_player.reserved_rooms.contains(&room_name) {
enemy_player.reserved_rooms.push(room_name);
}
} else {
if let std::collections::hash_map::Entry::Vacant(e) = memory
.enemy_players.entry(reserved.clone().unwrap().to_string()) {
let enemy = EnemyPlayer {
username: owner.clone().unwrap().to_string(),
username: reserved.clone().unwrap().to_string(),
hate: 0.0,
owned_rooms: vec![],
reserved_rooms: vec![room_name],
last_attack: 0,
};

memory
e.insert(enemy);
} else {
let enemy_player = memory
.enemy_players
.insert(owner.clone().unwrap().to_string(), enemy);
.get_mut(&owner.clone().unwrap().to_string())
.unwrap();

if !enemy_player.reserved_rooms.contains(&room_name) {
enemy_player.reserved_rooms.push(room_name);
}
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/room/cache/tick_cache/traffic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ pub fn run_movement(room_cache: &mut RoomCache) {

let mut visited_creeps = Vec::new();

info!(" {:?} - Running movement for creeps: {:?}", room_cache.room_name, creeps_with_movement_intent);

for creep_id in creeps_with_movement_intent {
let creep = game::get_object_by_id_typed(&creep_id).unwrap();
if room_cache.traffic.matched_coord.get(&creep_id) == room_cache.traffic.intended_move.get(&creep_id) {
Expand Down
12 changes: 9 additions & 3 deletions src/room/creeps/global/scout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCach

if creep.room().unwrap().name() == scout_target.room_name() {
creep_memory.scout_target = None;
run_creep(creep, memory, cache);
} else {
let _ = creep.say("🚚", false);
creep.better_move_to(
Expand All @@ -54,19 +55,24 @@ pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCach
}
}

let exit = if !exits.is_empty() {
let exit = if exit_clone.is_empty() {
let mut rng = StdRng::seed_from_u64(game::time() as u64);
exits.shuffle(&mut rng);
exits.first().unwrap()
} else {
exit_clone.choose(&mut StdRng::from_entropy()).unwrap()
let mut rng = StdRng::seed_from_u64(game::time() as u64);
exit_clone.choose(&mut rng).unwrap()
};

let pos = RoomPosition::new(25, 25, *exit);

let _ = creep.say(&format!("👁️ {}", pos.room_name()), true);

creep.better_move_to(
memory.creeps.get_mut(&creep.name()).unwrap(),
cache,
pos.pos(),
24,
23,
);
memory.creeps.get_mut(&creep.name()).unwrap().scout_target = Some(*exit);
}
Expand Down
7 changes: 5 additions & 2 deletions src/room/creeps/local/source_miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ use crate::{
};

pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCache) {
let creep_memory = memory.creeps.get_mut(&creep.name()).unwrap();
let creep_memory = memory.creeps.get_mut(&creep.name());

if creep_memory.task_id.is_none() {
if creep_memory.is_none() || creep_memory.as_ref().unwrap().task_id.is_none() {
let _ = creep.say("kurt kob", true);
let _ = creep.suicide();
return;
}

let creep_memory = creep_memory.unwrap();

let pointer_index = creep_memory.task_id.unwrap() as usize;
let scouted_source = &mut cache.resources.sources[pointer_index];
scouted_source.creeps.push(creep.try_id().unwrap());
Expand Down
18 changes: 10 additions & 8 deletions src/room/creeps/organizer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use log::info;
use screeps::{game, Room, SharedCreepProperties};
use screeps::{game, Room, RoomName, SharedCreepProperties};

use crate::{
combat::hate_handler::process_health_event, memory::{Role, ScreepsMemory}, room::{cache::{heap_cache::{HealthChangeType, HeapCreep}, tick_cache::RoomCache}, creeps::global}, utils
Expand All @@ -11,27 +11,29 @@ pub fn run_creeps(room: &Room, memory: &mut ScreepsMemory, cache: &mut RoomCache
// This is done in this manner to stop an "impossible" state
// I reached, idk how, idk why, idk who, but it happened
// and this is the only way I could think of to fix it
let creeps = memory.rooms.get(&room.name());
if creeps.is_none() {
info!(" [CREEPS] No creeps in room {}", room.name());
let creeps = cache.creeps.creeps_in_room.clone();
let creeps = creeps.keys();

if creeps.len() == 0 {
return;
}
let creeps = creeps.unwrap().creeps.clone();

let starting_cpu = game::cpu::get_used();

info!(" [CREEPS] Running {} creeps", creeps.len());
let creep_count = creeps.len();

let mut highest_user: String = "".to_string();
let mut highest_usage: f64 = 0.0;

for creep_name in creeps {
let creep = game::creeps().get(creep_name.clone());
let start_time = game::cpu::get_used();
let creep = game::creeps().get(creep_name.to_string());

if creep.is_none() {
let _ = memory.creeps.remove(&creep_name);
memory.rooms.get_mut(&room.name()).unwrap().creeps.retain(|x| x != &creep_name);
let creep_memory = memory.creeps.get(creep_name).unwrap().clone();
let _ = memory.creeps.remove(creep_name);
memory.rooms.get_mut(&RoomName::new(&creep_memory.owning_room).unwrap()).unwrap().creeps.retain(|x| x != creep_name);
continue;
}

Expand Down
25 changes: 7 additions & 18 deletions src/traits/creep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
memory::{CreepMemory, Role},
movement::{
move_target::MoveTarget,
utils::{dir_to_coords, num_to_dir},
utils::{dir_to_coords, num_to_dir, visualise_path},
},
room::{cache::tick_cache::RoomCache, planning::creep},
utils::{name_to_role, role_to_name},
Expand Down Expand Up @@ -59,25 +59,12 @@ impl CreepExtensions for screeps::Creep {
let x = target_position.0 as u8;
let y = target_position.1 as u8;

if self.room().is_some() && self.room().unwrap().my() {
info!("Creep is in room");
self.move_request(step_dir, cache);
} else {
info!("Creep is NOT in room - {}", step_dir);
let vis = self.room().unwrap().visual();
for i in 0..serialized_vec.len() {
let dir = num_to_dir(serialized_vec[i]);
let (x, y) = dir_to_coords(dir, x, y);
vis.circle(x as f32, y as f32, Some(CircleStyle::default().stroke("red")));
}
if x == 0 || x == 49 || y == 0 || y == 49 {
let _ = self.move_direction(step_dir);
} else {
self.move_request(step_dir, cache);
}

//if x == 0 || x == 49 || y == 0 || y == 49 {
// info!("Creep on exit.");
// let _ = self.move_direction(step_dir);
//}

let serialized_vec = serialized_vec[1..].to_vec();
let serialized_path = serialized_vec
.iter()
Expand All @@ -88,7 +75,7 @@ impl CreepExtensions for screeps::Creep {
memory.path = None;
return;
} else {
memory.path = Some(serialized_path);
memory.path = Some(serialized_path.clone());
}

let mut points = vec![];
Expand All @@ -99,6 +86,8 @@ impl CreepExtensions for screeps::Creep {
points.push((x, y));
cursor = (x as f32, y as f32);
}

visualise_path(serialized_path, self.room().unwrap().name_str(), (self.pos().x().u8() as f32, self.pos().y().u8() as f32));
}

fn better_move_to(
Expand Down
5 changes: 3 additions & 2 deletions src/traits/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ impl RoomExtensions for screeps::Room {
}

let sign_text = self.controller().unwrap().sign().unwrap().text();
let tag_without_alliance_marker = &sign_text.trim_start_matches(config::ALLIANCE_TAG);
let alliance_marker = format!("{} ", config::ALLIANCE_TAG);
let tag_without_alliance_marker = &sign_text.replace(&alliance_marker, "");

config::ROOM_SIGNS.contains(tag_without_alliance_marker)
config::ROOM_SIGNS.contains(&tag_without_alliance_marker.to_string().as_str())
}

fn get_room_type(&self) -> RoomType {
Expand Down

0 comments on commit 8261afe

Please sign in to comment.