-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer_state.rb
243 lines (198 loc) · 4.87 KB
/
player_state.rb
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
237
238
239
240
241
242
243
require 'bitset'
class PlayerState
def self.build_hash(values)
values.each_with_index.reduce({}) do |hash, (value, i)|
hash[value] = i if value != :_
hash
end.freeze
end
def self.build_numeric_hash(values)
build_hash(values.map { |n| n ? n.to_i : n })
end
def self.define_prop(name)
getter = name.to_s.downcase.to_sym
value_getter = "#{getter}_value".to_sym
hash = PlayerState.const_get(name)
inverted = PlayerState.const_get("#{name}_LOOKUP")
attr_accessor(getter)
define_method(value_getter) do
value = self.send(getter)
raise ArgumentError, "invalid #{value_getter} value: #{value.inspect}" unless hash.key?(value)
hash[value]
end
define_method("#{value_getter}=") do |value|
raise ArgumentError, "invalid #{value_getter} value: #{value.inspect}" unless inverted.key?(value)
self.send("#{getter}=", inverted[value])
end
end
# All constants defined in data order.
DIFFICULTY_LEVEL = build_hash(%i(veteran rookie _ warrior))
MONEY_HUNDREDS = {
0 => 0x5,
1 => 0x4,
2 => 0x7,
3 => 0x6,
4 => 0x1,
5 => 0x0,
6 => 0x3,
7 => 0x2,
8 => 0xd,
9 => 0xc,
}.freeze
MONEY_TENS = {
0 => 0xa,
1 => 0xb,
2 => 0x8,
3 => 0x9,
4 => 0xe,
5 => 0xf,
6 => 0xc,
7 => 0xd,
8 => 0x2,
9 => 0x3,
}.freeze
MONEY_ONES = {
0 => 0xc,
1 => 0xd,
2 => 0xe,
3 => 0xf,
4 => 0x8,
5 => 0x9,
6 => 0xa,
7 => 0xb,
8 => 0x4,
9 => 0x5,
}.freeze
CHARACTER = build_hash(%i(tarquinn jake _ olaf cyberhawk snake katarina ivan))
DIVISION = build_hash(%i(A B))
PLANET = {
0 => 0x6,
1 => 0x7,
2 => 0x4,
3 => 0x5,
4 => 0x2,
}
COLOR = build_hash(%i(red green black blue _ _ yellow _))
VEHICLE = build_hash(%i(_ _ _ air_blade marauder dirt_devil havac battle_trak))
ARMOR = build_hash(%i(B A D C))
SHOCK = build_hash(%i(B A D C))
TIRE = build_hash(%i(D C B A))
ENGINE = build_hash(%i(B A D C))
NITRO = build_numeric_hash(%w(4 5 6 7 _ 1 2 3))
MINE = build_numeric_hash(%w(4 5 6 7 _ 1 2 3))
GUN = build_numeric_hash(%w(_ 1 2 3 4 5 6 7))
constants.each do |name|
const = PlayerState.const_get(name)
inverted = "#{name}_LOOKUP".to_sym
PlayerState.const_set(inverted, const.invert)
PlayerState.define_prop(name)
end
DEFAULT_VALUES = {
difficulty_level: :warrior,
money: 100,
character: :cyberhawk,
division: :B,
planet: 0,
color: :red,
vehicle: :havac,
armor: :D,
shock: :D,
tire: :D,
engine: :D,
nitro: 7,
mine: 7,
gun: 7,
}.freeze
KEYS = DEFAULT_VALUES.keys.freeze
CODEC = {
difficulty_level: 2,
money_hundreds: 4,
money_tens: 4,
money_ones: 4,
character: 3,
division: 1,
planet: 3,
color: 3,
vehicle: 3,
armor: 2,
shock: 2,
tire: 2,
engine: 2,
nitro: 3,
mine: 3,
gun: 3,
}.freeze
DATA_SIZE = 60
CHAR_SIZE = 5
RNRR_BASE_32 = %w(B C D F G H J K L M N P Q R S T V W X Y Z 0 1 2 3 4 5 6 7 8 9 !).freeze
HASH_INITIAL = '1000000110110000'.chars.map { |c| c.to_i(2) }.freeze
def self.parse(code)
bitset = decode(code)
hash = bitset.take(16)
bits = bitset.drop(16).map { |b| b ? 1 : 0 }
player = new
offset = 0
CODEC.each do |key, length|
key_bits = bits.take(length)
bits = bits.drop(length)
player.send("#{key}_value=", key_bits.join.to_i(2))
end
player
end
def self.decode(code)
code = code.gsub(/\s/, '')[0..11]
bitset = Bitset.new(DATA_SIZE)
code.chars.each_with_index do |char, index|
bits = RNRR_BASE_32.index(char)
CHAR_SIZE.times do |i|
bit = (bits & (2 ** (CHAR_SIZE - i - 1))) != 0
bitset[index * 5 + i] = bit
end
end
bitset
end
def initialize
@props = {}
DEFAULT_VALUES.each do |key, value|
self.send("#{key}=", value)
end
end
def money
money_hundreds * 100 + money_tens * 10 + money_ones
end
def money=(dollars)
self.money_ones = dollars % 10
self.money_tens = dollars % 100 / 10
self.money_hundreds = dollars % 1_000 / 100
end
def attributes
KEYS.inject({}) { |hash, key| hash.update(key => self.send(key)) }
end
def to_s
encoded.each_slice(4).map(&:join).join(' ')
end
private
def encoded
data = player_data
hash = generate_hash(data)
bits = hash + data
bits.map(&:to_s).each_slice(5).map do |slice|
index = slice.join('').to_i(2)
RNRR_BASE_32[index]
end
end
def generate_hash(data)
slices = data.each_slice(16).to_a
tuples = HASH_INITIAL.zip(*slices)
tuples.map do |tuple|
tuple.map(&:to_i).inject(&:^)
end
end
def player_data
CODEC.map do |key, bits|
method = "#{key}_value"
value = self.send(method)
value.to_s(2).rjust(bits, '0').chars.map { |c| c.to_i(2) }
end.flatten
end
end