Avatar.java 29.8 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
package emu.grasscutter.game.avatar;

Yazawazi's avatar
Yazawazi committed
3
import java.util.ArrayList;
Melledy's avatar
Melledy committed
4
5
import java.util.HashMap;
import java.util.HashSet;
Yazawazi's avatar
Yazawazi committed
6
import java.util.List;
Melledy's avatar
Melledy committed
7
import java.util.Map;
8
import java.util.Map.Entry;
Melledy's avatar
Melledy committed
9
10
11
12
13
14
15
16
17
18
import java.util.Set;

import org.bson.types.ObjectId;

import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Id;
import dev.morphia.annotations.Indexed;
import dev.morphia.annotations.PostLoad;
import dev.morphia.annotations.PrePersist;
import dev.morphia.annotations.Transient;
19
import emu.grasscutter.data.GameData;
Melledy's avatar
Melledy committed
20
21
import emu.grasscutter.data.binout.OpenConfigEntry;
import emu.grasscutter.data.binout.OpenConfigEntry.SkillPointModifier;
Melledy's avatar
Melledy committed
22
import emu.grasscutter.data.common.FightPropData;
Melledy's avatar
Melledy committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import emu.grasscutter.data.excels.AvatarData;
import emu.grasscutter.data.excels.AvatarPromoteData;
import emu.grasscutter.data.excels.AvatarSkillData;
import emu.grasscutter.data.excels.AvatarSkillDepotData;
import emu.grasscutter.data.excels.AvatarTalentData;
import emu.grasscutter.data.excels.EquipAffixData;
import emu.grasscutter.data.excels.ProudSkillData;
import emu.grasscutter.data.excels.ReliquaryAffixData;
import emu.grasscutter.data.excels.ReliquaryLevelData;
import emu.grasscutter.data.excels.ReliquaryMainPropData;
import emu.grasscutter.data.excels.ReliquarySetData;
import emu.grasscutter.data.excels.WeaponCurveData;
import emu.grasscutter.data.excels.WeaponPromoteData;
import emu.grasscutter.data.excels.AvatarSkillDepotData.InherentProudSkillOpens;
import emu.grasscutter.data.excels.ItemData.WeaponProperty;
Melledy's avatar
Melledy committed
38
39
40
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.game.entity.EntityAvatar;
import emu.grasscutter.game.inventory.EquipType;
41
import emu.grasscutter.game.inventory.GameItem;
42
import emu.grasscutter.game.inventory.ItemType;
Melledy's avatar
Melledy committed
43
import emu.grasscutter.game.player.Player;
Melledy's avatar
Melledy committed
44
45
import emu.grasscutter.game.props.ElementType;
import emu.grasscutter.game.props.EntityIdType;
Yazawazi's avatar
Yazawazi committed
46
import emu.grasscutter.game.props.FetterState;
Melledy's avatar
Melledy committed
47
48
49
50
import emu.grasscutter.game.props.FightProperty;
import emu.grasscutter.game.props.PlayerProperty;
import emu.grasscutter.net.proto.AvatarFetterInfoOuterClass.AvatarFetterInfo;
import emu.grasscutter.net.proto.AvatarInfoOuterClass.AvatarInfo;
51
import emu.grasscutter.net.proto.AvatarSkillInfoOuterClass.AvatarSkillInfo;
52
53
54
55
import emu.grasscutter.net.proto.FetterDataOuterClass.FetterData;
import emu.grasscutter.net.proto.ShowAvatarInfoOuterClass;
import emu.grasscutter.net.proto.ShowAvatarInfoOuterClass.ShowAvatarInfo;
import emu.grasscutter.net.proto.ShowEquipOuterClass.ShowEquip;
56
import emu.grasscutter.server.packet.send.PacketAbilityChangeNotify;
Melledy's avatar
Melledy committed
57
58
59
60
61
62
63
64
import emu.grasscutter.server.packet.send.PacketAvatarEquipChangeNotify;
import emu.grasscutter.server.packet.send.PacketAvatarFightPropNotify;
import emu.grasscutter.utils.ProtoHelper;
import it.unimi.dsi.fastutil.ints.Int2FloatOpenHashMap;
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;

65
66
import static emu.grasscutter.Configuration.GAME_OPTIONS;

KingRainbow44's avatar
KingRainbow44 committed
67
@Entity(value = "avatars", useDiscriminator = false)
68
public class Avatar {
Melledy's avatar
Melledy committed
69
70
71
	@Id private ObjectId id;
	@Indexed private int ownerId;	// Id of player that this avatar belongs to
	
72
	@Transient private Player owner;
Melledy's avatar
Melledy committed
73
	@Transient private AvatarData data;
Melledy's avatar
Melledy committed
74
	@Transient private AvatarSkillDepotData skillDepot;
Melledy's avatar
Melledy committed
75
76
77
78
79
80
81
82
83
	@Transient private long guid;	// Player unique id
	private int avatarId;			// Id of avatar
	
	private int level = 1;
	private int exp;
	private int promoteLevel;
	private int satiation; // ?
	private int satiationPenalty; // ?
	private float currentHp;
logictc's avatar
logictc committed
84
	private float currentEnergy;
Melledy's avatar
Melledy committed
85
	
86
	@Transient private final Int2ObjectMap<GameItem> equips;
Melledy's avatar
Melledy committed
87
	@Transient private final Int2FloatOpenHashMap fightProp;
88
	@Transient private Set<String> extraAbilityEmbryos;
Melledy's avatar
Melledy committed
89
	
Yazawazi's avatar
typo    
Yazawazi committed
90
	private List<Integer> fetters;
Yazawazi's avatar
Yazawazi committed
91

Melledy's avatar
Melledy committed
92
	private Map<Integer, Integer> skillLevelMap; // Talent levels
93
	private Map<Integer, Integer> skillExtraChargeMap; // Charges
Melledy's avatar
Melledy committed
94
95
96
97
98
99
100
101
102
	private Map<Integer, Integer> proudSkillBonusMap; // Talent bonus levels (from const)
	private int skillDepotId;
	private int coreProudSkillLevel; // Constellation level
	private Set<Integer> talentIdList; // Constellation id list
	private Set<Integer> proudSkillList; // Character passives
	
