Skip to content

Commit

Permalink
Made source miners use the hauling system. Fast fillers got tweaks. F…
Browse files Browse the repository at this point in the history
…ixed spawn percentage.
  • Loading branch information
InfinityDevTech committed Jun 2, 2024
1 parent fcffa09 commit 591b29e
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 81 deletions.
1 change: 0 additions & 1 deletion javascript/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ module.exports.loop = function () {
global.Memory = global.TempMemory;

if (wasm_module) {
console.log("running");
wasm_module.game_loop();
} else {
console.log("[JS] Module not loaded... loading");
Expand Down
128 changes: 101 additions & 27 deletions src/room/creeps/local/fast_filler.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
use screeps::{find, game, look, Creep, HasId, HasPosition, ObjectId, RawObjectId, ResourceType, Room, RoomPosition, RoomXY, SharedCreepProperties, StructureContainer, StructureExtension, StructureObject, StructureProperties, StructureType};
use screeps::{
find, game, look, Creep, HasId, HasPosition, MaybeHasId, ObjectId, RawObjectId, ResourceType,
Room, RoomPosition, RoomXY, SharedCreepProperties, StructureContainer, StructureExtension,
StructureObject, StructureProperties, StructureType,
};

use wasm_bindgen::JsCast;

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

pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCache) {
let fastfiller_container = memory.creeps.get_mut(&creep.name()).unwrap().fastfiller_container;
let fastfiller_container = memory
.creeps
.get_mut(&creep.name())
.unwrap()
.fastfiller_container;

if creep.spawning() || creep.tired() {
let _ = creep.say("😴", false);
Expand All @@ -16,26 +31,46 @@ pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCach

if fastfiller_container.is_none() {
if let Some(container_id) = find_container(creep, memory, cache) {
memory.creeps.get_mut(&creep.name()).unwrap().fastfiller_container = Some(container_id);
} else {
return;
memory
.creeps
.get_mut(&creep.name())
.unwrap()
.fastfiller_container = Some(container_id);
}
}

let creep_memory = memory.creeps.get_mut(&creep.name()).unwrap();

if creep.store().get_used_capacity(Some(ResourceType::Energy)) == 0 {
let _ = creep.say("WTHD", false);
let container_id = creep_memory.fastfiller_container.unwrap();
let container = game::get_object_by_id_typed(&container_id).unwrap();

if creep.pos().is_near_to(container.pos()) {
let _ = creep.withdraw(&container, ResourceType::Energy, None);
let container_id = creep_memory.fastfiller_container;
if container_id.is_none() {
cache.hauling.create_order(
creep.try_raw_id().unwrap(),
ResourceType::Energy,
creep.store().get_free_capacity(Some(ResourceType::Energy)) as u32,
HaulingPriority::Spawning,
HaulingType::Transfer,
);
} else {
creep.better_move_to(creep_memory, cache, container.pos(), 1);
}
let container = game::get_object_by_id_typed(&container_id.unwrap());

return;
// Container gets destroyed...
if container.is_none() {
creep_memory.fastfiller_container = None;
return;
}

let container = container.unwrap();

if creep.pos().is_near_to(container.pos()) {
let _ = creep.withdraw(&container, ResourceType::Energy, None);
} else {
creep.better_move_to(creep_memory, cache, container.pos(), 1);
}

return;
}
}

let possible_targets = find_possible_targets(creep, cache);
Expand All @@ -47,11 +82,14 @@ pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCach
let target = game::get_object_by_id_erased(&target_id).unwrap();

if creep.pos().is_near_to(target.pos()) {
let _ = creep.transfer(target.unchecked_ref::<StructureExtension>(), ResourceType::Energy, None);
let _ = creep.transfer(
target.unchecked_ref::<StructureExtension>(),
ResourceType::Energy,
None,
);
} else {
creep.better_move_to(creep_memory, cache, target.pos(), 1);
}

}

pub fn self_renew(creep: &Creep, cache: &mut RoomCache) {
Expand All @@ -70,7 +108,11 @@ pub fn find_possible_targets(creep: &Creep, cache: &RoomCache) -> Vec<RawObjectI
for target in find_call {
match target {
StructureObject::StructureExtension(extension) => {
if extension.store().get_free_capacity(Some(ResourceType::Energy)) > 0 {
if extension
.store()
.get_free_capacity(Some(ResourceType::Energy))
> 0
{
possible_targets.push(extension.raw_id());
}
}
Expand All @@ -87,23 +129,55 @@ pub fn find_possible_targets(creep: &Creep, cache: &RoomCache) -> Vec<RawObjectI
possible_targets
}

pub fn find_container(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCache) -> Option<ObjectId<StructureContainer>> {
pub fn find_container(
creep: &Creep,
memory: &mut ScreepsMemory,
cache: &mut RoomCache,
) -> Option<ObjectId<StructureContainer>> {
let possible_containers = creep.pos().find_in_range(find::STRUCTURES, 1);

let current_pos = creep.pos().xy();
let spawn_pos = cache.structures.spawns.values().next().unwrap().pos().xy();

let position_1 = RoomPosition::new(spawn_pos.x.u8() + 1, spawn_pos.y.u8() - 1, creep.room().unwrap().name());
let position_2 = RoomPosition::new(spawn_pos.x.u8() - 1, spawn_pos.y.u8() - 1, creep.room().unwrap().name());

if current_pos != unsafe{RoomXY::unchecked_new(position_1.x(), position_1.y())} || current_pos != unsafe{RoomXY::unchecked_new(position_2.x(), position_2.y())} {
let pos_1_creep = creep.room().unwrap().look_for_at_xy(look::CREEPS, position_1.x(), position_1.y());
let pos_2_creep = creep.room().unwrap().look_for_at_xy(look::CREEPS, position_2.x(), position_2.y());
let position_1 = RoomPosition::new(
spawn_pos.x.u8() + 1,
spawn_pos.y.u8() - 1,
creep.room().unwrap().name(),
);
let position_2 = RoomPosition::new(
spawn_pos.x.u8() - 1,
spawn_pos.y.u8() - 1,
creep.room().unwrap().name(),
);

if current_pos != unsafe { RoomXY::unchecked_new(position_1.x(), position_1.y()) }
|| current_pos != unsafe { RoomXY::unchecked_new(position_2.x(), position_2.y()) }
{
let pos_1_creep =
creep
.room()
.unwrap()
.look_for_at_xy(look::CREEPS, position_1.x(), position_1.y());
let pos_2_creep =
creep
.room()
.unwrap()
.look_for_at_xy(look::CREEPS, position_2.x(), position_2.y());

if pos_1_creep.is_empty() {
creep.better_move_to(memory.creeps.get_mut(&creep.name()).unwrap(), cache, position_1.into(), 0);
creep.better_move_to(
memory.creeps.get_mut(&creep.name()).unwrap(),
cache,
position_1.into(),
0,
);
} else if pos_2_creep.is_empty() {
creep.better_move_to(memory.creeps.get_mut(&creep.name()).unwrap(), cache, position_2.into(), 0);
creep.better_move_to(
memory.creeps.get_mut(&creep.name()).unwrap(),
cache,
position_2.into(),
0,
);
}
}

Expand All @@ -114,4 +188,4 @@ pub fn find_container(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut Roo
}

None
}
}
102 changes: 76 additions & 26 deletions src/room/creeps/local/hauler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::vec;

use log::info;
use screeps::{
game, Creep, HasPosition, ObjectId, Resource, ResourceType, SharedCreepProperties, StructureStorage
game, Creep, HasPosition, ObjectId, Resource, ResourceType, SharedCreepProperties,
StructureStorage,
};

use wasm_bindgen::JsCast;
Expand All @@ -19,26 +21,73 @@ pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCach
}
let creep_name = creep.name();

let needs_energy = memory.creeps.get(&creep_name).unwrap().needs_energy.unwrap_or(false);
let needs_energy = memory
.creeps
.get(&creep_name)
.unwrap()
.needs_energy
.unwrap_or(false);

if let Some(order) = &memory.creeps.get(&creep_name).unwrap().hauling_task.clone() {
let _ = creep.say("EXEC", false);
execute_order(creep, memory.creeps.get_mut(&creep_name).unwrap(), cache, order);
execute_order(
creep,
memory.creeps.get_mut(&creep_name).unwrap(),
cache,
order,
);
} else {
let new_order = if creep.store().get_used_capacity(Some(ResourceType::Energy)) == 0 {
cache.hauling.find_new_order(creep, memory, None, vec![HaulingType::Pickup, HaulingType::Withdraw, HaulingType::Offer])
cache.hauling.find_new_order(
creep,
memory,
None,
vec![
HaulingType::Pickup,
HaulingType::Withdraw,
HaulingType::Offer,
],
)
} else {
cache.hauling.find_new_order(creep, memory, None, vec![HaulingType::Transfer])
cache
.hauling
.find_new_order(creep, memory, None, vec![HaulingType::Transfer])
};

if let Some(order) = new_order {
let _ = creep.say("EXEC", false);
execute_order(creep, memory.creeps.get_mut(&creep.name()).unwrap(), cache, &order);
execute_order(
creep,
memory.creeps.get_mut(&creep.name()).unwrap(),
cache,
&order,
);

let creep_memory = memory.creeps.get_mut(&creep_name).unwrap();

// Yes, I know this is primitive, but it works for now
// Im forcing it to fetch a new task if its full or empty
// TODO: Refactor this, it's ugly

if creep.store().get_free_capacity(None) == 0 {
creep_memory.hauling_task = None;
creep_memory.needs_energy = None;
}

if creep.store().get_used_capacity(None) == 0 {
creep_memory.hauling_task = None;
creep_memory.needs_energy = Some(true);
}
}
}
}

pub fn execute_order(creep: &Creep, creep_memory: &mut CreepMemory, cache: &mut RoomCache, order: &CreepHaulTask) {
pub fn execute_order(
creep: &Creep,
creep_memory: &mut CreepMemory,
cache: &mut RoomCache,
order: &CreepHaulTask,
) {
let pickup_target = order.target_id;
let target = game::get_object_by_id_erased(&pickup_target);
let position = order.get_target_position();
Expand All @@ -60,7 +109,8 @@ pub fn execute_order(creep: &Creep, creep_memory: &mut CreepMemory, cache: &mut
HaulingType::Pickup => {
let _ = creep.say("PKUP", false);

let resource: Option<Resource> = game::get_object_by_id_typed(&ObjectId::from(pickup_target));
let resource: Option<Resource> =
game::get_object_by_id_typed(&ObjectId::from(pickup_target));
if let Some(resource) = resource {
let _ = creep.pickup(&resource);
success = true;
Expand All @@ -70,8 +120,15 @@ pub fn execute_order(creep: &Creep, creep_memory: &mut CreepMemory, cache: &mut
let _ = creep.say("WTHD", false);

if let Some(target) = target {
let amount = std::cmp::min(creep.store().get_free_capacity(Some(ResourceType::Energy)), order.amount as i32);
let _ = creep.withdraw(target.unchecked_ref::<StructureStorage>(), order.resource, Some(amount.try_into().unwrap()));
let amount = std::cmp::min(
creep.store().get_free_capacity(Some(ResourceType::Energy)),
order.amount as i32,
);
let _ = creep.withdraw(
target.unchecked_ref::<StructureStorage>(),
order.resource,
Some(amount.try_into().unwrap()),
);
success = true;
}
}
Expand All @@ -91,8 +148,15 @@ pub fn execute_order(creep: &Creep, creep_memory: &mut CreepMemory, cache: &mut
let _ = creep.say("OFFR", false);

if let Some(target) = target {
let amount = std::cmp::min(creep.store().get_free_capacity(Some(order.resource)), order.amount as i32);
let _ = creep.withdraw(target.unchecked_ref::<StructureStorage>(), order.resource, Some(amount.try_into().unwrap()));
let amount = std::cmp::min(
creep.store().get_free_capacity(Some(order.resource)),
order.amount as i32,
);
let _ = creep.withdraw(
target.unchecked_ref::<StructureStorage>(),
order.resource,
Some(amount.try_into().unwrap()),
);
success = true;
}
}
Expand All @@ -101,18 +165,4 @@ pub fn execute_order(creep: &Creep, creep_memory: &mut CreepMemory, cache: &mut
if success {
creep_memory.hauling_task = None;
}

// Yes, I know this is primitive, but it works for now
// Im forcing it to fetch a new task if its full or empty
// TODO: Refactor this, it's ugly

if creep.store().get_free_capacity(None) == 0 {
creep_memory.hauling_task = None;
creep_memory.needs_energy = None;
}

if creep.store().get_used_capacity(None) == 0 {
creep_memory.hauling_task = None;
creep_memory.needs_energy = Some(true);
}
}
Loading

0 comments on commit 591b29e

Please sign in to comment.