~moody/pse

pse/pokemon.go -rw-r--r-- 7.0 KiB
e4de17d5 — Jacob Moody Introduce pse(1) 9 months ago
                                                                                
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//go:generate sh gen/pokedata.sh
package pse

import (
	"errors"
	"fmt"
)

type PokeDataG struct {
	Species    uint16
	Item       uint16
	EXP        uint32
	PPBonus    byte
	Friendship byte
	magic      uint16

	checksum uint16
}

type PokeDataA struct {
	Move1 uint16
	Move2 uint16
	Move3 uint16
	Move4 uint16
	PP1   byte
	PP2   byte
	PP3   byte
	PP4   byte

	checksum uint16
}

type PokeDataM struct {
	Pokerus byte
	Met     byte
	Origins uint16
	IV      uint32
	Ribbons uint32

	checksum uint16
}

type PokeDataE struct {
	HP     byte
	Atk    byte
	Def    byte
	Spd    byte
	SpAtk  byte
	SpDef  byte
	Cool   byte
	Beauty byte
	Cute   byte
	Smart  byte
	Tough  byte
	Feel   byte

	checksum uint16
}

type PokeData struct {
	PokeDataG
	PokeDataA
	PokeDataM
	PokeDataE

	data []byte
}

func cryptPokeBlock(b []byte, ID uint32, personality uint32) []byte {
	var key [4]byte
	put4(ID^personality, key[:])
	out := make([]byte, len(b))
	for i := 0; i < len(b); i++ {
		out[i] = b[i] ^ key[i%4]
	}
	return out
}

func NewPokeDataG(b []byte, ID uint32, personality uint32) PokeDataG {
	b = cryptPokeBlock(b, ID, personality)
	p := PokeDataG{
		Species:    get2(b[0:]),
		Item:       get2(b[2:]),
		EXP:        get4(b[4:]),
		PPBonus:    b[8],
		Friendship: b[9],
		magic:      get2(b[10:]),
	}
	p.checksum = 0
	for i := 0; i < 12; i += 2 {
		p.checksum += get2(b[i:])
	}
	return p
}

func (p *PokeDataG) Commit(ID uint32, personality uint32) []byte {
	var out [12]byte
	put2(p.Species, out[0:])
	put2(p.Item, out[2:])
	put4(p.EXP, out[4:])
	out[8] = p.PPBonus
	out[9] = p.Friendship
	put2(p.magic, out[10:])
	p.checksum = 0
	for i := 0; i < 12; i += 2 {
		p.checksum += get2(out[i:])
	}
	return cryptPokeBlock(out[:], ID, personality)
}

func NewPokeDataA(b []byte, ID uint32, personality uint32) PokeDataA {
	b = cryptPokeBlock(b, ID, personality)
	p := PokeDataA{
		Move1: get2(b[0:]),
		Move2: get2(b[2:]),
		Move3: get2(b[4:]),
		Move4: get2(b[6:]),
		PP1:   b[8],
		PP2:   b[9],
		PP3:   b[10],
		PP4:   b[11],
	}
	p.checksum = 0
	for i := 0; i < 12; i += 2 {
		p.checksum += get2(b[i:])
	}
	return p
}

func (p *PokeDataA) Commit(ID uint32, personality uint32) []byte {
	var out [12]byte
	put2(p.Move1, out[0:])
	put2(p.Move2, out[2:])
	put2(p.Move3, out[4:])
	put2(p.Move4, out[6:])
	out[8] = p.PP1
	out[9] = p.PP2
	out[10] = p.PP3
	out[11] = p.PP4
	p.checksum = 0
	for i := 0; i < 12; i += 2 {
		p.checksum += get2(out[i:])
	}
	return cryptPokeBlock(out[:], ID, personality)
}

func NewPokeDataM(b []byte, ID uint32, personality uint32) PokeDataM {
	b = cryptPokeBlock(b, ID, personality)
	p := PokeDataM{
		Pokerus: b[0],
		Met:     b[1],
		Origins: get2(b[2:]),
		IV:      get4(b[4:]),
		Ribbons: get4(b[8:]),
	}
	p.checksum = 0
	for i := 0; i < 12; i += 2 {
		p.checksum += get2(b[i:])
	}
	return p
}

func (p *PokeDataM) Commit(ID uint32, personality uint32) []byte {
	var out [12]byte
	out[0] = p.Pokerus
	out[1] = p.Met
	put2(p.Origins, out[2:])
	put4(p.IV, out[4:])
	put4(p.Ribbons, out[8:])
	p.checksum = 0
	for i := 0; i < 12; i += 2 {
		p.checksum += get2(out[i:])
	}
	return cryptPokeBlock(out[:], ID, personality)
}

func NewPokeDataE(b []byte, ID uint32, personality uint32) PokeDataE {
	b = cryptPokeBlock(b, ID, personality)
	p := PokeDataE{
		HP:     b[0],
		Atk:    b[1],
		Def:    b[2],
		Spd:    b[3],
		SpAtk:  b[4],
		SpDef:  b[5],
		Cool:   b[6],
		Beauty: b[7],
		Cute:   b[8],
		Smart:  b[9],
		Tough:  b[10],
		Feel:   b[11],
	}
	p.checksum = 0
	for i := 0; i < 12; i += 2 {
		p.checksum += get2(b[i:])
	}
	return p
}