	private int flyCloak;
	private int costume;
	private int bornTime;
Yazawazi's avatar
Yazawazi committed
103
104
105

	private int fetterLevel = 1;
	private int fetterExp;
Yazawazi's avatar
Yazawazi committed
106
107

	private int nameCardRewardId;
Yazawazi's avatar
Yazawazi committed
108
	private int nameCardId;
Melledy's avatar
Melledy committed
109
	
Melledy's avatar
Melledy committed
110
	@Deprecated // Do not use. Morhpia only!
111
	public Avatar() {
Melledy's avatar
Melledy committed
112
113
		this.equips = new Int2ObjectOpenHashMap<>();
		this.fightProp = new Int2FloatOpenHashMap();
114
		this.extraAbilityEmbryos = new HashSet<>();
Yazawazi's avatar
Yazawazi committed
115
		this.proudSkillBonusMap = new HashMap<>(); 
116
		this.fetters = new ArrayList<>(); // TODO Move to avatar
Melledy's avatar
Melledy committed
117
118
119
	}
	
	// On creation
120
121
	public Avatar(int avatarId) {
		this(GameData.getAvatarDataMap().get(avatarId));
Melledy's avatar
Melledy committed
122
123
	}
	
124
	public Avatar(AvatarData data) {
Melledy's avatar
Melledy committed
125
126
		this();
		this.avatarId = data.getId();
Yazawazi's avatar
Yazawazi committed
127
		this.nameCardRewardId = data.getNameCardRewardId();
Yazawazi's avatar
Yazawazi committed
128
		this.nameCardId = data.getNameCardId();
Melledy's avatar
Melledy committed
129
130
131
132
133
		this.data = data;
		this.bornTime = (int) (System.currentTimeMillis() / 1000);
		this.flyCloak = 140001;
		
		this.skillLevelMap = new HashMap<>();
134
		this.skillExtraChargeMap = new HashMap<>();
Melledy's avatar
Melledy committed
135
136
137
138
139
140
141
142
143
144
145
146
		this.talentIdList = new HashSet<>();
		this.proudSkillList = new HashSet<>();
		
		// Combat properties
		for (FightProperty prop : FightProperty.values()) {
			if (prop.getId() <= 0 || prop.getId() >= 3000) {
				continue;
			}
			this.setFightProperty(prop, 0f);
		}
		
		// Skill depot
Melledy's avatar
Melledy committed
147
		this.setSkillDepotData(getAvatarData().getSkillDepot());
Melledy's avatar
Melledy committed
148
149
150
151
152
		
		// Set stats
		this.recalcStats();
		this.currentHp = getFightProperty(FightProperty.FIGHT_PROP_MAX_HP);
		setFightProperty(FightProperty.FIGHT_PROP_CUR_HP, this.currentHp);
logictc's avatar
logictc committed
153
		this.currentEnergy = 0f;
Melledy's avatar
Melledy committed
154
155
156
157
		// Load handler
		this.onLoad();
	}
	
158
	public Player getPlayer() {
Melledy's avatar
Melledy committed
159
160
161
162
163
164
165
166
167
168
169
170
		return this.owner;
	}

	public ObjectId getObjectId() {
		return id;
	}

	public AvatarData getAvatarData() {
		return data;
	}

	protected void setAvatarData(AvatarData data) {
Melledy's avatar
Melledy committed
171
172
		if (this.data != null) return;
		this.data = data; // Used while loading this from the database
Melledy's avatar
Melledy committed
173
174
175
176
177
178
	}

	public int getOwnerId() {
		return ownerId;
	}

179
	public void setOwner(Player player) {
Melledy's avatar
Melledy committed
180
		this.owner = player;
181
		this.ownerId = player.getUid();
182
		this.guid = player.getNextGameGuid();
Melledy's avatar
Melledy committed
183
184
185
186
187
188
189
190
191
192
	}
	
	public int getSatiation() {
		return satiation;
	}

	public void setSatiation(int satiation) {
		this.satiation = satiation;
	}

Yazawazi's avatar
Yazawazi committed
193
194
195
196
197
198
199
200
	public int getNameCardRewardId() {
		return nameCardRewardId;
	}

	public void setNameCardRewardId(int nameCardRewardId) {
		this.nameCardRewardId = nameCardRewardId;
	}

Melledy's avatar
Melledy committed
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
	public int getSatiationPenalty() {
		return satiationPenalty;
	}

	public void setSatiationPenalty(int satiationPenalty) {
		this.satiationPenalty = satiationPenalty;
	}

	public AvatarData getData() {
		return data;
	}

	public long getGuid() {
		return guid;
	}

	public int getAvatarId() {
		return avatarId;
	}

	public int getLevel() {
		return level;
	}
	
	public void setLevel(int level) {
		this.level = level;
	}

	public int getExp() {
		return exp;
	}
	
	public void setExp(int exp) {
		this.exp = exp;
	}

	public int getPromoteLevel() {
		return promoteLevel;
	}

