Skip to content

Commit

Permalink
Implemented DerivedRef for character
Browse files Browse the repository at this point in the history
and removed the CultureRef object
  • Loading branch information
TCA166 committed May 2, 2024
1 parent fa3ba17 commit c769b15
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 71 deletions.
31 changes: 18 additions & 13 deletions src/structures/character.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::cell::{Ref, RefCell};
use std::rc::Rc;
use std::cell::Ref;

use minijinja::{Environment, context};

Expand All @@ -11,7 +10,7 @@ use crate::game_state::GameState;

use super::renderer::Renderable;

use super::{Cullable, CultureRef, Dynasty, Faith, GameObjectDerived, Memory, Shared, Title};
use super::{Cullable, DerivedRef, Culture, Dynasty, Faith, GameObjectDerived, Memory, Shared, Title};

pub struct Character {
pub id: u32,
Expand All @@ -22,7 +21,7 @@ pub struct Character {
pub date: Option<Shared<String>>,
pub reason: Option<Shared<String>>,
pub faith: Option<Shared<Faith>>,
pub culture: Option<Shared<CultureRef>>,
pub culture: Option<Shared<Culture>>,
pub house: Option<Shared<Dynasty>>,
pub skills: Vec<i8>,
pub traits: Vec<Shared<String>>,
Expand All @@ -49,7 +48,7 @@ pub struct Character {
fn get_faith(house:&Option<Shared<Dynasty>>, base:&GameObject, game_state:&mut GameState) -> Shared<Faith>{
let faith_node = base.get("faith");
if faith_node.is_some(){
return game_state.get_faith(faith_node.unwrap().as_string_ref().unwrap().as_str()).clone();
game_state.get_faith(faith_node.unwrap().as_string_ref().unwrap().as_str()).clone()
}
else{
let h = house.as_ref().unwrap().borrow();
Expand All @@ -68,22 +67,25 @@ fn get_faith(house:&Option<Shared<Dynasty>>, base:&GameObject, game_state:&mut G
}
else{
let mut i = 0;
loop {
loop { //FIXME sometimes house leaders don't have faith
let l = h.leaders[i].try_borrow();
if l.is_ok(){
return l.unwrap().faith.as_ref().unwrap().clone();
let o = l.unwrap(); //dumb compiler forced me to create a new variable here
let f = o.faith.as_ref();
if f.is_some(){
return f.unwrap().clone();
}
}
i += 1;
}
}
}
}

fn get_culture(house:&Option<Shared<Dynasty>>, base:&GameObject, game_state:&mut GameState) -> Shared<CultureRef>{
fn get_culture(house:&Option<Shared<Dynasty>>, base:&GameObject, game_state:&mut GameState) -> Shared<Culture>{
let culture_node = base.get("culture");
if culture_node.is_some(){
let r = CultureRef::from_derived(game_state.get_culture(culture_node.unwrap().as_string_ref().unwrap().as_str()).clone());
return Rc::new(RefCell::new(r));
game_state.get_culture(culture_node.unwrap().as_string_ref().unwrap().as_str()).clone()
}
else{
let h = house.as_ref().unwrap().borrow();
Expand Down Expand Up @@ -411,9 +413,12 @@ impl Serialize for Character {
state.serialize_field("dead", &self.dead)?;
state.serialize_field("date", &self.date)?;
state.serialize_field("reason", &self.reason)?;
state.serialize_field("faith", &self.faith)?;
state.serialize_field("culture", &self.culture)?;
state.serialize_field("house", &self.house)?;
let rf = DerivedRef::<Faith>::from_derived(self.faith.as_ref().unwrap().clone());
state.serialize_field("faith", &rf)?;
let rc = DerivedRef::<Culture>::from_derived(self.culture.as_ref().unwrap().clone());
state.serialize_field("culture", &rc)?;
let rd = DerivedRef::<Dynasty>::from_derived(self.house.as_ref().unwrap().clone());
state.serialize_field("house", &rd)?;
state.serialize_field("skills", &self.skills)?;
state.serialize_field("traits", &self.traits)?;
state.serialize_field("recessive", &self.recessive)?;
Expand Down
41 changes: 0 additions & 41 deletions src/structures/culture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,44 +135,3 @@ impl Cullable for Culture {
}
}
}

/// A struct representing a shallow reference to a culture
/// This is used to avoid infinite recursion when serializing cultures
pub struct CultureRef{
pub id: u32,
pub name: Shared<String>,
culture: Shared<Culture>
}

impl Serialize for CultureRef{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut state = serializer.serialize_struct("CultureRef", 2)?;
state.serialize_field("id", &self.id)?;
state.serialize_field("name", &self.name)?;
state.end()
}
}

impl Cullable for CultureRef{
fn get_depth(&self) -> usize {
self.culture.borrow().get_depth()
}

fn set_depth(&mut self, depth:usize) {
self.culture.borrow_mut().set_depth(depth);
}
}

impl CultureRef{
pub fn from_derived(culture:Shared<Culture>) -> Self{
let c = culture.borrow();
CultureRef{
id: c.id,
name: c.name.clone(),
culture: culture.clone()
}
}
}
28 changes: 12 additions & 16 deletions src/structures/derived_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,22 @@ use serde::ser::SerializeStruct;

use super::{Cullable, GameObjectDerived, Shared};

struct DerivedRef<T> where T:GameObjectDerived + Cullable{
/// A shallow serializable reference to a derived game object.
/// The idea is to provide the id and name of the object, without serializing the whole object.
/// This is useful for serializing references to objects that are not in the current scope.
pub struct DerivedRef<T> where T:GameObjectDerived + Cullable{
pub id: u32,
pub name: Shared<String>,
obj: T
obj: Shared<T>
}

impl<T> DerivedRef<T> where T:GameObjectDerived + Cullable{
pub fn new(id:u32, name:Shared<String>, obj:T) -> Self{
pub fn from_derived(obj:Shared<T>) -> Self{
let o = obj.borrow();
DerivedRef{
id,
name,
obj
}
}

pub fn from_derived(obj:T) -> Self{
DerivedRef{
id: obj.get_id(),
name: obj.get_name(),
obj
id: o.get_id(),
name: o.get_name(),
obj: obj.clone()
}
}
}
Expand All @@ -38,10 +34,10 @@ impl<T> Serialize for DerivedRef<T> where T:GameObjectDerived + Cullable{

impl<T> Cullable for DerivedRef<T> where T:GameObjectDerived + Cullable{
fn get_depth(&self) -> usize {
self.obj.get_depth()
self.obj.borrow().get_depth()
}

fn set_depth(&mut self, depth:usize) {
self.obj.set_depth(depth);
self.obj.borrow_mut().set_depth(depth);
}
}
2 changes: 1 addition & 1 deletion src/structures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub use faith::Faith;

mod culture;
pub use culture::Culture;
pub use culture::CultureRef;

mod dynasty;
pub use dynasty::Dynasty;
Expand All @@ -33,6 +32,7 @@ mod lineage;
pub use lineage::LineageNode;

mod derived_ref;
pub use derived_ref::DerivedRef;

/// A type alias for shared objects.
/// Aliases: [std::rc::Rc]<[std::cell::RefCell]<>>
Expand Down

0 comments on commit c769b15

Please sign in to comment.