GenshinAvatar.java 22.9 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
8
9
10
11
12
13
14
15
16
17
import java.util.Map;
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;
18
import emu.grasscutter.Grasscutter;
Melledy's avatar
Melledy committed
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
import emu.grasscutter.data.GenshinData;
import emu.grasscutter.data.common.FightPropData;
import emu.grasscutter.data.custom.OpenConfigEntry;
import emu.grasscutter.data.def.AvatarData;
import emu.grasscutter.data.def.AvatarPromoteData;
import emu.grasscutter.data.def.AvatarSkillData;
import emu.grasscutter.data.def.AvatarSkillDepotData;
import emu.grasscutter.data.def.AvatarSkillDepotData.InherentProudSkillOpens;
import emu.grasscutter.data.def.AvatarTalentData;
import emu.grasscutter.data.def.EquipAffixData;
import emu.grasscutter.data.def.ReliquaryAffixData;
import emu.grasscutter.data.def.ReliquaryLevelData;
import emu.grasscutter.data.def.ReliquaryMainPropData;
import emu.grasscutter.data.def.ReliquarySetData;
import emu.grasscutter.data.def.WeaponCurveData;
import emu.grasscutter.data.def.WeaponPromoteData;
import emu.grasscutter.data.def.ItemData.WeaponProperty;
import emu.grasscutter.data.def.ProudSkillData;
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.game.entity.EntityAvatar;
import emu.grasscutter.game.inventory.EquipType;
import emu.grasscutter.game.inventory.GenshinItem;
import emu.grasscutter.game.props.ElementType;
import emu.grasscutter.game.props.EntityIdType;
Yazawazi's avatar
Yazawazi committed
44
import emu.grasscutter.game.props.FetterState;
Melledy's avatar
Melledy committed
45
46
47
import emu.grasscutter.game.props.FightProperty;
import emu.grasscutter.game.props.PlayerProperty;
import emu.grasscutter.net.proto.AvatarFetterInfoOuterClass.AvatarFetterInfo;
Yazawazi's avatar
Yazawazi committed
48
import emu.grasscutter.net.proto.FetterDataOuterClass.FetterData;
Melledy's avatar
Melledy committed
49
import emu.grasscutter.net.proto.AvatarInfoOuterClass.AvatarInfo;
50
import emu.grasscutter.server.packet.send.PacketAbilityChangeNotify;
Melledy's avatar
Melledy committed
51
52
53
54
55
56
57
58
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;

59
@Entity(value = "avatars", useDiscriminator = false)
Melledy's avatar
Melledy committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
public class GenshinAvatar {
	@Id private ObjectId id;
	@Indexed private int ownerId;	// Id of player that this avatar belongs to
	
	@Transient private GenshinPlayer owner;
	@Transient private AvatarData data;
	@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;
	
	@Transient private final Int2ObjectMap<GenshinItem> equips;
	@Transient private final Int2FloatOpenHashMap fightProp;
78
	@Transient private Set<String> extraAbilityEmbryos;
Melledy's avatar
Melledy committed
79
	
Yazawazi's avatar
typo    
Yazawazi committed
80
	private List<Integer> fetters;
Yazawazi's avatar
Yazawazi committed
81

Melledy's avatar
Melledy committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
	private Map<Integer, Integer> skillLevelMap; // Talent levels
	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;
	
	public GenshinAvatar() {
		// Morhpia only!
		this.equips = new Int2ObjectOpenHashMap<>();
		this.fightProp = new Int2FloatOpenHashMap();
97
		this.extraAbilityEmbryos = new HashSet<>();
Yazawazi's avatar
Yazawazi committed
98
99
		this.proudSkillBonusMap = new HashMap<>(); 
		this.fetters = new ArrayList<>(); // TODO Move to genshin avatar
Melledy's avatar
Melledy committed
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
	}
	
	// On creation
	public GenshinAvatar(int avatarId) {
		this(GenshinData.getAvatarDataMap().get(avatarId));
	}
	
