Skip to content

Commit

Permalink
Change weights of hauling orders, add signs against marvin.
Browse files Browse the repository at this point in the history
  • Loading branch information
InfinityDevTech committed Jun 2, 2024
1 parent 6dcf31b commit d3997e7
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 33 deletions.
29 changes: 29 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
pub const MEMORY_VERSION: u8 = 1;

pub const ROOM_SIGNS: [&str; 21] = [
//"Rust programming is just crab game.",
//"Web Assembly is overrated",
//"Warning: This room is under the control of an idiot.",
//"Why did the creep cross the road? To escape a tigga quad!",
"Marvin lies!",
//"Kick me.",
"Marx would be dissapointed",
"Made a mess and the war got cold.",
"Bourgeoisie member.",
"The international will be defeated.",
"We must stop Marvin at all costs!",
"Pride of Lenin took Trotsky out of the picture.",
"Stop the revolution!",
"Tore down that wall like the koolaid man.",
"Communism, everyones mortal enemy.",
"Stop the red iceberg!",
"Cease the Collectivization!",
"Its not Communism if its under Marvin.",
"Communism != Collectivization",
"Why did the creeps cross the road. They were under Marvin's collectivized control.",
"Real communism requires individual control.",
"The top 1% dont control as much as Marvin. Stop the collectivization!",
"Marvin is a collectivized menace.",
"Workers of the world, unite! Against Marvin.",
"Screeps bots spend a combined 13 years of ther life under a dictatorship: Marvin.",
"We already eat from the trashcan all the time. The name of this trash is collectivization - Infinity Dev"
];

// Hate weights, determins how much weight is adder per event.
pub const HATE_CREEP_ATTACK_WEIGHT: f32 = 1.0;
pub const HATE_CREEP_HEAL_WEIGHT: f32 = -0.5;
Expand Down
88 changes: 65 additions & 23 deletions src/room/cache/tick_cache/structures.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use std::{cmp, collections::HashMap};

use screeps::{
find, game, look::{self, LookResult}, source, ConstructionSite, Creep, HasId, HasPosition, LocalRoomTerrain, ObjectId, OwnedStructureProperties, Part, ResourceType, Room, Ruin, Source, StructureContainer, StructureController, StructureExtension, StructureLink, StructureObject, StructureRoad, StructureSpawn, StructureTower, Terrain
find, game,
look::{self, LookResult},
ConstructionSite, Creep, HasId, HasPosition, LocalRoomTerrain, ObjectId,
OwnedStructureProperties, Part, ResourceType, Room, Ruin, Source, StructureContainer,
StructureController, StructureExtension, StructureLink, StructureObject, StructureRoad,
StructureSpawn, StructureTower, Terrain,
};

use crate::{memory::ScreepsMemory, room::cache::heap_cache::RoomHeapCache};
Expand Down Expand Up @@ -48,7 +53,11 @@ pub struct RoomStructureCache {
}

