AbilityManager.java 6.49 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
package emu.grasscutter.game.ability;

3
4
import java.util.Optional;

Melledy's avatar
Melledy committed
5
6
import com.google.protobuf.InvalidProtocolBufferException;

7
8
import emu.grasscutter.Grasscutter;

Melledy's avatar
Melledy committed
9
import emu.grasscutter.data.GameData;
Melledy's avatar
Melledy committed
10
11
12
13
import emu.grasscutter.data.binout.AbilityModifierEntry;
import emu.grasscutter.data.binout.AbilityModifier.AbilityModifierAction;
import emu.grasscutter.data.excels.AvatarSkillDepotData;
import emu.grasscutter.data.excels.ItemData;
14
15
16
import emu.grasscutter.game.avatar.Avatar;
import emu.grasscutter.game.entity.EntityAvatar;
import emu.grasscutter.game.entity.EntityClientGadget;
Melledy's avatar
Melledy committed
17
import emu.grasscutter.game.entity.EntityItem;
Melledy's avatar
Melledy committed
18
19
import emu.grasscutter.game.entity.GameEntity;
import emu.grasscutter.game.player.Player;
20
import emu.grasscutter.game.props.ElementType;
Melledy's avatar
Melledy committed
21
import emu.grasscutter.net.proto.AbilityActionGenerateElemBallOuterClass.AbilityActionGenerateElemBall;
Melledy's avatar
Melledy committed
22
23
24
25
import emu.grasscutter.net.proto.AbilityInvokeEntryHeadOuterClass.AbilityInvokeEntryHead;
import emu.grasscutter.net.proto.AbilityInvokeEntryOuterClass.AbilityInvokeEntry;
import emu.grasscutter.net.proto.AbilityMetaModifierChangeOuterClass.AbilityMetaModifierChange;
import emu.grasscutter.net.proto.AbilityMetaReInitOverrideMapOuterClass.AbilityMetaReInitOverrideMap;
gentlespoon's avatar
gentlespoon committed
26
import emu.grasscutter.net.proto.AbilityMixinCostStaminaOuterClass.AbilityMixinCostStamina;
Melledy's avatar
Melledy committed
27
28
import emu.grasscutter.net.proto.AbilityScalarValueEntryOuterClass.AbilityScalarValueEntry;
import emu.grasscutter.net.proto.ModifierActionOuterClass.ModifierAction;
Melledy's avatar
Melledy committed
29
import emu.grasscutter.utils.Position;
30
import emu.grasscutter.utils.Utils;
Melledy's avatar
Melledy committed
31
32
33
34
35
36
37
38
39
40
41
42
43

public class AbilityManager {
	private Player player;
	
	public AbilityManager(Player player) {
		this.player = player;
	}
	
	public Player getPlayer() {
		return this.player;
	}

	public void onAbilityInvoke(AbilityInvokeEntry invoke) throws Exception {
44
		// Grasscutter.getLogger().info(invoke.getArgumentType() + " (" + invoke.getArgumentTypeValue() + "): " + Utils.bytesToHex(invoke.toByteArray()));
Melledy's avatar
Melledy committed
45
		switch (invoke.getArgumentType()) {
46
			case ABILITY_INVOKE_ARGUMENT_META_OVERRIDE_PARAM:
Melledy's avatar
Melledy committed
47
48
				handleOverrideParam(invoke);
				break;
49
			case ABILITY_INVOKE_ARGUMENT_META_REINIT_OVERRIDEMAP:
Melledy's avatar
Melledy committed
50
51
				handleReinitOverrideMap(invoke);
				break;
52
			case ABILITY_INVOKE_ARGUMENT_META_MODIFIER_CHANGE:
Melledy's avatar
Melledy committed
53
54
				handleModifierChange(invoke);
				break;
55
			case ABILITY_INVOKE_ARGUMENT_MIXIN_COST_STAMINA:
Melledy's avatar
Melledy committed
56
57
				handleMixinCostStamina(invoke);
				break;
58
			case ABILITY_INVOKE_ARGUMENT_ACTION_GENERATE_ELEM_BALL:
Melledy's avatar
Melledy committed
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
				handleGenerateElemBall(invoke);
				break;
			default:
				break;
		}
	}

	private void handleOverrideParam(AbilityInvokeEntry invoke) throws Exception {
		GameEntity entity = player.getScene().getEntityById(invoke.getEntityId());
		
		if (entity == null) {
			return;
		}
		
		AbilityScalarValueEntry entry = AbilityScalarValueEntry.parseFrom(invoke.getAbilityData());
		
		entity.getMetaOverrideMap().put(entry.getKey().getStr(), entry.getFloatValue());
	}