	public GenshinAvatar(AvatarData data) {
		this();
		this.avatarId = data.getId();
		this.data = data;
		this.bornTime = (int) (System.currentTimeMillis() / 1000);
		this.flyCloak = 140001;
		
		this.skillLevelMap = new HashMap<>();
		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
		this.setSkillDepot(getAvatarData().getSkillDepot());
		
		// Set stats
		this.recalcStats();
		this.currentHp = getFightProperty(FightProperty.FIGHT_PROP_MAX_HP);
		setFightProperty(FightProperty.FIGHT_PROP_CUR_HP, this.currentHp);
		
		// Load handler
		this.onLoad();
	}
	
	public GenshinPlayer getPlayer() {
		return this.owner;
	}

	public ObjectId getObjectId() {
		return id;
	}

	public AvatarData getAvatarData() {
		return data;
	}

	protected void setAvatarData(AvatarData data) {
		this.data = data;
	}

	public int getOwnerId() {
		return ownerId;
	}

	public void setOwner(GenshinPlayer player) {
		this.owner = player;
160
161
		this.ownerId = player.getUid();
		this.guid = player.getNextGenshinGuid();
Melledy's avatar
Melledy committed
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
	}
	
	public int getSatiation() {
		return satiation;
	}

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

	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;
	}

	public Int2ObjectMap<GenshinItem> getEquips() {
		return equips;
	}
	
	public GenshinItem getEquipBySlot(EquipType slot) {
		return this.getEquips().get(slot.getValue());
	}
	
	private GenshinItem getEquipBySlot(int slotId) {
		return this.getEquips().get(slotId);
	}
	
	public GenshinItem getWeapon() {
		return this.getEquipBySlot(EquipType.EQUIP_WEAPON);
	}

	public int getSkillDepotId() {
		return skillDepotId;
	}

	public void setSkillDepot(AvatarSkillDepotData skillDepot) {
		// Set id
		this.skillDepotId = skillDepot.getId();
		// 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;
				if (GenshinData.getProudSkillDataMap().containsKey(proudSkillId)) {
					this.getProudSkillList().add(proudSkillId);
				}
			}
		}
	}

	public Map<Integer, Integer> getSkillLevelMap() {
		return skillLevelMap;
	}

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

272
273
	public Set<String> getExtraAbilityEmbryos() {
		return extraAbilityEmbryos;
Melledy's avatar
Melledy committed
274
275
	}

Yazawazi's avatar
Yazawazi committed
276
	public void setFetterList(List<Integer> fetterList) {
277
		this.fetters = fetterList;
Yazawazi's avatar
Yazawazi committed
278
279
280
	}

	public List<Integer> getFetterList() {
281
		return fetters;
Yazawazi's avatar
Yazawazi committed
282
283
	}