impl RoomStructureCache {
pub fn new_from_room(room: &Room, _memory: &mut ScreepsMemory, heap_cache: &mut RoomHeapCache) -> RoomStructureCache {
pub fn new_from_room(
room: &Room,
_memory: &mut ScreepsMemory,
heap_cache: &mut RoomHeapCache,
) -> RoomStructureCache {
let mut cache = RoomStructureCache {
all_structures: Vec::new(),
construction_sites: Vec::new(),
Expand Down Expand Up @@ -155,36 +164,69 @@ impl RoomStructureCache {
if controller.container.is_some() {
let controller_container = controller.container.as_ref().unwrap();
if container.id() == controller_container.id() {

pull = false;
}
}
}

if pull && used_capacity > 0 {
hauling.create_order(
container.raw_id(),
ResourceType::Energy,
container
.store()
.get_used_capacity(Some(ResourceType::Energy)),
HaulingPriority::Energy,
HaulingType::Offer,
);
if container
.pos()
.get_range_to(self.spawns.values().next().unwrap().pos())
<= 3
{
hauling.create_order(
container.raw_id(),
ResourceType::Energy,
container
.store()
.get_used_capacity(Some(ResourceType::Energy)),
HaulingPriority::Minerals,
HaulingType::Offer,
);
} else {
hauling.create_order(
container.raw_id(),
ResourceType::Energy,
container
.store()
.get_used_capacity(Some(ResourceType::Energy)),
HaulingPriority::Energy,
HaulingType::Offer,
);
}
}

if !matching && (used_capacity as f32) <= (max_capacity as f32 * 0.5) {
hauling.create_order(
container.raw_id(),
ResourceType::Energy,
container
.store()
.get_free_capacity(Some(ResourceType::Energy))
.try_into()
.unwrap(),
HaulingPriority::Energy,
HaulingType::Transfer,
);
if container
.pos()
.get_range_to(self.spawns.values().next().unwrap().pos())
<= 3
{
hauling.create_order(
container.raw_id(),
ResourceType::Energy,
container
.store()
.get_free_capacity(Some(ResourceType::Energy))
.try_into()
.unwrap(),
HaulingPriority::Spawning,
HaulingType::Transfer,
);
} else {
hauling.create_order(
container.raw_id(),
ResourceType::Energy,
container
.store()
.get_free_capacity(Some(ResourceType::Energy))
.try_into()
.unwrap(),
HaulingPriority::Energy,
HaulingType::Transfer,
);
}
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/room/cache/tick_cache/traffic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@

use log::info;
use screeps::{
game::{self, get_object_by_id_typed}, Creep, Direction, HasPosition, MaybeHasId, ObjectId, Position, RoomCoordinate, RoomXY
game::{self, get_object_by_id_typed}, Creep, HasPosition, MaybeHasId, ObjectId, Position, RoomCoordinate, RoomXY
};

use rand::prelude::SliceRandom;

use super::RoomCache;
use crate::{movement::utils::dir_to_coords, traits::creep::CreepExtensions};
use crate::traits::creep::CreepExtensions;

pub struct TrafficCache {
pub move_targets: HashMap<ObjectId<Creep>, RoomXY>,
Expand Down
2 changes: 1 addition & 1 deletion src/room/creeps/local/hauler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCach
let _ = creep.say("EXEC", false);
execute_order(creep, memory.creeps.get_mut(&creep_name).unwrap(), cache, order);
} else {
let new_order = if needs_energy {
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])
} else {
cache.hauling.find_new_order(creep, memory, None, vec![HaulingType::Transfer])
Expand Down
44 changes: 40 additions & 4 deletions src/room/creeps/local/upgrader.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
use screeps::{Creep, HasPosition, MaybeHasId, ResourceType, SharedCreepProperties};
use rand::{rngs::StdRng, seq::IteratorRandom, Rng, SeedableRng};
use screeps::{game, Creep, HasPosition, MaybeHasId, ResourceType, SharedCreepProperties};

use crate::{memory::ScreepsMemory, room::cache::tick_cache::{hauling::{HaulingPriority, HaulingType}, RoomCache}, traits::creep::CreepExtensions};
use crate::{config, 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 controller = cache.structures.controller.as_ref().unwrap();

if creep.spawning() || creep.tired() {
let _ = creep.say("😴", false);
return;
}

if sign_controller(creep, memory, cache) {
return;
}

let controller = cache.structures.controller.as_ref().unwrap();

if creep.store().get_used_capacity(Some(ResourceType::Energy)) == 0 {
let container = &controller.container;
if let Some(container) = container {
Expand All @@ -33,3 +38,34 @@ pub fn run_creep(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCach
let _ = creep.upgrade_controller(&controller.controller);
}
}

pub fn sign_controller(creep: &Creep, memory: &mut ScreepsMemory, cache: &mut RoomCache) -> bool {
let controller = cache.structures.controller.as_ref().unwrap();

let mut seedable = StdRng::seed_from_u64(game::time().into());

let signs = config::ROOM_SIGNS;
let random = &mut seedable.gen_range(0..signs.len());
let random = signs.get(*random).unwrap();

if controller.controller.sign().is_none() {
if creep.pos().is_near_to(controller.controller.pos()) {
let _ = creep.sign_controller(&controller.controller, random);
} else {
creep.better_move_to(memory.creeps.get_mut(&creep.name()).unwrap(), cache, controller.controller.pos(), 1);
}
return true;
}

if !config::ROOM_SIGNS.contains(&controller.controller.sign().unwrap().text().as_str()) {
if creep.pos().is_near_to(controller.controller.pos()) {
let _ = creep.sign_controller(&controller.controller, random);
} else {
creep.better_move_to(memory.creeps.get_mut(&creep.name()).unwrap(), cache, controller.controller.pos(), 1);
}

return true;
}

false
}
2 changes: 1 addition & 1 deletion src/room/planning/creep/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub fn formulate_miner(room: &Room, memory: &mut ScreepsMemory, cache: &mut Room

return true;
}
} else if upgrader_count < 6 {
} else if upgrader_count < 5 {
let mut body = Vec::new();
let cost = 300;
let max = room.energy_capacity_available();
Expand Down

0 comments on commit d3997e7

Please sign in to comment.