Skip to content

Commit

Permalink
tes
Browse files Browse the repository at this point in the history
  • Loading branch information
InfinityDevTech committed May 16, 2024
1 parent db0d325 commit 8a55cb9
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 209 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub fn game_loop() {
for room in game::rooms().values() {
if let Some(controller) = room.controller() {
if controller.my() && !memory.get_room(&room.name()).init {
memory.create_room(&room.name_str());
memory.create_room(&room.name());
}
}
}
Expand All @@ -77,7 +77,7 @@ pub fn game_loop() {
}
if let Some(controller) = room.controller() {
if controller.my() {
memory.create_room(&room.name_str());
memory.create_room(&room.name());
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub struct RoomMemory{
// Initialised
pub init: bool,
// Mining stuffs
pub sources: HashMap<ObjectId<Source>, pub struct {
pub sources: HashMap<ObjectId<Source>, pub struct ScoutedSource {
pub mining_spots: u8,
pub assigned_creeps: u8,
}>,
Expand Down Expand Up @@ -114,7 +114,7 @@ impl ScreepsMemory {
info!("Created creep");
}

pub fn create_room(&mut self, name: &str) {
pub fn create_room(&mut self, name: &RoomName) {
self.rooms.insert(
name.to_string(),
RoomMemory {
Expand Down
72 changes: 0 additions & 72 deletions src/room/census.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/room/creeps/local/source_miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::{memory::ScreepsMemory, traits::creep::CreepExtensions, utils::creep:

pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory) {
let owning_room = memory.get_creep(&creep.name()).o_r.clone();
if creep.pos().is_near_to(source.pos()) {

if creep.pos().is_near_to(source.pos()) {
if creep.store().get_free_capacity(Some(ResourceType::Energy)) > creep.store().get_used_capacity(Some(ResourceType::Energy)) as i32 {

let _ = creep.drop(ResourceType::Energy, Some(creep.store().get_used_capacity(Some(ResourceType::Energy))));
Expand Down
2 changes: 1 addition & 1 deletion src/room/creeps/local/upgrader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use screeps::{Creep, StructureController, HasPosition, ResourceType, find};

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

pub fn run_creep(creep: &Creep, creepmem: &mut CreepMemory, controller: StructureController) {
pub fn run_creep(creep: &Creep, creepmem: &mut CreepMemory) {
let inventory = creep.store();
if creep.store().get_used_capacity(Some(ResourceType::Energy)) == 0 {
let closest_energy = creep
Expand Down
4 changes: 2 additions & 2 deletions src/room/creeps/organizer.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use log::info;
use screeps::{creep, game, Room};

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

use super::local;

pub fn run_creeps(room: &Room, memory: &mut ScreepsMemory) {
info!("[CREEPS] Running creeps in room: {}", room.name_str());
info!("[CREEPS] Running creeps in room: {}", room.name());
let creeps = memory.get_room(&room.name()).creeps.clone();

for creep_name in creeps {
Expand Down
151 changes: 34 additions & 117 deletions src/room/democracy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,134 +2,51 @@ use std::{collections::HashMap, str::FromStr};

use log::info;
use screeps::{
find, game,
look::{self, LookResult},
HasPosition, ObjectId, Part, Room,
find, game, look::{self, LookResult}, HasId, HasPosition, ObjectId, Part, Room, Terrain
};

use crate::{
memory::{, ScreepsMemory},
room::census, traits::room::RoomExtensions,
};

use super::{creeps, tower};

const UPGRADER_COUNT: u8 = 8;
const BUILDER_COUNT: u8 = 4;
use crate::{memory::{ScoutedSource, ScreepsMemory}, traits::room::RoomExtensions};

pub fn start_government(room: Room, memory: &mut ScreepsMemory) {
// Horray, i did it better.
let roommem = memory.get_room(&room.name_str());
let roommem = memory.get_room(&room.name());

if !roommem.init {
if game::cpu::bucket() >= 100 {
info!("Initialising room: {}", room.name().to_string());
let sources = room.find(find::SOURCES, None);
let mut mining_spots = HashMap::new();
for spot in sources {
let x = spot.pos().x().u8();
let y = spot.pos().y().u8();
let areas = room.look_for_at_area(look::TERRAIN, y - 1, x - 1, y + 1, x + 1);
let mut available_spots = 0;
for area in areas {
if let LookResult::Terrain(screeps::Terrain::Plain) = area.look_result {
available_spots += 1;
} else if let LookResult::Terrain(screeps::Terrain::Swamp) = area.look_result {
available_spots += 1;
}
}
mining_spots.insert(
spot.id(),
Mine {
u: 0,
s: available_spots,
},
);
}
roommem.available_mining = mining_spots.len() as u8;
roommem.mine = mining_spots;
roommem.init = true;
} else {
info!(
"CPU bucket is too low to initialise room: {}",
room.name().to_string()
);
return;
}
}
if game::cpu::bucket() >= 100 {
info!("Initialising room: {}", room.name().to_string());
let sources = room.find(find::SOURCES, None);
let mut mining_sources = HashMap::new();

creeps::organizer::run_creeps(&room, memory);
tower::run_towers(&room);
for source in sources {

do_spawning(memory, &room);
memory.stats.cpu.rooms += game::cpu::get_used() - starting_cpu;
memory.stats.rooms.get_mut(&room.name_str()).unwrap().cpu += game::cpu::get_used() - starting_cpu;
}
let x = source.pos().x().u8();
let y = source.pos().y().u8();
let areas = room.look_for_at_area(look::TERRAIN, y - 1, x - 1, y + 1, x + 1);
let mut available_spots = 0;

pub fn do_spawning(memory: &mut ScreepsMemory, room: &Room) {
let binding = memory.clone();
let roommem_readonly = binding.rooms.get(&room.name_str()).unwrap();
let binding = room.find(find::MY_SPAWNS, None);
let spawn = binding.first().unwrap();
let room_name = &room.name_str();
for area in areas {
match area.look_result {
LookResult::Terrain(Terrain::Plain) => available_spots += 1,
LookResult::Terrain(Terrain::Swamp) => available_spots += 1,
_ => {}
}
}

if census::create_miner(memory, room.clone()) {
} else if memory.get_room(&room.name_str()).get_creeps_by_role("hauler").len() < 5 {
let name = format!("h-{}", roommem_readonly.creeps_made);
let body = [Part::Move, Part::Move, Part::Carry, Part::Work];
let spawn_res = spawn.spawn_creep(&body, &name);
if spawn_res.is_ok() {
memory.create_creep(
room_name,
&name,
crate::memory::Careers::Mining,
Some(Task::Hauler(
ObjectId::from_str(
&room
.find(find::MY_SPAWNS, None)
.first()
.unwrap()
.id()
.to_string(),
)
.unwrap(),
)),
);
memory.get_room(&room.name_str()).creeps.insert(name.to_string(), "hauler".to_string());
memory.get_room(&room.name_str()).creeps_made += 1;
memory.stats.rooms.get_mut(&room.name_str()).unwrap().creeps_made += 1;
}
} else if (memory.get_room(&room.name_str()).get_creeps_by_role("upgrader").len() as u8) < UPGRADER_COUNT {
let name = format!("u-{}", roommem_readonly.creeps_made);
let body = [Part::Move, Part::Move, Part::Carry, Part::Carry, Part::Work];
let spawn_res = spawn.spawn_creep(&body, &name);
if spawn_res.is_ok() {
memory.create_creep(
room_name,
&name,
crate::memory::Careers::Mining,
Some(Task::Upgrader(
ObjectId::from_str(&room.controller().unwrap().id().to_string()).unwrap(),
)),
);
memory.get_room(&room.name().to_string()).creeps.insert(name.to_string(), "upgrader".to_string());
memory.get_room(&room.name_str()).creeps_made += 1;
memory.stats.rooms.get_mut(&room.name_str()).unwrap().creeps_made += 1;
}
} else if (memory.get_room(&room.name_str()).get_creeps_by_role("builder").len() as u8) < BUILDER_COUNT {
let name = format!("b-{}", roommem_readonly.creeps_made);
let body = [Part::Move, Part::Carry, Part::Carry, Part::Work];
let spawn_res = spawn.spawn_creep(&body, &name);
if spawn_res.is_ok() {
memory.create_creep(
room_name,
&name,
crate::memory::Careers::Mining,
Some(Task::Builder()),
mining_sources.insert(
source.id(),
ScoutedSource {
assigned_creeps: 0,
mining_spots: available_spots as u8,
},
);
memory.get_room(&room.name_str()).creeps.insert(name.to_string(), "builder".to_string());
memory.get_room(&room.name_str()).creeps_made += 1;
memory.stats.rooms.get_mut(&room.name_str()).unwrap().creeps_made += 1;
}

roommem.sources = mining_sources;
roommem.init = true;
} else {
info!(
"CPU bucket is too low to initialise room: {}",
room.name().to_string()
);
return;
}
}
1 change: 0 additions & 1 deletion src/room/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod democracy;
pub mod census;
pub mod building;
pub mod creeps;
pub mod tower;
9 changes: 0 additions & 9 deletions src/traits/creep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ pub trait CreepExtensions {
// Movement
fn better_move_by_path(&self, path: String, memory: &mut CreepMemory);
fn better_move_to(&self, creep_memory: &mut CreepMemory, target: Position, range: u16);

fn better_is_near(&self, x: u8, y: u8) -> u8;
}

impl CreepExtensions for screeps::Creep {
Expand Down Expand Up @@ -97,11 +95,4 @@ impl CreepExtensions for screeps::Creep {
}
}
}

fn better_is_near(&self, x: u8, y: u8) -> u8 {
let creep_x = self.pos().x().u8();
let creep_y = self.pos().y().u8();
let int = (creep_x - x).pow(2) + (creep_y - y).pow(2);
(((int as f64).sqrt() * 10.0).round() / 10.0) as u8
}
}
3 changes: 2 additions & 1 deletion src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod creep;
pub mod room;
pub mod room;
pub mod utils;
5 changes: 5 additions & 0 deletions src/utils/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub fn distance_formula(source: (u32, u32), target: (u32, u32)) -> u32 {
let x = source.0 as i32 - target.0 as i32;
let y = source.1 as i32 - target.1 as i32;
((x.pow(2) + y.pow(2)) as f64).sqrt() as u32
}
2 changes: 1 addition & 1 deletion src/visual/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub fn classify_rooms(memory: &ScreepsMemory) {
);
MapVisual::text(
pos,
get_room_type(&room.name_str()),
get_room_type(&room.name()),
Some(
TextStyle::default()
.color("#ffffff")
Expand Down

0 comments on commit 8a55cb9

Please sign in to comment.