Melledy's avatar
Melledy committed
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
	public float getCurrentHp() {
		return currentHp;
	}

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

	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;
	}
	
	public boolean equipItem(GenshinItem item, boolean shouldRecalc) {
		EquipType itemEquipType = item.getItemData().getEquipType();
		if (itemEquipType == EquipType.EQUIP_NONE) {
			return false;
		}
		
		if (getEquips().containsKey(itemEquipType.getValue())) {
			unequipItem(itemEquipType);
		}
		
		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();
		
367
368
369
370
		if (this.getPlayer().hasSentAvatarDataNotify()) {
			this.getPlayer().sendPacket(new PacketAvatarEquipChangeNotify(this, item));
		}
		
Melledy's avatar
Melledy committed
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
		if (shouldRecalc) {
			this.recalcStats();
		}
		
		return true;
	}
	
	public boolean unequipItem(EquipType slot) {
		GenshinItem item = getEquips().remove(slot.getValue());
		
		if (item != null) {
			item.setEquipCharacter(0);
			item.save();
			return true;
		}
		
		return false;
	}
	
	public void recalcStats() {
391
392
393
394
		recalcStats(false);
	}
	
	public void recalcStats(boolean forceSendAbilityChange) {
Melledy's avatar
Melledy committed
395
396
397
398
		// Setup
		AvatarData data = this.getAvatarData();
		AvatarPromoteData promoteData = GenshinData.getAvatarPromoteData(data.getAvatarPromoteId(), this.getPromoteLevel());
		Int2IntOpenHashMap setMap = new Int2IntOpenHashMap();
399
400
401
402
		
		// Extra ability embryos
		Set<String> prevExtraAbilityEmbryos = this.getExtraAbilityEmbryos();
		this.extraAbilityEmbryos = new HashSet<>();
Yazawazi's avatar
Yazawazi committed
403
404
405

		// Fetters
		this.setFetterList(data.getFetters());
Melledy's avatar
Melledy committed
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
		
		// 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);
		
		// 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
		if (data.getSkillDepot() != null && data.getSkillDepot().getEnergySkillData() != null) {
			ElementType element = data.getSkillDepot().getElementType();
			this.setFightProperty(element.getEnergyProperty(), data.getSkillDepot().getEnergySkillData().getCostElemVal());
			this.setFightProperty((element.getEnergyProperty().getId() % 70) + 1000, data.getSkillDepot().getEnergySkillData().getCostElemVal());
		}
		
		// Artifacts
		for (int slotId = 1; slotId <= 5; slotId++) {
			// Get artifact
			GenshinItem equip = this.getEquipBySlot(slotId);
			if (equip == null) {
				continue;
			}
			// Artifact main stat
			ReliquaryMainPropData mainPropData = GenshinData.getReliquaryMainPropDataMap().get(equip.getMainPropId());
			if (mainPropData != null) {
				ReliquaryLevelData levelData = GenshinData.getRelicLevelData(equip.getItemData().getRankLevel(), equip.getLevel());
				if (levelData != null) {
					this.addFightProperty(mainPropData.getFightProp(), levelData.getPropValue(mainPropData.getFightProp()));
				}	
			}
			// Artifact sub stats
			for (int appendPropId : equip.getAppendPropIdList()) {
				ReliquaryAffixData affixData = GenshinData.getReliquaryAffixDataMap().get(appendPropId);
				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()) {
			ReliquarySetData setData = GenshinData.getReliquarySetDataMap().get(e.getIntKey());
			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;
					
					EquipAffixData affix = GenshinData.getEquipAffixDataMap().get(affixId);
					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
488
					this.addToExtraAbilityEmbryos(affix.getOpenConfig(), true);
Melledy's avatar
Melledy committed
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
528
529
530
531
532
533
534
				} else {
					break;
				}
			}
		}
		
		// Weapon
		GenshinItem weapon = this.getWeapon();
		if (weapon != null) {
			// Add stats
			WeaponCurveData curveData = GenshinData.getWeaponCurveDataMap().get(weapon.getLevel());
			if (curveData != null) {
				for (WeaponProperty weaponProperty : weapon.getItemData().getWeaponProperties()) {
					this.addFightProperty(weaponProperty.getFightProp(), weaponProperty.getInitValue() * curveData.getMultByProp(weaponProperty.getType()));
				}
			}
			// Weapon promotion stats
			WeaponPromoteData wepPromoteData = GenshinData.getWeaponPromoteData(weapon.getItemData().getWeaponPromoteId(), weapon.getPromoteLevel());
			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();
					EquipAffixData affix = GenshinData.getEquipAffixDataMap().get(affixId);
					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
535
					this.addToExtraAbilityEmbryos(affix.getOpenConfig(), true);
Melledy's avatar
Melledy committed
536
537
538
539
				}
			}
		}
		
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
		// Add proud skills and unlock them if needed
		AvatarSkillDepotData skillDepot = GenshinData.getAvatarSkillDepotDataMap().get(this.getSkillDepotId());
		this.getProudSkillList().clear();
		for (InherentProudSkillOpens openData : skillDepot.getInherentProudSkillOpens()) {
			if (openData.getProudSkillGroupId() == 0) {
				continue;
			}
			if (openData.getNeedAvatarPromoteLevel() <= this.getPromoteLevel()) {
				int proudSkillId = (openData.getProudSkillGroupId() * 100) + 1;
				if (GenshinData.getProudSkillDataMap().containsKey(proudSkillId)) {
					this.getProudSkillList().add(proudSkillId);
				}
			}
		}

