mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Merge branch 'zesterer/item-merging' into 'master'
Zesterer/item merging See merge request veloren/veloren!3912
This commit is contained in:
commit
b6d8182382
@ -45,6 +45,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
- Loot that drops multiple items is now distributed fairly between damage contributors.
|
||||
- Added accessibility settings tab.
|
||||
- Setting to enable subtitles describing sfx.
|
||||
- Item drops that are spatially close and compatible will now merge with one-another to reduce performance problems.
|
||||
|
||||
### Changed
|
||||
|
||||
|
@ -1158,6 +1158,37 @@ impl Item {
|
||||
}
|
||||
}
|
||||
|
||||
/// Return `true` if `other` can be merged into this item. This is generally
|
||||
/// only possible if the item has a compatible item ID and is stackable,
|
||||
/// along with any other similarity checks.
|
||||
pub fn can_merge(&self, other: &Item) -> bool {
|
||||
if self.is_stackable()
|
||||
&& let ItemBase::Simple(other_item_def) = &other.item_base
|
||||
&& self.is_same_item_def(other_item_def)
|
||||
&& u32::from(self.amount)
|
||||
.checked_add(other.amount())
|
||||
.filter(|&amount| amount <= self.max_amount())
|
||||
.is_some()
|
||||
{
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Try to merge `other` into this item. This is generally only possible if
|
||||
/// the item has a compatible item ID and is stackable, along with any
|
||||
/// other similarity checks.
|
||||
pub fn try_merge(&mut self, other: Item) -> Result<(), Item> {
|
||||
if self.can_merge(&other) {
|
||||
self.increase_amount(other.amount())
|
||||
.expect("`can_merge` succeeded but `increase_amount` did not");
|
||||
Ok(())
|
||||
} else {
|
||||
Err(other)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn num_slots(&self) -> u16 { self.item_base.num_slots() }
|
||||
|
||||
/// NOTE: invariant that amount() ≤ max_amount(), 1 ≤ max_amount(),
|
||||
|
@ -12,7 +12,7 @@ use std::sync::Arc;
|
||||
use vek::*;
|
||||
|
||||
/// Position
|
||||
#[derive(Copy, Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Pos(pub Vec3<f32>);
|
||||
|
||||
impl Component for Pos {
|
||||
|
@ -503,16 +503,16 @@ fn handle_drop_all(
|
||||
for item in item_to_place {
|
||||
let vel = Vec3::new(rng.gen_range(-0.1..0.1), rng.gen_range(-0.1..0.1), 0.5);
|
||||
|
||||
server
|
||||
.state
|
||||
.create_item_drop(Default::default(), item)
|
||||
.with(comp::Pos(Vec3::new(
|
||||
server.state.create_item_drop(
|
||||
comp::Pos(Vec3::new(
|
||||
pos.0.x + rng.gen_range(5.0..10.0),
|
||||
pos.0.y + rng.gen_range(5.0..10.0),
|
||||
pos.0.z + 5.0,
|
||||
)))
|
||||
.with(comp::Vel(vel))
|
||||
.build();
|
||||
)),
|
||||
comp::Vel(vel),
|
||||
item,
|
||||
None,
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
@ -473,20 +473,17 @@ pub fn handle_destroy(server: &mut Server, entity: EcsEntity, last_change: Healt
|
||||
|
||||
let mut spawn_item = |item, loot_owner| {
|
||||
let offset = item_offset_spiral.next().unwrap_or_default();
|
||||
let item_drop_entity = state
|
||||
.create_item_drop(Pos(pos.0 + Vec3::unit_z() * 0.25 + offset), item)
|
||||
.maybe_with(vel)
|
||||
.build();
|
||||
state.create_item_drop(
|
||||
Pos(pos.0 + Vec3::unit_z() * 0.25 + offset),
|
||||
vel.unwrap_or(comp::Vel(Vec3::zero())),
|
||||
item,
|
||||
if let Some(loot_owner) = loot_owner {
|
||||
debug!("Assigned UID {loot_owner:?} as the winner for the loot drop");
|
||||
if let Err(err) = state
|
||||
.ecs()
|
||||
.write_storage::<LootOwner>()
|
||||
.insert(item_drop_entity, LootOwner::new(loot_owner))
|
||||
{
|
||||
error!("Failed to set loot owner on item drop: {err}");
|
||||
};
|
||||
}
|
||||
Some(LootOwner::new(loot_owner))
|
||||
} else {
|
||||
None
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
let msm = &MaterialStatManifest::load().read();
|
||||
@ -1103,15 +1100,17 @@ pub fn handle_bonk(server: &mut Server, pos: Vec3<f32>, owner: Option<Uid>, targ
|
||||
for item in flatten_counted_items(&items, ability_map, msm) {
|
||||
server
|
||||
.state
|
||||
.create_object(Default::default(), match block.get_sprite() {
|
||||
.create_object(
|
||||
Pos(pos.map(|e| e as f32) + Vec3::new(0.5, 0.5, 0.0)),
|
||||
match block.get_sprite() {
|
||||
// Create different containers depending on the original sprite
|
||||
Some(SpriteKind::Apple) => comp::object::Body::Apple,
|
||||
Some(SpriteKind::Beehive) => comp::object::Body::Hive,
|
||||
Some(SpriteKind::Coconut) => comp::object::Body::Coconut,
|
||||
Some(SpriteKind::Bomb) => comp::object::Body::Bomb,
|
||||
_ => comp::object::Body::Pouch,
|
||||
})
|
||||
.with(Pos(pos.map(|e| e as f32) + Vec3::new(0.5, 0.5, 0.0)))
|
||||
},
|
||||
)
|
||||
.with(item)
|
||||
.maybe_with(match block.get_sprite() {
|
||||
Some(SpriteKind::Bomb) => Some(comp::Object::Bomb { owner }),
|
||||
|
@ -1,4 +1,4 @@
|
||||
use specs::{world::WorldExt, Builder, Entity as EcsEntity, Join};
|
||||
use specs::{world::WorldExt, Entity as EcsEntity, Join};
|
||||
use vek::*;
|
||||
|
||||
use common::{
|
||||
@ -253,15 +253,13 @@ pub fn handle_mine_block(
|
||||
}
|
||||
}
|
||||
for item in items {
|
||||
let item_drop = state
|
||||
.create_item_drop(Default::default(), item)
|
||||
.with(Pos(pos.map(|e| e as f32) + Vec3::new(0.5, 0.5, 0.0)));
|
||||
if let Some(uid) = maybe_uid {
|
||||
item_drop.with(LootOwner::new(LootOwnerKind::Player(uid)))
|
||||
} else {
|
||||
item_drop
|
||||
}
|
||||
.build();
|
||||
let loot_owner = maybe_uid.map(LootOwnerKind::Player).map(LootOwner::new);
|
||||
state.create_item_drop(
|
||||
Pos(pos.map(|e| e as f32) + Vec3::new(0.5, 0.5, 0.0)),
|
||||
comp::Vel(Vec3::zero()),
|
||||
item,
|
||||
loot_owner,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -377,17 +377,18 @@ pub fn handle_inventory(server: &mut Server, entity: EcsEntity, manip: comp::Inv
|
||||
drop(inventory_updates);
|
||||
|
||||
for item in drop_items {
|
||||
state
|
||||
.create_item_drop(Default::default(), item)
|
||||
.with(comp::Pos(
|
||||
state.create_item_drop(
|
||||
comp::Pos(
|
||||
Vec3::new(
|
||||
sprite_pos.x as f32,
|
||||
sprite_pos.y as f32,
|
||||
sprite_pos.z as f32,
|
||||
) + Vec3::one().with_z(0.0) * 0.5,
|
||||
))
|
||||
.with(comp::Vel(Vec3::zero()))
|
||||
.build();
|
||||
),
|
||||
comp::Vel(Vec3::zero()),
|
||||
item,
|
||||
None,
|
||||
);
|
||||
}
|
||||
},
|
||||
comp::InventoryManip::Use(slot) => {
|
||||
@ -890,10 +891,9 @@ pub fn handle_inventory(server: &mut Server, entity: EcsEntity, manip: comp::Inv
|
||||
let items_were_crafted = if let Some(crafted_items) = crafted_items {
|
||||
for item in crafted_items {
|
||||
if let Err(item) = inventory.push(item) {
|
||||
if let Some(pos) = state.read_component_copied::<comp::Pos>(entity) {
|
||||
dropped_items.push((
|
||||
state
|
||||
.read_component_copied::<comp::Pos>(entity)
|
||||
.unwrap_or_default(),
|
||||
pos,
|
||||
state
|
||||
.read_component_copied::<comp::Ori>(entity)
|
||||
.unwrap_or_default(),
|
||||
@ -901,6 +901,7 @@ pub fn handle_inventory(server: &mut Server, entity: EcsEntity, manip: comp::Inv
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
true
|
||||
} else {
|
||||
false
|
||||
@ -940,11 +941,12 @@ pub fn handle_inventory(server: &mut Server, entity: EcsEntity, manip: comp::Inv
|
||||
}
|
||||
});
|
||||
|
||||
state
|
||||
.create_item_drop(Default::default(), item)
|
||||
.with(comp::Pos(pos.0 + *ori.look_dir() + Vec3::unit_z()))
|
||||
.with(comp::Vel(Vec3::zero()))
|
||||
.build();
|
||||
state.create_item_drop(
|
||||
comp::Pos(pos.0 + *ori.look_dir() + Vec3::unit_z()),
|
||||
comp::Vel(Vec3::zero()),
|
||||
item,
|
||||
None,
|
||||
);
|
||||
}
|
||||
|
||||
let mut rng = rand::thread_rng();
|
||||
@ -963,12 +965,11 @@ pub fn handle_inventory(server: &mut Server, entity: EcsEntity, manip: comp::Inv
|
||||
let uid = state.read_component_copied::<Uid>(entity);
|
||||
|
||||
let mut new_entity = state
|
||||
.create_object(Default::default(), match kind {
|
||||
.create_object(comp::Pos(pos.0 + Vec3::unit_z() * 0.25), match kind {
|
||||
item::Throwable::Bomb => comp::object::Body::Bomb,
|
||||
item::Throwable::Firework(reagent) => comp::object::Body::for_firework(reagent),
|
||||
item::Throwable::TrainingDummy => comp::object::Body::TrainingDummy,
|
||||
})
|
||||
.with(comp::Pos(pos.0 + Vec3::unit_z() * 0.25))
|
||||
.with(comp::Vel(vel));
|
||||
|
||||
match kind {
|
||||
|
@ -19,7 +19,7 @@ use common::{
|
||||
self,
|
||||
item::{ItemKind, MaterialStatManifest},
|
||||
skills::{GeneralSkill, Skill},
|
||||
ChatType, Group, Inventory, Item, Player, Poise, Presence, PresenceKind,
|
||||
ChatType, Group, Inventory, Item, LootOwner, Player, Poise, Presence, PresenceKind,
|
||||
},
|
||||
effect::Effect,
|
||||
link::{Link, LinkHandle},
|
||||
@ -60,7 +60,15 @@ pub trait StateExt {
|
||||
) -> EcsEntityBuilder;
|
||||
/// Build a static object entity
|
||||
fn create_object(&mut self, pos: comp::Pos, object: comp::object::Body) -> EcsEntityBuilder;
|
||||
fn create_item_drop(&mut self, pos: comp::Pos, item: Item) -> EcsEntityBuilder;
|
||||
/// Create an item drop or merge the item with an existing drop, if a
|
||||
/// suitable candidate exists.
|
||||
fn create_item_drop(
|
||||
&mut self,
|
||||
pos: comp::Pos,
|
||||
vel: comp::Vel,
|
||||
item: Item,
|
||||
loot_owner: Option<LootOwner>,
|
||||
) -> Option<EcsEntity>;
|
||||
fn create_ship<F: FnOnce(comp::ship::Body) -> comp::Collider>(
|
||||
&mut self,
|
||||
pos: comp::Pos,
|
||||
@ -311,7 +319,53 @@ impl StateExt for State {
|
||||
.with(body)
|
||||
}
|
||||
|
||||
fn create_item_drop(&mut self, pos: comp::Pos, item: Item) -> EcsEntityBuilder {
|
||||
fn create_item_drop(
|
||||
&mut self,
|
||||
pos: comp::Pos,
|
||||
vel: comp::Vel,
|
||||
item: Item,
|
||||
loot_owner: Option<LootOwner>,
|
||||
) -> Option<EcsEntity> {
|
||||
{
|
||||
const MAX_MERGE_DIST: f32 = 1.5;
|
||||
|
||||
// First, try to identify possible candidates for item merging
|
||||
// We limit our search to just a few blocks and we prioritise merging with the
|
||||
// closest
|
||||
let positions = self.ecs().read_storage::<comp::Pos>();
|
||||
let loot_owners = self.ecs().read_storage::<LootOwner>();
|
||||
let mut items = self.ecs().write_storage::<Item>();
|
||||
let mut nearby_items = self
|
||||
.ecs()
|
||||
.read_resource::<common::CachedSpatialGrid>()
|
||||
.0
|
||||
.in_circle_aabr(pos.0.xy(), MAX_MERGE_DIST)
|
||||
.filter(|entity| items.contains(*entity))
|
||||
.filter_map(|entity| {
|
||||
Some((entity, positions.get(entity)?.0.distance_squared(pos.0)))
|
||||
})
|
||||
.filter(|(_, dist_sqrd)| *dist_sqrd < MAX_MERGE_DIST.powi(2))
|
||||
.collect::<Vec<_>>();
|
||||
nearby_items.sort_by_key(|(_, dist_sqrd)| (dist_sqrd * 1000.0) as i32);
|
||||
for (nearby, _) in nearby_items {
|
||||
// Only merge if the loot owner is the same
|
||||
if loot_owners.get(nearby).map(|lo| lo.owner()) == loot_owner.map(|lo| lo.owner())
|
||||
&& items
|
||||
.get(nearby)
|
||||
.map_or(false, |nearby_item| nearby_item.can_merge(&item))
|
||||
{
|
||||
// Merging can occur! Perform the merge:
|
||||
items
|
||||
.get_mut(nearby)
|
||||
.expect("we know that the item exists")
|
||||
.try_merge(item)
|
||||
.expect("`try_merge` should succeed because `can_merge` returned `true`");
|
||||
return None;
|
||||
}
|
||||
}
|
||||
// Only if merging items fails do we give up and create a new item
|
||||
}
|
||||
|
||||
let item_drop = comp::item_drop::Body::from(&item);
|
||||
let body = comp::Body::ItemDrop(item_drop);
|
||||
let light_emitter = match &*item.kind() {
|
||||
@ -323,17 +377,21 @@ impl StateExt for State {
|
||||
}),
|
||||
_ => None,
|
||||
};
|
||||
Some(
|
||||
self.ecs_mut()
|
||||
.create_entity_synced()
|
||||
.with(item)
|
||||
.with(pos)
|
||||
.with(comp::Vel(Vec3::zero()))
|
||||
.with(vel)
|
||||
.with(item_drop.orientation(&mut thread_rng()))
|
||||
.with(item_drop.mass())
|
||||
.with(item_drop.density())
|
||||
.with(body.collider())
|
||||
.with(body)
|
||||
.maybe_with(loot_owner)
|
||||
.maybe_with(light_emitter)
|
||||
.build(),
|
||||
)
|
||||
}
|
||||
|
||||
fn create_ship<F: FnOnce(comp::ship::Body) -> comp::Collider>(
|
||||
|
@ -56,9 +56,7 @@ impl EventMapper for BlockEventMapper {
|
||||
let cam_pos = camera.dependents().cam_pos + focus_off;
|
||||
|
||||
// Get the player position and chunk
|
||||
let player_pos = state
|
||||
.read_component_copied::<Pos>(player_entity)
|
||||
.unwrap_or_default();
|
||||
if let Some(player_pos) = state.read_component_copied::<Pos>(player_entity) {
|
||||
let player_chunk = player_pos.0.xy().map2(TerrainChunk::RECT_SIZE, |e, sz| {
|
||||
(e.floor() as i32).div_euclid(sz as i32)
|
||||
});
|
||||
@ -263,6 +261,7 @@ impl EventMapper for BlockEventMapper {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl BlockEventMapper {
|
||||
|
Loading…
Reference in New Issue
Block a user