Skip to content

Commit

Permalink
Sync
Browse files Browse the repository at this point in the history
  • Loading branch information
InfinityDevTech committed May 22, 2024
1 parent 2542c04 commit 6b46fd4
Show file tree
Hide file tree
Showing 20 changed files with 504 additions and 158 deletions.
302 changes: 299 additions & 3 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ serde_json = "1.0.104"
serde = { version = "1.0.183", features = ["derive"] }
regex = "1.9.4"
console_error_panic_hook = "0.1.7"
qcell = "0.5.4"

[dev-dependencies]
wasm-bindgen-test = "0.3"
Expand Down
4 changes: 2 additions & 2 deletions screeps.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ prefix = "ptr"

[copy]
# Windows Desktop
destination = "C:\\Users\\jwjoh\\AppData\\Local\\Screeps\\scripts\\127_0_0_1___21025"
#destination = "C:\\Users\\jwjoh\\AppData\\Local\\Screeps\\scripts\\127_0_0_1___21025"
# Linux Laptop
#destination = "/home/jwjoh/.config/Screeps/scripts/127_0_0_1___21025"
destination = "/home/jwjoh/.config/Screeps/scripts/127_0_0_1___21025"
# Bot arena
#destination = "C:\\Users\\jwjoh\\AppData\\Local\\Screeps\\scripts\\botarena_screepspl_us___21025"

