-
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.
Upgraders work. Haul queue works. Bot *may* break.
- Loading branch information
1 parent
6b46fd4
commit 0416520
Showing
20 changed files
with
566 additions
and
377 deletions.
There are no files selected for viewing
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
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,57 @@ | ||
use std::collections::HashMap; | ||
|
||
use screeps::{find, Creep, Room, SharedCreepProperties}; | ||
|
||
use crate::memory::{Role, ScreepsMemory, ALLIES}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct CreepCache { | ||
pub creeps: HashMap<String, Creep>, | ||
pub creeps_of_role: HashMap<Role, Vec<String>>, | ||
|
||
pub enemy_creeps: Vec<Creep>, | ||
pub allied_creeps: Vec<Creep>, | ||
} | ||
|
||
impl CreepCache { | ||
pub fn new_from_room(room: &Room, memory: &mut ScreepsMemory) -> CreepCache { | ||
let mut cache = CreepCache { | ||
creeps: HashMap::new(), | ||
creeps_of_role: HashMap::new(), | ||
|
||
enemy_creeps: Vec::new(), | ||
allied_creeps: Vec::new(), | ||
}; | ||
|
||
cache.refresh_creep_cache(room, memory); | ||
cache | ||
} | ||
|
||
pub fn refresh_creep_cache(&mut self, room: &Room, memory: &mut ScreepsMemory) { | ||
let creeps = room.find(find::CREEPS, None); | ||
|
||
for creep in creeps { | ||
if creep.my() { | ||
let creep_memory = memory.creeps.get(&creep.name()); | ||
|
||
if creep_memory.is_none() { | ||
continue; | ||
} | ||
|
||
let creep_memory = creep_memory.unwrap(); | ||
|
||
if let Some(role_vec) = self.creeps_of_role.get_mut(&creep_memory.role) { | ||
role_vec.push(creep.name()); | ||
} else { | ||
self.creeps_of_role.insert(creep_memory.role, vec![creep.name()]); | ||
} | ||
|
||
self.creeps.insert(creep.name(), creep); | ||
} else if ALLIES.contains(&creep.owner().username().as_str()) { | ||
self.allied_creeps.push(creep); | ||
} else { | ||
self.enemy_creeps.push(creep); | ||
} | ||
} | ||
} | ||
} |
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,115 @@ | ||
use std::collections::HashMap; | ||
|
||
use log::info; | ||
use screeps::{game, Creep, HasPosition, Position, RawObjectId, ResourceType, SharedCreepProperties}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::memory::{CreepHaulTask, ScreepsMemory}; | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord)] | ||
pub enum HaulingPriority { | ||
Combat = 0, | ||
Emergency = 1, | ||
Energy = 2, | ||
Minerals = 3, | ||
Market = 4 | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord)] | ||
pub enum HaulingType { | ||
Deposit = 0, | ||
Withdraw = 1, | ||
Pickup = 2, | ||
Transfer = 3 | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone)] | ||
pub struct RoomHaulingOrder { | ||
pub id: u32, | ||
pub target: RawObjectId, | ||
pub resource: ResourceType, | ||
pub amount: u32, | ||
pub priority: HaulingPriority, | ||
pub haul_type: HaulingType, | ||
} | ||
|
||
pub struct HaulingCache { | ||
pub new_orders: HashMap<u32, RoomHaulingOrder>, | ||
|
||
pub current_id_index: u32, | ||
|
||
pub haulers: Vec<String>, | ||
} | ||
|
||
impl HaulingCache { | ||
pub fn new() -> HaulingCache { | ||
HaulingCache { | ||
new_orders: HashMap::new(), | ||
current_id_index: game::time(), | ||
haulers: Vec::new(), | ||
} | ||
} | ||
|
||
pub fn get_unique_id(&mut self) -> u32 { | ||
self.current_id_index += 1; | ||
self.current_id_index | ||
} | ||
|
||
pub fn create_order(&mut self, target: RawObjectId, resource: ResourceType, amount: u32, priority: HaulingPriority, haul_type: HaulingType) { | ||
let id = self.get_unique_id(); | ||
|
||
let order = RoomHaulingOrder { | ||
id, | ||
target, | ||
resource, | ||
amount, | ||
priority, | ||
haul_type, | ||
}; | ||
|
||
self.new_orders.insert(id, order); | ||
} | ||
|
||
pub fn find_new_order(&mut self, creep: &Creep, memory: &mut ScreepsMemory) -> Option<CreepHaulTask> { | ||
let unsorted_orders = self.new_orders.values().collect::<Vec<&RoomHaulingOrder>>(); | ||
let mut orders = unsorted_orders.clone(); | ||
|
||
orders.sort_by(|a, b| a.priority.cmp(&b.priority)); | ||
|
||
if creep.store().get_free_capacity(Some(ResourceType::Energy)) as u32 == 0 { | ||
orders.retain(|x| x.haul_type == HaulingType::Deposit || x.haul_type == HaulingType::Transfer); | ||
info!("Hauling: Deposit or Transfer {}", orders.len()); | ||
} else { | ||
orders.retain(|x| x.haul_type == HaulingType::Withdraw || x.haul_type == HaulingType::Pickup); | ||
} | ||
|
||
if let Some(order) = orders.into_iter().next() { | ||
let id = order.id; | ||
let order = self.new_orders.get_mut(&id).unwrap(); | ||
|
||
let creep_memory = memory.creeps.get_mut(&creep.name()).unwrap(); | ||
let task = CreepHaulTask { | ||
target_id: order.target, | ||
resource: order.resource, | ||
amount: order.amount, | ||
priority: order.priority, | ||
haul_type: order.haul_type, | ||
}; | ||
|
||
self.new_orders.remove(&id); | ||
creep_memory.hauling_task = Some(task); | ||
return creep_memory.hauling_task.clone(); | ||
} | ||
None | ||
} | ||
} | ||
|
||
impl CreepHaulTask { | ||
pub fn get_target_position(&self) -> Option<Position> { | ||
let target = game::get_object_by_id_erased(&self.target_id); | ||
|
||
target.as_ref()?; | ||
|
||
Some(target.unwrap().pos()) | ||
} | ||
} |
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,42 @@ | ||
use screeps::Room; | ||
|
||
use crate::memory::ScreepsMemory; | ||
|
||
use self::{creeps::CreepCache, hauling::HaulingCache, resources::RoomResourceCache, structures::RoomStructureCache}; | ||
|
||
pub mod structures; | ||
pub mod creeps; | ||
pub mod hauling; | ||
pub mod resources; | ||
|
||
pub struct RoomCache { | ||
pub structures: RoomStructureCache, | ||
pub creeps: CreepCache, | ||
|
||
pub resources: RoomResourceCache, | ||
|
||
//pub hauling: RefCell<HaulingCache>, | ||
pub hauling: HaulingCache, | ||
} | ||
|
||
impl RoomCache { | ||
pub fn new_from_room(room: &Room, memory: &mut ScreepsMemory) -> RoomCache { | ||
RoomCache { | ||
structures: RoomStructureCache::new_from_room(room, memory), | ||
creeps: CreepCache::new_from_room(room, memory), | ||
|
||
resources: RoomResourceCache::new_from_room(room, memory), | ||
|
||
hauling: HaulingCache::new(), | ||
//hauling: RefCell::new(HaulingCache::new()), | ||
} | ||
} | ||
|
||
pub fn _refresh_cache(&mut self, room: &Room, memory: &mut ScreepsMemory) { | ||
self.structures.refresh_structure_cache(room); | ||
self.structures.refresh_source_cache(room); | ||
self.structures.refresh_spawn_cache(room); | ||
|
||
self.creeps.refresh_creep_cache(room, memory); | ||
} | ||
} |
Oops, something went wrong.