use crate::mechanics::inventory::policies::StackingPolicy;
use crate::mechanics::inventory::types::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AlwaysStack;
impl StackingPolicy for AlwaysStack {
fn add_to_inventory(state: &mut InventoryState, stack: ItemStack, weight_per_item: Weight) {
if let Some(existing) = state.find_stack_mut(stack.item_id) {
existing.quantity += stack.quantity;
} else {
state.stacks.push(stack);
state.occupied_slots += 1;
}
state.total_weight += weight_per_item * stack.quantity as Weight;
}
fn remove_from_inventory(state: &mut InventoryState, stack: ItemStack) {
if let Some(idx) = state.stacks.iter().position(|s| s.item_id == stack.item_id) {
let existing = &mut state.stacks[idx];
if existing.quantity <= stack.quantity {
state.stacks.remove(idx);
state.occupied_slots -= 1;
} else {
existing.quantity -= stack.quantity;
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NeverStack;
impl StackingPolicy for NeverStack {
fn add_to_inventory(state: &mut InventoryState, stack: ItemStack, weight_per_item: Weight) {
state.stacks.push(ItemStack::new(stack.item_id, 1));
state.occupied_slots += 1;
state.total_weight += weight_per_item;
}
fn remove_from_inventory(state: &mut InventoryState, stack: ItemStack) {
if let Some(idx) = state.stacks.iter().position(|s| s.item_id == stack.item_id) {
state.stacks.remove(idx);
state.occupied_slots -= 1;
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LimitedStack;
impl StackingPolicy for LimitedStack {
fn add_to_inventory(state: &mut InventoryState, stack: ItemStack, weight_per_item: Weight) {
if let Some(existing) = state.find_stack_mut(stack.item_id) {
existing.quantity += stack.quantity;
} else {
state.stacks.push(stack);
state.occupied_slots += 1;
}
state.total_weight += weight_per_item * stack.quantity as Weight;
}
fn remove_from_inventory(state: &mut InventoryState, stack: ItemStack) {
if let Some(idx) = state.stacks.iter().position(|s| s.item_id == stack.item_id) {
let existing = &mut state.stacks[idx];
if existing.quantity <= stack.quantity {
state.stacks.remove(idx);
state.occupied_slots -= 1;
} else {
existing.quantity -= stack.quantity;
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_always_stack_adds_to_existing() {
let mut state = InventoryState::new();
state.stacks.push(ItemStack::new(1, 10));
state.occupied_slots = 1;
state.total_weight = 10.0;
AlwaysStack::add_to_inventory(&mut state, ItemStack::new(1, 5), 1.0);
assert_eq!(state.stacks.len(), 1);
assert_eq!(state.stacks[0].quantity, 15);
assert_eq!(state.occupied_slots, 1);
assert_eq!(state.total_weight, 15.0);
}
#[test]
fn test_always_stack_creates_new_stack() {
let mut state = InventoryState::new();
AlwaysStack::add_to_inventory(&mut state, ItemStack::new(1, 5), 2.0);
assert_eq!(state.stacks.len(), 1);
assert_eq!(state.stacks[0].quantity, 5);
assert_eq!(state.occupied_slots, 1);
assert_eq!(state.total_weight, 10.0);
}
#[test]
fn test_always_stack_removes_partial() {
let mut state = InventoryState::new();
state.stacks.push(ItemStack::new(1, 10));
state.occupied_slots = 1;
AlwaysStack::remove_from_inventory(&mut state, ItemStack::new(1, 3));
assert_eq!(state.stacks.len(), 1);
assert_eq!(state.stacks[0].quantity, 7);
assert_eq!(state.occupied_slots, 1);
}
#[test]
fn test_always_stack_removes_entire() {
let mut state = InventoryState::new();
state.stacks.push(ItemStack::new(1, 10));
state.occupied_slots = 1;
AlwaysStack::remove_from_inventory(&mut state, ItemStack::new(1, 10));
assert_eq!(state.stacks.len(), 0);
assert_eq!(state.occupied_slots, 0);
}
#[test]
fn test_never_stack_creates_separate_stacks() {
let mut state = InventoryState::new();
NeverStack::add_to_inventory(&mut state, ItemStack::new(1, 1), 1.0);
NeverStack::add_to_inventory(&mut state, ItemStack::new(1, 1), 1.0);
assert_eq!(state.stacks.len(), 2);
assert_eq!(state.occupied_slots, 2);
assert_eq!(state.total_weight, 2.0);
}
#[test]
fn test_never_stack_removes_first_match() {
let mut state = InventoryState::new();
state.stacks.push(ItemStack::new(1, 1));
state.stacks.push(ItemStack::new(1, 1));
state.occupied_slots = 2;
NeverStack::remove_from_inventory(&mut state, ItemStack::new(1, 1));
assert_eq!(state.stacks.len(), 1);
assert_eq!(state.occupied_slots, 1);
}
#[test]
fn test_limited_stack_adds_to_existing() {
let mut state = InventoryState::new();
state.stacks.push(ItemStack::new(1, 50));
state.occupied_slots = 1;
LimitedStack::add_to_inventory(&mut state, ItemStack::new(1, 30), 1.0);
assert_eq!(state.stacks.len(), 1);
assert_eq!(state.stacks[0].quantity, 80);
}
#[test]
fn test_limited_stack_creates_new_for_different_item() {
let mut state = InventoryState::new();
state.stacks.push(ItemStack::new(1, 50));
state.occupied_slots = 1;
LimitedStack::add_to_inventory(&mut state, ItemStack::new(2, 30), 1.0);
assert_eq!(state.stacks.len(), 2);
assert_eq!(state.occupied_slots, 2);
}
}