Skip to content

Commit

Permalink
Fixed 'Unknown' bug by redoing the trait structure
Browse files Browse the repository at this point in the history
  • Loading branch information
TCA166 committed May 18, 2024
1 parent decd03f commit 414072e
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 290 deletions.
22 changes: 14 additions & 8 deletions src/game_state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use crate::structures::{Character, Culture, DerivedRef, Dynasty, Faith, GameObjectDerived, Memory, Title};
use crate::structures::{Character, Culture, DerivedRef, Dynasty, Faith, DummyInit, Memory, Title};
use crate::game_object::{GameId, GameObject, GameString};
use crate::types::{Shared, Wrapper, WrapperMut};

Expand Down Expand Up @@ -157,8 +157,9 @@ impl GameState{
c.get_internal_mut().init(value, self);
}
else{
let c = Character::from_game_object(value, self);
self.characters.insert(key.clone(), Shared::wrap(c));
let c = Shared::wrap(Character::dummy(key));
self.characters.insert(key.clone(), c.clone());
c.get_internal_mut().init(value, self);
}
}

Expand All @@ -170,7 +171,8 @@ impl GameState{
t.get_internal_mut().init(value, self);
}
else{
let t = Title::from_game_object(value, self);
let mut t = Title::dummy(key);
t.init(value, self);
self.titles.insert(key.clone(), Shared::wrap(t));
}
}
Expand All @@ -183,7 +185,8 @@ impl GameState{
f.get_internal_mut().init(value, self);
}
else{
let f = Faith::from_game_object(value, self);
let mut f = Faith::dummy(key);
f.init(value, self);
self.faiths.insert(key.clone(), Shared::wrap(f));
}
}
Expand All @@ -196,7 +199,8 @@ impl GameState{
c.get_internal_mut().init(value, self);
}
else{
let c = Culture::from_game_object(value, self);
let mut c = Culture::dummy(key);
c.init(value, self);
self.cultures.insert(key.clone(), Shared::wrap(c));
}
}
Expand All @@ -209,7 +213,8 @@ impl GameState{
d.get_internal_mut().init(value, self);
}
else{
let d = Dynasty::from_game_object(value, self);
let mut d = Dynasty::dummy(key);
d.init(value, self);
self.dynasties.insert(key.clone(), Shared::wrap(d));
}
}
Expand All @@ -222,7 +227,8 @@ impl GameState{
m.get_internal_mut().init(value, self);
}
else{
let m = Memory::from_game_object(value, self);
let mut m = Memory::dummy(key);
m.init(value, self);
self.memories.insert(key.clone(), Shared::wrap(m));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/localizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl Localizer{
return d;
}
}
println!("Key not found: {}", key);
//println!("Key not found: {}", key);
GameString::wrap(demangle_generic(key))
}
}
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ use game_state::GameState;
/// A submodule that provides [GameObjectDerived] objects which are serialized and rendered into HTML.
/// You can think of them like frontend DB view objects into parsed save files.
mod structures;
use structures::{Player, GameObjectDerived, Renderable, Renderer, Cullable};
use structures::{Player, Renderable, Renderer, Cullable};

/// A submodule that handles the creation of the minijinja [Environment] and loading of templates.
mod jinja_env;
use jinja_env::create_env;

use crate::game_object::GameId;
use crate::{game_object::GameId, structures::FromGameObject};

/// A convenience function to create a directory if it doesn't exist, and do nothing if it does.
/// Also prints an error message if the directory creation fails.
Expand Down
88 changes: 4 additions & 84 deletions src/structures/character.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{Serialize, ser::SerializeStruct};

use crate::{game_object::{GameObject, GameString, SaveFileValue}, game_state::GameState, localizer::Localizer, types::{Wrapper, WrapperMut}};

use super::{renderer::Renderable, serialize_array, Cullable, Culture, DerivedRef, Dynasty, Faith, GameId, GameObjectDerived, Memory, Renderer, Shared, Title};
use super::{renderer::Renderable, serialize_array, Cullable, Culture, DerivedRef, DummyInit, Dynasty, Faith, GameId, GameObjectDerived, Memory, Renderer, Shared, Title};

/// Represents a character in the game.
/// Implements [GameObjectDerived], [Renderable] and [Cullable].
Expand Down Expand Up @@ -43,8 +43,6 @@ pub struct Character {
name_localized: bool
}

//TODO some characters are stored within history files. Will need to parse those too godamit

// So both faith and culture can be stored for a character in the latest leader of their house.
// The problem with reading that now is that while Houses are already likely loaded,
// the characters that the houses hold reference to are likely still dummy, so we can't read the faith and culture from the house leader.
Expand Down Expand Up @@ -265,86 +263,7 @@ impl Character {
}
}