Melledy's avatar
Melledy committed
555
556
557
558
559
560
561
562
563
564
565
566
567
		// Proud skills
		for (int proudSkillId : this.getProudSkillList()) {
			ProudSkillData proudSkillData = GenshinData.getProudSkillDataMap().get(proudSkillId);
			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
568
			this.addToExtraAbilityEmbryos(proudSkillData.getOpenConfig(), true);
Melledy's avatar
Melledy committed
569
570
571
572
573
574
575
576
577
578
579
		}
		
		// Constellations
		if (this.getTalentIdList().size() > 0) {
			for (int talentId : this.getTalentIdList()) {
				AvatarTalentData avatarTalentData = GenshinData.getAvatarTalentDataMap().get(talentId);
				if (avatarTalentData == null) {
					return;
				}
				
				// Add any skill strings from this constellation
580
				this.addToExtraAbilityEmbryos(avatarTalentData.getOpenConfig(), false);
Melledy's avatar
Melledy committed
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
			}
		}

		// 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()) {
603
			// Update stats for client
Melledy's avatar
Melledy committed
604
			getPlayer().sendPacket(new PacketAvatarFightPropNotify(this));
605
606
607
608
609
			// 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
610
611
612
		}
	}
	
613
	public void addToExtraAbilityEmbryos(String openConfig, boolean forceAdd) {
Melledy's avatar
Melledy committed
614
615
616
617
618
619
620
621
		if (openConfig == null || openConfig.length() == 0) {
			return;
		}
		
		OpenConfigEntry entry = GenshinData.getOpenConfigEntries().get(openConfig);
		if (entry == null) {
			if (forceAdd) {
				// Add config string to ability skill list anyways
622
				this.getExtraAbilityEmbryos().add(openConfig);
Melledy's avatar
Melledy committed
623
624
625
626
627
628
			}
			return;
		}
		
		if (entry.getAddAbilities() != null) {
			for (String ability : entry.getAddAbilities()) {
629
				this.getExtraAbilityEmbryos().add(ability);
Melledy's avatar
Melledy committed
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
			}
		}
	}
	
	public void recalcProudSkillBonusMap() {
		// Clear first
		this.getProudSkillBonusMap().clear();
		
		// Sanity checks
		if (getData() == null || getData().getSkillDepot() == null) {
			return;
		}
				
		if (this.getTalentIdList().size() > 0) {
			for (int talentId : this.getTalentIdList()) {
				AvatarTalentData avatarTalentData = GenshinData.getAvatarTalentDataMap().get(talentId);
				
				if (avatarTalentData == null || avatarTalentData.getOpenConfig() == null || avatarTalentData.getOpenConfig().length() == 0) {
					continue;
				}
				
				// Get open config to find which skill should be boosted
				OpenConfigEntry entry = GenshinData.getOpenConfigEntries().get(avatarTalentData.getOpenConfig());
				if (entry == null) {
					continue;
				}
				
				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
				AvatarSkillData skillData = GenshinData.getAvatarSkillDataMap().get(skillId);
				
				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
704
705
706
707
		AvatarFetterInfo.Builder avatarFetter = AvatarFetterInfo.newBuilder()
				.setExpLevel(10)
				.setExpNumber(6325); // Highest Level
		
Yazawazi's avatar
Yazawazi committed
708
709
710
711
712
713
714
715
		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
716
717
		}

Melledy's avatar
Melledy committed
718
719
720
721
722
723
724
725
726
727
728
729
730
		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())
				.putAllProudSkillExtraLevel(getProudSkillBonusMap())
				.setAvatarType(1)
				.setBornTime(this.getBornTime())
Yazawazi's avatar
Yazawazi committed
731
				.setFetterInfo(avatarFetter)
Melledy's avatar
Melledy committed
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
				.setWearingFlycloakId(this.getFlyCloak())
				.setCostumeId(this.getCostume());
		
		for (GenshinItem item : this.getEquips().values()) {
			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();
	}
	
	@PostLoad
	private void onLoad() {
		
	}
	
	@PrePersist
	private void prePersist() {
		this.currentHp = this.getFightProperty(FightProperty.FIGHT_PROP_CUR_HP);
	}
}