	private void handleReinitOverrideMap(AbilityInvokeEntry invoke) throws Exception {
		GameEntity entity = player.getScene().getEntityById(invoke.getEntityId());
		
		if (entity == null) {
			return;
		}
		
		AbilityMetaReInitOverrideMap map = AbilityMetaReInitOverrideMap.parseFrom(invoke.getAbilityData());
		
		for (AbilityScalarValueEntry entry : map.getOverrideMapList()) {
			entity.getMetaOverrideMap().put(entry.getKey().getStr(), entry.getFloatValue());
		}
	}
	
	private void handleModifierChange(AbilityInvokeEntry invoke) throws Exception {
		GameEntity target = player.getScene().getEntityById(invoke.getEntityId());
		if (target == null) {
			return;
		}
		
		AbilityInvokeEntryHead head = invoke.getHead();
		if (head == null) {
			return;
		}
		
		AbilityMetaModifierChange data = AbilityMetaModifierChange.parseFrom(invoke.getAbilityData());
		if (data == null) {
			return;
		}
		
		GameEntity sourceEntity = player.getScene().getEntityById(data.getApplyEntityId());
		if (sourceEntity == null) {
			return;
		}
		
		// This is not how it works but we will keep it for now since healing abilities dont work properly anyways
		if (data.getAction() == ModifierAction.ADDED && data.getParentAbilityName() != null) {
			// Handle add modifier here
			String modifierString = data.getParentAbilityName().getStr();
			AbilityModifierEntry modifier = GameData.getAbilityModifiers().get(modifierString);
			
			if (modifier != null && modifier.getOnAdded().size() > 0) {
				for (AbilityModifierAction action : modifier.getOnAdded()) {
					invokeAction(action, target, sourceEntity);
				}
			}
			
			// Add to meta modifier list
			target.getMetaModifiers().put(head.getInstancedModifierId(), modifierString);
		} else if (data.getAction() == ModifierAction.REMOVED) {
			String modifierString = target.getMetaModifiers().get(head.getInstancedModifierId());
			
			if (modifierString != null) {
				// Get modifier and call on remove event
				AbilityModifierEntry modifier = GameData.getAbilityModifiers().get(modifierString);
				
				if (modifier != null && modifier.getOnRemoved().size() > 0) {
					for (AbilityModifierAction action : modifier.getOnRemoved()) {
						invokeAction(action, target, sourceEntity);
					}
				}
				
				// Remove from meta modifiers
				target.getMetaModifiers().remove(head.getInstancedModifierId());
			}
		}
	}
	
gentlespoon's avatar
gentlespoon committed
146
147
148
	private void handleMixinCostStamina(AbilityInvokeEntry invoke) throws InvalidProtocolBufferException {
		AbilityMixinCostStamina costStamina = AbilityMixinCostStamina.parseFrom((invoke.getAbilityData()));
		getPlayer().getStaminaManager().handleMixinCostStamina(costStamina.getIsSwim());
Melledy's avatar
Melledy committed
149
	}
150

Melledy's avatar
Melledy committed
151
	private void handleGenerateElemBall(AbilityInvokeEntry invoke) throws InvalidProtocolBufferException {
152
		this.player.getEnergyManager().handleGenerateElemBall(invoke);
Melledy's avatar
Melledy committed
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
	}
	
	private void invokeAction(AbilityModifierAction action, GameEntity target, GameEntity sourceEntity) {
		switch (action.type) {
			case HealHP -> {
				if (action.amount == null) {
					return;
				}
				
				float healAmount = 0;
				
				if (action.amount.isDynamic && action.amount.dynamicKey != null) {
					healAmount = sourceEntity.getMetaOverrideMap().getOrDefault(action.amount.dynamicKey, 0f);
				}
				
				if (healAmount > 0) {
					target.heal(healAmount);
				}
			}
			case LoseHP -> {
				if (action.amountByTargetCurrentHPRatio == null) {
					return;
				}
				
				float damageAmount = 0;
				
				if (action.amount.isDynamic && action.amount.dynamicKey != null) {
					damageAmount = sourceEntity.getMetaOverrideMap().getOrDefault(action.amount.dynamicKey, 0f);
				}
				
				if (damageAmount > 0) {
					target.damage(damageAmount);
				}
			}
		}
	}
}