Skip to content

Commit

Permalink
Start the haul ordering process.
Browse files Browse the repository at this point in the history
  • Loading branch information
InfinityDevTech committed May 21, 2024
1 parent f7fee11 commit da01ca3
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
11 changes: 8 additions & 3 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, RoomName, Source, Structure, StructureLink};
use screeps::{game, ObjectId, Resource, ResourceType, RoomName, Source, Structure, StructureLink};
use serde::{Deserialize, Serialize};

use js_sys::JsString;

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

#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
pub enum Role {
Expand Down Expand Up @@ -52,6 +52,7 @@ structstruck::strike! {
pub struct RoomMemory{
// Name
pub name: String,
pub id: u128,
// Mining stuffs
pub sources: Vec<pub struct ScoutedSource {
pub id: ObjectId<Source>,
Expand All @@ -61,8 +62,12 @@ pub struct RoomMemory{
}>,

pub haul_orders: Vec<pub struct HaulOrder {
pub id: u128,
pub priority: HaulPriorities,
pub target_id: ObjectId<Structure>,
pub target_type: String,
pub target_type: ResourceType,
pub responder: Option<String>,
pub amount: u32,
}>,
#[serde(skip_serializing_if = "Option::is_none")]
pub links: Option<Vec<ObjectId<StructureLink>>>,
Expand Down
44 changes: 44 additions & 0 deletions src/room/hauling.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use screeps::{Creep, HasId, ResourceType, SharedCreepProperties, Structure};
use serde::{Deserialize, Serialize};

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

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum HaulPriorities {
Combat = 0,
Spawning = 1,
Defensive = 2,
Normal = 3,
}

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

impl RoomMemory {
pub fn create_unique_id(&mut self) -> u128 {
let id = self.id;
self.id += 1;
id
}
pub fn destroy_haul_order(&mut self, order_id: u128) {
self.haul_orders.retain(|x| x.id != order_id);
}

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

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

self.haul_orders.push(order);
}
}
3 changes: 2 additions & 1 deletion src/room/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pub mod building;
pub mod creeps;
pub mod tower;
pub mod planning;
pub mod structure_cache;
pub mod structure_cache;
pub mod hauling;
1 change: 1 addition & 0 deletions src/room/planning/room/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub fn plan_room(room: &Room, memory: &mut ScreepsMemory) -> bool {

let mut room_memory = RoomMemory {
name: room.name_str(),
id: 0,
creeps: Vec::new(),
sources: Vec::new(),
haul_orders: Vec::new(),
Expand Down

0 comments on commit da01ca3

Please sign in to comment.