	public void setPromoteLevel(int promoteLevel) {
		this.promoteLevel = promoteLevel;
	}

245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
	static public int getMinPromoteLevel(int level) {
		if (level > 80) {
			return 6;
		} else if (level > 70) {
			return 5;
		} else if (level > 60) {
			return 4;
		} else if (level > 50) {
			return 3;
		} else if (level > 40) {
			return 2;
		} else if (level > 20) {
			return 1;
		}
		return 0;
	}

262
	public Int2ObjectMap<GameItem> getEquips() {
Melledy's avatar
Melledy committed
263
264
265
		return equips;
	}
	
266
	public GameItem getEquipBySlot(EquipType slot) {
Melledy's avatar
Melledy committed
267
268
269
		return this.getEquips().get(slot.getValue());
	}
	
270
	private GameItem getEquipBySlot(int slotId) {
Melledy's avatar
Melledy committed
271
272
273
		return this.getEquips().get(slotId);
	}
	
274
	public GameItem getWeapon() {
Melledy's avatar
Melledy committed
275
276
277
278
279
280
281
		return this.getEquipBySlot(EquipType.EQUIP_WEAPON);
	}

	public int getSkillDepotId() {
		return skillDepotId;
	}

Melledy's avatar
Melledy committed
282
283
284
285
286
287
288
289
290
291
292
	public AvatarSkillDepotData getSkillDepot() {
		return skillDepot;
	}
	
	protected void setSkillDepot(AvatarSkillDepotData skillDepot) {
		if (this.skillDepot != null) return;
		this.skillDepot = skillDepot; // Used while loading this from the database
	}

	public void setSkillDepotData(AvatarSkillDepotData skillDepot) {
		// Set id and depot
Melledy's avatar
Melledy committed
293
		this.skillDepotId = skillDepot.getId();
Melledy's avatar
Melledy committed
294
		this.skillDepot = skillDepot;
Melledy's avatar
Melledy committed
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
		// Clear, then add skills
		getSkillLevelMap().clear();
		if (skillDepot.getEnergySkill() > 0) {
			getSkillLevelMap().put(skillDepot.getEnergySkill(), 1);
		}
		for (int skillId : skillDepot.getSkills()) {
			if (skillId > 0) {
				getSkillLevelMap().put(skillId, 1);
			}
		}
		// Add proud skills
		this.getProudSkillList().clear();
		for (InherentProudSkillOpens openData : skillDepot.getInherentProudSkillOpens()) {
			if (openData.getProudSkillGroupId() == 0) {
				continue;
			}
			if (openData.getNeedAvatarPromoteLevel() <= this.getPromoteLevel()) {
				int proudSkillId = (openData.getProudSkillGroupId() * 100) + 1;
313
				if (GameData.getProudSkillDataMap().containsKey(proudSkillId)) {
Melledy's avatar
Melledy committed
314
315
316
317
318
319
320
321
322
					this.getProudSkillList().add(proudSkillId);
				}
			}
		}
	}

	public Map<Integer, Integer> getSkillLevelMap() {
		return skillLevelMap;
	}
323
324
325
326
327
328
329
	
	public Map<Integer, Integer> getSkillExtraChargeMap() {
		if (skillExtraChargeMap == null) {
			skillExtraChargeMap = new HashMap<>();
		}
		return skillExtraChargeMap;
	}
Melledy's avatar
Melledy committed
330
331
332
333
334

	public Map<Integer, Integer> getProudSkillBonusMap() {
		return proudSkillBonusMap;
	}

335
336
	public Set<String> getExtraAbilityEmbryos() {
		return extraAbilityEmbryos;
Melledy's avatar
Melledy committed
337
338
	}

Yazawazi's avatar
Yazawazi committed
339
	public void setFetterList(List<Integer> fetterList) {
340
		this.fetters = fetterList;
Yazawazi's avatar
Yazawazi committed
341
342
343
	}

	public List<Integer> getFetterList() {
344
		return fetters;
Yazawazi's avatar
Yazawazi committed
345
346
	}

Yazawazi's avatar
Yazawazi committed
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
	public int getFetterLevel() {
		return fetterLevel;
	}

	public void setFetterLevel(int fetterLevel) {
		this.fetterLevel = fetterLevel;
	}

	public int getFetterExp() {
		return fetterExp;
	}

	public void setFetterExp(int fetterExp) {
		this.fetterExp = fetterExp;
	}

Yazawazi's avatar
Yazawazi committed
363
364
365
366
367
368
369
370
	public int getNameCardId() {
		return nameCardId;
	}

	public void setNameCardId(int nameCardId) {
		this.nameCardId = nameCardId;
	}

Melledy's avatar
Melledy committed
371
372
373
374
375
376
377
378
	public float getCurrentHp() {
		return currentHp;
	}

	public void setCurrentHp(float currentHp) {
		this.currentHp = currentHp;
	}

logictc's avatar
logictc committed
379
	public void setCurrentEnergy() {
380
381
382
		if (GAME_OPTIONS.energyUsage) {
			this.setCurrentEnergy(this.currentEnergy);
		}
logictc's avatar
logictc committed
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
	}
	
	public void setCurrentEnergy(float currentEnergy) {
		if (this.getSkillDepot() != null && this.getSkillDepot().getEnergySkillData() != null) {
			ElementType element = this.getSkillDepot().getElementType();
			this.setFightProperty(element.getMaxEnergyProp(), this.getSkillDepot().getEnergySkillData().getCostElemVal());
			
			if (GAME_OPTIONS.energyUsage) {
				this.setFightProperty(element.getCurEnergyProp(), currentEnergy);
			}
			else {
				this.setFightProperty(element.getCurEnergyProp(), this.getSkillDepot().getEnergySkillData().getCostElemVal());
			}
		}		
	}

