Skip to content

Commit

Permalink
Id breh
Browse files Browse the repository at this point in the history
  • Loading branch information
InfinityDevTech committed May 22, 2024
1 parent da01ca3 commit 2542c04
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 66 deletions.
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,14 @@ pub fn game_loop() {
// This is done like this because its basically MemHack for you JS people.
memory.write_memory();

let heap = game::cpu::get_heap_statistics();

info!("[STATS] Statistics are as follows: ");
info!(" GCL {}. Next: {} / {}", game::gcl::level(), game::gcl::progress(), game::gcl::progress_total());
info!(" CPU Usage:");
info!(" Total: {}", game::cpu::get_used());
info!(" Bucket: {}", game::cpu::bucket());
info!(" Heap: {:.1}/{:.1}", (heap.total_heap_size() / 1000000), (heap.heap_size_limit() / 1000000));
}

#[wasm_bindgen(js_name = red_button)]
Expand Down
11 changes: 6 additions & 5 deletions src/memory.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::{cmp, collections::HashMap};

use log::error;
use screeps::{game, ObjectId, Resource, ResourceType, RoomName, Source, Structure, StructureLink};
use screeps::{game, ObjectId, RawObjectId, Resource, ResourceType, RoomName, Source, Structure, StructureLink};
use serde::{Deserialize, Serialize};

use js_sys::JsString;

use crate::{room::hauling::HaulPriorities, MEMORY_VERSION};
use crate::{room::hauling::{HaulPriorities, HaulType}, MEMORY_VERSION};

#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
pub enum Role {
Expand Down Expand Up @@ -43,7 +43,7 @@ pub struct CreepMemory{
// Hauler - A reference to the ID of the current haul orders
// Miner - A reference to the source in the vec of sources
#[serde(skip_serializing_if = "Option::is_none")]
pub t_id: Option<u8>,
pub t_id: Option<u128>,
}
}

Expand All @@ -61,12 +61,13 @@ pub struct RoomMemory{
pub work_parts: u8,
}>,

