mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Added item merging
This commit is contained in:
parent
ce4beff7fe
commit
ee971e4056
@ -1158,6 +1158,21 @@ impl Item {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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.is_stackable()
|
||||||
|
&& let ItemBase::Simple(other_item_def) = &other.item_base
|
||||||
|
&& self.is_same_item_def(other_item_def)
|
||||||
|
{
|
||||||
|
self.increase_amount(other.amount()).map_err(|_| other)?;
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn num_slots(&self) -> u16 { self.item_base.num_slots() }
|
pub fn num_slots(&self) -> u16 { self.item_base.num_slots() }
|
||||||
|
|
||||||
/// NOTE: invariant that amount() ≤ max_amount(), 1 ≤ max_amount(),
|
/// NOTE: invariant that amount() ≤ max_amount(), 1 ≤ max_amount(),
|
||||||
|
@ -12,7 +12,7 @@ use std::sync::Arc;
|
|||||||
use vek::*;
|
use vek::*;
|
||||||
|
|
||||||
/// Position
|
/// Position
|
||||||
#[derive(Copy, Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
|
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||||
pub struct Pos(pub Vec3<f32>);
|
pub struct Pos(pub Vec3<f32>);
|
||||||
|
|
||||||
impl Component for Pos {
|
impl Component for Pos {
|
||||||
|
@ -503,16 +503,16 @@ fn handle_drop_all(
|
|||||||
for item in item_to_place {
|
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);
|
let vel = Vec3::new(rng.gen_range(-0.1..0.1), rng.gen_range(-0.1..0.1), 0.5);
|
||||||
|
|
||||||
server
|
server.state.create_item_drop(
|
||||||
.state
|
comp::Pos(Vec3::new(
|
||||||
.create_item_drop(Default::default(), item)
|
|
||||||
.with(comp::Pos(Vec3::new(
|
|
||||||
pos.0.x + rng.gen_range(5.0..10.0),
|
pos.0.x + rng.gen_range(5.0..10.0),
|
||||||
pos.0.y + rng.gen_range(5.0..10.0),
|
pos.0.y + rng.gen_range(5.0..10.0),
|
||||||
pos.0.z + 5.0,
|
pos.0.z + 5.0,
|
||||||
)))
|
)),
|
||||||
.with(comp::Vel(vel))
|
comp::Vel(vel),
|
||||||
.build();
|
item,
|
||||||
|
None,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
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 mut spawn_item = |item, loot_owner| {
|
||||||
let offset = item_offset_spiral.next().unwrap_or_default();
|
let offset = item_offset_spiral.next().unwrap_or_default();
|
||||||
let item_drop_entity = state
|
state.create_item_drop(
|
||||||
.create_item_drop(Pos(pos.0 + Vec3::unit_z() * 0.25 + offset), item)
|
Pos(pos.0 + Vec3::unit_z() * 0.25 + offset),
|
||||||
.maybe_with(vel)
|
vel.unwrap_or(comp::Vel(Vec3::zero())),
|
||||||
.build();
|
item,
|
||||||
if let Some(loot_owner) = loot_owner {
|
if let Some(loot_owner) = loot_owner {
|
||||||
debug!("Assigned UID {loot_owner:?} as the winner for the loot drop");
|
debug!("Assigned UID {loot_owner:?} as the winner for the loot drop");
|
||||||
if let Err(err) = state
|
Some(LootOwner::new(loot_owner))
|
||||||
.ecs()
|
} else {
|
||||||
.write_storage::<LootOwner>()
|
None
|
||||||
.insert(item_drop_entity, LootOwner::new(loot_owner))
|
},
|
||||||
{
|
);
|
||||||
error!("Failed to set loot owner on item drop: {err}");
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let msm = &MaterialStatManifest::load().read();
|
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) {
|
for item in flatten_counted_items(&items, ability_map, msm) {
|
||||||
server
|
server
|
||||||
.state
|
.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
|
// Create different containers depending on the original sprite
|
||||||
Some(SpriteKind::Apple) => comp::object::Body::Apple,
|
Some(SpriteKind::Apple) => comp::object::Body::Apple,
|
||||||
Some(SpriteKind::Beehive) => comp::object::Body::Hive,
|
Some(SpriteKind::Beehive) => comp::object::Body::Hive,
|
||||||
Some(SpriteKind::Coconut) => comp::object::Body::Coconut,
|
Some(SpriteKind::Coconut) => comp::object::Body::Coconut,
|
||||||
Some(SpriteKind::Bomb) => comp::object::Body::Bomb,
|
Some(SpriteKind::Bomb) => comp::object::Body::Bomb,
|
||||||
_ => comp::object::Body::Pouch,
|
_ => comp::object::Body::Pouch,
|
||||||
})
|
},
|
||||||
.with(Pos(pos.map(|e| e as f32) + Vec3::new(0.5, 0.5, 0.0)))
|
)
|
||||||
.with(item)
|
.with(item)
|
||||||
.maybe_with(match block.get_sprite() {
|
.maybe_with(match block.get_sprite() {
|
||||||
Some(SpriteKind::Bomb) => Some(comp::Object::Bomb { owner }),
|
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 vek::*;
|
||||||
|
|
||||||
use common::{
|
use common::{
|
||||||
@ -253,15 +253,13 @@ pub fn handle_mine_block(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for item in items {
|
for item in items {
|
||||||
let item_drop = state
|
let loot_owner = maybe_uid.map(LootOwnerKind::Player).map(LootOwner::new);
|
||||||
.create_item_drop(Default::default(), item)
|
state.create_item_drop(
|
||||||
.with(Pos(pos.map(|e| e as f32) + Vec3::new(0.5, 0.5, 0.0)));
|
Pos(pos.map(|e| e as f32) + Vec3::new(0.5, 0.5, 0.0)),
|
||||||
if let Some(uid) = maybe_uid {
|
comp::Vel(Vec3::zero()),
|
||||||
item_drop.with(LootOwner::new(LootOwnerKind::Player(uid)))
|
item,
|
||||||
} else {
|
loot_owner,
|
||||||
item_drop
|
);
|
||||||
}
|
|
||||||
.build();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -377,17 +377,18 @@ pub fn handle_inventory(server: &mut Server, entity: EcsEntity, manip: comp::Inv
|
|||||||
drop(inventory_updates);
|
drop(inventory_updates);
|
||||||
|
|
||||||
for item in drop_items {
|
for item in drop_items {
|
||||||
state
|
state.create_item_drop(
|
||||||
.create_item_drop(Default::default(), item)
|
comp::Pos(
|
||||||
.with(comp::Pos(
|
|
||||||
Vec3::new(
|
Vec3::new(
|
||||||
sprite_pos.x as f32,
|
sprite_pos.x as f32,
|
||||||
sprite_pos.y as f32,
|
sprite_pos.y as f32,
|
||||||
sprite_pos.z as f32,
|
sprite_pos.z as f32,
|
||||||
) + Vec3::one().with_z(0.0) * 0.5,
|
) + Vec3::one().with_z(0.0) * 0.5,
|
||||||
))
|
),
|
||||||
.with(comp::Vel(Vec3::zero()))
|
comp::Vel(Vec3::zero()),
|
||||||
.build();
|
item,
|
||||||
|
None,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
comp::InventoryManip::Use(slot) => {
|
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 {
|
let items_were_crafted = if let Some(crafted_items) = crafted_items {
|
||||||
for item in crafted_items {
|
for item in crafted_items {
|
||||||
if let Err(item) = inventory.push(item) {
|
if let Err(item) = inventory.push(item) {
|
||||||
|
if let Some(pos) = state.read_component_copied::<comp::Pos>(entity) {
|
||||||
dropped_items.push((
|
dropped_items.push((
|
||||||
state
|
pos,
|
||||||
.read_component_copied::<comp::Pos>(entity)
|
|
||||||
.unwrap_or_default(),
|
|
||||||
state
|
state
|
||||||
.read_component_copied::<comp::Ori>(entity)
|
.read_component_copied::<comp::Ori>(entity)
|
||||||
.unwrap_or_default(),
|
.unwrap_or_default(),
|
||||||
@ -901,6 +901,7 @@ pub fn handle_inventory(server: &mut Server, entity: EcsEntity, manip: comp::Inv
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
@ -940,11 +941,12 @@ pub fn handle_inventory(server: &mut Server, entity: EcsEntity, manip: comp::Inv
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
state
|
state.create_item_drop(
|
||||||
.create_item_drop(Default::default(), item)
|
comp::Pos(pos.0 + *ori.look_dir() + Vec3::unit_z()),
|
||||||
.with(comp::Pos(pos.0 + *ori.look_dir() + Vec3::unit_z()))
|
comp::Vel(Vec3::zero()),
|
||||||
.with(comp::Vel(Vec3::zero()))
|
item,
|
||||||
.build();
|
None,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut rng = rand::thread_rng();
|
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 uid = state.read_component_copied::<Uid>(entity);
|
||||||
|
|
||||||
let mut new_entity = state
|
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::Bomb => comp::object::Body::Bomb,
|
||||||
item::Throwable::Firework(reagent) => comp::object::Body::for_firework(reagent),
|
item::Throwable::Firework(reagent) => comp::object::Body::for_firework(reagent),
|
||||||
item::Throwable::TrainingDummy => comp::object::Body::TrainingDummy,
|
item::Throwable::TrainingDummy => comp::object::Body::TrainingDummy,
|
||||||
})
|
})
|
||||||
.with(comp::Pos(pos.0 + Vec3::unit_z() * 0.25))
|
|
||||||
.with(comp::Vel(vel));
|
.with(comp::Vel(vel));
|
||||||
|
|
||||||
match kind {
|
match kind {
|
||||||
|
@ -19,7 +19,7 @@ use common::{
|
|||||||
self,
|
self,
|
||||||
item::{ItemKind, MaterialStatManifest},
|
item::{ItemKind, MaterialStatManifest},
|
||||||
skills::{GeneralSkill, Skill},
|
skills::{GeneralSkill, Skill},
|
||||||
ChatType, Group, Inventory, Item, Player, Poise, Presence, PresenceKind,
|
ChatType, Group, Inventory, Item, LootOwner, Player, Poise, Presence, PresenceKind,
|
||||||
},
|
},
|
||||||
effect::Effect,
|
effect::Effect,
|
||||||
link::{Link, LinkHandle},
|
link::{Link, LinkHandle},
|
||||||
@ -60,7 +60,15 @@ pub trait StateExt {
|
|||||||
) -> EcsEntityBuilder;
|
) -> EcsEntityBuilder;
|
||||||
/// Build a static object entity
|
/// Build a static object entity
|
||||||
fn create_object(&mut self, pos: comp::Pos, object: comp::object::Body) -> EcsEntityBuilder;
|
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>(
|
fn create_ship<F: FnOnce(comp::ship::Body) -> comp::Collider>(
|
||||||
&mut self,
|
&mut self,
|
||||||
pos: comp::Pos,
|
pos: comp::Pos,
|
||||||
@ -311,7 +319,48 @@ impl StateExt for State {
|
|||||||
.with(body)
|
.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,
|
||||||
|
mut 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.get(*entity).is_some())
|
||||||
|
.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()) {
|
||||||
|
if let Some(mut nearby_item) = items.get_mut(nearby) {
|
||||||
|
match nearby_item.try_merge(item) {
|
||||||
|
Ok(()) => return None, // Merging was successful!
|
||||||
|
Err(rejected_item) => item = rejected_item,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Only if merging items fails do we give up and create a new item
|
||||||
|
}
|
||||||
|
|
||||||
let item_drop = comp::item_drop::Body::from(&item);
|
let item_drop = comp::item_drop::Body::from(&item);
|
||||||
let body = comp::Body::ItemDrop(item_drop);
|
let body = comp::Body::ItemDrop(item_drop);
|
||||||
let light_emitter = match &*item.kind() {
|
let light_emitter = match &*item.kind() {
|
||||||
@ -323,17 +372,21 @@ impl StateExt for State {
|
|||||||
}),
|
}),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
|
Some(
|
||||||
self.ecs_mut()
|
self.ecs_mut()
|
||||||
.create_entity_synced()
|
.create_entity_synced()
|
||||||
.with(item)
|
.with(item)
|
||||||
.with(pos)
|
.with(pos)
|
||||||
.with(comp::Vel(Vec3::zero()))
|
.with(vel)
|
||||||
.with(item_drop.orientation(&mut thread_rng()))
|
.with(item_drop.orientation(&mut thread_rng()))
|
||||||
.with(item_drop.mass())
|
.with(item_drop.mass())
|
||||||
.with(item_drop.density())
|
.with(item_drop.density())
|
||||||
.with(body.collider())
|
.with(body.collider())
|
||||||
.with(body)
|
.with(body)
|
||||||
|
.maybe_with(loot_owner)
|
||||||
.maybe_with(light_emitter)
|
.maybe_with(light_emitter)
|
||||||
|
.build(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_ship<F: FnOnce(comp::ship::Body) -> comp::Collider>(
|
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;
|
let cam_pos = camera.dependents().cam_pos + focus_off;
|
||||||
|
|
||||||
// Get the player position and chunk
|
// Get the player position and chunk
|
||||||
let player_pos = state
|
if let Some(player_pos) = state.read_component_copied::<Pos>(player_entity) {
|
||||||
.read_component_copied::<Pos>(player_entity)
|
|
||||||
.unwrap_or_default();
|
|
||||||
let player_chunk = player_pos.0.xy().map2(TerrainChunk::RECT_SIZE, |e, sz| {
|
let player_chunk = player_pos.0.xy().map2(TerrainChunk::RECT_SIZE, |e, sz| {
|
||||||
(e.floor() as i32).div_euclid(sz as i32)
|
(e.floor() as i32).div_euclid(sz as i32)
|
||||||
});
|
});
|
||||||
@ -264,6 +262,7 @@ impl EventMapper for BlockEventMapper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl BlockEventMapper {
|
impl BlockEventMapper {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
|
Loading…
Reference in New Issue
Block a user