-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
219 lines (174 loc) · 6.04 KB
/
Copy pathindex.js
File metadata and controls
219 lines (174 loc) · 6.04 KB
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
const defaults = require('defaults');
const definitions = require('./res/definitions');
const fs = require('fs').promises;
const path = require('path');
const { createCanvas } = require('canvas');
let { Image } = require('canvas');
const AVATAR_WIDTH = 86;
const AVATAR_HEIGHT = 115;
const AVATAR_DEFAULTS = {
angle: 1,
head: 0, // possible values are 0, 3, 5, 6 and 7
body: 1, // possible values are 1 and 4
colours: { hair: 2, top: 8, legs: 14, skin: 0 },
wielding: []
};
// various character colours that are defined in the client defined in 32-bit
// integers (rgb).
const SKIN_COLOURS = [ 0xecded0, 0xccb366, 0xb38c40, 0x997326, 0x906020 ];
const HAIR_COLOURS = [
0xffc030, 0xffa040, 0x805030, 0x604020, 0x303030, 0xff6020,
0xff4000, 0xffffff, 65280, 65535
];
const TOP_COLOURS = [
0xff0000, 0xff8000, 0xffe000, 0xa0e000, 57344, 32768, 41088, 45311,
33023, 12528, 0xe000e0, 0x303030, 0x604000, 0x805000, 0xffffff
];
// the orders of which part of the character stack in depending on the angle.
const SPRITE_ORDER = {
FRONT: [
'cape', 'legs', 'skirt', 'body', 'neck', 'head', 'med', 'chain',
'left', 'right', 'boots'
],
BACK: [
'left', 'right', 'legs', 'skirt', 'body', 'neck', 'head', 'med',
'chain', 'cape', 'boots'
],
COMBAT: [
'cape', 'legs', 'skirt', 'body', 'neck', 'head', 'med',
'chain', 'boots', 'left', 'right'
]
};
// which part of the character each position number corresponds to.
const WIELD_SLOTS = {
11: 'cape', 4: 'right', 3: 'left', 6: 'chain', 0: 'head', 5: 'med',
8: 'neck', 1: 'body', 7: 'skirt', 2: 'legs', 9: 'boots'
};
const SPRITE_PATH = path.resolve(__dirname, 'res', 'sprites', '%id.png');
if (typeof window !== 'undefined') {
module.exports.spritePath = '/res/sprites/%id.png';
Image = window.Image;
} else {
module.exports.spritePath = SPRITE_PATH;
}
function fetchSprite(index) {
return new Promise((resolve, reject) => {
const image = new Image();
image.onerror = err => reject(err);
image.onload = () => resolve(image);
image.src = module.exports.spritePath.replace('%id', index);
});
};
// apply a colour filter to the sprite (for skin and/or armour colours).
function colourizeSprite(sprite, overlay, skin) {
const canvas = createCanvas(sprite.width, sprite.height);
const context = canvas.getContext('2d');
if (overlay) {
overlay = {
r: (overlay >> 16) & 0xff,
g: (overlay >> 8) & 0xff,
b: overlay & 0xff
};
}
if (skin) {
skin = {
r: (skin >> 16) & 0xff,
g: (skin >> 8) & 0xff,
b: skin & 0xff
};
}
context.drawImage(sprite, 0, 0);
const pixels = context.getImageData(0, 0, canvas.width, canvas.height);
for (i = 0; i < pixels.data.length; i += 4) {
const r = pixels.data[i];
const g = pixels.data[i + 1];
const b = pixels.data[i + 2];
if (skin) {
if (r === 255 && g === b) {
pixels.data[i] = (r * skin.r >> 8);
pixels.data[i + 1] = (g * skin.g >> 8);
pixels.data[i + 2] = (b * skin.b >> 8);
}
}
if (overlay) {
if (r === g && g === b && r === b) {
pixels.data[i] = (r * overlay.r >> 8);
pixels.data[i + 1] = (g * overlay.g >> 8);
pixels.data[i + 2] = (b * overlay.b >> 8);
}
}
}
context.putImageData(pixels, 0, 0);
return canvas;
}
async function fetchAnimation(animation, angle) {
let sprite = await fetchSprite(animation.sprite + angle);
if (animation.overlay > 5) {
sprite = colourizeSprite(sprite, animation.overlay);
}
return sprite;
}
async function generatePlayer(player) {
const canvas = createCanvas(AVATAR_WIDTH, AVATAR_HEIGHT);
const context = canvas.getContext('2d');
const character = {};
const colours = {};
defaults(player, AVATAR_DEFAULTS);
for (const id of player.wielding) {
const wield = definitions.wieldable[id];
const animation = wield.animation;
const name = WIELD_SLOTS[wield.position];
character[name] = animation - 1;
}
// only add character sprites when there are no equibable items in those
// slots.
if (!character.head) {
character.head = player.head;
colours.head = HAIR_COLOURS[player.colours.hair];
}
if (!character.body) {
character.body = player.body;
colours.body = TOP_COLOURS[player.colours.top];
}
if (!character.legs) {
character.legs = 2;
colours.legs = TOP_COLOURS[player.colours.legs];
}
// apply and store the image buffers for each section of the character,
// along with the offset to draw them at.
for (const section of Object.keys(character)) {
const animation = definitions.animations[character[section]];
let image = await fetchAnimation(animation, player.angle);
const colour = colours[section];
if (colour) {
image = colourizeSprite(image, colour,
SKIN_COLOURS[player.colours.skin]);
}
// skin colour overlay for female platebodies.
if (animation.sprite === 486) {
image = colourizeSprite(image, null,
SKIN_COLOURS[player.colours.skin]);
}
const shift = definitions.shifts[animation.sprite + player.angle];
character[section] = {
image: image,
shift: shift
};
}
let order;
if (player.angle < 15) {
order = player.angle >= 8 ? SPRITE_ORDER.BACK : SPRITE_ORDER.FRONT;
} else {
order = SPRITE_ORDER.COMBAT;
}
// draw the buffered, coloured sprites in the correct order given the
// angle.
for (const section of order) {
const sprite = character[section];
if (sprite) {
context.drawImage(sprite.image, sprite.shift.x, sprite.shift.y);
}
}
return canvas;
};
module.exports.player = generatePlayer;