/
Outfit.java
527 lines (479 loc) · 16.8 KB
/
Outfit.java
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/***************************************************************************
* (C) Copyright 2003-2010 - Stendhal *
***************************************************************************
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
package games.stendhal.server.entity;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import games.stendhal.common.MathHelper;
import games.stendhal.common.Outfits;
import games.stendhal.common.Rand;
/**
* A data structure that represents the outfit of an RPEntity. This RPEntity can
* either be an NPC which uses the outfit sprite system, or of a player.
*
* You can use this data structure so that you don't have to deal with the way
* outfits are stored internally.
*
* An outfit can contain up to nine parts: body, dress, head, mouth, eyes, mask,
* hair, hat & detail.
*
* Note, however, that you can create outfit objects that consist of less than
* nine parts by setting the other parts to <code>null</code>. For example,
* you can create a dress outfit that you can combine with the player's current
* so that the player gets the dress, but keeps his hair, head, and body. To
* denote that a layer should not be drawn, its value can be set to -1.
*
* Not all outfits can be chosen by players.
*
* @author daniel
*
*/
public class Outfit {
/** the logger instance. */
private static final Logger LOGGER = Logger.getLogger(Outfit.class);
private static final Map<String, String> EMPTY_MAP = new HashMap<>();
private final Map<String, Integer> layers = new HashMap<>();
/**
* Creates a new outfit. Set some of the parameters to null if you want an
* entity that put on this outfit to keep on the corresponding parts of its
* current outfit.
*
* Currently supported layers should be in this order:
* body, dress, head, mouth, eyes, mask, hair, hat, detail
*
* @param layers
* Integer indexes of each outfit layer or null.
*/
public Outfit(final Integer... layers) {
int idx = 0;
for (final String lname: Outfits.LAYER_NAMES) {
if (idx >= layers.length) {
break;
}
this.layers.put(lname, layers[idx]);
idx++;
}
}
/**
* Construct an outfit using a string.
*
* @param strcode
* Can be a comma separated key=value list or a 10-digit integer
* for backward compatibility.
*/
public Outfit(final String strcode) {
if (strcode.contains("=")) {
final String[] layers;
if (strcode.contains(",")) {
layers = strcode.split(",");
} else {
layers = new String[] {strcode};
}
for (int idx = 0; idx < layers.length; idx++) {
final String layer = layers[idx];
if (layer.contains("=")) {
final String[] key = layer.split("=");
if (Outfits.LAYER_NAMES.contains(key[0])) {
this.layers.put(key[0], Integer.parseInt(key[1]));
}
}
}
} else {
try {
final int code = Integer.parseInt(strcode);
this.layers.put("body", code % 100);
this.layers.put("dress", code / 100 % 100);
this.layers.put("head", (int) (code / Math.pow(100, 2) % 100));
this.layers.put("eyes", 0);
this.layers.put("mouth", 0);
this.layers.put("mask", 0);
this.layers.put("hair", (int) (code / Math.pow(100, 3) % 100));
this.layers.put("hat", 0);
this.layers.put("detail", (int) (code / Math.pow(100, 4) % 100));
} catch (NumberFormatException e) {
LOGGER.warn("Can't parse outfit code, setting failsafe outfit.");
}
}
}
/**
* This method is added for backwards compatibility. Anything using this should be updated
* for new method.
*
* Creates a new outfit. Set some of the parameters to null if you want an
* entity that put on this outfit to keep on the corresponding parts of its
* current outfit.
*
* @param detail
* The index of the detail style, or null
* @param hair
* The index of the hair style, or null
* @param head
* The index of the head style, or null
* @param dress
* The index of the dress style, or null
* @param body
* The index of the body style, or null
*/
@Deprecated
public Outfit(final Integer detail, Integer hair, Integer head,
Integer dress, Integer body) {
layers.put("body", body);
layers.put("dress", dress);
layers.put("head", head);
layers.put("hair", hair);
layers.put("detail", detail);
}
public Integer getLayer(final String layerName) {
Integer layer = layers.get(layerName);
if (layer == null) {
layer = 0;
}
return layer;
}
public void setLayer(final String layerName, final Integer code) {
layers.put(layerName, code);
}
/**
* Represents this outfit in a numeric code.
*
* This is for backward-compatibility with old outfit system.
*
* @return A 10-digit decimal number where the first pair of digits stand for
* detail, the second pair for hair, the third pair for head, the
* fourth pair for dress, and the fifth pair for body
*/
public int getCode() {
Integer detail = layers.get("detail");
Integer hair = layers.get("hair");
Integer head = layers.get("head");
Integer dress = layers.get("dress");
Integer body = layers.get("body");
int de = 0;
int ha = 0;
int he = 0;
int dr = 0;
int bo = 0;
if (detail != null) {
de = detail.intValue();
}
if (hair != null) {
ha = hair.intValue();
}
if (head != null) {
he = head.intValue();
}
if (dress != null) {
dr = dress.intValue();
}
if (body != null) {
bo = body.intValue();
}
return (de * 100000000) + (ha * 1000000) + (he * 10000) + (dr * 100)
+ bo;
}
/**
* Gets the result that you get when you wear this outfit over another
* outfit. Note that this new outfit can contain parts that are marked as
* NONE; in this case, the parts from the other outfit will be used.
*
* FIXME: the Java client cannot render outfit correctly when this is called
* using an outfit created with Outfit(String) constructor. Not sure
* if problem is in server or client. (AntumDeluge)
*
* @param other
* the outfit that should be worn 'under' the current one
* @return the combined outfit
*/
public Outfit putOver(Outfit old) {
// make sure old outfit is not null
if (old == null) {
old = new Outfit();
}
Integer newBody = layers.get("body");
Integer newDress = layers.get("dress");
Integer newHead = layers.get("head");
Integer newMouth = layers.get("mouth");
Integer newEyes = layers.get("eyes");
Integer newMask = layers.get("mask");
Integer newHair = layers.get("hair");
Integer newHat = layers.get("hat");
Integer newDetail = layers.get("detail");
// wear the this outfit 'over' the other outfit;
// use the other outfit for parts that are not defined for this outfit.
if (newBody == null) {
newBody = old.getLayer("body");
}
if (newDress == null) {
newDress = old.getLayer("dress");
}
if (newHead == null) {
newHead = old.getLayer("head");
}
if (newMouth == null) {
newMouth = old.getLayer("mouth");
}
if (newEyes == null) {
newEyes = old.getLayer("eyes");
}
if (newMask == null) {
newMask = old.getLayer("mask");
}
if (newHair == null) {
newHair = old.getLayer("hair");
}
if (newHat == null) {
newHat = old.getLayer("hat");
}
if (newDetail == null) {
newDetail = old.getLayer("detail");
}
return new Outfit(newBody, newDress, newHead, newMouth, newEyes, newMask, newHair, newHat, newDetail);
}
/**
* Gets the result that you get when you remove (parts of) an outfit.
* Removes the parts in the parameter, from the current outfit.
* NOTE: If a part does not match, the current outfit part will remain the same.
*
* @param other
* the outfit that should be removed from the current one
* @return the new outfit, with the parameter-outfit removed
*/
public Outfit removeOutfit(final Outfit other) {
Integer newBody = layers.get("body");
Integer newDress = layers.get("dress");
Integer newHead = layers.get("head");
Integer newMouth = layers.get("mouth");
Integer newEyes = layers.get("eyes");
Integer newMask = layers.get("mask");
Integer newHair = layers.get("hair");
Integer newHat = layers.get("hat");
Integer newDetail = layers.get("detail");
// wear the this outfit 'over' the other outfit;
// use the other outfit for parts that are not defined for this outfit.
if (newBody == null || newBody.equals(other.getLayer("body"))) {
newBody = 0;
}
if ((newDress == null) || newDress.equals(other.getLayer("dress"))) {
newDress = 0;
}
if ((newHead == null) || newHead.equals(other.getLayer("head"))) {
newHead = 0;
}
if ((newMouth == null) || newMouth.equals(other.getLayer("mouth"))) {
newMouth = 0;
}
if ((newEyes == null) || newEyes.equals(other.getLayer("eyes"))) {
newEyes = 0;
}
if ((newMask == null) || newMask.equals(other.getLayer("mask"))) {
newMask = 0;
}
if ((newHair == null) || newHair.equals(other.getLayer("hair"))) {
newHair = 0;
}
if ((newHat == null) || newHat.equals(other.getLayer("hat"))) {
newHat = 0;
}
if ((newDetail == null) || newDetail.equals(other.getLayer("detail"))) {
newDetail = 0;
}
return new Outfit(newBody, newDress, newHead, newMouth, newEyes, newMask, newHair, newHat, newDetail);
}
/**
* Gets the result that you get when you remove (parts of) an outfit.
* Removes the parts in the parameter, from the current outfit.
* NOTE: If a part does not match, the current outfit part will remain the same.
*
* Currently supported layers should be in this order:
* body, dress, head, mouth, eyes, mask, hair, hat, detail
*
* @param layers
* Integer indexes of each outfit layer that should be removed.
* @return the new outfit, with the layers removed.
*/
public Outfit removeOutfit(final Integer... layers) {
return removeOutfit(new Outfit(layers));
}
/**
* removes the details
*/
public void removeDetail() {
// XXX: would it be better to use put("detail", 0)???
layers.remove("detail");
}
/**
* Checks whether this outfit is equal to or part of another outfit.
*
* @param other
* Another outfit.
* @return true iff this outfit is part of the given outfit.
*/
public boolean isPartOf(final Outfit other) {
Integer hat = layers.get("hat");
Integer mask = layers.get("mask");
Integer eyes = layers.get("eyes");
Integer mouth = layers.get("mouth");
Integer detail = layers.get("detail");
Integer hair = layers.get("hair");
Integer head = layers.get("head");
Integer dress = layers.get("dress");
Integer body = layers.get("body");
return ((hat == null) || hat.equals(other.getLayer("hat")))
&& ((mask == null) || mask.equals(other.getLayer("mask")))
&& ((eyes == null) || eyes.equals(other.getLayer("eyes")))
&& ((mouth == null) || mouth.equals(other.getLayer("mouth")))
&& ((detail == null) || detail.equals(other.getLayer("detail")))
&& ((hair == null) || hair.equals(other.getLayer("hair")))
&& ((head == null) || head.equals(other.getLayer("head")))
&& ((dress == null) || dress.equals(other.getLayer("dress")))
&& ((body == null) || body.equals(other.getLayer("body")));
}
/**
* Checks a single layer of the outfit.
*
* @param layer
* @param index
* @return
*/
public boolean isPartOf(final String layer, final Integer index) {
return index.equals(layers.get(layer));
}
/**
* Checks whether this outfit may be selected by a normal player as normal
* outfit. It returns false for special event and GM outfits.
*
* @return true if it is a normal outfit
*/
public boolean isChoosableByPlayers() {
Integer hat = layers.get("hat");
Integer mask = layers.get("mask");
Integer eyes = layers.get("eyes");
Integer mouth = layers.get("mouth");
Integer detail = layers.get("detail");
Integer hair = layers.get("hair");
Integer head = layers.get("head");
Integer dress = layers.get("dress");
Integer body = layers.get("body");
return (hat == null || (hat < Outfits.HAT_OUTFITS) && (hat >= 0))
&& (mask == null || (mask < Outfits.MASK_OUTFITS) && (mask >= 0))
&& (eyes == null || (eyes < Outfits.EYES_OUTFITS) && (eyes >= 0))
&& (mouth == null || (mouth < Outfits.MOUTH_OUTFITS) && (mouth >= 0))
&& (detail == null || detail == 0)
&& (hair == null || (hair < Outfits.HAIR_OUTFITS) && (hair >= 0))
&& (head == null || (head < Outfits.HEAD_OUTFITS) && (head >= 0))
&& (dress == null || (dress < Outfits.CLOTHES_OUTFITS) && (dress >= 0))
&& (body == null || (body < Outfits.BODY_OUTFITS) && (body >= 0));
}
/**
* Is outfit missing a dress?
*
* @return true if naked, false if dressed
*/
public boolean isNaked() {
final Integer dress = layers.get("dress");
if (isCompatibleWithClothes()) {
return (dress == null) || dress.equals(0);
}
return false;
}
/**
* Create a random unisex outfit, with a 'normal' face and unisex body
*
* <ul>
* <li>hair number (1 to 26) selection of hairs which look ok with both goblin
* face and cute face (later hairs only look right with cute face)</li>
* <li>head numbers (1 to 15) to avoid the cut eye, pink eyes, weird green eyeshadow etc</li>
* <li>dress numbers (1 to 16) from the early outfits before lady player base got introduced i.e. they are all unisex</li>
* <li>base numbers ( 1 to 15), these are the early bodies which were unisex</li>
* </ul>
* @return the new random outfit
*/
public static Outfit getRandomOutfit() {
final int newEyes = Rand.randUniform(0, Outfits.EYES_OUTFITS - 1);
final int newMouth = Rand.randUniform(0, Outfits.MOUTH_OUTFITS - 1);
final int newHair = Rand.randUniform(0, Outfits.HAIR_OUTFITS - 1);
final int newHead = Rand.randUniform(0, Outfits.HEAD_OUTFITS - 1);
final int newDress = Rand.randUniform(1, Outfits.CLOTHES_OUTFITS - 1);
final int newBody = Rand.randUniform(0, Outfits.BODY_OUTFITS - 1);
LOGGER.debug("chose random outfit: "
+ " " + newEyes + " " + newMouth + " " + newHair
+ " " + newHead + " " + newDress + " " + newBody);
return new Outfit(newBody, newDress, newHead, newMouth, newEyes, 0, newHair, 0, 0);
}
/**
* Can this outfit be worn with normal clothes
*
* @return true if the outfit is compatible with clothes, false otherwise
*/
public boolean isCompatibleWithClothes() {
return Outfits.isDressCompatibleBody(layers.get("body"));
}
public boolean isFemale() {
return Outfits.isFemaleBody(layers.get("body"));
}
@Override
public boolean equals(Object other) {
boolean ret = false;
if (!(other instanceof Outfit)) {
return ret;
}
else {
Outfit outfit = (Outfit)other;
return this.getCode() == outfit.getCode();
}
}
@Override
public int hashCode() {
return this.getCode();
}
public String getData(Map<String, String> colors) {
if (colors == null) {
colors = EMPTY_MAP;
}
StringBuilder sb = new StringBuilder();
sb.append("body-" + getLayer("body") + "-");
sb.append(Integer.toHexString(MathHelper.parseIntDefault(colors.get("skin"), 0)));
sb.append("_dress-" + getLayer("dress") + "-");
sb.append(Integer.toHexString(MathHelper.parseIntDefault(colors.get("dress"), 0)));
sb.append("_head-" + getLayer("head") + "-");
sb.append(Integer.toHexString(MathHelper.parseIntDefault(colors.get("skin"), 0)));
sb.append("_mouth-" + getLayer("mouth") + "-");
sb.append(Integer.toHexString(MathHelper.parseIntDefault(colors.get("mouth"), 0)));
sb.append("_eyes-" + getLayer("eyes") + "-");
sb.append(Integer.toHexString(MathHelper.parseIntDefault(colors.get("eyes"), 0)));
sb.append("_mask-" + getLayer("mask") + "-");
sb.append(Integer.toHexString(MathHelper.parseIntDefault(colors.get("mask"), 0)));
sb.append("_hair-" + getLayer("hair") + "-");
sb.append(Integer.toHexString(MathHelper.parseIntDefault(colors.get("hair"), 0)));
sb.append("_hat-" + getLayer("hat") + "-");
sb.append(Integer.toHexString(MathHelper.parseIntDefault(colors.get("hat"), 0)));
sb.append("_detail-" + getLayer("detail") + "-");
sb.append(Integer.toHexString(MathHelper.parseIntDefault(colors.get("detail"), 0)));
return sb.toString();
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
int idx = 0;
for (final String layer: Outfits.LAYER_NAMES) {
final Integer value = layers.get(layer);
if (value != null) {
if (idx > 0) {
sb.append(",");
}
sb.append(layer + "=" + layers.get(layer));
idx++;
}
}
return sb.toString();
}
}