mirror of
https://github.com/Melledy/Nebula.git
synced 2026-09-20 01:29:54 +02:00
Implement character talents
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
package emu.nebula.game.player;
|
||||
|
||||
import emu.nebula.proto.Public.HandbookInfo;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class PlayerHandbook {
|
||||
private int type;
|
||||
private long[] data;
|
||||
|
||||
public PlayerHandbook(int type) {
|
||||
this.type = type;
|
||||
this.data = new long[1];
|
||||
}
|
||||
|
||||
public void setBit(int index) {
|
||||
int longArrayOffset = (int) Math.floor((index - 1) / 64D);
|
||||
int bytePosition = ((index - 1) % 64);
|
||||
|
||||
if (longArrayOffset >= this.data.length) {
|
||||
var oldData = this.data;
|
||||
this.data = new long[longArrayOffset + 1];
|
||||
System.arraycopy(oldData, 0, this.data, 0, oldData.length);
|
||||
}
|
||||
|
||||
this.data[longArrayOffset] |= (1L << bytePosition);
|
||||
}
|
||||
|
||||
public byte[] toByteArray() {
|
||||
byte[] array = new byte[this.getData().length * 8];
|
||||
|
||||
for (int i = 0; i < this.getData().length; i++) {
|
||||
long value = this.getData()[i];
|
||||
|
||||
for (int x = 7; x >= 0; x--) {
|
||||
array[(i * 8) + x] = (byte) (value & 0xFF);
|
||||
value >>= Byte.SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
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