wiwi/vh/
jewel.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
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
}

/// Type of modifier (flag or with numeric value, and associated
/// metadata if applicable)
enum ModifierType {
	/// A modifier that is either present, or not (eg. Soulbound)
	///
	/// For modifiers of this type, the modifier value is meaningless, and for
	/// the sake of being deterministic, it should be set to 0.
	Flag,

	/// A modifier that has an attached number (eg. +100% Trap Disarm, or +15.3 Mining Speed)
	NumericValue {
		/// The power of 10 the stored value needs to be divided by
		/// to get the true value
		///
		/// For example, if you have mining speed with value of 76 (76.0), and decimal
		/// shift 1, you'd need to divide the value by 10^1, or shift the decimal left
		/// by 1, to get the real value of 7.6.
		decimal_shift: u8
	}
}

/// The value of a modifier, including whether or not the modifier is legendary
struct ModifierValue {
	/// The highest bit stores if the modifier is legendary, and the rest store
	/// the modifier's value (if applicable)
	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
{
	/// Register a modifier by its identifier, return [`Ok`] if added and [`Err`]
	/// if the modifier already exists
	#[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);

			// SAFETY: just checked `id` is not in set
			unsafe {
				self.modifiers.insert_unique_unchecked(modifier);
			}

			Ok(())
		} else {
			Err(())
		}
	}
}

impl<C> Modifier<C>
where
	C: Counter
{
	/// Create new modifier
	///
	/// This will always allocate, as it has no knowledge of existing modifier
	/// instances. Clone an existing modifier instance if you want to reuse
	/// the allocation.
	#[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 {
	/// Maximum storable modifier value
	const MAX: i32 = i32::MAX >> 1;

	/// Minimum storable modifier value (negative)
	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(|| {
			// SAFETY: we just checked we're within the allowed range
			unsafe { Self::new_unchecked(value, is_legendary) }
		})
	}

	/// # Safety
	///
	/// `value` must be within the range `MIN..=MAX`
	#[inline]
	unsafe fn new_unchecked(value: i32, is_legendary: bool) -> Self {
		Self { raw: value | (is_legendary.into_i32() << 31) }
	}

	#[inline]
	fn value(&self) -> i32 {
		// first shift left one to push out the legendary bit, then
		// (arithmetic) shift right one to bring back the bit with correct sign
		(self.raw << 1) >> 1
	}

	#[inline]
	fn is_legendary(&self) -> bool {
		self.raw >> 31 == 1
	}
}