pub haul_orders: Vec<pub struct HaulOrder {
pub haul_orders: HashMap<u128, pub struct HaulOrder {
pub id: u128,
pub priority: HaulPriorities,
pub target_id: ObjectId<Structure>,
pub target_id: RawObjectId,
pub target_type: ResourceType,
pub responder: Option<String>,
pub haul_type: HaulType,
pub amount: u32,
}>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down
97 changes: 46 additions & 51 deletions src/room/creeps/local/hauler.rs
Original file line number Diff line number Diff line change
@@ -1,70 +1,65 @@
use std::cmp::min;
use std::{cmp::min, str::FromStr};

use screeps::{
find, game, Creep, HasPosition, ResourceType, SharedCreepProperties, StructureObject,
find, game, Creep, HasPosition, ObjectId, Resource, ResourceType, RoomName, SharedCreepProperties, Structure, StructureObject, Transferable
};

use crate::{
memory::{CreepMemory, ScreepsMemory},
traits::creep::CreepExtensions,
memory::{CreepMemory, ScreepsMemory}, room, traits::creep::CreepExtensions
};

pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory) {
let creep_memory = memory.get_creep_mut(&creep.name());
let _needs_energy = creep_memory.n_e.unwrap_or(false);

if creep.store().get_free_capacity(Some(ResourceType::Energy)) > 0 {
get_energy(creep, creep_memory);
let order = creep_memory.t_id;
if let Some(order_id) = order {
execute_order(creep, memory, order_id)
} else {
haul_energy(creep, memory);
}
}

pub fn get_energy(creep: &Creep, creep_memory: &mut CreepMemory) {
let closest_energy = creep.pos().find_closest_by_range(find::DROPPED_RESOURCES);
if let Some(energy) = closest_energy {
if creep.pos().is_near_to(energy.clone().pos()) {
let _ = creep.pickup(&energy);
} else {
creep.better_move_to(creep_memory, energy.pos(), 1)
let room_memory = memory.get_room_mut(&RoomName::from_str(&creep_memory.o_r).unwrap());
let new_order = room_memory.find_haul_order(creep, memory);
if let Some(order) = new_order {
execute_order(creep, memory, order.id);
}
}
}

pub fn haul_energy(creep: &Creep, screeps_memory: &mut ScreepsMemory) {
let room_memory = screeps_memory.get_room(&creep.room().unwrap().name());
let creep_memory = screeps_memory.get_creep_mut(&creep.name());
pub fn execute_order(creep: &Creep, memory: &mut ScreepsMemory, order_id: u128) {
let room_memory = memory.get_room_mut(&RoomName::from_str(&memory.get_creep(&creep.name()).o_r).unwrap());

let order = room_memory.get_haul_order(order_id).unwrap();
let pickup_target = order.target_id;

let position = order.get_target_position();

if position.get_range_to(creep.pos()) > 1 {
let creep_memory = memory.get_creep_mut(&creep.name());
creep.better_move_to(creep_memory, position, 1);
return;
}

//let task_id = &room_memory.haul_orders[creep_memory.t_id.unwrap() as usize];
let task_id = &room_memory.haul_orders[0];
let target_structure = game::get_object_by_id_typed(&task_id.target_id).unwrap();
let structure_object = StructureObject::from(target_structure);
match order.haul_type {
crate::room::hauling::HaulType::Pickup => {
let target: Option<Resource> = game::get_object_by_id_typed(&ObjectId::from(pickup_target));

if let Some(structure) = structure_object.as_transferable() {
if structure_object
.as_has_store()
.unwrap()
.store()
.get_free_capacity(Some(ResourceType::Energy))
> 0
{
if creep.pos().is_near_to(structure.pos()) {
let _ = creep.transfer(
structure,
ResourceType::Energy,
Some(min(
creep.store().get_used_capacity(Some(ResourceType::Energy)),
structure_object
.as_has_store()
.unwrap()
.store()
.get_free_capacity(Some(ResourceType::Energy))
as u32,
)),
);
} else {
creep.better_move_to(creep_memory, structure.pos(), 1);
if target.is_none() {
room_memory.destroy_haul_order(order_id);
return;
}
}

let _ = creep.pickup(&target.unwrap());
},
crate::room::hauling::HaulType::Deposit => {
//let target = game::get_object_by_id_erased(&pickup_target);

//if target.is_none() {
// room_memory.destroy_haul_order(order_id);
// return;
//}

//let transferrable: &dyn Transferable = target.unwrap().as_ref();

//let _ = creep.transfer(target.unwrap().as_ref(), order.target_type, Some(min(creep.store().get_used_capacity(Some(order.target_type)), 32)));
},
crate::room::hauling::HaulType::Withdraw => todo!(),
}
}
}
54 changes: 47 additions & 7 deletions src/room/hauling.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
use screeps::{Creep, HasId, ResourceType, SharedCreepProperties, Structure};
use screeps::{game, Creep, HasId, HasPosition, ObjectId, Position, RawObjectId, ResourceType, SharedCreepProperties, Structure};
use serde::{Deserialize, Serialize};

use crate::memory::{HaulOrder, RoomMemory};
use crate::memory::{CreepMemory, HaulOrder, RoomMemory, ScreepsMemory};

#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord)]
pub enum HaulPriorities {
Combat = 0,
Spawning = 1,
Defensive = 2,
Normal = 3,
Storage = 4,
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord)]
pub enum HaulType {
Pickup = 0,
Withdraw = 1,
Deposit = 2,
}

impl HaulOrder {
pub fn add_responder(&mut self, creep: &Creep) {
self.responder = Some(creep.name());
}

pub fn get_target_position(&self) -> Position {
let target = game::get_object_by_id_erased(&self.target_id).unwrap();

target.pos()
}
}

impl RoomMemory {
Expand All @@ -24,21 +38,47 @@ impl RoomMemory {
id
}
pub fn destroy_haul_order(&mut self, order_id: u128) {
self.haul_orders.retain(|x| x.id != order_id);
self.haul_orders.remove(&order_id);
}

pub fn create_haul_order(&mut self, priority: HaulPriorities, target: Structure, resource: ResourceType, amount: u32) {
pub fn create_haul_order(&mut self, priority: HaulPriorities, target_id: RawObjectId, resource: ResourceType, amount: u32, haul_type: HaulType) {
let id = self.create_unique_id();

let order = HaulOrder {
id,
priority,
target_id: target.id(),
target_id,
target_type: resource,
responder: None,
haul_type,
amount,
};

self.haul_orders.push(order);
self.haul_orders.insert(id, order);
}

pub fn find_haul_order(&mut self, creep: &Creep, memory: &mut ScreepsMemory) -> Option<&HaulOrder> {
let mut orders = self.haul_orders.values().collect::<Vec<&HaulOrder>>();
orders.sort_by(|a, b| a.priority.cmp(&b.priority));

let unresponded_order = orders.into_iter().find(|&order| order.responder.is_none());

if let Some(order) = unresponded_order {
let order = self.get_haul_order_mut(order.id).unwrap();
order.add_responder(creep);
let creep_memory = memory.get_creep_mut(&creep.name());
creep_memory.t_id = Some(order.id);
Some(order)
} else {
None
}
}

pub fn get_haul_order(&self, order_id: u128) -> Option<&HaulOrder> {
self.haul_orders.get(&order_id)
}

pub fn get_haul_order_mut(&mut self, order_id: u128) -> Option<&mut HaulOrder> {
self.haul_orders.get_mut(&order_id)
}
}
2 changes: 1 addition & 1 deletion src/room/planning/creep/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub fn formulate_miner(room: &Room, memory: &mut ScreepsMemory, spawn: Structure
let cmemory = CreepMemory {
r: crate::memory::Role::Miner,
n_e: Some(true),
t_id: Some(needed.unwrap()),
t_id: Some(needed.unwrap().into()),
l_id: None,
o_r: room.name().to_string(),
p: None,
Expand Down
4 changes: 3 additions & 1 deletion src/room/planning/room/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use log::info;
use screeps::{game, Room};

Expand All @@ -18,7 +20,7 @@ pub fn plan_room(room: &Room, memory: &mut ScreepsMemory) -> bool {
id: 0,
creeps: Vec::new(),
sources: Vec::new(),
haul_orders: Vec::new(),
haul_orders: HashMap::new(),
links: None,
creeps_manufactured: 0
};
Expand Down
22 changes: 21 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
use crate::memory::Role;
use screeps::{Room, RoomName};

use crate::{memory::Role, traits::room::RoomExtensions};

pub fn room_to_number(room: &Room) -> u32 {
let parts = room.split_name();

let w_e = match parts.0.as_str() {
"W" => 1,
"E" => 2,
_ => 0,
};

let n_s = match parts.2.as_str() {
"N" => 1,
"S" => 2,
_ => 0,
};

return w_e + parts.1 + n_s + parts.3;
}

pub fn role_to_name(role: Role) -> String {
let data = match role {
Expand Down

0 comments on commit 2542c04

Please sign in to comment.