	public void setCurrentEnergy(FightProperty curEnergyProp, float currentEnergy) {
400
401
402
403
404
		if (GAME_OPTIONS.energyUsage) {
			this.setFightProperty(curEnergyProp, currentEnergy);
			this.currentEnergy = currentEnergy;
			this.save();
		}
logictc's avatar
logictc committed
405
406
	}

Melledy's avatar
Melledy committed
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
	public Int2FloatOpenHashMap getFightProperties() {
		return fightProp;
	}
	
	public void setFightProperty(FightProperty prop, float value) {
		this.getFightProperties().put(prop.getId(), value);
	}
	
	private void setFightProperty(int id, float value) {
		this.getFightProperties().put(id, value);
	}
	
	public void addFightProperty(FightProperty prop, float value) {
		this.getFightProperties().put(prop.getId(), getFightProperty(prop) + value);
	}
	
	public float getFightProperty(FightProperty prop) {
		return getFightProperties().getOrDefault(prop.getId(), 0f);
	}

	public Set<Integer> getTalentIdList() {
		return talentIdList;
	}

	public int getCoreProudSkillLevel() {
		return coreProudSkillLevel;
	}

	public void setCoreProudSkillLevel(int constLevel) {
		this.coreProudSkillLevel = constLevel;
	}

	public Set<Integer> getProudSkillList() {
		return proudSkillList;
	}
	
	public int getFlyCloak() {
		return flyCloak;
	}
	
	public void setFlyCloak(int flyCloak) {
		this.flyCloak = flyCloak;
	}

	public int getCostume() {
		return costume;
	}

	public void setCostume(int costume) {
		this.costume = costume;
	}

	public int getBornTime() {
		return bornTime;
	}
	
463
	public boolean equipItem(GameItem item, boolean shouldRecalc) {
Melledy's avatar
Melledy committed
464
		// Sanity check equip type
Melledy's avatar
Melledy committed
465
466
467
468
		EquipType itemEquipType = item.getItemData().getEquipType();
		if (itemEquipType == EquipType.EQUIP_NONE) {
			return false;
		}
Melledy's avatar
Melledy committed
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485

		// Check if other avatars have this item equipped
		Avatar otherAvatar = getPlayer().getAvatars().getAvatarById(item.getEquipCharacter());
		if (otherAvatar != null) {
			// Unequip other avatar's item
			if (otherAvatar.unequipItem(item.getItemData().getEquipType())) {
				getPlayer().sendPacket(new PacketAvatarEquipChangeNotify(otherAvatar, item.getItemData().getEquipType()));
			} 
			// Swap with other avatar
			if (getEquips().containsKey(itemEquipType.getValue())) {
				GameItem toSwap = this.getEquipBySlot(itemEquipType);
				otherAvatar.equipItem(toSwap, false);
			}
			// Recalc
			otherAvatar.recalcStats();
		} else if (getEquips().containsKey(itemEquipType.getValue())) {
			// Unequip item in current slot if it exists
Melledy's avatar
Melledy committed
486
487
488
			unequipItem(itemEquipType);
		}
		
Melledy's avatar
Melledy committed
489
		// Set equip
Melledy's avatar
Melledy committed
490
491
492
493
494
495
496
497
498
		getEquips().put(itemEquipType.getValue(), item);
		
		if (itemEquipType == EquipType.EQUIP_WEAPON && getPlayer().getWorld() != null) {
			item.setWeaponEntityId(this.getPlayer().getWorld().getNextEntityId(EntityIdType.WEAPON));
		}
		
		item.setEquipCharacter(this.getAvatarId());
		item.save();
		
499
500
501
502
		if (this.getPlayer().hasSentAvatarDataNotify()) {
			this.getPlayer().sendPacket(new PacketAvatarEquipChangeNotify(this, item));
		}
		
Melledy's avatar
Melledy committed
503
504
505
506
507
508
509
510
		if (shouldRecalc) {
			this.recalcStats();
		}
		
		return true;
	}
	
	public boolean unequipItem(EquipType slot) {
511
		GameItem item = getEquips().remove(slot.getValue());
Melledy's avatar
Melledy committed
512
513
514
515
516
517
518
519
520
521
522
		
		if (item != null) {
			item.setEquipCharacter(0);
			item.save();
			return true;
		}
		
		return false;
	}
	
	public void recalcStats() {
523
524
525
526
		recalcStats(false);
	}
	
