-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
28 changed files
with
531 additions
and
438 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,134 @@ | ||
use screeps::{game, Part, RoomName, StructureProperties}; | ||
|
||
use crate::{memory::ScreepsMemory, room::cache::tick_cache::RoomCache, utils}; | ||
|
||
use log::info; | ||
use screeps::{game, Part, RoomName, SharedCreepProperties, StructureProperties}; | ||
|
||
use crate::{ | ||
goal_memory::RemoteInvaderCleanup, | ||
memory::{CreepMemory, Role, ScreepsMemory}, | ||
room::cache::tick_cache::RoomCache, | ||
utils, | ||
}; | ||
// TODO: Something is telling me that there might be invaders in the room | ||
// at the same time, plan for that please. | ||
// (Potentially: Switch it to a remote_defense goal) | ||
pub fn run_goal(memory: &mut ScreepsMemory, cache: &mut RoomCache) { | ||
let cloned_goals = memory.goals.remote_invader_cleanup.clone(); | ||
let invader_goals = cloned_goals.keys(); | ||
|
||
for goal_room in invader_goals { | ||
achieve_goal(goal_room, memory, cache); | ||
for (goal_room, goal_mem) in cloned_goals { | ||
achieve_goal(&goal_room, memory, cache); | ||
} | ||
} | ||
|
||
#[cfg_attr(feature = "profile", screeps_timing_annotate::timing)] | ||
pub fn clear_creeps(goal: &mut RemoteInvaderCleanup) { | ||
let mut new_creeps = Vec::new(); | ||
|
||
for creep in &goal.creeps_assigned { | ||
let creep = game::creeps().get(creep.to_string()); | ||
|
||
if let Some(creep) = creep { | ||
new_creeps.push(creep.name()); | ||
} | ||
} | ||
|
||
goal.creeps_assigned = new_creeps; | ||
} | ||
|
||
pub fn achieve_goal(target_room: &RoomName, memory: &mut ScreepsMemory, cache: &mut RoomCache) { | ||
let goal = memory.goals.remote_invader_cleanup.get_mut(target_room).unwrap(); | ||
let goal = memory | ||
.goals | ||
.remote_invader_cleanup | ||
.get_mut(target_room) | ||
.unwrap(); | ||
|
||
if goal.creeps_assigned.is_empty() { | ||
let responsible_room = utils::find_closest_owned_room(target_room, cache, Some(4)); | ||
clear_creeps(goal); | ||
|
||
if let Some(responsible_room) = responsible_room { | ||
if let Some(room_cache) = cache.rooms.get_mut(target_room) { | ||
let hostile_structures = &room_cache.structures.hostile_structures; | ||
let responsible_room = utils::find_closest_owned_room(target_room, cache, Some(2)); | ||
|
||
if hostile_structures.is_empty() { | ||
memory.goals.remote_invader_cleanup.remove(target_room); | ||
return; | ||
} | ||
if let Some(room_cache) = cache.rooms.get_mut(target_room) { | ||
let invader_core = &room_cache.structures.invader_core; | ||
|
||
let invader_core = hostile_structures.iter().find(|s| s.structure_type() == screeps::StructureType::InvaderCore); | ||
if invader_core.is_none() { | ||
info!("No invader core found in room {}", target_room); | ||
memory.goals.remote_invader_cleanup.remove(target_room); | ||
return; | ||
} | ||
} | ||
|
||
if invader_core.is_none() { | ||
memory.goals.remote_invader_cleanup.remove(target_room); | ||
return; | ||
} | ||
if goal.creeps_assigned.is_empty() { | ||
if let Some(responsible_room) = responsible_room { | ||
let mut reservation = 0.0; | ||
|
||
let stamp = vec![Part::Attack, Part::Move]; | ||
let stamp_cost = utils::get_body_cost(&stamp); | ||
if let Some(room_cache) = cache.rooms.get(target_room) { | ||
reservation = room_cache.reservation as f32; | ||
} | ||
|
||
let mut body = Vec::new(); | ||
let mut current_cost = 0; | ||
let stamp = vec![Part::Attack, Part::Move]; | ||
let stamp_cost = utils::get_body_cost(&stamp); | ||
|
||
let responsible_room = cache.rooms.get_mut(&responsible_room).unwrap(); | ||
let available_energy = game::rooms().get(responsible_room.room_name).unwrap().energy_available(); | ||
let mut body = Vec::new(); | ||
let mut current_cost = 0; | ||
|
||
if available_energy < stamp_cost { | ||
if current_cost + available_energy < stamp_cost { | ||
return; | ||
} | ||
let responsible_room = cache.rooms.get_mut(&responsible_room).unwrap(); | ||
let available_energy = game::rooms() | ||
.get(responsible_room.room_name) | ||
.unwrap() | ||
.energy_available(); | ||
|
||
body.extend(stamp.clone()); | ||
current_cost += stamp_cost; | ||
while current_cost < available_energy { | ||
if current_cost + available_energy < stamp_cost { | ||
break; | ||
} | ||
|
||
/*let memory = CreepMemory { | ||
role: Role::InvaderCleaner, | ||
owning_room: responsible_room.room_name, | ||
target_room: Some(*target_room), | ||
..CreepMemory::default() | ||
}; | ||
let name = format!("{}-{}-{}", Role::InvaderCleaner, responsible_room.room_name, utils::get_unique_id()); | ||
let req = cache.spawning.create_room_spawn_request(Role::InvaderCleaner, body, 4.0, current_cost, responsible_room.room_name, Some(memory), None, Some(name)); | ||
if let Some(reqs) = cache.spawning.room_spawn_queue.get_mut(&responsible_room.room_name) { | ||
reqs.push(req); | ||
} else { | ||
cache | ||
.spawning | ||
.room_spawn_queue | ||
.insert(responsible_room.room_name, vec![req]); | ||
}*/ | ||
body.extend(stamp.clone()); | ||
current_cost += stamp_cost; | ||
} | ||
|
||
let memory = CreepMemory { | ||
role: Role::InvaderCoreCleaner, | ||
owning_room: responsible_room.room_name, | ||
target_room: Some(*target_room), | ||
..CreepMemory::default() | ||
}; | ||
|
||
let name = format!( | ||
"{}-{}-{}", | ||
utils::role_to_name(Role::InvaderCoreCleaner), | ||
responsible_room.room_name, | ||
utils::get_unique_id() | ||
); | ||
|
||
// These guys dont fight back, just suck my eco dry is all. | ||
let mut priority = 5.0; | ||
|
||
priority += reservation as f64 / 100.0; | ||
|
||
let req = cache.spawning.create_room_spawn_request( | ||
Role::InvaderCoreCleaner, | ||
body, | ||
priority, | ||
current_cost, | ||
responsible_room.room_name, | ||
Some(memory), | ||
None, | ||
Some(name), | ||
); | ||
|
||
info!("Spawning invader core cleaner for {} - {}", target_room, priority); | ||
|
||
if let Some(reqs) = cache | ||
.spawning | ||
.room_spawn_queue | ||
.get_mut(&responsible_room.room_name) | ||
{ | ||
reqs.push(req); | ||
} else { | ||
cache | ||
.spawning | ||
.room_spawn_queue | ||
.insert(responsible_room.room_name, vec![req]); | ||
} | ||
} else { | ||
info!("No responsible room found for {}", target_room); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod remote_defense; | ||
pub mod remote_reservation; | ||
pub mod room_claim; | ||
pub mod room_claim; | ||
pub mod remote_invader_cleanup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use crate::{goal_memory::RemoteInvaderCleanup, memory::ScreepsMemory, room::cache::tick_cache::RoomCache}; | ||
|
||
pub fn determine_cleanup(memory: &mut ScreepsMemory, cache: &mut RoomCache) { | ||
for (remote_name, remote_memory) in memory.remote_rooms.clone() { | ||
if memory.goals.remote_invader_cleanup.contains_key(&remote_name) { | ||
continue; | ||
} | ||
|
||
if let Some(remote_cache) = cache.rooms.get_mut(&remote_name) { | ||
if remote_cache.structures.invader_core.is_some() || remote_cache.current_holder == Some("Invader".to_string()) { | ||
let goal = RemoteInvaderCleanup { | ||
cleanup_target: remote_name, | ||
creeps_assigned: Vec::new(), | ||
}; | ||
|
||
memory.goals.remote_invader_cleanup.insert(remote_name, goal); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use screeps::{Creep, Part, SharedCreepProperties}; | ||
|
||
pub fn get_healer(creeps: &Vec<Creep>) -> Option<Creep> { | ||
let mut highest_heal_count = u32::MIN; | ||
let mut healer = 0; | ||
|
||
for creep in creeps { | ||
let heal_count = creep.body().iter().filter(|part| part.part() == Part::Heal).count() as u32; | ||
|
||
if heal_count > highest_heal_count { | ||
highest_heal_count = heal_count; | ||
healer = creeps.iter().position(|c| c.name() == creep.name()).unwrap(); | ||
} | ||
} | ||
|
||
creeps.get(healer).cloned() | ||
} | ||
|
||
pub fn get_attacker(creeps: &Vec<Creep>) -> Option<Creep> { | ||
let mut highest_attack_count = u32::MIN; | ||
let mut attacker = 0; | ||
|
||
for creep in creeps { | ||
let attack_count = creep.body().iter().filter(|part| part.part() == Part::Attack || part.part() == Part::RangedAttack || part.part() == Part::Work).count() as u32; | ||
|
||
if attack_count > highest_attack_count { | ||
highest_attack_count = attack_count; | ||
attacker = creeps.iter().position(|c| c.name() == creep.name()).unwrap(); | ||
} | ||
} | ||
|
||
creeps.get(attacker).cloned() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod movement; | ||
pub mod run_duo; | ||
pub mod duo_utils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use screeps::{Creep, HasPosition, Part, Position, SharedCreepProperties}; | ||
|
||
use crate::{memory::ScreepsMemory, movement::move_target::MoveOptions, room::cache::tick_cache::RoomCache, traits::creep::CreepExtensions}; | ||
|
||
use super::duo_utils::{get_attacker, get_healer}; | ||
|
||
pub fn move_duo(creeps: Vec<Creep>, memory: &mut ScreepsMemory, cache: &mut RoomCache, dest: Position, range: u16, move_options: Option<MoveOptions>) { | ||
let healer = get_healer(&creeps); | ||
let attacker = get_attacker(&creeps); | ||
|
||
if healer.is_none() || attacker.is_none() { | ||
return; | ||
} | ||
|
||
let healer = healer.unwrap(); | ||
let attacker = attacker.unwrap(); | ||
|
||
if !can_duo_move(&creeps) { | ||
return; | ||
} | ||
|
||
let healer_pos = healer.pos(); | ||
let attacker_pos = attacker.pos(); | ||
|
||
form_duo(&creeps, memory, cache, dest, range, move_options); | ||
if !is_duo_formed(&creeps) { | ||
return; | ||
} | ||
|
||
attacker.better_move_to(memory, cache.rooms.get_mut(&attacker.room().unwrap().name()).unwrap(), dest, range, move_options.unwrap_or_default()); | ||
|
||
let _ = healer.move_direction(healer_pos.get_direction_to(attacker_pos).unwrap()); | ||
} | ||
|
||
fn can_duo_move(creeps: &Vec<Creep>) -> bool { | ||
for creep in creeps { | ||
if creep.tired() { | ||
return false; | ||
} | ||
} | ||
|
||
true | ||
} | ||
|
||
fn form_duo(creeps: &Vec<Creep>, memory: &mut ScreepsMemory, cache: &mut RoomCache, dest: Position, range: u16, move_options: Option<MoveOptions>) { | ||
let healer = get_healer(creeps).unwrap(); | ||
let attacker = get_attacker(creeps).unwrap(); | ||
|
||
let healer_room = healer.room().unwrap(); | ||
let attacker_room = attacker.room().unwrap(); | ||
|
||
if is_duo_formed(creeps) { | ||
return; | ||
} | ||
|
||
if healer_room.name() != attacker_room.name() { | ||
if attacker.pos().is_room_edge() { | ||
// This is to move it off the edge, so the healer can come in. | ||
attacker.better_move_to(memory, cache.rooms.get_mut(&attacker.room().unwrap().name()).unwrap(), dest, range, move_options.unwrap_or_default()); | ||
return; | ||
} | ||
healer.better_move_to(memory, cache.rooms.get_mut(&healer.room().unwrap().name()).unwrap(), attacker.pos(), 1, MoveOptions::default()); | ||
return; | ||
} | ||
|
||
healer.better_move_to(memory, cache.rooms.get_mut(&healer.room().unwrap().name()).unwrap(), attacker.pos(), 1, MoveOptions::default()); | ||
} | ||
|
||
fn is_duo_formed(creeps: &Vec<Creep>) -> bool { | ||
for top_creep in creeps.clone() { | ||
for bottom_creep in creeps { | ||
if !top_creep.pos().is_near_to(bottom_creep.pos()) { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
true | ||
} |
Oops, something went wrong.