Loadout Udpate: NpcData part

Actually implement creating npc with new EntitytInfo by chaning
CreateNpc.loadout to CreateNpc.inventory and cleaning code in
NpcData::from_entity_info.
This commit is contained in:
juliancoffee 2022-01-23 18:07:54 +02:00
parent e1bfa6c7e2
commit d5b927602a
10 changed files with 42 additions and 64 deletions

View File

@ -1,5 +1,5 @@
#!/bin/bash #!/bin/bash
time cargo clippy --all-targets --locked --features="bin_cmd_doc_gen,bin_compression,bin_csv,bin_graphviz,bin_bot,asset_tweak" -- -D warnings && time cargo clippy --all-targets --locked --features="bin_cmd_doc_gen,bin_compression,bin_csv,bin_graphviz,bin_bot,bin_entity_migrate, asset_tweak" -- -D warnings &&
# Ensure that the veloren-voxygen default-publish feature builds as it excludes some default features # Ensure that the veloren-voxygen default-publish feature builds as it excludes some default features
time cargo clippy -p veloren-voxygen --locked --no-default-features --features="default-publish" -- -D warnings && time cargo clippy -p veloren-voxygen --locked --no-default-features --features="default-publish" -- -D warnings &&
time cargo fmt --all -- --check time cargo fmt --all -- --check

View File