	public void recalcStats(boolean forceSendAbilityChange) {
Melledy's avatar
Melledy committed
527
528
		// Setup
		AvatarData data = this.getAvatarData();
529
		AvatarPromoteData promoteData = GameData.getAvatarPromoteData(data.getAvatarPromoteId(), this.getPromoteLevel());
Melledy's avatar
Melledy committed
530
		Int2IntOpenHashMap setMap = new Int2IntOpenHashMap();
531
532
533
534
		
		// Extra ability embryos
		Set<String> prevExtraAbilityEmbryos = this.getExtraAbilityEmbryos();
		this.extraAbilityEmbryos = new HashSet<>();
Yazawazi's avatar
Yazawazi committed
535
536
537

		// Fetters
		this.setFetterList(data.getFetters());
Yazawazi's avatar
Yazawazi committed
538
		this.setNameCardRewardId(data.getNameCardRewardId());
Yazawazi's avatar
Yazawazi committed
539
		this.setNameCardId(data.getNameCardId());
Melledy's avatar
Melledy committed
540
541
542
543
		
		// Get hp percent, set to 100% if none
		float hpPercent = this.getFightProperty(FightProperty.FIGHT_PROP_MAX_HP) <= 0 ? 1f : this.getFightProperty(FightProperty.FIGHT_PROP_CUR_HP) / this.getFightProperty(FightProperty.FIGHT_PROP_MAX_HP);
		
544
		// Store current energy value for later
545
		float currentEnergy = (this.getSkillDepot() != null) ? this.getFightProperty(this.getSkillDepot().getElementType().getCurEnergyProp()) : 0f;
546

Melledy's avatar
Melledy committed
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
		// Clear properties
		this.getFightProperties().clear();
		
		// Base stats
		this.setFightProperty(FightProperty.FIGHT_PROP_BASE_HP, data.getBaseHp(this.getLevel()));
		this.setFightProperty(FightProperty.FIGHT_PROP_BASE_ATTACK, data.getBaseAttack(this.getLevel()));
		this.setFightProperty(FightProperty.FIGHT_PROP_BASE_DEFENSE, data.getBaseDefense(this.getLevel()));
		this.setFightProperty(FightProperty.FIGHT_PROP_CRITICAL, data.getBaseCritical());
		this.setFightProperty(FightProperty.FIGHT_PROP_CRITICAL_HURT, data.getBaseCriticalHurt());
		this.setFightProperty(FightProperty.FIGHT_PROP_CHARGE_EFFICIENCY, 1f);
		
		if (promoteData != null) {
			for (FightPropData fightPropData : promoteData.getAddProps()) {
				this.addFightProperty(fightPropData.getProp(), fightPropData.getValue());
			}
		}
		
		// Set energy usage
logictc's avatar
logictc committed
565
		setCurrentEnergy(currentEnergy);
Melledy's avatar
Melledy committed
566
567
568
569
		
		// Artifacts
		for (int slotId = 1; slotId <= 5; slotId++) {
			// Get artifact
570
			GameItem equip = this.getEquipBySlot(slotId);
Melledy's avatar
Melledy committed
571
572
573
574
			if (equip == null) {
				continue;
			}
			// Artifact main stat
575
			ReliquaryMainPropData mainPropData = GameData.getReliquaryMainPropDataMap().get(equip.getMainPropId());
Melledy's avatar
Melledy committed
576
			if (mainPropData != null) {
577
				ReliquaryLevelData levelData = GameData.getRelicLevelData(equip.getItemData().getRankLevel(), equip.getLevel());
Melledy's avatar
Melledy committed
578
579
580
581
582
583
				if (levelData != null) {
					this.addFightProperty(mainPropData.getFightProp(), levelData.getPropValue(mainPropData.getFightProp()));
				}	
			}
			// Artifact sub stats
			for (int appendPropId : equip.getAppendPropIdList()) {
584
				ReliquaryAffixData affixData = GameData.getReliquaryAffixDataMap().get(appendPropId);
Melledy's avatar
Melledy committed
585
586
587
588
589
590
591
592
593
594
595
596
				if (affixData != null) {
					this.addFightProperty(affixData.getFightProp(), affixData.getPropValue());
				}
			}
			// Set bonus
			if (equip.getItemData().getSetId() > 0) {
				setMap.addTo(equip.getItemData().getSetId(), 1);
			}
		}

		// Set stuff
		for (Int2IntOpenHashMap.Entry e : setMap.int2IntEntrySet()) {
597
			ReliquarySetData setData = GameData.getReliquarySetDataMap().get(e.getIntKey());
Melledy's avatar
Melledy committed
598
599
600
601
602
603
604
605
606
607
608
609
			if (setData == null) {
				continue;
			}
			
			// Calculate how many items are from the set
			int amount = e.getIntValue();
			
			// Add affix data from set bonus
			for (int setIndex = 0; setIndex < setData.getSetNeedNum().length; setIndex++) {
				if (amount >= setData.getSetNeedNum()[setIndex]) {
					int affixId = (setData.getEquipAffixId() * 10) + setIndex;
					
610
					EquipAffixData affix = GameData.getEquipAffixDataMap().get(affixId);
Melledy's avatar
Melledy committed
611
612
613
614
615
616
617
618
619
620
					if (affix == null) {
						continue;
					}
					
					// Add properties from this affix to our avatar
					for (FightPropData prop : affix.getAddProps()) {
						this.addFightProperty(prop.getProp(), prop.getValue());
					}
					
					// Add any skill strings from this affix
621
					this.addToExtraAbilityEmbryos(affix.getOpenConfig(), true);
Melledy's avatar
Melledy committed
622
623
624
625
626
627
628
				} else {
					break;
				}
			}
		}
		
		// Weapon
629
		GameItem weapon = this.getWeapon();
Melledy's avatar
Melledy committed
630
631
		if (weapon != null) {
			// Add stats
632
			WeaponCurveData curveData = GameData.getWeaponCurveDataMap().get(weapon.getLevel());
Melledy's avatar
Melledy committed
633
634
635
636
637
638
			if (curveData != null) {
				for (WeaponProperty weaponProperty : weapon.getItemData().getWeaponProperties()) {
					this.addFightProperty(weaponProperty.getFightProp(), weaponProperty.getInitValue() * curveData.getMultByProp(weaponProperty.getType()));
				}
			}
			// Weapon promotion stats
639
			WeaponPromoteData wepPromoteData = GameData.getWeaponPromoteData(weapon.getItemData().getWeaponPromoteId(), weapon.getPromoteLevel());
Melledy's avatar
Melledy committed
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
			if (wepPromoteData != null) {
				for (FightPropData prop : wepPromoteData.getAddProps()) {
					if (prop.getValue() == 0f || prop.getProp() == null) {
						continue;
					}
					this.addFightProperty(prop.getProp(), prop.getValue());
				}
			}
			// Add weapon skill from affixes
			if (weapon.getAffixes() != null && weapon.getAffixes().size() > 0) {
				// Weapons usually dont have more than one affix but just in case...
				for (int af : weapon.getAffixes()) {
					if (af == 0) {
						continue;
					}
					// Calculate affix id
					int affixId = (af * 10) + weapon.getRefinement();
657
					EquipAffixData affix = GameData.getEquipAffixDataMap().get(affixId);
Melledy's avatar
Melledy committed
658
659
660
661
662
663
664
665
666
667
					if (affix == null) {
						continue;
					}
					
					// Add properties from this affix to our avatar
					for (FightPropData prop : affix.getAddProps()) {
						this.addFightProperty(prop.getProp(), prop.getValue());
					}
					
					// Add any skill strings from this affix
668
					this.addToExtraAbilityEmbryos(affix.getOpenConfig(), true);
Melledy's avatar
Melledy committed
669
670
671
672
				}
			}
		}
		
673
		// Add proud skills and unlock them if needed
674
		AvatarSkillDepotData skillDepot = GameData.getAvatarSkillDepotDataMap().get(this.getSkillDepotId());
675
676
677
678
679
680
681
		this.getProudSkillList().clear();
		for (InherentProudSkillOpens openData : skillDepot.getInherentProudSkillOpens()) {
			if (openData.getProudSkillGroupId() == 0) {
				continue;
			}
			if (openData.getNeedAvatarPromoteLevel() <= this.getPromoteLevel()) {
				int proudSkillId = (openData.getProudSkillGroupId() * 100) + 1;
682
				if (GameData.getProudSkillDataMap().containsKey(proudSkillId)) {
683
684
685
686
687
					this.getProudSkillList().add(proudSkillId);
				}
			}
		}

Melledy's avatar
Melledy committed
688
689
		// Proud skills
		for (int proudSkillId : this.getProudSkillList()) {
690
			ProudSkillData proudSkillData = GameData.getProudSkillDataMap().get(proudSkillId);
Melledy's avatar
Melledy committed
691
692
693
694
695
696
697
698
699
700
			if (proudSkillData == null) {
				continue;
			} 
			
			// Add properties from this proud skill to our avatar
			for (FightPropData prop : proudSkillData.getAddProps()) {
				this.addFightProperty(prop.getProp(), prop.getValue());
			}
			
			// Add any skill strings from this proud skill
701
			this.addToExtraAbilityEmbryos(proudSkillData.getOpenConfig(), true);
Melledy's avatar
Melledy committed
702
703
704
705
706
		}
		
		// Constellations
		if (this.getTalentIdList().size() > 0) {
			for (int talentId : this.getTalentIdList()) {
707
				AvatarTalentData avatarTalentData = GameData.getAvatarTalentDataMap().get(talentId);
Melledy's avatar
Melledy committed
708
709
710
711
712
				if (avatarTalentData == null) {
					return;
				}
				
				// Add any skill strings from this constellation
713
				this.addToExtraAbilityEmbryos(avatarTalentData.getOpenConfig(), false);
Melledy's avatar
Melledy committed
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
			}
		}

		// Set % stats
		this.setFightProperty(
			FightProperty.FIGHT_PROP_MAX_HP, 
			(getFightProperty(FightProperty.FIGHT_PROP_BASE_HP) * (1f + getFightProperty(FightProperty.FIGHT_PROP_HP_PERCENT))) + getFightProperty(FightProperty.FIGHT_PROP_HP)
		);
		this.setFightProperty(
			FightProperty.FIGHT_PROP_CUR_ATTACK, 
			(getFightProperty(FightProperty.FIGHT_PROP_BASE_ATTACK) * (1f + getFightProperty(FightProperty.FIGHT_PROP_ATTACK_PERCENT))) + getFightProperty(FightProperty.FIGHT_PROP_ATTACK)
		);
		this.setFightProperty(
			FightProperty.FIGHT_PROP_CUR_DEFENSE, 
			(getFightProperty(FightProperty.FIGHT_PROP_BASE_DEFENSE) * (1f + getFightProperty(FightProperty.FIGHT_PROP_DEFENSE_PERCENT))) + getFightProperty(FightProperty.FIGHT_PROP_DEFENSE)
		);
		
		// Set current hp
		this.setFightProperty(FightProperty.FIGHT_PROP_CUR_HP, this.getFightProperty(FightProperty.FIGHT_PROP_MAX_HP) * hpPercent);
		
		// Packet
		if (getPlayer() != null && getPlayer().hasSentAvatarDataNotify()) {
736
			// Update stats for client
Melledy's avatar
Melledy committed
737
			getPlayer().sendPacket(new PacketAvatarFightPropNotify(this));
738
739
740
741
742
			// Update client abilities
			EntityAvatar entity = this.getAsEntity();
			if (entity != null && (!this.getExtraAbilityEmbryos().equals(prevExtraAbilityEmbryos) || forceSendAbilityChange)) {
				getPlayer().sendPacket(new PacketAbilityChangeNotify(entity));
			}
Melledy's avatar
Melledy committed
743
744
745
		}
	}
	
746
	public void addToExtraAbilityEmbryos(String openConfig, boolean forceAdd) {
Melledy's avatar
Melledy committed
747
748
749
750
		if (openConfig == null || openConfig.length() == 0) {
			return;
		}
		
751
		OpenConfigEntry entry = GameData.getOpenConfigEntries().get(openConfig);
Melledy's avatar
Melledy committed
752
753
754
		if (entry == null) {
			if (forceAdd) {
				// Add config string to ability skill list anyways
755
				this.getExtraAbilityEmbryos().add(openConfig);
Melledy's avatar
Melledy committed
756
757
758
759
760
761
			}
			return;
		}
		
		if (entry.getAddAbilities() != null) {
			for (String ability : entry.getAddAbilities()) {
762
				this.getExtraAbilityEmbryos().add(ability);
Melledy's avatar
Melledy committed
763
764
765
766
			}
		}
	}
	
767
	public void recalcConstellations() {
Melledy's avatar
Melledy committed
768
769
		// Clear first
		this.getProudSkillBonusMap().clear();
770
		this.getSkillExtraChargeMap().clear();
Melledy's avatar
Melledy committed
771
772
773
774
775
776
777
778
		
		// Sanity checks
		if (getData() == null || getData().getSkillDepot() == null) {
			return;
		}
				
		if (this.getTalentIdList().size() > 0) {
			for (int talentId : this.getTalentIdList()) {
779
				AvatarTalentData avatarTalentData = GameData.getAvatarTalentDataMap().get(talentId);
Melledy's avatar
Melledy committed
780
781
782
783
784
785
				
				if (avatarTalentData == null || avatarTalentData.getOpenConfig() == null || avatarTalentData.getOpenConfig().length() == 0) {
					continue;
				}
				
				// Get open config to find which skill should be boosted
786
				OpenConfigEntry entry = GameData.getOpenConfigEntries().get(avatarTalentData.getOpenConfig());
Melledy's avatar
Melledy committed
787
788
789
790
				if (entry == null) {
					continue;
				}
				
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
				// Check if we can add charges to a skill
				if (entry.getSkillPointModifiers() != null) {
					for (SkillPointModifier mod : entry.getSkillPointModifiers()) {
						AvatarSkillData skillData = GameData.getAvatarSkillDataMap().get(mod.getSkillId());
						
						if (skillData == null) continue;
						
						int charges = skillData.getMaxChargeNum() + mod.getDelta();
						
						this.getSkillExtraChargeMap().put(mod.getSkillId(), charges);
					}
					continue;
				}
				
				// Check if a skill can be boosted by +3 levels
Melledy's avatar
Melledy committed
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
				int skillId = 0;
				
				if (entry.getExtraTalentIndex() == 2 && this.getData().getSkillDepot().getSkills().size() >= 2) {
					// E skill
					skillId = this.getData().getSkillDepot().getSkills().get(1);
				} else if (entry.getExtraTalentIndex() == 9) {
					// Ult skill
					skillId = this.getData().getSkillDepot().getEnergySkill();
				}
				
				// Sanity check
				if (skillId == 0) {
					continue;
				}
				
				// Get proud skill group id
822
				AvatarSkillData skillData = GameData.getAvatarSkillDataMap().get(skillId);
Melledy's avatar
Melledy committed
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
				
				if (skillData == null) {
					continue;
				}
				
				// Add to bonus list
				this.getProudSkillBonusMap().put(skillData.getProudSkillGroupId(), 3);
			}
		}
	}
	
