extern crate hashbrown;
use crate::prelude_std::*;
use crate::num::*;
use crate::rc::{ RcStr, Counter, AtomicCounter };
use hashbrown::HashSet;
struct JewelStore<C = AtomicCounter>
where
C: Counter
{
modifiers: HashSet<Modifier<C>>,
jewels: Vec<Jewel<C>>
}
struct Jewel<C = AtomicCounter>
where
C: Counter
{
level: u16,
size: i16,
modifiers: JewelModifiers<C>
}
enum JewelModifiers<C = AtomicCounter>
where
C: Counter
{
Chipped([ModifierInstance<C>; 1]),
Flawed([ModifierInstance<C>; 2]),
Flawless([ModifierInstance<C>; 3]),
Perfect([ModifierInstance<C>; 4])
}
struct ModifierInstance<C = AtomicCounter>
where
C: Counter
{
modifier: Modifier<C>,
value: ModifierValue
}
struct Modifier<C = AtomicCounter>
where
C: Counter
{
inner: RcStr<C, ModifierMeta<C>>
}
struct ModifierMeta<C = AtomicCounter>
where
C: Counter
{
display_name: RcStr<C>,
modifier_type: ModifierType
}
enum ModifierType {
Flag,
NumericValue {
decimal_shift: u8
}
}
struct ModifierValue {
raw: i32
}
impl JewelStore {
#[inline]
fn new() -> Self {
Self::with_counter()
}
#[inline]
fn with_counter<C>() -> JewelStore<C>
where
C: Counter
{
JewelStore {
modifiers: HashSet::new(),
jewels: Vec::new()
}
}
}
impl<C> JewelStore<C>
where
C: Counter
{
#[inline]
fn add_modifier(&mut self, id: &str, display_name: &str, modifier_type: ModifierType) -> Result<(), ()> {
if self.modifiers.get(id).is_none() {
let modifier = Modifier::new(id, display_name, modifier_type);
unsafe {
self.modifiers.insert_unique_unchecked(modifier);
}
Ok(())
} else {
Err(())
}
}
}
impl<C> Modifier<C>
where
C: Counter
{
#[inline]
fn new(id: &str, display_name: &str, modifier_type: ModifierType) -> Self {
Self {
inner: RcStr::with_metadata(id, ModifierMeta {
display_name: RcStr::new(display_name),
modifier_type
})
}
}
}
impl<C> Borrow<str> for Modifier<C>
where
C: Counter
{
#[inline]
fn borrow(&self) -> &str {
&self.inner
}
}
impl<C> Clone for Modifier<C>
where
C: Counter
{
#[inline]
fn clone(&self) -> Self {
Self { inner: self.inner.clone() }
}
}
impl<C, C2> PartialEq<Modifier<C2>> for Modifier<C>
where
C: Counter,
C2: Counter
{
#[inline]
fn eq(&self, other: &Modifier<C2>) -> bool {
*self.inner == *other.inner
}
}
impl<C> Eq for Modifier<C>
where
C: Counter
{}
impl<C> Hash for Modifier<C>
where
C: Counter
{
#[inline]
fn hash<H>(&self, state: &mut H)
where
H: Hasher
{
Hash::hash(&*self.inner, state)
}
}
impl ModifierValue {
const MAX: i32 = i32::MAX >> 1;
const MIN: i32 = i32::MIN >> 1;
#[inline]
fn new(value: i32, is_legendary: bool) -> Self {
Self::new_checked(value, is_legendary)
.expect("modifier value out of bounds")
}
#[inline]
fn new_checked(value: i32, is_legendary: bool) -> Option<Self> {
(Self::MIN..=Self::MAX).contains(&value).then(|| {
unsafe { Self::new_unchecked(value, is_legendary) }
})
}
#[inline]
unsafe fn new_unchecked(value: i32, is_legendary: bool) -> Self {
Self { raw: value | (is_legendary.into_i32() << 31) }
}
#[inline]
fn value(&self) -> i32 {
(self.raw << 1) >> 1
}
#[inline]
fn is_legendary(&self) -> bool {
self.raw >> 31 == 1
}
}