EntityMonster.java 10 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
package emu.grasscutter.game.entity;

3
import emu.grasscutter.data.GameData;
Melledy's avatar
Melledy committed
4
import emu.grasscutter.data.common.PropGrowCurve;
Melledy's avatar
Melledy committed
5
6
import emu.grasscutter.data.excels.MonsterCurveData;
import emu.grasscutter.data.excels.MonsterData;
7
import emu.grasscutter.game.player.Player;
Melledy's avatar
Melledy committed
8
9
10
import emu.grasscutter.game.props.EntityIdType;
import emu.grasscutter.game.props.FightProperty;
import emu.grasscutter.game.props.PlayerProperty;
11
import emu.grasscutter.game.props.WatcherTriggerType;
Melledy's avatar
Melledy committed
12
import emu.grasscutter.game.world.Scene;
Melledy's avatar
Melledy committed
13
14
15
16
17
18
19
20
21
22
23
24
25
import emu.grasscutter.net.proto.AbilitySyncStateInfoOuterClass.AbilitySyncStateInfo;
import emu.grasscutter.net.proto.AnimatorParameterValueInfoPairOuterClass.AnimatorParameterValueInfoPair;
import emu.grasscutter.net.proto.EntityAuthorityInfoOuterClass.EntityAuthorityInfo;
import emu.grasscutter.net.proto.EntityClientDataOuterClass.EntityClientData;
import emu.grasscutter.net.proto.EntityRendererChangedInfoOuterClass.EntityRendererChangedInfo;
import emu.grasscutter.net.proto.FightPropPairOuterClass.FightPropPair;
import emu.grasscutter.net.proto.MonsterBornTypeOuterClass.MonsterBornType;
import emu.grasscutter.net.proto.PropPairOuterClass.PropPair;
import emu.grasscutter.net.proto.ProtEntityTypeOuterClass.ProtEntityType;
import emu.grasscutter.net.proto.SceneEntityAiInfoOuterClass.SceneEntityAiInfo;
import emu.grasscutter.net.proto.SceneEntityInfoOuterClass.SceneEntityInfo;
import emu.grasscutter.net.proto.SceneMonsterInfoOuterClass.SceneMonsterInfo;
import emu.grasscutter.net.proto.SceneWeaponInfoOuterClass.SceneWeaponInfo;
26
import emu.grasscutter.scripts.constants.EventType;
Akka's avatar
Akka committed
27
import emu.grasscutter.scripts.data.ScriptArgs;
Melledy's avatar
Melledy committed
28
29
30
31
32
import emu.grasscutter.utils.Position;
import emu.grasscutter.utils.ProtoHelper;
import it.unimi.dsi.fastutil.ints.Int2FloatMap;
import it.unimi.dsi.fastutil.ints.Int2FloatOpenHashMap;

33
public class EntityMonster extends GameEntity {
Melledy's avatar
Melledy committed
34
35
36
37
38
39
40
41
	private final MonsterData monsterData;
	private final Int2FloatOpenHashMap fightProp;
	
	private final Position pos;
	private final Position rot;
	private final Position bornPos;
	private final int level;
	private int weaponEntityId;
Melledy's avatar
Melledy committed
42
43
	private int poseId;
	
44
	public EntityMonster(Scene scene, MonsterData monsterData, Position pos, int level) {
45
46
		super(scene);
		this.id = getWorld().getNextEntityId(EntityIdType.MONSTER);
Melledy's avatar
Melledy committed
47
48
49
50
51
52
53
54
55
		this.monsterData = monsterData;
		this.fightProp = new Int2FloatOpenHashMap();
		this.pos = new Position(pos);
		this.rot = new Position();
		this.bornPos = getPosition().clone();
		this.level = level;
		
		// Monster weapon
		if (getMonsterWeaponId() > 0) {
56
			this.weaponEntityId = getWorld().getNextEntityId(EntityIdType.WEAPON);
Melledy's avatar
Melledy committed
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
		}
		
		this.recalcStats();
	}
	
	@Override
	public int getId() {
		return this.id;
	}

	public MonsterData getMonsterData() {
		return monsterData;
	}
	
	public int getMonsterWeaponId() {
		return getMonsterData().getWeaponId();
	}
	
	private int getMonsterId() {
		return this.getMonsterData().getId();
	}