	public EntityAvatar getAsEntity() {
		for (EntityAvatar entity : getPlayer().getTeamManager().getActiveTeam()) {
			if (entity.getAvatar() == this) {
				return entity;
			}
		}
		return null;
	}
	
	public int getEntityId() {
		EntityAvatar entity = getAsEntity();
		return entity != null ? entity.getId() : 0;
	}

	public void save() {
		DatabaseHelper.saveAvatar(this);
	}
	
	public AvatarInfo toProto() {
Yazawazi's avatar
Yazawazi committed
853
		int fetterLevel = this.getFetterLevel();
Yazawazi's avatar
Yazawazi committed
854
		AvatarFetterInfo.Builder avatarFetter = AvatarFetterInfo.newBuilder()
Yazawazi's avatar
Yazawazi committed
855
856
857
858
859
860
				.setExpLevel(fetterLevel);
		
		if (fetterLevel != 10) {
			avatarFetter.setExpNumber(this.getFetterExp());
		}
				
Yazawazi's avatar
Yazawazi committed
861
		
Yazawazi's avatar
Yazawazi committed
862
863
864
865
866
867
868
869
		if (this.getFetterList() != null) {
			for (int i = 0; i < this.getFetterList().size(); i++) {
				avatarFetter.addFetterList(
					FetterData.newBuilder()
						.setFetterId(this.getFetterList().get(i))
						.setFetterState(FetterState.FINISH.getValue())
				);
			}
Yazawazi's avatar
Yazawazi committed
870
871
		}

Yazawazi's avatar
Yazawazi committed
872
873
874
		int cardId = this.getNameCardId();

		if (this.getPlayer().getNameCardList().contains(cardId)) {
Yazawazi's avatar
Yazawazi committed
875
			avatarFetter.addRewardedFetterLevelList(10);
Yazawazi's avatar
Yazawazi committed
876
877
		}

Melledy's avatar
Melledy committed
878
879
880
881
882
883
884
885
886
887
		AvatarInfo.Builder avatarInfo = AvatarInfo.newBuilder()
				.setAvatarId(this.getAvatarId())
				.setGuid(this.getGuid())
				.setLifeState(1)
				.addAllTalentIdList(this.getTalentIdList())
				.putAllFightPropMap(this.getFightProperties())
				.setSkillDepotId(this.getSkillDepotId())
				.setCoreProudSkillLevel(this.getCoreProudSkillLevel())
				.putAllSkillLevelMap(this.getSkillLevelMap())
				.addAllInherentProudSkillList(this.getProudSkillList())
888
				.putAllProudSkillExtraLevelMap(getProudSkillBonusMap())
Melledy's avatar
Melledy committed
889
890
				.setAvatarType(1)
				.setBornTime(this.getBornTime())
Yazawazi's avatar
Yazawazi committed
891
				.setFetterInfo(avatarFetter)
Melledy's avatar
Melledy committed
892
893
894
				.setWearingFlycloakId(this.getFlyCloak())
				.setCostumeId(this.getCostume());
		
895
896
897
898
		for (Entry<Integer, Integer> entry : this.getSkillExtraChargeMap().entrySet()) {
			avatarInfo.putSkillMap(entry.getKey(), AvatarSkillInfo.newBuilder().setMaxChargeCount(entry.getValue()).build());
		}
		
899
		for (GameItem item : this.getEquips().values()) {
Melledy's avatar
Melledy committed
900
901
902
903
904
905
906
907
908
909
910
			avatarInfo.addEquipGuidList(item.getGuid());
		}
		
		avatarInfo.putPropMap(PlayerProperty.PROP_LEVEL.getId(), ProtoHelper.newPropValue(PlayerProperty.PROP_LEVEL, this.getLevel()));
		avatarInfo.putPropMap(PlayerProperty.PROP_EXP.getId(), ProtoHelper.newPropValue(PlayerProperty.PROP_EXP, this.getExp()));
		avatarInfo.putPropMap(PlayerProperty.PROP_BREAK_LEVEL.getId(), ProtoHelper.newPropValue(PlayerProperty.PROP_BREAK_LEVEL, this.getPromoteLevel()));
		avatarInfo.putPropMap(PlayerProperty.PROP_SATIATION_VAL.getId(), ProtoHelper.newPropValue(PlayerProperty.PROP_SATIATION_VAL, 0));
		avatarInfo.putPropMap(PlayerProperty.PROP_SATIATION_PENALTY_TIME.getId(), ProtoHelper.newPropValue(PlayerProperty.PROP_SATIATION_PENALTY_TIME, 0));
		
		return avatarInfo.build();
	}
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950

