Skip to content

Commit

Permalink
Basic duo code, maybe.
Browse files Browse the repository at this point in the history
Invader core cleaner.
Small bug fixes.
  • Loading branch information
InfinityDevTech committed Jul 21, 2024
1 parent 0698607 commit df9ee54
Show file tree
Hide file tree
Showing 28 changed files with 531 additions and 438 deletions.
323 changes: 0 additions & 323 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ regex = "1.9.4"
console_error_panic_hook = "0.1.7"
rand = "0.8.5"
getrandom = { version = "0.2.15", features = ["js"] }
rust_decimal = "1.35.0"
enum-map = "2.7.3"
#screeps-timing = { git = "https://github.com/InfinityDevTech/screeps-timing", optional = true }
screeps-timing-annotate = { git = "https://github.com/InfinityDevTech/screeps-timing-annotate", optional = true }
Expand Down
3 changes: 2 additions & 1 deletion src/combat/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use crate::{memory::ScreepsMemory, room::cache::tick_cache::RoomCache};
use super::setters;

#[cfg_attr(feature = "profile", screeps_timing_annotate::timing)]
pub fn run_global_setters(memory: &mut ScreepsMemory, cache: &mut RoomCache) {
pub fn run_global_goal_setters(memory: &mut ScreepsMemory, cache: &mut RoomCache) {
setters::remote_reservation::determine_reservations(memory, cache);
setters::remote_invader_cleanup::determine_cleanup(memory, cache);
setters::remote_defense::determine_remote_defense_needs(cache, memory);
setters::room_claim::determine_room_claim_needs(memory, cache);
}
166 changes: 112 additions & 54 deletions src/combat/goals/remote_invader_cleanup.rs
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);
}
}
}
}
3 changes: 2 additions & 1 deletion src/combat/setters/mod.rs
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;
20 changes: 20 additions & 0 deletions src/combat/setters/remote_invader_cleanup.rs
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);
}
}
}
}
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub const REMOTE_SIGNS: [&str; 3] = [
//"oOoOoOoOoh Taxes. - Infinity Dev",
"Dont mess with me, my mom called me handsome. - Infinity Dev",
//"I think im a bot? - Infinity Dev",
"liaohao isnt gone. Just ask lp136. - Infinity Dev",
"liaohuo isnt gone. Just ask lp136. - Infinity Dev",
];

pub const ROOM_SIGNS: [&str; 24] = [
Expand Down
33 changes: 33 additions & 0 deletions src/formation/duo/duo_utils.rs
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()
}
3 changes: 3 additions & 0 deletions src/formation/duo/mod.rs
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;
79 changes: 79 additions & 0 deletions src/formation/duo/movement.rs
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
}
Loading

0 comments on commit df9ee54

Please sign in to comment.