Expand Down
17 changes: 8 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,11 @@ pub fn game_loop() {
if game::time() % 10 == 0 {
for room in game::rooms().values() {
if let Some(controller) = room.controller() {
if controller.my() && !memory.rooms.contains_key(&room.name_str()) {
if controller.my() && !memory.rooms.contains_key(&room.name()) {
plan_room(&room, &mut memory);
}
}
}

for creep in memory.clone().creeps.keys() {
if game::creeps().get(creep.clone()).is_none() {
memory.creeps.remove(&creep.clone());
}
}
}

if just_reset() {
Expand All @@ -57,8 +51,13 @@ pub fn game_loop() {
}
}

for room in memory.clone().rooms.values() {
room::democracy::start_government(game::rooms().get(RoomName::from_str(&room.name).unwrap()).unwrap(), &mut memory);
for room in game::rooms().keys() {
let game_room = game::rooms().get(room).unwrap();
let room_memory = memory.rooms.get(&game_room.name());

if room_memory.is_none() && game_room.my() { plan_room(&game_room, &mut memory); }

room::democracy::start_government(game::rooms().get(room).unwrap(), &mut memory);
}

// Bot is finished, write the stats and local copy of memory.
Expand Down
56 changes: 29 additions & 27 deletions src/memory.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{cmp, collections::HashMap};

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

Expand All @@ -25,25 +26,31 @@ structstruck::strike! {
#[strikethrough[derive(Serialize, Deserialize, Debug, Clone)]]
pub struct CreepMemory{
// Owning room
pub o_r: String,
#[serde(rename = "0")]
pub owning_room: String,
// Path
#[serde(skip_serializing_if = "Option::is_none")]
pub p: Option<String>,
#[serde(rename = "1")]
pub path: Option<String>,
// Career
pub r: Role,
#[serde(rename = "2")]
pub role: Role,
// Needs Energy?
#[serde(skip_serializing_if = "Option::is_none")]
pub n_e: Option<bool>,
#[serde(rename = "3")]
pub needs_energy: Option<bool>,
// This is miner specific, the ID of the link next to it
// If this is empty, then the miner is not linked to a link and it will drop resources on the ground
// If it is, but the link isnt next to it, the miner will clear the link id. If it is, the miner will deposit resources into the link
#[serde(skip_serializing_if = "Option::is_none")]
pub l_id: Option<u8>,
#[serde(rename = "4")]
pub link_id: Option<u8>,
// This is a pointer that changes based on the role of the creep
// 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<u128>,
#[serde(rename = "5")]
pub task_id: Option<u128>,
}
}

Expand All @@ -62,12 +69,19 @@ pub struct RoomMemory{
}>,

pub haul_orders: HashMap<u128, pub struct HaulOrder {
#[serde(rename = "0")]
pub id: u128,
#[serde(rename = "1")]
pub priority: HaulPriorities,
#[serde(rename = "2")]
pub target_id: RawObjectId,
#[serde(rename = "3")]
pub target_type: ResourceType,
#[serde(rename = "4")]
pub responder: Option<String>,
#[serde(rename = "5")]
pub haul_type: HaulType,
#[serde(rename = "6")]
pub amount: u32,
}>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand All @@ -82,7 +96,7 @@ structstruck::strike! {
#[strikethrough[derive(Serialize, Deserialize, Debug, Clone)]]
pub struct ScreepsMemory {
pub mem_version: u8,
pub rooms: HashMap<String, RoomMemory>,
pub rooms: HashMap<RoomName, RoomMemory>,
pub creeps: HashMap<String, CreepMemory>
}
}
Expand All @@ -92,11 +106,13 @@ impl ScreepsMemory {
let memory_jsstring = screeps::raw_memory::get();
let memory_string = memory_jsstring.as_string().unwrap();
if memory_string.is_empty() {

let mut memory = ScreepsMemory {
mem_version: MEMORY_VERSION,
rooms: HashMap::new(),
creeps: HashMap::new(),
};

memory.write_memory();
memory
} else {
Expand Down Expand Up @@ -125,33 +141,19 @@ impl ScreepsMemory {
screeps::raw_memory::set(&js_serialized);
}

pub fn create_creep(&mut self, room_name: &str, creep_name: &str, object: &CreepMemory) {
self.creeps.insert(creep_name.to_string(), object.clone());
pub fn create_creep(&mut self, room_name: &str, creep_name: &str, object: CreepMemory) {
self.creeps.insert(creep_name.to_string(), object);

let room = self.get_room_mut(&RoomName::new(room_name).unwrap());
let room = self.rooms.get_mut(&RoomName::new(room_name).unwrap()).unwrap();
room.creeps.push(creep_name.to_string());
}

pub fn create_room(&mut self, name: &RoomName, object: &RoomMemory) {
pub fn create_room(&mut self, name: &RoomName, object: RoomMemory) {
self.rooms.insert(
name.to_string(),
object.clone()
*name,
object
);
}

pub fn get_room_mut(&mut self, name: &RoomName) -> &mut RoomMemory {
self.rooms.get_mut(&name.to_string()).expect("Failure to resolve room in memory.")
}
pub fn get_creep_mut(&mut self, name: &str) -> &mut CreepMemory {
self.creeps.get_mut(name).expect("Failure to resolve creep in memory.")
}

pub fn get_room(&self, name: &RoomName) -> RoomMemory {
self.rooms.get(&name.to_string()).expect("Failure to resolve room in memory.").clone()
}
pub fn get_creep(&self, name: &str) -> CreepMemory {
self.creeps.get(name).expect("Failure to resolve in memory.").clone()
}
}

impl ScoutedSource {
Expand Down
4 changes: 2 additions & 2 deletions src/movement/creep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ pub fn move_to(creep_name: &String, creep_memory: &mut CreepMemory, target: Posi
let creep = game::creeps().get(creep_name.to_string()).unwrap();
match &creep_memory.movement {
Some(path) => {
move_by_path(creep_name.to_string(), path.clone(), creep_memory)
move_by_path(creep_name.to_string(), path, creep_memory)
}
None => {
let target = MoveTarget {
pos: target,
range: 1,
}.find_path_to(creep.pos());
creep_memory.movement = Some(target.clone());
creep_memory.movement = Some(target);
move_by_path(creep_name.to_string(), target, creep_memory);
}

Expand Down
8 changes: 4 additions & 4 deletions src/room/creeps/local/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use screeps::{find, Creep, HasPosition, ResourceType, SharedCreepProperties};
use crate::{memory::{CreepMemory, ScreepsMemory}, traits::creep::CreepExtensions};

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

if needs_energy || creep.store().get_used_capacity(Some(ResourceType::Energy)) == 0 {
find_energy(creep, creep_memory)
Expand All @@ -17,7 +17,7 @@ pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory) {
pub fn build(creep: &Creep, creepmem: &mut CreepMemory) {
let closest_site = creep.pos().find_closest_by_range(find::CONSTRUCTION_SITES);
if let Some(site) = closest_site {
if creep.pos().is_near_to(site.clone().pos()) {
if creep.pos().is_near_to(site.pos()) {
let _ = creep.build(&site);
} else {
creep.better_move_to(creepmem, site.pos(), 1)
Expand All @@ -30,7 +30,7 @@ pub fn find_energy(creep: &Creep, creepmem: &mut CreepMemory) {
.pos()
.find_closest_by_range(find::DROPPED_RESOURCES);
if let Some(energy) = closest_energy {
if creep.pos().is_near_to(energy.clone().pos()) {
if creep.pos().is_near_to(energy.pos()) {
let _ = creep.pickup(&energy);
} else {
creep.better_move_to(creepmem, energy.pos(), 1)
Expand Down
36 changes: 28 additions & 8 deletions src/room/creeps/local/hauler.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,58 @@
use std::{cmp::min, str::FromStr};

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

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

pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory) {
let creep_memory = memory.get_creep_mut(&creep.name());
let creep_memory = memory.creeps.get(&creep.name()).unwrap();

let order = creep_memory.t_id;
let order = creep_memory.task_id;
if let Some(order_id) = order {
execute_order(creep, memory, order_id)
} else {
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);
let new_order = find_haul_order(creep, memory);
if let Some(order) = new_order {
execute_order(creep, memory, order.id);
}
}
}

pub fn find_haul_order(creep: &Creep, memory: &mut ScreepsMemory) -> Option<HaulOrder> {
let creep_memory = memory.creeps.get(&creep.name()).unwrap();
let room_memory = memory.rooms.get_mut(&RoomName::from_str(&creep_memory.owning_room).unwrap()).unwrap();

let order_list = room_memory.haul_orders.clone();
let mut orders = order_list.values().collect::<Vec<&HaulOrder>>();
orders.sort_by(|a, b| a.priority.cmp(&b.priority));

let unresponded_orders = orders.into_iter().filter(|&order| order.responder.is_none());

if let Some(order) = unresponded_orders.into_iter().next() {
let order = room_memory.haul_orders.get_mut(&order.id).unwrap();
order.add_responder(creep);
let creep_memory = memory.creeps.get_mut(&creep.name()).unwrap();
creep_memory.task_id = Some(order.id);
Some(order.clone())
} else {
None
}
}

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 room_memory = memory.rooms.get_mut(&RoomName::from_str(&memory.creeps.get(&creep.name()).unwrap().owning_room).unwrap()).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());
let creep_memory = memory.creeps.get_mut(&creep.name()).unwrap();
creep.better_move_to(creep_memory, position, 1);
return;
}
Expand Down
Loading

0 comments on commit 6b46fd4

Please sign in to comment.