	// used only in character showcase
	public ShowAvatarInfo toShowAvatarInfoProto() {
		AvatarFetterInfo.Builder avatarFetter = AvatarFetterInfo.newBuilder()
				.setExpLevel(this.getFetterLevel());

		ShowAvatarInfo.Builder showAvatarInfo = ShowAvatarInfoOuterClass.ShowAvatarInfo.newBuilder()
				.setAvatarId(avatarId)
				.addAllTalentIdList(this.getTalentIdList())
				.putAllFightPropMap(this.getFightProperties())
				.setSkillDepotId(this.getSkillDepotId())
				.setCoreProudSkillLevel(this.getCoreProudSkillLevel())
				.addAllInherentProudSkillList(this.getProudSkillList())
				.putAllSkillLevelMap(this.getSkillLevelMap())
				.putAllProudSkillExtraLevelMap(this.getProudSkillBonusMap())
				.setFetterInfo(avatarFetter)
				.setCostumeId(this.getCostume());

		showAvatarInfo.putPropMap(PlayerProperty.PROP_LEVEL.getId(), ProtoHelper.newPropValue(PlayerProperty.PROP_LEVEL, this.getLevel()));
		showAvatarInfo.putPropMap(PlayerProperty.PROP_EXP.getId(), ProtoHelper.newPropValue(PlayerProperty.PROP_EXP, this.getExp()));
		showAvatarInfo.putPropMap(PlayerProperty.PROP_BREAK_LEVEL.getId(), ProtoHelper.newPropValue(PlayerProperty.PROP_BREAK_LEVEL, this.getPromoteLevel()));
		showAvatarInfo.putPropMap(PlayerProperty.PROP_SATIATION_VAL.getId(), ProtoHelper.newPropValue(PlayerProperty.PROP_SATIATION_VAL, this.getSatiation()));
		showAvatarInfo.putPropMap(PlayerProperty.PROP_SATIATION_PENALTY_TIME.getId(), ProtoHelper.newPropValue(PlayerProperty.PROP_SATIATION_VAL, this.getSatiationPenalty()));
		int maxStamina = this.getPlayer().getProperty(PlayerProperty.PROP_MAX_STAMINA);
		showAvatarInfo.putPropMap(PlayerProperty.PROP_MAX_STAMINA.getId(), ProtoHelper.newPropValue(PlayerProperty.PROP_MAX_STAMINA, maxStamina));

		for (GameItem item : this.getEquips().values()) {
			if (item.getItemType() == ItemType.ITEM_RELIQUARY) {
				showAvatarInfo.addEquipList(ShowEquip.newBuilder()
						.setItemId(item.getItemId())
						.setReliquary(item.toReliquaryProto()));
			} else if (item.getItemType() == ItemType.ITEM_WEAPON) {
				showAvatarInfo.addEquipList(ShowEquip.newBuilder()
						.setItemId(item.getItemId())
						.setWeapon(item.toWeaponProto()));
			}
		}

		return showAvatarInfo.build();
	}
Melledy's avatar
Melledy committed
951
952
953
954
955
956
957
958
959
960
961
	
	@PostLoad
	private void onLoad() {
		
	}
	
	@PrePersist
	private void prePersist() {
		this.currentHp = this.getFightProperty(FightProperty.FIGHT_PROP_CUR_HP);
	}
}