AbilityManager.java 6.19 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
3
4
5
package emu.grasscutter.game.ability;

import com.google.protobuf.InvalidProtocolBufferException;

import emu.grasscutter.data.GameData;
Melledy's avatar
Melledy committed
6
7
import emu.grasscutter.data.binout.AbilityModifierEntry;
import emu.grasscutter.data.binout.AbilityModifier.AbilityModifierAction;
8
import emu.grasscutter.game.entity.EntityGadget;
Melledy's avatar
Melledy committed
9
import emu.grasscutter.game.entity.GameEntity;
10
import emu.grasscutter.game.entity.gadget.GadgetGatherObject;
Melledy's avatar
Melledy committed
11
import emu.grasscutter.game.player.BasePlayerManager;
Melledy's avatar
Melledy committed
12
13
14
15
16
import emu.grasscutter.game.player.Player;
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
17
import emu.grasscutter.net.proto.AbilityMixinCostStaminaOuterClass.AbilityMixinCostStamina;
Melledy's avatar
Melledy committed
18
19
20
import emu.grasscutter.net.proto.AbilityScalarValueEntryOuterClass.AbilityScalarValueEntry;
import emu.grasscutter.net.proto.ModifierActionOuterClass.ModifierAction;

Melledy's avatar
Melledy committed
21
public class AbilityManager extends BasePlayerManager {
22
    HealAbilityManager healAbilityManager;
Melledy's avatar
Melledy committed
23
24
	
	public AbilityManager(Player player) {
Melledy's avatar
Melledy committed
25
		super(player);
26
        this.healAbilityManager = new HealAbilityManager(player);
Melledy's avatar
Melledy committed
27
28
29
	}

	public void onAbilityInvoke(AbilityInvokeEntry invoke) throws Exception {
30
31
32
        healAbilityManager.healHandler(invoke);

		 //Grasscutter.getLogger().info(invoke.getArgumentType() + " (" + invoke.getArgumentTypeValue() + "): " + Utils.bytesToHex(invoke.toByteArray()));
Melledy's avatar
Melledy committed
33
		switch (invoke.getArgumentType()) {
34
			case ABILITY_INVOKE_ARGUMENT_META_OVERRIDE_PARAM:
Melledy's avatar
Melledy committed
35
36
				handleOverrideParam(invoke);
				break;
37
			case ABILITY_INVOKE_ARGUMENT_META_REINIT_OVERRIDEMAP:
Melledy's avatar
Melledy committed
38
39
				handleReinitOverrideMap(invoke);
				break;
40
			case ABILITY_INVOKE_ARGUMENT_META_MODIFIER_CHANGE:
Melledy's avatar
Melledy committed
41
42
				handleModifierChange(invoke);
				break;
43
			case ABILITY_INVOKE_ARGUMENT_MIXIN_COST_STAMINA:
Melledy's avatar
Melledy committed
44
45
				handleMixinCostStamina(invoke);
				break;
46
			case ABILITY_INVOKE_ARGUMENT_ACTION_GENERATE_ELEM_BALL:
Melledy's avatar
Melledy committed
47
48
49
50
51
				handleGenerateElemBall(invoke);
				break;
			default:
				break;
		}
52

Melledy's avatar
Melledy committed
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
	}

	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 {
82
		// Sanity checks
Melledy's avatar
Melledy committed
83
84
85
86
87
		GameEntity target = player.getScene().getEntityById(invoke.getEntityId());
		if (target == null) {
			return;
		}
		
88
89
		AbilityMetaModifierChange data = AbilityMetaModifierChange.parseFrom(invoke.getAbilityData());
		if (data == null) {
Melledy's avatar
Melledy committed
90
91
92
			return;
		}
		
93
94
95
96
97
98
99
100
101
102
103
		// Destroying rocks
		if (target instanceof EntityGadget targetGadget && targetGadget.getContent() instanceof GadgetGatherObject gatherObject) {
			if (data.getAction() == ModifierAction.REMOVED) {
				gatherObject.dropItems(this.getPlayer());
				return;
			}
        }
		
		// Sanity checks
		AbilityInvokeEntryHead head = invoke.getHead();
		if (head == null) {
Melledy's avatar
Melledy committed
104
105
106
107
108
109
110
			return;
		}
		
		GameEntity sourceEntity = player.getScene().getEntityById(data.getApplyEntityId());
		if (sourceEntity == null) {
			return;
		}
111
		
Melledy's avatar
Melledy committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
		// 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) {
127
			// Handle remove modifier
Melledy's avatar
Melledy committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
			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
	}
	
	private void invokeAction(AbilityModifierAction action, GameEntity target, GameEntity sourceEntity) {
		switch (action.type) {
			case HealHP -> {
			}
			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);
				}
			}
		}
	}
}