@ -272,7 +272,7 @@ fn convert_loop(from: &str, to: &str, old_ver: &str, new_ver: &str) {
let root = Path::new(from); let root = Path::new(from);
let files = Walk::Dir { let files = Walk::Dir {
path: Path::new("").to_owned(), path: Path::new("").to_owned(),
content: walk_tree(&root, &root).unwrap(), content: walk_tree(root, root).unwrap(),
}; };
if old_ver == "v1" && new_ver == "v2" { if old_ver == "v1" && new_ver == "v2" {
walk_with_migrate::<v1::EntityConfig, v2::EntityConfig>( walk_with_migrate::<v1::EntityConfig, v2::EntityConfig>(

View File

@ -128,7 +128,7 @@ pub enum ServerEvent {
skill_set: comp::SkillSet, skill_set: comp::SkillSet,
health: Option<comp::Health>, health: Option<comp::Health>,
poise: comp::Poise, poise: comp::Poise,
loadout: comp::inventory::loadout::Loadout, inventory: comp::inventory::Inventory,
body: comp::Body, body: comp::Body,
agent: Option<comp::Agent>, agent: Option<comp::Agent>,
alignment: comp::Alignment, alignment: comp::Alignment,

View File

@ -185,7 +185,7 @@ pub struct EntityInfo {
pub loot: LootSpec<String>, pub loot: LootSpec<String>,
// Loadout // Loadout
pub inventory: Vec<(u32, Item)>, pub inventory: Vec<(u32, Item)>,
pub loadout: Option<LoadoutBuilder>, pub loadout: LoadoutBuilder,
pub make_loadout: Option<fn(LoadoutBuilder, Option<&trade::SiteInformation>) -> LoadoutBuilder>, pub make_loadout: Option<fn(LoadoutBuilder, Option<&trade::SiteInformation>) -> LoadoutBuilder>,
// Skills // Skills
pub skillset_asset: Option<String>, pub skillset_asset: Option<String>,
@ -212,7 +212,7 @@ impl EntityInfo {
scale: 1.0, scale: 1.0,
loot: LootSpec::Nothing, loot: LootSpec::Nothing,
inventory: vec![], inventory: vec![],
loadout: None, loadout: LoadoutBuilder::empty(),
make_loadout: None, make_loadout: None,
skillset_asset: None, skillset_asset: None,
pet: None, pet: None,
@ -322,7 +322,7 @@ impl EntityInfo {
// NOTE: helpder function, think twice before exposing it // NOTE: helpder function, think twice before exposing it
fn with_default_equip(mut self) -> Self { fn with_default_equip(mut self) -> Self {
let loadout_builder = LoadoutBuilder::from_default(&self.body); let loadout_builder = LoadoutBuilder::from_default(&self.body);
self.loadout = Some(loadout_builder); self.loadout = loadout_builder;
self self
} }
@ -335,7 +335,7 @@ impl EntityInfo {
LoadoutAsset::Loadout(asset) => { LoadoutAsset::Loadout(asset) => {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let loadout = LoadoutBuilder::from_asset_expect(&asset, Some(&mut rng)); let loadout = LoadoutBuilder::from_asset_expect(&asset, Some(&mut rng));
self.loadout = Some(loadout); self.loadout = loadout;
}, },
LoadoutAsset::Choice(assets) => { LoadoutAsset::Choice(assets) => {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
@ -343,8 +343,8 @@ impl EntityInfo {
.choose_weighted(&mut rng, |(p, _asset)| *p) .choose_weighted(&mut rng, |(p, _asset)| *p)
.expect("rng error"); .expect("rng error");
let loadout = LoadoutBuilder::from_asset_expect(&asset, Some(&mut rng)); let loadout = LoadoutBuilder::from_asset_expect(asset, Some(&mut rng));
self.loadout = Some(loadout); self.loadout = loadout;
}, },
} }
@ -352,20 +352,16 @@ impl EntityInfo {
} }
#[must_use] #[must_use]
/// Return EntityInfo, create new loadout if needed /// Return EntityInfo with weapons applied to LoadoutBuilder
// NOTE: helpder function, think twice before exposing it // NOTE: helpder function, think twice before exposing it
fn with_hands(mut self, hands: Hands, config_asset: Option<&str>) -> Self { fn with_hands(mut self, hands: Hands, config_asset: Option<&str>) -> Self {
let rng = &mut rand::thread_rng(); let rng = &mut rand::thread_rng();
if self.loadout.is_none() {
self.loadout = Some(LoadoutBuilder::empty());
}
match hands { match hands {
Hands::TwoHanded(main_tool) => { Hands::TwoHanded(main_tool) => {
let tool = main_tool.try_to_item(config_asset.unwrap_or("??"), rng); let tool = main_tool.try_to_item(config_asset.unwrap_or("??"), rng);
if let Some(tool) = tool { if let Some(tool) = tool {
self.loadout = self.loadout.map(|l| l.active_mainhand(Some(tool))); self.loadout = self.loadout.active_mainhand(Some(tool));
} }
}, },
Hands::Paired(tool) => { Hands::Paired(tool) => {
@ -375,20 +371,21 @@ impl EntityInfo {
let second_tool = tool.try_to_item(config_asset.unwrap_or("??"), rng); let second_tool = tool.try_to_item(config_asset.unwrap_or("??"), rng);
if let Some(main_tool) = main_tool { if let Some(main_tool) = main_tool {
self.loadout = self.loadout.map(|l| l.active_mainhand(Some(main_tool))); self.loadout = self.loadout.active_mainhand(Some(main_tool));
} }
if let Some(second_tool) = second_tool { if let Some(second_tool) = second_tool {
self.loadout = self.loadout.map(|l| l.active_offhand(Some(second_tool))); self.loadout = self.loadout.active_offhand(Some(second_tool));
} }
}, },
Hands::Mix { mainhand, offhand } => { Hands::Mix { mainhand, offhand } => {
let main_tool = mainhand.try_to_item(config_asset.unwrap_or("??"), rng); let main_tool = mainhand.try_to_item(config_asset.unwrap_or("??"), rng);
let second_tool = offhand.try_to_item(config_asset.unwrap_or("??"), rng); let second_tool = offhand.try_to_item(config_asset.unwrap_or("??"), rng);
if let Some(main_tool) = main_tool { if let Some(main_tool) = main_tool {
self.loadout = self.loadout.map(|l| l.active_mainhand(Some(main_tool))); self.loadout = self.loadout.active_mainhand(Some(main_tool));
} }
if let Some(second_tool) = second_tool { if let Some(second_tool) = second_tool {
self.loadout = self.loadout.map(|l| l.active_offhand(Some(second_tool))); self.loadout = self.loadout.active_offhand(Some(second_tool));
} }
}, },
} }

View File

@ -179,7 +179,7 @@ impl CharacterBehavior for Data {
skill_set, skill_set,
health, health,
poise: comp::Poise::new(body), poise: comp::Poise::new(body),
loadout, inventory: comp::Inventory::new_with_loadout(loadout),
body, body,
agent: Some( agent: Some(
comp::Agent::from_body(&body) comp::Agent::from_body(&body)

View File

@ -631,7 +631,7 @@ fn handle_make_npc(
return Err("Waypoint spawning is not implemented".to_owned()); return Err("Waypoint spawning is not implemented".to_owned());
}, },
NpcData::Data { NpcData::Data {
loadout, inventory,
pos, pos,
stats, stats,
skill_set, skill_set,
@ -643,8 +643,6 @@ fn handle_make_npc(
scale, scale,
loot, loot,
} => { } => {
let inventory = Inventory::new_with_loadout(loadout);
let mut entity_builder = server let mut entity_builder = server
.state .state
.create_npc(pos, stats, skill_set, health, poise, inventory, body) .create_npc(pos, stats, skill_set, health, poise, inventory, body)

View File

@ -7,7 +7,6 @@ use common::{
aura::{Aura, AuraKind, AuraTarget}, aura::{Aura, AuraKind, AuraTarget},
beam, beam,
buff::{BuffCategory, BuffData, BuffKind, BuffSource}, buff::{BuffCategory, BuffData, BuffKind, BuffSource},
inventory::loadout::Loadout,
shockwave, Agent, Alignment, Anchor, Body, Health, Inventory, ItemDrop, LightEmitter, shockwave, Agent, Alignment, Anchor, Body, Health, Inventory, ItemDrop, LightEmitter,
Object, Ori, PidController, Poise, Pos, Projectile, Scale, SkillSet, Stats, Vel, Object, Ori, PidController, Poise, Pos, Projectile, Scale, SkillSet, Stats, Vel,
WaypointArea, WaypointArea,
@ -51,7 +50,7 @@ pub fn handle_create_npc(
skill_set: SkillSet, skill_set: SkillSet,
health: Option<Health>, health: Option<Health>,
poise: Poise, poise: Poise,
loadout: Loadout, inventory: Inventory,
body: Body, body: Body,
agent: impl Into<Option<Agent>>, agent: impl Into<Option<Agent>>,
alignment: Alignment, alignment: Alignment,
@ -61,8 +60,6 @@ pub fn handle_create_npc(
rtsim_entity: Option<RtSimEntity>, rtsim_entity: Option<RtSimEntity>,
projectile: Option<Projectile>, projectile: Option<Projectile>,
) { ) {
let inventory = Inventory::new_with_loadout(loadout);
let entity = server let entity = server
.state .state
.create_npc(pos, stats, skill_set, health, poise, inventory, body) .create_npc(pos, stats, skill_set, health, poise, inventory, body)

View File

@ -155,7 +155,7 @@ impl Server {
skill_set, skill_set,
health, health,
poise, poise,
loadout, inventory,
body, body,
agent, agent,
alignment, alignment,
@ -171,7 +171,7 @@ impl Server {
skill_set, skill_set,
health, health,
poise, poise,
loadout, inventory,
body, body,
agent, agent,
alignment, alignment,

View File

@ -151,7 +151,7 @@ impl<'a> System<'a> for Sys {
skill_set, skill_set,
health, health,
poise, poise,
loadout, inventory,
agent, agent,
body, body,
alignment, alignment,
@ -163,7 +163,7 @@ impl<'a> System<'a> for Sys {
skill_set, skill_set,
health, health,
poise, poise,
loadout, inventory,
agent, agent,
body, body,
alignment, alignment,

View File

@ -25,8 +25,9 @@ use common::{
resources::{Time, TimeOfDay}, resources::{Time, TimeOfDay},
slowjob::SlowJobPool, slowjob::SlowJobPool,
terrain::TerrainGrid, terrain::TerrainGrid,
LoadoutBuilder, SkillSetBuilder, SkillSetBuilder,
}; };
use common_ecs::{Job, Origin, Phase, System}; use common_ecs::{Job, Origin, Phase, System};
use common_net::msg::{SerializedTerrainChunk, ServerGeneral}; use common_net::msg::{SerializedTerrainChunk, ServerGeneral};
use common_state::TerrainChanges; use common_state::TerrainChanges;
@ -250,7 +251,7 @@ impl<'a> System<'a> for Sys {
skill_set, skill_set,
health, health,
poise, poise,
loadout, inventory,
agent, agent,
body, body,
alignment, alignment,
@ -263,7 +264,7 @@ impl<'a> System<'a> for Sys {
skill_set, skill_set,
health, health,
poise, poise,
loadout, inventory,
agent, agent,
body, body,
alignment, alignment,
@ -391,7 +392,7 @@ pub enum NpcData {
skill_set: comp::SkillSet, skill_set: comp::SkillSet,
health: Option<comp::Health>, health: Option<comp::Health>,
poise: comp::Poise, poise: comp::Poise,
loadout: comp::inventory::loadout::Loadout, inventory: comp::inventory::Inventory,
agent: Option<comp::Agent>, agent: Option<comp::Agent>,
body: comp::Body, body: comp::Body,
alignment: comp::Alignment, alignment: comp::Alignment,
@ -402,7 +403,7 @@ pub enum NpcData {
} }
impl NpcData { impl NpcData {
pub fn from_entity_info(entity: EntityInfo, loadout_rng: &mut impl Rng) -> Self { pub fn from_entity_info(entity: EntityInfo, _loadout_rng: &mut impl Rng) -> Self {
let EntityInfo { let EntityInfo {
// flags // flags
is_waypoint, is_waypoint,
@ -417,9 +418,8 @@ impl NpcData {
loot, loot,
// tools and skills // tools and skills
skillset_asset, skillset_asset,
main_tool, loadout: mut loadout_builder,
second_tool, inventory: items,
loadout_asset,
make_loadout, make_loadout,
trading_information: economy, trading_information: economy,
// unused // unused
@ -442,34 +442,20 @@ impl NpcData {
} }
}; };
let loadout = { let inventory = {
let mut loadout_builder = LoadoutBuilder::empty();
// If main tool is passed, use it. Otherwise fallback to default tool
if let Some(main_tool) = main_tool {
loadout_builder = loadout_builder.active_mainhand(Some(main_tool));
} else {
loadout_builder = loadout_builder.with_default_maintool(&body);
}
// If second tool is passed, use it as well
if let Some(second_tool) = second_tool {
loadout_builder = loadout_builder.active_offhand(Some(second_tool));
}
// If there is config, apply it.
// If not, use default equipement for this body.
if let Some(asset) = loadout_asset {
loadout_builder = loadout_builder.with_asset_expect(&asset, loadout_rng);
} else {
loadout_builder = loadout_builder.with_default_equipment(&body);
}
// Evaluate lazy function for loadout creation // Evaluate lazy function for loadout creation
if let Some(make_loadout) = make_loadout { if let Some(make_loadout) = make_loadout {
loadout_builder = loadout_builder.with_creator(make_loadout, economy.as_ref()); loadout_builder = loadout_builder.with_creator(make_loadout, economy.as_ref());
} }
loadout_builder.build() let loadout = loadout_builder.build();
let mut inventory = comp::inventory::Inventory::new_with_loadout(loadout);
for (num, mut item) in items {
// I don't think we can produce any helpful error there.
let _ = item.set_amount(num);
let _ = inventory.push(item);
}
inventory
}; };
let health_level = skill_set let health_level = skill_set
@ -519,7 +505,7 @@ impl NpcData {
skill_set, skill_set,
health, health,
poise, poise,
loadout, inventory,
agent, agent,
body, body,
alignment, alignment,