Skip to content

Commit

Permalink
Sync.
Browse files Browse the repository at this point in the history
  • Loading branch information
InfinityDevTech committed Jul 3, 2024
1 parent 68cff48 commit 7fdc517
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 26 deletions.
7 changes: 5 additions & 2 deletions javascript/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ global.pause_exec = function() {
}

global.wipe_memory = function() {
RawMemory._parsed = {};
Memory = {};
Memory.rooms = {}
RawMemory.set(JSON.stringify(Memory));

if (wasm_module) {
wasm_module.wipe_memory();
}

return "[JS] Memory wiped";
}
Expand Down
2 changes: 1 addition & 1 deletion src/combat/setters/remote_reservation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub fn remote_need_reservation(room: &Room, memory: &ScreepsMemory, cache: &Room

let twenty_five = RoomCoordinate::new(25).unwrap();
let center_position = Position::new(twenty_five, twenty_five, room.name());
let owner_center = owning_room_cache.spawn_center.as_position(&room.name());
let owner_center = owning_room_cache.spawn_center.unwrap().as_position(&room.name());

let distance = center_position.get_range_to(owner_center);

Expand Down
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,16 @@ pub fn big_red_button() {
memory.write_memory();
}

#[wasm_bindgen(js_name = wipe_memory)]
pub fn wipe_memory() {
let mut heap_mem = heap().memory.lock().unwrap();

let mut new_mem = ScreepsMemory::init_memory();

new_mem.write_memory();
*heap_mem = new_mem;
}

#[wasm_bindgen(js_name = hauler_rescan)]
pub fn manual_hauler_rescan() {
let mut memory = heap().memory.lock().unwrap();
Expand Down
18 changes: 11 additions & 7 deletions src/room/cache/tick_cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ impl RoomCache {

pub fn create_if_not_exists(&mut self, room: &Room, memory: &mut ScreepsMemory, remote_manager: Option<RoomName>) {
self.rooms.entry(room.name()).or_insert_with(|| {


CachedRoom::new_from_room(room, memory, remote_manager)
});
}
Expand All @@ -52,8 +50,8 @@ pub struct CachedRoom {
pub manager: Option<RoomName>,

pub remotes: Vec<RoomName>,
pub spawn_center: RoomXY,
pub storage_center: RoomXY,
pub spawn_center: Option<RoomXY>,
pub storage_center: Option<RoomXY>,

pub structures: RoomStructureCache,
pub creeps: CreepCache,
Expand Down Expand Up @@ -81,15 +79,21 @@ impl CachedRoom {
let mut stats = StatsCache::default();
stats.energy.spending_spawning = 0;

let mut room_memory = memory.rooms.get_mut(&room.name()).unwrap();
let mut sp_center = None;
let mut st_center = None;

if let Some(room_memory) = memory.rooms.get_mut(&room.name()) {
sp_center = Some(room_memory.spawn_center);
st_center = Some(room_memory.storage_center);
}

let mut cached = CachedRoom {
room_name: room.name(),
manager: remote_manager,
remotes: Vec::new(),

spawn_center: room_memory.spawn_center,
storage_center: room_memory.storage_center,
spawn_center: sp_center,
storage_center: st_center,

structures,
creeps: CreepCache::new_from_room(room, memory),
Expand Down
2 changes: 1 addition & 1 deletion src/room/creeps/local/fast_filler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub fn find_possible_targets(creep: &Creep, cache: &CachedRoom) -> Vec<RawObject
#[cfg_attr(feature = "profile", screeps_timing_annotate::timing)]
pub fn check_current_position(creep: &Creep, creep_memory: &mut CreepMemory, cache: &mut CachedRoom) -> bool {
let current_pos = creep.pos().xy();
let spawn_pos = cache.spawn_center;
let spawn_pos = cache.spawn_center.unwrap();

let position_1 = RoomPosition::new(
spawn_pos.x.u8() + 1,
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 @@ -31,7 +31,7 @@ pub fn run_upgrader(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomC
pub fn get_energy(creep: &Creep, creep_memory: &mut CreepMemory, cached_room: &mut CachedRoom) -> bool {
let controller = cached_room.structures.controller.as_ref().unwrap();

if (creep.store().get_used_capacity(Some(ResourceType::Energy)) as f32) < (creep.store().get_capacity(Some(ResourceType::Energy)) as f32 * 0.5) {
if (creep.store().get_used_capacity(Some(ResourceType::Energy)) as f32) < (creep.store().get_capacity(Some(ResourceType::Energy)) as f32 * 0.75) {
if let Some(controller_link) = cached_room.structures.links.controller.as_ref() {
if controller_link.store().get_used_capacity(Some(ResourceType::Energy)) > 0 {

Expand Down
14 changes: 7 additions & 7 deletions src/room/democracy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ pub fn start_government(room: Room, memory: &mut ScreepsMemory, cache: &mut Room
return;
}

if room.my() && !memory.rooms.contains_key(&room.name()) && !plan_room(&room, memory, cache) {
return;
}

visualise_room_visual(&room.name());
// Caches various things, like resources
// Caches structures and other creep things
Expand All @@ -41,10 +45,6 @@ pub fn start_government(room: Room, memory: &mut ScreepsMemory, cache: &mut Room
if room.my() {
info!("[GOVERNMENT] Starting government for room: {}", room.name());

if !memory.rooms.contains_key(&room.name()) && !plan_room(&room, memory, cache) {
return;
}

if !memory.rooms.contains_key(&room.name()) || !cache.rooms.contains_key(&room.name()) {
return;
}
Expand Down Expand Up @@ -106,7 +106,7 @@ pub fn start_government(room: Room, memory: &mut ScreepsMemory, cache: &mut Room

let room_memory = memory.rooms.get_mut(&room.name()).unwrap();

if room_memory.remotes.len() < 5 || game::time() % 3000 == 0 || lifetime == 0 && game::cpu::bucket() > 1000 {
if room_memory.remotes.len() < 5 || game::time() % 3000 == 0 || lifetime == 0 && game::cpu::bucket() > 5000 {
let remotes = remotes::fetch_possible_remotes(&room, memory, room_cache);

info!("Setting remotes for room: {} - {:?}", room.name(), remotes);
Expand Down Expand Up @@ -183,10 +183,10 @@ pub fn run_crap_planner_code(room: &Room, memory: &mut ScreepsMemory, room_cache
if game::cpu::bucket() > 1000 && game::time() % 1000 == 0 {
let stuffs = get_roads_and_ramparts();

let pos = room_cache.spawn_center;
let pos = room_cache.spawn_center.unwrap();

let offset_x = pos.x;
let offset_y = unsafe { RoomCoordinate::unchecked_new(pos.y.u8() - 1) };
let offset_y = unsafe { RoomCoordinate::unchecked_new(pos.y.u8() + 1) };

let should_rampart = room_cache.structures.storage.is_some() && room_cache.structures.controller.as_ref().unwrap().controller.level() >= 4;
let should_road = room_cache.structures.controller.as_ref().unwrap().controller.level() > 3;
Expand Down
8 changes: 5 additions & 3 deletions src/room/planning/room/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ pub fn plan_room(room: &Room, memory: &mut ScreepsMemory, cache: &mut RoomCache)

let mut spawn = my_spawn.first().unwrap();

let spawn = unsafe { RoomXY::unchecked_new(spawn.pos().x().u8(), spawn.pos().y().u8() - 1) };

let store_pos = if my_storage.is_empty() {
let spawn_x = spawn.pos().x().u8();
let spawn_y = spawn.pos().y().u8();
let spawn_x = spawn.x.u8();
let spawn_y = spawn.y.u8();

unsafe { RoomXY::unchecked_new(spawn_x + 1, spawn_y + 3) }
} else {
Expand All @@ -46,7 +48,7 @@ pub fn plan_room(room: &Room, memory: &mut ScreepsMemory, cache: &mut RoomCache)
creeps: Vec::new(),
remotes: Vec::new(),

spawn_center: spawn.pos().xy(),
spawn_center: spawn,
storage_center: store_pos,

hauler_count: 0,
Expand Down
2 changes: 1 addition & 1 deletion src/room/planning/room/remotes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub fn rank_remote_room(
remote_room: &RoomName,
) -> u32 {
// If our room doesnt have a spawn placed yet.
let spawn_pos = room_cache.spawn_center.as_position(&room_cache.room_name);
let spawn_pos = room_cache.spawn_center.unwrap().as_position(&room_cache.room_name);
let mut i = 0;
let mut current_avg = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/room/planning/room/roads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn plan_main_room_roads(room: &Room, cache: &CachedRoom, memory: &mut Screep
let origin_position = if cache.structures.storage.is_some() {
cache.structures.storage.as_ref().unwrap().pos()
} else {
let pos = cache.spawn_center;
let pos = cache.spawn_center.unwrap();

let y = pos.y.u8();

Expand Down
4 changes: 2 additions & 2 deletions src/room/spawning/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ pub fn hauler(

(out_steps, in_steps)
} else {
let spawn = owning_cache.spawn_center.as_position(&owning_cache.room_name);
let spawn = owning_cache.spawn_center.unwrap().as_position(&owning_cache.room_name);

let mut out_target = MoveTarget { pos: source.pos(), range: 1 };
let mut in_target = MoveTarget { pos: spawn, range: 1 };
Expand Down Expand Up @@ -324,7 +324,7 @@ pub fn hauler(

(out_steps, in_steps)
} else {
let spawn = owning_cache.spawn_center.as_position(&owning_cache.room_name);
let spawn = owning_cache.spawn_center.unwrap().as_position(&owning_cache.room_name);

let mut out_target = MoveTarget { pos: source.pos(), range: 1 };
let mut in_target = MoveTarget { pos: spawn, range: 1 };
Expand Down
4 changes: 4 additions & 0 deletions src/room/spawning/spawn_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ pub fn run_spawning(memory: &mut ScreepsMemory, cache: &mut RoomCache) {

fn randomize_top_priorities(requests: Vec<SpawnRequest>) -> Vec<SpawnRequest> {
let mut top_scorers = Vec::new();
if requests.is_empty() {
return top_scorers;
}

let top_scorer = requests.first().unwrap().priority;

for request in requests {
Expand Down

0 comments on commit 7fdc517

Please sign in to comment.