	public int getLevel() {
		return level;
	}

	@Override
	public Position getPosition() {
		return this.pos;
	}

	@Override
	public Position getRotation() {
		return this.rot;
	}
	
	public Position getBornPos() {
		return bornPos;
	}

	@Override
	public Int2FloatOpenHashMap getFightProperties() {
		return fightProp;
	}
	
	@Override
	public boolean isAlive() {
		return this.getFightProperty(FightProperty.FIGHT_PROP_CUR_HP) > 0f;
	}
Melledy's avatar
Melledy committed
106
107
108
109
110
111
112
113

	public int getPoseId() {
		return poseId;
	}

	public void setPoseId(int poseId) {
		this.poseId = poseId;
	}
114
115
116
117
118
119
	
	@Override
	public void onCreate() {
		// Lua event
		getScene().getScriptManager().callEvent(EventType.EVENT_ANY_MONSTER_LIVE, new ScriptArgs(this.getConfigId()));
	}
120

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
	@Override
	public void damage(float amount, int killerId) {
		// Get HP before damage.
		float hpBeforeDamage = this.getFightProperty(FightProperty.FIGHT_PROP_CUR_HP);

		// Apply damage.
		super.damage(amount, killerId);

		// Get HP after damage.
		float hpAfterDamage = this.getFightProperty(FightProperty.FIGHT_PROP_CUR_HP);

		// Invoke energy drop logic.
		for (Player player : this.getScene().getPlayers()) {
			player.getEnergyManager().handleMonsterEnergyDrop(this, hpBeforeDamage, hpAfterDamage);
		}
	}

Melledy's avatar
Melledy committed
138
139
	@Override
	public void onDeath(int killerId) {
Melledy's avatar
Melledy committed
140
141
142
		if (this.getSpawnEntry() != null) {
			this.getScene().getDeadSpawnedEntities().add(getSpawnEntry());
		}
143
		// first set the challenge data
Akka's avatar
Akka committed
144
145
		if (getScene().getChallenge() != null) {
			getScene().getChallenge().onMonsterDeath(this);
146
		}
147
		if (getScene().getScriptManager().isInit() && this.getGroupId() > 0) {
148
149
150
			if(getScene().getScriptManager().getScriptMonsterSpawnService() != null){
				getScene().getScriptManager().getScriptMonsterSpawnService().onMonsterDead(this);
			}
151
152
			// prevent spawn monster after success
			if(getScene().getChallenge() != null && getScene().getChallenge().inProgress()){
Akka's avatar
Akka committed
153
154
155
				getScene().getScriptManager().callEvent(EventType.EVENT_ANY_MONSTER_DIE, new ScriptArgs().setParam1(this.getConfigId()));
			}else if(getScene().getChallenge() == null){
				getScene().getScriptManager().callEvent(EventType.EVENT_ANY_MONSTER_DIE, new ScriptArgs().setParam1(this.getConfigId()));
156
			}
157
		}
158
159
		// Battle Pass trigger
		getScene().getPlayers().forEach(p -> p.getBattlePassManager().triggerMission(WatcherTriggerType.TRIGGER_MONSTER_DIE, this.getMonsterId(), 1));
Melledy's avatar
Melledy committed
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
	}
	
	public void recalcStats() {
		// Monster data
		MonsterData data = this.getMonsterData();

		// 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.setFightProperty(FightProperty.FIGHT_PROP_BASE_ATTACK, data.getBaseAttack());
		this.setFightProperty(FightProperty.FIGHT_PROP_BASE_DEFENSE, data.getBaseDefense());
		
		this.setFightProperty(FightProperty.FIGHT_PROP_PHYSICAL_SUB_HURT, data.getPhysicalSubHurt());
		this.setFightProperty(FightProperty.FIGHT_PROP_FIRE_SUB_HURT, .1f);
		this.setFightProperty(FightProperty.FIGHT_PROP_ELEC_SUB_HURT, data.getElecSubHurt());
		this.setFightProperty(FightProperty.FIGHT_PROP_WATER_SUB_HURT, data.getWaterSubHurt());
		this.setFightProperty(FightProperty.FIGHT_PROP_GRASS_SUB_HURT, data.getGrassSubHurt());
		this.setFightProperty(FightProperty.FIGHT_PROP_WIND_SUB_HURT, data.getWindSubHurt());
		this.setFightProperty(FightProperty.FIGHT_PROP_ROCK_SUB_HURT, .1f);
		this.setFightProperty(FightProperty.FIGHT_PROP_ICE_SUB_HURT, data.getIceSubHurt());
		
		// Level curve
187
		MonsterCurveData curve = GameData.getMonsterCurveDataMap().get(this.getLevel());
Melledy's avatar
Melledy committed
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
		if (curve != null) {
			for (PropGrowCurve growCurve : data.getPropGrowCurves()) {
				FightProperty prop = FightProperty.getPropByName(growCurve.getType());
				this.setFightProperty(prop, this.getFightProperty(prop) * curve.getMultByProp(growCurve.getGrowCurve()));
			}
		}
		
		// 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);
	}

