Compare commits

...

6 Commits

Author SHA1 Message Date
ImmuState
cb2d6da2c5 Some refactoring. 2022-05-26 03:41:19 -07:00
ImmuState
35ad284c42 Introduce a simpler way to get the original owner of an EntityClientGadget 2022-05-26 03:41:19 -07:00
logictc
24dc2c47a6 remove debug msg 2022-05-25 21:10:10 -07:00
logictc
bdbdbd53b9 change indentation to tab 2022-05-25 21:10:10 -07:00
logictc
a5ab979418 fix indentation 2022-05-25 21:10:10 -07:00
logictc
eb5ff22464 implement skill particle generation 2022-05-25 21:10:10 -07:00
5 changed files with 722 additions and 48 deletions

View File

@ -35,6 +35,8 @@ public class EntityClientGadget extends EntityBaseGadget {
private int ownerEntityId;
private int targetEntityId;
private boolean asyncLoad;
private int originalOwnerEntityId;
public EntityClientGadget(Scene scene, Player player, EvtCreateGadgetNotify notify) {
super(scene);
@ -48,6 +50,14 @@ public class EntityClientGadget extends EntityBaseGadget {
this.ownerEntityId = notify.getPropOwnerEntityId();
this.targetEntityId = notify.getTargetEntityId();
this.asyncLoad = notify.getIsAsyncLoad();
GameEntity owner = scene.getEntityById(this.ownerEntityId);
if (owner instanceof EntityClientGadget ownerGadget) {
this.originalOwnerEntityId = ownerGadget.getOriginalOwnerEntityId();
}
else {
this.originalOwnerEntityId = this.ownerEntityId;
}
}
@Override
@ -79,6 +89,10 @@ public class EntityClientGadget extends EntityBaseGadget {
return this.asyncLoad;
}
public int getOriginalOwnerEntityId() {
return this.originalOwnerEntityId;
}
@Override
public void onDeath(int killerId) {

View File

@ -31,13 +31,15 @@ import java.io.Reader;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom;
import com.google.gson.reflect.TypeToken;
import com.google.protobuf.InvalidProtocolBufferException;
public class EnergyManager {
private final Player player;
private final static Int2ObjectMap<List<EnergyDropInfo>> energyDropData = new Int2ObjectOpenHashMap<>();
private final static Int2ObjectMap<List<EnergyDropInfo>> energyDropData = new Int2ObjectOpenHashMap<>();
private final static Int2ObjectMap<List<SkillParticleGenerationInfo>> skillParticleGenerationData = new Int2ObjectOpenHashMap<>();
public EnergyManager(Player player) {
this.player = player;
@ -50,7 +52,7 @@ public class EnergyManager {
public static void initialize() {
// Read the data we need for monster energy drops.
try (Reader fileReader = new InputStreamReader(DataLoader.load("EnergyDrop.json"))) {
List<EnergyDropEntry> energyDropList = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, EnergyDropEntry.class).getType());
List<EnergyDropEntry> energyDropList = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, EnergyDropEntry.class).getType());
for (EnergyDropEntry entry : energyDropList) {
energyDropData.put(entry.getDropId(), entry.getDropList());
@ -59,39 +61,96 @@ public class EnergyManager {
Grasscutter.getLogger().info("Energy drop data successfully loaded.");
}
catch (Exception ex) {
Grasscutter.getLogger().error("Unable to load energy drop data.", ex);
Grasscutter.getLogger().error("Unable to load energy drop data.", ex);
}
// Read the data for particle generation from skills
try (Reader fileReader = new InputStreamReader(DataLoader.load("SkillParticleGeneration.json"))) {
List<SkillParticleGenerationEntry> skillParticleGenerationList = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, SkillParticleGenerationEntry.class).getType());
for (SkillParticleGenerationEntry entry : skillParticleGenerationList) {
skillParticleGenerationData.put(entry.getAvatarId(), entry.getAmountList());
}
Grasscutter.getLogger().info("Skill particle generation data successfully loaded.");
}
catch (Exception ex) {
Grasscutter.getLogger().error("Unable to load skill particle generation data data.", ex);
}
}
/**********
Particle creation for elemental skills.
**********/
private int getCastingAvatarIdForElemBall(int invokeEntityId) {
private Optional<EntityAvatar> getCastingAvatarEntityForElemBall(int invokeEntityId) {
// To determine the avatar that has cast the skill that caused the energy particle to be generated,
// we have to look at the entity that has invoked the ability. This can either be that avatar directly,
// or it can be an `EntityClientGadget`, owned (some way up the owner hierarchy) by the avatar
// we have to look at the entity that has invoked the ability. This can either be that avatar directly,
// or it can be an `EntityClientGadget`, owned (some way up the owner hierarchy) by the avatar
// that cast the skill.
int res = 0;
// Try to get the invoking entity from the scene.
GameEntity entity = player.getScene().getEntityById(invokeEntityId);
// If this entity is null, or not an `EntityClientGadget`, we assume that we are directly
// looking at the casting avatar (the null case will happen if the avatar was switched out
// between casting the skill and the particle being generated).
if (!(entity instanceof EntityClientGadget)) {
res = invokeEntityId;
// Determine the ID of the entity that originally cast this skill. If the scene entity is null,
// or not an `EntityClientGadget`, we assume that we are directly looking at the casting avatar
// (the null case will happen if the avatar was switched out between casting the skill and the
// particle being generated). If the scene entity is an `EntityClientGadget`, we need to find the
// ID of the original owner of that gadget.
int avatarEntityId =
(!(entity instanceof EntityClientGadget))
? invokeEntityId
: ((EntityClientGadget)entity).getOriginalOwnerEntityId();
// Finally, find the avatar entity in the player's team.
return player.getTeamManager().getActiveTeam()
.stream()
.filter(character -> character.getId() == avatarEntityId)
.findFirst();
}
private int getBallCountForAvatar(int avatarId) {
// We default to two particles.
int count = 2;
// If we don't have any data for this avatar, stop.
if (!skillParticleGenerationData.containsKey(avatarId)) {
Grasscutter.getLogger().warn("No particle generation data for avatarId {} found.", avatarId);
}
// If the entity is a `EntityClientGadget`, we need to "walk up" the owner hierarchy,
// until the owner is no longer a gadget. This should then be the ID of the casting avatar.
else {
while (entity instanceof EntityClientGadget gadget) {
res = gadget.getOwnerEntityId();
entity = player.getScene().getEntityById(gadget.getOwnerEntityId());
// If we do have data, roll for how many particles we should generate.
else {
int roll = ThreadLocalRandom.current().nextInt(0, 100);
int percentageStack = 0;
for (SkillParticleGenerationInfo info : skillParticleGenerationData.get(avatarId)) {
int chance = info.getChance();
percentageStack += chance;
if (roll < percentageStack) {
count = info.getValue();
break;
}
}
}
return res;
// Done.
return count;
}
private int getBallIdForElement(ElementType element) {
// If we have no element, we default to an elementless particle.
if (element == null) {
return 2024;
}
// Otherwise, we determin the particle's ID based on the element.
return switch (element) {
case Fire -> 2017;
case Water -> 2018;
case Grass -> 2019;
case Electric -> 2020;
case Wind -> 2021;
case Ice -> 2022;
case Rock -> 2023;
default -> 2024;
};
}
public void handleGenerateElemBall(AbilityInvokeEntry invoke) throws InvalidProtocolBufferException {
@ -105,49 +164,40 @@ public class EnergyManager {
return;
}
// Determine the element of the energy particle that we have to generate.
// In case we can't, we default to an elementless particle.
// The element is the element of the avatar that has cast the ability.
// We can get that from the avatar's skill depot.
// Default to an elementless particle.
int itemId = 2024;
// Try to fetch the avatar from the player's party and determine their element.
// ToDo: Does this work in co-op?
int avatarId = getCastingAvatarIdForElemBall(invoke.getEntityId());
Optional<EntityAvatar> avatarEntity = player.getTeamManager().getActiveTeam()
.stream()
.filter(character -> character.getId() == avatarId)
.findFirst();
// Generate 2 particles by default.
int amount = 2;
// Try to get the casting avatar from the player's party.
Optional<EntityAvatar> avatarEntity = getCastingAvatarEntityForElemBall(invoke.getEntityId());
// Bug: invokes twice sometimes, Ayato, Keqing
// ToDo: deal with press, hold difference. deal with charge(Beidou, Yunjin)
if (avatarEntity.isPresent()) {
Avatar avatar = avatarEntity.get().getAvatar();
if (avatar != null) {
int avatarId = avatar.getAvatarId();
AvatarSkillDepotData skillDepotData = avatar.getSkillDepot();
// Determine how many particles we need to create for this avatar.
amount = this.getBallCountForAvatar(avatarId);
// Determine the avatar's element, and based on that the ID of the
// particles we have to generate.
if (skillDepotData != null) {
ElementType element = skillDepotData.getElementType();
// If we found the element, we use it to deterine the ID of the
// energy particle that we have to generate.
if (element != null) {
itemId = switch (element) {
case Fire -> 2017;
case Water -> 2018;
case Grass -> 2019;
case Electric -> 2020;
case Wind -> 2021;
case Ice -> 2022;
case Rock -> 2023;
default -> 2024;
};
}
itemId = getBallIdForElement(element);
}
}
}
// Generate the particle/orb.
generateElemBall(itemId, new Position(action.getPos()), 1);
// Generate the particles.
for (int i = 0; i < amount; i++) {
generateElemBall(itemId, new Position(action.getPos()), 1);
}
}
/**********

View File

@ -0,0 +1,16 @@
package emu.grasscutter.game.managers.EnergyManager;
import java.util.List;
public class SkillParticleGenerationEntry {
private int avatarId;
private List<SkillParticleGenerationInfo> amountList;
public int getAvatarId() {
return this.avatarId;
}
public List<SkillParticleGenerationInfo> getAmountList() {
return this.amountList;
}
}

View File

@ -0,0 +1,14 @@
package emu.grasscutter.game.managers.EnergyManager;
public class SkillParticleGenerationInfo {
private int value;
private int chance;
public int getValue() {
return this.value;
}
public int getChance() {
return this.chance;
}
}

View File

@ -0,0 +1,580 @@
[
{
"avatarId": 10000002,
"name": "Kamisato Ayaka",
"amountList": [
{
"value": 4,
"chance": 50
},
{
"value": 5,
"chance": 50
}
]
},
{
"avatarId": 10000003,
"name": "Jean",
"amountList": [
{
"value": 2,
"chance": 33
},
{
"value": 3,
"chance": 67
}
]
},
{
"avatarId": 10000005,
"name": "Traveler",
"amountList": [
{
"value": 3,
"chance": 67
},
{
"value": 4,
"chance": 33
}
]
},
{
"avatarId": 10000006,
"name": "Lisa",
"amountList": [
{
"value": 5,
"chance": 100
}
]
},
{
"avatarId": 10000007,
"name": "Traveler",
"amountList": [
{
"value": 3,
"chance": 67
},
{
"value": 4,
"chance": 33
}
]
},
{
"avatarId": 10000014,
"name": "Barbara",
"amountList": [
{
"value": 0,
"chance": 100
}
]
},
{
"avatarId": 10000015,
"name": "Kaeya",
"amountList": [
{
"value": 2,
"chance": 33
},
{
"value": 3,
"chance": 67
}
]
},
{
"avatarId": 10000016,
"name": "Diluc",
"amountList": [
{
"value": 1,
"chance": 33
},
{
"value": 2,
"chance": 67
}
]
},
{
"avatarId": 10000020,
"name": "Razor",
"amountList": [
{
"value": 3,
"chance": 100
}
]
},
{
"avatarId": 10000021,
"name": "Amber",
"amountList": [
{
"value": 4,
"chance": 100
}
]
},
{
"avatarId": 10000022,
"name": "Venti",
"amountList": [
{
"value": 3,
"chance": 100
}
]
},
{
"avatarId": 10000023,
"name": "Xiangling",
"amountList": [
{
"value": 1,
"chance": 100
}
]
},
{
"avatarId": 10000024,
"name": "Beidou",
"amountList": [
{
"value": 2,
"chance": 100
}
]
},
{
"avatarId": 10000025,
"name": "Xingqiu",
"amountList": [
{
"value": 5,
"chance": 100
}
]
},
{
"avatarId": 10000026,
"name": "Xiao",
"amountList": [
{
"value": 3,
"chance": 100
}
]
},
{
"avatarId": 10000027,
"name": "Ningguang",
"amountList": [
{
"value": 3,
"chance": 33
},
{
"value": 4,
"chance": 67
}
]
},
{
"avatarId": 10000029,
"name": "Klee",
"amountList": [
{
"value": 4,
"chance": 100
}
]
},
{
"avatarId": 10000030,
"name": "Zhongli",
"amountList": [
{
"value": 0,
"chance": 50
},
{
"value": 1,
"chance": 50
}
]
},
{
"avatarId": 10000031,
"name": "Fischl",
"amountList": [
{
"value": 0,
"chance": 33
},
{
"value": 1,
"chance": 67
}
]
},
{
"avatarId": 10000032,
"name": "Bennett",
"amountList": [
{
"value": 2,
"chance": 75
},
{
"value": 3,
"chance": 25
}
]
},
{
"avatarId": 10000033,
"name": "Tartaglia",
"amountList": [
{
"value": 1,
"chance": 100
}
]
},
{
"avatarId": 10000034,
"name": "Noelle",
"amountList": [
{
"value": 0,
"chance": 100
}
]
},
{
"avatarId": 10000035,
"name": "Qiqi",
"amountList": [
{
"value": 0,
"chance": 100
}
]
},
{
"avatarId": 10000036,
"name": "Chongyun",
"amountList": [
{
"value": 4,
"chance": 100
}
]
},
{
"avatarId": 10000037,
"name": "Ganyu",
"amountList": [
{
"value": 2,
"chance": 100
}
]
},
{
"avatarId": 10000038,
"name": "Albedo",
"amountList": [
{
"value": 0,
"chance": 33
},
{
"value": 1,
"chance": 67
}
]
},
{
"avatarId": 10000039,
"name": "Diona",
"amountList": [
{
"value": 0,
"chance": 20
},
{
"value": 1,
"chance": 80
}
]
},
{
"avatarId": 10000041,
"name": "Mona",
"amountList": [
{
"value": 3,
"chance": 67
},
{
"value": 4,
"chance": 33
}
]
},
{
"avatarId": 10000042,
"name": "Keqing",
"amountList": [
{
"value": 2,
"chance": 50
},
{
"value": 3,
"chance": 50
}
]
},
{
"avatarId": 10000043,
"name": "Sucrose",
"amountList": [
{
"value": 4,
"chance": 100
}
]
},
{
"avatarId": 10000044,
"name": "Xinyan",
"amountList": [
{
"value": 4,
"chance": 100
}
]
},
{
"avatarId": 10000045,
"name": "Rosaria",
"amountList": [
{
"value": 3,
"chance": 100
}
]
},
{
"avatarId": 10000046,
"name": "Hu Tao",
"amountList": [
{
"value": 2,
"chance": 50
},
{
"value": 3,
"chance": 50
}
]
},
{
"avatarId": 10000047,
"name": "Kaedehara Kazuha",
"amountList": [
{
"value": 3,
"chance": 50
},
{
"value": 4,
"chance": 50
}
]
},
{
"avatarId": 10000048,
"name": "Yanfei",
"amountList": [
{
"value": 3,
"chance": 100
}
]
},
{
"avatarId": 10000049,
"name": "Yoimiya",
"amountList": [
{
"value": 1,
"chance": 100
}
]
},
{
"avatarId": 10000050,
"name": "Thoma",
"amountList": [
{
"value": 3,
"chance": 50
},
{
"value": 4,
"chance": 50
}
]
},
{
"avatarId": 10000051,
"name": "Eula",
"amountList": [
{
"value": 1,
"chance": 50
},
{
"value": 2,
"chance": 50
}
]
},
{
"avatarId": 10000052,
"name": "Raiden Shogun",
"amountList": [
{
"value": 0,
"chance": 50
},
{
"value": 1,
"chance": 50
}
]
},
{
"avatarId": 10000053,
"name": "Sayu",
"amountList": [
{
"value": 2,
"chance": 100
}
]
},
{
"avatarId": 10000054,
"name": "Sangonomiya Kokomi",
"amountList": [
{
"value": 0,
"chance": 33
},
{
"value": 1,
"chance": 67
}
]
},
{
"avatarId": 10000055,
"name": "Gorou",
"amountList": [
{
"value": 2,
"chance": 100
}
]
},
{
"avatarId": 10000056,
"name": "Kujou Sara",
"amountList": [
{
"value": 3,
"chance": 100
}
]
},
{
"avatarId": 10000057,
"name": "Arataki Itto",
"amountList": [
{
"value": 3,
"chance": 50
},
{
"value": 4,
"chance": 50
}
]
},
{
"avatarId": 10000058,
"name": "Yae Miko",
"amountList": [
{
"value": 1,
"chance": 100
}
]
},
{
"avatarId": 10000062,
"name": "Aloy",
"amountList": [
{
"value": 5,
"chance": 100
}
]
},
{
"avatarId": 10000063,
"name": "Shenhe",
"amountList": [
{
"value": 3,
"chance": 100
}
]
},
{
"avatarId": 10000064,
"name": "Yun Jin",
"amountList": [
{
"value": 2,
"chance": 100
}
]
},
{
"avatarId": 10000066,
"name": "Kamisato Ayato",
"amountList": [
{
"value": 1,
"chance": 50
},
{
"value": 2,
"chance": 50
}
]
}
]