Skip to content

Commit

Permalink
Basically copy marvins priority system, because its simple and it works.
Browse files Browse the repository at this point in the history
  • Loading branch information
InfinityDevTech committed Jun 4, 2024
1 parent 4575057 commit 57f8e87
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub struct CreepMemory{
#[serde(rename = "1")]
pub haul_type: HaulingType,
#[serde(rename = "2")]
pub priority: HaulingPriority,
pub priority: u32,
#[serde(rename = "3")]
pub resource: ResourceType,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down
27 changes: 15 additions & 12 deletions src/room/cache/tick_cache/hauling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ use rand::{prelude::SliceRandom, rngs::StdRng, Rng, SeedableRng};
use screeps::{game, Creep, HasId, HasPosition, Position, RawObjectId, ResourceType, SharedCreepProperties};
use serde::{Deserialize, Serialize};

use crate::memory::{CreepHaulTask, ScreepsMemory};
use crate::{memory::{CreepHaulTask, ScreepsMemory}, utils::scale_haul_priority};

use super::structures::RoomStructureCache;

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord)]
pub enum HaulingPriority {
Combat = 0,
Emergency = 1,
Spawning = 2,
Combat = 7,
Emergency = 6,
Spawning = 5,
Ruins = 4,
Energy = 3,
Minerals = 4,
Market = 5,
Storage = 6
Minerals = 2,
Market = 1,
Storage = 0,
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord)]
Expand All @@ -33,7 +34,7 @@ pub struct RoomHaulingOrder {
pub target: RawObjectId,
pub resource: Option<ResourceType>,
pub amount: Option<u32>,
pub priority: HaulingPriority,
pub priority: u32,
pub haul_type: HaulingType,
}

Expand All @@ -59,7 +60,7 @@ impl HaulingCache {
self.current_id_index
}