func (p *PokeDataE) Commit(ID uint32, personality uint32) []byte {
	var out [12]byte
	out[0] = p.HP
	out[1] = p.Atk
	out[2] = p.Def
	out[3] = p.Spd
	out[4] = p.SpAtk
	out[5] = p.SpDef
	out[6] = p.Cool
	out[7] = p.Beauty
	out[8] = p.Cute
	out[9] = p.Smart
	out[10] = p.Tough
	out[11] = p.Feel
	p.checksum = 0
	for i := 0; i < 12; i += 2 {
		p.checksum += get2(out[i:])
	}
	return cryptPokeBlock(out[:], ID, personality)
}

type Pokemon struct {
	Personality uint32
	OTID        uint32
	Name        *PkString
	Lang        uint16
	OTName      *PkString
	Marks       byte
	Checksum    uint16
	magic       uint16   //Unused magic value
	derived     [20]byte //Derived stats that get reset on boxing

	PokeData PokeData //Parsed PokeData block

	isParty bool   //If the pokemon is in the party and thus has derived stats
	data    []byte //Backing data store
}

func (p *Pokemon) Format(f fmt.State, verb rune) {
	switch verb {
	case 'a':
		pd := &p.PokeData
		fmt.Fprintf(f, "%v\t%v\t%q\t%v\t%q\t%v\t", p.Personality, p.OTID, p.Name.string, p.Lang, p.OTName.string, p.Marks)
		fmt.Fprintf(f, "%v\t%v\t%v\t%v\t%v\t", pd.Species, pd.Item, pd.EXP, pd.PPBonus, pd.Friendship)
		fmt.Fprintf(f, "%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t", pd.Move1, pd.Move2, pd.Move3, pd.Move4, pd.PP1, pd.PP2, pd.PP3, pd.PP4)
		fmt.Fprintf(f, "%v\t%v\t", pd.Pokerus, pd.IV)
		fmt.Fprintf(f, "%v\t%v\t%v\t%v\t%v\t%v", pd.HP, pd.Atk, pd.Def, pd.Spd, pd.SpAtk, pd.SpDef)
	}
}

func (p *Pokemon) Scan(state fmt.ScanState, verb rune) error {
	pd := &p.PokeData
	_, err := fmt.Fscanf(state, "%v\t%v\t%q\t%v\t%q\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t\n",
		&p.Personality, &p.OTID, &p.Name.string, &p.Lang, &p.OTName.string, &p.Marks,
		&pd.Species, &pd.Item, &pd.EXP, &pd.PPBonus, &pd.Friendship,
		&pd.Move1, &pd.Move2, &pd.Move3, &pd.Move4, &pd.PP1, &pd.PP2, &pd.PP3, &pd.PP4,
		&pd.Pokerus, &pd.IV,
		&pd.HP, &pd.Atk, &pd.Def, &pd.Spd, &pd.SpAtk, &pd.SpDef)
	return err
}

func NewPokemon(data []byte, isParty bool) (*Pokemon, error) {
	p := &Pokemon{
		Personality: get4(data[0:]),
		OTID:        get4(data[4:]),
		Lang:        get2(data[18:]),
		Marks:       data[27],
		Checksum:    get2(data[28:]),
		magic:       get2(data[30:]),
		isParty:     isParty,
		data:        data,
	}
	if p.Personality == 0 && p.OTID == 0 {
		return nil, errors.New("null'd pokemon")
	}

	var err error
	p.Name, err = NewPkString(data[8:18], int(p.Lang))
	if err != nil {
		return nil, err
	}

	p.OTName, err = NewPkString(data[20:27], int(p.Lang))
	if err != nil {
		return nil, err
	}

	p.PokeData = NewPokeData(data[32:32+48], p.OTID, p.Personality)
	if p.Checksum != p.PokeData.PokeDataG.checksum+p.PokeData.PokeDataA.checksum+p.PokeData.PokeDataM.checksum+p.PokeData.PokeDataE.checksum {
		return nil, errors.New("NewPokemon: checksum mismatch")
	}
	if isParty {
		copy(p.derived[:], data[80:])
	}
	return p, nil
}

func (p *Pokemon) Commit() error {
	put4(p.Personality, p.data[0:])
	put4(p.OTID, p.data[4:])

	err := p.Name.Commit(int(p.Lang))
	if err != nil {
		return err
	}

	put2(p.Lang, p.data[18:])

	err = p.OTName.Commit(int(p.Lang))
	if err != nil {
		return err
	}

	p.data[27] = p.Marks
	put2(p.magic, p.data[30:])
	p.PokeData.Commit(p.OTID, p.Personality)
	copy(p.data[32:32+48], p.PokeData.data)

	var checksum uint16 = 0
	checksum += p.PokeData.PokeDataG.checksum
	checksum += p.PokeData.PokeDataA.checksum
	checksum += p.PokeData.PokeDataM.checksum
	checksum += p.PokeData.PokeDataE.checksum
	put2(checksum, p.data[28:])

	if p.isParty {
		copy(p.data[80:], p.derived[:])
	}
	return nil
}