-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
da01ca3
commit 2542c04
Showing
7 changed files
with
127 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,65 @@ | ||
use std::cmp::min; | ||
use std::{cmp::min, str::FromStr}; | ||
|
||
use screeps::{ | ||
find, game, Creep, HasPosition, ResourceType, SharedCreepProperties, StructureObject, | ||
find, game, Creep, HasPosition, ObjectId, Resource, ResourceType, RoomName, SharedCreepProperties, Structure, StructureObject, Transferable | ||
}; | ||
|
||
use crate::{ | ||
memory::{CreepMemory, ScreepsMemory}, | ||
traits::creep::CreepExtensions, | ||
memory::{CreepMemory, ScreepsMemory}, room, traits::creep::CreepExtensions | ||
}; | ||
|
||
pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory) { | ||
let creep_memory = memory.get_creep_mut(&creep.name()); | ||
let _needs_energy = creep_memory.n_e.unwrap_or(false); | ||
|
||
if creep.store().get_free_capacity(Some(ResourceType::Energy)) > 0 { | ||
get_energy(creep, creep_memory); | ||
let order = creep_memory.t_id; | ||
if let Some(order_id) = order { | ||
execute_order(creep, memory, order_id) | ||
} else { | ||
haul_energy(creep, memory); | ||
} | ||
} | ||
|
||
pub fn get_energy(creep: &Creep, creep_memory: &mut CreepMemory) { | ||
let closest_energy = creep.pos().find_closest_by_range(find::DROPPED_RESOURCES); | ||
if let Some(energy) = closest_energy { | ||
if creep.pos().is_near_to(energy.clone().pos()) { | ||
let _ = creep.pickup(&energy); | ||
} else { | ||
creep.better_move_to(creep_memory, energy.pos(), 1) | ||
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); | ||
if let Some(order) = new_order { | ||
execute_order(creep, memory, order.id); | ||
} | ||
} | ||
} | ||
|
||
pub fn haul_energy(creep: &Creep, screeps_memory: &mut ScreepsMemory) { | ||
let room_memory = screeps_memory.get_room(&creep.room().unwrap().name()); | ||
let creep_memory = screeps_memory.get_creep_mut(&creep.name()); | ||
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 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()); | ||
creep.better_move_to(creep_memory, position, 1); | ||
return; | ||
} | ||
|
||
//let task_id = &room_memory.haul_orders[creep_memory.t_id.unwrap() as usize]; | ||
let task_id = &room_memory.haul_orders[0]; | ||
let target_structure = game::get_object_by_id_typed(&task_id.target_id).unwrap(); | ||
let structure_object = StructureObject::from(target_structure); | ||
match order.haul_type { | ||
crate::room::hauling::HaulType::Pickup => { | ||
let target: Option<Resource> = game::get_object_by_id_typed(&ObjectId::from(pickup_target)); | ||
|
||
if let Some(structure) = structure_object.as_transferable() { | ||
if structure_object | ||
.as_has_store() | ||
.unwrap() | ||
.store() | ||
.get_free_capacity(Some(ResourceType::Energy)) | ||
> 0 | ||
{ | ||
if creep.pos().is_near_to(structure.pos()) { | ||
let _ = creep.transfer( | ||
structure, | ||
ResourceType::Energy, | ||
Some(min( | ||
creep.store().get_used_capacity(Some(ResourceType::Energy)), | ||
structure_object | ||
.as_has_store() | ||
.unwrap() | ||
.store() | ||
.get_free_capacity(Some(ResourceType::Energy)) | ||
as u32, | ||
)), | ||
); | ||
} else { | ||
creep.better_move_to(creep_memory, structure.pos(), 1); | ||
if target.is_none() { | ||
room_memory.destroy_haul_order(order_id); | ||
return; | ||
} | ||
} | ||
|
||
let _ = creep.pickup(&target.unwrap()); | ||
}, | ||
crate::room::hauling::HaulType::Deposit => { | ||
//let target = game::get_object_by_id_erased(&pickup_target); | ||
|
||
//if target.is_none() { | ||
// room_memory.destroy_haul_order(order_id); | ||
// return; | ||
//} | ||
|
||
//let transferrable: &dyn Transferable = target.unwrap().as_ref(); | ||
|
||
//let _ = creep.transfer(target.unwrap().as_ref(), order.target_type, Some(min(creep.store().get_used_capacity(Some(order.target_type)), 32))); | ||
}, | ||
crate::room::hauling::HaulType::Withdraw => todo!(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters