use crate::rules::Version;
use crate::sim::Model;
use crate::stack::{ItemData, Stack, last, number, tag_eq};
use ghast_protocol::nbt::{Compound, RawNbt, Tag};
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct TradeOffer {
pub first: Option<Stack>,
pub second: Option<Stack>,
pub result: Option<Stack>,
pub uses: i32,
pub max_uses: i32,
pub xp: i32,
pub special_price: i32,
pub price_multiplier: f32,
pub demand: i32,
}
impl TradeOffer {
#[must_use]
pub fn new(
first: Option<Stack>,
second: Option<Stack>,
result: Option<Stack>,
uses: i32,
max_uses: i32,
) -> TradeOffer {
TradeOffer {
first,
second,
result,
uses,
max_uses,
xp: 0,
special_price: 0,
price_multiplier: 0.0,
demand: 0,
}
}
#[must_use]
pub fn disabled(&self) -> bool {
self.uses >= self.max_uses
}
pub(crate) fn first_price(&self, v: Version) -> Option<Stack> {
let first = self.first.as_ref()?;
if v != Version::V1_16_5 {
return Some(first.clone());
}
let i = first.count;
let f = i.wrapping_mul(self.demand) as f32 * self.price_multiplier;
let fl = {
let t = f as i32;
if f < t as f32 { t - 1 } else { t }
};
let j = fl.max(0);
let n = i.wrapping_add(j).wrapping_add(self.special_price);
Some(first.with_count(n.clamp(1, v.max_count(first))))
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub(crate) struct Merchant {
pub offers: Option<Vec<TradeOffer>>,
pub index: i32,
pub current: Option<usize>,
}
fn accepts(v: Version, given: Option<&Stack>, sample: Option<&Stack>) -> bool {
let (g, s) = match (given, sample) {
(Some(g), Some(s)) => (g, s),
(None, None) => return v == Version::V1_16_5,
_ => return false,
};
if g.id != s.id || (v != Version::V1_16_5 && g.damage != s.damage) {
return false;
}
let Some(want) = s.nbt() else {
return true;
};
let Ok(Some(want)) = want.parse() else {
return false;
};
let got = if v == Version::V1_16_5 && v.is_damageable(g) {
let mut c = match g.nbt().map(RawNbt::parse) {
Some(Ok(Some(t))) => match t.tag {
Tag::Compound(c) => c,
_ => return false,
},
None => Compound::default(),
Some(_) => return false,
};
let d = last(&c, "Damage").and_then(number).unwrap_or(0) as i32;
c.0.retain(|(k, _)| k != "Damage");
c.0.push(("Damage".into(), Tag::Int(d.max(0))));
Tag::Compound(c)
} else {
match g.nbt().map(RawNbt::parse) {
Some(Ok(Some(t))) => t.tag,
_ => return false,
}
};
nbt_contains(&want.tag, &got)
}
fn nbt_contains(standard: &Tag, subject: &Tag) -> bool {
if standard.type_id() != subject.type_id() {
return false;
}
match (standard, subject) {
(Tag::Compound(a), Tag::Compound(b)) => a.0.iter().enumerate().all(|(i, (k, v))| {
if a.0[i + 1..].iter().any(|(k2, _)| k2 == k) {
return true;
}
last(b, k).is_some_and(|w| nbt_contains(v, w))
}),
_ => tag_eq(standard, subject),
}
}
fn count(s: Option<&Stack>) -> i32 {
s.map_or(0, |s| s.count)
}
fn lookup(
v: Version,
offers: &[TradeOffer],
first: Option<&Stack>,
second: Option<&Stack>,
index: i32,
) -> Option<usize> {
let fits = |o: &TradeOffer| -> bool {
if v == Version::V1_16_5 {
let price = o.first_price(v);
return accepts(v, first, price.as_ref())
&& count(first) >= count(price.as_ref())
&& accepts(v, second, o.second.as_ref())
&& count(second) >= count(o.second.as_ref());
}
accepts(v, first, o.first.as_ref())
&& count(first) >= count(o.first.as_ref())
&& match &o.second {
None => second.is_none(),
Some(b) => accepts(v, second, Some(b)) && count(second) >= b.count,
}
};
match usize::try_from(index) {
Ok(k) if k > 0 && k < offers.len() => fits(&offers[k]).then_some(k),
_ => offers.iter().position(fits),
}
}
pub(crate) fn update(v: Version, m: &mut Model) {
let Some(mer) = m.merchant.as_deref_mut() else {
return;
};
if m.container.len() != 3 {
return;
}
mer.current = None;
let (a, b) = if m.container[0].is_none() {
(m.container[1].as_ref(), None)
} else {
(m.container[0].as_ref(), m.container[1].as_ref())
};
if a.is_none() {
m.container[2] = None;
return;
}
let Some(offers) = mer.offers.as_deref().filter(|o| !o.is_empty()) else {
return;
};
let idx = mer.index;
let usable = |k: Option<usize>| k.filter(|&k| !offers[k].disabled());
let found = if v == Version::V1_16_5 {
let t = lookup(v, offers, a, b, idx);
match usable(t) {
Some(k) => Some(k),
None => {
mer.current = t;
usable(lookup(v, offers, b, a, idx))
}
}
} else {
match usable(lookup(v, offers, a, b, idx)) {
Some(k) => Some(k),
None if b.is_some() => usable(lookup(v, offers, b, a, idx)),
None => None,
}
};
let out = match found {
Some(k) => {
mer.current = Some(k);
let mut r = offers[k].result.clone();
crate::sim::clamp(&mut r);
r
}
None => None,
};
m.container[2] = out;
}
fn deplete(
v: Version,
o: &TradeOffer,
first: &mut Option<Stack>,
second: &mut Option<Stack>,
) -> bool {
match v {
Version::V1_16_5 => {
let price = o.first_price(v);
let ok = accepts(v, first.as_ref(), price.as_ref())
&& count(first.as_ref()) >= count(price.as_ref())
&& accepts(v, second.as_ref(), o.second.as_ref())
&& count(second.as_ref()) >= count(o.second.as_ref());
if !ok {
return false;
}
if let Some(f) = first {
f.count -= count(price.as_ref());
}
if let (Some(s), Some(b)) = (second.as_mut(), &o.second) {
s.count -= b.count;
}
true
}
_ => {
let (Some(f), Some(a)) = (first.as_mut(), &o.first) else {
return false;
};
if f.id != a.id || (v == Version::V1_12 && f.count < a.count) {
return false;
}
match (&o.second, second.as_mut()) {
(Some(b), Some(s))
if s.id == b.id && (v != Version::V1_12 || s.count >= b.count) =>
{
f.count -= a.count;
s.count -= b.count;
true
}
(None, None) => {
f.count -= a.count;
true
}
_ => false,
}
}
}
}
pub(crate) fn take_needs_world(v: Version, out: &Stack) -> bool {
let map = match v {
Version::V1_8 | Version::V1_12 => out.id == 358,
_ => v.name(out.id) == "filled_map",
};
map && matches!(out.data, ItemData::Nbt(_))
}
pub(crate) fn take_output(v: Version, m: &mut Model) {
let Some(mer) = m.merchant.as_deref_mut() else {
return;
};
let (Some(k), Some(offers)) = (mer.current, mer.offers.as_mut()) else {
return;
};
let o = offers[k].clone();
let (mut a, mut b) = (m.container[0].clone(), m.container[1].clone());
if !(deplete(v, &o, &mut a, &mut b) || deplete(v, &o, &mut b, &mut a)) {
return;
}
offers[k].uses = offers[k].uses.wrapping_add(1);
let gone = |s: Option<Stack>| s.filter(|s| s.count > 0);
m.container[0] = gone(a);
m.container[1] = gone(b);
update(v, m);
}
impl crate::sim::Sim<'_> {
pub(crate) fn switch_trade(&mut self, index: usize) {
let Some(o) = self
.m
.merchant
.as_ref()
.and_then(|m| m.offers.as_ref()?.get(index).cloned())
else {
return;
};
for k in 0..2 {
if let Some(mut st) = self.get(k).cloned() {
if !self.insert(&mut st, 3, 39, true).unwrap_or(false) {
return;
}
let left = self.norm(st);
self.set(k, left);
}
}
if self.get(0).is_some() || self.get(1).is_some() {
return;
}
self.autofill(0, o.first_price(self.v).as_ref());
self.autofill(1, o.second.as_ref());
}
fn autofill(&mut self, slot: usize, want: Option<&Stack>) {
let Some(want) = want else { return };
let max = self.v.max_count(want);
for i in 3..39 {
let Some(t) = self.get(i).cloned() else {
continue;
};
if t.id != want.id || !t.nbt_eq(want) {
continue;
}
let j = self.get(slot).map_or(0, |s| s.count);
let k = (max - j).min(t.count);
let l = j + k;
let rest = t.with_count(t.count - k);
*self.slot_mut(i) = self.norm(rest);
self.set(slot, Some(t.with_count(l)));
if l >= max {
break;
}
}
}
}
#[cfg(any(feature = "v1_8", feature = "v1_12"))]
pub(crate) fn parse_legacy(data: &bytes::Bytes) -> Option<(i32, Vec<TradeOffer>)> {
use ghast_protocol::codec::{Decode, Reader};
use ghast_protocol::types::slot::legacy;
let mut r = Reader::new(data);
let window = r.read_i32().ok()?;
let n = r.read_u8().ok()?;
let mut offers = Vec::with_capacity(usize::from(n));
let slot = |r: &mut Reader<'_>| -> Option<Option<Stack>> {
Some(crate::stack::from_legacy_slot(
legacy::Slot::decode(r).ok()?,
))
};
for _ in 0..n {
let first = slot(&mut r)?;
let result = slot(&mut r)?;
let second = if r.read_bool().ok()? {
slot(&mut r)?
} else {
None
};
let disabled = r.read_bool().ok()?;
let uses = r.read_i32().ok()?;
let max_uses = r.read_i32().ok()?;
offers.push(TradeOffer::new(
first,
second,
result,
if disabled { max_uses } else { uses },
max_uses,
));
}
Some((window, offers))
}
#[cfg(feature = "v1_16_5")]
pub(crate) fn from_v1_16_5(
p: &ghast_protocol::v1_16_5::play::clientbound::TradeList,
) -> Vec<TradeOffer> {
let stack = |s: &ghast_protocol::types::slot::modern::Slot| {
crate::stack::from_modern_slot(s.clone()).and_then(|s| {
let c = i32::from(s.count as i8);
(c > 0).then(|| s.with_count(c))
})
};
p.trades
.iter()
.map(|t| TradeOffer {
first: stack(&t.input_item1),
second: t.input_item2.as_ref().and_then(stack),
result: stack(&t.output_item),
uses: if t.trade_disabled {
t.maximum_nb_trade_uses
} else {
t.nb_trade_uses
},
max_uses: t.maximum_nb_trade_uses,
xp: t.xp,
special_price: t.special_price,
price_multiplier: t.price_multiplier,
demand: t.demand,
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(all(feature = "v1_12", feature = "v1_16_5"))]
fn nbt(t: Tag) -> RawNbt {
RawNbt::from_named(&ghast_protocol::nbt::NamedTag {
name: String::new(),
tag: t,
})
.unwrap()
}
#[test]
fn nbt_subset_matches() {
let want = Tag::Compound(Compound(vec![("a".into(), Tag::Int(1))]));
let got = Tag::Compound(Compound(vec![
("b".into(), Tag::Int(2)),
("a".into(), Tag::Int(1)),
]));
assert!(nbt_contains(&want, &got));
assert!(!nbt_contains(&got, &want));
assert!(!nbt_contains(&Tag::Int(1), &Tag::Short(1)));
}
#[cfg(all(feature = "v1_12", feature = "v1_16_5"))]
#[test]
fn damage_written_on_1_16() {
let v = Version::V1_16_5;
let sword = (1..2000).find(|&i| v.name(i) == "diamond_sword").unwrap();
let sample = Stack::new(sword, 1, 0).with_nbt(Some(nbt(Tag::Compound(Compound(vec![(
"Damage".into(),
Tag::Int(0),
)])))));
assert!(accepts(v, Some(&Stack::new(sword, 1, 0)), Some(&sample)));
assert!(!accepts(
Version::V1_12,
Some(&Stack::new(276, 1, 0)),
Some(
&Stack::new(276, 1, 0).with_nbt(Some(nbt(Tag::Compound(Compound(vec![(
"Damage".into(),
Tag::Int(0),
)])))))
)
));
}
#[cfg(feature = "v1_16_5")]
#[test]
fn adjusted_price() {
let mut o = TradeOffer::new(Some(Stack::new(1, 10, 0)), None, None, 0, 1);
o.demand = 3;
o.price_multiplier = 0.05;
o.special_price = -2;
assert_eq!(o.first_price(Version::V1_16_5).unwrap().count, 9);
o.special_price = -100;
assert_eq!(o.first_price(Version::V1_16_5).unwrap().count, 1);
}
}