Skip to content

Commit

Permalink
traffic refactoring to remove clone!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonBurke committed May 30, 2024
1 parent 090ec80 commit 9b41bec
Show file tree
Hide file tree
Showing 6 changed files with 295 additions and 246 deletions.
18 changes: 13 additions & 5 deletions src/room/cache/mod.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
use movement::RoomMovementCache;
use std::collections::HashMap;

use screeps::Room;

use crate::memory::ScreepsMemory;

use self::{creeps::CreepCache, hauling::HaulingCache, resources::RoomResourceCache, structures::RoomStructureCache};
use self::{creeps::CreepCache, hauling::HaulingCache, resources::RoomResourceCache, structures::RoomStructureCache, traffic::TrafficCache};

pub mod structures;
pub mod creeps;
pub mod hauling;
pub mod resources;
pub mod movement;
pub mod traffic;

pub struct RoomCache {
pub structures: RoomStructureCache,
pub creeps: CreepCache,
pub movement: RoomMovementCache,
pub traffic: TrafficCache,

pub resources: RoomResourceCache,

Expand All @@ -27,7 +28,7 @@ impl RoomCache {
RoomCache {
structures: RoomStructureCache::new_from_room(room, memory),
creeps: CreepCache::new_from_room(room, memory),
movement: RoomMovementCache::new(),
traffic: TrafficCache::new(),

resources: RoomResourceCache::new_from_room(room, memory),

Expand All @@ -42,5 +43,12 @@ impl RoomCache {
self.structures.refresh_spawn_cache(room);

self.creeps.refresh_creep_cache(room);

self.traffic.move_targets = HashMap::new();
self.traffic.move_requests = HashMap::new();
self.traffic.movement_map = HashMap::new();
self.traffic.visited_creeps = HashMap::new();
self.traffic.cached_ops = HashMap::new();
self.traffic.move_intents = 0;
}
}
210 changes: 0 additions & 210 deletions src/room/cache/movement.rs

This file was deleted.

98 changes: 98 additions & 0 deletions src/room/cache/traffic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use std::collections::HashMap;

use screeps::{
game::get_object_by_id_typed, Creep, Direction, HasPosition, MaybeHasId, ObjectId, Position,
Room, RoomCoordinate, RoomXY,
};

use super::RoomCache;
use crate::{movement::utils::dir_to_coords, traits::creep::CreepExtensions};
use rand::prelude::SliceRandom;

pub struct TrafficCache {
pub move_targets: HashMap<ObjectId<Creep>, RoomXY>,
pub move_requests: HashMap<ObjectId<Creep>, RoomXY>,
pub movement_map: HashMap<RoomXY, ObjectId<Creep>>,
pub visited_creeps: HashMap<ObjectId<Creep>, bool>,

pub cached_ops: HashMap<ObjectId<Creep>, Vec<RoomXY>>,
pub move_intents: u8,
}

impl TrafficCache {
pub fn new() -> Self {
Self {
move_targets: HashMap::new(),
move_requests: HashMap::new(),
movement_map: HashMap::new(),
visited_creeps: HashMap::new(),
cached_ops: HashMap::new(),
move_intents: 0,
}
}
}

pub struct TrafficProcs;

impl TrafficProcs {
pub fn run_movement(room_cache: &mut RoomCache) {
let mut creeps_with_movement: Vec<(ObjectId<Creep>, RoomXY)> = Vec::new();

for creep in room_cache.creeps.creeps.values() {
let Some(id) = creep.try_id() else {
continue;
};

if let Some(creep_dest) = room_cache.traffic.move_requests.get(&id) {
creeps_with_movement.push((id, *creep_dest));
}
}

for (id, target) in creeps_with_movement {
if room_cache.traffic.move_targets.get(&id)
== room_cache.traffic.move_requests.get(&id)
{
continue;
}

room_cache.traffic.movement_map.remove(&target);
room_cache.traffic.move_targets.remove(&id);

let creep = get_object_by_id_typed(&id).unwrap();

if creep.depth_first_searh(room_cache, None) > 0 {
continue;
}

creep.assign_move_target(room_cache, target);
}

for creep in room_cache.creeps.creeps.values() {
let matched_move = room_cache
.traffic
.move_targets
.get(&creep.try_id().unwrap());
if matched_move.is_none() {
continue;
}

if &creep.pos().xy() == matched_move.unwrap() {
continue;
}

let mat = matched_move.unwrap();

let dir = creep
.pos()
.get_direction_to(Position::new(
RoomCoordinate::new(mat.x.u8()).unwrap(),
RoomCoordinate::new(mat.y.u8()).unwrap(),
creep.room().unwrap().name(),
))
.unwrap();
if creep.move_direction(dir).is_ok() {
room_cache.traffic.move_intents += 1;
}
}
}
}
Loading

0 comments on commit 9b41bec

Please sign in to comment.