impl GameObjectDerived for Character {

fn from_game_object(base:&GameObject, game_state:&mut GameState) -> Self {
let id = base.get_name().parse::<GameId>().unwrap();
let mut dead = false;
let mut reason = None;
let mut date = None;
let mut titles: Vec<Shared<Title>> = Vec::new();
get_dead(&mut dead, &mut reason, &mut date, &mut titles, &base, game_state);
//find skills
let mut skills = Vec::new();
get_skills(&mut skills, &base);
//find recessive traits
let mut recessive = Vec::new();
get_recessive(&mut recessive, &base);
//find family data
let mut spouses = Vec::new();
let mut former_spouses = Vec::new();
let mut children = Vec::new();
get_family(id, &mut spouses, &mut former_spouses, &mut children, &base, game_state);
//find dna
let dna = match base.get("dna"){
Some(d) => Some(d.as_string()),
None => None
};
//find traits
let mut traits = Vec::new();
get_traits(&mut traits, &base, game_state);
//find alive data
let mut gold = 0.0;
let mut piety = 0.0;
let mut prestige = 0.0;
let mut kills: Vec<Shared<Character>> = Vec::new();
let mut languages: Vec<GameString> = Vec::new();
let mut memories:Vec<Shared<Memory>> = Vec::new();
if !dead {
parse_alive_data(&base, &mut piety, &mut prestige, &mut gold, &mut kills, &mut languages, &mut traits, &mut memories, game_state);
}
//find landed data
let mut dread = 0.0;
let mut strength = 0.0;
let mut vassals:Vec<Shared<DerivedRef<Character>>> = Vec::new();
get_landed_data(&mut dread, &mut strength, &mut titles, &mut vassals, &base, game_state);
//find house
let house = get_dynasty(&base, game_state);
Character{
name: Some(base.get("first_name").unwrap().as_string()),
nick: base.get("nickname").map(|v| v.as_string()),
birth: Some(base.get("birth").unwrap().as_string()),
dead: dead,
date: date,
reason: reason,
house: house.clone(),
faith: get_faith(&base, game_state),
culture: get_culture(&base, game_state),
skills: skills,
traits: traits,
recessive:recessive,
spouses: spouses,
former: former_spouses,
children: children,
dna: dna,
memories: memories,
titles: titles,
piety: piety,
prestige: prestige,
dread: dread,
strength: strength,
gold: gold,
kills: kills,
languages: languages,
vassals: vassals,
id: id,
parents: Vec::new(),
depth: 0,
localized:false,
name_localized:false
}
}

impl DummyInit for Character{
fn dummy(id:GameId) -> Self {
Character{
name: None,
Expand Down Expand Up @@ -411,7 +330,9 @@ impl GameObjectDerived for Character {
self.culture = get_culture(&base, game_state);
self.dna = dna;
}
}

impl GameObjectDerived for Character {
fn get_id(&self) -> GameId {
self.id
}
Expand Down Expand Up @@ -554,7 +475,6 @@ impl Cullable for Character {
if depth <= self.depth && depth != 0{
return;
}
//TODO add separate toggle for name localization
if !self.name_localized {
if self.name.is_none() {
self.name = Some(GameString::wrap("Unknown".to_string()));
Expand Down
27 changes: 4 additions & 23 deletions src/structures/culture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use minijinja::context;
use serde::Serialize;
use serde::ser::SerializeStruct;
use super::renderer::Renderable;
use super::{serialize_array, Cullable, GameId, GameObjectDerived, Renderer, Shared};
use super::{serialize_array, Cullable, DummyInit, GameId, GameObjectDerived, Renderer, Shared};
use crate::game_object::{GameObject, GameString};
use crate::game_state::GameState;
use crate::localizer::Localizer;
Expand Down Expand Up @@ -53,28 +53,7 @@ fn get_date(base:&GameObject) -> Option<GameString>{
None
}

impl GameObjectDerived for Culture {
fn from_game_object(base:&GameObject, game_state:&mut GameState) -> Self {
let mut parents = Vec::new();
get_parents(&mut parents, base, game_state);
let mut traditions = Vec::new();
get_traditions(&mut traditions, &base);
Culture{
name: base.get("name").unwrap().as_string(),
ethos: base.get("ethos").unwrap().as_string(),
heritage: base.get("heritage").unwrap().as_string(),
martial: base.get("martial_custom").unwrap().as_string(),
date: get_date(&base),
parents: parents,
traditions: traditions,
id: base.get_name().parse::<GameId>().unwrap(),
language: base.get("language").unwrap().as_string(),
depth: 0,
localized:false,
name_localized:false
}
}

impl DummyInit for Culture {
fn dummy(id:GameId) -> Self {
Culture{
name: GameString::wrap("".to_owned().into()),
Expand Down Expand Up @@ -102,7 +81,9 @@ impl GameObjectDerived for Culture {
self.date = get_date(&base);
self.language = base.get("language").unwrap().as_string();
}
}

impl GameObjectDerived for Culture {
fn get_id(&self) -> GameId {
self.id
}
Expand Down
20 changes: 15 additions & 5 deletions src/structures/derived_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ use super::{Cullable, GameId, GameObjectDerived, Shared, Wrapper};
/// 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 struct DerivedRef<T> where T:Cullable{
id: GameId,
obj: Option<Shared<T>>
}

impl<T> DerivedRef<T> where T:GameObjectDerived + Cullable{
impl<T> DerivedRef<T> where T:Cullable{
/// Create a new DerivedRef from a [Shared] object.
/// This will clone the object and store a reference to it.
pub fn from_derived(obj:Shared<T>) -> Self{
Expand Down Expand Up @@ -46,15 +46,15 @@ impl<T> DerivedRef<T> where T:GameObjectDerived + Cullable{
}

/// Converts an array of GameObjectDerived to an array of DerivedRef
pub fn serialize_array<T>(array:&Vec<Shared<T>>) -> Vec<DerivedRef<T>> where T:GameObjectDerived + Cullable{
pub fn serialize_array<T>(array:&Vec<Shared<T>>) -> Vec<DerivedRef<T>> where T:Cullable{
let mut res = Vec::new();
for s in array.iter(){
res.push(DerivedRef::<T>::from_derived(s.clone()));
}
res
}

impl<T> Serialize for DerivedRef<T> where T:GameObjectDerived + Cullable{
impl<T> Serialize for DerivedRef<T> where T:Cullable{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
let mut state = serializer.serialize_struct("DerivedRef", 3)?;
state.serialize_field("id", &self.id)?;
Expand All @@ -66,7 +66,17 @@ impl<T> Serialize for DerivedRef<T> where T:GameObjectDerived + Cullable{
}
}

impl<T> Cullable for DerivedRef<T> where T:GameObjectDerived + Cullable{
impl<T> GameObjectDerived for DerivedRef<T> where T:Cullable {
fn get_id(&self) -> GameId {
self.id
}

fn get_name(&self) -> crate::game_object::GameString {
self.obj.as_ref().unwrap().get_internal().get_name()
}
}

impl<T> Cullable for DerivedRef<T> where T:Cullable{
fn get_depth(&self) -> usize {
self.obj.as_ref().unwrap().get_internal().get_depth()
}
Expand Down
34 changes: 4 additions & 30 deletions src/structures/dynasty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use minijinja::context;
use serde::Serialize;
use serde::ser::SerializeStruct;
use super::renderer::Renderable;
use super::{serialize_array, Character, Cullable, Culture, DerivedRef, Faith, GameId, GameObjectDerived, Renderer, Shared};
use super::{serialize_array, Character, Cullable, Culture, DerivedRef, DummyInit, Faith, GameId, GameObjectDerived, Renderer, Shared};
use crate::game_object::{GameObject, GameString, SaveFileValue};
use crate::game_state::GameState;
use crate::localizer::Localizer;
Expand All @@ -25,8 +25,6 @@ pub struct Dynasty{
name_localized: bool,
}

//TODO it's possible that dynasties sometimes are stored within history files like characters

impl Dynasty {
/// Gets the faith of the dynasty.
/// Really this is just the faith of the current house leader.
Expand Down Expand Up @@ -180,33 +178,7 @@ fn get_date(base:&GameObject) -> Option<GameString>{
Some(date.unwrap().as_string())
}

impl GameObjectDerived for Dynasty {
fn from_game_object(base:&GameObject, game_state:&mut GameState) -> Self {
//get the dynasty legacies
let mut perks = Vec::new();
get_perks(&mut perks, &base);
//get the array of leaders
let mut leaders = Vec::new();
get_leaders(&mut leaders, &base, game_state);
let res = get_prestige(&base);
let p = get_parent(&base, game_state);
Dynasty{
name: get_name(&base, p.clone()),
parent: p,
members: 0,
houses: 0,
prestige_tot: res.0,
prestige: res.1,
perks: perks,
leaders: leaders,
found_date: get_date(&base),
id: base.get_name().parse::<GameId>().unwrap(),
depth: 0,
localized:false,
name_localized: false
}
}

impl DummyInit for Dynasty {
fn dummy(id:GameId) -> Self {
Dynasty{
name: None,
Expand Down Expand Up @@ -240,7 +212,9 @@ impl GameObjectDerived for Dynasty {
}
self.found_date = get_date(&base);
}
}

impl GameObjectDerived for Dynasty {
fn get_id(&self) -> GameId {
self.id
}
Expand Down
Loading

0 comments on commit 414072e

Please sign in to comment.