mirror of
https://github.com/Melledy/Nebula.git
synced 2025-12-12 12:24:35 +01:00
Implement character talents
This commit is contained in:
@@ -23,6 +23,8 @@ public class GameData {
|
||||
@Getter private static DataTable<CharacterSkillUpgradeDef> CharacterSkillUpgradeDataTable = new DataTable<>();
|
||||
@Getter private static DataTable<CharacterUpgradeDef> CharacterUpgradeDataTable = new DataTable<>();
|
||||
@Getter private static DataTable<CharItemExpDef> CharItemExpDataTable = new DataTable<>();
|
||||
@Getter private static DataTable<TalentGroupDef> TalentGroupDataTable = new DataTable<>();
|
||||
@Getter private static DataTable<TalentDef> TalentDataTable = new DataTable<>();
|
||||
|
||||
@Getter private static DataTable<DiscDef> DiscDataTable = new DataTable<>();
|
||||
@Getter private static DataTable<DiscStrengthenDef> DiscStrengthenDataTable = new DataTable<>();
|
||||
|
||||
@@ -9,12 +9,13 @@ import lombok.Getter;
|
||||
public class CharacterDef extends BaseDef {
|
||||
private int Id;
|
||||
private String Name;
|
||||
|
||||
private boolean Available;
|
||||
private int Grade;
|
||||
private int DefaultSkinId;
|
||||
private int AdvanceSkinId;
|
||||
private int AdvanceGroup;
|
||||
private int FragmentsId;
|
||||
private int TransformQty;
|
||||
private int[] SkillsUpgradeGroup;
|
||||
|
||||
@Override
|
||||
|
||||
36
src/main/java/emu/nebula/data/resources/TalentDef.java
Normal file
36
src/main/java/emu/nebula/data/resources/TalentDef.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package emu.nebula.data.resources;
|
||||
|
||||
import emu.nebula.data.BaseDef;
|
||||
import emu.nebula.data.GameData;
|
||||
import emu.nebula.data.ResourceType;
|
||||
import emu.nebula.data.ResourceType.LoadPriority;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@ResourceType(name = "Talent.json", loadPriority = LoadPriority.LOW)
|
||||
public class TalentDef extends BaseDef {
|
||||
private int Id;
|
||||
private int Index;
|
||||
private int Type;
|
||||
private int GroupId;
|
||||
private int Sort;
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return Id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoad() {
|
||||
var talentGroup = GameData.getTalentGroupDataTable().get(this.getGroupId());
|
||||
if (talentGroup == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.Type == 1) {
|
||||
talentGroup.setMainTalent(this);
|
||||
} else if (this.Type == 2) {
|
||||
talentGroup.getTalents().add(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
30
src/main/java/emu/nebula/data/resources/TalentGroupDef.java
Normal file
30
src/main/java/emu/nebula/data/resources/TalentGroupDef.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package emu.nebula.data.resources;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import emu.nebula.data.BaseDef;
|
||||
import emu.nebula.data.ResourceType;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@ResourceType(name = "TalentGroup.json")
|
||||
public class TalentGroupDef extends BaseDef {
|
||||
private int Id;
|
||||
private int CharId;
|
||||
private int PreGroup;
|
||||
|
||||
@Setter
|
||||
private transient TalentDef mainTalent;
|
||||
private transient Set<TalentDef> talents;
|
||||
|
||||
public TalentGroupDef() {
|
||||
this.talents = new TreeSet<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return Id;
|
||||
}
|
||||
}
|
||||
@@ -61,7 +61,7 @@ public final class DatabaseManager {
|
||||
|
||||
// Add our custom fastutil codecs
|
||||
var codecProvider = CodecRegistries.fromCodecs(
|
||||
new IntSetCodec(), new IntListCodec(), new Int2IntMapCodec(), new ItemParamMapCodec()
|
||||
new IntSetCodec(), new IntListCodec(), new Int2IntMapCodec(), new ItemParamMapCodec(), new BitsetCodec()
|
||||
);
|
||||
|
||||
// Set mapper options
|
||||
|
||||
46
src/main/java/emu/nebula/database/codecs/BitsetCodec.java
Normal file
46
src/main/java/emu/nebula/database/codecs/BitsetCodec.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package emu.nebula.database.codecs;
|
||||
|
||||
import org.bson.BsonReader;
|
||||
import org.bson.BsonType;
|
||||
import org.bson.BsonWriter;
|
||||
import org.bson.codecs.Codec;
|
||||
import org.bson.codecs.DecoderContext;
|
||||
import org.bson.codecs.EncoderContext;
|
||||
|
||||
import emu.nebula.util.Bitset;
|
||||
|
||||
import it.unimi.dsi.fastutil.longs.LongArrayList;
|
||||
import it.unimi.dsi.fastutil.longs.LongList;
|
||||
|
||||
/**
|
||||
* Custom mongodb codec for encoding/decoding fastutil int sets.
|
||||
*/
|
||||
public class BitsetCodec implements Codec<Bitset> {
|
||||
|
||||
@Override
|
||||
public Class<Bitset> getEncoderClass() {
|
||||
return Bitset.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(BsonWriter writer, Bitset bitset, EncoderContext encoderContext) {
|
||||
writer.writeStartArray();
|
||||
for (long value : bitset.getData()) {
|
||||
writer.writeInt64(value);
|
||||
}
|
||||
writer.writeEndArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bitset decode(BsonReader reader, DecoderContext decoderContext) {
|
||||
LongList array = new LongArrayList();
|
||||
|
||||
reader.readStartArray();
|
||||
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
|
||||
array.add(reader.readInt64());
|
||||
}
|
||||
reader.readEndArray();
|
||||
|
||||
return new Bitset(array.toLongArray());
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,18 @@
|
||||
package emu.nebula.game.character;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.bson.types.Binary;
|
||||
import org.bson.types.ObjectId;
|
||||
|
||||
import dev.morphia.annotations.Entity;
|
||||
import dev.morphia.annotations.Id;
|
||||
import dev.morphia.annotations.Indexed;
|
||||
|
||||
import dev.morphia.annotations.PreLoad;
|
||||
import emu.nebula.GameConstants;
|
||||
import emu.nebula.Nebula;
|
||||
import emu.nebula.data.GameData;
|
||||
import emu.nebula.data.resources.CharacterDef;
|
||||
import emu.nebula.data.resources.TalentGroupDef;
|
||||
import emu.nebula.database.GameDatabaseObject;
|
||||
import emu.nebula.game.inventory.ItemParamMap;
|
||||
import emu.nebula.game.player.Player;
|
||||
@@ -19,7 +22,8 @@ import emu.nebula.proto.Public.CharGemPreset;
|
||||
import emu.nebula.proto.Public.CharGemSlot;
|
||||
import emu.nebula.proto.PublicStarTower.StarTowerChar;
|
||||
import emu.nebula.proto.PublicStarTower.StarTowerCharGem;
|
||||
|
||||
import emu.nebula.util.Bitset;
|
||||
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@@ -39,7 +43,7 @@ public class Character implements GameDatabaseObject {
|
||||
private int exp;
|
||||
private int skin;
|
||||
private int[] skills;
|
||||
private byte[] talents;
|
||||
private Bitset talents;
|
||||
|
||||
private long createTime;
|
||||
|
||||
@@ -60,7 +64,7 @@ public class Character implements GameDatabaseObject {
|
||||
this.level = 1;
|
||||
this.skin = data.getDefaultSkinId();
|
||||
this.skills = new int[] {1, 1, 1, 1, 1};
|
||||
this.talents = new byte[8];
|
||||
this.talents = new Bitset();
|
||||
this.createTime = Nebula.getCurrentTime();
|
||||
}
|
||||
|
||||
@@ -228,6 +232,63 @@ public class Character implements GameDatabaseObject {
|
||||
return changes.setSuccess(true);
|
||||
}
|
||||
|
||||
public PlayerChangeInfo unlockTalent(TalentGroupDef talentGroup) {
|
||||
// Get talent item
|
||||
int talentItemId = this.getData().getFragmentsId();
|
||||
int talentItemCount = this.getPlayer().getInventory().getItemCount(talentItemId);
|
||||
int unlockCount = (int) Math.floor(talentItemCount / 6); // Max unlock count
|
||||
|
||||
// Sanity check
|
||||
if (unlockCount <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Amount of talents unlocked
|
||||
int amount = 0;
|
||||
var nodes = new IntArrayList();
|
||||
|
||||
// Unlock talents
|
||||
for (var talent : talentGroup.getTalents()) {
|
||||
// Skip unlocked talents
|
||||
if (this.getTalents().isSet(talent.getIndex())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Set bit
|
||||
this.getTalents().setBit(talent.getIndex());
|
||||
|
||||
// Add nodes
|
||||
nodes.add(talent.getId());
|
||||
amount++;
|
||||
|
||||
// Set last talent if we unlocked everything
|
||||
if (talent.getSort() == 10) {
|
||||
this.getTalents().setBit(talentGroup.getMainTalent().getIndex());
|
||||
nodes.add(talentGroup.getMainTalent().getId());
|
||||
}
|
||||
|
||||
// End
|
||||
if (amount >= unlockCount) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Skip if we didn't unlock anything
|
||||
if (nodes.size() <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Remove items
|
||||
var changes = getPlayer().getInventory().removeItem(talentItemId, amount * 6);
|
||||
changes.setExtraData(nodes);
|
||||
|
||||
// Save to database
|
||||
this.save();
|
||||
|
||||
// Success
|
||||
return changes.setSuccess(true);
|
||||
}
|
||||
|
||||
// Proto
|
||||
|
||||
public Char toProto() {
|
||||
@@ -236,7 +297,7 @@ public class Character implements GameDatabaseObject {
|
||||
.setLevel(this.getLevel())
|
||||
.setSkin(this.getSkin())
|
||||
.setAdvance(this.getAdvance())
|
||||
.setTalentNodes(this.getTalents())
|
||||
.setTalentNodes(this.getTalents().toByteArray())
|
||||
.addAllSkillLvs(this.getSkills())
|
||||
.setCreateTime(this.getCreateTime());
|
||||
|
||||
@@ -267,7 +328,7 @@ public class Character implements GameDatabaseObject {
|
||||
.setId(this.getCharId())
|
||||
.setAdvance(this.getAdvance())
|
||||
.setLevel(this.getLevel())
|
||||
.setTalentNodes(this.getTalents())
|
||||
.setTalentNodes(this.getTalents().toByteArray())
|
||||
.addAllSkillLvs(this.getSkills());
|
||||
|
||||
for (int i = 1; i <= 3; i++) {
|
||||
@@ -280,4 +341,15 @@ public class Character implements GameDatabaseObject {
|
||||
|
||||
return proto;
|
||||
}
|
||||
|
||||
// Database fix
|
||||
|
||||
@PreLoad
|
||||
public void onLoad(Document doc) {
|
||||
var talents = doc.get("talents");
|
||||
if (talents != null && talents.getClass() == Binary.class) {
|
||||
doc.remove("talents");
|
||||
this.talents = new Bitset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,8 +7,9 @@ import emu.nebula.data.GameData;
|
||||
import emu.nebula.data.resources.CharacterDef;
|
||||
import emu.nebula.data.resources.DiscDef;
|
||||
import emu.nebula.game.player.PlayerManager;
|
||||
import emu.nebula.proto.Public.HandbookInfo;
|
||||
import emu.nebula.util.Bitset;
|
||||
import emu.nebula.game.player.Player;
|
||||
import emu.nebula.game.player.PlayerHandbook;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
||||
import lombok.Getter;
|
||||
@@ -69,8 +70,8 @@ public class CharacterStorage extends PlayerManager {
|
||||
return this.getCharacters().values();
|
||||
}
|
||||
|
||||
public PlayerHandbook getCharacterHandbook() {
|
||||
var handbook = new PlayerHandbook(1);
|
||||
public HandbookInfo getCharacterHandbook() {
|
||||
var bitset = new Bitset();
|
||||
|
||||
for (var character : this.getCharacterCollection()) {
|
||||
// Get handbook
|
||||
@@ -78,9 +79,13 @@ public class CharacterStorage extends PlayerManager {
|
||||
if (data == null) continue;
|
||||
|
||||
// Set flag
|
||||
handbook.setBit(data.getIndex());
|
||||
bitset.setBit(data.getIndex());
|
||||
}
|
||||
|
||||
var handbook = HandbookInfo.newInstance()
|
||||
.setType(1)
|
||||
.setData(bitset.toByteArray());
|
||||
|
||||
return handbook;
|
||||
}
|
||||
|
||||
@@ -128,8 +133,8 @@ public class CharacterStorage extends PlayerManager {
|
||||
return this.getDiscs().values();
|
||||
}
|
||||
|
||||
public PlayerHandbook getDiscHandbook() {
|
||||
var handbook = new PlayerHandbook(2);
|
||||
public HandbookInfo getDiscHandbook() {
|
||||
var bitset = new Bitset();
|
||||
|
||||
for (var disc : this.getDiscCollection()) {
|
||||
// Get handbook
|
||||
@@ -137,9 +142,13 @@ public class CharacterStorage extends PlayerManager {
|
||||
if (data == null) continue;
|
||||
|
||||
// Set flag
|
||||
handbook.setBit(data.getIndex());
|
||||
bitset.setBit(data.getIndex());
|
||||
}
|
||||
|
||||
var handbook = HandbookInfo.newInstance()
|
||||
.setType(2)
|
||||
.setData(bitset.toByteArray());
|
||||
|
||||
return handbook;
|
||||
}
|
||||
|
||||
|
||||
@@ -41,6 +41,10 @@ public class Inventory extends PlayerManager {
|
||||
|
||||
//
|
||||
|
||||
public PlayerChangeInfo addItem(int id, int count) {
|
||||
return this.addItem(id, count, null);
|
||||
}
|
||||
|
||||
public synchronized PlayerChangeInfo addItem(int id, int count, PlayerChangeInfo changes) {
|
||||
// Changes
|
||||
if (changes == null) {
|
||||
@@ -210,6 +214,10 @@ public class Inventory extends PlayerManager {
|
||||
return changes;
|
||||
}
|
||||
|
||||
public PlayerChangeInfo removeItem(int id, int count) {
|
||||
return this.removeItem(id, count, null);
|
||||
}
|
||||
|
||||
public synchronized PlayerChangeInfo removeItem(int id, int count, PlayerChangeInfo changes) {
|
||||
if (count > 0) {
|
||||
count = -count;
|
||||
|
||||
@@ -479,8 +479,8 @@ public class Player implements GameDatabaseObject {
|
||||
this.getInstanceManager().toProto(proto);
|
||||
|
||||
// Handbook
|
||||
proto.addHandbook(this.getCharacters().getCharacterHandbook().toProto());
|
||||
proto.addHandbook(this.getCharacters().getDiscHandbook().toProto());
|
||||
proto.addHandbook(this.getCharacters().getCharacterHandbook());
|
||||
proto.addHandbook(this.getCharacters().getDiscHandbook());
|
||||
|
||||
// Extra
|
||||
proto.getMutableVampireSurvivorRecord()
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package emu.nebula.server.handlers;
|
||||
|
||||
import emu.nebula.net.NetHandler;
|
||||
import emu.nebula.net.NetMsgId;
|
||||
import emu.nebula.proto.Public.UI32;
|
||||
import emu.nebula.proto.TalentGroupUnlock.TalentGroupUnlockResp;
|
||||
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||
import emu.nebula.net.HandlerId;
|
||||
import emu.nebula.data.GameData;
|
||||
import emu.nebula.net.GameSession;
|
||||
|
||||
@HandlerId(NetMsgId.talent_group_unlock_req)
|
||||
public class HandlerTalentGroupUnlockReq extends NetHandler {
|
||||
|
||||
@Override
|
||||
public byte[] handle(GameSession session, byte[] message) throws Exception {
|
||||
// Parse request
|
||||
var req = UI32.parseFrom(message);
|
||||
|
||||
// Get talent group data
|
||||
var talentGroup = GameData.getTalentGroupDataTable().get(req.getValue());
|
||||
if (talentGroup == null) {
|
||||
return session.encodeMsg(NetMsgId.talent_group_unlock_failed_ack);
|
||||
}
|
||||
|
||||
// Get character
|
||||
var character = session.getPlayer().getCharacters().getCharacterById(talentGroup.getCharId());
|
||||
if (character == null) {
|
||||
return session.encodeMsg(NetMsgId.talent_group_unlock_failed_ack);
|
||||
}
|
||||
|
||||
// Unlock talent
|
||||
var changes = character.unlockTalent(talentGroup);
|
||||
|
||||
if (changes == null) {
|
||||
return session.encodeMsg(NetMsgId.talent_group_unlock_failed_ack);
|
||||
}
|
||||
|
||||
// Build response
|
||||
var rsp = TalentGroupUnlockResp.newInstance()
|
||||
.setChange(changes.toProto());
|
||||
|
||||
if (changes.getExtraData() != null) {
|
||||
var nodes = (IntArrayList) changes.getExtraData();
|
||||
nodes.forEach(rsp::addNodes);
|
||||
}
|
||||
|
||||
// Encode response
|
||||
return session.encodeMsg(NetMsgId.talent_group_unlock_succeed_ack, rsp);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,18 +1,31 @@
|
||||
package emu.nebula.game.player;
|
||||
package emu.nebula.util;
|
||||
|
||||
import emu.nebula.proto.Public.HandbookInfo;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class PlayerHandbook {
|
||||
private int type;
|
||||
public class Bitset {
|
||||
private long[] data;
|
||||
|
||||
public PlayerHandbook(int type) {
|
||||
this.type = type;
|
||||
public Bitset() {
|
||||
this.data = new long[1];
|
||||
}
|
||||
|
||||
public Bitset(long[] longArray) {
|
||||
this.data = longArray;
|
||||
}
|
||||
|
||||
public boolean isSet(int index) {
|
||||
int longArrayOffset = (int) Math.floor((index - 1) / 64D);
|
||||
int bytePosition = ((index - 1) % 64);
|
||||
|
||||
if (longArrayOffset >= this.data.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
long flag = 1L << bytePosition;
|
||||
return (this.data[longArrayOffset] & flag) == flag;
|
||||
}
|
||||
|
||||
public void setBit(int index) {
|
||||
int longArrayOffset = (int) Math.floor((index - 1) / 64D);
|
||||
int bytePosition = ((index - 1) % 64);
|
||||
@@ -40,14 +53,4 @@ public class PlayerHandbook {
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
// Proto
|
||||
|
||||
public HandbookInfo toProto() {
|
||||
var proto = HandbookInfo.newInstance()
|
||||
.setType(this.getType())
|
||||
.setData(this.toByteArray());
|
||||
|
||||
return proto;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user