pub fn create_order(&mut self, target: RawObjectId, resource: Option<ResourceType>, amount: Option<u32>, priority: HaulingPriority, haul_type: HaulingType) {
pub fn create_order(&mut self, target: RawObjectId, resource: Option<ResourceType>, amount: Option<u32>, priority: u32, haul_type: HaulingType) {
let id = self.get_unique_id();

let order = RoomHaulingOrder {
Expand Down Expand Up @@ -129,7 +130,7 @@ impl HaulingCache {
storage.raw_id(),
Some(ResourceType::Energy),
Some(storage.store().get_used_capacity(Some(ResourceType::Energy))),
HaulingPriority::Storage,
0,
HaulingType::Offer
)
}
Expand All @@ -140,12 +141,14 @@ impl HaulingCache {
let ruins = &structures.ruins;

for ruin in ruins.values() {
if ruin.store().get_used_capacity(Some(ResourceType::Energy)) > 0 {
let energy_amount = ruin.store().get_used_capacity(Some(ResourceType::Energy));

if energy_amount > 0 {
self.create_order(
ruin.raw_id(),
Some(ResourceType::Energy),
Some(ruin.store().get_used_capacity(Some(ResourceType::Energy))),
HaulingPriority::Energy,
scale_haul_priority(ruin.store().get_capacity(None), energy_amount, HaulingPriority::Ruins, false),
HaulingType::Offer
);
return;
Expand Down
5 changes: 3 additions & 2 deletions src/room/cache/tick_cache/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use screeps::{find, HasId, Resource, ResourceType, Room};

use crate::memory::ScreepsMemory;
use crate::{memory::ScreepsMemory, utils::scale_haul_priority};

use super::hauling::{HaulingCache, HaulingPriority, HaulingType};

Expand All @@ -27,7 +27,8 @@ impl RoomResourceCache {

pub fn haul_dropped_resources(&self, hauling: &mut HaulingCache) {
for resource in &self.dropped_energy {
hauling.create_order(resource.id().into(), Some(resource.resource_type()), Some(resource.amount()), HaulingPriority::Energy, HaulingType::Pickup);
let priority = scale_haul_priority(resource.amount(), resource.amount(), HaulingPriority::Energy, false);
hauling.create_order(resource.id().into(), Some(resource.resource_type()), Some(resource.amount()), priority, HaulingType::Pickup);
}
}

Expand Down
47 changes: 41 additions & 6 deletions src/room/cache/tick_cache/structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use screeps::{
find, game, look::{self, LookResult}, ConstructionSite, Creep, HasId, HasPosition, LocalRoomTerrain, ObjectId, OwnedStructureProperties, Part, ResourceType, Room, Ruin, Source, StructureContainer, StructureController, StructureExtension, StructureLink, StructureObject, StructureRoad, StructureSpawn, StructureStorage, StructureTower, Terrain
};

use crate::{memory::ScreepsMemory, room::cache::heap_cache::RoomHeapCache};
use crate::{memory::ScreepsMemory, room::cache::heap_cache::RoomHeapCache, utils::scale_haul_priority};

use super::hauling::{HaulingCache, HaulingPriority, HaulingType};

Expand Down Expand Up @@ -115,6 +115,15 @@ impl RoomStructureCache {
pub fn temp(&mut self, hauling: &mut HaulingCache) {
for source in self.extensions.values() {
if source.store().get_free_capacity(Some(ResourceType::Energy)) > 0 {
let priority = scale_haul_priority(
source.store().get_capacity(Some(ResourceType::Energy)),
source.store().get_used_capacity(Some(ResourceType::Energy)),
HaulingPriority::Spawning,
true
);

info!("Creating order for extension {}", priority);

hauling.create_order(
source.raw_id(),
Some(ResourceType::Energy),
Expand All @@ -123,7 +132,7 @@ impl RoomStructureCache {
.get_free_capacity(Some(ResourceType::Energy))
.try_into()
.unwrap()),
HaulingPriority::Spawning,
priority,
HaulingType::Transfer,
);
}
Expand Down Expand Up @@ -175,23 +184,35 @@ impl RoomStructureCache {
&&
container.store().get_used_capacity(Some(ResourceType::Energy)) as f32 > container.store().get_capacity(Some(ResourceType::Energy)) as f32 * 0.5
{
let priority = scale_haul_priority(
container.store().get_capacity(Some(ResourceType::Energy)),
container.store().get_used_capacity(Some(ResourceType::Energy)),
HaulingPriority::Minerals,
true
);
hauling.create_order(
container.raw_id(),
Some(ResourceType::Energy),
Some(container
.store()
.get_used_capacity(Some(ResourceType::Energy))),
HaulingPriority::Minerals,
priority,
HaulingType::Offer,
);
} else {
let priority = scale_haul_priority(
container.store().get_capacity(Some(ResourceType::Energy)),
container.store().get_used_capacity(Some(ResourceType::Energy)),
HaulingPriority::Energy,
true
);
hauling.create_order(
container.raw_id(),
Some(ResourceType::Energy),
Some(container
.store()
.get_used_capacity(Some(ResourceType::Energy))),
HaulingPriority::Energy,
priority,
HaulingType::Offer,
);
}
Expand All @@ -203,16 +224,30 @@ impl RoomStructureCache {
.get_range_to(self.spawns.values().next().unwrap().pos())
<= 3
{
let priority = scale_haul_priority(
container.store().get_capacity(Some(ResourceType::Energy)),
container.store().get_used_capacity(Some(ResourceType::Energy)),
HaulingPriority::Spawning,
true
);

hauling.create_order(
container.raw_id(),
Some(ResourceType::Energy),
Some(container
.store()
.get_free_capacity(Some(ResourceType::Energy)).try_into().unwrap()),
HaulingPriority::Spawning,
priority,
HaulingType::Transfer,
);
} else {
let priority = scale_haul_priority(
container.store().get_capacity(Some(ResourceType::Energy)),
container.store().get_used_capacity(Some(ResourceType::Energy)),
HaulingPriority::Energy,
true
);

hauling.create_order(
container.raw_id(),
Some(ResourceType::Energy),
Expand All @@ -221,7 +256,7 @@ impl RoomStructureCache {
.get_free_capacity(Some(ResourceType::Energy))
.try_into()
.unwrap()),
HaulingPriority::Energy,
priority,
HaulingType::Transfer,
);
}
Expand Down
10 changes: 8 additions & 2 deletions src/room/creeps/local/fast_filler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
hauling::{HaulingPriority, HaulingType},
RoomCache,
},
traits::creep::CreepExtensions,
traits::creep::CreepExtensions, utils::scale_haul_priority,
};

pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCache) {
Expand Down Expand Up @@ -45,11 +45,17 @@ pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCach
let _ = creep.say("WTHD", false);
let container_id = creep_memory.fastfiller_container;
if container_id.is_none() {
let priority = scale_haul_priority(
creep.store().get_capacity(None),
creep.store().get_free_capacity(None) as u32,
HaulingPriority::Emergency,
true
);
cache.hauling.create_order(
creep.try_raw_id().unwrap(),
Some(ResourceType::Energy),
Some(creep.store().get_free_capacity(Some(ResourceType::Energy)) as u32),
HaulingPriority::Emergency,
priority,
HaulingType::Transfer,
);
} else {
Expand Down
21 changes: 11 additions & 10 deletions src/room/creeps/local/source_miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
hauling::{HaulingPriority, HaulingType},
RoomCache,
},
traits::creep::CreepExtensions,
traits::creep::CreepExtensions, utils::scale_haul_priority,
};

use super::hauler;
Expand Down Expand Up @@ -137,7 +137,7 @@ fn deposit_energy(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCac
//if build_around_source(creep, creep_memory, cache) {
// return;
//}

if repair_container(creep, creep_memory, cache) {
return;
}
Expand All @@ -154,11 +154,19 @@ fn deposit_energy(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCac
let amount = creep.store().get_used_capacity(Some(ResourceType::Energy));

let _ = creep.drop(ResourceType::Energy, Some(amount));

let priority = scale_haul_priority(
container.store().get_capacity(Some(ResourceType::Energy)) as u32,
amount,
HaulingPriority::Energy,
false
);

cache.hauling.create_order(
creep.try_raw_id().unwrap(),
Some(ResourceType::Energy),
Some(creep.store().get_used_capacity(Some(ResourceType::Energy))),
HaulingPriority::Energy,
priority,
HaulingType::Pickup,
);
} else if creep.pos().is_near_to(container.pos()) {
Expand All @@ -167,13 +175,6 @@ fn deposit_energy(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCac
creep.better_move_to(creep_memory, cache, container.pos(), 1);
}
} else {
cache.hauling.create_order(
creep.try_raw_id().unwrap(),
Some(ResourceType::Energy),
Some(creep.store().get_used_capacity(Some(ResourceType::Energy))),
HaulingPriority::Energy,
HaulingType::Pickup,
);
let _ = creep.drop(ResourceType::Energy, None);
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/room/creeps/local/upgrader.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rand::{rngs::StdRng, seq::IteratorRandom, Rng, SeedableRng};
use screeps::{game, Creep, HasPosition, MaybeHasId, ResourceType, SharedCreepProperties};

use crate::{config, memory::ScreepsMemory, room::cache::tick_cache::{hauling::{HaulingPriority, HaulingType}, RoomCache}, traits::creep::CreepExtensions};
use crate::{config, memory::ScreepsMemory, room::cache::tick_cache::{hauling::{HaulingPriority, HaulingType}, RoomCache}, traits::creep::CreepExtensions, utils::scale_haul_priority};

pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCache) {
if creep.spawning() || creep.tired() {
Expand All @@ -27,7 +27,14 @@ pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCach
}

} else {
cache.hauling.create_order(creep.try_raw_id().unwrap(), Some(ResourceType::Energy), Some(creep.store().get_free_capacity(Some(ResourceType::Energy)).try_into().unwrap()), HaulingPriority::Energy, HaulingType::Transfer);
let priority = scale_haul_priority(
creep.store().get_free_capacity(None) as u32,
creep.store().get_used_capacity(None) as u32,
HaulingPriority::Energy,
true
);

cache.hauling.create_order(creep.try_raw_id().unwrap(), Some(ResourceType::Energy), Some(creep.store().get_free_capacity(Some(ResourceType::Energy)).try_into().unwrap()), priority, HaulingType::Transfer);
}
}

Expand Down
11 changes: 9 additions & 2 deletions src/room/planning/creep/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
RoomCache,
}, creeps::local::hauler},
traits::room::RoomExtensions,
utils::role_to_name,
utils::{role_to_name, scale_haul_priority},
};

pub fn formulate_miner(room: &Room, memory: &mut ScreepsMemory, cache: &mut RoomCache) -> bool {
Expand Down Expand Up @@ -43,11 +43,18 @@ pub fn formulate_miner(room: &Room, memory: &mut ScreepsMemory, cache: &mut Room
.len();

if fastfiller_count == 0 && spawn.store().get_used_capacity(Some(ResourceType::Energy)) < 300 {
let priority = scale_haul_priority(
spawn.store().get_capacity(None),
spawn.store().get_free_capacity(None) as u32,
HaulingPriority::Spawning,
true
);

cache.hauling.create_order(
spawn.raw_id(),
Some(ResourceType::Energy),
Some(spawn.store().get_free_capacity(Some(ResourceType::Energy)).try_into().unwrap()),
HaulingPriority::Spawning,
priority,
HaulingType::Transfer,
);
}
Expand Down
30 changes: 20 additions & 10 deletions src/room/tower.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
use log::info;
use screeps::{find, HasId, ResourceType, Room};

use super::cache::tick_cache::{hauling::HaulingType, RoomCache};
use crate::utils::scale_haul_priority;

use super::cache::tick_cache::{hauling::{HaulingPriority, HaulingType}, RoomCache};

pub fn run_towers(room: &Room, cache: &mut RoomCache) {
for tower in cache.structures.towers.values() {
// Use cache here
let enemies = room.find(find::HOSTILE_CREEPS, None);
if enemies.is_empty() {
return;
}
if (tower.store().get_used_capacity(Some(ResourceType::Energy)) as f32)
< (tower.store().get_capacity(Some(ResourceType::Energy)) as f32 * 0.5)
{
if tower.store().get_free_capacity(Some(ResourceType::Energy)) > 0 {
let priority = scale_haul_priority(
tower.store().get_capacity(None),
tower.store().get_free_capacity(None) as u32,
HaulingPriority::Combat,
true,
);

info!("Tower {} is hauling energy", priority);

cache.hauling.create_order(
tower.raw_id(),
Some(ResourceType::Energy),
Some(tower.store().get_free_capacity(Some(ResourceType::Energy)) as u32),
super::cache::tick_cache::hauling::HaulingPriority::Combat,
priority,
HaulingType::Transfer,
);
}
// Use cache here
let enemies = &cache.creeps.enemy_creeps;
if enemies.is_empty() {
return;
}
let _ = tower.attack(enemies.first().unwrap());
}
}
Loading

0 comments on commit 57f8e87

Please sign in to comment.