Skip to content

Commit

Permalink
wip social network
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Jan 9, 2024
1 parent 8fae1f6 commit a1d1413
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
3 changes: 3 additions & 0 deletions simulation/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::physics::coworld_synchronize;
use crate::souls::freight_station::freight_station_system;
use crate::souls::goods_company::{company_system, GoodsCompanyRegistry};
use crate::souls::human::update_decision_system;
use crate::souls::social_network::{clean_kinships_system, SocialNetworkRes};
use crate::transportation::pedestrian_decision_system;
use crate::transportation::road::{vehicle_decision_system, vehicle_state_update_system};
use crate::transportation::testing_vehicles::{random_vehicles_update, RandomVehicles};
Expand Down Expand Up @@ -45,6 +46,7 @@ pub fn init() {
register_system("freight_station", freight_station_system);
register_system("random_vehicles", random_vehicles_update);
register_system("update_map", |_, res| res.write::<Map>().update());
register_system("social_cleaner", clean_kinships_system);

register_system_sim("add_souls_to_empty_buildings", add_souls_to_empty_buildings);

Expand All @@ -62,6 +64,7 @@ pub fn init() {

register_init(init_market);

register_resource_default::<SocialNetworkRes, Bincode>("social_network");
register_resource_default::<MultiplayerState, Bincode>("multiplayer_state");
register_resource_default::<RandomVehicles, Bincode>("random_vehicles");
register_resource_default::<Tick, Bincode>("tick");
Expand Down
1 change: 1 addition & 0 deletions simulation/src/souls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod desire;
pub mod freight_station;
pub mod goods_company;
pub mod human;
pub mod social_network;

/// Adds souls to empty buildings
pub(crate) fn add_souls_to_empty_buildings(sim: &mut Simulation) {
Expand Down
91 changes: 91 additions & 0 deletions simulation/src/souls/social_network.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use crate::utils::resources::Resources;
use crate::{HumanID, World};
use common::scroll::BTreeMapScroller;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::ops::Bound;

#[derive(Default, Serialize, Deserialize)]
pub struct SocialNetworkRes {
// Symmetric
kinship: BTreeMap<(HumanID, HumanID), f32>,

social_maker_pivot: Option<HumanID>,

cleaner: BTreeMapScroller<(HumanID, HumanID)>,
}

impl SocialNetworkRes {
pub fn new() -> Self {
Self::default()
}

pub fn set_kinship(&mut self, a: HumanID, b: HumanID, value: f32) {
self.kinship.insert((a, b), value);
self.kinship.insert((b, a), value);
}

/// If kinship is not set, returns 0.0
pub fn kinship(&self, a: HumanID, b: HumanID) -> f32 {
self.kinship.get(&(a, b)).copied().unwrap_or(0.0)
}

/// may return kinships to nonexistent humans
pub fn nonzero_kinships(&self, a: HumanID) -> impl Iterator<Item = (HumanID, f32)> + '_ {
self.kinship
.range((a, HumanID::default())..)
.take_while(move |(x, _)| x.0 == a)
.map(|(x, y)| (x.1, *y))
}
}

pub fn generate_social_events(world: &mut World, res: &mut Resources) {

Check warning on line 42 in simulation/src/souls/social_network.rs

View workflow job for this annotation

GitHub Actions / build

unused variable: `world`

Check warning on line 42 in simulation/src/souls/social_network.rs

View workflow job for this annotation

GitHub Actions / build

unused variable: `world`
let mut social = res.write::<SocialNetworkRes>();

fn next_pivot(kinship: &BTreeMap<(HumanID, HumanID), f32>, pivot: &mut Option<HumanID>) {
let left_b = pivot
.map(|v| Bound::Excluded((v, HumanID::default())))
.unwrap_or(Bound::Unbounded);

*pivot = kinship
.range((left_b, Bound::Unbounded))
.next()
.map(|(x, _)| x.0);
}

let SocialNetworkRes {
ref kinship,
ref mut social_maker_pivot,
..
} = *social;

for _ in 0..10 {
next_pivot(kinship, social_maker_pivot);
let Some(v) = *social_maker_pivot else {

Check warning on line 64 in simulation/src/souls/social_network.rs

View workflow job for this annotation

GitHub Actions / build

unused variable: `v`

Check warning on line 64 in simulation/src/souls/social_network.rs

View workflow job for this annotation

GitHub Actions / build

unused variable: `v`
break;
};
}
}

pub fn clean_kinships_system(world: &mut World, res: &mut Resources) {
let mut social = res.write::<SocialNetworkRes>();

let mut to_remove = Vec::new();

let SocialNetworkRes {
ref kinship,
ref mut cleaner,
..
} = *social;

for (pair, _) in cleaner.iter(&kinship).take(10) {
if !world.humans.contains_key(pair.0) || !world.humans.contains_key(pair.1) {
to_remove.push(pair.clone());
}
}

for pair in to_remove {
social.kinship.remove(&pair);
social.kinship.remove(&(pair.1, pair.0));
}
}
1 change: 1 addition & 0 deletions simulation/src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::souls::human::{HumanDecision, PersonalInfo};
use crate::transportation::train::{Locomotive, LocomotiveReservation, RailWagon};
use crate::transportation::{Location, Pedestrian, Vehicle, VehicleKind, VehicleState};
use crate::utils::par_command_buffer::SimDrop;
use crate::utils::rand_provider::RandProvider;

Check warning on line 14 in simulation/src/world.rs

View workflow job for this annotation

GitHub Actions / build

unused import: `crate::utils::rand_provider::RandProvider`

Check warning on line 14 in simulation/src/world.rs

View workflow job for this annotation

GitHub Actions / build

unused import: `crate::utils::rand_provider::RandProvider`
use crate::utils::resources::Resources;
use crate::{impl_entity, impl_trans, SoulID};
use derive_more::{From, TryInto};
Expand Down

0 comments on commit a1d1413

Please sign in to comment.