	@Override
	public SceneEntityInfo toProto() {
		EntityAuthorityInfo authority = EntityAuthorityInfo.newBuilder()
				.setAbilityInfo(AbilitySyncStateInfo.newBuilder())
				.setRendererChangedInfo(EntityRendererChangedInfo.newBuilder())
				.setAiInfo(SceneEntityAiInfo.newBuilder().setIsAiOpen(true).setBornPos(this.getBornPos().toProto()))
				.setBornPos(this.getBornPos().toProto())
				.build();
		
		SceneEntityInfo.Builder entityInfo = SceneEntityInfo.newBuilder()
				.setEntityId(getId())
224
				.setEntityType(ProtEntityType.PROT_ENTITY_TYPE_MONSTER)
Melledy's avatar
Melledy committed
225
226
227
228
229
230
231
232
233
234
				.setMotionInfo(this.getMotionInfo())
				.addAnimatorParaList(AnimatorParameterValueInfoPair.newBuilder())
				.setEntityClientData(EntityClientData.newBuilder())
				.setEntityAuthorityInfo(authority)
				.setLifeState(this.getLifeState().getValue());
		
		for (Int2FloatMap.Entry entry : getFightProperties().int2FloatEntrySet()) {
			if (entry.getIntKey() == 0) {
				continue;
			}
235
			FightPropPair fightProp = FightPropPair.newBuilder().setPropType(entry.getIntKey()).setPropValue(entry.getFloatValue()).build();
Melledy's avatar
Melledy committed
236
237
238
239
240
241
242
243
244
245
246
			entityInfo.addFightPropList(fightProp);
		}
		
		PropPair pair = PropPair.newBuilder()
				.setType(PlayerProperty.PROP_LEVEL.getId())
				.setPropValue(ProtoHelper.newPropValue(PlayerProperty.PROP_LEVEL, getLevel()))
				.build();
		entityInfo.addPropList(pair);
		
		SceneMonsterInfo.Builder monsterInfo = SceneMonsterInfo.newBuilder()
				.setMonsterId(getMonsterId())
Melledy's avatar
Melledy committed
247
248
				.setGroupId(this.getGroupId())
				.setConfigId(this.getConfigId())
Melledy's avatar
Melledy committed
249
250
				.addAllAffixList(getMonsterData().getAffix())
				.setAuthorityPeerId(getWorld().getHostPeerId())
Melledy's avatar
Melledy committed
251
				.setPoseId(this.getPoseId())
Melledy's avatar
Melledy committed
252
				.setBlockId(3001)
253
				.setBornType(MonsterBornType.MONSTER_BORN_TYPE_DEFAULT)
Melledy's avatar
Melledy committed
254
255
256
257
258
259
260
261
262
263
264
265
266
				.setSpecialNameId(40);
		
		if (getMonsterData().getDescribeData() != null) {
			monsterInfo.setTitleId(getMonsterData().getDescribeData().getTitleID());
		}
		
		if (this.getMonsterWeaponId() > 0) {
			SceneWeaponInfo weaponInfo = SceneWeaponInfo.newBuilder()
					.setEntityId(this.weaponEntityId)
					.setGadgetId(this.getMonsterWeaponId())
					.setAbilityInfo(AbilitySyncStateInfo.newBuilder())
					.build();
			
267
			monsterInfo.addWeaponList(weaponInfo);
Melledy's avatar
Melledy committed
268
269
270
271
272
273
274
		}
		
		entityInfo.setMonster(monsterInfo);

		return entityInfo.build();
	}
}